blob: 7ba8775b134a05e0e1588330ff5d9ea3bc6a1294 [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>
Christopher Ferrisc58287d2014-05-09 16:59:06 -070022#include <ucontext.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070023
Christopher Ferris17e91d42013-10-21 13:30:52 -070024#include <libunwind.h>
25#include <libunwind-ptrace.h>
26
Christopher Ferrise2960912014-03-07 19:42:19 -080027#include "BacktraceLog.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080028#include "UnwindMap.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070029#include "UnwindPtrace.h"
30
31UnwindPtrace::UnwindPtrace() : addr_space_(NULL), upt_info_(NULL) {
32}
33
34UnwindPtrace::~UnwindPtrace() {
35 if (upt_info_) {
36 _UPT_destroy(upt_info_);
37 upt_info_ = NULL;
38 }
39 if (addr_space_) {
Christopher Ferrisdf290612014-01-22 19:21:07 -080040 // Remove the map from the address space before destroying it.
41 // It will be freed in the UnwindMap destructor.
42 unw_map_set(addr_space_, NULL);
43
Christopher Ferris17e91d42013-10-21 13:30:52 -070044 unw_destroy_addr_space(addr_space_);
45 addr_space_ = NULL;
46 }
47}
48
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070049bool UnwindPtrace::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
50 if (ucontext) {
51 BACK_LOGW("Unwinding from a specified context not supported yet.");
52 return false;
53 }
54
Christopher Ferris17e91d42013-10-21 13:30:52 -070055 addr_space_ = unw_create_addr_space(&_UPT_accessors, 0);
56 if (!addr_space_) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070057 BACK_LOGW("unw_create_addr_space failed.");
Christopher Ferris17e91d42013-10-21 13:30:52 -070058 return false;
59 }
60
Christopher Ferrisdf290612014-01-22 19:21:07 -080061 UnwindMap* map = static_cast<UnwindMap*>(GetMap());
62 unw_map_set(addr_space_, map->GetMapCursor());
63
64 upt_info_ = reinterpret_cast<struct UPT_info*>(_UPT_create(Tid()));
Christopher Ferris17e91d42013-10-21 13:30:52 -070065 if (!upt_info_) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070066 BACK_LOGW("Failed to create upt info.");
Christopher Ferris17e91d42013-10-21 13:30:52 -070067 return false;
68 }
69
Christopher Ferris17e91d42013-10-21 13:30:52 -070070 unw_cursor_t cursor;
71 int ret = unw_init_remote(&cursor, addr_space_, upt_info_);
72 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070073 BACK_LOGW("unw_init_remote failed %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070074 return false;
75 }
76
Christopher Ferris46756822014-01-14 20:16:30 -080077 std::vector<backtrace_frame_data_t>* frames = GetFrames();
78 frames->reserve(MAX_BACKTRACE_FRAMES);
79 size_t num_frames = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070080 do {
81 unw_word_t pc;
82 ret = unw_get_reg(&cursor, UNW_REG_IP, &pc);
83 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070084 BACK_LOGW("Failed to read IP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070085 break;
86 }
87 unw_word_t sp;
88 ret = unw_get_reg(&cursor, UNW_REG_SP, &sp);
89 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070090 BACK_LOGW("Failed to read SP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070091 break;
92 }
93
94 if (num_ignore_frames == 0) {
Christopher Ferris46756822014-01-14 20:16:30 -080095 frames->resize(num_frames+1);
96 backtrace_frame_data_t* frame = &frames->at(num_frames);
Christopher Ferris20303f82014-01-10 16:33:16 -080097 frame->num = num_frames;
Christopher Ferris17e91d42013-10-21 13:30:52 -070098 frame->pc = static_cast<uintptr_t>(pc);
99 frame->sp = static_cast<uintptr_t>(sp);
100 frame->stack_size = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700101
102 if (num_frames > 0) {
Christopher Ferris46756822014-01-14 20:16:30 -0800103 backtrace_frame_data_t* prev = &frames->at(num_frames-1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700104 prev->stack_size = frame->sp - prev->sp;
105 }
106
Christopher Ferrisdf290612014-01-22 19:21:07 -0800107 frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700108
Christopher Ferrisdf290612014-01-22 19:21:07 -0800109 frame->map = FindMap(frame->pc);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700110
Christopher Ferris46756822014-01-14 20:16:30 -0800111 num_frames++;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700112 } else {
113 num_ignore_frames--;
114 }
115 ret = unw_step (&cursor);
Christopher Ferris46756822014-01-14 20:16:30 -0800116 } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700117
118 return true;
119}
120
121std::string UnwindPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
122 *offset = 0;
123 char buf[512];
124 unw_word_t value;
125 if (unw_get_proc_name_by_ip(addr_space_, pc, buf, sizeof(buf), &value,
126 upt_info_) >= 0 && buf[0] != '\0') {
127 *offset = static_cast<uintptr_t>(value);
128 return buf;
129 }
130 return "";
131}
132
133//-------------------------------------------------------------------------
134// C++ object creation function.
135//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -0800136Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, BacktraceMap* map) {
137 return new BacktracePtrace(new UnwindPtrace(), pid, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700138}