Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 |
Eugene Zelenko | 9394d772 | 2016-02-18 00:10:17 +0000 | [diff] [blame] | 12 | #include <climits> |
| 13 | #include <cstring> |
| 14 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | // Other libraries and framework includes |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 16 | // Project includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Error.h" |
| 18 | #include "lldb/Core/Stream.h" |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 19 | #include "lldb/Host/FileSpec.h" |
Zachary Turner | f343968f | 2016-08-09 23:06:08 +0000 | [diff] [blame] | 20 | #include "lldb/Host/PosixApi.h" |
Eli Friedman | 48862d4 | 2010-06-09 09:32:42 +0000 | [diff] [blame] | 21 | #include "lldb/Target/PathMappingList.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
| 26 | //---------------------------------------------------------------------- |
| 27 | // PathMappingList constructor |
| 28 | //---------------------------------------------------------------------- |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 29 | PathMappingList::PathMappingList () : |
Eugene Zelenko | 9394d772 | 2016-02-18 00:10:17 +0000 | [diff] [blame] | 30 | m_pairs(), |
| 31 | m_callback(nullptr), |
| 32 | m_callback_baton(nullptr), |
| 33 | m_mod_id(0) |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
Greg Clayton | 4a89501 | 2013-03-14 22:52:17 +0000 | [diff] [blame] | 37 | PathMappingList::PathMappingList (ChangedCallback callback, |
| 38 | void *callback_baton) : |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | m_pairs (), |
| 40 | m_callback (callback), |
Greg Clayton | 4a89501 | 2013-03-14 22:52:17 +0000 | [diff] [blame] | 41 | m_callback_baton (callback_baton), |
| 42 | m_mod_id (0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 46 | PathMappingList::PathMappingList (const PathMappingList &rhs) : |
Eugene Zelenko | 9394d772 | 2016-02-18 00:10:17 +0000 | [diff] [blame] | 47 | m_pairs(rhs.m_pairs), |
| 48 | m_callback(nullptr), |
| 49 | m_callback_baton(nullptr), |
| 50 | m_mod_id(0) |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 51 | { |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | const PathMappingList & |
| 55 | PathMappingList::operator =(const PathMappingList &rhs) |
| 56 | { |
| 57 | if (this != &rhs) |
| 58 | { |
| 59 | m_pairs = rhs.m_pairs; |
Eugene Zelenko | 9394d772 | 2016-02-18 00:10:17 +0000 | [diff] [blame] | 60 | m_callback = nullptr; |
| 61 | m_callback_baton = nullptr; |
Greg Clayton | 4a89501 | 2013-03-14 22:52:17 +0000 | [diff] [blame] | 62 | m_mod_id = rhs.m_mod_id; |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 63 | } |
| 64 | return *this; |
| 65 | } |
| 66 | |
Eugene Zelenko | 9394d772 | 2016-02-18 00:10:17 +0000 | [diff] [blame] | 67 | PathMappingList::~PathMappingList() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | |
| 69 | void |
| 70 | PathMappingList::Append (const ConstString &path, |
| 71 | const ConstString &replacement, |
| 72 | bool notify) |
| 73 | { |
Greg Clayton | 4a89501 | 2013-03-14 22:52:17 +0000 | [diff] [blame] | 74 | ++m_mod_id; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 75 | m_pairs.push_back(pair(path, replacement)); |
| 76 | if (notify && m_callback) |
| 77 | m_callback (*this, m_callback_baton); |
| 78 | } |
| 79 | |
| 80 | void |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 81 | PathMappingList::Append (const PathMappingList &rhs, bool notify) |
| 82 | { |
Greg Clayton | 4a89501 | 2013-03-14 22:52:17 +0000 | [diff] [blame] | 83 | ++m_mod_id; |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 95 | PathMappingList::Insert (const ConstString &path, |
| 96 | const ConstString &replacement, |
| 97 | uint32_t index, |
| 98 | bool notify) |
| 99 | { |
Greg Clayton | 4a89501 | 2013-03-14 22:52:17 +0000 | [diff] [blame] | 100 | ++m_mod_id; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 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; |
Greg Clayton | 4a89501 | 2013-03-14 22:52:17 +0000 | [diff] [blame] | 120 | ++m_mod_id; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 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 |
Zachary Turner | a746e8e | 2014-07-02 17:24:07 +0000 | [diff] [blame] | 128 | PathMappingList::Remove (size_t index, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 129 | { |
Zachary Turner | a746e8e | 2014-07-02 17:24:07 +0000 | [diff] [blame] | 130 | if (index >= m_pairs.size()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 131 | return false; |
| 132 | |
Greg Clayton | 4a89501 | 2013-03-14 22:52:17 +0000 | [diff] [blame] | 133 | ++m_mod_id; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Johnny Chen | 64bab48 | 2011-12-12 21:59:28 +0000 | [diff] [blame] | 141 | // For clients which do not need the pair index dumped, pass a pair_index >= 0 |
| 142 | // to only dump the indicated pair. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | void |
Johnny Chen | 64bab48 | 2011-12-12 21:59:28 +0000 | [diff] [blame] | 144 | PathMappingList::Dump (Stream *s, int pair_index) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 145 | { |
| 146 | unsigned int numPairs = m_pairs.size(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | |
Johnny Chen | 64bab48 | 2011-12-12 21:59:28 +0000 | [diff] [blame] | 148 | if (pair_index < 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | { |
Johnny Chen | 64bab48 | 2011-12-12 21:59:28 +0000 | [diff] [blame] | 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 | { |
Saleem Abdulrasool | 3985c8c | 2014-04-02 03:51:35 +0000 | [diff] [blame] | 157 | if (static_cast<unsigned int>(pair_index) < numPairs) |
Johnny Chen | 64bab48 | 2011-12-12 21:59:28 +0000 | [diff] [blame] | 158 | s->Printf("%s -> %s", |
| 159 | m_pairs[pair_index].first.GetCString(), m_pairs[pair_index].second.GetCString()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | PathMappingList::Clear (bool notify) |
| 165 | { |
Greg Clayton | 4a89501 | 2013-03-14 22:52:17 +0000 | [diff] [blame] | 166 | if (!m_pairs.empty()) |
| 167 | ++m_mod_id; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | m_pairs.clear(); |
| 169 | if (notify && m_callback) |
| 170 | m_callback (*this, m_callback_baton); |
| 171 | } |
| 172 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 173 | bool |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 174 | PathMappingList::RemapPath (const ConstString &path, ConstString &new_path) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 175 | { |
Sean Callanan | f601503 | 2012-09-26 01:28:11 +0000 | [diff] [blame] | 176 | const char *path_cstr = path.GetCString(); |
| 177 | |
| 178 | if (!path_cstr) |
| 179 | return false; |
| 180 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Sean Callanan | f601503 | 2012-09-26 01:28:11 +0000 | [diff] [blame] | 186 | if (::strncmp (pos->first.GetCString(), path_cstr, prefixLen) == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | } |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 196 | |
| 197 | bool |
Greg Clayton | f9be693 | 2012-03-19 22:22:41 +0000 | [diff] [blame] | 198 | PathMappingList::RemapPath (const char *path, std::string &new_path) const |
| 199 | { |
Eugene Zelenko | 9394d772 | 2016-02-18 00:10:17 +0000 | [diff] [blame] | 200 | if (m_pairs.empty() || path == nullptr || path[0] == '\0') |
Greg Clayton | f9be693 | 2012-03-19 22:22:41 +0000 | [diff] [blame] | 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 |
Tamas Berghammer | b0b1ea3 | 2016-03-04 11:26:44 +0000 | [diff] [blame] | 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 | { |
Jim Ingham | 291fd35 | 2016-08-23 17:13:33 +0000 | [diff] [blame^] | 227 | // FIXME: This should be using FileSpec API's to do the path appending. |
Tamas Berghammer | b0b1ea3 | 2016-03-04 11:26:44 +0000 | [diff] [blame] | 228 | const size_t prefixLen = it.second.GetLength(); |
| 229 | if (::strncmp (it.second.GetCString(), path_cstr, prefixLen) == 0) |
| 230 | { |
| 231 | std::string new_path_str (it.first.GetCString()); |
| 232 | new_path_str.append(path.GetCString() + prefixLen); |
| 233 | new_path.SetCString(new_path_str.c_str()); |
| 234 | return true; |
| 235 | } |
| 236 | } |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | bool |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 241 | PathMappingList::FindFile (const FileSpec &orig_spec, FileSpec &new_spec) const |
| 242 | { |
| 243 | if (!m_pairs.empty()) |
| 244 | { |
| 245 | char orig_path[PATH_MAX]; |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 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 | { |
Jim Ingham | 291fd35 | 2016-08-23 17:13:33 +0000 | [diff] [blame^] | 258 | new_spec.SetFile(pos->second.GetCString(), false); |
| 259 | new_spec.AppendPathComponent(orig_path+prefix_len); |
| 260 | if (new_spec.Exists()) |
| 261 | return true; |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | new_spec.Clear(); |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | bool |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 272 | PathMappingList::Replace (const ConstString &path, const ConstString &new_path, bool notify) |
| 273 | { |
| 274 | uint32_t idx = FindIndexForPath (path); |
| 275 | if (idx < m_pairs.size()) |
| 276 | { |
Greg Clayton | 4a89501 | 2013-03-14 22:52:17 +0000 | [diff] [blame] | 277 | ++m_mod_id; |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 278 | m_pairs[idx].second = new_path; |
| 279 | if (notify && m_callback) |
| 280 | m_callback (*this, m_callback_baton); |
| 281 | return true; |
| 282 | } |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | bool |
| 287 | PathMappingList::Remove (const ConstString &path, bool notify) |
| 288 | { |
| 289 | iterator pos = FindIteratorForPath (path); |
| 290 | if (pos != m_pairs.end()) |
| 291 | { |
Greg Clayton | 4a89501 | 2013-03-14 22:52:17 +0000 | [diff] [blame] | 292 | ++m_mod_id; |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 293 | m_pairs.erase (pos); |
| 294 | if (notify && m_callback) |
| 295 | m_callback (*this, m_callback_baton); |
| 296 | return true; |
| 297 | } |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | PathMappingList::const_iterator |
| 302 | PathMappingList::FindIteratorForPath (const ConstString &path) const |
| 303 | { |
| 304 | const_iterator pos; |
| 305 | const_iterator begin = m_pairs.begin(); |
| 306 | const_iterator end = m_pairs.end(); |
| 307 | |
| 308 | for (pos = begin; pos != end; ++pos) |
| 309 | { |
| 310 | if (pos->first == path) |
| 311 | break; |
| 312 | } |
| 313 | return pos; |
| 314 | } |
| 315 | |
| 316 | PathMappingList::iterator |
| 317 | PathMappingList::FindIteratorForPath (const ConstString &path) |
| 318 | { |
| 319 | iterator pos; |
| 320 | iterator begin = m_pairs.begin(); |
| 321 | iterator end = m_pairs.end(); |
| 322 | |
| 323 | for (pos = begin; pos != end; ++pos) |
| 324 | { |
| 325 | if (pos->first == path) |
| 326 | break; |
| 327 | } |
| 328 | return pos; |
| 329 | } |
| 330 | |
| 331 | bool |
| 332 | PathMappingList::GetPathsAtIndex (uint32_t idx, ConstString &path, ConstString &new_path) const |
| 333 | { |
| 334 | if (idx < m_pairs.size()) |
| 335 | { |
| 336 | path = m_pairs[idx].first; |
| 337 | new_path = m_pairs[idx].second; |
| 338 | return true; |
| 339 | } |
| 340 | return false; |
| 341 | } |
| 342 | |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 343 | uint32_t |
| 344 | PathMappingList::FindIndexForPath (const ConstString &path) const |
| 345 | { |
| 346 | const_iterator pos; |
| 347 | const_iterator begin = m_pairs.begin(); |
| 348 | const_iterator end = m_pairs.end(); |
| 349 | |
| 350 | for (pos = begin; pos != end; ++pos) |
| 351 | { |
| 352 | if (pos->first == path) |
| 353 | return std::distance (begin, pos); |
| 354 | } |
| 355 | return UINT32_MAX; |
| 356 | } |