m_execontext.c revision 31513b4ab8477029517f07f0dcf1b441cb818548
1 2/*--------------------------------------------------------------------*/ 3/*--- Store and compare stack backtraces m_execontext.c ---*/ 4/*--------------------------------------------------------------------*/ 5 6/* 7 This file is part of Valgrind, a dynamic binary instrumentation 8 framework. 9 10 Copyright (C) 2000-2005 Julian Seward 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#include "core.h" 32#include "pub_core_execontext.h" 33#include "pub_core_options.h" 34#include "pub_core_profile.h" 35 36/*------------------------------------------------------------*/ 37/*--- Low-level ExeContext storage. ---*/ 38/*------------------------------------------------------------*/ 39 40/* The first 4 IP values are used in comparisons do remove duplicate errors, 41 and for comparing against suppression specifications. The rest are 42 purely informational (but often important). */ 43 44struct _ExeContext { 45 struct _ExeContext * next; 46 /* Variable-length array. The size is VG_(clo_backtrace_size); at 47 least 1, at most VG_DEEPEST_BACKTRACE. [0] is the current IP, 48 [1] is its caller, [2] is the caller of [1], etc. */ 49 Addr ips[0]; 50}; 51 52/* Number of lists in which we keep track of ExeContexts. Should be 53 prime. */ 54#define N_EC_LISTS 4999 /* a prime number */ 55 56/* The idea is only to ever store any one context once, so as to save 57 space and make exact comparisons faster. */ 58 59static ExeContext* ec_list[N_EC_LISTS]; 60 61/* Stats only: the number of times the system was searched to locate a 62 context. */ 63static UInt ec_searchreqs; 64 65/* Stats only: the number of full context comparisons done. */ 66static UInt ec_searchcmps; 67 68/* Stats only: total number of stored contexts. */ 69static UInt ec_totstored; 70 71/* Number of 2, 4 and (fast) full cmps done. */ 72static UInt ec_cmp2s; 73static UInt ec_cmp4s; 74static UInt ec_cmpAlls; 75 76 77/*------------------------------------------------------------*/ 78/*--- Exported functions. ---*/ 79/*------------------------------------------------------------*/ 80 81 82/* Initialise this subsystem. */ 83static void init_ExeContext_storage ( void ) 84{ 85 Int i; 86 static Bool init_done = False; 87 if (init_done) 88 return; 89 ec_searchreqs = 0; 90 ec_searchcmps = 0; 91 ec_totstored = 0; 92 ec_cmp2s = 0; 93 ec_cmp4s = 0; 94 ec_cmpAlls = 0; 95 for (i = 0; i < N_EC_LISTS; i++) 96 ec_list[i] = NULL; 97 init_done = True; 98} 99 100 101/* Print stats. */ 102void VG_(print_ExeContext_stats) ( void ) 103{ 104 init_ExeContext_storage(); 105 VG_(message)(Vg_DebugMsg, 106 " exectx: %d lists, %d contexts (avg %d per list)", 107 N_EC_LISTS, ec_totstored, 108 ec_totstored / N_EC_LISTS 109 ); 110 VG_(message)(Vg_DebugMsg, 111 " exectx: %d searches, %d full compares (%d per 1000)", 112 ec_searchreqs, ec_searchcmps, 113 ec_searchreqs == 0 114 ? 0 115 : (UInt)( (((ULong)ec_searchcmps) * 1000) 116 / ((ULong)ec_searchreqs )) 117 ); 118 VG_(message)(Vg_DebugMsg, 119 " exectx: %d cmp2, %d cmp4, %d cmpAll", 120 ec_cmp2s, ec_cmp4s, ec_cmpAlls 121 ); 122} 123 124 125/* Print an ExeContext. */ 126void VG_(pp_ExeContext) ( ExeContext* ec ) 127{ 128 VG_(pp_StackTrace)( ec->ips, VG_(clo_backtrace_size) ); 129} 130 131 132/* Compare two ExeContexts, comparing all callers. */ 133Bool VG_(eq_ExeContext) ( VgRes res, ExeContext* e1, ExeContext* e2 ) 134{ 135 if (e1 == NULL || e2 == NULL) 136 return False; 137 switch (res) { 138 case Vg_LowRes: 139 /* Just compare the top two callers. */ 140 ec_cmp2s++; 141 if (e1->ips[0] != e2->ips[0]) return False; 142 143 if (VG_(clo_backtrace_size) < 2) return True; 144 if (e1->ips[1] != e2->ips[1]) return False; 145 return True; 146 147 case Vg_MedRes: 148 /* Just compare the top four callers. */ 149 ec_cmp4s++; 150 if (e1->ips[0] != e2->ips[0]) return False; 151 152 if (VG_(clo_backtrace_size) < 2) return True; 153 if (e1->ips[1] != e2->ips[1]) return False; 154 155 if (VG_(clo_backtrace_size) < 3) return True; 156 if (e1->ips[2] != e2->ips[2]) return False; 157 158 if (VG_(clo_backtrace_size) < 4) return True; 159 if (e1->ips[3] != e2->ips[3]) return False; 160 return True; 161 162 case Vg_HighRes: 163 ec_cmpAlls++; 164 /* Compare them all -- just do pointer comparison. */ 165 if (e1 != e2) return False; 166 return True; 167 168 default: 169 VG_(core_panic)("VG_(eq_ExeContext): unrecognised VgRes"); 170 } 171} 172 173/* This guy is the head honcho here. Take a snapshot of the client's 174 stack. Search our collection of ExeContexts to see if we already 175 have it, and if not, allocate a new one. Either way, return a 176 pointer to the context. If there is a matching context we 177 guarantee to not allocate a new one. Thus we never store 178 duplicates, and so exact equality can be quickly done as equality 179 on the returned ExeContext* values themselves. Inspired by Hugs's 180 Text type. 181*/ 182ExeContext* VG_(record_ExeContext) ( ThreadId tid ) 183{ 184 Int i; 185 Addr ips[VG_DEEPEST_BACKTRACE]; 186 Bool same; 187 UWord hash; 188 ExeContext* new_ec; 189 ExeContext* list; 190 191 VGP_PUSHCC(VgpExeContext); 192 193 init_ExeContext_storage(); 194 vg_assert(VG_(clo_backtrace_size) >= 1 195 && VG_(clo_backtrace_size) <= VG_DEEPEST_BACKTRACE); 196 197 VG_(get_StackTrace)( tid, ips, VG_(clo_backtrace_size) ); 198 199 /* Now figure out if we've seen this one before. First hash it so 200 as to determine the list number. */ 201 202 hash = 0; 203 for (i = 0; i < VG_(clo_backtrace_size); i++) { 204 hash ^= ips[i]; 205 hash = (hash << 29) | (hash >> 3); 206 } 207 hash = hash % N_EC_LISTS; 208 209 /* And (the expensive bit) look a matching entry in the list. */ 210 211 ec_searchreqs++; 212 213 list = ec_list[hash]; 214 215 while (True) { 216 if (list == NULL) break; 217 ec_searchcmps++; 218 same = True; 219 for (i = 0; i < VG_(clo_backtrace_size); i++) { 220 if (list->ips[i] != ips[i]) { 221 same = False; 222 break; 223 } 224 } 225 if (same) break; 226 list = list->next; 227 } 228 229 if (list != NULL) { 230 /* Yay! We found it. */ 231 VGP_POPCC(VgpExeContext); 232 return list; 233 } 234 235 /* Bummer. We have to allocate a new context record. */ 236 ec_totstored++; 237 238 new_ec = VG_(arena_malloc)( VG_AR_EXECTXT, 239 sizeof(struct _ExeContext *) 240 + VG_(clo_backtrace_size) * sizeof(Addr) ); 241 242 for (i = 0; i < VG_(clo_backtrace_size); i++) 243 new_ec->ips[i] = ips[i]; 244 245 new_ec->next = ec_list[hash]; 246 ec_list[hash] = new_ec; 247 248 VGP_POPCC(VgpExeContext); 249 return new_ec; 250} 251 252StackTrace VG_(extract_StackTrace) ( ExeContext* e ) 253{ 254 return e->ips; 255} 256 257/*--------------------------------------------------------------------*/ 258/*--- end m_execontext.c ---*/ 259/*--------------------------------------------------------------------*/ 260