1 /*
2  * Copyright (C) 2018 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 #ifndef _LIBUNWINDSTACK_DEX_FILES_H
18 #define _LIBUNWINDSTACK_DEX_FILES_H
19 
20 #include <stdint.h>
21 
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <unordered_map>
26 #include <vector>
27 
28 namespace unwindstack {
29 
30 // Forward declarations.
31 class DexFile;
32 class Maps;
33 struct MapInfo;
34 class Memory;
35 enum ArchEnum : uint8_t;
36 
37 class DexFiles {
38  public:
39   explicit DexFiles(std::shared_ptr<Memory>& memory);
40   DexFiles(std::shared_ptr<Memory>& memory, std::vector<std::string>& search_libs);
41   ~DexFiles();
42 
43   DexFile* GetDexFile(uint64_t dex_file_offset, MapInfo* info);
44 
45   void GetMethodInformation(Maps* maps, MapInfo* info, uint64_t dex_pc, std::string* method_name,
46                             uint64_t* method_offset);
47 
48   void SetArch(ArchEnum arch);
49 
50  private:
51   void Init(Maps* maps);
52 
53   bool GetAddr(size_t index, uint64_t* addr);
54 
55   uint64_t ReadEntryPtr32(uint64_t addr);
56 
57   uint64_t ReadEntryPtr64(uint64_t addr);
58 
59   bool ReadEntry32();
60 
61   bool ReadEntry64();
62 
63   std::shared_ptr<Memory> memory_;
64   std::vector<std::string> search_libs_;
65 
66   std::mutex lock_;
67   bool initialized_ = false;
68   std::unordered_map<uint64_t, DexFile*> files_;
69 
70   uint64_t entry_addr_ = 0;
71   uint64_t (DexFiles::*read_entry_ptr_func_)(uint64_t) = nullptr;
72   bool (DexFiles::*read_entry_func_)() = nullptr;
73   std::vector<uint64_t> addrs_;
74 };
75 
76 }  // namespace unwindstack
77 
78 #endif  // _LIBUNWINDSTACK_DEX_FILES_H
79