From 038ac694b36de2b081e62fcfb9b5f4a8c918c533 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Thu, 31 Oct 2013 16:25:04 -0700 Subject: [PATCH] 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 --- include/utils/CallStack.h | 39 +++---------- include/utils/ProcessCallStack.h | 2 +- libutils/Android.mk | 10 +++- libutils/CallStack.cpp | 99 ++++++-------------------------- libutils/Printer.cpp | 1 + libutils/ProcessCallStack.cpp | 10 ++-- 6 files changed, 37 insertions(+), 124 deletions(-) diff --git a/include/utils/CallStack.h b/include/utils/CallStack.h index 205675173..583603768 100644 --- a/include/utils/CallStack.h +++ b/include/utils/CallStack.h @@ -18,8 +18,9 @@ #define ANDROID_CALLSTACK_H #include +#include #include -#include +#include #include #include @@ -31,42 +32,19 @@ class Printer; // Collect/print the call stack (function, file, line) traces for a single thread. class CallStack { public: - enum { - // Prune the lowest-most stack frames until we have at most MAX_DEPTH. - MAX_DEPTH = 31, - // Placeholder for specifying the current thread when updating the stack. - CURRENT_THREAD = -1, - }; - // Create an empty call stack. No-op. CallStack(); // Create a callstack with the current thread's stack trace. // Immediately dump it to logcat using the given logtag. - CallStack(const char* logtag, int32_t ignoreDepth=1, - int32_t maxDepth=MAX_DEPTH); - // Copy the existing callstack (no other side effects). - CallStack(const CallStack& rhs); + CallStack(const char* logtag, int32_t ignoreDepth=1); ~CallStack(); - // Copy the existing callstack (no other side effects). - CallStack& operator = (const CallStack& rhs); - - // Compare call stacks by their backtrace frame memory. - bool operator == (const CallStack& rhs) const; - bool operator != (const CallStack& rhs) const; - bool operator < (const CallStack& rhs) const; - bool operator >= (const CallStack& rhs) const; - bool operator > (const CallStack& rhs) const; - bool operator <= (const CallStack& rhs) const; - - // Get the PC address for the stack frame specified by index. - const void* operator [] (int index) const; - // Reset the stack frames (same as creating an empty call stack). - void clear(); + void clear() { mFrameLines.clear(); } // Immediately collect the stack traces for the specified thread. - void update(int32_t ignoreDepth=1, int32_t maxDepth=MAX_DEPTH, pid_t tid=CURRENT_THREAD); + // The default is to dump the stack of the current call. + void update(int32_t ignoreDepth=1, pid_t tid=BACKTRACE_NO_TID); // Dump a stack trace to the log using the supplied logtag. void log(const char* logtag, @@ -83,11 +61,10 @@ public: void print(Printer& printer) const; // Get the count of stack frames that are in this call stack. - size_t size() const { return mCount; } + size_t size() const { return mFrameLines.size(); } private: - size_t mCount; - backtrace_frame_t mStack[MAX_DEPTH]; + Vector mFrameLines; }; }; // namespace android diff --git a/include/utils/ProcessCallStack.h b/include/utils/ProcessCallStack.h index 4a8686958..32458b8b1 100644 --- a/include/utils/ProcessCallStack.h +++ b/include/utils/ProcessCallStack.h @@ -39,7 +39,7 @@ public: ~ProcessCallStack(); // Immediately collect the stack traces for all threads. - void update(int32_t maxDepth = CallStack::MAX_DEPTH); + void update(); // Print all stack traces to the log using the supplied logtag. void log(const char* logtag, android_LogPriority priority = ANDROID_LOG_DEBUG, diff --git a/libutils/Android.mk b/libutils/Android.mk index 720443e88..1710d3671 100644 --- a/libutils/Android.mk +++ b/libutils/Android.mk @@ -116,10 +116,12 @@ LOCAL_STATIC_LIBRARIES := \ libcutils LOCAL_SHARED_LIBRARIES := \ - libcorkscrew \ + libbacktrace \ liblog \ libdl +include external/stlport/libstlport.mk + LOCAL_MODULE:= libutils include $(BUILD_STATIC_LIBRARY) @@ -129,10 +131,12 @@ include $(CLEAR_VARS) LOCAL_MODULE:= libutils LOCAL_WHOLE_STATIC_LIBRARIES := libutils LOCAL_SHARED_LIBRARIES := \ - liblog \ + libbacktrace \ libcutils \ libdl \ - libcorkscrew + liblog \ + +include external/stlport/libstlport.mk include $(BUILD_SHARED_LIBRARY) 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 #include #include -#include +#include + +#include 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)); +void CallStack::update(int32_t ignoreDepth, pid_t tid) { + mFrameLines.clear(); + + UniquePtr backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid)); + if (!backtrace->Unwind(ignoreDepth)) { + ALOGW("%s: Failed to unwind callstack.", __FUNCTION__); } - 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; -} - -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(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; + for (size_t i = 0; i < backtrace->NumFrames(); i++) { + mFrameLines.push_back(String8(backtrace->FormatFrameData(i).c_str())); } - 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; - } - - 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 diff --git a/libutils/Printer.cpp b/libutils/Printer.cpp index ac729e05c..263e7404b 100644 --- a/libutils/Printer.cpp +++ b/libutils/Printer.cpp @@ -145,6 +145,7 @@ void String8Printer::printLine(const char* string) { return; } + mTarget->append(mPrefix); mTarget->append(string); mTarget->append("\n"); } diff --git a/libutils/ProcessCallStack.cpp b/libutils/ProcessCallStack.cpp index f9340c5b0..f837bcb63 100644 --- a/libutils/ProcessCallStack.cpp +++ b/libutils/ProcessCallStack.cpp @@ -123,7 +123,7 @@ void ProcessCallStack::clear() { mTimeUpdated = tm(); } -void ProcessCallStack::update(int32_t maxDepth) { +void ProcessCallStack::update() { DIR *dp; struct dirent *ep; struct dirent entry; @@ -181,14 +181,13 @@ void ProcessCallStack::update(int32_t maxDepth) { int ignoreDepth = (selfPid == tid) ? IGNORE_DEPTH_CURRENT_THREAD : 0; // Update thread's call stacks - CallStack& cs = threadInfo.callStack; - cs.update(ignoreDepth, maxDepth, tid); + threadInfo.callStack.update(ignoreDepth, tid); // Read/save thread name threadInfo.threadName = getThreadName(tid); ALOGV("%s: Got call stack for tid %d (size %zu)", - __FUNCTION__, tid, cs.size()); + __FUNCTION__, tid, threadInfo.callStack.size()); } if (code != 0) { // returns positive error value on error ALOGE("%s: Failed to readdir from %s (errno = %d, '%s')", @@ -221,13 +220,12 @@ void ProcessCallStack::printInternal(Printer& printer, Printer& csPrinter) const for (size_t i = 0; i < mThreadMap.size(); ++i) { pid_t tid = mThreadMap.keyAt(i); const ThreadInfo& threadInfo = mThreadMap.valueAt(i); - const CallStack& cs = threadInfo.callStack; const String8& threadName = threadInfo.threadName; printer.printLine(""); printer.printFormatLine("\"%s\" sysTid=%d", threadName.string(), tid); - cs.print(csPrinter); + threadInfo.callStack.print(csPrinter); } dumpProcessFooter(printer, getpid());