2
* Copyright (C) 2007 The Android Open Source Project
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
#define LOG_TAG "CallStack"
21
#include <utils/Log.h>
22
#include <utils/Errors.h>
23
#include <utils/CallStack.h>
24
#include <corkscrew/backtrace.h>
26
/*****************************************************************************/
29
CallStack::CallStack() :
33
CallStack::CallStack(const CallStack& rhs) :
36
memcpy(mStack, rhs.mStack, mCount * sizeof(backtrace_frame_t));
40
CallStack::~CallStack() {
43
CallStack& CallStack::operator = (const CallStack& rhs) {
46
memcpy(mStack, rhs.mStack, mCount * sizeof(backtrace_frame_t));
51
bool CallStack::operator == (const CallStack& rhs) const {
52
if (mCount != rhs.mCount)
54
return !mCount || memcmp(mStack, rhs.mStack, mCount * sizeof(backtrace_frame_t)) == 0;
57
bool CallStack::operator != (const CallStack& rhs) const {
58
return !operator == (rhs);
61
bool 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;
67
bool CallStack::operator >= (const CallStack& rhs) const {
68
return !operator < (rhs);
71
bool 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;
77
bool CallStack::operator <= (const CallStack& rhs) const {
78
return !operator > (rhs);
81
const void* CallStack::operator [] (int index) const {
82
if (index >= int(mCount))
84
return reinterpret_cast<const void*>(mStack[index].absolute_pc);
87
void CallStack::clear() {
91
void CallStack::update(int32_t ignoreDepth, int32_t maxDepth) {
92
if (maxDepth > MAX_DEPTH) {
95
ssize_t count = unwind_backtrace(mStack, ignoreDepth + 1, maxDepth);
96
mCount = count > 0 ? count : 0;
99
void CallStack::dump(const char* prefix) const {
100
backtrace_symbol_t symbols[mCount];
102
get_backtrace_symbols(mStack, mCount, symbols);
103
for (size_t i = 0; i < mCount; i++) {
104
char line[MAX_BACKTRACE_LINE_LENGTH];
105
format_backtrace_line(i, &mStack[i], &symbols[i],
106
line, MAX_BACKTRACE_LINE_LENGTH);
107
ALOGD("%s%s", prefix, line);
109
free_backtrace_symbols(symbols, mCount);
112
String8 CallStack::toString(const char* prefix) const {
114
backtrace_symbol_t symbols[mCount];
116
get_backtrace_symbols(mStack, mCount, symbols);
117
for (size_t i = 0; i < mCount; i++) {
118
char line[MAX_BACKTRACE_LINE_LENGTH];
119
format_backtrace_line(i, &mStack[i], &symbols[i],
120
line, MAX_BACKTRACE_LINE_LENGTH);
125
free_backtrace_symbols(symbols, mCount);
129
}; // namespace android