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 //===----------------------------------------------------------------------===//
13 #include "lldb/Host/FileSystem.h"
14 #include "lldb/Host/PosixApi.h"
15 #include "lldb/Target/PathMappingList.h"
16 #include "lldb/Utility/FileSpec.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/Utility/Stream.h"
19 #include "lldb/lldb-private-enumerations.h"
22 using namespace lldb_private;
25 // We must normalize our path pairs that we store because if we don't then
26 // things won't always work. We found a case where if we did:
27 // (lldb) settings set target.source-map . /tmp
28 // We would store a path pairs of "." and "/tmp" as raw strings. If the debug
29 // info contains "./foo/bar.c", the path will get normalized to "foo/bar.c".
30 // When PathMappingList::RemapPath() is called, it expects the path to start
31 // with the raw path pair, which doesn't work anymore because the paths have
32 // been normalized when the debug info was loaded. So we need to store
33 // nomalized path pairs to ensure things match up.
34 ConstString NormalizePath(const ConstString &path) {
35 // If we use "path" to construct a FileSpec, it will normalize the path for
36 // us. We then grab the string and turn it back into a ConstString.
37 return ConstString(FileSpec(path.GetStringRef()).GetPath());
40 //----------------------------------------------------------------------
41 // PathMappingList constructor
42 //----------------------------------------------------------------------
43 PathMappingList::PathMappingList()
44 : m_pairs(), m_callback(nullptr), m_callback_baton(nullptr), m_mod_id(0) {}
46 PathMappingList::PathMappingList(ChangedCallback callback, void *callback_baton)
47 : m_pairs(), m_callback(callback), m_callback_baton(callback_baton),
50 PathMappingList::PathMappingList(const PathMappingList &rhs)
51 : m_pairs(rhs.m_pairs), m_callback(nullptr), m_callback_baton(nullptr),
54 const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) {
56 m_pairs = rhs.m_pairs;
58 m_callback_baton = nullptr;
59 m_mod_id = rhs.m_mod_id;
64 PathMappingList::~PathMappingList() = default;
66 void PathMappingList::Append(const ConstString &path,
67 const ConstString &replacement, bool notify) {
69 m_pairs.emplace_back(pair(NormalizePath(path), NormalizePath(replacement)));
70 if (notify && m_callback)
71 m_callback(*this, m_callback_baton);
74 void PathMappingList::Append(const PathMappingList &rhs, bool notify) {
76 if (!rhs.m_pairs.empty()) {
77 const_iterator pos, end = rhs.m_pairs.end();
78 for (pos = rhs.m_pairs.begin(); pos != end; ++pos)
79 m_pairs.push_back(*pos);
80 if (notify && m_callback)
81 m_callback(*this, m_callback_baton);
85 void PathMappingList::Insert(const ConstString &path,
86 const ConstString &replacement, uint32_t index,
90 if (index >= m_pairs.size())
91 insert_iter = m_pairs.end();
93 insert_iter = m_pairs.begin() + index;
94 m_pairs.emplace(insert_iter, pair(NormalizePath(path),
95 NormalizePath(replacement)));
96 if (notify && m_callback)
97 m_callback(*this, m_callback_baton);
100 bool PathMappingList::Replace(const ConstString &path,
101 const ConstString &replacement, uint32_t index,
103 if (index >= m_pairs.size())
106 m_pairs[index] = pair(NormalizePath(path), NormalizePath(replacement));
107 if (notify && m_callback)
108 m_callback(*this, m_callback_baton);
112 bool PathMappingList::Remove(size_t index, bool notify) {
113 if (index >= m_pairs.size())
117 iterator iter = m_pairs.begin() + index;
119 if (notify && m_callback)
120 m_callback(*this, m_callback_baton);
124 // For clients which do not need the pair index dumped, pass a pair_index >= 0
125 // to only dump the indicated pair.
126 void PathMappingList::Dump(Stream *s, int pair_index) {
127 unsigned int numPairs = m_pairs.size();
129 if (pair_index < 0) {
131 for (index = 0; index < numPairs; ++index)
132 s->Printf("[%d] \"%s\" -> \"%s\"\n", index,
133 m_pairs[index].first.GetCString(),
134 m_pairs[index].second.GetCString());
136 if (static_cast<unsigned int>(pair_index) < numPairs)
137 s->Printf("%s -> %s", m_pairs[pair_index].first.GetCString(),
138 m_pairs[pair_index].second.GetCString());
142 void PathMappingList::Clear(bool notify) {
143 if (!m_pairs.empty())
146 if (notify && m_callback)
147 m_callback(*this, m_callback_baton);
150 bool PathMappingList::RemapPath(const ConstString &path,
151 ConstString &new_path) const {
152 std::string remapped;
153 if (RemapPath(path.GetStringRef(), remapped)) {
154 new_path.SetString(remapped);
160 bool PathMappingList::RemapPath(llvm::StringRef path,
161 std::string &new_path) const {
162 if (m_pairs.empty() || path.empty())
164 LazyBool path_is_relative = eLazyBoolCalculate;
165 for (const auto &it : m_pairs) {
166 auto prefix = it.first.GetStringRef();
167 if (!path.consume_front(prefix)) {
168 // Relative paths won't have a leading "./" in them unless "." is the
169 // only thing in the relative path so we need to work around "."
173 // We need to figure out if the "path" argument is relative. If it is,
174 // then we should remap, else skip this entry.
175 if (path_is_relative == eLazyBoolCalculate) {
177 FileSpec(path).IsRelative() ? eLazyBoolYes : eLazyBoolNo;
179 if (!path_is_relative)
182 FileSpec remapped(it.second.GetStringRef());
183 remapped.AppendPathComponent(path);
184 new_path = remapped.GetPath();
190 bool PathMappingList::ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const {
191 std::string path = file.GetPath();
192 llvm::StringRef path_ref(path);
193 for (const auto &it : m_pairs) {
194 if (!path_ref.consume_front(it.second.GetStringRef()))
196 fixed.SetFile(it.first.GetStringRef(), FileSpec::Style::native);
197 fixed.AppendPathComponent(path_ref);
203 bool PathMappingList::FindFile(const FileSpec &orig_spec,
204 FileSpec &new_spec) const {
205 if (!m_pairs.empty()) {
206 char orig_path[PATH_MAX];
207 const size_t orig_path_len =
208 orig_spec.GetPath(orig_path, sizeof(orig_path));
209 if (orig_path_len > 0) {
210 const_iterator pos, end = m_pairs.end();
211 for (pos = m_pairs.begin(); pos != end; ++pos) {
212 const size_t prefix_len = pos->first.GetLength();
214 if (orig_path_len >= prefix_len) {
215 if (::strncmp(pos->first.GetCString(), orig_path, prefix_len) == 0) {
216 new_spec.SetFile(pos->second.GetCString(), FileSpec::Style::native);
217 new_spec.AppendPathComponent(orig_path + prefix_len);
218 if (FileSystem::Instance().Exists(new_spec))
229 bool PathMappingList::Replace(const ConstString &path,
230 const ConstString &new_path, bool notify) {
231 uint32_t idx = FindIndexForPath(path);
232 if (idx < m_pairs.size()) {
234 m_pairs[idx].second = new_path;
235 if (notify && m_callback)
236 m_callback(*this, m_callback_baton);
242 bool PathMappingList::Remove(const ConstString &path, bool notify) {
243 iterator pos = FindIteratorForPath(path);
244 if (pos != m_pairs.end()) {
247 if (notify && m_callback)
248 m_callback(*this, m_callback_baton);
254 PathMappingList::const_iterator
255 PathMappingList::FindIteratorForPath(const ConstString &path) const {
257 const_iterator begin = m_pairs.begin();
258 const_iterator end = m_pairs.end();
260 for (pos = begin; pos != end; ++pos) {
261 if (pos->first == path)
267 PathMappingList::iterator
268 PathMappingList::FindIteratorForPath(const ConstString &path) {
270 iterator begin = m_pairs.begin();
271 iterator end = m_pairs.end();
273 for (pos = begin; pos != end; ++pos) {
274 if (pos->first == path)
280 bool PathMappingList::GetPathsAtIndex(uint32_t idx, ConstString &path,
281 ConstString &new_path) const {
282 if (idx < m_pairs.size()) {
283 path = m_pairs[idx].first;
284 new_path = m_pairs[idx].second;
290 uint32_t PathMappingList::FindIndexForPath(const ConstString &orig_path) const {
291 const ConstString path = NormalizePath(orig_path);
293 const_iterator begin = m_pairs.begin();
294 const_iterator end = m_pairs.end();
296 for (pos = begin; pos != end; ++pos) {
297 if (pos->first == path)
298 return std::distance(begin, pos);