Skip to content

Commit fbc32ba

Browse files
committed
[lldb][Commands] image lookup: avoid double type lookup into current module
The `current_module` pointer here was never set, but we check it when looping over the `target_modules` list. Presumably the intention was to avoid calling `LookupInModule` if we already found the type in the current module. This only affects `image lookup --all`. This patch sets `current_module` if we successfully completed a lookup into it. Before: ``` (lldb) im loo -vt Foo --all Best match found in /Users/jonas/Git/llvm-worktrees/llvm-project/a.out: id = {0x00000037}, name = "Foo", byte-size = 1, decl = foo.cpp:1, compiler_type = "struct Foo { }" 1 match found in /Users/jonas/Git/llvm-worktrees/llvm-project/a.out: id = {0x00000037}, name = "Foo", byte-size = 1, decl = foo.cpp:1, compiler_type = "struct Foo { }" ``` After: ``` (lldb) im loo -vt Foo --all Best match found in /Users/jonas/Git/llvm-worktrees/llvm-project/a.out: id = {0x00000037}, name = "Foo", byte-size = 1, decl = foo.cpp:1, compiler_type = "struct Foo { }" ```
1 parent 24fd343 commit fbc32ba

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

lldb/source/Commands/CommandObjectTarget.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,46 +3946,46 @@ class CommandObjectTargetModulesLookup : public CommandObjectParsed {
39463946

39473947
Options *GetOptions() override { return &m_options; }
39483948

3949-
bool LookupHere(CommandInterpreter &interpreter, CommandReturnObject &result,
3950-
bool &syntax_error) {
3949+
ModuleSP LookupHere(CommandInterpreter &interpreter,
3950+
CommandReturnObject &result, bool &syntax_error) {
39513951
switch (m_options.m_type) {
39523952
case eLookupTypeAddress:
39533953
case eLookupTypeFileLine:
39543954
case eLookupTypeFunction:
39553955
case eLookupTypeFunctionOrSymbol:
39563956
case eLookupTypeSymbol:
39573957
default:
3958-
return false;
3958+
return nullptr;
39593959
case eLookupTypeType:
39603960
break;
39613961
}
39623962

39633963
StackFrameSP frame = m_exe_ctx.GetFrameSP();
39643964

39653965
if (!frame)
3966-
return false;
3966+
return nullptr;
39673967

39683968
const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
39693969

39703970
if (!sym_ctx.module_sp)
3971-
return false;
3971+
return nullptr;
39723972

39733973
switch (m_options.m_type) {
39743974
default:
3975-
return false;
3975+
return nullptr;
39763976
case eLookupTypeType:
39773977
if (!m_options.m_str.empty()) {
39783978
if (LookupTypeHere(&GetTarget(), m_interpreter,
39793979
result.GetOutputStream(), *sym_ctx.module_sp,
39803980
m_options.m_str.c_str(), m_options.m_use_regex)) {
39813981
result.SetStatus(eReturnStatusSuccessFinishResult);
3982-
return true;
3982+
return sym_ctx.module_sp;
39833983
}
39843984
}
39853985
break;
39863986
}
39873987

3988-
return false;
3988+
return nullptr;
39893989
}
39903990

39913991
bool LookupInModule(CommandInterpreter &interpreter, Module *module,
@@ -4086,12 +4086,12 @@ class CommandObjectTargetModulesLookup : public CommandObjectParsed {
40864086
// Dump all sections for all modules images
40874087

40884088
if (command.GetArgumentCount() == 0) {
4089-
ModuleSP current_module;
4090-
40914089
// Where it is possible to look in the current symbol context first,
40924090
// try that. If this search was successful and --all was not passed,
40934091
// don't print anything else.
4094-
if (LookupHere(m_interpreter, result, syntax_error)) {
4092+
ModuleSP current_module_sp =
4093+
LookupHere(m_interpreter, result, syntax_error);
4094+
if (current_module_sp) {
40954095
result.GetOutputStream().EOL();
40964096
num_successful_lookups++;
40974097
if (!m_options.m_print_all) {
@@ -4110,7 +4110,7 @@ class CommandObjectTargetModulesLookup : public CommandObjectParsed {
41104110
}
41114111

41124112
for (ModuleSP module_sp : target_modules.ModulesNoLocking()) {
4113-
if (module_sp != current_module &&
4113+
if (module_sp != current_module_sp &&
41144114
LookupInModule(m_interpreter, module_sp.get(), result,
41154115
syntax_error)) {
41164116
result.GetOutputStream().EOL();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# REQUIRES: system-darwin
2+
3+
# RUN: split-file %s %t
4+
# RUN: %clang_host -g -gdwarf %t/lib1.cpp -shared -o %t-lib1.dylib
5+
# RUN: %clang_host -g -gdwarf %t/lib2.cpp -shared -o %t-lib2.dylib
6+
# RUN: %clang_host -g -gdwarf %t/main.cpp %t-lib1.dylib %t-lib2.dylib -o %t.out
7+
# RUN: %lldb -x -b -s %t/commands.input %t.out -o exit 2>&1 \
8+
# RUN: | FileCheck %s
9+
10+
#--- main.cpp
11+
12+
struct Foo {} f;
13+
14+
int main() { __builtin_debugtrap(); }
15+
16+
#--- lib1.cpp
17+
18+
struct Foo {} f1;
19+
20+
#--- lib2.cpp
21+
22+
struct Foo {} f2;
23+
24+
#--- commands.input
25+
26+
run
27+
target modules lookup --type Foo
28+
29+
# CHECK: (lldb) target modules lookup --type Foo
30+
# CHECK-NEXT: Best match found in
31+
# CHECK-NEXT: name = "Foo"
32+
33+
# Confirm we only dumped the match once.
34+
# CHECK-NOT: name = "Foo"
35+
36+
target modules lookup --type Foo --all
37+
38+
# CHECK: (lldb) target modules lookup --type Foo --all
39+
# CHECK-NEXT: Best match found in
40+
# CHECK-NEXT: name = "Foo"
41+
42+
# CHECK: 1 match found in {{.*}}lib1.dylib
43+
# CHECK: 1 match found in {{.*}}lib2.dylib

0 commit comments

Comments
 (0)