1 //===-- SourceManager.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 #include "lldb/lldb-python.h" 11 12 #include "lldb/Core/SourceManager.h" 13 14 // C Includes 15 // C++ Includes 16 // Other libraries and framework includes 17 // Project includes 18 #include "lldb/Core/DataBuffer.h" 19 #include "lldb/Core/Debugger.h" 20 #include "lldb/Core/Module.h" 21 #include "lldb/Core/RegularExpression.h" 22 #include "lldb/Core/Stream.h" 23 #include "lldb/Symbol/ClangNamespaceDecl.h" 24 #include "lldb/Symbol/CompileUnit.h" 25 #include "lldb/Symbol/Function.h" 26 #include "lldb/Symbol/SymbolContext.h" 27 #include "lldb/Target/Target.h" 28 29 using namespace lldb; 30 using namespace lldb_private; 31 32 33 static inline bool is_newline_char(char ch) 34 { 35 return ch == '\n' || ch == '\r'; 36 } 37 38 39 //---------------------------------------------------------------------- 40 // SourceManager constructor 41 //---------------------------------------------------------------------- 42 SourceManager::SourceManager(const TargetSP &target_sp) : 43 m_last_file_sp (), 44 m_last_line (0), 45 m_last_count (0), 46 m_default_set(false), 47 m_target_wp (target_sp), 48 m_debugger_wp(target_sp->GetDebugger().shared_from_this()) 49 { 50 } 51 52 SourceManager::SourceManager(const DebuggerSP &debugger_sp) : 53 m_last_file_sp (), 54 m_last_line (0), 55 m_last_count (0), 56 m_default_set(false), 57 m_target_wp (), 58 m_debugger_wp (debugger_sp) 59 { 60 } 61 62 //---------------------------------------------------------------------- 63 // Destructor 64 //---------------------------------------------------------------------- 65 SourceManager::~SourceManager() 66 { 67 } 68 69 SourceManager::FileSP 70 SourceManager::GetFile (const FileSpec &file_spec) 71 { 72 bool same_as_previous = m_last_file_sp && m_last_file_sp->FileSpecMatches (file_spec); 73 74 DebuggerSP debugger_sp (m_debugger_wp.lock()); 75 FileSP file_sp; 76 if (same_as_previous) 77 file_sp = m_last_file_sp; 78 else if (debugger_sp) 79 file_sp = debugger_sp->GetSourceFileCache().FindSourceFile (file_spec); 80 81 TargetSP target_sp (m_target_wp.lock()); 82 83 // It the target source path map has been updated, get this file again so we 84 // can successfully remap the source file 85 if (target_sp && file_sp && file_sp->GetSourceMapModificationID() != target_sp->GetSourcePathMap().GetModificationID()) 86 file_sp.reset(); 87 88 // If file_sp is no good or it points to a non-existent file, reset it. 89 if (!file_sp || !file_sp->GetFileSpec().Exists()) 90 { 91 file_sp.reset (new File (file_spec, target_sp.get())); 92 93 if (debugger_sp) 94 debugger_sp->GetSourceFileCache().AddSourceFile(file_sp); 95 } 96 return file_sp; 97 } 98 99 size_t 100 SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile (uint32_t start_line, 101 uint32_t count, 102 uint32_t curr_line, 103 const char* current_line_cstr, 104 Stream *s, 105 const SymbolContextList *bp_locs) 106 { 107 if (count == 0) 108 return 0; 109 size_t return_value = 0; 110 if (start_line == 0) 111 { 112 if (m_last_line != 0 && m_last_line != UINT32_MAX) 113 start_line = m_last_line + m_last_count; 114 else 115 start_line = 1; 116 } 117 118 if (!m_default_set) 119 { 120 FileSpec tmp_spec; 121 uint32_t tmp_line; 122 GetDefaultFileAndLine(tmp_spec, tmp_line); 123 } 124 125 m_last_line = start_line; 126 m_last_count = count; 127 128 if (m_last_file_sp.get()) 129 { 130 const uint32_t end_line = start_line + count - 1; 131 for (uint32_t line = start_line; line <= end_line; ++line) 132 { 133 if (!m_last_file_sp->LineIsValid (line)) 134 { 135 m_last_line = UINT32_MAX; 136 break; 137 } 138 139 char prefix[32] = ""; 140 if (bp_locs) 141 { 142 uint32_t bp_count = bp_locs->NumLineEntriesWithLine (line); 143 144 if (bp_count > 0) 145 ::snprintf (prefix, sizeof (prefix), "[%u] ", bp_count); 146 else 147 ::snprintf (prefix, sizeof (prefix), " "); 148 } 149 150 return_value += s->Printf("%s%2.2s %-4u\t", 151 prefix, 152 line == curr_line ? current_line_cstr : "", 153 line); 154 size_t this_line_size = m_last_file_sp->DisplaySourceLines (line, 0, 0, s); 155 if (this_line_size == 0) 156 { 157 m_last_line = UINT32_MAX; 158 break; 159 } 160 else 161 return_value += this_line_size; 162 } 163 } 164 return return_value; 165 } 166 167 size_t 168 SourceManager::DisplaySourceLinesWithLineNumbers 169 ( 170 const FileSpec &file_spec, 171 uint32_t line, 172 uint32_t context_before, 173 uint32_t context_after, 174 const char* current_line_cstr, 175 Stream *s, 176 const SymbolContextList *bp_locs 177 ) 178 { 179 FileSP file_sp (GetFile (file_spec)); 180 181 uint32_t start_line; 182 uint32_t count = context_before + context_after + 1; 183 if (line > context_before) 184 start_line = line - context_before; 185 else 186 start_line = 1; 187 188 if (m_last_file_sp.get() != file_sp.get()) 189 { 190 if (line == 0) 191 m_last_line = 0; 192 m_last_file_sp = file_sp; 193 } 194 return DisplaySourceLinesWithLineNumbersUsingLastFile (start_line, count, line, current_line_cstr, s, bp_locs); 195 } 196 197 size_t 198 SourceManager::DisplayMoreWithLineNumbers (Stream *s, 199 uint32_t count, 200 bool reverse, 201 const SymbolContextList *bp_locs) 202 { 203 // If we get called before anybody has set a default file and line, then try to figure it out here. 204 const bool have_default_file_line = m_last_file_sp && m_last_line > 0; 205 if (!m_default_set) 206 { 207 FileSpec tmp_spec; 208 uint32_t tmp_line; 209 GetDefaultFileAndLine(tmp_spec, tmp_line); 210 } 211 212 if (m_last_file_sp) 213 { 214 if (m_last_line == UINT32_MAX) 215 return 0; 216 217 if (reverse && m_last_line == 1) 218 return 0; 219 220 if (count > 0) 221 m_last_count = count; 222 else if (m_last_count == 0) 223 m_last_count = 10; 224 225 if (m_last_line > 0) 226 { 227 if (reverse) 228 { 229 // If this is the first time we've done a reverse, then back up one more time so we end 230 // up showing the chunk before the last one we've shown: 231 if (m_last_line > m_last_count) 232 m_last_line -= m_last_count; 233 else 234 m_last_line = 1; 235 } 236 else if (have_default_file_line) 237 m_last_line += m_last_count; 238 } 239 else 240 m_last_line = 1; 241 242 return DisplaySourceLinesWithLineNumbersUsingLastFile (m_last_line, m_last_count, UINT32_MAX, "", s, bp_locs); 243 } 244 return 0; 245 } 246 247 bool 248 SourceManager::SetDefaultFileAndLine (const FileSpec &file_spec, uint32_t line) 249 { 250 FileSP old_file_sp = m_last_file_sp; 251 m_last_file_sp = GetFile (file_spec); 252 253 m_default_set = true; 254 if (m_last_file_sp) 255 { 256 m_last_line = line; 257 return true; 258 } 259 else 260 { 261 m_last_file_sp = old_file_sp; 262 return false; 263 } 264 } 265 266 bool 267 SourceManager::GetDefaultFileAndLine (FileSpec &file_spec, uint32_t &line) 268 { 269 if (m_last_file_sp) 270 { 271 file_spec = m_last_file_sp->GetFileSpec(); 272 line = m_last_line; 273 return true; 274 } 275 else if (!m_default_set) 276 { 277 TargetSP target_sp (m_target_wp.lock()); 278 279 if (target_sp) 280 { 281 // If nobody has set the default file and line then try here. If there's no executable, then we 282 // will try again later when there is one. Otherwise, if we can't find it we won't look again, 283 // somebody will have to set it (for instance when we stop somewhere...) 284 Module *executable_ptr = target_sp->GetExecutableModulePointer(); 285 if (executable_ptr) 286 { 287 SymbolContextList sc_list; 288 ConstString main_name("main"); 289 bool symbols_okay = false; // Force it to be a debug symbol. 290 bool inlines_okay = true; 291 bool append = false; 292 size_t num_matches = executable_ptr->FindFunctions (main_name, 293 NULL, 294 lldb::eFunctionNameTypeBase, 295 inlines_okay, 296 symbols_okay, 297 append, 298 sc_list); 299 for (size_t idx = 0; idx < num_matches; idx++) 300 { 301 SymbolContext sc; 302 sc_list.GetContextAtIndex(idx, sc); 303 if (sc.function) 304 { 305 lldb_private::LineEntry line_entry; 306 if (sc.function->GetAddressRange().GetBaseAddress().CalculateSymbolContextLineEntry (line_entry)) 307 { 308 SetDefaultFileAndLine (line_entry.file, 309 line_entry.line); 310 file_spec = m_last_file_sp->GetFileSpec(); 311 line = m_last_line; 312 return true; 313 } 314 } 315 } 316 } 317 } 318 } 319 return false; 320 } 321 322 void 323 SourceManager::FindLinesMatchingRegex (FileSpec &file_spec, 324 RegularExpression& regex, 325 uint32_t start_line, 326 uint32_t end_line, 327 std::vector<uint32_t> &match_lines) 328 { 329 match_lines.clear(); 330 FileSP file_sp = GetFile (file_spec); 331 if (!file_sp) 332 return; 333 return file_sp->FindLinesMatchingRegex (regex, start_line, end_line, match_lines); 334 } 335 336 SourceManager::File::File(const FileSpec &file_spec, Target *target) : 337 m_file_spec_orig (file_spec), 338 m_file_spec(file_spec), 339 m_mod_time (file_spec.GetModificationTime()), 340 m_source_map_mod_id (0), 341 m_data_sp(), 342 m_offsets() 343 { 344 if (!m_mod_time.IsValid()) 345 { 346 if (target) 347 { 348 m_source_map_mod_id = target->GetSourcePathMap().GetModificationID(); 349 350 if (!file_spec.GetDirectory() && file_spec.GetFilename()) 351 { 352 // If this is just a file name, lets see if we can find it in the target: 353 bool check_inlines = false; 354 SymbolContextList sc_list; 355 size_t num_matches = target->GetImages().ResolveSymbolContextForFilePath (file_spec.GetFilename().AsCString(), 356 0, 357 check_inlines, 358 lldb::eSymbolContextModule | lldb::eSymbolContextCompUnit, 359 sc_list); 360 bool got_multiple = false; 361 if (num_matches != 0) 362 { 363 if (num_matches > 1) 364 { 365 SymbolContext sc; 366 FileSpec *test_cu_spec = NULL; 367 368 for (unsigned i = 0; i < num_matches; i++) 369 { 370 sc_list.GetContextAtIndex(i, sc); 371 if (sc.comp_unit) 372 { 373 if (test_cu_spec) 374 { 375 if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit)) 376 got_multiple = true; 377 break; 378 } 379 else 380 test_cu_spec = sc.comp_unit; 381 } 382 } 383 } 384 if (!got_multiple) 385 { 386 SymbolContext sc; 387 sc_list.GetContextAtIndex (0, sc); 388 m_file_spec = sc.comp_unit; 389 m_mod_time = m_file_spec.GetModificationTime(); 390 } 391 } 392 } 393 // Try remapping if m_file_spec does not correspond to an existing file. 394 if (!m_file_spec.Exists()) 395 { 396 FileSpec new_file_spec; 397 // Check target specific source remappings first, then fall back to 398 // modules objects can have individual path remappings that were detected 399 // when the debug info for a module was found. 400 // then 401 if (target->GetSourcePathMap().FindFile (m_file_spec, new_file_spec) || 402 target->GetImages().FindSourceFile (m_file_spec, new_file_spec)) 403 { 404 m_file_spec = new_file_spec; 405 m_mod_time = m_file_spec.GetModificationTime(); 406 } 407 } 408 } 409 } 410 411 if (m_mod_time.IsValid()) 412 m_data_sp = m_file_spec.ReadFileContents (); 413 } 414 415 SourceManager::File::~File() 416 { 417 } 418 419 uint32_t 420 SourceManager::File::GetLineOffset (uint32_t line) 421 { 422 if (line == 0) 423 return UINT32_MAX; 424 425 if (line == 1) 426 return 0; 427 428 if (CalculateLineOffsets (line)) 429 { 430 if (line < m_offsets.size()) 431 return m_offsets[line - 1]; // yes we want "line - 1" in the index 432 } 433 return UINT32_MAX; 434 } 435 436 uint32_t 437 SourceManager::File::GetNumLines () 438 { 439 CalculateLineOffsets(); 440 return m_offsets.size(); 441 } 442 443 const char * 444 SourceManager::File::PeekLineData (uint32_t line) 445 { 446 if (!LineIsValid(line)) 447 return NULL; 448 449 size_t line_offset = GetLineOffset (line); 450 if (line_offset < m_data_sp->GetByteSize()) 451 return (const char *)m_data_sp->GetBytes() + line_offset; 452 return NULL; 453 } 454 455 uint32_t 456 SourceManager::File::GetLineLength (uint32_t line, bool include_newline_chars) 457 { 458 if (!LineIsValid(line)) 459 return false; 460 461 size_t start_offset = GetLineOffset (line); 462 size_t end_offset = GetLineOffset (line + 1); 463 if (end_offset == UINT32_MAX) 464 end_offset = m_data_sp->GetByteSize(); 465 466 if (end_offset > start_offset) 467 { 468 uint32_t length = end_offset - start_offset; 469 if (include_newline_chars == false) 470 { 471 const char *line_start = (const char *)m_data_sp->GetBytes() + start_offset; 472 while (length > 0) 473 { 474 const char last_char = line_start[length-1]; 475 if ((last_char == '\r') || (last_char == '\n')) 476 --length; 477 else 478 break; 479 } 480 } 481 return length; 482 } 483 return 0; 484 } 485 486 bool 487 SourceManager::File::LineIsValid (uint32_t line) 488 { 489 if (line == 0) 490 return false; 491 492 if (CalculateLineOffsets (line)) 493 return line < m_offsets.size(); 494 return false; 495 } 496 497 size_t 498 SourceManager::File::DisplaySourceLines (uint32_t line, uint32_t context_before, uint32_t context_after, Stream *s) 499 { 500 // TODO: use host API to sign up for file modifications to anything in our 501 // source cache and only update when we determine a file has been updated. 502 // For now we check each time we want to display info for the file. 503 TimeValue curr_mod_time (m_file_spec.GetModificationTime()); 504 505 if (curr_mod_time.IsValid() && m_mod_time != curr_mod_time) 506 { 507 m_mod_time = curr_mod_time; 508 m_data_sp = m_file_spec.ReadFileContents (); 509 m_offsets.clear(); 510 } 511 512 // Sanity check m_data_sp before proceeding. 513 if (!m_data_sp) 514 return 0; 515 516 const uint32_t start_line = line <= context_before ? 1 : line - context_before; 517 const uint32_t start_line_offset = GetLineOffset (start_line); 518 if (start_line_offset != UINT32_MAX) 519 { 520 const uint32_t end_line = line + context_after; 521 uint32_t end_line_offset = GetLineOffset (end_line + 1); 522 if (end_line_offset == UINT32_MAX) 523 end_line_offset = m_data_sp->GetByteSize(); 524 525 assert (start_line_offset <= end_line_offset); 526 size_t bytes_written = 0; 527 if (start_line_offset < end_line_offset) 528 { 529 size_t count = end_line_offset - start_line_offset; 530 const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset; 531 bytes_written = s->Write(cstr, count); 532 if (!is_newline_char(cstr[count-1])) 533 bytes_written += s->EOL(); 534 } 535 return bytes_written; 536 } 537 return 0; 538 } 539 540 void 541 SourceManager::File::FindLinesMatchingRegex (RegularExpression& regex, uint32_t start_line, uint32_t end_line, std::vector<uint32_t> &match_lines) 542 { 543 TimeValue curr_mod_time (m_file_spec.GetModificationTime()); 544 if (m_mod_time != curr_mod_time) 545 { 546 m_mod_time = curr_mod_time; 547 m_data_sp = m_file_spec.ReadFileContents (); 548 m_offsets.clear(); 549 } 550 551 match_lines.clear(); 552 553 if (!LineIsValid(start_line) || (end_line != UINT32_MAX && !LineIsValid(end_line))) 554 return; 555 if (start_line > end_line) 556 return; 557 558 for (uint32_t line_no = start_line; line_no < end_line; line_no++) 559 { 560 std::string buffer; 561 if (!GetLine (line_no, buffer)) 562 break; 563 if (regex.Execute(buffer.c_str())) 564 { 565 match_lines.push_back(line_no); 566 } 567 } 568 } 569 570 bool 571 SourceManager::File::FileSpecMatches (const FileSpec &file_spec) 572 { 573 return FileSpec::Equal (m_file_spec, file_spec, false); 574 } 575 576 bool 577 lldb_private::operator== (const SourceManager::File &lhs, const SourceManager::File &rhs) 578 { 579 if (lhs.m_file_spec == rhs.m_file_spec) 580 { 581 if (lhs.m_mod_time.IsValid()) 582 { 583 if (rhs.m_mod_time.IsValid()) 584 return lhs.m_mod_time == rhs.m_mod_time; 585 else 586 return false; 587 } 588 else if (rhs.m_mod_time.IsValid()) 589 return false; 590 else 591 return true; 592 } 593 else 594 return false; 595 } 596 597 bool 598 SourceManager::File::CalculateLineOffsets (uint32_t line) 599 { 600 line = UINT32_MAX; // TODO: take this line out when we support partial indexing 601 if (line == UINT32_MAX) 602 { 603 // Already done? 604 if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX) 605 return true; 606 607 if (m_offsets.empty()) 608 { 609 if (m_data_sp.get() == NULL) 610 return false; 611 612 const char *start = (char *)m_data_sp->GetBytes(); 613 if (start) 614 { 615 const char *end = start + m_data_sp->GetByteSize(); 616 617 // Calculate all line offsets from scratch 618 619 // Push a 1 at index zero to indicate the file has been completely indexed. 620 m_offsets.push_back(UINT32_MAX); 621 const char *s; 622 for (s = start; s < end; ++s) 623 { 624 char curr_ch = *s; 625 if (is_newline_char (curr_ch)) 626 { 627 if (s + 1 < end) 628 { 629 char next_ch = s[1]; 630 if (is_newline_char (next_ch)) 631 { 632 if (curr_ch != next_ch) 633 ++s; 634 } 635 } 636 m_offsets.push_back(s + 1 - start); 637 } 638 } 639 if (!m_offsets.empty()) 640 { 641 if (m_offsets.back() < end - start) 642 m_offsets.push_back(end - start); 643 } 644 return true; 645 } 646 } 647 else 648 { 649 // Some lines have been populated, start where we last left off 650 assert("Not implemented yet" == NULL); 651 } 652 653 } 654 else 655 { 656 // Calculate all line offsets up to "line" 657 assert("Not implemented yet" == NULL); 658 } 659 return false; 660 } 661 662 bool 663 SourceManager::File::GetLine (uint32_t line_no, std::string &buffer) 664 { 665 if (!LineIsValid(line_no)) 666 return false; 667 668 size_t start_offset = GetLineOffset (line_no); 669 size_t end_offset = GetLineOffset (line_no + 1); 670 if (end_offset == UINT32_MAX) 671 { 672 end_offset = m_data_sp->GetByteSize(); 673 } 674 buffer.assign((char *) m_data_sp->GetBytes() + start_offset, end_offset - start_offset); 675 676 return true; 677 } 678 679 void 680 SourceManager::SourceFileCache::AddSourceFile (const FileSP &file_sp) 681 { 682 FileSpec file_spec; 683 FileCache::iterator pos = m_file_cache.find(file_spec); 684 if (pos == m_file_cache.end()) 685 m_file_cache[file_spec] = file_sp; 686 else 687 { 688 if (file_sp != pos->second) 689 m_file_cache[file_spec] = file_sp; 690 } 691 } 692 693 SourceManager::FileSP 694 SourceManager::SourceFileCache::FindSourceFile (const FileSpec &file_spec) const 695 { 696 FileSP file_sp; 697 FileCache::const_iterator pos = m_file_cache.find(file_spec); 698 if (pos != m_file_cache.end()) 699 file_sp = pos->second; 700 return file_sp; 701 } 702 703