home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2870 / crash.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-28  |  4.7 KB  |  261 lines

  1. /*
  2.  * Copyright 1988, 1991, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #ifndef    lint
  13. static    char    sccsid[] = "@(#)crash.c    2.1    07:11:33    2/27/91";
  14. #endif
  15.  
  16. #include <sys/param.h>
  17. #include <sys/sysmacros.h>
  18. #include <sys/types.h>
  19. #include <sys/page.h>
  20. #include <sys/seg.h>
  21. #include <sys/proc.h>
  22. #include <sys/signal.h>
  23. #include <sys/dir.h>
  24. #include <sys/user.h>
  25. #include <sys/var.h>
  26. #include <sys/utsname.h>
  27. #include <sys/conf.h>
  28. #include <fcntl.h>
  29. #include <stdio.h>
  30. #include "crash.h"
  31.  
  32. int    memfd;
  33. int    kmemfd;
  34. int    swapfd;
  35.  
  36. int    bflag;
  37. int    fflag;
  38. int    iflag;
  39. int    mflag;
  40. int    pflag;
  41. int    sflag;
  42. int    tflag;
  43. int    uflag;
  44. int    vflag;
  45.  
  46. int    anyflag;
  47.  
  48. struct    var    v;
  49. struct    file    *files;
  50. struct    inode    *inodes;
  51. struct    text    *texts;
  52. struct    proc    *procs;
  53. struct    mount    *mounts;
  54. struct    buf    *bufs;
  55. struct    buf    *bufstart;
  56. struct    cdevsw    *Cdevsw;
  57. struct    bdevsw    *Bdevsw;
  58. int    Cdevcnt;
  59. int    Bdevcnt;
  60. struct    user    user;
  61. daddr_t    swplo;
  62. struct    utsname    utsname;
  63. time_t    ktime;
  64. time_t    klbolt;
  65.  
  66. struct    xlist    namelist[] = {
  67.     { 0, 0, 0, "_v" },
  68.     { 0, 0, 0, "_file" },
  69.     { 0, 0, 0, "_inode" },
  70.     { 0, 0, 0, "_proc" },
  71.     { 0, 0, 0, "_text" },
  72.     { 0, 0, 0, "_mount" },
  73.     { 0, 0, 0, "_bufstrt" },
  74.     { 0, 0, 0, "_swplo" },
  75.     { 0, 0, 0, "_time" },
  76.     { 0, 0, 0, "_lbolt" },
  77.     { 0, 0, 0, "_utsname" },
  78.     { 0, 0, 0, "_u" },
  79.     { 0, 0, 0, "_cdevcnt" },
  80.     { 0, 0, 0, "_cdevsw" },
  81.     { 0, 0, 0, "_bdevcnt" },
  82.     { 0, 0, 0, "_bdevsw" },
  83.     { 0, 0, 0, (char *) 0 }
  84. };
  85.  
  86. usage ()
  87. {
  88.     fprintf (stderr, "usage: crash -bfimpstv [ -N namelist ] ");
  89.     fprintf (stderr, "[ -C corefile ] [ -S swapfile ]\n");
  90.     exit (1);
  91. }
  92.  
  93. r_read (fd, buf, n)
  94. int    fd;
  95. char    *buf;
  96. int    n;
  97. {
  98.     int    i;
  99.  
  100.     if ((i = read (fd, buf, n)) == -1) {
  101.         perror ("error on read");
  102.         return (-1);
  103.     } else
  104.         return (i);
  105. }
  106.  
  107. long    l_lseek (fd, offs, whence)
  108. int    fd;
  109. long    offs;
  110. int    whence;
  111. {
  112.     long    i;
  113.     long    lseek ();
  114.  
  115.     if ((i = lseek (fd, offs, whence)) == -1L) {
  116.         perror ("error on lseek");
  117.         return (-1);
  118.     } else
  119.         return (i);
  120. }
  121.  
  122. main (argc, argv)
  123. int    argc;
  124. char    **argv;
  125. {
  126.     char    newname[10];
  127.     char    *namefile = "/xenix";
  128.     char    *corefile = "/dev/mem";
  129.     char    *kmemfile = "/dev/kmem";
  130.     char    *swapfile = "/dev/swap";
  131.     int    c;
  132.     extern    int    optind;
  133.     extern    char    *optarg;
  134.  
  135.     setbuf (stdout, NULL);
  136.     setbuf (stderr, NULL);
  137.  
  138.     while ((c = getopt (argc, argv, "bfimpstuvN:C:S:")) != EOF) {
  139.         switch (c) {
  140.             case 'b':
  141.                 bflag++;
  142.                 anyflag++;
  143.                 break;
  144.             case 'C':
  145.                 corefile = optarg;
  146.                 kmemfile = optarg;
  147.                 break;
  148.             case 'f':
  149.                 fflag++;
  150.                 anyflag++;
  151.                 break;
  152.             case 'i':
  153.                 iflag++;
  154.                 anyflag++;
  155.                 break;
  156.             case 'm':
  157.                 mflag++;
  158.                 anyflag++;
  159.                 break;
  160.             case 'N':
  161.                 namefile = optarg;
  162.                 break;
  163.             case 'p':
  164.                 pflag++;
  165.                 anyflag++;
  166.                 break;
  167.             case 's':
  168.                 sflag++;
  169.                 anyflag++;
  170.                 break;
  171.             case 'S':
  172.                 swapfile = optarg;
  173.                 break;
  174.             case 't':
  175.                 tflag++;
  176.                 anyflag++;
  177.                 break;
  178.             case 'u':
  179.                 uflag++;
  180.                 anyflag++;
  181.                 break;
  182.             case 'v':
  183.                 vflag++;
  184.                 anyflag++;
  185.                 break;
  186.             default:
  187.                 usage ();
  188.         }
  189.     }
  190.     if (xlist (namefile, namelist) != 0) {
  191.         perror ("pstat: namelist");
  192.         exit (1);
  193.     }
  194.     if (initsyms (namefile)) {
  195.         fprintf (stderr, "unable to initialize symbol table\n");
  196.         exit (1);
  197.     }
  198.     if ((memfd = open (corefile, O_RDONLY)) < 0) {
  199.         perror ("pstat: corefile");
  200.         exit (1);
  201.     }
  202.     if ((kmemfd = open (kmemfile, O_RDONLY)) < 0) {
  203.         perror ("pstat: kmemfile");
  204.         exit (1);
  205.     }
  206.     if ((swapfd = open (swapfile, O_RDONLY)) < 0) {
  207.         perror ("pstat: swapfile");
  208.         exit (1);
  209.     }
  210.     l_lseek (kmemfd, namelist[NM_V].xl_value, 0);
  211.     r_read (kmemfd, &v, sizeof v);
  212.     l_lseek (kmemfd, namelist[NM_SWPLO].xl_value, 0);
  213.     r_read (kmemfd, &swplo, sizeof swplo);
  214.  
  215.     l_lseek (kmemfd, namelist[NM_CDEVCNT].xl_value, 0);
  216.     r_read (kmemfd, &Cdevcnt, sizeof Cdevcnt);
  217.     Cdevsw = (struct cdevsw *) malloc (Cdevcnt * sizeof *Cdevsw);
  218.     l_lseek (kmemfd, namelist[NM_CDEVSW].xl_value, 0);
  219.     r_read (kmemfd, Cdevsw, Cdevcnt * sizeof *Cdevsw);
  220.  
  221.     l_lseek (kmemfd, namelist[NM_BDEVCNT].xl_value, 0);
  222.     r_read (kmemfd, &Bdevcnt, sizeof Bdevcnt);
  223.     Bdevsw = (struct bdevsw *) malloc (Bdevcnt * sizeof *Bdevsw);
  224.     l_lseek (kmemfd, namelist[NM_BDEVSW].xl_value, 0);
  225.     r_read (kmemfd, Bdevsw, Bdevcnt * sizeof *Bdevsw);
  226.  
  227.     if (bflag)
  228.         prbufs ((int *) 0, 0);
  229.  
  230.     if (fflag)
  231.         prfiles ((int *) 0, 0);
  232.  
  233.     if (iflag)
  234.         prinodes ((int *) 0, 0);
  235.  
  236.     if (mflag) 
  237.         prmounts ((int *) 0, 0);
  238.  
  239.     if (pflag)
  240.         prprocs ((int *) 0, 0);
  241.  
  242.     if (sflag)
  243.         prstats ((int *) 0, 0);
  244.  
  245.     if (tflag)
  246.         prtexts ((int *) 0, 0);
  247.  
  248.     if (uflag)
  249.         prusers ((int *) 0, 0);
  250.  
  251.     if (vflag)
  252.         prvars ((int *) 0, 0);
  253.  
  254.     if (! anyflag) {
  255.         interact ();
  256.         exit (0);
  257.     } else {
  258.         exit (0);
  259.     }
  260. }
  261.