vgdb: Handle EAGAIN in read_buf
[valgrind.git] / coregrind / m_gdbserver / server.h
blob953abbd61d5c377a5d7d3dc34f2b293ee66364bf
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.
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. */
24 #ifndef SERVER_H
25 #define SERVER_H
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
53 be called */
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
67 to gdb or to log. */
68 extern void monitor_output (char *s);
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);
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
87 to be reopened. */
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);
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.
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.
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__); } \
133 while (0)
136 /* vki only defines VKI_POLLIN but even not on all OS.
137 Below is from linux bits/poll.h */
138 #ifndef VKI_POLLIN
139 #define VKI_POLLIN 0x0001
140 #endif
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 malloc(sz) VG_(malloc) ("gdbsrv", sz)
166 #define calloc(n,sz) VG_(calloc) ("gdbsrv", n, sz)
167 #define realloc(p,size) VG_(realloc) ("gdbsrv", p, size)
168 #define strdup(s) VG_(strdup) ("gdbsrv", (s))
169 #define free(b) VG_(free) (b)
171 #ifndef ATTR_NORETURN
172 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
173 #define ATTR_NORETURN __attribute__ ((noreturn))
174 #else
175 #define ATTR_NORETURN /* nothing */
176 #endif
177 #endif
179 #ifndef ATTR_FORMAT
180 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 4))
181 #define ATTR_FORMAT(type, x, y) __attribute__ ((format(type, x, y)))
182 #else
183 #define ATTR_FORMAT(type, x, y) /* nothing */
184 #endif
185 #endif
187 /* A type used for binary buffers. */
188 typedef unsigned char gdb_byte;
190 typedef Addr CORE_ADDR;
192 /* Generic information for tracking a list of ``inferiors'' - threads,
193 processes, etc. */
194 struct inferior_list
196 struct inferior_list_entry *head;
197 struct inferior_list_entry *tail;
199 struct inferior_list_entry
201 unsigned long id;
202 struct inferior_list_entry *next;
205 /* Opaque type for user-visible threads. */
206 struct thread_info;
208 #include "regcache.h"
209 #include "gdb/signals.h"
211 /* signal handling with gdbserver: before delivering a signal,
212 call gdbserver_signal_encountered. This will set
213 the signal to report in the next resume reply sent to GDB.
214 A call to call_gdbserver is needed to send the resume reply to GDB.
215 After this call, gdbserver_deliver_signal indicates if the signal
216 is effectively to be delivered to the guest process. */
217 extern void gdbserver_signal_encountered (const vki_siginfo_t *info);
218 /* between these two calls, call call_gdbserver.
219 Between these 2 calls the signal to report to GDB can be retrieved using
220 gdbserver_pending_signal_to_report. */
221 /* If gdbserver_deliver_signal True, then gdb did not ask
222 to ignore the signal, so signal can be delivered to the guest. */
223 extern Bool gdbserver_deliver_signal (vki_siginfo_t *info);
225 /* Signal info last provided with gdbserver_signal_encountered.
226 It is what is/will be reported to GDB. */
227 extern void gdbserver_pending_signal_to_report (vki_siginfo_t /* OUT */ *info);
229 /* Called when a process is about to go with reason ('W' or 'X') and code.
230 This sets global variables that will be used to return the process
231 exit status to GDB in the next resume_reply.
232 Similarly to gdbserver_signal_encountered, a call to call_gdbserver
233 is needed to send the resume reply. */
234 extern void gdbserver_process_exit_encountered (unsigned char status, Int code);
236 /* To optimise signal handling, gdb can instruct gdbserver to
237 not stop on some signals. In the below, a 1 indicates the gdb_nr signal
238 has to be passed directly to the guest, without asking gdb.
239 A 0 indicates gdb has to be consulted to see if signal has
240 or has not to be passed. The gdb consultation is to
241 be done using the above two functions. */
242 extern int pass_signals[]; /* indexed by gdb signal nr */
245 #include "target.h"
247 /* Target-specific functions */
249 /* From inferiors.c. */
251 extern struct inferior_list all_threads;
252 void add_inferior_to_list (struct inferior_list *list,
253 struct inferior_list_entry *new_inferior);
254 void for_each_inferior (struct inferior_list *list,
255 void (*action) (struct inferior_list_entry *));
256 extern struct thread_info *current_inferior;
257 void remove_inferior (struct inferior_list *list,
258 struct inferior_list_entry *entry);
259 void remove_thread (struct thread_info *thread);
260 void add_thread (unsigned long thread_id, void *target_data, unsigned int);
261 unsigned int thread_id_to_gdb_id (unsigned long);
262 unsigned int thread_to_gdb_id (struct thread_info *);
263 unsigned long gdb_id_to_thread_id (unsigned int);
264 struct thread_info *gdb_id_to_thread (unsigned int);
265 void clear_inferiors (void);
266 struct inferior_list_entry *find_inferior (struct inferior_list *,
267 int (*func) (struct
268 inferior_list_entry *,
269 void *),
270 void *arg);
271 struct inferior_list_entry *find_inferior_id (struct inferior_list *list,
272 unsigned long id);
273 void *inferior_target_data (struct thread_info *);
274 void set_inferior_target_data (struct thread_info *, void *);
275 void *inferior_regcache_data (struct thread_info *);
276 void set_inferior_regcache_data (struct thread_info *, void *);
277 void change_inferior_id (struct inferior_list *list,
278 unsigned long new_id);
280 /* Public variables in server.c */
282 extern unsigned long cont_thread;
283 extern unsigned long general_thread;
284 extern unsigned long step_thread;
285 extern unsigned long thread_from_wait;
286 extern unsigned long old_thread_from_wait;
288 extern VG_MINIMAL_JMP_BUF(toplevel);
290 /* From remote-utils.c */
292 extern Bool noack_mode;
293 int putpkt (char *buf);
294 int putpkt_binary (char *buf, int len);
295 int getpkt (char *buf);
296 void remote_open (const HChar *name);
297 void remote_close (void);
299 void sync_gdb_connection (void);
300 void write_ok (char *buf);
301 void write_enn (char *buf);
302 void convert_ascii_to_int (const char *from, unsigned char *to, int n);
303 void convert_int_to_ascii (const unsigned char *from, char *to, int n);
304 void prepare_resume_reply (char *buf, char status, unsigned char sig);
306 void decode_address (CORE_ADDR *addrp, const char *start, int len);
307 void decode_m_packet (char *from, CORE_ADDR * mem_addr_ptr,
308 unsigned int *len_ptr);
309 void decode_M_packet (char *from, CORE_ADDR * mem_addr_ptr,
310 unsigned int *len_ptr, unsigned char *to);
311 int decode_X_packet (char *from, int packet_len, CORE_ADDR * mem_addr_ptr,
312 unsigned int *len_ptr, unsigned char *to);
314 int unhexify (char *bin, const char *hex, int count);
315 int hexify (char *hex, const char *bin, int count);
316 /* heximage builds an image of bin according to byte order of the architecture
317 Useful for register and int image */
318 char* heximage (char *buf, const char *bin, int count);
320 /* convert from CORE_ADDR to void* */
321 void* C2v(CORE_ADDR addr);
324 int remote_escape_output (const gdb_byte *buffer, int len,
325 gdb_byte *out_buf, int *out_len,
326 int out_maxlen);
328 /* Functions from ``signals.c''. */
329 enum target_signal target_signal_from_host (int hostsig);
330 int target_signal_to_host_p (enum target_signal oursig);
331 int target_signal_to_host (enum target_signal oursig);
332 const char *target_signal_to_name (enum target_signal);
334 /* Functions from utils.c */
336 /* error is like VG_(umsg), then VG_MINIMAL_LONGJMP to gdbserver toplevel. */
337 void error (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);
338 /* first output a description of the error inside sr, then like VG_(umsg). */
339 void sr_perror (SysRes sr,const char *string,...) ATTR_FORMAT (printf, 2, 3);
340 /* fatal is like VG_(umsg), then exit(1). */
341 void fatal (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);
342 /* warning is like VG_(umsg). */
343 void warning (const char *string,...) ATTR_FORMAT (printf, 1, 2);
345 /* Functions from the register cache definition. */
347 void init_registers (void);
349 /* Maximum number of bytes to read/write at once. The value here
350 is chosen to fill up a packet (the headers account for the 32). */
351 #define MAXBUFBYTES(N) (((N)-32)/2)
353 /* PBUFSIZ : Buffers size for transferring memory, registers, etc.
354 Must be big enough to hold all the registers, at least.
355 Must be at least big as 2*DATASIZ + 5:
356 1 : packet begin ($ or %)
357 + 2*DATASIZ : encoded string
358 + 1 : packet end (#)
359 + 2 : packet checksum
360 + 1 : \0
362 Max value gdb likes is 16384.
364 Note that what is sent/received to/from gdb does
365 not have a trailing null byte. We are adding 1 here to allow
366 null terminating the strings e.g. for printf.
368 => packet Packet OVERHead SIZe is 5:*/
370 /* keep PBUFSIZ value in sync with vgdb.c */
371 #define PBUFSIZ 16384
372 #define POVERHSIZ 5
374 /* Max size of a string encoded in a packet. Hex Encoding can
375 multiply the size by 2 (trailing null byte not sent). */
376 #define DATASIZ ((PBUFSIZ-POVERHSIZ)/2)
378 /* Version information, from version.c. */
379 extern const char version[];
381 #endif /* SERVER_H */