home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume3 / strace / part03 / file.c next >
Encoding:
C/C++ Source or Header  |  1992-03-02  |  11.8 KB  |  645 lines

  1. /*
  2.  * @(#)file.c    2.3 92/01/10
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/ptrace.h>
  9. #include <sys/file.h>
  10. #include <sys/dirent.h>
  11. #include <sys/stat.h>
  12. #include <ustat.h>
  13. #include <sys/vfs.h>
  14. #include <sys/time.h>
  15. #include <sys/asynch.h>
  16.  
  17. #include "defs.h"
  18.  
  19. static Xlat openmodes[] = {
  20.     {O_RDONLY, "RDONLY"},    /* read enable */
  21.     {O_WRONLY, "WRONLY"},    /* write enable */
  22.     {O_RDWR, "RDWR"},    /* read/write enable */
  23.     {_FNDELAY, "NDELAY"},    /* non blocking I/O (4.2 style) */
  24.     {_FAPPEND, "APPEND"},    /* append (writes guaranteed at the end) */
  25.     {_FMARK, "MARK"},    /* internal; mark during gc() */
  26.     {_FDEFER, "DEFER"},    /* internal; defer for next gc pass */
  27.     {_FASYNC, "ASYNC"},    /* signal pgrp when data ready */
  28.     {_FSHLOCK, "SHLOCK"},    /* BSD flock() shared lock present */
  29.     {_FEXLOCK, "EXLOCK"},    /* BSD flock() exclusive lock present */
  30.     {_FCREAT, "CREAT"},    /* open with file create */
  31.     {_FTRUNC, "TRUNC"},    /* open with truncation */
  32.     {_FEXCL, "EXCL"},    /* error on open if file exists */
  33.     {_FNBIO, "NBIO"},    /* non blocking I/O (sys5 style) */
  34.     {_FSYNC, "SYNC"},    /* do all writes synchronously */
  35.     {_FNONBLOCK, "NONBLOCK"},    /* non blocking I/O (POSIX style) */
  36.     {_FNOCTTY, "NOCTTY"},    /* don't assign a ctty on this open */
  37.     {0, NULL},
  38. };
  39.  
  40. int
  41. sys_open(tcp)
  42. struct tcb *tcp;
  43. {
  44.     if (entering(tcp)) {
  45.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  46.         tprintf(", ");
  47.         /* flags */
  48.         printxval(openmodes, tcp->u_args[1] & O_ACCMODE, "O_???");
  49.         (void)addflags(openmodes, tcp->u_args[1] & ~O_ACCMODE);
  50.         /* mode */
  51.         tprintf(", %o", tcp->u_args[2]);
  52.     }
  53.     return 0;
  54. }
  55.  
  56. int
  57. sys_creat(tcp)
  58. struct tcb *tcp;
  59. {
  60.     if (entering(tcp)) {
  61.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  62.         tprintf(", %o", tcp->u_args[1]);
  63.     }
  64.     return 0;
  65. }
  66.  
  67. int
  68. sys_umask(tcp)
  69. struct tcb *tcp;
  70. {
  71.     if (entering(tcp)) {
  72.         tprintf("%o", tcp->u_args[0]);
  73.     }
  74.     return RVAL_OCT;
  75. }
  76.  
  77. static Xlat whence[] = {
  78.     0,    "SET",
  79.     1,    "CUR",
  80.     2,    "END",
  81.     0,    NULL,
  82. };
  83.  
  84. int
  85. sys_lseek(tcp)
  86. struct tcb *tcp;
  87. {
  88.     if (entering(tcp)) {
  89.         tprintf("%u, %u, ", tcp->u_args[0], tcp->u_args[1]);
  90.         printxval(whence, tcp->u_args[2], "L_???");
  91.     }
  92.     return RVAL_UDEC;
  93. }
  94.  
  95. int
  96. sys_truncate(tcp)
  97. struct tcb *tcp;
  98. {
  99.     if (entering(tcp)) {
  100.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  101.         tprintf(", %u", tcp->u_args[1]);
  102.     }
  103.     return 0;
  104. }
  105.  
  106. int
  107. sys_ftruncate(tcp)
  108. struct tcb *tcp;
  109. {
  110.     if (entering(tcp)) {
  111.         tprintf("%u, %u", tcp->u_args[0], tcp->u_args[1]);
  112.     }
  113.     return 0;
  114. }
  115.  
  116. static Xlat accessmodes[] = {
  117.     {R_OK, "R_OK"},    /* read permission */
  118.     {W_OK, "W_OK"},    /* write permission */
  119.     {X_OK, "X_OK"},    /* execute permission */
  120.     {F_OK, "F_OK"},    /* search permission */
  121.     0, NULL,
  122. };
  123.  
  124. int
  125. sys_access(tcp)
  126. struct tcb *tcp;
  127. {
  128.     if (entering(tcp)) {
  129.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  130.         tprintf(", ");
  131.         if (tcp->u_args[1] == F_OK)
  132.             tprintf("F_OK");
  133.         else if (!printflags(accessmodes, tcp->u_args[1]))
  134.             tprintf("???_OK");
  135.     }
  136.     return 0;
  137. }
  138.  
  139. static Xlat statmodes[] = {
  140.     S_IFDIR,    "DIR",    /* directory */
  141.     S_IFCHR,    "CHR",    /* character special */
  142.     S_IFBLK,    "BLK",    /* block special */
  143.     S_IFREG,    "REG",    /* regular */
  144.     S_IFLNK,    "LNK",    /* symbolic link */
  145.     S_IFSOCK,    "SOCK",    /* socket */
  146.     S_IFIFO,    "FIFO",    /* fifo */
  147.     0,        NULL,
  148. };
  149.  
  150. /* several stats */
  151. static void
  152. printstat(pid, addr)
  153. {
  154.     struct stat statbuf;
  155.  
  156.     if (umove(pid, addr, sizeof statbuf, (char *)&statbuf) < 0) {
  157.         tprintf("(struct stat *)%#x", addr);
  158.         return;
  159.     }
  160.     if (!verbose) {
  161.         tprintf("[");
  162.         printxval(statmodes, (int)(statbuf.st_mode & S_IFMT), "S_???");
  163.         if (statbuf.st_mode & S_ISUID)
  164.             tprintf(" SUID");
  165.         if (statbuf.st_mode & S_ISGID)
  166.             tprintf(" SGID");
  167.         tprintf(" ino %u nlnks %d ...]",
  168.             statbuf.st_ino, statbuf.st_nlink);
  169.     } else {
  170.         tprintf(
  171.             "[dev %x,%x ino %u mode %o nlnks %d uid %u gid %u rdev %x,%x size %u atime %u mtime %u ctime %u blksiz %u blks %u]",
  172.             major(statbuf.st_dev), minor(statbuf.st_dev),
  173.             statbuf.st_mode, statbuf.st_ino, statbuf.st_nlink,
  174.             statbuf.st_uid, statbuf.st_gid,
  175.             major(statbuf.st_rdev), minor(statbuf.st_rdev),
  176.             statbuf.st_size,
  177.             statbuf.st_atime, statbuf.st_mtime, statbuf.st_ctime,
  178.             statbuf.st_blksize, statbuf.st_blocks);
  179.     }
  180. }
  181.  
  182. int
  183. sys_stat(tcp)
  184. struct tcb *tcp;
  185. {
  186.     if (entering(tcp)) {
  187.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  188.         tprintf(", ");
  189.     } else {
  190.         if (syserror(tcp))
  191.             tprintf("%#x", tcp->u_args[1]);
  192.         else
  193.             printstat(tcp->pid, tcp->u_args[1]);
  194.     }
  195.     return 0;
  196. }
  197.  
  198. int
  199. sys_fstat(tcp)
  200. struct tcb *tcp;
  201. {
  202.     if (entering(tcp)) {
  203.         tprintf("%u, ", tcp->u_args[0]);
  204.     } else {
  205.         if (syserror(tcp))
  206.             tprintf("%#x", tcp->u_args[1]);
  207.         else
  208.             printstat(tcp->pid, tcp->u_args[1]);
  209.     }
  210.     return 0;
  211. }
  212.  
  213. int
  214. sys_lstat(tcp)
  215. struct tcb *tcp;
  216. {
  217.     if (entering(tcp)) {
  218.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  219.         tprintf(", ");
  220.     } else {
  221.         if (syserror(tcp))
  222.             tprintf("%#x", tcp->u_args[1]);
  223.         else
  224.             printstat(tcp->pid, tcp->u_args[1]);
  225.     }
  226.     return 0;
  227. }
  228.  
  229. static void
  230. printstatfs(pid, addr)
  231. {
  232.     struct statfs statbuf;
  233.  
  234.     if (umove(pid, addr, sizeof statbuf, (char *)&statbuf) < 0) {
  235.         tprintf("[...]");
  236.         return;
  237.     }
  238.     tprintf(
  239.     "[type %x bsize %u blocks %u free %u files %u ffree %u fsid %u %u]",
  240.         statbuf.f_type, statbuf.f_bsize, statbuf.f_blocks,
  241.         statbuf.f_bfree, statbuf.f_files, statbuf.f_ffree,
  242.         statbuf.f_fsid.val[0], statbuf.f_fsid.val[1]);
  243. }
  244.  
  245. int
  246. sys_statfs(tcp)
  247. struct tcb *tcp;
  248. {
  249.     if (entering(tcp)) {
  250.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  251.         tprintf(", ");
  252.     } else {
  253.         if (syserror(tcp))
  254.             tprintf("%#x", tcp->u_args[1]);
  255.         else
  256.             printstatfs(tcp->pid, tcp->u_args[1]);
  257.     }
  258.     return 0;
  259. }
  260.  
  261. int
  262. sys_fstatfs(tcp)
  263. struct tcb *tcp;
  264. {
  265.     if (entering(tcp)) {
  266.         tprintf("%u, ", tcp->u_args[0]);
  267.     } else {
  268.         if (syserror(tcp))
  269.             tprintf("%#x", tcp->u_args[1]);
  270.         else
  271.             printstatfs(tcp->pid, tcp->u_args[1]);
  272.     }
  273.     return 0;
  274. }
  275.  
  276. int
  277. sys_ustat(tcp)
  278. struct tcb *tcp;
  279. {
  280.     struct ustat statbuf;
  281.  
  282.     if (entering(tcp)) {
  283.         tprintf("dev %u %u, ",
  284.                 major(tcp->u_args[0]), minor(tcp->u_args[0]));
  285.     } else {
  286.         if (syserror(tcp)) {
  287.             tprintf("%#x", tcp->u_args[1]);
  288.             return 0;
  289.         }
  290.         if (umove(tcp->pid, tcp->u_args[1],
  291.                 sizeof statbuf, (char *)&statbuf) < 0) {
  292.             tprintf("%#x", tcp->u_args[1]);
  293.             return 0;
  294.         }
  295.         tprintf("[tfree %u tinode %u fname %.*s fpack %.*s]",
  296.             statbuf.f_tfree, statbuf.f_tinode,
  297.             sizeof(statbuf.f_fname), statbuf.f_fname,
  298.             sizeof(statbuf.f_fpack), statbuf.f_fpack);
  299.     }
  300.     return 0;
  301. }
  302.  
  303. /* directory */
  304. int
  305. sys_chdir(tcp)
  306. struct tcb *tcp;
  307. {
  308.     if (entering(tcp)) {
  309.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  310.     }
  311.     return 0;
  312. }
  313.  
  314. int
  315. sys_mkdir(tcp)
  316. struct tcb *tcp;
  317. {
  318.     if (entering(tcp)) {
  319.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  320.         tprintf(", %o", tcp->u_args[1]);
  321.     }
  322.     return 0;
  323. }
  324.  
  325. int
  326. sys_rmdir(tcp)
  327. struct tcb *tcp;
  328. {
  329.     if (entering(tcp)) {
  330.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  331.     }
  332.     return 0;
  333. }
  334.  
  335. int
  336. sys_fchdir(tcp)
  337. struct tcb *tcp;
  338. {
  339.     if (entering(tcp)) {
  340.         tprintf("%u", tcp->u_args[0]);
  341.     }
  342.     return 0;
  343. }
  344.  
  345. int
  346. sys_chroot(tcp)
  347. struct tcb *tcp;
  348. {
  349.     if (entering(tcp)) {
  350.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  351.     }
  352.     return 0;
  353. }
  354.  
  355. int
  356. sys_fchroot(tcp)
  357. struct tcb *tcp;
  358. {
  359.     if (entering(tcp)) {
  360.         tprintf("%u", tcp->u_args[0]);
  361.     }
  362.     return 0;
  363. }
  364.  
  365. int
  366. sys_link(tcp)
  367. struct tcb *tcp;
  368. {
  369.     if (entering(tcp)) {
  370.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  371.         tprintf(", ");
  372.         (void)printstr(tcp->pid, tcp->u_args[1], -1);
  373.     }
  374.     return 0;
  375. }
  376.  
  377. int
  378. sys_unlink(tcp)
  379. struct tcb *tcp;
  380. {
  381.     if (entering(tcp)) {
  382.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  383.     }
  384.     return 0;
  385. }
  386.  
  387. int
  388. sys_symlink(tcp)
  389. struct tcb *tcp;
  390. {
  391.     if (entering(tcp)) {
  392.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  393.         tprintf(", ");
  394.         (void)printstr(tcp->pid, tcp->u_args[1], -1);
  395.     }
  396.     return 0;
  397. }
  398.  
  399. int
  400. sys_readlink(tcp)
  401. struct tcb *tcp;
  402. {
  403.     if (entering(tcp)) {
  404.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  405.         tprintf(", ");
  406.     } else {
  407.         if (syserror(tcp))
  408.             tprintf("%#x", tcp->u_args[1]);
  409.         else
  410.             (void)printstr(tcp->pid, tcp->u_args[1], tcp->u_rval);
  411.         tprintf(", %u", tcp->u_args[2]);
  412.     }
  413.     return 0;
  414. }
  415.  
  416. int
  417. sys_rename(tcp)
  418. struct tcb *tcp;
  419. {
  420.     if (entering(tcp)) {
  421.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  422.         tprintf(", ");
  423.         (void)printstr(tcp->pid, tcp->u_args[1], -1);
  424.     }
  425.     return 0;
  426. }
  427.  
  428. int
  429. sys_chown(tcp)
  430. struct tcb *tcp;
  431. {
  432.     if (entering(tcp)) {
  433.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  434.         tprintf(", %u, %u", tcp->u_args[1], tcp->u_args[2]);
  435.     }
  436.     return 0;
  437. }
  438.  
  439. int
  440. sys_fchown(tcp)
  441. struct tcb *tcp;
  442. {
  443.     if (entering(tcp)) {
  444.         tprintf("%u, %u, %u",
  445.             tcp->u_args[0], tcp->u_args[1], tcp->u_args[2]);
  446.     }
  447.     return 0;
  448. }
  449.  
  450. int
  451. sys_chmod(tcp)
  452. struct tcb *tcp;
  453. {
  454.     if (entering(tcp)) {
  455.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  456.         tprintf(", %o", tcp->u_args[1]);
  457.     }
  458.     return 0;
  459. }
  460.  
  461. int
  462. sys_fchmod(tcp)
  463. struct tcb *tcp;
  464. {
  465.     if (entering(tcp)) {
  466.         tprintf("%u, %o", tcp->u_args[0], tcp->u_args[1]);
  467.     }
  468.     return 0;
  469. }
  470.  
  471. int
  472. sys_utimes(tcp)
  473. struct tcb *tcp;
  474. {
  475.     if (entering(tcp)) {
  476.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  477.         tprintf(", ");
  478.         printtv(tcp->pid, tcp->u_args[1]);
  479.     }
  480.     return 0;
  481. }
  482.  
  483. int
  484. sys_mknod(tcp)
  485. struct tcb *tcp;
  486. {
  487.     int mode = tcp->u_args[1];
  488.  
  489.     if (entering(tcp)) {
  490.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  491.         tprintf(", %o(%s)", mode,
  492.             mode==S_IFCHR?"CHR":(
  493.                 mode==S_IFBLK?"BLK":(
  494.                     mode==S_IFREG?"REG":(
  495.                         mode==S_IFIFO?"FIFO":""
  496.                     )
  497.                 )
  498.             )
  499.         );
  500.         tprintf(", dev %x %x",
  501.                 major(tcp->u_args[2]), minor(tcp->u_args[2]));
  502.     }
  503.     return 0;
  504. }
  505.  
  506. int
  507. sys_mkfifo(tcp)
  508. struct tcb *tcp;
  509. {
  510.     if (entering(tcp)) {
  511.         (void)printstr(tcp->pid, tcp->u_args[0], -1);
  512.         tprintf(", %o", tcp->u_args[1]);
  513.     }
  514.     return 0;
  515. }
  516.  
  517. int
  518. sys_fsync(tcp)
  519. struct tcb *tcp;
  520. {
  521.     if (entering(tcp)) {
  522.         tprintf("%u", tcp->u_args[0]);
  523.     }
  524.     return 0;
  525. }
  526.  
  527. int
  528. sys_getdents(tcp)
  529. struct tcb *tcp;
  530. {
  531.     int i, len;
  532.     char *buf;
  533.  
  534.     if (entering(tcp)) {
  535.         tprintf("%u, ", tcp->u_args[0]);
  536.     } else {
  537.         if (syserror(tcp)) {
  538.             tprintf("%#x, %u", tcp->u_args[1], tcp->u_args[2]);
  539.             return 0;
  540.         }
  541.         len = tcp->u_rval;
  542.         if ((buf = malloc((u_int)len)) == NULL ||
  543.             umove(tcp->pid, tcp->u_args[1], len, buf) < 0) {
  544.             tprintf("%#x, %u", tcp->u_args[1], tcp->u_args[2]);
  545.         } else {
  546.             int dents = 0;
  547.             for (i = 0; i < len;) {
  548.                 struct dirent *d = (struct dirent *)&buf[i];
  549.                 if (verbose) tprintf(
  550.                     "{off:%u,fno:%u,rl:%u,nl:%u\"%.*s\"}",
  551.                     d->d_off, d->d_fileno,
  552.                     d->d_reclen, d->d_namlen,
  553.                     d->d_namlen, d->d_name);
  554.                 i += d->d_reclen;
  555.                 dents++;
  556.             }
  557.             if (!verbose)
  558.                 tprintf("{Total: %u dents}", dents);
  559.             tprintf(", %u", tcp->u_args[2]);
  560.         }
  561.         if (buf) free((char *)buf);
  562.     }
  563.     return 0;
  564. }
  565.  
  566. int
  567. sys_aioread(tcp)
  568. struct tcb *tcp;
  569. {
  570.     struct aio_result_t res;
  571.  
  572.     if (entering(tcp)) {
  573.         tprintf("%u, ", tcp->u_args[0]);
  574.     } else {
  575.         if (syserror(tcp))
  576.             tprintf("%#x", tcp->u_args[1]);
  577.         else
  578.             (void)printstr(tcp->pid, tcp->u_args[1], tcp->u_args[2]);
  579.         tprintf(", %u, %u, ", tcp->u_args[2], tcp->u_args[3]);
  580.         printxval(whence, tcp->u_args[4], "L_???");
  581.         if (syserror(tcp) || tcp->u_args[5] == 0 ||
  582.             umove(tcp->pid, tcp->u_args[5],
  583.                 sizeof res, (char *)&res) < 0)
  584.             tprintf(", (struct aio_result_t *)%#x",
  585.                         tcp->u_args[5]);
  586.         else
  587.             tprintf(", {%d,%d}",
  588.                 res.aio_return, res.aio_errno);
  589.     }
  590.     return 0;
  591. }
  592.  
  593. int
  594. sys_aiowrite(tcp)
  595. struct tcb *tcp;
  596. {
  597.     struct aio_result_t res;
  598.  
  599.     if (entering(tcp)) {
  600.         tprintf("%u, ", tcp->u_args[0]);
  601.         (void)printstr(tcp->pid, tcp->u_args[1], tcp->u_args[2]);
  602.         tprintf(", %u, %u, ", tcp->u_args[2], tcp->u_args[3]);
  603.         printxval(whence, tcp->u_args[4], "L_???");
  604.     } else {
  605.         if (syserror(tcp) || tcp->u_args[5] == 0 ||
  606.             umove(tcp->pid, tcp->u_args[5],
  607.                 sizeof res, (char *)&res) < 0)
  608.             tprintf(", (struct aio_result_t *)%#x",
  609.                         tcp->u_args[5]);
  610.         else
  611.             tprintf(", {%d,%d}",
  612.                 res.aio_return, res.aio_errno);
  613.     }
  614.     return 0;
  615. }
  616.  
  617. int
  618. sys_aiowait(tcp)
  619. struct tcb *tcp;
  620. {
  621.     if (entering(tcp)) {
  622.         printtv(tcp->pid, tcp->u_args[0]);
  623.     }
  624.     return 0;
  625. }
  626.  
  627. int
  628. sys_aiocancel(tcp)
  629. struct tcb *tcp;
  630. {
  631.     struct aio_result_t res;
  632.  
  633.     if (exiting(tcp)) {
  634.         if (syserror(tcp) || tcp->u_args[0] == 0 ||
  635.             umove(tcp->pid, tcp->u_args[0],
  636.                 sizeof res, (char *)&res) < 0)
  637.             tprintf(", (struct aio_result_t *)%#x",
  638.                         tcp->u_args[0]);
  639.         else
  640.             tprintf(", {%d,%d}",
  641.                 res.aio_return, res.aio_errno);
  642.     }
  643.     return 0;
  644. }
  645.