home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 545a.lha / HitMe / hitme.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-07  |  6.5 KB  |  270 lines

  1. /*****************************************/
  2. /*                                       */
  3. /* COPYRIGHT (c) 1991 SAS Institute, Inc */
  4. /*                    SAS Campus Drive   */
  5. /*                    Cary, NC 27513     */
  6. /*                    USA                */
  7. /*                                       */
  8. /* Written by Doug Walker                */
  9. /* bix: djwalker  portal: djwalker       */
  10. /* usenet: walker@unx.sas.com            */
  11. /*                                       */
  12. /* Please do not redistribute any        */
  13. /* modified versions of this source      */
  14. /* without permission from the copyright */
  15. /* holder!                               */
  16. /*                                       */
  17. /*****************************************/
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <libraries/dosextens.h>
  23. #include <proto/exec.h>
  24. #include <proto/dos.h>
  25.  
  26. #define HUNKHEAD " num hunk         size filename         punit    type         base  alv"
  27.  
  28. void gethead(char *buf, int size, FILE *fp);
  29. int doomd(char *oname, char *cname, long targoff);
  30.  
  31. char buf[300];
  32.  
  33. #define WORKFILE "ram:hitme.tmp"
  34.  
  35. char *omdfile = WORKFILE;
  36. char *cfiles = "";
  37. char *ofiles = "";
  38.  
  39. int keepfile;
  40.  
  41. #define EMSG(x) fprintf(stderr, x);
  42.  
  43. void main(int argc, char **argv)
  44. {
  45.    FILE *fp;
  46.    char *p;
  47.    char *cmdname;
  48.    char fname[2][50], punit[2][50], type[2][10];
  49.    int hunk, cur, base, lbase, offset, toggle;
  50.    int rc = 69;
  51.  
  52.    if(argc > 0) cmdname = argv[0];
  53.    else cmdname = NULL;
  54.  
  55.    if(argc < 3)
  56.    {
  57.       usage:
  58.       if(cmdname)
  59.       {
  60.          fprintf(stderr, "USAGE: %s <options> <mapfilename> <offset> [<hunk>]\n", 
  61.             cmdname);
  62.          EMSG("   <options> is one or more of\n");
  63.          EMSG("      -n         Do not run OMD\n");
  64.          EMSG("      -d         Keep OMD output\n");
  65.          EMSG("      -d<file>   Keep OMD output in <file>\n");
  66.          EMSG("      -t         Delete OMD output (default)\n");
  67.          EMSG("      -t<file>   Use <file> for OMD output, then delete\n");
  68.          EMSG("      -o<dir>    Directory for object files (default current directory)\n");
  69.          EMSG("      -c<dir>    Directory for C source files (default current directory)\n");
  70.          EMSG("   <mapfilename> is a BLINK-produced map of your executable\n");
  71.          EMSG("   <offset>      is the offset reported by ENFORCER\n");
  72.          EMSG("   <hunk>        is the hunk reported by ENFORCER (default 0)\n");
  73.       }
  74.       exit(99);
  75.    }
  76.  
  77.    while(argv[1][0] == '-')
  78.    {
  79.       switch(argv[1][1])
  80.       {
  81.          case 'c': case 'C':  /* C source file path */
  82.             cfiles = argv[1]+2;
  83.             break;
  84.  
  85.          case 'o': case 'O':  /* Object file path */
  86.             ofiles = argv[1]+2;
  87.             break;
  88.  
  89.          case 'd': case 'D':  /* Disassembly output */
  90.             keepfile = 1;
  91.             if(argv[1][2]) omdfile = argv[1]+2;
  92.             break;
  93.  
  94.          case 't': case 'T':  /* Temp file name */
  95.             keepfile = 0;
  96.             if(argv[1][2]) omdfile = argv[1]+2;
  97.             break;
  98.  
  99.          case 'n': case 'N':
  100.             keepfile = 0;
  101.             omdfile = NULL;
  102.             break;
  103.       }
  104.       argv++, argc--;
  105.    }
  106.  
  107.    if(argc < 2) goto usage;
  108.  
  109.    if(!(fp = fopen(argv[1], "r")))
  110.    {
  111.       fprintf(stderr, "ERROR: Can't open file '%s'\n", argv[1]);
  112.       exit(89);
  113.    }
  114.  
  115.    stch_i(argv[2], &offset); 
  116.  
  117.    if(argc >= 4)
  118.       stcd_i(argv[3], &hunk);
  119.    else
  120.       hunk = 0;
  121.  
  122.    buf[0] = 0x0c;  /* Force a gethead call */
  123.    do
  124.    {
  125.       if(buf[0] == 0x0c) gethead(buf, sizeof(buf)-1, fp);
  126.  
  127.       for(p=buf; *p == ' '; p++);
  128.  
  129.       if(stcd_i(p, &cur) && cur == hunk)
  130.       {
  131.          /* Bingo - found the right hunk */
  132.          base = 0;
  133.          toggle = 0;
  134.          do
  135.          {
  136.             if(buf[0] == 0x0c) gethead(buf, sizeof(buf)-1, fp);
  137.  
  138.             for(p=buf; *p == ' '; p++);
  139.  
  140.             if(stcd_i(buf, &cur) && cur != hunk) break;  /* New hunk */
  141.             lbase = base;
  142.             sscanf(buf+23, "%s %s %s %lx", 
  143.                fname[toggle], punit[toggle], type[toggle], &base);
  144.             toggle = !toggle;
  145.          }
  146.          while(base < offset && fgets(buf, sizeof(buf)-1, fp));
  147.  
  148.          if(base < offset)
  149.          {
  150.             fprintf(stderr, "ERROR: Can't find offset 0x%08lx in hunk %d\n",
  151.                offset, hunk);
  152.             rc = 79;
  153.             goto endit;
  154.          }
  155.          if(base == offset) 
  156.          {
  157.             toggle = !toggle;
  158.             lbase = base;
  159.          }
  160.  
  161.          offset -= lbase;
  162.  
  163.          if(strcmp(type[toggle], "code") ||
  164.             doomd(fname[toggle], punit[toggle], offset))
  165.          {
  166.             printf("Offset 0x%08lx file '%s' punit '%s' type %s\n", 
  167.                offset, fname[toggle], punit[toggle], type[toggle]);
  168.             if(keepfile) printf("No OMD output available\n");
  169.          }
  170.          rc = 0;
  171.          goto endit;
  172.       }
  173.    }
  174.    while(fgets(buf, sizeof(buf)-1, fp));
  175.  
  176.    fprintf(stderr, "Can't find hunk %d\n", hunk);
  177.  
  178. endit:
  179.    if(fp) fclose(fp);
  180.  
  181.    exit(rc);
  182. }
  183.  
  184. void gethead(char *buf, int size, FILE *fp)
  185. {
  186.    while(fgets(buf, size, fp))
  187.    {
  188.       if(!memcmp(buf, HUNKHEAD, strlen(HUNKHEAD)))
  189.          break;
  190.    }
  191.    fgets(buf, size, fp);
  192. }
  193.  
  194.  
  195. char onambuf[256];
  196. char cnambuf[304];
  197.  
  198. int doomd(char *oname, char *cname, long targoff)
  199. {
  200.    BPTR ofp, ifp;
  201.    FILE *fp;
  202.    char *p;
  203.    int lno, offset;
  204.    int rc;
  205.  
  206.    lno = -1;
  207.  
  208.    if(!omdfile) return(1);
  209.  
  210.    strcpy(onambuf, ofiles);
  211.    if(ofiles[0] && ofiles[0] != ':') strcat(onambuf, "/");
  212.    strcat(onambuf, oname);
  213.  
  214.    if(!(ifp = Open(onambuf, MODE_NEWFILE)))
  215.    {
  216.       omdfile = onambuf;
  217.       goto errfile;
  218.    }
  219.    if(Read(ifp, cnambuf, sizeof(cnambuf)) > 0)
  220.       cname = cnambuf + 8;
  221.    Close(ifp);
  222.  
  223.    if(!(ofp = Open(omdfile, MODE_NEWFILE)))
  224.    {
  225.       errfile:
  226.       fprintf(stderr, "ERROR: Can't open file '%s'\n", omdfile);
  227.       return(1);
  228.    }
  229.  
  230.    ifp = Open("NIL:", MODE_NEWFILE);
  231.  
  232.    sprintf(buf, "LC:OMD %s %s%s%s", onambuf,
  233.           cfiles, (cfiles[0] && cfiles[0] != ':' ? "/" : ""), cname);
  234.    Execute(buf, ifp, ofp);
  235.  
  236.    Close(ofp);
  237.    if(ifp) Close(ifp);
  238.  
  239.    if(!(fp = fopen(omdfile, "r"))) goto errfile;
  240.  
  241.    rc = 1;
  242.    while(fgets(buf, sizeof(buf)-1, fp))
  243.    {
  244.       if(buf[0] == ';')
  245.       {
  246.          for(p=buf+1; *p == ' '; p++);
  247.          stcd_i(p, &lno);
  248.       }
  249.       else if(buf[7] == '|')
  250.       {
  251.          if(lno < 0) break;   /* No debug */
  252.  
  253.          stch_i(buf+9, &offset);
  254.          if(offset >= targoff)
  255.          {
  256.             printf("Line %d file '%s'\n", lno, cname);
  257.             rc = 0;
  258.             break;
  259.          }
  260.       }
  261.    }
  262.  
  263.    fclose(fp);
  264.  
  265.    if(!keepfile) DeleteFile(omdfile);
  266.    else if(!rc) printf("OMD output is in '%s'\n", omdfile);
  267.  
  268.    return(rc);
  269. }
  270.