• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #include <stdint.h>
18 #include <sys/types.h>
19 #include <ucontext.h>
20 
21 #include <libunwind.h>
22 #include <libunwind-ptrace.h>
23 
24 #include <backtrace/Backtrace.h>
25 #include <backtrace/BacktraceMap.h>
26 
27 #include "BacktraceLog.h"
28 #include "UnwindMap.h"
29 #include "UnwindPtrace.h"
30 
UnwindPtrace(pid_t pid,pid_t tid,BacktraceMap * map)31 UnwindPtrace::UnwindPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
32     : BacktracePtrace(pid, tid, map), addr_space_(nullptr), upt_info_(nullptr) {
33 }
34 
~UnwindPtrace()35 UnwindPtrace::~UnwindPtrace() {
36   if (upt_info_) {
37     _UPT_destroy(upt_info_);
38     upt_info_ = nullptr;
39   }
40   if (addr_space_) {
41     // Remove the map from the address space before destroying it.
42     // It will be freed in the UnwindMap destructor.
43     unw_map_set(addr_space_, nullptr);
44 
45     unw_destroy_addr_space(addr_space_);
46     addr_space_ = nullptr;
47   }
48 }
49 
Unwind(size_t num_ignore_frames,ucontext_t * ucontext)50 bool UnwindPtrace::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
51   if (GetMap() == nullptr) {
52     // Without a map object, we can't do anything.
53     return false;
54   }
55 
56   if (ucontext) {
57     BACK_LOGW("Unwinding from a specified context not supported yet.");
58     return false;
59   }
60 
61   addr_space_ = unw_create_addr_space(&_UPT_accessors, 0);
62   if (!addr_space_) {
63     BACK_LOGW("unw_create_addr_space failed.");
64     return false;
65   }
66 
67   UnwindMap* map = static_cast<UnwindMap*>(GetMap());
68   unw_map_set(addr_space_, map->GetMapCursor());
69 
70   upt_info_ = reinterpret_cast<struct UPT_info*>(_UPT_create(Tid()));
71   if (!upt_info_) {
72     BACK_LOGW("Failed to create upt info.");
73     return false;
74   }
75 
76   unw_cursor_t cursor;
77   int ret = unw_init_remote(&cursor, addr_space_, upt_info_);
78   if (ret < 0) {
79     BACK_LOGW("unw_init_remote failed %d", ret);
80     return false;
81   }
82 
83   size_t num_frames = 0;
84   do {
85     unw_word_t pc;
86     ret = unw_get_reg(&cursor, UNW_REG_IP, &pc);
87     if (ret < 0) {
88       BACK_LOGW("Failed to read IP %d", ret);
89       break;
90     }
91     unw_word_t sp;
92     ret = unw_get_reg(&cursor, UNW_REG_SP, &sp);
93     if (ret < 0) {
94       BACK_LOGW("Failed to read SP %d", ret);
95       break;
96     }
97 
98     if (num_ignore_frames == 0) {
99       frames_.resize(num_frames+1);
100       backtrace_frame_data_t* frame = &frames_.at(num_frames);
101       frame->num = num_frames;
102       frame->pc = static_cast<uintptr_t>(pc);
103       frame->sp = static_cast<uintptr_t>(sp);
104       frame->stack_size = 0;
105 
106       if (num_frames > 0) {
107         backtrace_frame_data_t* prev = &frames_.at(num_frames-1);
108         prev->stack_size = frame->sp - prev->sp;
109       }
110 
111       frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
112 
113       FillInMap(frame->pc, &frame->map);
114 
115       num_frames++;
116     } else {
117       num_ignore_frames--;
118     }
119     ret = unw_step (&cursor);
120   } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
121 
122   return true;
123 }
124 
GetFunctionNameRaw(uintptr_t pc,uintptr_t * offset)125 std::string UnwindPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
126   *offset = 0;
127   char buf[512];
128   unw_word_t value;
129   if (unw_get_proc_name_by_ip(addr_space_, pc, buf, sizeof(buf), &value,
130                               upt_info_) >= 0 && buf[0] != '\0') {
131     *offset = static_cast<uintptr_t>(value);
132     return buf;
133   }
134   return "";
135 }
136