blob: 561dfeb90e6197c7efa158111fdc7d06546ab5b2 [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
Pavel Chupinc6c194c2013-11-21 23:17:20 +040020#include <inttypes.h>
Christopher Ferris46756822014-01-14 20:16:30 -080021#include <stdint.h>
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070022#include <ucontext.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070023
24#include <string>
Christopher Ferris46756822014-01-14 20:16:30 -080025#include <vector>
Christopher Ferris17e91d42013-10-21 13:30:52 -070026
Christopher Ferris46756822014-01-14 20:16:30 -080027#include <backtrace/backtrace_constants.h>
28#include <backtrace/BacktraceMap.h>
29
Pavel Chupinc6c194c2013-11-21 23:17:20 +040030#if __LP64__
31#define PRIPTR "016" PRIxPTR
32typedef uint64_t word_t;
33#else
34#define PRIPTR "08" PRIxPTR
35typedef uint32_t word_t;
36#endif
37
Christopher Ferris46756822014-01-14 20:16:30 -080038struct backtrace_frame_data_t {
39 size_t num; // The current fame number.
40 uintptr_t pc; // The absolute pc.
41 uintptr_t sp; // The top of the stack.
42 size_t stack_size; // The size of the stack, zero indicate an unknown stack size.
43 const backtrace_map_t* map; // The map associated with the given pc.
44 std::string func_name; // The function name associated with this pc, NULL if not found.
45 uintptr_t func_offset; // pc relative to the start of the function, only valid if func_name is not NULL.
46};
47
48// Forward declarations.
Christopher Ferris17e91d42013-10-21 13:30:52 -070049class BacktraceImpl;
50
51class Backtrace {
52public:
Christopher Ferris8ed46272013-10-29 15:44:25 -070053 // Create the correct Backtrace object based on what is to be unwound.
54 // If pid < 0 or equals the current pid, then the Backtrace object
55 // corresponds to the current process.
56 // If pid < 0 or equals the current pid and tid >= 0, then the Backtrace
57 // object corresponds to a thread in the current process.
58 // If pid >= 0 and tid < 0, then the Backtrace object corresponds to a
59 // different process.
60 // Tracing a thread in a different process is not supported.
Christopher Ferris46756822014-01-14 20:16:30 -080061 // If map is NULL, then create the map and manage it internally.
62 // If map is not NULL, the map is still owned by the caller.
63 static Backtrace* Create(pid_t pid, pid_t tid, BacktraceMap* map = NULL);
Christopher Ferris8ed46272013-10-29 15:44:25 -070064
Christopher Ferris17e91d42013-10-21 13:30:52 -070065 virtual ~Backtrace();
66
67 // Get the current stack trace and store in the backtrace_ structure.
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070068 virtual bool Unwind(size_t num_ignore_frames, ucontext_t* context = NULL);
Christopher Ferris17e91d42013-10-21 13:30:52 -070069
70 // Get the function name and offset into the function given the pc.
71 // If the string is empty, then no valid function name was found.
72 virtual std::string GetFunctionName(uintptr_t pc, uintptr_t* offset);
73
Christopher Ferris46756822014-01-14 20:16:30 -080074 // Find the map associated with the given pc.
75 virtual const backtrace_map_t* FindMap(uintptr_t pc);
Christopher Ferris17e91d42013-10-21 13:30:52 -070076
Christopher Ferris17e91d42013-10-21 13:30:52 -070077 // Read the data at a specific address.
Pavel Chupinc6c194c2013-11-21 23:17:20 +040078 virtual bool ReadWord(uintptr_t ptr, word_t* out_value) = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070079
80 // Create a string representing the formatted line of backtrace information
81 // for a single frame.
82 virtual std::string FormatFrameData(size_t frame_num);
Christopher Ferris20303f82014-01-10 16:33:16 -080083 virtual std::string FormatFrameData(const backtrace_frame_data_t* frame);
Christopher Ferris17e91d42013-10-21 13:30:52 -070084
Christopher Ferris46756822014-01-14 20:16:30 -080085 pid_t Pid() { return pid_; }
86 pid_t Tid() { return tid_; }
87 size_t NumFrames() { return frames_.size(); }
Christopher Ferris17e91d42013-10-21 13:30:52 -070088
89 const backtrace_frame_data_t* GetFrame(size_t frame_num) {
Christopher Ferris46756822014-01-14 20:16:30 -080090 if (frame_num >= frames_.size()) {
Christopher Ferris20303f82014-01-10 16:33:16 -080091 return NULL;
92 }
Christopher Ferris46756822014-01-14 20:16:30 -080093 return &frames_[frame_num];
Christopher Ferris17e91d42013-10-21 13:30:52 -070094 }
95
Christopher Ferris46756822014-01-14 20:16:30 -080096 typedef std::vector<backtrace_frame_data_t>::iterator iterator;
97 iterator begin() { return frames_.begin(); }
98 iterator end() { return frames_.end(); }
99
100 typedef std::vector<backtrace_frame_data_t>::const_iterator const_iterator;
101 const_iterator begin() const { return frames_.begin(); }
102 const_iterator end() const { return frames_.end(); }
103
104 BacktraceMap* GetMap() { return map_; }
Christopher Ferris20303f82014-01-10 16:33:16 -0800105
Christopher Ferris17e91d42013-10-21 13:30:52 -0700106protected:
Christopher Ferris46756822014-01-14 20:16:30 -0800107 Backtrace(BacktraceImpl* impl, pid_t pid, BacktraceMap* map);
Christopher Ferris8ed46272013-10-29 15:44:25 -0700108
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400109 virtual bool VerifyReadWordArgs(uintptr_t ptr, word_t* out_value);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700110
Christopher Ferris46756822014-01-14 20:16:30 -0800111 bool BuildMap();
112
113 pid_t pid_;
114 pid_t tid_;
115
116 BacktraceMap* map_;
117 bool map_shared_;
118
119 std::vector<backtrace_frame_data_t> frames_;
120
Christopher Ferris17e91d42013-10-21 13:30:52 -0700121 BacktraceImpl* impl_;
122
Christopher Ferris17e91d42013-10-21 13:30:52 -0700123 friend class BacktraceImpl;
124};
125
126#endif // _BACKTRACE_BACKTRACE_H