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