philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 1 | |
| 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" |
philippe | 0c9ac8d | 2014-07-18 00:03:58 +0000 | [diff] [blame^] | 35 | #include "pub_tool_wordfm.h" |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 36 | #include "pub_tool_xarray.h" |
| 37 | #include "pub_tool_execontext.h" |
| 38 | #include "pub_tool_debuginfo.h" |
| 39 | #include "pub_tool_threadstate.h" |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 40 | #include "pub_tool_addrinfo.h" |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 41 | |
| 42 | #include "hg_basics.h" |
philippe | 0c9ac8d | 2014-07-18 00:03:58 +0000 | [diff] [blame^] | 43 | #include "hg_wordset.h" |
| 44 | #include "hg_lock_n_thread.h" |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 45 | #include "hg_addrdescr.h" /* self */ |
| 46 | |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 47 | void HG_(describe_addr) ( Addr a, /*OUT*/AddrInfo* ai ) |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 48 | { |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 49 | tl_assert(ai->tag == Addr_Undescribed); |
| 50 | |
philippe | 0c9ac8d | 2014-07-18 00:03:58 +0000 | [diff] [blame^] | 51 | /* hctxt/tnr/haddr/hszB describe the addr if it is a heap block. */ |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 52 | ExeContext* hctxt; |
philippe | 0c9ac8d | 2014-07-18 00:03:58 +0000 | [diff] [blame^] | 53 | UInt tnr; |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 54 | Addr haddr; |
| 55 | SizeT hszB; |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 56 | |
| 57 | /* First, see if it's in any heap block. Unfortunately this |
| 58 | means a linear search through all allocated heap blocks. The |
| 59 | assertion says that if it's detected as a heap block, then we |
| 60 | must have an allocation context for it, since all heap blocks |
| 61 | should have an allocation context. */ |
| 62 | Bool is_heapblock |
| 63 | = HG_(mm_find_containing_block)( |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 64 | &hctxt, |
philippe | 0c9ac8d | 2014-07-18 00:03:58 +0000 | [diff] [blame^] | 65 | &tnr, |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 66 | &haddr, |
| 67 | &hszB, |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 68 | a |
| 69 | ); |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 70 | if (is_heapblock) { |
| 71 | tl_assert(is_heapblock == (hctxt != NULL)); |
| 72 | ai->tag = Addr_Block; |
| 73 | ai->Addr.Block.block_kind = Block_Mallocd; |
| 74 | ai->Addr.Block.block_desc = "block"; |
| 75 | ai->Addr.Block.block_szB = hszB; |
| 76 | ai->Addr.Block.rwoffset = (Word)(a) - (Word)(haddr); |
| 77 | ai->Addr.Block.allocated_at = hctxt; |
philippe | 0c9ac8d | 2014-07-18 00:03:58 +0000 | [diff] [blame^] | 78 | VG_(initThreadInfo) (&ai->Addr.Block.alloc_tinfo); |
| 79 | ai->Addr.Block.alloc_tinfo.tnr = tnr; |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 80 | ai->Addr.Block.freed_at = VG_(null_ExeContext)();; |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 81 | } else { |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 82 | /* No block found. Search a non-heap block description. */ |
| 83 | VG_(describe_addr) (a, ai); |
philippe | 0c9ac8d | 2014-07-18 00:03:58 +0000 | [diff] [blame^] | 84 | |
| 85 | /* In case ai contains a tid, set tnr to the corresponding helgrind |
| 86 | thread number. */ |
| 87 | if (ai->tag == Addr_Stack) { |
| 88 | Thread* thr = get_admin_threads(); |
| 89 | |
| 90 | tl_assert(ai->Addr.Stack.tinfo.tid); |
| 91 | while (thr) { |
| 92 | if (thr->coretid == ai->Addr.Stack.tinfo.tid) { |
| 93 | ai->Addr.Stack.tinfo.tnr = thr->errmsg_index; |
| 94 | break; |
| 95 | } |
| 96 | thr = thr->admin; |
| 97 | } |
| 98 | } |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 102 | Bool HG_(get_and_pp_addrdescr) (Addr addr) |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 103 | { |
| 104 | |
| 105 | Bool ret; |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 106 | AddrInfo glai; |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 107 | |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 108 | glai.tag = Addr_Undescribed; |
| 109 | HG_(describe_addr) (addr, &glai); |
| 110 | VG_(pp_addrinfo) (addr, &glai); |
| 111 | ret = glai.tag != Addr_Unknown; |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 112 | |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 113 | VG_(clear_addrinfo) (&glai); |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 118 | /*--------------------------------------------------------------------*/ |
| 119 | /*--- end hg_addrdescr.c ---*/ |
| 120 | /*--------------------------------------------------------------------*/ |