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        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);
108    }
109    free_backtrace_symbols(symbols, mCount);
110}
111
112String8 CallStack::toString(const char* prefix) const {
113    String8 str;
114    backtrace_symbol_t symbols[mCount];
115
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);
121        str.append(prefix);
122        str.append(line);
123        str.append("\n");
124    }
125    free_backtrace_symbols(symbols, mCount);
126    return str;
127}
128
129}; // namespace android
130