LLDB mainline
PathMappingList.h
Go to the documentation of this file.
1//===-- PathMappingList.h ---------------------------------------*- C++ -*-===//
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_TARGET_PATHMAPPINGLIST_H
10#define LLDB_TARGET_PATHMAPPINGLIST_H
11
12#include "lldb/Utility/Status.h"
13#include "llvm/Support/Error.h"
14#include "llvm/Support/JSON.h"
15#include <map>
16#include <mutex>
17#include <optional>
18#include <vector>
19
20namespace lldb_private {
21
23public:
24 typedef void (*ChangedCallback)(const PathMappingList &path_list,
25 void *baton);
26
28
29 PathMappingList(ChangedCallback callback, void *callback_baton);
30
32
34
35 const PathMappingList &operator=(const PathMappingList &rhs);
36
37 void Append(llvm::StringRef path, llvm::StringRef replacement, bool notify);
38
39 void Append(const PathMappingList &rhs, bool notify);
40
41 /// Append <path, replacement> pair without duplication.
42 /// \return whether appending suceeds without duplication or not.
43 bool AppendUnique(llvm::StringRef path, llvm::StringRef replacement,
44 bool notify);
45
46 void Clear(bool notify);
47
48 // By default, dump all pairs.
49 void Dump(Stream *s, int pair_index = -1);
50
51 llvm::json::Value ToJSON() const;
52
53 bool IsEmpty() const {
54 std::lock_guard<std::mutex> lock(m_pairs_mutex);
55 return m_pairs.empty();
56 }
57
58 size_t GetSize() const {
59 std::lock_guard<std::mutex> lock(m_pairs_mutex);
60 return m_pairs.size();
61 }
62
63 bool GetPathsAtIndex(uint32_t idx, std::string &path,
64 std::string &new_path) const;
65
66 void Insert(llvm::StringRef path, llvm::StringRef replacement,
67 uint32_t insert_idx, bool notify);
68
69 bool Remove(size_t index, bool notify);
70
71 bool Remove(llvm::StringRef path, bool notify);
72
73 bool Replace(llvm::StringRef path, llvm::StringRef replacement, bool notify);
74
75 bool Replace(llvm::StringRef path, llvm::StringRef replacement,
76 uint32_t index, bool notify);
77 bool RemapPath(llvm::StringRef path, std::string &new_path) const;
78
79 /// Remaps a source file given \a path into \a new_path.
80 ///
81 /// Remaps \a path if any source remappings match. This function
82 /// does NOT stat the file system so it can be used in tight loops
83 /// where debug info is being parsed.
84 ///
85 /// \param[in] path
86 /// The original source file path to try and remap.
87 ///
88 /// \param[in] only_if_exists
89 /// If \b true, besides matching \p path with the remapping rules, this
90 /// tries to check with the filesystem that the remapped file exists. If
91 /// no valid file is found, \b std::nullopt is returned. This might be
92 /// expensive, specially on a network.
93 ///
94 /// If \b false, then the existence of the returned remapping is not
95 /// checked.
96 ///
97 /// \return
98 /// The remapped filespec that may or may not exist on disk.
99 std::optional<FileSpec> RemapPath(llvm::StringRef path,
100 bool only_if_exists = false) const;
101 bool RemapPath(const char *, std::string &) const = delete;
102
103 /// Perform reverse source path remap for input \a file.
104 /// Source maps contains a list of <from_original_path, to_new_path> mappings.
105 /// Reverse remap means locating a matching entry prefix using "to_new_path"
106 /// part and replacing it with "from_original_path" part if found.
107 ///
108 /// \param[in] file
109 /// The source path to reverse remap.
110 /// \param[in] fixed
111 /// The reversed mapped new path.
112 ///
113 /// \return
114 /// std::nullopt if no remapping happens, otherwise, the matching source
115 /// map entry's ""to_new_pathto"" part (which is the prefix of \a file) is
116 /// returned.
117 std::optional<llvm::StringRef> ReverseRemapPath(const FileSpec &file,
118 FileSpec &fixed) const;
119
120 /// Finds a source file given a file spec using the path remappings.
121 ///
122 /// Tries to resolve \a orig_spec by checking the path remappings.
123 /// It makes sure the file exists by checking with the file system,
124 /// so this call can be expensive if the remappings are on a network
125 /// or are even on the local file system, so use this function
126 /// sparingly (not in a tight debug info parsing loop).
127 ///
128 /// \param[in] orig_spec
129 /// The original source file path to try and remap.
130 ///
131 /// \return
132 /// The newly remapped filespec that is guaranteed to exist.
133 std::optional<FileSpec> FindFile(const FileSpec &orig_spec) const;
134
135 uint32_t GetModificationID() const {
136 std::lock_guard<std::mutex> lock(m_pairs_mutex);
137 return m_mod_id;
138 }
139
140protected:
141 typedef std::pair<std::string, std::string> pair;
142 typedef std::vector<pair> collection;
143 typedef collection::iterator iterator;
144 typedef collection::const_iterator const_iterator;
145
146 void AppendNoLock(llvm::StringRef path, llvm::StringRef replacement);
147 uint32_t FindIndexForPathNoLock(llvm::StringRef path) const;
148 void Notify(bool notify) const;
149
150 iterator FindIteratorForPath(llvm::StringRef path);
151
153 mutable std::mutex m_pairs_mutex;
154
156 void *m_callback_baton = nullptr;
157 mutable std::mutex m_callback_mutex;
158
159 /// Incremented anytime anything is added to or removed from m_pairs. Also
160 /// protected by m_pairs_mutex.
161 uint32_t m_mod_id = 0;
162};
163
164} // namespace lldb_private
165
166#endif // LLDB_TARGET_PATHMAPPINGLIST_H
A file utility class.
Definition FileSpec.h:56
bool Remove(size_t index, bool notify)
bool RemapPath(const char *, std::string &) const =delete
bool AppendUnique(llvm::StringRef path, llvm::StringRef replacement, bool notify)
Append <path, replacement> pair without duplication.
uint32_t FindIndexForPathNoLock(llvm::StringRef path) const
std::optional< FileSpec > FindFile(const FileSpec &orig_spec) const
Finds a source file given a file spec using the path remappings.
bool Replace(llvm::StringRef path, llvm::StringRef replacement, bool notify)
llvm::json::Value ToJSON() const
bool RemapPath(llvm::StringRef path, std::string &new_path) const
void(* ChangedCallback)(const PathMappingList &path_list, void *baton)
void Insert(llvm::StringRef path, llvm::StringRef replacement, uint32_t insert_idx, bool notify)
collection::const_iterator const_iterator
const PathMappingList & operator=(const PathMappingList &rhs)
void AppendNoLock(llvm::StringRef path, llvm::StringRef replacement)
void Append(llvm::StringRef path, llvm::StringRef replacement, bool notify)
iterator FindIteratorForPath(llvm::StringRef path)
std::pair< std::string, std::string > pair
collection::iterator iterator
bool GetPathsAtIndex(uint32_t idx, std::string &path, std::string &new_path) const
uint32_t m_mod_id
Incremented anytime anything is added to or removed from m_pairs.
void Notify(bool notify) const
std::optional< llvm::StringRef > ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const
Perform reverse source path remap for input file.
void Dump(Stream *s, int pair_index=-1)
A stream class that can stream formatted output to a file.
Definition Stream.h:28
A class that represents a running process on the host machine.