1 //===-- SourceManager.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 "lldb/Core/SourceManager.h" 10 11 #include "lldb/Core/Address.h" 12 #include "lldb/Core/AddressRange.h" 13 #include "lldb/Core/Debugger.h" 14 #include "lldb/Core/FormatEntity.h" 15 #include "lldb/Core/Highlighter.h" 16 #include "lldb/Core/Module.h" 17 #include "lldb/Core/ModuleList.h" 18 #include "lldb/Host/FileSystem.h" 19 #include "lldb/Symbol/CompileUnit.h" 20 #include "lldb/Symbol/Function.h" 21 #include "lldb/Symbol/LineEntry.h" 22 #include "lldb/Symbol/SymbolContext.h" 23 #include "lldb/Target/PathMappingList.h" 24 #include "lldb/Target/Process.h" 25 #include "lldb/Target/Target.h" 26 #include "lldb/Utility/AnsiTerminal.h" 27 #include "lldb/Utility/ConstString.h" 28 #include "lldb/Utility/DataBuffer.h" 29 #include "lldb/Utility/LLDBLog.h" 30 #include "lldb/Utility/Log.h" 31 #include "lldb/Utility/RegularExpression.h" 32 #include "lldb/Utility/Stream.h" 33 #include "lldb/lldb-enumerations.h" 34 35 #include "llvm/ADT/Twine.h" 36 37 #include <memory> 38 #include <optional> 39 #include <utility> 40 41 #include <cassert> 42 #include <cstdio> 43 44 namespace lldb_private { 45 class ExecutionContext; 46 } 47 namespace lldb_private { 48 class ValueObject; 49 } 50 51 using namespace lldb; 52 using namespace lldb_private; 53 54 static inline bool is_newline_char(char ch) { return ch == '\n' || ch == '\r'; } 55 56 static void resolve_tilde(FileSpec &file_spec) { 57 if (!FileSystem::Instance().Exists(file_spec) && 58 file_spec.GetDirectory() && 59 file_spec.GetDirectory().GetCString()[0] == '~') { 60 FileSystem::Instance().Resolve(file_spec); 61 } 62 } 63 64 // SourceManager constructor 65 SourceManager::SourceManager(const TargetSP &target_sp) 66 : m_last_line(0), m_last_count(0), m_default_set(false), 67 m_target_wp(target_sp), 68 m_debugger_wp(target_sp->GetDebugger().shared_from_this()) {} 69 70 SourceManager::SourceManager(const DebuggerSP &debugger_sp) 71 : m_last_line(0), m_last_count(0), m_default_set(false), m_target_wp(), 72 m_debugger_wp(debugger_sp) {} 73 74 // Destructor 75 SourceManager::~SourceManager() = default; 76 77 SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) { 78 if (!file_spec) 79 return {}; 80 81 Log *log = GetLog(LLDBLog::Source); 82 83 DebuggerSP debugger_sp(m_debugger_wp.lock()); 84 TargetSP target_sp(m_target_wp.lock()); 85 86 if (!debugger_sp || !debugger_sp->GetUseSourceCache()) { 87 LLDB_LOG(log, "Source file caching disabled: creating new source file: {0}", 88 file_spec); 89 if (target_sp) 90 return std::make_shared<File>(std::make_shared<SupportFile>(file_spec), 91 target_sp); 92 return std::make_shared<File>(std::make_shared<SupportFile>(file_spec), 93 debugger_sp); 94 } 95 96 ProcessSP process_sp = target_sp ? target_sp->GetProcessSP() : ProcessSP(); 97 98 // Check the process source cache first. This is the fast path which avoids 99 // touching the file system unless the path remapping has changed. 100 if (process_sp) { 101 if (FileSP file_sp = 102 process_sp->GetSourceFileCache().FindSourceFile(file_spec)) { 103 LLDB_LOG(log, "Found source file in the process cache: {0}", file_spec); 104 if (file_sp->PathRemappingIsStale()) { 105 LLDB_LOG(log, "Path remapping is stale: removing file from caches: {0}", 106 file_spec); 107 108 // Remove the file from the debugger and process cache. Otherwise we'll 109 // hit the same issue again below when querying the debugger cache. 110 debugger_sp->GetSourceFileCache().RemoveSourceFile(file_sp); 111 process_sp->GetSourceFileCache().RemoveSourceFile(file_sp); 112 113 file_sp.reset(); 114 } else { 115 return file_sp; 116 } 117 } 118 } 119 120 // Cache miss in the process cache. Check the debugger source cache. 121 FileSP file_sp = debugger_sp->GetSourceFileCache().FindSourceFile(file_spec); 122 123 // We found the file in the debugger cache. Check if anything invalidated our 124 // cache result. 125 if (file_sp) 126 LLDB_LOG(log, "Found source file in the debugger cache: {0}", file_spec); 127 128 // Check if the path remapping has changed. 129 if (file_sp && file_sp->PathRemappingIsStale()) { 130 LLDB_LOG(log, "Path remapping is stale: {0}", file_spec); 131 file_sp.reset(); 132 } 133 134 // Check if the modification time has changed. 135 if (file_sp && file_sp->ModificationTimeIsStale()) { 136 LLDB_LOG(log, "Modification time is stale: {0}", file_spec); 137 file_sp.reset(); 138 } 139 140 // Check if the file exists on disk. 141 if (file_sp && !FileSystem::Instance().Exists( 142 file_sp->GetSupportFile()->GetSpecOnly())) { 143 LLDB_LOG(log, "File doesn't exist on disk: {0}", file_spec); 144 file_sp.reset(); 145 } 146 147 // If at this point we don't have a valid file, it means we either didn't find 148 // it in the debugger cache or something caused it to be invalidated. 149 if (!file_sp) { 150 LLDB_LOG(log, "Creating and caching new source file: {0}", file_spec); 151 152 // (Re)create the file. 153 if (target_sp) 154 file_sp = std::make_shared<File>(std::make_shared<SupportFile>(file_spec), 155 target_sp); 156 else 157 file_sp = std::make_shared<File>(std::make_shared<SupportFile>(file_spec), 158 debugger_sp); 159 160 // Add the file to the debugger and process cache. If the file was 161 // invalidated, this will overwrite it. 162 debugger_sp->GetSourceFileCache().AddSourceFile(file_spec, file_sp); 163 if (process_sp) 164 process_sp->GetSourceFileCache().AddSourceFile(file_spec, file_sp); 165 } 166 167 return file_sp; 168 } 169 170 static bool should_highlight_source(DebuggerSP debugger_sp) { 171 if (!debugger_sp) 172 return false; 173 174 // We don't use ANSI stop column formatting if the debugger doesn't think it 175 // should be using color. 176 if (!debugger_sp->GetUseColor()) 177 return false; 178 179 return debugger_sp->GetHighlightSource(); 180 } 181 182 static bool should_show_stop_column_with_ansi(DebuggerSP debugger_sp) { 183 // We don't use ANSI stop column formatting if we can't lookup values from 184 // the debugger. 185 if (!debugger_sp) 186 return false; 187 188 // We don't use ANSI stop column formatting if the debugger doesn't think it 189 // should be using color. 190 if (!debugger_sp->GetUseColor()) 191 return false; 192 193 // We only use ANSI stop column formatting if we're either supposed to show 194 // ANSI where available (which we know we have when we get to this point), or 195 // if we're only supposed to use ANSI. 196 const auto value = debugger_sp->GetStopShowColumn(); 197 return ((value == eStopShowColumnAnsiOrCaret) || 198 (value == eStopShowColumnAnsi)); 199 } 200 201 static bool should_show_stop_column_with_caret(DebuggerSP debugger_sp) { 202 // We don't use text-based stop column formatting if we can't lookup values 203 // from the debugger. 204 if (!debugger_sp) 205 return false; 206 207 // If we're asked to show the first available of ANSI or caret, then we do 208 // show the caret when ANSI is not available. 209 const auto value = debugger_sp->GetStopShowColumn(); 210 if ((value == eStopShowColumnAnsiOrCaret) && !debugger_sp->GetUseColor()) 211 return true; 212 213 // The only other time we use caret is if we're explicitly asked to show 214 // caret. 215 return value == eStopShowColumnCaret; 216 } 217 218 static bool should_show_stop_line_with_ansi(DebuggerSP debugger_sp) { 219 return debugger_sp && debugger_sp->GetUseColor(); 220 } 221 222 size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile( 223 uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column, 224 const char *current_line_cstr, Stream *s, 225 const SymbolContextList *bp_locs) { 226 if (count == 0) 227 return 0; 228 229 Stream::ByteDelta delta(*s); 230 231 if (start_line == 0) { 232 if (m_last_line != 0 && m_last_line != UINT32_MAX) 233 start_line = m_last_line + m_last_count; 234 else 235 start_line = 1; 236 } 237 238 if (!m_default_set) { 239 FileSpec tmp_spec; 240 uint32_t tmp_line; 241 GetDefaultFileAndLine(tmp_spec, tmp_line); 242 } 243 244 m_last_line = start_line; 245 m_last_count = count; 246 247 if (FileSP last_file_sp = GetLastFile()) { 248 const uint32_t end_line = start_line + count - 1; 249 for (uint32_t line = start_line; line <= end_line; ++line) { 250 if (!last_file_sp->LineIsValid(line)) { 251 m_last_line = UINT32_MAX; 252 break; 253 } 254 255 std::string prefix; 256 if (bp_locs) { 257 uint32_t bp_count = bp_locs->NumLineEntriesWithLine(line); 258 259 if (bp_count > 0) 260 prefix = llvm::formatv("[{0}]", bp_count); 261 else 262 prefix = " "; 263 } 264 265 char buffer[3]; 266 snprintf(buffer, sizeof(buffer), "%2.2s", 267 (line == curr_line) ? current_line_cstr : ""); 268 std::string current_line_highlight(buffer); 269 270 auto debugger_sp = m_debugger_wp.lock(); 271 if (should_show_stop_line_with_ansi(debugger_sp)) { 272 current_line_highlight = ansi::FormatAnsiTerminalCodes( 273 (debugger_sp->GetStopShowLineMarkerAnsiPrefix() + 274 current_line_highlight + 275 debugger_sp->GetStopShowLineMarkerAnsiSuffix()) 276 .str()); 277 } 278 279 s->Printf("%s%s %-4u\t", prefix.c_str(), current_line_highlight.c_str(), 280 line); 281 282 // So far we treated column 0 as a special 'no column value', but 283 // DisplaySourceLines starts counting columns from 0 (and no column is 284 // expressed by passing an empty optional). 285 std::optional<size_t> columnToHighlight; 286 if (line == curr_line && column) 287 columnToHighlight = column - 1; 288 289 size_t this_line_size = 290 last_file_sp->DisplaySourceLines(line, columnToHighlight, 0, 0, s); 291 if (column != 0 && line == curr_line && 292 should_show_stop_column_with_caret(debugger_sp)) { 293 // Display caret cursor. 294 std::string src_line; 295 last_file_sp->GetLine(line, src_line); 296 s->Printf(" \t"); 297 // Insert a space for every non-tab character in the source line. 298 for (size_t i = 0; i + 1 < column && i < src_line.length(); ++i) 299 s->PutChar(src_line[i] == '\t' ? '\t' : ' '); 300 // Now add the caret. 301 s->Printf("^\n"); 302 } 303 if (this_line_size == 0) { 304 m_last_line = UINT32_MAX; 305 break; 306 } 307 } 308 } 309 return *delta; 310 } 311 312 size_t SourceManager::DisplaySourceLinesWithLineNumbers( 313 const FileSpec &file_spec, uint32_t line, uint32_t column, 314 uint32_t context_before, uint32_t context_after, 315 const char *current_line_cstr, Stream *s, 316 const SymbolContextList *bp_locs) { 317 FileSP file_sp(GetFile(file_spec)); 318 319 uint32_t start_line; 320 uint32_t count = context_before + context_after + 1; 321 if (line > context_before) 322 start_line = line - context_before; 323 else 324 start_line = 1; 325 326 FileSP last_file_sp(GetLastFile()); 327 if (last_file_sp.get() != file_sp.get()) { 328 if (line == 0) 329 m_last_line = 0; 330 m_last_file_spec = file_spec; 331 } 332 return DisplaySourceLinesWithLineNumbersUsingLastFile( 333 start_line, count, line, column, current_line_cstr, s, bp_locs); 334 } 335 336 size_t SourceManager::DisplayMoreWithLineNumbers( 337 Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs) { 338 // If we get called before anybody has set a default file and line, then try 339 // to figure it out here. 340 FileSP last_file_sp(GetLastFile()); 341 const bool have_default_file_line = last_file_sp && m_last_line > 0; 342 if (!m_default_set) { 343 FileSpec tmp_spec; 344 uint32_t tmp_line; 345 GetDefaultFileAndLine(tmp_spec, tmp_line); 346 } 347 348 if (last_file_sp) { 349 if (m_last_line == UINT32_MAX) 350 return 0; 351 352 if (reverse && m_last_line == 1) 353 return 0; 354 355 if (count > 0) 356 m_last_count = count; 357 else if (m_last_count == 0) 358 m_last_count = 10; 359 360 if (m_last_line > 0) { 361 if (reverse) { 362 // If this is the first time we've done a reverse, then back up one 363 // more time so we end up showing the chunk before the last one we've 364 // shown: 365 if (m_last_line > m_last_count) 366 m_last_line -= m_last_count; 367 else 368 m_last_line = 1; 369 } else if (have_default_file_line) 370 m_last_line += m_last_count; 371 } else 372 m_last_line = 1; 373 374 const uint32_t column = 0; 375 return DisplaySourceLinesWithLineNumbersUsingLastFile( 376 m_last_line, m_last_count, UINT32_MAX, column, "", s, bp_locs); 377 } 378 return 0; 379 } 380 381 bool SourceManager::SetDefaultFileAndLine(const FileSpec &file_spec, 382 uint32_t line) { 383 m_default_set = true; 384 FileSP file_sp(GetFile(file_spec)); 385 386 if (file_sp) { 387 m_last_line = line; 388 m_last_file_spec = file_spec; 389 return true; 390 } else { 391 return false; 392 } 393 } 394 395 bool SourceManager::GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line) { 396 if (FileSP last_file_sp = GetLastFile()) { 397 file_spec = m_last_file_spec; 398 line = m_last_line; 399 return true; 400 } else if (!m_default_set) { 401 TargetSP target_sp(m_target_wp.lock()); 402 403 if (target_sp) { 404 // If nobody has set the default file and line then try here. If there's 405 // no executable, then we will try again later when there is one. 406 // Otherwise, if we can't find it we won't look again, somebody will have 407 // to set it (for instance when we stop somewhere...) 408 Module *executable_ptr = target_sp->GetExecutableModulePointer(); 409 if (executable_ptr) { 410 SymbolContextList sc_list; 411 ConstString main_name("main"); 412 413 ModuleFunctionSearchOptions function_options; 414 function_options.include_symbols = 415 false; // Force it to be a debug symbol. 416 function_options.include_inlines = true; 417 executable_ptr->FindFunctions(main_name, CompilerDeclContext(), 418 lldb::eFunctionNameTypeBase, 419 function_options, sc_list); 420 for (const SymbolContext &sc : sc_list) { 421 if (sc.function) { 422 lldb_private::LineEntry line_entry; 423 if (sc.function->GetAddressRange() 424 .GetBaseAddress() 425 .CalculateSymbolContextLineEntry(line_entry)) { 426 SetDefaultFileAndLine(line_entry.GetFile(), line_entry.line); 427 file_spec = m_last_file_spec; 428 line = m_last_line; 429 return true; 430 } 431 } 432 } 433 } 434 } 435 } 436 return false; 437 } 438 439 void SourceManager::FindLinesMatchingRegex(FileSpec &file_spec, 440 RegularExpression ®ex, 441 uint32_t start_line, 442 uint32_t end_line, 443 std::vector<uint32_t> &match_lines) { 444 match_lines.clear(); 445 FileSP file_sp = GetFile(file_spec); 446 if (!file_sp) 447 return; 448 return file_sp->FindLinesMatchingRegex(regex, start_line, end_line, 449 match_lines); 450 } 451 452 SourceManager::File::File(SupportFileSP support_file_sp, 453 lldb::DebuggerSP debugger_sp) 454 : m_support_file_sp(std::make_shared<SupportFile>()), m_mod_time(), 455 m_debugger_wp(debugger_sp), m_target_wp(TargetSP()) { 456 CommonInitializer(support_file_sp, {}); 457 } 458 459 SourceManager::File::File(SupportFileSP support_file_sp, TargetSP target_sp) 460 : m_support_file_sp(std::make_shared<SupportFile>()), m_mod_time(), 461 m_debugger_wp(target_sp ? target_sp->GetDebugger().shared_from_this() 462 : DebuggerSP()), 463 m_target_wp(target_sp) { 464 CommonInitializer(support_file_sp, target_sp); 465 } 466 467 void SourceManager::File::CommonInitializer(SupportFileSP support_file_sp, 468 TargetSP target_sp) { 469 // Set the file and update the modification time. 470 SetSupportFile(support_file_sp); 471 472 // Always update the source map modification ID if we have a target. 473 if (target_sp) 474 m_source_map_mod_id = target_sp->GetSourcePathMap().GetModificationID(); 475 476 // File doesn't exist. 477 if (m_mod_time == llvm::sys::TimePoint<>()) { 478 if (target_sp) { 479 // If this is just a file name, try finding it in the target. 480 { 481 FileSpec file_spec = support_file_sp->GetSpecOnly(); 482 if (!file_spec.GetDirectory() && file_spec.GetFilename()) { 483 bool check_inlines = false; 484 SymbolContextList sc_list; 485 size_t num_matches = 486 target_sp->GetImages().ResolveSymbolContextForFilePath( 487 file_spec.GetFilename().AsCString(), 0, check_inlines, 488 SymbolContextItem(eSymbolContextModule | 489 eSymbolContextCompUnit), 490 sc_list); 491 bool got_multiple = false; 492 if (num_matches != 0) { 493 if (num_matches > 1) { 494 CompileUnit *test_cu = nullptr; 495 for (const SymbolContext &sc : sc_list) { 496 if (sc.comp_unit) { 497 if (test_cu) { 498 if (test_cu != sc.comp_unit) 499 got_multiple = true; 500 break; 501 } else 502 test_cu = sc.comp_unit; 503 } 504 } 505 } 506 if (!got_multiple) { 507 SymbolContext sc; 508 sc_list.GetContextAtIndex(0, sc); 509 if (sc.comp_unit) 510 SetSupportFile(std::make_shared<SupportFile>( 511 sc.comp_unit->GetPrimaryFile())); 512 } 513 } 514 } 515 } 516 517 // Try remapping the file if it doesn't exist. 518 { 519 FileSpec file_spec = support_file_sp->GetSpecOnly(); 520 if (!FileSystem::Instance().Exists(file_spec)) { 521 // Check target specific source remappings (i.e., the 522 // target.source-map setting), then fall back to the module 523 // specific remapping (i.e., the .dSYM remapping dictionary). 524 auto remapped = target_sp->GetSourcePathMap().FindFile(file_spec); 525 if (!remapped) { 526 FileSpec new_spec; 527 if (target_sp->GetImages().FindSourceFile(file_spec, new_spec)) 528 remapped = new_spec; 529 } 530 if (remapped) 531 SetSupportFile(std::make_shared<SupportFile>( 532 *remapped, support_file_sp->GetChecksum())); 533 } 534 } 535 } 536 } 537 538 // If the file exists, read in the data. 539 if (m_mod_time != llvm::sys::TimePoint<>()) 540 m_data_sp = FileSystem::Instance().CreateDataBuffer( 541 m_support_file_sp->GetSpecOnly()); 542 } 543 544 void SourceManager::File::SetSupportFile(lldb::SupportFileSP support_file_sp) { 545 FileSpec file_spec = support_file_sp->GetSpecOnly(); 546 resolve_tilde(file_spec); 547 m_support_file_sp = 548 std::make_shared<SupportFile>(file_spec, support_file_sp->GetChecksum()); 549 m_mod_time = FileSystem::Instance().GetModificationTime(file_spec); 550 } 551 552 uint32_t SourceManager::File::GetLineOffset(uint32_t line) { 553 if (line == 0) 554 return UINT32_MAX; 555 556 if (line == 1) 557 return 0; 558 559 if (CalculateLineOffsets(line)) { 560 if (line < m_offsets.size()) 561 return m_offsets[line - 1]; // yes we want "line - 1" in the index 562 } 563 return UINT32_MAX; 564 } 565 566 uint32_t SourceManager::File::GetNumLines() { 567 CalculateLineOffsets(); 568 return m_offsets.size(); 569 } 570 571 const char *SourceManager::File::PeekLineData(uint32_t line) { 572 if (!LineIsValid(line)) 573 return nullptr; 574 575 size_t line_offset = GetLineOffset(line); 576 if (line_offset < m_data_sp->GetByteSize()) 577 return (const char *)m_data_sp->GetBytes() + line_offset; 578 return nullptr; 579 } 580 581 uint32_t SourceManager::File::GetLineLength(uint32_t line, 582 bool include_newline_chars) { 583 if (!LineIsValid(line)) 584 return false; 585 586 size_t start_offset = GetLineOffset(line); 587 size_t end_offset = GetLineOffset(line + 1); 588 if (end_offset == UINT32_MAX) 589 end_offset = m_data_sp->GetByteSize(); 590 591 if (end_offset > start_offset) { 592 uint32_t length = end_offset - start_offset; 593 if (!include_newline_chars) { 594 const char *line_start = 595 (const char *)m_data_sp->GetBytes() + start_offset; 596 while (length > 0) { 597 const char last_char = line_start[length - 1]; 598 if ((last_char == '\r') || (last_char == '\n')) 599 --length; 600 else 601 break; 602 } 603 } 604 return length; 605 } 606 return 0; 607 } 608 609 bool SourceManager::File::LineIsValid(uint32_t line) { 610 if (line == 0) 611 return false; 612 613 if (CalculateLineOffsets(line)) 614 return line < m_offsets.size(); 615 return false; 616 } 617 618 bool SourceManager::File::ModificationTimeIsStale() const { 619 // TODO: use host API to sign up for file modifications to anything in our 620 // source cache and only update when we determine a file has been updated. 621 // For now we check each time we want to display info for the file. 622 auto curr_mod_time = FileSystem::Instance().GetModificationTime( 623 m_support_file_sp->GetSpecOnly()); 624 return curr_mod_time != llvm::sys::TimePoint<>() && 625 m_mod_time != curr_mod_time; 626 } 627 628 bool SourceManager::File::PathRemappingIsStale() const { 629 if (TargetSP target_sp = m_target_wp.lock()) 630 return GetSourceMapModificationID() != 631 target_sp->GetSourcePathMap().GetModificationID(); 632 return false; 633 } 634 635 size_t SourceManager::File::DisplaySourceLines(uint32_t line, 636 std::optional<size_t> column, 637 uint32_t context_before, 638 uint32_t context_after, 639 Stream *s) { 640 // Nothing to write if there's no stream. 641 if (!s) 642 return 0; 643 644 // Sanity check m_data_sp before proceeding. 645 if (!m_data_sp) 646 return 0; 647 648 size_t bytes_written = s->GetWrittenBytes(); 649 650 auto debugger_sp = m_debugger_wp.lock(); 651 652 HighlightStyle style; 653 // Use the default Vim style if source highlighting is enabled. 654 if (should_highlight_source(debugger_sp)) 655 style = HighlightStyle::MakeVimStyle(); 656 657 // If we should mark the stop column with color codes, then copy the prefix 658 // and suffix to our color style. 659 if (should_show_stop_column_with_ansi(debugger_sp)) 660 style.selected.Set(debugger_sp->GetStopShowColumnAnsiPrefix(), 661 debugger_sp->GetStopShowColumnAnsiSuffix()); 662 663 HighlighterManager mgr; 664 std::string path = 665 GetSupportFile()->GetSpecOnly().GetPath(/*denormalize*/ false); 666 // FIXME: Find a way to get the definitive language this file was written in 667 // and pass it to the highlighter. 668 const auto &h = mgr.getHighlighterFor(lldb::eLanguageTypeUnknown, path); 669 670 const uint32_t start_line = 671 line <= context_before ? 1 : line - context_before; 672 const uint32_t start_line_offset = GetLineOffset(start_line); 673 if (start_line_offset != UINT32_MAX) { 674 const uint32_t end_line = line + context_after; 675 uint32_t end_line_offset = GetLineOffset(end_line + 1); 676 if (end_line_offset == UINT32_MAX) 677 end_line_offset = m_data_sp->GetByteSize(); 678 679 assert(start_line_offset <= end_line_offset); 680 if (start_line_offset < end_line_offset) { 681 size_t count = end_line_offset - start_line_offset; 682 const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset; 683 684 auto ref = llvm::StringRef(reinterpret_cast<const char *>(cstr), count); 685 686 h.Highlight(style, ref, column, "", *s); 687 688 // Ensure we get an end of line character one way or another. 689 if (!is_newline_char(ref.back())) 690 s->EOL(); 691 } 692 } 693 return s->GetWrittenBytes() - bytes_written; 694 } 695 696 void SourceManager::File::FindLinesMatchingRegex( 697 RegularExpression ®ex, uint32_t start_line, uint32_t end_line, 698 std::vector<uint32_t> &match_lines) { 699 match_lines.clear(); 700 701 if (!LineIsValid(start_line) || 702 (end_line != UINT32_MAX && !LineIsValid(end_line))) 703 return; 704 if (start_line > end_line) 705 return; 706 707 for (uint32_t line_no = start_line; line_no < end_line; line_no++) { 708 std::string buffer; 709 if (!GetLine(line_no, buffer)) 710 break; 711 if (regex.Execute(buffer)) { 712 match_lines.push_back(line_no); 713 } 714 } 715 } 716 717 bool lldb_private::operator==(const SourceManager::File &lhs, 718 const SourceManager::File &rhs) { 719 if (!lhs.GetSupportFile()->Equal(*rhs.GetSupportFile(), 720 SupportFile::eEqualChecksumIfSet)) 721 return false; 722 return lhs.m_mod_time == rhs.m_mod_time; 723 } 724 725 bool SourceManager::File::CalculateLineOffsets(uint32_t line) { 726 line = 727 UINT32_MAX; // TODO: take this line out when we support partial indexing 728 if (line == UINT32_MAX) { 729 // Already done? 730 if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX) 731 return true; 732 733 if (m_offsets.empty()) { 734 if (m_data_sp.get() == nullptr) 735 return false; 736 737 const char *start = (const char *)m_data_sp->GetBytes(); 738 if (start) { 739 const char *end = start + m_data_sp->GetByteSize(); 740 741 // Calculate all line offsets from scratch 742 743 // Push a 1 at index zero to indicate the file has been completely 744 // indexed. 745 m_offsets.push_back(UINT32_MAX); 746 const char *s; 747 for (s = start; s < end; ++s) { 748 char curr_ch = *s; 749 if (is_newline_char(curr_ch)) { 750 if (s + 1 < end) { 751 char next_ch = s[1]; 752 if (is_newline_char(next_ch)) { 753 if (curr_ch != next_ch) 754 ++s; 755 } 756 } 757 m_offsets.push_back(s + 1 - start); 758 } 759 } 760 if (!m_offsets.empty()) { 761 if (m_offsets.back() < size_t(end - start)) 762 m_offsets.push_back(end - start); 763 } 764 return true; 765 } 766 } else { 767 // Some lines have been populated, start where we last left off 768 assert("Not implemented yet" && false); 769 } 770 771 } else { 772 // Calculate all line offsets up to "line" 773 assert("Not implemented yet" && false); 774 } 775 return false; 776 } 777 778 bool SourceManager::File::GetLine(uint32_t line_no, std::string &buffer) { 779 if (!LineIsValid(line_no)) 780 return false; 781 782 size_t start_offset = GetLineOffset(line_no); 783 size_t end_offset = GetLineOffset(line_no + 1); 784 if (end_offset == UINT32_MAX) { 785 end_offset = m_data_sp->GetByteSize(); 786 } 787 buffer.assign((const char *)m_data_sp->GetBytes() + start_offset, 788 end_offset - start_offset); 789 790 return true; 791 } 792 793 void SourceManager::SourceFileCache::AddSourceFile(const FileSpec &file_spec, 794 FileSP file_sp) { 795 llvm::sys::ScopedWriter guard(m_mutex); 796 797 assert(file_sp && "invalid FileSP"); 798 799 AddSourceFileImpl(file_spec, file_sp); 800 const FileSpec &resolved_file_spec = file_sp->GetSupportFile()->GetSpecOnly(); 801 if (file_spec != resolved_file_spec) 802 AddSourceFileImpl(file_sp->GetSupportFile()->GetSpecOnly(), file_sp); 803 } 804 805 void SourceManager::SourceFileCache::RemoveSourceFile(const FileSP &file_sp) { 806 llvm::sys::ScopedWriter guard(m_mutex); 807 808 assert(file_sp && "invalid FileSP"); 809 810 // Iterate over all the elements in the cache. 811 // This is expensive but a relatively uncommon operation. 812 auto it = m_file_cache.begin(); 813 while (it != m_file_cache.end()) { 814 if (it->second == file_sp) 815 it = m_file_cache.erase(it); 816 else 817 it++; 818 } 819 } 820 821 void SourceManager::SourceFileCache::AddSourceFileImpl( 822 const FileSpec &file_spec, FileSP file_sp) { 823 FileCache::iterator pos = m_file_cache.find(file_spec); 824 if (pos == m_file_cache.end()) { 825 m_file_cache[file_spec] = file_sp; 826 } else { 827 if (file_sp != pos->second) 828 m_file_cache[file_spec] = file_sp; 829 } 830 } 831 832 SourceManager::FileSP SourceManager::SourceFileCache::FindSourceFile( 833 const FileSpec &file_spec) const { 834 llvm::sys::ScopedReader guard(m_mutex); 835 836 FileCache::const_iterator pos = m_file_cache.find(file_spec); 837 if (pos != m_file_cache.end()) 838 return pos->second; 839 return {}; 840 } 841 842 void SourceManager::SourceFileCache::Dump(Stream &stream) const { 843 stream << "Modification time Lines Path\n"; 844 stream << "------------------- -------- --------------------------------\n"; 845 for (auto &entry : m_file_cache) { 846 if (!entry.second) 847 continue; 848 FileSP file = entry.second; 849 stream.Format("{0:%Y-%m-%d %H:%M:%S} {1,8:d} {2}\n", file->GetTimestamp(), 850 file->GetNumLines(), entry.first.GetPath()); 851 } 852 } 853