gdb/gdbserver/
[external/binutils.git] / gdb / gdbserver / server.h
1 /* Common definitions for remote server for GDB.
2    Copyright (C) 1993, 1995, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005,
3    2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #ifndef SERVER_H
21 #define SERVER_H
22
23 #include "config.h"
24
25 #ifdef __MINGW32CE__
26 #include "wincecompat.h"
27 #endif
28
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #ifdef HAVE_ERRNO_H
33 #include <errno.h>
34 #endif
35 #include <setjmp.h>
36
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40
41 #if !HAVE_DECL_STRERROR
42 #ifndef strerror
43 extern char *strerror (int);    /* X3.159-1989  4.11.6.2 */
44 #endif
45 #endif
46
47 #if !HAVE_DECL_PERROR
48 #ifndef perror
49 extern void perror (const char *);
50 #endif
51 #endif
52
53 #if !HAVE_DECL_MEMMEM
54 extern void *memmem (const void *, size_t , const void *, size_t);
55 #endif
56
57 #ifndef ATTR_NORETURN
58 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
59 #define ATTR_NORETURN __attribute__ ((noreturn))
60 #else
61 #define ATTR_NORETURN           /* nothing */
62 #endif
63 #endif
64
65 #ifndef ATTR_FORMAT
66 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 4))
67 #define ATTR_FORMAT(type, x, y) __attribute__ ((format(type, x, y)))
68 #else
69 #define ATTR_FORMAT(type, x, y) /* nothing */
70 #endif
71 #endif
72
73 #ifndef ATTR_MALLOC
74 #if defined(__GNUC__) && (__GNUC__ >= 3)
75 #define ATTR_MALLOC __attribute__ ((__malloc__))
76 #else
77 #define ATTR_MALLOC             /* nothing */
78 #endif
79 #endif
80
81 /* A type used for binary buffers.  */
82 typedef unsigned char gdb_byte;
83
84 /* FIXME: This should probably be autoconf'd for.  It's an integer type at
85    least the size of a (void *).  */
86 typedef long long CORE_ADDR;
87
88 typedef long long LONGEST;
89 typedef unsigned long long ULONGEST;
90
91 /* The ptid struct is a collection of the various "ids" necessary
92    for identifying the inferior.  This consists of the process id
93    (pid), thread id (tid), and other fields necessary for uniquely
94    identifying the inferior process/thread being debugged.  When
95    manipulating ptids, the constructors, accessors, and predicate
96    declared in server.h should be used.  These are as follows:
97
98       ptid_build        - Make a new ptid from a pid, lwp, and tid.
99       pid_to_ptid       - Make a new ptid from just a pid.
100       ptid_get_pid      - Fetch the pid component of a ptid.
101       ptid_get_lwp      - Fetch the lwp component of a ptid.
102       ptid_get_tid      - Fetch the tid component of a ptid.
103       ptid_equal        - Test to see if two ptids are equal.
104
105    Please do NOT access the struct ptid members directly (except, of
106    course, in the implementation of the above ptid manipulation
107    functions).  */
108
109 struct ptid
110   {
111     /* Process id */
112     int pid;
113
114     /* Lightweight process id */
115     long lwp;
116
117     /* Thread id */
118     long tid;
119   };
120
121 typedef struct ptid ptid_t;
122
123 /* The -1 ptid, often used to indicate either an error condition or a
124    "don't care" condition, i.e, "run all threads".  */
125 extern ptid_t minus_one_ptid;
126
127 /* The null or zero ptid, often used to indicate no process.  */
128 extern ptid_t null_ptid;
129
130 /* Attempt to find and return an existing ptid with the given PID,
131    LWP, and TID components.  If none exists, create a new one and
132    return that.  */
133 ptid_t ptid_build (int pid, long lwp, long tid);
134
135 /* Create a ptid from just a pid.  */
136 ptid_t pid_to_ptid (int pid);
137
138 /* Fetch the pid (process id) component from a ptid.  */
139 int ptid_get_pid (ptid_t ptid);
140
141 /* Fetch the lwp (lightweight process) component from a ptid.  */
142 long ptid_get_lwp (ptid_t ptid);
143
144 /* Fetch the tid (thread id) component from a ptid.  */
145 long ptid_get_tid (ptid_t ptid);
146
147 /* Compare two ptids to see if they are equal.  */
148 extern int ptid_equal (ptid_t p1, ptid_t p2);
149
150 /* Return true if this ptid represents a process id.  */
151 extern int ptid_is_pid (ptid_t ptid);
152
153 /* Generic information for tracking a list of ``inferiors'' - threads,
154    processes, etc.  */
155 struct inferior_list
156 {
157   struct inferior_list_entry *head;
158   struct inferior_list_entry *tail;
159 };
160 struct inferior_list_entry
161 {
162   ptid_t id;
163   struct inferior_list_entry *next;
164 };
165
166 struct thread_info;
167 struct process_info;
168 struct regcache;
169
170 #include "regcache.h"
171 #include "gdb/signals.h"
172 #include "gdb_signals.h"
173 #include "target.h"
174 #include "mem-break.h"
175
176 struct thread_info
177 {
178   struct inferior_list_entry entry;
179   void *target_data;
180   void *regcache_data;
181
182   /* The last wait status reported for this thread.  */
183   struct target_waitstatus last_status;
184
185   /* Given `while-stepping', a thread may be collecting data for more
186      than one tracepoint simultaneously.  E.g.:
187
188     ff0001  INSN1 <-- TP1, while-stepping 10 collect $regs
189     ff0002  INSN2
190     ff0003  INSN3 <-- TP2, collect $regs
191     ff0004  INSN4 <-- TP3, while-stepping 10 collect $regs
192     ff0005  INSN5
193
194    Notice that when instruction INSN5 is reached, the while-stepping
195    actions of both TP1 and TP3 are still being collected, and that TP2
196    had been collected meanwhile.  The whole range of ff0001-ff0005
197    should be single-stepped, due to at least TP1's while-stepping
198    action covering the whole range.
199
200    On the other hand, the same tracepoint with a while-stepping action
201    may be hit by more than one thread simultaneously, hence we can't
202    keep the current step count in the tracepoint itself.
203
204    This is the head of the list of the states of `while-stepping'
205    tracepoint actions this thread is now collecting; NULL if empty.
206    Each item in the list holds the current step of the while-stepping
207    action.  */
208   struct wstep_state *while_stepping;
209 };
210
211 struct dll_info
212 {
213   struct inferior_list_entry entry;
214   char *name;
215   CORE_ADDR base_addr;
216 };
217
218 struct sym_cache;
219 struct breakpoint;
220 struct raw_breakpoint;
221 struct process_info_private;
222
223 struct process_info
224 {
225   struct inferior_list_entry head;
226
227   int attached;
228
229   /* The symbol cache.  */
230   struct sym_cache *symbol_cache;
231
232   /* If this flag has been set, assume symbol cache misses are
233      failures.  */
234   int all_symbols_looked_up;
235
236   /* The list of memory breakpoints.  */
237   struct breakpoint *breakpoints;
238
239   /* The list of raw memory breakpoints.  */
240   struct raw_breakpoint *raw_breakpoints;
241
242   /* Private target data.  */
243   struct process_info_private *private;
244 };
245
246 /* Return a pointer to the process that corresponds to the current
247    thread (current_inferior).  It is an error to call this if there is
248    no current thread selected.  */
249
250 struct process_info *current_process (void);
251 struct process_info *get_thread_process (struct thread_info *);
252
253 /* Target-specific functions */
254
255 void initialize_low ();
256
257 /* From inferiors.c.  */
258
259 extern struct inferior_list all_processes;
260 extern struct inferior_list all_threads;
261 extern struct inferior_list all_dlls;
262 extern int dlls_changed;
263
264 void initialize_inferiors (void);
265
266 void add_inferior_to_list (struct inferior_list *list,
267                            struct inferior_list_entry *new_inferior);
268 void for_each_inferior (struct inferior_list *list,
269                         void (*action) (struct inferior_list_entry *));
270
271 extern struct thread_info *current_inferior;
272 void remove_inferior (struct inferior_list *list,
273                       struct inferior_list_entry *entry);
274 void remove_thread (struct thread_info *thread);
275 void add_thread (ptid_t ptid, void *target_data);
276
277 struct process_info *add_process (int pid, int attached);
278 void remove_process (struct process_info *process);
279 struct process_info *find_process_pid (int pid);
280 int have_started_inferiors_p (void);
281 int have_attached_inferiors_p (void);
282
283 struct thread_info *find_thread_ptid (ptid_t ptid);
284
285 ptid_t thread_id_to_gdb_id (ptid_t);
286 ptid_t thread_to_gdb_id (struct thread_info *);
287 ptid_t gdb_id_to_thread_id (ptid_t);
288 struct thread_info *gdb_id_to_thread (unsigned int);
289 void clear_inferiors (void);
290 struct inferior_list_entry *find_inferior
291      (struct inferior_list *,
292       int (*func) (struct inferior_list_entry *,
293                    void *),
294       void *arg);
295 struct inferior_list_entry *find_inferior_id (struct inferior_list *list,
296                                               ptid_t id);
297 void *inferior_target_data (struct thread_info *);
298 void set_inferior_target_data (struct thread_info *, void *);
299 void *inferior_regcache_data (struct thread_info *);
300 void set_inferior_regcache_data (struct thread_info *, void *);
301 void add_pid_to_list (struct inferior_list *list, unsigned long pid);
302 int pull_pid_from_list (struct inferior_list *list, unsigned long pid);
303
304 void loaded_dll (const char *name, CORE_ADDR base_addr);
305 void unloaded_dll (const char *name, CORE_ADDR base_addr);
306
307 /* Public variables in server.c */
308
309 extern ptid_t cont_thread;
310 extern ptid_t general_thread;
311 extern ptid_t step_thread;
312
313 extern int server_waiting;
314 extern int debug_threads;
315 extern int debug_hw_points;
316 extern int pass_signals[];
317
318 extern jmp_buf toplevel;
319
320 extern int disable_packet_vCont;
321 extern int disable_packet_Tthread;
322 extern int disable_packet_qC;
323 extern int disable_packet_qfThreadInfo;
324
325 extern int multi_process;
326 extern int non_stop;
327
328 /* Functions from event-loop.c.  */
329 typedef void *gdb_client_data;
330 typedef void (handler_func) (int, gdb_client_data);
331
332 extern void delete_file_handler (int fd);
333 extern void add_file_handler (int fd, handler_func *proc,
334                               gdb_client_data client_data);
335
336 extern void start_event_loop (void);
337
338 /* Functions from server.c.  */
339 extern void handle_serial_event (int err, gdb_client_data client_data);
340 extern void handle_target_event (int err, gdb_client_data client_data);
341
342 extern void push_event (ptid_t ptid, struct target_waitstatus *status);
343
344 /* Functions from hostio.c.  */
345 extern int handle_vFile (char *, int, int *);
346
347 /* Functions from hostio-errno.c.  */
348 extern void hostio_last_error_from_errno (char *own_buf);
349
350 /* From remote-utils.c */
351
352 extern int remote_debug;
353 extern int all_symbols_looked_up;
354 extern int noack_mode;
355 extern int transport_is_reliable;
356
357 ptid_t read_ptid (char *buf, char **obuf);
358 char *write_ptid (char *buf, ptid_t ptid);
359
360 int putpkt (char *buf);
361 int putpkt_binary (char *buf, int len);
362 int putpkt_notif (char *buf);
363 int getpkt (char *buf);
364 void remote_open (char *name);
365 void remote_close (void);
366 void write_ok (char *buf);
367 void write_enn (char *buf);
368 void initialize_async_io (void);
369 void enable_async_io (void);
370 void disable_async_io (void);
371 void check_remote_input_interrupt_request (void);
372 void convert_ascii_to_int (char *from, unsigned char *to, int n);
373 void convert_int_to_ascii (unsigned char *from, char *to, int n);
374 void new_thread_notify (int id);
375 void dead_thread_notify (int id);
376 void prepare_resume_reply (char *buf, ptid_t ptid,
377                            struct target_waitstatus *status);
378
379 const char *decode_address_to_semicolon (CORE_ADDR *addrp, const char *start);
380 void decode_address (CORE_ADDR *addrp, const char *start, int len);
381 void decode_m_packet (char *from, CORE_ADDR * mem_addr_ptr,
382                       unsigned int *len_ptr);
383 void decode_M_packet (char *from, CORE_ADDR * mem_addr_ptr,
384                       unsigned int *len_ptr, unsigned char *to);
385 int decode_X_packet (char *from, int packet_len, CORE_ADDR * mem_addr_ptr,
386                      unsigned int *len_ptr, unsigned char *to);
387 int decode_xfer_write (char *buf, int packet_len, char **annex,
388                        CORE_ADDR *offset, unsigned int *len,
389                        unsigned char *data);
390 int decode_search_memory_packet (const char *buf, int packet_len,
391                                  CORE_ADDR *start_addrp,
392                                  CORE_ADDR *search_space_lenp,
393                                  gdb_byte *pattern, unsigned int *pattern_lenp);
394
395 int unhexify (char *bin, const char *hex, int count);
396 int hexify (char *hex, const char *bin, int count);
397 int remote_escape_output (const gdb_byte *buffer, int len,
398                           gdb_byte *out_buf, int *out_len,
399                           int out_maxlen);
400 char *unpack_varlen_hex (char *buff,  ULONGEST *result);
401
402 void clear_symbol_cache (struct sym_cache **symcache_p);
403 int look_up_one_symbol (const char *name, CORE_ADDR *addrp);
404
405 void monitor_output (const char *msg);
406
407 char *xml_escape_text (const char *text);
408
409 /* Simple growing buffer.  */
410
411 struct buffer
412 {
413   char *buffer;
414   size_t buffer_size; /* allocated size */
415   size_t used_size; /* actually used size */
416 };
417
418 /* Append DATA of size SIZE to the end of BUFFER.  Grows the buffer to
419    accommodate the new data.  */
420 void buffer_grow (struct buffer *buffer, const char *data, size_t size);
421
422 /* Release any memory held by BUFFER.  */
423 void buffer_free (struct buffer *buffer);
424
425 /* Initialize BUFFER.  BUFFER holds no memory afterwards.  */
426 void buffer_init (struct buffer *buffer);
427
428 /* Return a pointer into BUFFER data, effectivelly transfering
429    ownership of the buffer memory to the caller.  Calling buffer_free
430    afterwards has no effect on the returned data.  */
431 char* buffer_finish (struct buffer *buffer);
432
433 /* Simple printf to BUFFER function.  Current implemented formatters:
434    %s - grow an xml escaped text in OBSTACK.  */
435 void buffer_xml_printf (struct buffer *buffer, const char *format, ...)
436   ATTR_FORMAT (printf, 2, 3);;
437
438 #define buffer_grow_str(BUFFER,STRING)         \
439   buffer_grow (BUFFER, STRING, strlen (STRING))
440 #define buffer_grow_str0(BUFFER,STRING)                        \
441   buffer_grow (BUFFER, STRING, strlen (STRING) + 1)
442
443 /* Functions from utils.c */
444
445 void *xmalloc (size_t) ATTR_MALLOC;
446 void *xrealloc (void *, size_t);
447 void *xcalloc (size_t, size_t) ATTR_MALLOC;
448 char *xstrdup (const char *) ATTR_MALLOC;
449 void freeargv (char **argv);
450 void perror_with_name (const char *string);
451 void error (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);
452 void fatal (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);
453 void internal_error (const char *file, int line, const char *, ...)
454      ATTR_NORETURN ATTR_FORMAT (printf, 3, 4);
455 void warning (const char *string,...) ATTR_FORMAT (printf, 1, 2);
456 char *paddress (CORE_ADDR addr);
457 char *pulongest (ULONGEST u);
458 char *plongest (LONGEST l);
459 char *phex_nz (ULONGEST l, int sizeof_l);
460
461 #define gdb_assert(expr)                                                      \
462   ((void) ((expr) ? 0 :                                                       \
463            (gdb_assert_fail (#expr, __FILE__, __LINE__, ASSERT_FUNCTION), 0)))
464
465 /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
466    which contains the name of the function currently being defined.
467    This is broken in G++ before version 2.6.
468    C9x has a similar variable called __func__, but prefer the GCC one since
469    it demangles C++ function names.  */
470 #if (GCC_VERSION >= 2004)
471 #define ASSERT_FUNCTION         __PRETTY_FUNCTION__
472 #else
473 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
474 #define ASSERT_FUNCTION         __func__
475 #endif
476 #endif
477
478 /* This prints an "Assertion failed" message, and exits.  */
479 #if defined (ASSERT_FUNCTION)
480 #define gdb_assert_fail(assertion, file, line, function)                \
481   internal_error (file, line, "%s: Assertion `%s' failed.",             \
482                   function, assertion)
483 #else
484 #define gdb_assert_fail(assertion, file, line, function)                \
485   internal_error (file, line, "Assertion `%s' failed.",                 \
486                   assertion)
487 #endif
488
489 /* Maximum number of bytes to read/write at once.  The value here
490    is chosen to fill up a packet (the headers account for the 32).  */
491 #define MAXBUFBYTES(N) (((N)-32)/2)
492
493 /* Buffer sizes for transferring memory, registers, etc.   Set to a constant
494    value to accomodate multiple register formats.  This value must be at least
495    as large as the largest register set supported by gdbserver.  */
496 #define PBUFSIZ 16384
497
498 /* Functions from tracepoint.c */
499
500 void initialize_tracepoint (void);
501
502 int handle_tracepoint_general_set (char *own_buf);
503 int handle_tracepoint_query (char *own_buf);
504
505 int tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc);
506 int tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc);
507
508 void release_while_stepping_state_list (struct thread_info *tinfo);
509
510 extern int current_traceframe;
511
512 int in_readonly_region (CORE_ADDR addr, ULONGEST length);
513 int traceframe_read_mem (int tfnum, CORE_ADDR addr,
514                          unsigned char *buf, ULONGEST length,
515                          ULONGEST *nbytes);
516 int fetch_traceframe_registers (int tfnum,
517                                 struct regcache *regcache,
518                                 int regnum);
519
520 /* Version information, from version.c.  */
521 extern const char version[];
522 extern const char host_name[];
523
524 #endif /* SERVER_H */