1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/base/nsStackWalk.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,125 @@ 1.4 +/* vim: set shiftwidth=4 tabstop=8 autoindent cindent expandtab: */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* API for getting a stack trace of the C/C++ stack on the current thread */ 1.10 + 1.11 +#ifndef nsStackWalk_h_ 1.12 +#define nsStackWalk_h_ 1.13 + 1.14 +/* WARNING: This file is intended to be included from C or C++ files. */ 1.15 + 1.16 +#include "nscore.h" 1.17 +#include <stdint.h> 1.18 + 1.19 +#ifdef __cplusplus 1.20 +extern "C" { 1.21 +#endif 1.22 + 1.23 +// aSP will be the best approximation possible of what the stack pointer will be 1.24 +// pointing to when the execution returns to executing that at that PC. 1.25 +// If no approximation can be made it will be nullptr. 1.26 +typedef void 1.27 +(* NS_WalkStackCallback)(void *aPC, void *aSP, void *aClosure); 1.28 + 1.29 +/** 1.30 + * Call aCallback for the C/C++ stack frames on the current thread, from 1.31 + * the caller of NS_StackWalk to main (or above). 1.32 + * 1.33 + * @param aCallback Callback function, called once per frame. 1.34 + * @param aSkipFrames Number of initial frames to skip. 0 means that 1.35 + * the first callback will be for the caller of 1.36 + * NS_StackWalk. 1.37 + * @param aMaxFrames Maximum number of frames to trace. 0 means no limit. 1.38 + * @param aClosure Caller-supplied data passed through to aCallback. 1.39 + * @param aThread The thread for which the stack is to be retrieved. 1.40 + * Passing null causes us to walk the stack of the 1.41 + * current thread. On Windows, this is a thread HANDLE. 1.42 + * It is currently not supported on any other platform. 1.43 + * @param aPlatformData Platform specific data that can help in walking the 1.44 + * stack, this should be nullptr unless you really know 1.45 + * what you're doing! This needs to be a pointer to a 1.46 + * CONTEXT on Windows and should not be passed on other 1.47 + * platforms. 1.48 + * 1.49 + * Return values: 1.50 + * - NS_ERROR_NOT_IMPLEMENTED. Occurs on platforms where it is unimplemented. 1.51 + * 1.52 + * - NS_ERROR_UNEXPECTED. Occurs when the stack indicates that the thread 1.53 + * is in a very dangerous situation (e.g., holding sem_pool_lock in Mac OS X 1.54 + * pthreads code). Callers should then bail out immediately. 1.55 + * 1.56 + * - NS_ERROR_FAILURE. Occurs when stack walking completely failed, i.e. 1.57 + * aCallback was never called. 1.58 + * 1.59 + * - NS_OK. Occurs when stack walking succeeded, i.e. aCallback was called at 1.60 + * least once (and there was no need to exit with NS_ERROR_UNEXPECTED). 1.61 + * 1.62 + * May skip some stack frames due to compiler optimizations or code 1.63 + * generation. 1.64 + */ 1.65 +XPCOM_API(nsresult) 1.66 +NS_StackWalk(NS_WalkStackCallback aCallback, uint32_t aSkipFrames, 1.67 + uint32_t aMaxFrames, void *aClosure, uintptr_t aThread, 1.68 + void *aPlatformData); 1.69 + 1.70 +typedef struct { 1.71 + /* 1.72 + * The name of the shared library or executable containing an 1.73 + * address and the address's offset within that library, or empty 1.74 + * string and zero if unknown. 1.75 + */ 1.76 + char library[256]; 1.77 + ptrdiff_t loffset; 1.78 + /* 1.79 + * The name of the file name and line number of the code 1.80 + * corresponding to the address, or empty string and zero if 1.81 + * unknown. 1.82 + */ 1.83 + char filename[256]; 1.84 + unsigned long lineno; 1.85 + /* 1.86 + * The name of the function containing an address and the address's 1.87 + * offset within that function, or empty string and zero if unknown. 1.88 + */ 1.89 + char function[256]; 1.90 + ptrdiff_t foffset; 1.91 +} nsCodeAddressDetails; 1.92 + 1.93 +/** 1.94 + * For a given pointer to code, fill in the pieces of information used 1.95 + * when printing a stack trace. 1.96 + * 1.97 + * @param aPC The code address. 1.98 + * @param aDetails A structure to be filled in with the result. 1.99 + */ 1.100 +XPCOM_API(nsresult) 1.101 +NS_DescribeCodeAddress(void *aPC, nsCodeAddressDetails *aDetails); 1.102 + 1.103 +/** 1.104 + * Format the information about a code address in a format suitable for 1.105 + * stack traces on the current platform. When available, this string 1.106 + * should contain the function name, source file, and line number. When 1.107 + * these are not available, library and offset should be reported, if 1.108 + * possible. 1.109 + * 1.110 + * @param aPC The code address. 1.111 + * @param aDetails The value filled in by NS_DescribeCodeAddress(aPC). 1.112 + * @param aBuffer A string to be filled in with the description. 1.113 + * The string will always be null-terminated. 1.114 + * @param aBufferSize The size, in bytes, of aBuffer, including 1.115 + * room for the terminating null. If the information 1.116 + * to be printed would be larger than aBuffer, it 1.117 + * will be truncated so that aBuffer[aBufferSize-1] 1.118 + * is the terminating null. 1.119 + */ 1.120 +XPCOM_API(nsresult) 1.121 +NS_FormatCodeAddressDetails(void *aPC, const nsCodeAddressDetails *aDetails, 1.122 + char *aBuffer, uint32_t aBufferSize); 1.123 + 1.124 +#ifdef __cplusplus 1.125 +} 1.126 +#endif 1.127 + 1.128 +#endif /* !defined(nsStackWalk_h_) */