Skip to content

[lldb] Add support for sorting by size to target module dump symtab #8313

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
Mar 15, 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
5 changes: 5 additions & 0 deletions lldb/include/lldb/Interpreter/CommandOptionArgumentTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ static constexpr OptionEnumValueElement g_sort_option_enumeration[] = {
"name",
"Sort output by symbol name.",
},
{
eSortOrderBySize,
"size",
"Sort output by symbol byte size.",
},
};

// Note that the negation in the argument name causes a slightly confusing
Expand Down
7 changes: 6 additions & 1 deletion lldb/include/lldb/lldb-private-enumerations.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ enum ArgumentRepetitionType {
// optional
};

enum SortOrder { eSortOrderNone, eSortOrderByAddress, eSortOrderByName };
enum SortOrder {
eSortOrderNone,
eSortOrderByAddress,
eSortOrderByName,
eSortOrderBySize
};

// LazyBool is for boolean values that need to be calculated lazily. Values
// start off set to eLazyBoolCalculate, and then they can be calculated once
Expand Down
24 changes: 18 additions & 6 deletions lldb/source/Symbol/Symtab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,8 @@ void Symtab::Dump(Stream *s, Target *target, SortOrder sort_order,
DumpSymbolHeader(s);

std::multimap<llvm::StringRef, const Symbol *> name_map;
for (const_iterator pos = m_symbols.begin(), end = m_symbols.end();
pos != end; ++pos) {
const char *name = pos->GetName().AsCString();
if (name && name[0])
name_map.insert(std::make_pair(name, &(*pos)));
}
for (const Symbol &symbol : m_symbols)
name_map.emplace(symbol.GetName().GetStringRef(), &symbol);

for (const auto &name_to_symbol : name_map) {
const Symbol *symbol = name_to_symbol.second;
Expand All @@ -142,6 +138,22 @@ void Symtab::Dump(Stream *s, Target *target, SortOrder sort_order,
}
} break;

case eSortOrderBySize: {
s->PutCString(" (sorted by size):\n");
DumpSymbolHeader(s);

std::multimap<size_t, const Symbol *, std::greater<size_t>> size_map;
for (const Symbol &symbol : m_symbols)
size_map.emplace(symbol.GetByteSize(), &symbol);

size_t idx = 0;
for (const auto &size_to_symbol : size_map) {
const Symbol *symbol = size_to_symbol.second;
s->Indent();
symbol->Dump(s, target, idx++, name_preference);
}
} break;

case eSortOrderByAddress:
s->PutCString(" (sorted by address):\n");
DumpSymbolHeader(s);
Expand Down
11 changes: 11 additions & 0 deletions lldb/test/Shell/SymbolFile/Breakpad/symtab-sorted-by-size.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# RUN: yaml2obj %S/Inputs/basic-elf.yaml -o %T/symtab.out
# RUN: %lldb %T/symtab.out -o "target symbols add -s symtab.out %S/Inputs/symtab.syms" \
# RUN: -s %s | FileCheck %s

# CHECK: num_symbols = 4 (sorted by size):
# CHECK: [ 0] 0 SX Code 0x0000000000400000 0x00000000000000b0 0x00000000 ___lldb_unnamed_symbol0
# CHECK: [ 1] 0 X Code 0x00000000004000d0 0x0000000000000022 0x00000000 _start
# CHECK: [ 2] 0 X Code 0x00000000004000b0 0x0000000000000010 0x00000000 f1
# CHECK: [ 3] 0 X Code 0x00000000004000c0 0x0000000000000010 0x00000000 f2

image dump symtab -s size symtab.out