CallStack.h revision ec79ef2e7b6b1d81266637ca0e002b5c0c5a789b
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#ifndef ANDROID_CALLSTACK_H
18#define ANDROID_CALLSTACK_H
19
20#include <android/log.h>
21#include <utils/String8.h>
22#include <corkscrew/backtrace.h>
23
24#include <stdint.h>
25#include <sys/types.h>
26
27namespace android {
28
29class Printer;
30
31// Collect/print the call stack (function, file, line) traces for a single thread.
32class CallStack {
33public:
34    enum {
35        // Prune the lowest-most stack frames until we have at most MAX_DEPTH.
36        MAX_DEPTH = 31,
37        // Placeholder for specifying the current thread when updating the stack.
38        CURRENT_THREAD = -1,
39    };
40
41    // Create an empty call stack. No-op.
42    CallStack();
43    // Create a callstack with the current thread's stack trace.
44    // Immediately dump it to logcat using the given logtag.
45    CallStack(const char* logtag, int32_t ignoreDepth=1,
46            int32_t maxDepth=MAX_DEPTH);
47    // Copy the existing callstack (no other side effects).
48    CallStack(const CallStack& rhs);
49    ~CallStack();
50
51    // Copy the existing callstack (no other side effects).
52    CallStack& operator = (const CallStack& rhs);
53
54    // Compare call stacks by their backtrace frame memory.
55    bool operator == (const CallStack& rhs) const;
56    bool operator != (const CallStack& rhs) const;
57    bool operator < (const CallStack& rhs) const;
58    bool operator >= (const CallStack& rhs) const;
59    bool operator > (const CallStack& rhs) const;
60    bool operator <= (const CallStack& rhs) const;
61
62    // Get the PC address for the stack frame specified by index.
63    const void* operator [] (int index) const;
64
65    // Reset the stack frames (same as creating an empty call stack).
66    void clear();
67
68    // Immediately collect the stack traces for the specified thread.
69    void update(int32_t ignoreDepth=1, int32_t maxDepth=MAX_DEPTH, pid_t tid=CURRENT_THREAD);
70
71    // Dump a stack trace to the log using the supplied logtag.
72    void log(const char* logtag,
73             android_LogPriority priority = ANDROID_LOG_DEBUG,
74             const char* prefix = 0) const;
75
76    // Dump a stack trace to the specified file descriptor.
77    void dump(int fd, int indent = 0, const char* prefix = 0) const;
78
79    // Return a string (possibly very long) containing the complete stack trace.
80    String8 toString(const char* prefix = 0) const;
81
82    // Dump a serialized representation of the stack trace to the specified printer.
83    void print(Printer& printer) const;
84
85    // Get the count of stack frames that are in this call stack.
86    size_t size() const { return mCount; }
87
88private:
89    size_t mCount;
90    backtrace_frame_t mStack[MAX_DEPTH];
91};
92
93}; // namespace android
94
95#endif // ANDROID_CALLSTACK_H
96