pub_tool_gdbserver.h revision 0f157ddb404bcde7815a1c5bf2d7e41c114f3d73
14341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
24341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin/*--------------------------------------------------------------------*/
34341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin/*--- Handle remote gdb protocol.             pub_tool_gdbserver.h ---*/
44341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin/*--------------------------------------------------------------------*/
54341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
64341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin/*
74341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   This file is part of Valgrind, a dynamic binary instrumentation
84341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   framework.
94341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
104341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   Copyright (C) 2011-2013 Philippe Waroquiers
114341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
124341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   This program is free software; you can redistribute it and/or
134341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   modify it under the terms of the GNU General Public License as
144341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   published by the Free Software Foundation; either version 2 of the
154341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   License, or (at your option) any later version.
164341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
174341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   This program is distributed in the hope that it will be useful, but
184341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   WITHOUT ANY WARRANTY; without even the implied warranty of
194341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
204341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   General Public License for more details.
214341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
224341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   You should have received a copy of the GNU General Public License
234341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   along with this program; if not, write to the Free Software
244341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
254341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   02111-1307, USA.
264341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
274341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   The GNU General Public License is contained in the file COPYING.
284341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin*/
294341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
304341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin#ifndef __PUB_TOOL_GDBSERVER_H
314341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin#define __PUB_TOOL_GDBSERVER_H
324341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
334341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin#include "pub_tool_basics.h"   // VG_ macro
344341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin#include "libvex.h"
354341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin#include "libvex_ir.h"
364341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
374341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin//--------------------------------------------------------------------
384341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// PURPOSE: This module provides the support to have a gdb
394341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// connecting to a valgrind process using remote gdb protocol. It provides
404341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin//  * A function to allow a tool (or the valgrind core) to
414341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin//    wait for a gdb to connect and then handle gdb commands.
424341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin//    Typically, this can be used to let the user debug the process
434341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin//    when valgrind reports an error.
444341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin//  * A function allowing to instrument the code to support gdb breakpoints.
454341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin//  * A function allowing the tool to support watchpoints.
464341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin//  * A utility function to help implementing the processing of the
474341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin//    gdb_monitor_command strings.
484341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
494341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
504341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// Function to be used by tool or coregrind to allow a gdb to connect
514341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// to this process.
524341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// Calling VG_(gdbserver) with tid > 0 means to let a debugger attach
534341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// to the valgrind process. gdbserver will report to gdb that the
544341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// process stopped in thread tid.
554341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// Calling VG_(gdbserver) with tid == 0 indicates to close
564341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// the connection with GDB (if still open) and stop gdbserver.
574341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin//--------------------------------------------------------------------
584341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levinextern void VG_(gdbserver) ( ThreadId tid );
594341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
604341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin/* VG_(dyn_vgdb_error) gets its initial value from
614341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   VG_(clo_vgdb_error).  It can be changed after initial command
624341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   processing in order to enable/disable the call to VG_(gdbserver) in
634341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   m_errormgr.c.  The main reasons to change the below is either
644341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   because the user updates it via a monitor command or to
654341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   (temporarily) avoid calling gdbserver for error reporting during
664341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   monitor command handling.
674341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin*/
684341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levinextern Int VG_(dyn_vgdb_error);
694341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
704341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin/* defines the various kinds of breakpoints that gdbserver
714341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   might ask to insert/remove. Note that the below matches
724341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   the gdbserver protocol definition. The level of support
734341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   of the various breakpoint kinds depends on the tool.
744341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
754341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   For the moment, it is unclear how a tool would implement
764341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   hardware_breakpoint in valgrind  :).
774341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
784341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   software_breakpoint implies some (small) specific
794341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   instrumentation to be done for gdbserver. This instrumentation
804341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   is implemented for all tools in m_translate.c.
814341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
824341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   write/read/access watchpoints can only be done by tools
834341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   which are maintaining some notion of address accessibility
844341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   as part of their instrumentation. watchpoints can then
854341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   be done by marking the watched address(es) as not accessible.
864341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   But instead of giving back an error (or whatever the tool
874341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   wants to do with unaccessible mechanism), the tool must then
884341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   just call gdbserver. See memcheck for an example of reusing
894341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   accessibility for watchpoint support.
904341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin*/
914341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levintypedef
924341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   enum {
934341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin      software_breakpoint,
944341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin      hardware_breakpoint,
954341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin      write_watchpoint,
964341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin      read_watchpoint,
974341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin      access_watchpoint } PointKind;
984341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levinextern const HChar* VG_(ppPointKind) (PointKind kind);
994341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
1004341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
1014341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin/* watchpoint support --------------------------------------*/
1024341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin/* True if one or more bytes in [addr, addr+len[ are being watched by
1034341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   gdbserver for write or read or access.
1044341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   In addition, VG_(is_watched) will invoke gdbserver if
1054341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   the access provided by the tool matches the watchpoint kind.
1064341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   For this, the tool must pass the kind of access it has detected:
1074341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin      write_watchpoint indicates the tool has detected a write
1084341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin      read_watchpoint indicates the tool has detected a read
1094341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin      access_watchpoint indicates the tool has detected an access but does
1104341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin      not know if this is a read or a write
1114341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin*/
1124341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levinextern Bool VG_(is_watched)(PointKind kind, Addr addr, Int szB);
1134341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
1144341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levinextern void VG_(needs_watchpoint) (
1154341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   // indicates the given Addr/len is being watched (insert)
1164341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   // or not watched anymore (! insert).
1174341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   // gdbserver will maintain the list of watched addresses.
1184341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   // The tool can use VG_(is_watched) to verify if an
1194341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   // access to an Addr is in one of the watched intervals.
1204341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   // Must return True if the watchpoint has been properly inserted or
1214341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   // removed. False if not supported.
1224341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   // Note that an address can only be watched for a single kind.
1234341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   // The tool must be ready to be called successively with
1244341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   // multiple kinds for the same addr and len and with
1254341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   // different kinds. The last kind must replace the previous values.
1264341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   // Behaviour with multiple watches having overlapping addr+len
1274341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   // is undefined.
1284341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   Bool (*watchpoint) (PointKind kind, Bool insert, Addr addr, SizeT len)
1294341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin);
1304341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
1314341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
1324341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// can be used during the processing of the VG_USERREQ__GDB_MONITOR_COMMAND
1334341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// tool client request to output information to gdb or vgdb.
1344341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// The output of VG_(gdb_printf) is not subject to 'output control'
1354341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// by the user: e.g. the monitor command 'v.set log_output' has no effect.
1364341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// The output of VG_(gdb_printf) is given to gdb/vgdb. The only case
1374341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// in which this output is not given to gdb/vgdb is when the connection
1384341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// with gdb/vgdb has been lost : in such a case, output is written
1394341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// to the valgrind log output.
1404341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// To produce some output which is subject to user output control via
1414341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// monitor command v.set gdb_output or mixed output, use VG_(printf)
1424341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// or VG_(umsg) or similar.
1434341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// Typically, VG_(gdb_printf) has to be used when there is no point
1444341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// having this output in the output log of Valgrind. Examples
1454341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// is the monitor help output, or if vgdb is used to implement
1464341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin// 'tool control scripts' such as callgrind_control.
1474341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levinextern UInt VG_(gdb_printf) ( const HChar *format, ... ) PRINTF_CHECK(1, 2);
1484341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
1494341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin/* Utility functions to (e.g.) parse gdb monitor commands.
1504341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
1514341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   keywords is a set of keywords separated by a space
1524341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   keyword_id will search for the keyword starting with the string input_word
1534341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   and return its position.
1544341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   It returns -1 if no keyword matches.
1554341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   It returns -2 if two or more keywords are starting with input_word
1564341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin                 and none of these matches exactly input_word
1574341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   Example with keywords = "hello world here is hell" :
1584341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   input_word    result
1594341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   ----------    ------
1604341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   paradise   => -1
1614341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   i          =>  3
1624341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   hell       =>  4
1634341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   hel        => -2
1644341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   ishtar     => -1
1654341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
1664341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   report indicates when to output an error msg with VG_(gdb_printf).
1674341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   kwd_report_none : no error is reported.
1684341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   kwd_report_all : the error msg will show all possible keywords
1694341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   kwd_report_duplicated_matches : the error msg will show only the
1704341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin     ambiguous matches.
1714341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin*/
1724341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levintypedef
1734341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   enum {
1744341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin      kwd_report_none,
1754341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin      kwd_report_all,
1764341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin      kwd_report_duplicated_matches } kwd_report_error;
1774341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levinextern Int VG_(keyword_id) (const HChar* keywords, const HChar* input_word,
1784341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin                            kwd_report_error report);
1794341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
1804341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin/* Extract an address and (optionally) a size from the string
1814341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   currently being parsed by strtok_r (see pub_tool_libcbase.h).
1824341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   If no size in the string, keeps the current value of szB.
1834341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   Returns address 0 and szB 0 if there is an error.  Reports to the
1844341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin   user problems via VG_(gdb_printf).  */
1854341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levinextern void VG_(strtok_get_address_and_size) (Addr* address,
1864341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin                                              SizeT* szB,
1874341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin                                              HChar **ssaveptr);
1884341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
1894341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin#endif   // __PUB_TOOL_GDBSERVER_H
1904341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin
1914341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin/*--------------------------------------------------------------------*/
1924341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin/*--- end                                                          ---*/
1934341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin/*--------------------------------------------------------------------*/
1944341373f2c651e058a14a12080f9fc668c9bfb68Dmitry V. Levin