• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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_PROCESS_CALLSTACK_H
18 #define ANDROID_PROCESS_CALLSTACK_H
19 
20 #include <utils/CallStack.h>
21 #include <android/log.h>
22 #include <utils/KeyedVector.h>
23 #include <utils/String8.h>
24 
25 #include <time.h>
26 #include <sys/types.h>
27 
28 namespace android {
29 
30 class Printer;
31 
32 // Collect/print the call stack (function, file, line) traces for all threads in a process.
33 class ProcessCallStack {
34 public:
35     // Create an empty call stack. No-op.
36     ProcessCallStack();
37     // Copy the existing process callstack (no other side effects).
38     ProcessCallStack(const ProcessCallStack& rhs);
39     ~ProcessCallStack();
40 
41     // Immediately collect the stack traces for all threads.
42     void update();
43 
44     // Print all stack traces to the log using the supplied logtag.
45     void log(const char* logtag, android_LogPriority priority = ANDROID_LOG_DEBUG,
46              const char* prefix = nullptr) const;
47 
48     // Dump all stack traces to the specified file descriptor.
49     void dump(int fd, int indent = 0, const char* prefix = nullptr) const;
50 
51     // Return a string (possibly very long) containing all the stack traces.
52     String8 toString(const char* prefix = nullptr) const;
53 
54     // Dump a serialized representation of all the stack traces to the specified printer.
55     void print(Printer& printer) const;
56 
57     // Get the number of threads whose stack traces were collected.
58     size_t size() const;
59 
60 private:
61     void printInternal(Printer& printer, Printer& csPrinter) const;
62 
63     // Reset the process's stack frames and metadata.
64     void clear();
65 
66     struct ThreadInfo {
67         CallStack callStack;
68         String8 threadName;
69     };
70 
71     // tid -> ThreadInfo
72     KeyedVector<pid_t, ThreadInfo> mThreadMap;
73     // Time that update() was last called
74     struct tm mTimeUpdated;
75 };
76 
77 }  // namespace android
78 
79 #endif // ANDROID_PROCESS_CALLSTACK_H
80