1b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Common definitions for remote server for GDB.
2b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Copyright (C) 1993, 1995, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005,
3436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov   2006, 2012
4b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Free Software Foundation, Inc.
5b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
6b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   This file is part of GDB.
7b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   It has been modified to integrate it in valgrind
8b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
9b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   This program is free software; you can redistribute it and/or modify
10b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   it under the terms of the GNU General Public License as published by
11b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   the Free Software Foundation; either version 2 of the License, or
12b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   (at your option) any later version.
13b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
14b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   This program is distributed in the hope that it will be useful,
15b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   but WITHOUT ANY WARRANTY; without even the implied warranty of
16b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   GNU General Public License for more details.
18b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
19b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   You should have received a copy of the GNU General Public License
20b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   along with this program; if not, write to the Free Software
21b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Foundation, Inc., 51 Franklin Street, Fifth Floor,
22b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Boston, MA 02110-1301, USA.  */
23b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
24b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#ifndef SERVER_H
25b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define SERVER_H
26b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
27b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_basics.h"
28b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_vki.h"
29b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_xarray.h"
30b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_clientstate.h"
31b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_debuglog.h"
32b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_errormgr.h"
33b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_libcassert.h"
34b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_libcfile.h"
35b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_libcprint.h"
36b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_mallocfree.h"
37b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_syscall.h"
38436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#include "pub_core_libcproc.h"
39b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_tooliface.h"
40436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#include "pub_core_libcassert.h"
41436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#include "pub_core_libcbase.h"
42436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#include "pub_core_options.h"
43436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#include "pub_core_libcsetjmp.h"
44b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_threadstate.h"
45436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#include "pub_core_gdbserver.h"
46b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_aspacemgr.h"
47436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#include "pub_core_vki.h"
48436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#include "pub_core_clreq.h"
49b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
50b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/*------------- interface m_gdbserver <=> low level gdbserver */
51b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
52b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Initializes gdbserver. After a call to gdbserver_init, vgdb
53b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   can contact the gdbserver embedded in valgrind.
54b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   The rest of the low level gdbserver interface can only
55b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   be called */
56b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern void gdbserver_init (void);
57b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
58b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern void server_main (void);
59b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
60b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* To be called to indicate that gdbserver usage is finished.
61b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Resources (e.g. FIFOs) will be destroyed. */
62b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern void gdbserver_terminate (void);
63b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
64b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
65b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Output string s to the gdb debugging this process or to vgdb.
66436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov   Do not call this directly. Rather use VG_(gdb_printf)
67b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   to output something to gdb, use normal valgrind messaging
68b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   (e.g. VG_(umsg)) to send output that can either go
69b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   to gdb or to log. */
70b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern void monitor_output (char *s);
71b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
72b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* returns 0 if there is no connection or no event on the connection
73b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov             with gdb.
74b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   returns 1 if there are some data which has been received from gdb
75b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov             and that must (still) be handled.
76b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   returns 2 if remote_desc_activity detected the connection has been
77b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov             lost and should be reopened.
78b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   msg is used for debug logging.*/
79436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanovextern int remote_desc_activity(const char *msg);
80b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
81b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* output some status of gdbserver communication */
82b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern void remote_utils_output_status(void);
83b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
84b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* True if there is a connection with gdb. */
85b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern Bool remote_connected(void);
86b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
87b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Finish the connection with gdb and reset_valgrind_sink.
88b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Keeps the FIFOs and shared mem so as to allow connection
89b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   to be reopened. */
90b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern void remote_finish(FinishReason reason);
91b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
92b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* If Valgrind sink was changed by gdbserver:
93b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      Resets the valgrind sink to before the changes done by gdbserver,
94b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      and does VG_(umsg). If info != NULL, info added in VG_(usmg). */
95436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanovextern void reset_valgrind_sink(const char* info);
96436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov
97436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov// VG_(gdb_printf) by default writes to vgdb/gdb.
98436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov// If there is no connection, it will rather write to the initial (log)
99436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov// valgrind fd using the below.
100436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanovextern void print_to_initial_valgrind_sink (const char *msg);
101b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
102b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* For ARM usage.
103b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Guesses if pc is a thumb pc.
104b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   In this case, returns pc with the thumb bit set (bit0)
105b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   else just returns pc.
106b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
107b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   The guess is based on the following set of check:
108b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   if bit0 set      => thumb
109b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   else if bit1 set => thumb
110b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   else uses the debuginfo to guess.
111b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
112b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   If debug info not found for this pc, assumes arm */
113b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern Addr thumb_pc (Addr pc);
114b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
115b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* when invoked by vgdb using ptrace, contains the tid chosen
116b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   by vgdb (if vgdb gives a tid different of 0: a 0 tid by
117b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   vgdb means use the running_tid if there is one running
118b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   or tid 1 otherwise). */
119b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern ThreadId vgdb_interrupted_tid;
120b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
121b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/*------------ end of interface to low level gdbserver */
122b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
123b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
124b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define dlog(level, ...) \
125b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   do { if (UNLIKELY(VG_(debugLog_getLevel)() >= level))  \
126b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov         VG_(debugLog) (level, "gdbsrv",__VA_ARGS__); }   \
127b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   while (0)
128b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
129b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
130b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* vki only defines VKI_POLLIN but even not on all OS.
131b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Below is from linux bits/poll.h */
132b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#ifndef VKI_POLLIN
133b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define VKI_POLLIN            0x0001
134b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif
135b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define VKI_POLLPRI           0x0002
136b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define VKI_POLLOUT           0x0004
137b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define VKI_POLLERR           0x0008
138b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define VKI_POLLHUP           0x0010
139b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define VKI_POLLNVAL          0x0020
140b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
141b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* a bunch of macros to avoid libc usage in valgrind-ified gdbserver */
142436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#define strcmp(s1,s2)         VG_(strcmp) ((s1),(s2))
143436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#define strncmp(s1,s2,nmax)   VG_(strncmp) ((s1),(s2),nmax)
144436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#define strcat(s1,s2)         VG_(strcat) ((s1),(s2))
145436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#define strcpy(s1,s2)         VG_(strcpy) ((s1),(s2))
146436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#define strncpy(s1,s2,nmax)   VG_(strncpy) ((s1),(s2),nmax)
147436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#define strlen(s)             VG_(strlen) ((s))
148436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#define strtok(p,s)           VG_(strtok) ((p),(s))
149436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#define strtok_r(p,s,ss)      VG_(strtok_r) ((p),(s),(ss))
150436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#define strchr(s,c)           VG_(strchr) ((s),c)
151b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* strtol and strtoul supports base 16 or else assumes it is base 10 */
152b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define strtol(s,r,b)         ((b) == 16 ? \
153436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov                               VG_(strtoll16) ((s),(r)) \
154436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov                               : VG_(strtoll10) ((s),(r)))
155b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define strtoul(s,r,b)        ((b) == 16 ? \
156436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov                               VG_(strtoull16) ((s),(r)) \
157436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov                               : VG_(strtoull10) ((s),(r)))
158b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
159b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define malloc(sz)            VG_(arena_malloc)  (VG_AR_CORE, "gdbsrv", sz)
160b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define calloc(n,sz)          VG_(arena_calloc)  (VG_AR_CORE, "gdbsrv", n, sz)
161b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define realloc(p,size)       VG_(arena_realloc) (VG_AR_CORE, "gdbsrv", p, size)
162436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#define strdup(s)             VG_(arena_strdup)  (VG_AR_CORE, "gdbsrv", (s))
163b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define free(b)               VG_(arena_free)    (VG_AR_CORE, b)
164b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
165b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#ifndef ATTR_NORETURN
166b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
167b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define ATTR_NORETURN __attribute__ ((noreturn))
168b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#else
169b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define ATTR_NORETURN           /* nothing */
170b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif
171b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif
172b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
173b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#ifndef ATTR_FORMAT
174b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 4))
175b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define ATTR_FORMAT(type, x, y) __attribute__ ((format(type, x, y)))
176b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#else
177b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define ATTR_FORMAT(type, x, y) /* nothing */
178b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif
179b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif
180b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
181b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* A type used for binary buffers.  */
182b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovtypedef unsigned char gdb_byte;
183b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
184b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovtypedef Addr CORE_ADDR;
185b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
186b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Generic information for tracking a list of ``inferiors'' - threads,
187b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   processes, etc.  */
188b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovstruct inferior_list
189b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov{
190b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   struct inferior_list_entry *head;
191b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   struct inferior_list_entry *tail;
192b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov};
193b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovstruct inferior_list_entry
194b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov{
195b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   unsigned long id;
196b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   struct inferior_list_entry *next;
197b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov};
198b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
199b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Opaque type for user-visible threads.  */
200b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovstruct thread_info;
201b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
202b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "regcache.h"
203b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "gdb/signals.h"
204b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
205b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* signal handling with gdbserver: before delivering a signal,
206436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov   call gdbserver_signal_encountered. This will set
207436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov   the signal to report in the next resume reply sent to GDB.
208436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov   A call to call_gdbserver is needed to send the resume reply to GDB.
209436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov   After this call, gdbserver_deliver_signal indicates if the signal
210436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov   is effectively to be delivered to the guest process. */
211663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengextern void gdbserver_signal_encountered (Int vki_sigNo);
212b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* between these two calls, call call_gdbserver */
213b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* If gdbserver_deliver_signal True, then gdb did not ask
214b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   to ignore the signal, so signal can be delivered to the guest. */
215663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengextern Bool gdbserver_deliver_signal (Int vki_sigNo);
216b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
217436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov/* Called when a process is about to go with reason ('W' or 'X') and code.
218436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov   This sets global variables that will be used to return the process
219436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov   exit status to GDB in the next resume_reply.
220436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov   Similarly to gdbserver_signal_encountered, a call to call_gdbserver
221436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov   is needed to send the resume reply. */
222436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanovextern void gdbserver_process_exit_encountered (unsigned char status, Int code);
223436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov
224b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* To optimise signal handling, gdb can instruct gdbserver to
225663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   not stop on some signals. In the below, a 1 indicates the gdb_nr signal
226b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   has to be passed directly to the guest, without asking gdb.
227b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   A 0 indicates gdb has to be consulted to see if signal has
228b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   or has not to be passed. The gdb consultation is to
229b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   be done using the above two functions. */
230663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengextern int pass_signals[]; /* indexed by gdb signal nr */
231b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
232b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
233b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "target.h"
234b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
235b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Target-specific functions */
236b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
237b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* From inferiors.c.  */
238b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
239b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern struct inferior_list all_threads;
240b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid add_inferior_to_list (struct inferior_list *list,
241b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov			   struct inferior_list_entry *new_inferior);
242b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid for_each_inferior (struct inferior_list *list,
243b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov			void (*action) (struct inferior_list_entry *));
244b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern struct thread_info *current_inferior;
245b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid remove_inferior (struct inferior_list *list,
246b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov		      struct inferior_list_entry *entry);
247b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid remove_thread (struct thread_info *thread);
248b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid add_thread (unsigned long thread_id, void *target_data, unsigned int);
249b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovunsigned int thread_id_to_gdb_id (unsigned long);
250b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovunsigned int thread_to_gdb_id (struct thread_info *);
251b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovunsigned long gdb_id_to_thread_id (unsigned int);
252b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovstruct thread_info *gdb_id_to_thread (unsigned int);
253b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid clear_inferiors (void);
254b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovstruct inferior_list_entry *find_inferior (struct inferior_list *,
255b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                                           int (*func) (struct
256b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                                                        inferior_list_entry *,
257b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                                                        void *),
258b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                                           void *arg);
259b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovstruct inferior_list_entry *find_inferior_id (struct inferior_list *list,
260b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov					      unsigned long id);
261b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid *inferior_target_data (struct thread_info *);
262b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid set_inferior_target_data (struct thread_info *, void *);
263b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid *inferior_regcache_data (struct thread_info *);
264b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid set_inferior_regcache_data (struct thread_info *, void *);
265b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid change_inferior_id (struct inferior_list *list,
266b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov			 unsigned long new_id);
267b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
268b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Public variables in server.c */
269b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
270b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern unsigned long cont_thread;
271b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern unsigned long general_thread;
272b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern unsigned long step_thread;
273b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern unsigned long thread_from_wait;
274b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern unsigned long old_thread_from_wait;
275b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
276b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern VG_MINIMAL_JMP_BUF(toplevel);
277b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
278b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* From remote-utils.c */
279b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
280b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern Bool noack_mode;
281b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovint putpkt (char *buf);
282b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovint putpkt_binary (char *buf, int len);
283b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovint getpkt (char *buf);
284436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanovvoid remote_open (const HChar *name);
285b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid remote_close (void);
286b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
287b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid sync_gdb_connection (void);
288b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid write_ok (char *buf);
289b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid write_enn (char *buf);
290436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanovvoid convert_ascii_to_int (const char *from, unsigned char *to, int n);
291436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanovvoid convert_int_to_ascii (const unsigned char *from, char *to, int n);
292b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid prepare_resume_reply (char *buf, char status, unsigned char sig);
293b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
294b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid decode_address (CORE_ADDR *addrp, const char *start, int len);
295b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid decode_m_packet (char *from, CORE_ADDR * mem_addr_ptr,
296b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov		      unsigned int *len_ptr);
297b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid decode_M_packet (char *from, CORE_ADDR * mem_addr_ptr,
298b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov		      unsigned int *len_ptr, unsigned char *to);
299b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovint decode_X_packet (char *from, int packet_len, CORE_ADDR * mem_addr_ptr,
300b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov		     unsigned int *len_ptr, unsigned char *to);
301b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
302b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovint unhexify (char *bin, const char *hex, int count);
303b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovint hexify (char *hex, const char *bin, int count);
304663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng/* heximage builds an image of bin according to byte order of the architecture
305663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   Useful for register and int image */
306663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengchar* heximage (char *buf, char *bin, int count);
307663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
308663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng/* convert from CORE_ADDR to void* */
309663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengvoid* C2v(CORE_ADDR addr);
310663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
311663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
312b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovint remote_escape_output (const gdb_byte *buffer, int len,
313b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov			  gdb_byte *out_buf, int *out_len,
314b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov			  int out_maxlen);
315b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
316b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Functions from ``signals.c''.  */
317b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovenum target_signal target_signal_from_host (int hostsig);
318b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovint target_signal_to_host_p (enum target_signal oursig);
319b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovint target_signal_to_host (enum target_signal oursig);
320436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanovconst char *target_signal_to_name (enum target_signal);
321b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
322b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Functions from utils.c */
323b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
324b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* error is like VG_(umsg), then VG_MINIMAL_LONGJMP to gdbserver toplevel. */
325b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid error (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);
326b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* first output a description of the error inside sr, then like VG_(umsg). */
327436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanovvoid sr_perror (SysRes sr,const char *string,...) ATTR_FORMAT (printf, 2, 3);
328b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* fatal is like VG_(umsg), then exit(1). */
329b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid fatal (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);
330b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* warning is like VG_(umsg). */
331b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid warning (const char *string,...) ATTR_FORMAT (printf, 1, 2);
332b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
333b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Functions from the register cache definition.  */
334b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
335b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovvoid init_registers (void);
336b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
337b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Maximum number of bytes to read/write at once.  The value here
338b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   is chosen to fill up a packet (the headers account for the 32).  */
339b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define MAXBUFBYTES(N) (((N)-32)/2)
340b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
341b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* PBUFSIZ : Buffers size for transferring memory, registers, etc.
342b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Must be big enough to hold all the registers, at least.
343b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Must be at least big as 2*DATASIZ + 5:
344b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      1         : packet begin ($ or %)
345b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov    + 2*DATASIZ : encoded string
346b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov    + 1         : packet end (#)
347b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov    + 2         : packet checksum
348b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov    + 1         : \0
349b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
350b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov    Max value gdb likes is 16384.
351b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
352b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov    Note that what is sent/received to/from gdb does
353b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov    not have a trailing null byte. We are adding 1 here to allow
354b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov    null terminating the strings e.g. for printf.
355b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
356b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov    => packet Packet OVERHead SIZe is 5:*/
357b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
358b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* keep PBUFSIZ value in sync with vgdb.c */
359b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define PBUFSIZ 16384
360b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define POVERHSIZ 5
361b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
362b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Max size of a string encoded in a packet. Hex Encoding can
363b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   multiply the size by 2 (trailing null byte not sent). */
364b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define DATASIZ ((PBUFSIZ-POVERHSIZ)/2)
365b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
366b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Version information, from version.c.  */
367b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern const char version[];
368b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
369b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif /* SERVER_H */
370