From e5c80f63d7f85b69e83cd82a98faa3dc8f26218d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 4 Feb 2019 21:25:42 +1030 Subject: [PATCH] lightningd: add code to search strmaps for memleak detection. Didn't put this in common/memleak because only lightningd currently needs it (as of next patch). Signed-off-by: Rusty Russell --- Makefile | 1 + lightningd/memdump.c | 17 +++++++++++++++++ lightningd/memdump.h | 8 ++++++++ 3 files changed, 26 insertions(+) diff --git a/Makefile b/Makefile index f0cb9eeb3..8b85da7d6 100644 --- a/Makefile +++ b/Makefile @@ -96,6 +96,7 @@ CCAN_OBJS := \ ccan-str-base32.o \ ccan-str-hex.o \ ccan-str.o \ + ccan-strmap.o \ ccan-take.o \ ccan-tal-grab_file.o \ ccan-tal-link.o \ diff --git a/lightningd/memdump.c b/lightningd/memdump.c index 258f84c90..a7901d251 100644 --- a/lightningd/memdump.c +++ b/lightningd/memdump.c @@ -2,6 +2,7 @@ #include "memdump.h" #if DEVELOPER #include +#include #include #include #include @@ -120,6 +121,22 @@ static void json_add_backtrace(struct json_stream *response, json_array_end(response); } +static bool handle_strmap(const char *member, void *p, void *memtable_) +{ + struct htable *memtable = memtable_; + + memleak_scan_region(memtable, p, tal_bytelen(p)); + + /* Keep going */ + return true; +} + +/* FIXME: If strmap used tal, this wouldn't be necessary! */ +void memleak_remove_strmap_(struct htable *memtable, const struct strmap *m) +{ + strmap_iterate_(m, handle_strmap, memtable); +} + static void scan_mem(struct command *cmd, struct json_stream *response, struct lightningd *ld, diff --git a/lightningd/memdump.h b/lightningd/memdump.h index b7837d1b3..552609987 100644 --- a/lightningd/memdump.h +++ b/lightningd/memdump.h @@ -5,8 +5,16 @@ #include struct command; +struct htable; +struct strmap; struct subd; void opening_memleak_done(struct command *cmd, struct subd *leaker); void peer_memleak_done(struct command *cmd, struct subd *leaker); + +/* Remove any pointers inside this strmap (which is opaque to memleak). */ +#define memleak_remove_strmap(memtable, strmap) \ + memleak_remove_strmap_((memtable), tcon_unwrap(strmap)) +void memleak_remove_strmap_(struct htable *memtable, const struct strmap *m); + #endif /* LIGHTNING_LIGHTNINGD_MEMDUMP_H */