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/Core/Error.h" 18 #include "lldb/Core/Stream.h" 19 #include "lldb/Host/FileSpec.h" 20 #include "lldb/Host/PosixApi.h" 21 #include "lldb/Target/PathMappingList.h" 22 23 using namespace lldb; 24 using namespace lldb_private; 25 26 //---------------------------------------------------------------------- 27 // PathMappingList constructor 28 //---------------------------------------------------------------------- 29 PathMappingList::PathMappingList () : 30 m_pairs(), 31 m_callback(nullptr), 32 m_callback_baton(nullptr), 33 m_mod_id(0) 34 { 35 } 36 37 PathMappingList::PathMappingList (ChangedCallback callback, 38 void *callback_baton) : 39 m_pairs (), 40 m_callback (callback), 41 m_callback_baton (callback_baton), 42 m_mod_id (0) 43 { 44 } 45 46 PathMappingList::PathMappingList (const PathMappingList &rhs) : 47 m_pairs(rhs.m_pairs), 48 m_callback(nullptr), 49 m_callback_baton(nullptr), 50 m_mod_id(0) 51 { 52 } 53 54 const PathMappingList & 55 PathMappingList::operator =(const PathMappingList &rhs) 56 { 57 if (this != &rhs) 58 { 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 70 PathMappingList::Append (const ConstString &path, 71 const ConstString &replacement, 72 bool notify) 73 { 74 ++m_mod_id; 75 m_pairs.push_back(pair(path, replacement)); 76 if (notify && m_callback) 77 m_callback (*this, m_callback_baton); 78 } 79 80 void 81 PathMappingList::Append (const PathMappingList &rhs, bool notify) 82 { 83 ++m_mod_id; 84 if (!rhs.m_pairs.empty()) 85 { 86 const_iterator pos, end = rhs.m_pairs.end(); 87 for (pos = rhs.m_pairs.begin(); pos != end; ++pos) 88 m_pairs.push_back(*pos); 89 if (notify && m_callback) 90 m_callback (*this, m_callback_baton); 91 } 92 } 93 94 void 95 PathMappingList::Insert (const ConstString &path, 96 const ConstString &replacement, 97 uint32_t index, 98 bool notify) 99 { 100 ++m_mod_id; 101 iterator insert_iter; 102 if (index >= m_pairs.size()) 103 insert_iter = m_pairs.end(); 104 else 105 insert_iter = m_pairs.begin() + index; 106 m_pairs.insert(insert_iter, pair(path, replacement)); 107 if (notify && m_callback) 108 m_callback (*this, m_callback_baton); 109 } 110 111 bool 112 PathMappingList::Replace (const ConstString &path, 113 const ConstString &replacement, 114 uint32_t index, 115 bool notify) 116 { 117 iterator insert_iter; 118 if (index >= m_pairs.size()) 119 return false; 120 ++m_mod_id; 121 m_pairs[index] = pair(path, replacement); 122 if (notify && m_callback) 123 m_callback (*this, m_callback_baton); 124 return true; 125 } 126 127 bool 128 PathMappingList::Remove (size_t index, bool notify) 129 { 130 if (index >= m_pairs.size()) 131 return false; 132 133 ++m_mod_id; 134 iterator iter = m_pairs.begin() + index; 135 m_pairs.erase(iter); 136 if (notify && m_callback) 137 m_callback (*this, m_callback_baton); 138 return true; 139 } 140 141 // For clients which do not need the pair index dumped, pass a pair_index >= 0 142 // to only dump the indicated pair. 143 void 144 PathMappingList::Dump (Stream *s, int pair_index) 145 { 146 unsigned int numPairs = m_pairs.size(); 147 148 if (pair_index < 0) 149 { 150 unsigned int index; 151 for (index = 0; index < numPairs; ++index) 152 s->Printf("[%d] \"%s\" -> \"%s\"\n", 153 index, m_pairs[index].first.GetCString(), m_pairs[index].second.GetCString()); 154 } 155 else 156 { 157 if (static_cast<unsigned int>(pair_index) < numPairs) 158 s->Printf("%s -> %s", 159 m_pairs[pair_index].first.GetCString(), m_pairs[pair_index].second.GetCString()); 160 } 161 } 162 163 void 164 PathMappingList::Clear (bool notify) 165 { 166 if (!m_pairs.empty()) 167 ++m_mod_id; 168 m_pairs.clear(); 169 if (notify && m_callback) 170 m_callback (*this, m_callback_baton); 171 } 172 173 bool 174 PathMappingList::RemapPath (const ConstString &path, ConstString &new_path) const 175 { 176 const char *path_cstr = path.GetCString(); 177 178 if (!path_cstr) 179 return false; 180 181 const_iterator pos, end = m_pairs.end(); 182 for (pos = m_pairs.begin(); pos != end; ++pos) 183 { 184 const size_t prefixLen = pos->first.GetLength(); 185 186 if (::strncmp (pos->first.GetCString(), path_cstr, prefixLen) == 0) 187 { 188 std::string new_path_str (pos->second.GetCString()); 189 new_path_str.append(path.GetCString() + prefixLen); 190 new_path.SetCString(new_path_str.c_str()); 191 return true; 192 } 193 } 194 return false; 195 } 196 197 bool 198 PathMappingList::RemapPath (const char *path, std::string &new_path) const 199 { 200 if (m_pairs.empty() || path == nullptr || path[0] == '\0') 201 return false; 202 203 const_iterator pos, end = m_pairs.end(); 204 for (pos = m_pairs.begin(); pos != end; ++pos) 205 { 206 const size_t prefix_len = pos->first.GetLength(); 207 208 if (::strncmp (pos->first.GetCString(), path, prefix_len) == 0) 209 { 210 new_path = pos->second.GetCString(); 211 new_path.append(path + prefix_len); 212 return true; 213 } 214 } 215 return false; 216 } 217 218 bool 219 PathMappingList::ReverseRemapPath (const ConstString &path, ConstString &new_path) const 220 { 221 const char *path_cstr = path.GetCString(); 222 if (!path_cstr) 223 return false; 224 225 for (const auto& it : m_pairs) 226 { 227 const size_t prefixLen = it.second.GetLength(); 228 if (::strncmp (it.second.GetCString(), path_cstr, prefixLen) == 0) 229 { 230 std::string new_path_str (it.first.GetCString()); 231 new_path_str.append(path.GetCString() + prefixLen); 232 new_path.SetCString(new_path_str.c_str()); 233 return true; 234 } 235 } 236 return false; 237 } 238 239 bool 240 PathMappingList::FindFile (const FileSpec &orig_spec, FileSpec &new_spec) const 241 { 242 if (!m_pairs.empty()) 243 { 244 char orig_path[PATH_MAX]; 245 char new_path[PATH_MAX]; 246 const size_t orig_path_len = orig_spec.GetPath (orig_path, sizeof(orig_path)); 247 if (orig_path_len > 0) 248 { 249 const_iterator pos, end = m_pairs.end(); 250 for (pos = m_pairs.begin(); pos != end; ++pos) 251 { 252 const size_t prefix_len = pos->first.GetLength(); 253 254 if (orig_path_len >= prefix_len) 255 { 256 if (::strncmp (pos->first.GetCString(), orig_path, prefix_len) == 0) 257 { 258 const size_t new_path_len = snprintf(new_path, sizeof(new_path), "%s/%s", pos->second.GetCString(), orig_path + prefix_len); 259 if (new_path_len < sizeof(new_path)) 260 { 261 new_spec.SetFile (new_path, true); 262 if (new_spec.Exists()) 263 return true; 264 } 265 } 266 } 267 } 268 } 269 } 270 new_spec.Clear(); 271 return false; 272 } 273 274 bool 275 PathMappingList::Replace (const ConstString &path, const ConstString &new_path, bool notify) 276 { 277 uint32_t idx = FindIndexForPath (path); 278 if (idx < m_pairs.size()) 279 { 280 ++m_mod_id; 281 m_pairs[idx].second = new_path; 282 if (notify && m_callback) 283 m_callback (*this, m_callback_baton); 284 return true; 285 } 286 return false; 287 } 288 289 bool 290 PathMappingList::Remove (const ConstString &path, bool notify) 291 { 292 iterator pos = FindIteratorForPath (path); 293 if (pos != m_pairs.end()) 294 { 295 ++m_mod_id; 296 m_pairs.erase (pos); 297 if (notify && m_callback) 298 m_callback (*this, m_callback_baton); 299 return true; 300 } 301 return false; 302 } 303 304 PathMappingList::const_iterator 305 PathMappingList::FindIteratorForPath (const ConstString &path) const 306 { 307 const_iterator pos; 308 const_iterator begin = m_pairs.begin(); 309 const_iterator end = m_pairs.end(); 310 311 for (pos = begin; pos != end; ++pos) 312 { 313 if (pos->first == path) 314 break; 315 } 316 return pos; 317 } 318 319 PathMappingList::iterator 320 PathMappingList::FindIteratorForPath (const ConstString &path) 321 { 322 iterator pos; 323 iterator begin = m_pairs.begin(); 324 iterator end = m_pairs.end(); 325 326 for (pos = begin; pos != end; ++pos) 327 { 328 if (pos->first == path) 329 break; 330 } 331 return pos; 332 } 333 334 bool 335 PathMappingList::GetPathsAtIndex (uint32_t idx, ConstString &path, ConstString &new_path) const 336 { 337 if (idx < m_pairs.size()) 338 { 339 path = m_pairs[idx].first; 340 new_path = m_pairs[idx].second; 341 return true; 342 } 343 return false; 344 } 345 346 uint32_t 347 PathMappingList::FindIndexForPath (const ConstString &path) const 348 { 349 const_iterator pos; 350 const_iterator begin = m_pairs.begin(); 351 const_iterator end = m_pairs.end(); 352 353 for (pos = begin; pos != end; ++pos) 354 { 355 if (pos->first == path) 356 return std::distance (begin, pos); 357 } 358 return UINT32_MAX; 359 } 360