blob: e464e39e465861a8a6ef4881c73e0727b55b3250 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
3/*--- Storage, and equality on, execution contexts (backtraces). ---*/
4/*--- vg_execontext.c ---*/
5/*--------------------------------------------------------------------*/
6
7/*
njnc9539842002-10-02 13:26:35 +00008 This file is part of Valgrind, an extensible x86 protected-mode
9 emulator for monitoring program execution on x86-Unixes.
sewardjde4a1d02002-03-22 01:27:54 +000010
njn0e1b5142003-04-15 14:58:06 +000011 Copyright (C) 2000-2003 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000012 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000013
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
njn25e49d8e72002-09-23 09:36:25 +000029 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000030*/
31
32#include "vg_include.h"
sewardjde4a1d02002-03-22 01:27:54 +000033
34
35/*------------------------------------------------------------*/
36/*--- Low-level ExeContext storage. ---*/
37/*------------------------------------------------------------*/
38
39/* The idea is only to ever store any one context once, so as to save
40 space and make exact comparisons faster. */
41
42static ExeContext* vg_ec_list[VG_N_EC_LISTS];
43
44/* Stats only: the number of times the system was searched to locate a
45 context. */
46static UInt vg_ec_searchreqs;
47
48/* Stats only: the number of full context comparisons done. */
49static UInt vg_ec_searchcmps;
50
51/* Stats only: total number of stored contexts. */
52static UInt vg_ec_totstored;
53
54/* Number of 2, 4 and (fast) full cmps done. */
55static UInt vg_ec_cmp2s;
56static UInt vg_ec_cmp4s;
57static UInt vg_ec_cmpAlls;
58
59
60/*------------------------------------------------------------*/
61/*--- Exported functions. ---*/
62/*------------------------------------------------------------*/
63
64
65/* Initialise this subsystem. */
sewardjc6b0fe52003-07-23 23:01:11 +000066static void init_ExeContext_storage ( void )
sewardjde4a1d02002-03-22 01:27:54 +000067{
68 Int i;
sewardjc6b0fe52003-07-23 23:01:11 +000069 static Bool init_done = False;
70 if (init_done)
71 return;
sewardjde4a1d02002-03-22 01:27:54 +000072 vg_ec_searchreqs = 0;
73 vg_ec_searchcmps = 0;
74 vg_ec_totstored = 0;
75 vg_ec_cmp2s = 0;
76 vg_ec_cmp4s = 0;
77 vg_ec_cmpAlls = 0;
78 for (i = 0; i < VG_N_EC_LISTS; i++)
79 vg_ec_list[i] = NULL;
sewardjc6b0fe52003-07-23 23:01:11 +000080 init_done = True;
sewardjde4a1d02002-03-22 01:27:54 +000081}
82
83
84/* Show stats. */
85void VG_(show_ExeContext_stats) ( void )
86{
sewardjc6b0fe52003-07-23 23:01:11 +000087 init_ExeContext_storage();
sewardjde4a1d02002-03-22 01:27:54 +000088 VG_(message)(Vg_DebugMsg,
89 "exectx: %d lists, %d contexts (avg %d per list)",
90 VG_N_EC_LISTS, vg_ec_totstored,
91 vg_ec_totstored / VG_N_EC_LISTS
92 );
93 VG_(message)(Vg_DebugMsg,
94 "exectx: %d searches, %d full compares (%d per 1000)",
95 vg_ec_searchreqs, vg_ec_searchcmps,
96 vg_ec_searchreqs == 0
97 ? 0
98 : (UInt)( (((ULong)vg_ec_searchcmps) * 1000)
99 / ((ULong)vg_ec_searchreqs ))
100 );
101 VG_(message)(Vg_DebugMsg,
102 "exectx: %d cmp2, %d cmp4, %d cmpAll",
103 vg_ec_cmp2s, vg_ec_cmp4s, vg_ec_cmpAlls
104 );
105}
106
107
108/* Print an ExeContext. */
109void VG_(pp_ExeContext) ( ExeContext* e )
110{
sewardjc6b0fe52003-07-23 23:01:11 +0000111 init_ExeContext_storage();
njn6c846552003-09-16 07:41:43 +0000112 VG_(mini_stack_dump) ( e->eips, VG_(clo_backtrace_size) );
sewardjde4a1d02002-03-22 01:27:54 +0000113}
114
115
116/* Compare two ExeContexts, comparing all callers. */
njn25e49d8e72002-09-23 09:36:25 +0000117Bool VG_(eq_ExeContext) ( VgRes res, ExeContext* e1, ExeContext* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000118{
njn25e49d8e72002-09-23 09:36:25 +0000119 if (e1 == NULL || e2 == NULL)
120 return False;
121 switch (res) {
122 case Vg_LowRes:
123 /* Just compare the top two callers. */
124 vg_ec_cmp2s++;
125 if (e1->eips[0] != e2->eips[0]
126 || e1->eips[1] != e2->eips[1]) return False;
127 return True;
sewardjde4a1d02002-03-22 01:27:54 +0000128
njn25e49d8e72002-09-23 09:36:25 +0000129 case Vg_MedRes:
130 /* Just compare the top four callers. */
131 vg_ec_cmp4s++;
njn6c846552003-09-16 07:41:43 +0000132 if (e1->eips[0] != e2->eips[0]) return False;
133
134 if (VG_(clo_backtrace_size) < 2) return True;
135 if (e1->eips[1] != e2->eips[1]) return False;
sewardjde4a1d02002-03-22 01:27:54 +0000136
njn25e49d8e72002-09-23 09:36:25 +0000137 if (VG_(clo_backtrace_size) < 3) return True;
138 if (e1->eips[2] != e2->eips[2]) return False;
sewardjde4a1d02002-03-22 01:27:54 +0000139
njn25e49d8e72002-09-23 09:36:25 +0000140 if (VG_(clo_backtrace_size) < 4) return True;
141 if (e1->eips[3] != e2->eips[3]) return False;
142 return True;
sewardjde4a1d02002-03-22 01:27:54 +0000143
njn25e49d8e72002-09-23 09:36:25 +0000144 case Vg_HighRes:
145 vg_ec_cmpAlls++;
146 /* Compare them all -- just do pointer comparison. */
147 if (e1 != e2) return False;
148 return True;
sewardjde4a1d02002-03-22 01:27:54 +0000149
njn25e49d8e72002-09-23 09:36:25 +0000150 default:
njne427a662002-10-02 11:08:25 +0000151 VG_(core_panic)("VG_(eq_ExeContext): unrecognised VgRes");
njn25e49d8e72002-09-23 09:36:25 +0000152 }
sewardjde4a1d02002-03-22 01:27:54 +0000153}
154
155
njn6c846552003-09-16 07:41:43 +0000156/* Take a snapshot of the client's stack, putting the up to 'n_eips' %eips
157 into 'eips'. In order to be thread-safe, we pass in the thread's %EIP
158 and %EBP. Returns number of %eips put in 'eips'. */
159static UInt stack_snapshot2 ( Addr* eips, UInt n_eips, Addr eip, Addr ebp,
160 Addr ebp_min, Addr ebp_max_orig )
sewardjde4a1d02002-03-22 01:27:54 +0000161{
162 Int i;
njn25e49d8e72002-09-23 09:36:25 +0000163 Addr ebp_max;
njn6c846552003-09-16 07:41:43 +0000164 UInt n_found = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000165
166 VGP_PUSHCC(VgpExeContext);
167
njn6c846552003-09-16 07:41:43 +0000168 /* First snaffle %EIPs from the client's stack into eips[0 .. n_eips-1],
169 putting zeroes in when the trail goes cold, which we guess to be when
170 %ebp is not a reasonable stack location. We also assert that %ebp
171 increases down the chain. */
sewardjde4a1d02002-03-22 01:27:54 +0000172
njn25e49d8e72002-09-23 09:36:25 +0000173 // Gives shorter stack trace for tests/badjump.c
174 // JRS 2002-aug-16: I don't think this is a big deal; looks ok for
175 // most "normal" backtraces.
176 // NJN 2002-sep-05: traces for pthreaded programs are particularly bad.
177
178 // JRS 2002-sep-17: hack, to round up ebp_max to the end of the
179 // current page, at least. Dunno if it helps.
180 // NJN 2002-sep-17: seems to -- stack traces look like 1.0.X again
181 ebp_max = (ebp_max_orig + VKI_BYTES_PER_PAGE - 1)
182 & ~(VKI_BYTES_PER_PAGE - 1);
183 ebp_max -= sizeof(Addr);
184
185 /* Assertion broken before main() is reached in pthreaded programs; the
186 * offending stack traces only have one item. --njn, 2002-aug-16 */
187 /* vg_assert(ebp_min <= ebp_max);*/
188
njnac5b0612002-10-02 07:56:02 +0000189 if (ebp_min + 4000000 <= ebp_max) {
sewardjc32b9d62002-10-01 09:02:47 +0000190 /* If the stack is ridiculously big, don't poke around ... but
191 don't bomb out either. Needed to make John Regehr's
192 user-space threads package work. JRS 20021001 */
193 eips[0] = eip;
194 i = 1;
195 } else {
196 /* Get whatever we safely can ... */
197 eips[0] = eip;
njn6c846552003-09-16 07:41:43 +0000198 for (i = 1; i < n_eips; i++) {
sewardjc32b9d62002-10-01 09:02:47 +0000199 if (!(ebp_min <= ebp && ebp <= ebp_max)) {
200 //VG_(printf)("... out of range %p\n", ebp);
201 break; /* ebp gone baaaad */
202 }
203 // NJN 2002-sep-17: monotonicity doesn't work -- gives wrong traces...
204 // if (ebp >= ((UInt*)ebp)[0]) {
205 // VG_(printf)("nonmonotonic\n");
206 // break; /* ebp gone nonmonotonic */
207 // }
208 eips[i] = ((UInt*)ebp)[1]; /* ret addr */
209 ebp = ((UInt*)ebp)[0]; /* old ebp */
210 //VG_(printf)(" %p\n", eips[i]);
njn25e49d8e72002-09-23 09:36:25 +0000211 }
njn25e49d8e72002-09-23 09:36:25 +0000212 }
njn6c846552003-09-16 07:41:43 +0000213 n_found = i;
njn25e49d8e72002-09-23 09:36:25 +0000214
215 /* Put zeroes in the rest. */
njn6c846552003-09-16 07:41:43 +0000216 for (; i < n_eips; i++) {
sewardjde4a1d02002-03-22 01:27:54 +0000217 eips[i] = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000218 }
njn6c846552003-09-16 07:41:43 +0000219 VGP_POPCC(VgpExeContext);
220
221 return n_found;
222}
223
224/* This guy is the head honcho here. Take a snapshot of the client's
225 stack. Search our collection of ExeContexts to see if we already
226 have it, and if not, allocate a new one. Either way, return a
227 pointer to the context. If there is a matching context we
228 guarantee to not allocate a new one. Thus we never store
229 duplicates, and so exact equality can be quickly done as equality
230 on the returned ExeContext* values themselves. Inspired by Hugs's
231 Text type.
232*/
233ExeContext* VG_(get_ExeContext2) ( Addr eip, Addr ebp,
234 Addr ebp_min, Addr ebp_max_orig )
235{
236 Int i;
237 Addr eips[VG_DEEPEST_BACKTRACE];
238 Bool same;
239 UInt hash;
240 ExeContext* new_ec;
241 ExeContext* list;
242
243 VGP_PUSHCC(VgpExeContext);
244
245 init_ExeContext_storage();
246 vg_assert(VG_(clo_backtrace_size) >= 1
247 && VG_(clo_backtrace_size) <= VG_DEEPEST_BACKTRACE);
248
249 stack_snapshot2( eips, VG_(clo_backtrace_size),
250 eip, ebp, ebp_min, ebp_max_orig );
sewardjde4a1d02002-03-22 01:27:54 +0000251
sewardjde4a1d02002-03-22 01:27:54 +0000252 /* Now figure out if we've seen this one before. First hash it so
253 as to determine the list number. */
254
255 hash = 0;
256 for (i = 0; i < VG_(clo_backtrace_size); i++) {
257 hash ^= (UInt)eips[i];
258 hash = (hash << 29) | (hash >> 3);
259 }
260 hash = hash % VG_N_EC_LISTS;
261
262 /* And (the expensive bit) look a matching entry in the list. */
263
264 vg_ec_searchreqs++;
265
266 list = vg_ec_list[hash];
267
268 while (True) {
269 if (list == NULL) break;
270 vg_ec_searchcmps++;
271 same = True;
272 for (i = 0; i < VG_(clo_backtrace_size); i++) {
273 if (list->eips[i] != eips[i]) {
274 same = False;
275 break;
276 }
277 }
278 if (same) break;
279 list = list->next;
280 }
281
282 if (list != NULL) {
283 /* Yay! We found it. */
njn25e49d8e72002-09-23 09:36:25 +0000284 VGP_POPCC(VgpExeContext);
sewardjde4a1d02002-03-22 01:27:54 +0000285 return list;
286 }
287
288 /* Bummer. We have to allocate a new context record. */
289 vg_ec_totstored++;
290
njn25e49d8e72002-09-23 09:36:25 +0000291 new_ec = VG_(arena_malloc)( VG_AR_EXECTXT,
292 sizeof(struct _ExeContext *)
293 + VG_(clo_backtrace_size) * sizeof(Addr) );
sewardjde4a1d02002-03-22 01:27:54 +0000294
295 for (i = 0; i < VG_(clo_backtrace_size); i++)
296 new_ec->eips[i] = eips[i];
297
298 new_ec->next = vg_ec_list[hash];
299 vg_ec_list[hash] = new_ec;
300
njn25e49d8e72002-09-23 09:36:25 +0000301 VGP_POPCC(VgpExeContext);
sewardjde4a1d02002-03-22 01:27:54 +0000302 return new_ec;
303}
304
njn6c846552003-09-16 07:41:43 +0000305void get_needed_regs(ThreadId tid, Addr* eip, Addr* ebp, Addr* esp,
306 Addr* stack_highest_word)
njn25e49d8e72002-09-23 09:36:25 +0000307{
njn72718642003-07-24 08:45:32 +0000308 if (VG_(is_running_thread)(tid)) {
sewardj499e3de2002-11-13 22:22:25 +0000309 /* thread currently in baseblock */
njn6c846552003-09-16 07:41:43 +0000310 *eip = VG_(baseBlock)[VGOFF_(m_eip)];
311 *ebp = VG_(baseBlock)[VGOFF_(m_ebp)];
312 *esp = VG_(baseBlock)[VGOFF_(m_esp)];
313 *stack_highest_word = VG_(threads)[tid].stack_highest_word;
sewardj499e3de2002-11-13 22:22:25 +0000314 } else {
njn72718642003-07-24 08:45:32 +0000315 /* thread in thread table */
316 ThreadState* tst = & VG_(threads)[ tid ];
njn6c846552003-09-16 07:41:43 +0000317 *eip = tst->m_eip;
318 *ebp = tst->m_ebp;
319 *esp = tst->m_esp;
320 *stack_highest_word = tst->stack_highest_word;
sewardj499e3de2002-11-13 22:22:25 +0000321 }
njn6c846552003-09-16 07:41:43 +0000322}
323
324ExeContext* VG_(get_ExeContext) ( ThreadId tid )
325{
326 Addr eip, ebp, esp, stack_highest_word;
327
328 get_needed_regs(tid, &eip, &ebp, &esp, &stack_highest_word);
329 return VG_(get_ExeContext2)(eip, ebp, esp, stack_highest_word);
330}
331
332/* Take a snapshot of the client's stack, putting the up to 'n_eips' %eips
333 into 'eips'. In order to be thread-safe, we pass in the thread's %EIP
334 and %EBP. Returns number of %eips put in 'eips'. */
335UInt VG_(stack_snapshot) ( ThreadId tid, Addr* eips, UInt n_eips )
336{
337 Addr eip, ebp, esp, stack_highest_word;
338
339 get_needed_regs(tid, &eip, &ebp, &esp, &stack_highest_word);
340 return stack_snapshot2(eips, n_eips,
341 eip, ebp, esp, stack_highest_word);
342}
343
344
345Addr VG_(get_EIP_from_ExeContext) ( ExeContext* e, UInt n )
346{
347 if (n > VG_(clo_backtrace_size)) return 0;
348 return e->eips[n];
njn25e49d8e72002-09-23 09:36:25 +0000349}
350
njn72718642003-07-24 08:45:32 +0000351Addr VG_(get_EIP) ( ThreadId tid )
sewardj499e3de2002-11-13 22:22:25 +0000352{
353 Addr ret;
354
njn72718642003-07-24 08:45:32 +0000355 if (VG_(is_running_thread)(tid))
sewardj499e3de2002-11-13 22:22:25 +0000356 ret = VG_(baseBlock)[VGOFF_(m_eip)];
357 else
njn72718642003-07-24 08:45:32 +0000358 ret = VG_(threads)[ tid ].m_eip;
sewardj499e3de2002-11-13 22:22:25 +0000359
360 return ret;
361}
sewardjde4a1d02002-03-22 01:27:54 +0000362
363/*--------------------------------------------------------------------*/
364/*--- end vg_execontext.c ---*/
365/*--------------------------------------------------------------------*/