1ac7ddfbfSEd Maste //===-- PathMappingList.cpp -------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste // The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste
104bb0738eSEd Maste #include <climits>
114bb0738eSEd Maste #include <cstring>
124bb0738eSEd Maste
13*b5893f02SDimitry Andric #include "lldb/Host/FileSystem.h"
14435933ddSDimitry Andric #include "lldb/Host/PosixApi.h"
15ac7ddfbfSEd Maste #include "lldb/Target/PathMappingList.h"
16f678e45dSDimitry Andric #include "lldb/Utility/FileSpec.h"
175517e702SDimitry Andric #include "lldb/Utility/Status.h"
18f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
19*b5893f02SDimitry Andric #include "lldb/lldb-private-enumerations.h"
20ac7ddfbfSEd Maste
21ac7ddfbfSEd Maste using namespace lldb;
22ac7ddfbfSEd Maste using namespace lldb_private;
23ac7ddfbfSEd Maste
244ba319b5SDimitry Andric namespace {
254ba319b5SDimitry Andric // We must normalize our path pairs that we store because if we don't then
264ba319b5SDimitry Andric // things won't always work. We found a case where if we did:
274ba319b5SDimitry Andric // (lldb) settings set target.source-map . /tmp
284ba319b5SDimitry Andric // We would store a path pairs of "." and "/tmp" as raw strings. If the debug
294ba319b5SDimitry Andric // info contains "./foo/bar.c", the path will get normalized to "foo/bar.c".
304ba319b5SDimitry Andric // When PathMappingList::RemapPath() is called, it expects the path to start
314ba319b5SDimitry Andric // with the raw path pair, which doesn't work anymore because the paths have
324ba319b5SDimitry Andric // been normalized when the debug info was loaded. So we need to store
334ba319b5SDimitry Andric // nomalized path pairs to ensure things match up.
NormalizePath(const ConstString & path)344ba319b5SDimitry Andric ConstString NormalizePath(const ConstString &path) {
354ba319b5SDimitry Andric // If we use "path" to construct a FileSpec, it will normalize the path for
364ba319b5SDimitry Andric // us. We then grab the string and turn it back into a ConstString.
37*b5893f02SDimitry Andric return ConstString(FileSpec(path.GetStringRef()).GetPath());
384ba319b5SDimitry Andric }
394ba319b5SDimitry Andric }
40ac7ddfbfSEd Maste //----------------------------------------------------------------------
41ac7ddfbfSEd Maste // PathMappingList constructor
42ac7ddfbfSEd Maste //----------------------------------------------------------------------
PathMappingList()43435933ddSDimitry Andric PathMappingList::PathMappingList()
44435933ddSDimitry Andric : m_pairs(), m_callback(nullptr), m_callback_baton(nullptr), m_mod_id(0) {}
45ac7ddfbfSEd Maste
PathMappingList(ChangedCallback callback,void * callback_baton)46435933ddSDimitry Andric PathMappingList::PathMappingList(ChangedCallback callback, void *callback_baton)
47435933ddSDimitry Andric : m_pairs(), m_callback(callback), m_callback_baton(callback_baton),
48435933ddSDimitry Andric m_mod_id(0) {}
49ac7ddfbfSEd Maste
PathMappingList(const PathMappingList & rhs)50435933ddSDimitry Andric PathMappingList::PathMappingList(const PathMappingList &rhs)
51435933ddSDimitry Andric : m_pairs(rhs.m_pairs), m_callback(nullptr), m_callback_baton(nullptr),
52435933ddSDimitry Andric m_mod_id(0) {}
53ac7ddfbfSEd Maste
operator =(const PathMappingList & rhs)54435933ddSDimitry Andric const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) {
55435933ddSDimitry Andric if (this != &rhs) {
56ac7ddfbfSEd Maste m_pairs = rhs.m_pairs;
574bb0738eSEd Maste m_callback = nullptr;
584bb0738eSEd Maste m_callback_baton = nullptr;
59ac7ddfbfSEd Maste m_mod_id = rhs.m_mod_id;
60ac7ddfbfSEd Maste }
61ac7ddfbfSEd Maste return *this;
62ac7ddfbfSEd Maste }
63ac7ddfbfSEd Maste
644bb0738eSEd Maste PathMappingList::~PathMappingList() = default;
65ac7ddfbfSEd Maste
Append(const ConstString & path,const ConstString & replacement,bool notify)66435933ddSDimitry Andric void PathMappingList::Append(const ConstString &path,
67435933ddSDimitry Andric const ConstString &replacement, bool notify) {
68ac7ddfbfSEd Maste ++m_mod_id;
694ba319b5SDimitry Andric m_pairs.emplace_back(pair(NormalizePath(path), NormalizePath(replacement)));
70ac7ddfbfSEd Maste if (notify && m_callback)
71ac7ddfbfSEd Maste m_callback(*this, m_callback_baton);
72ac7ddfbfSEd Maste }
73ac7ddfbfSEd Maste
Append(const PathMappingList & rhs,bool notify)74435933ddSDimitry Andric void PathMappingList::Append(const PathMappingList &rhs, bool notify) {
75ac7ddfbfSEd Maste ++m_mod_id;
76435933ddSDimitry Andric if (!rhs.m_pairs.empty()) {
77ac7ddfbfSEd Maste const_iterator pos, end = rhs.m_pairs.end();
78ac7ddfbfSEd Maste for (pos = rhs.m_pairs.begin(); pos != end; ++pos)
79ac7ddfbfSEd Maste m_pairs.push_back(*pos);
80ac7ddfbfSEd Maste if (notify && m_callback)
81ac7ddfbfSEd Maste m_callback(*this, m_callback_baton);
82ac7ddfbfSEd Maste }
83ac7ddfbfSEd Maste }
84ac7ddfbfSEd Maste
Insert(const ConstString & path,const ConstString & replacement,uint32_t index,bool notify)85435933ddSDimitry Andric void PathMappingList::Insert(const ConstString &path,
86435933ddSDimitry Andric const ConstString &replacement, uint32_t index,
87435933ddSDimitry Andric bool notify) {
88ac7ddfbfSEd Maste ++m_mod_id;
89ac7ddfbfSEd Maste iterator insert_iter;
90ac7ddfbfSEd Maste if (index >= m_pairs.size())
91ac7ddfbfSEd Maste insert_iter = m_pairs.end();
92ac7ddfbfSEd Maste else
93ac7ddfbfSEd Maste insert_iter = m_pairs.begin() + index;
944ba319b5SDimitry Andric m_pairs.emplace(insert_iter, pair(NormalizePath(path),
954ba319b5SDimitry Andric NormalizePath(replacement)));
96ac7ddfbfSEd Maste if (notify && m_callback)
97ac7ddfbfSEd Maste m_callback(*this, m_callback_baton);
98ac7ddfbfSEd Maste }
99ac7ddfbfSEd Maste
Replace(const ConstString & path,const ConstString & replacement,uint32_t index,bool notify)100435933ddSDimitry Andric bool PathMappingList::Replace(const ConstString &path,
101435933ddSDimitry Andric const ConstString &replacement, uint32_t index,
102435933ddSDimitry Andric bool notify) {
103ac7ddfbfSEd Maste if (index >= m_pairs.size())
104ac7ddfbfSEd Maste return false;
105ac7ddfbfSEd Maste ++m_mod_id;
1064ba319b5SDimitry Andric m_pairs[index] = pair(NormalizePath(path), NormalizePath(replacement));
107ac7ddfbfSEd Maste if (notify && m_callback)
108ac7ddfbfSEd Maste m_callback(*this, m_callback_baton);
109ac7ddfbfSEd Maste return true;
110ac7ddfbfSEd Maste }
111ac7ddfbfSEd Maste
Remove(size_t index,bool notify)112435933ddSDimitry Andric bool PathMappingList::Remove(size_t index, bool notify) {
113ac7ddfbfSEd Maste if (index >= m_pairs.size())
114ac7ddfbfSEd Maste return false;
115ac7ddfbfSEd Maste
116ac7ddfbfSEd Maste ++m_mod_id;
117ac7ddfbfSEd Maste iterator iter = m_pairs.begin() + index;
118ac7ddfbfSEd Maste m_pairs.erase(iter);
119ac7ddfbfSEd Maste if (notify && m_callback)
120ac7ddfbfSEd Maste m_callback(*this, m_callback_baton);
121ac7ddfbfSEd Maste return true;
122ac7ddfbfSEd Maste }
123ac7ddfbfSEd Maste
124ac7ddfbfSEd Maste // For clients which do not need the pair index dumped, pass a pair_index >= 0
125ac7ddfbfSEd Maste // to only dump the indicated pair.
Dump(Stream * s,int pair_index)126435933ddSDimitry Andric void PathMappingList::Dump(Stream *s, int pair_index) {
127ac7ddfbfSEd Maste unsigned int numPairs = m_pairs.size();
128ac7ddfbfSEd Maste
129435933ddSDimitry Andric if (pair_index < 0) {
130ac7ddfbfSEd Maste unsigned int index;
131ac7ddfbfSEd Maste for (index = 0; index < numPairs; ++index)
132435933ddSDimitry Andric s->Printf("[%d] \"%s\" -> \"%s\"\n", index,
133435933ddSDimitry Andric m_pairs[index].first.GetCString(),
134435933ddSDimitry Andric m_pairs[index].second.GetCString());
135435933ddSDimitry Andric } else {
1360127ef0fSEd Maste if (static_cast<unsigned int>(pair_index) < numPairs)
137435933ddSDimitry Andric s->Printf("%s -> %s", m_pairs[pair_index].first.GetCString(),
138435933ddSDimitry Andric m_pairs[pair_index].second.GetCString());
139ac7ddfbfSEd Maste }
140ac7ddfbfSEd Maste }
141ac7ddfbfSEd Maste
Clear(bool notify)142435933ddSDimitry Andric void PathMappingList::Clear(bool notify) {
143ac7ddfbfSEd Maste if (!m_pairs.empty())
144ac7ddfbfSEd Maste ++m_mod_id;
145ac7ddfbfSEd Maste m_pairs.clear();
146ac7ddfbfSEd Maste if (notify && m_callback)
147ac7ddfbfSEd Maste m_callback(*this, m_callback_baton);
148ac7ddfbfSEd Maste }
149ac7ddfbfSEd Maste
RemapPath(const ConstString & path,ConstString & new_path) const150435933ddSDimitry Andric bool PathMappingList::RemapPath(const ConstString &path,
151435933ddSDimitry Andric ConstString &new_path) const {
1524ba319b5SDimitry Andric std::string remapped;
1534ba319b5SDimitry Andric if (RemapPath(path.GetStringRef(), remapped)) {
1544ba319b5SDimitry Andric new_path.SetString(remapped);
155ac7ddfbfSEd Maste return true;
156ac7ddfbfSEd Maste }
157ac7ddfbfSEd Maste return false;
158ac7ddfbfSEd Maste }
159ac7ddfbfSEd Maste
RemapPath(llvm::StringRef path,std::string & new_path) const160435933ddSDimitry Andric bool PathMappingList::RemapPath(llvm::StringRef path,
161435933ddSDimitry Andric std::string &new_path) const {
162435933ddSDimitry Andric if (m_pairs.empty() || path.empty())
163ac7ddfbfSEd Maste return false;
1644ba319b5SDimitry Andric LazyBool path_is_relative = eLazyBoolCalculate;
165435933ddSDimitry Andric for (const auto &it : m_pairs) {
1664ba319b5SDimitry Andric auto prefix = it.first.GetStringRef();
1674ba319b5SDimitry Andric if (!path.consume_front(prefix)) {
1684ba319b5SDimitry Andric // Relative paths won't have a leading "./" in them unless "." is the
1694ba319b5SDimitry Andric // only thing in the relative path so we need to work around "."
1704ba319b5SDimitry Andric // carefully.
1714ba319b5SDimitry Andric if (prefix != ".")
1724ba319b5SDimitry Andric continue;
1734ba319b5SDimitry Andric // We need to figure out if the "path" argument is relative. If it is,
1744ba319b5SDimitry Andric // then we should remap, else skip this entry.
1754ba319b5SDimitry Andric if (path_is_relative == eLazyBoolCalculate) {
176*b5893f02SDimitry Andric path_is_relative =
177*b5893f02SDimitry Andric FileSpec(path).IsRelative() ? eLazyBoolYes : eLazyBoolNo;
1784ba319b5SDimitry Andric }
1794ba319b5SDimitry Andric if (!path_is_relative)
1804ba319b5SDimitry Andric continue;
1814ba319b5SDimitry Andric }
182*b5893f02SDimitry Andric FileSpec remapped(it.second.GetStringRef());
1834ba319b5SDimitry Andric remapped.AppendPathComponent(path);
1844ba319b5SDimitry Andric new_path = remapped.GetPath();
1854bb0738eSEd Maste return true;
1864bb0738eSEd Maste }
1874ba319b5SDimitry Andric return false;
1884ba319b5SDimitry Andric }
1894ba319b5SDimitry Andric
ReverseRemapPath(const FileSpec & file,FileSpec & fixed) const1904ba319b5SDimitry Andric bool PathMappingList::ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const {
1914ba319b5SDimitry Andric std::string path = file.GetPath();
1924ba319b5SDimitry Andric llvm::StringRef path_ref(path);
1934ba319b5SDimitry Andric for (const auto &it : m_pairs) {
1944ba319b5SDimitry Andric if (!path_ref.consume_front(it.second.GetStringRef()))
1954ba319b5SDimitry Andric continue;
196*b5893f02SDimitry Andric fixed.SetFile(it.first.GetStringRef(), FileSpec::Style::native);
1974ba319b5SDimitry Andric fixed.AppendPathComponent(path_ref);
1984ba319b5SDimitry Andric return true;
1994bb0738eSEd Maste }
2004bb0738eSEd Maste return false;
2014bb0738eSEd Maste }
2024bb0738eSEd Maste
FindFile(const FileSpec & orig_spec,FileSpec & new_spec) const203435933ddSDimitry Andric bool PathMappingList::FindFile(const FileSpec &orig_spec,
204435933ddSDimitry Andric FileSpec &new_spec) const {
205435933ddSDimitry Andric if (!m_pairs.empty()) {
206ac7ddfbfSEd Maste char orig_path[PATH_MAX];
207435933ddSDimitry Andric const size_t orig_path_len =
208435933ddSDimitry Andric orig_spec.GetPath(orig_path, sizeof(orig_path));
209435933ddSDimitry Andric if (orig_path_len > 0) {
210ac7ddfbfSEd Maste const_iterator pos, end = m_pairs.end();
211435933ddSDimitry Andric for (pos = m_pairs.begin(); pos != end; ++pos) {
212ac7ddfbfSEd Maste const size_t prefix_len = pos->first.GetLength();
213ac7ddfbfSEd Maste
214435933ddSDimitry Andric if (orig_path_len >= prefix_len) {
215435933ddSDimitry Andric if (::strncmp(pos->first.GetCString(), orig_path, prefix_len) == 0) {
216*b5893f02SDimitry Andric new_spec.SetFile(pos->second.GetCString(), FileSpec::Style::native);
217435933ddSDimitry Andric new_spec.AppendPathComponent(orig_path + prefix_len);
218*b5893f02SDimitry Andric if (FileSystem::Instance().Exists(new_spec))
219ac7ddfbfSEd Maste return true;
220ac7ddfbfSEd Maste }
221ac7ddfbfSEd Maste }
222ac7ddfbfSEd Maste }
223ac7ddfbfSEd Maste }
224ac7ddfbfSEd Maste }
225ac7ddfbfSEd Maste new_spec.Clear();
226ac7ddfbfSEd Maste return false;
227ac7ddfbfSEd Maste }
228ac7ddfbfSEd Maste
Replace(const ConstString & path,const ConstString & new_path,bool notify)229435933ddSDimitry Andric bool PathMappingList::Replace(const ConstString &path,
230435933ddSDimitry Andric const ConstString &new_path, bool notify) {
231ac7ddfbfSEd Maste uint32_t idx = FindIndexForPath(path);
232435933ddSDimitry Andric if (idx < m_pairs.size()) {
233ac7ddfbfSEd Maste ++m_mod_id;
234ac7ddfbfSEd Maste m_pairs[idx].second = new_path;
235ac7ddfbfSEd Maste if (notify && m_callback)
236ac7ddfbfSEd Maste m_callback(*this, m_callback_baton);
237ac7ddfbfSEd Maste return true;
238ac7ddfbfSEd Maste }
239ac7ddfbfSEd Maste return false;
240ac7ddfbfSEd Maste }
241ac7ddfbfSEd Maste
Remove(const ConstString & path,bool notify)242435933ddSDimitry Andric bool PathMappingList::Remove(const ConstString &path, bool notify) {
243ac7ddfbfSEd Maste iterator pos = FindIteratorForPath(path);
244435933ddSDimitry Andric if (pos != m_pairs.end()) {
245ac7ddfbfSEd Maste ++m_mod_id;
246ac7ddfbfSEd Maste m_pairs.erase(pos);
247ac7ddfbfSEd Maste if (notify && m_callback)
248ac7ddfbfSEd Maste m_callback(*this, m_callback_baton);
249ac7ddfbfSEd Maste return true;
250ac7ddfbfSEd Maste }
251ac7ddfbfSEd Maste return false;
252ac7ddfbfSEd Maste }
253ac7ddfbfSEd Maste
254ac7ddfbfSEd Maste PathMappingList::const_iterator
FindIteratorForPath(const ConstString & path) const255435933ddSDimitry Andric PathMappingList::FindIteratorForPath(const ConstString &path) const {
256ac7ddfbfSEd Maste const_iterator pos;
257ac7ddfbfSEd Maste const_iterator begin = m_pairs.begin();
258ac7ddfbfSEd Maste const_iterator end = m_pairs.end();
259ac7ddfbfSEd Maste
260435933ddSDimitry Andric for (pos = begin; pos != end; ++pos) {
261ac7ddfbfSEd Maste if (pos->first == path)
262ac7ddfbfSEd Maste break;
263ac7ddfbfSEd Maste }
264ac7ddfbfSEd Maste return pos;
265ac7ddfbfSEd Maste }
266ac7ddfbfSEd Maste
267ac7ddfbfSEd Maste PathMappingList::iterator
FindIteratorForPath(const ConstString & path)268435933ddSDimitry Andric PathMappingList::FindIteratorForPath(const ConstString &path) {
269ac7ddfbfSEd Maste iterator pos;
270ac7ddfbfSEd Maste iterator begin = m_pairs.begin();
271ac7ddfbfSEd Maste iterator end = m_pairs.end();
272ac7ddfbfSEd Maste
273435933ddSDimitry Andric for (pos = begin; pos != end; ++pos) {
274ac7ddfbfSEd Maste if (pos->first == path)
275ac7ddfbfSEd Maste break;
276ac7ddfbfSEd Maste }
277ac7ddfbfSEd Maste return pos;
278ac7ddfbfSEd Maste }
279ac7ddfbfSEd Maste
GetPathsAtIndex(uint32_t idx,ConstString & path,ConstString & new_path) const280435933ddSDimitry Andric bool PathMappingList::GetPathsAtIndex(uint32_t idx, ConstString &path,
281435933ddSDimitry Andric ConstString &new_path) const {
282435933ddSDimitry Andric if (idx < m_pairs.size()) {
283ac7ddfbfSEd Maste path = m_pairs[idx].first;
284ac7ddfbfSEd Maste new_path = m_pairs[idx].second;
285ac7ddfbfSEd Maste return true;
286ac7ddfbfSEd Maste }
287ac7ddfbfSEd Maste return false;
288ac7ddfbfSEd Maste }
289ac7ddfbfSEd Maste
FindIndexForPath(const ConstString & orig_path) const2904ba319b5SDimitry Andric uint32_t PathMappingList::FindIndexForPath(const ConstString &orig_path) const {
2914ba319b5SDimitry Andric const ConstString path = NormalizePath(orig_path);
292ac7ddfbfSEd Maste const_iterator pos;
293ac7ddfbfSEd Maste const_iterator begin = m_pairs.begin();
294ac7ddfbfSEd Maste const_iterator end = m_pairs.end();
295ac7ddfbfSEd Maste
296435933ddSDimitry Andric for (pos = begin; pos != end; ++pos) {
297ac7ddfbfSEd Maste if (pos->first == path)
298ac7ddfbfSEd Maste return std::distance(begin, pos);
299ac7ddfbfSEd Maste }
300ac7ddfbfSEd Maste return UINT32_MAX;
301ac7ddfbfSEd Maste }
302