Skip to content

Commit 5236e15

Browse files
committed
[lldb] Add support for sorting by size to target module dump symtab
This patch adds support to sort the symbol table by size. The command already supports sorting and it already reports sizes. Sorting by size helps diagnosing size issues. rdar://123788375
1 parent a6d9b7b commit 5236e15

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

lldb/include/lldb/Interpreter/CommandOptionArgumentTable.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ static constexpr OptionEnumValueElement g_sort_option_enumeration[] = {
5050
"name",
5151
"Sort output by symbol name.",
5252
},
53+
{
54+
eSortOrderBySize,
55+
"size",
56+
"Sort output by symbol byte size.",
57+
},
5358
};
5459

5560
// Note that the negation in the argument name causes a slightly confusing

lldb/include/lldb/lldb-private-enumerations.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ enum ArgumentRepetitionType {
108108
// optional
109109
};
110110

111-
enum SortOrder { eSortOrderNone, eSortOrderByAddress, eSortOrderByName };
111+
enum SortOrder {
112+
eSortOrderNone,
113+
eSortOrderByAddress,
114+
eSortOrderByName,
115+
eSortOrderBySize
116+
};
112117

113118
// LazyBool is for boolean values that need to be calculated lazily. Values
114119
// start off set to eLazyBoolCalculate, and then they can be calculated once

lldb/source/Symbol/Symtab.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,8 @@ void Symtab::Dump(Stream *s, Target *target, SortOrder sort_order,
124124
DumpSymbolHeader(s);
125125

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

134130
for (const auto &name_to_symbol : name_map) {
135131
const Symbol *symbol = name_to_symbol.second;
@@ -138,6 +134,21 @@ void Symtab::Dump(Stream *s, Target *target, SortOrder sort_order,
138134
}
139135
} break;
140136

137+
case eSortOrderBySize: {
138+
s->PutCString(" (sorted by size):\n");
139+
DumpSymbolHeader(s);
140+
141+
std::multimap<size_t, const Symbol *, std::greater<size_t>> size_map;
142+
for (const Symbol &symbol : m_symbols)
143+
size_map.emplace(symbol.GetByteSize(), &symbol);
144+
145+
for (const auto &size_to_symbol : size_map) {
146+
const Symbol *symbol = size_to_symbol.second;
147+
s->Indent();
148+
symbol->Dump(s, target, symbol - &m_symbols[0], name_preference);
149+
}
150+
} break;
151+
141152
case eSortOrderByAddress:
142153
s->PutCString(" (sorted by address):\n");
143154
DumpSymbolHeader(s);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# RUN: yaml2obj %S/Inputs/basic-elf.yaml -o %T/symtab.out
2+
# RUN: %lldb %T/symtab.out -o "target symbols add -s symtab.out %S/Inputs/symtab.syms" \
3+
# RUN: -s %s | FileCheck %s
4+
5+
# CHECK: num_symbols = 4 (sorted by size):
6+
# CHECK: [ 0] 0 SX Code 0x0000000000400000 0x00000000000000b0 0x00000000 ___lldb_unnamed_symbol0
7+
# CHECK: [ 3] 0 X Code 0x00000000004000d0 0x0000000000000022 0x00000000 _start
8+
# CHECK: [ 1] 0 X Code 0x00000000004000b0 0x0000000000000010 0x00000000 f1
9+
# CHECK: [ 2] 0 X Code 0x00000000004000c0 0x0000000000000010 0x00000000 f2
10+
11+
image dump symtab -s size symtab.out

0 commit comments

Comments
 (0)