From: Eugene Syromyatnikov Date: Sun, 11 Dec 2016 15:50:53 +0000 (+0300) Subject: Print indirect pointers as pointers X-Git-Tag: v4.16~146 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3b4612b3ac908c5618b8fa4b6fd1e700fd11f2a6;p=strace Print indirect pointers as pointers Originally, printnum_long_int was used, but it prints NULL incorrectly. * defs.h (DECL_PRINTNUM_ADDR): New macro. (DECL_PRINTNUM_ADDR(int), DECL_PRINTNUM_ADDR(int64)): New prototypes. [!current_wordsize] (printnum_addr_long_int): New prototype. [!current_wordsize] (printnum_ptr): Use it. [current_wordsize > 4] (printnum_ptr): Use printnum_addr_int64. [current_wordsize == 4] (printnum_ptr) Use printnum_addr_int. * util.c (DEF_PRINTNUM_ADDR): New macro. (DEF_PRINTNUM_ADDR(int, unsigned int), DEF_PRINTNUM_ADDR(int64, uint64_t)): New macro instantiations that provide printnum_addr_int and printnum_addr_int64, accordingly. [!current_wordsize] (printnum_addr_long_int): New function. * xet_robust_list.c (sprintaddr): New function. (main): Use it, update expected output. * tests/ipc_sem.c (main): Update expected output. Co-authored-by: Dmitry V. Levin --- diff --git a/defs.h b/defs.h index 96fe4050..72b4fabb 100644 --- a/defs.h +++ b/defs.h @@ -805,32 +805,40 @@ DECL_PRINTNUM(int); DECL_PRINTNUM(int64); #undef DECL_PRINTNUM +#define DECL_PRINTNUM_ADDR(name) \ +extern bool \ +printnum_addr_ ## name(struct tcb *, kernel_ulong_t addr) +DECL_PRINTNUM_ADDR(int); +DECL_PRINTNUM_ADDR(int64); +#undef DECL_PRINTNUM_ADDR + #ifndef current_wordsize extern bool printnum_long_int(struct tcb *, kernel_ulong_t addr, const char *fmt_long, const char *fmt_int) ATTRIBUTE_FORMAT((printf, 3, 0)) ATTRIBUTE_FORMAT((printf, 4, 0)); +extern bool printnum_addr_long_int(struct tcb *, kernel_ulong_t addr); # define printnum_slong(tcp, addr) \ printnum_long_int((tcp), (addr), "%" PRId64, "%d") # define printnum_ulong(tcp, addr) \ printnum_long_int((tcp), (addr), "%" PRIu64, "%u") # define printnum_ptr(tcp, addr) \ - printnum_long_int((tcp), (addr), "%#" PRIx64, "%#x") + printnum_addr_long_int((tcp), (addr)) #elif current_wordsize > 4 # define printnum_slong(tcp, addr) \ printnum_int64((tcp), (addr), "%" PRId64) # define printnum_ulong(tcp, addr) \ printnum_int64((tcp), (addr), "%" PRIu64) # define printnum_ptr(tcp, addr) \ - printnum_int64((tcp), (addr), "%#" PRIx64) + printnum_addr_int64((tcp), (addr)) #else /* current_wordsize == 4 */ # define printnum_slong(tcp, addr) \ printnum_int((tcp), (addr), "%d") # define printnum_ulong(tcp, addr) \ printnum_int((tcp), (addr), "%u") # define printnum_ptr(tcp, addr) \ - printnum_int((tcp), (addr), "%#x") + printnum_addr_int((tcp), (addr)) #endif #define DECL_PRINTPAIR(name) \ diff --git a/tests/ipc_sem.c b/tests/ipc_sem.c index 5524b868..1891eefc 100644 --- a/tests/ipc_sem.c +++ b/tests/ipc_sem.c @@ -90,9 +90,9 @@ main(void) rc = semctl(bogus_semid, bogus_semnum, bogus_cmd, bogus_arg); #ifdef __GLIBC__ -# define SEMCTL_BOGUS_ARG_FMT "(%#lx|\\[(%#lx|0)\\])" +# define SEMCTL_BOGUS_ARG_FMT "(%#lx|\\[(%#lx|NULL)\\])" #else -# define SEMCTL_BOGUS_ARG_FMT "(%#lx|\\[(%#lx|0)\\]|NULL)" +# define SEMCTL_BOGUS_ARG_FMT "(%#lx|\\[(%#lx|NULL)\\]|NULL)" #endif printf("semctl\\(%d, %d, (IPC_64\\|)?%#x /\\* SEM_\\?\\?\\? \\*/" ", " SEMCTL_BOGUS_ARG_FMT "\\) += %s\n", diff --git a/tests/xet_robust_list.c b/tests/xet_robust_list.c index f8349303..211dec45 100644 --- a/tests/xet_robust_list.c +++ b/tests/xet_robust_list.c @@ -33,6 +33,19 @@ # include # include +static const char * +sprintaddr(void *addr) +{ + static char buf[sizeof(addr) * 2 + sizeof("0x")]; + + if (!addr) + return "NULL"; + else + snprintf(buf, sizeof(buf), "%p", addr); + + return buf; +} + int main(void) { @@ -43,8 +56,8 @@ main(void) if (syscall(__NR_get_robust_list, long_pid, p_head, p_len)) perror_msg_and_skip("get_robust_list"); - printf("get_robust_list(%d, [%#lx], [%lu]) = 0\n", - (int) pid, (unsigned long) *p_head, (unsigned long) *p_len); + printf("get_robust_list(%d, [%s], [%lu]) = 0\n", + (int) pid, sprintaddr(*p_head), (unsigned long) *p_len); void *head = tail_alloc(*p_len); if (syscall(__NR_set_robust_list, head, *p_len)) @@ -54,8 +67,8 @@ main(void) if (syscall(__NR_get_robust_list, long_pid, p_head, p_len)) perror_msg_and_skip("get_robust_list"); - printf("get_robust_list(%d, [%#lx], [%lu]) = 0\n", - (int) pid, (unsigned long) *p_head, (unsigned long) *p_len); + printf("get_robust_list(%d, [%s], [%lu]) = 0\n", + (int) pid, sprintaddr(*p_head), (unsigned long) *p_len); puts("+++ exited with 0 +++"); return 0; diff --git a/util.c b/util.c index 3fbe2dbc..acb04618 100644 --- a/util.c +++ b/util.c @@ -484,6 +484,19 @@ printnum_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \ return true; \ } +#define DEF_PRINTNUM_ADDR(name, type) \ +bool \ +printnum_addr_ ## name(struct tcb *tcp, const kernel_ulong_t addr) \ +{ \ + type num; \ + if (umove_or_printaddr(tcp, addr, &num)) \ + return false; \ + tprints("["); \ + printaddr(num); \ + tprints("]"); \ + return true; \ +} + #define DEF_PRINTPAIR(name, type) \ bool \ printpair_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \ @@ -501,9 +514,11 @@ printpair_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \ } DEF_PRINTNUM(int, int) +DEF_PRINTNUM_ADDR(int, unsigned int) DEF_PRINTPAIR(int, int) DEF_PRINTNUM(short, short) DEF_PRINTNUM(int64, uint64_t) +DEF_PRINTNUM_ADDR(int64, uint64_t) DEF_PRINTPAIR(int64, uint64_t) #ifndef current_wordsize @@ -517,6 +532,16 @@ printnum_long_int(struct tcb *const tcp, const kernel_ulong_t addr, return printnum_int(tcp, addr, fmt_int); } } + +bool +printnum_addr_long_int(struct tcb *tcp, const kernel_ulong_t addr) +{ + if (current_wordsize > sizeof(int)) { + return printnum_addr_int64(tcp, addr); + } else { + return printnum_addr_int(tcp, addr); + } +} #endif /* !current_wordsize */ const char *