Skip to content

[lldb] Deal with SupportFiles in SourceManager (NFC) #106740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions lldb/include/lldb/Core/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,13 @@ class SourceManager {

~SourceManager();

FileSP GetLastFile() { return GetFile(m_last_file_spec); }
FileSP GetLastFile() { return GetFile(m_last_support_file_sp); }

size_t
DisplaySourceLinesWithLineNumbers(const FileSpec &file, uint32_t line,
uint32_t column, uint32_t context_before,
uint32_t context_after,
const char *current_line_cstr, Stream *s,
const SymbolContextList *bp_locs = nullptr);
size_t DisplaySourceLinesWithLineNumbers(
lldb::SupportFileSP support_file_sp, uint32_t line, uint32_t column,
uint32_t context_before, uint32_t context_after,
const char *current_line_cstr, Stream *s,
const SymbolContextList *bp_locs = nullptr);

// This variant uses the last file we visited.
size_t DisplaySourceLinesWithLineNumbersUsingLastFile(
Expand All @@ -159,22 +158,31 @@ class SourceManager {
size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse,
const SymbolContextList *bp_locs = nullptr);

bool SetDefaultFileAndLine(const FileSpec &file_spec, uint32_t line);
bool SetDefaultFileAndLine(lldb::SupportFileSP support_file_sp,
uint32_t line);

struct SupportFileAndLine {
lldb::SupportFileSP support_file_sp;
uint32_t line;
SupportFileAndLine(lldb::SupportFileSP support_file_sp, uint32_t line)
: support_file_sp(support_file_sp), line(line) {}
};

bool GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line);
std::optional<SupportFileAndLine> GetDefaultFileAndLine();

bool DefaultFileAndLineSet() {
return (GetFile(m_last_file_spec).get() != nullptr);
return (GetFile(m_last_support_file_sp).get() != nullptr);
}

