1 //===-- PathMappingList.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
15 // Other libraries and framework includes
17 #include "lldb/lldb-private-enumerations.h"
18 #include "lldb/Host/PosixApi.h"
19 #include "lldb/Target/PathMappingList.h"
20 #include "lldb/Utility/FileSpec.h"
21 #include "lldb/Utility/Status.h"
22 #include "lldb/Utility/Stream.h"
25 using namespace lldb_private;
28 // We must normalize our path pairs that we store because if we don't then
29 // things won't always work. We found a case where if we did:
30 // (lldb) settings set target.source-map . /tmp
31 // We would store a path pairs of "." and "/tmp" as raw strings. If the debug
32 // info contains "./foo/bar.c", the path will get normalized to "foo/bar.c".
33 // When PathMappingList::RemapPath() is called, it expects the path to start
34 // with the raw path pair, which doesn't work anymore because the paths have
35 // been normalized when the debug info was loaded. So we need to store
36 // nomalized path pairs to ensure things match up.
37 ConstString NormalizePath(const ConstString &path) {
38 // If we use "path" to construct a FileSpec, it will normalize the path for
39 // us. We then grab the string and turn it back into a ConstString.
40 return ConstString(FileSpec(path.GetStringRef(), false).GetPath());
43 //----------------------------------------------------------------------
44 // PathMappingList constructor
45 //----------------------------------------------------------------------
46 PathMappingList::PathMappingList()
47 : m_pairs(), m_callback(nullptr), m_callback_baton(nullptr), m_mod_id(0) {}
49 PathMappingList::PathMappingList(ChangedCallback callback, void *callback_baton)
50 : m_pairs(), m_callback(callback), m_callback_baton(callback_baton),
53 PathMappingList::PathMappingList(const PathMappingList &rhs)
54 : m_pairs(rhs.m_pairs), m_callback(nullptr), m_callback_baton(nullptr),
57 const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) {
59 m_pairs = rhs.m_pairs;
61 m_callback_baton = nullptr;
62 m_mod_id = rhs.m_mod_id;
67 PathMappingList::~PathMappingList() = default;
69 void PathMappingList::Append(const ConstString &path,
70 const ConstString &replacement, bool notify) {
72 m_pairs.emplace_back(pair(NormalizePath(path), NormalizePath(replacement)));
73 if (notify && m_callback)
74 m_callback(*this, m_callback_baton);
77 void PathMappingList::Append(const PathMappingList &rhs, bool notify) {
79 if (!rhs.m_pairs.empty()) {
80 const_iterator pos, end = rhs.m_pairs.end();
81 for (pos = rhs.m_pairs.begin(); pos != end; ++pos)
82 m_pairs.push_back(*pos);
83 if (notify && m_callback)
84 m_callback(*this, m_callback_baton);
88 void PathMappingList::Insert(const ConstString &path,
89 const ConstString &replacement, uint32_t index,
93 if (index >= m_pairs.size())
94 insert_iter = m_pairs.end();
96 insert_iter = m_pairs.begin() + index;
97 m_pairs.emplace(insert_iter, pair(NormalizePath(path),
98 NormalizePath(replacement)));
99 if (notify && m_callback)
100 m_callback(*this, m_callback_baton);
103 bool PathMappingList::Replace(const ConstString &path,
104 const ConstString &replacement, uint32_t index,
106 if (index >= m_pairs.size())
109 m_pairs[index] = pair(NormalizePath(path), NormalizePath(replacement));
110 if (notify && m_callback)
111 m_callback(*this, m_callback_baton);
115 bool PathMappingList::Remove(size_t index, bool notify) {
116 if (index >= m_pairs.size())
120 iterator iter = m_pairs.begin() + index;
122 if (notify && m_callback)
123 m_callback(*this, m_callback_baton);
127 // For clients which do not need the pair index dumped, pass a pair_index >= 0
128 // to only dump the indicated pair.
129 void PathMappingList::Dump(Stream *s, int pair_index) {
130 unsigned int numPairs = m_pairs.size();
132 if (pair_index < 0) {
134 for (index = 0; index < numPairs; ++index)
135 s->Printf("[%d] \"%s\" -> \"%s\"\n", index,
136 m_pairs[index].first.GetCString(),
137 m_pairs[index].second.GetCString());
139 if (static_cast<unsigned int>(pair_index) < numPairs)
140 s->Printf("%s -> %s", m_pairs[pair_index].first.GetCString(),
141 m_pairs[pair_index].second.GetCString());
145 void PathMappingList::Clear(bool notify) {
146 if (!m_pairs.empty())
149 if (notify && m_callback)
150 m_callback(*this, m_callback_baton);
153 bool PathMappingList::RemapPath(const ConstString &path,
154 ConstString &new_path) const {
155 std::string remapped;
156 if (RemapPath(path.GetStringRef(), remapped)) {
157 new_path.SetString(remapped);
163 bool PathMappingList::RemapPath(llvm::StringRef path,
164 std::string &new_path) const {
165 if (m_pairs.empty() || path.empty())
167 LazyBool path_is_relative = eLazyBoolCalculate;
168 for (const auto &it : m_pairs) {
169 auto prefix = it.first.GetStringRef();
170 if (!path.consume_front(prefix)) {
171 // Relative paths won't have a leading "./" in them unless "." is the
172 // only thing in the relative path so we need to work around "."
176 // We need to figure out if the "path" argument is relative. If it is,
177 // then we should remap, else skip this entry.
178 if (path_is_relative == eLazyBoolCalculate) {
179 path_is_relative = FileSpec(path, false).IsRelative() ? eLazyBoolYes :
182 if (!path_is_relative)
185 FileSpec remapped(it.second.GetStringRef(), false);
186 remapped.AppendPathComponent(path);
187 new_path = remapped.GetPath();
193 bool PathMappingList::ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const {
194 std::string path = file.GetPath();
195 llvm::StringRef path_ref(path);
196 for (const auto &it : m_pairs) {
197 if (!path_ref.consume_front(it.second.GetStringRef()))
199 fixed.SetFile(it.first.GetStringRef(), false, FileSpec::Style::native);
200 fixed.AppendPathComponent(path_ref);
206 bool PathMappingList::FindFile(const FileSpec &orig_spec,
207 FileSpec &new_spec) const {
208 if (!m_pairs.empty()) {
209 char orig_path[PATH_MAX];
210 const size_t orig_path_len =
211 orig_spec.GetPath(orig_path, sizeof(orig_path));
212 if (orig_path_len > 0) {
213 const_iterator pos, end = m_pairs.end();
214 for (pos = m_pairs.begin(); pos != end; ++pos) {
215 const size_t prefix_len = pos->first.GetLength();
217 if (orig_path_len >= prefix_len) {
218 if (::strncmp(pos->first.GetCString(), orig_path, prefix_len) == 0) {
219 new_spec.SetFile(pos->second.GetCString(), false,
220 FileSpec::Style::native);
221 new_spec.AppendPathComponent(orig_path + prefix_len);
222 if (new_spec.Exists())
233 bool PathMappingList::Replace(const ConstString &path,
234 const ConstString &new_path, bool notify) {
235 uint32_t idx = FindIndexForPath(path);
236 if (idx < m_pairs.size()) {
238 m_pairs[idx].second = new_path;
239 if (notify && m_callback)
240 m_callback(*this, m_callback_baton);
246 bool PathMappingList::Remove(const ConstString &path, bool notify) {
247 iterator pos = FindIteratorForPath(path);
248 if (pos != m_pairs.end()) {
251 if (notify && m_callback)
252 m_callback(*this, m_callback_baton);
258 PathMappingList::const_iterator
259 PathMappingList::FindIteratorForPath(const ConstString &path) const {
261 const_iterator begin = m_pairs.begin();
262 const_iterator end = m_pairs.end();
264 for (pos = begin; pos != end; ++pos) {
265 if (pos->first == path)
271 PathMappingList::iterator
272 PathMappingList::FindIteratorForPath(const ConstString &path) {
274 iterator begin = m_pairs.begin();
275 iterator end = m_pairs.end();
277 for (pos = begin; pos != end; ++pos) {
278 if (pos->first == path)
284 bool PathMappingList::GetPathsAtIndex(uint32_t idx, ConstString &path,
285 ConstString &new_path) const {
286 if (idx < m_pairs.size()) {
287 path = m_pairs[idx].first;
288 new_path = m_pairs[idx].second;
294 uint32_t PathMappingList::FindIndexForPath(const ConstString &orig_path) const {
295 const ConstString path = NormalizePath(orig_path);
297 const_iterator begin = m_pairs.begin();
298 const_iterator end = m_pairs.end();
300 for (pos = begin; pos != end; ++pos) {
301 if (pos->first == path)
302 return std::distance(begin, pos);