LLDB mainline
LoadedModuleList.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_SOURCE_PLUGINS_PROCESS_WINDOWS_COMMON_LOADEDMODULELIST_H
10#define LLDB_SOURCE_PLUGINS_PROCESS_WINDOWS_COMMON_LOADEDMODULELIST_H
11
13#include "lldb/lldb-types.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/SmallVector.h"
16
17#include <map>
18#include <optional>
19
20namespace lldb_private {
21
22/// The images mapped into the inferior. The same file can be mapped more than
23/// once, and an UNLOAD_DLL_DEBUG_EVENT only carries a base address, so every
24/// mapping has to be resolvable back to its file.
26 // Entries are removed as soon as their address list becomes empty (see
27 // Remove()), so every address list in the map is guaranteed non-empty and
28 // it is safe to call front() on it.
29 using Container = std::map<FileSpec, llvm::SmallVector<lldb::addr_t, 1>>;
30
31public:
32 void Add(const FileSpec &file_spec, lldb::addr_t base_addr) {
33 m_modules[file_spec].push_back(base_addr);
34 }
35
36 /// Drop the mapping at \p base_addr. \return the file if that was its last
37 /// mapping, an empty FileSpec otherwise.
39 for (auto it = m_modules.begin(); it != m_modules.end(); ++it) {
40 auto &base_addrs = it->second;
41 auto addr_it = llvm::find(base_addrs, base_addr);
42 if (addr_it == base_addrs.end())
43 continue;
44
45 base_addrs.erase(addr_it);
46 if (!base_addrs.empty())
47 return {};
48
49 FileSpec file_spec = it->first;
50 m_modules.erase(it);
51 return file_spec;
52 }
53 return {};
54 }
55
56 /// The address \p file_spec was first mapped at. That is the mapping the
57 /// loader bound the image's imports against, so it is the one to report.
58 std::optional<lldb::addr_t> GetBaseAddress(const FileSpec &file_spec) const {
59 auto it = m_modules.find(file_spec);
60 if (it == m_modules.end())
61 return std::nullopt;
62 return it->second.front();
63 }
64
65 /// \return the stored spelling of \p file_spec, or nullptr if it is not
66 /// mapped.
67 const FileSpec *FindFile(const FileSpec &file_spec) const {
68 auto it = m_modules.find(file_spec);
69 return it == m_modules.end() ? nullptr : &it->first;
70 }
71
72 bool IsEmpty() const { return m_modules.empty(); }
73 size_t GetSize() const { return m_modules.size(); }
74
75 Container::const_iterator begin() const { return m_modules.begin(); }
76 Container::const_iterator end() const { return m_modules.end(); }
77
78private:
80};
81
82} // namespace lldb_private
83
84#endif
A file utility class.
Definition FileSpec.h:56
The images mapped into the inferior.
void Add(const FileSpec &file_spec, lldb::addr_t base_addr)
Container::const_iterator end() const
std::map< FileSpec, llvm::SmallVector< lldb::addr_t, 1 > > Container
std::optional< lldb::addr_t > GetBaseAddress(const FileSpec &file_spec) const
The address file_spec was first mapped at.
const FileSpec * FindFile(const FileSpec &file_spec) const
FileSpec Remove(lldb::addr_t base_addr)
Drop the mapping at base_addr.
Container::const_iterator begin() const
A class that represents a running process on the host machine.
uint64_t addr_t
Definition lldb-types.h:80