xref: /openbsd-src/gnu/llvm/lldb/source/Target/PathMappingList.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1dda28197Spatrick //===-- PathMappingList.cpp -----------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include <climits>
10061da546Spatrick #include <cstring>
11*f6aab3d8Srobert #include <optional>
12061da546Spatrick 
13061da546Spatrick #include "lldb/Host/FileSystem.h"
14061da546Spatrick #include "lldb/Host/PosixApi.h"
15061da546Spatrick #include "lldb/Target/PathMappingList.h"
16061da546Spatrick #include "lldb/Utility/FileSpec.h"
17061da546Spatrick #include "lldb/Utility/Status.h"
18061da546Spatrick #include "lldb/Utility/Stream.h"
19061da546Spatrick #include "lldb/lldb-private-enumerations.h"
20061da546Spatrick 
21061da546Spatrick using namespace lldb;
22061da546Spatrick using namespace lldb_private;
23061da546Spatrick 
24061da546Spatrick namespace {
25061da546Spatrick   // We must normalize our path pairs that we store because if we don't then
26061da546Spatrick   // things won't always work. We found a case where if we did:
27061da546Spatrick   // (lldb) settings set target.source-map . /tmp
28061da546Spatrick   // We would store a path pairs of "." and "/tmp" as raw strings. If the debug
29061da546Spatrick   // info contains "./foo/bar.c", the path will get normalized to "foo/bar.c".
30061da546Spatrick   // When PathMappingList::RemapPath() is called, it expects the path to start
31061da546Spatrick   // with the raw path pair, which doesn't work anymore because the paths have
32061da546Spatrick   // been normalized when the debug info was loaded. So we need to store
33061da546Spatrick   // nomalized path pairs to ensure things match up.
NormalizePath(llvm::StringRef path)34*f6aab3d8Srobert std::string NormalizePath(llvm::StringRef path) {
35061da546Spatrick   // If we use "path" to construct a FileSpec, it will normalize the path for
36*f6aab3d8Srobert   // us. We then grab the string.
37*f6aab3d8Srobert   return FileSpec(path).GetPath();
38061da546Spatrick }
39061da546Spatrick }
40061da546Spatrick // PathMappingList constructor
PathMappingList()41be691f3bSpatrick PathMappingList::PathMappingList() : m_pairs() {}
42061da546Spatrick 
PathMappingList(ChangedCallback callback,void * callback_baton)43061da546Spatrick PathMappingList::PathMappingList(ChangedCallback callback, void *callback_baton)
44*f6aab3d8Srobert     : m_pairs(), m_callback(callback), m_callback_baton(callback_baton) {}
45061da546Spatrick 
PathMappingList(const PathMappingList & rhs)46061da546Spatrick PathMappingList::PathMappingList(const PathMappingList &rhs)
47*f6aab3d8Srobert     : m_pairs(rhs.m_pairs) {}
48061da546Spatrick 
operator =(const PathMappingList & rhs)49061da546Spatrick const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) {
50061da546Spatrick   if (this != &rhs) {
51061da546Spatrick     m_pairs = rhs.m_pairs;
52061da546Spatrick     m_callback = nullptr;
53061da546Spatrick     m_callback_baton = nullptr;
54061da546Spatrick     m_mod_id = rhs.m_mod_id;
55061da546Spatrick   }
56061da546Spatrick   return *this;
57061da546Spatrick }
58061da546Spatrick 
59061da546Spatrick PathMappingList::~PathMappingList() = default;
60061da546Spatrick 
Append(llvm::StringRef path,llvm::StringRef replacement,bool notify)61*f6aab3d8Srobert void PathMappingList::Append(llvm::StringRef path, llvm::StringRef replacement,
62*f6aab3d8Srobert                              bool notify) {
63061da546Spatrick   ++m_mod_id;
64061da546Spatrick   m_pairs.emplace_back(pair(NormalizePath(path), NormalizePath(replacement)));
65061da546Spatrick   if (notify && m_callback)
66061da546Spatrick     m_callback(*this, m_callback_baton);
67061da546Spatrick }
68061da546Spatrick 
Append(const PathMappingList & rhs,bool notify)69061da546Spatrick void PathMappingList::Append(const PathMappingList &rhs, bool notify) {
70061da546Spatrick   ++m_mod_id;
71061da546Spatrick   if (!rhs.m_pairs.empty()) {
72061da546Spatrick     const_iterator pos, end = rhs.m_pairs.end();
73061da546Spatrick     for (pos = rhs.m_pairs.begin(); pos != end; ++pos)
74061da546Spatrick       m_pairs.push_back(*pos);
75061da546Spatrick     if (notify && m_callback)
76061da546Spatrick       m_callback(*this, m_callback_baton);
77061da546Spatrick   }
78061da546Spatrick }
79061da546Spatrick 
AppendUnique(llvm::StringRef path,llvm::StringRef replacement,bool notify)80*f6aab3d8Srobert bool PathMappingList::AppendUnique(llvm::StringRef path,
81*f6aab3d8Srobert                                    llvm::StringRef replacement, bool notify) {
82*f6aab3d8Srobert   auto normalized_path = NormalizePath(path);
83*f6aab3d8Srobert   auto normalized_replacement = NormalizePath(replacement);
84*f6aab3d8Srobert   for (const auto &pair : m_pairs) {
85*f6aab3d8Srobert     if (pair.first.GetStringRef().equals(normalized_path) &&
86*f6aab3d8Srobert         pair.second.GetStringRef().equals(normalized_replacement))
87*f6aab3d8Srobert       return false;
88*f6aab3d8Srobert   }
89*f6aab3d8Srobert   Append(path, replacement, notify);
90*f6aab3d8Srobert   return true;
91*f6aab3d8Srobert }
92*f6aab3d8Srobert 
Insert(llvm::StringRef path,llvm::StringRef replacement,uint32_t index,bool notify)93*f6aab3d8Srobert void PathMappingList::Insert(llvm::StringRef path, llvm::StringRef replacement,
94*f6aab3d8Srobert                              uint32_t index, bool notify) {
95061da546Spatrick   ++m_mod_id;
96061da546Spatrick   iterator insert_iter;
97061da546Spatrick   if (index >= m_pairs.size())
98061da546Spatrick     insert_iter = m_pairs.end();
99061da546Spatrick   else
100061da546Spatrick     insert_iter = m_pairs.begin() + index;
101061da546Spatrick   m_pairs.emplace(insert_iter, pair(NormalizePath(path),
102061da546Spatrick                                     NormalizePath(replacement)));
103061da546Spatrick   if (notify && m_callback)
104061da546Spatrick     m_callback(*this, m_callback_baton);
105061da546Spatrick }
106061da546Spatrick 
Replace(llvm::StringRef path,llvm::StringRef replacement,uint32_t index,bool notify)107*f6aab3d8Srobert bool PathMappingList::Replace(llvm::StringRef path, llvm::StringRef replacement,
108*f6aab3d8Srobert                               uint32_t index, bool notify) {
109061da546Spatrick   if (index >= m_pairs.size())
110061da546Spatrick     return false;
111061da546Spatrick   ++m_mod_id;
112061da546Spatrick   m_pairs[index] = pair(NormalizePath(path), NormalizePath(replacement));
113061da546Spatrick   if (notify && m_callback)
114061da546Spatrick     m_callback(*this, m_callback_baton);
115061da546Spatrick   return true;
116061da546Spatrick }
117061da546Spatrick 
Remove(size_t index,bool notify)118061da546Spatrick bool PathMappingList::Remove(size_t index, bool notify) {
119061da546Spatrick   if (index >= m_pairs.size())
120061da546Spatrick     return false;
121061da546Spatrick 
122061da546Spatrick   ++m_mod_id;
123061da546Spatrick   iterator iter = m_pairs.begin() + index;
124061da546Spatrick   m_pairs.erase(iter);
125061da546Spatrick   if (notify && m_callback)
126061da546Spatrick     m_callback(*this, m_callback_baton);
127061da546Spatrick   return true;
128061da546Spatrick }
129061da546Spatrick 
130061da546Spatrick // For clients which do not need the pair index dumped, pass a pair_index >= 0
131061da546Spatrick // to only dump the indicated pair.
Dump(Stream * s,int pair_index)132061da546Spatrick void PathMappingList::Dump(Stream *s, int pair_index) {
133061da546Spatrick   unsigned int numPairs = m_pairs.size();
134061da546Spatrick 
135061da546Spatrick   if (pair_index < 0) {
136061da546Spatrick     unsigned int index;
137061da546Spatrick     for (index = 0; index < numPairs; ++index)
138061da546Spatrick       s->Printf("[%d] \"%s\" -> \"%s\"\n", index,
139061da546Spatrick                 m_pairs[index].first.GetCString(),
140061da546Spatrick                 m_pairs[index].second.GetCString());
141061da546Spatrick   } else {
142061da546Spatrick     if (static_cast<unsigned int>(pair_index) < numPairs)
143061da546Spatrick       s->Printf("%s -> %s", m_pairs[pair_index].first.GetCString(),
144061da546Spatrick                 m_pairs[pair_index].second.GetCString());
145061da546Spatrick   }
146061da546Spatrick }
147061da546Spatrick 
ToJSON()148*f6aab3d8Srobert llvm::json::Value PathMappingList::ToJSON() {
149*f6aab3d8Srobert   llvm::json::Array entries;
150*f6aab3d8Srobert   for (const auto &pair : m_pairs) {
151*f6aab3d8Srobert     llvm::json::Array entry{pair.first.GetStringRef().str(),
152*f6aab3d8Srobert                             pair.second.GetStringRef().str()};
153*f6aab3d8Srobert     entries.emplace_back(std::move(entry));
154*f6aab3d8Srobert   }
155*f6aab3d8Srobert   return entries;
156*f6aab3d8Srobert }
157*f6aab3d8Srobert 
Clear(bool notify)158061da546Spatrick void PathMappingList::Clear(bool notify) {
159061da546Spatrick   if (!m_pairs.empty())
160061da546Spatrick     ++m_mod_id;
161061da546Spatrick   m_pairs.clear();
162061da546Spatrick   if (notify && m_callback)
163061da546Spatrick     m_callback(*this, m_callback_baton);
164061da546Spatrick }
165061da546Spatrick 
RemapPath(ConstString path,ConstString & new_path) const166061da546Spatrick bool PathMappingList::RemapPath(ConstString path,
167061da546Spatrick                                 ConstString &new_path) const {
168*f6aab3d8Srobert   if (std::optional<FileSpec> remapped = RemapPath(path.GetStringRef())) {
169be691f3bSpatrick     new_path.SetString(remapped->GetPath());
170061da546Spatrick     return true;
171061da546Spatrick   }
172061da546Spatrick   return false;
173061da546Spatrick }
174061da546Spatrick 
175be691f3bSpatrick /// Append components to path, applying style.
AppendPathComponents(FileSpec & path,llvm::StringRef components,llvm::sys::path::Style style)176be691f3bSpatrick static void AppendPathComponents(FileSpec &path, llvm::StringRef components,
177be691f3bSpatrick                                  llvm::sys::path::Style style) {
178be691f3bSpatrick   auto component = llvm::sys::path::begin(components, style);
179be691f3bSpatrick   auto e = llvm::sys::path::end(components);
180be691f3bSpatrick   while (component != e &&
181be691f3bSpatrick          llvm::sys::path::is_separator(*component->data(), style))
182be691f3bSpatrick     ++component;
183be691f3bSpatrick   for (; component != e; ++component)
184be691f3bSpatrick     path.AppendPathComponent(*component);
185be691f3bSpatrick }
186be691f3bSpatrick 
RemapPath(llvm::StringRef mapping_path,bool only_if_exists) const187*f6aab3d8Srobert std::optional<FileSpec> PathMappingList::RemapPath(llvm::StringRef mapping_path,
188be691f3bSpatrick                                                    bool only_if_exists) const {
189be691f3bSpatrick   if (m_pairs.empty() || mapping_path.empty())
190be691f3bSpatrick     return {};
191061da546Spatrick   LazyBool path_is_relative = eLazyBoolCalculate;
192be691f3bSpatrick 
193061da546Spatrick   for (const auto &it : m_pairs) {
194be691f3bSpatrick     llvm::StringRef prefix = it.first.GetStringRef();
195be691f3bSpatrick     // We create a copy of mapping_path because StringRef::consume_from
196be691f3bSpatrick     // effectively modifies the instance itself.
197be691f3bSpatrick     llvm::StringRef path = mapping_path;
198061da546Spatrick     if (!path.consume_front(prefix)) {
199061da546Spatrick       // Relative paths won't have a leading "./" in them unless "." is the
200061da546Spatrick       // only thing in the relative path so we need to work around "."
201061da546Spatrick       // carefully.
202061da546Spatrick       if (prefix != ".")
203061da546Spatrick         continue;
204061da546Spatrick       // We need to figure out if the "path" argument is relative. If it is,
205061da546Spatrick       // then we should remap, else skip this entry.
206061da546Spatrick       if (path_is_relative == eLazyBoolCalculate) {
207061da546Spatrick         path_is_relative =
208061da546Spatrick             FileSpec(path).IsRelative() ? eLazyBoolYes : eLazyBoolNo;
209061da546Spatrick       }
210061da546Spatrick       if (!path_is_relative)
211061da546Spatrick         continue;
212061da546Spatrick     }
213061da546Spatrick     FileSpec remapped(it.second.GetStringRef());
214*f6aab3d8Srobert     auto orig_style = FileSpec::GuessPathStyle(prefix).value_or(
215be691f3bSpatrick         llvm::sys::path::Style::native);
216be691f3bSpatrick     AppendPathComponents(remapped, path, orig_style);
217be691f3bSpatrick     if (!only_if_exists || FileSystem::Instance().Exists(remapped))
218be691f3bSpatrick       return remapped;
219061da546Spatrick   }
220be691f3bSpatrick   return {};
221061da546Spatrick }
222061da546Spatrick 
223*f6aab3d8Srobert std::optional<llvm::StringRef>
ReverseRemapPath(const FileSpec & file,FileSpec & fixed) const224*f6aab3d8Srobert PathMappingList::ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const {
225061da546Spatrick   std::string path = file.GetPath();
226061da546Spatrick   llvm::StringRef path_ref(path);
227061da546Spatrick   for (const auto &it : m_pairs) {
228*f6aab3d8Srobert     llvm::StringRef removed_prefix = it.second.GetStringRef();
229061da546Spatrick     if (!path_ref.consume_front(it.second.GetStringRef()))
230061da546Spatrick       continue;
231be691f3bSpatrick     auto orig_file = it.first.GetStringRef();
232*f6aab3d8Srobert     auto orig_style = FileSpec::GuessPathStyle(orig_file).value_or(
233be691f3bSpatrick         llvm::sys::path::Style::native);
234be691f3bSpatrick     fixed.SetFile(orig_file, orig_style);
235be691f3bSpatrick     AppendPathComponents(fixed, path_ref, orig_style);
236*f6aab3d8Srobert     return removed_prefix;
237061da546Spatrick   }
238*f6aab3d8Srobert   return std::nullopt;
239061da546Spatrick }
240061da546Spatrick 
241*f6aab3d8Srobert std::optional<FileSpec>
FindFile(const FileSpec & orig_spec) const242*f6aab3d8Srobert PathMappingList::FindFile(const FileSpec &orig_spec) const {
243*f6aab3d8Srobert   // We must normalize the orig_spec again using the host's path style,
244*f6aab3d8Srobert   // otherwise there will be mismatch between the host and remote platform
245*f6aab3d8Srobert   // if they use different path styles.
246*f6aab3d8Srobert   if (auto remapped = RemapPath(NormalizePath(orig_spec.GetPath()),
247*f6aab3d8Srobert                                 /*only_if_exists=*/true))
248be691f3bSpatrick     return remapped;
249061da546Spatrick 
250be691f3bSpatrick   return {};
251061da546Spatrick }
252061da546Spatrick 
Replace(llvm::StringRef path,llvm::StringRef new_path,bool notify)253*f6aab3d8Srobert bool PathMappingList::Replace(llvm::StringRef path, llvm::StringRef new_path,
254*f6aab3d8Srobert                               bool notify) {
255061da546Spatrick   uint32_t idx = FindIndexForPath(path);
256061da546Spatrick   if (idx < m_pairs.size()) {
257061da546Spatrick     ++m_mod_id;
258*f6aab3d8Srobert     m_pairs[idx].second = ConstString(new_path);
259061da546Spatrick     if (notify && m_callback)
260061da546Spatrick       m_callback(*this, m_callback_baton);
261061da546Spatrick     return true;
262061da546Spatrick   }
263061da546Spatrick   return false;
264061da546Spatrick }
265061da546Spatrick 
Remove(ConstString path,bool notify)266061da546Spatrick bool PathMappingList::Remove(ConstString path, bool notify) {
267061da546Spatrick   iterator pos = FindIteratorForPath(path);
268061da546Spatrick   if (pos != m_pairs.end()) {
269061da546Spatrick     ++m_mod_id;
270061da546Spatrick     m_pairs.erase(pos);
271061da546Spatrick     if (notify && m_callback)
272061da546Spatrick       m_callback(*this, m_callback_baton);
273061da546Spatrick     return true;
274061da546Spatrick   }
275061da546Spatrick   return false;
276061da546Spatrick }
277061da546Spatrick 
278061da546Spatrick PathMappingList::const_iterator
FindIteratorForPath(ConstString path) const279061da546Spatrick PathMappingList::FindIteratorForPath(ConstString path) const {
280061da546Spatrick   const_iterator pos;
281061da546Spatrick   const_iterator begin = m_pairs.begin();
282061da546Spatrick   const_iterator end = m_pairs.end();
283061da546Spatrick 
284061da546Spatrick   for (pos = begin; pos != end; ++pos) {
285061da546Spatrick     if (pos->first == path)
286061da546Spatrick       break;
287061da546Spatrick   }
288061da546Spatrick   return pos;
289061da546Spatrick }
290061da546Spatrick 
291061da546Spatrick PathMappingList::iterator
FindIteratorForPath(ConstString path)292061da546Spatrick PathMappingList::FindIteratorForPath(ConstString path) {
293061da546Spatrick   iterator pos;
294061da546Spatrick   iterator begin = m_pairs.begin();
295061da546Spatrick   iterator end = m_pairs.end();
296061da546Spatrick 
297061da546Spatrick   for (pos = begin; pos != end; ++pos) {
298061da546Spatrick     if (pos->first == path)
299061da546Spatrick       break;
300061da546Spatrick   }
301061da546Spatrick   return pos;
302061da546Spatrick }
303061da546Spatrick 
GetPathsAtIndex(uint32_t idx,ConstString & path,ConstString & new_path) const304061da546Spatrick bool PathMappingList::GetPathsAtIndex(uint32_t idx, ConstString &path,
305061da546Spatrick                                       ConstString &new_path) const {
306061da546Spatrick   if (idx < m_pairs.size()) {
307061da546Spatrick     path = m_pairs[idx].first;
308061da546Spatrick     new_path = m_pairs[idx].second;
309061da546Spatrick     return true;
310061da546Spatrick   }
311061da546Spatrick   return false;
312061da546Spatrick }
313061da546Spatrick 
FindIndexForPath(llvm::StringRef orig_path) const314*f6aab3d8Srobert uint32_t PathMappingList::FindIndexForPath(llvm::StringRef orig_path) const {
315*f6aab3d8Srobert   const ConstString path = ConstString(NormalizePath(orig_path));
316061da546Spatrick   const_iterator pos;
317061da546Spatrick   const_iterator begin = m_pairs.begin();
318061da546Spatrick   const_iterator end = m_pairs.end();
319061da546Spatrick 
320061da546Spatrick   for (pos = begin; pos != end; ++pos) {
321061da546Spatrick     if (pos->first == path)
322061da546Spatrick       return std::distance(begin, pos);
323061da546Spatrick   }
324061da546Spatrick   return UINT32_MAX;
325061da546Spatrick }
326