1 //===-- SourceManager.cpp ---------------------------------------*- C++ -*-===//
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/ConstString.h"
26 #include "lldb/Utility/DataBuffer.h"
27 #include "lldb/Utility/DataBufferLLVM.h"
28 #include "lldb/Utility/RegularExpression.h"
29 #include "lldb/Utility/Stream.h"
30 #include "lldb/lldb-enumerations.h"
32 #include "llvm/ADT/Twine.h"
40 namespace lldb_private {
41 class ExecutionContext;
43 namespace lldb_private {
48 using namespace lldb_private;
50 static inline bool is_newline_char(char ch) { return ch == '\n' || ch == '\r'; }
52 // SourceManager constructor
53 SourceManager::SourceManager(const TargetSP &target_sp)
54 : m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
55 m_target_wp(target_sp),
56 m_debugger_wp(target_sp->GetDebugger().shared_from_this()) {}
58 SourceManager::SourceManager(const DebuggerSP &debugger_sp)
59 : m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
60 m_target_wp(), m_debugger_wp(debugger_sp) {}
63 SourceManager::~SourceManager() {}
65 SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
66 bool same_as_previous =
67 m_last_file_sp && m_last_file_sp->FileSpecMatches(file_spec);
69 DebuggerSP debugger_sp(m_debugger_wp.lock());
72 file_sp = m_last_file_sp;
74 file_sp = debugger_sp->GetSourceFileCache().FindSourceFile(file_spec);
76 TargetSP target_sp(m_target_wp.lock());
78 // It the target source path map has been updated, get this file again so we
79 // can successfully remap the source file
80 if (target_sp && file_sp &&
81 file_sp->GetSourceMapModificationID() !=
82 target_sp->GetSourcePathMap().GetModificationID())
85 // Update the file contents if needed if we found a file
87 file_sp->UpdateIfNeeded();
89 // If file_sp is no good or it points to a non-existent file, reset it.
90 if (!file_sp || !FileSystem::Instance().Exists(file_sp->GetFileSpec())) {
92 file_sp = std::make_shared<File>(file_spec, target_sp.get());
94 file_sp = std::make_shared<File>(file_spec, debugger_sp);
97 debugger_sp->GetSourceFileCache().AddSourceFile(file_sp);
102 static bool should_highlight_source(DebuggerSP debugger_sp) {
106 // We don't use ANSI stop column formatting if the debugger doesn't think it
107 // should be using color.
108 if (!debugger_sp->GetUseColor())
111 return debugger_sp->GetHighlightSource();
114 static bool should_show_stop_column_with_ansi(DebuggerSP debugger_sp) {
115 // We don't use ANSI stop column formatting if we can't lookup values from
120 // We don't use ANSI stop column formatting if the debugger doesn't think it
121 // should be using color.
122 if (!debugger_sp->GetUseColor())
125 // We only use ANSI stop column formatting if we're either supposed to show
126 // ANSI where available (which we know we have when we get to this point), or
127 // if we're only supposed to use ANSI.
128 const auto value = debugger_sp->GetStopShowColumn();
129 return ((value == eStopShowColumnAnsiOrCaret) ||
130 (value == eStopShowColumnAnsi));
133 static bool should_show_stop_column_with_caret(DebuggerSP debugger_sp) {
134 // We don't use text-based stop column formatting if we can't lookup values
135 // from the debugger.
139 // If we're asked to show the first available of ANSI or caret, then we do
140 // show the caret when ANSI is not available.
141 const auto value = debugger_sp->GetStopShowColumn();
142 if ((value == eStopShowColumnAnsiOrCaret) && !debugger_sp->GetUseColor())
145 // The only other time we use caret is if we're explicitly asked to show
147 return value == eStopShowColumnCaret;
150 size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
151 uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
152 const char *current_line_cstr, Stream *s,
153 const SymbolContextList *bp_locs) {
157 Stream::ByteDelta delta(*s);
159 if (start_line == 0) {
160 if (m_last_line != 0 && m_last_line != UINT32_MAX)
161 start_line = m_last_line + m_last_count;
166 if (!m_default_set) {
169 GetDefaultFileAndLine(tmp_spec, tmp_line);
172 m_last_line = start_line;
173 m_last_count = count;
175 if (m_last_file_sp.get()) {
176 const uint32_t end_line = start_line + count - 1;
177 for (uint32_t line = start_line; line <= end_line; ++line) {
178 if (!m_last_file_sp->LineIsValid(line)) {
179 m_last_line = UINT32_MAX;
183 char prefix[32] = "";
185 uint32_t bp_count = bp_locs->NumLineEntriesWithLine(line);
188 ::snprintf(prefix, sizeof(prefix), "[%u] ", bp_count);
190 ::snprintf(prefix, sizeof(prefix), " ");
193 s->Printf("%s%2.2s %-4u\t", prefix,
194 line == curr_line ? current_line_cstr : "", line);
196 // So far we treated column 0 as a special 'no column value', but
197 // DisplaySourceLines starts counting columns from 0 (and no column is
198 // expressed by passing an empty optional).
199 llvm::Optional<size_t> columnToHighlight;
200 if (line == curr_line && column)
201 columnToHighlight = column - 1;
203 size_t this_line_size =
204 m_last_file_sp->DisplaySourceLines(line, columnToHighlight, 0, 0, s);
205 if (column != 0 && line == curr_line &&
206 should_show_stop_column_with_caret(m_debugger_wp.lock())) {
207 // Display caret cursor.
208 std::string src_line;
209 m_last_file_sp->GetLine(line, src_line);
211 // Insert a space for every non-tab character in the source line.
212 for (size_t i = 0; i + 1 < column && i < src_line.length(); ++i)
213 s->PutChar(src_line[i] == '\t' ? '\t' : ' ');
214 // Now add the caret.
217 if (this_line_size == 0) {
218 m_last_line = UINT32_MAX;
226 size_t SourceManager::DisplaySourceLinesWithLineNumbers(
227 const FileSpec &file_spec, uint32_t line, uint32_t column,
228 uint32_t context_before, uint32_t context_after,
229 const char *current_line_cstr, Stream *s,
230 const SymbolContextList *bp_locs) {
231 FileSP file_sp(GetFile(file_spec));
234 uint32_t count = context_before + context_after + 1;
235 if (line > context_before)
236 start_line = line - context_before;
240 if (m_last_file_sp.get() != file_sp.get()) {
243 m_last_file_sp = file_sp;
245 return DisplaySourceLinesWithLineNumbersUsingLastFile(
246 start_line, count, line, column, current_line_cstr, s, bp_locs);
249 size_t SourceManager::DisplayMoreWithLineNumbers(
250 Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs) {
251 // If we get called before anybody has set a default file and line, then try
252 // to figure it out here.
253 const bool have_default_file_line = m_last_file_sp && m_last_line > 0;
254 if (!m_default_set) {
257 GetDefaultFileAndLine(tmp_spec, tmp_line);
260 if (m_last_file_sp) {
261 if (m_last_line == UINT32_MAX)
264 if (reverse && m_last_line == 1)
268 m_last_count = count;
269 else if (m_last_count == 0)
272 if (m_last_line > 0) {
274 // If this is the first time we've done a reverse, then back up one
275 // more time so we end up showing the chunk before the last one we've
277 if (m_last_line > m_last_count)
278 m_last_line -= m_last_count;
281 } else if (have_default_file_line)
282 m_last_line += m_last_count;
286 const uint32_t column = 0;
287 return DisplaySourceLinesWithLineNumbersUsingLastFile(
288 m_last_line, m_last_count, UINT32_MAX, column, "", s, bp_locs);
293 bool SourceManager::SetDefaultFileAndLine(const FileSpec &file_spec,
295 FileSP old_file_sp = m_last_file_sp;
296 m_last_file_sp = GetFile(file_spec);
298 m_default_set = true;
299 if (m_last_file_sp) {
303 m_last_file_sp = old_file_sp;
308 bool SourceManager::GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line) {
309 if (m_last_file_sp) {
310 file_spec = m_last_file_sp->GetFileSpec();
313 } else if (!m_default_set) {
314 TargetSP target_sp(m_target_wp.lock());
317 // If nobody has set the default file and line then try here. If there's
318 // no executable, then we will try again later when there is one.
319 // Otherwise, if we can't find it we won't look again, somebody will have
320 // to set it (for instance when we stop somewhere...)
321 Module *executable_ptr = target_sp->GetExecutableModulePointer();
322 if (executable_ptr) {
323 SymbolContextList sc_list;
324 ConstString main_name("main");
325 bool symbols_okay = false; // Force it to be a debug symbol.
326 bool inlines_okay = true;
328 size_t num_matches = executable_ptr->FindFunctions(
329 main_name, nullptr, lldb::eFunctionNameTypeBase, inlines_okay,
330 symbols_okay, append, sc_list);
331 for (size_t idx = 0; idx < num_matches; idx++) {
333 sc_list.GetContextAtIndex(idx, sc);
335 lldb_private::LineEntry line_entry;
336 if (sc.function->GetAddressRange()
338 .CalculateSymbolContextLineEntry(line_entry)) {
339 SetDefaultFileAndLine(line_entry.file, line_entry.line);
340 file_spec = m_last_file_sp->GetFileSpec();
352 void SourceManager::FindLinesMatchingRegex(FileSpec &file_spec,
353 RegularExpression ®ex,
356 std::vector<uint32_t> &match_lines) {
358 FileSP file_sp = GetFile(file_spec);
361 return file_sp->FindLinesMatchingRegex(regex, start_line, end_line,
365 SourceManager::File::File(const FileSpec &file_spec,
366 lldb::DebuggerSP debugger_sp)
367 : m_file_spec_orig(file_spec), m_file_spec(file_spec),
368 m_mod_time(FileSystem::Instance().GetModificationTime(file_spec)),
369 m_debugger_wp(debugger_sp) {
370 CommonInitializer(file_spec, nullptr);
373 SourceManager::File::File(const FileSpec &file_spec, Target *target)
374 : m_file_spec_orig(file_spec), m_file_spec(file_spec),
375 m_mod_time(FileSystem::Instance().GetModificationTime(file_spec)),
376 m_debugger_wp(target ? target->GetDebugger().shared_from_this()
378 CommonInitializer(file_spec, target);
381 void SourceManager::File::CommonInitializer(const FileSpec &file_spec,
383 if (m_mod_time == llvm::sys::TimePoint<>()) {
385 m_source_map_mod_id = target->GetSourcePathMap().GetModificationID();
387 if (!file_spec.GetDirectory() && file_spec.GetFilename()) {
388 // If this is just a file name, lets see if we can find it in the
390 bool check_inlines = false;
391 SymbolContextList sc_list;
393 target->GetImages().ResolveSymbolContextForFilePath(
394 file_spec.GetFilename().AsCString(), 0, check_inlines,
395 SymbolContextItem(eSymbolContextModule |
396 eSymbolContextCompUnit),
398 bool got_multiple = false;
399 if (num_matches != 0) {
400 if (num_matches > 1) {
402 FileSpec *test_cu_spec = nullptr;
404 for (unsigned i = 0; i < num_matches; i++) {
405 sc_list.GetContextAtIndex(i, sc);
408 if (test_cu_spec != static_cast<FileSpec *>(sc.comp_unit))
412 test_cu_spec = sc.comp_unit;
418 sc_list.GetContextAtIndex(0, sc);
419 m_file_spec = sc.comp_unit;
420 m_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
424 // Try remapping if m_file_spec does not correspond to an existing file.
425 if (!FileSystem::Instance().Exists(m_file_spec)) {
426 FileSpec new_file_spec;
427 // Check target specific source remappings first, then fall back to
428 // modules objects can have individual path remappings that were
429 // detected when the debug info for a module was found. then
430 if (target->GetSourcePathMap().FindFile(m_file_spec, new_file_spec) ||
431 target->GetImages().FindSourceFile(m_file_spec, new_file_spec)) {
432 m_file_spec = new_file_spec;
433 m_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
439 if (m_mod_time != llvm::sys::TimePoint<>())
440 m_data_sp = FileSystem::Instance().CreateDataBuffer(m_file_spec);
443 uint32_t SourceManager::File::GetLineOffset(uint32_t line) {
450 if (CalculateLineOffsets(line)) {
451 if (line < m_offsets.size())
452 return m_offsets[line - 1]; // yes we want "line - 1" in the index
457 uint32_t SourceManager::File::GetNumLines() {
458 CalculateLineOffsets();
459 return m_offsets.size();
462 const char *SourceManager::File::PeekLineData(uint32_t line) {
463 if (!LineIsValid(line))
466 size_t line_offset = GetLineOffset(line);
467 if (line_offset < m_data_sp->GetByteSize())
468 return (const char *)m_data_sp->GetBytes() + line_offset;
472 uint32_t SourceManager::File::GetLineLength(uint32_t line,
473 bool include_newline_chars) {
474 if (!LineIsValid(line))
477 size_t start_offset = GetLineOffset(line);
478 size_t end_offset = GetLineOffset(line + 1);
479 if (end_offset == UINT32_MAX)
480 end_offset = m_data_sp->GetByteSize();
482 if (end_offset > start_offset) {
483 uint32_t length = end_offset - start_offset;
484 if (!include_newline_chars) {
485 const char *line_start =
486 (const char *)m_data_sp->GetBytes() + start_offset;
488 const char last_char = line_start[length - 1];
489 if ((last_char == '\r') || (last_char == '\n'))
500 bool SourceManager::File::LineIsValid(uint32_t line) {
504 if (CalculateLineOffsets(line))
505 return line < m_offsets.size();
509 void SourceManager::File::UpdateIfNeeded() {
510 // TODO: use host API to sign up for file modifications to anything in our
511 // source cache and only update when we determine a file has been updated.
512 // For now we check each time we want to display info for the file.
513 auto curr_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
515 if (curr_mod_time != llvm::sys::TimePoint<>() &&
516 m_mod_time != curr_mod_time) {
517 m_mod_time = curr_mod_time;
518 m_data_sp = FileSystem::Instance().CreateDataBuffer(m_file_spec);
523 size_t SourceManager::File::DisplaySourceLines(uint32_t line,
524 llvm::Optional<size_t> column,
525 uint32_t context_before,
526 uint32_t context_after,
528 // Nothing to write if there's no stream.
532 // Sanity check m_data_sp before proceeding.
536 size_t bytes_written = s->GetWrittenBytes();
538 auto debugger_sp = m_debugger_wp.lock();
540 HighlightStyle style;
541 // Use the default Vim style if source highlighting is enabled.
542 if (should_highlight_source(debugger_sp))
543 style = HighlightStyle::MakeVimStyle();
545 // If we should mark the stop column with color codes, then copy the prefix
546 // and suffix to our color style.
547 if (should_show_stop_column_with_ansi(debugger_sp))
548 style.selected.Set(debugger_sp->GetStopShowColumnAnsiPrefix(),
549 debugger_sp->GetStopShowColumnAnsiSuffix());
551 HighlighterManager mgr;
552 std::string path = GetFileSpec().GetPath(/*denormalize*/ false);
553 // FIXME: Find a way to get the definitive language this file was written in
554 // and pass it to the highlighter.
555 const auto &h = mgr.getHighlighterFor(lldb::eLanguageTypeUnknown, path);
557 const uint32_t start_line =
558 line <= context_before ? 1 : line - context_before;
559 const uint32_t start_line_offset = GetLineOffset(start_line);
560 if (start_line_offset != UINT32_MAX) {
561 const uint32_t end_line = line + context_after;
562 uint32_t end_line_offset = GetLineOffset(end_line + 1);
563 if (end_line_offset == UINT32_MAX)
564 end_line_offset = m_data_sp->GetByteSize();
566 assert(start_line_offset <= end_line_offset);
567 if (start_line_offset < end_line_offset) {
568 size_t count = end_line_offset - start_line_offset;
569 const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
571 auto ref = llvm::StringRef(reinterpret_cast<const char *>(cstr), count);
573 h.Highlight(style, ref, column, "", *s);
575 // Ensure we get an end of line character one way or another.
576 if (!is_newline_char(ref.back()))
580 return s->GetWrittenBytes() - bytes_written;
583 void SourceManager::File::FindLinesMatchingRegex(
584 RegularExpression ®ex, uint32_t start_line, uint32_t end_line,
585 std::vector<uint32_t> &match_lines) {
588 if (!LineIsValid(start_line) ||
589 (end_line != UINT32_MAX && !LineIsValid(end_line)))
591 if (start_line > end_line)
594 for (uint32_t line_no = start_line; line_no < end_line; line_no++) {
596 if (!GetLine(line_no, buffer))
598 if (regex.Execute(buffer)) {
599 match_lines.push_back(line_no);
604 bool SourceManager::File::FileSpecMatches(const FileSpec &file_spec) {
605 return FileSpec::Equal(m_file_spec, file_spec, false);
608 bool lldb_private::operator==(const SourceManager::File &lhs,
609 const SourceManager::File &rhs) {
610 if (lhs.m_file_spec != rhs.m_file_spec)
612 return lhs.m_mod_time == rhs.m_mod_time;
615 bool SourceManager::File::CalculateLineOffsets(uint32_t line) {
617 UINT32_MAX; // TODO: take this line out when we support partial indexing
618 if (line == UINT32_MAX) {
620 if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX)
623 if (m_offsets.empty()) {
624 if (m_data_sp.get() == nullptr)
627 const char *start = (char *)m_data_sp->GetBytes();
629 const char *end = start + m_data_sp->GetByteSize();
631 // Calculate all line offsets from scratch
633 // Push a 1 at index zero to indicate the file has been completely
635 m_offsets.push_back(UINT32_MAX);
637 for (s = start; s < end; ++s) {
639 if (is_newline_char(curr_ch)) {
642 if (is_newline_char(next_ch)) {
643 if (curr_ch != next_ch)
647 m_offsets.push_back(s + 1 - start);
650 if (!m_offsets.empty()) {
651 if (m_offsets.back() < size_t(end - start))
652 m_offsets.push_back(end - start);
657 // Some lines have been populated, start where we last left off
658 assert("Not implemented yet" && false);
662 // Calculate all line offsets up to "line"
663 assert("Not implemented yet" && false);
668 bool SourceManager::File::GetLine(uint32_t line_no, std::string &buffer) {
669 if (!LineIsValid(line_no))
672 size_t start_offset = GetLineOffset(line_no);
673 size_t end_offset = GetLineOffset(line_no + 1);
674 if (end_offset == UINT32_MAX) {
675 end_offset = m_data_sp->GetByteSize();
677 buffer.assign((char *)m_data_sp->GetBytes() + start_offset,
678 end_offset - start_offset);
683 void SourceManager::SourceFileCache::AddSourceFile(const FileSP &file_sp) {
685 FileCache::iterator pos = m_file_cache.find(file_spec);
686 if (pos == m_file_cache.end())
687 m_file_cache[file_spec] = file_sp;
689 if (file_sp != pos->second)
690 m_file_cache[file_spec] = file_sp;
694 SourceManager::FileSP SourceManager::SourceFileCache::FindSourceFile(
695 const FileSpec &file_spec) const {
697 FileCache::const_iterator pos = m_file_cache.find(file_spec);
698 if (pos != m_file_cache.end())
699 file_sp = pos->second;