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