1 /* Common definitions for remote server for GDB.
2 Copyright (C) 1993, 1995, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
7 It has been modified to integrate it in valgrind
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
27 #include "pub_core_basics.h"
28 #include "pub_core_vki.h"
29 #include "pub_core_xarray.h"
30 #include "pub_core_clientstate.h"
31 #include "pub_core_debuglog.h"
32 #include "pub_core_errormgr.h"
33 #include "pub_core_libcassert.h"
34 #include "pub_core_libcfile.h"
35 #include "pub_core_libcprint.h"
36 #include "pub_core_mallocfree.h"
37 #include "pub_core_syscall.h"
38 #include "pub_core_libcproc.h"
39 #include "pub_core_tooliface.h"
40 #include "pub_core_libcassert.h"
41 #include "pub_core_libcbase.h"
42 #include "pub_core_options.h"
43 #include "pub_core_threadstate.h"
44 #include "pub_core_gdbserver.h"
45 #include "pub_core_vki.h"
46 #include "pub_core_clreq.h"
48 /*------------- interface m_gdbserver <=> low level gdbserver */
50 /* Initializes gdbserver. After a call to gdbserver_init, vgdb
51 can contact the gdbserver embedded in valgrind.
52 The rest of the low level gdbserver interface can only
54 extern void gdbserver_init (void);
56 extern void server_main (void);
58 /* To be called to indicate that gdbserver usage is finished.
59 Resources (e.g. FIFOs) will be destroyed. */
60 extern void gdbserver_terminate (void);
63 /* Output string s to the gdb debugging this process or to vgdb.
64 Do not call this directly. Rather use VG_(gdb_printf)
65 to output something to gdb, use normal valgrind messaging
66 (e.g. VG_(umsg)) to send output that can either go
68 extern void monitor_output (char *s
);
70 /* returns 0 if there is no connection or no event on the connection
72 returns 1 if there are some data which has been received from gdb
73 and that must (still) be handled.
74 returns 2 if remote_desc_activity detected the connection has been
75 lost and should be reopened.
76 msg is used for debug logging.*/
77 extern int remote_desc_activity(const char *msg
);
79 /* output some status of gdbserver communication */
80 extern void remote_utils_output_status(void);
82 /* True if there is a connection with gdb. */
83 extern Bool
remote_connected(void);
85 /* Finish the connection with gdb and reset_valgrind_sink.
86 Keeps the FIFOs and shared mem so as to allow connection
88 extern void remote_finish(FinishReason reason
);
90 /* If Valgrind sink was changed by gdbserver:
91 Resets the valgrind sink to before the changes done by gdbserver,
92 and does VG_(umsg). If info != NULL, info added in VG_(usmg). */
93 extern void reset_valgrind_sink(const char* info
);
95 // VG_(gdb_printf) by default writes to vgdb/gdb.
96 // If there is no connection, it will rather write to the initial (log)
97 // valgrind fd using the below.
98 extern void print_to_initial_valgrind_sink (const char *msg
);
101 Guesses if pc is a thumb pc.
102 In this case, returns pc with the thumb bit set (bit0)
103 else just returns pc.
105 The guess is based on the following set of check:
107 else if bit1 set => thumb
108 else uses the debuginfo to guess.
110 If debug info not found for this pc, assumes arm */
111 extern Addr
thumb_pc (Addr pc
);
113 /* when invoked by vgdb using ptrace, contains the tid chosen
114 by vgdb (if vgdb gives a tid different of 0: a 0 tid by
115 vgdb means use the running_tid if there is one running
116 or tid 1 otherwise). */
117 extern ThreadId vgdb_interrupted_tid
;
119 /* True if GDB is catching client syscalls. */
120 extern Bool catching_syscalls
;
122 /* Size of the syscalls_to_catch. Only useful if catching_syscalls True.
123 syscalls_to_catch_size 0 means all syscalls are caught. */
124 extern Int syscalls_to_catch_size
;
125 extern Int
*syscalls_to_catch
;
127 /*------------ end of interface to low level gdbserver */
130 #define dlog(level, ...) \
131 do { if (UNLIKELY(VG_(debugLog_getLevel)() >= level)) \
132 VG_(debugLog) (level, "gdbsrv",__VA_ARGS__); } \
136 /* vki only defines VKI_POLLIN but even not on all OS.
137 Below is from linux bits/poll.h */
139 #define VKI_POLLIN 0x0001
141 #define VKI_POLLPRI 0x0002
142 #define VKI_POLLOUT 0x0004
143 #define VKI_POLLERR 0x0008
144 #define VKI_POLLHUP 0x0010
145 #define VKI_POLLNVAL 0x0020
147 /* a bunch of macros to avoid libc usage in valgrind-ified gdbserver */
148 #define strcmp(s1,s2) VG_(strcmp) ((s1),(s2))
149 #define strncmp(s1,s2,nmax) VG_(strncmp) ((s1),(s2),nmax)
150 #define strcat(s1,s2) VG_(strcat) ((s1),(s2))
151 #define strcpy(s1,s2) VG_(strcpy) ((s1),(s2))
152 #define strncpy(s1,s2,nmax) VG_(strncpy) ((s1),(s2),nmax)
153 #define strlen(s) VG_(strlen) ((s))
154 #define strtok(p,s) VG_(strtok) ((p),(s))
155 #define strtok_r(p,s,ss) VG_(strtok_r) ((p),(s),(ss))
156 #define strchr(s,c) VG_(strchr) ((s),c)
157 /* strtol and strtoul supports base 16 or else assumes it is base 10 */
158 #define strtol(s,r,b) ((b) == 16 ? \
159 VG_(strtoll16) ((s),(r)) \
160 : VG_(strtoll10) ((s),(r)))
161 #define strtoul(s,r,b) ((b) == 16 ? \
162 VG_(strtoull16) ((s),(r)) \
163 : VG_(strtoull10) ((s),(r)))
165 #define memcpy(_dd,_ss,_sz) VG_(memcpy)((_dd),(_ss),(_sz))
166 #define memset(_ss,_cc,_sz) VG_(memset)((_ss),(_cc),(_sz))
168 #define malloc(sz) VG_(malloc) ("gdbsrv", sz)
169 #define calloc(n,sz) VG_(calloc) ("gdbsrv", n, sz)
170 #define realloc(p,size) VG_(realloc) ("gdbsrv", p, size)
171 #define strdup(s) VG_(strdup) ("gdbsrv", (s))
172 #define free(b) VG_(free) (b)
174 #ifndef ATTR_NORETURN
175 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
176 #define ATTR_NORETURN __attribute__ ((noreturn))
178 #define ATTR_NORETURN /* nothing */
183 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 4))
184 #define ATTR_FORMAT(type, x, y) __attribute__ ((format(type, x, y)))
186 #define ATTR_FORMAT(type, x, y) /* nothing */
190 /* A type used for binary buffers. */
191 typedef unsigned char gdb_byte
;
193 typedef Addr CORE_ADDR
;
195 /* Generic information for tracking a list of ``inferiors'' - threads,
199 struct inferior_list_entry
*head
;
200 struct inferior_list_entry
*tail
;
202 struct inferior_list_entry
205 struct inferior_list_entry
*next
;
208 /* Opaque type for user-visible threads. */
211 #include "regcache.h"
212 #include "gdb/signals.h"
214 /* signal handling with gdbserver: before delivering a signal,
215 call gdbserver_signal_encountered. This will set
216 the signal to report in the next resume reply sent to GDB.
217 A call to call_gdbserver is needed to send the resume reply to GDB.
218 After this call, gdbserver_deliver_signal indicates if the signal
219 is effectively to be delivered to the guest process. */
220 extern void gdbserver_signal_encountered (const vki_siginfo_t
*info
);
221 /* between these two calls, call call_gdbserver.
222 Between these 2 calls the signal to report to GDB can be retrieved using
223 gdbserver_pending_signal_to_report. */
224 /* If gdbserver_deliver_signal True, then gdb did not ask
225 to ignore the signal, so signal can be delivered to the guest. */
226 extern Bool
gdbserver_deliver_signal (vki_siginfo_t
*info
);
228 /* Signal info last provided with gdbserver_signal_encountered.
229 It is what is/will be reported to GDB. */
230 extern void gdbserver_pending_signal_to_report (vki_siginfo_t
/* OUT */ *info
);
232 /* Called when a process is about to go with reason ('W' or 'X') and code.
233 This sets global variables that will be used to return the process
234 exit status to GDB in the next resume_reply.
235 Similarly to gdbserver_signal_encountered, a call to call_gdbserver
236 is needed to send the resume reply. */
237 extern void gdbserver_process_exit_encountered (unsigned char status
, Int code
);
239 /* To optimise signal handling, gdb can instruct gdbserver to
240 not stop on some signals. In the below, a 1 indicates the gdb_nr signal
241 has to be passed directly to the guest, without asking gdb.
242 A 0 indicates gdb has to be consulted to see if signal has
243 or has not to be passed. The gdb consultation is to
244 be done using the above two functions. */
245 extern int pass_signals
[]; /* indexed by gdb signal nr */
250 /* Target-specific functions */
252 /* From inferiors.c. */
254 extern struct inferior_list all_threads
;
255 void add_inferior_to_list (struct inferior_list
*list
,
256 struct inferior_list_entry
*new_inferior
);
257 void for_each_inferior (struct inferior_list
*list
,
258 void (*action
) (struct inferior_list_entry
*));
259 extern struct thread_info
*current_inferior
;
260 void remove_inferior (struct inferior_list
*list
,
261 struct inferior_list_entry
*entry
);
262 void remove_thread (struct thread_info
*thread
);
263 void add_thread (unsigned long thread_id
, void *target_data
, unsigned int);
264 unsigned int thread_id_to_gdb_id (unsigned long);
265 unsigned int thread_to_gdb_id (struct thread_info
*);
266 unsigned long gdb_id_to_thread_id (unsigned int);
267 struct thread_info
*gdb_id_to_thread (unsigned int);
268 void clear_inferiors (void);
269 struct inferior_list_entry
*find_inferior (struct inferior_list
*,
271 inferior_list_entry
*,
274 struct inferior_list_entry
*find_inferior_id (struct inferior_list
*list
,
276 void *inferior_target_data (struct thread_info
*);
277 void set_inferior_target_data (struct thread_info
*, void *);
278 void *inferior_regcache_data (struct thread_info
*);
279 void set_inferior_regcache_data (struct thread_info
*, void *);
280 void change_inferior_id (struct inferior_list
*list
,
281 unsigned long new_id
);
283 /* Public variables in server.c */
285 extern unsigned long cont_thread
;
286 extern unsigned long general_thread
;
287 extern unsigned long step_thread
;
288 extern unsigned long thread_from_wait
;
289 extern unsigned long old_thread_from_wait
;
291 extern VG_MINIMAL_JMP_BUF(toplevel
);
293 /* From remote-utils.c */
295 extern Bool noack_mode
;
296 int putpkt (char *buf
);
297 int putpkt_binary (char *buf
, int len
);
298 int getpkt (char *buf
);
299 void remote_open (const HChar
*name
);
300 void remote_close (void);
302 void sync_gdb_connection (void);
303 void write_ok (char *buf
);
304 void write_enn (char *buf
);
305 void convert_ascii_to_int (const char *from
, unsigned char *to
, int n
);
306 void convert_int_to_ascii (const unsigned char *from
, char *to
, int n
);
307 void prepare_resume_reply (char *buf
, char status
, unsigned char sig
);
309 void decode_address (CORE_ADDR
*addrp
, const char *start
, int len
);
310 void decode_m_packet (char *from
, CORE_ADDR
* mem_addr_ptr
,
311 unsigned int *len_ptr
);
312 void decode_M_packet (char *from
, CORE_ADDR
* mem_addr_ptr
,
313 unsigned int *len_ptr
, unsigned char *to
);
314 int decode_X_packet (char *from
, int packet_len
, CORE_ADDR
* mem_addr_ptr
,
315 unsigned int *len_ptr
, unsigned char *to
);
317 int unhexify (char *bin
, const char *hex
, int count
);
318 /* heximage builds an image of bin according to byte order of the architecture
319 Useful for register and int image */
320 char* heximage (char *buf
, const char *bin
, int count
);
322 /* convert from CORE_ADDR to void* */
323 void* C2v(CORE_ADDR addr
);
326 int remote_escape_output (const gdb_byte
*buffer
, int len
,
327 gdb_byte
*out_buf
, int *out_len
,
330 /* Functions from ``signals.c''. */
331 enum target_signal
target_signal_from_host (int hostsig
);
332 int target_signal_to_host_p (enum target_signal oursig
);
333 int target_signal_to_host (enum target_signal oursig
);
334 const char *target_signal_to_name (enum target_signal
);
336 /* Functions from utils.c */
338 /* error is like VG_(umsg), then VG_MINIMAL_LONGJMP to gdbserver toplevel. */
339 void error (const char *string
,...) ATTR_NORETURN
ATTR_FORMAT (printf
, 1, 2);
340 /* first output a description of the error inside sr, then like VG_(umsg). */
341 void sr_perror (SysRes sr
,const char *string
,...) ATTR_FORMAT (printf
, 2, 3);
342 /* fatal is like VG_(umsg), then exit(1). */
343 void fatal (const char *string
,...) ATTR_NORETURN
ATTR_FORMAT (printf
, 1, 2);
344 /* warning is like VG_(umsg). */
345 void warning (const char *string
,...) ATTR_FORMAT (printf
, 1, 2);
347 /* Functions from the register cache definition. */
349 void init_registers (void);
351 /* Maximum number of bytes to read/write at once. The value here
352 is chosen to fill up a packet (the headers account for the 32). */
353 #define MAXBUFBYTES(N) (((N)-32)/2)
355 /* PBUFSIZ : Buffers size for transferring memory, registers, etc.
356 Must be big enough to hold all the registers, at least.
357 Must be at least big as 2*DATASIZ + 5:
358 1 : packet begin ($ or %)
359 + 2*DATASIZ : encoded string
361 + 2 : packet checksum
364 Max value gdb likes is 16384.
366 Note that what is sent/received to/from gdb does
367 not have a trailing null byte. We are adding 1 here to allow
368 null terminating the strings e.g. for printf.
370 => packet Packet OVERHead SIZe is 5:*/
372 /* keep PBUFSIZ value in sync with vgdb.c */
373 #define PBUFSIZ 16384
376 /* Max size of a string encoded in a packet. Hex Encoding can
377 multiply the size by 2 (trailing null byte not sent). */
378 #define DATASIZ ((PBUFSIZ-POVERHSIZ)/2)
380 /* Version information, from version.c. */
381 extern const char version
[];
383 /* Shared remote utils functions with vgdb. */
384 #include "remote-utils-shared.h"
386 #endif /* SERVER_H */