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(), regs->Arch());
56   }
57 #if !defined(NO_LIBDEXFILE_SUPPORT)
58   if (stack_map->GetDexFiles() != nullptr) {
59     unwinder.SetDexFiles(stack_map->GetDexFiles(), regs->Arch());
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   }
99 
100   if (num_ignore_frames >= unwinder.NumFrames()) {
101     frames->resize(0);
102     return true;
103   }
104 
105   auto unwinder_frames = unwinder.frames();
106   frames->resize(unwinder.NumFrames() - num_ignore_frames);
107   size_t cur_frame = 0;
108   for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) {
109     auto frame = &unwinder_frames[i];
110 
111     backtrace_frame_data_t* back_frame = &frames->at(cur_frame);
112 
113     back_frame->num = cur_frame++;
114 
115     back_frame->rel_pc = frame->rel_pc;
116     back_frame->pc = frame->pc;
117     back_frame->sp = frame->sp;
118 
119     char* demangled_name = __cxa_demangle(frame->function_name.c_str(), nullptr, nullptr, nullptr);
120     if (demangled_name != nullptr) {
121       back_frame->func_name = demangled_name;
122       free(demangled_name);
123     } else {
124       back_frame->func_name = frame->function_name;
125     }
126     back_frame->func_offset = frame->function_offset;
127 
128     back_frame->map.name = frame->map_name;
129     back_frame->map.start = frame->map_start;
130     back_frame->map.end = frame->map_end;
131     back_frame->map.offset = frame->map_elf_start_offset;
132     back_frame->map.load_bias = frame->map_load_bias;
133     back_frame->map.flags = frame->map_flags;
134   }
135 
136   return true;
137 }
138 
UnwindStackCurrent(pid_t pid,pid_t tid,BacktraceMap * map)139 UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
140     : BacktraceCurrent(pid, tid, map) {}
141 
GetFunctionNameRaw(uint64_t pc,uint64_t * offset)142 std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
143   return GetMap()->GetFunctionName(pc, offset);
144 }
145 
UnwindFromContext(size_t num_ignore_frames,void * ucontext)146 bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucontext) {
147   std::unique_ptr<unwindstack::Regs> regs;
148   if (ucontext == nullptr) {
149     regs.reset(unwindstack::Regs::CreateFromLocal());
150     // Fill in the registers from this function. Do it here to avoid
151     // one extra function call appearing in the unwind.
152     unwindstack::RegsGetLocal(regs.get());
153   } else {
154     regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
155   }
156 
157   std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
158   if (!skip_frames_) {
159     skip_names.clear();
160   }
161   return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names, &error_);
162 }
163 
UnwindStackPtrace(pid_t pid,pid_t tid,BacktraceMap * map)164 UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
165     : BacktracePtrace(pid, tid, map), memory_(unwindstack::Memory::CreateProcessMemory(pid)) {}
166 
GetFunctionNameRaw(uint64_t pc,uint64_t * offset)167 std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
168   return GetMap()->GetFunctionName(pc, offset);
169 }
170 
Unwind(size_t num_ignore_frames,void * context)171 bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) {
172   std::unique_ptr<unwindstack::Regs> regs;
173   if (context == nullptr) {
174     regs.reset(unwindstack::Regs::RemoteGet(Tid()));
175   } else {
176     regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context));
177   }
178 
179   return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
180 }
181 
Read(uint64_t addr,uint8_t * buffer,size_t bytes)182 size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
183   return memory_->Read(addr, buffer, bytes);
184 }
185