From: JingPiao Chen Date: Tue, 4 Jul 2017 06:21:18 +0000 (+0800) Subject: netlink: print unrecognized attribute data in hex X-Git-Tag: v4.19~330 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=73698eb8f143260d806da8fd0472b5c739464787;p=strace netlink: print unrecognized attribute data in hex * nlattr.c (decode_nlattr_with_data): Replace printstrn with printstr_ex and set QUOTE_FORCE_HEX flag. * tests/tests.h (print_quoted_hex): New prototype. * tests/print_quoted_string.c (print_quoted_hex): New function. * tests/test_nlattr.h (TEST_NLATTR_OBJECT, TEST_NLATTR_ARRAY): Use it for updated expected output. Change the type of "plen" variable to unsigned int. * tests/nlattr.c (test_nlattr): Update expected output. --- diff --git a/nlattr.c b/nlattr.c index ef3d7622..00f8924e 100644 --- a/nlattr.c +++ b/nlattr.c @@ -86,7 +86,8 @@ decode_nlattr_with_data(struct tcb *const tcp, || !decoders[nla->nla_type](tcp, addr + NLA_HDRLEN, nla_len - NLA_HDRLEN, opaque_data)) - printstrn(tcp, addr + NLA_HDRLEN, len - NLA_HDRLEN); + printstr_ex(tcp, addr + NLA_HDRLEN, + len - NLA_HDRLEN, QUOTE_FORCE_HEX); tprints("}"); } } diff --git a/tests/nlattr.c b/tests/nlattr.c index 7361eced..ae3ad4dc 100644 --- a/tests/nlattr.c +++ b/tests/nlattr.c @@ -133,7 +133,8 @@ test_nlattr(const int fd) ", flags=NLM_F_DUMP, seq=0, pid=0}, {udiag_family=AF_UNIX" ", udiag_type=SOCK_STREAM, udiag_state=TCP_FIN_WAIT1" ", udiag_ino=0, udiag_cookie=[0, 0]}, {{nla_len=%u" - ", nla_type=%#x /* UNIX_DIAG_??? */}, \"1234\"}}" + ", nla_type=%#x /* UNIX_DIAG_??? */}" + ", \"\\x31\\x32\\x33\\x34\"}}" ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", fd, msg_len, nla->nla_len, UNIX_DIAG_SHUTDOWN + 1, msg_len, sprintrc(rc)); diff --git a/tests/print_quoted_string.c b/tests/print_quoted_string.c index ea87d117..9103eed6 100644 --- a/tests/print_quoted_string.c +++ b/tests/print_quoted_string.c @@ -75,3 +75,14 @@ print_quoted_memory(const char *instr, const size_t len) putchar('"'); } + +void +print_quoted_hex(const char *str, const size_t len) +{ + size_t i; + + printf("\""); + for (i = 0; i < len; i++) + printf("\\x%02x", str[i]); + printf("\""); +} diff --git a/tests/test_nlattr.h b/tests/test_nlattr.h index 8cbb2115..24aeb607 100644 --- a/tests/test_nlattr.h +++ b/tests/test_nlattr.h @@ -99,7 +99,8 @@ print_nlattr(const unsigned int nla_len, const char *const nla_type) init_msg_, print_msg_, \ nla_type_, pattern_, obj_, ...) \ do { \ - const int plen = sizeof(obj_) - 1 > DEFAULT_STRLEN \ + const unsigned int plen = \ + sizeof(obj_) - 1 > DEFAULT_STRLEN \ ? DEFAULT_STRLEN : (int) sizeof(obj_) - 1; \ /* len < sizeof(obj_) */ \ TEST_NLATTR_((fd_), (nlh0_), (hdrlen_), \ @@ -107,7 +108,7 @@ print_nlattr(const unsigned int nla_len, const char *const nla_type) (nla_type_), #nla_type_, \ sizeof(obj_) - 1, \ (pattern_), sizeof(obj_) - 1, \ - printf("\"%.*s\"", plen, (pattern_))); \ + print_quoted_hex((pattern_), plen)); \ /* short read of sizeof(obj_) */ \ TEST_NLATTR_((fd_), (nlh0_), (hdrlen_), \ (init_msg_), (print_msg_), \ @@ -129,7 +130,7 @@ print_nlattr(const unsigned int nla_len, const char *const nla_type) init_msg_, print_msg_, \ nla_type_, pattern_, obj_, print_elem_) \ do { \ - const int plen = \ + const unsigned int plen = \ sizeof((obj_)[0]) - 1 > DEFAULT_STRLEN \ ? DEFAULT_STRLEN : (int) sizeof((obj_)[0]) - 1; \ /* len < sizeof((obj_)[0]) */ \ @@ -138,7 +139,7 @@ print_nlattr(const unsigned int nla_len, const char *const nla_type) (nla_type_), #nla_type_, \ sizeof((obj_)[0]) - 1, \ (pattern_), sizeof((obj_)[0]) - 1, \ - printf("\"%.*s\"", plen, (pattern_))); \ + print_quoted_hex((pattern_), plen)); \ /* sizeof((obj_)[0]) < len < sizeof(obj_) */ \ TEST_NLATTR_((fd_), (nlh0_), (hdrlen_), \ (init_msg_), (print_msg_), \ diff --git a/tests/tests.h b/tests/tests.h index 094f3650..bc7dc988 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -137,6 +137,9 @@ void print_quoted_string(const char *); /* Print memory in a quoted form. */ void print_quoted_memory(const char *, size_t); +/* Print memory in a hexquoted form. */ +void print_quoted_hex(const char *, size_t); + /* Print time_t and nanoseconds in symbolic format. */ void print_time_t_nsec(time_t, unsigned long long, int);