• 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
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_tool_libcproc.h"
39 #include "pub_core_tooliface.h"
40 #include "pub_tool_libcassert.h"
41 #include "pub_tool_libcbase.h"
42 #include "pub_tool_options.h"
43 #include "pub_core_gdbserver.h"
44 #include "pub_tool_libcsetjmp.h"
45 #include "pub_core_threadstate.h"
46 #include "pub_core_aspacemgr.h"
47 #include "pub_tool_vki.h"
48 #include "valgrind.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_(monitor_print)
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(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(char* info);
96 
97 /* For ARM usage.
98    Guesses if pc is a thumb pc.
99    In this case, returns pc with the thumb bit set (bit0)
100    else just returns pc.
101 
102    The guess is based on the following set of check:
103    if bit0 set      => thumb
104    else if bit1 set => thumb
105    else uses the debuginfo to guess.
106 
107    If debug info not found for this pc, assumes arm */
108 extern Addr thumb_pc (Addr pc);
109 
110 /* when invoked by vgdb using ptrace, contains the tid chosen
111    by vgdb (if vgdb gives a tid different of 0: a 0 tid by
112    vgdb means use the running_tid if there is one running
113    or tid 1 otherwise). */
114 extern ThreadId vgdb_interrupted_tid;
115 
116 /*------------ end of interface to low level gdbserver */
117 
118 
119 #define dlog(level, ...) \
120    do { if (UNLIKELY(VG_(debugLog_getLevel)() >= level))  \
121          VG_(debugLog) (level, "gdbsrv",__VA_ARGS__); }   \
122    while (0)
123 
124 
125 /* vki only defines VKI_POLLIN but even not on all OS.
126    Below is from linux bits/poll.h */
127 #ifndef VKI_POLLIN
128 #define VKI_POLLIN            0x0001
129 #endif
130 #define VKI_POLLPRI           0x0002
131 #define VKI_POLLOUT           0x0004
132 #define VKI_POLLERR           0x0008
133 #define VKI_POLLHUP           0x0010
134 #define VKI_POLLNVAL          0x0020
135 
136 /* a bunch of macros to avoid libc usage in valgrind-ified gdbserver */
137 #define strcmp(s1,s2)         VG_(strcmp) ((Char *)(s1),(Char *)(s2))
138 #define strncmp(s1,s2,nmax)   VG_(strncmp) ((Char *)(s1),(Char *)(s2),nmax)
139 #define strcat(s1,s2)         VG_(strcat) ((Char *)(s1),(Char *)(s2))
140 #define strcpy(s1,s2)         VG_(strcpy) ((Char *)(s1),(Char *)(s2))
141 #define strncpy(s1,s2,nmax)   VG_(strncpy) ((Char *)(s1),(Char *)(s2),nmax)
142 #define strlen(s)             VG_(strlen) ((Char *)(s))
143 #define strtok(p,s)           (char *) VG_(strtok) ((Char *)(p),(Char *)(s))
144 #define strtok_r(p,s,ss)      (char *) VG_(strtok_r) ((Char *)(p),(Char *)(s),(Char **)(ss))
145 #define strchr(s,c)           (char *) VG_(strchr) ((Char *)(s),c)
146 /* strtol and strtoul supports base 16 or else assumes it is base 10 */
147 #define strtol(s,r,b)         ((b) == 16 ? \
148                                VG_(strtoll16) ((Char *)(s),(Char **)(r)) \
149                                : VG_(strtoll10) ((Char *)(s),(Char **)(r)))
150 #define strtoul(s,r,b)        ((b) == 16 ? \
151                                VG_(strtoull16) ((Char *)(s),(Char **)(r)) \
152                                : VG_(strtoull10) ((Char *)(s),(Char **)(r)))
153 
154 #define malloc(sz)            VG_(arena_malloc)  (VG_AR_CORE, "gdbsrv", sz)
155 #define calloc(n,sz)          VG_(arena_calloc)  (VG_AR_CORE, "gdbsrv", n, sz)
156 #define realloc(p,size)       VG_(arena_realloc) (VG_AR_CORE, "gdbsrv", p, size)
157 #define strdup(s)             (char *) VG_(arena_strdup)  (VG_AR_CORE, "gdbsrv", (Char *)(s))
158 #define free(b)               VG_(arena_free)    (VG_AR_CORE, b)
159 
160 #ifndef ATTR_NORETURN
161 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
162 #define ATTR_NORETURN __attribute__ ((noreturn))
163 #else
164 #define ATTR_NORETURN           /* nothing */
165 #endif
166 #endif
167 
168 #ifndef ATTR_FORMAT
169 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 4))
170 #define ATTR_FORMAT(type, x, y) __attribute__ ((format(type, x, y)))
171 #else
172 #define ATTR_FORMAT(type, x, y) /* nothing */
173 #endif
174 #endif
175 
176 /* A type used for binary buffers.  */
177 typedef unsigned char gdb_byte;
178 
179 typedef Addr CORE_ADDR;
180 
181 /* Generic information for tracking a list of ``inferiors'' - threads,
182    processes, etc.  */
183 struct inferior_list
184 {
185    struct inferior_list_entry *head;
186    struct inferior_list_entry *tail;
187 };
188 struct inferior_list_entry
189 {
190    unsigned long id;
191    struct inferior_list_entry *next;
192 };
193 
194 /* Opaque type for user-visible threads.  */
195 struct thread_info;
196 
197 #include "regcache.h"
198 #include "gdb/signals.h"
199 
200 /* signal handling with gdbserver: before delivering a signal,
201    call gdbserver_signal_encountered then give control to
202    gdbserver by calling call_gdbserver.
203    On return, call gdbserver_deliver_signal to effectively
204    deliver the signal or not. */
205 extern void gdbserver_signal_encountered (Int vki_sigNo);
206 /* between these two calls, call call_gdbserver */
207 /* If gdbserver_deliver_signal True, then gdb did not ask
208    to ignore the signal, so signal can be delivered to the guest. */
209 extern Bool gdbserver_deliver_signal (Int vki_sigNo);
210 
211 /* To optimise signal handling, gdb can instruct gdbserver to
212    not stop on some signals. In the below, a 1 indicates the gdb_nr signal
213    has to be passed directly to the guest, without asking gdb.
214    A 0 indicates gdb has to be consulted to see if signal has
215    or has not to be passed. The gdb consultation is to
216    be done using the above two functions. */
217 extern int pass_signals[]; /* indexed by gdb signal nr */
218 
219 
220 #include "target.h"
221 
222 /* Target-specific functions */
223 
224 /* From inferiors.c.  */
225 
226 extern struct inferior_list all_threads;
227 void add_inferior_to_list (struct inferior_list *list,
228 			   struct inferior_list_entry *new_inferior);
229 void for_each_inferior (struct inferior_list *list,
230 			void (*action) (struct inferior_list_entry *));
231 extern struct thread_info *current_inferior;
232 void remove_inferior (struct inferior_list *list,
233 		      struct inferior_list_entry *entry);
234 void remove_thread (struct thread_info *thread);
235 void add_thread (unsigned long thread_id, void *target_data, unsigned int);
236 unsigned int thread_id_to_gdb_id (unsigned long);
237 unsigned int thread_to_gdb_id (struct thread_info *);
238 unsigned long gdb_id_to_thread_id (unsigned int);
239 struct thread_info *gdb_id_to_thread (unsigned int);
240 void clear_inferiors (void);
241 struct inferior_list_entry *find_inferior (struct inferior_list *,
242                                            int (*func) (struct
243                                                         inferior_list_entry *,
244                                                         void *),
245                                            void *arg);
246 struct inferior_list_entry *find_inferior_id (struct inferior_list *list,
247 					      unsigned long id);
248 void *inferior_target_data (struct thread_info *);
249 void set_inferior_target_data (struct thread_info *, void *);
250 void *inferior_regcache_data (struct thread_info *);
251 void set_inferior_regcache_data (struct thread_info *, void *);
252 void change_inferior_id (struct inferior_list *list,
253 			 unsigned long new_id);
254 
255 /* Public variables in server.c */
256 
257 extern unsigned long cont_thread;
258 extern unsigned long general_thread;
259 extern unsigned long step_thread;
260 extern unsigned long thread_from_wait;
261 extern unsigned long old_thread_from_wait;
262 
263 extern VG_MINIMAL_JMP_BUF(toplevel);
264 
265 /* From remote-utils.c */
266 
267 extern Bool noack_mode;
268 int putpkt (char *buf);
269 int putpkt_binary (char *buf, int len);
270 int getpkt (char *buf);
271 void remote_open (char *name);
272 void remote_close (void);
273 
274 void sync_gdb_connection (void);
275 void write_ok (char *buf);
276 void write_enn (char *buf);
277 void convert_ascii_to_int (char *from, unsigned char *to, int n);
278 void convert_int_to_ascii (unsigned char *from, char *to, int n);
279 void prepare_resume_reply (char *buf, char status, unsigned char sig);
280 
281 void decode_address (CORE_ADDR *addrp, const char *start, int len);
282 void decode_m_packet (char *from, CORE_ADDR * mem_addr_ptr,
283 		      unsigned int *len_ptr);
284 void decode_M_packet (char *from, CORE_ADDR * mem_addr_ptr,
285 		      unsigned int *len_ptr, unsigned char *to);
286 int decode_X_packet (char *from, int packet_len, CORE_ADDR * mem_addr_ptr,
287 		     unsigned int *len_ptr, unsigned char *to);
288 
289 int unhexify (char *bin, const char *hex, int count);
290 int hexify (char *hex, const char *bin, int count);
291 /* heximage builds an image of bin according to byte order of the architecture
292    Useful for register and int image */
293 char* heximage (char *buf, char *bin, int count);
294 
295 /* convert from CORE_ADDR to void* */
296 void* C2v(CORE_ADDR addr);
297 
298 
299 int remote_escape_output (const gdb_byte *buffer, int len,
300 			  gdb_byte *out_buf, int *out_len,
301 			  int out_maxlen);
302 
303 /* Functions from ``signals.c''.  */
304 enum target_signal target_signal_from_host (int hostsig);
305 int target_signal_to_host_p (enum target_signal oursig);
306 int target_signal_to_host (enum target_signal oursig);
307 char *target_signal_to_name (enum target_signal);
308 
309 /* Functions from utils.c */
310 
311 /* error is like VG_(umsg), then VG_MINIMAL_LONGJMP to gdbserver toplevel. */
312 void error (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);
313 /* first output a description of the error inside sr, then like VG_(umsg). */
314 void sr_perror (SysRes sr,char *string,...) ATTR_FORMAT (printf, 2, 3);
315 /* fatal is like VG_(umsg), then exit(1). */
316 void fatal (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);
317 /* warning is like VG_(umsg). */
318 void warning (const char *string,...) ATTR_FORMAT (printf, 1, 2);
319 
320 /* Functions from the register cache definition.  */
321 
322 void init_registers (void);
323 
324 /* Maximum number of bytes to read/write at once.  The value here
325    is chosen to fill up a packet (the headers account for the 32).  */
326 #define MAXBUFBYTES(N) (((N)-32)/2)
327 
328 /* PBUFSIZ : Buffers size for transferring memory, registers, etc.
329    Must be big enough to hold all the registers, at least.
330    Must be at least big as 2*DATASIZ + 5:
331       1         : packet begin ($ or %)
332     + 2*DATASIZ : encoded string
333     + 1         : packet end (#)
334     + 2         : packet checksum
335     + 1         : \0
336 
337     Max value gdb likes is 16384.
338 
339     Note that what is sent/received to/from gdb does
340     not have a trailing null byte. We are adding 1 here to allow
341     null terminating the strings e.g. for printf.
342 
343     => packet Packet OVERHead SIZe is 5:*/
344 
345 /* keep PBUFSIZ value in sync with vgdb.c */
346 #define PBUFSIZ 16384
347 #define POVERHSIZ 5
348 
349 /* Max size of a string encoded in a packet. Hex Encoding can
350    multiply the size by 2 (trailing null byte not sent). */
351 #define DATASIZ ((PBUFSIZ-POVERHSIZ)/2)
352 
353 /* Version information, from version.c.  */
354 extern const char version[];
355 
356 #endif /* SERVER_H */
357