blob: 583603768c4452321334fdcae71adf5cd85d02f3 [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>
Christopher Ferris038ac692013-10-31 16:25:04 -070021#include <backtrace/backtrace.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080022#include <utils/String8.h>
Christopher Ferris038ac692013-10-31 16:25:04 -070023#include <utils/Vector.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080024
Igor Murashkinec79ef22013-10-24 17:09:15 -070025#include <stdint.h>
26#include <sys/types.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080027
28namespace android {
29
Igor Murashkinec79ef22013-10-24 17:09:15 -070030class Printer;
31
32// Collect/print the call stack (function, file, line) traces for a single thread.
33class CallStack {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080034public:
Igor Murashkinec79ef22013-10-24 17:09:15 -070035 // Create an empty call stack. No-op.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080036 CallStack();
Igor Murashkinec79ef22013-10-24 17:09:15 -070037 // Create a callstack with the current thread's stack trace.
38 // Immediately dump it to logcat using the given logtag.
Christopher Ferris038ac692013-10-31 16:25:04 -070039 CallStack(const char* logtag, int32_t ignoreDepth=1);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080040 ~CallStack();
41
Igor Murashkinec79ef22013-10-24 17:09:15 -070042 // Reset the stack frames (same as creating an empty call stack).
Christopher Ferris038ac692013-10-31 16:25:04 -070043 void clear() { mFrameLines.clear(); }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080044
Igor Murashkinec79ef22013-10-24 17:09:15 -070045 // Immediately collect the stack traces for the specified thread.
Christopher Ferris038ac692013-10-31 16:25:04 -070046 // The default is to dump the stack of the current call.
47 void update(int32_t ignoreDepth=1, pid_t tid=BACKTRACE_NO_TID);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080048
Igor Murashkinec79ef22013-10-24 17:09:15 -070049 // 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 Projectcbb10112009-03-03 19:31:44 -080053
Igor Murashkinec79ef22013-10-24 17:09:15 -070054 // 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 Projectcbb10112009-03-03 19:31:44 -080058 String8 toString(const char* prefix = 0) const;
Igor Murashkinec79ef22013-10-24 17:09:15 -070059
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 Ferris038ac692013-10-31 16:25:04 -070064 size_t size() const { return mFrameLines.size(); }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080065
66private:
Christopher Ferris038ac692013-10-31 16:25:04 -070067 Vector<String8> mFrameLines;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080068};
69
70}; // namespace android
71
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080072#endif // ANDROID_CALLSTACK_H