blob: df40b87562f01648b6642ce1bed5bf641bf4a160 [file] [log] [blame]
Christopher Ferris17e91d42013-10-21 13:30:52 -07001/*
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
24class BacktraceImpl;
25
26class Backtrace {
27public:
Christopher Ferris8ed46272013-10-29 15:44:25 -070028 // 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 Ferris98464972014-01-06 19:16:33 -080036 // 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 Ferris8ed46272013-10-29 15:44:25 -070039
Christopher Ferris17e91d42013-10-21 13:30:52 -070040 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 Ferris20303f82014-01-10 16:33:16 -080063 virtual std::string FormatFrameData(const backtrace_frame_data_t* frame);
Christopher Ferris17e91d42013-10-21 13:30:52 -070064
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 Ferris20303f82014-01-10 16:33:16 -080072 if (frame_num > NumFrames()) {
73 return NULL;
74 }
Christopher Ferris17e91d42013-10-21 13:30:52 -070075 return &backtrace_.frames[frame_num];
76 }
77
Christopher Ferris20303f82014-01-10 16:33:16 -080078 const backtrace_map_info_t* GetMapList() {
79 return map_info_;
80 }
81
Christopher Ferris17e91d42013-10-21 13:30:52 -070082protected:
Christopher Ferris98464972014-01-06 19:16:33 -080083 Backtrace(BacktraceImpl* impl, pid_t pid, backtrace_map_info_t* map_info);
Christopher Ferris8ed46272013-10-29 15:44:25 -070084
Christopher Ferris17e91d42013-10-21 13:30:52 -070085 virtual bool VerifyReadWordArgs(uintptr_t ptr, uint32_t* out_value);
86
87 BacktraceImpl* impl_;
88
89 backtrace_map_info_t* map_info_;
90
Christopher Ferris98464972014-01-06 19:16:33 -080091 bool map_info_requires_delete_;
92
Christopher Ferris17e91d42013-10-21 13:30:52 -070093 backtrace_t backtrace_;
94
95 friend class BacktraceImpl;
96};
97
98#endif // _BACKTRACE_BACKTRACE_H