1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "libbacktrace"
#include <backtrace/Backtrace.h>
#include <string.h>
#include <backtrace-arch.h>
#include <corkscrew/backtrace.h>
#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <dlfcn.h>
#include "Corkscrew.h"
//-------------------------------------------------------------------------
// CorkscrewMap functions.
//-------------------------------------------------------------------------
CorkscrewMap::CorkscrewMap(pid_t pid) : BacktraceMap(pid), map_info_(NULL) {
}
CorkscrewMap::~CorkscrewMap() {
if (map_info_) {
free_map_info_list(map_info_);
map_info_ = NULL;
}
}
bool CorkscrewMap::Build() {
map_info_ = load_map_info_list(pid_);
// Use the information in map_info_ to construct the BacktraceMap data
// rather than reparsing /proc/self/maps.
map_info_t* cur_map = map_info_;
while (cur_map) {
backtrace_map_t map;
map.start = cur_map->start;
map.end = cur_map->end;
map.flags = 0;
if (cur_map->is_readable) {
map.flags |= PROT_READ;
}
if (cur_map->is_writable) {
map.flags |= PROT_WRITE;
}
if (cur_map->is_executable) {
map.flags |= PROT_EXEC;
}
map.name = cur_map->name;
// The maps are in descending order, but we want them in ascending order.
maps_.push_front(map);
cur_map = cur_map->next;
}
return map_info_ != NULL;
}
//-------------------------------------------------------------------------
// CorkscrewCommon functions.
//-------------------------------------------------------------------------
bool CorkscrewCommon::GenerateFrameData(
backtrace_frame_t* cork_frames, ssize_t num_frames) {
if (num_frames < 0) {
BACK_LOGW("libcorkscrew unwind failed.");
return false;
}
std::vector<backtrace_frame_data_t>* frames = GetFrames();
frames->resize(num_frames);
size_t i = 0;
for (std::vector<backtrace_frame_data_t>::iterator it = frames->begin();
it != frames->end(); ++it, ++i) {
it->num = i;
it->pc = cork_frames[i].absolute_pc;
it->sp = cork_frames[i].stack_top;
it->stack_size = cork_frames[i].stack_size;
it->func_offset = 0;
it->map = FindMap(it->pc);
it->func_name = GetFunctionName(it->pc, &it->func_offset);
}
return true;
}
//-------------------------------------------------------------------------
// CorkscrewCurrent functions.
//-------------------------------------------------------------------------
CorkscrewCurrent::CorkscrewCurrent() {
}
CorkscrewCurrent::~CorkscrewCurrent() {
}
bool CorkscrewCurrent::Unwind(size_t num_ignore_frames) {
backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
ssize_t num_frames = unwind_backtrace(frames, num_ignore_frames, MAX_BACKTRACE_FRAMES);
return GenerateFrameData(frames, num_frames);
}
std::string CorkscrewCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
*offset = 0;
Dl_info info;
const backtrace_map_t* map = FindMap(pc);
if (map) {
if (dladdr((const void*)pc, &info)) {
if (info.dli_sname) {
*offset = pc - map->start - (uintptr_t)info.dli_saddr + (uintptr_t)info.dli_fbase;
return info.dli_sname;
}
} else {
// dladdr(3) didn't find a symbol; maybe it's static? Look in the ELF file...
symbol_table_t* symbol_table = load_symbol_table(map->name.c_str());
if (symbol_table) {
// First check if we can find the symbol using a relative pc.
std::string name;
const symbol_t* elf_symbol = find_symbol(symbol_table, pc - map->start);
if (elf_symbol) {
name = elf_symbol->name;
*offset = pc - map->start - elf_symbol->start;
} else if ((elf_symbol = find_symbol(symbol_table, pc)) != NULL) {
// Found the symbol using the absolute pc.
name = elf_symbol->name;
*offset = pc - elf_symbol->start;
}
free_symbol_table(symbol_table);
return name;
}
}
}
return "";
}
//-------------------------------------------------------------------------
// CorkscrewThread functions.
//-------------------------------------------------------------------------
CorkscrewThread::CorkscrewThread() {
}
CorkscrewThread::~CorkscrewThread() {
}
void CorkscrewThread::ThreadUnwind(
siginfo_t* siginfo, void* sigcontext, size_t num_ignore_frames) {
backtrace_frame_t cork_frames[MAX_BACKTRACE_FRAMES];
CorkscrewMap* map = static_cast<CorkscrewMap*>(GetMap());
ssize_t num_frames = unwind_backtrace_signal_arch(
siginfo, sigcontext, map->GetMapInfo(), cork_frames,
num_ignore_frames, MAX_BACKTRACE_FRAMES);
if (num_frames > 0) {
std::vector<backtrace_frame_data_t>* frames = GetFrames();
frames->resize(num_frames);
size_t i = 0;
for (std::vector<backtrace_frame_data_t>::iterator it = frames->begin();
it != frames->end(); ++it, ++i) {
it->num = i;
it->pc = cork_frames[i].absolute_pc;
it->sp = cork_frames[i].stack_top;
it->stack_size = cork_frames[i].stack_size;
it->map = NULL;
it->func_offset = 0;
}
}
}
//-------------------------------------------------------------------------
// CorkscrewPtrace functions.
//-------------------------------------------------------------------------
CorkscrewPtrace::CorkscrewPtrace() : ptrace_context_(NULL) {
}
CorkscrewPtrace::~CorkscrewPtrace() {
if (ptrace_context_) {
free_ptrace_context(ptrace_context_);
ptrace_context_ = NULL;
}
}
bool CorkscrewPtrace::Unwind(size_t num_ignore_frames) {
ptrace_context_ = load_ptrace_context(Tid());
backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
ssize_t num_frames = unwind_backtrace_ptrace(
Tid(), ptrace_context_, frames, num_ignore_frames, MAX_BACKTRACE_FRAMES);
return GenerateFrameData(frames, num_frames);
}
std::string CorkscrewPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
// Get information about a different process.
const map_info_t* map_info;
const symbol_t* symbol;
find_symbol_ptrace(ptrace_context_, pc, &map_info, &symbol);
char* symbol_name = NULL;
if (symbol) {
if (map_info) {
*offset = pc - map_info->start - symbol->start;
}
symbol_name = symbol->name;
return symbol_name;
}
return "";
}
//-------------------------------------------------------------------------
// C++ object creation functions.
//-------------------------------------------------------------------------
Backtrace* CreateCurrentObj(BacktraceMap* map) {
return new BacktraceCurrent(new CorkscrewCurrent(), map);
}
Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, BacktraceMap* map) {
return new BacktracePtrace(new CorkscrewPtrace(), pid, tid, map);
}
Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) {
CorkscrewThread* thread_obj = new CorkscrewThread();
return new BacktraceThread(thread_obj, thread_obj, tid, map);
}
//-------------------------------------------------------------------------
// BacktraceMap create function.
//-------------------------------------------------------------------------
BacktraceMap* BacktraceMap::Create(pid_t pid) {
BacktraceMap* map = new CorkscrewMap(pid);
if (!map->Build()) {
delete map;
return NULL;
}
return map;
}
|