Backtrace.h revision 9846497f7926fc3240c2893d89e60880c22d1fd6
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
24class BacktraceImpl;
25
26class Backtrace {
27public:
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  // 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);
39
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);
63
64  pid_t Pid() { return backtrace_.pid; }
65  pid_t Tid() { return backtrace_.tid; }
66  size_t NumFrames() { return backtrace_.num_frames; }
67
68  const backtrace_t* GetBacktrace() { return &backtrace_; }
69
70  const backtrace_frame_data_t* GetFrame(size_t frame_num) {
71    return &backtrace_.frames[frame_num];
72  }
73
74protected:
75  Backtrace(BacktraceImpl* impl, pid_t pid, backtrace_map_info_t* map_info);
76
77  virtual bool VerifyReadWordArgs(uintptr_t ptr, uint32_t* out_value);
78
79  BacktraceImpl* impl_;
80
81  backtrace_map_info_t* map_info_;
82
83  bool map_info_requires_delete_;
84
85  backtrace_t backtrace_;
86
87  friend class BacktraceImpl;
88};
89
90#endif // _BACKTRACE_BACKTRACE_H
91