]> granicus.if.org Git - strace/blobdiff - syscall.c
Print kernel_ureg_t and kernel_scno_t using dedicated format strings
[strace] / syscall.c
index e5ab44a54659384cb519d1e56eaf4641b5f2a769..d19777e834ba055db99b443e969ab2c76112769e 100644 (file)
--- a/syscall.c
+++ b/syscall.c
@@ -82,6 +82,7 @@
 #define MA MAX_ARGS
 #define SI STACKTRACE_INVALIDATE_CACHE
 #define SE STACKTRACE_CAPTURE_ON_ENTER
+#define CST COMPAT_SYSCALL_TYPES
 
 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
 
@@ -116,6 +117,7 @@ static const struct_sysent sysent2[] = {
 #undef MA
 #undef SI
 #undef SE
+#undef CST
 
 /*
  * `ioctlent[012].h' files are automatically generated by the auxiliary
@@ -224,10 +226,7 @@ unsigned nerrnos = nerrnos0;
 unsigned nsignals = nsignals0;
 unsigned nioctlents = nioctlents0;
 
-unsigned num_quals;
-qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
-
-static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
+const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
        nsyscalls0,
 #if SUPPORTED_PERSONALITIES > 1
        nsyscalls1,
@@ -236,7 +235,7 @@ static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
        nsyscalls2,
 #endif
 };
-static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
+const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
        sysent0,
 #if SUPPORTED_PERSONALITIES > 1
        sysent1,
@@ -246,26 +245,6 @@ static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
 #endif
 };
 
-enum {
-       MAX_NSYSCALLS1 = (nsyscalls0
-#if SUPPORTED_PERSONALITIES > 1
-                       > nsyscalls1 ? nsyscalls0 : nsyscalls1
-#endif
-                       ),
-       MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
-#if SUPPORTED_PERSONALITIES > 2
-                       > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
-#endif
-                       ),
-       MAX_NSYSCALLS = MAX_NSYSCALLS2,
-       /* We are ready for arches with up to 255 signals,
-        * even though the largest known signo is on MIPS and it is 128.
-        * The number of existing syscalls on all arches is
-        * larger that 255 anyway, so it is just a pedantic matter.
-        */
-       MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
-};
-
 #if SUPPORTED_PERSONALITIES > 1
 unsigned current_personality;
 
@@ -280,6 +259,17 @@ static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
 };
 # endif
 
+# ifndef current_klongsize
+unsigned current_klongsize;
+static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
+       PERSONALITY0_KLONGSIZE,
+       PERSONALITY1_KLONGSIZE,
+#  if SUPPORTED_PERSONALITIES > 2
+       PERSONALITY2_KLONGSIZE,
+#  endif
+};
+# endif
+
 void
 set_personality(int personality)
 {
@@ -324,6 +314,9 @@ set_personality(int personality)
 # ifndef current_wordsize
        current_wordsize = personality_wordsize[personality];
 # endif
+# ifndef current_klongsize
+       current_klongsize = personality_klongsize[personality];
+# endif
 }
 
 static void
@@ -359,426 +352,6 @@ update_personality(struct tcb *tcp, unsigned int personality)
 }
 #endif
 
