1 //===-- SourceManager.cpp -------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Core/SourceManager.h"
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/Target.h"
25 #include "lldb/Utility/AnsiTerminal.h"
26 #include "lldb/Utility/ConstString.h"
27 #include "lldb/Utility/DataBuffer.h"
28 #include "lldb/Utility/DataBufferLLVM.h"
29 #include "lldb/Utility/RegularExpression.h"
30 #include "lldb/Utility/Stream.h"
31 #include "lldb/lldb-enumerations.h"
33 #include "llvm/ADT/Twine.h"
41 namespace lldb_private {
42 class ExecutionContext;
44 namespace lldb_private {
49 using namespace lldb_private;
51 static inline bool is_newline_char(char ch) { return ch == '\n' || ch == '\r'; }
53 // SourceManager constructor
54 SourceManager::SourceManager(const TargetSP &target_sp)
55 : m_last_line(0), m_last_count(0), m_default_set(false),
56 m_target_wp(target_sp),
57 m_debugger_wp(target_sp->GetDebugger().shared_from_this()) {}
59 SourceManager::SourceManager(const DebuggerSP &debugger_sp)
60 : m_last_line(0), m_last_count(0), m_default_set(false), m_target_wp(),
61 m_debugger_wp(debugger_sp) {}
64 SourceManager::~SourceManager() {}
66 SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
70 DebuggerSP debugger_sp(m_debugger_wp.lock());
72 if (debugger_sp && debugger_sp->GetUseSourceCache())
73 file_sp = debugger_sp->GetSourceFileCache().FindSourceFile(file_spec);
75 TargetSP target_sp(m_target_wp.lock());
77 // It the target source path map has been updated, get this file again so we
78 // can successfully remap the source file
79 if (target_sp && file_sp &&
80 file_sp->GetSourceMapModificationID() !=
81 target_sp->GetSourcePathMap().GetModificationID())
84 // Update the file contents if needed if we found a file
86 file_sp->UpdateIfNeeded();
88 // If file_sp is no good or it points to a non-existent file, reset it.
89 if (!file_sp || !FileSystem::Instance().Exists(file_sp->GetFileSpec())) {
91 file_sp = std::make_shared<File>(file_spec, target_sp.get());
93 file_sp = std::make_shared<File>(file_spec, debugger_sp);
95 if (debugger_sp && debugger_sp->GetUseSourceCache())
96 debugger_sp->GetSourceFileCache().AddSourceFile(file_sp);
101 static bool should_highlight_source(DebuggerSP debugger_sp) {
105 // We don't use ANSI stop column formatting if the debugger doesn't think it
106 // should be using color.
107 if (!debugger_sp->GetUseColor())
110 return debugger_sp->GetHighlightSource();
113 static bool should_show_stop_column_with_ansi(DebuggerSP debugger_sp) {
114 // We don't use ANSI stop column formatting if we can't lookup values from
119 // We don't use ANSI stop column formatting if the debugger doesn't think it
120 // should be using color.
121 if (!debugger_sp->GetUseColor())
124 // We only use ANSI stop column formatting if we're either supposed to show
125 // ANSI where available (which we know we have when we get to this point), or
126 // if we're only supposed to use ANSI.
127 const auto value = debugger_sp->GetStopShowColumn();
128 return ((value == eStopShowColumnAnsiOrCaret) ||
129 (value == eStopShowColumnAnsi));
132 static bool should_show_stop_column_with_caret(DebuggerSP debugger_sp) {
133 // We don't use text-based stop column formatting if we can't lookup values
134 // from the debugger.
138 // If we're asked to show the first available of ANSI or caret, then we do
139 // show the caret when ANSI is not available.
140 const auto value = debugger_sp->GetStopShowColumn();
141 if ((value == eStopShowColumnAnsiOrCaret) && !debugger_sp->GetUseColor())
144 // The only other time we use caret is if we're explicitly asked to show
146 return value == eStopShowColumnCaret;
149 static bool should_show_stop_line_with_ansi(DebuggerSP debugger_sp) {
150 return debugger_sp && debugger_sp->GetUseColor();
153 size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
154 uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
155 const char *current_line_cstr, Stream *s,
156 const SymbolContextList *bp_locs) {
160 Stream::ByteDelta delta(*s);
162 if (start_line == 0) {
163 if (m_last_line != 0 && m_last_line != UINT32_MAX)
164 start_line = m_last_line + m_last_count;
169 if (!m_default_set) {
172 GetDefaultFileAndLine(tmp_spec, tmp_line);
175 m_last_line = start_line;
176 m_last_count = count;
178 if (FileSP last_file_sp = GetLastFile()) {
179 const uint32_t end_line = start_line + count - 1;
180 for (uint32_t line = start_line; line <= end_line; ++line) {
181 if (!last_file_sp->LineIsValid(line)) {
182 m_last_line = UINT32_MAX;
186 char prefix[32] = "";
188 uint32_t bp_count = bp_locs->NumLineEntriesWithLine(line);
191 ::snprintf(prefix, sizeof(prefix), "[%u] ", bp_count);
193 ::snprintf(prefix, sizeof(prefix), " ");
197 sprintf(buffer, "%2.2s", (line == curr_line) ? current_line_cstr : "");
198 std::string current_line_highlight(buffer);
200 auto debugger_sp = m_debugger_wp.lock();
201 if (should_show_stop_line_with_ansi(debugger_sp)) {
202 current_line_highlight = ansi::FormatAnsiTerminalCodes(
203 (debugger_sp->GetStopShowLineMarkerAnsiPrefix() +
204 current_line_highlight +
205 debugger_sp->GetStopShowLineMarkerAnsiSuffix())
209 s->Printf("%s%s %-4u\t", prefix, current_line_highlight.c_str(), line);
211 // So far we treated column 0 as a special 'no column value', but
212 // DisplaySourceLines starts counting columns from 0 (and no column is
213 // expressed by passing an empty optional).
214 llvm::Optional<size_t> columnToHighlight;
215 if (line == curr_line && column)
216 columnToHighlight = column - 1;
218 size_t this_line_size =
219 last_file_sp->DisplaySourceLines(line, columnToHighlight, 0, 0, s);
220 if (column != 0 && line == curr_line &&
221 should_show_stop_column_with_caret(debugger_sp)) {
222 // Display caret cursor.
223 std::string src_line;
224 last_file_sp->GetLine(line, src_line);
226 // Insert a space for every non-tab character in the source line.
227 for (size_t i = 0; i + 1 < column && i < src_line.length(); ++i)
228 s->PutChar(src_line[i] == '\t' ? '\t' : ' ');
229 // Now add the caret.
232 if (this_line_size == 0) {
233 m_last_line = UINT32_MAX;
241 size_t SourceManager::DisplaySourceLinesWithLineNumbers(
242 const FileSpec &file_spec, uint32_t line, uint32_t column,
243 uint32_t context_before, uint32_t context_after,
244 const char *current_line_cstr, Stream *s,
245 const SymbolContextList *bp_locs) {
246 FileSP file_sp(GetFile(file_spec));
249 uint32_t count = context_before + context_after + 1;
250 if (line > context_before)
251 start_line = line - context_before;
255 FileSP last_file_sp(GetLastFile());
256 if (last_file_sp.get() != file_sp.get()) {
259 m_last_file_spec = file_spec;
261 return DisplaySourceLinesWithLineNumbersUsingLastFile(
262 start_line, count, line, column, current_line_cstr, s, bp_locs);
265 size_t SourceManager::DisplayMoreWithLineNumbers(
266 Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs) {
267 // If we get called before anybody has set a default file and line, then try
268 // to figure it out here.
269 FileSP last_file_sp(GetLastFile());
270 const bool have_default_file_line = last_file_sp && m_last_line > 0;
271 if (!m_default_set) {
274 GetDefaultFileAndLine(tmp_spec, tmp_line);
278 if (m_last_line == UINT32_MAX)
281 if (reverse && m_last_line == 1)
285 m_last_count = count;
286 else if (m_last_count == 0)
289 if (m_last_line > 0) {
291 // If this is the first time we've done a reverse, then back up one
292 // more time so we end up showing the chunk before the last one we've
294 if (m_last_line > m_last_count)
295 m_last_line -= m_last_count;
298 } else if (have_default_file_line)
299 m_last_line += m_last_count;
303 const uint32_t column = 0;
304 return DisplaySourceLinesWithLineNumbersUsingLastFile(
305 m_last_line, m_last_count, UINT32_MAX, column, "", s, bp_locs);
310 bool SourceManager::SetDefaultFileAndLine(const FileSpec &file_spec,
312 m_default_set = true;
313 FileSP file_sp(GetFile(file_spec));
317 m_last_file_spec = file_spec;
324 bool SourceManager::GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line) {
325 if (FileSP last_file_sp = GetLastFile()) {
326 file_spec = m_last_file_spec;
329 } else if (!m_default_set) {
330 TargetSP target_sp(m_target_wp.lock());
333 // If nobody has set the default file and line then try here. If there's
334 // no executable, then we will try again later when there is one.
335 // Otherwise, if we can't find it we won't look again, somebody will have
336 // to set it (for instance when we stop somewhere...)
337 Module *executable_ptr = target_sp->GetExecutableModulePointer();
338 if (executable_ptr) {
339 SymbolContextList sc_list;
340 ConstString main_name("main");
341 bool symbols_okay = false; // Force it to be a debug symbol.
342 bool inlines_okay = true;
343 executable_ptr->FindFunctions(main_name, CompilerDeclContext(),
344 lldb::eFunctionNameTypeBase, inlines_okay,
345 symbols_okay, sc_list);
346 size_t num_matches = sc_list.GetSize();
347 for (size_t idx = 0; idx < num_matches; idx++) {
349 sc_list.GetContextAtIndex(idx, sc);
351 lldb_private::LineEntry line_entry;
352 if (sc.function->GetAddressRange()
354 .CalculateSymbolContextLineEntry(line_entry)) {
355 SetDefaultFileAndLine(line_entry.file, line_entry.line);
356 file_spec = m_last_file_spec;
368 void SourceManager::FindLinesMatchingRegex(FileSpec &file_spec,
369 RegularExpression ®ex,
372 std::vector<uint32_t> &match_lines) {
374 FileSP file_sp = GetFile(file_spec);
377 return file_sp->FindLinesMatchingRegex(regex, start_line, end_line,
381 SourceManager::File::File(const FileSpec &file_spec,
382 lldb::DebuggerSP debugger_sp)
383 : m_file_spec_orig(file_spec), m_file_spec(file_spec),
384 m_mod_time(FileSystem::Instance().GetModificationTime(file_spec)),
385 m_debugger_wp(debugger_sp) {
386 CommonInitializer(file_spec, nullptr);
389 SourceManager::File::File(const FileSpec &file_spec, Target *target)
390 : m_file_spec_orig(file_spec), m_file_spec(file_spec),
391 m_mod_time(FileSystem::Instance().GetModificationTime(file_spec)),
392 m_debugger_wp(target ? target->GetDebugger().shared_from_this()
394 CommonInitializer(file_spec, target);
397 void SourceManager::File::CommonInitializer(const FileSpec &file_spec,
399 if (m_mod_time == llvm::sys::TimePoint<>()) {
401 m_source_map_mod_id = target->GetSourcePathMap().GetModificationID();
403 if (!file_spec.GetDirectory() && file_spec.GetFilename()) {
404 // If this is just a file name, lets see if we can find it in the
406 bool check_inlines = false;
407 SymbolContextList sc_list;
409 target->GetImages().ResolveSymbolContextForFilePath(
410 file_spec.GetFilename().AsCString(), 0, check_inlines,
411 SymbolContextItem(eSymbolContextModule |
412 eSymbolContextCompUnit),
414 bool got_multiple = false;
415 if (num_matches != 0) {
416 if (num_matches > 1) {
418 CompileUnit *test_cu = nullptr;
420 for (unsigned i = 0; i < num_matches; i++) {
421 sc_list.GetContextAtIndex(i, sc);
424 if (test_cu != sc.comp_unit)
428 test_cu = sc.comp_unit;
434 sc_list.GetContextAtIndex(0, sc);
436 m_file_spec = sc.comp_unit->GetPrimaryFile();
437 m_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
441 // Try remapping if m_file_spec does not correspond to an existing file.
442 if (!FileSystem::Instance().Exists(m_file_spec)) {
443 FileSpec new_file_spec;
444 // Check target specific source remappings first, then fall back to
445 // modules objects can have individual path remappings that were
446 // detected when the debug info for a module was found. then
447 if (target->GetSourcePathMap().FindFile(m_file_spec, new_file_spec) ||
448 target->GetImages().FindSourceFile(m_file_spec, new_file_spec)) {
449 m_file_spec = new_file_spec;
450 m_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
456 if (m_mod_time != llvm::sys::TimePoint<>())
457 m_data_sp = FileSystem::Instance().CreateDataBuffer(m_file_spec);
460 uint32_t SourceManager::File::GetLineOffset(uint32_t line) {
467 if (CalculateLineOffsets(line)) {
468 if (line < m_offsets.size())
469 return m_offsets[line - 1]; // yes we want "line - 1" in the index
474 uint32_t SourceManager::File::GetNumLines() {
475 CalculateLineOffsets();
476 return m_offsets.size();
479 const char *SourceManager::File::PeekLineData(uint32_t line) {
480 if (!LineIsValid(line))
483 size_t line_offset = GetLineOffset(line);
484 if (line_offset < m_data_sp->GetByteSize())
485 return (const char *)m_data_sp->GetBytes() + line_offset;
489 uint32_t SourceManager::File::GetLineLength(uint32_t line,
490 bool include_newline_chars) {
491 if (!LineIsValid(line))
494 size_t start_offset = GetLineOffset(line);
495 size_t end_offset = GetLineOffset(line + 1);
496 if (end_offset == UINT32_MAX)
497 end_offset = m_data_sp->GetByteSize();
499 if (end_offset > start_offset) {
500 uint32_t length = end_offset - start_offset;
501 if (!include_newline_chars) {
502 const char *line_start =
503 (const char *)m_data_sp->GetBytes() + start_offset;
505 const char last_char = line_start[length - 1];
506 if ((last_char == '\r') || (last_char == '\n'))
517 bool SourceManager::File::LineIsValid(uint32_t line) {
521 if (CalculateLineOffsets(line))
522 return line < m_offsets.size();
526 void SourceManager::File::UpdateIfNeeded() {
527 // TODO: use host API to sign up for file modifications to anything in our
528 // source cache and only update when we determine a file has been updated.
529 // For now we check each time we want to display info for the file.
530 auto curr_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
532 if (curr_mod_time != llvm::sys::TimePoint<>() &&
533 m_mod_time != curr_mod_time) {
534 m_mod_time = curr_mod_time;
535 m_data_sp = FileSystem::Instance().CreateDataBuffer(m_file_spec);
540 size_t SourceManager::File::DisplaySourceLines(uint32_t line,
541 llvm::Optional<size_t> column,
542 uint32_t context_before,
543 uint32_t context_after,
545 // Nothing to write if there's no stream.
549 // Sanity check m_data_sp before proceeding.
553 size_t bytes_written = s->GetWrittenBytes();
555 auto debugger_sp = m_debugger_wp.lock();
557 HighlightStyle style;
558 // Use the default Vim style if source highlighting is enabled.
559 if (should_highlight_source(debugger_sp))
560 style = HighlightStyle::MakeVimStyle();
562 // If we should mark the stop column with color codes, then copy the prefix
563 // and suffix to our color style.
564 if (should_show_stop_column_with_ansi(debugger_sp))
565 style.selected.Set(debugger_sp->GetStopShowColumnAnsiPrefix(),
566 debugger_sp->GetStopShowColumnAnsiSuffix());
568 HighlighterManager mgr;
569 std::string path = GetFileSpec().GetPath(/*denormalize*/ false);
570 // FIXME: Find a way to get the definitive language this file was written in
571 // and pass it to the highlighter.
572 const auto &h = mgr.getHighlighterFor(lldb::eLanguageTypeUnknown, path);
574 const uint32_t start_line =
575 line <= context_before ? 1 : line - context_before;
576 const uint32_t start_line_offset = GetLineOffset(start_line);
577 if (start_line_offset != UINT32_MAX) {
578 const uint32_t end_line = line + context_after;
579 uint32_t end_line_offset = GetLineOffset(end_line + 1);
580 if (end_line_offset == UINT32_MAX)
581 end_line_offset = m_data_sp->GetByteSize();
583 assert(start_line_offset <= end_line_offset);
584 if (start_line_offset < end_line_offset) {
585 size_t count = end_line_offset - start_line_offset;
586 const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
588 auto ref = llvm::StringRef(reinterpret_cast<const char *>(cstr), count);
590 h.Highlight(style, ref, column, "", *s);
592 // Ensure we get an end of line character one way or another.
593 if (!is_newline_char(ref.back()))
597 return s->GetWrittenBytes() - bytes_written;
600 void SourceManager::File::FindLinesMatchingRegex(
601 RegularExpression ®ex, uint32_t start_line, uint32_t end_line,
602 std::vector<uint32_t> &match_lines) {
605 if (!LineIsValid(start_line) ||
606 (end_line != UINT32_MAX && !LineIsValid(end_line)))
608 if (start_line > end_line)
611 for (uint32_t line_no = start_line; line_no < end_line; line_no++) {
613 if (!GetLine(line_no, buffer))
615 if (regex.Execute(buffer)) {
616 match_lines.push_back(line_no);
621 bool lldb_private::operator==(const SourceManager::File &lhs,
622 const SourceManager::File &rhs) {
623 if (lhs.m_file_spec != rhs.m_file_spec)
625 return lhs.m_mod_time == rhs.m_mod_time;
628 bool SourceManager::File::CalculateLineOffsets(uint32_t line) {
630 UINT32_MAX; // TODO: take this line out when we support partial indexing
631 if (line == UINT32_MAX) {
633 if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX)
636 if (m_offsets.empty()) {
637 if (m_data_sp.get() == nullptr)
640 const char *start = (char *)m_data_sp->GetBytes();
642 const char *end = start + m_data_sp->GetByteSize();
644 // Calculate all line offsets from scratch
646 // Push a 1 at index zero to indicate the file has been completely
648 m_offsets.push_back(UINT32_MAX);
650 for (s = start; s < end; ++s) {
652 if (is_newline_char(curr_ch)) {
655 if (is_newline_char(next_ch)) {
656 if (curr_ch != next_ch)
660 m_offsets.push_back(s + 1 - start);
663 if (!m_offsets.empty()) {
664 if (m_offsets.back() < size_t(end - start))
665 m_offsets.push_back(end - start);
670 // Some lines have been populated, start where we last left off
671 assert("Not implemented yet" && false);
675 // Calculate all line offsets up to "line"
676 assert("Not implemented yet" && false);
681 bool SourceManager::File::GetLine(uint32_t line_no, std::string &buffer) {
682 if (!LineIsValid(line_no))
685 size_t start_offset = GetLineOffset(line_no);
686 size_t end_offset = GetLineOffset(line_no + 1);
687 if (end_offset == UINT32_MAX) {
688 end_offset = m_data_sp->GetByteSize();
690 buffer.assign((char *)m_data_sp->GetBytes() + start_offset,
691 end_offset - start_offset);
696 void SourceManager::SourceFileCache::AddSourceFile(const FileSP &file_sp) {
697 FileSpec file_spec = file_sp->GetFileSpec();
698 FileCache::iterator pos = m_file_cache.find(file_spec);
699 if (pos == m_file_cache.end())
700 m_file_cache[file_spec] = file_sp;
702 if (file_sp != pos->second)
703 m_file_cache[file_spec] = file_sp;
707 SourceManager::FileSP SourceManager::SourceFileCache::FindSourceFile(
708 const FileSpec &file_spec) const {
710 FileCache::const_iterator pos = m_file_cache.find(file_spec);
711 if (pos != m_file_cache.end())
712 file_sp = pos->second;