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 | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 37 | struct backtrace_frame_data_t { |
| 38 | size_t num; // The current fame number. |
| 39 | uintptr_t pc; // The absolute pc. |
| 40 | uintptr_t sp; // The top of the stack. |
| 41 | size_t stack_size; // The size of the stack, zero indicate an unknown stack size. |
| 42 | const backtrace_map_t* map; // The map associated with the given pc. |
| 43 | std::string func_name; // The function name associated with this pc, NULL if not found. |
| 44 | uintptr_t func_offset; // pc relative to the start of the function, only valid if func_name is not NULL. |
| 45 | }; |
| 46 | |
| 47 | // Forward declarations. |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 48 | class BacktraceImpl; |
| 49 | |
Christopher Ferris | ee15dde | 2014-05-09 13:19:34 -0700 | [diff] [blame] | 50 | #if defined(__APPLE__) |
| 51 | struct __darwin_ucontext; |
| 52 | typedef __darwin_ucontext ucontext_t; |
| 53 | #else |
Christopher Ferris | 8fb224d | 2014-05-09 11:04:09 -0700 | [diff] [blame] | 54 | struct ucontext; |
| 55 | typedef ucontext ucontext_t; |
Christopher Ferris | ee15dde | 2014-05-09 13:19:34 -0700 | [diff] [blame] | 56 | #endif |
Christopher Ferris | 8fb224d | 2014-05-09 11:04:09 -0700 | [diff] [blame] | 57 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 58 | class Backtrace { |
| 59 | public: |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 60 | // Create the correct Backtrace object based on what is to be unwound. |
| 61 | // If pid < 0 or equals the current pid, then the Backtrace object |
| 62 | // corresponds to the current process. |
| 63 | // If pid < 0 or equals the current pid and tid >= 0, then the Backtrace |
| 64 | // object corresponds to a thread in the current process. |
| 65 | // If pid >= 0 and tid < 0, then the Backtrace object corresponds to a |
| 66 | // different process. |
| 67 | // Tracing a thread in a different process is not supported. |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 68 | // If map is NULL, then create the map and manage it internally. |
| 69 | // If map is not NULL, the map is still owned by the caller. |
| 70 | static Backtrace* Create(pid_t pid, pid_t tid, BacktraceMap* map = NULL); |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 71 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 72 | virtual ~Backtrace(); |
| 73 | |
| 74 | // Get the current stack trace and store in the backtrace_ structure. |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 75 | virtual bool Unwind(size_t num_ignore_frames, ucontext_t* context = NULL); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 76 | |
| 77 | // Get the function name and offset into the function given the pc. |
| 78 | // If the string is empty, then no valid function name was found. |
| 79 | virtual std::string GetFunctionName(uintptr_t pc, uintptr_t* offset); |
| 80 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 81 | // Find the map associated with the given pc. |
| 82 | virtual const backtrace_map_t* FindMap(uintptr_t pc); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 83 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 84 | // Read the data at a specific address. |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 85 | virtual bool ReadWord(uintptr_t ptr, word_t* out_value) = 0; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 86 | |
| 87 | // Create a string representing the formatted line of backtrace information |
| 88 | // for a single frame. |
| 89 | virtual std::string FormatFrameData(size_t frame_num); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 90 | virtual std::string FormatFrameData(const backtrace_frame_data_t* frame); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 91 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 92 | pid_t Pid() { return pid_; } |
| 93 | pid_t Tid() { return tid_; } |
| 94 | size_t NumFrames() { return frames_.size(); } |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 95 | |
| 96 | const backtrace_frame_data_t* GetFrame(size_t frame_num) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 97 | if (frame_num >= frames_.size()) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 98 | return NULL; |
| 99 | } |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 100 | return &frames_[frame_num]; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 103 | typedef std::vector<backtrace_frame_data_t>::iterator iterator; |
| 104 | iterator begin() { return frames_.begin(); } |
| 105 | iterator end() { return frames_.end(); } |
| 106 | |
| 107 | typedef std::vector<backtrace_frame_data_t>::const_iterator const_iterator; |
| 108 | const_iterator begin() const { return frames_.begin(); } |
| 109 | const_iterator end() const { return frames_.end(); } |
| 110 | |
| 111 | BacktraceMap* GetMap() { return map_; } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 112 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 113 | protected: |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 114 | Backtrace(BacktraceImpl* impl, pid_t pid, BacktraceMap* map); |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 115 | |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 116 | virtual bool VerifyReadWordArgs(uintptr_t ptr, word_t* out_value); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 117 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 118 | bool BuildMap(); |
| 119 | |
| 120 | pid_t pid_; |
| 121 | pid_t tid_; |
| 122 | |
| 123 | BacktraceMap* map_; |
| 124 | bool map_shared_; |
| 125 | |
| 126 | std::vector<backtrace_frame_data_t> frames_; |
| 127 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 128 | BacktraceImpl* impl_; |
| 129 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 130 | friend class BacktraceImpl; |
| 131 | }; |
| 132 | |
| 133 | #endif // _BACKTRACE_BACKTRACE_H |