-static int qual_desc(const char *, unsigned int, int);
-static int qual_fault(const char *, unsigned int, int);
-static int qual_signal(const char *, unsigned int, int);
-static int qual_syscall(const char *, unsigned int, int);
-
-static const struct qual_options {
-       unsigned int bitflag;
-       const char *option_name;
-       int (*qualify)(const char *, unsigned int, int);
-       const char *argument_name;
-} qual_options[] = {
-       { QUAL_TRACE,   "trace",        qual_syscall,   "system call"   },
-       { QUAL_TRACE,   "t",            qual_syscall,   "system call"   },
-       { QUAL_ABBREV,  "abbrev",       qual_syscall,   "system call"   },
-       { QUAL_ABBREV,  "a",            qual_syscall,   "system call"   },
-       { QUAL_VERBOSE, "verbose",      qual_syscall,   "system call"   },
-       { QUAL_VERBOSE, "v",            qual_syscall,   "system call"   },
-       { QUAL_RAW,     "raw",          qual_syscall,   "system call"   },
-       { QUAL_RAW,     "x",            qual_syscall,   "system call"   },
-       { QUAL_SIGNAL,  "signal",       qual_signal,    "signal"        },
-       { QUAL_SIGNAL,  "signals",      qual_signal,    "signal"        },
-       { QUAL_SIGNAL,  "s",            qual_signal,    "signal"        },
-       { QUAL_READ,    "read",         qual_desc,      "descriptor"    },
-       { QUAL_READ,    "reads",        qual_desc,      "descriptor"    },
-       { QUAL_READ,    "r",            qual_desc,      "descriptor"    },
-       { QUAL_WRITE,   "write",        qual_desc,      "descriptor"    },
-       { QUAL_WRITE,   "writes",       qual_desc,      "descriptor"    },
-       { QUAL_WRITE,   "w",            qual_desc,      "descriptor"    },
-       { QUAL_FAULT,   "fault",        qual_fault,     "fault argument"},
-       { 0,            NULL,           NULL,           NULL            },
-};
-
-static void
-reallocate_vec(void **vec, unsigned int old_nmemb,
-              size_t size, unsigned int new_nmemb)
-{
-       unsigned int p;
-
-       for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
-               vec[p] = xreallocarray(vec[p], new_nmemb, size);
-               memset(vec[p] + size * old_nmemb, 0,
-                      (new_nmemb - old_nmemb) * size);
-       }
-}
-
-static void
-reallocate_qual(const unsigned int n)
-{
-       reallocate_vec((void **) qual_vec, num_quals, sizeof(qualbits_t), n);
-       num_quals = n;
-}
-
-struct fault_opts {
-       uint16_t first;
-       uint16_t step;
-       uint16_t err;
-};
-
-static unsigned int num_faults;
-static struct fault_opts *fault_vec[SUPPORTED_PERSONALITIES];
-
-static inline void
-reallocate_fault(const unsigned int n)
-{
-       reallocate_vec((void **) fault_vec, num_faults,
-                      sizeof(struct fault_opts), n);
-       num_faults = n;
-}
-
-static void
-qualify_one(const unsigned int n, unsigned int bitflag, const int not,
-           const int pers, const struct fault_opts *fopts)
-{
-       int p;
-
-       if (num_quals <= n) {
-               reallocate_qual(n + 1);
-               reallocate_fault(n + 1);
-       }
-
-       for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
-               if (pers == p || pers < 0) {
-                       if (not)
-                               qual_vec[p][n] &= ~bitflag;
-                       else {
-                               qual_vec[p][n] |= bitflag;
-                               if (fopts)
-                                       memcpy(&fault_vec[p][n], fopts,
-                                              sizeof(*fopts));
-                       }
-               }
-       }
-}
-
-static bool
-qualify_scno(const char *const s, const unsigned int bitflag,
-            const int not, const struct fault_opts *const fopts)
-{
-       int i = string_to_uint_upto(s, MAX_NSYSCALLS - 1);
-       if (i < 0)
-               return false;
-
-       qualify_one(i, bitflag, not, -1, fopts);
-       return true;
-}
-
-static int
-lookup_class(const char *s)
-{
-       if (strcmp(s, "all") == 0)
-               return 0;
-       if (strcmp(s, "file") == 0)
-               return TRACE_FILE;
-       if (strcmp(s, "ipc") == 0)
-               return TRACE_IPC;
-       if (strcmp(s, "network") == 0)
-               return TRACE_NETWORK;
-       if (strcmp(s, "process") == 0)
-               return TRACE_PROCESS;
-       if (strcmp(s, "signal") == 0)
-               return TRACE_SIGNAL;
-       if (strcmp(s, "desc") == 0)
-               return TRACE_DESC;
-       if (strcmp(s, "memory") == 0)
-               return TRACE_MEMORY;
-       return -1;
-}
-
-static bool
-qualify_syscall_class(const char *const s, const unsigned int bitflag,
-                     const int not, const struct fault_opts *const fopts)
-{
-       unsigned int p;
-       const int n = lookup_class(s);
-
-       if (n < 0)
-               return false;
-
-       for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
-               unsigned int i;
-
-               for (i = 0; i < nsyscall_vec[p]; ++i) {
-                       if (sysent_vec[p][i].sys_name
-                           && (sysent_vec[p][i].sys_flags & n) == n) {
-                               qualify_one(i, bitflag, not, p, fopts);
-                       }
-               }
-       }
-
-       return true;
-}
-
-static bool
-qualify_syscall_name(const char *const s, const unsigned int bitflag,
-                    const int not, const struct fault_opts *const fopts)
-{
-       bool found = false;
-       unsigned int p;
-
-       for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
-               unsigned int i;
-
-               for (i = 0; i < nsyscall_vec[p]; ++i) {
-                       if (sysent_vec[p][i].sys_name
-                           && strcmp(s, sysent_vec[p][i].sys_name) == 0) {
-                               qualify_one(i, bitflag, not, p, fopts);
-                               found = true;
-                       }
-               }
-       }
-
-       return found;
-}
-
-static int
-qual_syscall_ex(const char *const s, const unsigned int bitflag,
-               const int not, const struct fault_opts *const fopts)
-{
-       if (qualify_scno(s, bitflag, not, fopts)
-           || qualify_syscall_class(s, bitflag, not, fopts)
-           || qualify_syscall_name(s, bitflag, not, fopts)) {
-               return 0;
-       }
-
-       return -1;
-}
-
-static int
-qual_syscall(const char *const s, const unsigned int bitflag, const int not)
-{
-       return qual_syscall_ex(s, bitflag, not, NULL);
-}
-
-/*
- * Returns NULL if STR does not start with PREFIX,
- * or a pointer to the first char in STR after PREFIX.
- */
-static const char *
-strip_prefix(const char *prefix, const char *str)
-{
-       size_t len = strlen(prefix);
-
-       return (len > strlen(str) || memcmp(prefix, str, len))
-              ? NULL : str + len;
-}
-
-static int
-find_errno_by_name(const char *name)
-{
-       unsigned int i;
-
-       for (i = 1; i < nerrnos; ++i) {
-               if (errnoent[i] && (strcmp(name, errnoent[i]) == 0))
-                       return i;
-       }
-
-       return -1;
-}
-
-static bool
-parse_fault_token(const char *const token, struct fault_opts *const fopts)
-{
-       const char *val;
-       int intval;
-
-       if ((val = strip_prefix("when=", token))) {
-               /*
-                *      == 1+1
-                * F    == F+0
-                * F+   == F+1
-                * F+S
-                */
-               char *end;
-               intval = string_to_uint_ex(val, &end, 0xffff, "+");
-               if (intval < 1)
-                       return false;
-
-               fopts->first = intval;
-
-               if (*end) {
-                       val = end + 1;
-                       if (*val) {
-                               /* F+S */
-                               intval = string_to_uint_upto(val, 0xffff);
-                               if (intval < 1)
-                                       return false;
-                               fopts->step = intval;
-                       } else {
-                               /* F+ == F+1 */
-                               fopts->step = 1;
-                       }
-               } else {
-                       /* F == F+0 */
-                       fopts->step = 0;
-               }
-       } else if ((val = strip_prefix("error=", token))) {
-               intval = string_to_uint_upto(val, 4095);
-               if (intval < 0)
-                       intval = find_errno_by_name(val);
-               if (intval < 1)
-                       return false;
-               fopts->err = intval;
-       } else {
-               return false;
-       }
-
-       return true;
-}
-
-static char *
-parse_fault_expression(const char *const s, char **buf,
-                      struct fault_opts *const fopts)
-{
-       char *saveptr = NULL;
-       char *name = NULL;
-       char *token;
-
-       *buf = xstrdup(s);
-       for (token = strtok_r(*buf, ":", &saveptr); token;
-            token = strtok_r(NULL, ":", &saveptr)) {
-               if (!name)
-                       name = token;
-               else if (!parse_fault_token(token, fopts))
-                       goto parse_error;
-       }
-
-       if (name)
-               return name;
-
-parse_error:
-       free(*buf);
-       return *buf = NULL;
-}
-
-static int
-qual_fault(const char *const s, const unsigned int bitflag, const int not)
-{
-       struct fault_opts opts = {
-               .first = 1,
-               .step = 1,
-               .err = 0
-       };
-
-       char *buf = NULL;
-       char *name = parse_fault_expression(s, &buf, &opts);
-       char *saveptr = NULL;
-       const char *token;
-       int rc = -1;
-
-       if (!name)
-               return -1;
-
-       for (token = strtok_r(name, ",", &saveptr); token;
-            token = strtok_r(NULL, ",", &saveptr)) {
-               rc = qual_syscall_ex(token, bitflag, not, &opts);
-               if (rc)
-                       break;
-       }
-
-       free(buf);
-       return rc;
-}
-
-static int
-qual_signal(const char *s, const unsigned int bitflag, const int not)
-{
-       int i;
-
-       if (*s >= '0' && *s <= '9') {
-               i = string_to_uint_upto(s, 255);
-               if (i < 0)
-                       return -1;
-               qualify_one(i, bitflag, not, -1, NULL);
-               return 0;
-       }
-       if (strncasecmp(s, "SIG", 3) == 0)
-               s += 3;
-       for (i = 0; i <= NSIG; ++i) {
-               const char *name = signame(i);
-               if (strncasecmp(name, "SIG", 3) != 0)
-                       continue;
-               name += 3;
-
-               if (strcasecmp(name, s) != 0)
-                       continue;
-               qualify_one(i, bitflag, not, -1, NULL);
-               return 0;
-       }
-       return -1;
-}
-
-static int
-qual_desc(const char *s, const unsigned int bitflag, const int not)
-{
-       int desc = string_to_uint_upto(s, 0x7fff);
-       if (desc < 0)
-               return -1;
-       qualify_one(desc, bitflag, not, -1, NULL);
-       return 0;
-}
-
-void
-qualify(const char *s)
-{
-       const struct qual_options *opt;
-       char *copy;
-       const char *p;
-       int not;
-       unsigned int i;
-
-       if (num_quals == 0) {
-               reallocate_qual(MIN_QUALS);
-               reallocate_fault(MIN_QUALS);
-       }
-
-       opt = &qual_options[0];
-       for (i = 0; (p = qual_options[i].option_name); i++) {
-               unsigned int len = strlen(p);
-               if (strncmp(s, p, len) == 0 && s[len] == '=') {
-                       opt = &qual_options[i];
-                       s += len + 1;
-                       break;
-               }
-       }
-       not = 0;
-       if (*s == '!') {
-               not = 1;
-               s++;
-       }
-       if (strcmp(s, "none") == 0) {
-               not = 1 - not;
-               s = "all";
-       }
-       if (opt->bitflag == QUAL_FAULT) {
-               if (opt->qualify(s, opt->bitflag, not)) {
-                       error_msg_and_die("invalid %s '%s'",
-                               opt->argument_name, s);
-               }
-               return;
-       }
-       if (strcmp(s, "all") == 0) {
-               for (i = 0; i < num_quals; ++i) {
-                       qualify_one(i, opt->bitflag, not, -1, NULL);
-               }
-               return;
-       }
-       for (i = 0; i < num_quals; ++i) {
-               qualify_one(i, opt->bitflag, !not, -1, NULL);
-       }
-       copy = xstrdup(s);
-       for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
-               if (opt->qualify(p, opt->bitflag, not)) {
-                       error_msg_and_die("invalid %s '%s'",
-                               opt->argument_name, p);
-               }
-       }
-       free(copy);
-       return;
-}
-
 #ifdef SYS_socket_subcall
 static void
 decode_socket_subcall(struct tcb *tcp)
