pub_tool_addrinfo.h revision 0c9ac8d0deca2f2a552fb2b0cab24efe6191bac7
107c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
207c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/*--------------------------------------------------------------------*/
307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/*--- Obtaining information about an address.  pub_tool_addrinfo.h ---*/
407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/*--------------------------------------------------------------------*/
507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
607c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/*
707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   This file is part of Valgrind, a dynamic binary instrumentation
807c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   framework.
907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
1007c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   Copyright (C) 2000-2013 Julian Seward
1107c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      jseward@acm.org
1207c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
1307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   This program is free software; you can redistribute it and/or
1407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   modify it under the terms of the GNU General Public License as
1507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   published by the Free Software Foundation; either version 2 of the
1607c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   License, or (at your option) any later version.
1707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
1807c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   This program is distributed in the hope that it will be useful, but
1907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   WITHOUT ANY WARRANTY; without even the implied warranty of
2007c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2107c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   General Public License for more details.
2207c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
2307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   You should have received a copy of the GNU General Public License
2407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   along with this program; if not, write to the Free Software
2507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
2607c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   02111-1307, USA.
2707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
2807c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   The GNU General Public License is contained in the file COPYING.
2907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe*/
3007c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
3107c08527f05caeb0062b42ca9a58ee774ec5fba1philippe#ifndef __PUB_TOOL_ADDRINFO_H
3207c08527f05caeb0062b42ca9a58ee774ec5fba1philippe#define __PUB_TOOL_ADDRINFO_H
3307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
3407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe#include "pub_tool_basics.h"   // VG_ macro
3507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
3607c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/*====================================================================*/
3707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/*=== Obtaining information about an address                       ===*/
3807c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/*====================================================================*/
3907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
4007c08527f05caeb0062b42ca9a58ee774ec5fba1philippe// Different kinds of blocks.
4107c08527f05caeb0062b42ca9a58ee774ec5fba1philippe// Block_Mallocd is used by tools that maintain detailed information about
4207c08527f05caeb0062b42ca9a58ee774ec5fba1philippe//   Client allocated heap blocks.
4307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe// Block_Freed is used by tools such as memcheck that maintain a 'quarantine'
4407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe//   list of blocks freed by the Client but not yet physically freed.
4507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe// Block_MempoolChunk and Block_UserG are used for mempool or user defined heap
4607c08527f05caeb0062b42ca9a58ee774ec5fba1philippe//   blocks.
4707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe// Block_ClientArenaMallocd and Block_ClientArenaFree are used when the tool
4807c08527f05caeb0062b42ca9a58ee774ec5fba1philippe//   replaces the malloc/free/... functions but does not maintain detailed
4907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe//   information about Client allocated heap blocks.
50fed894d33a94f840c013d1f127a6e819fd26ea2aphilippe// Block_ValgrindArenaMallocd and Block_ValgrindArenaFree are used for heap
51fed894d33a94f840c013d1f127a6e819fd26ea2aphilippe//   blocks of Valgrind internal heap.
5207c08527f05caeb0062b42ca9a58ee774ec5fba1philippetypedef enum {
5307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   Block_Mallocd = 111,
5407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   Block_Freed,
5507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   Block_MempoolChunk,
5607c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   Block_UserG,
5707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   Block_ClientArenaMallocd,
5807c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   Block_ClientArenaFree,
5907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   Block_ValgrindArenaMallocd,
6007c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   Block_ValgrindArenaFree,
6107c08527f05caeb0062b42ca9a58ee774ec5fba1philippe} BlockKind;
6207c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
6307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/* ------------------ Addresses -------------------- */
6407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
6507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/* The classification of a faulting address. */
6607c08527f05caeb0062b42ca9a58ee774ec5fba1philippetypedef
6707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   enum {
6807c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      Addr_Undescribed, // as-yet unclassified
6907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      Addr_Unknown,     // classification yielded nothing useful
7007c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      Addr_Block,       // in malloc'd/free'd block
7107c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      Addr_Stack,       // on a thread's stack
7207c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      Addr_DataSym,     // in a global data sym
7307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      Addr_Variable,    // variable described by the debug info
7407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      Addr_SectKind     // last-ditch classification attempt
7507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   }
7607c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   AddrTag;
7707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
780c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe/* Note about ThreadInfo tid and tnr in various parts of _Addrinfo:
790c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   A tid is an index in the VG_(threads)[] array. The entries
800c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   in  VG_(threads) array are re-used, so the tid in an 'old' _Addrinfo
810c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   might be misleading: if the thread that was using tid has been terminated
820c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   and the tid was re-used by another thread, the tid will very probably
830c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   be wrongly interpreted by the user.
840c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   So, such an _Addrinfo should be printed just after it has been produced,
850c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   before the tid could possibly be re-used by another thread.
860c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe
870c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   A tool such as helgrind is uniquely/unambiguously identifying each thread
880c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   by a number. If the tool sets tnr between the call to
890c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   VG_(describe_addr) and the call to VG_(pp_addrinfo), then VG_(pp_addrinfo)
900c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   will by preference print tnr instead of tid.
910c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   Visually, a tid will be printed as   thread %d
920c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   while a tnr will be printed as       thread #%d
930c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe*/
940c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe
950c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippetypedef
960c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   struct _ThreadInfo {
970c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe      ThreadId tid;   // 0 means thread not known.
980c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe      UInt     tnr;   // 0 means no tool specific thread nr, or not known.
990c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe   } ThreadInfo;
1000c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe
1010c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe/* Zeroes/clear all the fields of *tinfo. */
1020c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippeextern void VG_(initThreadInfo) (ThreadInfo *tinfo);
1030c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe
10407c08527f05caeb0062b42ca9a58ee774ec5fba1philippetypedef
10507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   struct _AddrInfo
10607c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   AddrInfo;
1070c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe
10807c08527f05caeb0062b42ca9a58ee774ec5fba1philippestruct _AddrInfo {
10907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   AddrTag tag;
11007c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   union {
11107c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      // As-yet unclassified.
11207c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      struct { } Undescribed;
11307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
1140c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe      // On a stack. tinfo indicates which thread's stack?
11518d6f4ec1b86057e1a906db1a1d31c89472fb1adphilippe      // IP is the address of an instruction of the function where the
11618d6f4ec1b86057e1a906db1a1d31c89472fb1adphilippe      // stack address was. 0 if not found.
11718d6f4ec1b86057e1a906db1a1d31c89472fb1adphilippe      // frameNo is the frame nr of the call where the stack address was.
11818d6f4ec1b86057e1a906db1a1d31c89472fb1adphilippe      // -1 if not found.
11907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      struct {
1200c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe         ThreadInfo tinfo;
12118d6f4ec1b86057e1a906db1a1d31c89472fb1adphilippe         Addr     IP;
12218d6f4ec1b86057e1a906db1a1d31c89472fb1adphilippe         Int      frameNo;
12307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      } Stack;
12407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
125fed894d33a94f840c013d1f127a6e819fd26ea2aphilippe      // This covers heap blocks (normal and from mempools), user-defined
126fed894d33a94f840c013d1f127a6e819fd26ea2aphilippe      // blocks and Arena blocks.
1270c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe      // alloc_tinfo identifies the thread that has allocated the block.
1280c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe      // This is used by tools such as helgrind that maintain
1290c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe      // more detailed informations about client blocks.
13007c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      struct {
13107c08527f05caeb0062b42ca9a58ee774ec5fba1philippe         BlockKind   block_kind;
132fed894d33a94f840c013d1f127a6e819fd26ea2aphilippe         const HChar* block_desc;   // "block","mempool","user-defined",arena
13307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe         SizeT       block_szB;
13407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe         PtrdiffT    rwoffset;
13507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe         ExeContext* allocated_at;  // might be null_ExeContext.
1360c9ac8d0deca2f2a552fb2b0cab24efe6191bac7philippe         ThreadInfo  alloc_tinfo;   // which thread did alloc this block.
13707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe         ExeContext* freed_at;      // might be null_ExeContext.
13807c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      } Block;
13907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
14007c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      // In a global .data symbol.  This holds the first 127 chars of
14107c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      // the variable's name (zero terminated), plus a (memory) offset.
14207c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      struct {
14307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe         HChar    name[128];
14407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe         PtrdiffT offset;
14507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      } DataSym;
14607c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
14707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      // Is described by Dwarf debug info.  XArray*s of HChar.
14807c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      struct {
14907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe         XArray* /* of HChar */ descr1;
15007c08527f05caeb0062b42ca9a58ee774ec5fba1philippe         XArray* /* of HChar */ descr2;
15107c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      } Variable;
15207c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
15307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      // Could only narrow it down to be the PLT/GOT/etc of a given
15407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      // object.  Better than nothing, perhaps.
15507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      struct {
15607c08527f05caeb0062b42ca9a58ee774ec5fba1philippe         HChar      objname[128];
15707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe         VgSectKind kind;
15807c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      } SectKind;
15907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
16007c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      // Classification yielded nothing useful.
16107c08527f05caeb0062b42ca9a58ee774ec5fba1philippe      struct { } Unknown;
16207c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
16307c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   } Addr;
16407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe};
16507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
16607c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
16707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/* Describe an address as best you can, putting the result in ai.
16807c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   On entry, ai->tag must be equal to Addr_Undescribed.
16907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   This might allocate some memory, that can be cleared with
17007c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   VG_(clear_addrinfo). */
17107c08527f05caeb0062b42ca9a58ee774ec5fba1philippeextern void VG_(describe_addr) ( Addr a, /*OUT*/AddrInfo* ai );
17207c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
17307c08527f05caeb0062b42ca9a58ee774ec5fba1philippeextern void VG_(clear_addrinfo) ( AddrInfo* ai);
17407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
17507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/* Prints the AddrInfo ai describing a. */
17607c08527f05caeb0062b42ca9a58ee774ec5fba1philippeextern void VG_(pp_addrinfo) ( Addr a, AddrInfo* ai );
17707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
17807c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/* Same as VG_(pp_addrinfo) but provides some memcheck specific behaviour:
17907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   * maybe_gcc indicates Addr a was just below the stack ptr when the error
18007c08527f05caeb0062b42ca9a58ee774ec5fba1philippe     with a was encountered.
18107c08527f05caeb0062b42ca9a58ee774ec5fba1philippe   * the message for Unknown tag is slightly different, as memcheck
18207c08527f05caeb0062b42ca9a58ee774ec5fba1philippe     has a recently freed list. */
18307c08527f05caeb0062b42ca9a58ee774ec5fba1philippeextern void VG_(pp_addrinfo_mc) ( Addr a, AddrInfo* ai, Bool maybe_gcc );
18407c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
18507c08527f05caeb0062b42ca9a58ee774ec5fba1philippe#endif   // __PUB_TOOL_ADDRINFO_H
18607c08527f05caeb0062b42ca9a58ee774ec5fba1philippe
18707c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/*--------------------------------------------------------------------*/
18807c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/*--- end                                                          ---*/
18907c08527f05caeb0062b42ca9a58ee774ec5fba1philippe/*--------------------------------------------------------------------*/
190