CallStack.cpp revision e27d62bbf24272d72fcbad5e6f8b69608151205b
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 19#include <string.h> 20 21#include <utils/Log.h> 22#include <utils/Errors.h> 23#include <utils/CallStack.h> 24#include <corkscrew/backtrace.h> 25 26/*****************************************************************************/ 27namespace android { 28 29CallStack::CallStack() : 30 mCount(0) { 31} 32 33CallStack::CallStack(const CallStack& rhs) : 34 mCount(rhs.mCount) { 35 if (mCount) { 36 memcpy(mStack, rhs.mStack, mCount * sizeof(backtrace_frame_t)); 37 } 38} 39 40CallStack::~CallStack() { 41} 42 43CallStack& CallStack::operator = (const CallStack& rhs) { 44 mCount = rhs.mCount; 45 if (mCount) { 46 memcpy(mStack, rhs.mStack, mCount * sizeof(backtrace_frame_t)); 47 } 48 return *this; 49} 50 51bool CallStack::operator == (const CallStack& rhs) const { 52 if (mCount != rhs.mCount) 53 return false; 54 return !mCount || memcmp(mStack, rhs.mStack, mCount * sizeof(backtrace_frame_t)) == 0; 55} 56 57bool CallStack::operator != (const CallStack& rhs) const { 58 return !operator == (rhs); 59} 60 61bool CallStack::operator < (const CallStack& rhs) const { 62 if (mCount != rhs.mCount) 63 return mCount < rhs.mCount; 64 return memcmp(mStack, rhs.mStack, mCount * sizeof(backtrace_frame_t)) < 0; 65} 66 67bool CallStack::operator >= (const CallStack& rhs) const { 68 return !operator < (rhs); 69} 70 71bool CallStack::operator > (const CallStack& rhs) const { 72 if (mCount != rhs.mCount) 73 return mCount > rhs.mCount; 74 return memcmp(mStack, rhs.mStack, mCount * sizeof(backtrace_frame_t)) > 0; 75} 76 77bool CallStack::operator <= (const CallStack& rhs) const { 78 return !operator > (rhs); 79} 80 81const void* CallStack::operator [] (int index) const { 82 if (index >= int(mCount)) 83 return 0; 84 return reinterpret_cast<const void*>(mStack[index].absolute_pc); 85} 86 87void CallStack::clear() { 88 mCount = 0; 89} 90 91void CallStack::update(int32_t ignoreDepth, int32_t maxDepth) { 92 if (maxDepth > MAX_DEPTH) { 93 maxDepth = MAX_DEPTH; 94 } 95 ssize_t count = unwind_backtrace(mStack, ignoreDepth + 1, maxDepth); 96 mCount = count > 0 ? count : 0; 97} 98 99void CallStack::dump(const char* prefix) const { 100 backtrace_symbol_t symbols[mCount]; 101 102 get_backtrace_symbols(mStack, mCount, symbols); 103 for (size_t i = 0; i < mCount; i++) { 104 const backtrace_frame_t& frame = mStack[i]; 105 const backtrace_symbol_t& symbol = symbols[i]; 106 const char* mapName = symbol.map_info ? symbol.map_info->name : "<unknown>"; 107 const char* symbolName = symbol.demangled_name ? symbol.demangled_name : symbol.name; 108 if (symbolName) { 109 LOGD("%s#%02d pc %08x %s (%s)\n", prefix, 110 int(i), uint32_t(symbol.relative_pc), mapName, symbolName); 111 } else { 112 LOGD("%s#%02d pc %08x %s\n", prefix, 113 int(i), uint32_t(symbol.relative_pc), mapName); 114 } 115 } 116 free_backtrace_symbols(symbols, mCount); 117} 118 119String8 CallStack::toString(const char* prefix) const { 120 String8 str; 121 backtrace_symbol_t symbols[mCount]; 122 123 get_backtrace_symbols(mStack, mCount, symbols); 124 for (size_t i = 0; i < mCount; i++) { 125 const backtrace_frame_t& frame = mStack[i]; 126 const backtrace_symbol_t& symbol = symbols[i]; 127 const char* mapName = symbol.map_info ? symbol.map_info->name : "<unknown>"; 128 const char* symbolName = symbol.demangled_name ? symbol.demangled_name : symbol.name; 129 if (symbolName) { 130 str.appendFormat("%s#%02d pc %08x %s (%s)\n", prefix, 131 int(i), uint32_t(symbol.relative_pc), mapName, symbolName); 132 } else { 133 str.appendFormat("%s#%02d pc %08x %s\n", prefix, 134 int(i), uint32_t(symbol.relative_pc), mapName); 135 } 136 } 137 free_backtrace_symbols(symbols, mCount); 138 return str; 139} 140 141}; // namespace android 142