home *** CD-ROM | disk | FTP | other *** search
/ Doom Fever / Doom_Fever-1995_Maple_Media.iso / dmutil / drivers.zip / DUMP.C < prev    next >
C/C++ Source or Header  |  1992-01-06  |  6KB  |  274 lines

  1. /*  Copyright, 1988-1992, Russell Nelson, Crynwr Software */
  2.  
  3. /*   This program is free software; you can redistribute it and/or modify
  4. **   it under the terms of the GNU General Public License as published by
  5. **   the Free Software Foundation, version 1.
  6. **
  7. **   This program is distributed in the hope that it will be useful,
  8. **   but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. **   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. **   GNU General Public License for more details.
  11. **
  12. **   You should have received a copy of the GNU General Public License
  13. **   along with this program; if not, write to the Free Software
  14. **   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16.  
  17. /* comments are an admission that your code isn't clear enough */
  18.  
  19. /* History:86,1 */
  20. /* Fri Dec 15 11:08:53 1989 send_pkt was also dumping two bytes too many. */
  21. /* Thu Dec 14 14:31:36 1989 receive_upcall was dumping another two bytes too many */
  22. /* Wed Aug 09 12:22:30 1989 receive_upcall was dumping two bytes too many */
  23.  
  24. #include <stdio.h>
  25. #include <ctype.h>
  26.  
  27. /* the format of the following struct is dependent upon trace.asm */
  28.  
  29. struct {
  30.     unsigned length;
  31.     unsigned char function;
  32.     unsigned long time;
  33.     unsigned char error;
  34.     union {
  35.         int handle;
  36.         unsigned char bytes[2000];
  37.         struct {
  38.             unsigned version;
  39.             unsigned char class;
  40.             unsigned type;
  41.             unsigned char number;
  42.             unsigned char basic;
  43.         } di;
  44.         struct {
  45.             unsigned char if_class;
  46.             unsigned if_type;
  47.             unsigned char if_number;
  48.             unsigned typelen;
  49.             unsigned handle;
  50.         } at;
  51.         struct {
  52.             unsigned handle;
  53.             unsigned length;
  54.         } ga;
  55.         struct {
  56.             unsigned handle;
  57.             unsigned mode;
  58.         } srm;
  59.     } data;
  60. } record;
  61.  
  62. char *functions[] = {
  63.     "function_0",
  64.     "driver_info",
  65.     "access_type",
  66.     "release_type",
  67.     "send_pkt",
  68.     "terminate",
  69.     "get_address",
  70.     "reset_interface",
  71.     "function_8",
  72.     "function_9",
  73.     "get_parameters",
  74.     "as_send_pkt",
  75.     "function_12",
  76.     "function_13",
  77.     "function_14",
  78.     "function_15",
  79.     "function_16",
  80.     "function_17",
  81.     "function_18",
  82.     "function_19",
  83.     "set_rcv_mode",
  84.     "get_rcv_mode",
  85.     "set_multicast_list",
  86.     "get_multicast_list",
  87.     "get_statistics",
  88.     "set_address",
  89. };
  90.  
  91. enum {
  92.     bad_function,
  93.     driver_info,
  94.     access_type,
  95.     release_type,
  96.     send_pkt,
  97.     terminate,
  98.     get_address,
  99.     reset_interface,
  100.     get_parameters = 8,
  101.     as_send_pkt,
  102.     set_rcv_mode = 20,
  103.     get_rcv_mode,
  104.     set_multicast_list,
  105.     get_multicast_list,
  106.     get_statistics,
  107.     set_address,
  108.     receive_upcall = 255
  109. };
  110.  
  111. char *errors[] = {
  112.     "no_error",
  113.     "bad_handle",
  114.     "no_class",
  115.     "no_type",
  116.     "no_number",
  117.     "bad_type",
  118.     "no_multicast",
  119.     "cant_terminate",
  120.     "bad_mode",
  121.     "no_space",
  122.     "type_inuse",
  123.     "bad_command",
  124.     "cant_send",
  125.     "cant_set",
  126.     "bad_address",
  127. };
  128.  
  129. main()
  130. {
  131.     FILE *inf;
  132.     static first_time = 1;
  133.     unsigned long time_zero;
  134.     char args[80], results[80];
  135.  
  136.     inf = fopen("trace.out", "rb");
  137.     if (!inf) {
  138.         fprintf(stderr, "dump: cannot open \"trace.out\"\n");
  139.         exit(1);
  140.     }
  141.  
  142.     setvbuf(stdout, NULL, _IOFBF, 4096);
  143.  
  144.     for(;;) {
  145.         if (fread(&record.length, 2, 1, inf) != 1)
  146.             break;
  147.         fread(&record.function, 1, record.length - 2, inf);
  148.         if (first_time) {
  149.             first_time = 0;
  150.             time_zero = record.time;
  151.         }
  152.         args[0] = '\0';
  153.         switch(record.function) {
  154.         case get_statistics:
  155.         case terminate:
  156.         case reset_interface:
  157.         case release_type:
  158.         case get_address:
  159.         case receive_upcall:
  160.             sprintf(args, "%d", record.data.handle);
  161.             break;
  162.         case set_rcv_mode:
  163.             sprintf(args, "%d %d", record.data.srm.handle, record.data.srm.mode);
  164.             break;
  165.         case access_type:
  166.             sprintf(args, "%d %d %d %d", record.data.at.if_class,
  167.                 record.data.at.if_type,
  168.                 record.data.at.if_number,
  169.                 record.data.at.typelen);
  170.             break;
  171.         default:
  172.             strcpy(args, "");
  173.         }
  174.         if (record.error != 0) {
  175.             sprintf(results, "%s (%d)",
  176.                 record.error >= (sizeof errors)/(sizeof errors[0])?"?":errors[record.error],
  177.                 record.error);
  178.         } else switch(record.function) {
  179.         case driver_info:
  180.             sprintf(results, "%d %d %d %d %d",
  181.                 record.data.di.version, record.data.di.class,
  182.                 record.data.di.type, record.data.di.number,
  183.                 record.data.di.basic);
  184.             break;
  185.         case access_type:
  186.             sprintf(results, "%d", record.data.at.handle);
  187.             break;
  188.         case send_pkt:
  189.             sprintf(results, "%d bytes", record.length - 8);
  190.             break;
  191.         case receive_upcall:
  192.             sprintf(results, "%d bytes", record.length - 10);
  193.             break;
  194.         default:
  195.             strcpy(results, errors[0]);
  196.         }
  197.         printf("%7.2f %s(%s) = %s\n",
  198.             (float)(record.time - time_zero) / 18.2,
  199.             record.function == receive_upcall?"receive_upcall":
  200.             functions[record.function], args, results);
  201.         switch(record.function) {
  202.         case get_address:
  203.             dump_bytes(&record.data.bytes[4], record.data.ga.length);
  204.             break;
  205.         case receive_upcall:
  206.             dump_bytes(&record.data.bytes[2], record.length - 10);
  207.             break;
  208.         case send_pkt:
  209.             dump_bytes(&record.data.bytes[0], record.length - 8);
  210.             break;
  211.         }
  212.     }
  213. }
  214.  
  215. dump_bytes(unsigned char *bytes, int count)
  216. {
  217.     int n;
  218.     char buf[16];
  219.     int address;
  220.     void fmtline();
  221.  
  222.     address = 0;
  223.     while(count){
  224.         if (count > 16) n = 16;
  225.         else n = count;
  226.         fmtline(address,bytes,n);
  227.         address += n;
  228.         count -= n;
  229.         bytes += n;
  230.     }
  231. }
  232. /* Print a buffer up to 16 bytes long in formatted hex with ascii
  233.  * translation, e.g.,
  234.  * 0000: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f  0123456789:;<=>?
  235.  */
  236. void
  237. fmtline(addr,buf,len)
  238. int addr;
  239. char *buf;
  240. int len;
  241. {
  242.     char line[80];
  243.     register char *aptr,*cptr;
  244.     unsigned register char c;
  245.     void ctohex();
  246.  
  247.     memset(line,' ',sizeof(line));
  248.     ctohex(line,addr >> 8);
  249.     ctohex(line+2,addr & 0xff);
  250.     aptr = &line[6];
  251.     cptr = &line[55];
  252.     while(len-- != 0){
  253.         c = *buf++;
  254.         ctohex(aptr,c);
  255.         aptr += 3;
  256.         c &= 0x7f;
  257.         *cptr++ = isprint(c) ? c : '.';
  258.     }
  259.     *cptr++ = '\n';
  260.     fwrite(line,1,(unsigned)(cptr-line),stdout);
  261. }
  262. /* Convert byte to two ascii-hex characters */
  263. static
  264. void
  265. ctohex(buf,c)
  266. register char *buf;
  267. register int c;
  268. {
  269.     static char hex[] = "0123456789abcdef";
  270.  
  271.     *buf++ = hex[c >> 4];
  272.     *buf = hex[c & 0xf];
  273. }
  274.