blob: 5b2fadf2b2e062f2d3ecea5da011ad1816de78ce [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());
Christopher Ferris4568f4b2018-10-23 17:42:41 -070091 ArchEnum arch = regs->Arch();
Christopher Ferrisca9a54b2018-04-05 11:15:00 -070092
93 size_t num_frames = 0;
94 bool adjust_pc = false;
95 while (true) {
96 uint64_t cur_pc = regs->pc();
97 uint64_t cur_sp = regs->sp();
98
99 MapInfo* map_info = GetMapInfo(cur_pc);
100 if (map_info == nullptr) {
101 break;
102 }
103
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700104 Elf* elf = map_info->GetElf(process_memory_, arch);
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700105 uint64_t rel_pc = elf->GetRelPc(cur_pc, map_info);
106 uint64_t step_pc = rel_pc;
107 uint64_t pc_adjustment;
108 if (adjust_pc) {
109 pc_adjustment = regs->GetPcAdjustment(rel_pc, elf);
110 } else {
111 pc_adjustment = 0;
112 }
113 step_pc -= pc_adjustment;
114 // Skip any locations that are within this library.
115 if (num_frames != 0 || !ShouldSkipLibrary(map_info->name)) {
116 // Add frame information.
117 std::string func_name;
118 uint64_t func_offset;
119 if (elf->GetFunctionName(rel_pc, &func_name, &func_offset)) {
120 frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment,
121 func_name, func_offset);
122 } else {
123 frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment, "", 0);
124 }
125 num_frames++;
126 }
127 if (!elf->valid()) {
128 break;
129 }
130 if (frame_info->size() == max_frames) {
131 break;
132 }
133
134 adjust_pc = true;
135 bool finished;
136 if (!elf->Step(rel_pc, step_pc, regs.get(), process_memory_.get(), &finished) || finished) {
137 break;
138 }
139 // pc and sp are the same, terminate the unwind.
140 if (cur_pc == regs->pc() && cur_sp == regs->sp()) {
141 break;
142 }
143 }
144 return num_frames != 0;
145}
146
147} // namespace unwindstack