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. |
Christopher Ferris | 9846497 | 2014-01-06 19:16:33 -0800 | [diff] [blame] | 36 | // If map_info is NULL, then create the map and manage it internally. |
| 37 | // If map_info is not NULL, the map is still owned by the caller. |
| 38 | static Backtrace* Create(pid_t pid, pid_t tid, backtrace_map_info_t* map_info = NULL); |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 39 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 40 | virtual ~Backtrace(); |
| 41 | |
| 42 | // Get the current stack trace and store in the backtrace_ structure. |
| 43 | virtual bool Unwind(size_t num_ignore_frames); |
| 44 | |
| 45 | // Get the function name and offset into the function given the pc. |
| 46 | // If the string is empty, then no valid function name was found. |
| 47 | virtual std::string GetFunctionName(uintptr_t pc, uintptr_t* offset); |
| 48 | |
| 49 | // Get the name of the map associated with the given pc. If NULL is returned, |
| 50 | // then map_start is not set. Otherwise, map_start is the beginning of this |
| 51 | // map. |
| 52 | virtual const char* GetMapName(uintptr_t pc, uintptr_t* map_start); |
| 53 | |
| 54 | // Finds the memory map associated with the given ptr. |
| 55 | virtual const backtrace_map_info_t* FindMapInfo(uintptr_t ptr); |
| 56 | |
| 57 | // Read the data at a specific address. |
| 58 | virtual bool ReadWord(uintptr_t ptr, uint32_t* out_value) = 0; |
| 59 | |
| 60 | // Create a string representing the formatted line of backtrace information |
| 61 | // for a single frame. |
| 62 | virtual std::string FormatFrameData(size_t frame_num); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 63 | virtual std::string FormatFrameData(const backtrace_frame_data_t* frame); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 64 | |
| 65 | pid_t Pid() { return backtrace_.pid; } |
| 66 | pid_t Tid() { return backtrace_.tid; } |
| 67 | size_t NumFrames() { return backtrace_.num_frames; } |
| 68 | |
| 69 | const backtrace_t* GetBacktrace() { return &backtrace_; } |
| 70 | |
| 71 | const backtrace_frame_data_t* GetFrame(size_t frame_num) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 72 | if (frame_num > NumFrames()) { |
| 73 | return NULL; |
| 74 | } |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 75 | return &backtrace_.frames[frame_num]; |
| 76 | } |
| 77 | |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 78 | const backtrace_map_info_t* GetMapList() { |
| 79 | return map_info_; |
| 80 | } |
| 81 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 82 | protected: |
Christopher Ferris | 9846497 | 2014-01-06 19:16:33 -0800 | [diff] [blame] | 83 | Backtrace(BacktraceImpl* impl, pid_t pid, backtrace_map_info_t* map_info); |
Christopher Ferris | 8ed4627 | 2013-10-29 15:44:25 -0700 | [diff] [blame] | 84 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 85 | virtual bool VerifyReadWordArgs(uintptr_t ptr, uint32_t* out_value); |
| 86 | |
| 87 | BacktraceImpl* impl_; |
| 88 | |
| 89 | backtrace_map_info_t* map_info_; |
| 90 | |
Christopher Ferris | 9846497 | 2014-01-06 19:16:33 -0800 | [diff] [blame] | 91 | bool map_info_requires_delete_; |
| 92 | |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 93 | backtrace_t backtrace_; |
| 94 | |
| 95 | friend class BacktraceImpl; |
| 96 | }; |
| 97 | |
| 98 | #endif // _BACKTRACE_BACKTRACE_H |