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