1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <pthread.h>
30 #include <stdint.h>
31 
32 #include <memory>
33 #include <string>
34 #include <vector>
35 
36 #include <unwindstack/Elf.h>
37 #include <unwindstack/LocalUnwinder.h>
38 #include <unwindstack/MapInfo.h>
39 #include <unwindstack/Maps.h>
40 #include <unwindstack/Memory.h>
41 #include <unwindstack/Regs.h>
42 #include <unwindstack/RegsGetLocal.h>
43 
44 namespace unwindstack {
45 
Init()46 bool LocalUnwinder::Init() {
47   pthread_rwlock_init(&maps_rwlock_, nullptr);
48 
49   // Create the maps.
50   maps_.reset(new unwindstack::LocalUpdatableMaps());
51   if (!maps_->Parse()) {
52     maps_.reset();
53     return false;
54   }
55 
56   process_memory_ = unwindstack::Memory::CreateProcessMemoryThreadCached(getpid());
57 
58   return true;
59 }
60 
ShouldSkipLibrary(const std::string & map_name)61 bool LocalUnwinder::ShouldSkipLibrary(const std::string& map_name) {
62   for (const std::string& skip_library : skip_libraries_) {
63     if (skip_library == map_name) {
64       return true;
65     }
66   }
67   return false;
68 }
69 
Unwind(std::vector<LocalFrameData> * frame_info,size_t max_frames)70 bool LocalUnwinder::Unwind(std::vector<LocalFrameData>* frame_info, size_t max_frames) {
71   std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal());
72   unwindstack::RegsGetLocal(regs.get());
73   ArchEnum arch = regs->Arch();
74 
75   // Clear any cached data from previous unwinds.
76   process_memory_->Clear();
77 
78   size_t num_frames = 0;
79   bool adjust_pc = false;
80   while (true) {
81     uint64_t cur_pc = regs->pc();
82     uint64_t cur_sp = regs->sp();
83 
84     MapInfo* map_info = maps_->Find(cur_pc);
85     if (map_info == nullptr) {
86       break;
87     }
88 
89     Elf* elf = map_info->GetElf(process_memory_, arch);
90     uint64_t rel_pc = elf->GetRelPc(cur_pc, map_info);
91     uint64_t step_pc = rel_pc;
92     uint64_t pc_adjustment;
93     if (adjust_pc) {
94       pc_adjustment = GetPcAdjustment(rel_pc, elf, arch);
95     } else {
96       pc_adjustment = 0;
97     }
98     step_pc -= pc_adjustment;
99 
100     bool finished = false;
101     bool is_signal_frame = false;
102     if (elf->StepIfSignalHandler(rel_pc, regs.get(), process_memory_.get())) {
103       step_pc = rel_pc;
104     } else if (!elf->Step(step_pc, regs.get(), process_memory_.get(), &finished,
105                           &is_signal_frame)) {
106       finished = true;
107     }
108 
109     // Skip any locations that are within this library.
110     if (num_frames != 0 || !ShouldSkipLibrary(map_info->name())) {
111       // Add frame information.
112       SharedString func_name;
113       uint64_t func_offset;
114       if (elf->GetFunctionName(rel_pc, &func_name, &func_offset)) {
115         frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment,
116                                  func_name, func_offset);
117       } else {
118         frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment, "", 0);
119       }
120       num_frames++;
121     }
122 
123     if (finished || frame_info->size() == max_frames ||
124         (cur_pc == regs->pc() && cur_sp == regs->sp())) {
125       break;
126     }
127     adjust_pc = true;
128   }
129   return num_frames != 0;
130 }
131 
132 }  // namespace unwindstack
133