blob: 029f4a76e87bc00c9af02bba59aa887acff9c2de [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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 Murashkinec79ef22013-10-24 17:09:15 -070019#include <utils/Printer.h>
20#include <utils/Errors.h>
Steven Moreland066e6252023-10-07 00:29:44 +000021#include <log/log.h>
Christopher Ferris9b0e0742013-10-31 16:25:04 -070022
Christopher Ferrisab631242022-05-10 16:13:02 -070023#include <unwindstack/AndroidUnwinder.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080024
Hans Boehm2a019ec2018-08-07 23:45:25 +000025#define CALLSTACK_WEAK // Don't generate weak definitions.
26#include <utils/CallStack.h>
27
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080028namespace android {
29
Christopher Ferris9b0e0742013-10-31 16:25:04 -070030CallStack::CallStack() {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080031}
32
Christopher Ferris9b0e0742013-10-31 16:25:04 -070033CallStack::CallStack(const char* logtag, int32_t ignoreDepth) {
34 this->update(ignoreDepth+1);
Igor Murashkinec79ef22013-10-24 17:09:15 -070035 this->log(logtag);
Mathias Agopiand34a8ca2013-03-21 17:12:40 -070036}
37
Jeff Brownea45b012011-10-19 20:32:43 -070038CallStack::~CallStack() {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080039}
40
Christopher Ferris9b0e0742013-10-31 16:25:04 -070041void CallStack::update(int32_t ignoreDepth, pid_t tid) {
Christopher Ferrisab631242022-05-10 16:13:02 -070042 if (ignoreDepth < 0) {
43 ignoreDepth = 0;
44 }
45
Christopher Ferris9b0e0742013-10-31 16:25:04 -070046 mFrameLines.clear();
47
Christopher Ferrisab631242022-05-10 16:13:02 -070048 unwindstack::AndroidLocalUnwinder unwinder;
49 unwindstack::AndroidUnwinderData data;
50 std::optional<pid_t> tid_val;
51 if (tid != -1) {
Christopher Ferris15fee822022-09-12 18:00:10 -070052 tid_val = tid;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080053 }
Christopher Ferrisab631242022-05-10 16:13:02 -070054 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 Projectcbb10112009-03-03 19:31:44 -080061 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080062}
63
Igor Murashkinec79ef22013-10-24 17:09:15 -070064void 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 Brownea45b012011-10-19 20:32:43 -070068
Igor Murashkinec79ef22013-10-24 17:09:15 -070069void CallStack::dump(int fd, int indent, const char* prefix) const {
70 FdPrinter printer(fd, indent, prefix);
71 print(printer);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080072}
73
Jeff Brownea45b012011-10-19 20:32:43 -070074String8 CallStack::toString(const char* prefix) const {
75 String8 str;
Igor Murashkinec79ef22013-10-24 17:09:15 -070076
77 String8Printer printer(&str, prefix);
78 print(printer);
79
80 return str;
81}
82
83void CallStack::print(Printer& printer) const {
Christopher Ferris9b0e0742013-10-31 16:25:04 -070084 for (size_t i = 0; i < mFrameLines.size(); i++) {
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +000085 printer.printLine(mFrameLines[i].c_str());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080086 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080087}
88
Hans Boehm2a019ec2018-08-07 23:45:25 +000089// 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 Ferrisbcaeacc2024-03-27 16:17:50 -070092// Apple and Windows does not support this, so only compile on other platforms.
93#if !defined(__APPLE__) && !defined(_WIN32)
Hans Boehm2a019ec2018-08-07 23:45:25 +000094
95CallStack::CallStackUPtr CallStack::getCurrentInternal(int ignoreDepth) {
96 CallStack::CallStackUPtr stack(new CallStack());
97 stack->update(ignoreDepth + 1);
98 return stack;
99}
100
101void CallStack::logStackInternal(const char* logtag, const CallStack* stack,
102 android_LogPriority priority) {
103 stack->log(logtag, priority);
104}
105
106String8 CallStack::stackToStringInternal(const char* prefix, const CallStack* stack) {
107 return stack->toString(prefix);
108}
109
110void CallStack::deleteStack(CallStack* stack) {
111 delete stack;
112}
113
Christopher Ferrisbcaeacc2024-03-27 16:17:50 -0700114#endif // !defined(__APPLE__) && !defined(_WIN32)
Hans Boehm2a019ec2018-08-07 23:45:25 +0000115
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800116}; // namespace android