blob: bd6015e7928c2b638f990cef2c3ee597a6dc3150 [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
Mathias Agopian22dbf392017-02-28 15:06:51 -080019#include <utils/CallStack.h>
20
Dan Albertf2d25092015-11-05 01:09:22 -080021#include <memory>
22
Igor Murashkinec79ef22013-10-24 17:09:15 -070023#include <utils/Printer.h>
24#include <utils/Errors.h>
25#include <utils/Log.h>
Christopher Ferris9b0e0742013-10-31 16:25:04 -070026
27#include <backtrace/Backtrace.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080028
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080029namespace android {
30
Christopher Ferris9b0e0742013-10-31 16:25:04 -070031CallStack::CallStack() {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080032}
33
Christopher Ferris9b0e0742013-10-31 16:25:04 -070034CallStack::CallStack(const char* logtag, int32_t ignoreDepth) {
35 this->update(ignoreDepth+1);
Igor Murashkinec79ef22013-10-24 17:09:15 -070036 this->log(logtag);
Mathias Agopiand34a8ca2013-03-21 17:12:40 -070037}
38
Jeff Brownea45b012011-10-19 20:32:43 -070039CallStack::~CallStack() {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080040}
41
Christopher Ferris9b0e0742013-10-31 16:25:04 -070042void CallStack::update(int32_t ignoreDepth, pid_t tid) {
43 mFrameLines.clear();
44
Dan Albertf2d25092015-11-05 01:09:22 -080045 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid));
Christopher Ferris9b0e0742013-10-31 16:25:04 -070046 if (!backtrace->Unwind(ignoreDepth)) {
47 ALOGW("%s: Failed to unwind callstack.", __FUNCTION__);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080048 }
Christopher Ferris9b0e0742013-10-31 16:25:04 -070049 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
50 mFrameLines.push_back(String8(backtrace->FormatFrameData(i).c_str()));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080051 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080052}
53
Igor Murashkinec79ef22013-10-24 17:09:15 -070054void CallStack::log(const char* logtag, android_LogPriority priority, const char* prefix) const {
55 LogPrinter printer(logtag, priority, prefix, /*ignoreBlankLines*/false);
56 print(printer);
57}
Jeff Brownea45b012011-10-19 20:32:43 -070058
Igor Murashkinec79ef22013-10-24 17:09:15 -070059void CallStack::dump(int fd, int indent, const char* prefix) const {
60 FdPrinter printer(fd, indent, prefix);
61 print(printer);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080062}
63
Jeff Brownea45b012011-10-19 20:32:43 -070064String8 CallStack::toString(const char* prefix) const {
65 String8 str;
Igor Murashkinec79ef22013-10-24 17:09:15 -070066
67 String8Printer printer(&str, prefix);
68 print(printer);
69
70 return str;
71}
72
73void CallStack::print(Printer& printer) const {
Christopher Ferris9b0e0742013-10-31 16:25:04 -070074 for (size_t i = 0; i < mFrameLines.size(); i++) {
75 printer.printLine(mFrameLines[i]);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080076 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080077}
78
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080079}; // namespace android