Home | History | Annotate | Download | only in m_gdbserver
      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 /* True if gdbserver is single stepping the valgrind process */
    111 extern Bool valgrind_single_stepping(void);
    112 
    113 /* Set Valgrind in single stepping mode or not according to Bool. */
    114 extern void valgrind_set_single_stepping(Bool);
    115 
    116 /* gets the addr at which a (possible) break must be ignored once.
    117    If there is no such break to be ignored once, 0 is returned.
    118    This is needed for the following case:
    119    The user sets a break at address AAA.
    120    The break is encountered. Then the user does stepi
    121    (i.e. step one instruction).
    122    In such a case, the already encountered break must be ignored
    123    to ensure the stepi will advance by one instruction: a "break"
    124    is implemented in valgrind by some helper code just after the
    125    instruction mark at which the break is set. This helper code
    126    verifies if either there is a break at the current PC
    127    or if we are in stepping mode. If we are in stepping mode,
    128    the already encountered break must be ignored once to advance
    129    to the next instruction.
    130    ??? need to check if this is *really* needed. */
    131 extern Addr valgrind_get_ignore_break_once(void);
    132 
    133 /* When addr > 0, ensures the next stop reply packet informs
    134    gdb about the encountered watchpoint.
    135    Use addr 0x0 to reset. */
    136 extern void VG_(set_watchpoint_stop_address) (Addr addr);
    137 
    138 /* when invoked by vgdb using ptrace, contains the tid chosen
    139    by vgdb (if vgdb gives a tid different of 0: a 0 tid by
    140    vgdb means use the running_tid if there is one running
    141    or tid 1 otherwise). */
    142 extern ThreadId vgdb_interrupted_tid;
    143 
    144 /*------------ end of interface to low level gdbserver */
    145 
    146 
    147 #define dlog(level, ...) \
    148    do { if (UNLIKELY(VG_(debugLog_getLevel)() >= level))  \
    149          VG_(debugLog) (level, "gdbsrv",__VA_ARGS__); }   \
    150    while (0)
    151 
    152 
    153 /* vki only defines VKI_POLLIN but even not on all OS.
    154    Below is from linux bits/poll.h */
    155 #ifndef VKI_POLLIN
    156 #define VKI_POLLIN            0x0001
    157 #endif
    158 #define VKI_POLLPRI           0x0002
    159 #define VKI_POLLOUT           0x0004
    160 #define VKI_POLLERR           0x0008
    161 #define VKI_POLLHUP           0x0010
    162 #define VKI_POLLNVAL          0x0020
    163 
    164 /* a bunch of macros to avoid libc usage in valgrind-ified gdbserver */
    165 #define strcmp(s1,s2)         VG_(strcmp) ((Char *)(s1),(Char *)(s2))
    166 #define strncmp(s1,s2,nmax)   VG_(strncmp) ((Char *)(s1),(Char *)(s2),nmax)
    167 #define strcat(s1,s2)         VG_(strcat) ((Char *)(s1),(Char *)(s2))
    168 #define strcpy(s1,s2)         VG_(strcpy) ((Char *)(s1),(Char *)(s2))
    169 #define strncpy(s1,s2,nmax)   VG_(strncpy) ((Char *)(s1),(Char *)(s2),nmax)
    170 #define strlen(s)             VG_(strlen) ((Char *)(s))
    171 #define strtok(p,s)           (char *) VG_(strtok) ((Char *)(p),(Char *)(s))
    172 #define strtok_r(p,s,ss)      (char *) VG_(strtok_r) ((Char *)(p),(Char *)(s),(Char **)(ss))
    173 #define strchr(s,c)           (char *) VG_(strchr) ((Char *)(s),c)
    174 /* strtol and strtoul supports base 16 or else assumes it is base 10 */
    175 #define strtol(s,r,b)         ((b) == 16 ? \
    176                                VG_(strtoll16) ((Char *)(s),(Char **)(r)) \
    177                                : VG_(strtoll10) ((Char *)(s),(Char **)(r)))
    178 #define strtoul(s,r,b)        ((b) == 16 ? \
    179                                VG_(strtoull16) ((Char *)(s),(Char **)(r)) \
    180                                : VG_(strtoull10) ((Char *)(s),(Char **)(r)))
    181 
    182 #define malloc(sz)            VG_(arena_malloc)  (VG_AR_CORE, "gdbsrv", sz)
    183 #define calloc(n,sz)          VG_(arena_calloc)  (VG_AR_CORE, "gdbsrv", n, sz)
    184 #define realloc(p,size)       VG_(arena_realloc) (VG_AR_CORE, "gdbsrv", p, size)
    185 #define strdup(s)             (char *) VG_(arena_strdup)  (VG_AR_CORE, "gdbsrv", (Char *)(s))
    186 #define free(b)               VG_(arena_free)    (VG_AR_CORE, b)
    187 
    188 #ifndef ATTR_NORETURN
    189 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
    190 #define ATTR_NORETURN __attribute__ ((noreturn))
    191 #else
    192 #define ATTR_NORETURN           /* nothing */
    193 #endif
    194 #endif
    195 
    196 #ifndef ATTR_FORMAT
    197 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 4))
    198 #define ATTR_FORMAT(type, x, y) __attribute__ ((format(type, x, y)))
    199 #else
    200 #define ATTR_FORMAT(type, x, y) /* nothing */
    201 #endif
    202 #endif
    203 
    204 /* A type used for binary buffers.  */
    205 typedef unsigned char gdb_byte;
    206 
    207 typedef Addr CORE_ADDR;
    208 
    209 /* Generic information for tracking a list of ``inferiors'' - threads,
    210    processes, etc.  */
    211 struct inferior_list
    212 {
    213    struct inferior_list_entry *head;
    214    struct inferior_list_entry *tail;
    215 };
    216 struct inferior_list_entry
    217 {
    218    unsigned long id;
    219    struct inferior_list_entry *next;
    220 };
    221 
    222 /* Opaque type for user-visible threads.  */
    223 struct thread_info;
    224 
    225 #include "regcache.h"
    226 #include "gdb/signals.h"
    227 
    228 /* signal handling with gdbserver: before delivering a signal,
    229    call gdbserver_signal_encountered then give control to
    230    gdbserver by calling call_gdbserver.
    231    On return, call gdbserver_deliver_signal to effectively
    232    deliver the signal or not. */
    233 extern void gdbserver_signal_encountered (Int sigNo);
    234 /* between these two calls, call call_gdbserver */
    235 /* If gdbserver_deliver_signal True, then gdb did not ask
    236    to ignore the signal, so signal can be delivered to the guest. */
    237 extern Bool gdbserver_deliver_signal (Int sigNo);
    238 
    239 /* To optimise signal handling, gdb can instruct gdbserver to
    240    not stop on some signals. In the below, a 1 indicates the 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[];
    246 
    247 
    248 #include "target.h"
    249 
    250 /* Target-specific functions */
    251 
    252 void initialize_low (void);
    253 
    254 /* initialize or re-initialize the register set of the low target.
    255    if shadow_mode, then (re-)define the normal and valgrind shadow registers
    256    else (re-)define only the normal registers. */
    257 void initialize_shadow_low (Bool shadow_mode);
    258 
    259 /* From inferiors.c.  */
    260 
    261 extern struct inferior_list all_threads;
    262 void add_inferior_to_list (struct inferior_list *list,
    263 			   struct inferior_list_entry *new_inferior);
    264 void for_each_inferior (struct inferior_list *list,
    265 			void (*action) (struct inferior_list_entry *));
    266 extern struct thread_info *current_inferior;
    267 void remove_inferior (struct inferior_list *list,
    268 		      struct inferior_list_entry *entry);
    269 void remove_thread (struct thread_info *thread);
    270 void add_thread (unsigned long thread_id, void *target_data, unsigned int);
    271 unsigned int thread_id_to_gdb_id (unsigned long);
    272 unsigned int thread_to_gdb_id (struct thread_info *);
    273 unsigned long gdb_id_to_thread_id (unsigned int);
    274 struct thread_info *gdb_id_to_thread (unsigned int);
    275 void clear_inferiors (void);
    276 struct inferior_list_entry *find_inferior (struct inferior_list *,
    277                                            int (*func) (struct
    278                                                         inferior_list_entry *,
    279                                                         void *),
    280                                            void *arg);
    281 struct inferior_list_entry *find_inferior_id (struct inferior_list *list,
    282 					      unsigned long id);
    283 void *inferior_target_data (struct thread_info *);
    284 void set_inferior_target_data (struct thread_info *, void *);
    285 void *inferior_regcache_data (struct thread_info *);
    286 void set_inferior_regcache_data (struct thread_info *, void *);
    287 void change_inferior_id (struct inferior_list *list,
    288 			 unsigned long new_id);
    289 
    290 /* Public variables in server.c */
    291 
    292 extern unsigned long cont_thread;
    293 extern unsigned long general_thread;
    294 extern unsigned long step_thread;
    295 extern unsigned long thread_from_wait;
    296 extern unsigned long old_thread_from_wait;
    297 
    298 extern VG_MINIMAL_JMP_BUF(toplevel);
    299 
    300 /* From remote-utils.c */
    301 
    302 extern Bool noack_mode;
    303 int putpkt (char *buf);
    304 int putpkt_binary (char *buf, int len);
    305 int getpkt (char *buf);
    306 void remote_open (char *name);
    307 void remote_close (void);
    308 
    309 void sync_gdb_connection (void);
    310 void write_ok (char *buf);
    311 void write_enn (char *buf);
    312 void convert_ascii_to_int (char *from, unsigned char *to, int n);
    313 void convert_int_to_ascii (unsigned char *from, char *to, int n);
    314 void prepare_resume_reply (char *buf, char status, unsigned char sig);
    315 
    316 void decode_address (CORE_ADDR *addrp, const char *start, int len);
    317 void decode_m_packet (char *from, CORE_ADDR * mem_addr_ptr,
    318 		      unsigned int *len_ptr);
    319 void decode_M_packet (char *from, CORE_ADDR * mem_addr_ptr,
    320 		      unsigned int *len_ptr, unsigned char *to);
    321 int decode_X_packet (char *from, int packet_len, CORE_ADDR * mem_addr_ptr,
    322 		     unsigned int *len_ptr, unsigned char *to);
    323 
    324 int unhexify (char *bin, const char *hex, int count);
    325 int hexify (char *hex, const char *bin, int count);
    326 int remote_escape_output (const gdb_byte *buffer, int len,
    327 			  gdb_byte *out_buf, int *out_len,
    328 			  int out_maxlen);
    329 
    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 char *target_signal_to_name (enum target_signal);
    335 
    336 /* Functions from utils.c */
    337 
    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,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);
    346 
    347 /* Functions from the register cache definition.  */
    348 
    349 void init_registers (void);
    350 
    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)
    354 
    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
    360     + 1         : packet end (#)
    361     + 2         : packet checksum
    362     + 1         : \0
    363 
    364     Max value gdb likes is 16384.
    365 
    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.
    369 
    370     => packet Packet OVERHead SIZe is 5:*/
    371 
    372 /* keep PBUFSIZ value in sync with vgdb.c */
    373 #define PBUFSIZ 16384
    374 #define POVERHSIZ 5
    375 
    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)
    379 
    380 /* Version information, from version.c.  */
    381 extern const char version[];
    382 
    383 #endif /* SERVER_H */
    384