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 /* True if gdbserver is single stepping the valgrind process */
148 extern Bool valgrind_single_stepping(void);
149 
150 /* Set Valgrind in single stepping mode or not according to Bool. */
151 extern void valgrind_set_single_stepping(Bool);
152 
153 /* -------------------------------------------------------------------------- */
154 /* ----------------- Examining/modifying data while stopped ----------------- */
155 /* -------------------------------------------------------------------------- */
156 
157 /* Return 1 iff the thread with ID tid is alive.  */
158 extern int valgrind_thread_alive (unsigned long tid);
159 
160 /* Allows to controls the thread (current_inferior) used for following
161    valgrind_(fetch|store)_registers calls.
162    If USE_GENERAL,
163      current_inferior is set to general_thread
164    else
165      current_inferior is set to step_thread or else cont_thread.
166    If the above gives no valid thread, then current_inferior is
167    set to the first valid thread. */
168 extern void set_desired_inferior (int use_general);
169 
170 /* Fetch registers from the current_inferior thread.
171    If REGNO is -1, fetch all registers; otherwise, fetch at least REGNO.  */
172 extern void valgrind_fetch_registers (int regno);
173 
174 /* Store registers to the current_inferior thread.
175    If REGNO is -1, store all registers; otherwise, store at least REGNO.  */
176 extern void valgrind_store_registers (int regno);
177 
178 
179 
180 /* Read memory from the inferior process.
181    Read LEN bytes at MEMADDR into a buffer at MYADDR.
182    Returns 0 on success and errno on failure.  */
183 extern int valgrind_read_memory (CORE_ADDR memaddr,
184                                  unsigned char *myaddr, int len);
185 
186 /* Write memory to the inferior process.
187    Write LEN bytes from the buffer at MYADDR to MEMADDR.
188    Returns 0 on success and errno on failure.  */
189 extern int valgrind_write_memory (CORE_ADDR memaddr,
190                                   const unsigned char *myaddr, int len);
191 
192 
193 /* Insert and remove a hardware watchpoint.
194    Returns 0 on success, -1 on failure and 1 on unsupported.
195    The type is coded as follows:
196    2 = write watchpoint
197    3 = read watchpoint
198    4 = access watchpoint
199 */
200 extern int valgrind_insert_watchpoint (char type, CORE_ADDR addr, int len);
201 extern int valgrind_remove_watchpoint (char type, CORE_ADDR addr, int len);
202 
203 /* Get the address of a thread local variable.
204    'tst' is the thread for which thread local address is searched for.
205    'offset' is the offset of the variable in the tls data of the load
206    module identified by 'lm'.
207    'lm' is the link_map address of the loaded  module : it is the address
208    of the data structure used by the dynamic linker to maintain various
209    information about a loaded object.
210 
211    Returns True if the address of the variable could be found.
212       *tls_addr is then set to this address.
213    Returns False if tls support is not available for this arch, or
214    if an error occured. *tls_addr is set to NULL. */
215 extern Bool valgrind_get_tls_addr (ThreadState *tst,
216                                    CORE_ADDR offset,
217                                    CORE_ADDR lm,
218                                    CORE_ADDR *tls_addr);
219 
220 
221 /* -------------------------------------------------------------------------- */
222 /* ----------- Utils functions for low level arch specific files ------------ */
223 /* -------------------------------------------------------------------------- */
224 
225 
226 /* returns a pointer to the architecture state corresponding to
227    the provided register set: 0 => normal guest registers,
228                               1 => shadow1
229                               2 => shadow2
230 */
231 extern VexGuestArchState* get_arch (int set, ThreadState* tst);
232 
233 /* like memcpy but first check if content of destination and source
234    differs. If no difference, no copy is done, *mod set to False.
235    If different; copy is done, *mod set to True. */
236 extern void* VG_(dmemcpy) ( void *d, const void *s, SizeT sz, Bool *mod );
237 
238 typedef
239    enum {
240       valgrind_to_gdbserver,
241       gdbserver_to_valgrind} transfer_direction;
242 
243 // According to dir, calls VG_(dmemcpy)
244 // to copy data from/to valgrind to/from gdbserver.
245 // If the transferred data differs from what is currently stored,
246 // sets *mod to True otherwise set *mod to False.
247 extern void  VG_(transfer) (void *valgrind,
248                             void *gdbserver,
249                             transfer_direction dir,
250                             SizeT sz,
251                             Bool *mod);
252 
253 
254 // True means gdbserver can access (internal) Valgrind memory.
255 // Otherwise, only the client memory can be accessed.
256 extern Bool hostvisibility;
257 
258 #endif /* TARGET_H */
259