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 
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Host/PosixApi.h"
15 #include "lldb/Utility/FileSpec.h"
16 #include "lldb/Utility/Status.h"
17 #include "lldb/Utility/Stream.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 namespace {
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 std::string NormalizePath(llvm::StringRef path) {
34  // If we use "path" to construct a FileSpec, it will normalize the path for
35  // us. We then grab the string.
36  return FileSpec(path).GetPath();
37 }
38 }
39 // PathMappingList constructor
40 PathMappingList::PathMappingList() : m_pairs() {}
41 
42 PathMappingList::PathMappingList(ChangedCallback callback, void *callback_baton)
43  : m_pairs(), m_callback(callback), m_callback_baton(callback_baton) {}
44 
46  : m_pairs(rhs.m_pairs) {}
47 
49  if (this != &rhs) {
50  m_pairs = rhs.m_pairs;
51  m_callback = nullptr;
52  m_callback_baton = nullptr;
53  m_mod_id = rhs.m_mod_id;
54  }
55  return *this;
56 }
57 
59 
60 void PathMappingList::Append(llvm::StringRef path, llvm::StringRef replacement,
61  bool notify) {
62  ++m_mod_id;
63  m_pairs.emplace_back(pair(NormalizePath(path), NormalizePath(replacement)));
64  if (notify && m_callback)
66 }
67 
68 void PathMappingList::Append(const PathMappingList &rhs, bool notify) {
69  ++m_mod_id;
70  if (!rhs.m_pairs.empty()) {
71  const_iterator pos, end = rhs.m_pairs.end();
72  for (pos = rhs.m_pairs.begin(); pos != end; ++pos)
73  m_pairs.push_back(*pos);
74  if (notify && m_callback)
76  }
77 }
78 
79 bool PathMappingList::AppendUnique(llvm::StringRef path,
80  llvm::StringRef replacement, bool notify) {
81  auto normalized_path = NormalizePath(path);
82  auto normalized_replacement = NormalizePath(replacement);
83  for (const auto &pair : m_pairs) {
84  if (pair.first.GetStringRef().equals(normalized_path) &&
85  pair.second.GetStringRef().equals(normalized_replacement))
86  return false;
87  }
88  Append(path, replacement, notify);
89  return true;
90 }
91 
92 void PathMappingList::Insert(llvm::StringRef path, llvm::StringRef replacement,
93  uint32_t index, bool notify) {
94  ++m_mod_id;
95  iterator insert_iter;
96  if (index >= m_pairs.size())
97  insert_iter = m_pairs.end();
98  else
99  insert_iter = m_pairs.begin() + index;
100  m_pairs.emplace(insert_iter, pair(NormalizePath(path),
101  NormalizePath(replacement)));
102  if (notify && m_callback)
104 }
105 
106 bool PathMappingList::Replace(llvm::StringRef path, llvm::StringRef replacement,
107  uint32_t index, bool notify) {
108  if (index >= m_pairs.size())
109  return false;
110  ++m_mod_id;
111  m_pairs[index] = pair(NormalizePath(path), NormalizePath(replacement));
112  if (notify && m_callback)
114  return true;
115 }
116 
117 bool PathMappingList::Remove(size_t index, bool notify) {
118  if (index >= m_pairs.size())
119  return false;
120 
121  ++m_mod_id;
122  iterator iter = m_pairs.begin() + index;
123  m_pairs.erase(iter);
124  if (notify && m_callback)
126  return true;
127 }
128 
129 // For clients which do not need the pair index dumped, pass a pair_index >= 0
130 // to only dump the indicated pair.
131 void PathMappingList::Dump(Stream *s, int pair_index) {
132  unsigned int numPairs = m_pairs.size();
133 
134  if (pair_index < 0) {
135  unsigned int index;
136  for (index = 0; index < numPairs; ++index)
137  s->Printf("[%d] \"%s\" -> \"%s\"\n", index,
138  m_pairs[index].first.GetCString(),
139  m_pairs[index].second.GetCString());
140  } else {
141  if (static_cast<unsigned int>(pair_index) < numPairs)
142  s->Printf("%s -> %s", m_pairs[pair_index].first.GetCString(),
143  m_pairs[pair_index].second.GetCString());
144  }
145 }
146 
147 llvm::json::Value PathMappingList::ToJSON() {
148  llvm::json::Array entries;
149  for (const auto &pair : m_pairs) {
150  llvm::json::Array entry{pair.first.GetStringRef().str(),
151  pair.second.GetStringRef().str()};
152  entries.emplace_back(std::move(entry));
153  }
154  return entries;
155 }
156 
157 void PathMappingList::Clear(bool notify) {
158  if (!m_pairs.empty())
159  ++m_mod_id;
160  m_pairs.clear();
161  if (notify && m_callback)
163 }
164 
166  ConstString &new_path) const {
167  if (llvm::Optional<FileSpec> remapped = RemapPath(path.GetStringRef())) {
168  new_path.SetString(remapped->GetPath());
169  return true;
170  }
171  return false;
172 }
173 
174 /// Append components to path, applying style.
175 static void AppendPathComponents(FileSpec &path, llvm::StringRef components,
176  llvm::sys::path::Style style) {
177  auto component = llvm::sys::path::begin(components, style);
178  auto e = llvm::sys::path::end(components);
179  while (component != e &&
180  llvm::sys::path::is_separator(*component->data(), style))
181  ++component;
182  for (; component != e; ++component)
183  path.AppendPathComponent(*component);
184 }
185 
186 llvm::Optional<FileSpec>
187 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 
223 llvm::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 llvm::None;
239 }
240 
241 llvm::Optional<FileSpec> PathMappingList::FindFile(const FileSpec &orig_spec) const {
242  // We must normalize the orig_spec again using the host's path style,
243  // otherwise there will be mismatch between the host and remote platform
244  // if they use different path styles.
245  if (auto remapped = RemapPath(NormalizePath(orig_spec.GetPath()),
246  /*only_if_exists=*/true))
247  return remapped;
248 
249  return {};
250 }
251 
252 bool PathMappingList::Replace(llvm::StringRef path, llvm::StringRef new_path,
253  bool notify) {
254  uint32_t idx = FindIndexForPath(path);
255  if (idx < m_pairs.size()) {
256  ++m_mod_id;
257  m_pairs[idx].second = ConstString(new_path);
258  if (notify && m_callback)
260  return true;
261  }
262  return false;
263 }
264 
265 bool PathMappingList::Remove(ConstString path, bool notify) {
266  iterator pos = FindIteratorForPath(path);
267  if (pos != m_pairs.end()) {
268  ++m_mod_id;
269  m_pairs.erase(pos);
270  if (notify && m_callback)
272  return true;
273  }
274  return false;
275 }
276 
279  const_iterator pos;
280  const_iterator begin = m_pairs.begin();
281  const_iterator end = m_pairs.end();
282 
283  for (pos = begin; pos != end; ++pos) {
284  if (pos->first == path)
285  break;
286  }
287  return pos;
288 }
289 
292  iterator pos;
293  iterator begin = m_pairs.begin();
294  iterator end = m_pairs.end();
295 
296  for (pos = begin; pos != end; ++pos) {
297  if (pos->first == path)
298  break;
299  }
300  return pos;
301 }
302 
304  ConstString &new_path) const {
305  if (idx < m_pairs.size()) {
306  path = m_pairs[idx].first;
307  new_path = m_pairs[idx].second;
308  return true;
309  }
310  return false;
311 }
312 
313 uint32_t PathMappingList::FindIndexForPath(llvm::StringRef orig_path) const {
314  const ConstString path = ConstString(NormalizePath(orig_path));
315  const_iterator pos;
316  const_iterator begin = m_pairs.begin();
317  const_iterator end = m_pairs.end();
318 
319  for (pos = begin; pos != end; ++pos) {
320  if (pos->first == path)
321  return std::distance(begin, pos);
322  }
323  return UINT32_MAX;
324 }
lldb_private::PathMappingList::iterator
collection::iterator iterator
Definition: PathMappingList.h:135
FileSystem.h
lldb_private::PathMappingList::const_iterator
collection::const_iterator const_iterator
Definition: PathMappingList.h:136
lldb_private::PathMappingList::GetPathsAtIndex
bool GetPathsAtIndex(uint32_t idx, ConstString &path, ConstString &new_path) const
Definition: PathMappingList.cpp:303
lldb_private::eLazyBoolYes
@ eLazyBoolYes
Definition: lldb-private-enumerations.h:115
lldb_private::PathMappingList::AppendUnique
bool AppendUnique(llvm::StringRef path, llvm::StringRef replacement, bool notify)
Append <path, replacement> pair without duplication.
Definition: PathMappingList.cpp:79
lldb_private::PathMappingList::FindIndexForPath
uint32_t FindIndexForPath(llvm::StringRef path) const
Definition: PathMappingList.cpp:313
lldb_private::Stream
Definition: Stream.h:28
lldb_private::LazyBool
LazyBool
Definition: lldb-private-enumerations.h:115
lldb_private::PathMappingList::Insert
void Insert(llvm::StringRef path, llvm::StringRef replacement, uint32_t insert_idx, bool notify)
Definition: PathMappingList.cpp:92
lldb-private-enumerations.h
lldb_private::PathMappingList::~PathMappingList
~PathMappingList()
lldb_private::PathMappingList::PathMappingList
PathMappingList()
Definition: PathMappingList.cpp:40
lldb_private::FileSpec
Definition: FileSpec.h:55
lldb_private::ConstString::SetString
void SetString(const llvm::StringRef &s)
Definition: ConstString.cpp:305
lldb_private::ConstString::GetStringRef
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:201
lldb_private::PathMappingList::m_mod_id
uint32_t m_mod_id
Definition: PathMappingList.h:145
lldb_private::ConstString
Definition: ConstString.h:39
lldb_private::PathMappingList::ReverseRemapPath
llvm::Optional< llvm::StringRef > ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const
Perform reverse source path remap for input file.
Definition: PathMappingList.cpp:224
lldb_private::PathMappingList::ToJSON
llvm::json::Value ToJSON()
Definition: PathMappingList.cpp:147
lldb_private::PathMappingList::Replace
bool Replace(llvm::StringRef path, llvm::StringRef replacement, bool notify)
Definition: PathMappingList.cpp:252
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::PathMappingList::Append
void Append(llvm::StringRef path, llvm::StringRef replacement, bool notify)
Definition: PathMappingList.cpp:60
lldb_private::PathMappingList::m_pairs
collection m_pairs
Definition: PathMappingList.h:142
lldb_private::FileSpec::GuessPathStyle
static llvm::Optional< Style > GuessPathStyle(llvm::StringRef absolute_path)
Attempt to guess path style for a given path string.
Definition: FileSpec.cpp:307
lldb_private::PathMappingList::FindFile
llvm::Optional< FileSpec > FindFile(const FileSpec &orig_spec) const
Finds a source file given a file spec using the path remappings.
Definition: PathMappingList.cpp:241
lldb_private::FileSpec::IsRelative
bool IsRelative() const
Returns true if the filespec represents a relative path.
Definition: FileSpec.cpp:491
lldb_private::PathMappingList::operator=
const PathMappingList & operator=(const PathMappingList &rhs)
Definition: PathMappingList.cpp:48
uint32_t
lldb_private::eLazyBoolNo
@ eLazyBoolNo
Definition: lldb-private-enumerations.h:115
lldb_private::PathMappingList
Definition: PathMappingList.h:20
lldb_private::PathMappingList::m_callback
ChangedCallback m_callback
Definition: PathMappingList.h:143
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:19
lldb_private::PathMappingList::pair
std::pair< ConstString, ConstString > pair
Definition: PathMappingList.h:133
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::FileSystem::Instance
static FileSystem & Instance()
Definition: common/FileSystem.cpp:46
Status.h
lldb_private::PathMappingList::m_callback_baton
void * m_callback_baton
Definition: PathMappingList.h:144
lldb_private::FileSpec::AppendPathComponent
void AppendPathComponent(llvm::StringRef component)
Definition: FileSpec.cpp:451
lldb_private::PathMappingList::Dump
void Dump(Stream *s, int pair_index=-1)
Definition: PathMappingList.cpp:131
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::eLazyBoolCalculate
@ eLazyBoolCalculate
Definition: lldb-private-enumerations.h:115
PosixApi.h
lldb_private::PathMappingList::FindIteratorForPath
iterator FindIteratorForPath(ConstString path)
Definition: PathMappingList.cpp:291
FileSpec.h
Stream.h
lldb_private::FileSpec::SetFile
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:172
lldb_private::PathMappingList::Remove
bool Remove(size_t index, bool notify)
Definition: PathMappingList.cpp:117
lldb_private::PathMappingList::Clear
void Clear(bool notify)
Definition: PathMappingList.cpp:157
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:364
PathMappingList.h
lldb
Definition: SBAddress.h:15
AppendPathComponents
static void AppendPathComponents(FileSpec &path, llvm::StringRef components, llvm::sys::path::Style style)
Append components to path, applying style.
Definition: PathMappingList.cpp:175
lldb_private::PathMappingList::RemapPath
bool RemapPath(ConstString path, ConstString &new_path) const
Definition: PathMappingList.cpp:165