blob: 899409aea866934ec8aa8c72217a5a47bf58a408 [file] [log] [blame]
Christopher Ferris7fb22872013-09-27 12:43:15 -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#define LOG_TAG "libbacktrace"
18
19#include <string.h>
20
21#include <cutils/log.h>
22#include <backtrace/backtrace.h>
23
24#include <corkscrew/backtrace.h>
25
26#define __USE_GNU
27#include <dlfcn.h>
28
29#include "common.h"
30#include "demangle.h"
31
32bool backtrace_get_data(backtrace_t* backtrace, pid_t tid) {
33 backtrace->num_frames = 0;
34 backtrace->tid = tid;
35 backtrace->private_data = NULL;
36 backtrace->map_info_list = backtrace_create_map_info_list(tid);
37
38 backtrace_frame_t frames[MAX_BACKTRACE_FRAMES];
39 ssize_t num_frames;
40 if (tid < 0) {
41 // Get data for the current thread.
42 num_frames = unwind_backtrace(frames, 0, MAX_BACKTRACE_FRAMES);
43 } else {
44 // Get data for a different thread.
45 ptrace_context_t* ptrace_context = load_ptrace_context(tid);
46 backtrace->private_data = ptrace_context;
47
48 num_frames = unwind_backtrace_ptrace(
49 tid, ptrace_context, frames, 0, MAX_BACKTRACE_FRAMES);
50 }
51 if (num_frames < 0) {
52 ALOGW("backtrace_get_data: unwind_backtrace_ptrace failed %d\n",
53 num_frames);
54 backtrace_free_data(backtrace);
55 return false;
56 }
57
58 backtrace->num_frames = num_frames;
59 backtrace_frame_data_t* frame;
60 uintptr_t map_start;
61 for (size_t i = 0; i < backtrace->num_frames; i++) {
62 frame = &backtrace->frames[i];
63 frame->pc = frames[i].absolute_pc;
64 frame->sp = frames[i].stack_top;
65 frame->stack_size = frames[i].stack_size;
66
67 frame->map_offset = 0;
68 frame->map_name = backtrace_get_map_info(backtrace, frame->pc, &map_start);
69 if (frame->map_name) {
70 frame->map_offset = frame->pc - map_start;
71 }
72
73 frame->proc_offset = 0;
74 frame->proc_name = backtrace_get_proc_name(backtrace, frame->pc, &frame->proc_offset);
75 }
76
77 return true;
78}
79
80void backtrace_free_data(backtrace_t* backtrace) {
81 free_frame_data(backtrace);
82
83 if (backtrace->map_info_list) {
84 backtrace_destroy_map_info_list(backtrace->map_info_list);
85 backtrace->map_info_list = NULL;
86 }
87
88 if (backtrace->private_data) {
89 ptrace_context_t* ptrace_context = (ptrace_context_t*)backtrace->private_data;
90 free_ptrace_context(ptrace_context);
91 backtrace->private_data = NULL;
92 }
93}
94
95char* backtrace_get_proc_name(const backtrace_t* backtrace, uintptr_t pc,
96 uintptr_t* offset) {
97 const char* symbol_name = NULL;
98 *offset = 0;
99 if (backtrace->tid < 0) {
100 // Get information about the current thread.
101 Dl_info info;
102 const backtrace_map_info_t* map_info;
103 map_info = backtrace_find_map_info(backtrace->map_info_list, pc);
104 if (map_info && dladdr((const void*)pc, &info) && info.dli_sname) {
105 *offset = pc - map_info->start - (uintptr_t)info.dli_saddr + (uintptr_t)info.dli_fbase;
106 symbol_name = info.dli_sname;
107 }
108 } else {
109 // Get information about a different thread.
110 ptrace_context_t* ptrace_context = (ptrace_context_t*)backtrace->private_data;
111 const map_info_t* map_info;
112 const symbol_t* symbol;
113 find_symbol_ptrace(ptrace_context, pc, &map_info, &symbol);
114 if (symbol) {
115 if (map_info) {
116 *offset = pc - map_info->start - symbol->start;
117 }
118 symbol_name = symbol->name;
119 }
120 }
121
122 char* name = NULL;
123 if (symbol_name) {
124 name = demangle_symbol_name(symbol_name);
125 if (!name) {
126 name = strdup(symbol_name);
127 }
128 }
129 return name;
130}