• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 #pragma once
18 
19 #include <stdint.h>
20 
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 
25 #include <unwindstack/Memory.h>
26 
27 namespace unwindstack {
28 
29 class MemoryFake : public Memory {
30  public:
31   MemoryFake() = default;
32   virtual ~MemoryFake() = default;
33 
34   size_t Read(uint64_t addr, void* buffer, size_t size) override;
35 
36   void SetMemory(uint64_t addr, const void* memory, size_t length);
37 
38   void SetMemoryBlock(uint64_t addr, size_t length, uint8_t value);
39 
SetData8(uint64_t addr,uint8_t value)40   void SetData8(uint64_t addr, uint8_t value) { SetMemory(addr, &value, sizeof(value)); }
41 
SetData16(uint64_t addr,uint16_t value)42   void SetData16(uint64_t addr, uint16_t value) { SetMemory(addr, &value, sizeof(value)); }
43 
SetData32(uint64_t addr,uint32_t value)44   void SetData32(uint64_t addr, uint32_t value) { SetMemory(addr, &value, sizeof(value)); }
45 
SetData64(uint64_t addr,uint64_t value)46   void SetData64(uint64_t addr, uint64_t value) { SetMemory(addr, &value, sizeof(value)); }
47 
SetMemory(uint64_t addr,std::vector<uint8_t> values)48   void SetMemory(uint64_t addr, std::vector<uint8_t> values) {
49     SetMemory(addr, values.data(), values.size());
50   }
51 
SetMemory(uint64_t addr,std::string string)52   void SetMemory(uint64_t addr, std::string string) {
53     SetMemory(addr, string.c_str(), string.size() + 1);
54   }
55 
Clear()56   void Clear() { data_.clear(); }
57 
58  private:
59   std::unordered_map<uint64_t, uint8_t> data_;
60 };
61 
62 class MemoryFakeAlwaysReadZero : public Memory {
63  public:
64   MemoryFakeAlwaysReadZero() = default;
65   virtual ~MemoryFakeAlwaysReadZero() = default;
66 
Read(uint64_t,void * buffer,size_t size)67   size_t Read(uint64_t, void* buffer, size_t size) override {
68     memset(buffer, 0, size);
69     return size;
70   }
71 };
72 
73 }  // namespace unwindstack
74