blob: 9a8b5f2b4e0d8027a988ee1826f4323e04bd2609 [file] [log] [blame]
philippe349a3912012-05-23 21:50:36 +00001/* Target operations for the Valgrind remote server for GDB.
2 Copyright (C) 2002, 2003, 2004, 2005, 2012
sewardj3b290482011-05-06 21:02:55 +00003 Free Software Foundation, Inc.
philippe349a3912012-05-23 21:50:36 +00004 Philippe Waroquiers.
sewardj3b290482011-05-06 21:02:55 +00005
6 Contributed by MontaVista Software.
7
8 This file is part of GDB.
9 It has been modified to integrate it in valgrind
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 Boston, MA 02110-1301, USA. */
25
26#ifndef TARGET_H
27#define TARGET_H
28
florian535fb1b2013-09-15 13:54:34 +000029#include "pub_core_basics.h" // Addr
30#include "server.h" // CORE_ADDR
31
philippe349a3912012-05-23 21:50:36 +000032/* This file defines the architecture independent Valgrind gdbserver
33 high level operations such as read memory, get/set registers, ...
sewardj3b290482011-05-06 21:02:55 +000034
philippe349a3912012-05-23 21:50:36 +000035 These high level operations are called by the gdbserver
36 protocol implementation (e.g. typically server.c).
37
38 For some of these high level operations, target.c will call
39 low level operations dependent on the architecture.
40
41 For example, getting or setting the registers will work on a
42 register cache. The exact details of the registers (how much,
43 their size, etc) is not defined by target.c or the register cache.
44
45 Such architecture dependent information is defined by
46 valgrind_low.h/valgrind-low-xxxxx.c providing 'low level operations'
47 specific to the xxxxx architecture (for example,
48 valgrind-low-x86.c, valgrind-low-armc.c). */
49
50/* -------------------------------------------------------------------------- */
51/* ------------------------ Initialisation ---------------------------------- */
52/* -------------------------------------------------------------------------- */
53
54/* Initialize the Valgrind high target. This will in turn
55 initialise the low (architecture specific) target. */
56extern void valgrind_initialize_target(void);
57
58/* initialize or re-initialize the register set of the low target.
59 if shadow_mode, then (re-)define the normal and valgrind shadow registers
60 else (re-)define only the normal registers. */
61extern void initialize_shadow_low (Bool shadow_mode);
62
63/* Returns the name of the xml target description file.
philippe419d5f22012-05-24 21:33:17 +000064 returns NULL if no xml target description available.
65 if shadow_mode, then returns the xml target description
66 with the shadow registers
67 else returns the xml target description only for
68 the normal registers. */
florian6bd9dc12012-11-23 16:17:43 +000069extern const char* valgrind_target_xml (Bool shadow_mode);
philippe349a3912012-05-23 21:50:36 +000070
71
72/* -------------------------------------------------------------------------- */
73/* --------------------------- Execution control ---------------------------- */
74/* -------------------------------------------------------------------------- */
75
76/* This structure describes how to resume the execution.
77 Currently, there is no way to resume only a specific thread. */
sewardj3b290482011-05-06 21:02:55 +000078struct thread_resume
79{
sewardj3b290482011-05-06 21:02:55 +000080 /* If non-zero, we want to single-step. */
81 int step;
82
83 /* If non-zero, send this signal when we resume. */
84 int sig;
85};
86
philippe349a3912012-05-23 21:50:36 +000087/* Prepare to Resume (i.e. restart) the guest.
88 The resume info indicates how the resume will be done.
89 In case GDB has changed the program counter, valgrind_resume
90 will also ensure that the execution will be resumed at this
91 new program counter.
92 The Resume is really only executed once the gdbserver
93 returns (giving back the control to Valgrind). */
94extern void valgrind_resume (struct thread_resume *resume_info);
sewardj3b290482011-05-06 21:02:55 +000095
philippe349a3912012-05-23 21:50:36 +000096/* When Valgrind gets the control, it will execute the guest
97 process till there is a reason to call the gdbserver
98 again (e.g. because a breakpoint is encountered or the
99 tool reports an error).
100 In such case, the executionof guest code stops, and the
101 control is given to gdbserver. Gdbserver will send a resume
102 reply packet to GDB.
sewardj3b290482011-05-06 21:02:55 +0000103
philippe349a3912012-05-23 21:50:36 +0000104 valgrind_wait gets from Valgrind data structures the
105 information needed produce the resume reply for GDB:
106 a.o. OURSTATUS will be filled in with a response code to send to GDB.
sewardj3b290482011-05-06 21:02:55 +0000107
philippe349a3912012-05-23 21:50:36 +0000108 Returns the signal which caused the process to stop, in the
109 remote protocol numbering (e.g. TARGET_SIGNAL_STOP), or the
110 exit code as an integer if *OURSTATUS is 'W'. */
111extern unsigned char valgrind_wait (char *outstatus);
sewardj3b290482011-05-06 21:02:55 +0000112
philippe349a3912012-05-23 21:50:36 +0000113/* When execution is stopped and gdbserver has control, more
114 info about the stop reason can be retrieved using the following
115 functions. */
sewardj3b290482011-05-06 21:02:55 +0000116
philippe349a3912012-05-23 21:50:36 +0000117/* gets the addr at which a (possible) break must be ignored once.
118 If there is no such break to be ignored once, 0 is returned.
119 This is needed for the following case:
120 The user sets a break at address AAA.
121 The break is encountered. Then the user does stepi
122 (i.e. step one instruction).
123 In such a case, the already encountered break must be ignored
124 to ensure the stepi will advance by one instruction: a "break"
125 is implemented in valgrind by some helper code just after the
126 instruction mark at which the break is set. This helper code
127 verifies if either there is a break at the current PC
128 or if we are in stepping mode. If we are in stepping mode,
129 the already encountered break must be ignored once to advance
130 to the next instruction.
131 ??? need to check if this is *really* needed. */
132extern Addr valgrind_get_ignore_break_once(void);
sewardj3b290482011-05-06 21:02:55 +0000133
philippe349a3912012-05-23 21:50:36 +0000134/* When addr > 0, ensures the next resume reply packet informs
135 gdb about the encountered watchpoint.
136 valgrind_stopped_by_watchpoint() will return 1 till reset.
137 Use addr 0x0 to reset. */
138extern void VG_(set_watchpoint_stop_address) (Addr addr);
sewardj3b290482011-05-06 21:02:55 +0000139
philippe349a3912012-05-23 21:50:36 +0000140/* Returns 1 if target was stopped due to a watchpoint hit, 0 otherwise. */
141extern int valgrind_stopped_by_watchpoint (void);
sewardj3b290482011-05-06 21:02:55 +0000142
philippe349a3912012-05-23 21:50:36 +0000143/* Returns the address associated with the watchpoint that hit, if any;
144 returns 0 otherwise. */
145extern CORE_ADDR valgrind_stopped_data_address (void);
sewardj3b290482011-05-06 21:02:55 +0000146
Elliott Hughesa0664b92017-04-18 17:46:52 -0700147
148/* Inform GDB (if needed) that client is before (or after) syscall sysno.
149 sysno -1 is used to clear the fact that a syscall has been encountered. */
150extern void gdbserver_syscall_encountered (Bool before, Int sysno);
151
152/* >= 0 if valgrind stopped due to syscall, -1 if not stopped due to syscall. */
153extern Int valgrind_stopped_by_syscall (void);
154
155/* if valgrind_stopped_by_syscall() >= 0, tells if stopped before or after
156 syscall. */
157extern Bool valgrind_stopped_before_syscall (void);
158
philippe349a3912012-05-23 21:50:36 +0000159/* True if gdbserver is single stepping the valgrind process */
Elliott Hughesa0664b92017-04-18 17:46:52 -0700160extern Bool valgrind_single_stepping (void);
sewardj3b290482011-05-06 21:02:55 +0000161
philippe349a3912012-05-23 21:50:36 +0000162/* Set Valgrind in single stepping mode or not according to Bool. */
Elliott Hughesa0664b92017-04-18 17:46:52 -0700163extern void valgrind_set_single_stepping (Bool);
sewardj3b290482011-05-06 21:02:55 +0000164
philippe349a3912012-05-23 21:50:36 +0000165/* -------------------------------------------------------------------------- */
166/* ----------------- Examining/modifying data while stopped ----------------- */
167/* -------------------------------------------------------------------------- */
sewardj3b290482011-05-06 21:02:55 +0000168
philippe349a3912012-05-23 21:50:36 +0000169/* Return 1 iff the thread with ID tid is alive. */
170extern int valgrind_thread_alive (unsigned long tid);
sewardj3b290482011-05-06 21:02:55 +0000171
philippe349a3912012-05-23 21:50:36 +0000172/* Allows to controls the thread (current_inferior) used for following
173 valgrind_(fetch|store)_registers calls.
174 If USE_GENERAL,
175 current_inferior is set to general_thread
176 else
177 current_inferior is set to step_thread or else cont_thread.
178 If the above gives no valid thread, then current_inferior is
179 set to the first valid thread. */
180extern void set_desired_inferior (int use_general);
sewardj3b290482011-05-06 21:02:55 +0000181
philippe349a3912012-05-23 21:50:36 +0000182/* Fetch registers from the current_inferior thread.
183 If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO. */
184extern void valgrind_fetch_registers (int regno);
sewardj3b290482011-05-06 21:02:55 +0000185
philippe349a3912012-05-23 21:50:36 +0000186/* Store registers to the current_inferior thread.
187 If REGNO is -1, store all registers; otherwise, store at least REGNO. */
188extern void valgrind_store_registers (int regno);
sewardj3b290482011-05-06 21:02:55 +0000189
sewardj3b290482011-05-06 21:02:55 +0000190
sewardj3b290482011-05-06 21:02:55 +0000191
philippe349a3912012-05-23 21:50:36 +0000192/* Read memory from the inferior process.
193 Read LEN bytes at MEMADDR into a buffer at MYADDR.
194 Returns 0 on success and errno on failure. */
195extern int valgrind_read_memory (CORE_ADDR memaddr,
196 unsigned char *myaddr, int len);
sewardj3b290482011-05-06 21:02:55 +0000197
philippe349a3912012-05-23 21:50:36 +0000198/* Write memory to the inferior process.
199 Write LEN bytes from the buffer at MYADDR to MEMADDR.
200 Returns 0 on success and errno on failure. */
201extern int valgrind_write_memory (CORE_ADDR memaddr,
202 const unsigned char *myaddr, int len);
sewardj3b290482011-05-06 21:02:55 +0000203
sewardj3b290482011-05-06 21:02:55 +0000204
philippe349a3912012-05-23 21:50:36 +0000205/* Insert and remove a hardware watchpoint.
206 Returns 0 on success, -1 on failure and 1 on unsupported.
207 The type is coded as follows:
208 2 = write watchpoint
209 3 = read watchpoint
210 4 = access watchpoint
211*/
212extern int valgrind_insert_watchpoint (char type, CORE_ADDR addr, int len);
213extern int valgrind_remove_watchpoint (char type, CORE_ADDR addr, int len);
sewardj3b290482011-05-06 21:02:55 +0000214
philippe1670b052014-08-15 10:27:52 +0000215/* Get the address of a thread local variable.
216 'tst' is the thread for which thread local address is searched for.
217 'offset' is the offset of the variable in the tls data of the load
218 module identified by 'lm'.
219 'lm' is the link_map address of the loaded module : it is the address
220 of the data structure used by the dynamic linker to maintain various
221 information about a loaded object.
222
223 Returns True if the address of the variable could be found.
224 *tls_addr is then set to this address.
225 Returns False if tls support is not available for this arch, or
florianad4e9792015-07-05 21:53:33 +0000226 if an error occurred. *tls_addr is set to NULL. */
philippe1670b052014-08-15 10:27:52 +0000227extern Bool valgrind_get_tls_addr (ThreadState *tst,
228 CORE_ADDR offset,
229 CORE_ADDR lm,
230 CORE_ADDR *tls_addr);
231
sewardj3b290482011-05-06 21:02:55 +0000232
philippe349a3912012-05-23 21:50:36 +0000233/* -------------------------------------------------------------------------- */
234/* ----------- Utils functions for low level arch specific files ------------ */
235/* -------------------------------------------------------------------------- */
sewardj3b290482011-05-06 21:02:55 +0000236
philippe1670b052014-08-15 10:27:52 +0000237
philippe349a3912012-05-23 21:50:36 +0000238/* returns a pointer to the architecture state corresponding to
239 the provided register set: 0 => normal guest registers,
240 1 => shadow1
241 2 => shadow2
242*/
243extern VexGuestArchState* get_arch (int set, ThreadState* tst);
sewardj3b290482011-05-06 21:02:55 +0000244
245/* like memcpy but first check if content of destination and source
246 differs. If no difference, no copy is done, *mod set to False.
247 If different; copy is done, *mod set to True. */
248extern void* VG_(dmemcpy) ( void *d, const void *s, SizeT sz, Bool *mod );
249
250typedef
251 enum {
252 valgrind_to_gdbserver,
253 gdbserver_to_valgrind} transfer_direction;
254
255// According to dir, calls VG_(dmemcpy)
256// to copy data from/to valgrind to/from gdbserver.
257// If the transferred data differs from what is currently stored,
258// sets *mod to True otherwise set *mod to False.
259extern void VG_(transfer) (void *valgrind,
260 void *gdbserver,
261 transfer_direction dir,
262 SizeT sz,
263 Bool *mod);
264
philippe349a3912012-05-23 21:50:36 +0000265
philippe180a7502014-04-20 13:41:10 +0000266// True means gdbserver can access (internal) Valgrind memory.
267// Otherwise, only the client memory can be accessed.
268extern Bool hostvisibility;
philippe349a3912012-05-23 21:50:36 +0000269
sewardj3b290482011-05-06 21:02:55 +0000270#endif /* TARGET_H */