blob: 952b332f00580fa40186d00b31a91d93cc7beb8d [file] [log] [blame]
Christopher Ferrisca9a54b2018-04-05 11:15:00 -07001/*
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
44namespace unwindstack {
45
46bool 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::CreateProcessMemory(getpid());
57
58 return true;
59}
60
61bool 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
70MapInfo* LocalUnwinder::GetMapInfo(uint64_t pc) {
71 pthread_rwlock_rdlock(&maps_rwlock_);
72 MapInfo* map_info = maps_->Find(pc);
73 pthread_rwlock_unlock(&maps_rwlock_);
74
75 if (map_info == nullptr) {
76 pthread_rwlock_wrlock(&maps_rwlock_);
77 // This is guaranteed not to invalidate any previous MapInfo objects so
78 // we don't need to worry about any MapInfo* values already in use.
79 if (maps_->Reparse()) {
80 map_info = maps_->Find(pc);
81 }
82 pthread_rwlock_unlock(&maps_rwlock_);
83 }
84
85 return map_info;
86}
87
88bool LocalUnwinder::Unwind(std::vector<LocalFrameData>* frame_info, size_t max_frames) {
89 std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal());
90 unwindstack::RegsGetLocal(regs.get());
91
92 size_t num_frames = 0;
93 bool adjust_pc = false;
94 while (true) {
95 uint64_t cur_pc = regs->pc();
96 uint64_t cur_sp = regs->sp();
97
98 MapInfo* map_info = GetMapInfo(cur_pc);
99 if (map_info == nullptr) {
100 break;
101 }
102
103 Elf* elf = map_info->GetElf(process_memory_, true);
104 uint64_t rel_pc = elf->GetRelPc(cur_pc, map_info);
105 uint64_t step_pc = rel_pc;
106 uint64_t pc_adjustment;
107 if (adjust_pc) {
108 pc_adjustment = regs->GetPcAdjustment(rel_pc, elf);
109 } else {
110 pc_adjustment = 0;
111 }
112 step_pc -= pc_adjustment;
113 // Skip any locations that are within this library.
114 if (num_frames != 0 || !ShouldSkipLibrary(map_info->name)) {
115 // Add frame information.
116 std::string func_name;
117 uint64_t func_offset;
118 if (elf->GetFunctionName(rel_pc, &func_name, &func_offset)) {
119 frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment,
120 func_name, func_offset);
121 } else {
122 frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment, "", 0);
123 }
124 num_frames++;
125 }
126 if (!elf->valid()) {
127 break;
128 }
129 if (frame_info->size() == max_frames) {
130 break;
131 }
132
133 adjust_pc = true;
134 bool finished;
135 if (!elf->Step(rel_pc, step_pc, regs.get(), process_memory_.get(), &finished) || finished) {
136 break;
137 }
138 // pc and sp are the same, terminate the unwind.
139 if (cur_pc == regs->pc() && cur_sp == regs->sp()) {
140 break;
141 }
142 }
143 return num_frames != 0;
144}
145
146} // namespace unwindstack