symmisc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* Do various things to symbol tables (other than lookup)), for GDB.
  2. Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  3. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the GDB General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute GDB,
  9. but only under the conditions described in the GDB General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with GDB so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, go ahead and share GDB, but don't try to stop
  15. anyone else from sharing it farther. Help stamp out software hoarding!
  16. */
  17. #include "defs.h"
  18. #include "initialize.h"
  19. #include "symtab.h"
  20. #include <stdio.h>
  21. #include <obstack.h>
  22. static void free_symtab ();
  23. START_FILE
  24. /* Free all the symtabs that are currently installed,
  25. and all storage associated with them.
  26. Leaves us in a consistent state with no symtabs installed. */
  27. void
  28. free_all_symtabs ()
  29. {
  30. register struct symtab *s, *snext;
  31. /* All values will be invalid because their types will be! */
  32. clear_value_history ();
  33. clear_displays ();
  34. clear_internalvars ();
  35. clear_breakpoints ();
  36. set_default_breakpoint (0, 0, 0, 0);
  37. current_source_symtab = 0;
  38. for (s = symtab_list; s; s = snext)
  39. {
  40. snext = s->next;
  41. free_symtab (s);
  42. }
  43. symtab_list = 0;
  44. obstack_free (symbol_obstack, 0);
  45. obstack_init (symbol_obstack);
  46. if (misc_function_vector)
  47. free (misc_function_vector);
  48. misc_function_count = 0;
  49. misc_function_vector = 0;
  50. }
  51. /* Free a struct block <- B and all the symbols defined in that block. */
  52. static void
  53. free_symtab_block (b)
  54. struct block *b;
  55. {
  56. register int i, n;
  57. n = BLOCK_NSYMS (b);
  58. for (i = 0; i < n; i++)
  59. {
  60. free (SYMBOL_NAME (BLOCK_SYM (b, i)));
  61. free (BLOCK_SYM (b, i));
  62. }
  63. free (b);
  64. }
  65. /* Free all the storage associated with the struct symtab <- S.
  66. Note that some symtabs have contents malloc'ed structure by structure,
  67. while some have contents that all live inside one big block of memory,
  68. and some share the contents of another symbol table and so you should
  69. not free the contents on their behalf (except sometimes the linetable,
  70. which maybe per symtab even when the rest is not).
  71. It is s->free_code that says which alternative to use. */
  72. static void
  73. free_symtab (s)
  74. register struct symtab *s;
  75. {
  76. register int i, n;
  77. register struct blockvector *bv;
  78. register struct type *type;
  79. register struct typevector *tv;
  80. switch (s->free_code)
  81. {
  82. case free_nothing:
  83. /* All the contents are part of a big block of memory
  84. and some other symtab is in charge of freeing that block.
  85. Therefore, do nothing. */
  86. break;
  87. case free_contents:
  88. /* Here all the contents were malloc'ed structure by structure
  89. and must be freed that way. */
  90. /* First free the blocks (and their symbols. */
  91. bv = BLOCKVECTOR (s);
  92. n = BLOCKVECTOR_NBLOCKS (bv);
  93. for (i = 0; i < n; i++)
  94. free_symtab_block (BLOCKVECTOR_BLOCK (bv, i));
  95. /* Free the blockvector itself. */
  96. free (bv);
  97. /* Free all the data types. */
  98. tv = TYPEVECTOR (s);
  99. /* Free the type vector. */
  100. free (tv);
  101. /* Also free the linetable. */
  102. case free_linetable:
  103. /* All the contents were malloc'ed structure by structure,
  104. but some other symtab is in charge of freeing them.
  105. So here just free the linetable, since that is the only
  106. thing not shared with that other symtab. */
  107. free (LINETABLE (s));
  108. break;
  109. case free_explicit:
  110. /* All the contents live in one big block of memory: free it. */
  111. free (s->free_ptr);
  112. }
  113. if (s->line_charpos)
  114. free (s->line_charpos);
  115. free (s->filename);
  116. free (s);
  117. }
  118. /* Convert a raw symbol-segment to a struct symtab,
  119. and relocate its internal pointers so that it is valid. */
  120. /* This is how to relocate one pointer, given a name for it.
  121. Works independent of the type of object pointed to. */
  122. #define RELOCATE(slot) (slot ? (* (char **) &slot += relocation) : 0)
  123. /* This is the inverse of RELOCATE. We use it when storing
  124. a core address into a slot that has yet to be relocated. */
  125. #define UNRELOCATE(slot) (slot ? (* (char **) &slot -= relocation) : 0)
  126. /* During the process of relocation, this holds the amount to relocate by
  127. (the address of the file's symtab data, in core in the debugger). */
  128. static int relocation;
  129. #define CORE_RELOCATE(slot) \
  130. ((slot) += (((slot) < data_start) ? text_relocation \
  131. : ((slot) < bss_start) ? data_relocation : bss_relocation))
  132. #define TEXT_RELOCATE(slot) ((slot) += text_relocation)
  133. /* Relocation amounts for addresses in the program's core image. */
  134. static int text_relocation, data_relocation, bss_relocation;
  135. /* Boundaries that divide program core addresses into text, data and bss;
  136. used to determine which relocation amount to use. */
  137. static int data_start, bss_start;
  138. static void relocate_typevector ();
  139. static void relocate_blockvector ();
  140. static void relocate_type ();
  141. static void relocate_block ();
  142. static void relocate_symbol ();
  143. /* Relocate a file symbol table so that all the pointers
  144. are valid C pointers. Pass the struct symtab for the file
  145. and the amount to relocate by. */
  146. static struct symtab *
  147. relocate_symtab (root)
  148. struct symbol_root *root;
  149. {
  150. struct symtab *sp = (struct symtab *) xmalloc (sizeof (struct symtab));
  151. bzero (sp, sizeof (struct symtab));
  152. relocation = (int) root;
  153. text_relocation = root->textrel;
  154. data_relocation = root->datarel;
  155. bss_relocation = root->bssrel;
  156. data_start = root->databeg;
  157. bss_start = root->bssbeg;
  158. sp->filename = root->filename;
  159. sp->ldsymoff = root->ldsymoff;
  160. sp->language = root->language;
  161. sp->compilation = root->compilation;
  162. sp->version = root->version;
  163. sp->blockvector = root->blockvector;
  164. sp->typevector = root->typevector;
  165. sp->free_code = free_explicit;
  166. sp->free_ptr = (char *) root;
  167. RELOCATE (TYPEVECTOR (sp));
  168. RELOCATE (BLOCKVECTOR (sp));
  169. RELOCATE (sp->version);
  170. RELOCATE (sp->compilation);
  171. RELOCATE (sp->filename);
  172. relocate_typevector (TYPEVECTOR (sp));
  173. relocate_blockvector (BLOCKVECTOR (sp));
  174. return sp;
  175. }
  176. static void
  177. relocate_typevector (tv)
  178. struct typevector *tv;
  179. {
  180. register int ntypes = TYPEVECTOR_NTYPES (tv);
  181. register int i;
  182. for (i = 0; i < ntypes; i++)
  183. RELOCATE (TYPEVECTOR_TYPE (tv, i));
  184. for (i = 0; i < ntypes; i++)
  185. relocate_type (TYPEVECTOR_TYPE (tv, i));
  186. }
  187. static void
  188. relocate_blockvector (blp)
  189. register struct blockvector *blp;
  190. {
  191. register int nblocks = BLOCKVECTOR_NBLOCKS (blp);
  192. register int i;
  193. for (i = 0; i < nblocks; i++)
  194. RELOCATE (BLOCKVECTOR_BLOCK (blp, i));
  195. for (i = 0; i < nblocks; i++)
  196. relocate_block (BLOCKVECTOR_BLOCK (blp, i));
  197. }
  198. static void
  199. relocate_block (bp)
  200. register struct block *bp;
  201. {
  202. register int nsyms = BLOCK_NSYMS (bp);
  203. register int i;
  204. TEXT_RELOCATE (BLOCK_START (bp));
  205. TEXT_RELOCATE (BLOCK_END (bp));
  206. /* These two should not be recursively processed.
  207. The superblock need not be because all blocks are
  208. processed from relocate_blockvector.
  209. The function need not be because it will be processed
  210. under the block which is its scope. */
  211. RELOCATE (BLOCK_SUPERBLOCK (bp));
  212. RELOCATE (BLOCK_FUNCTION (bp));
  213. for (i = 0; i < nsyms; i++)
  214. RELOCATE (BLOCK_SYM (bp, i));
  215. for (i = 0; i < nsyms; i++)
  216. relocate_symbol (BLOCK_SYM (bp, i));
  217. }
  218. static void
  219. relocate_symbol (sp)
  220. register struct symbol *sp;
  221. {
  222. RELOCATE (SYMBOL_NAME (sp));
  223. if (SYMBOL_CLASS (sp) == LOC_BLOCK)
  224. {
  225. RELOCATE (SYMBOL_BLOCK_VALUE (sp));
  226. /* We can assume the block that belongs to this symbol
  227. is not relocated yet, since it comes after
  228. the block that contains this symbol. */
  229. BLOCK_FUNCTION (SYMBOL_BLOCK_VALUE (sp)) = sp;
  230. UNRELOCATE (BLOCK_FUNCTION (SYMBOL_BLOCK_VALUE (sp)));
  231. }
  232. else if (SYMBOL_CLASS (sp) == LOC_STATIC)
  233. CORE_RELOCATE (SYMBOL_VALUE (sp));
  234. else if (SYMBOL_CLASS (sp) == LOC_LABEL)
  235. TEXT_RELOCATE (SYMBOL_VALUE (sp));
  236. RELOCATE (SYMBOL_TYPE (sp));
  237. }
  238. /* We cannot come up with an a priori spanning tree
  239. for the network of types, since types can be used
  240. for many symbols and also as components of other types.
  241. Therefore, we need to be able to mark types that we
  242. already have relocated (or are already in the middle of relocating)
  243. as in a garbage collector. */
  244. static void
  245. relocate_type (tp)
  246. register struct type *tp;
  247. {
  248. register int nfields = TYPE_NFIELDS (tp);
  249. register int i;
  250. RELOCATE (TYPE_NAME (tp));
  251. RELOCATE (TYPE_TARGET_TYPE (tp));
  252. RELOCATE (TYPE_FIELDS (tp));
  253. RELOCATE (TYPE_POINTER_TYPE (tp));
  254. for (i = 0; i < nfields; i++)
  255. {
  256. RELOCATE (TYPE_FIELD_TYPE (tp, i));
  257. RELOCATE (TYPE_FIELD_NAME (tp, i));
  258. }
  259. }
  260. /* Read symsegs from file named NAME open on DESC,
  261. make symtabs from them, and return a chain of them.
  262. Assumes DESC is prepositioned at the end of the string table,
  263. just before the symsegs if there are any. */
  264. struct symtab *
  265. read_symsegs (desc, name)
  266. int desc;
  267. char *name;
  268. {
  269. struct symbol_root root;
  270. register char *data;
  271. register struct symtab *sp, *chain = 0;
  272. register int len;
  273. while (1)
  274. {
  275. len = myread (desc, &root, sizeof root, name);
  276. if (len == 0 || root.format == 0)
  277. break;
  278. if (root.format != 1 ||
  279. root.length < sizeof root)
  280. error ("Invalid symbol segment format code");
  281. data = (char *) xmalloc (root.length);
  282. bcopy (&root, data, sizeof root);
  283. len = myread (desc, data + sizeof root,
  284. root.length - sizeof root, name);
  285. sp = relocate_symtab (data);
  286. sp->next = chain;
  287. chain = sp;
  288. }
  289. return chain;
  290. }
  291. static int block_depth ();
  292. static void print_spaces ();
  293. static void print_symbol ();
  294. print_symtabs (filename)
  295. char *filename;
  296. {
  297. FILE *outfile;
  298. register struct symtab *s;
  299. register int i, j;
  300. int len, line, blen;
  301. register struct linetable *l;
  302. struct blockvector *bv;
  303. register struct block *b;
  304. int depth;
  305. if (filename == 0)
  306. error_no_arg ("file to write symbol data in");
  307. outfile = fopen (filename, "w");
  308. for (s = symtab_list; s; s = s->next)
  309. {
  310. /* First print the line table. */
  311. fprintf (outfile, "Symtab for file %s\n\n", s->filename);
  312. fprintf (outfile, "Line table:\n\n");
  313. l = LINETABLE (s);
  314. len = l->nitems;
  315. for (i = 0; i < len; i++)
  316. {
  317. if (l->item[i] < 0)
  318. line = - l->item[i] - 1;
  319. else
  320. fprintf (outfile, " line %d at %x\n", ++line, l->item[i]);
  321. }
  322. /* Now print the block info. */
  323. fprintf (outfile, "\nBlockvector:\n\n");
  324. bv = BLOCKVECTOR (s);
  325. len = BLOCKVECTOR_NBLOCKS (bv);
  326. for (i = 0; i < len; i++)
  327. {
  328. b = BLOCKVECTOR_BLOCK (bv, i);
  329. depth = block_depth (b) * 2;
  330. print_spaces (depth, outfile);
  331. fprintf (outfile, "block #%03d (object 0x%x) ", i, b);
  332. fprintf (outfile, "[0x%x..0x%x]", BLOCK_START (b), BLOCK_END (b));
  333. if (BLOCK_SUPERBLOCK (b))
  334. fprintf (outfile, " (under 0x%x)", BLOCK_SUPERBLOCK (b));
  335. if (BLOCK_FUNCTION (b))
  336. fprintf (outfile, " %s", SYMBOL_NAME (BLOCK_FUNCTION (b)));
  337. fputc ('\n', outfile);
  338. blen = BLOCK_NSYMS (b);
  339. for (j = 0; j < blen; j++)
  340. {
  341. print_symbol (BLOCK_SYM (b, j), depth + 1, outfile);
  342. }
  343. }
  344. fprintf (outfile, "\n\n");
  345. }
  346. fclose (outfile);
  347. }
  348. static void
  349. print_symbol (symbol, depth, outfile)
  350. struct symbol *symbol;
  351. int depth;
  352. FILE *outfile;
  353. {
  354. print_spaces (depth, outfile);
  355. if (SYMBOL_NAMESPACE (symbol) == LABEL_NAMESPACE)
  356. {
  357. fprintf (outfile, "label %s at 0x%x", SYMBOL_NAME (symbol),
  358. SYMBOL_VALUE (symbol));
  359. return;
  360. }
  361. if (SYMBOL_NAMESPACE (symbol) == STRUCT_NAMESPACE)
  362. {
  363. if (TYPE_NAME (SYMBOL_TYPE (symbol)))
  364. {
  365. type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
  366. }
  367. else
  368. {
  369. fprintf (outfile, "%s %s = ",
  370. (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
  371. ? "enum"
  372. : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
  373. ? "struct" : "union")),
  374. SYMBOL_NAME (symbol));
  375. type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
  376. }
  377. fprintf (outfile, ";\n");
  378. }
  379. else
  380. {
  381. if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
  382. fprintf (outfile, "typedef ");
  383. if (SYMBOL_TYPE (symbol))
  384. {
  385. type_print_1 (SYMBOL_TYPE (symbol), SYMBOL_NAME (symbol),
  386. outfile, 1, depth);
  387. fprintf (outfile, "; ");
  388. }
  389. else
  390. fprintf (outfile, "%s ", SYMBOL_NAME (symbol));
  391. switch (SYMBOL_CLASS (symbol))
  392. {
  393. case LOC_CONST:
  394. fprintf (outfile, "const %d (0x%x),",
  395. SYMBOL_VALUE (symbol), SYMBOL_VALUE (symbol));
  396. break;
  397. case LOC_STATIC:
  398. fprintf (outfile, "static at 0x%x,", SYMBOL_VALUE (symbol));
  399. break;
  400. case LOC_REGISTER:
  401. fprintf (outfile, "register %d,", SYMBOL_VALUE (symbol));
  402. break;
  403. case LOC_ARG:
  404. fprintf (outfile, "arg at 0x%x,", SYMBOL_VALUE (symbol));
  405. break;
  406. case LOC_LOCAL:
  407. fprintf (outfile, "local at 0x%x,", SYMBOL_VALUE (symbol));
  408. break;
  409. case LOC_TYPEDEF:
  410. break;
  411. case LOC_LABEL:
  412. fprintf (outfile, "label at 0x%x", SYMBOL_VALUE (symbol));
  413. break;
  414. case LOC_BLOCK:
  415. fprintf (outfile, "block (object 0x%x) starting at 0x%x,",
  416. SYMBOL_VALUE (symbol),
  417. BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)));
  418. break;
  419. }
  420. }
  421. fprintf (outfile, "\n");
  422. }
  423. /* Return the nexting depth of a block within other blocks in its symtab. */
  424. static int
  425. block_depth (block)
  426. struct block *block;
  427. {
  428. register int i = 0;
  429. while (block = BLOCK_SUPERBLOCK (block)) i++;
  430. return i;
  431. }
  432. static
  433. initialize ()
  434. {
  435. add_com ("printsyms", class_obscure, print_symtabs,
  436. "Print dump of current symbol definitions to file OUTFILE.");
  437. }
  438. END_FILE