home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / utlsrc33.lzh / UTLSRC33 / PRINTSTK.C < prev    next >
C/C++ Source or Header  |  1993-07-30  |  4KB  |  182 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 <st-out.h>
  14.  
  15. #if __STDC__
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #else
  20. #include <string.h>
  21. extern char *malloc();
  22. extern long lseek();
  23. #define size_t unsigned long
  24. #endif
  25.  
  26. #ifndef FILENAME_MAX
  27. # define FILENAME_MAX 128
  28. #endif
  29.  
  30. #ifdef WORD_ALIGNED
  31. # define SIZEOF_AEXEC ((2*sizeof(short)) + (6*sizeof(long)))
  32. # define SIZEOF_ASYM  ((SYMLEN*sizeof(char)) + sizeof(short) + sizeof(long))
  33. # define SYM_OFFSET   (sizeof(short) + (3*sizeof(long)))
  34. #endif
  35.  
  36. static char *sym_names[] = { "__stksize", "__initial_stack" };
  37.  
  38. long find_offset (fd, fn, what)
  39. int fd;
  40. char *fn;
  41. int *what;
  42. {
  43.     struct aexec head;
  44.     struct asym  sym;
  45.     int found;
  46.     int    all = 0;
  47.     int index = 1;
  48.     
  49. #ifndef WORD_ALIGNED
  50.     if(read(fd, &head, sizeof(head)) != sizeof(head))
  51. #else
  52.     if(read_head(fd, &head))
  53. #endif
  54.     {
  55.     perror(fn);
  56.     exit(2);
  57.     }
  58.     if(head.a_magic != CMAGIC)
  59.     {
  60.     fprintf(stderr,"Invalid magic number %x\n", head.a_magic);
  61.     exit(3);
  62.     }
  63.     if(head.a_syms == 0)
  64.     {
  65.     fprintf(stderr,"%s: no symbol table\n", fn);
  66.     exit(4);
  67.     }
  68.     if(lseek(fd, head.a_text+head.a_data, 1) != 
  69. #ifndef WORD_ALIGNED
  70.        (head.a_text+head.a_data+sizeof(head)))
  71. #else
  72.        (head.a_text+head.a_data+SIZEOF_AEXEC))
  73. #endif
  74.     {
  75.     perror(fn);
  76.     exit(5);
  77.     }
  78.     for(;;)
  79.     {
  80. #ifndef WORD_ALIGNED
  81.     if(index && (read(fd, &sym, sizeof(sym)) != sizeof(sym)))
  82. #else
  83.     if(index && read_sym(fd, &sym))
  84. #endif
  85.     {
  86.         fprintf(stderr, "symbol _stksize not found\n");
  87.         exit(6);
  88.     }
  89.     /* after symbol read check first for _stksize */
  90.     index ^= 1;
  91.     if (strncmp(sym_names[index], sym.a_name, 8) == 0)
  92.     {
  93.         if ((found = (sym.a_type & A_DATA)) || all++)
  94.         break;
  95.     }
  96.     }
  97.     
  98.     if(!found)
  99.     {
  100.     fprintf(stderr, "symbol _stksize is undefined\n");
  101.     exit(9);
  102.     }
  103.     *what = index;
  104. #ifndef WORD_ALIGNED
  105.     return sym.a_value + sizeof(head);
  106. #else
  107.     return sym.a_value + SIZEOF_AEXEC;
  108. #endif
  109. }
  110.  
  111. int main(argc, argv)
  112. int argc;
  113. char **argv;
  114. {
  115.     int fd;
  116.     int what;
  117.     long stksize, offset;
  118.     char fn[FILENAME_MAX];
  119.     
  120.     if(argc > 1)
  121.     (void) strcpy(fn, *++argv);
  122.     else
  123.     (void) strcpy(fn, "gcc-cc1.ttp");
  124.     
  125.     if((fd = open(fn, 0)) < 0)
  126.     {
  127.     perror(fn);
  128.     exit(1);
  129.     }
  130.     
  131.     offset = find_offset(fd, fn, &what);
  132.     
  133.     if(lseek(fd, offset, 0) != offset)
  134.     {
  135.     perror(fn);
  136.     exit(7);
  137.     }
  138.     read(fd, &stksize, sizeof(long));
  139.     printf("%s: %s is %ld (%dK)\n",
  140.          fn, sym_names[what] + 1, stksize, (int)(stksize/1024));
  141.     
  142.     return close(fd);
  143. }
  144.  
  145. #ifdef WORD_ALIGNED
  146. #ifndef atarist
  147. # define lread  read
  148. # define lwrite write
  149. #endif
  150.  
  151. /*
  152.  * read header -- return !0 on err
  153.   */
  154. #define ck_read(fd, addr, siz) \
  155.   if((long)siz != lread(fd, addr, (long)siz)) return !0;
  156.   
  157. int read_head (fd, a)
  158. int fd;
  159. struct aexec *a;
  160. {
  161.     ck_read(fd, &a->a_magic,   sizeof(a->a_magic));
  162.     ck_read(fd, &a->a_text,    sizeof(a->a_text));
  163.     ck_read(fd, &a->a_data,    sizeof(a->a_data));
  164.     ck_read(fd, &a->a_bss,     sizeof(a->a_bss));
  165.     ck_read(fd, &a->a_syms,    sizeof(a->a_syms));
  166.     ck_read(fd, &a->a_AZero1,  sizeof(a->a_AZero1));
  167.     ck_read(fd, &a->a_AZero2,  sizeof(a->a_AZero2));
  168.     ck_read(fd, &a->a_isreloc, sizeof(a->a_isreloc));
  169.     return 0;
  170. }
  171.  
  172. int read_sym(fd, s)
  173. int fd;
  174. struct asym *s;
  175. {
  176.     ck_read(fd, s->a_name, 8);
  177.     ck_read(fd, &(s->a_type), sizeof(short));
  178.     ck_read(fd, &(s->a_value), sizeof(long));
  179.     return 0;
  180. }
  181. #endif
  182.