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 | |
| 20 | #include <backtrace/backtrace.h> |
| 21 | |
| 22 | #include <string> |
| 23 | |
| 24 | class BacktraceImpl; |
| 25 | |
| 26 | class Backtrace { |
| 27 | public: |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 28 | // Create the correct Backtrace object based on what is to be unwound. |
| 29 | // If pid < 0 or equals the current pid, then the Backtrace object |
| 30 | // corresponds to the current process. |
| 31 | // If pid < 0 or equals the current pid and tid >= 0, then the Backtrace |
| 32 | // object corresponds to a thread in the current process. |
| 33 | // If pid >= 0 and tid < 0, then the Backtrace object corresponds to a |
| 34 | // different process. |
| 35 | // Tracing a thread in a different process is not supported. |
| 36 | static Backtrace* Create(pid_t pid, pid_t tid); |
| 37 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 38 | virtual ~Backtrace(); |
| 39 | |
| 40 | // Get the current stack trace and store in the backtrace_ structure. |
| 41 | virtual bool Unwind(size_t num_ignore_frames); |
| 42 | |
| 43 | // Get the function name and offset into the function given the pc. |
| 44 | // If the string is empty, then no valid function name was found. |
| 45 | virtual std::string GetFunctionName(uintptr_t pc, uintptr_t* offset); |
| 46 | |
| 47 | // Get the name of the map associated with the given pc. If NULL is returned, |
| 48 | // then map_start is not set. Otherwise, map_start is the beginning of this |
| 49 | // map. |
| 50 | virtual const char* GetMapName(uintptr_t pc, uintptr_t* map_start); |
| 51 | |
| 52 | // Finds the memory map associated with the given ptr. |
| 53 | virtual const backtrace_map_info_t* FindMapInfo(uintptr_t ptr); |
| 54 | |
| 55 | // Read the data at a specific address. |
| 56 | virtual bool ReadWord(uintptr_t ptr, uint32_t* out_value) = 0; |
| 57 | |
| 58 | // Create a string representing the formatted line of backtrace information |
| 59 | // for a single frame. |
| 60 | virtual std::string FormatFrameData(size_t frame_num); |
| 61 | |
| 62 | pid_t Pid() { return backtrace_.pid; } |
| 63 | pid_t Tid() { return backtrace_.tid; } |
| 64 | size_t NumFrames() { return backtrace_.num_frames; } |
| 65 | |
| 66 | const backtrace_t* GetBacktrace() { return &backtrace_; } |
| 67 | |
| 68 | const backtrace_frame_data_t* GetFrame(size_t frame_num) { |
| 69 | return &backtrace_.frames[frame_num]; |
| 70 | } |
| 71 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 72 | protected: |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 73 | Backtrace(BacktraceImpl* impl); |
| 74 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 75 | virtual bool VerifyReadWordArgs(uintptr_t ptr, uint32_t* out_value); |
| 76 | |
| 77 | BacktraceImpl* impl_; |
| 78 | |
| 79 | backtrace_map_info_t* map_info_; |
| 80 | |
| 81 | backtrace_t backtrace_; |
| 82 | |
| 83 | friend class BacktraceImpl; |
| 84 | }; |
| 85 | |
| 86 | #endif // _BACKTRACE_BACKTRACE_H |