• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Common definitions for remote server for GDB.
2    Copyright (C) 1993, 1995, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005,
3    2006, 2012
4    Free Software Foundation, Inc.
5 
6    This file is part of GDB.
7    It has been modified to integrate it in valgrind
8 
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.
13 
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.
18 
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.  */
23 
24 #ifndef SERVER_H
25 #define SERVER_H
26 
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_libcsetjmp.h"
44 #include "pub_core_threadstate.h"
45 #include "pub_core_gdbserver.h"
46 #include "pub_core_aspacemgr.h"
47 #include "pub_core_vki.h"
48 #include "pub_core_clreq.h"
49 
50 /*------------- interface m_gdbserver <=> low level gdbserver */
51 
52 /* Initializes gdbserver. After a call to gdbserver_init, vgdb
53    can contact the gdbserver embedded in valgrind.
54    The rest of the low level gdbserver interface can only
55    be called */
56 extern void gdbserver_init (void);
57 
58 extern void server_main (void);
59 
60 /* To be called to indicate that gdbserver usage is finished.
61    Resources (e.g. FIFOs) will be destroyed. */
62 extern void gdbserver_terminate (void);
63 
64 
65 /* Output string s to the gdb debugging this process or to vgdb.
66    Do not call this directly. Rather use VG_(gdb_printf)
67    to output something to gdb, use normal valgrind messaging
68    (e.g. VG_(umsg)) to send output that can either go
69    to gdb or to log. */
70 extern void monitor_output (char *s);
71 
72 /* returns 0 if there is no connection or no event on the connection
73              with gdb.
74    returns 1 if there are some data which has been received from gdb
75              and that must (still) be handled.
76    returns 2 if remote_desc_activity detected the connection has been
77              lost and should be reopened.
78    msg is used for debug logging.*/
79 extern int remote_desc_activity(const char *msg);
80 
81 /* output some status of gdbserver communication */
82 extern void remote_utils_output_status(void);
83 
84 /* True if there is a connection with gdb. */
85 extern Bool remote_connected(void);
86 
87 /* Finish the connection with gdb and reset_valgrind_sink.
88    Keeps the FIFOs and shared mem so as to allow connection
89    to be reopened. */
90 extern void remote_finish(FinishReason reason);
91 
92 /* If Valgrind sink was changed by gdbserver:
93       Resets the valgrind sink to before the changes done by gdbserver,
94       and does VG_(umsg). If info != NULL, info added in VG_(usmg). */
95 extern void reset_valgrind_sink(const char* info);
96 
97 // VG_(gdb_printf) by default writes to vgdb/gdb.
98 // If there is no connection, it will rather write to the initial (log)
99 // valgrind fd using the below.
100 extern void print_to_initial_valgrind_sink (const char *msg);
101 
102 /* For ARM usage.
103    Guesses if pc is a thumb pc.
104    In this case, returns pc with the thumb bit set (bit0)
105    else just returns pc.
106 
107    The guess is based on the following set of check:
108    if bit0 set      => thumb
109    else if bit1 set => thumb
110    else uses the debuginfo to guess.
111 
112    If debug info not found for this pc, assumes arm */
113 extern Addr thumb_pc (Addr pc);
114 
115 /* when invoked by vgdb using ptrace, contains the tid chosen
116    by vgdb (if vgdb gives a tid different of 0: a 0 tid by
117    vgdb means use the running_tid if there is one running
118    or tid 1 otherwise). */
119 extern ThreadId vgdb_interrupted_tid;
120 
121 /*------------ end of interface to low level gdbserver */
122 
123 
124 #define dlog(level, ...) \
125    do { if (UNLIKELY(VG_(debugLog_getLevel)() >= level))  \
126          VG_(debugLog) (level, "gdbsrv",__VA_ARGS__); }   \
127    while (0)
128 
129 
130 /* vki only defines VKI_POLLIN but even not on all OS.
131    Below is from linux bits/poll.h */
132 #ifndef VKI_POLLIN
133 #define VKI_POLLIN            0x0001
134 #endif
135 #define VKI_POLLPRI           0x0002
136 #define VKI_POLLOUT           0x0004
137 #define VKI_POLLERR           0x0008
138 #define VKI_POLLHUP           0x0010
139 #define VKI_POLLNVAL          0x0020
140 
141 /* a bunch of macros to avoid libc usage in valgrind-ified gdbserver */
142 #define strcmp(s1,s2)         VG_(strcmp) ((s1),(s2))
143 #define strncmp(s1,s2,nmax)   VG_(strncmp) ((s1),(s2),nmax)
144 #define strcat(s1,s2)         VG_(strcat) ((s1),(s2))
145 #define strcpy(s1,s2)         VG_(strcpy) ((s1),(s2))
146 #define strncpy(s1,s2,nmax)   VG_(strncpy) ((s1),(s2),nmax)
147 #define strlen(s)             VG_(strlen) ((s))
148 #define strtok(p,s)           VG_(strtok) ((p),(s))
149 #define strtok_r(p,s,ss)      VG_(strtok_r) ((p),(s),(ss))
150 #define strchr(s,c)           VG_(strchr) ((s),c)
151 /* strtol and strtoul supports base 16 or else assumes it is base 10 */
152 #define strtol(s,r,b)         ((b) == 16 ? \
153                                VG_(strtoll16) ((s),(r)) \
154                                : VG_(strtoll10) ((s),(r)))
155 #define strtoul(s,r,b)        ((b) == 16 ? \
156                                VG_(strtoull16) ((s),(r)) \
157                                : VG_(strtoull10) ((s),(r)))
158 
159 #define malloc(sz)            VG_(arena_malloc)  (VG_AR_CORE, "gdbsrv", sz)
160 #define calloc(n,sz)          VG_(arena_calloc)  (VG_AR_CORE, "gdbsrv", n, sz)
161 #define realloc(p,size)       VG_(arena_realloc) (VG_AR_CORE, "gdbsrv", p, size)
162 #define strdup(s)             VG_(arena_strdup)  (VG_AR_CORE, "gdbsrv", (s))
163 #define free(b)               VG_(arena_free)    (VG_AR_CORE, b)
164 
165 #ifndef ATTR_NORETURN
166 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
167 #define ATTR_NORETURN __attribute__ ((noreturn))
168 #else
169 #define ATTR_NORETURN           /* nothing */
170 #endif
171 #endif
172 
173 #ifndef ATTR_FORMAT
174 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 4))
175 #define ATTR_FORMAT(type, x, y) __attribute__ ((format(type, x, y)))
176 #else
177 #define ATTR_FORMAT(type, x, y) /* nothing */
178 #endif
179 #endif
180 
181 /* A type used for binary buffers.  */
182 typedef unsigned char gdb_byte;
183 
184 typedef Addr CORE_ADDR;
185 
186 /* Generic information for tracking a list of ``inferiors'' - threads,
187    processes, etc.  */
188 struct inferior_list
189 {
190    struct inferior_list_entry *head;
191    struct inferior_list_entry *tail;
192 };
193 struct inferior_list_entry
194 {
195    unsigned long id;
196    struct inferior_list_entry *next;
197 };
198 
199 /* Opaque type for user-visible threads.  */
200 struct thread_info;
201 
202 #include "regcache.h"
203 #include "gdb/signals.h"
204 
205 /* signal handling with gdbserver: before delivering a signal,
206    call gdbserver_signal_encountered. This will set
207    the signal to report in the next resume reply sent to GDB.
208    A call to call_gdbserver is needed to send the resume reply to GDB.
209    After this call, gdbserver_deliver_signal indicates if the signal
210    is effectively to be delivered to the guest process. */
211 extern void gdbserver_signal_encountered (Int vki_sigNo);
212 /* between these two calls, call call_gdbserver */
213 /* If gdbserver_deliver_signal True, then gdb did not ask
214    to ignore the signal, so signal can be delivered to the guest. */
215 extern Bool gdbserver_deliver_signal (Int vki_sigNo);
216 
217 /* Called when a process is about to go with reason ('W' or 'X') and code.
218    This sets global variables that will be used to return the process
219    exit status to GDB in the next resume_reply.
220    Similarly to gdbserver_signal_encountered, a call to call_gdbserver
221    is needed to send the resume reply. */
222 extern void gdbserver_process_exit_encountered (unsigned char status, Int code);
223 
224 /* To optimise signal handling, gdb can instruct gdbserver to
225    not stop on some signals. In the below, a 1 indicates the gdb_nr signal
226    has to be passed directly to the guest, without asking gdb.
227    A 0 indicates gdb has to be consulted to see if signal has
228    or has not to be passed. The gdb consultation is to
229    be done using the above two functions. */
230 extern int pass_signals[]; /* indexed by gdb signal nr */
231 
232 
233 #include "target.h"
234 
235 /* Target-specific functions */
236 
237 /* From inferiors.c.  */
238 
239 extern struct inferior_list all_threads;
240 void add_inferior_to_list (struct inferior_list *list,
241 			   struct inferior_list_entry *new_inferior);
242 void for_each_inferior (struct inferior_list *list,
243 			void (*action) (struct inferior_list_entry *));
244 extern struct thread_info *current_inferior;
245 void remove_inferior (struct inferior_list *list,
246 		      struct inferior_list_entry *entry);
247 void remove_thread (struct thread_info *thread);
248 void add_thread (unsigned long thread_id, void *target_data, unsigned int);
249 unsigned int thread_id_to_gdb_id (unsigned long);
250 unsigned int thread_to_gdb_id (struct thread_info *);
251 unsigned long gdb_id_to_thread_id (unsigned int);
252 struct thread_info *gdb_id_to_thread (unsigned int);
253 void clear_inferiors (void);
254 struct inferior_list_entry *find_inferior (struct inferior_list *,
255                                            int (*func) (struct
256                                                         inferior_list_entry *,
257                                                         void *),
258                                            void *arg);
259 struct inferior_list_entry *find_inferior_id (struct inferior_list *list,
260 					      unsigned long id);
261 void *inferior_target_data (struct thread_info *);
262 void set_inferior_target_data (struct thread_info *, void *);
263 void *inferior_regcache_data (struct thread_info *);
264 void set_inferior_regcache_data (struct thread_info *, void *);
265 void change_inferior_id (struct inferior_list *list,
266 			 unsigned long new_id);
267 
268 /* Public variables in server.c */
269 
270 extern unsigned long cont_thread;
271 extern unsigned long general_thread;
272 extern unsigned long step_thread;
273 extern unsigned long thread_from_wait;
274 extern unsigned long old_thread_from_wait;
275 
276 extern VG_MINIMAL_JMP_BUF(toplevel);
277 
278 /* From remote-utils.c */
279 
280 extern Bool noack_mode;
281 int putpkt (char *buf);
282 int putpkt_binary (char *buf, int len);
283 int getpkt (char *buf);
284 void remote_open (const HChar *name);
285 void remote_close (void);
286 
287 void sync_gdb_connection (void);
288 void write_ok (char *buf);
289 void write_enn (char *buf);
290 void convert_ascii_to_int (const char *from, unsigned char *to, int n);
291 void convert_int_to_ascii (const unsigned char *from, char *to, int n);
292 void prepare_resume_reply (char *buf, char status, unsigned char sig);
293 
294 void decode_address (CORE_ADDR *addrp, const char *start, int len);
295 void decode_m_packet (char *from, CORE_ADDR * mem_addr_ptr,
296 		      unsigned int *len_ptr);
297 void decode_M_packet (char *from, CORE_ADDR * mem_addr_ptr,
298 		      unsigned int *len_ptr, unsigned char *to);
299 int decode_X_packet (char *from, int packet_len, CORE_ADDR * mem_addr_ptr,
300 		     unsigned int *len_ptr, unsigned char *to);
301 
302 int unhexify (char *bin, const char *hex, int count);
303 int hexify (char *hex, const char *bin, int count);
304 /* heximage builds an image of bin according to byte order of the architecture
305    Useful for register and int image */
306 char* heximage (char *buf, char *bin, int count);
307 
308 /* convert from CORE_ADDR to void* */
309 void* C2v(CORE_ADDR addr);
310 
311 
312 int remote_escape_output (const gdb_byte *buffer, int len,
313 			  gdb_byte *out_buf, int *out_len,
314 			  int out_maxlen);
315 
316 /* Functions from ``signals.c''.  */
317 enum target_signal target_signal_from_host (int hostsig);
318 int target_signal_to_host_p (enum target_signal oursig);
319 int target_signal_to_host (enum target_signal oursig);
320 const char *target_signal_to_name (enum target_signal);
321 
322 /* Functions from utils.c */
323 
324 /* error is like VG_(umsg), then VG_MINIMAL_LONGJMP to gdbserver toplevel. */
325 void error (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);
326 /* first output a description of the error inside sr, then like VG_(umsg). */
327 void sr_perror (SysRes sr,const char *string,...) ATTR_FORMAT (printf, 2, 3);
328 /* fatal is like VG_(umsg), then exit(1). */
329 void fatal (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);
330 /* warning is like VG_(umsg). */
331 void warning (const char *string,...) ATTR_FORMAT (printf, 1, 2);
332 
333 /* Functions from the register cache definition.  */
334 
335 void init_registers (void);
336 
337 /* Maximum number of bytes to read/write at once.  The value here
338    is chosen to fill up a packet (the headers account for the 32).  */
339 #define MAXBUFBYTES(N) (((N)-32)/2)
340 
341 /* PBUFSIZ : Buffers size for transferring memory, registers, etc.
342    Must be big enough to hold all the registers, at least.
343    Must be at least big as 2*DATASIZ + 5:
344       1         : packet begin ($ or %)
345     + 2*DATASIZ : encoded string
346     + 1         : packet end (#)
347     + 2         : packet checksum
348     + 1         : \0
349 
350     Max value gdb likes is 16384.
351 
352     Note that what is sent/received to/from gdb does
353     not have a trailing null byte. We are adding 1 here to allow
354     null terminating the strings e.g. for printf.
355 
356     => packet Packet OVERHead SIZe is 5:*/
357 
358 /* keep PBUFSIZ value in sync with vgdb.c */
359 #define PBUFSIZ 16384
360 #define POVERHSIZ 5
361 
362 /* Max size of a string encoded in a packet. Hex Encoding can
363    multiply the size by 2 (trailing null byte not sent). */
364 #define DATASIZ ((PBUFSIZ-POVERHSIZ)/2)
365 
366 /* Version information, from version.c.  */
367 extern const char version[];
368 
369 #endif /* SERVER_H */
370