blob: 306d2acf0e1af4c1893bda3ada0bd152a3c6af5d [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 Ferris2c43cff2015-03-26 19:18:36 -070017#include <stdint.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070018#include <sys/types.h>
Christopher Ferrisc58287d2014-05-09 16:59:06 -070019#include <ucontext.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070020
Christopher Ferris17e91d42013-10-21 13:30:52 -070021#include <libunwind.h>
22#include <libunwind-ptrace.h>
23
Christopher Ferris2c43cff2015-03-26 19:18:36 -070024#include <backtrace/Backtrace.h>
25#include <backtrace/BacktraceMap.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
Christopher Ferris2c43cff2015-03-26 19:18:36 -070031UnwindPtrace::UnwindPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
32 : BacktracePtrace(pid, tid, map), addr_space_(nullptr), upt_info_(nullptr) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070033}
34
35UnwindPtrace::~UnwindPtrace() {
36 if (upt_info_) {
37 _UPT_destroy(upt_info_);
Christopher Ferris2c43cff2015-03-26 19:18:36 -070038 upt_info_ = nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -070039 }
40 if (addr_space_) {
Christopher Ferrisdf290612014-01-22 19:21:07 -080041 // Remove the map from the address space before destroying it.
42 // It will be freed in the UnwindMap destructor.
Christopher Ferris2c43cff2015-03-26 19:18:36 -070043 unw_map_set(addr_space_, nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -080044
Christopher Ferris17e91d42013-10-21 13:30:52 -070045 unw_destroy_addr_space(addr_space_);
Christopher Ferris2c43cff2015-03-26 19:18:36 -070046 addr_space_ = nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -070047 }
48}
49
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070050bool UnwindPtrace::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
Christopher Ferris30c942c2015-05-14 15:39:52 -070051 if (GetMap() == nullptr) {
52 // Without a map object, we can't do anything.
Christopher Ferrisc463ba42016-03-09 14:35:54 -080053 error_ = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
Christopher Ferris30c942c2015-05-14 15:39:52 -070054 return false;
55 }
56
Christopher Ferrisc463ba42016-03-09 14:35:54 -080057 error_ = BACKTRACE_UNWIND_NO_ERROR;
58
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070059 if (ucontext) {
60 BACK_LOGW("Unwinding from a specified context not supported yet.");
Christopher Ferrisc463ba42016-03-09 14:35:54 -080061 error_ = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION;
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070062 return false;
63 }
64
Christopher Ferris17e91d42013-10-21 13:30:52 -070065 addr_space_ = unw_create_addr_space(&_UPT_accessors, 0);
66 if (!addr_space_) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070067 BACK_LOGW("unw_create_addr_space failed.");
Christopher Ferrisc463ba42016-03-09 14:35:54 -080068 error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
Christopher Ferris17e91d42013-10-21 13:30:52 -070069 return false;
70 }
71
Christopher Ferrisdf290612014-01-22 19:21:07 -080072 UnwindMap* map = static_cast<UnwindMap*>(GetMap());
73 unw_map_set(addr_space_, map->GetMapCursor());
74
75 upt_info_ = reinterpret_cast<struct UPT_info*>(_UPT_create(Tid()));
Christopher Ferris17e91d42013-10-21 13:30:52 -070076 if (!upt_info_) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070077 BACK_LOGW("Failed to create upt info.");
Christopher Ferrisc463ba42016-03-09 14:35:54 -080078 error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
Christopher Ferris17e91d42013-10-21 13:30:52 -070079 return false;
80 }
81
Christopher Ferris17e91d42013-10-21 13:30:52 -070082 unw_cursor_t cursor;
83 int ret = unw_init_remote(&cursor, addr_space_, upt_info_);
84 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070085 BACK_LOGW("unw_init_remote failed %d", ret);
Christopher Ferrisc463ba42016-03-09 14:35:54 -080086 error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
Christopher Ferris17e91d42013-10-21 13:30:52 -070087 return false;
88 }
89
Christopher Ferris46756822014-01-14 20:16:30 -080090 size_t num_frames = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070091 do {
92 unw_word_t pc;
93 ret = unw_get_reg(&cursor, UNW_REG_IP, &pc);
94 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070095 BACK_LOGW("Failed to read IP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070096 break;
97 }
98 unw_word_t sp;
99 ret = unw_get_reg(&cursor, UNW_REG_SP, &sp);
100 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700101 BACK_LOGW("Failed to read SP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700102 break;
103 }
104
105 if (num_ignore_frames == 0) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700106 frames_.resize(num_frames+1);
107 backtrace_frame_data_t* frame = &frames_.at(num_frames);
Christopher Ferris20303f82014-01-10 16:33:16 -0800108 frame->num = num_frames;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700109 frame->pc = static_cast<uintptr_t>(pc);
110 frame->sp = static_cast<uintptr_t>(sp);
111 frame->stack_size = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700112
113 if (num_frames > 0) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700114 backtrace_frame_data_t* prev = &frames_.at(num_frames-1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700115 prev->stack_size = frame->sp - prev->sp;
116 }
117
Christopher Ferrisdf290612014-01-22 19:21:07 -0800118 frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700119
Christopher Ferris12385e32015-02-06 13:22:01 -0800120 FillInMap(frame->pc, &frame->map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700121
Christopher Ferris46756822014-01-14 20:16:30 -0800122 num_frames++;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700123 } else {
124 num_ignore_frames--;
125 }
126 ret = unw_step (&cursor);
Christopher Ferris46756822014-01-14 20:16:30 -0800127 } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700128
129 return true;
130}
131
132std::string UnwindPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
133 *offset = 0;
134 char buf[512];
135 unw_word_t value;
136 if (unw_get_proc_name_by_ip(addr_space_, pc, buf, sizeof(buf), &value,
137 upt_info_) >= 0 && buf[0] != '\0') {
138 *offset = static_cast<uintptr_t>(value);
139 return buf;
140 }
141 return "";
142}