blob: ac1404602d6bb242d2a5740b9761148e1a4e84ec [file] [log] [blame]
Yabin Cui9e402bb2015-09-22 04:46:57 +00001/*
2 * Copyright (C) 2015 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#include "BacktraceOffline.h"
18
19extern "C" {
20#define UNW_REMOTE_ONLY
21#include <dwarf.h>
22}
23
24#include <stdint.h>
Yabin Cui2ad59db2015-12-08 18:43:00 -080025#include <stdio.h>
Yabin Cui9e402bb2015-09-22 04:46:57 +000026#include <string.h>
Yabin Cui2ad59db2015-12-08 18:43:00 -080027#include <sys/stat.h>
Yabin Cui9e402bb2015-09-22 04:46:57 +000028#include <sys/types.h>
29#include <ucontext.h>
30#include <unistd.h>
31
32#include <string>
33#include <vector>
34
35#include <backtrace/Backtrace.h>
36#include <backtrace/BacktraceMap.h>
37
38#pragma clang diagnostic push
39#pragma clang diagnostic ignored "-Wunused-parameter"
40
41#include <llvm/ADT/StringRef.h>
42#include <llvm/Object/Binary.h>
43#include <llvm/Object/ELFObjectFile.h>
44#include <llvm/Object/ObjectFile.h>
45
46#pragma clang diagnostic pop
47
48#include "BacktraceLog.h"
49
50void Space::Clear() {
51 start = 0;
52 end = 0;
53 data = nullptr;
54}
55
56size_t Space::Read(uint64_t addr, uint8_t* buffer, size_t size) {
57 if (addr >= start && addr < end) {
58 size_t read_size = std::min(size, static_cast<size_t>(end - addr));
59 memcpy(buffer, data + (addr - start), read_size);
60 return read_size;
61 }
62 return 0;
63}
64
65static int FindProcInfo(unw_addr_space_t addr_space, unw_word_t ip, unw_proc_info* proc_info,
66 int need_unwind_info, void* arg) {
67 BacktraceOffline* backtrace = reinterpret_cast<BacktraceOffline*>(arg);
68 bool result = backtrace->FindProcInfo(addr_space, ip, proc_info, need_unwind_info);
69 return result ? 0 : -UNW_EINVAL;
70}
71
72static void PutUnwindInfo(unw_addr_space_t, unw_proc_info_t*, void*) {
73}
74
75static int GetDynInfoListAddr(unw_addr_space_t, unw_word_t*, void*) {
76 return -UNW_ENOINFO;
77}
78
79static int AccessMem(unw_addr_space_t, unw_word_t addr, unw_word_t* value, int write, void* arg) {
80 if (write == 1) {
81 return -UNW_EINVAL;
82 }
83 BacktraceOffline* backtrace = reinterpret_cast<BacktraceOffline*>(arg);
84 *value = 0;
85 size_t read_size = backtrace->Read(addr, reinterpret_cast<uint8_t*>(value), sizeof(unw_word_t));
86 // Strictly we should check if read_size matches sizeof(unw_word_t), but it is possible in
87 // .eh_frame_hdr that the section can end at a position not aligned in sizeof(unw_word_t), and
88 // we should permit the read at the end of the section.
89 return (read_size > 0u ? 0 : -UNW_EINVAL);
90}
91
92static int AccessReg(unw_addr_space_t, unw_regnum_t unwind_reg, unw_word_t* value, int write,
93 void* arg) {
94 if (write == 1) {
95 return -UNW_EINVAL;
96 }
97 BacktraceOffline* backtrace = reinterpret_cast<BacktraceOffline*>(arg);
98 uint64_t reg_value;
99 bool result = backtrace->ReadReg(unwind_reg, &reg_value);
100 if (result) {
101 *value = static_cast<unw_word_t>(reg_value);
102 }
103 return result ? 0 : -UNW_EINVAL;
104}
105
106static int AccessFpReg(unw_addr_space_t, unw_regnum_t, unw_fpreg_t*, int, void*) {
107 return -UNW_EINVAL;
108}
109
110static int Resume(unw_addr_space_t, unw_cursor_t*, void*) {
111 return -UNW_EINVAL;
112}
113
114static int GetProcName(unw_addr_space_t, unw_word_t, char*, size_t, unw_word_t*, void*) {
115 return -UNW_EINVAL;
116}
117
118static unw_accessors_t accessors = {
119 .find_proc_info = FindProcInfo,
120 .put_unwind_info = PutUnwindInfo,
121 .get_dyn_info_list_addr = GetDynInfoListAddr,
122 .access_mem = AccessMem,
123 .access_reg = AccessReg,
124 .access_fpreg = AccessFpReg,
125 .resume = Resume,
126 .get_proc_name = GetProcName,
127};
128
129bool BacktraceOffline::Unwind(size_t num_ignore_frames, ucontext_t* context) {
130 if (context == nullptr) {
131 BACK_LOGW("The context is needed for offline backtracing.");
Christopher Ferris206a3b92016-03-09 14:35:54 -0800132 error_ = BACKTRACE_UNWIND_ERROR_NO_CONTEXT;
Yabin Cui9e402bb2015-09-22 04:46:57 +0000133 return false;
134 }
135 context_ = context;
Christopher Ferris206a3b92016-03-09 14:35:54 -0800136 error_ = BACKTRACE_UNWIND_NO_ERROR;
Yabin Cui9e402bb2015-09-22 04:46:57 +0000137
138 unw_addr_space_t addr_space = unw_create_addr_space(&accessors, 0);
139 unw_cursor_t cursor;
140 int ret = unw_init_remote(&cursor, addr_space, this);
141 if (ret != 0) {
142 BACK_LOGW("unw_init_remote failed %d", ret);
143 unw_destroy_addr_space(addr_space);
Christopher Ferris206a3b92016-03-09 14:35:54 -0800144 error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
Yabin Cui9e402bb2015-09-22 04:46:57 +0000145 return false;
146 }
147 size_t num_frames = 0;
148 do {
149 unw_word_t pc;
150 ret = unw_get_reg(&cursor, UNW_REG_IP, &pc);
151 if (ret < 0) {
152 BACK_LOGW("Failed to read IP %d", ret);
153 break;
154 }
155 unw_word_t sp;
156 ret = unw_get_reg(&cursor, UNW_REG_SP, &sp);
157 if (ret < 0) {
158 BACK_LOGW("Failed to read SP %d", ret);
159 break;
160 }
161
162 if (num_ignore_frames == 0) {
163 frames_.resize(num_frames + 1);
164 backtrace_frame_data_t* frame = &frames_[num_frames];
165 frame->num = num_frames;
166 frame->pc = static_cast<uintptr_t>(pc);
167 frame->sp = static_cast<uintptr_t>(sp);
168 frame->stack_size = 0;
169
170 if (num_frames > 0) {
171 backtrace_frame_data_t* prev = &frames_[num_frames - 1];
172 prev->stack_size = frame->sp - prev->sp;
173 }
174 frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
175 FillInMap(frame->pc, &frame->map);
176 num_frames++;
177 } else {
178 num_ignore_frames--;
179 }
180 ret = unw_step(&cursor);
181 } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
182
183 unw_destroy_addr_space(addr_space);
184 context_ = nullptr;
185 return true;
186}
187
188bool BacktraceOffline::ReadWord(uintptr_t ptr, word_t* out_value) {
189 size_t bytes_read = Read(ptr, reinterpret_cast<uint8_t*>(out_value), sizeof(word_t));
190 return bytes_read == sizeof(word_t);
191}
192
193size_t BacktraceOffline::Read(uintptr_t addr, uint8_t* buffer, size_t bytes) {
194 // Normally, libunwind needs stack information and call frame information to do remote unwinding.
195 // If call frame information is stored in .debug_frame, libunwind can read it from file
196 // by itself. If call frame information is stored in .eh_frame, we need to provide data in
197 // .eh_frame/.eh_frame_hdr sections.
198 // The order of readings below doesn't matter, as the spaces don't overlap with each other.
199 size_t read_size = eh_frame_hdr_space_.Read(addr, buffer, bytes);
200 if (read_size != 0) {
201 return read_size;
202 }
203 read_size = eh_frame_space_.Read(addr, buffer, bytes);
204 if (read_size != 0) {
205 return read_size;
206 }
207 read_size = stack_space_.Read(addr, buffer, bytes);
208 return read_size;
209}
210
211static bool FileOffsetToVaddr(
212 const std::vector<DebugFrameInfo::EhFrame::ProgramHeader>& program_headers,
213 uint64_t file_offset, uint64_t* vaddr) {
214 for (auto& header : program_headers) {
215 if (file_offset >= header.file_offset && file_offset < header.file_offset + header.file_size) {
216 // TODO: Consider load_bias?
217 *vaddr = file_offset - header.file_offset + header.vaddr;
218 return true;
219 }
220 }
221 return false;
222}
223
224bool BacktraceOffline::FindProcInfo(unw_addr_space_t addr_space, uint64_t ip,
225 unw_proc_info_t* proc_info, int need_unwind_info) {
226 backtrace_map_t map;
227 FillInMap(ip, &map);
228 if (!BacktraceMap::IsValid(map)) {
229 return false;
230 }
231 const std::string& filename = map.name;
232 DebugFrameInfo* debug_frame = GetDebugFrameInFile(filename);
233 if (debug_frame == nullptr) {
234 return false;
235 }
236 if (debug_frame->is_eh_frame) {
237 uint64_t ip_offset = ip - map.start + map.offset;
238 uint64_t ip_vaddr; // vaddr in the elf file.
239 bool result = FileOffsetToVaddr(debug_frame->eh_frame.program_headers, ip_offset, &ip_vaddr);
240 if (!result) {
241 return false;
242 }
243 // Calculate the addresses where .eh_frame_hdr and .eh_frame stay when the process was running.
244 eh_frame_hdr_space_.start = (ip - ip_vaddr) + debug_frame->eh_frame.eh_frame_hdr_vaddr;
245 eh_frame_hdr_space_.end =
246 eh_frame_hdr_space_.start + debug_frame->eh_frame.eh_frame_hdr_data.size();
247 eh_frame_hdr_space_.data = debug_frame->eh_frame.eh_frame_hdr_data.data();
248
249 eh_frame_space_.start = (ip - ip_vaddr) + debug_frame->eh_frame.eh_frame_vaddr;
250 eh_frame_space_.end = eh_frame_space_.start + debug_frame->eh_frame.eh_frame_data.size();
251 eh_frame_space_.data = debug_frame->eh_frame.eh_frame_data.data();
252
253 unw_dyn_info di;
254 memset(&di, '\0', sizeof(di));
255 di.start_ip = map.start;
256 di.end_ip = map.end;
257 di.format = UNW_INFO_FORMAT_REMOTE_TABLE;
258 di.u.rti.name_ptr = 0;
259 di.u.rti.segbase = eh_frame_hdr_space_.start;
260 di.u.rti.table_data =
261 eh_frame_hdr_space_.start + debug_frame->eh_frame.fde_table_offset_in_eh_frame_hdr;
262 di.u.rti.table_len = (eh_frame_hdr_space_.end - di.u.rti.table_data) / sizeof(unw_word_t);
263 int ret = dwarf_search_unwind_table(addr_space, ip, &di, proc_info, need_unwind_info, this);
264 return ret == 0;
265 }
266
267 eh_frame_hdr_space_.Clear();
268 eh_frame_space_.Clear();
269 unw_dyn_info_t di;
270 unw_word_t segbase = map.start - map.offset;
271 int found = dwarf_find_debug_frame(0, &di, ip, segbase, filename.c_str(), map.start, map.end);
272 if (found == 1) {
273 int ret = dwarf_search_unwind_table(addr_space, ip, &di, proc_info, need_unwind_info, this);
274 return ret == 0;
275 }
276 return false;
277}
278
279bool BacktraceOffline::ReadReg(size_t reg, uint64_t* value) {
280 bool result = true;
281#if defined(__arm__)
282 switch (reg) {
283 case UNW_ARM_R0:
284 *value = context_->uc_mcontext.arm_r0;
285 break;
286 case UNW_ARM_R1:
287 *value = context_->uc_mcontext.arm_r1;
288 break;
289 case UNW_ARM_R2:
290 *value = context_->uc_mcontext.arm_r2;
291 break;
292 case UNW_ARM_R3:
293 *value = context_->uc_mcontext.arm_r3;
294 break;
295 case UNW_ARM_R4:
296 *value = context_->uc_mcontext.arm_r4;
297 break;
298 case UNW_ARM_R5:
299 *value = context_->uc_mcontext.arm_r5;
300 break;
301 case UNW_ARM_R6:
302 *value = context_->uc_mcontext.arm_r6;
303 break;
304 case UNW_ARM_R7:
305 *value = context_->uc_mcontext.arm_r7;
306 break;
307 case UNW_ARM_R8:
308 *value = context_->uc_mcontext.arm_r8;
309 break;
310 case UNW_ARM_R9:
311 *value = context_->uc_mcontext.arm_r9;
312 break;
313 case UNW_ARM_R10:
314 *value = context_->uc_mcontext.arm_r10;
315 break;
316 case UNW_ARM_R11:
317 *value = context_->uc_mcontext.arm_fp;
318 break;
319 case UNW_ARM_R12:
320 *value = context_->uc_mcontext.arm_ip;
321 break;
322 case UNW_ARM_R13:
323 *value = context_->uc_mcontext.arm_sp;
324 break;
325 case UNW_ARM_R14:
326 *value = context_->uc_mcontext.arm_lr;
327 break;
328 case UNW_ARM_R15:
329 *value = context_->uc_mcontext.arm_pc;
330 break;
331 default:
332 result = false;
333 }
334#elif defined(__aarch64__)
335 if (reg <= UNW_AARCH64_PC) {
336 *value = context_->uc_mcontext.regs[reg];
337 } else {
338 result = false;
339 }
340#elif defined(__x86_64__)
341 switch (reg) {
342 case UNW_X86_64_R8:
343 *value = context_->uc_mcontext.gregs[REG_R8];
344 break;
345 case UNW_X86_64_R9:
346 *value = context_->uc_mcontext.gregs[REG_R9];
347 break;
348 case UNW_X86_64_R10:
349 *value = context_->uc_mcontext.gregs[REG_R10];
350 break;
351 case UNW_X86_64_R11:
352 *value = context_->uc_mcontext.gregs[REG_R11];
353 break;
354 case UNW_X86_64_R12:
355 *value = context_->uc_mcontext.gregs[REG_R12];
356 break;
357 case UNW_X86_64_R13:
358 *value = context_->uc_mcontext.gregs[REG_R13];
359 break;
360 case UNW_X86_64_R14:
361 *value = context_->uc_mcontext.gregs[REG_R14];
362 break;
363 case UNW_X86_64_R15:
364 *value = context_->uc_mcontext.gregs[REG_R15];
365 break;
366 case UNW_X86_64_RDI:
367 *value = context_->uc_mcontext.gregs[REG_RDI];
368 break;
369 case UNW_X86_64_RSI:
370 *value = context_->uc_mcontext.gregs[REG_RSI];
371 break;
372 case UNW_X86_64_RBP:
373 *value = context_->uc_mcontext.gregs[REG_RBP];
374 break;
375 case UNW_X86_64_RBX:
376 *value = context_->uc_mcontext.gregs[REG_RBX];
377 break;
378 case UNW_X86_64_RDX:
379 *value = context_->uc_mcontext.gregs[REG_RDX];
380 break;
381 case UNW_X86_64_RAX:
382 *value = context_->uc_mcontext.gregs[REG_RAX];
383 break;
384 case UNW_X86_64_RCX:
385 *value = context_->uc_mcontext.gregs[REG_RCX];
386 break;
387 case UNW_X86_64_RSP:
388 *value = context_->uc_mcontext.gregs[REG_RSP];
389 break;
390 case UNW_X86_64_RIP:
391 *value = context_->uc_mcontext.gregs[REG_RIP];
392 break;
393 default:
394 result = false;
395 }
396#elif defined(__i386__)
397 switch (reg) {
398 case UNW_X86_GS:
399 *value = context_->uc_mcontext.gregs[REG_GS];
400 break;
401 case UNW_X86_FS:
402 *value = context_->uc_mcontext.gregs[REG_FS];
403 break;
404 case UNW_X86_ES:
405 *value = context_->uc_mcontext.gregs[REG_ES];
406 break;
407 case UNW_X86_DS:
408 *value = context_->uc_mcontext.gregs[REG_DS];
409 break;
410 case UNW_X86_EAX:
411 *value = context_->uc_mcontext.gregs[REG_EAX];
412 break;
413 case UNW_X86_EBX:
414 *value = context_->uc_mcontext.gregs[REG_EBX];
415 break;
416 case UNW_X86_ECX:
417 *value = context_->uc_mcontext.gregs[REG_ECX];
418 break;
419 case UNW_X86_EDX:
420 *value = context_->uc_mcontext.gregs[REG_EDX];
421 break;
422 case UNW_X86_ESI:
423 *value = context_->uc_mcontext.gregs[REG_ESI];
424 break;
425 case UNW_X86_EDI:
426 *value = context_->uc_mcontext.gregs[REG_EDI];
427 break;
428 case UNW_X86_EBP:
429 *value = context_->uc_mcontext.gregs[REG_EBP];
430 break;
431 case UNW_X86_EIP:
432 *value = context_->uc_mcontext.gregs[REG_EIP];
433 break;
434 case UNW_X86_ESP:
435 *value = context_->uc_mcontext.gregs[REG_ESP];
436 break;
437 case UNW_X86_TRAPNO:
438 *value = context_->uc_mcontext.gregs[REG_TRAPNO];
439 break;
440 case UNW_X86_CS:
441 *value = context_->uc_mcontext.gregs[REG_CS];
442 break;
443 case UNW_X86_EFLAGS:
444 *value = context_->uc_mcontext.gregs[REG_EFL];
445 break;
446 case UNW_X86_SS:
447 *value = context_->uc_mcontext.gregs[REG_SS];
448 break;
449 default:
450 result = false;
451 }
452#endif
453 return result;
454}
455
456std::string BacktraceOffline::GetFunctionNameRaw(uintptr_t, uintptr_t* offset) {
457 // We don't have enough information to support this. And it is expensive.
458 *offset = 0;
459 return "";
460}
461
462std::unordered_map<std::string, std::unique_ptr<DebugFrameInfo>> BacktraceOffline::debug_frames_;
463std::unordered_set<std::string> BacktraceOffline::debug_frame_missing_files_;
464
465static DebugFrameInfo* ReadDebugFrameFromFile(const std::string& filename);
466
467DebugFrameInfo* BacktraceOffline::GetDebugFrameInFile(const std::string& filename) {
468 if (cache_file_) {
469 auto it = debug_frames_.find(filename);
470 if (it != debug_frames_.end()) {
471 return it->second.get();
472 }
473 if (debug_frame_missing_files_.find(filename) != debug_frame_missing_files_.end()) {
474 return nullptr;
475 }
476 }
477 DebugFrameInfo* debug_frame = ReadDebugFrameFromFile(filename);
478 if (cache_file_) {
479 if (debug_frame != nullptr) {
480 debug_frames_.emplace(filename, std::unique_ptr<DebugFrameInfo>(debug_frame));
481 } else {
482 debug_frame_missing_files_.insert(filename);
483 }
484 } else {
485 if (last_debug_frame_ != nullptr) {
486 delete last_debug_frame_;
487 }
488 last_debug_frame_ = debug_frame;
489 }
490 return debug_frame;
491}
492
493static bool OmitEncodedValue(uint8_t encode, const uint8_t*& p) {
494 if (encode == DW_EH_PE_omit) {
495 return 0;
496 }
497 uint8_t format = encode & 0x0f;
498 switch (format) {
499 case DW_EH_PE_ptr:
500 p += sizeof(unw_word_t);
501 break;
502 case DW_EH_PE_uleb128:
503 case DW_EH_PE_sleb128:
504 while ((*p & 0x80) != 0) {
505 ++p;
506 }
507 ++p;
508 break;
509 case DW_EH_PE_udata2:
510 case DW_EH_PE_sdata2:
511 p += 2;
512 break;
513 case DW_EH_PE_udata4:
514 case DW_EH_PE_sdata4:
515 p += 4;
516 break;
517 case DW_EH_PE_udata8:
518 case DW_EH_PE_sdata8:
519 p += 8;
520 break;
521 default:
522 return false;
523 }
524 return true;
525}
526
527static bool GetFdeTableOffsetInEhFrameHdr(const std::vector<uint8_t>& data,
528 uint64_t* table_offset_in_eh_frame_hdr) {
529 const uint8_t* p = data.data();
530 const uint8_t* end = p + data.size();
531 if (p + 4 > end) {
532 return false;
533 }
534 uint8_t version = *p++;
535 if (version != 1) {
536 return false;
537 }
538 uint8_t eh_frame_ptr_encode = *p++;
539 uint8_t fde_count_encode = *p++;
540 uint8_t fde_table_encode = *p++;
541
542 if (fde_table_encode != (DW_EH_PE_datarel | DW_EH_PE_sdata4)) {
543 return false;
544 }
545
546 if (!OmitEncodedValue(eh_frame_ptr_encode, p) || !OmitEncodedValue(fde_count_encode, p)) {
547 return false;
548 }
549 if (p >= end) {
550 return false;
551 }
552 *table_offset_in_eh_frame_hdr = p - data.data();
553 return true;
554}
555
556using ProgramHeader = DebugFrameInfo::EhFrame::ProgramHeader;
557
558template <class ELFT>
559DebugFrameInfo* ReadDebugFrameFromELFFile(const llvm::object::ELFFile<ELFT>* elf) {
560 bool has_eh_frame_hdr = false;
561 uint64_t eh_frame_hdr_vaddr = 0;
562 std::vector<uint8_t> eh_frame_hdr_data;
563 bool has_eh_frame = false;
564 uint64_t eh_frame_vaddr = 0;
565 std::vector<uint8_t> eh_frame_data;
566
567 for (auto it = elf->begin_sections(); it != elf->end_sections(); ++it) {
568 llvm::ErrorOr<llvm::StringRef> name = elf->getSectionName(&*it);
569 if (name) {
570 if (name.get() == ".debug_frame") {
571 DebugFrameInfo* debug_frame = new DebugFrameInfo;
572 debug_frame->is_eh_frame = false;
573 return debug_frame;
574 }
575 if (name.get() == ".eh_frame_hdr") {
576 has_eh_frame_hdr = true;
577 eh_frame_hdr_vaddr = it->sh_addr;
578 llvm::ErrorOr<llvm::ArrayRef<uint8_t>> data = elf->getSectionContents(&*it);
579 if (data) {
580 eh_frame_hdr_data.insert(eh_frame_hdr_data.begin(), data->data(),
581 data->data() + data->size());
582 } else {
583 return nullptr;
584 }
585 } else if (name.get() == ".eh_frame") {
586 has_eh_frame = true;
587 eh_frame_vaddr = it->sh_addr;
588 llvm::ErrorOr<llvm::ArrayRef<uint8_t>> data = elf->getSectionContents(&*it);
589 if (data) {
590 eh_frame_data.insert(eh_frame_data.begin(), data->data(), data->data() + data->size());
591 } else {
592 return nullptr;
593 }
594 }
595 }
596 }
597 if (!(has_eh_frame_hdr && has_eh_frame)) {
598 return nullptr;
599 }
600 uint64_t fde_table_offset;
601 if (!GetFdeTableOffsetInEhFrameHdr(eh_frame_hdr_data, &fde_table_offset)) {
602 return nullptr;
603 }
604
605 std::vector<ProgramHeader> program_headers;
606 for (auto it = elf->begin_program_headers(); it != elf->end_program_headers(); ++it) {
607 ProgramHeader header;
608 header.vaddr = it->p_vaddr;
609 header.file_offset = it->p_offset;
610 header.file_size = it->p_filesz;
611 program_headers.push_back(header);
612 }
613 DebugFrameInfo* debug_frame = new DebugFrameInfo;
614 debug_frame->is_eh_frame = true;
615 debug_frame->eh_frame.eh_frame_hdr_vaddr = eh_frame_hdr_vaddr;
616 debug_frame->eh_frame.eh_frame_vaddr = eh_frame_vaddr;
617 debug_frame->eh_frame.fde_table_offset_in_eh_frame_hdr = fde_table_offset;
618 debug_frame->eh_frame.eh_frame_hdr_data = std::move(eh_frame_hdr_data);
619 debug_frame->eh_frame.eh_frame_data = std::move(eh_frame_data);
620 debug_frame->eh_frame.program_headers = program_headers;
621 return debug_frame;
622}
623
Yabin Cui2ad59db2015-12-08 18:43:00 -0800624static bool IsValidElfPath(const std::string& filename) {
625 static const char elf_magic[] = {0x7f, 'E', 'L', 'F'};
626
627 struct stat st;
628 if (stat(filename.c_str(), &st) != 0 || !S_ISREG(st.st_mode)) {
629 return false;
630 }
631 FILE* fp = fopen(filename.c_str(), "reb");
632 if (fp == nullptr) {
633 return false;
634 }
635 char buf[4];
636 if (fread(buf, 4, 1, fp) != 1) {
637 fclose(fp);
638 return false;
639 }
640 fclose(fp);
641 return memcmp(buf, elf_magic, 4) == 0;
642}
643
Yabin Cui9e402bb2015-09-22 04:46:57 +0000644static DebugFrameInfo* ReadDebugFrameFromFile(const std::string& filename) {
Yabin Cui2ad59db2015-12-08 18:43:00 -0800645 if (!IsValidElfPath(filename)) {
646 return nullptr;
647 }
Yabin Cui9e402bb2015-09-22 04:46:57 +0000648 auto owning_binary = llvm::object::createBinary(llvm::StringRef(filename));
649 if (owning_binary.getError()) {
650 return nullptr;
651 }
652 llvm::object::Binary* binary = owning_binary.get().getBinary();
653 auto obj = llvm::dyn_cast<llvm::object::ObjectFile>(binary);
654 if (obj == nullptr) {
655 return nullptr;
656 }
657 if (auto elf = llvm::dyn_cast<llvm::object::ELF32LEObjectFile>(obj)) {
658 return ReadDebugFrameFromELFFile(elf->getELFFile());
659 }
660 if (auto elf = llvm::dyn_cast<llvm::object::ELF64LEObjectFile>(obj)) {
661 return ReadDebugFrameFromELFFile(elf->getELFFile());
662 }
663 return nullptr;
664}
Christopher Ferris85402162016-01-25 16:17:48 -0800665
666Backtrace* Backtrace::CreateOffline(pid_t pid, pid_t tid, BacktraceMap* map,
667 const backtrace_stackinfo_t& stack, bool cache_file) {
668 return new BacktraceOffline(pid, tid, map, stack, cache_file);
669}