• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FALLTHROUGH_INTENDED
21 #define FALLTHROUGH_INTENDED [[clang::fallthrough]]  // NOLINT
22 #endif
23 
24 #define BACKTRACE_CURRENT_THREAD 0
25 
26 #include <android/log.h>
27 #include <log/log.h>
28 // #include <backtrace/backtrace_constants.h>
29 #include <utils/String8.h>
30 // #include <utils/Vector.h>
31 
32 #include <stdint.h>
33 #include <sys/types.h>
34 
35 #include <vector>
36 
37 namespace android {
38 
39 class Printer;
40 
41 // Collect/print the call stack (function, file, line) traces for a single thread.
42 class CallStack {
43 public:
44     // Create an empty call stack. No-op.
45     CallStack();
46     // Create a callstack with the current thread's stack trace.
47     // Immediately dump it to logcat using the given logtag.
48     CallStack(const char* logtag, int32_t ignoreDepth=1);
49     ~CallStack();
50 
51     // Reset the stack frames (same as creating an empty call stack).
clear()52     void clear() { mFrameLines.clear(); }
53 
54     // Immediately collect the stack traces for the specified thread.
55     // The default is to dump the stack of the current call.
56     void update(int32_t ignoreDepth=1, pid_t tid=BACKTRACE_CURRENT_THREAD);
57 
58     // Dump a stack trace to the log using the supplied logtag.
59     void log(const char* logtag,
60              android_LogPriority priority = ANDROID_LOG_DEBUG,
61              const char* prefix = 0) const;
62 
63     // Dump a stack trace to the specified file descriptor.
64     void dump(int fd, int indent = 0, const char* prefix = 0) const;
65 
66     // Return a string (possibly very long) containing the complete stack trace.
67     String8 toString(const char* prefix = 0) const;
68 
69     // Dump a serialized representation of the stack trace to the specified printer.
70     void print(Printer& printer) const;
71 
72     // Get the count of stack frames that are in this call stack.
size()73     size_t size() const { return mFrameLines.size(); }
74 
75 private:
76     std::vector<String8> mFrameLines;
77 };
78 
79 }  // namespace android
80 
81 #endif // ANDROID_CALLSTACK_H
82