Skip to content

Commit 62f479a

Browse files
authored
Merge pull request #8313 from apple/jdevlieghere/rdar/123788375
[lldb] Add support for sorting by size to `target module dump symtab`…
2 parents e452404 + 0d210a4 commit 62f479a

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-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: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,8 @@ void Symtab::Dump(Stream *s, Target *target, SortOrder sort_order,
128128
DumpSymbolHeader(s);
129129

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

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

141+
case eSortOrderBySize: {
142+
s->PutCString(" (sorted by size):\n");
143+
DumpSymbolHeader(s);
144+
145+
std::multimap<size_t, const Symbol *, std::greater<size_t>> size_map;
146+
for (const Symbol &symbol : m_symbols)
147+
size_map.emplace(symbol.GetByteSize(), &symbol);
148+
149+
size_t idx = 0;
150+
for (const auto &size_to_symbol : size_map) {
151+
const Symbol *symbol = size_to_symbol.second;
152+
s->Indent();
153+
symbol->Dump(s, target, idx++, name_preference);
154+
}
155+
} break;
156+
145157
case eSortOrderByAddress:
146158
s->PutCString(" (sorted by address):\n");
147159
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: [ 1] 0 X Code 0x00000000004000d0 0x0000000000000022 0x00000000 _start
8+
# CHECK: [ 2] 0 X Code 0x00000000004000b0 0x0000000000000010 0x00000000 f1
9+
# CHECK: [ 3] 0 X Code 0x00000000004000c0 0x0000000000000010 0x00000000 f2
10+
11+
image dump symtab -s size symtab.out

0 commit comments

Comments
 (0)