-
Notifications
You must be signed in to change notification settings - Fork 37
/
hdmp.cc
135 lines (115 loc) · 4.31 KB
/
hdmp.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (c) 2022 Arista Networks, Inc. All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.
#include "libpstack/proc.h"
#include "heap.h"
#include <unistd.h>
#include <set>
#include <cassert>
using namespace pstack;
enum printoption {
heap_allocated, heap_recentfree, heap_historicbig
};
std::set<printoption> options;
void printStack(std::ostream &os, Dwarf::ImageCache &ic, std::shared_ptr<Procman::Process> &proc, const hdbg_info &info, void **frames) {
for (size_t i = 0; i < info.maxframes && frames[i] != nullptr; ++i) {
uintptr_t frameip = uintptr_t(frames[i]);
if (i != 0)
frameip--;
// find the segment containing the instruction pointer.
auto &&[elfReloc, elf, phdr] = proc->findSegment(frameip);
os << "\t" << frames[i];
if (elf) {
auto found = elf->findSymbolByAddress(frameip - elfReloc, STT_FUNC);
if (found) {
auto &[ sym, name ] = *found;
os << "\t" << name << "+" << uintptr_t(frames[i]) - elfReloc - sym.st_value;
}
auto dwarf = ic.getDwarf(elf);
if (dwarf) {
auto sep = "in";
for (auto &&[file, line] : dwarf->sourceFromAddr(frameip - elfReloc)) {
os << " " << sep << " " << file << ":" << line;
sep = ",";
}
}
}
os << "\n";
}
os << "\n";
}
void printBlocks(std::ostream &os, Dwarf::ImageCache &ic, std::shared_ptr<Procman::Process> proc, const hdbg_info &info, const memdesc_list &list, enum memstate state) {
size_t sz = sizeof (struct memdesc) + info.maxframes * sizeof (void *);
struct memdesc *hdr = (memdesc *)malloc(sz);
for (Elf::Addr addr = (Elf::Addr)list.tqh_first; addr; addr = (Elf::Addr)hdr->node.tqe_next) {
if (proc->io->read((uintptr_t)addr, sz, (char *)hdr) != sz)
break;
os << "ptr=" << hdr->data + 1;
memstate head = proc->io->readObj<memstate>((Elf::Addr)&hdr->data->state);
memstate tail = proc->io->readObj<memstate>((Elf::Addr)(hdr->data + 1) + hdr->len);
if (head != state) {
std::cout << " BADHEAD";
}
if (tail != state) {
std::cout << " BADTAIL";
}
os << " size=" << hdr->len << "\n";
printStack(os, ic, proc, info, hdr->stack);
}
free(hdr);
}
void dumpHeap(std::shared_ptr<Procman::Process> proc, std::ostream &os, Dwarf::ImageCache &ic)
{
Procman::StopProcess here(proc.get());
Elf::Addr sym = proc->resolveSymbol("hdbg", false);
assert(sym);
auto info = proc->io->readObj<hdbg_info>(sym);
os << "Allocator usage statistics:\n\n"
<< "Calls to malloc: " << info.stats.malloc_calls << "\n"
<< "Calls to free: " << info.stats.free_calls << "\n"
<< "Calls to calloc: " << info.stats.calloc_calls << "\n"
<< "Calls to realloc: " << info.stats.realloc_calls << "\n"
<< "Calls to aligned_alloc et al: " << info.stats.aligned_alloc_calls << "\n"
;
os << "\nStack at termination:\n\n";
printStack(os, ic, proc, info, info.crashstack);
if (options.find(heap_allocated) != options.end()) {
os << "\nCurrently allocated memory:\n\n";
printBlocks(os, ic, proc, info, info.heap, mem_allocated);
}
if (options.find(heap_recentfree) != options.end()) {
os << "\nRecently freed memory:\n\n";
printBlocks(os, ic, proc, info, info.freelist, mem_free);
}
if (options.find(heap_historicbig) != options.end()) {
os << "\nHistoric large allocations:\n\n";
printBlocks(os, ic, proc, info, info.freebig, mem_free);
}
}
int
main(int argc, char *argv[])
{
Dwarf::ImageCache ic;
std::shared_ptr<Elf::Object> exec;
int c;
while ((c = getopt(argc, argv, "e:fab")) != -1) {
switch (c) {
case 'e':
exec = ic.getImageForName(optarg);
break;
case 'f':
options.insert(heap_recentfree);
break;
case 'a':
options.insert(heap_allocated);
break;
case 'b':
options.insert(heap_historicbig);
break;
}
}
if (options.empty()) {
options = { heap_recentfree, heap_allocated, heap_historicbig };
}
for (int i = optind; i < argc; ++i)
dumpHeap(Procman::Process::load(exec, argv[i], PstackOptions{}, ic), std::cout, ic);
}