1b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
2b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/*--------------------------------------------------------------------*/
3b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/*--- Handle remote gdb protocol.             pub_tool_gdbserver.h ---*/
4b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/*--------------------------------------------------------------------*/
5b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
6b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/*
7b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   This file is part of Valgrind, a dynamic binary instrumentation
8b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   framework.
9b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
10663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   Copyright (C) 2011-2012 Philippe Waroquiers
11b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
12b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   This program is free software; you can redistribute it and/or
13b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   modify it under the terms of the GNU General Public License as
14b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   published by the Free Software Foundation; either version 2 of the
15b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   License, or (at your option) any later version.
16b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
17b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   This program is distributed in the hope that it will be useful, but
18b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   WITHOUT ANY WARRANTY; without even the implied warranty of
19b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   General Public License for more details.
21b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
22b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   You should have received a copy of the GNU General Public License
23b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   along with this program; if not, write to the Free Software
24b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   02111-1307, USA.
26b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
27b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   The GNU General Public License is contained in the file COPYING.
28b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov*/
29b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
30b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#ifndef __PUB_TOOL_GDBSERVER_H
31b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#define __PUB_TOOL_GDBSERVER_H
32b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
33b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "libvex.h"
34b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "libvex_ir.h"
35b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
36b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov//--------------------------------------------------------------------
37b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// PURPOSE: This module provides the support to have a gdb
38b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// connecting to a valgrind process using remote gdb protocol. It provides
39b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov//  * A function to allow a tool (or the valgrind core) to
40b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov//    wait for a gdb to connect and then handle gdb commands.
41b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov//    Typically, this can be used to let the user debug the process
42b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov//    when valgrind reports an error.
43b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov//  * A function allowing to instrument the code to support gdb breakpoints.
44b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov//  * A function allowing the tool to support watchpoints.
45b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov//  * A utility function to help implementing the processing of the
46b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov//    gdb_monitor_command strings.
47b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
48b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
49b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// Function to be used by tool or coregrind to allow a gdb to connect
50b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// to this process.
51b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// Calling VG_(gdbserver) with tid > 0 means to let a debugger attach
52b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// to the valgrind process. gdbserver will report to gdb that the
53b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// process stopped in thread tid.
54b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// tid == 0 indicates to stop gdbserver and report to gdb
55b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// that the valgrind-ified process has exited.
56b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov//--------------------------------------------------------------------
57b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern void VG_(gdbserver) ( ThreadId tid );
58b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
59b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* VG_(dyn_vgdb_error) gets its initial value from
60b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   VG_(clo_vgdb_error).  It can be changed after initial command
61b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   processing in order to enable/disable the call to VG_(gdbserver) in
62b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   m_errormgr.c.  The main reasons to change the below is either
63b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   because the user updates it via a monitor command or to
64b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   (temporarily) avoid calling gdbserver for error reporting during
65b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   monitor command handling.
66b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov*/
67b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern Int VG_(dyn_vgdb_error);
68b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
69b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* defines the various kinds of breakpoints that gdbserver
70b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   might ask to insert/remove. Note that the below matches
71b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   the gdbserver protocol definition. The level of support
72b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   of the various breakpoint kinds depends on the tool.
73b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
74b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   For the moment, it is unclear how a tool would implement
75b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   hardware_breakpoint in valgrind  :).
76b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
77b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   software_breakpoint implies some (small) specific
78b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   instrumentation to be done for gdbserver. This instrumentation
79b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   is implemented for all tools in m_translate.c.
80b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
81b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   write/read/access watchpoints can only be done by tools
82b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   which are maintaining some notion of address accessibility
83b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   as part of their instrumentation. watchpoints can then
84b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   be done by marking the watched address(es) as not accessible.
85b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   But instead of giving back an error (or whatever the tool
86b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   wants to do with unaccessible mechanism), the tool must then
87b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   just call gdbserver. See memcheck for an example of reusing
88b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   accessibility for watchpoint support.
89b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov*/
90b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovtypedef
91b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   enum {
92b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      software_breakpoint,
93b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      hardware_breakpoint,
94b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      write_watchpoint,
95b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      read_watchpoint,
96b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      access_watchpoint } PointKind;
97b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern char* VG_(ppPointKind) (PointKind kind);
98b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
99b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
100b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* watchpoint support --------------------------------------*/
101b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* True if one or more bytes in [addr, addr+len[ are being watched by
102b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   gdbserver for write or read or access.
103b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   In addition, VG_(is_watched) will invoke gdbserver if
104b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   the access provided by the tool matches the watchpoint kind.
105b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   For this, the tool must pass the kind of access it has detected:
106b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      write_watchpoint indicates the tool has detected a write
107b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      read_watchpoint indicates the tool has detected a read
108b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      access_watchpoint indicates the tool has detected an access but does
109b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      not know if this is a read or a write
110b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov*/
111b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern Bool VG_(is_watched)(PointKind kind, Addr addr, Int szB);
112b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
113b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern void VG_(needs_watchpoint) (
114b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   // indicates the given Addr/len is being watched (insert)
115b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   // or not watched anymore (! insert).
116b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   // gdbserver will maintain the list of watched addresses.
117b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   // The tool can use VG_(is_watched) to verify if an
118b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   // access to an Addr is in one of the watched intervals.
119b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   // Must return True if the watchpoint has been properly inserted or
120b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   // removed. False if not supported.
121b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   // Note that an address can only be watched for a single kind.
122b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   // The tool must be ready to be called successively with
123b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   // multiple kinds for the same addr and len and with
124b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   // different kinds. The last kind must replace the previous values.
125b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   // Behaviour with multiple watches having overlapping addr+len
126b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   // is undefined.
127b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Bool (*watchpoint) (PointKind kind, Bool insert, Addr addr, SizeT len)
128b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov);
129b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
130b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
131b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// can be used during the processing of the VG_USERREQ__GDB_MONITOR_COMMAND
132b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// tool client request to output information to gdb or vgdb.
133b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern UInt VG_(gdb_printf) ( const HChar *format, ... ) PRINTF_CHECK(1, 2);
134b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
135b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Utility functions to (e.g.) parse gdb monitor commands.
136b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
137b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   keywords is a set of keywords separated by a space
138b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   keyword_id will search for the keyword starting with the string input_word
139b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   and return its position.
140b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   It returns -1 if no keyword matches.
141b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   It returns -2 if two or more keywords are starting with input_word
142b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                 and none of these matches exactly input_word
143b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Example with keywords = "hello world here is hell" :
144b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   input_word    result
145b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   ----------    ------
146b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   paradise   => -1
147b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   i          =>  3
148b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   hell       =>  4
149b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   hel        => -2
150b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   ishtar     => -1
151b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
152b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   report indicates when to output an error msg with VG_(gdb_printf).
153b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   kwd_report_none : no error is reported.
154b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   kwd_report_all : the error msg will show all possible keywords
155b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   kwd_report_duplicated_matches : the error msg will show only the
156b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov     ambiguous matches.
157b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov*/
158b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovtypedef
159b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   enum {
160b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      kwd_report_none,
161b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      kwd_report_all,
162b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov      kwd_report_duplicated_matches } kwd_report_error;
163b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern Int VG_(keyword_id) (Char* keywords, Char* input_word,
164b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                            kwd_report_error report);
165b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
166b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/* Extract an address and (optionally) a size from the string
167b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   currently being parsed by strtok_r (see pub_tool_libcbase.h).
168b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   If no size in the string, keeps the current value of szB.
169b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Returns address 0 and szB 0 if there is an error.  Reports to the
170b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   user problems via VG_(gdb_printf).  */
171b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovextern void VG_(strtok_get_address_and_size) (Addr* address,
172b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                                              SizeT* szB,
173b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov                                              Char **ssaveptr);
174b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
175b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif   // __PUB_TOOL_GDBSERVER_H
176b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov
177b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/*--------------------------------------------------------------------*/
178b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/*--- end                                                          ---*/
179b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov/*--------------------------------------------------------------------*/
180