1 /* Target operations for the Valgrind remote server for GDB.
2    Copyright (C) 2002, 2003, 2004, 2005, 2012
3    Free Software Foundation, Inc.
4    Philippe Waroquiers.
5 
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 
29 #include "pub_core_basics.h"    // Addr
30 #include "server.h"             // CORE_ADDR
31 
32 /* This file defines the architecture independent Valgrind gdbserver
33    high level operations such as read memory, get/set registers, ...
34 
35    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. */
56 extern 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. */
61 extern void initialize_shadow_low (Bool shadow_mode);
62 
63 /* Returns the name of the xml target description file.
64    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. */
69 extern const char* valgrind_target_xml (Bool shadow_mode);
70 
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.  */
78 struct thread_resume
79 {
80   /* 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 
87 /* 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). */
94 extern void valgrind_resume (struct thread_resume *resume_info);
95 
96 /* 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.
103 
104    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.
107 
108    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'.  */
111 extern unsigned char valgrind_wait (char *outstatus);
112 
113 /* When execution is stopped and gdbserver has control, more
114    info about the stop reason can be retrieved using the following
115    functions. */
116 
117 /* 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. */
132 extern Addr valgrind_get_ignore_break_once(void);
133 
134 /* 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. */
138 extern void VG_(set_watchpoint_stop_address) (Addr addr);
139 
140 /* Returns 1 if target was stopped due to a watchpoint hit, 0 otherwise.  */
141 extern int valgrind_stopped_by_watchpoint (void);
142 
143 /* Returns the address associated with the watchpoint that hit, if any;
144    returns 0 otherwise.  */
145 extern CORE_ADDR valgrind_stopped_data_address (void);
146 
147 
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. */
150 extern void gdbserver_syscall_encountered (Bool before, Int sysno);
151 
152 /* >= 0 if valgrind stopped due to syscall, -1 if not stopped due to syscall. */
153 extern Int valgrind_stopped_by_syscall (void);
154 
155 /* if valgrind_stopped_by_syscall() >= 0, tells if stopped before or after
156    syscall. */
157 extern Bool valgrind_stopped_before_syscall (void);
158 
159 /* True if gdbserver is single stepping the valgrind process */
160 extern Bool valgrind_single_stepping (void);
161 
162 /* Set Valgrind in single stepping mode or not according to Bool. */
163 extern void valgrind_set_single_stepping (Bool);
164 
165 /* -------------------------------------------------------------------------- */
166 /* ----------------- Examining/modifying data while stopped ----------------- */
167 /* -------------------------------------------------------------------------- */
168 
169 /* Return 1 iff the thread with ID tid is alive.  */
170 extern int valgrind_thread_alive (unsigned long tid);
171 
172 /* 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. */
180 extern void set_desired_inferior (int use_general);
181 
182 /* Fetch registers from the current_inferior thread.
183    If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO.  */
184 extern void valgrind_fetch_registers (int regno);
185 
186 /* Store registers to the current_inferior thread.
187    If REGNO is -1, store all registers; otherwise, store at least REGNO.  */
188 extern void valgrind_store_registers (int regno);
189 
190 
191 
192 /* 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.  */
195 extern int valgrind_read_memory (CORE_ADDR memaddr,
196                                  unsigned char *myaddr, int len);
197 
198 /* 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.  */
201 extern int valgrind_write_memory (CORE_ADDR memaddr,
202                                   const unsigned char *myaddr, int len);
203 
204 
205 /* 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 */
212 extern int valgrind_insert_watchpoint (char type, CORE_ADDR addr, int len);
213 extern int valgrind_remove_watchpoint (char type, CORE_ADDR addr, int len);
214 
215 /* 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
226    if an error occurred. *tls_addr is set to NULL. */
227 extern Bool valgrind_get_tls_addr (ThreadState *tst,
228                                    CORE_ADDR offset,
229                                    CORE_ADDR lm,
230                                    CORE_ADDR *tls_addr);
231 
232 
233 /* -------------------------------------------------------------------------- */
234 /* ----------- Utils functions for low level arch specific files ------------ */
235 /* -------------------------------------------------------------------------- */
236 
237 
238 /* 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 */
243 extern VexGuestArchState* get_arch (int set, ThreadState* tst);
244 
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. */
248 extern void* VG_(dmemcpy) ( void *d, const void *s, SizeT sz, Bool *mod );
249 
250 typedef
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.
259 extern void  VG_(transfer) (void *valgrind,
260                             void *gdbserver,
261                             transfer_direction dir,
262                             SizeT sz,
263                             Bool *mod);
264 
265 
266 // True means gdbserver can access (internal) Valgrind memory.
267 // Otherwise, only the client memory can be accessed.
268 extern Bool hostvisibility;
269 
270 #endif /* TARGET_H */
271