LLDB mainline
PathMappingList.cpp
Go to the documentation of this file.
1//===-- PathMappingList.cpp -----------------------------------------------===//
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#include <climits>
10#include <cstring>
11#include <optional>
12
14#include "lldb/Host/PosixApi.h"
17#include "lldb/Utility/Status.h"
18#include "lldb/Utility/Stream.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24namespace {
25 // We must normalize our path pairs that we store because if we don't then
26 // things won't always work. We found a case where if we did:
27 // (lldb) settings set target.source-map . /tmp
28 // We would store a path pairs of "." and "/tmp" as raw strings. If the debug
29 // info contains "./foo/bar.c", the path will get normalized to "foo/bar.c".
30 // When PathMappingList::RemapPath() is called, it expects the path to start
31 // with the raw path pair, which doesn't work anymore because the paths have
32 // been normalized when the debug info was loaded. So we need to store
33 // nomalized path pairs to ensure things match up.
34std::string NormalizePath(llvm::StringRef path) {
35 // If we use "path" to construct a FileSpec, it will normalize the path for
36 // us. We then grab the string.
37 return FileSpec(path).GetPath();
38}
39}
40// PathMappingList constructor
42
43PathMappingList::PathMappingList(ChangedCallback callback, void *callback_baton)
44 : m_pairs(), m_callback(callback), m_callback_baton(callback_baton) {}
45
47 : m_pairs(rhs.m_pairs) {}
48
50 if (this != &rhs) {
51 m_pairs = rhs.m_pairs;
52 m_callback = nullptr;
53 m_callback_baton = nullptr;
54 m_mod_id = rhs.m_mod_id;
55 }
56 return *this;
57}
58
60
61void PathMappingList::Append(llvm::StringRef path, llvm::StringRef replacement,
62 bool notify) {
63 ++m_mod_id;
64 m_pairs.emplace_back(pair(NormalizePath(path), NormalizePath(replacement)));
65 if (notify && m_callback)
67}
68
69void PathMappingList::Append(const PathMappingList &rhs, bool notify) {
70 ++m_mod_id;
71 if (!rhs.m_pairs.empty()) {
72 const_iterator pos, end = rhs.m_pairs.end();
73 for (pos = rhs.m_pairs.begin(); pos != end; ++pos)
74 m_pairs.push_back(*pos);
75 if (notify && m_callback)
77 }
78}
79
80bool PathMappingList::AppendUnique(llvm::StringRef path,
81 llvm::StringRef replacement, bool notify) {
82 auto normalized_path = NormalizePath(path);
83 auto normalized_replacement = NormalizePath(replacement);
84 for (const auto &pair : m_pairs) {
85 if (pair.first.GetStringRef().equals(normalized_path) &&
86 pair.second.GetStringRef().equals(normalized_replacement))
87 return false;
88 }
89 Append(path, replacement, notify);
90 return true;
91}
92
93void PathMappingList::Insert(llvm::StringRef path, llvm::StringRef replacement,
94 uint32_t index, bool notify) {
95 ++m_mod_id;
96 iterator insert_iter;
97 if (index >= m_pairs.size())
98 insert_iter = m_pairs.end();
99 else
100 insert_iter = m_pairs.begin() + index;
101 m_pairs.emplace(insert_iter, pair(NormalizePath(path),
102 NormalizePath(replacement)));
103 if (notify && m_callback)
105}
106
107bool PathMappingList::Replace(llvm::StringRef path, llvm::StringRef replacement,
108 uint32_t index, bool notify) {
109 if (index >= m_pairs.size())
110 return false;
111 ++m_mod_id;
112 m_pairs[index] = pair(NormalizePath(path), NormalizePath(replacement));
113 if (notify && m_callback)
115 return true;
116}
117
118bool PathMappingList::Remove(size_t index, bool notify) {
119 if (index >= m_pairs.size())
120 return false;
121
122 ++m_mod_id;
123 iterator iter = m_pairs.begin() + index;
124 m_pairs.erase(iter);
125 if (notify && m_callback)
127 return true;
128}
129
130// For clients which do not need the pair index dumped, pass a pair_index >= 0
131// to only dump the indicated pair.
132void PathMappingList::Dump(Stream *s, int pair_index) {
133 unsigned int numPairs = m_pairs.size();
134
135 if (pair_index < 0) {
136 unsigned int index;
137 for (index = 0; index < numPairs; ++index)
138 s->Printf("[%d] \"%s\" -> \"%s\"\n", index,
139 m_pairs[index].first.GetCString(),
140 m_pairs[index].second.GetCString());
141 } else {
142 if (static_cast<unsigned int>(pair_index) < numPairs)
143 s->Printf("%s -> %s", m_pairs[pair_index].first.GetCString(),
144 m_pairs[pair_index].second.GetCString());
145 }
146}
147
148llvm::json::Value PathMappingList::ToJSON() {
149 llvm::json::Array entries;
150 for (const auto &pair : m_pairs) {
151 llvm::json::Array entry{pair.first.GetStringRef().str(),
152 pair.second.GetStringRef().str()};
153 entries.emplace_back(std::move(entry));
154 }
155 return entries;
156}
157
158void PathMappingList::Clear(bool notify) {
159 if (!m_pairs.empty())
160 ++m_mod_id;
161 m_pairs.clear();
162 if (notify && m_callback)
164}
165
167 ConstString &new_path) const {
168 if (std::optional<FileSpec> remapped = RemapPath(path.GetStringRef())) {
169 new_path.SetString(remapped->GetPath());
170 return true;
171 }
172 return false;
173}
174
175/// Append components to path, applying style.
176static void AppendPathComponents(FileSpec &path, llvm::StringRef components,
177 llvm::sys::path::Style style) {
178 auto component = llvm::sys::path::begin(components, style);
179 auto e = llvm::sys::path::end(components);
180 while (component != e &&
181 llvm::sys::path::is_separator(*component->data(), style))
182 ++component;
183 for (; component != e; ++component)
184 path.AppendPathComponent(*component);
185}
186
187std::optional<FileSpec> PathMappingList::RemapPath(llvm::StringRef mapping_path,
188 bool only_if_exists) const {
189 if (m_pairs.empty() || mapping_path.empty())
190 return {};
191 LazyBool path_is_relative = eLazyBoolCalculate;
192
193 for (const auto &it : m_pairs) {
194 llvm::StringRef prefix = it.first.GetStringRef();
195 // We create a copy of mapping_path because StringRef::consume_from
196 // effectively modifies the instance itself.
197 llvm::StringRef path = mapping_path;
198 if (!path.consume_front(prefix)) {
199 // Relative paths won't have a leading "./" in them unless "." is the
200 // only thing in the relative path so we need to work around "."
201 // carefully.
202 if (prefix != ".")
203 continue;
204 // We need to figure out if the "path" argument is relative. If it is,
205 // then we should remap, else skip this entry.
206 if (path_is_relative == eLazyBoolCalculate) {
207 path_is_relative =
209 }
210 if (!path_is_relative)
211 continue;
212 }
213 FileSpec remapped(it.second.GetStringRef());
214 auto orig_style = FileSpec::GuessPathStyle(prefix).value_or(
215 llvm::sys::path::Style::native);
216 AppendPathComponents(remapped, path, orig_style);
217 if (!only_if_exists || FileSystem::Instance().Exists(remapped))
218 return remapped;
219 }
220 return {};
221}
222
223std::optional<llvm::StringRef>
225 std::string path = file.GetPath();
226 llvm::StringRef path_ref(path);
227 for (const auto &it : m_pairs) {
228 llvm::StringRef removed_prefix = it.second.GetStringRef();
229 if (!path_ref.consume_front(it.second.GetStringRef()))
230 continue;
231 auto orig_file = it.first.GetStringRef();
232 auto orig_style = FileSpec::GuessPathStyle(orig_file).value_or(
233 llvm::sys::path::Style::native);
234 fixed.SetFile(orig_file, orig_style);
235 AppendPathComponents(fixed, path_ref, orig_style);
236 return removed_prefix;
237 }
238 return std::nullopt;
239}
240
241std::optional<FileSpec>
242PathMappingList::FindFile(const FileSpec &orig_spec) const {
243 // We must normalize the orig_spec again using the host's path style,
244 // otherwise there will be mismatch between the host and remote platform
245 // if they use different path styles.
246 if (auto remapped = RemapPath(NormalizePath(orig_spec.GetPath()),
247 /*only_if_exists=*/true))
248 return remapped;
249
250 return {};
251}
252
253bool PathMappingList::Replace(llvm::StringRef path, llvm::StringRef new_path,
254 bool notify) {
255 uint32_t idx = FindIndexForPath(path);
256 if (idx < m_pairs.size()) {
257 ++m_mod_id;
258 m_pairs[idx].second = ConstString(new_path);
259 if (notify && m_callback)
261 return true;
262 }
263 return false;
264}
265
266bool PathMappingList::Remove(ConstString path, bool notify) {
267 iterator pos = FindIteratorForPath(path);
268 if (pos != m_pairs.end()) {
269 ++m_mod_id;
270 m_pairs.erase(pos);
271 if (notify && m_callback)
273 return true;
274 }
275 return false;
276}
277
280 const_iterator pos;
281 const_iterator begin = m_pairs.begin();
282 const_iterator end = m_pairs.end();
283
284 for (pos = begin; pos != end; ++pos) {
285 if (pos->first == path)
286 break;
287 }
288 return pos;
289}
290
293 iterator pos;
294 iterator begin = m_pairs.begin();
295 iterator end = m_pairs.end();
296
297 for (pos = begin; pos != end; ++pos) {
298 if (pos->first == path)
299 break;
300 }
301 return pos;
302}
303
305 ConstString &new_path) const {
306 if (idx < m_pairs.size()) {
307 path = m_pairs[idx].first;
308 new_path = m_pairs[idx].second;
309 return true;
310 }
311 return false;
312}
313
314uint32_t PathMappingList::FindIndexForPath(llvm::StringRef orig_path) const {
315 const ConstString path = ConstString(NormalizePath(orig_path));
316 const_iterator pos;
317 const_iterator begin = m_pairs.begin();
318 const_iterator end = m_pairs.end();
319
320 for (pos = begin; pos != end; ++pos) {
321 if (pos->first == path)
322 return std::distance(begin, pos);
323 }
324 return UINT32_MAX;
325}
static void AppendPathComponents(FileSpec &path, llvm::StringRef components, llvm::sys::path::Style style)
Append components to path, applying style.
A uniqued constant string class.
Definition: ConstString.h:39
void SetString(const llvm::StringRef &s)
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:201
A file utility class.
Definition: FileSpec.h:56
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:173
void AppendPathComponent(llvm::StringRef component)
Definition: FileSpec.cpp:453
static std::optional< Style > GuessPathStyle(llvm::StringRef absolute_path)
Attempt to guess path style for a given path string.
Definition: FileSpec.cpp:309
bool IsRelative() const
Returns true if the filespec represents a relative path.
Definition: FileSpec.cpp:493
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:366
static FileSystem & Instance()
bool Remove(size_t index, bool notify)
bool AppendUnique(llvm::StringRef path, llvm::StringRef replacement, bool notify)
Append <path, replacement> pair without duplication.
iterator FindIteratorForPath(ConstString path)
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)
uint32_t FindIndexForPath(llvm::StringRef path) const
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 Append(llvm::StringRef path, llvm::StringRef replacement, bool notify)
bool RemapPath(ConstString path, ConstString &new_path) const
bool GetPathsAtIndex(uint32_t idx, ConstString &path, ConstString &new_path) const
collection::iterator iterator
std::pair< ConstString, ConstString > pair
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
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15