blob: e0b72d846952b7e84e02d7f47aa6d70bffeae170 [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#define UNW_LOCAL_ONLY
25#include <libunwind.h>
26#include <libunwind-ptrace.h>
27
28#include "common.h"
29#include "demangle.h"
30
31static bool local_get_frames(backtrace_t* backtrace) {
32 unw_context_t* context = (unw_context_t*)backtrace->private_data;
33 unw_cursor_t cursor;
34
35 int ret = unw_getcontext(context);
36 if (ret < 0) {
37 ALOGW("local_get_frames: unw_getcontext failed %d\n", ret);
38 return false;
39 }
40
41 ret = unw_init_local(&cursor, context);
42 if (ret < 0) {
43 ALOGW("local_get_frames: unw_init_local failed %d\n", ret);
44 return false;
45 }
46
47 backtrace_frame_data_t* frame;
48 bool returnValue = true;
49 backtrace->num_frames = 0;
50 uintptr_t map_start;
51 do {
52 frame = &backtrace->frames[backtrace->num_frames];
53 frame->stack_size = 0;
54 frame->map_name = NULL;
55 frame->map_offset = 0;
56 frame->proc_name = NULL;
57 frame->proc_offset = 0;
58
59 ret = unw_get_reg(&cursor, UNW_REG_IP, &frame->pc);
60 if (ret < 0) {
61 ALOGW("get_frames: Failed to read IP %d\n", ret);
62 returnValue = false;
63 break;
64 }
65 ret = unw_get_reg(&cursor, UNW_REG_SP, &frame->sp);
66 if (ret < 0) {
67 ALOGW("get_frames: Failed to read IP %d\n", ret);
68 returnValue = false;
69 break;
70 }
71 if (backtrace->num_frames) {
72 backtrace_frame_data_t* prev = &backtrace->frames[backtrace->num_frames-1];
73 prev->stack_size = frame->sp - prev->sp;
74 }
75
76 frame->proc_name = backtrace_get_proc_name(backtrace, frame->pc, &frame->proc_offset);
77
78 frame->map_name = backtrace_get_map_info(backtrace, frame->pc, &map_start);
79 if (frame->map_name) {
80 frame->map_offset = frame->pc - map_start;
81 }
82
83 backtrace->num_frames++;
84 ret = unw_step (&cursor);
85 } while (ret > 0 && backtrace->num_frames < MAX_BACKTRACE_FRAMES);
86
87 return returnValue;
88}
89
90bool local_get_data(backtrace_t* backtrace) {
91 unw_context_t *context = (unw_context_t*)malloc(sizeof(unw_context_t));
92 backtrace->private_data = context;
93
94 if (!local_get_frames(backtrace)) {
95 backtrace_free_data(backtrace);
96 return false;
97 }
98
99 return true;
100}
101
102void local_free_data(backtrace_t* backtrace) {
103 if (backtrace->private_data) {
104 free(backtrace->private_data);
105 backtrace->private_data = NULL;
106 }
107}
108
109char* local_get_proc_name(const backtrace_t* backtrace, uintptr_t pc,
110 uintptr_t* offset) {
111 unw_context_t* context = (unw_context_t*)backtrace->private_data;
112 char buf[512];
113
114 if (unw_get_proc_name_by_ip(unw_local_addr_space, pc, buf, sizeof(buf),
115 offset, context) >= 0 && buf[0] != '\0') {
116 char* symbol = demangle_symbol_name(buf);
117 if (!symbol) {
118 symbol = strdup(buf);
119 }
120 return symbol;
121 }
122 return NULL;
123}