blob: ab9f755c9075ffaa6744a0a334eb5c5b8f347ff3 [file] [log] [blame]
sewardj3b290482011-05-06 21:02:55 +00001
2/*--------------------------------------------------------------------*/
3/*--- Handle remote gdb protocol. pub_core_gdbserver.h ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
Elliott Hughesed398002017-06-21 14:41:24 -070010 Copyright (C) 2011-2017 Philippe Waroquiers
sewardj3b290482011-05-06 21:02:55 +000011
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 02111-1307, USA.
26
27 The GNU General Public License is contained in the file COPYING.
28*/
29
30#ifndef __PUB_CORE_GDBSERVER_H
31#define __PUB_CORE_GDBSERVER_H
32
33#include "pub_tool_gdbserver.h"
philippe180a7502014-04-20 13:41:10 +000034#include "pub_core_options.h"
florianc91f5842013-09-15 10:42:26 +000035#include "pub_core_threadstate.h" // VgSchedReturnCode
sewardj3b290482011-05-06 21:02:55 +000036
philippecffe2a52014-01-11 13:56:48 +000037/* Return the default path prefix for the named pipes (FIFOs) used by vgdb/gdb
florianb985e2d2011-09-29 03:03:45 +000038 to communicate with valgrind */
philippecffe2a52014-01-11 13:56:48 +000039HChar* VG_(vgdb_prefix_default)(void);
sewardj997546c2011-05-17 18:14:53 +000040
41// After a fork or after an exec, call the below to (possibly) terminate
42// the previous gdbserver and then activate a new gdbserver
43// before any guest code execution, to e.g. allow the user to set
44// breakpoints before execution.
45// If VG_(clo_vgdb) == No, the below has no effect.
46void VG_(gdbserver_prerun_action) (ThreadId tid);
47
philippe180a7502014-04-20 13:41:10 +000048// True if the initialisation of gdbserver was done,
49// i.e. VG_(gdbserver_prerun_action) was called.
50Bool VG_(gdbserver_init_done) (void);
51
52// True if gdbserver should stop execution for the specified stop at reason
53Bool VG_(gdbserver_stop_at) (VgdbStopAt stopat);
54
sewardj3b290482011-05-06 21:02:55 +000055// True if there is some activity from vgdb
56// If it returns True, then extern void VG_(gdbserver) can be called
57// to handle this incoming vgdb request.
58extern Bool VG_(gdbserver_activity) (ThreadId tid);
59
philippe0447bbd2012-10-17 21:32:03 +000060// If connected to GDB, VG_(gdbserver_exit) reports to GDB that the process
61// is about to exit.
62// gdbserver is then stopped (using VG_(gdbserver) (0))
63void VG_(gdbserver_exit) (ThreadId tid, VgSchedReturnCode tids_schedretcode);
64
philippe25583172013-05-09 21:29:23 +000065/* On systems that defines PR_SET_PTRACER, verify if ptrace_scope is
florianb9749a52015-07-24 11:50:12 +000066 is permissive enough for vgdb.
philippe25583172013-05-09 21:29:23 +000067 Otherwise, call set_ptracer.
68 This is especially aimed at Ubuntu >= 10.10 which has added
69 the ptrace_scope context. */
70void VG_(set_ptracer)(void);
sewardj3b290482011-05-06 21:02:55 +000071
72/* Called by low level to insert or remove a break or watch point.
73 Break or watch point implementation is done using help from the tool.
74 break point support implies some (small) specific instrumentation
75 taken in charge for all tools by m_translate.c.
76
77 Write/read/access watchpoint can only be provided by tools which are
78 tracking addressability and/or accessibility of memory
79 (so typically memcheck can provide it). Note that memcheck addressability
80 bits do not differentiate between read and write accessibility.
81 However, when accessing unaddressable byte, memcheck can differentiate
82 reads from write, thereby providing read/write or access watchpoints.
83
84 Note that gdbserver assumes that software breakpoint is supported
85 (as this will be done by re-instrumenting the code).
florianad4e9792015-07-05 21:53:33 +000086 Note that len is ignored for software breakpoints. hardware_breakpoint
sewardj3b290482011-05-06 21:02:55 +000087 are not supported.
88
89 Returns True if the point has properly been inserted or removed
90 Returns False otherwise. */
91Bool VG_(gdbserver_point) (PointKind kind, Bool insert,
92 Addr addr, int len);
93
philippe02320982013-05-02 22:06:31 +000094/* True if there is a breakpoint at addr. */
95Bool VG_(has_gdbserver_breakpoint) (Addr addr);
96
sewardj3b290482011-05-06 21:02:55 +000097/* Entry point invoked by vgdb when it uses ptrace to cause a gdbserver
98 invocation. A magic value is passed by vgdb in check as a verification
99 that the call has been properly pushed by vgdb. */
100extern void VG_(invoke_gdbserver) ( int check );
101
philippe0447bbd2012-10-17 21:32:03 +0000102// To be called by core (m_signals.c) before delivering a signal.
philippe2d1f2562014-09-19 08:57:29 +0000103// Returns False if gdb user asks to not pass the signal to the client.
104// Returns True if signal must be passed to the client, either because
105// no gdb is connected, or gdb instructs to pass the signal.
sewardj3b290482011-05-06 21:02:55 +0000106// Note that if the below returns True, the signal might
107// still be ignored if this is the action desired by the
philippeb3014692015-05-17 13:38:25 +0000108// guest program. Using GDB, the user can also modify the signal to be
109// reported (e.g. changing the signo to pass to the guest).
110// If this function returns True, m_signals.c should deliver the signal
111// info as modified by VG_(gdbserver_report_signal).
112// If this function returns False, no signal should be reported.
113extern Bool VG_(gdbserver_report_signal) (vki_siginfo_t *info, ThreadId tid);
philippe2d1f2562014-09-19 08:57:29 +0000114
115// If no gdb is connected yet, wait for a gdb to connect and report
116// this (supposedly) fatal signal.
117// If a gdb is already connected, this does nothing (as normally
118// the signal was already reported to the already connected gdb).
philippeb3014692015-05-17 13:38:25 +0000119extern void VG_(gdbserver_report_fatal_signal) (const vki_siginfo_t *info,
120 ThreadId tid);
sewardj3b290482011-05-06 21:02:55 +0000121
Elliott Hughesa0664b92017-04-18 17:46:52 -0700122// To be called by core before and after a client syscall.
123// If GDB has asked to observe the syscall, control will be given to GDB.
124// When Before is True, it is a report before the syscall,
125// False means a report after the syscall.
126extern void VG_(gdbserver_report_syscall) (Bool before, UWord sysno,
127 ThreadId tid);
128
philippe46207652013-01-20 17:11:58 +0000129/* Entry point invoked by scheduler.c to execute the request
130 VALGRIND_CLIENT_MONITOR_COMMAND.
131 Returns True if command was not recognised. */
132extern Bool VG_(client_monitor_command) (HChar* cmd);
133
sewardj3b290482011-05-06 21:02:55 +0000134/* software_breakpoint, single step and jump support ------------------------*/
135/* VG_(instrument_for_gdbserver_if_needed) allows to do "standard and easy"
136 instrumentation for gdbserver.
137 VG_(instrument_for_gdbserver_if_needed) does the following:
138 * checks if gdbserver instrumentation is needed for vge.
139 * if no gdbserver instrumentation needed,
140 returns sb_in
141 * otherwise
142 It will instrument sb_in to allow gdbserver to properly
143 handle breakpoints and single_stepping in sb_in.
144 All the target jumps of sb_in will also be invalidated
145 if these are not yet instrumented for gdbserver.
146 This allows to have single_step working, using a lazily
147 translation of the blocks which are being single stepped
148 in.
149
150 The typical usage of this function is to call it on the block
151 instrumented by the tool instrument function i.e. :
152 return VG_(instrument_for_gdbserver_if_needed) (sb_out,
153 layout,
154 vge,
155 gWordTy,
156 hWordTy);
157 where sb_out is the block instrumented by the tool.
158
159 If the block contains a call to a dirty helper that indirectly
160 calls gdbserver, then this dirty helper can (indirectly) change
161 the IP. This implies to jump to this IP after the call to
162 gdbserver. */
163extern IRSB* VG_(instrument_for_gdbserver_if_needed)
164 (IRSB* sb_in, /* block to be instrumented */
florian3c0c9472014-09-24 12:06:55 +0000165 const VexGuestLayout* layout,
166 const VexGuestExtents* vge,
sewardj3b290482011-05-06 21:02:55 +0000167 IRType gWordTy, IRType hWordTy);
168
169/* reason for which gdbserver connection must be finished */
170typedef
171 enum {
172 orderly_finish,
173 reset_after_error,
174 reset_after_fork} FinishReason;
175
176/* output various gdbserver statistics and status. */
177extern void VG_(gdbserver_status_output)(void);
178
179/* Shared structure between vgdb and the process running
180 under valgrind.
181 We define two variants: a 32 bit and a 64 bit.
182 The valgrind process will use the appropriate size,
183 according to the architecture.
184 vgdb will use what the valgrind process is using. */
185/* The below takes care that sizes will be 32 or 64 bits,
186 whatever the architecture. A.o., vgdb.c cannot use directly
187 the types from pub_core_threadstate.h as we want vgdb.c to
188 be independent of the arch it is debugging in case of bi-arch
189 Valgrind (e.g. x86 and amd64). So, the valgrind process must
190 give all the needed info/offset to vgdb in the below structure. */
191
192typedef
193 struct {
sewardj3b290482011-05-06 21:02:55 +0000194 // nr of bytes vgdb has written to valgrind
195 volatile int written_by_vgdb;
196 // nr of bytes seen by valgrind
197 volatile int seen_by_valgrind;
198
199 // address at which gdbserver can be invoked
200 Addr32 invoke_gdbserver;
201
202 // address of VG_(threads) and various sizes
203 // and offset needed by vgdb.
204 Addr32 threads;
philippeb12f5022015-02-09 21:30:58 +0000205 int vg_n_threads;
sewardj3b290482011-05-06 21:02:55 +0000206 int sizeof_ThreadState;
207 int offset_status;
208 int offset_lwpid;
philippe57864202012-02-22 19:47:27 +0000209
210 // PID of the vgdb that last connected to the Valgrind gdbserver.
211 // It will be set by vgdb after connecting.
212 int vgdb_pid;
sewardj3b290482011-05-06 21:02:55 +0000213 } VgdbShared32;
214
215/* Same as VgdbShared32 but for 64 bits arch. */
216typedef
217 struct {
sewardj3b290482011-05-06 21:02:55 +0000218 volatile int written_by_vgdb;
219 volatile int seen_by_valgrind;
220
221 Addr64 invoke_gdbserver;
222
223 Addr64 threads;
philippeb12f5022015-02-09 21:30:58 +0000224 int vg_n_threads;
sewardj3b290482011-05-06 21:02:55 +0000225 int sizeof_ThreadState;
226 int offset_status;
227 int offset_lwpid;
philippe57864202012-02-22 19:47:27 +0000228
229 int vgdb_pid;
sewardj3b290482011-05-06 21:02:55 +0000230 } VgdbShared64;
231
232// The below typedef makes the life of valgrind easier.
Elliott Hughesed398002017-06-21 14:41:24 -0700233// vgdb must however work explicitly with the specific 32 or 64 bits version.
sewardj3b290482011-05-06 21:02:55 +0000234
235#if VEX_HOST_WORDSIZE == 8
236typedef VgdbShared64 VgdbShared;
237#elif VEX_HOST_WORDSIZE == 4
238typedef VgdbShared32 VgdbShared;
239#else
240# error "unexpected wordsize"
241#endif
242
243
244#endif // __PUB_CORE_GDBSERVER_H
245/*--------------------------------------------------------------------*/
246/*--- end ---*/
247/*--------------------------------------------------------------------*/