1 //===-- PathMappingList.cpp -------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Host/PosixApi.h"
14 #include "lldb/Target/PathMappingList.h"
15 #include "lldb/Utility/FileSpec.h"
16 #include "lldb/Utility/Status.h"
17 #include "lldb/Utility/Stream.h"
18 #include "lldb/lldb-private-enumerations.h"
21 using namespace lldb_private;
24 // We must normalize our path pairs that we store because if we don't then
25 // things won't always work. We found a case where if we did:
26 // (lldb) settings set target.source-map . /tmp
27 // We would store a path pairs of "." and "/tmp" as raw strings. If the debug
28 // info contains "./foo/bar.c", the path will get normalized to "foo/bar.c".
29 // When PathMappingList::RemapPath() is called, it expects the path to start
30 // with the raw path pair, which doesn't work anymore because the paths have
31 // been normalized when the debug info was loaded. So we need to store
32 // nomalized path pairs to ensure things match up.
33 ConstString NormalizePath(ConstString path) {
34 // If we use "path" to construct a FileSpec, it will normalize the path for
35 // us. We then grab the string and turn it back into a ConstString.
36 return ConstString(FileSpec(path.GetStringRef()).GetPath());
39 // PathMappingList constructor
40 PathMappingList::PathMappingList()
41 : m_pairs(), m_callback(nullptr), m_callback_baton(nullptr), m_mod_id(0) {}
43 PathMappingList::PathMappingList(ChangedCallback callback, void *callback_baton)
44 : m_pairs(), m_callback(callback), m_callback_baton(callback_baton),
47 PathMappingList::PathMappingList(const PathMappingList &rhs)
48 : m_pairs(rhs.m_pairs), m_callback(nullptr), m_callback_baton(nullptr),
51 const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) {
53 m_pairs = rhs.m_pairs;
55 m_callback_baton = nullptr;
56 m_mod_id = rhs.m_mod_id;
61 PathMappingList::~PathMappingList() = default;
63 void PathMappingList::Append(ConstString path,
64 ConstString replacement, bool notify) {
66 m_pairs.emplace_back(pair(NormalizePath(path), NormalizePath(replacement)));
67 if (notify && m_callback)
68 m_callback(*this, m_callback_baton);
71 void PathMappingList::Append(const PathMappingList &rhs, bool notify) {
73 if (!rhs.m_pairs.empty()) {
74 const_iterator pos, end = rhs.m_pairs.end();
75 for (pos = rhs.m_pairs.begin(); pos != end; ++pos)
76 m_pairs.push_back(*pos);
77 if (notify && m_callback)
78 m_callback(*this, m_callback_baton);
82 void PathMappingList::Insert(ConstString path,
83 ConstString replacement, uint32_t index,
87 if (index >= m_pairs.size())
88 insert_iter = m_pairs.end();
90 insert_iter = m_pairs.begin() + index;
91 m_pairs.emplace(insert_iter, pair(NormalizePath(path),
92 NormalizePath(replacement)));
93 if (notify && m_callback)
94 m_callback(*this, m_callback_baton);
97 bool PathMappingList::Replace(ConstString path,
98 ConstString replacement, uint32_t index,
100 if (index >= m_pairs.size())
103 m_pairs[index] = pair(NormalizePath(path), NormalizePath(replacement));
104 if (notify && m_callback)
105 m_callback(*this, m_callback_baton);
109 bool PathMappingList::Remove(size_t index, bool notify) {
110 if (index >= m_pairs.size())
114 iterator iter = m_pairs.begin() + index;
116 if (notify && m_callback)
117 m_callback(*this, m_callback_baton);
121 // For clients which do not need the pair index dumped, pass a pair_index >= 0
122 // to only dump the indicated pair.
123 void PathMappingList::Dump(Stream *s, int pair_index) {
124 unsigned int numPairs = m_pairs.size();
126 if (pair_index < 0) {
128 for (index = 0; index < numPairs; ++index)
129 s->Printf("[%d] \"%s\" -> \"%s\"\n", index,
130 m_pairs[index].first.GetCString(),
131 m_pairs[index].second.GetCString());
133 if (static_cast<unsigned int>(pair_index) < numPairs)
134 s->Printf("%s -> %s", m_pairs[pair_index].first.GetCString(),
135 m_pairs[pair_index].second.GetCString());
139 void PathMappingList::Clear(bool notify) {
140 if (!m_pairs.empty())
143 if (notify && m_callback)
144 m_callback(*this, m_callback_baton);
147 bool PathMappingList::RemapPath(ConstString path,
148 ConstString &new_path) const {
149 std::string remapped;
150 if (RemapPath(path.GetStringRef(), remapped)) {
151 new_path.SetString(remapped);
157 bool PathMappingList::RemapPath(llvm::StringRef path,
158 std::string &new_path) const {
159 if (m_pairs.empty() || path.empty())
161 LazyBool path_is_relative = eLazyBoolCalculate;
162 for (const auto &it : m_pairs) {
163 auto prefix = it.first.GetStringRef();
164 if (!path.consume_front(prefix)) {
165 // Relative paths won't have a leading "./" in them unless "." is the
166 // only thing in the relative path so we need to work around "."
170 // We need to figure out if the "path" argument is relative. If it is,
171 // then we should remap, else skip this entry.
172 if (path_is_relative == eLazyBoolCalculate) {
174 FileSpec(path).IsRelative() ? eLazyBoolYes : eLazyBoolNo;
176 if (!path_is_relative)
179 FileSpec remapped(it.second.GetStringRef());
180 remapped.AppendPathComponent(path);
181 new_path = remapped.GetPath();
187 bool PathMappingList::ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const {
188 std::string path = file.GetPath();
189 llvm::StringRef path_ref(path);
190 for (const auto &it : m_pairs) {
191 if (!path_ref.consume_front(it.second.GetStringRef()))
193 fixed.SetFile(it.first.GetStringRef(), FileSpec::Style::native);
194 fixed.AppendPathComponent(path_ref);
200 bool PathMappingList::FindFile(const FileSpec &orig_spec,
201 FileSpec &new_spec) const {
205 std::string orig_path = orig_spec.GetPath();
207 if (orig_path.empty())
210 bool orig_is_relative = orig_spec.IsRelative();
212 for (auto entry : m_pairs) {
213 llvm::StringRef orig_ref(orig_path);
214 llvm::StringRef prefix_ref = entry.first.GetStringRef();
215 if (orig_ref.size() < prefix_ref.size())
217 // We consider a relative prefix or one of just "." to
218 // mean "only apply to relative paths".
219 bool prefix_is_relative = false;
221 if (prefix_ref == ".") {
222 prefix_is_relative = true;
223 // Remove the "." since it will have been removed from the
224 // FileSpec paths already.
225 prefix_ref = prefix_ref.drop_front();
227 FileSpec prefix_spec(prefix_ref, FileSpec::Style::native);
228 prefix_is_relative = prefix_spec.IsRelative();
230 if (prefix_is_relative != orig_is_relative)
233 if (orig_ref.consume_front(prefix_ref)) {
234 new_spec.SetFile(entry.second.GetCString(), FileSpec::Style::native);
235 new_spec.AppendPathComponent(orig_ref);
236 if (FileSystem::Instance().Exists(new_spec))
245 bool PathMappingList::Replace(ConstString path,
246 ConstString new_path, bool notify) {
247 uint32_t idx = FindIndexForPath(path);
248 if (idx < m_pairs.size()) {
250 m_pairs[idx].second = new_path;
251 if (notify && m_callback)
252 m_callback(*this, m_callback_baton);
258 bool PathMappingList::Remove(ConstString path, bool notify) {
259 iterator pos = FindIteratorForPath(path);
260 if (pos != m_pairs.end()) {
263 if (notify && m_callback)
264 m_callback(*this, m_callback_baton);
270 PathMappingList::const_iterator
271 PathMappingList::FindIteratorForPath(ConstString path) const {
273 const_iterator begin = m_pairs.begin();
274 const_iterator end = m_pairs.end();
276 for (pos = begin; pos != end; ++pos) {
277 if (pos->first == path)
283 PathMappingList::iterator
284 PathMappingList::FindIteratorForPath(ConstString path) {
286 iterator begin = m_pairs.begin();
287 iterator end = m_pairs.end();
289 for (pos = begin; pos != end; ++pos) {
290 if (pos->first == path)
296 bool PathMappingList::GetPathsAtIndex(uint32_t idx, ConstString &path,
297 ConstString &new_path) const {
298 if (idx < m_pairs.size()) {
299 path = m_pairs[idx].first;
300 new_path = m_pairs[idx].second;
306 uint32_t PathMappingList::FindIndexForPath(ConstString orig_path) const {
307 const ConstString path = NormalizePath(orig_path);
309 const_iterator begin = m_pairs.begin();
310 const_iterator end = m_pairs.end();
312 for (pos = begin; pos != end; ++pos) {
313 if (pos->first == path)
314 return std::distance(begin, pos);