| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 17 | #pragma once |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 21 | #include <android/log.h> |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <utils/String8.h> |
| Christopher Ferris | 038ac69 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 23 | #include <utils/Vector.h> |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 25 | #include <stdint.h> |
| 26 | #include <sys/types.h> |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 28 | #if !defined(__APPLE__) && !defined(_WIN32) |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 29 | # define CALLSTACK_WEAKS_AVAILABLE 1 |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 30 | #endif |
| 31 | #ifndef CALLSTACK_WEAK |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 32 | # ifdef CALLSTACK_WEAKS_AVAILABLE |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 33 | # define CALLSTACK_WEAK __attribute__((weak)) |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 34 | # else // !CALLSTACK_WEAKS_AVAILABLE |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 35 | # define CALLSTACK_WEAK |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 36 | # endif // !CALLSTACK_WEAKS_AVAILABLE |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 37 | #endif // CALLSTACK_WEAK predefined |
| 38 | |
| Serdar Kocdemir | 593b19b | 2024-02-14 13:53:13 +0000 | [diff] [blame] | 39 | #ifndef CALLSTACK_ALWAYS_INLINE |
| 40 | #define CALLSTACK_ALWAYS_INLINE __attribute__((always_inline)) |
| 41 | #endif // CALLSTACK_ALWAYS_INLINE predefined |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 42 | |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | namespace android { |
| 44 | |
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 45 | class Printer; |
| 46 | |
| 47 | // Collect/print the call stack (function, file, line) traces for a single thread. |
| 48 | class CallStack { |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | public: |
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 50 | // Create an empty call stack. No-op. |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | CallStack(); |
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 52 | // Create a callstack with the current thread's stack trace. |
| 53 | // Immediately dump it to logcat using the given logtag. |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 54 | CallStack(const char* logtag, int32_t ignoreDepth = 1); |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | ~CallStack(); |
| 56 | |
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 57 | // Reset the stack frames (same as creating an empty call stack). |
| Christopher Ferris | 038ac69 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 58 | void clear() { mFrameLines.clear(); } |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | |
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 60 | // Immediately collect the stack traces for the specified thread. |
| Christopher Ferris | 038ac69 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 61 | // The default is to dump the stack of the current call. |
| Christopher Ferris | ab63124 | 2022-05-10 16:13:02 -0700 | [diff] [blame] | 62 | void update(int32_t ignoreDepth = 1, pid_t tid = -1); |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 64 | // Dump a stack trace to the log using the supplied logtag. |
| 65 | void log(const char* logtag, |
| 66 | android_LogPriority priority = ANDROID_LOG_DEBUG, |
| Yi Kong | c1a1562 | 2018-07-11 17:37:34 -0700 | [diff] [blame] | 67 | const char* prefix = nullptr) const; |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 69 | // Dump a stack trace to the specified file descriptor. |
| Yi Kong | c1a1562 | 2018-07-11 17:37:34 -0700 | [diff] [blame] | 70 | void dump(int fd, int indent = 0, const char* prefix = nullptr) const; |
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 71 | |
| 72 | // Return a string (possibly very long) containing the complete stack trace. |
| Yi Kong | c1a1562 | 2018-07-11 17:37:34 -0700 | [diff] [blame] | 73 | String8 toString(const char* prefix = nullptr) const; |
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 74 | |
| 75 | // Dump a serialized representation of the stack trace to the specified printer. |
| 76 | void print(Printer& printer) const; |
| 77 | |
| 78 | // Get the count of stack frames that are in this call stack. |
| Christopher Ferris | 038ac69 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 79 | size_t size() const { return mFrameLines.size(); } |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 81 | // DO NOT USE ANYTHING BELOW HERE. The following public members are expected |
| 82 | // to disappear again shortly, once a better replacement facility exists. |
| 83 | // The replacement facility will be incompatible! |
| 84 | |
| 85 | // Debugging accesses to some basic functionality. These use weak symbols to |
| 86 | // avoid introducing a dependency on libutilscallstack. Such a dependency from |
| 87 | // libutils results in a cyclic build dependency. These routines can be called |
| 88 | // from within libutils. But if the actual library is unavailable, they do |
| 89 | // nothing. |
| 90 | // |
| 91 | // DO NOT USE THESE. They will disappear. |
| 92 | struct StackDeleter { |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 93 | #ifdef CALLSTACK_WEAKS_AVAILABLE |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 94 | void operator()(CallStack* stack) { |
| 95 | deleteStack(stack); |
| 96 | } |
| 97 | #else |
| 98 | void operator()(CallStack*) {} |
| 99 | #endif |
| 100 | }; |
| 101 | |
| 102 | typedef std::unique_ptr<CallStack, StackDeleter> CallStackUPtr; |
| 103 | |
| 104 | // Return current call stack if possible, nullptr otherwise. |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 105 | #ifdef CALLSTACK_WEAKS_AVAILABLE |
| Serdar Kocdemir | 593b19b | 2024-02-14 13:53:13 +0000 | [diff] [blame] | 106 | static CallStackUPtr CALLSTACK_ALWAYS_INLINE getCurrent(int32_t ignoreDepth = 1) { |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 107 | if (reinterpret_cast<uintptr_t>(getCurrentInternal) == 0) { |
| 108 | ALOGW("CallStack::getCurrentInternal not linked, returning null"); |
| 109 | return CallStackUPtr(nullptr); |
| 110 | } else { |
| 111 | return getCurrentInternal(ignoreDepth); |
| 112 | } |
| 113 | } |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 114 | #else // !CALLSTACK_WEAKS_AVAILABLE |
| Serdar Kocdemir | 593b19b | 2024-02-14 13:53:13 +0000 | [diff] [blame] | 115 | static CallStackUPtr CALLSTACK_ALWAYS_INLINE getCurrent(int32_t = 1) { |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 116 | return CallStackUPtr(nullptr); |
| 117 | } |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 118 | #endif // !CALLSTACK_WEAKS_AVAILABLE |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 119 | |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 120 | #ifdef CALLSTACK_WEAKS_AVAILABLE |
| Serdar Kocdemir | 593b19b | 2024-02-14 13:53:13 +0000 | [diff] [blame] | 121 | static void CALLSTACK_ALWAYS_INLINE logStack(const char* logtag, |
| 122 | CallStack* stack = getCurrent().get(), |
| 123 | android_LogPriority priority = ANDROID_LOG_DEBUG) { |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 124 | if (reinterpret_cast<uintptr_t>(logStackInternal) != 0 && stack != nullptr) { |
| 125 | logStackInternal(logtag, stack, priority); |
| 126 | } else { |
| Steven Moreland | 8338072 | 2019-01-02 18:29:28 -0800 | [diff] [blame] | 127 | ALOG(LOG_WARN, logtag, "CallStack::logStackInternal not linked"); |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | #else |
| Serdar Kocdemir | 593b19b | 2024-02-14 13:53:13 +0000 | [diff] [blame] | 132 | static void CALLSTACK_ALWAYS_INLINE logStack(const char* logtag, |
| 133 | CallStack* = getCurrent().get(), |
| 134 | android_LogPriority = ANDROID_LOG_DEBUG) { |
| Steven Moreland | 8338072 | 2019-01-02 18:29:28 -0800 | [diff] [blame] | 135 | ALOG(LOG_WARN, logtag, "CallStack::logStackInternal not linked"); |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 136 | } |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 137 | #endif // !CALLSTACK_WEAKS_AVAILABLE |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 138 | |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 139 | #ifdef CALLSTACK_WEAKS_AVAILABLE |
| Serdar Kocdemir | 593b19b | 2024-02-14 13:53:13 +0000 | [diff] [blame] | 140 | static String8 CALLSTACK_ALWAYS_INLINE |
| 141 | stackToString(const char* prefix = nullptr, const CallStack* stack = getCurrent().get()) { |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 142 | if (reinterpret_cast<uintptr_t>(stackToStringInternal) != 0 && stack != nullptr) { |
| 143 | return stackToStringInternal(prefix, stack); |
| 144 | } else { |
| Steven Moreland | 8338072 | 2019-01-02 18:29:28 -0800 | [diff] [blame] | 145 | return String8::format("%s<CallStack package not linked>", (prefix ? prefix : "")); |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 148 | #else // !CALLSTACK_WEAKS_AVAILABLE |
| Serdar Kocdemir | 593b19b | 2024-02-14 13:53:13 +0000 | [diff] [blame] | 149 | static String8 CALLSTACK_ALWAYS_INLINE stackToString(const char* prefix = nullptr, |
| 150 | const CallStack* = getCurrent().get()) { |
| Steven Moreland | 8338072 | 2019-01-02 18:29:28 -0800 | [diff] [blame] | 151 | return String8::format("%s<CallStack package not linked>", (prefix ? prefix : "")); |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 152 | } |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 153 | #endif // !CALLSTACK_WEAKS_AVAILABLE |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 154 | |
| 155 | private: |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 156 | #ifdef CALLSTACK_WEAKS_AVAILABLE |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 157 | static CallStackUPtr CALLSTACK_WEAK getCurrentInternal(int32_t ignoreDepth); |
| 158 | static void CALLSTACK_WEAK logStackInternal(const char* logtag, const CallStack* stack, |
| 159 | android_LogPriority priority); |
| 160 | static String8 CALLSTACK_WEAK stackToStringInternal(const char* prefix, const CallStack* stack); |
| 161 | // The deleter is only invoked on non-null pointers. Hence it will never be |
| 162 | // invoked if CallStack is not linked. |
| 163 | static void CALLSTACK_WEAK deleteStack(CallStack* stack); |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 164 | #endif // CALLSTACK_WEAKS_AVAILABLE |
| Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 165 | |
| Christopher Ferris | 038ac69 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 166 | Vector<String8> mFrameLines; |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | }; |
| 168 | |
| Pirama Arumuga Nainar | 90affce | 2018-04-11 23:10:28 -0700 | [diff] [blame] | 169 | } // namespace android |
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | |
| Elliott Hughes | fdc6426 | 2024-02-23 00:46:16 +0000 | [diff] [blame] | 171 | #undef CALLSTACK_WEAKS_AVAILABLE |
| 172 | #undef CALLSTACK_WEAK |
| Serdar Kocdemir | 593b19b | 2024-02-14 13:53:13 +0000 | [diff] [blame] | 173 | #undef CALLSTACK_ALWAYS_INLINE |