njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- DebugInfo. pub_tool_debuginfo.h ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
| 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
| 9 | |
njn | 9f20746 | 2009-03-10 22:02:09 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2009 Julian Seward |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 11 | jseward@acm.org |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
| 31 | #ifndef __PUB_TOOL_DEBUGINFO_H |
| 32 | #define __PUB_TOOL_DEBUGINFO_H |
| 33 | |
| 34 | /*====================================================================*/ |
| 35 | /*=== Obtaining debug information ===*/ |
| 36 | /*====================================================================*/ |
| 37 | |
| 38 | /* Get the file/function/line number of the instruction at address |
| 39 | 'a'. For these four, if debug info for the address is found, it |
| 40 | copies the info into the buffer/UInt and returns True. If not, it |
| 41 | returns False and nothing is copied. VG_(get_fnname) always |
| 42 | demangles C++ function names. VG_(get_fnname_w_offset) is the |
| 43 | same, except it appends "+N" to symbol names to indicate offsets. */ |
| 44 | extern Bool VG_(get_filename) ( Addr a, Char* filename, Int n_filename ); |
| 45 | extern Bool VG_(get_fnname) ( Addr a, Char* fnname, Int n_fnname ); |
| 46 | extern Bool VG_(get_linenum) ( Addr a, UInt* linenum ); |
| 47 | extern Bool VG_(get_fnname_w_offset) |
| 48 | ( Addr a, Char* fnname, Int n_fnname ); |
| 49 | |
sewardj | 7cee6f9 | 2005-06-13 17:39:06 +0000 | [diff] [blame] | 50 | /* This one is the most general. It gives filename, line number and |
| 51 | optionally directory name. filename and linenum may not be NULL. |
| 52 | dirname may be NULL, meaning that the caller does not want |
| 53 | directory name info, in which case dirname_available must also be |
| 54 | NULL. If dirname is non-null, directory info is written to it, if |
| 55 | it is available; if not available, '\0' is written to the first |
| 56 | byte. In either case *dirname_available is set to indicate whether |
| 57 | or not directory information was available. |
| 58 | |
| 59 | Returned value indicates whether any filename/line info could be |
| 60 | found. */ |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 61 | extern Bool VG_(get_filename_linenum) |
sewardj | 7cee6f9 | 2005-06-13 17:39:06 +0000 | [diff] [blame] | 62 | ( Addr a, |
| 63 | /*OUT*/Char* filename, Int n_filename, |
| 64 | /*OUT*/Char* dirname, Int n_dirname, |
| 65 | /*OUT*/Bool* dirname_available, |
| 66 | /*OUT*/UInt* linenum ); |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 67 | |
| 68 | /* Succeeds only if we find from debug info that 'a' is the address of the |
| 69 | first instruction in a function -- as opposed to VG_(get_fnname) which |
| 70 | succeeds if we find from debug info that 'a' is the address of any |
| 71 | instruction in a function. Use this to instrument the start of |
| 72 | a particular function. Nb: if an executable/shared object is stripped |
| 73 | of its symbols, this function will not be able to recognise function |
| 74 | entry points within it. */ |
| 75 | extern Bool VG_(get_fnname_if_entry) ( Addr a, Char* fnname, Int n_fnname ); |
| 76 | |
njn | 6882443 | 2009-02-10 06:48:00 +0000 | [diff] [blame] | 77 | typedef |
| 78 | enum { |
| 79 | Vg_FnNameNormal, // A normal function. |
| 80 | Vg_FnNameMain, // "main" |
| 81 | Vg_FnNameBelowMain // Something below "main", eg. __libc_start_main. |
| 82 | } Vg_FnNameKind; // Such names are often filtered. |
| 83 | |
| 84 | /* Indicates what kind of fnname it is. */ |
| 85 | extern Vg_FnNameKind VG_(get_fnname_kind) ( Char* name ); |
| 86 | |
| 87 | /* Like VG_(get_fnname_kind), but takes a code address. */ |
| 88 | extern Vg_FnNameKind VG_(get_fnname_kind_from_IP) ( Addr ip ); |
| 89 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 90 | /* Looks up data_addr in the collection of data symbols, and if found |
| 91 | puts its name (or as much as will fit) into dname[0 .. n_dname-1], |
| 92 | which is guaranteed to be zero terminated. Also data_addr's offset |
| 93 | from the symbol start is put into *offset. */ |
| 94 | extern Bool VG_(get_datasym_and_offset)( Addr data_addr, |
| 95 | /*OUT*/Char* dname, Int n_dname, |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 96 | /*OUT*/PtrdiffT* offset ); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 97 | |
sewardj | 5d9dc75 | 2009-07-15 14:52:18 +0000 | [diff] [blame] | 98 | /* Try to form some description of DATA_ADDR by looking at the DWARF3 |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 99 | debug info we have. This considers all global variables, and all |
sewardj | 5d9dc75 | 2009-07-15 14:52:18 +0000 | [diff] [blame] | 100 | frames in the stacks of all threads. Result is written at the ends |
| 101 | of DNAME{1,2}V, which are XArray*s of HChar, that have been |
| 102 | initialised by the caller, and True is returned. If no description |
| 103 | is created, False is returned. Regardless of the return value, |
| 104 | DNAME{1,2}V are guaranteed to be zero terminated after the call. |
| 105 | |
| 106 | Note that after the call, DNAME{1,2} may have more than one |
| 107 | trailing zero, so callers should establish the useful text length |
| 108 | using VG_(strlen) on the contents, rather than VG_(sizeXA) on the |
| 109 | XArray itself. |
| 110 | */ |
| 111 | Bool VG_(get_data_description)( |
| 112 | /*MOD*/ void* /* really, XArray* of HChar */ dname1v, |
| 113 | /*MOD*/ void* /* really, XArray* of HChar */ dname2v, |
| 114 | Addr data_addr |
| 115 | ); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 116 | |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 117 | /* Succeeds if the address is within a shared object or the main executable. |
| 118 | It doesn't matter if debug info is present or not. */ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 119 | extern Bool VG_(get_objname) ( Addr a, Char* objname, Int n_objname ); |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 120 | |
| 121 | /* Puts into 'buf' info about the code address %eip: the address, function |
| 122 | name (if known) and filename/line number (if known), like this: |
| 123 | |
| 124 | 0x4001BF05: realloc (vg_replace_malloc.c:339) |
| 125 | |
| 126 | 'n_buf' gives length of 'buf'. Returns 'buf'. |
| 127 | */ |
| 128 | extern Char* VG_(describe_IP)(Addr eip, Char* buf, Int n_buf); |
| 129 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 130 | |
| 131 | /* Get an XArray of StackBlock which describe the stack (auto) blocks |
| 132 | for this ip. The caller is expected to free the XArray at some |
| 133 | point. If 'arrays_only' is True, only array-typed blocks are |
| 134 | returned; otherwise blocks of all types are returned. */ |
| 135 | |
| 136 | typedef |
| 137 | struct { |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 138 | PtrdiffT base; /* offset from sp or fp */ |
| 139 | SizeT szB; /* size in bytes */ |
| 140 | Bool spRel; /* True => sp-rel, False => fp-rel */ |
| 141 | Bool isVec; /* does block have an array type, or not? */ |
| 142 | HChar name[16]; /* first 15 chars of name (asciiz) */ |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 143 | } |
| 144 | StackBlock; |
| 145 | |
| 146 | extern void* /* really, XArray* of StackBlock */ |
| 147 | VG_(di_get_stack_blocks_at_ip)( Addr ip, Bool arrays_only ); |
| 148 | |
| 149 | |
| 150 | /* Get an array of GlobalBlock which describe the global blocks owned |
| 151 | by the shared object characterised by the given di_handle. Asserts |
| 152 | if the handle is invalid. The caller is responsible for freeing |
| 153 | the array at some point. If 'arrays_only' is True, only |
| 154 | array-typed blocks are returned; otherwise blocks of all types are |
| 155 | returned. */ |
| 156 | |
| 157 | typedef |
| 158 | struct { |
| 159 | Addr addr; |
| 160 | SizeT szB; |
| 161 | Bool isVec; /* does block have an array type, or not? */ |
| 162 | HChar name[16]; /* first 15 chars of name (asciiz) */ |
| 163 | HChar soname[16]; /* first 15 chars of name (asciiz) */ |
| 164 | } |
| 165 | GlobalBlock; |
| 166 | |
| 167 | extern void* /* really, XArray* of GlobalBlock */ |
| 168 | VG_(di_get_global_blocks_from_dihandle) ( ULong di_handle, |
| 169 | Bool arrays_only ); |
| 170 | |
| 171 | |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 172 | /*====================================================================*/ |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 173 | /*=== Obtaining debug information ===*/ |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 174 | /*====================================================================*/ |
| 175 | |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 176 | /* A way to make limited debuginfo queries on a per-mapped-object |
| 177 | basis. */ |
| 178 | typedef struct _DebugInfo DebugInfo; |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 179 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 180 | /* Returns NULL if the DebugInfo isn't found. It doesn't matter if |
| 181 | debug info is present or not. */ |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 182 | DebugInfo* VG_(find_DebugInfo) ( Addr a ); |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 183 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 184 | /* Fish bits out of DebugInfos. */ |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 185 | Addr VG_(DebugInfo_get_text_avma) ( const DebugInfo *di ); |
| 186 | SizeT VG_(DebugInfo_get_text_size) ( const DebugInfo *di ); |
| 187 | Addr VG_(DebugInfo_get_plt_avma) ( const DebugInfo *di ); |
| 188 | SizeT VG_(DebugInfo_get_plt_size) ( const DebugInfo *di ); |
| 189 | Addr VG_(DebugInfo_get_gotplt_avma) ( const DebugInfo *di ); |
| 190 | SizeT VG_(DebugInfo_get_gotplt_size) ( const DebugInfo *di ); |
| 191 | const UChar* VG_(DebugInfo_get_soname) ( const DebugInfo *di ); |
| 192 | const UChar* VG_(DebugInfo_get_filename) ( const DebugInfo *di ); |
| 193 | PtrdiffT VG_(DebugInfo_get_text_bias) ( const DebugInfo *di ); |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 194 | |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 195 | /* Function for traversing the DebugInfo list. When called with NULL |
| 196 | it returns the first element; otherwise it returns the given |
| 197 | element's successor. Note that the order of elements in the list |
| 198 | changes in response to most of the queries listed in this header, |
| 199 | that explicitly or implicitly have to search the list for a |
| 200 | particular code address. So it isn't safe to assume that the order |
| 201 | of the list stays constant. */ |
| 202 | const DebugInfo* VG_(next_DebugInfo) ( const DebugInfo *di ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 203 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 204 | /* Functions for traversing all the symbols in a DebugInfo. _howmany |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 205 | tells how many there are. _getidx retrieves the n'th, for n in 0 |
| 206 | .. _howmany-1. You may not modify the function name thereby |
| 207 | acquired; if you want to do so, first strdup it. */ |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 208 | Int VG_(DebugInfo_syms_howmany) ( const DebugInfo *di ); |
| 209 | void VG_(DebugInfo_syms_getidx) ( const DebugInfo *di, |
| 210 | Int idx, |
| 211 | /*OUT*/Addr* avma, |
| 212 | /*OUT*/Addr* tocptr, |
| 213 | /*OUT*/UInt* size, |
| 214 | /*OUT*/HChar** name, |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 215 | /*OUT*/Bool* isText, |
| 216 | /*OUT*/Bool* isIFunc ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 217 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 218 | /* A simple enumeration to describe the 'kind' of various kinds of |
| 219 | segments that arise from the mapping of object files. */ |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 220 | typedef |
| 221 | enum { |
| 222 | Vg_SectUnknown, |
| 223 | Vg_SectText, |
| 224 | Vg_SectData, |
| 225 | Vg_SectBSS, |
| 226 | Vg_SectGOT, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 227 | Vg_SectPLT, |
bart | 092b626 | 2008-05-25 16:37:22 +0000 | [diff] [blame] | 228 | Vg_SectGOTPLT, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 229 | Vg_SectOPD |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 230 | } |
| 231 | VgSectKind; |
| 232 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 233 | /* Convert a VgSectKind to a string, which must be copied if you want |
| 234 | to change it. */ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 235 | const HChar* VG_(pp_SectKind)( VgSectKind kind ); |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 236 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 237 | /* Given an address 'a', make a guess of which section of which object |
| 238 | it comes from. If name is non-NULL, then the last n_name-1 |
| 239 | characters of the object's name is put in name[0 .. n_name-2], and |
| 240 | name[n_name-1] is set to zero (guaranteed zero terminated). */ |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 241 | VgSectKind VG_(DebugInfo_sect_kind)( /*OUT*/UChar* name, SizeT n_name, |
| 242 | Addr a); |
sewardj | bbec772 | 2007-11-25 14:08:53 +0000 | [diff] [blame] | 243 | |
| 244 | |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 245 | #endif // __PUB_TOOL_DEBUGINFO_H |
| 246 | |
| 247 | /*--------------------------------------------------------------------*/ |
| 248 | /*--- end ---*/ |
| 249 | /*--------------------------------------------------------------------*/ |