Skip to content

Commit 9e7be80

Browse files
committed
lightningd: add dev option to save hooks and notifications to/from plugins.
Signed-off-by: Rusty Russell <[email protected]>
1 parent 69a8ccc commit 9e7be80

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

lightningd/options.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,10 @@ static void dev_register_opts(struct lightningd *ld)
931931
opt_set_bool,
932932
&ld->dev_hsmd_warn_on_overgrind,
933933
"Warn if we create signatures that are not exactly 71 bytes.");
934+
clnopt_witharg("--dev-save-plugin-io", OPT_DEV,
935+
opt_set_charp, opt_show_charp,
936+
&ld->plugins->dev_save_io,
937+
"Directory to place all plugin notifications/hooks JSON into.");
934938
/* This is handled directly in daemon_developer_mode(), so we ignore it here */
935939
clnopt_noarg("--dev-debug-self", OPT_DEV,
936940
opt_ignore,

lightningd/plugin.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
#include <ccan/crc32c/crc32c.h>
55
#include <ccan/io/io.h>
66
#include <ccan/json_escape/json_escape.h>
7+
#include <ccan/json_out/json_out.h>
78
#include <ccan/mem/mem.h>
89
#include <ccan/opt/opt.h>
910
#include <ccan/pipecmd/pipecmd.h>
11+
#include <ccan/read_write_all/read_write_all.h>
1012
#include <ccan/tal/path/path.h>
1113
#include <ccan/tal/str/str.h>
1214
#include <ccan/utf8/utf8.h>
@@ -23,6 +25,7 @@
2325
#include <db/exec.h>
2426
#include <dirent.h>
2527
#include <errno.h>
28+
#include <fcntl.h>
2629
#include <lightningd/io_loop_with_timers.h>
2730
#include <lightningd/notification.h>
2831
#include <lightningd/plugin.h>
@@ -76,6 +79,7 @@ struct plugins *plugins_new(const tal_t *ctx, struct log_book *log_book,
7679
p->plugin_idx = 0;
7780
p->dev_builtin_plugins_unimportant = false;
7881
p->want_db_transaction = true;
82+
p->dev_save_io = NULL;
7983
p->subscriptions = tal(p, struct plugin_subscription_htable);
8084
plugin_subscription_htable_init(p->subscriptions);
8185

@@ -2534,6 +2538,10 @@ void plugins_notify(struct plugins *plugins,
25342538
if (!plugins)
25352539
return;
25362540

2541+
dev_save_plugin_io_out(plugins,
2542+
"notification_out",
2543+
n->method, n->stream);
2544+
25372545
for (struct plugin_subscription *sub
25382546
= plugin_subscription_htable_getfirst(plugins->subscriptions,
25392547
n->method, &it);
@@ -2673,3 +2681,49 @@ void shutdown_plugins(struct lightningd *ld)
26732681
}
26742682
}
26752683
}
2684+
2685+
static void dev_save_plugin_io(struct plugins *plugins,
2686+
const char *type,
2687+
const char *name,
2688+
const char *buf, size_t len)
2689+
{
2690+
static size_t counter;
2691+
const char *file;
2692+
int fd;
2693+
2694+
if (!plugins->dev_save_io)
2695+
return;
2696+
2697+
file = path_join(tmpctx, plugins->dev_save_io,
2698+
take(tal_fmt(NULL, "%s-%s-%u-%zu",
2699+
type, name,
2700+
(unsigned int)getpid(),
2701+
counter++)));
2702+
fd = open(file, O_CREAT|O_EXCL|O_WRONLY, 0600);
2703+
if (fd < 0 || !write_all(fd, buf, len))
2704+
fatal("Writing --dev-save-plugin-io %s: %s",
2705+
file, strerror(errno));
2706+
close(fd);
2707+
}
2708+
2709+
void dev_save_plugin_io_in(struct plugins *plugins,
2710+
const char *type,
2711+
const char *name,
2712+
const char *buffer,
2713+
const jsmntok_t *tok)
2714+
{
2715+
dev_save_plugin_io(plugins, type, name,
2716+
buffer + tok->start, tok->end - tok->start);
2717+
}
2718+
2719+
void dev_save_plugin_io_out(struct plugins *plugins,
2720+
const char *type,
2721+
const char *name,
2722+
const struct json_stream *stream)
2723+
{
2724+
size_t len;
2725+
const char *buf;
2726+
2727+
buf = json_out_contents(stream->jout, &len);
2728+
dev_save_plugin_io(plugins, type, name, buf, len);
2729+
}

lightningd/plugin.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ struct plugins {
154154

155155
/* Whether builtin plugins should be overridden as unimportant. */
156156
bool dev_builtin_plugins_unimportant;
157+
158+
/* Whether to save all IO to a file */
159+
char *dev_save_io;
157160
};
158161

159162
/**
@@ -415,4 +418,17 @@ struct command_result *plugin_set_dynamic_opt(struct command *cmd,
415418
const struct opt_table *,
416419
const char *,
417420
bool));
421+
422+
/* --dev-plugin-save-io */
423+
void dev_save_plugin_io_in(struct plugins *plugins,
424+
const char *type,
425+
const char *name,
426+
const char *buffer,
427+
const jsmntok_t *tok);
428+
429+
void dev_save_plugin_io_out(struct plugins *plugins,
430+
const char *type,
431+
const char *name,
432+
const struct json_stream *stream);
433+
418434
#endif /* LIGHTNING_LIGHTNINGD_PLUGIN_H */

lightningd/plugin_hook.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ static void plugin_hook_callback(const char *buffer, const jsmntok_t *toks,
169169
ph_req->hook->name, toks->end - toks->start,
170170
buffer + toks->start);
171171

172+
dev_save_plugin_io_in(h->plugin->plugins, "hook_in",
173+
ph_req->hook->name,
174+
buffer, toks);
172175
if (!ph_req->hook->deserialize_cb(ph_req->cb_arg,
173176
buffer, resulttok)) {
174177
tal_free(ph_req->cb_arg);
@@ -209,6 +212,11 @@ static void plugin_hook_call_next(struct plugin_hook_request *ph_req)
209212

210213
hook->serialize_payload(ph_req->cb_arg, req->stream, plugin);
211214
jsonrpc_request_end(req);
215+
216+
dev_save_plugin_io_out(plugin->plugins,
217+
"hook_out", hook->name,
218+
req->stream);
219+
212220
plugin_request_send(plugin, req);
213221
}
214222

0 commit comments

Comments
 (0)