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 | |
| 17 | #define LOG_TAG "CallStack" |
| 18 | |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 19 | #include <utils/Printer.h> |
| 20 | #include <utils/Errors.h> |
Steven Moreland | 066e625 | 2023-10-07 00:29:44 +0000 | [diff] [blame] | 21 | #include <log/log.h> |
Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 22 | |
Christopher Ferris | ab63124 | 2022-05-10 16:13:02 -0700 | [diff] [blame] | 23 | #include <unwindstack/AndroidUnwinder.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 25 | #define CALLSTACK_WEAK // Don't generate weak definitions. |
| 26 | #include <utils/CallStack.h> |
| 27 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | namespace android { |
| 29 | |
Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 30 | CallStack::CallStack() { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 33 | CallStack::CallStack(const char* logtag, int32_t ignoreDepth) { |
| 34 | this->update(ignoreDepth+1); |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 35 | this->log(logtag); |
Mathias Agopian | d34a8ca | 2013-03-21 17:12:40 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Jeff Brown | ea45b01 | 2011-10-19 20:32:43 -0700 | [diff] [blame] | 38 | CallStack::~CallStack() { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 41 | void CallStack::update(int32_t ignoreDepth, pid_t tid) { |
Christopher Ferris | ab63124 | 2022-05-10 16:13:02 -0700 | [diff] [blame] | 42 | if (ignoreDepth < 0) { |
| 43 | ignoreDepth = 0; |
| 44 | } |
| 45 | |
Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 46 | mFrameLines.clear(); |
| 47 | |
Christopher Ferris | ab63124 | 2022-05-10 16:13:02 -0700 | [diff] [blame] | 48 | unwindstack::AndroidLocalUnwinder unwinder; |
| 49 | unwindstack::AndroidUnwinderData data; |
| 50 | std::optional<pid_t> tid_val; |
| 51 | if (tid != -1) { |
Christopher Ferris | 15fee82 | 2022-09-12 18:00:10 -0700 | [diff] [blame] | 52 | tid_val = tid; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | } |
Christopher Ferris | ab63124 | 2022-05-10 16:13:02 -0700 | [diff] [blame] | 54 | if (!unwinder.Unwind(tid_val, data)) { |
| 55 | ALOGW("%s: Failed to unwind callstack: %s", __FUNCTION__, data.GetErrorString().c_str()); |
| 56 | } |
| 57 | for (size_t i = ignoreDepth; i < data.frames.size(); i++) { |
| 58 | auto& frame = data.frames[i]; |
| 59 | frame.num -= ignoreDepth; |
| 60 | mFrameLines.push_back(String8(unwinder.FormatFrame(frame).c_str())); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 64 | void CallStack::log(const char* logtag, android_LogPriority priority, const char* prefix) const { |
| 65 | LogPrinter printer(logtag, priority, prefix, /*ignoreBlankLines*/false); |
| 66 | print(printer); |
| 67 | } |
Jeff Brown | ea45b01 | 2011-10-19 20:32:43 -0700 | [diff] [blame] | 68 | |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 69 | void CallStack::dump(int fd, int indent, const char* prefix) const { |
| 70 | FdPrinter printer(fd, indent, prefix); |
| 71 | print(printer); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Jeff Brown | ea45b01 | 2011-10-19 20:32:43 -0700 | [diff] [blame] | 74 | String8 CallStack::toString(const char* prefix) const { |
| 75 | String8 str; |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 76 | |
| 77 | String8Printer printer(&str, prefix); |
| 78 | print(printer); |
| 79 | |
| 80 | return str; |
| 81 | } |
| 82 | |
| 83 | void CallStack::print(Printer& printer) const { |
Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 84 | for (size_t i = 0; i < mFrameLines.size(); i++) { |
Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 85 | printer.printLine(mFrameLines[i].c_str()); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 89 | // The following four functions may be used via weak symbol references from libutils. |
| 90 | // Clients assume that if any of these symbols are available, then deleteStack() is. |
| 91 | |
Christopher Ferris | bcaeacc | 2024-03-27 16:17:50 -0700 | [diff] [blame] | 92 | // Apple and Windows does not support this, so only compile on other platforms. |
| 93 | #if !defined(__APPLE__) && !defined(_WIN32) |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 94 | |
| 95 | CallStack::CallStackUPtr CallStack::getCurrentInternal(int ignoreDepth) { |
| 96 | CallStack::CallStackUPtr stack(new CallStack()); |
| 97 | stack->update(ignoreDepth + 1); |
| 98 | return stack; |
| 99 | } |
| 100 | |
| 101 | void CallStack::logStackInternal(const char* logtag, const CallStack* stack, |
| 102 | android_LogPriority priority) { |
| 103 | stack->log(logtag, priority); |
| 104 | } |
| 105 | |
| 106 | String8 CallStack::stackToStringInternal(const char* prefix, const CallStack* stack) { |
| 107 | return stack->toString(prefix); |
| 108 | } |
| 109 | |
| 110 | void CallStack::deleteStack(CallStack* stack) { |
| 111 | delete stack; |
| 112 | } |
| 113 | |
Christopher Ferris | bcaeacc | 2024-03-27 16:17:50 -0700 | [diff] [blame] | 114 | #endif // !defined(__APPLE__) && !defined(_WIN32) |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 115 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | }; // namespace android |