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" 14 #include "lldb/Target/PathMappingList.h" 15 #include "lldb/Utility/FileSpec.h" 16 #include "lldb/Utility/Status.h" 17 #include "lldb/Utility/Stream.h" 18 #include "lldb/lldb-private-enumerations.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 m_mod_id(0) {} 45 46 PathMappingList::PathMappingList(const PathMappingList &rhs) 47 : m_pairs(rhs.m_pairs), m_callback(nullptr), m_callback_baton(nullptr), 48 m_mod_id(0) {} 49 50 const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) { 51 if (this != &rhs) { 52 m_pairs = rhs.m_pairs; 53 m_callback = nullptr; 54 m_callback_baton = nullptr; 55 m_mod_id = rhs.m_mod_id; 56 } 57 return *this; 58 } 59 60 PathMappingList::~PathMappingList() = default; 61 62 void PathMappingList::Append(llvm::StringRef path, llvm::StringRef replacement, 63 bool notify) { 64 ++m_mod_id; 65 m_pairs.emplace_back(pair(NormalizePath(path), NormalizePath(replacement))); 66 if (notify && m_callback) 67 m_callback(*this, m_callback_baton); 68 } 69 70 void PathMappingList::Append(const PathMappingList &rhs, bool notify) { 71 ++m_mod_id; 72 if (!rhs.m_pairs.empty()) { 73 const_iterator pos, end = rhs.m_pairs.end(); 74 for (pos = rhs.m_pairs.begin(); pos != end; ++pos) 75 m_pairs.push_back(*pos); 76 if (notify && m_callback) 77 m_callback(*this, m_callback_baton); 78 } 79 } 80 81 void PathMappingList::Insert(llvm::StringRef path, llvm::StringRef replacement, 82 uint32_t index, bool notify) { 83 ++m_mod_id; 84 iterator insert_iter; 85 if (index >= m_pairs.size()) 86 insert_iter = m_pairs.end(); 87 else 88 insert_iter = m_pairs.begin() + index; 89 m_pairs.emplace(insert_iter, pair(NormalizePath(path), 90 NormalizePath(replacement))); 91 if (notify && m_callback) 92 m_callback(*this, m_callback_baton); 93 } 94 95 bool PathMappingList::Replace(llvm::StringRef path, llvm::StringRef replacement, 96 uint32_t index, bool notify) { 97 if (index >= m_pairs.size()) 98 return false; 99 ++m_mod_id; 100 m_pairs[index] = pair(NormalizePath(path), NormalizePath(replacement)); 101 if (notify && m_callback) 102 m_callback(*this, m_callback_baton); 103 return true; 104 } 105 106 bool PathMappingList::Remove(size_t index, bool notify) { 107 if (index >= m_pairs.size()) 108 return false; 109 110 ++m_mod_id; 111 iterator iter = m_pairs.begin() + index; 112 m_pairs.erase(iter); 113 if (notify && m_callback) 114 m_callback(*this, m_callback_baton); 115 return true; 116 } 117 118 // For clients which do not need the pair index dumped, pass a pair_index >= 0 119 // to only dump the indicated pair. 120 void PathMappingList::Dump(Stream *s, int pair_index) { 121 unsigned int numPairs = m_pairs.size(); 122 123 if (pair_index < 0) { 124 unsigned int index; 125 for (index = 0; index < numPairs; ++index) 126 s->Printf("[%d] \"%s\" -> \"%s\"\n", index, 127 m_pairs[index].first.GetCString(), 128 m_pairs[index].second.GetCString()); 129 } else { 130 if (static_cast<unsigned int>(pair_index) < numPairs) 131 s->Printf("%s -> %s", m_pairs[pair_index].first.GetCString(), 132 m_pairs[pair_index].second.GetCString()); 133 } 134 } 135 136 void PathMappingList::Clear(bool notify) { 137 if (!m_pairs.empty()) 138 ++m_mod_id; 139 m_pairs.clear(); 140 if (notify && m_callback) 141 m_callback(*this, m_callback_baton); 142 } 143 144 bool PathMappingList::RemapPath(ConstString path, 145 ConstString &new_path) const { 146 if (llvm::Optional<FileSpec> remapped = RemapPath(path.GetStringRef())) { 147 new_path.SetString(remapped->GetPath()); 148 return true; 149 } 150 return false; 151 } 152 153 /// Append components to path, applying style. 154 static void AppendPathComponents(FileSpec &path, llvm::StringRef components, 155 llvm::sys::path::Style style) { 156 auto component = llvm::sys::path::begin(components, style); 157 auto e = llvm::sys::path::end(components); 158 while (component != e && 159 llvm::sys::path::is_separator(*component->data(), style)) 160 ++component; 161 for (; component != e; ++component) 162 path.AppendPathComponent(*component); 163 } 164 165 llvm::Optional<FileSpec> 166 PathMappingList::RemapPath(llvm::StringRef mapping_path, 167 bool only_if_exists) const { 168 if (m_pairs.empty() || mapping_path.empty()) 169 return {}; 170 LazyBool path_is_relative = eLazyBoolCalculate; 171 172 for (const auto &it : m_pairs) { 173 llvm::StringRef prefix = it.first.GetStringRef(); 174 // We create a copy of mapping_path because StringRef::consume_from 175 // effectively modifies the instance itself. 176 llvm::StringRef path = mapping_path; 177 if (!path.consume_front(prefix)) { 178 // Relative paths won't have a leading "./" in them unless "." is the 179 // only thing in the relative path so we need to work around "." 180 // carefully. 181 if (prefix != ".") 182 continue; 183 // We need to figure out if the "path" argument is relative. If it is, 184 // then we should remap, else skip this entry. 185 if (path_is_relative == eLazyBoolCalculate) { 186 path_is_relative = 187 FileSpec(path).IsRelative() ? eLazyBoolYes : eLazyBoolNo; 188 } 189 if (!path_is_relative) 190 continue; 191 } 192 FileSpec remapped(it.second.GetStringRef()); 193 auto orig_style = FileSpec::GuessPathStyle(prefix).getValueOr( 194 llvm::sys::path::Style::native); 195 AppendPathComponents(remapped, path, orig_style); 196 if (!only_if_exists || FileSystem::Instance().Exists(remapped)) 197 return remapped; 198 } 199 return {}; 200 } 201 202 bool PathMappingList::ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const { 203 std::string path = file.GetPath(); 204 llvm::StringRef path_ref(path); 205 for (const auto &it : m_pairs) { 206 if (!path_ref.consume_front(it.second.GetStringRef())) 207 continue; 208 auto orig_file = it.first.GetStringRef(); 209 auto orig_style = FileSpec::GuessPathStyle(orig_file).getValueOr( 210 llvm::sys::path::Style::native); 211 fixed.SetFile(orig_file, orig_style); 212 AppendPathComponents(fixed, path_ref, orig_style); 213 return true; 214 } 215 return false; 216 } 217 218 llvm::Optional<FileSpec> PathMappingList::FindFile(const FileSpec &orig_spec) const { 219 // We must normalize the orig_spec again using the host's path style, 220 // otherwise there will be mismatch between the host and remote platform 221 // if they use different path styles. 222 if (auto remapped = RemapPath(NormalizePath(orig_spec.GetPath()), 223 /*only_if_exists=*/true)) 224 return remapped; 225 226 return {}; 227 } 228 229 bool PathMappingList::Replace(llvm::StringRef path, llvm::StringRef new_path, 230 bool notify) { 231 uint32_t idx = FindIndexForPath(path); 232 if (idx < m_pairs.size()) { 233 ++m_mod_id; 234 m_pairs[idx].second = ConstString(new_path); 235 if (notify && m_callback) 236 m_callback(*this, m_callback_baton); 237 return true; 238 } 239 return false; 240 } 241 242 bool PathMappingList::Remove(ConstString path, bool notify) { 243 iterator pos = FindIteratorForPath(path); 244 if (pos != m_pairs.end()) { 245 ++m_mod_id; 246 m_pairs.erase(pos); 247 if (notify && m_callback) 248 m_callback(*this, m_callback_baton); 249 return true; 250 } 251 return false; 252 } 253 254 PathMappingList::const_iterator 255 PathMappingList::FindIteratorForPath(ConstString path) const { 256 const_iterator pos; 257 const_iterator begin = m_pairs.begin(); 258 const_iterator end = m_pairs.end(); 259 260 for (pos = begin; pos != end; ++pos) { 261 if (pos->first == path) 262 break; 263 } 264 return pos; 265 } 266 267 PathMappingList::iterator 268 PathMappingList::FindIteratorForPath(ConstString path) { 269 iterator pos; 270 iterator begin = m_pairs.begin(); 271 iterator end = m_pairs.end(); 272 273 for (pos = begin; pos != end; ++pos) { 274 if (pos->first == path) 275 break; 276 } 277 return pos; 278 } 279 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; 285 return true; 286 } 287 return false; 288 } 289 290 uint32_t PathMappingList::FindIndexForPath(llvm::StringRef orig_path) const { 291 const ConstString path = ConstString(NormalizePath(orig_path)); 292 const_iterator pos; 293 const_iterator begin = m_pairs.begin(); 294 const_iterator end = m_pairs.end(); 295 296 for (pos = begin; pos != end; ++pos) { 297 if (pos->first == path) 298 return std::distance(begin, pos); 299 } 300 return UINT32_MAX; 301 } 302