home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3998 / syscall.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-09  |  3.2 KB  |  163 lines

  1. /*
  2.  * @(#)syscall.c    1.5 91/09/05
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <strings.h>
  7. #include <signal.h>
  8. #include <errno.h>
  9. #include <sys/types.h>
  10. #include <sys/param.h>
  11. #include <sys/ptrace.h>
  12. #include <sys/user.h>
  13.  
  14. #include "defs.h"
  15.  
  16. extern char *sys_errlist[];
  17. extern int sys_nerr;
  18.  
  19. #include "syscall.h"
  20. struct sysent {
  21.     int    nargs;
  22.     int    (*sys_func)();
  23.     char    *sys_name;
  24. } sysent[] = {
  25. #include "syscallent.h"
  26. };
  27. int nsyscall = sizeof sysent / sizeof sysent[0];
  28.  
  29. int
  30. syscall(tcp)
  31. struct tcb *tcp;
  32. {
  33.     int    i, sys_res;
  34.     int    pid = tcp->pid;
  35.     struct    user *u;
  36.  
  37. #if 0
  38.     errno = 0;
  39.     if ((scno = ptrace(PTRACE_PEEKUSER, pid, uoff(u_arg[7]), 0)) == -1) {
  40.         if (errno) {
  41.             perror("trace: ptrace(PTRACE_PEEKUSER)");
  42.             return -1;
  43.         }
  44.     }
  45. #endif
  46.     if (tcp->flags & TCB_INSYSCALL) {
  47.         u_int u_error;
  48.  
  49.         /* get error code from user struct */
  50.         errno = 0;
  51.         u_error = ptrace(PTRACE_PEEKUSER, pid, uoff(u_error), 0);
  52.         if (errno) {
  53.             perror("ptrace(PTRACE_PEEKUSER,..,u_error)");
  54.             return -1;
  55.         }
  56.         u_error >>= 24; /* u_error is a char */
  57.         tcp->u_error = u_error;
  58.  
  59.         /* get system call return value */
  60.         errno = 0;
  61.         tcp->u_rval = ptrace(PTRACE_PEEKUSER, pid,
  62.                     uoff(u_rval1), 0);
  63.         if (errno) {
  64.             perror("ptrace(PTRACE_PEEKUSER,..,u_rval1)");
  65.             return -1;
  66.         }
  67.         sys_res = (*sysent[tcp->scno].sys_func)(tcp);
  68.         if (u_error) {
  69.             fprintf(outf, ") = -1 (%s)\n",
  70.             (u_error<sys_nerr)?sys_errlist[u_error]:"???");
  71.         } else {
  72.             fprintf(outf, sys_res&RVAL_HEX?") = %#x":") = %d",
  73.                     tcp->u_rval);
  74.             fprintf(outf, sys_res&RVAL_STR?" (%s)\n":"\n",
  75.                     tcp->auxstr);
  76.         }
  77.         fflush(outf);
  78.         tcp->flags &= ~TCB_INSYSCALL;
  79.         return 0;
  80.     }
  81.     errno = 0;
  82.     if ((tcp->scno = ptrace(PTRACE_PEEKUSER, pid, uoff(u_arg[7]), 0)) == -1) {
  83.         if (errno) {
  84.             perror("trace: ptrace(PTRACE_PEEKUSER)");
  85.             return -1;
  86.         }
  87.     }
  88.     if (Nproc > 1)
  89.         fprintf(outf, " [pid %u]: ", pid);
  90.     if (tflag) {
  91.         time_t t = time(0);
  92.         char str[sizeof("HH:MM:SS")];
  93.  
  94.         strftime(str, sizeof(str), "%T", localtime(&t));
  95.         fprintf(outf, "%s ", str);
  96.     }
  97.     fprintf(outf, "%s(", sysent[tcp->scno].sys_name);
  98.     for (i=0; i<sysent[tcp->scno].nargs; i++) {
  99.         errno = 0;
  100.         tcp->u_args[i] = ptrace(PTRACE_PEEKUSER, pid,
  101.             uoff(u_arg[0]) + (i*sizeof(u->u_arg[0])), 0);
  102.         if (errno) {
  103.             perror("ptrace(PTRACE_PEEKUSER)");
  104.             return -1;
  105.         }
  106.     }
  107.     sys_res = (*sysent[tcp->scno].sys_func)(tcp);
  108.     fflush(outf);
  109.     tcp->flags |= TCB_INSYSCALL;
  110.     return sys_res;
  111. }
  112.  
  113. int
  114. printargs(tcp)
  115. struct tcb *tcp;
  116. {
  117.     int i, nargs = sysent[tcp->scno].nargs;
  118.  
  119.     if (tcp->flags & TCB_INSYSCALL)
  120.         return 0;
  121.  
  122.     for (i = 0; i < nargs; i++)
  123.         fprintf(outf, i>0?", %#x":"%#x", tcp->u_args[i]);
  124.     return 0;
  125. }
  126.  
  127. int
  128. getrval2(pid)
  129. {
  130.     int val;
  131.  
  132.     errno = 0;
  133.     val = ptrace(PTRACE_PEEKUSER, pid, uoff(u_rval2), 0);
  134.     if (errno) {
  135.         perror(" [ptrace(PTRACE_PEEKUSER,..,u_rval2)]");
  136.         return -1;
  137.     }
  138.     return val;
  139. }
  140.  
  141. /*
  142.  * Apparently, indirect system calls have already be converted by ptrace(2),
  143.  * so if you see "indir" this program has gone astray.
  144.  */
  145. int
  146. sys_indir(tcp)
  147. struct tcb *tcp;
  148. {
  149.     u_int i, scno, nargs;
  150.  
  151.     if (entering(tcp)) {
  152.         if ((scno = tcp->u_args[0]) > nsyscall) {
  153.             fprintf(stderr, "Bogus syscall: %u\n", scno);
  154.             return 0;
  155.         }
  156.         nargs = sysent[scno].nargs;
  157.         fprintf(outf, "%s", sysent[scno].sys_name);
  158.         for (i = 0; i < nargs; i++)
  159.             fprintf(outf, ", %#x", tcp->u_args[i+1]);
  160.     }
  161.     return 0;
  162. }
  163.