blob: fe6f33d7446fc8fd2faa0dc7a1de940f7557abe3 [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>
21#include <utils/Log.h>
Christopher Ferris9b0e0742013-10-31 16:25:04 -070022
23#include <backtrace/Backtrace.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) {
42 mFrameLines.clear();
43
Dan Albertf2d25092015-11-05 01:09:22 -080044 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid));
Christopher Ferris9b0e0742013-10-31 16:25:04 -070045 if (!backtrace->Unwind(ignoreDepth)) {
46 ALOGW("%s: Failed to unwind callstack.", __FUNCTION__);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080047 }
Christopher Ferris9b0e0742013-10-31 16:25:04 -070048 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
49 mFrameLines.push_back(String8(backtrace->FormatFrameData(i).c_str()));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080050 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080051}
52
Igor Murashkinec79ef22013-10-24 17:09:15 -070053void CallStack::log(const char* logtag, android_LogPriority priority, const char* prefix) const {
54 LogPrinter printer(logtag, priority, prefix, /*ignoreBlankLines*/false);
55 print(printer);
56}
Jeff Brownea45b012011-10-19 20:32:43 -070057
Igor Murashkinec79ef22013-10-24 17:09:15 -070058void CallStack::dump(int fd, int indent, const char* prefix) const {
59 FdPrinter printer(fd, indent, prefix);
60 print(printer);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080061}
62
Jeff Brownea45b012011-10-19 20:32:43 -070063String8 CallStack::toString(const char* prefix) const {
64 String8 str;
Igor Murashkinec79ef22013-10-24 17:09:15 -070065
66 String8Printer printer(&str, prefix);
67 print(printer);
68
69 return str;
70}
71
72void CallStack::print(Printer& printer) const {
Christopher Ferris9b0e0742013-10-31 16:25:04 -070073 for (size_t i = 0; i < mFrameLines.size(); i++) {
74 printer.printLine(mFrameLines[i]);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080075 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080076}
77
Hans Boehm2a019ec2018-08-07 23:45:25 +000078// The following four functions may be used via weak symbol references from libutils.
79// Clients assume that if any of these symbols are available, then deleteStack() is.
80
81#ifdef WEAKS_AVAILABLE
82
83CallStack::CallStackUPtr CallStack::getCurrentInternal(int ignoreDepth) {
84 CallStack::CallStackUPtr stack(new CallStack());
85 stack->update(ignoreDepth + 1);
86 return stack;
87}
88
89void CallStack::logStackInternal(const char* logtag, const CallStack* stack,
90 android_LogPriority priority) {
91 stack->log(logtag, priority);
92}
93
94String8 CallStack::stackToStringInternal(const char* prefix, const CallStack* stack) {
95 return stack->toString(prefix);
96}
97
98void CallStack::deleteStack(CallStack* stack) {
99 delete stack;
100}
101
102#endif // WEAKS_AVAILABLE
103
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800104}; // namespace android