@@ -788,7 +361,7 @@ decode_socket_subcall(struct tcb *tcp)
        if (call < 1 || call >= SYS_socket_nsubcalls)
                return;
 
-       const unsigned long scno = SYS_socket_subcall + call;
+       const kernel_scno_t scno = SYS_socket_subcall + call;
        const unsigned int nargs = sysent[scno].nargs;
        uint64_t buf[nargs];
 
@@ -796,7 +369,7 @@ decode_socket_subcall(struct tcb *tcp)
                return;
 
        tcp->scno = scno;
-       tcp->qual_flg = qual_flags[scno];
+       tcp->qual_flg = qual_flags(scno);
        tcp->s_ent = &sysent[scno];
 
        unsigned int i;
@@ -836,7 +409,7 @@ decode_ipc_subcall(struct tcb *tcp)
        }
 
        tcp->scno = SYS_ipc_subcall + call;
-       tcp->qual_flg = qual_flags[tcp->scno];
+       tcp->qual_flg = qual_flags(tcp->scno);
        tcp->s_ent = &sysent[tcp->scno];
 
        const unsigned int n = tcp->s_ent->nargs;
@@ -850,10 +423,10 @@ decode_ipc_subcall(struct tcb *tcp)
 static void
 decode_mips_subcall(struct tcb *tcp)
 {
-       if (!SCNO_IS_VALID(tcp->u_arg[0]))
+       if (!scno_is_valid(tcp->u_arg[0]))
                return;
        tcp->scno = tcp->u_arg[0];
-       tcp->qual_flg = qual_flags[tcp->scno];
+       tcp->qual_flg = qual_flags(tcp->scno);
        tcp->s_ent = &sysent[tcp->scno];
        memmove(&tcp->u_arg[0], &tcp->u_arg[1],
                sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
@@ -875,17 +448,15 @@ decode_mips_subcall(struct tcb *tcp)
 static void
 dumpio(struct tcb *tcp)
 {
-       int sen;
-
        if (syserror(tcp))
                return;
-       if ((unsigned long) tcp->u_arg[0] >= num_quals)
-               return;
-       sen = tcp->s_ent->sen;
-       if (SEN_printargs == sen)
+
+       int fd = tcp->u_arg[0];
+       if (fd < 0)
                return;
-       if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
-               switch (sen) {
+
+       if (is_number_in_set(fd, &read_set)) {
+               switch (tcp->s_ent->sen) {
                case SEN_read:
                case SEN_pread:
                case SEN_recv:
@@ -907,8 +478,8 @@ dumpio(struct tcb *tcp)
                        return;
                }
        }
-       if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
-               switch (sen) {
+       if (is_number_in_set(fd, &write_set)) {
+               switch (tcp->s_ent->sen) {
                case SEN_write:
                case SEN_pwrite:
                case SEN_send:
@@ -920,11 +491,10 @@ dumpio(struct tcb *tcp)
                case SEN_pwritev:
                case SEN_pwritev2:
                case SEN_vmsplice:
-                       dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
+                       dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
                        break;
                case SEN_sendmsg:
-                       dumpiov_in_msghdr(tcp, tcp->u_arg[1],
-                                         (unsigned long) -1L);
+                       dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
                        break;
                case SEN_sendmmsg:
                        dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
@@ -937,10 +507,10 @@ dumpio(struct tcb *tcp)
  * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
  * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
  */
-#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
-static long
-shuffle_scno(unsigned long scno)
+static kernel_scno_t
+shuffle_scno(kernel_scno_t scno)
 {
+#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
        if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
                return scno;
 
@@ -950,7 +520,7 @@ shuffle_scno(unsigned long scno)
        if (scno == 0x000ffff0)
                return ARM_FIRST_SHUFFLED_SYSCALL;
 
-#define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
+# define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
        /*
         * Is it ARM specific syscall?
         * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
@@ -963,25 +533,10 @@ shuffle_scno(unsigned long scno)
        if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
                return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
        }
+#endif /* ARM || AARCH64 */
 
        return scno;
 }
-#else
-# define shuffle_scno(scno) ((long)(scno))
-#endif
-
-const char *
-syscall_name(long scno)
-{
-       static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
-
-       if (SCNO_IS_VALID(scno))
-               return sysent[scno].sys_name;
-       else {
-               sprintf(buf, "syscall_%lu", scno);
-               return buf;
-       }
-}
 
 const char *
 err_name(unsigned long err)
@@ -1003,14 +558,16 @@ clear_regs(void)
 static int get_syscall_args(struct tcb *);
 static int get_syscall_result(struct tcb *);
 static int arch_get_scno(struct tcb *tcp);
-static int arch_set_scno(struct tcb *, long);
+static int arch_set_scno(struct tcb *, kernel_scno_t);
 static void get_error(struct tcb *, const bool);
 static int arch_set_error(struct tcb *);
 
+struct fault_opts *fault_vec[SUPPORTED_PERSONALITIES];
+
 static struct fault_opts *
 tcb_fault_opts(struct tcb *tcp)
 {
-       return (SCNO_IN_RANGE(tcp->scno) && tcp->fault_vec[current_personality])
+       return (scno_in_range(tcp->scno) && tcp->fault_vec[current_personality])
               ? &tcp->fault_vec[current_personality][tcp->scno] : NULL;
 }
 
@@ -1020,16 +577,15 @@ inject_syscall_fault_entering(struct tcb *tcp)
 {
        if (!tcp->fault_vec[current_personality]) {
                tcp->fault_vec[current_personality] =
-                       xreallocarray(NULL, num_faults,
-                                     sizeof(struct fault_opts));
+                       xcalloc(nsyscalls, sizeof(**fault_vec));
                memcpy(tcp->fault_vec[current_personality],
                       fault_vec[current_personality],
-                      num_faults * sizeof(struct fault_opts));
+                      nsyscalls * sizeof(**fault_vec));
        }
 
        struct fault_opts *opts = tcb_fault_opts(tcp);
 
-       if (opts->first == 0)
+       if (!opts || opts->first == 0)
                return 0;
 
        --opts->first;
@@ -1148,7 +704,7 @@ trace_syscall_entering(struct tcb *tcp)
 
        printleader(tcp);
        tprintf("%s(", tcp->s_ent->sys_name);
-       if ((tcp->qual_flg & QUAL_RAW) && SEN_exit != tcp->s_ent->sen)
+       if (tcp->qual_flg & QUAL_RAW)
                res = printargs(tcp);
        else
                res = tcp->s_ent->sys_func(tcp);
@@ -1266,7 +822,7 @@ trace_syscall_exiting(struct tcb *tcp)
                        if (syscall_fault_injected(tcp))
                                tprints(" (INJECTED)");
                } else {
-                       tprintf("= %#lx", tcp->u_rval);
+                       tprintf("= %#" PRI_krx, tcp->u_rval);
                }
        }
        else if (!(sys_res & RVAL_NONE) && u_error) {
@@ -1346,12 +902,12 @@ trace_syscall_exiting(struct tcb *tcp)
                        switch (sys_res & RVAL_MASK) {
                        case RVAL_HEX:
 #if SUPPORTED_PERSONALITIES > 1
-                               if (current_wordsize < sizeof(long))
+                               if (current_wordsize < sizeof(tcp->u_rval))
                                        tprintf("= %#x",
                                                (unsigned int) tcp->u_rval);
                                else
 #endif
-                                       tprintf("= %#lx", tcp->u_rval);
+                                       tprintf("= %#" PRI_krx, tcp->u_rval);
                                break;
                        case RVAL_OCTAL:
                                tprints("= ");
@@ -1359,15 +915,15 @@ trace_syscall_exiting(struct tcb *tcp)
                                break;
                        case RVAL_UDECIMAL:
 #if SUPPORTED_PERSONALITIES > 1
-                               if (current_wordsize < sizeof(long))
+                               if (current_wordsize < sizeof(tcp->u_rval))
                                        tprintf("= %u",
                                                (unsigned int) tcp->u_rval);
                                else
 #endif
-                                       tprintf("= %lu", tcp->u_rval);
+                                       tprintf("= %" PRI_kru, tcp->u_rval);
                                break;
                        case RVAL_DECIMAL:
-                               tprintf("= %ld", tcp->u_rval);
+                               tprintf("= %" PRI_krd, tcp->u_rval);
                                break;
                        case RVAL_FD:
                                if (show_fd_path) {
@@ -1375,7 +931,7 @@ trace_syscall_exiting(struct tcb *tcp)
                                        printfd(tcp, tcp->u_rval);
                                }
                                else
-                                       tprintf("= %ld", tcp->u_rval);
+                                       tprintf("= %" PRI_krd, tcp->u_rval);
                                break;
 #if HAVE_STRUCT_TCB_EXT_ARG
                        /*
@@ -1460,8 +1016,6 @@ restore_cleared_syserror(struct tcb *tcp)
        tcp->u_error = saved_u_error;
 }
 
-#include "kernel_types.h"
-
 /*
  * Check the syscall return value register value for whether it is
  * a negated errno code indicating an error, or a success return value.
@@ -1472,22 +1026,12 @@ is_negated_errno(kernel_ulong_t val)
        /* Linux kernel defines MAX_ERRNO to 4095. */
        kernel_ulong_t max = -(kernel_long_t) 4095;
 
-#if defined X86_64 || defined X32
-       /*
-        * current_wordsize is 4 for x32 personality
-        * but truncation _must not_ be done in it, so
-        * check current_personality instead.
-        */
-       if (current_personality == 1) {
-               val = (uint32_t) val;
-               max = (uint32_t) max;
-       }
-#elif SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
-       if (current_wordsize < sizeof(val)) {
+#ifndef current_klongsize
+       if (current_klongsize < sizeof(val)) {
                val = (uint32_t) val;
                max = (uint32_t) max;
        }
-#endif
+#endif /* !current_klongsize */
 
        return val >= max;
 }
@@ -1504,7 +1048,7 @@ print_pc(struct tcb *tcp)
 #if defined ARCH_PC_REG
 # define ARCH_GET_PC 0
 #elif defined ARCH_PC_PEEK_ADDR
-       long pc;
+       kernel_ureg_t pc;
 # define ARCH_PC_REG pc
 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
 #else
@@ -1514,8 +1058,9 @@ print_pc(struct tcb *tcp)
                tprints(current_wordsize == 4 ? "[????????] "
                                              : "[????????????????] ");
        else
-               tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
-                       (unsigned long) ARCH_PC_REG);
+               tprintf(current_wordsize == 4
+                       ? "[%08" PRI_krx "] " : "[%016" PRI_krx "] ",
+                       (kernel_ureg_t) ARCH_PC_REG);
 }
 
 #include "getregs_old.h"
@@ -1645,7 +1190,7 @@ set_regs(pid_t pid)
 struct sysent_buf {
        struct tcb *tcp;
        struct_sysent ent;
-       char buf[sizeof("syscall_%lu") + sizeof(long) * 3];
+       char buf[sizeof("syscall_%lu") + sizeof(kernel_scno_t) * 3];
 };
 
 static void
@@ -1673,9 +1218,9 @@ get_scno(struct tcb *tcp)
        if (rc != 1)
                return rc;
 
-       if (SCNO_IS_VALID(tcp->scno)) {
+       if (scno_is_valid(tcp->scno)) {
                tcp->s_ent = &sysent[tcp->scno];
-               tcp->qual_flg = qual_flags[tcp->scno];
+               tcp->qual_flg = qual_flags(tcp->scno);
        } else {
                struct sysent_buf *s = xcalloc(1, sizeof(*s));
 
@@ -1684,7 +1229,7 @@ get_scno(struct tcb *tcp)
                s->ent.sen = SEN_printargs;
                s->ent.sys_func = printargs;
                s->ent.sys_name = s->buf;
-               sprintf(s->buf, "syscall_%lu", shuffle_scno(tcp->scno));
+               sprintf(s->buf, "syscall_%" PRI_kru, shuffle_scno(tcp->scno));
 
                tcp->s_ent = &s->ent;
                tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
@@ -1692,7 +1237,8 @@ get_scno(struct tcb *tcp)
                set_tcb_priv_data(tcp, s, free_sysent_buf);
 
                if (debug_flag)
-                       error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
+                       error_msg("pid %d invalid syscall %" PRI_krd,
+                                 tcp->pid, tcp->scno);
        }
        return 1;
 }
@@ -1730,3 +1276,13 @@ get_syscall_result(struct tcb *tcp)
 #ifdef HAVE_GETREGS_OLD
 # include "getregs_old.c"
 #endif
+
+const char *
+syscall_name(kernel_scno_t scno)
+{
+#if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
+       if (current_personality == X32_PERSONALITY_NUMBER)
+               scno &= ~__X32_SYSCALL_BIT;
+#endif
+       return scno_is_valid(scno) ? sysent[scno].sys_name: NULL;
+}