blob: 0bfb520e92e6d7843a060604fcefd54f7b60e18a [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
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080019#include <utils/CallStack.h>
Igor Murashkinec79ef22013-10-24 17:09:15 -070020#include <utils/Printer.h>
21#include <utils/Errors.h>
22#include <utils/Log.h>
Christopher Ferris9b0e0742013-10-31 16:25:04 -070023#include <UniquePtr.h>
24
25#include <backtrace/Backtrace.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080026
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080027namespace android {
28
Christopher Ferris9b0e0742013-10-31 16:25:04 -070029CallStack::CallStack() {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080030}
31
Christopher Ferris9b0e0742013-10-31 16:25:04 -070032CallStack::CallStack(const char* logtag, int32_t ignoreDepth) {
33 this->update(ignoreDepth+1);
Igor Murashkinec79ef22013-10-24 17:09:15 -070034 this->log(logtag);
Mathias Agopiand34a8ca2013-03-21 17:12:40 -070035}
36
Jeff Brownea45b012011-10-19 20:32:43 -070037CallStack::~CallStack() {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080038}
39
Christopher Ferris9b0e0742013-10-31 16:25:04 -070040void CallStack::update(int32_t ignoreDepth, pid_t tid) {
41 mFrameLines.clear();
42
43 UniquePtr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid));
44 if (!backtrace->Unwind(ignoreDepth)) {
45 ALOGW("%s: Failed to unwind callstack.", __FUNCTION__);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080046 }
Christopher Ferris9b0e0742013-10-31 16:25:04 -070047 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
48 mFrameLines.push_back(String8(backtrace->FormatFrameData(i).c_str()));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080049 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080050}
51
Igor Murashkinec79ef22013-10-24 17:09:15 -070052void CallStack::log(const char* logtag, android_LogPriority priority, const char* prefix) const {
53 LogPrinter printer(logtag, priority, prefix, /*ignoreBlankLines*/false);
54 print(printer);
55}
Jeff Brownea45b012011-10-19 20:32:43 -070056
Igor Murashkinec79ef22013-10-24 17:09:15 -070057void CallStack::dump(int fd, int indent, const char* prefix) const {
58 FdPrinter printer(fd, indent, prefix);
59 print(printer);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080060}
61
Jeff Brownea45b012011-10-19 20:32:43 -070062String8 CallStack::toString(const char* prefix) const {
63 String8 str;
Igor Murashkinec79ef22013-10-24 17:09:15 -070064
65 String8Printer printer(&str, prefix);
66 print(printer);
67
68 return str;
69}
70
71void CallStack::print(Printer& printer) const {
Christopher Ferris9b0e0742013-10-31 16:25:04 -070072 for (size_t i = 0; i < mFrameLines.size(); i++) {
73 printer.printLine(mFrameLines[i]);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080074 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080075}
76
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080077}; // namespace android