From c02ff11506ebd45b6e346ff81f615a0a421b4322 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 28 Jun 2018 11:00:49 +0930 Subject: [PATCH] print_wire: hand field names to print routines. This lets us override how we print them. Also, add dependency on header for devtools/Makefile. Signed-off-by: Rusty Russell --- devtools/Makefile | 2 +- devtools/print_wire.c | 12 ++++++------ devtools/print_wire.h | 26 +++++++++++++------------- tools/generate-wire.py | 21 +++++++++++---------- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/devtools/Makefile b/devtools/Makefile index ee94335c6..b2b052fff 100644 --- a/devtools/Makefile +++ b/devtools/Makefile @@ -29,7 +29,7 @@ devtools/onion.c: ccan/config.h devtools/onion: $(DEVTOOLS_OBJS) $(DEVTOOLS_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) $(BITCOIN_OBJS) wire/fromwire.o wire/towire.o devtools/onion.o common/sphinx.o $(DEVTOOLS_OBJS) $(DEVTOOLS_TOOL_OBJS): wire/wire.h devtools/gen_print_wire.h -devtools/gen_print_wire.o: devtools/gen_print_wire.h wire/gen_peer_wire.h +devtools/gen_print_wire.o: devtools/gen_print_wire.h wire/gen_peer_wire.h devtools/print_wire.h # Make sure these depend on everything. ALL_PROGRAMS += devtools/bolt11-cli devtools/decodemsg devtools/onion diff --git a/devtools/print_wire.c b/devtools/print_wire.c index 3d51d7ec1..cd862164b 100644 --- a/devtools/print_wire.c +++ b/devtools/print_wire.c @@ -3,27 +3,27 @@ #include #include -void printwire_u8(const u8 *v) +void printwire_u8(const char *fieldname, const u8 *v) { printf("%u\n", *v); } -void printwire_u16(const u16 *v) +void printwire_u16(const char *fieldname, const u16 *v) { printf("%u\n", *v); } -void printwire_u32(const u32 *v) +void printwire_u32(const char *fieldname, const u32 *v) { printf("%u\n", *v); } -void printwire_u64(const u64 *v) +void printwire_u64(const char *fieldname, const u64 *v) { printf("%"PRIu64"\n", *v); } -void printwire_u8_array(const u8 **cursor, size_t *plen, size_t len) +void printwire_u8_array(const char *fieldname, const u8 **cursor, size_t *plen, size_t len) { printf("["); while (len) { @@ -40,7 +40,7 @@ void printwire_u8_array(const u8 **cursor, size_t *plen, size_t len) } #define PRINTWIRE_TYPE_TO_STRING(T, N) \ - void printwire_##N(const T *v) \ + void printwire_##N(const char *fieldname, const T *v) \ { \ const char *s = type_to_string(NULL, T, v); \ printf("%s\n", s); \ diff --git a/devtools/print_wire.h b/devtools/print_wire.h index b51a3a949..eb268a339 100644 --- a/devtools/print_wire.h +++ b/devtools/print_wire.h @@ -4,19 +4,19 @@ #include #include -void printwire_u8(const u8 *v); -void printwire_u16(const u16 *v); -void printwire_u32(const u32 *v); -void printwire_u64(const u64 *v); -void printwire_u8_array(const u8 **cursor, size_t *plen, size_t len); +void printwire_u8(const char *fieldname, const u8 *v); +void printwire_u16(const char *fieldname, const u16 *v); +void printwire_u32(const char *fieldname, const u32 *v); +void printwire_u64(const char *fieldname, const u64 *v); +void printwire_u8_array(const char *fieldname, const u8 **cursor, size_t *plen, size_t len); -void printwire_bitcoin_blkid(const struct bitcoin_blkid *bitcoin_blkid); -void printwire_bitcoin_txid(const struct bitcoin_txid *bitcoin_txid); -void printwire_channel_id(const struct channel_id *channel_id); -void printwire_preimage(const struct preimage *preimage); -void printwire_pubkey(const struct pubkey *pubkey); -void printwire_secp256k1_ecdsa_signature(const secp256k1_ecdsa_signature *); -void printwire_sha256(const struct sha256 *sha256); -void printwire_short_channel_id(const struct short_channel_id *short_channel_id); +void printwire_bitcoin_blkid(const char *fieldname, const struct bitcoin_blkid *bitcoin_blkid); +void printwire_bitcoin_txid(const char *fieldname, const struct bitcoin_txid *bitcoin_txid); +void printwire_channel_id(const char *fieldname, const struct channel_id *channel_id); +void printwire_preimage(const char *fieldname, const struct preimage *preimage); +void printwire_pubkey(const char *fieldname, const struct pubkey *pubkey); +void printwire_secp256k1_ecdsa_signature(const char *fieldname, const secp256k1_ecdsa_signature *); +void printwire_sha256(const char *fieldname, const struct sha256 *sha256); +void printwire_short_channel_id(const char *fieldname, const struct short_channel_id *short_channel_id); #endif /* LIGHTNING_DEVTOOLS_PRINT_WIRE_H */ diff --git a/tools/generate-wire.py b/tools/generate-wire.py index 253bf1e4c..ec6f91b23 100755 --- a/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -220,9 +220,9 @@ towire_impl_templ = """u8 *towire_{name}(const tal_t *ctx{args}) }} """ -printwire_header_templ = """void printwire_{name}(const u8 *cursor); +printwire_header_templ = """void printwire_{name}(const char *fieldname, const u8 *cursor); """ -printwire_impl_templ = """void printwire_{name}(const u8 *cursor) +printwire_impl_templ = """void printwire_{name}(const char *fieldname, const u8 *cursor) {{ \tsize_t plen = tal_len(cursor); @@ -437,8 +437,8 @@ class Message(object): def print_printwire_array(self, subcalls, basetype, f, num_elems): if f.has_array_helper(): - subcalls.append('\tprintwire_{}_array(&cursor, &plen, {});' - .format(basetype, num_elems)) + subcalls.append('\tprintwire_{}_array(tal_fmt(NULL, "%s.{}", fieldname), &cursor, &plen, {});' + .format(basetype, f.name, num_elems)) else: subcalls.append('\tprintf("[");') subcalls.append('\tfor (size_t i = 0; i < {}; i++) {{' @@ -456,7 +456,8 @@ class Message(object): self.add_truncate_check(subcalls, indent='\t\t') - subcalls.append('\t\tprintwire_{}(&v);'.format(basetype)) + subcalls.append('\t\tprintwire_{}(tal_fmt(NULL, "%s.{}", fieldname), &v);' + .format(basetype, f.name)) subcalls.append('\t}') subcalls.append('\tprintf("]");') @@ -479,8 +480,8 @@ class Message(object): subcalls.append('\tprintf("{}=");'.format(f.name)) if f.is_padding(): - subcalls.append('\tprintwire_pad(&cursor, &plen, {});' - .format(f.num_elems)) + subcalls.append('\tprintwire_pad(tal_fmt(NULL, "%s.{}", fieldname), &cursor, &plen, {});' + .format(f.name, f.num_elems)) self.add_truncate_check(subcalls) elif f.is_array(): self.print_printwire_array(subcalls, basetype, f, f.num_elems) @@ -501,8 +502,8 @@ class Message(object): .format(basetype, f.name)) self.add_truncate_check(subcalls) - subcalls.append('\tprintwire_{}(&{});' - .format(basetype, f.name)) + subcalls.append('\tprintwire_{}(tal_fmt(NULL, "%s.{}", fieldname), &{});' + .format(basetype, f.name, f.name)) return template.format( name=self.name, @@ -686,7 +687,7 @@ for m in messages: enums += '\t{} = {},\n'.format(m.enum.name, m.enum.value) includes = '\n'.join(includes) cases = ['case {enum.name}: return "{enum.name}";'.format(enum=m.enum) for m in messages] -printcases = ['case {enum.name}: printf("{enum.name}:\\n"); printwire_{name}(msg); return;'.format(enum=m.enum, name=m.name) for m in messages] +printcases = ['case {enum.name}: printf("{enum.name}:\\n"); printwire_{name}("{name}", msg); return;'.format(enum=m.enum, name=m.name) for m in messages] if options.printwire: decls = [m.print_printwire(options.header) for m in messages + messages_with_option]