Skip to content

Commit

Permalink
Re-land "[lldb] Expose a const iterator for SymbolContextList"
Browse files Browse the repository at this point in the history
Re-lands 04aa943 with modifications
to fix tests.
I originally reverted this because it caused a test to fail on Linux.
The problem was that I inverted a condition on accident.
  • Loading branch information
bulbazord committed May 5, 2023
1 parent 9c377c5 commit e9eaf7b
Show file tree
Hide file tree
Showing 20 changed files with 220 additions and 362 deletions.
2 changes: 1 addition & 1 deletion lldb/include/lldb/Breakpoint/BreakpointResolverFileLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class BreakpointResolverFileLine : public BreakpointResolver {

protected:
void FilterContexts(SymbolContextList &sc_list);
void DeduceSourceMapping(SymbolContextList &sc_list);
void DeduceSourceMapping(const SymbolContextList &sc_list);

friend class Breakpoint;
SourceLocationSpec m_location_spec;
Expand Down
4 changes: 4 additions & 0 deletions lldb/include/lldb/Symbol/SymbolContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,15 @@ class SymbolContextList {
protected:
typedef std::vector<SymbolContext>
collection; ///< The collection type for the list.
typedef collection::const_iterator const_iterator;

// Member variables.
collection m_symbol_contexts; ///< The list of symbol contexts.

public:
const_iterator begin() const { return m_symbol_contexts.begin(); }
const_iterator end() const { return m_symbol_contexts.end(); }

typedef AdaptedIterable<collection, SymbolContext, vector_adapter>
SymbolContextIterable;
SymbolContextIterable SymbolContexts() {
Expand Down
4 changes: 2 additions & 2 deletions lldb/include/lldb/Symbol/UnwindTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class UnwindTable {
// problem.
lldb::FuncUnwindersSP
GetUncachedFuncUnwindersContainingAddress(const Address &addr,
SymbolContext &sc);
const SymbolContext &sc);

ArchSpec GetArchitecture();

Expand All @@ -62,7 +62,7 @@ class UnwindTable {

void Initialize();
std::optional<AddressRange> GetAddressRange(const Address &addr,
SymbolContext &sc);
const SymbolContext &sc);

typedef std::map<lldb::addr_t, lldb::FuncUnwindersSP> collection;
typedef collection::iterator iterator;
Expand Down
22 changes: 8 additions & 14 deletions lldb/source/API/SBThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,20 +847,14 @@ SBError SBThread::StepOverUntil(lldb::SBFrame &sb_frame,
SymbolContextList sc_list;
frame_sc.comp_unit->ResolveSymbolContext(location_spec,
eSymbolContextLineEntry, sc_list);
const uint32_t num_matches = sc_list.GetSize();
if (num_matches > 0) {
SymbolContext sc;
for (uint32_t i = 0; i < num_matches; ++i) {
if (sc_list.GetContextAtIndex(i, sc)) {
addr_t step_addr =
sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
if (step_addr != LLDB_INVALID_ADDRESS) {
if (fun_range.ContainsLoadAddress(step_addr, target))
step_over_until_addrs.push_back(step_addr);
else
all_in_function = false;
}
}
for (const SymbolContext &sc : sc_list) {
addr_t step_addr =
sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
if (step_addr != LLDB_INVALID_ADDRESS) {
if (fun_range.ContainsLoadAddress(step_addr, target))
step_over_until_addrs.push_back(step_addr);
else
all_in_function = false;
}
}

Expand Down
9 changes: 3 additions & 6 deletions lldb/source/Breakpoint/BreakpointResolverFileLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list) {
}

void BreakpointResolverFileLine::DeduceSourceMapping(
SymbolContextList &sc_list) {
const SymbolContextList &sc_list) {
Target &target = GetBreakpoint()->GetTarget();
if (!target.GetAutoSourceMapRelative())
return;
Expand Down Expand Up @@ -223,13 +223,10 @@ void BreakpointResolverFileLine::DeduceSourceMapping(
return;

const bool case_sensitive = request_file.IsCaseSensitive();
for (uint32_t i = 0; i < sc_list.GetSize(); ++i) {
SymbolContext sc;
sc_list.GetContextAtIndex(i, sc);

for (const SymbolContext &sc : sc_list) {
FileSpec sc_file = sc.line_entry.file;

if (FileSpec::Equal(sc_file, request_file, /*full*/true))
if (FileSpec::Equal(sc_file, request_file, /*full*/ true))
continue;

llvm::StringRef sc_file_dir = sc_file.GetDirectory().GetStringRef();
Expand Down
9 changes: 1 addition & 8 deletions lldb/source/Breakpoint/BreakpointResolverName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,7 @@ BreakpointResolverName::SearchCallback(SearchFilter &filter,
Address break_addr;

// Remove any duplicates between the function list and the symbol list
SymbolContext sc;
if (!func_list.GetSize())
return Searcher::eCallbackReturnContinue;

for (uint32_t i = 0; i < func_list.GetSize(); i++) {
if (!func_list.GetContextAtIndex(i, sc))
continue;

for (const SymbolContext &sc : func_list) {
bool is_reexported = false;

if (sc.block && sc.block->GetInlinedFunctionInfo()) {
Expand Down
19 changes: 8 additions & 11 deletions lldb/source/Commands/CommandCompletions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,15 @@ class SymbolCompleter : public Completer {
function_options.include_inlines = true;
context.module_sp->FindFunctions(m_regex, function_options, sc_list);

SymbolContext sc;
// Now add the functions & symbols to the list - only add if unique:
for (uint32_t i = 0; i < sc_list.GetSize(); i++) {
if (sc_list.GetContextAtIndex(i, sc)) {
ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
// Ensure that the function name matches the regex. This is more than
// a sanity check. It is possible that the demangled function name
// does not start with the prefix, for example when it's in an
// anonymous namespace.
if (!func_name.IsEmpty() && m_regex.Execute(func_name.GetStringRef()))
m_match_set.insert(func_name);
}
for (const SymbolContext &sc : sc_list) {
ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
// Ensure that the function name matches the regex. This is more than
// a sanity check. It is possible that the demangled function name
// does not start with the prefix, for example when it's in an
// anonymous namespace.
if (!func_name.IsEmpty() && m_regex.Execute(func_name.GetStringRef()))
m_match_set.insert(func_name);
}
}
return Searcher::eCallbackReturnContinue;
Expand Down
81 changes: 22 additions & 59 deletions lldb/source/Commands/CommandObjectSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ class CommandObjectSourceInfo : public CommandObjectParsed {
uint32_t num_matches = 0;
// Dump all the line entries for the file in the list.
ConstString last_module_file_name;
uint32_t num_scs = sc_list.GetSize();
for (uint32_t i = 0; i < num_scs; ++i) {
SymbolContext sc;
sc_list.GetContextAtIndex(i, sc);
for (const SymbolContext &sc : sc_list) {
if (sc.comp_unit) {
Module *module = sc.module_sp.get();
CompileUnit *cu = sc.comp_unit;
Expand Down Expand Up @@ -393,10 +390,7 @@ class CommandObjectSourceInfo : public CommandObjectParsed {
SymbolContextList sc_list_symbols;
module_list.FindFunctionSymbols(name, eFunctionNameTypeAuto,
sc_list_symbols);
size_t num_symbol_matches = sc_list_symbols.GetSize();
for (size_t i = 0; i < num_symbol_matches; i++) {
SymbolContext sc;
sc_list_symbols.GetContextAtIndex(i, sc);
for (const SymbolContext &sc : sc_list_symbols) {
if (sc.symbol && sc.symbol->ValueIsAddress()) {
const Address &base_address = sc.symbol->GetAddressRef();
Function *function = base_address.CalculateSymbolContextFunction();
Expand All @@ -412,9 +406,7 @@ class CommandObjectSourceInfo : public CommandObjectParsed {
m_options.symbol_name.c_str());
return false;
}
for (size_t i = 0; i < num_matches; i++) {
SymbolContext sc;
sc_list_funcs.GetContextAtIndex(i, sc);
for (const SymbolContext &sc : sc_list_funcs) {
bool context_found_for_symbol = false;
// Loop through all the ranges in the function.
AddressRange range;
Expand Down Expand Up @@ -926,69 +918,45 @@ class CommandObjectSourceList : public CommandObjectParsed {

// Displaying the source for a symbol. Search for function named name.
FindMatchingFunctions(target, name, sc_list);
size_t num_matches = sc_list.GetSize();
if (!num_matches) {
if (sc_list.GetSize() == 0) {
// If we didn't find any functions with that name, try searching for
// symbols that line up exactly with function addresses.
SymbolContextList sc_list_symbols;
FindMatchingFunctionSymbols(target, name, sc_list_symbols);
size_t num_symbol_matches = sc_list_symbols.GetSize();

for (size_t i = 0; i < num_symbol_matches; i++) {
SymbolContext sc;
sc_list_symbols.GetContextAtIndex(i, sc);
for (const SymbolContext &sc : sc_list_symbols) {
if (sc.symbol && sc.symbol->ValueIsAddress()) {
const Address &base_address = sc.symbol->GetAddressRef();
Function *function = base_address.CalculateSymbolContextFunction();
if (function) {
sc_list.Append(SymbolContext(function));
num_matches++;
break;
}
}
}
}

if (num_matches == 0) {
if (sc_list.GetSize() == 0) {
result.AppendErrorWithFormat("Could not find function named: \"%s\".\n",
m_options.symbol_name.c_str());
return false;
}

if (num_matches > 1) {
std::set<SourceInfo> source_match_set;

bool displayed_something = false;
for (size_t i = 0; i < num_matches; i++) {
SymbolContext sc;
sc_list.GetContextAtIndex(i, sc);
SourceInfo source_info(sc.GetFunctionName(),
sc.GetFunctionStartLineEntry());

if (source_info.IsValid()) {
if (source_match_set.find(source_info) == source_match_set.end()) {
source_match_set.insert(source_info);
if (DisplayFunctionSource(sc, source_info, result))
displayed_something = true;
}
}
}

if (displayed_something)
result.SetStatus(eReturnStatusSuccessFinishResult);
else
result.SetStatus(eReturnStatusFailed);
} else {
SymbolContext sc;
sc_list.GetContextAtIndex(0, sc);
SourceInfo source_info;

if (DisplayFunctionSource(sc, source_info, result)) {
result.SetStatus(eReturnStatusSuccessFinishResult);
} else {
result.SetStatus(eReturnStatusFailed);
std::set<SourceInfo> source_match_set;
bool displayed_something = false;
for (const SymbolContext &sc : sc_list) {
SourceInfo source_info(sc.GetFunctionName(),
sc.GetFunctionStartLineEntry());
if (source_info.IsValid() &&
source_match_set.find(source_info) == source_match_set.end()) {
source_match_set.insert(source_info);
if (DisplayFunctionSource(sc, source_info, result))
displayed_something = true;
}
}
if (displayed_something)
result.SetStatus(eReturnStatusSuccessFinishResult);
else
result.SetStatus(eReturnStatusFailed);
return result.Succeeded();
} else if (m_options.address != LLDB_INVALID_ADDRESS) {
Address so_addr;
Expand Down Expand Up @@ -1052,10 +1020,7 @@ class CommandObjectSourceList : public CommandObjectParsed {
return false;
}
}
uint32_t num_matches = sc_list.GetSize();
for (uint32_t i = 0; i < num_matches; ++i) {
SymbolContext sc;
sc_list.GetContextAtIndex(i, sc);
for (const SymbolContext &sc : sc_list) {
if (sc.comp_unit) {
if (m_options.show_bp_locs) {
m_breakpoint_locations.Clear();
Expand Down Expand Up @@ -1175,9 +1140,7 @@ class CommandObjectSourceList : public CommandObjectParsed {
bool got_multiple = false;
CompileUnit *test_cu = nullptr;

for (unsigned i = 0; i < num_matches; i++) {
SymbolContext sc;
sc_list.GetContextAtIndex(i, sc);
for (const SymbolContext &sc : sc_list) {
if (sc.comp_unit) {
if (test_cu) {
if (test_cu != sc.comp_unit)
Expand Down
Loading

0 comments on commit e9eaf7b

Please sign in to comment.