Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / xpcom / base / nsStackWalk.cpp
blob3e26d76681e2f26cd6f984f95023e49071570aab
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set shiftwidth=4 tabstop=8 autoindent cindent expandtab: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
16 * The Original Code is Mozilla stack walking code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2000
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Michael Judge, 20-December-2000
25 * L. David Baron <dbaron@dbaron.org>, Mozilla Corporation
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 /* API for getting a stack trace of the C/C++ stack on the current thread */
43 #include "nsStackWalk.h"
45 #if defined(_WIN32) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_IA64)) && !defined(WINCE) // WIN32 x86 stack walking code
47 #include "nscore.h"
48 #include <windows.h>
49 #include <process.h>
50 #include <stdio.h>
51 #include "plstr.h"
52 #include "nsMemory.h" // for NS_ARRAY_LENGTH
54 #include "nspr.h"
55 #ifdef _M_IX86
56 #include <imagehlp.h>
57 // We need a way to know if we are building for WXP (or later), as if we are, we
58 // need to use the newer 64-bit APIs. API_VERSION_NUMBER seems to fit the bill.
59 // A value of 9 indicates we want to use the new APIs.
60 #if API_VERSION_NUMBER >= 9
61 #define USING_WXP_VERSION 1
62 #endif
63 #endif
65 // Define these as static pointers so that we can load the DLL on the
66 // fly (and not introduce a link-time dependency on it). Tip o' the
67 // hat to Matt Pietrick for this idea. See:
69 // http://msdn.microsoft.com/library/periodic/period97/F1/D3/S245C6.htm
71 PR_BEGIN_EXTERN_C
73 typedef DWORD (__stdcall *SYMSETOPTIONSPROC)(DWORD);
74 extern SYMSETOPTIONSPROC _SymSetOptions;
76 typedef BOOL (__stdcall *SYMINITIALIZEPROC)(HANDLE, LPSTR, BOOL);
77 extern SYMINITIALIZEPROC _SymInitialize;
79 typedef BOOL (__stdcall *SYMCLEANUPPROC)(HANDLE);
80 extern SYMCLEANUPPROC _SymCleanup;
82 typedef BOOL (__stdcall *STACKWALKPROC)(DWORD,
83 HANDLE,
84 HANDLE,
85 LPSTACKFRAME,
86 LPVOID,
87 PREAD_PROCESS_MEMORY_ROUTINE,
88 PFUNCTION_TABLE_ACCESS_ROUTINE,
89 PGET_MODULE_BASE_ROUTINE,
90 PTRANSLATE_ADDRESS_ROUTINE);
91 extern STACKWALKPROC _StackWalk;
93 #ifdef USING_WXP_VERSION
94 typedef BOOL (__stdcall *STACKWALKPROC64)(DWORD,
95 HANDLE,
96 HANDLE,
97 LPSTACKFRAME64,
98 PVOID,
99 PREAD_PROCESS_MEMORY_ROUTINE64,
100 PFUNCTION_TABLE_ACCESS_ROUTINE64,
101 PGET_MODULE_BASE_ROUTINE64,
102 PTRANSLATE_ADDRESS_ROUTINE64);
103 extern STACKWALKPROC64 _StackWalk64;
104 #endif
106 typedef LPVOID (__stdcall *SYMFUNCTIONTABLEACCESSPROC)(HANDLE, DWORD);
107 extern SYMFUNCTIONTABLEACCESSPROC _SymFunctionTableAccess;
109 #ifdef USING_WXP_VERSION
110 typedef LPVOID (__stdcall *SYMFUNCTIONTABLEACCESSPROC64)(HANDLE, DWORD64);
111 extern SYMFUNCTIONTABLEACCESSPROC64 _SymFunctionTableAccess64;
112 #endif
114 typedef DWORD (__stdcall *SYMGETMODULEBASEPROC)(HANDLE, DWORD);
115 extern SYMGETMODULEBASEPROC _SymGetModuleBase;
117 #ifdef USING_WXP_VERSION
118 typedef DWORD64 (__stdcall *SYMGETMODULEBASEPROC64)(HANDLE, DWORD64);
119 extern SYMGETMODULEBASEPROC64 _SymGetModuleBase64;
120 #endif
122 typedef BOOL (__stdcall *SYMGETSYMFROMADDRPROC)(HANDLE, DWORD, PDWORD, PIMAGEHLP_SYMBOL);
123 extern SYMGETSYMFROMADDRPROC _SymGetSymFromAddr;
125 #ifdef USING_WXP_VERSION
126 typedef BOOL (__stdcall *SYMFROMADDRPROC)(HANDLE, DWORD64, PDWORD64, PSYMBOL_INFO);
127 extern SYMFROMADDRPROC _SymFromAddr;
128 #endif
130 typedef DWORD ( __stdcall *SYMLOADMODULE)(HANDLE, HANDLE, PSTR, PSTR, DWORD, DWORD);
131 extern SYMLOADMODULE _SymLoadModule;
133 #ifdef USING_WXP_VERSION
134 typedef DWORD ( __stdcall *SYMLOADMODULE64)(HANDLE, HANDLE, PCSTR, PCSTR, DWORD64, DWORD);
135 extern SYMLOADMODULE64 _SymLoadModule64;
136 #endif
138 typedef DWORD ( __stdcall *SYMUNDNAME)(PIMAGEHLP_SYMBOL, PSTR, DWORD);
139 extern SYMUNDNAME _SymUnDName;
141 typedef DWORD ( __stdcall *SYMGETMODULEINFO)( HANDLE, DWORD, PIMAGEHLP_MODULE);
142 extern SYMGETMODULEINFO _SymGetModuleInfo;
144 #ifdef USING_WXP_VERSION
145 typedef BOOL ( __stdcall *SYMGETMODULEINFO64)( HANDLE, DWORD64, PIMAGEHLP_MODULE64);
146 extern SYMGETMODULEINFO64 _SymGetModuleInfo64;
147 #endif
149 typedef BOOL ( __stdcall *ENUMLOADEDMODULES)( HANDLE, PENUMLOADED_MODULES_CALLBACK, PVOID);
150 extern ENUMLOADEDMODULES _EnumerateLoadedModules;
152 #ifdef USING_WXP_VERSION
153 typedef BOOL ( __stdcall *ENUMLOADEDMODULES64)( HANDLE, PENUMLOADED_MODULES_CALLBACK64, PVOID);
154 extern ENUMLOADEDMODULES64 _EnumerateLoadedModules64;
155 #endif
157 typedef BOOL (__stdcall *SYMGETLINEFROMADDRPROC)(HANDLE, DWORD, PDWORD, PIMAGEHLP_LINE);
158 extern SYMGETLINEFROMADDRPROC _SymGetLineFromAddr;
160 #ifdef USING_WXP_VERSION
161 typedef BOOL (__stdcall *SYMGETLINEFROMADDRPROC64)(HANDLE, DWORD64, PDWORD, PIMAGEHLP_LINE64);
162 extern SYMGETLINEFROMADDRPROC64 _SymGetLineFromAddr64;
163 #endif
165 extern HANDLE hStackWalkMutex;
167 HANDLE GetCurrentPIDorHandle();
169 PRBool EnsureSymInitialized();
171 PRBool EnsureImageHlpInitialized();
174 * SymGetModuleInfoEspecial
176 * Attempt to determine the module information.
177 * Bug 112196 says this DLL may not have been loaded at the time
178 * SymInitialize was called, and thus the module information
179 * and symbol information is not available.
180 * This code rectifies that problem.
181 * Line information is optional.
183 BOOL SymGetModuleInfoEspecial(HANDLE aProcess, DWORD aAddr, PIMAGEHLP_MODULE aModuleInfo, PIMAGEHLP_LINE aLineInfo);
185 struct WalkStackData {
186 PRUint32 skipFrames;
187 HANDLE thread;
188 HANDLE process;
189 HANDLE eventStart;
190 HANDLE eventEnd;
191 void **pcs;
192 PRUint32 pc_size;
193 PRUint32 pc_count;
196 void PrintError(char *prefix, WalkStackData* data);
197 unsigned int WINAPI WalkStackThread(void* data);
198 void WalkStackMain64(struct WalkStackData* data);
199 void WalkStackMain(struct WalkStackData* data);
202 // Define these as static pointers so that we can load the DLL on the
203 // fly (and not introduce a link-time dependency on it). Tip o' the
204 // hat to Matt Pietrick for this idea. See:
206 // http://msdn.microsoft.com/library/periodic/period97/F1/D3/S245C6.htm
209 SYMSETOPTIONSPROC _SymSetOptions;
211 SYMINITIALIZEPROC _SymInitialize;
213 SYMCLEANUPPROC _SymCleanup;
215 STACKWALKPROC _StackWalk;
216 #ifdef USING_WXP_VERSION
217 STACKWALKPROC64 _StackWalk64;
218 #else
219 #define _StackWalk64 0
220 #endif
222 SYMFUNCTIONTABLEACCESSPROC _SymFunctionTableAccess;
223 #ifdef USING_WXP_VERSION
224 SYMFUNCTIONTABLEACCESSPROC64 _SymFunctionTableAccess64;
225 #else
226 #define _SymFunctionTableAccess64 0
227 #endif
229 SYMGETMODULEBASEPROC _SymGetModuleBase;
230 #ifdef USING_WXP_VERSION
231 SYMGETMODULEBASEPROC64 _SymGetModuleBase64;
232 #else
233 #define _SymGetModuleBase64 0
234 #endif
236 SYMGETSYMFROMADDRPROC _SymGetSymFromAddr;
237 #ifdef USING_WXP_VERSION
238 SYMFROMADDRPROC _SymFromAddr;
239 #else
240 #define _SymFromAddr 0
241 #endif
243 SYMLOADMODULE _SymLoadModule;
244 #ifdef USING_WXP_VERSION
245 SYMLOADMODULE64 _SymLoadModule64;
246 #else
247 #define _SymLoadModule64 0
248 #endif
250 SYMUNDNAME _SymUnDName;
252 SYMGETMODULEINFO _SymGetModuleInfo;
253 #ifdef USING_WXP_VERSION
254 SYMGETMODULEINFO64 _SymGetModuleInfo64;
255 #else
256 #define _SymGetModuleInfo64 0
257 #endif
259 ENUMLOADEDMODULES _EnumerateLoadedModules;
260 #ifdef USING_WXP_VERSION
261 ENUMLOADEDMODULES64 _EnumerateLoadedModules64;
262 #else
263 #define _EnumerateLoadedModules64 0
264 #endif
266 SYMGETLINEFROMADDRPROC _SymGetLineFromAddr;
267 #ifdef USING_WXP_VERSION
268 SYMGETLINEFROMADDRPROC64 _SymGetLineFromAddr64;
269 #else
270 #define _SymGetLineFromAddr64 0
271 #endif
273 DWORD gStackWalkThread;
274 CRITICAL_SECTION gDbgHelpCS;
276 PR_END_EXTERN_C
278 // Routine to print an error message to standard error.
279 // Will also call callback with error, if data supplied.
280 void PrintError(char *prefix)
282 LPVOID lpMsgBuf;
283 DWORD lastErr = GetLastError();
284 FormatMessage(
285 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
286 NULL,
287 lastErr,
288 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
289 (LPTSTR) &lpMsgBuf,
291 NULL
293 fprintf(stderr, "### ERROR: %s: %s",
294 prefix, lpMsgBuf ? lpMsgBuf : "(null)\n");
295 fflush(stderr);
296 LocalFree( lpMsgBuf );
299 PRBool
300 EnsureImageHlpInitialized()
302 static PRBool gInitialized = PR_FALSE;
304 if (gInitialized)
305 return gInitialized;
307 // Hope that our first call doesn't happen during static
308 // initialization. If it does, this CreateThread call won't
309 // actually start the thread until after the static initialization
310 // is done, which means we'll deadlock while waiting for it to
311 // process a stack.
312 HANDLE readyEvent = ::CreateEvent(NULL, FALSE /* auto-reset*/,
313 FALSE /* initially non-signaled */, NULL);
314 unsigned int threadID;
315 HANDLE hStackWalkThread = (HANDLE)
316 _beginthreadex(NULL, 0, WalkStackThread, (void*)readyEvent,
317 0, &threadID);
318 gStackWalkThread = threadID;
319 if (hStackWalkThread == NULL) {
320 PrintError("CreateThread");
321 return PR_FALSE;
323 ::CloseHandle(hStackWalkThread);
325 // Wait for the thread's event loop to start before posting events to it.
326 ::WaitForSingleObject(readyEvent, INFINITE);
327 ::CloseHandle(readyEvent);
329 ::InitializeCriticalSection(&gDbgHelpCS);
331 HMODULE module = ::LoadLibraryW(L"DBGHELP.DLL");
332 if (!module) {
333 module = ::LoadLibraryW(L"IMAGEHLP.DLL");
334 if (!module) return PR_FALSE;
337 _SymSetOptions = (SYMSETOPTIONSPROC) ::GetProcAddress(module, "SymSetOptions");
338 if (!_SymSetOptions) return PR_FALSE;
340 _SymInitialize = (SYMINITIALIZEPROC) ::GetProcAddress(module, "SymInitialize");
341 if (!_SymInitialize) return PR_FALSE;
343 _SymCleanup = (SYMCLEANUPPROC)GetProcAddress(module, "SymCleanup");
344 if (!_SymCleanup) return PR_FALSE;
346 #ifdef USING_WXP_VERSION
347 _StackWalk64 = (STACKWALKPROC64)GetProcAddress(module, "StackWalk64");
348 #endif
349 _StackWalk = (STACKWALKPROC)GetProcAddress(module, "StackWalk");
350 if (!_StackWalk64 && !_StackWalk) return PR_FALSE;
352 #ifdef USING_WXP_VERSION
353 _SymFunctionTableAccess64 = (SYMFUNCTIONTABLEACCESSPROC64) GetProcAddress(module, "SymFunctionTableAccess64");
354 #endif
355 _SymFunctionTableAccess = (SYMFUNCTIONTABLEACCESSPROC) GetProcAddress(module, "SymFunctionTableAccess");
356 if (!_SymFunctionTableAccess64 && !_SymFunctionTableAccess) return PR_FALSE;
358 #ifdef USING_WXP_VERSION
359 _SymGetModuleBase64 = (SYMGETMODULEBASEPROC64)GetProcAddress(module, "SymGetModuleBase64");
360 #endif
361 _SymGetModuleBase = (SYMGETMODULEBASEPROC)GetProcAddress(module, "SymGetModuleBase");
362 if (!_SymGetModuleBase64 && !_SymGetModuleBase) return PR_FALSE;
364 _SymGetSymFromAddr = (SYMGETSYMFROMADDRPROC)GetProcAddress(module, "SymGetSymFromAddr");
365 #ifdef USING_WXP_VERSION
366 _SymFromAddr = (SYMFROMADDRPROC)GetProcAddress(module, "SymFromAddr");
367 #endif
368 if (!_SymFromAddr && !_SymGetSymFromAddr) return PR_FALSE;
370 #ifdef USING_WXP_VERSION
371 _SymLoadModule64 = (SYMLOADMODULE64)GetProcAddress(module, "SymLoadModule64");
372 #endif
373 _SymLoadModule = (SYMLOADMODULE)GetProcAddress(module, "SymLoadModule");
374 if (!_SymLoadModule64 && !_SymLoadModule) return PR_FALSE;
376 _SymUnDName = (SYMUNDNAME)GetProcAddress(module, "SymUnDName");
377 if (!_SymUnDName) return PR_FALSE;
379 #ifdef USING_WXP_VERSION
380 _SymGetModuleInfo64 = (SYMGETMODULEINFO64)GetProcAddress(module, "SymGetModuleInfo64");
381 #endif
382 _SymGetModuleInfo = (SYMGETMODULEINFO)GetProcAddress(module, "SymGetModuleInfo");
383 if (!_SymGetModuleInfo64 && !_SymGetModuleInfo) return PR_FALSE;
385 #ifdef USING_WXP_VERSION
386 _EnumerateLoadedModules64 = (ENUMLOADEDMODULES64)GetProcAddress(module, "EnumerateLoadedModules64");
387 #endif
388 _EnumerateLoadedModules = (ENUMLOADEDMODULES)GetProcAddress(module, "EnumerateLoadedModules");
389 if (!_EnumerateLoadedModules64 && !_EnumerateLoadedModules) return PR_FALSE;
391 #ifdef USING_WXP_VERSION
392 _SymGetLineFromAddr64 = (SYMGETLINEFROMADDRPROC64)GetProcAddress(module, "SymGetLineFromAddr64");
393 #endif
394 _SymGetLineFromAddr = (SYMGETLINEFROMADDRPROC)GetProcAddress(module, "SymGetLineFromAddr");
395 if (!_SymGetLineFromAddr64 && !_SymGetLineFromAddr) return PR_FALSE;
397 return gInitialized = PR_TRUE;
400 void
401 WalkStackMain64(struct WalkStackData* data)
403 #ifdef USING_WXP_VERSION
404 // Get the context information for the thread. That way we will
405 // know where our sp, fp, pc, etc. are and can fill in the
406 // STACKFRAME64 with the initial values.
407 CONTEXT context;
408 HANDLE myProcess = data->process;
409 HANDLE myThread = data->thread;
410 DWORD64 addr;
411 STACKFRAME64 frame64;
412 int skip = 3 + data->skipFrames; // skip our own stack walking frames
413 BOOL ok;
415 // Get a context for the specified thread.
416 memset(&context, 0, sizeof(CONTEXT));
417 context.ContextFlags = CONTEXT_FULL;
418 if (!GetThreadContext(myThread, &context)) {
419 PrintError("GetThreadContext");
420 return;
423 // Setup initial stack frame to walk from
424 memset(&frame64, 0, sizeof(frame64));
425 #ifdef _M_IX86
426 frame64.AddrPC.Offset = context.Eip;
427 frame64.AddrStack.Offset = context.Esp;
428 frame64.AddrFrame.Offset = context.Ebp;
429 #elif defined _M_AMD64
430 frame64.AddrPC.Offset = context.Rip;
431 frame64.AddrStack.Offset = context.Rsp;
432 frame64.AddrFrame.Offset = context.Rbp;
433 #elif defined _M_IA64
434 frame64.AddrPC.Offset = context.StIIP;
435 frame64.AddrStack.Offset = context.SP;
436 frame64.AddrFrame.Offset = context.RsBSP;
437 #else
438 #error "Should not have compiled this code"
439 #endif
440 frame64.AddrPC.Mode = AddrModeFlat;
441 frame64.AddrStack.Mode = AddrModeFlat;
442 frame64.AddrFrame.Mode = AddrModeFlat;
443 frame64.AddrReturn.Mode = AddrModeFlat;
445 // Now walk the stack
446 while (1) {
448 // debug routines are not threadsafe, so grab the lock.
449 EnterCriticalSection(&gDbgHelpCS);
450 ok = _StackWalk64(
451 #ifdef _M_AMD64
452 IMAGE_FILE_MACHINE_AMD64,
453 #elif defined _M_IA64
454 IMAGE_FILE_MACHINE_IA64,
455 #elif defined _M_IX86
456 IMAGE_FILE_MACHINE_I386,
457 #else
458 #error "Should not have compiled this code"
459 #endif
460 myProcess,
461 myThread,
462 &frame64,
463 &context,
464 NULL,
465 _SymFunctionTableAccess64, // function table access routine
466 _SymGetModuleBase64, // module base routine
469 LeaveCriticalSection(&gDbgHelpCS);
471 if (ok)
472 addr = frame64.AddrPC.Offset;
473 else {
474 addr = 0;
475 PrintError("WalkStack64");
478 if (!ok || (addr == 0)) {
479 break;
482 if (skip-- > 0) {
483 continue;
486 if (data->pc_count < data->pc_size)
487 data->pcs[data->pc_count] = (void*)addr;
488 ++data->pc_count;
490 if (frame64.AddrReturn.Offset == 0)
491 break;
493 return;
494 #endif
498 void
499 WalkStackMain(struct WalkStackData* data)
501 // Get the context information for the thread. That way we will
502 // know where our sp, fp, pc, etc. are and can fill in the
503 // STACKFRAME with the initial values.
504 CONTEXT context;
505 HANDLE myProcess = data->process;
506 HANDLE myThread = data->thread;
507 DWORD addr;
508 STACKFRAME frame;
509 int skip = data->skipFrames; // skip our own stack walking frames
510 BOOL ok;
512 // Get a context for the specified thread.
513 memset(&context, 0, sizeof(CONTEXT));
514 context.ContextFlags = CONTEXT_FULL;
515 if (!GetThreadContext(myThread, &context)) {
516 PrintError("GetThreadContext");
517 return;
520 // Setup initial stack frame to walk from
521 #if defined _M_IX86
522 memset(&frame, 0, sizeof(frame));
523 frame.AddrPC.Offset = context.Eip;
524 frame.AddrPC.Mode = AddrModeFlat;
525 frame.AddrStack.Offset = context.Esp;
526 frame.AddrStack.Mode = AddrModeFlat;
527 frame.AddrFrame.Offset = context.Ebp;
528 frame.AddrFrame.Mode = AddrModeFlat;
529 #else
530 PrintError("Unknown platform. No stack walking.");
531 return;
532 #endif
534 // Now walk the stack
535 while (1) {
537 // debug routines are not threadsafe, so grab the lock.
538 EnterCriticalSection(&gDbgHelpCS);
539 ok = _StackWalk(
540 IMAGE_FILE_MACHINE_I386,
541 myProcess,
542 myThread,
543 &frame,
544 &context,
545 0, // read process memory routine
546 _SymFunctionTableAccess, // function table access routine
547 _SymGetModuleBase, // module base routine
548 0 // translate address routine
550 LeaveCriticalSection(&gDbgHelpCS);
552 if (ok)
553 addr = frame.AddrPC.Offset;
554 else {
555 addr = 0;
556 PrintError("WalkStack");
559 if (!ok || (addr == 0)) {
560 break;
563 if (skip-- > 0) {
564 continue;
567 if (data->pc_count < data->pc_size)
568 data->pcs[data->pc_count] = (void*)addr;
569 ++data->pc_count;
571 if (frame.AddrReturn.Offset == 0)
572 break;
575 return;
579 unsigned int WINAPI
580 WalkStackThread(void* aData)
582 BOOL msgRet;
583 MSG msg;
585 // Call PeekMessage to force creation of a message queue so that
586 // other threads can safely post events to us.
587 ::PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
589 // and tell the thread that created us that we're ready.
590 HANDLE readyEvent = (HANDLE)aData;
591 ::SetEvent(readyEvent);
593 while ((msgRet = ::GetMessage(&msg, (HWND)-1, 0, 0)) != 0) {
594 if (msgRet == -1) {
595 PrintError("GetMessage");
596 } else {
597 DWORD ret;
599 struct WalkStackData *data = (WalkStackData *)msg.lParam;
601 // Don't suspend the calling thread until it's waiting for
602 // us; otherwise the number of frames on the stack could vary.
603 ret = ::WaitForSingleObject(data->eventStart, INFINITE);
604 if (ret != WAIT_OBJECT_0)
605 PrintError("WaitForSingleObject");
607 // Suspend the calling thread, dump his stack, and then resume him.
608 // He's currently waiting for us to finish so now should be a good time.
609 ret = ::SuspendThread( data->thread );
610 if (ret == -1) {
611 PrintError("ThreadSuspend");
613 else {
614 if (_StackWalk64)
615 WalkStackMain64(data);
616 else
617 WalkStackMain(data);
619 ret = ::ResumeThread(data->thread);
620 if (ret == -1) {
621 PrintError("ThreadResume");
625 ::SetEvent(data->eventEnd);
629 return 0;
633 * Walk the stack, translating PC's found into strings and recording the
634 * chain in aBuffer. For this to work properly, the DLLs must be rebased
635 * so that the address in the file agrees with the address in memory.
636 * Otherwise StackWalk will return FALSE when it hits a frame in a DLL
637 * whose in memory address doesn't match its in-file address.
640 EXPORT_XPCOM_API(nsresult)
641 NS_StackWalk(NS_WalkStackCallback aCallback, PRUint32 aSkipFrames,
642 void *aClosure)
644 HANDLE myProcess, myThread;
645 DWORD walkerReturn;
646 struct WalkStackData data;
648 if (!EnsureImageHlpInitialized())
649 return PR_FALSE;
651 // Have to duplicate handle to get a real handle.
652 if (!::DuplicateHandle(::GetCurrentProcess(),
653 ::GetCurrentProcess(),
654 ::GetCurrentProcess(),
655 &myProcess,
656 PROCESS_ALL_ACCESS, FALSE, 0)) {
657 PrintError("DuplicateHandle (process)");
658 return NS_ERROR_FAILURE;
660 if (!::DuplicateHandle(::GetCurrentProcess(),
661 ::GetCurrentThread(),
662 ::GetCurrentProcess(),
663 &myThread,
664 THREAD_ALL_ACCESS, FALSE, 0)) {
665 PrintError("DuplicateHandle (thread)");
666 ::CloseHandle(myProcess);
667 return NS_ERROR_FAILURE;
670 data.skipFrames = aSkipFrames;
671 data.thread = myThread;
672 data.process = myProcess;
673 data.eventStart = ::CreateEvent(NULL, FALSE /* auto-reset*/,
674 FALSE /* initially non-signaled */, NULL);
675 data.eventEnd = ::CreateEvent(NULL, FALSE /* auto-reset*/,
676 FALSE /* initially non-signaled */, NULL);
677 void *local_pcs[1024];
678 data.pcs = local_pcs;
679 data.pc_count = 0;
680 data.pc_size = NS_ARRAY_LENGTH(local_pcs);
682 ::PostThreadMessage(gStackWalkThread, WM_USER, 0, (LPARAM)&data);
684 walkerReturn = ::SignalObjectAndWait(data.eventStart,
685 data.eventEnd, INFINITE, FALSE);
686 if (walkerReturn != WAIT_OBJECT_0)
687 PrintError("SignalObjectAndWait (1)");
688 if (data.pc_count > data.pc_size) {
689 data.pcs = (void**) malloc(data.pc_count * sizeof(void*));
690 data.pc_size = data.pc_count;
691 data.pc_count = 0;
692 ::PostThreadMessage(gStackWalkThread, WM_USER, 0, (LPARAM)&data);
693 walkerReturn = ::SignalObjectAndWait(data.eventStart,
694 data.eventEnd, INFINITE, FALSE);
695 if (walkerReturn != WAIT_OBJECT_0)
696 PrintError("SignalObjectAndWait (2)");
699 ::CloseHandle(myThread);
700 ::CloseHandle(myProcess);
701 ::CloseHandle(data.eventStart);
702 ::CloseHandle(data.eventEnd);
704 for (PRUint32 i = 0; i < data.pc_count; ++i)
705 (*aCallback)(data.pcs[i], aClosure);
707 if (data.pc_size > NS_ARRAY_LENGTH(local_pcs))
708 free(data.pcs);
710 return NS_OK;
714 static BOOL CALLBACK callbackEspecial(
715 PCSTR aModuleName,
716 ULONG aModuleBase,
717 ULONG aModuleSize,
718 PVOID aUserContext)
720 BOOL retval = TRUE;
721 DWORD addr = *(DWORD*)aUserContext;
724 * You'll want to control this if we are running on an
725 * architecture where the addresses go the other direction.
726 * Not sure this is even a realistic consideration.
728 const BOOL addressIncreases = TRUE;
731 * If it falls inside the known range, load the symbols.
733 if (addressIncreases
734 ? (addr >= aModuleBase && addr <= (aModuleBase + aModuleSize))
735 : (addr <= aModuleBase && addr >= (aModuleBase - aModuleSize))
737 retval = _SymLoadModule(GetCurrentProcess(), NULL, (PSTR)aModuleName, NULL, aModuleBase, aModuleSize);
738 if (!retval)
739 PrintError("SymLoadModule");
742 return retval;
745 static BOOL CALLBACK callbackEspecial64(
746 PCSTR aModuleName,
747 DWORD64 aModuleBase,
748 ULONG aModuleSize,
749 PVOID aUserContext)
751 #ifdef USING_WXP_VERSION
752 BOOL retval = TRUE;
753 DWORD64 addr = *(DWORD64*)aUserContext;
756 * You'll want to control this if we are running on an
757 * architecture where the addresses go the other direction.
758 * Not sure this is even a realistic consideration.
760 const BOOL addressIncreases = TRUE;
763 * If it falls in side the known range, load the symbols.
765 if (addressIncreases
766 ? (addr >= aModuleBase && addr <= (aModuleBase + aModuleSize))
767 : (addr <= aModuleBase && addr >= (aModuleBase - aModuleSize))
769 retval = _SymLoadModule64(GetCurrentProcess(), NULL, (PSTR)aModuleName, NULL, aModuleBase, aModuleSize);
770 if (!retval)
771 PrintError("SymLoadModule64");
774 return retval;
775 #else
776 return FALSE;
777 #endif
781 * SymGetModuleInfoEspecial
783 * Attempt to determine the module information.
784 * Bug 112196 says this DLL may not have been loaded at the time
785 * SymInitialize was called, and thus the module information
786 * and symbol information is not available.
787 * This code rectifies that problem.
789 BOOL SymGetModuleInfoEspecial(HANDLE aProcess, DWORD aAddr, PIMAGEHLP_MODULE aModuleInfo, PIMAGEHLP_LINE aLineInfo)
791 BOOL retval = FALSE;
794 * Init the vars if we have em.
796 aModuleInfo->SizeOfStruct = sizeof(IMAGEHLP_MODULE);
797 if (nsnull != aLineInfo) {
798 aLineInfo->SizeOfStruct = sizeof(IMAGEHLP_LINE);
802 * Give it a go.
803 * It may already be loaded.
805 retval = _SymGetModuleInfo(aProcess, aAddr, aModuleInfo);
807 if (FALSE == retval) {
808 BOOL enumRes = FALSE;
811 * Not loaded, here's the magic.
812 * Go through all the modules.
814 // Need to cast to PENUMLOADED_MODULES_CALLBACK because the
815 // constness of the first parameter of
816 // PENUMLOADED_MODULES_CALLBACK varies over SDK versions (from
817 // non-const to const over time). See bug 391848 and bug
818 // 415426.
819 enumRes = _EnumerateLoadedModules(aProcess, (PENUMLOADED_MODULES_CALLBACK)callbackEspecial, (PVOID)&aAddr);
820 if (FALSE != enumRes)
823 * One final go.
824 * If it fails, then well, we have other problems.
826 retval = _SymGetModuleInfo(aProcess, aAddr, aModuleInfo);
827 if (!retval)
828 PrintError("SymGetModuleInfo");
833 * If we got module info, we may attempt line info as well.
834 * We will not report failure if this does not work.
836 if (FALSE != retval && nsnull != aLineInfo && nsnull != _SymGetLineFromAddr) {
837 DWORD displacement = 0;
838 BOOL lineRes = FALSE;
839 lineRes = _SymGetLineFromAddr(aProcess, aAddr, &displacement, aLineInfo);
842 return retval;
845 // New members were added to IMAGEHLP_MODULE64 (that show up in the
846 // Platform SDK that ships with VC8, but not the Platform SDK that ships
847 // with VC7.1, i.e., between DbgHelp 6.0 and 6.1), but we don't need to
848 // use them, and it's useful to be able to function correctly with the
849 // older library. (Stock Windows XP SP2 seems to ship with dbghelp.dll
850 // version 5.1.) Since Platform SDK version need not correspond to
851 // compiler version, and the version number in debughlp.h was NOT bumped
852 // when these changes were made, ifdef based on a constant that was
853 // added between these versions.
854 #ifdef SSRVOPT_SETCONTEXT
855 #define NS_IMAGEHLP_MODULE64_SIZE (((offsetof(IMAGEHLP_MODULE64, LoadedPdbName) + sizeof(DWORD64) - 1) / sizeof(DWORD64)) * sizeof(DWORD64))
856 #else
857 #define NS_IMAGEHLP_MODULE64_SIZE sizeof(IMAGEHLP_MODULE64)
858 #endif
860 #ifdef USING_WXP_VERSION
861 BOOL SymGetModuleInfoEspecial64(HANDLE aProcess, DWORD64 aAddr, PIMAGEHLP_MODULE64 aModuleInfo, PIMAGEHLP_LINE64 aLineInfo)
863 BOOL retval = FALSE;
866 * Init the vars if we have em.
868 aModuleInfo->SizeOfStruct = NS_IMAGEHLP_MODULE64_SIZE;
869 if (nsnull != aLineInfo) {
870 aLineInfo->SizeOfStruct = sizeof(IMAGEHLP_LINE64);
874 * Give it a go.
875 * It may already be loaded.
877 retval = _SymGetModuleInfo64(aProcess, aAddr, aModuleInfo);
879 if (FALSE == retval) {
880 BOOL enumRes = FALSE;
883 * Not loaded, here's the magic.
884 * Go through all the modules.
886 // Need to cast to PENUMLOADED_MODULES_CALLBACK64 because the
887 // constness of the first parameter of
888 // PENUMLOADED_MODULES_CALLBACK64 varies over SDK versions (from
889 // non-const to const over time). See bug 391848 and bug
890 // 415426.
891 enumRes = _EnumerateLoadedModules64(aProcess, (PENUMLOADED_MODULES_CALLBACK64)callbackEspecial64, (PVOID)&aAddr);
892 if (FALSE != enumRes)
895 * One final go.
896 * If it fails, then well, we have other problems.
898 retval = _SymGetModuleInfo64(aProcess, aAddr, aModuleInfo);
899 if (!retval)
900 PrintError("SymGetModuleInfo64");
905 * If we got module info, we may attempt line info as well.
906 * We will not report failure if this does not work.
908 if (FALSE != retval && nsnull != aLineInfo && nsnull != _SymGetLineFromAddr64) {
909 DWORD displacement = 0;
910 BOOL lineRes = FALSE;
911 lineRes = _SymGetLineFromAddr64(aProcess, aAddr, &displacement, aLineInfo);
912 if (!lineRes) {
913 // Clear out aLineInfo to indicate that it's not valid
914 memset(aLineInfo, 0, sizeof(*aLineInfo));
918 return retval;
920 #endif
922 HANDLE
923 GetCurrentPIDorHandle()
925 if (_SymGetModuleBase64)
926 return GetCurrentProcess(); // winxp and friends use process handle
928 return (HANDLE) GetCurrentProcessId(); // winme win98 win95 etc use process identifier
931 PRBool
932 EnsureSymInitialized()
934 static PRBool gInitialized = PR_FALSE;
935 PRBool retStat;
937 if (gInitialized)
938 return gInitialized;
940 if (!EnsureImageHlpInitialized())
941 return PR_FALSE;
943 _SymSetOptions(SYMOPT_LOAD_LINES | SYMOPT_UNDNAME);
944 retStat = _SymInitialize(GetCurrentPIDorHandle(), NULL, TRUE);
945 if (!retStat)
946 PrintError("SymInitialize");
948 gInitialized = retStat;
949 /* XXX At some point we need to arrange to call _SymCleanup */
951 return retStat;
955 EXPORT_XPCOM_API(nsresult)
956 NS_DescribeCodeAddress(void *aPC, nsCodeAddressDetails *aDetails)
958 aDetails->library[0] = '\0';
959 aDetails->loffset = 0;
960 aDetails->filename[0] = '\0';
961 aDetails->lineno = 0;
962 aDetails->function[0] = '\0';
963 aDetails->foffset = 0;
965 if (!EnsureSymInitialized())
966 return NS_ERROR_FAILURE;
968 HANDLE myProcess = ::GetCurrentProcess();
969 BOOL ok;
971 // debug routines are not threadsafe, so grab the lock.
972 EnterCriticalSection(&gDbgHelpCS);
974 #ifdef USING_WXP_VERSION
975 if (_StackWalk64) {
977 // Attempt to load module info before we attempt to resolve the symbol.
978 // This just makes sure we get good info if available.
981 DWORD64 addr = (DWORD64)aPC;
982 IMAGEHLP_MODULE64 modInfo;
983 IMAGEHLP_LINE64 lineInfo;
984 BOOL modInfoRes;
985 modInfoRes = SymGetModuleInfoEspecial64(myProcess, addr, &modInfo, &lineInfo);
987 if (modInfoRes) {
988 PL_strncpyz(aDetails->library, modInfo.ModuleName,
989 sizeof(aDetails->library));
990 aDetails->loffset = (char*) aPC - (char*) modInfo.BaseOfImage;
992 if (lineInfo.FileName) {
993 PL_strncpyz(aDetails->filename, lineInfo.FileName,
994 sizeof(aDetails->filename));
995 aDetails->lineno = lineInfo.LineNumber;
999 ULONG64 buffer[(sizeof(SYMBOL_INFO) +
1000 MAX_SYM_NAME*sizeof(TCHAR) + sizeof(ULONG64) - 1) / sizeof(ULONG64)];
1001 PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
1002 pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
1003 pSymbol->MaxNameLen = MAX_SYM_NAME;
1005 DWORD64 displacement;
1006 ok = _SymFromAddr && _SymFromAddr(myProcess, addr, &displacement, pSymbol);
1008 if (ok) {
1009 PL_strncpyz(aDetails->function, pSymbol->Name,
1010 sizeof(aDetails->function));
1011 aDetails->foffset = displacement;
1013 } else
1014 #endif
1017 // Attempt to load module info before we attempt to resolve the symbol.
1018 // This just makes sure we get good info if available.
1021 DWORD addr = (DWORD)aPC;
1022 IMAGEHLP_MODULE modInfo;
1023 IMAGEHLP_LINE lineInfo;
1024 BOOL modInfoRes;
1025 modInfoRes = SymGetModuleInfoEspecial(myProcess, addr, &modInfo, &lineInfo);
1027 if (modInfoRes) {
1028 PL_strncpyz(aDetails->library, modInfo.ModuleName,
1029 sizeof(aDetails->library));
1030 aDetails->loffset = (char*) aPC - (char*) modInfo.BaseOfImage;
1031 PL_strncpyz(aDetails->filename, lineInfo.FileName,
1032 sizeof(aDetails->filename));
1033 aDetails->lineno = lineInfo.LineNumber;
1036 #ifdef USING_WXP_VERSION
1037 ULONG64 buffer[(sizeof(SYMBOL_INFO) +
1038 MAX_SYM_NAME*sizeof(TCHAR) + sizeof(ULONG64) - 1) / sizeof(ULONG64)];
1039 PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
1040 pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
1041 pSymbol->MaxNameLen = MAX_SYM_NAME;
1043 DWORD64 displacement;
1045 ok = _SymFromAddr && _SymFromAddr(myProcess, addr, &displacement, pSymbol);
1046 #else
1047 char buf[sizeof(IMAGEHLP_SYMBOL) + 512];
1048 PIMAGEHLP_SYMBOL pSymbol = (PIMAGEHLP_SYMBOL) buf;
1049 pSymbol->SizeOfStruct = sizeof(buf);
1050 pSymbol->MaxNameLength = 512;
1052 DWORD displacement;
1054 ok = _SymGetSymFromAddr(myProcess,
1055 frame.AddrPC.Offset,
1056 &displacement,
1057 pSymbol);
1058 #endif
1060 if (ok) {
1061 PL_strncpyz(aDetails->function, pSymbol->Name,
1062 sizeof(aDetails->function));
1063 aDetails->foffset = displacement;
1067 LeaveCriticalSection(&gDbgHelpCS); // release our lock
1068 return NS_OK;
1071 EXPORT_XPCOM_API(nsresult)
1072 NS_FormatCodeAddressDetails(void *aPC, const nsCodeAddressDetails *aDetails,
1073 char *aBuffer, PRUint32 aBufferSize)
1075 #ifdef USING_WXP_VERSION
1076 if (_StackWalk64) {
1077 if (aDetails->function[0])
1078 _snprintf(aBuffer, aBufferSize, "%s!%s+0x%016lX",
1079 aDetails->library, aDetails->function, aDetails->foffset);
1080 else
1081 _snprintf(aBuffer, aBufferSize, "0x%016lX", aPC);
1082 } else {
1083 #endif
1084 if (aDetails->function[0])
1085 _snprintf(aBuffer, aBufferSize, "%s!%s+0x%08lX",
1086 aDetails->library, aDetails->function, aDetails->foffset);
1087 else
1088 _snprintf(aBuffer, aBufferSize, "0x%08lX", aPC);
1089 #ifdef USING_WXP_VERSION
1091 #endif
1092 aBuffer[aBufferSize - 1] = '\0';
1094 PRUint32 len = strlen(aBuffer);
1095 if (aDetails->filename[0]) {
1096 _snprintf(aBuffer + len, aBufferSize - len, " (%s, line %d)\n",
1097 aDetails->filename, aDetails->lineno);
1098 } else {
1099 aBuffer[len] = '\n';
1100 if (++len != aBufferSize)
1101 aBuffer[len] = '\0';
1103 aBuffer[aBufferSize - 2] = '\n';
1104 aBuffer[aBufferSize - 1] = '\0';
1105 return NS_OK;
1108 // WIN32 x86 stack walking code
1109 // i386 or PPC Linux stackwalking code or Solaris
1110 #elif HAVE_DLADDR && (HAVE__UNWIND_BACKTRACE || (defined(linux) && defined(__GNUC__) && (defined(__i386) || defined(PPC))) || (defined(__sun) && (defined(__sparc) || defined(sparc) || defined(__i386) || defined(i386))) || (defined(XP_MACOSX) && (defined(__ppc__) || defined(__i386))))
1112 #include <stdlib.h>
1113 #include <string.h>
1114 #include <math.h>
1115 #include "nscore.h"
1116 #include <stdio.h>
1117 #include "plstr.h"
1119 // On glibc 2.1, the Dl_info api defined in <dlfcn.h> is only exposed
1120 // if __USE_GNU is defined. I suppose its some kind of standards
1121 // adherence thing.
1123 #if (__GLIBC_MINOR__ >= 1) && !defined(__USE_GNU)
1124 #define __USE_GNU
1125 #endif
1127 #if defined(HAVE_LIBDL) || defined(XP_MACOSX)
1128 #include <dlfcn.h>
1129 #endif
1133 // This thing is exported by libstdc++
1134 // Yes, this is a gcc only hack
1135 #if defined(MOZ_DEMANGLE_SYMBOLS)
1136 #include <cxxabi.h>
1137 #include <stdlib.h> // for free()
1138 #endif // MOZ_DEMANGLE_SYMBOLS
1140 void DemangleSymbol(const char * aSymbol,
1141 char * aBuffer,
1142 int aBufLen)
1144 aBuffer[0] = '\0';
1146 #if defined(MOZ_DEMANGLE_SYMBOLS)
1147 /* See demangle.h in the gcc source for the voodoo */
1148 char * demangled = abi::__cxa_demangle(aSymbol,0,0,0);
1150 if (demangled)
1152 strncpy(aBuffer,demangled,aBufLen);
1153 free(demangled);
1155 #endif // MOZ_DEMANGLE_SYMBOLS
1159 #if defined(__sun) && (defined(__sparc) || defined(sparc) || defined(__i386) || defined(i386))
1162 * Stack walking code for Solaris courtesy of Bart Smaalder's "memtrak".
1165 #include <synch.h>
1166 #include <ucontext.h>
1167 #include <sys/frame.h>
1168 #include <sys/regset.h>
1169 #include <sys/stack.h>
1171 static int load_address ( void * pc, void * arg );
1172 static struct bucket * newbucket ( void * pc );
1173 static struct frame * cs_getmyframeptr ( void );
1174 static void cs_walk_stack ( void * (*read_func)(char * address),
1175 struct frame * fp,
1176 int (*operate_func)(void *, void *),
1177 void * usrarg );
1178 static void cs_operate ( void (*operate_func)(void *, void *),
1179 void * usrarg );
1181 #ifndef STACK_BIAS
1182 #define STACK_BIAS 0
1183 #endif /*STACK_BIAS*/
1185 #define LOGSIZE 4096
1187 /* type of demangling function */
1188 typedef int demf_t(const char *, char *, size_t);
1190 static demf_t *demf;
1192 static int initialized = 0;
1194 #if defined(sparc) || defined(__sparc)
1195 #define FRAME_PTR_REGISTER REG_SP
1196 #endif
1198 #if defined(i386) || defined(__i386)
1199 #define FRAME_PTR_REGISTER EBP
1200 #endif
1202 struct bucket {
1203 void * pc;
1204 int index;
1205 struct bucket * next;
1208 struct my_user_args {
1209 NS_WalkStackCallback callback;
1210 PRUint32 skipFrames;
1211 void *closure;
1215 static void myinit();
1217 #pragma init (myinit)
1219 static void
1220 myinit()
1223 if (! initialized) {
1224 #ifndef __GNUC__
1225 void *handle;
1226 const char *libdem = "libdemangle.so.1";
1228 /* load libdemangle if we can and need to (only try this once) */
1229 if ((handle = dlopen(libdem, RTLD_LAZY)) != NULL) {
1230 demf = (demf_t *)dlsym(handle,
1231 "cplus_demangle"); /*lint !e611 */
1233 * lint override above is to prevent lint from
1234 * complaining about "suspicious cast".
1237 #endif /*__GNUC__*/
1239 initialized = 1;
1243 static int
1244 load_address(void * pc, void * arg )
1246 static struct bucket table[2048];
1247 static mutex_t lock;
1248 struct bucket * ptr;
1249 struct my_user_args * args = (struct my_user_args *) arg;
1251 unsigned int val = NS_PTR_TO_INT32(pc);
1253 ptr = table + ((val >> 2)&2047);
1255 mutex_lock(&lock);
1256 while (ptr->next) {
1257 if (ptr->next->pc == pc)
1258 break;
1259 ptr = ptr->next;
1262 if (ptr->next) {
1263 mutex_unlock(&lock);
1264 } else {
1265 (args->callback)(pc, args->closure);
1267 ptr->next = newbucket(pc);
1268 mutex_unlock(&lock);
1270 return 0;
1274 static struct bucket *
1275 newbucket(void * pc)
1277 struct bucket * ptr = (struct bucket *) malloc(sizeof (*ptr));
1278 static int index; /* protected by lock in caller */
1280 ptr->index = index++;
1281 ptr->next = NULL;
1282 ptr->pc = pc;
1283 return (ptr);
1287 static struct frame *
1288 csgetframeptr()
1290 ucontext_t u;
1291 struct frame *fp;
1293 (void) getcontext(&u);
1295 fp = (struct frame *)
1296 ((char *)u.uc_mcontext.gregs[FRAME_PTR_REGISTER] +
1297 STACK_BIAS);
1299 /* make sure to return parents frame pointer.... */
1301 return ((struct frame *)((ulong_t)fp->fr_savfp + STACK_BIAS));
1305 static void
1306 cswalkstack(struct frame *fp, int (*operate_func)(void *, void *),
1307 void *usrarg)
1310 while (fp != 0 && fp->fr_savpc != 0) {
1312 if (operate_func((void *)fp->fr_savpc, usrarg) != 0)
1313 break;
1315 * watch out - libthread stacks look funny at the top
1316 * so they may not have their STACK_BIAS set
1319 fp = (struct frame *)((ulong_t)fp->fr_savfp +
1320 (fp->fr_savfp?(ulong_t)STACK_BIAS:0));
1325 static void
1326 cs_operate(int (*operate_func)(void *, void *), void * usrarg)
1328 cswalkstack(csgetframeptr(), operate_func, usrarg);
1331 EXPORT_XPCOM_API(nsresult)
1332 NS_StackWalk(NS_WalkStackCallback aCallback, PRUint32 aSkipFrames,
1333 void *aClosure)
1335 struct my_user_args args;
1337 if (!initialized)
1338 myinit();
1340 args.callback = aCallback;
1341 args.skipFrames = aSkipFrames; /* XXX Not handled! */
1342 args.closure = aClosure;
1343 cs_operate(load_address, &args);
1344 return NS_OK;
1347 EXPORT_XPCOM_API(nsresult)
1348 NS_DescribeCodeAddress(void *aPC, nsCodeAddressDetails *aDetails)
1350 aDetails->library[0] = '\0';
1351 aDetails->loffset = 0;
1352 aDetails->filename[0] = '\0';
1353 aDetails->lineno = 0;
1354 aDetails->function[0] = '\0';
1355 aDetails->foffset = 0;
1357 char dembuff[4096];
1358 Dl_info info;
1360 if (dladdr(aPC, & info)) {
1361 if (info.dli_fname) {
1362 PL_strncpyz(aDetails->library, info.dli_fname,
1363 sizeof(aDetails->library));
1364 aDetails->loffset = (char*)aPC - (char*)info.dli_fbase;
1366 if (info.dli_sname) {
1367 aDetails->foffset = (char*)aPC - (char*)info.dli_saddr;
1368 #ifdef __GNUC__
1369 DemangleSymbol(info.dli_sname, dembuff, sizeof(dembuff));
1370 #else
1371 if (!demf || demf(info.dli_sname, dembuff, sizeof (dembuff)))
1372 dembuff[0] = 0;
1373 #endif /*__GNUC__*/
1374 PL_strncpyz(aDetails->function,
1375 (dembuff[0] != '\0') ? dembuff : info.dli_sname,
1376 sizeof(aDetails->function));
1380 return NS_OK;
1383 EXPORT_XPCOM_API(nsresult)
1384 NS_FormatCodeAddressDetails(void *aPC, const nsCodeAddressDetails *aDetails,
1385 char *aBuffer, PRUint32 aBufferSize)
1387 snprintf(aBuffer, aBufferSize, "%p %s:%s+0x%lx\n",
1388 aPC,
1389 aDetails->library[0] ? aDetails->library : "??",
1390 aDetails->function[0] ? aDetails->function : "??",
1391 aDetails->foffset);
1392 return NS_OK;
1395 #else // not __sun-specific
1397 #if (defined(linux) && defined(__GNUC__) && (defined(__i386) || defined(PPC))) || (defined(XP_MACOSX) && (defined(__i386) || defined(__ppc__))) // i386 or PPC Linux or Mac stackwalking code
1399 #if __GLIBC__ > 2 || __GLIBC_MINOR > 1
1400 #define HAVE___LIBC_STACK_END 1
1401 #else
1402 #define HAVE___LIBC_STACK_END 0
1403 #endif
1405 #if HAVE___LIBC_STACK_END
1406 extern void *__libc_stack_end; // from ld-linux.so
1407 #endif
1409 EXPORT_XPCOM_API(nsresult)
1410 NS_StackWalk(NS_WalkStackCallback aCallback, PRUint32 aSkipFrames,
1411 void *aClosure)
1413 // Stack walking code courtesy Kipp's "leaky".
1415 // Get the frame pointer
1416 void **bp;
1417 #if defined(__i386)
1418 __asm__( "movl %%ebp, %0" : "=g"(bp));
1419 #else
1420 // It would be nice if this worked uniformly, but at least on i386 and
1421 // x86_64, it stopped working with gcc 4.1, because it points to the
1422 // end of the saved registers instead of the start.
1423 bp = (void**) __builtin_frame_address(0);
1424 #endif
1426 int skip = aSkipFrames;
1427 while (1) {
1428 void **next = (void**)*bp;
1429 // bp may not be a frame pointer on i386 if code was compiled with
1430 // -fomit-frame-pointer, so do some sanity checks.
1431 // (bp should be a frame pointer on ppc(64) but checking anyway may help
1432 // a little if the stack has been corrupted.)
1433 if (next <= bp ||
1434 #if HAVE___LIBC_STACK_END
1435 next > __libc_stack_end ||
1436 #endif
1437 (long(next) & 3)) {
1438 break;
1440 #if (defined(__ppc__) && defined(XP_MACOSX)) || defined(__powerpc64__)
1441 // ppc mac or powerpc64 linux
1442 void *pc = *(bp+2);
1443 #else // i386 or powerpc32 linux
1444 void *pc = *(bp+1);
1445 #endif
1446 if (--skip < 0) {
1447 (*aCallback)(pc, aClosure);
1449 bp = next;
1451 return NS_OK;
1454 #elif defined(HAVE__UNWIND_BACKTRACE)
1456 // libgcc_s.so symbols _Unwind_Backtrace@@GCC_3.3 and _Unwind_GetIP@@GCC_3.0
1457 #include <unwind.h>
1459 struct unwind_info {
1460 NS_WalkStackCallback callback;
1461 int skip;
1462 void *closure;
1465 static _Unwind_Reason_Code
1466 unwind_callback (struct _Unwind_Context *context, void *closure)
1468 unwind_info *info = static_cast<unwind_info *>(closure);
1469 if (--info->skip < 0) {
1470 void *pc = reinterpret_cast<void *>(_Unwind_GetIP(context));
1471 (*info->callback)(pc, info->closure);
1473 return _URC_NO_REASON;
1476 EXPORT_XPCOM_API(nsresult)
1477 NS_StackWalk(NS_WalkStackCallback aCallback, PRUint32 aSkipFrames,
1478 void *aClosure)
1480 unwind_info info;
1481 info.callback = aCallback;
1482 info.skip = aSkipFrames + 1;
1483 info.closure = aClosure;
1485 _Unwind_Backtrace(unwind_callback, &info);
1487 return NS_OK;
1490 #endif
1492 EXPORT_XPCOM_API(nsresult)
1493 NS_DescribeCodeAddress(void *aPC, nsCodeAddressDetails *aDetails)
1495 aDetails->library[0] = '\0';
1496 aDetails->loffset = 0;
1497 aDetails->filename[0] = '\0';
1498 aDetails->lineno = 0;
1499 aDetails->function[0] = '\0';
1500 aDetails->foffset = 0;
1502 Dl_info info;
1503 int ok = dladdr(aPC, &info);
1504 if (!ok) {
1505 return NS_OK;
1508 PL_strncpyz(aDetails->library, info.dli_fname, sizeof(aDetails->library));
1509 aDetails->loffset = (char*)aPC - (char*)info.dli_fbase;
1511 const char * symbol = info.dli_sname;
1512 int len;
1513 if (!symbol || !(len = strlen(symbol))) {
1514 return NS_OK;
1517 char demangled[4096] = "\0";
1519 DemangleSymbol(symbol, demangled, sizeof(demangled));
1521 if (strlen(demangled)) {
1522 symbol = demangled;
1523 len = strlen(symbol);
1526 PL_strncpyz(aDetails->function, symbol, sizeof(aDetails->function));
1527 aDetails->foffset = (char*)aPC - (char*)info.dli_saddr;
1528 return NS_OK;
1531 EXPORT_XPCOM_API(nsresult)
1532 NS_FormatCodeAddressDetails(void *aPC, const nsCodeAddressDetails *aDetails,
1533 char *aBuffer, PRUint32 aBufferSize)
1535 if (!aDetails->library[0]) {
1536 snprintf(aBuffer, aBufferSize, "UNKNOWN %p\n", aPC);
1537 } else if (!aDetails->function[0]) {
1538 snprintf(aBuffer, aBufferSize, "UNKNOWN [%s +0x%08lX]\n",
1539 aDetails->library, aDetails->loffset);
1540 } else {
1541 snprintf(aBuffer, aBufferSize, "%s+0x%08lX [%s +0x%08lX]\n",
1542 aDetails->function, aDetails->foffset,
1543 aDetails->library, aDetails->loffset);
1545 return NS_OK;
1548 #endif
1550 #else // unsupported platform.
1552 EXPORT_XPCOM_API(nsresult)
1553 NS_StackWalk(NS_WalkStackCallback aCallback, PRUint32 aSkipFrames,
1554 void *aClosure)
1556 return NS_ERROR_NOT_IMPLEMENTED;
1559 EXPORT_XPCOM_API(nsresult)
1560 NS_DescribeCodeAddress(void *aPC, nsCodeAddressDetails *aDetails)
1562 aDetails->library[0] = '\0';
1563 aDetails->loffset = 0;
1564 aDetails->filename[0] = '\0';
1565 aDetails->lineno = 0;
1566 aDetails->function[0] = '\0';
1567 aDetails->foffset = 0;
1568 return NS_ERROR_NOT_IMPLEMENTED;
1571 EXPORT_XPCOM_API(nsresult)
1572 NS_FormatCodeAddressDetails(void *aPC, const nsCodeAddressDetails *aDetails,
1573 char *aBuffer, PRUint32 aBufferSize)
1575 aBuffer[0] = '\0';
1576 return NS_ERROR_NOT_IMPLEMENTED;
1579 #endif