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 #include <stdint.h>
18
19 #include <deque>
20 #include <string>
21
22 #include <unwindstack/Elf.h>
23 #include <unwindstack/ElfInterface.h>
24 #include <unwindstack/Memory.h>
25 #include <unwindstack/Regs.h>
26 #include <unwindstack/SharedString.h>
27
28 #include "ElfFake.h"
29 #include "utils/RegsFake.h"
30
31 namespace unwindstack {
32
33 std::deque<FunctionData> ElfInterfaceFake::functions_;
34 std::deque<StepData> ElfInterfaceFake::steps_;
35
GetFunctionName(uint64_t,SharedString * name,uint64_t * offset)36 bool ElfInterfaceFake::GetFunctionName(uint64_t, SharedString* name, uint64_t* offset) {
37 if (functions_.empty()) {
38 return false;
39 }
40 auto& entry = functions_.front();
41 *name = SharedString(std::move(entry.name));
42 *offset = entry.offset;
43 functions_.pop_front();
44 return true;
45 }
46
GetGlobalVariable(const std::string & global,uint64_t * offset)47 bool ElfInterfaceFake::GetGlobalVariable(const std::string& global, uint64_t* offset) {
48 auto entry = globals_.find(global);
49 if (entry == globals_.end()) {
50 return false;
51 }
52 *offset = entry->second;
53 return true;
54 }
55
Step(uint64_t,Regs * regs,Memory *,bool * finished,bool * is_signal_frame)56 bool ElfInterfaceFake::Step(uint64_t, Regs* regs, Memory*, bool* finished, bool* is_signal_frame) {
57 if (steps_.empty()) {
58 return false;
59 }
60 auto entry = steps_.front();
61 steps_.pop_front();
62
63 if (entry.pc == 0 && entry.sp == 0 && !entry.finished) {
64 // Pretend as though there is no frame.
65 return false;
66 }
67
68 RegsFake* fake_regs = reinterpret_cast<RegsFake*>(regs);
69 fake_regs->set_pc(entry.pc);
70 fake_regs->set_sp(entry.sp);
71 *finished = entry.finished;
72 *is_signal_frame = false;
73 return true;
74 }
75
76 } // namespace unwindstack
77