VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/base/nsStackFrameUnix.cpp@ 78394

Last change on this file since 78394 was 64625, checked in by vboxsync, 8 years ago

xpcom: Explicitly don't compile this code even if 'linux' is defined. 'linux' is not pre-defined by gcc if the source code is compiled with '-ansi' -- like done in our context. This change fixes compiling with Parfait which isn't aware of this condition.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is nsStackFrameWin.h code, released
17 * December 20, 2000.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 2003
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40#include "nsStackFrameUnix.h"
41#include <stdlib.h>
42#include <string.h>
43#include <math.h>
44#include "nscore.h"
45
46// On glibc 2.1, the Dl_info api defined in <dlfcn.h> is only exposed
47// if __USE_GNU is defined. I suppose its some kind of standards
48// adherence thing.
49//
50#if (__GLIBC_MINOR__ >= 1) && !defined(__USE_GNU)
51#define __USE_GNU
52#endif
53
54#ifdef HAVE_LIBDL
55#include <dlfcn.h>
56#endif
57
58
59
60// This thing is exported by libstdc++
61// Yes, this is a gcc only hack
62#if defined(MOZ_DEMANGLE_SYMBOLS)
63#include <cxxabi.h>
64#include <stdlib.h> // for free()
65#endif // MOZ_DEMANGLE_SYMBOLS
66
67void DemangleSymbol(const char * aSymbol,
68 char * aBuffer,
69 int aBufLen)
70{
71 aBuffer[0] = '\0';
72
73#if defined(MOZ_DEMANGLE_SYMBOLS)
74 /* See demangle.h in the gcc source for the voodoo */
75 char * demangled = abi::__cxa_demangle(aSymbol,0,0,0);
76
77 if (demangled)
78 {
79 strncpy(aBuffer,demangled,aBufLen);
80 free(demangled);
81 }
82#endif // MOZ_DEMANGLE_SYMBOLS
83}
84
85
86#if defined(linux) && !defined(VBOX) && defined(__GLIBC__) && (defined(__i386) || defined(PPC)) // i386 or PPC Linux stackwalking code
87
88#include <setjmp.h>
89//
90
91void DumpStackToFile(FILE* aStream)
92{
93 jmp_buf jb;
94 setjmp(jb);
95
96 // Stack walking code courtesy Kipp's "leaky".
97
98 // Get the frame pointer out of the jmp_buf
99 void **bp = (void**)
100#if defined(__i386)
101 (jb[0].__jmpbuf[JB_BP]);
102#elif defined(PPC)
103 (jb[0].__jmpbuf[JB_GPR1]);
104#endif
105
106 int skip = 2;
107 for ( ; (void**)*bp > bp; bp = (void**)*bp) {
108 void *pc = *(bp+1);
109 if (--skip <= 0) {
110 Dl_info info;
111 int ok = dladdr(pc, &info);
112 if (!ok) {
113 fprintf(aStream, "UNKNOWN %p\n", pc);
114 continue;
115 }
116
117 PRUint32 foff = (char*)pc - (char*)info.dli_fbase;
118
119 const char * symbol = info.dli_sname;
120 int len;
121 if (!symbol || !(len = strlen(symbol))) {
122 fprintf(aStream, "UNKNOWN [%s +0x%08X]\n",
123 info.dli_fname, foff);
124 continue;
125 }
126
127 char demangled[4096] = "\0";
128
129 DemangleSymbol(symbol, demangled, sizeof(demangled));
130
131 if (strlen(demangled)) {
132 symbol = demangled;
133 len = strlen(symbol);
134 }
135
136 PRUint32 off = (char*)pc - (char*)info.dli_saddr;
137 fprintf(aStream, "%s+0x%08X [%s +0x%08X]\n",
138 symbol, off, info.dli_fname, foff);
139 }
140 }
141}
142
143#elif defined(__sun) && (defined(__sparc) || defined(sparc) || defined(__i386) || defined(i386))
144
145/*
146 * Stack walking code for Solaris courtesy of Bart Smaalder's "memtrak".
147 */
148
149#include <synch.h>
150#include <ucontext.h>
151#include <sys/frame.h>
152#include <sys/regset.h>
153#include <sys/stack.h>
154
155static int load_address ( void * pc, void * arg, FILE * aStream );
156static int write_address_file ( void * pc );
157static struct bucket * newbucket ( void * pc );
158static struct frame * cs_getmyframeptr ( void );
159static void cs_walk_stack ( void * (*read_func)(char * address),
160 struct frame * fp,
161 int (*operate_func)(void *, void *),
162 void * usrarg, FILE * aStream );
163static void cs_operate ( void (*operate_func)(void *, void *),
164 void * usrarg, FILE * aStream );
165
166#ifndef STACK_BIAS
167#define STACK_BIAS 0
168#endif /*STACK_BIAS*/
169
170#define LOGSIZE 4096
171
172/* type of demangling function */
173typedef int demf_t(const char *, char *, size_t);
174
175static demf_t *demf;
176
177static int initialized = 0;
178
179#if defined(sparc) || defined(__sparc)
180#define FRAME_PTR_REGISTER REG_SP
181#endif
182
183#if defined(i386) || defined(__i386)
184#define FRAME_PTR_REGISTER EBP
185#endif
186
187struct bucket {
188 void * pc;
189 int index;
190 struct bucket * next;
191};
192
193struct mybuf {
194 char * buffer;
195 int chars_left;
196};
197
198
199static void myinit();
200
201#pragma init (myinit)
202
203static void
204myinit()
205{
206
207 if (! initialized) {
208#ifndef __GNUC__
209 void *handle;
210 const char *libdem = "libdemangle.so.1";
211
212 /* load libdemangle if we can and need to (only try this once) */
213 if ((handle = dlopen(libdem, RTLD_LAZY)) != NULL) {
214 demf = (demf_t *)dlsym(handle,
215 "cplus_demangle"); /*lint !e611 */
216 /*
217 * lint override above is to prevent lint from
218 * complaining about "suspicious cast".
219 */
220 }
221#endif /*__GNUC__*/
222 }
223 initialized = 1;
224}
225
226
227static int
228write_address_file(void * pc, FILE* aStream)
229{
230 static struct bucket table[2048];
231 static mutex_t lock;
232 struct bucket * ptr;
233
234 unsigned int val = NS_PTR_TO_INT32(pc);
235
236 ptr = table + ((val >> 2)&2047);
237
238 mutex_lock(&lock);
239 while (ptr->next) {
240 if (ptr->next->pc == pc)
241 break;
242 ptr = ptr->next;
243 }
244
245 if (ptr->next) {
246 mutex_unlock(&lock);
247 return (ptr->next->index);
248 } else {
249 char buffer[4096], dembuff[4096];
250 Dl_info info;
251 const char *func = "??", *lib = "??";
252
253 ptr->next = newbucket(pc);
254 mutex_unlock(&lock);
255
256 if (dladdr(pc, & info)) {
257 if (info.dli_fname)
258 lib = info.dli_fname;
259 if (info.dli_sname)
260 func = info.dli_sname;
261 }
262
263#ifdef __GNUC__
264 DemangleSymbol(func, dembuff, sizeof(dembuff));
265#else
266 if (!demf || demf(func, dembuff, sizeof (dembuff)))
267 dembuff[0] = 0;
268#endif /*__GNUC__*/
269 if (strlen(dembuff)) {
270 func = dembuff;
271 }
272 fprintf(aStream, "%u %s:%s+0x%x\n",
273 ptr->next->index,
274 lib,
275 func,
276 (char *)pc - (char*)info.dli_saddr);
277
278 return (ptr->next->index);
279 }
280}
281
282
283static int
284load_address(void * pc, void * arg, FILE * aStream)
285{
286 struct mybuf * buf = (struct mybuf *) arg;
287
288 char name[80];
289 int len;
290
291 sprintf(name, " %u", write_address_file(pc, aStream));
292
293 len = strlen(name);
294
295 if (len >= buf->chars_left)
296 return (1);
297
298 strcat(buf->buffer, name);
299
300 buf->chars_left -= len;
301
302 return (0);
303}
304
305
306static struct bucket *
307newbucket(void * pc)
308{
309 struct bucket * ptr = (struct bucket *) malloc(sizeof (*ptr));
310 static int index; /* protected by lock in caller */
311
312 ptr->index = index++;
313 ptr->next = NULL;
314 ptr->pc = pc;
315 return (ptr);
316}
317
318
319static struct frame *
320csgetframeptr()
321{
322 ucontext_t u;
323 struct frame *fp;
324
325 (void) getcontext(&u);
326
327 fp = (struct frame *)
328 ((char *)u.uc_mcontext.gregs[FRAME_PTR_REGISTER] +
329 STACK_BIAS);
330
331 /* make sure to return parents frame pointer.... */
332
333 return ((struct frame *)((ulong_t)fp->fr_savfp + STACK_BIAS));
334}
335
336
337static void
338cswalkstack(struct frame *fp, int (*operate_func)(void *, void *, FILE *),
339 void *usrarg, FILE * aStream)
340{
341
342 while (fp != 0 && fp->fr_savpc != 0) {
343
344 if (operate_func((void *)fp->fr_savpc, usrarg, aStream) != 0)
345 break;
346 /*
347 * watch out - libthread stacks look funny at the top
348 * so they may not have their STACK_BIAS set
349 */
350
351 fp = (struct frame *)((ulong_t)fp->fr_savfp +
352 (fp->fr_savfp?(ulong_t)STACK_BIAS:0));
353 }
354}
355
356
357static void
358cs_operate(int (*operate_func)(void *, void *, FILE *), void * usrarg, FILE *aStream)
359{
360 cswalkstack(csgetframeptr(), operate_func, usrarg, aStream);
361}
362
363void DumpStackToFile(FILE* aStream)
364{
365 char buffer[LOGSIZE];
366 struct mybuf mybuf;
367
368 if (!initialized)
369 myinit();
370
371 mybuf.chars_left = LOGSIZE - strlen(buffer)-1;
372 mybuf.buffer = buffer;
373 cs_operate(load_address, &mybuf, aStream);
374}
375#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette