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/Host/FileSystem.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 #include "lldb/lldb-private-enumerations.h" 24 25 using namespace lldb; 26 using namespace lldb_private; 27 28 namespace { 29 // We must normalize our path pairs that we store because if we don't then 30 // things won't always work. We found a case where if we did: 31 // (lldb) settings set target.source-map . /tmp 32 // We would store a path pairs of "." and "/tmp" as raw strings. If the debug 33 // info contains "./foo/bar.c", the path will get normalized to "foo/bar.c". 34 // When PathMappingList::RemapPath() is called, it expects the path to start 35 // with the raw path pair, which doesn't work anymore because the paths have 36 // been normalized when the debug info was loaded. So we need to store 37 // nomalized path pairs to ensure things match up. 38 ConstString NormalizePath(const ConstString &path) { 39 // If we use "path" to construct a FileSpec, it will normalize the path for 40 // us. We then grab the string and turn it back into a ConstString. 41 return ConstString(FileSpec(path.GetStringRef()).GetPath()); 42 } 43 } 44 //---------------------------------------------------------------------- 45 // PathMappingList constructor 46 //---------------------------------------------------------------------- 47 PathMappingList::PathMappingList() 48 : m_pairs(), m_callback(nullptr), m_callback_baton(nullptr), m_mod_id(0) {} 49 50 PathMappingList::PathMappingList(ChangedCallback callback, void *callback_baton) 51 : m_pairs(), m_callback(callback), m_callback_baton(callback_baton), 52 m_mod_id(0) {} 53 54 PathMappingList::PathMappingList(const PathMappingList &rhs) 55 : m_pairs(rhs.m_pairs), m_callback(nullptr), m_callback_baton(nullptr), 56 m_mod_id(0) {} 57 58 const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) { 59 if (this != &rhs) { 60 m_pairs = rhs.m_pairs; 61 m_callback = nullptr; 62 m_callback_baton = nullptr; 63 m_mod_id = rhs.m_mod_id; 64 } 65 return *this; 66 } 67 68 PathMappingList::~PathMappingList() = default; 69 70 void PathMappingList::Append(const ConstString &path, 71 const ConstString &replacement, bool notify) { 72 ++m_mod_id; 73 m_pairs.emplace_back(pair(NormalizePath(path), NormalizePath(replacement))); 74 if (notify && m_callback) 75 m_callback(*this, m_callback_baton); 76 } 77 78 void PathMappingList::Append(const PathMappingList &rhs, bool notify) { 79 ++m_mod_id; 80 if (!rhs.m_pairs.empty()) { 81 const_iterator pos, end = rhs.m_pairs.end(); 82 for (pos = rhs.m_pairs.begin(); pos != end; ++pos) 83 m_pairs.push_back(*pos); 84 if (notify && m_callback) 85 m_callback(*this, m_callback_baton); 86 } 87 } 88 89 void PathMappingList::Insert(const ConstString &path, 90 const ConstString &replacement, uint32_t index, 91 bool notify) { 92 ++m_mod_id; 93 iterator insert_iter; 94 if (index >= m_pairs.size()) 95 insert_iter = m_pairs.end(); 96 else 97 insert_iter = m_pairs.begin() + index; 98 m_pairs.emplace(insert_iter, pair(NormalizePath(path), 99 NormalizePath(replacement))); 100 if (notify && m_callback) 101 m_callback(*this, m_callback_baton); 102 } 103 104 bool PathMappingList::Replace(const ConstString &path, 105 const ConstString &replacement, uint32_t index, 106 bool notify) { 107 if (index >= m_pairs.size()) 108 return false; 109 ++m_mod_id; 110 m_pairs[index] = pair(NormalizePath(path), NormalizePath(replacement)); 111 if (notify && m_callback) 112 m_callback(*this, m_callback_baton); 113 return true; 114 } 115 116 bool PathMappingList::Remove(size_t index, bool notify) { 117 if (index >= m_pairs.size()) 118 return false; 119 120 ++m_mod_id; 121 iterator iter = m_pairs.begin() + index; 122 m_pairs.erase(iter); 123 if (notify && m_callback) 124 m_callback(*this, m_callback_baton); 125 return true; 126 } 127 128 // For clients which do not need the pair index dumped, pass a pair_index >= 0 129 // to only dump the indicated pair. 130 void PathMappingList::Dump(Stream *s, int pair_index) { 131 unsigned int numPairs = m_pairs.size(); 132 133 if (pair_index < 0) { 134 unsigned int index; 135 for (index = 0; index < numPairs; ++index) 136 s->Printf("[%d] \"%s\" -> \"%s\"\n", index, 137 m_pairs[index].first.GetCString(), 138 m_pairs[index].second.GetCString()); 139 } else { 140 if (static_cast<unsigned int>(pair_index) < numPairs) 141 s->Printf("%s -> %s", m_pairs[pair_index].first.GetCString(), 142 m_pairs[pair_index].second.GetCString()); 143 } 144 } 145 146 void PathMappingList::Clear(bool notify) { 147 if (!m_pairs.empty()) 148 ++m_mod_id; 149 m_pairs.clear(); 150 if (notify && m_callback) 151 m_callback(*this, m_callback_baton); 152 } 153 154 bool PathMappingList::RemapPath(const ConstString &path, 155 ConstString &new_path) const { 156 std::string remapped; 157 if (RemapPath(path.GetStringRef(), remapped)) { 158 new_path.SetString(remapped); 159 return true; 160 } 161 return false; 162 } 163 164 bool PathMappingList::RemapPath(llvm::StringRef path, 165 std::string &new_path) const { 166 if (m_pairs.empty() || path.empty()) 167 return false; 168 LazyBool path_is_relative = eLazyBoolCalculate; 169 for (const auto &it : m_pairs) { 170 auto prefix = it.first.GetStringRef(); 171 if (!path.consume_front(prefix)) { 172 // Relative paths won't have a leading "./" in them unless "." is the 173 // only thing in the relative path so we need to work around "." 174 // carefully. 175 if (prefix != ".") 176 continue; 177 // We need to figure out if the "path" argument is relative. If it is, 178 // then we should remap, else skip this entry. 179 if (path_is_relative == eLazyBoolCalculate) { 180 path_is_relative = 181 FileSpec(path).IsRelative() ? eLazyBoolYes : eLazyBoolNo; 182 } 183 if (!path_is_relative) 184 continue; 185 } 186 FileSpec remapped(it.second.GetStringRef()); 187 remapped.AppendPathComponent(path); 188 new_path = remapped.GetPath(); 189 return true; 190 } 191 return false; 192 } 193 194 bool PathMappingList::ReverseRemapPath(const FileSpec &file, FileSpec &fixed) const { 195 std::string path = file.GetPath(); 196 llvm::StringRef path_ref(path); 197 for (const auto &it : m_pairs) { 198 if (!path_ref.consume_front(it.second.GetStringRef())) 199 continue; 200 fixed.SetFile(it.first.GetStringRef(), FileSpec::Style::native); 201 fixed.AppendPathComponent(path_ref); 202 return true; 203 } 204 return false; 205 } 206 207 bool PathMappingList::FindFile(const FileSpec &orig_spec, 208 FileSpec &new_spec) const { 209 if (!m_pairs.empty()) { 210 char orig_path[PATH_MAX]; 211 const size_t orig_path_len = 212 orig_spec.GetPath(orig_path, sizeof(orig_path)); 213 if (orig_path_len > 0) { 214 const_iterator pos, end = m_pairs.end(); 215 for (pos = m_pairs.begin(); pos != end; ++pos) { 216 const size_t prefix_len = pos->first.GetLength(); 217 218 if (orig_path_len >= prefix_len) { 219 if (::strncmp(pos->first.GetCString(), orig_path, prefix_len) == 0) { 220 new_spec.SetFile(pos->second.GetCString(), FileSpec::Style::native); 221 new_spec.AppendPathComponent(orig_path + prefix_len); 222 if (FileSystem::Instance().Exists(new_spec)) 223 return true; 224 } 225 } 226 } 227 } 228 } 229 new_spec.Clear(); 230 return false; 231 } 232 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()) { 237 ++m_mod_id; 238 m_pairs[idx].second = new_path; 239 if (notify && m_callback) 240 m_callback(*this, m_callback_baton); 241 return true; 242 } 243 return false; 244 } 245 246 bool PathMappingList::Remove(const ConstString &path, bool notify) { 247 iterator pos = FindIteratorForPath(path); 248 if (pos != m_pairs.end()) { 249 ++m_mod_id; 250 m_pairs.erase(pos); 251 if (notify && m_callback) 252 m_callback(*this, m_callback_baton); 253 return true; 254 } 255 return false; 256 } 257 258 PathMappingList::const_iterator 259 PathMappingList::FindIteratorForPath(const ConstString &path) const { 260 const_iterator pos; 261 const_iterator begin = m_pairs.begin(); 262 const_iterator end = m_pairs.end(); 263 264 for (pos = begin; pos != end; ++pos) { 265 if (pos->first == path) 266 break; 267 } 268 return pos; 269 } 270 271 PathMappingList::iterator 272 PathMappingList::FindIteratorForPath(const ConstString &path) { 273 iterator pos; 274 iterator begin = m_pairs.begin(); 275 iterator end = m_pairs.end(); 276 277 for (pos = begin; pos != end; ++pos) { 278 if (pos->first == path) 279 break; 280 } 281 return pos; 282 } 283 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; 289 return true; 290 } 291 return false; 292 } 293 294 uint32_t PathMappingList::FindIndexForPath(const ConstString &orig_path) const { 295 const ConstString path = NormalizePath(orig_path); 296 const_iterator pos; 297 const_iterator begin = m_pairs.begin(); 298 const_iterator end = m_pairs.end(); 299 300 for (pos = begin; pos != end; ++pos) { 301 if (pos->first == path) 302 return std::distance(begin, pos); 303 } 304 return UINT32_MAX; 305 } 306