The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 20 | #include <android/log.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <utils/String8.h> |
Jeff Brown | ea45b01 | 2011-10-19 20:32:43 -0700 | [diff] [blame] | 22 | #include <corkscrew/backtrace.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 24 | #include <stdint.h> |
| 25 | #include <sys/types.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 29 | class Printer; |
| 30 | |
| 31 | // Collect/print the call stack (function, file, line) traces for a single thread. |
| 32 | class CallStack { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | public: |
| 34 | enum { |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 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, |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 41 | // Create an empty call stack. No-op. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | CallStack(); |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 43 | // Create a callstack with the current thread's stack trace. |
| 44 | // Immediately dump it to logcat using the given logtag. |
Mathias Agopian | d34a8ca | 2013-03-21 17:12:40 -0700 | [diff] [blame] | 45 | CallStack(const char* logtag, int32_t ignoreDepth=1, |
| 46 | int32_t maxDepth=MAX_DEPTH); |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 47 | // Copy the existing callstack (no other side effects). |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | CallStack(const CallStack& rhs); |
| 49 | ~CallStack(); |
| 50 | |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 51 | // Copy the existing callstack (no other side effects). |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | CallStack& operator = (const CallStack& rhs); |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 53 | |
| 54 | // Compare call stacks by their backtrace frame memory. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 61 | |
| 62 | // Get the PC address for the stack frame specified by index. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | const void* operator [] (int index) const; |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 64 | |
| 65 | // Reset the stack frames (same as creating an empty call stack). |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | void clear(); |
| 67 | |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 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); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 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; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 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. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | String8 toString(const char* prefix = 0) const; |
Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame^] | 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. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | size_t size() const { return mCount; } |
| 87 | |
| 88 | private: |
Jeff Brown | ea45b01 | 2011-10-19 20:32:43 -0700 | [diff] [blame] | 89 | size_t mCount; |
| 90 | backtrace_frame_t mStack[MAX_DEPTH]; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | }; // namespace android |
| 94 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | #endif // ANDROID_CALLSTACK_H |