Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 1 | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 | // Copyright (c) 2005, Google Inc. |
| 3 | // All rights reserved. |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 4 | // |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 8 | // |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 18 | // |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // --- |
| 32 | // Author: Sanjay Ghemawat |
| 33 | // |
| 34 | // Routines to extract the current stack trace. These functions are |
| 35 | // thread-safe. |
| 36 | |
| 37 | #ifndef GOOGLE_STACKTRACE_H_ |
| 38 | #define GOOGLE_STACKTRACE_H_ |
| 39 | |
| 40 | // Annoying stuff for windows -- makes sure clients can import these functions |
| 41 | #ifndef PERFTOOLS_DLL_DECL |
| 42 | # ifdef _WIN32 |
| 43 | # define PERFTOOLS_DLL_DECL __declspec(dllimport) |
| 44 | # else |
| 45 | # define PERFTOOLS_DLL_DECL |
| 46 | # endif |
| 47 | #endif |
| 48 | |
| 49 | |
| 50 | // Skips the most recent "skip_count" stack frames (also skips the |
| 51 | // frame generated for the "GetStackFrames" routine itself), and then |
| 52 | // records the pc values for up to the next "max_depth" frames in |
| 53 | // "result", and the corresponding stack frame sizes in "sizes". |
| 54 | // Returns the number of values recorded in "result"/"sizes". |
| 55 | // |
| 56 | // Example: |
| 57 | // main() { foo(); } |
| 58 | // foo() { bar(); } |
| 59 | // bar() { |
| 60 | // void* result[10]; |
| 61 | // int sizes[10]; |
| 62 | // int depth = GetStackFrames(result, sizes, 10, 1); |
| 63 | // } |
| 64 | // |
| 65 | // The GetStackFrames call will skip the frame for "bar". It will |
| 66 | // return 2 and will produce pc values that map to the following |
| 67 | // procedures: |
| 68 | // result[0] foo |
| 69 | // result[1] main |
| 70 | // (Actually, there may be a few more entries after "main" to account for |
| 71 | // startup procedures.) |
| 72 | // And corresponding stack frame sizes will also be recorded: |
| 73 | // sizes[0] 16 |
| 74 | // sizes[1] 16 |
| 75 | // (Stack frame sizes of 16 above are just for illustration purposes.) |
| 76 | // Stack frame sizes of 0 or less indicate that those frame sizes couldn't |
| 77 | // be identified. |
| 78 | // |
| 79 | // This routine may return fewer stack frame entries than are |
| 80 | // available. Also note that "result" and "sizes" must both be non-NULL. |
| 81 | extern PERFTOOLS_DLL_DECL int GetStackFrames(void** result, int* sizes, int max_depth, |
| 82 | int skip_count); |
| 83 | |
| 84 | // Same as above, but to be used from a signal handler. The "uc" parameter |
| 85 | // should be the pointer to ucontext_t which was passed as the 3rd parameter |
| 86 | // to sa_sigaction signal handler. It may help the unwinder to get a |
| 87 | // better stack trace under certain conditions. The "uc" may safely be NULL. |
| 88 | extern PERFTOOLS_DLL_DECL int GetStackFramesWithContext(void** result, int* sizes, int max_depth, |
| 89 | int skip_count, const void *uc); |
| 90 | |
| 91 | // This is similar to the GetStackFrames routine, except that it returns |
| 92 | // the stack trace only, and not the stack frame sizes as well. |
| 93 | // Example: |
| 94 | // main() { foo(); } |
| 95 | // foo() { bar(); } |
| 96 | // bar() { |
| 97 | // void* result[10]; |
| 98 | // int depth = GetStackTrace(result, 10, 1); |
| 99 | // } |
| 100 | // |
| 101 | // This produces: |
| 102 | // result[0] foo |
| 103 | // result[1] main |
| 104 | // .... ... |
| 105 | // |
| 106 | // "result" must not be NULL. |
| 107 | extern PERFTOOLS_DLL_DECL int GetStackTrace(void** result, int max_depth, |
| 108 | int skip_count); |
| 109 | |
| 110 | // Same as above, but to be used from a signal handler. The "uc" parameter |
| 111 | // should be the pointer to ucontext_t which was passed as the 3rd parameter |
| 112 | // to sa_sigaction signal handler. It may help the unwinder to get a |
| 113 | // better stack trace under certain conditions. The "uc" may safely be NULL. |
| 114 | extern PERFTOOLS_DLL_DECL int GetStackTraceWithContext(void** result, int max_depth, |
| 115 | int skip_count, const void *uc); |
| 116 | |
| 117 | #endif /* GOOGLE_STACKTRACE_H_ */ |