home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / emacs / emacs1857 / src_d2.zoo / source / utils / printstk.c < prev   
Encoding:
C/C++ Source or Header  |  1991-12-02  |  2.1 KB  |  112 lines

  1. /* 
  2.  * utility to print the value of _stksize from gcc-cc1.ttp
  3.  *
  4.  *    Usage: printstk [<filename>]
  5.  *        if <filename> is not specified defaults to .\gcc-cc1.ttp
  6.  *    ++jrb
  7.  *
  8.  *      modified to print a value of _initial_stack in cases when this
  9.  *      is defined instead of _stksize -- mj
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <unixlib.h>
  15. #include <string.h>
  16. #include <st-out.h>
  17.  
  18. long lseek(int, long, int);
  19. static char *sym_names[] = { "__stksize", "__initial_stack" };
  20.  
  21. long find_offset (fd, fn, what)
  22. int fd;
  23. char *fn;
  24. int *what;
  25. {
  26.     struct aexec head;
  27.     struct asym  sym;
  28.     int found;
  29.     int    all = 0;
  30.     int index = 1;
  31.     
  32.     if(read(fd, &head, sizeof(head)) != sizeof(head))
  33.     {
  34.     perror(fn);
  35.     exit(2);
  36.     }
  37.     if(head.a_magic != CMAGIC)
  38.     {
  39.     fprintf(stderr,"Invalid magic number %x\n", head.a_magic);
  40.     exit(3);
  41.     }
  42.     if(head.a_syms == 0)
  43.     {
  44.     fprintf(stderr,"%s: no symbol table\n", fn);
  45.     exit(4);
  46.     }
  47.     if(lseek(fd, head.a_text+head.a_data, 1) != 
  48.        (head.a_text+head.a_data+sizeof(head)))
  49.     {
  50.     perror(fn);
  51.     exit(5);
  52.     }
  53.     for(;;)
  54.     {
  55.     if(index && (read(fd, &sym, sizeof(sym)) != sizeof(sym)))
  56.     {
  57.         fprintf(stderr, "symbol _stksize not found\n");
  58.         exit(6);
  59.     }
  60.     /* after symbol read check first for _stksize */
  61.     index ^= 1;
  62.     if (strncmp(sym_names[index], sym.a_name, 8) == 0)
  63.     {
  64.         if ((found = (sym.a_type & A_DATA)) || all++)
  65.         break;
  66.     }
  67.     }
  68.     
  69.     if(!found)
  70.     {
  71.     fprintf(stderr, "symbol _stksize is undefined\n");
  72.     exit(9);
  73.     }
  74.     *what = index;
  75.     return sym.a_value + sizeof(head);
  76. }
  77.  
  78. int main(argc, argv)
  79. int argc;
  80. char **argv;
  81. {
  82.     int fd;
  83.     int what;
  84.     long stksize, offset;
  85.     char fn[FILENAME_MAX];
  86.     
  87.     if(argc > 1)
  88.     (void) strcpy(fn, *++argv);
  89.     else
  90.     (void) strcpy(fn, "gcc-cc1.ttp");
  91.     
  92.     if((fd = open(fn, 0)) < 0)
  93.     {
  94.     perror(fn);
  95.     exit(1);
  96.     }
  97.     
  98.     offset = find_offset(fd, fn, &what);
  99.     
  100.     if(lseek(fd, offset, 0) != offset)
  101.     {
  102.     perror(fn);
  103.     exit(7);
  104.     }
  105.     read(fd, &stksize, sizeof(long));
  106.     printf("%s: %s is %ld (%dK)\n",
  107.          fn, sym_names[what] + 1, stksize, (int)(stksize/1024));
  108.     
  109.     return close(fd);
  110. }
  111.  
  112.