• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <utils/Printer.h>
20 #include <utils/Errors.h>
21 #include <log/log.h>
22 
23 #include <unwindstack/AndroidUnwinder.h>
24 
25 #define CALLSTACK_WEAK  // Don't generate weak definitions.
26 #include <utils/CallStack.h>
27 
28 namespace android {
29 
CallStack()30 CallStack::CallStack() {
31 }
32 
CallStack(const char * logtag,int32_t ignoreDepth)33 CallStack::CallStack(const char* logtag, int32_t ignoreDepth) {
34     this->update(ignoreDepth+1);
35     this->log(logtag);
36 }
37 
~CallStack()38 CallStack::~CallStack() {
39 }
40 
update(int32_t ignoreDepth,pid_t tid)41 void CallStack::update(int32_t ignoreDepth, pid_t tid) {
42     if (ignoreDepth < 0) {
43         ignoreDepth = 0;
44     }
45 
46     mFrameLines.clear();
47 
48     unwindstack::AndroidLocalUnwinder unwinder;
49     unwindstack::AndroidUnwinderData data;
50     std::optional<pid_t> tid_val;
51     if (tid != -1) {
52         tid_val = tid;
53     }
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()));
61     }
62 }
63 
log(const char * logtag,android_LogPriority priority,const char * prefix) const64 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 }
68 
dump(int fd,int indent,const char * prefix) const69 void CallStack::dump(int fd, int indent, const char* prefix) const {
70     FdPrinter printer(fd, indent, prefix);
71     print(printer);
72 }
73 
toString(const char * prefix) const74 String8 CallStack::toString(const char* prefix) const {
75     String8 str;
76 
77     String8Printer printer(&str, prefix);
78     print(printer);
79 
80     return str;
81 }
82 
print(Printer & printer) const83 void CallStack::print(Printer& printer) const {
84     for (size_t i = 0; i < mFrameLines.size(); i++) {
85         printer.printLine(mFrameLines[i].c_str());
86     }
87 }
88 
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 
92 // Apple and Windows does not support this, so only compile on other platforms.
93 #if !defined(__APPLE__) && !defined(_WIN32)
94 
getCurrentInternal(int ignoreDepth)95 CallStack::CallStackUPtr CallStack::getCurrentInternal(int ignoreDepth) {
96     CallStack::CallStackUPtr stack(new CallStack());
97     stack->update(ignoreDepth + 1);
98     return stack;
99 }
100 
logStackInternal(const char * logtag,const CallStack * stack,android_LogPriority priority)101 void CallStack::logStackInternal(const char* logtag, const CallStack* stack,
102                                  android_LogPriority priority) {
103     stack->log(logtag, priority);
104 }
105 
stackToStringInternal(const char * prefix,const CallStack * stack)106 String8 CallStack::stackToStringInternal(const char* prefix, const CallStack* stack) {
107     return stack->toString(prefix);
108 }
109 
deleteStack(CallStack * stack)110 void CallStack::deleteStack(CallStack* stack) {
111     delete stack;
112 }
113 
114 #endif  // !defined(__APPLE__) && !defined(_WIN32)
115 
116 }; // namespace android
117