home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / debug / common / symify.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-13  |  3.2 KB  |  141 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <pc.h>
  6. #include <debug/syms.h>
  7. #include <debug/tss.h>
  8.  
  9. #define SC(r,c) (*(char *)(sc + (r)*ScreenCols() + (c)))
  10. #define SW(r,c) (*(sc + (r)*ScreenCols() + (c)))
  11.  
  12. TSS a_tss;
  13. int main(int argc, char **argv)
  14. {
  15.   int r, c;
  16.   short *sc;
  17.   char buf[90];
  18.   int i, lineno;
  19.   unsigned v;
  20.   unsigned long d;
  21.   char *func, *file;
  22.   FILE *ofile=0;
  23.   FILE *ifile=0;
  24.  
  25.   if (argc < 2)
  26.   {
  27.     fprintf(stderr, "Usage: symify [-o <outfile>] [-i <corefile>] <program>\n");
  28.     fprintf(stderr, "This program adds debug information to DJGPP program call frame tracebacks\n");
  29.     return 1;
  30.   }
  31.   while (argv[1][0] == '-')
  32.   {
  33.     if ((strcmp(argv[1], "-o") == 0) && (argc > 3))
  34.     {
  35.       ofile = fopen(argv[2], "w");
  36.       if (ofile == 0)
  37.         fprintf(stderr, "Error: unable to open file %s\n", argv[2]);
  38.       argc -= 2;
  39.       argv += 2;
  40.     }
  41.     else if ((strcmp(argv[1], "-i") == 0) && (argc > 3))
  42.     {
  43.       ifile = fopen(argv[2], "r");
  44.       if (ifile == 0)
  45.         fprintf(stderr, "Error: unable to open file %s\n", argv[2]);
  46.       argc -= 2;
  47.       argv += 2;
  48.     }
  49.     else
  50.     {
  51.       fprintf(stderr, "Invalid option %s - type `symify' for help\n", argv[1]);
  52.       exit(1);
  53.     }
  54.   }
  55.   syms_init(argv[1]);
  56.  
  57.   if (ifile)
  58.   {
  59.     char line[1000];
  60.     if (ofile == 0)
  61.       ofile = stdout;
  62.     while (fgets(line, 1000, ifile))
  63.     {
  64.       if (strncmp(line, "  0x", 4) == 0)
  65.       {
  66.         sscanf(line+4, "%x", &v);
  67.         func = syms_val2name(v, &d);
  68.         file = syms_val2line(v, &lineno, 0);
  69.         fprintf(ofile, "  0x%08x", v);
  70.         if (func)
  71.         {
  72.           fprintf(ofile, " %s", func);
  73.           if (d)
  74.             fprintf(ofile, "%+ld", d);
  75.         }
  76.         if (file)
  77.         {
  78.           if (func)
  79.             fprintf(ofile, ", ");
  80.           fprintf(ofile, "line %d of %s", lineno, file);
  81.         }
  82.         fputc('\n', ofile);
  83.       }
  84.       else
  85.         fputs(line, ofile);
  86.     }
  87.     return 0;
  88.   }
  89.  
  90.   sc = (short *)malloc(ScreenRows() * ScreenCols() * 2);
  91.  
  92.   ScreenRetrieve(sc);
  93.  
  94.   for (r=0; r<ScreenRows(); r++)
  95.   {
  96.     if (SC(r,0) == ' ' && SC(r,1) == ' ' && SC(r,2) == '0' && SC(r,3) == 'x')
  97.     {
  98.       buf[8] = 0;
  99.       for (i=0; i<8; i++)
  100.         buf[i] = SC(r, i+4);
  101.       sscanf(buf, "%x", &v);
  102.       func = syms_val2name(v, &d);
  103.       file = syms_val2line(v, &lineno, 0);
  104.       buf[0] = 0;
  105.       if (func)
  106.       {
  107.     strcpy(buf, func);
  108.     if (d)
  109.       sprintf(buf+strlen(buf), "%+ld", d);
  110.       }
  111.       if (file)
  112.       {
  113.         if (buf[0])
  114.           strcat(buf, ", ");
  115.         sprintf(buf+strlen(buf), "line %d of %s", lineno, file);
  116.       }
  117.       if (buf[0])
  118.         for (i=0; buf[i]; i++)
  119.           SW(r, 15+i) = 0x0f00 + buf[i];
  120.     }
  121.   }
  122.  
  123.   if (ofile)
  124.   {
  125.     for (r=0; r<ScreenRows(); r++)
  126.     {
  127.       c = 0;
  128.       for (i=0; i<ScreenCols(); i++)
  129.         if (SC(r, i) != ' ')
  130.           c = i;
  131.       for (i=0; i<=c; i++)
  132.         fputc(SC(r,i), ofile);
  133.       fputc('\n', ofile);
  134.     }
  135.     fclose(ofile);
  136.   }
  137.   else
  138.     ScreenUpdate(sc);
  139.   return 0;
  140. }
  141.