From: Eugene Syromyatnikov Date: Tue, 19 Feb 2019 01:50:53 +0000 (+0100) Subject: Generalise UUID printing X-Git-Tag: v5.0~46 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=328ff889b1c0fac23685ddbc7a3d6991b9899fc8;p=strace Generalise UUID printing It was open-coded (using yet another open-coded hexadecimal character printing) in btrfs.c and can be used in other places (like dm.c or rtnl_link.c). * defs.h (print_uuid): New declaration. * print_fields.h (PRINT_FIELD_UUID): New macro. * util.c (print_uuid): New function. * btrfs.c (prnibble, UUID_STRING_SIZE, btrfs_unparse_uuid): Remove. (btrfs_ioctl): Use PRINT_FIELD_UUID for UUID printing. --- diff --git a/btrfs.c b/btrfs.c index f3f9b920..0bf89c2b 100644 --- a/btrfs.c +++ b/btrfs.c @@ -126,35 +126,6 @@ struct btrfs_ioctl_search_args_v2 { # include "xlat/btrfs_space_info_flags.h" # include "xlat/btrfs_tree_objectids.h" -static inline char -prnibble(char v) -{ - if (v >= 10) - return 'a' + (v - 10); - return '0' + v; -} - -/* 8-4-4-4-12 = 36 characters */ -# define UUID_STRING_SIZE 36 - -/* Formats uuid, returns 0 if it's all zeroes */ -static int -btrfs_unparse_uuid(unsigned char *uuid, char *out) -{ - int i; - int ret = 0; - for (i = 0; i < BTRFS_UUID_SIZE; i++) { - if (i == 4 || i == 6 || i == 8 || i == 10) - *out++ = '-'; - *out++ = prnibble(uuid[i] >> 4); - *out++ = prnibble(uuid[i] & 0xf); - if (uuid[i]) - ret = 1; - } - *out = '\0'; - return ret; -} - static void btrfs_print_balance_args(const char *name, const struct btrfs_balance_args *bba) { @@ -563,8 +534,6 @@ MPERS_PRINTER_DECL(int, btrfs_ioctl, case BTRFS_IOC_DEV_INFO: { /* RW */ struct btrfs_ioctl_dev_info_args args; - char uuid[UUID_STRING_SIZE+1]; - int valid; if (entering(tcp)) tprints(", "); @@ -575,18 +544,20 @@ MPERS_PRINTER_DECL(int, btrfs_ioctl, if (umove_or_printaddr(tcp, arg, &args)) break; - valid = btrfs_unparse_uuid(args.uuid, uuid); if (entering(tcp)) { PRINT_FIELD_DEV("{", args, devid); - if (valid) - tprintf(", uuid=%s", uuid); + if (!IS_ARRAY_ZERO(args.uuid)) + PRINT_FIELD_UUID(", ", args, uuid); tprints("}"); return 0; } tprints("{"); - if (valid) - tprintf("uuid=%s, ", uuid); + + if (!IS_ARRAY_ZERO(args.uuid)) { + PRINT_FIELD_UUID("", args, uuid); + tprints(", "); + } PRINT_FIELD_U("", args, bytes_used); PRINT_FIELD_U(", ", args, total_bytes); @@ -716,7 +687,6 @@ MPERS_PRINTER_DECL(int, btrfs_ioctl, case BTRFS_IOC_FS_INFO: { /* R */ struct btrfs_ioctl_fs_info_args args; - char uuid[UUID_STRING_SIZE+1]; uint32_t nodesize, sectorsize, clone_alignment; # ifndef HAVE_STRUCT_BTRFS_IOCTL_FS_INFO_ARGS_NODESIZE uint32_t *reserved32; @@ -739,13 +709,11 @@ MPERS_PRINTER_DECL(int, btrfs_ioctl, sectorsize = reserved32[1]; clone_alignment = reserved32[2]; # endif - btrfs_unparse_uuid(args.fsid, uuid); - PRINT_FIELD_U("{", args, max_id); PRINT_FIELD_U(", ", args, num_devices); - tprintf(", fsid=%s, nodesize=%u, sectorsize=%u" - ", clone_alignment=%u", - uuid, nodesize, sectorsize, clone_alignment); + PRINT_FIELD_UUID(", ", args, fsid); + tprintf(", nodesize=%u, sectorsize=%u, clone_alignment=%u", + nodesize, sectorsize, clone_alignment); tprints("}"); break; } @@ -997,7 +965,6 @@ MPERS_PRINTER_DECL(int, btrfs_ioctl, case BTRFS_IOC_SET_RECEIVED_SUBVOL: { /* RW */ struct_btrfs_ioctl_received_subvol_args args; - char uuid[UUID_STRING_SIZE+1]; if (entering(tcp)) tprints(", "); @@ -1010,8 +977,7 @@ MPERS_PRINTER_DECL(int, btrfs_ioctl, break; if (entering(tcp)) { - btrfs_unparse_uuid((unsigned char *)args.uuid, uuid); - tprintf("{uuid=%s", uuid); + PRINT_FIELD_UUID("{", args, uuid); PRINT_FIELD_U(", ", args, stransid); print_btrfs_timespec(", stime=", args.stime.sec, args.stime.nsec); diff --git a/defs.h b/defs.h index 814b481b..42f09fed 100644 --- a/defs.h +++ b/defs.h @@ -803,6 +803,8 @@ extern const char *sprinttime_usec(long long sec, unsigned long long usec); extern const char *sprint_mac_addr(const uint8_t addr[], size_t size); +extern void print_uuid(const unsigned char *uuid); + extern void print_symbolic_mode_t(unsigned int); extern void print_numeric_umode_t(unsigned short); extern void print_numeric_long_umask(unsigned long); diff --git a/print_fields.h b/print_fields.h index b231be2d..1e261ddc 100644 --- a/print_fields.h +++ b/print_fields.h @@ -111,6 +111,12 @@ # define PRINT_FIELD_UID PRINT_FIELD_ID +# define PRINT_FIELD_UUID(prefix_, where_, field_) \ + do { \ + STRACE_PRINTF("%s%s=", (prefix_), #field_); \ + print_uuid((const unsigned char *) ((where_).field_)); \ + } while (0) + # define PRINT_FIELD_U64(prefix_, where_, field_) \ do { \ STRACE_PRINTF("%s%s=", (prefix_), #field_); \ diff --git a/util.c b/util.c index 239e0f13..9e88b3a2 100644 --- a/util.c +++ b/util.c @@ -359,6 +359,36 @@ sprinttime_nsec(long long sec, unsigned long long nsec) return sprinttime_ex(sec, nsec, 999999999, 9); } +void +print_uuid(const unsigned char *uuid) +{ + const char str[] = { + BYTE_HEX_CHARS(uuid[0]), + BYTE_HEX_CHARS(uuid[1]), + BYTE_HEX_CHARS(uuid[2]), + BYTE_HEX_CHARS(uuid[3]), + '-', + BYTE_HEX_CHARS(uuid[4]), + BYTE_HEX_CHARS(uuid[5]), + '-', + BYTE_HEX_CHARS(uuid[6]), + BYTE_HEX_CHARS(uuid[7]), + '-', + BYTE_HEX_CHARS(uuid[8]), + BYTE_HEX_CHARS(uuid[9]), + '-', + BYTE_HEX_CHARS(uuid[10]), + BYTE_HEX_CHARS(uuid[11]), + BYTE_HEX_CHARS(uuid[12]), + BYTE_HEX_CHARS(uuid[13]), + BYTE_HEX_CHARS(uuid[14]), + BYTE_HEX_CHARS(uuid[15]), + '\0' + }; + + tprints(str); +} + enum sock_proto getfdproto(struct tcb *tcp, int fd) {