forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemdump.c
311 lines (262 loc) · 8.3 KB
/
memdump.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* Only possible if we're in developer mode. */
#include "config.h"
#if DEVELOPER
#include <backtrace.h>
#include <ccan/tal/str/str.h>
#include <common/json_command.h>
#include <common/json_param.h>
#include <common/memleak.h>
#include <common/timeout.h>
#include <connectd/connectd_wiregen.h>
#include <errno.h>
#include <gossipd/gossipd_wiregen.h>
#include <hsmd/hsmd_wiregen.h>
#include <lightningd/chaintopology.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/memdump.h>
#include <lightningd/opening_common.h>
#include <lightningd/peer_control.h>
#include <lightningd/subd.h>
#include <wire/wire_sync.h>
static void json_add_ptr(struct json_stream *response, const char *name,
const void *ptr)
{
char ptrstr[STR_MAX_CHARS(void *)];
snprintf(ptrstr, sizeof(ptrstr), "%p", ptr);
json_add_string(response, name, ptrstr);
}
static size_t add_memdump(struct json_stream *response,
const char *name, const tal_t *root,
struct command *cmd)
{
const tal_t *i;
size_t cumulative_size = 0;
json_array_start(response, name);
for (i = tal_first(root); i; i = tal_next(i)) {
const char *name = tal_name(i);
size_t size = tal_bytelen(i);
/* Don't try to dump this command! */
if (i == cmd || i == cmd->jcon)
continue;
/* Don't dump logs, we know they grow. */
if (name && streq(name, "struct log_book"))
continue;
json_object_start(response, NULL);
json_add_ptr(response, "parent", tal_parent(i));
json_add_ptr(response, "value", i);
json_add_u64(response, "size", size);
if (name)
json_add_string(response, "label", name);
if (tal_first(i))
size += add_memdump(response, "children", i, cmd);
json_add_u64(response, "cumulative_size", size);
json_object_end(response);
cumulative_size += size;
}
json_array_end(response);
return cumulative_size;
}
static struct command_result *json_memdump(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct json_stream *response;
if (!param(cmd, buffer, params, NULL))
return command_param_failed();
response = json_stream_success(cmd);
add_memdump(response, "memdump", NULL, cmd);
return command_success(cmd, response);
}
static const struct json_command dev_memdump_command = {
"dev-memdump",
"developer",
json_memdump,
"Show memory objects currently in use"
};
AUTODATA(json_command, &dev_memdump_command);
static int json_add_syminfo(void *data, uintptr_t pc UNUSED,
const char *filename, int lineno,
const char *function)
{
struct json_stream *response = data;
char *str;
/* This can happen in backtraces. */
if (!filename || !function)
return 0;
str = tal_fmt(response, "%s:%u (%s)", filename, lineno, function);
json_add_string(response, NULL, str);
tal_free(str);
return 0;
}
static void json_add_backtrace(struct json_stream *response,
const uintptr_t *bt)
{
size_t i;
if (!bt)
return;
json_array_start(response, "backtrace");
/* First one serves as counter. */
for (i = 1; i < bt[0]; i++) {
backtrace_pcinfo(backtrace_state,
bt[i], json_add_syminfo,
NULL, response);
}
json_array_end(response);
}
static void finish_report(const struct leak_detect *leaks)
{
struct htable *memtable;
const tal_t *i;
const uintptr_t *backtrace;
struct command *cmd;
struct lightningd *ld;
struct json_stream *response;
/* If it timed out, we free ourselved and exit! */
if (!leaks->cmd) {
tal_free(leaks);
return;
}
/* Convenience variables */
cmd = leaks->cmd;
ld = cmd->ld;
/* Enter everything, except this cmd and its jcon */
memtable = memleak_find_allocations(cmd, cmd, cmd->jcon);
/* First delete known false positives. */
memleak_remove_htable(memtable, &ld->topology->txwatches.raw);
memleak_remove_htable(memtable, &ld->topology->txowatches.raw);
memleak_remove_htable(memtable, &ld->htlcs_in.raw);
memleak_remove_htable(memtable, &ld->htlcs_out.raw);
memleak_remove_htable(memtable, &ld->htlc_sets.raw);
/* Now delete ld and those which it has pointers to. */
memleak_remove_region(memtable, ld, sizeof(*ld));
response = json_stream_success(cmd);
json_array_start(response, "leaks");
while ((i = memleak_get(memtable, &backtrace)) != NULL) {
const tal_t *p;
json_object_start(response, NULL);
json_add_ptr(response, "value", i);
if (tal_name(i))
json_add_string(response, "label", tal_name(i));
json_add_backtrace(response, backtrace);
json_array_start(response, "parents");
for (p = tal_parent(i); p; p = tal_parent(p)) {
json_add_string(response, NULL, tal_name(p));
p = tal_parent(p);
}
json_array_end(response);
json_object_end(response);
}
for (size_t i = 0; i < tal_count(leaks->leakers); i++) {
json_object_start(response, NULL);
json_add_string(response, "subdaemon", leaks->leakers[i]);
json_object_end(response);
}
json_array_end(response);
/* Command is now done. */
was_pending(command_success(cmd, response));
}
static void leak_detect_timeout(struct leak_detect *leak_detect)
{
/* We actually *do* leak the leak_detect, but cmd is about
* to exit. */
notleak(tal_steal(NULL, leak_detect));
finish_report(leak_detect);
leak_detect->cmd = NULL;
}
static void leak_detect_req_done(const struct subd_req *req,
struct leak_detect *leak_detect)
{
leak_detect->num_outstanding_requests--;
if (leak_detect->num_outstanding_requests == 0)
finish_report(leak_detect);
}
/* Start a leak request: decrements num_outstanding_requests when freed. */
void start_leak_request(const struct subd_req *req,
struct leak_detect *leak_detect)
{
leak_detect->num_outstanding_requests++;
/* When req is freed, request finished. */
tal_add_destructor2(req, leak_detect_req_done, leak_detect);
}
/* Yep, found a leak in this subd. */
void report_subd_memleak(struct leak_detect *leak_detect, struct subd *leaker)
{
tal_arr_expand(&leak_detect->leakers,
tal_strdup(leak_detect, leaker->name));
}
static void gossip_dev_memleak_done(struct subd *gossipd,
const u8 *reply,
const int *fds UNUSED,
struct leak_detect *leaks)
{
bool found_leak;
if (!fromwire_gossipd_dev_memleak_reply(reply, &found_leak))
fatal("Bad gossip_dev_memleak");
if (found_leak)
report_subd_memleak(leaks, gossipd);
}
static void connect_dev_memleak_done(struct subd *connectd,
const u8 *reply,
const int *fds UNUSED,
struct leak_detect *leaks)
{
bool found_leak;
if (!fromwire_connectd_dev_memleak_reply(reply, &found_leak))
fatal("Bad connect_dev_memleak");
if (found_leak)
report_subd_memleak(leaks, connectd);
}
static struct command_result *json_memleak(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct lightningd *ld = cmd->ld;
u8 *msg;
bool found_leak;
struct leak_detect *leaks;
if (!param(cmd, buffer, params, NULL))
return command_param_failed();
if (!getenv("LIGHTNINGD_DEV_MEMLEAK")) {
return command_fail(cmd, LIGHTNINGD,
"Leak detection needs $LIGHTNINGD_DEV_MEMLEAK");
}
leaks = tal(cmd, struct leak_detect);
leaks->cmd = cmd;
leaks->num_outstanding_requests = 0;
leaks->leakers = tal_arr(leaks, const char *, 0);
/* hsmd is sync, so do that first. */
if (!wire_sync_write(ld->hsm_fd,
take(towire_hsmd_dev_memleak(NULL))))
fatal("Could not write to HSM: %s", strerror(errno));
msg = wire_sync_read(tmpctx, ld->hsm_fd);
if (!fromwire_hsmd_dev_memleak_reply(msg, &found_leak))
fatal("Bad HSMD_DEV_MEMLEAK_REPLY: %s", tal_hex(tmpctx, msg));
if (found_leak)
report_subd_memleak(leaks, ld->hsm);
/* Now do all the async ones. */
start_leak_request(subd_req(ld->connectd, ld->connectd,
take(towire_connectd_dev_memleak(NULL)),
-1, 0, connect_dev_memleak_done, leaks),
leaks);
start_leak_request(subd_req(ld->gossip, ld->gossip,
take(towire_gossipd_dev_memleak(NULL)),
-1, 0, gossip_dev_memleak_done, leaks),
leaks);
/* Ask all per-peer daemons */
peer_dev_memleak(ld, leaks);
/* Set timer: dualopend doesn't always listen! */
notleak(new_reltimer(ld->timers, leaks, time_from_sec(20),
leak_detect_timeout, leaks));
return command_still_pending(cmd);
}
static const struct json_command dev_memleak_command = {
"dev-memleak",
"developer",
json_memleak,
"Show unreferenced memory objects"
};
AUTODATA(json_command, &dev_memleak_command);
#endif /* DEVELOPER */