Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 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 _BACKTRACE_BACKTRACE_H |
| 18 | #define _BACKTRACE_BACKTRACE_H |
| 19 | |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 20 | #include <inttypes.h> |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 22 | |
| 23 | #include <string> |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 24 | #include <vector> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 25 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 26 | #include <backtrace/backtrace_constants.h> |
| 27 | #include <backtrace/BacktraceMap.h> |
| 28 | |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 29 | #if __LP64__ |
| 30 | #define PRIPTR "016" PRIxPTR |
| 31 | typedef uint64_t word_t; |
| 32 | #else |
| 33 | #define PRIPTR "08" PRIxPTR |
| 34 | typedef uint32_t word_t; |
| 35 | #endif |
| 36 | |
Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame^] | 37 | enum BacktraceUnwindError : uint32_t { |
| 38 | BACKTRACE_UNWIND_NO_ERROR, |
| 39 | // Something failed while trying to perform the setup to begin the unwind. |
| 40 | BACKTRACE_UNWIND_ERROR_SETUP_FAILED, |
| 41 | // There is no map information to use with the unwind. |
| 42 | BACKTRACE_UNWIND_ERROR_MAP_MISSING, |
| 43 | // An error occurred that indicates a programming error. |
| 44 | BACKTRACE_UNWIND_ERROR_INTERNAL, |
| 45 | // The thread to unwind has disappeared before the unwind can begin. |
| 46 | BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST, |
| 47 | // The thread to unwind has not responded to a signal in a timely manner. |
| 48 | BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT, |
| 49 | // Attempt to do an unsupported operation. |
| 50 | BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION, |
| 51 | // Attempt to do an offline unwind without a context. |
| 52 | BACKTRACE_UNWIND_ERROR_NO_CONTEXT, |
| 53 | }; |
| 54 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 55 | struct backtrace_frame_data_t { |
| 56 | size_t num; // The current fame number. |
| 57 | uintptr_t pc; // The absolute pc. |
| 58 | uintptr_t sp; // The top of the stack. |
| 59 | size_t stack_size; // The size of the stack, zero indicate an unknown stack size. |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 60 | backtrace_map_t map; // The map associated with the given pc. |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 61 | std::string func_name; // The function name associated with this pc, NULL if not found. |
| 62 | uintptr_t func_offset; // pc relative to the start of the function, only valid if func_name is not NULL. |
| 63 | }; |
| 64 | |
Christopher Ferris | afa9c9c | 2014-05-09 13:19:34 -0700 | [diff] [blame] | 65 | #if defined(__APPLE__) |
| 66 | struct __darwin_ucontext; |
| 67 | typedef __darwin_ucontext ucontext_t; |
| 68 | #else |
Christopher Ferris | b138037 | 2014-05-09 11:04:09 -0700 | [diff] [blame] | 69 | struct ucontext; |
| 70 | typedef ucontext ucontext_t; |
Christopher Ferris | afa9c9c | 2014-05-09 13:19:34 -0700 | [diff] [blame] | 71 | #endif |
Christopher Ferris | b138037 | 2014-05-09 11:04:09 -0700 | [diff] [blame] | 72 | |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 73 | struct backtrace_stackinfo_t { |
| 74 | uint64_t start; |
| 75 | uint64_t end; |
| 76 | const uint8_t* data; |
| 77 | }; |
| 78 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 79 | class Backtrace { |
| 80 | public: |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 81 | // Create the correct Backtrace object based on what is to be unwound. |
| 82 | // If pid < 0 or equals the current pid, then the Backtrace object |
| 83 | // corresponds to the current process. |
| 84 | // If pid < 0 or equals the current pid and tid >= 0, then the Backtrace |
| 85 | // object corresponds to a thread in the current process. |
| 86 | // If pid >= 0 and tid < 0, then the Backtrace object corresponds to a |
| 87 | // different process. |
| 88 | // Tracing a thread in a different process is not supported. |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 89 | // If map is NULL, then create the map and manage it internally. |
| 90 | // If map is not NULL, the map is still owned by the caller. |
| 91 | static Backtrace* Create(pid_t pid, pid_t tid, BacktraceMap* map = NULL); |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 92 | |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 93 | // Create an offline Backtrace object that can be used to do an unwind without a process |
| 94 | // that is still running. If cache_file is set to true, then elf information will be cached |
| 95 | // for this call. The cached information survives until the calling process ends. This means |
| 96 | // that subsequent calls to create offline Backtrace objects will continue to use the same |
| 97 | // cache. It also assumes that the elf files used for each offline unwind are the same. |
| 98 | static Backtrace* CreateOffline(pid_t pid, pid_t tid, BacktraceMap* map, |
| 99 | const backtrace_stackinfo_t& stack, bool cache_file = false); |
| 100 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 101 | virtual ~Backtrace(); |
| 102 | |
| 103 | // Get the current stack trace and store in the backtrace_ structure. |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 104 | virtual bool Unwind(size_t num_ignore_frames, ucontext_t* context = NULL) = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 105 | |
| 106 | // Get the function name and offset into the function given the pc. |
| 107 | // If the string is empty, then no valid function name was found. |
| 108 | virtual std::string GetFunctionName(uintptr_t pc, uintptr_t* offset); |
| 109 | |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 110 | // Fill in the map data associated with the given pc. |
| 111 | virtual void FillInMap(uintptr_t pc, backtrace_map_t* map); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 112 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 113 | // Read the data at a specific address. |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 114 | virtual bool ReadWord(uintptr_t ptr, word_t* out_value) = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 115 | |
Christopher Ferris | 2b4a63f | 2015-03-17 14:42:03 -0700 | [diff] [blame] | 116 | // Read arbitrary data from a specific address. If a read request would |
| 117 | // span from one map to another, this call only reads up until the end |
| 118 | // of the current map. |
| 119 | // Returns the total number of bytes actually read. |
| 120 | virtual size_t Read(uintptr_t addr, uint8_t* buffer, size_t bytes) = 0; |
| 121 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 122 | // Create a string representing the formatted line of backtrace information |
| 123 | // for a single frame. |
| 124 | virtual std::string FormatFrameData(size_t frame_num); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 125 | virtual std::string FormatFrameData(const backtrace_frame_data_t* frame); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 126 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 127 | pid_t Pid() const { return pid_; } |
| 128 | pid_t Tid() const { return tid_; } |
| 129 | size_t NumFrames() const { return frames_.size(); } |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 130 | |
| 131 | const backtrace_frame_data_t* GetFrame(size_t frame_num) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 132 | if (frame_num >= frames_.size()) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 133 | return NULL; |
| 134 | } |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 135 | return &frames_[frame_num]; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 138 | typedef std::vector<backtrace_frame_data_t>::iterator iterator; |
| 139 | iterator begin() { return frames_.begin(); } |
| 140 | iterator end() { return frames_.end(); } |
| 141 | |
| 142 | typedef std::vector<backtrace_frame_data_t>::const_iterator const_iterator; |
| 143 | const_iterator begin() const { return frames_.begin(); } |
| 144 | const_iterator end() const { return frames_.end(); } |
| 145 | |
| 146 | BacktraceMap* GetMap() { return map_; } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 147 | |
Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame^] | 148 | BacktraceUnwindError GetError() { return error_; } |
| 149 | |
| 150 | std::string GetErrorString(BacktraceUnwindError error); |
| 151 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 152 | protected: |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 153 | Backtrace(pid_t pid, pid_t tid, BacktraceMap* map); |
| 154 | |
| 155 | // The name returned is not demangled, GetFunctionName() takes care of |
| 156 | // demangling the name. |
| 157 | virtual std::string GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) = 0; |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 158 | |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 159 | virtual bool VerifyReadWordArgs(uintptr_t ptr, word_t* out_value); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 160 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 161 | bool BuildMap(); |
| 162 | |
| 163 | pid_t pid_; |
| 164 | pid_t tid_; |
| 165 | |
| 166 | BacktraceMap* map_; |
| 167 | bool map_shared_; |
| 168 | |
| 169 | std::vector<backtrace_frame_data_t> frames_; |
Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame^] | 170 | |
| 171 | BacktraceUnwindError error_; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | #endif // _BACKTRACE_BACKTRACE_H |