Index: usr.bin/truss/setup.c =================================================================== --- usr.bin/truss/setup.c (revision 240005) +++ usr.bin/truss/setup.c (working copy) @@ -158,12 +158,10 @@ find_thread(struct trussinfo *info, lwpid_t lwpid) } } - np = (struct threadinfo *)malloc(sizeof(struct threadinfo)); + np = (struct threadinfo *)calloc(1, sizeof(struct threadinfo)); if (np == NULL) - errx(1, "malloc() failed"); + err(1, "calloc() failed"); np->tid = lwpid; - np->in_fork = 0; - np->in_syscall = 0; SLIST_INSERT_HEAD(&info->threadlist, np, entries); info->curthread = np; } Index: usr.bin/truss/amd64-linux32.c =================================================================== --- usr.bin/truss/amd64-linux32.c (revision 240005) +++ usr.bin/truss/amd64-linux32.c (working copy) @@ -59,8 +59,6 @@ static const char rcsid[] = #include "syscall.h" #include "extern.h" -static int cpid = -1; - #include "linux32_syscalls.h" static int nsyscalls = @@ -75,28 +73,34 @@ static int nsyscalls = * 'struct syscall' describes the system call; it may be NULL, however, * if we don't know about this particular system call yet. */ -static struct linux_syscall { +struct linux_syscall { struct syscall *sc; const char *name; int number; unsigned long args[5]; int nargs; /* number of arguments -- *not* number of words! */ char **s_args; /* the printable arguments */ -} fsc; +}; +static struct linux_syscall * +alloc_fsc(void) +{ + + return (malloc(sizeof(struct linux_syscall))); +} + /* Clear up and free parts of the fsc structure. */ -static __inline void -clear_fsc(void) +static void +free_fsc(struct linux_syscall *fsc) { int i; - if (fsc.s_args) { - for (i = 0; i < fsc.nargs; i++) - if (fsc.s_args[i]) - free(fsc.s_args[i]); - free(fsc.s_args); + if (fsc->s_args) { + for (i = 0; i < fsc->nargs; i++) + if (fsc->s_args[i]) + free(fsc->s_args[i]); + free(fsc->s_args); } - memset(&fsc, 0, sizeof(fsc)); } /* @@ -110,31 +114,36 @@ void amd64_linux32_syscall_entry(struct trussinfo *trussinfo, int nargs) { struct reg regs; + struct linux_syscall *fsc; struct syscall *sc; + lwpid_t tid; int i, syscall_num; - clear_fsc(); + tid = trussinfo->curthread->tid; - cpid = trussinfo->curthread->tid; - - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return; } syscall_num = regs.r_rax; - fsc.number = syscall_num; - fsc.name = (syscall_num < 0 || syscall_num >= nsyscalls) ? + fsc = alloc_fsc(); + if (fsc == NULL) { + fprintf(trussinfo->outfile, "-- CANNOT ALLOCATE MEMORY --\n"); + return; + } + fsc->number = syscall_num; + fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ? NULL : linux32_syscallnames[syscall_num]; - if (!fsc.name) { + if (!fsc->name) { fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num); } - if (fsc.name && (trussinfo->flags & FOLLOWFORKS) && - (strcmp(fsc.name, "linux_fork") == 0|| - strcmp(fsc.name, "linux_vfork") == 0)) + if (fsc->name && (trussinfo->flags & FOLLOWFORKS) && + (strcmp(fsc->name, "linux_fork") == 0 || + strcmp(fsc->name, "linux_vfork") == 0)) trussinfo->curthread->in_fork = 1; if (nargs == 0) @@ -148,25 +157,25 @@ amd64_linux32_syscall_entry(struct trussinfo *trus * that have more than five arguments? */ - fsc.args[0] = regs.r_rbx; - fsc.args[1] = regs.r_rcx; - fsc.args[2] = regs.r_rdx; - fsc.args[3] = regs.r_rsi; - fsc.args[4] = regs.r_rdi; + fsc->args[0] = regs.r_rbx; + fsc->args[1] = regs.r_rcx; + fsc->args[2] = regs.r_rdx; + fsc->args[3] = regs.r_rsi; + fsc->args[4] = regs.r_rdi; - sc = get_syscall(fsc.name); + sc = get_syscall(fsc->name); if (sc) - fsc.nargs = sc->nargs; + fsc->nargs = sc->nargs; else { #if DEBUG fprintf(trussinfo->outfile, "unknown syscall %s -- setting " - "args to %d\n", fsc.name, nargs); + "args to %d\n", fsc->name, nargs); #endif - fsc.nargs = nargs; + fsc->nargs = nargs; } - fsc.s_args = calloc(1, (1 + fsc.nargs) * sizeof(char *)); - fsc.sc = sc; + fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *)); + fsc->sc = sc; /* * At this point, we set up the system call arguments. @@ -176,19 +185,19 @@ amd64_linux32_syscall_entry(struct trussinfo *trus * passed in *and* out, however. */ - if (fsc.name) { + if (fsc->name) { #if DEBUG - fprintf(stderr, "syscall %s(", fsc.name); + fprintf(stderr, "syscall %s(", fsc->name); #endif - for (i = 0; i < fsc.nargs; i++) { + for (i = 0; i < fsc->nargs; i++) { #if DEBUG fprintf(stderr, "0x%x%s", sc ? - fsc.args[sc->args[i].offset] : fsc.args[i], - i < (fsc.nargs - 1) ? "," : ""); + fsc->args[sc->args[i].offset] : fsc->args[i], + i < (fsc->nargs - 1) ? "," : ""); #endif if (sc && !(sc->args[i].type & OUT)) { - fsc.s_args[i] = print_arg(&sc->args[i], - fsc.args, 0, trussinfo); + fsc->s_args[i] = print_arg(&sc->args[i], + fsc->args, 0, trussinfo); } } #if DEBUG @@ -200,30 +209,29 @@ amd64_linux32_syscall_entry(struct trussinfo *trus fprintf(trussinfo->outfile, "\n"); #endif - if (fsc.name != NULL && (strcmp(fsc.name, "linux_execve") == 0 || - strcmp(fsc.name, "exit") == 0)) { + if (fsc->name != NULL && (strcmp(fsc->name, "linux_execve") == 0 || + strcmp(fsc->name, "exit") == 0)) { /* * XXX * This could be done in a more general * manner but it still wouldn't be very pretty. */ - if (strcmp(fsc.name, "linux_execve") == 0) { + if (strcmp(fsc->name, "linux_execve") == 0) { if ((trussinfo->flags & EXECVEARGS) == 0) { - if (fsc.s_args[1]) { - free(fsc.s_args[1]); - fsc.s_args[1] = NULL; + if (fsc->s_args[1]) { + free(fsc->s_args[1]); + fsc->s_args[1] = NULL; } } if ((trussinfo->flags & EXECVEENVS) == 0) { - if (fsc.s_args[2]) { - free(fsc.s_args[2]); - fsc.s_args[2] = NULL; + if (fsc->s_args[2]) { + free(fsc->s_args[2]); + fsc->s_args[2] = NULL; } } } } - - return; + trussinfo->curthread->fsc = fsc; } /* @@ -246,16 +254,18 @@ amd64_linux32_syscall_exit(struct trussinfo *truss int syscall_num __unused) { struct reg regs; + struct linux_syscall *fsc; struct syscall *sc; + lwpid_t tid; long retval; int errorp, i; - if (fsc.name == NULL) + if (trussinfo->curthread->fsc == NULL) return (-1); - cpid = trussinfo->curthread->tid; + tid = trussinfo->curthread->tid; - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return (-1); } @@ -268,10 +278,11 @@ amd64_linux32_syscall_exit(struct trussinfo *truss * stand some significant cleaning. */ - sc = fsc.sc; + fsc = trussinfo->curthread->fsc; + sc = fsc->sc; if (!sc) { - for (i = 0; i < fsc.nargs; i++) - asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]); + for (i = 0; i < fsc->nargs; i++) + asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]); } else { /* * Here, we only look for arguments that have OUT masked in -- @@ -286,12 +297,12 @@ amd64_linux32_syscall_exit(struct trussinfo *truss */ if (errorp) { asprintf(&temp, "0x%lx", - fsc.args[sc->args[i].offset]); + fsc->args[sc->args[i].offset]); } else { temp = print_arg(&sc->args[i], - fsc.args, retval, trussinfo); + fsc->args, retval, trussinfo); } - fsc.s_args[i] = temp; + fsc->s_args[i] = temp; } } } @@ -308,13 +319,13 @@ amd64_linux32_syscall_exit(struct trussinfo *truss } } - if (fsc.name != NULL && (strcmp(fsc.name, "linux_execve") == 0 || - strcmp(fsc.name, "exit") == 0)) + if (fsc->name != NULL && (strcmp(fsc->name, "linux_execve") == 0 || + strcmp(fsc->name, "exit") == 0)) trussinfo->curthread->in_syscall = 1; - print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp, - errorp ? i : retval, fsc.sc); - clear_fsc(); + print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp, + errorp ? i : retval, fsc->sc); + free_fsc(fsc); return (retval); } Index: usr.bin/truss/ia64-fbsd.c =================================================================== --- usr.bin/truss/ia64-fbsd.c (revision 240005) +++ usr.bin/truss/ia64-fbsd.c (working copy) @@ -61,8 +61,6 @@ static const char rcsid[] = #include "syscall.h" #include "extern.h" -static int cpid = -1; - #include "syscalls.h" static int nsyscalls = sizeof(syscallnames) / sizeof(syscallnames[0]); @@ -76,30 +74,36 @@ static int nsyscalls = sizeof(syscallnames) / size * 'struct syscall' describes the system call; it may be NULL, however, * if we don't know about this particular system call yet. */ -static struct freebsd_syscall { +struct freebsd_syscall { struct syscall *sc; const char *name; int number; unsigned long *args; int nargs; /* number of arguments -- *not* number of words! */ char **s_args; /* the printable arguments */ -} fsc; +}; +static struct freebsd_syscall * +alloc_fsc(void) +{ + + return (malloc(sizeof(struct freebsd_syscall))); +} + /* Clear up and free parts of the fsc structure. */ -static __inline void -clear_fsc(void) +static void +free_fsc(struct freebsd_syscall *fsc) { int i; - if (fsc.args) - free(fsc.args); - if (fsc.s_args) { - for (i = 0; i < fsc.nargs; i++) - if (fsc.s_args[i]) - free(fsc.s_args[i]); - free(fsc.s_args); + if (fsc->args) + free(fsc->args); + if (fsc->s_args) { + for (i = 0; i < fsc->nargs; i++) + if (fsc->s_args[i]) + free(fsc->s_args[i]); + free(fsc->s_args); } - memset(&fsc, 0, sizeof(fsc)); } /* @@ -113,15 +117,15 @@ void ia64_syscall_entry(struct trussinfo *trussinfo, int nargs) { struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; unsigned long *parm_offset; + lwpid_t tid; int i, syscall_num; - clear_fsc(); + tid = trussinfo->curthread->tid; - cpid = trussinfo->curthread->tid; - - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return; } @@ -136,39 +140,44 @@ ia64_syscall_entry(struct trussinfo *trussinfo, in if (syscall_num == SYS_syscall || syscall_num == SYS___syscall) syscall_num = (int)*parm_offset++; - fsc.number = syscall_num; - fsc.name = (syscall_num < 0 || syscall_num >= nsyscalls) ? + fsc = alloc_fsc(); + if (fsc == NULL) { + fprintf(trussinfo->outfile, "-- CANNOT ALLOCATE MEMORY --\n"); + return; + } + fsc->number = syscall_num; + fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ? NULL : syscallnames[syscall_num]; - if (!fsc.name) { + if (!fsc->name) { fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num); } - if (fsc.name && (trussinfo->flags & FOLLOWFORKS) && - (strcmp(fsc.name, "fork") == 0 || - strcmp(fsc.name, "rfork") == 0 || - strcmp(fsc.name, "vfork") == 0)) + if (fsc->name && (trussinfo->flags & FOLLOWFORKS) && + (strcmp(fsc->name, "fork") == 0 || + strcmp(fsc->name, "rfork") == 0 || + strcmp(fsc->name, "vfork") == 0)) trussinfo->curthread->in_fork = 1; if (nargs == 0) return; - fsc.args = malloc((1 + nargs) * sizeof(unsigned long)); - memcpy(fsc.args, parm_offset, nargs * sizeof(long)); + fsc->args = malloc((1 + nargs) * sizeof(unsigned long)); + memcpy(fsc->args, parm_offset, nargs * sizeof(long)); - sc = get_syscall(fsc.name); + sc = get_syscall(fsc->name); if (sc) - fsc.nargs = sc->nargs; + fsc->nargs = sc->nargs; else { #if DEBUG fprintf(trussinfo->outfile, "unknown syscall %s -- setting " - "args to %d\n", fsc.name, nargs); + "args to %d\n", fsc->name, nargs); #endif - fsc.nargs = nargs; + fsc->nargs = nargs; } - fsc.s_args = calloc(1, (1 + fsc.nargs) * sizeof(char *)); - fsc.sc = sc; + fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *)); + fsc->sc = sc; /* * At this point, we set up the system call arguments. @@ -178,19 +187,19 @@ ia64_syscall_entry(struct trussinfo *trussinfo, in * passed in *and* out, however. */ - if (fsc.name) { + if (fsc->name) { #if DEBUG - fprintf(stderr, "syscall %s(", fsc.name); + fprintf(stderr, "syscall %s(", fsc->name); #endif - for (i = 0; i < fsc.nargs; i++) { + for (i = 0; i < fsc->nargs; i++) { #if DEBUG fprintf(stderr, "0x%x%s", sc ? - fsc.args[sc->args[i].offset] : fsc.args[i], - i < (fsc.nargs - 1) ? "," : ""); + fsc->args[sc->args[i].offset] : fsc->args[i], + i < (fsc->nargs - 1) ? "," : ""); #endif if (sc && !(sc->args[i].type & OUT)) { - fsc.s_args[i] = print_arg(&sc->args[i], - fsc.args, 0, trussinfo); + fsc->s_args[i] = print_arg(&sc->args[i], + fsc->args, 0, trussinfo); } } #if DEBUG @@ -202,30 +211,29 @@ ia64_syscall_entry(struct trussinfo *trussinfo, in fprintf(trussinfo->outfile, "\n"); #endif - if (fsc.name != NULL && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) { + if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) { /* * XXX * This could be done in a more general * manner but it still wouldn't be very pretty. */ - if (strcmp(fsc.name, "execve") == 0) { + if (strcmp(fsc->name, "execve") == 0) { if ((trussinfo->flags & EXECVEARGS) == 0) { - if (fsc.s_args[1]) { - free(fsc.s_args[1]); - fsc.s_args[1] = NULL; + if (fsc->s_args[1]) { + free(fsc->s_args[1]); + fsc->s_args[1] = NULL; } } if ((trussinfo->flags & EXECVEENVS) == 0) { - if (fsc.s_args[2]) { - free(fsc.s_args[2]); - fsc.s_args[2] = NULL; + if (fsc->s_args[2]) { + free(fsc->s_args[2]); + fsc->s_args[2] = NULL; } } } } - - return; + trussinfo->curthread->fsc = fsc; } /* @@ -239,16 +247,18 @@ long ia64_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused) { struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; + lwpid_t tid; long retval; int errorp, i; - if (fsc.name == NULL) + if (trussinfo->curthread->fsc == NULL) return (-1); - cpid = trussinfo->curthread->tid; + tid = trussinfo->curthread->tid; - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return (-1); } @@ -261,10 +271,11 @@ ia64_syscall_exit(struct trussinfo *trussinfo, int * stand some significant cleaning. */ - sc = fsc.sc; + fsc = trussinfo->curthread->fsc; + sc = fsc->sc; if (!sc) { - for (i = 0; i < fsc.nargs; i++) - asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]); + for (i = 0; i < fsc->nargs; i++) + asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]); } else { /* * Here, we only look for arguments that have OUT masked in -- @@ -279,27 +290,28 @@ ia64_syscall_exit(struct trussinfo *trussinfo, int */ if (errorp) { asprintf(&temp, "0x%lx", - fsc.args[sc->args[i].offset]); + fsc->args[sc->args[i].offset]); } else { temp = print_arg(&sc->args[i], - fsc.args, retval, trussinfo); + fsc->args, retval, trussinfo); } - fsc.s_args[i] = temp; + fsc->s_args[i] = temp; } } } - if (fsc.name != NULL && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) + if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) trussinfo->curthread->in_syscall = 1; + /* * It would probably be a good idea to merge the error handling, * but that complicates things considerably. */ - print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp, - retval, fsc.sc); - clear_fsc(); + print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp, + retval, fsc->sc); + free_fsc(fsc); return (retval); } Index: usr.bin/truss/syscalls.c =================================================================== --- usr.bin/truss/syscalls.c (revision 240005) +++ usr.bin/truss/syscalls.c (working copy) @@ -1120,20 +1120,21 @@ print_syscall(struct trussinfo *trussinfo, const c if (trussinfo->flags & FOLLOWFORKS) len += fprintf(trussinfo->outfile, "%5d: ", trussinfo->pid); - if (name != NULL && (strcmp(name, "execve") == 0|| + if (name != NULL && (strcmp(name, "execve") == 0 || strcmp(name, "exit") == 0)) { - clock_gettime(CLOCK_REALTIME, &trussinfo->after); + clock_gettime(CLOCK_REALTIME, &trussinfo->curthread->after); } if (trussinfo->flags & ABSOLUTETIMESTAMPS) { - timespecsubt(&trussinfo->after, &trussinfo->start_time, - &timediff); + timespecsubt(&trussinfo->curthread->after, + &trussinfo->start_time, &timediff); len += fprintf(trussinfo->outfile, "%ld.%09ld ", (long)timediff.tv_sec, timediff.tv_nsec); } if (trussinfo->flags & RELATIVETIMESTAMPS) { - timespecsubt(&trussinfo->after, &trussinfo->before, &timediff); + timespecsubt(&trussinfo->curthread->after, + &trussinfo->curthread->before, &timediff); len += fprintf(trussinfo->outfile, "%ld.%09ld ", (long)timediff.tv_sec, timediff.tv_nsec); } @@ -1163,8 +1164,9 @@ print_syscall_ret(struct trussinfo *trussinfo, con if (trussinfo->flags & COUNTONLY) { if (!sc) return; - clock_gettime(CLOCK_REALTIME, &trussinfo->after); - timespecsubt(&trussinfo->after, &trussinfo->before, &timediff); + clock_gettime(CLOCK_REALTIME, &trussinfo->curthread->after); + timespecsubt(&trussinfo->curthread->after, + &trussinfo->curthread->before, &timediff); timespecadd(&sc->time, &timediff, &sc->time); sc->ncalls++; if (errorp) Index: usr.bin/truss/sparc64-fbsd.c =================================================================== --- usr.bin/truss/sparc64-fbsd.c (revision 240005) +++ usr.bin/truss/sparc64-fbsd.c (working copy) @@ -67,8 +67,6 @@ static const char rcsid[] = #include "syscall.h" #include "extern.h" -static int cpid = -1; - #include "syscalls.h" static int nsyscalls = sizeof(syscallnames) / sizeof(syscallnames[0]); @@ -82,30 +80,36 @@ static int nsyscalls = sizeof(syscallnames) / size * 'struct syscall' describes the system call; it may be NULL, however, * if we don't know about this particular system call yet. */ -static struct freebsd_syscall { +struct freebsd_syscall { struct syscall *sc; const char *name; int number; unsigned long *args; int nargs; /* number of arguments -- *not* number of words! */ char **s_args; /* the printable arguments */ -} fsc; +}; +static struct freebsd_syscall * +alloc_fsc(void) +{ + + return (malloc(sizeof(struct freebsd_syscall))); +} + /* Clear up and free parts of the fsc structure. */ -static __inline void -clear_fsc(void) +static void +free_fsc(struct freebsd_syscall *fsc) { int i; - if (fsc.args) - free(fsc.args); - if (fsc.s_args) { - for (i = 0; i < fsc.nargs; i++) - if (fsc.s_args[i]) - free(fsc.s_args[i]); - free(fsc.s_args); + if (fsc->args) + free(fsc->args); + if (fsc->s_args) { + for (i = 0; i < fsc->nargs; i++) + if (fsc->s_args[i]) + free(fsc->s_args[i]); + free(fsc->s_args); } - memset(&fsc, 0, sizeof(fsc)); } /* @@ -120,15 +124,15 @@ sparc64_syscall_entry(struct trussinfo *trussinfo, { struct ptrace_io_desc iorequest; struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; + lwpid_t tid; int i, syscall_num; int indir; /* indirect system call */ - clear_fsc(); + tid = trussinfo->curthread->tid; - cpid = trussinfo->curthread->tid; - - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return; } @@ -145,24 +149,29 @@ sparc64_syscall_entry(struct trussinfo *trussinfo, syscall_num = regs.r_out[0]; } - fsc.number = syscall_num; - fsc.name = (syscall_num < 0 || syscall_num >= nsyscalls) ? + fsc = alloc_fsc(); + if (fsc == NULL) { + fprintf(trussinfo->outfile, "-- CANNOT ALLOCATE MEMORY --\n"); + return; + } + fsc->number = syscall_num; + fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ? NULL : syscallnames[syscall_num]; - if (!fsc.name) { + if (!fsc->name) { fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num); } - if (fsc.name && (trussinfo->flags & FOLLOWFORKS) && - (strcmp(fsc.name, "fork") == 0 || - strcmp(fsc.name, "rfork") == 0 || - strcmp(fsc.name, "vfork") == 0)) + if (fsc->name && (trussinfo->flags & FOLLOWFORKS) && + (strcmp(fsc->name, "fork") == 0 || + strcmp(fsc->name, "rfork") == 0 || + strcmp(fsc->name, "vfork") == 0)) trussinfo->curthread->in_fork = 1; if (nargs == 0) return; - fsc.args = malloc((1 + nargs) * sizeof(unsigned long)); + fsc->args = malloc((1 + nargs) * sizeof(unsigned long)); switch (nargs) { default: /* @@ -183,38 +192,38 @@ sparc64_syscall_entry(struct trussinfo *trussinfo, iorequest.piod_op = PIOD_READ_D; iorequest.piod_offs = (void *)(regs.r_out[6] + SPOFF + offsetof(struct frame, fr_pad[6])); - iorequest.piod_addr = &fsc.args[6]; - iorequest.piod_len = (nargs - 6) * sizeof(fsc.args[0]); - ptrace(PT_IO, cpid, (caddr_t)&iorequest, 0); + iorequest.piod_addr = &fsc->args[6]; + iorequest.piod_len = (nargs - 6) * sizeof(fsc->args[0]); + ptrace(PT_IO, tid, (caddr_t)&iorequest, 0); if (iorequest.piod_len == 0) return; - case 6: fsc.args[5] = regs.r_out[5]; - case 5: fsc.args[4] = regs.r_out[4]; - case 4: fsc.args[3] = regs.r_out[3]; - case 3: fsc.args[2] = regs.r_out[2]; - case 2: fsc.args[1] = regs.r_out[1]; - case 1: fsc.args[0] = regs.r_out[0]; + case 6: fsc->args[5] = regs.r_out[5]; + case 5: fsc->args[4] = regs.r_out[4]; + case 4: fsc->args[3] = regs.r_out[3]; + case 3: fsc->args[2] = regs.r_out[2]; + case 2: fsc->args[1] = regs.r_out[1]; + case 1: fsc->args[0] = regs.r_out[0]; case 0: break; } if (indir) - memmove(&fsc.args[0], &fsc.args[1], (nargs - 1) * - sizeof(fsc.args[0])); + memmove(&fsc->args[0], &fsc->args[1], (nargs - 1) * + sizeof(fsc->args[0])); - sc = get_syscall(fsc.name); + sc = get_syscall(fsc->name); if (sc) - fsc.nargs = sc->nargs; + fsc->nargs = sc->nargs; else { #if DEBUG fprintf(trussinfo->outfile, "unknown syscall %s -- setting " - "args to %d\n", fsc.name, nargs); + "args to %d\n", fsc->name, nargs); #endif - fsc.nargs = nargs; + fsc->nargs = nargs; } - fsc.s_args = calloc(1, (1 + fsc.nargs) * sizeof(char *)); - fsc.sc = sc; + fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *)); + fsc->sc = sc; /* * At this point, we set up the system call arguments. @@ -224,19 +233,19 @@ sparc64_syscall_entry(struct trussinfo *trussinfo, * passed in *and* out, however. */ - if (fsc.name) { + if (fsc->name) { #if DEBUG - fprintf(stderr, "syscall %s(", fsc.name); + fprintf(stderr, "syscall %s(", fsc->name); #endif - for (i = 0; i < fsc.nargs; i++) { + for (i = 0; i < fsc->nargs; i++) { #if DEBUG fprintf(stderr, "0x%x%s", sc ? - fsc.args[sc->args[i].offset] : fsc.args[i], - i < (fsc.nargs - 1) ? "," : ""); + fsc->args[sc->args[i].offset] : fsc->args[i], + i < (fsc->nargs - 1) ? "," : ""); #endif if (sc && !(sc->args[i].type & OUT)) { - fsc.s_args[i] = print_arg(&sc->args[i], - fsc.args, 0, trussinfo); + fsc->s_args[i] = print_arg(&sc->args[i], + fsc->args, 0, trussinfo); } } #if DEBUG @@ -248,30 +257,29 @@ sparc64_syscall_entry(struct trussinfo *trussinfo, fprintf(trussinfo->outfile, "\n"); #endif - if (fsc.name != NULL && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) { + if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) { /* * XXX * This could be done in a more general * manner but it still wouldn't be very pretty. */ - if (strcmp(fsc.name, "execve") == 0) { + if (strcmp(fsc->name, "execve") == 0) { if ((trussinfo->flags & EXECVEARGS) == 0) { - if (fsc.s_args[1]) { - free(fsc.s_args[1]); - fsc.s_args[1] = NULL; + if (fsc->s_args[1]) { + free(fsc->s_args[1]); + fsc->s_args[1] = NULL; } } if ((trussinfo->flags & EXECVEENVS) == 0) { - if (fsc.s_args[2]) { - free(fsc.s_args[2]); - fsc.s_args[2] = NULL; + if (fsc->s_args[2]) { + free(fsc->s_args[2]); + fsc->s_args[2] = NULL; } } } } - - return; + trussinfo->curthread->fsc = fsc; } /* @@ -285,16 +293,18 @@ long sparc64_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused) { struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; + lwpid_t tid; long retval; int errorp, i; - if (fsc.name == NULL) + if (trussinfo->curthread->fsc == NULL) return (-1); - cpid = trussinfo->curthread->tid; + tid = trussinfo->curthread->tid; - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "\n"); return (-1); } @@ -307,10 +317,11 @@ sparc64_syscall_exit(struct trussinfo *trussinfo, * stand some significant cleaning. */ - sc = fsc.sc; + fsc = trussinfo->curthread->fsc; + sc = fsc->sc; if (!sc) { - for (i = 0; i < fsc.nargs; i++) - asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]); + for (i = 0; i < fsc->nargs; i++) + asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]); } else { /* * Here, we only look for arguments that have OUT masked in -- @@ -325,18 +336,18 @@ sparc64_syscall_exit(struct trussinfo *trussinfo, */ if (errorp) { asprintf(&temp, "0x%lx", - fsc.args[sc->args[i].offset]); + fsc->args[sc->args[i].offset]); } else { temp = print_arg(&sc->args[i], - fsc.args, retval, trussinfo); + fsc->args, retval, trussinfo); } - fsc.s_args[i] = temp; + fsc->s_args[i] = temp; } } } - if (fsc.name != NULL && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) + if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) trussinfo->curthread->in_syscall = 1; /* @@ -344,9 +355,9 @@ sparc64_syscall_exit(struct trussinfo *trussinfo, * but that complicates things considerably. */ - print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp, - retval, fsc.sc); - clear_fsc(); + print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp, + retval, fsc->sc); + free_fsc(fsc); return (retval); } Index: usr.bin/truss/i386-fbsd.c =================================================================== --- usr.bin/truss/i386-fbsd.c (revision 240005) +++ usr.bin/truss/i386-fbsd.c (working copy) @@ -62,8 +62,6 @@ static const char rcsid[] = #include "syscall.h" #include "extern.h" -static int cpid = -1; - #include "syscalls.h" static int nsyscalls = sizeof(syscallnames) / sizeof(syscallnames[0]); @@ -77,30 +75,36 @@ static int nsyscalls = sizeof(syscallnames) / size * 'struct syscall' describes the system call; it may be NULL, however, * if we don't know about this particular system call yet. */ -static struct freebsd_syscall { +struct freebsd_syscall { struct syscall *sc; const char *name; int number; unsigned long *args; int nargs; /* number of arguments -- *not* number of words! */ char **s_args; /* the printable arguments */ -} fsc; +}; +static struct freebsd_syscall * +alloc_fsc(void) +{ + + return (malloc(sizeof(struct freebsd_syscall))); +} + /* Clear up and free parts of the fsc structure. */ -static __inline void -clear_fsc(void) +static void +free_fsc(struct freebsd_syscall *fsc) { int i; - if (fsc.args) - free(fsc.args); - if (fsc.s_args) { - for (i = 0; i < fsc.nargs; i++) - if (fsc.s_args[i]) - free(fsc.s_args[i]); - free(fsc.s_args); + if (fsc->args) + free(fsc->args); + if (fsc->s_args) { + for (i = 0; i < fsc->nargs; i++) + if (fsc->s_args[i]) + free(fsc->s_args[i]); + free(fsc->s_args); } - memset(&fsc, 0, sizeof(fsc)); } /* @@ -115,15 +119,15 @@ i386_syscall_entry(struct trussinfo *trussinfo, in { struct ptrace_io_desc iorequest; struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; + lwpid_t tid; unsigned int parm_offset; int i, syscall_num; - clear_fsc(); + tid = trussinfo->curthread->tid; - cpid = trussinfo->curthread->tid; - - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return; } @@ -137,56 +141,61 @@ i386_syscall_entry(struct trussinfo *trussinfo, in syscall_num = regs.r_eax; switch (syscall_num) { case SYS_syscall: - syscall_num = ptrace(PT_READ_D, cpid, (caddr_t)parm_offset, 0); + syscall_num = ptrace(PT_READ_D, tid, (caddr_t)parm_offset, 0); parm_offset += sizeof(int); break; case SYS___syscall: - syscall_num = ptrace(PT_READ_D, cpid, (caddr_t)parm_offset, 0); + syscall_num = ptrace(PT_READ_D, tid, (caddr_t)parm_offset, 0); parm_offset += sizeof(quad_t); break; } - fsc.number = syscall_num; - fsc.name = (syscall_num < 0 || syscall_num >= nsyscalls) ? + fsc = alloc_fsc(); + if (fsc == NULL) { + fprintf(trussinfo->outfile, "-- CANNOT ALLOCATE MEMORY --\n"); + return; + } + fsc->number = syscall_num; + fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ? NULL : syscallnames[syscall_num]; - if (!fsc.name) { + if (!fsc->name) { fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num); } - if (fsc.name && (trussinfo->flags & FOLLOWFORKS) && - (strcmp(fsc.name, "fork") == 0 || - strcmp(fsc.name, "rfork") == 0 || - strcmp(fsc.name, "vfork") == 0)) + if (fsc->name && (trussinfo->flags & FOLLOWFORKS) && + (strcmp(fsc->name, "fork") == 0 || + strcmp(fsc->name, "rfork") == 0 || + strcmp(fsc->name, "vfork") == 0)) trussinfo->curthread->in_fork = 1; if (nargs == 0) return; - fsc.args = malloc((1 + nargs) * sizeof(unsigned long)); + fsc->args = malloc((1 + nargs) * sizeof(unsigned long)); iorequest.piod_op = PIOD_READ_D; iorequest.piod_offs = (void *)parm_offset; - iorequest.piod_addr = fsc.args; + iorequest.piod_addr = fsc->args; iorequest.piod_len = (1 + nargs) * sizeof(unsigned long); - ptrace(PT_IO, cpid, (caddr_t)&iorequest, 0); + ptrace(PT_IO, tid, (caddr_t)&iorequest, 0); if (iorequest.piod_len == 0) return; sc = NULL; - if (fsc.name) - sc = get_syscall(fsc.name); + if (fsc->name) + sc = get_syscall(fsc->name); if (sc) - fsc.nargs = sc->nargs; + fsc->nargs = sc->nargs; else { #if DEBUG fprintf(trussinfo->outfile, "unknown syscall %s -- setting " - "args to %d\n", fsc.name, nargs); + "args to %d\n", fsc->name, nargs); #endif - fsc.nargs = nargs; + fsc->nargs = nargs; } - fsc.s_args = calloc(1, (1 + fsc.nargs) * sizeof(char *)); - fsc.sc = sc; + fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *)); + fsc->sc = sc; /* * At this point, we set up the system call arguments. @@ -196,19 +205,19 @@ i386_syscall_entry(struct trussinfo *trussinfo, in * passed in *and* out, however. */ - if (fsc.name) { + if (fsc->name) { #if DEBUG - fprintf(stderr, "syscall %s(", fsc.name); + fprintf(stderr, "syscall %s(", fsc->name); #endif - for (i = 0; i < fsc.nargs; i++) { + for (i = 0; i < fsc->nargs; i++) { #if DEBUG fprintf(stderr, "0x%x%s", sc ? - fsc.args[sc->args[i].offset] : fsc.args[i], - i < (fsc.nargs - 1) ? "," : ""); + fsc->args[sc->args[i].offset] : fsc->args[i], + i < (fsc->nargs - 1) ? "," : ""); #endif if (sc && !(sc->args[i].type & OUT)) { - fsc.s_args[i] = print_arg(&sc->args[i], - fsc.args, 0, trussinfo); + fsc->s_args[i] = print_arg(&sc->args[i], + fsc->args, 0, trussinfo); } } #if DEBUG @@ -220,30 +229,29 @@ i386_syscall_entry(struct trussinfo *trussinfo, in fprintf(trussinfo->outfile, "\n"); #endif - if (fsc.name != NULL && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) { + if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) { /* * XXX * This could be done in a more general * manner but it still wouldn't be very pretty. */ - if (strcmp(fsc.name, "execve") == 0) { + if (strcmp(fsc->name, "execve") == 0) { if ((trussinfo->flags & EXECVEARGS) == 0) { - if (fsc.s_args[1]) { - free(fsc.s_args[1]); - fsc.s_args[1] = NULL; + if (fsc->s_args[1]) { + free(fsc->s_args[1]); + fsc->s_args[1] = NULL; } } if ((trussinfo->flags & EXECVEENVS) == 0) { - if (fsc.s_args[2]) { - free(fsc.s_args[2]); - fsc.s_args[2] = NULL; + if (fsc->s_args[2]) { + free(fsc->s_args[2]); + fsc->s_args[2] = NULL; } } } } - - return; + trussinfo->curthread->fsc = fsc; } /* @@ -257,16 +265,18 @@ long i386_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused) { struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; + lwpid_t tid; long retval; int errorp, i; - if (fsc.name == NULL) + if (trussinfo->curthread->fsc == NULL) return (-1); - cpid = trussinfo->curthread->tid; + tid = trussinfo->curthread->tid; - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return (-1); } @@ -279,10 +289,11 @@ i386_syscall_exit(struct trussinfo *trussinfo, int * stand some significant cleaning. */ - sc = fsc.sc; + fsc = trussinfo->curthread->fsc; + sc = fsc->sc; if (!sc) { - for (i = 0; i < fsc.nargs; i++) - asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]); + for (i = 0; i < fsc->nargs; i++) + asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]); } else { /* * Here, we only look for arguments that have OUT masked in -- @@ -297,18 +308,18 @@ i386_syscall_exit(struct trussinfo *trussinfo, int */ if (errorp) { asprintf(&temp, "0x%lx", - fsc.args[sc->args[i].offset]); + fsc->args[sc->args[i].offset]); } else { temp = print_arg(&sc->args[i], - fsc.args, retval, trussinfo); + fsc->args, retval, trussinfo); } - fsc.s_args[i] = temp; + fsc->s_args[i] = temp; } } } - if (fsc.name != NULL && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) + if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) trussinfo->curthread->in_syscall = 1; /* @@ -316,9 +327,9 @@ i386_syscall_exit(struct trussinfo *trussinfo, int * but that complicates things considerably. */ - print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp, - retval, fsc.sc); - clear_fsc(); + print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp, + retval, fsc->sc); + free_fsc(fsc); return (retval); } Index: usr.bin/truss/powerpc64-fbsd.c =================================================================== --- usr.bin/truss/powerpc64-fbsd.c (revision 240005) +++ usr.bin/truss/powerpc64-fbsd.c (working copy) @@ -61,8 +61,6 @@ static const char rcsid[] = #include "syscall.h" #include "extern.h" -static int cpid = -1; - #include "syscalls.h" static int nsyscalls = sizeof(syscallnames) / sizeof(syscallnames[0]); @@ -76,30 +74,36 @@ static int nsyscalls = sizeof(syscallnames) / size * 'struct syscall' describes the system call; it may be NULL, however, * if we don't know about this particular system call yet. */ -static struct freebsd_syscall { +struct freebsd_syscall { struct syscall *sc; const char *name; int number; unsigned long *args; int nargs; /* number of arguments -- *not* number of words! */ char **s_args; /* the printable arguments */ -} fsc; +}; +static struct freebsd_syscall * +alloc_fsc(void) +{ + + return (malloc(sizeof(struct freebsd_syscall))); +} + /* Clear up and free parts of the fsc structure. */ -static __inline void -clear_fsc(void) +static void +free_fsc(struct freebsd_syscall *fsc) { int i; - if (fsc.args) - free(fsc.args); - if (fsc.s_args) { - for (i = 0; i < fsc.nargs; i++) - if (fsc.s_args[i]) - free(fsc.s_args[i]); - free(fsc.s_args); + if (fsc->args) + free(fsc->args); + if (fsc->s_args) { + for (i = 0; i < fsc->nargs; i++) + if (fsc->s_args[i]) + free(fsc->s_args[i]); + free(fsc->s_args); } - memset(&fsc, 0, sizeof(fsc)); } /* @@ -114,15 +118,15 @@ powerpc64_syscall_entry(struct trussinfo *trussinf { struct ptrace_io_desc iorequest; struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; void *args; + lwpid_t tid; int i, regargs, syscall_num; - clear_fsc(); + tid = trussinfo->curthread->tid; - cpid = trussinfo->curthread->tid; - - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return; } @@ -141,51 +145,56 @@ powerpc64_syscall_entry(struct trussinfo *trussinf syscall_num = regs.fixreg[3]; } - fsc.number = syscall_num; - fsc.name = (syscall_num < 0 || syscall_num >= nsyscalls) ? + fsc = alloc_fsc(); + if (fsc == NULL) { + fprintf(trussinfo->outfile, "-- CANNOT ALLOCATE MEMORY --\n"); + return; + } + fsc->number = syscall_num; + fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ? NULL : syscallnames[syscall_num]; - if (!fsc.name) { + if (!fsc->name) { fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num); } - if (fsc.name && (trussinfo->flags & FOLLOWFORKS) && - (strcmp(fsc.name, "fork") == 0 || - strcmp(fsc.name, "rfork") == 0 || - strcmp(fsc.name, "vfork") == 0)) + if (fsc->name && (trussinfo->flags & FOLLOWFORKS) && + (strcmp(fsc->name, "fork") == 0 || + strcmp(fsc->name, "rfork") == 0 || + strcmp(fsc->name, "vfork") == 0)) trussinfo->curthread->in_fork = 1; if (nargs == 0) return; - fsc.args = malloc((1 + nargs) * sizeof(unsigned long)); + fsc->args = malloc((1 + nargs) * sizeof(unsigned long)); if (nargs > regargs) { - memmove(&fsc.args[0], args, regargs * sizeof(fsc.args[0])); + memmove(&fsc->args[0], args, regargs * sizeof(fsc->args[0])); iorequest.piod_op = PIOD_READ_D; iorequest.piod_offs = (void *)(regs.fixreg[1] + 48); - iorequest.piod_addr = &fsc.args[regargs]; - iorequest.piod_len = (nargs - regargs) * sizeof(fsc.args[0]); - ptrace(PT_IO, cpid, (caddr_t)&iorequest, 0); + iorequest.piod_addr = &fsc->args[regargs]; + iorequest.piod_len = (nargs - regargs) * sizeof(fsc->args[0]); + ptrace(PT_IO, tid, (caddr_t)&iorequest, 0); if (iorequest.piod_len == 0) return; } else - memmove(&fsc.args[0], args, nargs * sizeof(fsc.args[0])); + memmove(&fsc->args[0], args, nargs * sizeof(fsc->args[0])); - sc = get_syscall(fsc.name); + sc = get_syscall(fsc->name); if (sc) - fsc.nargs = sc->nargs; + fsc->nargs = sc->nargs; else { #if DEBUG fprintf(trussinfo->outfile, "unknown syscall %s -- setting " - "args to %d\n", fsc.name, nargs); + "args to %d\n", fsc->name, nargs); #endif - fsc.nargs = nargs; + fsc->nargs = nargs; } - fsc.s_args = calloc(1, (1 + fsc.nargs) * sizeof(char *)); - fsc.sc = sc; + fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *)); + fsc->sc = sc; /* * At this point, we set up the system call arguments. @@ -195,19 +204,19 @@ powerpc64_syscall_entry(struct trussinfo *trussinf * passed in *and* out, however. */ - if (fsc.name) { + if (fsc->name) { #if DEBUG - fprintf(stderr, "syscall %s(", fsc.name); + fprintf(stderr, "syscall %s(", fsc->name); #endif - for (i = 0; i < fsc.nargs; i++) { + for (i = 0; i < fsc->nargs; i++) { #if DEBUG fprintf(stderr, "0x%x%s", sc ? - fsc.args[sc->args[i].offset] : fsc.args[i], - i < (fsc.nargs - 1) ? "," : ""); + fsc->args[sc->args[i].offset] : fsc->args[i], + i < (fsc->nargs - 1) ? "," : ""); #endif if (sc && !(sc->args[i].type & OUT)) { - fsc.s_args[i] = print_arg(&sc->args[i], - fsc.args, 0, trussinfo); + fsc->s_args[i] = print_arg(&sc->args[i], + fsc->args, 0, trussinfo); } } #if DEBUG @@ -219,30 +228,29 @@ powerpc64_syscall_entry(struct trussinfo *trussinf fprintf(trussinfo->outfile, "\n"); #endif - if (fsc.name && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) { + if (fsc->name && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) { /* * XXX * This could be done in a more general * manner but it still wouldn't be very pretty. */ - if (strcmp(fsc.name, "execve") == 0) { + if (strcmp(fsc->name, "execve") == 0) { if ((trussinfo->flags & EXECVEARGS) == 0) { - if (fsc.s_args[1]) { - free(fsc.s_args[1]); - fsc.s_args[1] = NULL; + if (fsc->s_args[1]) { + free(fsc->s_args[1]); + fsc->s_args[1] = NULL; } } if ((trussinfo->flags & EXECVEENVS) == 0) { - if (fsc.s_args[2]) { - free(fsc.s_args[2]); - fsc.s_args[2] = NULL; + if (fsc->s_args[2]) { + free(fsc->s_args[2]); + fsc->s_args[2] = NULL; } } } } - - return; + trussinfo->curthread->fsc = fsc; } /* @@ -256,16 +264,18 @@ long powerpc64_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused) { struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; + lwpid_t tid; long retval; int errorp, i; - if (fsc.name == NULL) + if (trussinfo->curthread->fsc == NULL) return (-1); - cpid = trussinfo->curthread->tid; + tid = trussinfo->curthread->tid; - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "\n"); return (-1); } @@ -278,10 +288,11 @@ powerpc64_syscall_exit(struct trussinfo *trussinfo * stand some significant cleaning. */ - sc = fsc.sc; + fsc = trussinfo->curthread->fsc; + sc = fsc->sc; if (!sc) { - for (i = 0; i < fsc.nargs; i++) - asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]); + for (i = 0; i < fsc->nargs; i++) + asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]); } else { /* * Here, we only look for arguments that have OUT masked in -- @@ -296,18 +307,18 @@ powerpc64_syscall_exit(struct trussinfo *trussinfo */ if (errorp) { asprintf(&temp, "0x%lx", - fsc.args[sc->args[i].offset]); + fsc->args[sc->args[i].offset]); } else { temp = print_arg(&sc->args[i], - fsc.args, retval, trussinfo); + fsc->args, retval, trussinfo); } - fsc.s_args[i] = temp; + fsc->s_args[i] = temp; } } } - if (fsc.name != NULL && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) + if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) trussinfo->curthread->in_syscall = 1; /* @@ -315,9 +326,9 @@ powerpc64_syscall_exit(struct trussinfo *trussinfo * but that complicates things considerably. */ - print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp, - retval, fsc.sc); - clear_fsc(); + print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp, + retval, fsc->sc); + free_fsc(fsc); return (retval); } Index: usr.bin/truss/main.c =================================================================== --- usr.bin/truss/main.c (revision 240005) +++ usr.bin/truss/main.c (working copy) @@ -163,13 +163,14 @@ strsig(int sig) int main(int ac, char **av) { + struct timespec timediff; struct ex_types *funcs; struct trussinfo *trussinfo; char *fname; char *signame; char **command; pid_t childpid; - int c, i, initial_open, status; + int c, initial_open, status; fname = NULL; initial_open = 1; @@ -282,18 +283,17 @@ START_TRACE: clock_gettime(CLOCK_REALTIME, &trussinfo->start_time); do { - struct timespec timediff; waitevent(trussinfo); - switch (i = trussinfo->pr_why) { + switch (trussinfo->pr_why) { case S_SCE: funcs->enter_syscall(trussinfo, MAXARGS); clock_gettime(CLOCK_REALTIME, - &trussinfo->before); + &trussinfo->curthread->before); break; case S_SCX: clock_gettime(CLOCK_REALTIME, - &trussinfo->after); + &trussinfo->curthread->after); if (trussinfo->curthread->in_fork && (trussinfo->flags & FOLLOWFORKS)) { @@ -322,15 +322,15 @@ START_TRACE: fprintf(trussinfo->outfile, "%5d: ", trussinfo->pid); if (trussinfo->flags & ABSOLUTETIMESTAMPS) { - timespecsubt(&trussinfo->after, + timespecsubt(&trussinfo->curthread->after, &trussinfo->start_time, &timediff); fprintf(trussinfo->outfile, "%ld.%09ld ", (long)timediff.tv_sec, timediff.tv_nsec); } if (trussinfo->flags & RELATIVETIMESTAMPS) { - timespecsubt(&trussinfo->after, - &trussinfo->before, &timediff); + timespecsubt(&trussinfo->curthread->after, + &trussinfo->curthread->before, &timediff); fprintf(trussinfo->outfile, "%ld.%09ld ", (long)timediff.tv_sec, timediff.tv_nsec); @@ -348,15 +348,15 @@ START_TRACE: fprintf(trussinfo->outfile, "%5d: ", trussinfo->pid); if (trussinfo->flags & ABSOLUTETIMESTAMPS) { - timespecsubt(&trussinfo->after, + timespecsubt(&trussinfo->curthread->after, &trussinfo->start_time, &timediff); fprintf(trussinfo->outfile, "%ld.%09ld ", (long)timediff.tv_sec, timediff.tv_nsec); } if (trussinfo->flags & RELATIVETIMESTAMPS) { - timespecsubt(&trussinfo->after, - &trussinfo->before, &timediff); + timespecsubt(&trussinfo->curthread->after, + &trussinfo->curthread->before, &timediff); fprintf(trussinfo->outfile, "%ld.%09ld ", (long)timediff.tv_sec, timediff.tv_nsec); } Index: usr.bin/truss/mips-fbsd.c =================================================================== --- usr.bin/truss/mips-fbsd.c (revision 240005) +++ usr.bin/truss/mips-fbsd.c (working copy) @@ -66,8 +66,6 @@ static const char rcsid[] = #include "syscall.h" #include "extern.h" -static int cpid = -1; - #include "syscalls.h" static int nsyscalls = sizeof(syscallnames) / sizeof(syscallnames[0]); @@ -81,30 +79,36 @@ static int nsyscalls = sizeof(syscallnames) / size * 'struct syscall' describes the system call; it may be NULL, however, * if we don't know about this particular system call yet. */ -static struct freebsd_syscall { +struct freebsd_syscall { struct syscall *sc; const char *name; int number; unsigned long *args; int nargs; /* number of arguments -- *not* number of words! */ char **s_args; /* the printable arguments */ -} fsc; +}; +static struct freebsd_syscall * +alloc_fsc(void) +{ + + return (malloc(sizeof(struct freebsd_syscall))); +} + /* Clear up and free parts of the fsc structure. */ -static __inline void -clear_fsc(void) +static void +free_fsc(struct freebsd_syscall *fsc) { int i; - if (fsc.args) - free(fsc.args); - if (fsc.s_args) { - for (i = 0; i < fsc.nargs; i++) - if (fsc.s_args[i]) - free(fsc.s_args[i]); - free(fsc.s_args); + if (fsc->args) + free(fsc->args); + if (fsc->s_args) { + for (i = 0; i < fsc->nargs; i++) + if (fsc->s_args[i]) + free(fsc->s_args[i]); + free(fsc->s_args); } - memset(&fsc, 0, sizeof(fsc)); } /* @@ -119,15 +123,15 @@ mips_syscall_entry(struct trussinfo *trussinfo, in { struct ptrace_io_desc iorequest; struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; + lwpid_t tid; int i, syscall_num; int indir; /* indirect system call */ - clear_fsc(); + tid = trussinfo->curthread->tid; - cpid = trussinfo->curthread->tid; - - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return; } @@ -139,30 +143,35 @@ mips_syscall_entry(struct trussinfo *trussinfo, in syscall_num = regs.r_regs[A0]; } - fsc.number = syscall_num; - fsc.name = (syscall_num < 0 || syscall_num >= nsyscalls) ? + fsc = alloc_fsc(); + if (fsc == NULL) { + fprintf(trussinfo->outfile, "-- CANNOT ALLOCATE MEMORY --\n"); + return; + } + fsc->number = syscall_num; + fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ? NULL : syscallnames[syscall_num]; - if (!fsc.name) { + if (!fsc->name) { fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num); } - if (fsc.name && (trussinfo->flags & FOLLOWFORKS) && - (strcmp(fsc.name, "fork") == 0 || - strcmp(fsc.name, "rfork") == 0 || - strcmp(fsc.name, "vfork") == 0)) + if (fsc->name && (trussinfo->flags & FOLLOWFORKS) && + (strcmp(fsc->name, "fork") == 0 || + strcmp(fsc->name, "rfork") == 0 || + strcmp(fsc->name, "vfork") == 0)) trussinfo->curthread->in_fork = 1; if (nargs == 0) return; - fsc.args = malloc((1 + nargs) * sizeof(unsigned long)); + fsc->args = malloc((1 + nargs) * sizeof(unsigned long)); #if 0 // XXX iorequest.piod_op = PIOD_READ_D; iorequest.piod_offs = (void *)parm_offset; - iorequest.piod_addr = fsc.args; + iorequest.piod_addr = fsc->args; iorequest.piod_len = (1 + nargs) * sizeof(unsigned long); - ptrace(PT_IO, cpid, (caddr_t)&iorequest, 0); + ptrace(PT_IO, tid, (caddr_t)&iorequest, 0); if (iorequest.piod_len == 0) return; #else @@ -192,35 +201,35 @@ mips_syscall_entry(struct trussinfo *trussinfo, in iorequest.piod_op = PIOD_READ_D; iorequest.piod_offs = (void *)(regs.r_regs[SP] + 4 * sizeof(uint32_t)); - iorequest.piod_addr = &fsc.args[4]; - iorequest.piod_len = (nargs - 4) * sizeof(fsc.args[0]); - ptrace(PT_IO, cpid, (caddr_t)&iorequest, 0); + iorequest.piod_addr = &fsc->args[4]; + iorequest.piod_len = (nargs - 4) * sizeof(fsc->args[0]); + ptrace(PT_IO, tid, (caddr_t)&iorequest, 0); if (iorequest.piod_len == 0) return; - case 4: fsc.args[3] = regs.r_regs[A3]; - case 3: fsc.args[2] = regs.r_regs[A2]; - case 2: fsc.args[1] = regs.r_regs[A1]; - case 1: fsc.args[0] = regs.r_regs[A0]; + case 4: fsc->args[3] = regs.r_regs[A3]; + case 3: fsc->args[2] = regs.r_regs[A2]; + case 2: fsc->args[1] = regs.r_regs[A1]; + case 1: fsc->args[0] = regs.r_regs[A0]; case 0: break; } if (indir) { - memmove(&fsc.args[0], &fsc.args[1], - (nargs - 1) * sizeof(fsc.args[0])); + memmove(&fsc->args[0], &fsc->args[1], + (nargs - 1) * sizeof(fsc->args[0])); } - sc = get_syscall(fsc.name); + sc = get_syscall(fsc->name); if (sc) - fsc.nargs = sc->nargs; + fsc->nargs = sc->nargs; else { #if DEBUG fprintf(trussinfo->outfile, "unknown syscall %s -- setting " - "args to %d\n", fsc.name, nargs); + "args to %d\n", fsc->name, nargs); #endif - fsc.nargs = nargs; + fsc->nargs = nargs; } - fsc.s_args = calloc(1, (1 + fsc.nargs) * sizeof(char *)); - fsc.sc = sc; + fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *)); + fsc->sc = sc; /* * At this point, we set up the system call arguments. @@ -230,19 +239,19 @@ mips_syscall_entry(struct trussinfo *trussinfo, in * passed in *and* out, however. */ - if (fsc.name) { + if (fsc->name) { #if DEBUG - fprintf(stderr, "syscall %s(", fsc.name); + fprintf(stderr, "syscall %s(", fsc->name); #endif - for (i = 0; i < fsc.nargs; i++) { + for (i = 0; i < fsc->nargs; i++) { #if DEBUG fprintf(stderr, "0x%x%s", sc ? - fsc.args[sc->args[i].offset] : fsc.args[i], - i < (fsc.nargs - 1) ? "," : ""); + fsc->args[sc->args[i].offset] : fsc->args[i], + i < (fsc->nargs - 1) ? "," : ""); #endif if (sc && !(sc->args[i].type & OUT)) { - fsc.s_args[i] = print_arg(&sc->args[i], - fsc.args, 0, trussinfo); + fsc->s_args[i] = print_arg(&sc->args[i], + fsc->args, 0, trussinfo); } } #if DEBUG @@ -254,30 +263,29 @@ mips_syscall_entry(struct trussinfo *trussinfo, in fprintf(trussinfo->outfile, "\n"); #endif - if (fsc.name != NULL && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) { + if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) { /* * XXX * This could be done in a more general * manner but it still wouldn't be very pretty. */ - if (strcmp(fsc.name, "execve") == 0) { + if (strcmp(fsc->name, "execve") == 0) { if ((trussinfo->flags & EXECVEARGS) == 0) { - if (fsc.s_args[1]) { - free(fsc.s_args[1]); - fsc.s_args[1] = NULL; + if (fsc->s_args[1]) { + free(fsc->s_args[1]); + fsc->s_args[1] = NULL; } } if ((trussinfo->flags & EXECVEENVS) == 0) { - if (fsc.s_args[2]) { - free(fsc.s_args[2]); - fsc.s_args[2] = NULL; + if (fsc->s_args[2]) { + free(fsc->s_args[2]); + fsc->s_args[2] = NULL; } } } } - - return; + trussinfo->curthread->fsc = fsc; } /* @@ -291,16 +299,18 @@ long mips_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused) { struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; + lwpid_t tid; long retval; int errorp, i; - if (fsc.name == NULL) + if (trussinfo->curthread->fsc == NULL) return (-1); - cpid = trussinfo->curthread->tid; + tid = trussinfo->curthread->tid; - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "\n"); return (-1); } @@ -313,10 +323,11 @@ mips_syscall_exit(struct trussinfo *trussinfo, int * stand some significant cleaning. */ - sc = fsc.sc; + fsc = trussinfo->curthread->fsc; + sc = fsc->sc; if (!sc) { - for (i = 0; i < fsc.nargs; i++) - asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]); + for (i = 0; i < fsc->nargs; i++) + asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]); } else { /* * Here, we only look for arguments that have OUT masked in -- @@ -331,18 +342,18 @@ mips_syscall_exit(struct trussinfo *trussinfo, int */ if (errorp) { asprintf(&temp, "0x%lx", - fsc.args[sc->args[i].offset]); + fsc->args[sc->args[i].offset]); } else { temp = print_arg(&sc->args[i], - fsc.args, retval, trussinfo); + fsc->args, retval, trussinfo); } - fsc.s_args[i] = temp; + fsc->s_args[i] = temp; } } } - if (fsc.name != NULL && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) + if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) trussinfo->curthread->in_syscall = 1; /* @@ -350,9 +361,9 @@ mips_syscall_exit(struct trussinfo *trussinfo, int * but that complicates things considerably. */ - print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp, - retval, fsc.sc); - clear_fsc(); + print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp, + retval, fsc->sc); + free_fsc(fsc); return (retval); } Index: usr.bin/truss/truss.h =================================================================== --- usr.bin/truss/truss.h (revision 240005) +++ usr.bin/truss/truss.h (working copy) @@ -41,6 +41,9 @@ struct threadinfo lwpid_t tid; int in_syscall; int in_fork; + void *fsc; + struct timespec before; + struct timespec after; }; struct trussinfo @@ -53,8 +56,6 @@ struct trussinfo FILE *outfile; struct timespec start_time; - struct timespec before; - struct timespec after; struct threadinfo *curthread; Index: usr.bin/truss/i386-linux.c =================================================================== --- usr.bin/truss/i386-linux.c (revision 240005) +++ usr.bin/truss/i386-linux.c (working copy) @@ -59,8 +59,6 @@ static const char rcsid[] = #include "syscall.h" #include "extern.h" -static int cpid = -1; - #include "linux_syscalls.h" static int nsyscalls = @@ -75,28 +73,34 @@ static int nsyscalls = * 'struct syscall' describes the system call; it may be NULL, however, * if we don't know about this particular system call yet. */ -static struct linux_syscall { +struct linux_syscall { struct syscall *sc; const char *name; int number; unsigned long args[5]; int nargs; /* number of arguments -- *not* number of words! */ char **s_args; /* the printable arguments */ -} fsc; +}; +static struct linux_syscall * +alloc_fsc(void) +{ + + return (malloc(sizeof(struct linux_syscall))); +} + /* Clear up and free parts of the fsc structure. */ -static __inline void -clear_fsc(void) +static void +free_fsc(struct linux_syscall *fsc) { int i; - if (fsc.s_args) { - for (i = 0; i < fsc.nargs; i++) - if (fsc.s_args[i]) - free(fsc.s_args[i]); - free(fsc.s_args); + if (fsc->s_args) { + for (i = 0; i < fsc->nargs; i++) + if (fsc->s_args[i]) + free(fsc->s_args[i]); + free(fsc->s_args); } - memset(&fsc, 0, sizeof(fsc)); } /* @@ -110,31 +114,36 @@ void i386_linux_syscall_entry(struct trussinfo *trussinfo, int nargs) { struct reg regs; + struct linux_syscall *fsc; struct syscall *sc; + lwpid_t tid; int i, syscall_num; - clear_fsc(); + tid = trussinfo->curthread->tid; - cpid = trussinfo->curthread->tid; - - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return; } syscall_num = regs.r_eax; - fsc.number = syscall_num; - fsc.name = (syscall_num < 0 || syscall_num >= nsyscalls) ? + fsc = alloc_fsc(); + if (fsc == NULL) { + fprintf(trussinfo->outfile, "-- CANNOT ALLOCATE MEMORY --\n"); + return; + } + fsc->number = syscall_num; + fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ? NULL : linux_syscallnames[syscall_num]; - if (!fsc.name) { + if (!fsc->name) { fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num); } - if (fsc.name && (trussinfo->flags & FOLLOWFORKS) && - (strcmp(fsc.name, "linux_fork") == 0 || - strcmp(fsc.name, "linux_vfork") == 0)) + if (fsc->name && (trussinfo->flags & FOLLOWFORKS) && + (strcmp(fsc->name, "linux_fork") == 0 || + strcmp(fsc->name, "linux_vfork") == 0)) trussinfo->curthread->in_fork = 1; if (nargs == 0) @@ -148,25 +157,25 @@ i386_linux_syscall_entry(struct trussinfo *trussin * that have more than five arguments? */ - fsc.args[0] = regs.r_ebx; - fsc.args[1] = regs.r_ecx; - fsc.args[2] = regs.r_edx; - fsc.args[3] = regs.r_esi; - fsc.args[4] = regs.r_edi; + fsc->args[0] = regs.r_ebx; + fsc->args[1] = regs.r_ecx; + fsc->args[2] = regs.r_edx; + fsc->args[3] = regs.r_esi; + fsc->args[4] = regs.r_edi; - sc = get_syscall(fsc.name); + sc = get_syscall(fsc->name); if (sc) - fsc.nargs = sc->nargs; + fsc->nargs = sc->nargs; else { #if DEBUG fprintf(trussinfo->outfile, "unknown syscall %s -- setting " - "args to %d\n", fsc.name, nargs); + "args to %d\n", fsc->name, nargs); #endif - fsc.nargs = nargs; + fsc->nargs = nargs; } - fsc.s_args = calloc(1, (1 + fsc.nargs) * sizeof(char *)); - fsc.sc = sc; + fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *)); + fsc->sc = sc; /* * At this point, we set up the system call arguments. @@ -176,19 +185,19 @@ i386_linux_syscall_entry(struct trussinfo *trussin * passed in *and* out, however. */ - if (fsc.name) { + if (fsc->name) { #if DEBUG - fprintf(stderr, "syscall %s(", fsc.name); + fprintf(stderr, "syscall %s(", fsc->name); #endif - for (i = 0; i < fsc.nargs; i++) { + for (i = 0; i < fsc->nargs; i++) { #if DEBUG fprintf(stderr, "0x%x%s", sc ? - fsc.args[sc->args[i].offset] : fsc.args[i], - i < (fsc.nargs - 1) ? "," : ""); + fsc->args[sc->args[i].offset] : fsc->args[i], + i < (fsc->nargs - 1) ? "," : ""); #endif if (sc && !(sc->args[i].type & OUT)) { - fsc.s_args[i] = print_arg(&sc->args[i], - fsc.args, 0, trussinfo); + fsc->s_args[i] = print_arg(&sc->args[i], + fsc->args, 0, trussinfo); } } #if DEBUG @@ -200,30 +209,29 @@ i386_linux_syscall_entry(struct trussinfo *trussin fprintf(trussinfo->outfile, "\n"); #endif - if (fsc.name != NULL && (strcmp(fsc.name, "linux_execve") == 0 || - strcmp(fsc.name, "exit") == 0)) { + if (fsc->name != NULL && (strcmp(fsc->name, "linux_execve") == 0 || + strcmp(fsc->name, "exit") == 0)) { /* * XXX * This could be done in a more general * manner but it still wouldn't be very pretty. */ - if (strcmp(fsc.name, "linux_execve") == 0) { + if (strcmp(fsc->name, "linux_execve") == 0) { if ((trussinfo->flags & EXECVEARGS) == 0) { - if (fsc.s_args[1]) { - free(fsc.s_args[1]); - fsc.s_args[1] = NULL; + if (fsc->s_args[1]) { + free(fsc->s_args[1]); + fsc->s_args[1] = NULL; } } if ((trussinfo->flags & EXECVEENVS) == 0) { - if (fsc.s_args[2]) { - free(fsc.s_args[2]); - fsc.s_args[2] = NULL; + if (fsc->s_args[2]) { + free(fsc->s_args[2]); + fsc->s_args[2] = NULL; } } } } - - return; + trussinfo->curthread->fsc = fsc; } /* @@ -245,16 +253,18 @@ long i386_linux_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused) { struct reg regs; + struct linux_syscall *fsc; struct syscall *sc; + lwpid_t tid; long retval; int errorp, i; - if (fsc.name == NULL) + if (trussinfo->curthread->fsc == NULL) return (-1); - cpid = trussinfo->curthread->tid; + tid = trussinfo->curthread->tid; - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return (-1); } @@ -267,10 +277,11 @@ i386_linux_syscall_exit(struct trussinfo *trussinf * stand some significant cleaning. */ - sc = fsc.sc; + fsc = trussinfo->curthread->fsc; + sc = fsc->sc; if (!sc) { - for (i = 0; i < fsc.nargs; i++) - asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]); + for (i = 0; i < fsc->nargs; i++) + asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]); } else { /* * Here, we only look for arguments that have OUT masked in -- @@ -285,12 +296,12 @@ i386_linux_syscall_exit(struct trussinfo *trussinf */ if (errorp) { asprintf(&temp, "0x%lx", - fsc.args[sc->args[i].offset]); + fsc->args[sc->args[i].offset]); } else { temp = print_arg(&sc->args[i], - fsc.args, retval, trussinfo); + fsc->args, retval, trussinfo); } - fsc.s_args[i] = temp; + fsc->s_args[i] = temp; } } } @@ -307,13 +318,13 @@ i386_linux_syscall_exit(struct trussinfo *trussinf } } - if (fsc.name != NULL && (strcmp(fsc.name, "linux_execve") == 0 || - strcmp(fsc.name, "exit") == 0)) + if (fsc->name != NULL && (strcmp(fsc->name, "linux_execve") == 0 || + strcmp(fsc->name, "exit") == 0)) trussinfo->curthread->in_syscall = 1; - print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp, - errorp ? i : retval, fsc.sc); - clear_fsc(); + print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp, + errorp ? i : retval, fsc->sc); + free_fsc(fsc); return (retval); } Index: usr.bin/truss/amd64-fbsd.c =================================================================== --- usr.bin/truss/amd64-fbsd.c (revision 240005) +++ usr.bin/truss/amd64-fbsd.c (working copy) @@ -62,8 +62,6 @@ static const char rcsid[] = #include "syscall.h" #include "extern.h" -static int cpid = -1; - #include "syscalls.h" static int nsyscalls = sizeof(syscallnames) / sizeof(syscallnames[0]); @@ -77,30 +75,36 @@ static int nsyscalls = sizeof(syscallnames) / size * 'struct syscall' describes the system call; it may be NULL, however, * if we don't know about this particular system call yet. */ -static struct freebsd_syscall { +struct freebsd_syscall { struct syscall *sc; const char *name; int number; unsigned long *args; int nargs; /* number of arguments -- *not* number of words! */ char **s_args; /* the printable arguments */ -} fsc; +}; +static struct freebsd_syscall * +alloc_fsc(void) +{ + + return (malloc(sizeof(struct freebsd_syscall))); +} + /* Clear up and free parts of the fsc structure. */ -static __inline void -clear_fsc(void) +static void +free_fsc(struct freebsd_syscall *fsc) { int i; - if (fsc.args) - free(fsc.args); - if (fsc.s_args) { - for (i = 0; i < fsc.nargs; i++) - if (fsc.s_args[i]) - free(fsc.s_args[i]); - free(fsc.s_args); + if (fsc->args) + free(fsc->args); + if (fsc->s_args) { + for (i = 0; i < fsc->nargs; i++) + if (fsc->s_args[i]) + free(fsc->s_args[i]); + free(fsc->s_args); } - memset(&fsc, 0, sizeof(fsc)); } /* @@ -115,14 +119,14 @@ amd64_syscall_entry(struct trussinfo *trussinfo, i { struct ptrace_io_desc iorequest; struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; + lwpid_t tid; int i, reg, syscall_num; - clear_fsc(); + tid = trussinfo->curthread->tid; - cpid = trussinfo->curthread->tid; - - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return; } @@ -142,57 +146,62 @@ amd64_syscall_entry(struct trussinfo *trussinfo, i break; } - fsc.number = syscall_num; - fsc.name = (syscall_num < 0 || syscall_num >= nsyscalls) ? + fsc = alloc_fsc(); + if (fsc == NULL) { + fprintf(trussinfo->outfile, "-- CANNOT ALLOCATE MEMORY --\n"); + return; + } + fsc->number = syscall_num; + fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ? NULL : syscallnames[syscall_num]; - if (!fsc.name) { + if (!fsc->name) { fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num); } - if (fsc.name && (trussinfo->flags & FOLLOWFORKS) && - (strcmp(fsc.name, "fork") == 0 || - strcmp(fsc.name, "rfork") == 0|| - strcmp(fsc.name, "vfork") == 0)) + if (fsc->name && (trussinfo->flags & FOLLOWFORKS) && + (strcmp(fsc->name, "fork") == 0 || + strcmp(fsc->name, "rfork") == 0 || + strcmp(fsc->name, "vfork") == 0)) trussinfo->curthread->in_fork = 1; if (nargs == 0) return; - fsc.args = malloc((1 + nargs) * sizeof(unsigned long)); + fsc->args = malloc((1 + nargs) * sizeof(unsigned long)); for (i = 0; i < nargs && reg < 6; i++, reg++) { switch (reg) { - case 0: fsc.args[i] = regs.r_rdi; break; - case 1: fsc.args[i] = regs.r_rsi; break; - case 2: fsc.args[i] = regs.r_rdx; break; - case 3: fsc.args[i] = regs.r_rcx; break; - case 4: fsc.args[i] = regs.r_r8; break; - case 5: fsc.args[i] = regs.r_r9; break; + case 0: fsc->args[i] = regs.r_rdi; break; + case 1: fsc->args[i] = regs.r_rsi; break; + case 2: fsc->args[i] = regs.r_rdx; break; + case 3: fsc->args[i] = regs.r_rcx; break; + case 4: fsc->args[i] = regs.r_r8; break; + case 5: fsc->args[i] = regs.r_r9; break; } } if (nargs > i) { iorequest.piod_op = PIOD_READ_D; iorequest.piod_offs = (void *)(regs.r_rsp + sizeof(register_t)); - iorequest.piod_addr = &fsc.args[i]; + iorequest.piod_addr = &fsc->args[i]; iorequest.piod_len = (nargs - i) * sizeof(register_t); - ptrace(PT_IO, cpid, (caddr_t)&iorequest, 0); + ptrace(PT_IO, tid, (caddr_t)&iorequest, 0); if (iorequest.piod_len == 0) return; } - sc = get_syscall(fsc.name); + sc = get_syscall(fsc->name); if (sc) - fsc.nargs = sc->nargs; + fsc->nargs = sc->nargs; else { #if DEBUG fprintf(trussinfo->outfile, "unknown syscall %s -- setting " - "args to %d\n", fsc.name, nargs); + "args to %d\n", fsc->name, nargs); #endif - fsc.nargs = nargs; + fsc->nargs = nargs; } - fsc.s_args = calloc(1, (1 + fsc.nargs) * sizeof(char *)); - fsc.sc = sc; + fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *)); + fsc->sc = sc; /* * At this point, we set up the system call arguments. @@ -202,19 +211,19 @@ amd64_syscall_entry(struct trussinfo *trussinfo, i * passed in *and* out, however. */ - if (fsc.name) { + if (fsc->name) { #if DEBUG - fprintf(stderr, "syscall %s(", fsc.name); + fprintf(stderr, "syscall %s(", fsc->name); #endif - for (i = 0; i < fsc.nargs; i++) { + for (i = 0; i < fsc->nargs; i++) { #if DEBUG fprintf(stderr, "0x%lx%s", sc ? - fsc.args[sc->args[i].offset] : fsc.args[i], - i < (fsc.nargs - 1) ? "," : ""); + fsc->args[sc->args[i].offset] : fsc->args[i], + i < (fsc->nargs - 1) ? "," : ""); #endif if (sc && !(sc->args[i].type & OUT)) { - fsc.s_args[i] = print_arg(&sc->args[i], - fsc.args, 0, trussinfo); + fsc->s_args[i] = print_arg(&sc->args[i], + fsc->args, 0, trussinfo); } } #if DEBUG @@ -226,30 +235,29 @@ amd64_syscall_entry(struct trussinfo *trussinfo, i fprintf(trussinfo->outfile, "\n"); #endif - if (fsc.name != NULL && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) { + if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) { /* * XXX * This could be done in a more general * manner but it still wouldn't be very pretty. */ - if (strcmp(fsc.name, "execve") == 0) { + if (strcmp(fsc->name, "execve") == 0) { if ((trussinfo->flags & EXECVEARGS) == 0) { - if (fsc.s_args[1]) { - free(fsc.s_args[1]); - fsc.s_args[1] = NULL; + if (fsc->s_args[1]) { + free(fsc->s_args[1]); + fsc->s_args[1] = NULL; } } if ((trussinfo->flags & EXECVEENVS) == 0) { - if (fsc.s_args[2]) { - free(fsc.s_args[2]); - fsc.s_args[2] = NULL; + if (fsc->s_args[2]) { + free(fsc->s_args[2]); + fsc->s_args[2] = NULL; } } } } - - return; + trussinfo->curthread->fsc = fsc; } /* @@ -263,16 +271,18 @@ long amd64_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused) { struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; + lwpid_t tid; long retval; int errorp, i; - if (fsc.name == NULL) + if (trussinfo->curthread->fsc == NULL) return (-1); - cpid = trussinfo->curthread->tid; + tid = trussinfo->curthread->tid; - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return (-1); } @@ -285,10 +295,11 @@ amd64_syscall_exit(struct trussinfo *trussinfo, in * stand some significant cleaning. */ - sc = fsc.sc; + fsc = trussinfo->curthread->fsc; + sc = fsc->sc; if (!sc) { - for (i = 0; i < fsc.nargs; i++) - asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]); + for (i = 0; i < fsc->nargs; i++) + asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]); } else { /* * Here, we only look for arguments that have OUT masked in -- @@ -303,18 +314,18 @@ amd64_syscall_exit(struct trussinfo *trussinfo, in */ if (errorp) { asprintf(&temp, "0x%lx", - fsc.args[sc->args[i].offset]); + fsc->args[sc->args[i].offset]); } else { temp = print_arg(&sc->args[i], - fsc.args, retval, trussinfo); + fsc->args, retval, trussinfo); } - fsc.s_args[i] = temp; + fsc->s_args[i] = temp; } } } - if (fsc.name != NULL && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) + if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) trussinfo->curthread->in_syscall = 1; /* @@ -322,9 +333,9 @@ amd64_syscall_exit(struct trussinfo *trussinfo, in * but that complicates things considerably. */ - print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp, - retval, fsc.sc); - clear_fsc(); + print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp, + retval, fsc->sc); + free_fsc(fsc); return (retval); } Index: usr.bin/truss/powerpc-fbsd.c =================================================================== --- usr.bin/truss/powerpc-fbsd.c (revision 240005) +++ usr.bin/truss/powerpc-fbsd.c (working copy) @@ -61,8 +61,6 @@ static const char rcsid[] = #include "syscall.h" #include "extern.h" -static int cpid = -1; - #ifdef __powerpc64__ /* 32-bit compatibility */ #include "freebsd32_syscalls.h" #define syscallnames freebsd32_syscallnames @@ -81,30 +79,36 @@ static int nsyscalls = sizeof(syscallnames) / size * 'struct syscall' describes the system call; it may be NULL, however, * if we don't know about this particular system call yet. */ -static struct freebsd_syscall { +struct freebsd_syscall { struct syscall *sc; const char *name; int number; unsigned long *args; int nargs; /* number of arguments -- *not* number of words! */ char **s_args; /* the printable arguments */ -} fsc; +}; +static struct freebsd_syscall * +alloc_fsc(void) +{ + + return (malloc(sizeof(struct freebsd_syscall))); +} + /* Clear up and free parts of the fsc structure. */ -static __inline void -clear_fsc(void) +static void +free_fsc(struct freebsd_syscall *fsc) { int i; - if (fsc.args) - free(fsc.args); - if (fsc.s_args) { - for (i = 0; i < fsc.nargs; i++) - if (fsc.s_args[i]) - free(fsc.s_args[i]); - free(fsc.s_args); + if (fsc->args) + free(fsc->args); + if (fsc->s_args) { + for (i = 0; i < fsc->nargs; i++) + if (fsc->s_args[i]) + free(fsc->s_args[i]); + free(fsc->s_args); } - memset(&fsc, 0, sizeof(fsc)); } /* @@ -119,18 +123,18 @@ powerpc_syscall_entry(struct trussinfo *trussinfo, { struct ptrace_io_desc iorequest; struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; void *args; + lwpid_t tid; int i, regargs, syscall_num; /* Account for a 64-bit argument with corresponding alignment. */ nargs += 2; - clear_fsc(); + tid = trussinfo->curthread->tid; - cpid = trussinfo->curthread->tid; - - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return; } @@ -153,51 +157,56 @@ powerpc_syscall_entry(struct trussinfo *trussinfo, syscall_num = regs.fixreg[4]; } - fsc.number = syscall_num; - fsc.name = (syscall_num < 0 || syscall_num >= nsyscalls) ? + fsc = alloc_fsc(); + if (fsc == NULL) { + fprintf(trussinfo->outfile, "-- CANNOT ALLOCATE MEMORY --\n"); + return; + } + fsc->number = syscall_num; + fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ? NULL : syscallnames[syscall_num]; - if (!fsc.name) { + if (!fsc->name) { fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num); } - if (fsc.name && (trussinfo->flags & FOLLOWFORKS) && - (strcmp(fsc.name, "fork") == 0 || - strcmp(fsc.name, "rfork") == 0 || - strcmp(fsc.name, "vfork") == 0)) + if (fsc->name && (trussinfo->flags & FOLLOWFORKS) && + (strcmp(fsc->name, "fork") == 0 || + strcmp(fsc->name, "rfork") == 0 || + strcmp(fsc->name, "vfork") == 0)) trussinfo->curthread->in_fork = 1; if (nargs == 0) return; - fsc.args = malloc((1 + nargs) * sizeof(unsigned long)); + fsc->args = malloc((1 + nargs) * sizeof(unsigned long)); if (nargs > regargs) { - memmove(&fsc.args[0], args, regargs * sizeof(fsc.args[0])); + memmove(&fsc->args[0], args, regargs * sizeof(fsc->args[0])); iorequest.piod_op = PIOD_READ_D; iorequest.piod_offs = (void *)(regs.fixreg[1] + 8); - iorequest.piod_addr = &fsc.args[regargs]; - iorequest.piod_len = (nargs - regargs) * sizeof(fsc.args[0]); - ptrace(PT_IO, cpid, (caddr_t)&iorequest, 0); + iorequest.piod_addr = &fsc->args[regargs]; + iorequest.piod_len = (nargs - regargs) * sizeof(fsc->args[0]); + ptrace(PT_IO, tid, (caddr_t)&iorequest, 0); if (iorequest.piod_len == 0) return; } else - memmove(&fsc.args[0], args, nargs * sizeof(fsc.args[0])); + memmove(&fsc->args[0], args, nargs * sizeof(fsc->args[0])); - sc = get_syscall(fsc.name); + sc = get_syscall(fsc->name); if (sc) - fsc.nargs = sc->nargs; + fsc->nargs = sc->nargs; else { #if DEBUG fprintf(trussinfo->outfile, "unknown syscall %s -- setting " - "args to %d\n", fsc.name, nargs); + "args to %d\n", fsc->name, nargs); #endif - fsc.nargs = nargs; + fsc->nargs = nargs; } - fsc.s_args = calloc(1, (1 + fsc.nargs) * sizeof(char *)); - fsc.sc = sc; + fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *)); + fsc->sc = sc; /* * At this point, we set up the system call arguments. @@ -207,19 +216,19 @@ powerpc_syscall_entry(struct trussinfo *trussinfo, * passed in *and* out, however. */ - if (fsc.name) { + if (fsc->name) { #if DEBUG - fprintf(stderr, "syscall %s(", fsc.name); + fprintf(stderr, "syscall %s(", fsc->name); #endif - for (i = 0; i < fsc.nargs; i++) { + for (i = 0; i < fsc->nargs; i++) { #if DEBUG fprintf(stderr, "0x%x%s", sc ? - fsc.args[sc->args[i].offset] : fsc.args[i], - i < (fsc.nargs - 1) ? "," : ""); + fsc->args[sc->args[i].offset] : fsc->args[i], + i < (fsc->nargs - 1) ? "," : ""); #endif if (sc && !(sc->args[i].type & OUT)) { - fsc.s_args[i] = print_arg(&sc->args[i], - fsc.args, 0, trussinfo); + fsc->s_args[i] = print_arg(&sc->args[i], + fsc->args, 0, trussinfo); } } #if DEBUG @@ -231,30 +240,29 @@ powerpc_syscall_entry(struct trussinfo *trussinfo, fprintf(trussinfo->outfile, "\n"); #endif - if (fsc.name && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) { + if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) { /* * XXX * This could be done in a more general * manner but it still wouldn't be very pretty. */ - if (strcmp(fsc.name, "execve") == 0) { + if (strcmp(fsc->name, "execve") == 0) { if ((trussinfo->flags & EXECVEARGS) == 0) { - if (fsc.s_args[1]) { - free(fsc.s_args[1]); - fsc.s_args[1] = NULL; + if (fsc->s_args[1]) { + free(fsc->s_args[1]); + fsc->s_args[1] = NULL; } } if ((trussinfo->flags & EXECVEENVS) == 0) { - if (fsc.s_args[2]) { - free(fsc.s_args[2]); - fsc.s_args[2] = NULL; + if (fsc->s_args[2]) { + free(fsc->s_args[2]); + fsc->s_args[2] = NULL; } } } } - - return; + trussinfo->curthread->fsc = fsc; } /* @@ -268,16 +276,18 @@ long powerpc_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused) { struct reg regs; + struct freebsd_syscall *fsc; struct syscall *sc; + lwpid_t tid; long retval; int errorp, i; - if (fsc.name == NULL) + if (trussinfo->curthread->fsc == NULL) return (-1); - cpid = trussinfo->curthread->tid; + tid = trussinfo->curthread->tid; - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "\n"); return (-1); } @@ -290,10 +300,11 @@ powerpc_syscall_exit(struct trussinfo *trussinfo, * stand some significant cleaning. */ - sc = fsc.sc; + fsc = trussinfo->curthread->fsc; + sc = fsc->sc; if (!sc) { - for (i = 0; i < fsc.nargs; i++) - asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]); + for (i = 0; i < fsc->nargs; i++) + asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]); } else { /* * On 32-bit big-endian, the low word of a 64-bit return is @@ -316,18 +327,18 @@ powerpc_syscall_exit(struct trussinfo *trussinfo, */ if (errorp) { asprintf(&temp, "0x%lx", - fsc.args[sc->args[i].offset]); + fsc->args[sc->args[i].offset]); } else { temp = print_arg(&sc->args[i], - fsc.args, retval, trussinfo); + fsc->args, retval, trussinfo); } - fsc.s_args[i] = temp; + fsc->s_args[i] = temp; } } } - if (fsc.name != NULL && (strcmp(fsc.name, "execve") == 0 || - strcmp(fsc.name, "exit") == 0)) + if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 || + strcmp(fsc->name, "exit") == 0)) trussinfo->curthread->in_syscall = 1; /* @@ -335,9 +346,9 @@ powerpc_syscall_exit(struct trussinfo *trussinfo, * but that complicates things considerably. */ - print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp, - retval, fsc.sc); - clear_fsc(); + print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp, + retval, fsc->sc); + free_fsc(fsc); return (retval); } Index: usr.bin/truss/amd64-fbsd32.c =================================================================== --- usr.bin/truss/amd64-fbsd32.c (revision 240005) +++ usr.bin/truss/amd64-fbsd32.c (working copy) @@ -62,8 +62,6 @@ static const char rcsid[] = #include "syscall.h" #include "extern.h" -static int cpid = -1; - #include "freebsd32_syscalls.h" static int nsyscalls = sizeof(freebsd32_syscallnames) / @@ -78,7 +76,7 @@ static int nsyscalls = sizeof(freebsd32_syscallnam * 'struct syscall' describes the system call; it may be NULL, however, * if we don't know about this particular system call yet. */ -static struct freebsd32_syscall { +struct freebsd32_syscall { struct syscall *sc; const char *name; int number; @@ -86,25 +84,31 @@ static int nsyscalls = sizeof(freebsd32_syscallnam unsigned int *args32; int nargs; /* number of arguments -- *not* number of words! */ char **s_args; /* the printable arguments */ -} fsc; +}; +static struct freebsd32_syscall * +alloc_fsc(void) +{ + + return (malloc(sizeof(struct freebsd32_syscall))); +} + /* Clear up and free parts of the fsc structure. */ -static __inline void -clear_fsc(void) +static void +free_fsc(struct freebsd32_syscall *fsc) { int i; - if (fsc.args) - free(fsc.args); - if (fsc.args32) - free(fsc.args32); - if (fsc.s_args) { - for (i = 0; i < fsc.nargs; i++) - if (fsc.s_args[i]) - free(fsc.s_args[i]); - free(fsc.s_args); + if (fsc->args) + free(fsc->args); + if (fsc->args32) + free(fsc->args32); + if (fsc->s_args) { + for (i = 0; i < fsc->nargs; i++) + if (fsc->s_args[i]) + free(fsc->s_args[i]); + free(fsc->s_args); } - memset(&fsc, 0, sizeof(fsc)); } /* @@ -119,15 +123,15 @@ amd64_fbsd32_syscall_entry(struct trussinfo *truss { struct ptrace_io_desc iorequest; struct reg regs; + struct freebsd32_syscall *fsc; struct syscall *sc; + lwpid_t tid; unsigned long parm_offset; int i, syscall_num; - clear_fsc(); + tid = trussinfo->curthread->tid; - cpid = trussinfo->curthread->tid; - - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return; } @@ -141,60 +145,65 @@ amd64_fbsd32_syscall_entry(struct trussinfo *truss syscall_num = regs.r_rax; switch (syscall_num) { case SYS_syscall: - syscall_num = ptrace(PT_READ_D, cpid, (caddr_t)parm_offset, 0); + syscall_num = ptrace(PT_READ_D, tid, (caddr_t)parm_offset, 0); parm_offset += sizeof(int); break; case SYS___syscall: - syscall_num = ptrace(PT_READ_D, cpid, (caddr_t)parm_offset, 0); + syscall_num = ptrace(PT_READ_D, tid, (caddr_t)parm_offset, 0); parm_offset += sizeof(quad_t); break; } - fsc.number = syscall_num; - fsc.name = (syscall_num < 0 || syscall_num >= nsyscalls) ? + fsc = alloc_fsc(); + if (fsc == NULL) { + fprintf(trussinfo->outfile, "-- CANNOT ALLOCATE MEMORY --\n"); + return; + } + fsc->number = syscall_num; + fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ? NULL : freebsd32_syscallnames[syscall_num]; - if (!fsc.name) { + if (!fsc->name) { fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num); } - if (fsc.name && (trussinfo->flags & FOLLOWFORKS) && - (strcmp(fsc.name, "fork") == 0 || - strcmp(fsc.name, "rfork") == 0|| - strcmp(fsc.name, "vfork") == 0)) + if (fsc->name && (trussinfo->flags & FOLLOWFORKS) && + (strcmp(fsc->name, "fork") == 0 || + strcmp(fsc->name, "rfork") == 0 || + strcmp(fsc->name, "vfork") == 0)) trussinfo->curthread->in_fork = 1; if (nargs == 0) return; - fsc.args32 = malloc((1 + nargs) * sizeof(unsigned int)); + fsc->args32 = malloc((1 + nargs) * sizeof(unsigned int)); iorequest.piod_op = PIOD_READ_D; iorequest.piod_offs = (void *)parm_offset; - iorequest.piod_addr = fsc.args32; + iorequest.piod_addr = fsc->args32; iorequest.piod_len = (1 + nargs) * sizeof(unsigned int); - ptrace(PT_IO, cpid, (caddr_t)&iorequest, 0); + ptrace(PT_IO, tid, (caddr_t)&iorequest, 0); if (iorequest.piod_len == 0) return; - fsc.args = malloc((1 + nargs) * sizeof(unsigned long)); + fsc->args = malloc((1 + nargs) * sizeof(unsigned long)); for (i = 0; i < nargs + 1; i++) - fsc.args[i] = fsc.args32[i]; + fsc->args[i] = fsc->args32[i]; sc = NULL; - if (fsc.name) - sc = get_syscall(fsc.name); + if (fsc->name) + sc = get_syscall(fsc->name); if (sc) - fsc.nargs = sc->nargs; + fsc->nargs = sc->nargs; else { #if DEBUG fprintf(trussinfo->outfile, "unknown syscall %s -- setting " - "args to %d\n", fsc.name, nargs); + "args to %d\n", fsc->name, nargs); #endif - fsc.nargs = nargs; + fsc->nargs = nargs; } - fsc.s_args = calloc(1, (1 + fsc.nargs) * sizeof(char *)); - fsc.sc = sc; + fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *)); + fsc->sc = sc; /* * At this point, we set up the system call arguments. @@ -204,19 +213,19 @@ amd64_fbsd32_syscall_entry(struct trussinfo *truss * passed in *and* out, however. */ - if (fsc.name) { + if (fsc->name) { #if DEBUG - fprintf(stderr, "syscall %s(", fsc.name); + fprintf(stderr, "syscall %s(", fsc->name); #endif - for (i = 0; i < fsc.nargs; i++) { + for (i = 0; i < fsc->nargs; i++) { #if DEBUG fprintf(stderr, "0x%x%s", sc ? - fsc.args[sc->args[i].offset] : fsc.args[i], - i < (fsc.nargs - 1) ? "," : ""); + fsc->args[sc->args[i].offset] : fsc->args[i], + i < (fsc->nargs - 1) ? "," : ""); #endif if (sc && !(sc->args[i].type & OUT)) { - fsc.s_args[i] = print_arg(&sc->args[i], - fsc.args, 0, trussinfo); + fsc->s_args[i] = print_arg(&sc->args[i], + fsc->args, 0, trussinfo); } } #if DEBUG @@ -228,30 +237,29 @@ amd64_fbsd32_syscall_entry(struct trussinfo *truss fprintf(trussinfo->outfile, "\n"); #endif - if (fsc.name != NULL && (strcmp(fsc.name, "freebsd32_execve") == 0|| - strcmp(fsc.name, "exit") == 0)) { + if (fsc->name != NULL && (strcmp(fsc->name, "freebsd32_execve") == 0 || + strcmp(fsc->name, "exit") == 0)) { /* * XXX * This could be done in a more general * manner but it still wouldn't be very pretty. */ - if (strcmp(fsc.name, "freebsd32_execve") == 0) { + if (strcmp(fsc->name, "freebsd32_execve") == 0) { if ((trussinfo->flags & EXECVEARGS) == 0) { - if (fsc.s_args[1]) { - free(fsc.s_args[1]); - fsc.s_args[1] = NULL; + if (fsc->s_args[1]) { + free(fsc->s_args[1]); + fsc->s_args[1] = NULL; } } if ((trussinfo->flags & EXECVEENVS) == 0) { - if (fsc.s_args[2]) { - free(fsc.s_args[2]); - fsc.s_args[2] = NULL; + if (fsc->s_args[2]) { + free(fsc->s_args[2]); + fsc->s_args[2] = NULL; } } } } - - return; + trussinfo->curthread->fsc = fsc; } /* @@ -265,16 +273,18 @@ long amd64_fbsd32_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused) { struct reg regs; + struct freebsd32_syscall *fsc; struct syscall *sc; + lwpid_t tid; long retval; int errorp, i; - if (fsc.name == NULL) + if (trussinfo->curthread->fsc == NULL) return (-1); - cpid = trussinfo->curthread->tid; + tid = trussinfo->curthread->tid; - if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0) { + if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); return (-1); } @@ -287,10 +297,11 @@ amd64_fbsd32_syscall_exit(struct trussinfo *trussi * stand some significant cleaning. */ - sc = fsc.sc; + fsc = trussinfo->curthread->fsc; + sc = fsc->sc; if (!sc) { - for (i = 0; i < fsc.nargs; i++) - asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]); + for (i = 0; i < fsc->nargs; i++) + asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]); } else { /* * Here, we only look for arguments that have OUT masked in -- @@ -305,18 +316,18 @@ amd64_fbsd32_syscall_exit(struct trussinfo *trussi */ if (errorp) { asprintf(&temp, "0x%lx", - fsc.args[sc->args[i].offset]); + fsc->args[sc->args[i].offset]); } else { temp = print_arg(&sc->args[i], - fsc.args, retval, trussinfo); + fsc->args, retval, trussinfo); } - fsc.s_args[i] = temp; + fsc->s_args[i] = temp; } } } - if (fsc.name != NULL && (strcmp(fsc.name, "freebsd32_execve") == 0 || - strcmp(fsc.name, "exit") == 0)) + if (fsc->name != NULL && (strcmp(fsc->name, "freebsd32_execve") == 0 || + strcmp(fsc->name, "exit") == 0)) trussinfo->curthread->in_syscall = 1; /* @@ -324,9 +335,9 @@ amd64_fbsd32_syscall_exit(struct trussinfo *trussi * but that complicates things considerably. */ - print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp, - retval, fsc.sc); - clear_fsc(); + print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp, + retval, fsc->sc); + free_fsc(fsc); return (retval); }