blob: cf7de81c4e4d4c840c8525251ffaf8a93ce8b24a [file] [log] [blame]
philippef5774342014-05-03 11:12:50 +00001
2/*--------------------------------------------------------------------*/
3/*--- Address Description. ---*/
4/*--- hg_addrdescr.c ---*/
5/*--------------------------------------------------------------------*/
6
7/*
8 This file is part of Helgrind, a Valgrind tool for detecting errors
9 in threaded programs.
10
11 Copyright (C) 2007-2012 OpenWorks Ltd
12 info@open-works.co.uk
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
29 The GNU General Public License is contained in the file COPYING.
30*/
31#include "pub_tool_basics.h"
32#include "pub_tool_libcbase.h"
33#include "pub_tool_libcprint.h"
34#include "pub_tool_libcassert.h"
35#include "pub_tool_xarray.h"
36#include "pub_tool_execontext.h"
37#include "pub_tool_debuginfo.h"
38#include "pub_tool_threadstate.h"
philippe07c08522014-05-14 20:39:27 +000039#include "pub_tool_addrinfo.h"
philippef5774342014-05-03 11:12:50 +000040
41#include "hg_basics.h"
42#include "hg_addrdescr.h" /* self */
43
philippe07c08522014-05-14 20:39:27 +000044void HG_(describe_addr) ( Addr a, /*OUT*/AddrInfo* ai )
philippef5774342014-05-03 11:12:50 +000045{
philippe07c08522014-05-14 20:39:27 +000046 tl_assert(ai->tag == Addr_Undescribed);
47
48 /* hctxt/haddr/hszB describe the addr if it is a heap block. */
49 ExeContext* hctxt;
50 Addr haddr;
51 SizeT hszB;
philippef5774342014-05-03 11:12:50 +000052
53 /* First, see if it's in any heap block. Unfortunately this
54 means a linear search through all allocated heap blocks. The
55 assertion says that if it's detected as a heap block, then we
56 must have an allocation context for it, since all heap blocks
57 should have an allocation context. */
58 Bool is_heapblock
59 = HG_(mm_find_containing_block)(
philippe07c08522014-05-14 20:39:27 +000060 &hctxt,
61 &haddr,
62 &hszB,
philippef5774342014-05-03 11:12:50 +000063 a
64 );
philippe07c08522014-05-14 20:39:27 +000065 if (is_heapblock) {
66 tl_assert(is_heapblock == (hctxt != NULL));
67 ai->tag = Addr_Block;
68 ai->Addr.Block.block_kind = Block_Mallocd;
69 ai->Addr.Block.block_desc = "block";
70 ai->Addr.Block.block_szB = hszB;
71 ai->Addr.Block.rwoffset = (Word)(a) - (Word)(haddr);
72 ai->Addr.Block.allocated_at = hctxt;
73 ai->Addr.Block.freed_at = VG_(null_ExeContext)();;
philippef5774342014-05-03 11:12:50 +000074 } else {
philippe07c08522014-05-14 20:39:27 +000075 /* No block found. Search a non-heap block description. */
76 VG_(describe_addr) (a, ai);
philippef5774342014-05-03 11:12:50 +000077 }
78}
79
philippe07c08522014-05-14 20:39:27 +000080Bool HG_(get_and_pp_addrdescr) (Addr addr)
philippef5774342014-05-03 11:12:50 +000081{
82
83 Bool ret;
philippe07c08522014-05-14 20:39:27 +000084 AddrInfo glai;
philippef5774342014-05-03 11:12:50 +000085
philippe07c08522014-05-14 20:39:27 +000086 glai.tag = Addr_Undescribed;
87 HG_(describe_addr) (addr, &glai);
88 VG_(pp_addrinfo) (addr, &glai);
89 ret = glai.tag != Addr_Unknown;
philippef5774342014-05-03 11:12:50 +000090
philippe07c08522014-05-14 20:39:27 +000091 VG_(clear_addrinfo) (&glai);
philippef5774342014-05-03 11:12:50 +000092
93 return ret;
94}
95
philippef5774342014-05-03 11:12:50 +000096/*--------------------------------------------------------------------*/
97/*--- end hg_addrdescr.c ---*/
98/*--------------------------------------------------------------------*/