| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |  | 
| Dan Albert | f2d2509 | 2015-11-05 01:09:22 -0800 | [diff] [blame] | 19 | #include <memory> | 
 | 20 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <utils/CallStack.h> | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 22 | #include <utils/Printer.h> | 
 | 23 | #include <utils/Errors.h> | 
 | 24 | #include <utils/Log.h> | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 25 |  | 
 | 26 | #include <backtrace/Backtrace.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | namespace android { | 
 | 29 |  | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 30 | CallStack::CallStack() { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | } | 
 | 32 |  | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 33 | CallStack::CallStack(const char* logtag, int32_t ignoreDepth) { | 
 | 34 |     this->update(ignoreDepth+1); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 35 |     this->log(logtag); | 
| Mathias Agopian | d34a8ca | 2013-03-21 17:12:40 -0700 | [diff] [blame] | 36 | } | 
 | 37 |  | 
| Jeff Brown | ea45b01 | 2011-10-19 20:32:43 -0700 | [diff] [blame] | 38 | CallStack::~CallStack() { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | } | 
 | 40 |  | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 41 | void CallStack::update(int32_t ignoreDepth, pid_t tid) { | 
 | 42 |     mFrameLines.clear(); | 
 | 43 |  | 
| Dan Albert | f2d2509 | 2015-11-05 01:09:22 -0800 | [diff] [blame] | 44 |     std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid)); | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 45 |     if (!backtrace->Unwind(ignoreDepth)) { | 
 | 46 |         ALOGW("%s: Failed to unwind callstack.", __FUNCTION__); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 |     } | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 48 |     for (size_t i = 0; i < backtrace->NumFrames(); i++) { | 
 | 49 |       mFrameLines.push_back(String8(backtrace->FormatFrameData(i).c_str())); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 |     } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | } | 
 | 52 |  | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 53 | void 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 Brown | ea45b01 | 2011-10-19 20:32:43 -0700 | [diff] [blame] | 57 |  | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 58 | void CallStack::dump(int fd, int indent, const char* prefix) const { | 
 | 59 |     FdPrinter printer(fd, indent, prefix); | 
 | 60 |     print(printer); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | } | 
 | 62 |  | 
| Jeff Brown | ea45b01 | 2011-10-19 20:32:43 -0700 | [diff] [blame] | 63 | String8 CallStack::toString(const char* prefix) const { | 
 | 64 |     String8 str; | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 65 |  | 
 | 66 |     String8Printer printer(&str, prefix); | 
 | 67 |     print(printer); | 
 | 68 |  | 
 | 69 |     return str; | 
 | 70 | } | 
 | 71 |  | 
 | 72 | void CallStack::print(Printer& printer) const { | 
| Christopher Ferris | 9b0e074 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 73 |     for (size_t i = 0; i < mFrameLines.size(); i++) { | 
 | 74 |         printer.printLine(mFrameLines[i]); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 |     } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | } | 
 | 77 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | }; // namespace android |