home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / driver6s / dump.c < prev    next >
C/C++ Source or Header  |  1989-12-15  |  5KB  |  236 lines

  1. /* comments are an admission that your code isn't clear enough */
  2.  
  3. /* History:170,1 0 */
  4. /* Fri Dec 15 11:08:53 1989 send_pkt was also dumping two bytes too many. 0 */
  5. /* Thu Dec 14 14:31:36 1989 receive_upcall was dumping another two bytes too many */
  6. /* Wed Aug 09 12:22:30 1989 receive_upcall was dumping two bytes too many */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10.  
  11. /* the format of the following struct is dependent upon trace.asm */
  12.  
  13. struct {
  14.     unsigned length;
  15.     unsigned char function;
  16.     unsigned long time;
  17.     unsigned char error;
  18.     union {
  19.         int handle;
  20.         unsigned char bytes[2000];
  21.         struct {
  22.             unsigned version;
  23.             unsigned char class;
  24.             unsigned type;
  25.             unsigned char number;
  26.             unsigned char basic;
  27.         } di;
  28.         struct {
  29.             unsigned char if_class;
  30.             unsigned if_type;
  31.             unsigned char if_number;
  32.             unsigned typelen;
  33.             unsigned handle;
  34.         } at;
  35.         struct {
  36.             unsigned handle;
  37.             unsigned length;
  38.         } ga;
  39.     } data;
  40. } record;
  41.  
  42. char *functions[] = {
  43.     "bad function",
  44.     "driver_info",
  45.     "access_type",
  46.     "release_type",
  47.     "send_pkt",
  48.     "terminate",
  49.     "get_address",
  50.     "reset_interface",
  51.     "set_rcv_mode",
  52.     "get_rcv_mode",
  53.     "set_multicast_list",
  54.     "get_multicast_list",
  55.     "get_statistics",
  56.     "set_address",
  57. };
  58.  
  59. enum {
  60.     bad_function,
  61.     driver_info,
  62.     access_type,
  63.     release_type,
  64.     send_pkt,
  65.     terminate,
  66.     get_address,
  67.     reset_interface,
  68.     set_rcv_mode,
  69.     get_rcv_mode,
  70.     set_multicast_list,
  71.     get_multicast_list,
  72.     get_statistics,
  73.     set_address,
  74.     receive_upcall = 255
  75. };
  76.  
  77. char *errors[] = {
  78.     "no_error",
  79.     "bad_handle",
  80.     "no_class",
  81.     "no_type",
  82.     "no_number",
  83.     "bad_type",
  84.     "no_multicast",
  85.     "cant_terminate",
  86.     "bad_mode",
  87.     "no_space",
  88.     "type_inuse",
  89.     "bad_command",
  90.     "cant_send",
  91.     "cant_set",
  92.     "bad_address",
  93. };
  94.  
  95. main()
  96. {
  97.     FILE *inf;
  98.     static first_time = 1;
  99.     unsigned long time_zero;
  100.     char args[80], results[80];
  101.  
  102.     inf = fopen("trace.out", "rb");
  103.     if (!inf) {
  104.         fprintf(stderr, "dump: cannot open \"trace.out\"\n");
  105.         exit(1);
  106.     }
  107.  
  108.     setvbuf(stdout, NULL, _IOFBF, 4096);
  109.  
  110.     for(;;) {
  111.         if (fread(&record.length, 2, 1, inf) != 1)
  112.             break;
  113.         fread(&record.function, 1, record.length - 2, inf);
  114.         if (first_time) {
  115.             first_time = 0;
  116.             time_zero = record.time;
  117.         }
  118.         args[0] = '\0';
  119.         switch(record.function) {
  120.         case get_statistics:
  121.         case terminate:
  122.         case reset_interface:
  123.         case release_type:
  124.         case get_address:
  125.         case receive_upcall:
  126.             sprintf(args, "%d", record.data.handle);
  127.             break;
  128.         case access_type:
  129.             sprintf(args, "%d %d %d %d", record.data.at.if_class,
  130.                 record.data.at.if_type,
  131.                 record.data.at.if_number,
  132.                 record.data.at.typelen);
  133.             break;
  134.         default:
  135.             strcpy(args, "");
  136.         }
  137.         if (record.error != 0) {
  138.             strcpy(results, errors[record.error]);
  139.         } else switch(record.function) {
  140.         case driver_info:
  141.             sprintf(results, "%d %d %d %d %d",
  142.                 record.data.di.version, record.data.di.class,
  143.                 record.data.di.type, record.data.di.number,
  144.                 record.data.di.basic);
  145.             break;
  146.         case access_type:
  147.             sprintf(results, "%d", record.data.at.handle);
  148.             break;
  149.         case send_pkt:
  150.             sprintf(results, "%d bytes", record.length - 8);
  151.             break;
  152.         case receive_upcall:
  153.             sprintf(results, "%d bytes", record.length - 10);
  154.             break;
  155.         default:
  156.             strcpy(results, errors[0]);
  157.         }
  158.         printf("%7.2f %s(%s) = %s\n",
  159.             (float)(record.time - time_zero) / 18.2,
  160.             record.function == receive_upcall?"receive_upcall":
  161.             functions[record.function], args, results);
  162.         switch(record.function) {
  163.         case get_address:
  164.             dump_bytes(&record.data.bytes[4], record.data.ga.length);
  165.             break;
  166.         case receive_upcall:
  167.             dump_bytes(&record.data.bytes[2], record.length - 10);
  168.             break;
  169.         case send_pkt:
  170.         case set_rcv_mode:
  171.             dump_bytes(&record.data.bytes[0], record.length - 8);
  172.             break;
  173.         }
  174.     }
  175. }
  176.  
  177. dump_bytes(unsigned char *bytes, int count)
  178. {
  179.     int n;
  180.     char buf[16];
  181.     int address;
  182.     void fmtline();
  183.  
  184.     address = 0;
  185.     while(count){
  186.         if (count > 16) n = 16;
  187.         else n = count;
  188.         fmtline(address,bytes,n);
  189.         address += n;
  190.         count -= n;
  191.         bytes += n;
  192.     }
  193. }
  194. /* Print a buffer up to 16 bytes long in formatted hex with ascii
  195.  * translation, e.g.,
  196.  * 0000: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f  0123456789:;<=>?
  197.  */
  198. void
  199. fmtline(addr,buf,len)
  200. int addr;
  201. char *buf;
  202. int len;
  203. {
  204.     char line[80];
  205.     register char *aptr,*cptr;
  206.     unsigned register char c;
  207.     void ctohex();
  208.  
  209.     memset(line,' ',sizeof(line));
  210.     ctohex(line,addr >> 8);
  211.     ctohex(line+2,addr & 0xff);
  212.     aptr = &line[6];
  213.     cptr = &line[55];
  214.     while(len-- != 0){
  215.         c = *buf++;
  216.         ctohex(aptr,c);
  217.         aptr += 3;
  218.         c &= 0x7f;
  219.         *cptr++ = isprint(c) ? c : '.';
  220.     }
  221.     *cptr++ = '\n';
  222.     fwrite(line,1,(unsigned)(cptr-line),stdout);
  223. }
  224. /* Convert byte to two ascii-hex characters */
  225. static
  226. void
  227. ctohex(buf,c)
  228. register char *buf;
  229. register int c;
  230. {
  231.     static char hex[] = "0123456789abcdef";
  232.  
  233.     *buf++ = hex[c >> 4];
  234.     *buf = hex[c & 0xf];
  235. }
  236.