Skip to content

Commit

Permalink
print_wire: hand field names to print routines.
Browse files Browse the repository at this point in the history
This lets us override how we print them.

Also, add dependency on header for devtools/Makefile.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Jul 1, 2018
1 parent 2639b1e commit c02ff11
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
2 changes: 1 addition & 1 deletion devtools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions devtools/print_wire.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
#include <inttypes.h>
#include <stdio.h>

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) {
Expand All @@ -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); \
Expand Down
26 changes: 13 additions & 13 deletions devtools/print_wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
#include <bitcoin/tx.h>
#include <wire/gen_peer_wire.h>

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 */
21 changes: 11 additions & 10 deletions tools/generate-wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ def _guess_type(message, fieldname, base_size):
}}
"""

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);
Expand Down Expand Up @@ -437,8 +437,8 @@ def add_truncate_check(self, subcalls, indent='\t'):

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++) {{'
Expand All @@ -456,7 +456,8 @@ def print_printwire_array(self, subcalls, basetype, f, num_elems):

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("]");')

Expand All @@ -479,8 +480,8 @@ def print_printwire(self, is_header):

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)
Expand All @@ -501,8 +502,8 @@ def print_printwire(self, is_header):
.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,
Expand Down Expand Up @@ -686,7 +687,7 @@ def find_message_with_option(messages, optional_messages, name, option):
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]
Expand Down

0 comments on commit c02ff11

Please sign in to comment.