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