1 //===-- PathMappingList.cpp -------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // C Includes
11 // C++ Includes
12 #include <climits>
13 #include <cstring>
14 
15 // Other libraries and framework includes
16 // Project 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"
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 
27 namespace {
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());
41   }
42 }
43 //----------------------------------------------------------------------
44 // PathMappingList constructor
45 //----------------------------------------------------------------------
46 PathMappingList::PathMappingList()
47     : m_pairs(), m_callback(nullptr), m_callback_baton(nullptr), m_mod_id(0) {}
48 
49 PathMappingList::PathMappingList(ChangedCallback callback, void *callback_baton)
50     : m_pairs(), m_callback(callback), m_callback_baton(callback_baton),
51       m_mod_id(0) {}
52 
53 PathMappingList::PathMappingList(const PathMappingList &rhs)
54     : m_pairs(rhs.m_pairs), m_callback(nullptr), m_callback_baton(nullptr),
55       m_mod_id(0) {}
56 
57 const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) {
58   if (this != &rhs) {
59     m_pairs = rhs.m_pairs;
60     m_callback = nullptr;
61     m_callback_baton = nullptr;
62     m_mod_id = rhs.m_mod_id;
63   }
64   return *this;
65 }
66 
67 PathMappingList::~PathMappingList() = default;
68 
69 void PathMappingList::Append(const ConstString &path,
70                              const ConstString &replacement, bool notify) {
71   ++m_mod_id;
72   m_pairs.emplace_back(pair(NormalizePath(path), NormalizePath(replacement)));
73   if (notify && m_callback)
74     m_callback(*this, m_callback_baton);
75 }
76 
77 void PathMappingList::Append(const PathMappingList &rhs, bool notify) {
78   ++m_mod_id;
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);
85   }
86 }
87 
88 void PathMappingList::Insert(const ConstString &path,
89                              const ConstString &replacement, uint32_t index,
90                              bool notify) {
91   ++m_mod_id;
92   iterator insert_iter;
93   if (index >= m_pairs.size())
94     insert_iter = m_pairs.end();
95   else
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);
101 }
102 
103 bool PathMappingList::Replace(const ConstString &path,
104                               const ConstString &replacement, uint32_t index,
105                               bool notify) {
106   if (index >= m_pairs.size())
107     return false;
108   ++m_mod_id;
109   m_pairs[index] = pair(NormalizePath(path), NormalizePath(replacement));
110   if (notify && m_callback)
111     m_callback(*this, m_callback_baton);
112   return true;
113 }
114 
115 bool PathMappingList::Remove(size_t index, bool notify) {
116   if (index >= m_pairs.size())
117     return false;
118 
119   ++m_mod_id;
120   iterator iter = m_pairs.begin() + index;
121   m_pairs.erase(iter);
122   if (notify && m_callback)
123     m_callback(*this, m_callback_baton);
124   return true;
125 }
126 
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();
131 
132   if (pair_index < 0) {
133     unsigned int index;
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());
138   } else {
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());
142   }
143 }
144 
145 void PathMappingList::Clear(bool notify) {
146   if (!m_pairs.empty())
147     ++m_mod_id;
148   m_pairs.clear();
149   if (notify && m_callback)
150     m_callback(*this, m_callback_baton);
151 }
152 
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);
158     return true;
159   }
160   return false;
161 }
162 
163 bool PathMappingList::RemapPath(llvm::StringRef path,
164                                 std::string &new_path) const {
165   if (m_pairs.empty() || path.empty())
166     return false;
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 "."
173       // carefully.
174       if (prefix != ".")
175         continue;
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 :
180         eLazyBoolNo;
181       }
182       if (!path_is_relative)
183         continue;
184     }
185     FileSpec remapped(it.second.GetStringRef(), false);
186     remapped.AppendPathComponent(path);
187     new_path = remapped.GetPath();
188     return true;
189   }
190   return false;
191 }
192 
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()))
198       continue;
199     fixed.SetFile(it.first.GetStringRef(), false);
200     fixed.AppendPathComponent(path_ref);
201     return true;
202   }
203   return false;
204 }
205 
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();
216 
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             new_spec.AppendPathComponent(orig_path + prefix_len);
221             if (new_spec.Exists())
222               return true;
223           }
224         }
225       }
226     }
227   }
228   new_spec.Clear();
229   return false;
230 }
231 
232 bool PathMappingList::Replace(const ConstString &path,
233                               const ConstString &new_path, bool notify) {
234   uint32_t idx = FindIndexForPath(path);
235   if (idx < m_pairs.size()) {
236     ++m_mod_id;
237     m_pairs[idx].second = new_path;
238     if (notify && m_callback)
239       m_callback(*this, m_callback_baton);
240     return true;
241   }
242   return false;
243 }
244 
245 bool PathMappingList::Remove(const ConstString &path, bool notify) {
246   iterator pos = FindIteratorForPath(path);
247   if (pos != m_pairs.end()) {
248     ++m_mod_id;
249     m_pairs.erase(pos);
250     if (notify && m_callback)
251       m_callback(*this, m_callback_baton);
252     return true;
253   }
254   return false;
255 }
256 
257 PathMappingList::const_iterator
258 PathMappingList::FindIteratorForPath(const ConstString &path) const {
259   const_iterator pos;
260   const_iterator begin = m_pairs.begin();
261   const_iterator end = m_pairs.end();
262 
263   for (pos = begin; pos != end; ++pos) {
264     if (pos->first == path)
265       break;
266   }
267   return pos;
268 }
269 
270 PathMappingList::iterator
271 PathMappingList::FindIteratorForPath(const ConstString &path) {
272   iterator pos;
273   iterator begin = m_pairs.begin();
274   iterator end = m_pairs.end();
275 
276   for (pos = begin; pos != end; ++pos) {
277     if (pos->first == path)
278       break;
279   }
280   return pos;
281 }
282 
283 bool PathMappingList::GetPathsAtIndex(uint32_t idx, ConstString &path,
284                                       ConstString &new_path) const {
285   if (idx < m_pairs.size()) {
286     path = m_pairs[idx].first;
287     new_path = m_pairs[idx].second;
288     return true;
289   }
290   return false;
291 }
292 
293 uint32_t PathMappingList::FindIndexForPath(const ConstString &orig_path) const {
294   const ConstString path = NormalizePath(orig_path);
295   const_iterator pos;
296   const_iterator begin = m_pairs.begin();
297   const_iterator end = m_pairs.end();
298 
299   for (pos = begin; pos != end; ++pos) {
300     if (pos->first == path)
301       return std::distance(begin, pos);
302   }
303   return UINT32_MAX;
304 }
305