home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / pktsend.zip / DUMP.C < prev    next >
C/C++ Source or Header  |  1991-03-19  |  5KB  |  258 lines

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