blob: 2056751737c0df5068f0e2d8b913fd52ece98d02 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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
Igor Murashkinec79ef22013-10-24 17:09:15 -070020#include <android/log.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080021#include <utils/String8.h>
Jeff Brownea45b012011-10-19 20:32:43 -070022#include <corkscrew/backtrace.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080023
Igor Murashkinec79ef22013-10-24 17:09:15 -070024#include <stdint.h>
25#include <sys/types.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080026
27namespace android {
28
Igor Murashkinec79ef22013-10-24 17:09:15 -070029class Printer;
30
31// Collect/print the call stack (function, file, line) traces for a single thread.
32class CallStack {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080033public:
34 enum {
Igor Murashkinec79ef22013-10-24 17:09:15 -070035 // 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,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080039 };
40
Igor Murashkinec79ef22013-10-24 17:09:15 -070041 // Create an empty call stack. No-op.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080042 CallStack();
Igor Murashkinec79ef22013-10-24 17:09:15 -070043 // Create a callstack with the current thread's stack trace.
44 // Immediately dump it to logcat using the given logtag.
Mathias Agopiand34a8ca2013-03-21 17:12:40 -070045 CallStack(const char* logtag, int32_t ignoreDepth=1,
46 int32_t maxDepth=MAX_DEPTH);
Igor Murashkinec79ef22013-10-24 17:09:15 -070047 // Copy the existing callstack (no other side effects).
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080048 CallStack(const CallStack& rhs);
49 ~CallStack();
50
Igor Murashkinec79ef22013-10-24 17:09:15 -070051 // Copy the existing callstack (no other side effects).
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080052 CallStack& operator = (const CallStack& rhs);
Igor Murashkinec79ef22013-10-24 17:09:15 -070053
54 // Compare call stacks by their backtrace frame memory.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080055 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;
Igor Murashkinec79ef22013-10-24 17:09:15 -070061
62 // Get the PC address for the stack frame specified by index.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080063 const void* operator [] (int index) const;
Igor Murashkinec79ef22013-10-24 17:09:15 -070064
65 // Reset the stack frames (same as creating an empty call stack).
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080066 void clear();
67
Igor Murashkinec79ef22013-10-24 17:09:15 -070068 // 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);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080070
Igor Murashkinec79ef22013-10-24 17:09:15 -070071 // 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;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080075
Igor Murashkinec79ef22013-10-24 17:09:15 -070076 // 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.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080080 String8 toString(const char* prefix = 0) const;
Igor Murashkinec79ef22013-10-24 17:09:15 -070081
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.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080086 size_t size() const { return mCount; }
87
88private:
Jeff Brownea45b012011-10-19 20:32:43 -070089 size_t mCount;
90 backtrace_frame_t mStack[MAX_DEPTH];
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080091};
92
93}; // namespace android
94
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080095#endif // ANDROID_CALLSTACK_H