• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 _GNU_SOURCE 1
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include <memory>
23 #include <set>
24 #include <string>
25 
26 #include <backtrace/Backtrace.h>
27 #include <unwindstack/Elf.h>
28 #include <unwindstack/MapInfo.h>
29 #include <unwindstack/Maps.h>
30 #include <unwindstack/Memory.h>
31 #include <unwindstack/Regs.h>
32 #include <unwindstack/RegsGetLocal.h>
33 
34 #if !defined(NO_LIBDEXFILE_SUPPORT)
35 #include <unwindstack/DexFiles.h>
36 #endif
37 #include <unwindstack/Unwinder.h>
38 
39 #include "BacktraceLog.h"
40 #include "UnwindStack.h"
41 #include "UnwindStackMap.h"
42 
43 extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
44 
Unwind(unwindstack::Regs * regs,BacktraceMap * back_map,std::vector<backtrace_frame_data_t> * frames,size_t num_ignore_frames,std::vector<std::string> * skip_names,BacktraceUnwindError * error)45 bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
46                        std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames,
47                        std::vector<std::string>* skip_names, BacktraceUnwindError* error) {
48   UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
49   auto process_memory = stack_map->process_memory();
50   unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(),
51                                  regs, stack_map->process_memory());
52   unwinder.SetResolveNames(stack_map->ResolveNames());
53   stack_map->SetArch(regs->Arch());
54   if (stack_map->GetJitDebug() != nullptr) {
55     unwinder.SetJitDebug(stack_map->GetJitDebug());
56   }
57 #if !defined(NO_LIBDEXFILE_SUPPORT)
58   if (stack_map->GetDexFiles() != nullptr) {
59     unwinder.SetDexFiles(stack_map->GetDexFiles());
60   }
61 #endif
62   unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore());
63   if (error != nullptr) {
64     switch (unwinder.LastErrorCode()) {
65       case unwindstack::ERROR_NONE:
66         error->error_code = BACKTRACE_UNWIND_NO_ERROR;
67         break;
68 
69       case unwindstack::ERROR_MEMORY_INVALID:
70         error->error_code = BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED;
71         error->error_info.addr = unwinder.LastErrorAddress();
72         break;
73 
74       case unwindstack::ERROR_UNWIND_INFO:
75         error->error_code = BACKTRACE_UNWIND_ERROR_UNWIND_INFO;
76         break;
77 
78       case unwindstack::ERROR_UNSUPPORTED:
79         error->error_code = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION;
80         break;
81 
82       case unwindstack::ERROR_INVALID_MAP:
83         error->error_code = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
84         break;
85 
86       case unwindstack::ERROR_MAX_FRAMES_EXCEEDED:
87         error->error_code = BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT;
88         break;
89 
90       case unwindstack::ERROR_REPEATED_FRAME:
91         error->error_code = BACKTRACE_UNWIND_ERROR_REPEATED_FRAME;
92         break;
93 
94       case unwindstack::ERROR_INVALID_ELF:
95         error->error_code = BACKTRACE_UNWIND_ERROR_INVALID_ELF;
96         break;
97 
98       case unwindstack::ERROR_THREAD_DOES_NOT_EXIST:
99         error->error_code = BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST;
100         break;
101 
102       case unwindstack::ERROR_THREAD_TIMEOUT:
103         error->error_code = BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT;
104         break;
105 
106       case unwindstack::ERROR_SYSTEM_CALL:
107       case unwindstack::ERROR_MAPS_PARSE:
108       case unwindstack::ERROR_BAD_ARCH:
109       case unwindstack::ERROR_INVALID_PARAMETER:
110         error->error_code = BACKTRACE_UNWIND_ERROR_INTERNAL;
111         break;
112     }
113   }
114 
115   if (num_ignore_frames >= unwinder.NumFrames()) {
116     frames->resize(0);
117     return true;
118   }
119 
120   auto unwinder_frames = unwinder.frames();
121   frames->resize(unwinder.NumFrames() - num_ignore_frames);
122   size_t cur_frame = 0;
123   for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) {
124     auto frame = &unwinder_frames[i];
125 
126     backtrace_frame_data_t* back_frame = &frames->at(cur_frame);
127 
128     back_frame->num = cur_frame++;
129 
130     back_frame->rel_pc = frame->rel_pc;
131     back_frame->pc = frame->pc;
132     back_frame->sp = frame->sp;
133 
134     char* demangled_name = __cxa_demangle(frame->function_name.c_str(), nullptr, nullptr, nullptr);
135     if (demangled_name != nullptr) {
136       back_frame->func_name = demangled_name;
137       free(demangled_name);
138     } else {
139       back_frame->func_name = frame->function_name;
140     }
141     back_frame->func_offset = frame->function_offset;
142 
143     if (frame->map_info != nullptr) {
144       back_frame->map.name = frame->map_info->name();
145       back_frame->map.start = frame->map_info->start();
146       back_frame->map.end = frame->map_info->end();
147       back_frame->map.offset = frame->map_info->elf_start_offset();
148       back_frame->map.load_bias = frame->map_info->load_bias();
149       back_frame->map.flags = frame->map_info->flags();
150     } else {
151       back_frame->map.start = 0;
152       back_frame->map.end = 0;
153       back_frame->map.offset = 0;
154       back_frame->map.load_bias = 0;
155       back_frame->map.flags = 0;
156     }
157   }
158 
159   return true;
160 }
161 
UnwindStackCurrent(pid_t pid,pid_t tid,BacktraceMap * map)162 UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
163     : BacktraceCurrent(pid, tid, map) {}
164 
GetFunctionNameRaw(uint64_t pc,uint64_t * offset)165 std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
166   return GetMap()->GetFunctionName(pc, offset);
167 }
168 
UnwindFromContext(size_t num_ignore_frames,void * ucontext)169 bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucontext) {
170   std::unique_ptr<unwindstack::Regs> regs;
171   if (ucontext == nullptr) {
172     regs.reset(unwindstack::Regs::CreateFromLocal());
173     // Fill in the registers from this function. Do it here to avoid
174     // one extra function call appearing in the unwind.
175     unwindstack::RegsGetLocal(regs.get());
176   } else {
177     regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
178   }
179 
180   std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
181   if (!skip_frames_) {
182     skip_names.clear();
183   }
184   return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names, &error_);
185 }
186 
UnwindStackPtrace(pid_t pid,pid_t tid,BacktraceMap * map)187 UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
188     : BacktracePtrace(pid, tid, map), memory_(unwindstack::Memory::CreateProcessMemory(pid)) {}
189 
GetFunctionNameRaw(uint64_t pc,uint64_t * offset)190 std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
191   return GetMap()->GetFunctionName(pc, offset);
192 }
193 
Unwind(size_t num_ignore_frames,void * context)194 bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) {
195   std::unique_ptr<unwindstack::Regs> regs;
196   if (context == nullptr) {
197     regs.reset(unwindstack::Regs::RemoteGet(Tid()));
198   } else {
199     regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context));
200   }
201 
202   return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
203 }
204 
Read(uint64_t addr,uint8_t * buffer,size_t bytes)205 size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
206 #if defined(__aarch64__)
207   // Tagged pointer after Android R would lead top byte to have random values
208   // https://source.android.com/devices/tech/debug/tagged-pointers
209   addr &= (1ULL << 56) - 1;
210 #endif
211   return memory_->Read(addr, buffer, bytes);
212 }
213