Home | History | Annotate | Download | only in utils
      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 #include <android/log.h>
     21 #include <backtrace/backtrace_constants.h>
     22 #include <utils/String8.h>
     23 #include <utils/Vector.h>
     24 
     25 #include <stdint.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 a single thread.
     33 class CallStack {
     34 public:
     35     // Create an empty call stack. No-op.
     36     CallStack();
     37     // Create a callstack with the current thread's stack trace.
     38     // Immediately dump it to logcat using the given logtag.
     39     CallStack(const char* logtag, int32_t ignoreDepth=1);
     40     ~CallStack();
     41 
     42     // Reset the stack frames (same as creating an empty call stack).
     43     void clear() { mFrameLines.clear(); }
     44 
     45     // Immediately collect the stack traces for the specified thread.
     46     // The default is to dump the stack of the current call.
     47     void update(int32_t ignoreDepth=1, pid_t tid=BACKTRACE_CURRENT_THREAD);
     48 
     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;
     53 
     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.
     58     String8 toString(const char* prefix = 0) const;
     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.
     64     size_t size() const { return mFrameLines.size(); }
     65 
     66 private:
     67     Vector<String8> mFrameLines;
     68 };
     69 
     70 }; // namespace android
     71 
     72 #endif // ANDROID_CALLSTACK_H
     73