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