]> granicus.if.org Git - strace/blobdiff - util.c
Use printnum_int64 instead of print_loff_t
[strace] / util.c
diff --git a/util.c b/util.c
index 6afafbbf6d74aac60c713e5f910db0797a2c81a8..04cf413a6d578a12b16e05fdae32c46525294d01 100644 (file)
--- a/util.c
+++ b/util.c
@@ -34,7 +34,7 @@
 #include "defs.h"
 #include <sys/param.h>
 #include <fcntl.h>
-#if HAVE_SYS_XATTR_H
+#ifdef HAVE_SYS_XATTR_H
 # include <sys/xattr.h>
 #endif
 #include <sys/uio.h>
@@ -376,41 +376,50 @@ printflags(const struct xlat *xlat, int flags, const char *dflt)
 }
 
 void
-printnum_long(struct tcb *tcp, long addr, const char *fmt)
+printaddr(const long addr)
 {
-       long num;
-
-       if (!addr) {
+       if (!addr)
                tprints("NULL");
-               return;
-       }
-       if (umove(tcp, addr, &num) < 0) {
+       else
                tprintf("%#lx", addr);
-               return;
-       }
-       tprints("[");
-       tprintf(fmt, num);
-       tprints("]");
 }
 
-void
-printnum_int(struct tcb *tcp, long addr, const char *fmt)
-{
-       int num;
+#define DEF_PRINTNUM(name, type) \
+void                                                                   \
+printnum_ ## name(struct tcb *tcp, const long addr, const char *fmt)   \
+{                                                                      \
+       type num;                                                       \
+       if (!umove_or_printaddr(tcp, addr, &num)) {                     \
+               tprints("[");                                           \
+               tprintf(fmt, num);                                      \
+               tprints("]");                                           \
+       }                                                               \
+}
 
-       if (!addr) {
-               tprints("NULL");
-               return;
-       }
-       if (umove(tcp, addr, &num) < 0) {
-               tprintf("%#lx", addr);
-               return;
-       }
-       tprints("[");
-       tprintf(fmt, num);
-       tprints("]");
+#define DEF_PRINTPAIR(name, type) \
+void                                                                   \
+printpair_ ## name(struct tcb *tcp, const long addr, const char *fmt)  \
+{                                                                      \
+       type pair[2];                                                   \
+       if (!umove_or_printaddr(tcp, addr, &pair)) {                    \
+               tprints("[");                                           \
+               tprintf(fmt, pair[0]);                                  \
+               tprints(", ");                                          \
+               tprintf(fmt, pair[1]);                                  \
+               tprints("]");                                           \
+       }                                                               \
 }
 
+DEF_PRINTNUM(long, long)
+DEF_PRINTPAIR(long, long)
+DEF_PRINTNUM(int, int)
+DEF_PRINTPAIR(int, int)
+DEF_PRINTNUM(short, short)
+#if SIZEOF_LONG != 8
+DEF_PRINTNUM(int64, uint64_t)
+DEF_PRINTPAIR(int64, uint64_t)
+#endif
+
 const char *
 sprinttime(time_t t)
 {
@@ -435,7 +444,7 @@ sprinttime(time_t t)
 static char *
 getfdproto(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
 {
-#if HAVE_SYS_XATTR_H
+#ifdef HAVE_SYS_XATTR_H
        ssize_t r;
        char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
 
@@ -763,12 +772,8 @@ printstr(struct tcb *tcp, long addr, long len)
 
                if (outstr_size / 4 != max_strlen)
                        die_out_of_memory();
-               str = malloc(max_strlen + 1);
-               if (!str)
-                       die_out_of_memory();
-               outstr = malloc(outstr_size);
-               if (!outstr)
-                       die_out_of_memory();
+               str = xmalloc(max_strlen + 1);
+               outstr = xmalloc(outstr_size);
        }
 
        size = max_strlen;
@@ -832,7 +837,7 @@ dumpiov(struct tcb *tcp, int len, long addr)
        /* Assuming no sane program has millions of iovs */
        if ((unsigned)len > 1024*1024 /* insane or negative size? */
            || (iov = malloc(size)) == NULL) {
-               fprintf(stderr, "Out of memory\n");
+               error_msg("Out of memory");
                return;
        }
        if (umoven(tcp, addr, size, iov) >= 0) {
@@ -875,7 +880,7 @@ dumpstr(struct tcb *tcp, long addr, int len)
                str = malloc(len + 16);
                if (!str) {
                        strsize = -1;
-                       fprintf(stderr, "Out of memory\n");
+                       error_msg("Out of memory");
                        return;
                }
                strsize = len + 16;
@@ -1089,6 +1094,22 @@ umoven(struct tcb *tcp, long addr, unsigned int len, void *our_addr)
        return 0;
 }
 
+int
+umoven_or_printaddr(struct tcb *tcp, const long addr, const unsigned int len,
+                   void *our_addr)
+{
+       if (!addr) {
+               tprints("NULL");
+               return -1;
+       }
+       if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
+           umoven(tcp, addr, len, our_addr) < 0) {
+               tprintf("%#lx", addr);
+               return -1;
+       }
+       return 0;
+}
+
 /*
  * Like `umove' but make the additional effort of looking
  * for a terminating zero byte.