| 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> | 
| Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 21 | #include <backtrace/backtrace_constants.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <utils/String8.h> | 
| Christopher Ferris | 038ac69 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 23 | #include <utils/Vector.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 |  | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 25 | #include <stdint.h> | 
 | 26 | #include <sys/types.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 |  | 
 | 28 | namespace android { | 
 | 29 |  | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 30 | class Printer; | 
 | 31 |  | 
 | 32 | // Collect/print the call stack (function, file, line) traces for a single thread. | 
 | 33 | class CallStack { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | public: | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 35 |     // Create an empty call stack. No-op. | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 |     CallStack(); | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 37 |     // Create a callstack with the current thread's stack trace. | 
 | 38 |     // Immediately dump it to logcat using the given logtag. | 
| Christopher Ferris | 038ac69 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 39 |     CallStack(const char* logtag, int32_t ignoreDepth=1); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 |     ~CallStack(); | 
 | 41 |  | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 42 |     // Reset the stack frames (same as creating an empty call stack). | 
| Christopher Ferris | 038ac69 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 43 |     void clear() { mFrameLines.clear(); } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 |  | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 45 |     // Immediately collect the stack traces for the specified thread. | 
| Christopher Ferris | 038ac69 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 46 |     // The default is to dump the stack of the current call. | 
| Christopher Ferris | bc12d63 | 2013-11-12 10:54:16 -0800 | [diff] [blame] | 47 |     void update(int32_t ignoreDepth=1, pid_t tid=BACKTRACE_CURRENT_THREAD); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 |  | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 49 |     // Dump a stack trace to the log using the supplied logtag. | 
 | 50 |     void log(const char* logtag, | 
 | 51 |              android_LogPriority priority = ANDROID_LOG_DEBUG, | 
 | 52 |              const char* prefix = 0) const; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 |  | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 54 |     // Dump a stack trace to the specified file descriptor. | 
 | 55 |     void dump(int fd, int indent = 0, const char* prefix = 0) const; | 
 | 56 |  | 
 | 57 |     // 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] | 58 |     String8 toString(const char* prefix = 0) const; | 
| Igor Murashkin | ec79ef2 | 2013-10-24 17:09:15 -0700 | [diff] [blame] | 59 |  | 
 | 60 |     // Dump a serialized representation of the stack trace to the specified printer. | 
 | 61 |     void print(Printer& printer) const; | 
 | 62 |  | 
 | 63 |     // Get the count of stack frames that are in this call stack. | 
| Christopher Ferris | 038ac69 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 64 |     size_t size() const { return mFrameLines.size(); } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 |  | 
 | 66 | private: | 
| Christopher Ferris | 038ac69 | 2013-10-31 16:25:04 -0700 | [diff] [blame] | 67 |     Vector<String8> mFrameLines; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | }; | 
 | 69 |  | 
 | 70 | }; // namespace android | 
 | 71 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | #endif // ANDROID_CALLSTACK_H |