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