blob: 5ca7e6037634dd89b2ddb079f300c1531696b8a8 [file] [log] [blame]
Christopher Ferris17e91d42013-10-21 13:30:52 -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
Christopher Ferris46756822014-01-14 20:16:30 -080017#include <backtrace/Backtrace.h>
18#include <backtrace/BacktraceMap.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070019
20#include <sys/types.h>
21#include <string.h>
22
Christopher Ferris17e91d42013-10-21 13:30:52 -070023#include <libunwind.h>
24#include <libunwind-ptrace.h>
25
Christopher Ferrise2960912014-03-07 19:42:19 -080026#include "BacktraceLog.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080027#include "UnwindMap.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070028#include "UnwindPtrace.h"
29
30UnwindPtrace::UnwindPtrace() : addr_space_(NULL), upt_info_(NULL) {
31}
32
33UnwindPtrace::~UnwindPtrace() {
34 if (upt_info_) {
35 _UPT_destroy(upt_info_);
36 upt_info_ = NULL;
37 }
38 if (addr_space_) {
Christopher Ferrisdf290612014-01-22 19:21:07 -080039 // Remove the map from the address space before destroying it.
40 // It will be freed in the UnwindMap destructor.
41 unw_map_set(addr_space_, NULL);
42
Christopher Ferris17e91d42013-10-21 13:30:52 -070043 unw_destroy_addr_space(addr_space_);
44 addr_space_ = NULL;
45 }
46}
47
48bool UnwindPtrace::Unwind(size_t num_ignore_frames) {
49 addr_space_ = unw_create_addr_space(&_UPT_accessors, 0);
50 if (!addr_space_) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070051 BACK_LOGW("unw_create_addr_space failed.");
Christopher Ferris17e91d42013-10-21 13:30:52 -070052 return false;
53 }
54
Christopher Ferrisdf290612014-01-22 19:21:07 -080055 UnwindMap* map = static_cast<UnwindMap*>(GetMap());
56 unw_map_set(addr_space_, map->GetMapCursor());
57
58 upt_info_ = reinterpret_cast<struct UPT_info*>(_UPT_create(Tid()));
Christopher Ferris17e91d42013-10-21 13:30:52 -070059 if (!upt_info_) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070060 BACK_LOGW("Failed to create upt info.");
Christopher Ferris17e91d42013-10-21 13:30:52 -070061 return false;
62 }
63
Christopher Ferris17e91d42013-10-21 13:30:52 -070064 unw_cursor_t cursor;
65 int ret = unw_init_remote(&cursor, addr_space_, upt_info_);
66 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070067 BACK_LOGW("unw_init_remote failed %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070068 return false;
69 }
70
Christopher Ferris46756822014-01-14 20:16:30 -080071 std::vector<backtrace_frame_data_t>* frames = GetFrames();
72 frames->reserve(MAX_BACKTRACE_FRAMES);
73 size_t num_frames = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070074 do {
75 unw_word_t pc;
76 ret = unw_get_reg(&cursor, UNW_REG_IP, &pc);
77 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070078 BACK_LOGW("Failed to read IP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070079 break;
80 }
81 unw_word_t sp;
82 ret = unw_get_reg(&cursor, UNW_REG_SP, &sp);
83 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070084 BACK_LOGW("Failed to read SP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070085 break;
86 }
87
88 if (num_ignore_frames == 0) {
Christopher Ferris46756822014-01-14 20:16:30 -080089 frames->resize(num_frames+1);
90 backtrace_frame_data_t* frame = &frames->at(num_frames);
Christopher Ferris20303f82014-01-10 16:33:16 -080091 frame->num = num_frames;
Christopher Ferris17e91d42013-10-21 13:30:52 -070092 frame->pc = static_cast<uintptr_t>(pc);
93 frame->sp = static_cast<uintptr_t>(sp);
94 frame->stack_size = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070095
96 if (num_frames > 0) {
Christopher Ferris46756822014-01-14 20:16:30 -080097 backtrace_frame_data_t* prev = &frames->at(num_frames-1);
Christopher Ferris17e91d42013-10-21 13:30:52 -070098 prev->stack_size = frame->sp - prev->sp;
99 }
100
Christopher Ferrisdf290612014-01-22 19:21:07 -0800101 frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700102
Christopher Ferrisdf290612014-01-22 19:21:07 -0800103 frame->map = FindMap(frame->pc);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700104
Christopher Ferris46756822014-01-14 20:16:30 -0800105 num_frames++;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700106 } else {
107 num_ignore_frames--;
108 }
109 ret = unw_step (&cursor);
Christopher Ferris46756822014-01-14 20:16:30 -0800110 } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700111
112 return true;
113}
114
115std::string UnwindPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
116 *offset = 0;
117 char buf[512];
118 unw_word_t value;
119 if (unw_get_proc_name_by_ip(addr_space_, pc, buf, sizeof(buf), &value,
120 upt_info_) >= 0 && buf[0] != '\0') {
121 *offset = static_cast<uintptr_t>(value);
122 return buf;
123 }
124 return "";
125}
126
127//-------------------------------------------------------------------------
128// C++ object creation function.
129//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -0800130Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, BacktraceMap* map) {
131 return new BacktracePtrace(new UnwindPtrace(), pid, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700132}