void FindLinesMatchingRegex(FileSpec &file_spec, RegularExpression &regex,
uint32_t start_line, uint32_t end_line,
void FindLinesMatchingRegex(lldb::SupportFileSP support_file_sp,
RegularExpression &regex, uint32_t start_line,
uint32_t end_line,
std::vector<uint32_t> &match_lines);

FileSP GetFile(const FileSpec &file_spec);
FileSP GetFile(lldb::SupportFileSP support_file_sp);

protected:
FileSpec m_last_file_spec;
lldb::SupportFileSP m_last_support_file_sp;
uint32_t m_last_line;
uint32_t m_last_count;
bool m_default_set;
Expand Down
10 changes: 5 additions & 5 deletions lldb/source/API/SBSourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ class SourceManagerImpl {
lldb::TargetSP target_sp(m_target_wp.lock());
if (target_sp) {
return target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers(
file, line, column, context_before, context_after, current_line_cstr,
s);
std::make_shared<SupportFile>(file), line, column, context_before,
context_after, current_line_cstr, s);
} else {
lldb::DebuggerSP debugger_sp(m_debugger_wp.lock());
if (debugger_sp) {
return debugger_sp->GetSourceManager()
.DisplaySourceLinesWithLineNumbers(file, line, column,
context_before, context_after,
current_line_cstr, s);
.DisplaySourceLinesWithLineNumbers(
std::make_shared<SupportFile>(file), line, column,
context_before, context_after, current_line_cstr, s);
}
}
return 0;
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Breakpoint/BreakpointResolverFileRegex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ Searcher::CallbackReturn BreakpointResolverFileRegex::SearchCallback(
FileSpec cu_file_spec = cu->GetPrimaryFile();
std::vector<uint32_t> line_matches;
context.target_sp->GetSourceManager().FindLinesMatchingRegex(
cu_file_spec, m_regex, 1, UINT32_MAX, line_matches);
std::make_shared<SupportFile>(cu_file_spec), m_regex, 1, UINT32_MAX,
line_matches);

uint32_t num_matches = line_matches.size();
for (uint32_t i = 0; i < num_matches; i++) {
Expand Down
16 changes: 10 additions & 6 deletions lldb/source/Commands/CommandObjectBreakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,20 +769,26 @@ class CommandObjectBreakpointSet : public CommandObjectParsed {
private:
bool GetDefaultFile(Target &target, FileSpec &file,
CommandReturnObject &result) {
uint32_t default_line;
// First use the Source Manager's default file. Then use the current stack
// frame's file.
if (!target.GetSourceManager().GetDefaultFileAndLine(file, default_line)) {
if (auto maybe_file_and_line =
target.GetSourceManager().GetDefaultFileAndLine()) {
file = maybe_file_and_line->support_file_sp->GetSpecOnly();
return true;
}

StackFrame *cur_frame = m_exe_ctx.GetFramePtr();
if (cur_frame == nullptr) {
result.AppendError(
"No selected frame to use to find the default file.");
return false;
} else if (!cur_frame->HasDebugInformation()) {
}
if (!cur_frame->HasDebugInformation()) {
result.AppendError("Cannot use the selected frame to find the default "
"file, it has no debug info.");
return false;
} else {
}

const SymbolContext &sc =
cur_frame->GetSymbolContext(eSymbolContextLineEntry);
if (sc.line_entry.GetFile()) {
Expand All @@ -791,8 +797,6 @@ class CommandObjectBreakpointSet : public CommandObjectParsed {
result.AppendError("Can't find the file for the selected frame to "
"use as the default file.");
return false;
}
}
}
return true;
}
Expand Down
26 changes: 15 additions & 11 deletions lldb/source/Commands/CommandObjectSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,14 +777,16 @@ class CommandObjectSourceList : public CommandObjectParsed {
if (sc.function) {
Target &target = GetTarget();

FileSpec start_file;
SupportFileSP start_file = std::make_shared<SupportFile>();
uint32_t start_line;
uint32_t end_line;
FileSpec end_file;

if (sc.block == nullptr) {
// Not an inlined function
sc.function->GetStartLineSourceInfo(start_file, start_line);
FileSpec function_file_spec;
sc.function->GetStartLineSourceInfo(function_file_spec, start_line);
start_file = std::make_shared<SupportFile>(function_file_spec);
if (start_line == 0) {
result.AppendErrorWithFormat("Could not find line information for "
"start of function: \"%s\".\n",
Expand All @@ -794,7 +796,7 @@ class CommandObjectSourceList : public CommandObjectParsed {
sc.function->GetEndLineSourceInfo(end_file, end_line);
} else {
// We have an inlined function
start_file = source_info.line_entry.GetFile();
start_file = source_info.line_entry.file_sp;
start_line = source_info.line_entry.line;
end_line = start_line + m_options.num_lines;
}
Expand Down Expand Up @@ -825,14 +827,15 @@ class CommandObjectSourceList : public CommandObjectParsed {

if (m_options.show_bp_locs) {
const bool show_inlines = true;
m_breakpoint_locations.Reset(start_file, 0, show_inlines);
m_breakpoint_locations.Reset(start_file->GetSpecOnly(), 0,
show_inlines);
SearchFilterForUnconstrainedSearches target_search_filter(
m_exe_ctx.GetTargetSP());
target_search_filter.Search(m_breakpoint_locations);
}

result.AppendMessageWithFormat("File: %s\n",
start_file.GetPath().c_str());
result.AppendMessageWithFormat(
"File: %s\n", start_file->GetSpecOnly().GetPath().c_str());
// We don't care about the column here.
const uint32_t column = 0;
return target.GetSourceManager().DisplaySourceLinesWithLineNumbers(
Expand Down Expand Up @@ -1050,8 +1053,9 @@ class CommandObjectSourceList : public CommandObjectParsed {
? sc.line_entry.column
: 0;
target.GetSourceManager().DisplaySourceLinesWithLineNumbers(
sc.comp_unit->GetPrimaryFile(), sc.line_entry.line, column,
lines_to_back_up, m_options.num_lines - lines_to_back_up, "->",
std::make_shared<SupportFile>(sc.comp_unit->GetPrimaryFile()),
sc.line_entry.line, column, lines_to_back_up,
m_options.num_lines - lines_to_back_up, "->",
&result.GetOutputStream(), GetBreakpointLocations());
result.SetStatus(eReturnStatusSuccessFinishResult);
}
Expand Down Expand Up @@ -1170,9 +1174,9 @@ class CommandObjectSourceList : public CommandObjectParsed {
m_options.num_lines = 10;
const uint32_t column = 0;
target.GetSourceManager().DisplaySourceLinesWithLineNumbers(
sc.comp_unit->GetPrimaryFile(), m_options.start_line, column, 0,
m_options.num_lines, "", &result.GetOutputStream(),
GetBreakpointLocations());
std::make_shared<SupportFile>(sc.comp_unit->GetPrimaryFile()),
m_options.start_line, column, 0, m_options.num_lines, "",
&result.GetOutputStream(), GetBreakpointLocations());

result.SetStatus(eReturnStatusSuccessFinishResult);
} else {
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Core/Disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,8 @@ void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch,
line_highlight = "**";
}
source_manager.DisplaySourceLinesWithLineNumbers(
ln.file, ln.line, ln.column, 0, 0, line_highlight, &strm);
std::make_shared<SupportFile>(ln.file), ln.line, ln.column, 0, 0,
line_highlight, &strm);
}
if (source_lines_to_display.print_source_context_end_eol)
strm.EOL();
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Core/IOHandlerCursesGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6910,8 +6910,8 @@ class SourceFileWindowDelegate : public WindowDelegate {
} else {
// File changed, set selected line to the line with the PC
m_selected_line = m_pc_line;
m_file_sp = m_debugger.GetSourceManager().GetFile(
m_sc.line_entry.GetFile());
m_file_sp =
m_debugger.GetSourceManager().GetFile(m_sc.line_entry.file_sp);
if (m_file_sp) {
const size_t num_lines = m_file_sp->GetNumLines();
m_line_width = 1;
Expand Down
Loading
Loading