diff options
author | Christopher Ferris <cferris@google.com> | 2013-10-31 16:25:04 -0700 |
---|---|---|
committer | Christopher Ferris <cferris@google.com> | 2014-01-09 15:09:13 -0800 |
commit | 038ac694b36de2b081e62fcfb9b5f4a8c918c533 (patch) | |
tree | bde917eef3e385029bee6bd87682705a709ef331 /libutils/CallStack.cpp | |
parent | b0751101b090a8732c62ad261fa44e5d6a69a2cc (diff) | |
download | core-038ac694b36de2b081e62fcfb9b5f4a8c918c533.tar.gz core-038ac694b36de2b081e62fcfb9b5f4a8c918c533.tar.bz2 core-038ac694b36de2b081e62fcfb9b5f4a8c918c533.zip |
Move CallStack to libbacktrace.
Fix a small bug in the Printer for strings that didn't properly
prepend the prefix.
(cherry picked from commit 9b0e074c6d38143e01616404a08b0c7aa992f3c3)
Change-Id: I78bfa3f76864c34f33fb439bf20dfc85616f1077
Diffstat (limited to 'libutils/CallStack.cpp')
-rw-r--r-- | libutils/CallStack.cpp | 97 |
1 files changed, 15 insertions, 82 deletions
diff --git a/libutils/CallStack.cpp b/libutils/CallStack.cpp index 4ceaa7c91..0bfb520e9 100644 --- a/libutils/CallStack.cpp +++ b/libutils/CallStack.cpp @@ -20,93 +20,33 @@ #include <utils/Printer.h> #include <utils/Errors.h> #include <utils/Log.h> -#include <corkscrew/backtrace.h> +#include <UniquePtr.h> + +#include <backtrace/Backtrace.h> namespace android { -CallStack::CallStack() : - mCount(0) { +CallStack::CallStack() { } -CallStack::CallStack(const char* logtag, int32_t ignoreDepth, int32_t maxDepth) { - this->update(ignoreDepth+1, maxDepth, CURRENT_THREAD); +CallStack::CallStack(const char* logtag, int32_t ignoreDepth) { + this->update(ignoreDepth+1); this->log(logtag); } -CallStack::CallStack(const CallStack& rhs) : - mCount(rhs.mCount) { - if (mCount) { - memcpy(mStack, rhs.mStack, mCount * sizeof(backtrace_frame_t)); - } -} - CallStack::~CallStack() { } -CallStack& CallStack::operator = (const CallStack& rhs) { - mCount = rhs.mCount; - if (mCount) { - memcpy(mStack, rhs.mStack, mCount * sizeof(backtrace_frame_t)); - } - return *this; -} - -bool CallStack::operator == (const CallStack& rhs) const { - if (mCount != rhs.mCount) - return false; - return !mCount || memcmp(mStack, rhs.mStack, mCount * sizeof(backtrace_frame_t)) == 0; -} - -bool CallStack::operator != (const CallStack& rhs) const { - return !operator == (rhs); -} - -bool CallStack::operator < (const CallStack& rhs) const { - if (mCount != rhs.mCount) - return mCount < rhs.mCount; - return memcmp(mStack, rhs.mStack, mCount * sizeof(backtrace_frame_t)) < 0; -} +void CallStack::update(int32_t ignoreDepth, pid_t tid) { + mFrameLines.clear(); -bool CallStack::operator >= (const CallStack& rhs) const { - return !operator < (rhs); -} - -bool CallStack::operator > (const CallStack& rhs) const { - if (mCount != rhs.mCount) - return mCount > rhs.mCount; - return memcmp(mStack, rhs.mStack, mCount * sizeof(backtrace_frame_t)) > 0; -} - -bool CallStack::operator <= (const CallStack& rhs) const { - return !operator > (rhs); -} - -const void* CallStack::operator [] (int index) const { - if (index >= int(mCount)) - return 0; - return reinterpret_cast<const void*>(mStack[index].absolute_pc); -} - -void CallStack::clear() { - mCount = 0; -} - -void CallStack::update(int32_t ignoreDepth, int32_t maxDepth, pid_t tid) { - if (maxDepth > MAX_DEPTH) { - maxDepth = MAX_DEPTH; + UniquePtr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid)); + if (!backtrace->Unwind(ignoreDepth)) { + ALOGW("%s: Failed to unwind callstack.", __FUNCTION__); } - ssize_t count; - - if (tid >= 0) { - count = unwind_backtrace_thread(tid, mStack, ignoreDepth + 1, maxDepth); - } else if (tid == CURRENT_THREAD) { - count = unwind_backtrace(mStack, ignoreDepth + 1, maxDepth); - } else { - ALOGE("%s: Invalid tid specified (%d)", __FUNCTION__, tid); - count = 0; + for (size_t i = 0; i < backtrace->NumFrames(); i++) { + mFrameLines.push_back(String8(backtrace->FormatFrameData(i).c_str())); } - - mCount = count > 0 ? count : 0; } void CallStack::log(const char* logtag, android_LogPriority priority, const char* prefix) const { @@ -129,16 +69,9 @@ String8 CallStack::toString(const char* prefix) const { } void CallStack::print(Printer& printer) const { - backtrace_symbol_t symbols[mCount]; - - get_backtrace_symbols(mStack, mCount, symbols); - for (size_t i = 0; i < mCount; i++) { - char line[MAX_BACKTRACE_LINE_LENGTH]; - format_backtrace_line(i, &mStack[i], &symbols[i], - line, MAX_BACKTRACE_LINE_LENGTH); - printer.printLine(line); + for (size_t i = 0; i < mFrameLines.size(); i++) { + printer.printLine(mFrameLines[i]); } - free_backtrace_symbols(symbols, mCount); } }; // namespace android |