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
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.
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
52 #include "nsMemory.h" // for NS_ARRAY_LENGTH
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
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
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
,
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
,
99 PREAD_PROCESS_MEMORY_ROUTINE64
,
100 PFUNCTION_TABLE_ACCESS_ROUTINE64
,
101 PGET_MODULE_BASE_ROUTINE64
,
102 PTRANSLATE_ADDRESS_ROUTINE64
);
103 extern STACKWALKPROC64 _StackWalk64
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
{
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
;
219 #define _StackWalk64 0
222 SYMFUNCTIONTABLEACCESSPROC _SymFunctionTableAccess
;
223 #ifdef USING_WXP_VERSION
224 SYMFUNCTIONTABLEACCESSPROC64 _SymFunctionTableAccess64
;
226 #define _SymFunctionTableAccess64 0
229 SYMGETMODULEBASEPROC _SymGetModuleBase
;
230 #ifdef USING_WXP_VERSION
231 SYMGETMODULEBASEPROC64 _SymGetModuleBase64
;
233 #define _SymGetModuleBase64 0
236 SYMGETSYMFROMADDRPROC _SymGetSymFromAddr
;
237 #ifdef USING_WXP_VERSION
238 SYMFROMADDRPROC _SymFromAddr
;
240 #define _SymFromAddr 0
243 SYMLOADMODULE _SymLoadModule
;
244 #ifdef USING_WXP_VERSION
245 SYMLOADMODULE64 _SymLoadModule64
;
247 #define _SymLoadModule64 0
250 SYMUNDNAME _SymUnDName
;
252 SYMGETMODULEINFO _SymGetModuleInfo
;
253 #ifdef USING_WXP_VERSION
254 SYMGETMODULEINFO64 _SymGetModuleInfo64
;
256 #define _SymGetModuleInfo64 0
259 ENUMLOADEDMODULES _EnumerateLoadedModules
;
260 #ifdef USING_WXP_VERSION
261 ENUMLOADEDMODULES64 _EnumerateLoadedModules64
;
263 #define _EnumerateLoadedModules64 0
266 SYMGETLINEFROMADDRPROC _SymGetLineFromAddr
;
267 #ifdef USING_WXP_VERSION
268 SYMGETLINEFROMADDRPROC64 _SymGetLineFromAddr64
;
270 #define _SymGetLineFromAddr64 0
273 DWORD gStackWalkThread
;
274 CRITICAL_SECTION gDbgHelpCS
;
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
)
283 DWORD lastErr
= GetLastError();
285 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS
,
288 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), // Default language
293 fprintf(stderr
, "### ERROR: %s: %s",
294 prefix
, lpMsgBuf
? lpMsgBuf
: "(null)\n");
296 LocalFree( lpMsgBuf
);
300 EnsureImageHlpInitialized()
302 static PRBool gInitialized
= PR_FALSE
;
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
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
,
318 gStackWalkThread
= threadID
;
319 if (hStackWalkThread
== NULL
) {
320 PrintError("CreateThread");
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");
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");
349 _StackWalk
= (STACKWALKPROC
)GetProcAddress(module
, "StackWalk");
350 if (!_StackWalk64
&& !_StackWalk
) return PR_FALSE
;
352 #ifdef USING_WXP_VERSION
353 _SymFunctionTableAccess64
= (SYMFUNCTIONTABLEACCESSPROC64
) GetProcAddress(module
, "SymFunctionTableAccess64");
355 _SymFunctionTableAccess
= (SYMFUNCTIONTABLEACCESSPROC
) GetProcAddress(module
, "SymFunctionTableAccess");
356 if (!_SymFunctionTableAccess64
&& !_SymFunctionTableAccess
) return PR_FALSE
;
358 #ifdef USING_WXP_VERSION
359 _SymGetModuleBase64
= (SYMGETMODULEBASEPROC64
)GetProcAddress(module
, "SymGetModuleBase64");
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");
368 if (!_SymFromAddr
&& !_SymGetSymFromAddr
) return PR_FALSE
;
370 #ifdef USING_WXP_VERSION
371 _SymLoadModule64
= (SYMLOADMODULE64
)GetProcAddress(module
, "SymLoadModule64");
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");
382 _SymGetModuleInfo
= (SYMGETMODULEINFO
)GetProcAddress(module
, "SymGetModuleInfo");
383 if (!_SymGetModuleInfo64
&& !_SymGetModuleInfo
) return PR_FALSE
;
385 #ifdef USING_WXP_VERSION
386 _EnumerateLoadedModules64
= (ENUMLOADEDMODULES64
)GetProcAddress(module
, "EnumerateLoadedModules64");
388 _EnumerateLoadedModules
= (ENUMLOADEDMODULES
)GetProcAddress(module
, "EnumerateLoadedModules");
389 if (!_EnumerateLoadedModules64
&& !_EnumerateLoadedModules
) return PR_FALSE
;
391 #ifdef USING_WXP_VERSION
392 _SymGetLineFromAddr64
= (SYMGETLINEFROMADDRPROC64
)GetProcAddress(module
, "SymGetLineFromAddr64");
394 _SymGetLineFromAddr
= (SYMGETLINEFROMADDRPROC
)GetProcAddress(module
, "SymGetLineFromAddr");
395 if (!_SymGetLineFromAddr64
&& !_SymGetLineFromAddr
) return PR_FALSE
;
397 return gInitialized
= PR_TRUE
;
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.
408 HANDLE myProcess
= data
->process
;
409 HANDLE myThread
= data
->thread
;
411 STACKFRAME64 frame64
;
412 int skip
= 3 + data
->skipFrames
; // skip our own stack walking frames
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");
423 // Setup initial stack frame to walk from
424 memset(&frame64
, 0, sizeof(frame64
));
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
;
438 #error "Should not have compiled this code"
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
448 // debug routines are not threadsafe, so grab the lock.
449 EnterCriticalSection(&gDbgHelpCS
);
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
,
458 #error "Should not have compiled this code"
465 _SymFunctionTableAccess64
, // function table access routine
466 _SymGetModuleBase64
, // module base routine
469 LeaveCriticalSection(&gDbgHelpCS
);
472 addr
= frame64
.AddrPC
.Offset
;
475 PrintError("WalkStack64");
478 if (!ok
|| (addr
== 0)) {
486 if (data
->pc_count
< data
->pc_size
)
487 data
->pcs
[data
->pc_count
] = (void*)addr
;
490 if (frame64
.AddrReturn
.Offset
== 0)
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.
505 HANDLE myProcess
= data
->process
;
506 HANDLE myThread
= data
->thread
;
509 int skip
= data
->skipFrames
; // skip our own stack walking frames
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");
520 // Setup initial stack frame to walk from
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
;
530 PrintError("Unknown platform. No stack walking.");
534 // Now walk the stack
537 // debug routines are not threadsafe, so grab the lock.
538 EnterCriticalSection(&gDbgHelpCS
);
540 IMAGE_FILE_MACHINE_I386
,
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
);
553 addr
= frame
.AddrPC
.Offset
;
556 PrintError("WalkStack");
559 if (!ok
|| (addr
== 0)) {
567 if (data
->pc_count
< data
->pc_size
)
568 data
->pcs
[data
->pc_count
] = (void*)addr
;
571 if (frame
.AddrReturn
.Offset
== 0)
580 WalkStackThread(void* aData
)
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) {
595 PrintError("GetMessage");
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
);
611 PrintError("ThreadSuspend");
615 WalkStackMain64(data
);
619 ret
= ::ResumeThread(data
->thread
);
621 PrintError("ThreadResume");
625 ::SetEvent(data
->eventEnd
);
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
,
644 HANDLE myProcess
, myThread
;
646 struct WalkStackData data
;
648 if (!EnsureImageHlpInitialized())
651 // Have to duplicate handle to get a real handle.
652 if (!::DuplicateHandle(::GetCurrentProcess(),
653 ::GetCurrentProcess(),
654 ::GetCurrentProcess(),
656 PROCESS_ALL_ACCESS
, FALSE
, 0)) {
657 PrintError("DuplicateHandle (process)");
658 return NS_ERROR_FAILURE
;
660 if (!::DuplicateHandle(::GetCurrentProcess(),
661 ::GetCurrentThread(),
662 ::GetCurrentProcess(),
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
;
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
;
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
))
714 static BOOL CALLBACK
callbackEspecial(
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.
734 ? (addr
>= aModuleBase
&& addr
<= (aModuleBase
+ aModuleSize
))
735 : (addr
<= aModuleBase
&& addr
>= (aModuleBase
- aModuleSize
))
737 retval
= _SymLoadModule(GetCurrentProcess(), NULL
, (PSTR
)aModuleName
, NULL
, aModuleBase
, aModuleSize
);
739 PrintError("SymLoadModule");
745 static BOOL CALLBACK
callbackEspecial64(
751 #ifdef USING_WXP_VERSION
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.
766 ? (addr
>= aModuleBase
&& addr
<= (aModuleBase
+ aModuleSize
))
767 : (addr
<= aModuleBase
&& addr
>= (aModuleBase
- aModuleSize
))
769 retval
= _SymLoadModule64(GetCurrentProcess(), NULL
, (PSTR
)aModuleName
, NULL
, aModuleBase
, aModuleSize
);
771 PrintError("SymLoadModule64");
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
)
794 * Init the vars if we have em.
796 aModuleInfo
->SizeOfStruct
= sizeof(IMAGEHLP_MODULE
);
797 if (nsnull
!= aLineInfo
) {
798 aLineInfo
->SizeOfStruct
= sizeof(IMAGEHLP_LINE
);
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
819 enumRes
= _EnumerateLoadedModules(aProcess
, (PENUMLOADED_MODULES_CALLBACK
)callbackEspecial
, (PVOID
)&aAddr
);
820 if (FALSE
!= enumRes
)
824 * If it fails, then well, we have other problems.
826 retval
= _SymGetModuleInfo(aProcess
, aAddr
, aModuleInfo
);
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
);
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))
857 #define NS_IMAGEHLP_MODULE64_SIZE sizeof(IMAGEHLP_MODULE64)
860 #ifdef USING_WXP_VERSION
861 BOOL
SymGetModuleInfoEspecial64(HANDLE aProcess
, DWORD64 aAddr
, PIMAGEHLP_MODULE64 aModuleInfo
, PIMAGEHLP_LINE64 aLineInfo
)
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
);
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
891 enumRes
= _EnumerateLoadedModules64(aProcess
, (PENUMLOADED_MODULES_CALLBACK64
)callbackEspecial64
, (PVOID
)&aAddr
);
892 if (FALSE
!= enumRes
)
896 * If it fails, then well, we have other problems.
898 retval
= _SymGetModuleInfo64(aProcess
, aAddr
, aModuleInfo
);
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
);
913 // Clear out aLineInfo to indicate that it's not valid
914 memset(aLineInfo
, 0, sizeof(*aLineInfo
));
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
932 EnsureSymInitialized()
934 static PRBool gInitialized
= PR_FALSE
;
940 if (!EnsureImageHlpInitialized())
943 _SymSetOptions(SYMOPT_LOAD_LINES
| SYMOPT_UNDNAME
);
944 retStat
= _SymInitialize(GetCurrentPIDorHandle(), NULL
, TRUE
);
946 PrintError("SymInitialize");
948 gInitialized
= retStat
;
949 /* XXX At some point we need to arrange to call _SymCleanup */
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();
971 // debug routines are not threadsafe, so grab the lock.
972 EnterCriticalSection(&gDbgHelpCS
);
974 #ifdef USING_WXP_VERSION
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
;
985 modInfoRes
= SymGetModuleInfoEspecial64(myProcess
, addr
, &modInfo
, &lineInfo
);
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
);
1009 PL_strncpyz(aDetails
->function
, pSymbol
->Name
,
1010 sizeof(aDetails
->function
));
1011 aDetails
->foffset
= displacement
;
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
;
1025 modInfoRes
= SymGetModuleInfoEspecial(myProcess
, addr
, &modInfo
, &lineInfo
);
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
);
1047 char buf
[sizeof(IMAGEHLP_SYMBOL
) + 512];
1048 PIMAGEHLP_SYMBOL pSymbol
= (PIMAGEHLP_SYMBOL
) buf
;
1049 pSymbol
->SizeOfStruct
= sizeof(buf
);
1050 pSymbol
->MaxNameLength
= 512;
1054 ok
= _SymGetSymFromAddr(myProcess
,
1055 frame
.AddrPC
.Offset
,
1061 PL_strncpyz(aDetails
->function
, pSymbol
->Name
,
1062 sizeof(aDetails
->function
));
1063 aDetails
->foffset
= displacement
;
1067 LeaveCriticalSection(&gDbgHelpCS
); // release our lock
1071 EXPORT_XPCOM_API(nsresult
)
1072 NS_FormatCodeAddressDetails(void *aPC
, const nsCodeAddressDetails
*aDetails
,
1073 char *aBuffer
, PRUint32 aBufferSize
)
1075 #ifdef USING_WXP_VERSION
1077 if (aDetails
->function
[0])
1078 _snprintf(aBuffer
, aBufferSize
, "%s!%s+0x%016lX",
1079 aDetails
->library
, aDetails
->function
, aDetails
->foffset
);
1081 _snprintf(aBuffer
, aBufferSize
, "0x%016lX", aPC
);
1084 if (aDetails
->function
[0])
1085 _snprintf(aBuffer
, aBufferSize
, "%s!%s+0x%08lX",
1086 aDetails
->library
, aDetails
->function
, aDetails
->foffset
);
1088 _snprintf(aBuffer
, aBufferSize
, "0x%08lX", aPC
);
1089 #ifdef USING_WXP_VERSION
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
);
1099 aBuffer
[len
] = '\n';
1100 if (++len
!= aBufferSize
)
1101 aBuffer
[len
] = '\0';
1103 aBuffer
[aBufferSize
- 2] = '\n';
1104 aBuffer
[aBufferSize
- 1] = '\0';
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))))
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
1123 #if (__GLIBC_MINOR__ >= 1) && !defined(__USE_GNU)
1127 #if defined(HAVE_LIBDL) || defined(XP_MACOSX)
1133 // This thing is exported by libstdc++
1134 // Yes, this is a gcc only hack
1135 #if defined(MOZ_DEMANGLE_SYMBOLS)
1137 #include <stdlib.h> // for free()
1138 #endif // MOZ_DEMANGLE_SYMBOLS
1140 void DemangleSymbol(const char * aSymbol
,
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);
1152 strncpy(aBuffer
,demangled
,aBufLen
);
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".
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
),
1176 int (*operate_func
)(void *, void *),
1178 static void cs_operate ( void (*operate_func
)(void *, void *),
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
1198 #if defined(i386) || defined(__i386)
1199 #define FRAME_PTR_REGISTER EBP
1205 struct bucket
* next
;
1208 struct my_user_args
{
1209 NS_WalkStackCallback callback
;
1210 PRUint32 skipFrames
;
1215 static void myinit();
1217 #pragma init (myinit)
1223 if (! initialized
) {
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".
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);
1257 if (ptr
->next
->pc
== pc
)
1263 mutex_unlock(&lock
);
1265 (args
->callback
)(pc
, args
->closure
);
1267 ptr
->next
= newbucket(pc
);
1268 mutex_unlock(&lock
);
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
++;
1287 static struct frame
*
1293 (void) getcontext(&u
);
1295 fp
= (struct frame
*)
1296 ((char *)u
.uc_mcontext
.gregs
[FRAME_PTR_REGISTER
] +
1299 /* make sure to return parents frame pointer.... */
1301 return ((struct frame
*)((ulong_t
)fp
->fr_savfp
+ STACK_BIAS
));
1306 cswalkstack(struct frame
*fp
, int (*operate_func
)(void *, void *),
1310 while (fp
!= 0 && fp
->fr_savpc
!= 0) {
1312 if (operate_func((void *)fp
->fr_savpc
, usrarg
) != 0)
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));
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
,
1335 struct my_user_args args
;
1340 args
.callback
= aCallback
;
1341 args
.skipFrames
= aSkipFrames
; /* XXX Not handled! */
1342 args
.closure
= aClosure
;
1343 cs_operate(load_address
, &args
);
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;
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
;
1369 DemangleSymbol(info
.dli_sname
, dembuff
, sizeof(dembuff
));
1371 if (!demf
|| demf(info
.dli_sname
, dembuff
, sizeof (dembuff
)))
1374 PL_strncpyz(aDetails
->function
,
1375 (dembuff
[0] != '\0') ? dembuff
: info
.dli_sname
,
1376 sizeof(aDetails
->function
));
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",
1389 aDetails
->library
[0] ? aDetails
->library
: "??",
1390 aDetails
->function
[0] ? aDetails
->function
: "??",
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
1402 #define HAVE___LIBC_STACK_END 0
1405 #if HAVE___LIBC_STACK_END
1406 extern void *__libc_stack_end
; // from ld-linux.so
1409 EXPORT_XPCOM_API(nsresult
)
1410 NS_StackWalk(NS_WalkStackCallback aCallback
, PRUint32 aSkipFrames
,
1413 // Stack walking code courtesy Kipp's "leaky".
1415 // Get the frame pointer
1418 __asm__( "movl %%ebp, %0" : "=g"(bp
));
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);
1426 int skip
= aSkipFrames
;
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.)
1434 #if HAVE___LIBC_STACK_END
1435 next
> __libc_stack_end
||
1440 #if (defined(__ppc__) && defined(XP_MACOSX)) || defined(__powerpc64__)
1441 // ppc mac or powerpc64 linux
1443 #else // i386 or powerpc32 linux
1447 (*aCallback
)(pc
, aClosure
);
1454 #elif defined(HAVE__UNWIND_BACKTRACE)
1456 // libgcc_s.so symbols _Unwind_Backtrace@@GCC_3.3 and _Unwind_GetIP@@GCC_3.0
1459 struct unwind_info
{
1460 NS_WalkStackCallback callback
;
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
,
1481 info
.callback
= aCallback
;
1482 info
.skip
= aSkipFrames
+ 1;
1483 info
.closure
= aClosure
;
1485 _Unwind_Backtrace(unwind_callback
, &info
);
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;
1503 int ok
= dladdr(aPC
, &info
);
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
;
1513 if (!symbol
|| !(len
= strlen(symbol
))) {
1517 char demangled
[4096] = "\0";
1519 DemangleSymbol(symbol
, demangled
, sizeof(demangled
));
1521 if (strlen(demangled
)) {
1523 len
= strlen(symbol
);
1526 PL_strncpyz(aDetails
->function
, symbol
, sizeof(aDetails
->function
));
1527 aDetails
->foffset
= (char*)aPC
- (char*)info
.dli_saddr
;
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
);
1541 snprintf(aBuffer
, aBufferSize
, "%s+0x%08lX [%s +0x%08lX]\n",
1542 aDetails
->function
, aDetails
->foffset
,
1543 aDetails
->library
, aDetails
->loffset
);
1550 #else // unsupported platform.
1552 EXPORT_XPCOM_API(nsresult
)
1553 NS_StackWalk(NS_WalkStackCallback aCallback
, PRUint32 aSkipFrames
,
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
)
1576 return NS_ERROR_NOT_IMPLEMENTED
;