blob: f440bd28331d828751271c1580105afb6771ead8 [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 Ferris17e91d42013-10-21 13:30:52 -070022
23#include <string>
Christopher Ferris46756822014-01-14 20:16:30 -080024#include <vector>
Christopher Ferris17e91d42013-10-21 13:30:52 -070025
Christopher Ferris46756822014-01-14 20:16:30 -080026#include <backtrace/backtrace_constants.h>
27#include <backtrace/BacktraceMap.h>
28
Pavel Chupinc6c194c2013-11-21 23:17:20 +040029#if __LP64__
30#define PRIPTR "016" PRIxPTR
31typedef uint64_t word_t;
32#else
33#define PRIPTR "08" PRIxPTR
34typedef uint32_t word_t;
35#endif
36
Christopher Ferris46756822014-01-14 20:16:30 -080037struct backtrace_frame_data_t {
38 size_t num; // The current fame number.
39 uintptr_t pc; // The absolute pc.
40 uintptr_t sp; // The top of the stack.
41 size_t stack_size; // The size of the stack, zero indicate an unknown stack size.
Christopher Ferris12385e32015-02-06 13:22:01 -080042 backtrace_map_t map; // The map associated with the given pc.
Christopher Ferris46756822014-01-14 20:16:30 -080043 std::string func_name; // The function name associated with this pc, NULL if not found.
44 uintptr_t func_offset; // pc relative to the start of the function, only valid if func_name is not NULL.
45};
46
Christopher Ferrisafa9c9c2014-05-09 13:19:34 -070047#if defined(__APPLE__)
48struct __darwin_ucontext;
49typedef __darwin_ucontext ucontext_t;
50#else
Christopher Ferrisb1380372014-05-09 11:04:09 -070051struct ucontext;
52typedef ucontext ucontext_t;
Christopher Ferrisafa9c9c2014-05-09 13:19:34 -070053#endif
Christopher Ferrisb1380372014-05-09 11:04:09 -070054
Yabin Cui9e402bb2015-09-22 04:46:57 +000055struct backtrace_stackinfo_t {
56 uint64_t start;
57 uint64_t end;
58 const uint8_t* data;
59};
60
Christopher Ferris17e91d42013-10-21 13:30:52 -070061class Backtrace {
62public:
Christopher Ferris8ed46272013-10-29 15:44:25 -070063 // Create the correct Backtrace object based on what is to be unwound.
64 // If pid < 0 or equals the current pid, then the Backtrace object
65 // corresponds to the current process.
66 // If pid < 0 or equals the current pid and tid >= 0, then the Backtrace
67 // object corresponds to a thread in the current process.
68 // If pid >= 0 and tid < 0, then the Backtrace object corresponds to a
69 // different process.
70 // Tracing a thread in a different process is not supported.
Christopher Ferris46756822014-01-14 20:16:30 -080071 // If map is NULL, then create the map and manage it internally.
72 // If map is not NULL, the map is still owned by the caller.
73 static Backtrace* Create(pid_t pid, pid_t tid, BacktraceMap* map = NULL);
Christopher Ferris8ed46272013-10-29 15:44:25 -070074
Yabin Cui9e402bb2015-09-22 04:46:57 +000075 // Create an offline Backtrace object that can be used to do an unwind without a process
76 // that is still running. If cache_file is set to true, then elf information will be cached
77 // for this call. The cached information survives until the calling process ends. This means
78 // that subsequent calls to create offline Backtrace objects will continue to use the same
79 // cache. It also assumes that the elf files used for each offline unwind are the same.
80 static Backtrace* CreateOffline(pid_t pid, pid_t tid, BacktraceMap* map,
81 const backtrace_stackinfo_t& stack, bool cache_file = false);
82
Christopher Ferris17e91d42013-10-21 13:30:52 -070083 virtual ~Backtrace();
84
85 // Get the current stack trace and store in the backtrace_ structure.
Christopher Ferris2c43cff2015-03-26 19:18:36 -070086 virtual bool Unwind(size_t num_ignore_frames, ucontext_t* context = NULL) = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070087
88 // Get the function name and offset into the function given the pc.
89 // If the string is empty, then no valid function name was found.
90 virtual std::string GetFunctionName(uintptr_t pc, uintptr_t* offset);
91
Christopher Ferris12385e32015-02-06 13:22:01 -080092 // Fill in the map data associated with the given pc.
93 virtual void FillInMap(uintptr_t pc, backtrace_map_t* map);
Christopher Ferris17e91d42013-10-21 13:30:52 -070094
Christopher Ferris17e91d42013-10-21 13:30:52 -070095 // Read the data at a specific address.
Pavel Chupinc6c194c2013-11-21 23:17:20 +040096 virtual bool ReadWord(uintptr_t ptr, word_t* out_value) = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070097
Christopher Ferris2b4a63f2015-03-17 14:42:03 -070098 // Read arbitrary data from a specific address. If a read request would
99 // span from one map to another, this call only reads up until the end
100 // of the current map.
101 // Returns the total number of bytes actually read.
102 virtual size_t Read(uintptr_t addr, uint8_t* buffer, size_t bytes) = 0;
103
Christopher Ferris17e91d42013-10-21 13:30:52 -0700104 // Create a string representing the formatted line of backtrace information
105 // for a single frame.
106 virtual std::string FormatFrameData(size_t frame_num);
Christopher Ferris20303f82014-01-10 16:33:16 -0800107 virtual std::string FormatFrameData(const backtrace_frame_data_t* frame);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700108
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700109 pid_t Pid() const { return pid_; }
110 pid_t Tid() const { return tid_; }
111 size_t NumFrames() const { return frames_.size(); }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700112
113 const backtrace_frame_data_t* GetFrame(size_t frame_num) {
Christopher Ferris46756822014-01-14 20:16:30 -0800114 if (frame_num >= frames_.size()) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800115 return NULL;
116 }
Christopher Ferris46756822014-01-14 20:16:30 -0800117 return &frames_[frame_num];
Christopher Ferris17e91d42013-10-21 13:30:52 -0700118 }
119
Christopher Ferris46756822014-01-14 20:16:30 -0800120 typedef std::vector<backtrace_frame_data_t>::iterator iterator;
121 iterator begin() { return frames_.begin(); }
122 iterator end() { return frames_.end(); }
123
124 typedef std::vector<backtrace_frame_data_t>::const_iterator const_iterator;
125 const_iterator begin() const { return frames_.begin(); }
126 const_iterator end() const { return frames_.end(); }
127
128 BacktraceMap* GetMap() { return map_; }
Christopher Ferris20303f82014-01-10 16:33:16 -0800129
Christopher Ferris17e91d42013-10-21 13:30:52 -0700130protected:
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700131 Backtrace(pid_t pid, pid_t tid, BacktraceMap* map);
132
133 // The name returned is not demangled, GetFunctionName() takes care of
134 // demangling the name.
135 virtual std::string GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) = 0;
Christopher Ferris8ed46272013-10-29 15:44:25 -0700136
Pavel Chupinc6c194c2013-11-21 23:17:20 +0400137 virtual bool VerifyReadWordArgs(uintptr_t ptr, word_t* out_value);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700138
Christopher Ferris46756822014-01-14 20:16:30 -0800139 bool BuildMap();
140
141 pid_t pid_;
142 pid_t tid_;
143
144 BacktraceMap* map_;
145 bool map_shared_;
146
147 std::vector<backtrace_frame_data_t> frames_;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700148};
149
150#endif // _BACKTRACE_BACKTRACE_H