symtab.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /* Symbol table lookup for the GNU debugger, 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 "param.h"
  21. #include <stdio.h>
  22. #include <obstack.h>
  23. START_FILE
  24. /* Allocate an obstack to hold objects that should be freed
  25. when we load a new symbol table.
  26. This includes the symbols made by dbxread
  27. and the types that are not permanent. */
  28. struct obstack obstack1;
  29. struct obstack *symbol_obstack = &obstack1;
  30. /* These variables point to the objects
  31. representing the predefined C data types. */
  32. struct type *builtin_type_void;
  33. struct type *builtin_type_char;
  34. struct type *builtin_type_short;
  35. struct type *builtin_type_int;
  36. struct type *builtin_type_long;
  37. struct type *builtin_type_unsigned_char;
  38. struct type *builtin_type_unsigned_short;
  39. struct type *builtin_type_unsigned_int;
  40. struct type *builtin_type_unsigned_long;
  41. struct type *builtin_type_float;
  42. struct type *builtin_type_double;
  43. /* Lookup the symbol table of a source file named NAME. */
  44. struct symtab *
  45. lookup_symtab (name)
  46. char *name;
  47. {
  48. register struct symtab *s;
  49. register char *copy;
  50. for (s = symtab_list; s; s = s->next)
  51. if (!strcmp (name, s->filename))
  52. return s;
  53. /* If name not found as specified, see if adding ".c" helps. */
  54. copy = (char *) alloca (strlen (name) + 3);
  55. strcpy (copy, name);
  56. strcat (copy, ".c");
  57. for (s = symtab_list; s; s = s->next)
  58. if (!strcmp (copy, s->filename))
  59. return s;
  60. return 0;
  61. }
  62. /* Lookup a typedef or primitive type named NAME,
  63. visible in lexical block BLOCK.
  64. If NOERR is nonzero, return zero if NAME is not suitably defined. */
  65. struct type *
  66. lookup_typename (name, block, noerr)
  67. char *name;
  68. struct block *block;
  69. int noerr;
  70. {
  71. register struct symbol *sym = lookup_symbol (name, block, VAR_NAMESPACE);
  72. if (sym == 0 || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
  73. {
  74. if (!strcmp (name, "int"))
  75. return builtin_type_int;
  76. if (!strcmp (name, "long"))
  77. return builtin_type_long;
  78. if (!strcmp (name, "short"))
  79. return builtin_type_short;
  80. if (!strcmp (name, "char"))
  81. return builtin_type_char;
  82. if (!strcmp (name, "float"))
  83. return builtin_type_float;
  84. if (!strcmp (name, "double"))
  85. return builtin_type_double;
  86. if (!strcmp (name, "void"))
  87. return builtin_type_void;
  88. if (noerr)
  89. return 0;
  90. error ("No type named %s.", name);
  91. }
  92. return SYMBOL_TYPE (sym);
  93. }
  94. struct type *
  95. lookup_unsigned_typename (name)
  96. char *name;
  97. {
  98. if (!strcmp (name, "int"))
  99. return builtin_type_unsigned_int;
  100. if (!strcmp (name, "long"))
  101. return builtin_type_unsigned_long;
  102. if (!strcmp (name, "short"))
  103. return builtin_type_unsigned_short;
  104. if (!strcmp (name, "char"))
  105. return builtin_type_unsigned_char;
  106. error ("No type named unsigned %s.", name);
  107. }
  108. /* Lookup a structure type named "struct NAME",
  109. visible in lexical block BLOCK. */
  110. struct type *
  111. lookup_struct (name, block)
  112. char *name;
  113. struct block *block;
  114. {
  115. register struct symbol *sym = lookup_symbol (name, block, STRUCT_NAMESPACE);
  116. if (sym == 0)
  117. error ("No struct type named %s.", name);
  118. if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
  119. error ("This context has union or enum %s, not a struct.", name);
  120. return SYMBOL_TYPE (sym);
  121. }
  122. /* Lookup a union type named "union NAME",
  123. visible in lexical block BLOCK. */
  124. struct type *
  125. lookup_union (name, block)
  126. char *name;
  127. struct block *block;
  128. {
  129. register struct symbol *sym = lookup_symbol (name, block, STRUCT_NAMESPACE);
  130. if (sym == 0)
  131. error ("No union type named %s.", name);
  132. if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION)
  133. error ("This context has struct or enum %s, not a union.", name);
  134. return SYMBOL_TYPE (sym);
  135. }
  136. /* Lookup an enum type named "enum NAME",
  137. visible in lexical block BLOCK. */
  138. struct type *
  139. lookup_enum (name, block)
  140. char *name;
  141. struct block *block;
  142. {
  143. register struct symbol *sym = lookup_symbol (name, block, STRUCT_NAMESPACE);
  144. if (sym == 0)
  145. error ("No enum type named %s.", name);
  146. if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
  147. error ("This context has struct or union %s, not an enum.", name);
  148. return SYMBOL_TYPE (sym);
  149. }
  150. /* Given a type TYPE, return a type of pointers to that type.
  151. May need to construct such a type if this is the first use. */
  152. struct type *
  153. lookup_pointer_type (type)
  154. struct type *type;
  155. {
  156. register struct type *ptype = TYPE_POINTER_TYPE (type);
  157. if (ptype) return ptype;
  158. /* This is the first time anyone wanted a pointer to a TYPE. */
  159. if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  160. ptype = (struct type *) xmalloc (sizeof (struct type));
  161. else
  162. ptype = (struct type *) obstack_alloc (symbol_obstack,
  163. sizeof (struct type));
  164. bzero (ptype, sizeof (struct type));
  165. TYPE_TARGET_TYPE (ptype) = type;
  166. TYPE_POINTER_TYPE (type) = ptype;
  167. /* New type is permanent if type pointed to is permanent. */
  168. if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  169. TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
  170. /* We assume the machine has only one representation for pointers! */
  171. TYPE_LENGTH (ptype) = sizeof (char *);
  172. TYPE_CODE (ptype) = TYPE_CODE_PTR;
  173. return ptype;
  174. }
  175. /* Given a type TYPE, return a type of functions that return that type.
  176. May need to construct such a type if this is the first use. */
  177. struct type *
  178. lookup_function_type (type)
  179. struct type *type;
  180. {
  181. register struct type *ptype = TYPE_FUNCTION_TYPE (type);
  182. if (ptype) return ptype;
  183. /* This is the first time anyone wanted a function returning a TYPE. */
  184. if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  185. ptype = (struct type *) xmalloc (sizeof (struct type));
  186. else
  187. ptype = (struct type *) obstack_alloc (symbol_obstack,
  188. sizeof (struct type));
  189. bzero (ptype, sizeof (struct type));
  190. TYPE_TARGET_TYPE (ptype) = type;
  191. TYPE_FUNCTION_TYPE (type) = ptype;
  192. /* New type is permanent if type returned is permanent. */
  193. if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  194. TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
  195. TYPE_LENGTH (ptype) = 1;
  196. TYPE_CODE (ptype) = TYPE_CODE_FUNC;
  197. TYPE_NFIELDS (ptype) = 0;
  198. return ptype;
  199. }
  200. /* Smash TYPE to be a type of pointers to TO_TYPE.
  201. If TO_TYPE is not permanent and has no pointer-type yet,
  202. record TYPE as its pointer-type. */
  203. void
  204. smash_to_pointer_type (type, to_type)
  205. struct type *type, *to_type;
  206. {
  207. bzero (type, sizeof (struct type));
  208. TYPE_TARGET_TYPE (type) = to_type;
  209. /* We assume the machine has only one representation for pointers! */
  210. TYPE_LENGTH (type) = sizeof (char *);
  211. TYPE_CODE (type) = TYPE_CODE_PTR;
  212. if (TYPE_POINTER_TYPE (to_type) == 0
  213. && !(TYPE_FLAGS (type) & TYPE_FLAG_PERM))
  214. {
  215. TYPE_POINTER_TYPE (to_type) = type;
  216. }
  217. }
  218. /* Smash TYPE to be a type of functions returning TO_TYPE.
  219. If TO_TYPE is not permanent and has no function-type yet,
  220. record TYPE as its function-type. */
  221. void
  222. smash_to_function_type (type, to_type)
  223. struct type *type, *to_type;
  224. {
  225. bzero (type, sizeof (struct type));
  226. TYPE_TARGET_TYPE (type) = to_type;
  227. TYPE_LENGTH (type) = 1;
  228. TYPE_CODE (type) = TYPE_CODE_FUNC;
  229. TYPE_NFIELDS (type) = 0;
  230. if (TYPE_FUNCTION_TYPE (to_type) == 0
  231. && !(TYPE_FLAGS (type) & TYPE_FLAG_PERM))
  232. {
  233. TYPE_FUNCTION_TYPE (to_type) = type;
  234. }
  235. }
  236. static struct symbol *lookup_block_symbol ();
  237. /* Find the definition for a specified symbol name NAME
  238. in namespace NAMESPACE, visible from lexical block BLOCK.
  239. Returns the struct symbol pointer, or zero if no symbol is found. */
  240. struct symbol *
  241. lookup_symbol (name, block, namespace)
  242. char *name;
  243. register struct block *block;
  244. enum namespace namespace;
  245. {
  246. register int i, n;
  247. register struct symbol *sym;
  248. register struct symtab *s;
  249. struct blockvector *bv;
  250. /* Search specified block and its superiors. */
  251. while (block != 0)
  252. {
  253. sym = lookup_block_symbol (block, name, namespace);
  254. if (sym) return sym;
  255. block = BLOCK_SUPERBLOCK (block);
  256. }
  257. /* Now search all symtabs' global blocks. */
  258. for (s = symtab_list; s; s = s->next)
  259. {
  260. bv = BLOCKVECTOR (s);
  261. block = BLOCKVECTOR_BLOCK (bv, 0);
  262. sym = lookup_block_symbol (block, name, namespace);
  263. if (sym) return sym;
  264. }
  265. /* Now search all symtabs' per-file blocks.
  266. Not strictly correct, but more useful than an error. */
  267. for (s = symtab_list; s; s = s->next)
  268. {
  269. bv = BLOCKVECTOR (s);
  270. block = BLOCKVECTOR_BLOCK (bv, 1);
  271. sym = lookup_block_symbol (block, name, namespace);
  272. if (sym) return sym;
  273. }
  274. return 0;
  275. }
  276. /* Look for a symbol in block BLOCK using binary search. */
  277. static struct symbol *
  278. lookup_block_symbol (block, name, namespace)
  279. register struct block *block;
  280. char *name;
  281. enum namespace namespace;
  282. {
  283. register int bot, top, inc;
  284. register struct symbol *sym;
  285. top = BLOCK_NSYMS (block);
  286. bot = 0;
  287. /* First, advance BOT to not far before
  288. the first symbol whose name is NAME. */
  289. while (1)
  290. {
  291. inc = (top - bot + 1);
  292. /* No need to keep binary searching for the last few bits worth. */
  293. if (inc < 7)
  294. break;
  295. inc >>= 1;
  296. sym = BLOCK_SYM (block, bot + inc);
  297. if (strcmp (SYMBOL_NAME (sym), name) < 0)
  298. bot += inc;
  299. else
  300. top = bot + inc;
  301. }
  302. /* Now scan forward until we run out of symbols,
  303. find one whose name is greater than NAME,
  304. or find one we want.
  305. If there is more than one symbol with the right name and namespace,
  306. we return the first one. dbxread.c is careful to make sure
  307. that if one is a register then it comes first. */
  308. top = BLOCK_NSYMS (block);
  309. while (bot < top)
  310. {
  311. sym = BLOCK_SYM (block, bot);
  312. inc = strcmp (SYMBOL_NAME (sym), name);
  313. if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
  314. return sym;
  315. if (inc > 0)
  316. return 0;
  317. bot++;
  318. }
  319. return 0;
  320. }
  321. /* Return the symbol for the function which contains a specified
  322. lexical block, described by a struct block BL. */
  323. struct symbol *
  324. block_function (bl)
  325. struct block *bl;
  326. {
  327. while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
  328. bl = BLOCK_SUPERBLOCK (bl);
  329. return BLOCK_FUNCTION (bl);
  330. }
  331. /* Subroutine of find_pc_line */
  332. static struct symtab *
  333. find_pc_symtab (pc)
  334. register CORE_ADDR pc;
  335. {
  336. register struct block *b;
  337. struct blockvector *bv;
  338. register struct symtab *s;
  339. /* Search all symtabs for one whose file contains our pc */
  340. for (s = symtab_list; s; s = s->next)
  341. {
  342. bv = BLOCKVECTOR (s);
  343. b = BLOCKVECTOR_BLOCK (bv, 0);
  344. if (BLOCK_START (b) <= pc
  345. && BLOCK_END (b) > pc)
  346. break;
  347. }
  348. return s;
  349. }
  350. /* Find the source file and line number for a given PC value.
  351. Return a structure containing a symtab pointer, a line number,
  352. and a pc range for the entire source line.
  353. The value's .pc field is NOT the specified pc.
  354. NOTCURRENT nonzero means, if specified pc is on a line boundary,
  355. use the line that ends there. Otherwise, in that case, the line
  356. that begins there is used. */
  357. struct symtab_and_line
  358. find_pc_line (pc, notcurrent)
  359. CORE_ADDR pc;
  360. int notcurrent;
  361. {
  362. struct symtab *s;
  363. register struct linetable *l;
  364. register int len;
  365. register int i, item;
  366. int line;
  367. struct symtab_and_line value;
  368. struct blockvector *bv;
  369. /* Info on best line seen so far, and where it starts, and its file. */
  370. int best_line = 0;
  371. CORE_ADDR best_pc = 0;
  372. CORE_ADDR best_end = 0;
  373. struct symtab *best_symtab = 0;
  374. /* Store here the first line number
  375. of a file which contains the line at the smallest pc after PC.
  376. If we don't find a line whose range contains PC,
  377. we will use a line one less than this,
  378. with a range from the start of that file to the first line's pc. */
  379. int alt_line = 0;
  380. CORE_ADDR alt_pc = 0;
  381. struct symtab *alt_symtab = 0;
  382. /* Info on best line seen in this file. */
  383. int prev_line;
  384. CORE_ADDR prev_pc;
  385. /* Info on first line of this file. */
  386. int first_line;
  387. CORE_ADDR first_pc;
  388. /* If this pc is not from the current frame,
  389. it is the address of the end of a call instruction.
  390. Quite likely that is the start of the following statement.
  391. But what we want is the statement containing the instruction.
  392. Fudge the pc to make sure we get that. */
  393. if (notcurrent) pc -= 1;
  394. s = find_pc_symtab (pc);
  395. if (s == 0)
  396. {
  397. value.symtab = 0;
  398. value.line = 0;
  399. value.pc = pc;
  400. return value;
  401. }
  402. bv = BLOCKVECTOR (s);
  403. /* Look at all the symtabs that share this blockvector.
  404. They all have the same apriori range, that we found was right;
  405. but they have different line tables. */
  406. for (; s && BLOCKVECTOR (s) == bv; s = s->next)
  407. {
  408. /* Find the best line in this symtab. */
  409. l = LINETABLE (s);
  410. len = l->nitems;
  411. prev_line = -1;
  412. first_line = -1;
  413. for (i = 0; i < len; i++)
  414. {
  415. item = l->item[i];
  416. if (item < 0)
  417. line = - item - 1;
  418. else
  419. {
  420. line++;
  421. if (first_line < 0)
  422. {
  423. first_line = line;
  424. first_pc = item;
  425. }
  426. /* Return the last line that did not start after PC. */
  427. if (pc >= item)
  428. {
  429. prev_line = line;
  430. prev_pc = item;
  431. }
  432. else
  433. break;
  434. }
  435. }
  436. /* Is this file's best line closer than the best in the other files?
  437. If so, record this file, and its best line, as best so far. */
  438. if (prev_line >= 0 && prev_pc > best_pc)
  439. {
  440. best_pc = prev_pc;
  441. best_line = prev_line;
  442. best_symtab = s;
  443. if (i < len)
  444. best_end = item;
  445. else
  446. best_end = 0;
  447. }
  448. /* Is this file's first line closer than the first lines of other files?
  449. If so, record this file, and its first line, as best alternate. */
  450. if (first_line >= 0 && first_pc > pc
  451. && (alt_pc == 0 || first_pc < alt_pc))
  452. {
  453. alt_pc = first_pc;
  454. alt_line = first_line;
  455. alt_symtab = s;
  456. }
  457. }
  458. if (best_symtab == 0)
  459. {
  460. value.symtab = alt_symtab;
  461. value.line = alt_line - 1;
  462. value.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, 0));
  463. value.end = alt_pc;
  464. }
  465. else
  466. {
  467. value.symtab = best_symtab;
  468. value.line = best_line;
  469. value.pc = best_pc;
  470. value.end = (best_end ? best_end
  471. : (alt_pc ? alt_pc
  472. : BLOCK_END (BLOCKVECTOR_BLOCK (bv, 0))));
  473. }
  474. return value;
  475. }
  476. /* Find the range of pc values in a line.
  477. Store the starting pc of the line into *STARTPTR
  478. and the ending pc (start of next line) into *ENDPTR.
  479. Returns 1 to indicate success.
  480. Returns 0 if could not find the specified line. */
  481. int
  482. find_line_pc_range (symtab, thisline, startptr, endptr)
  483. struct symtab *symtab;
  484. int thisline;
  485. CORE_ADDR *startptr, *endptr;
  486. {
  487. register struct linetable *l;
  488. register int i, line, item;
  489. int len;
  490. int return_next = 0;
  491. register CORE_ADDR prev_pc;
  492. CORE_ADDR last_pc;
  493. if (symtab == 0)
  494. return 0;
  495. l = LINETABLE (symtab);
  496. len = l->nitems;
  497. prev_pc = -1;
  498. for (i = 0; i < len; i++)
  499. {
  500. item = l->item[i];
  501. if (item < 0)
  502. line = - item - 1;
  503. else
  504. {
  505. line++;
  506. /* As soon as we find a line following the specified one
  507. we know the end pc and can return. */
  508. if (line > thisline)
  509. {
  510. /* If we have not seen an entry for the specified line,
  511. assume that means the specified line has zero bytes. */
  512. *startptr = prev_pc == -1 ? item : prev_pc;
  513. *endptr = item;
  514. return 1;
  515. }
  516. /* If we see an entry for the specified line,
  517. it gives the beginning. */
  518. if (line == thisline)
  519. prev_pc = item;
  520. last_pc = item;
  521. }
  522. }
  523. if (return_next)
  524. {
  525. /* Range of file's last line is from line's pc to file's end pc. */
  526. *startptr = last_pc;
  527. *endptr = BLOCK_END (BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), 0));
  528. return 1;
  529. }
  530. return 0;
  531. }
  532. /* Find the PC value for a given source file and line number.
  533. Returns zero for invalid line number.
  534. The source file is specified with a struct symtab. */
  535. CORE_ADDR
  536. find_line_pc (symtab, line)
  537. struct symtab *symtab;
  538. int line;
  539. {
  540. register struct linetable *l;
  541. register int len;
  542. register int i;
  543. register int item;
  544. register int nextline = -1;
  545. if (line <= 0)
  546. return 0;
  547. l = LINETABLE (symtab);
  548. len = l->nitems;
  549. for (i = 0; i < len; i++)
  550. {
  551. item = l->item[i];
  552. if (item < 0)
  553. nextline = - item - 1;
  554. else
  555. {
  556. nextline++;
  557. if (line <= nextline)
  558. return item;
  559. }
  560. }
  561. return 0;
  562. }
  563. int
  564. find_pc_line_pc_range (pc, startptr, endptr)
  565. CORE_ADDR pc;
  566. CORE_ADDR *startptr, *endptr;
  567. {
  568. struct symtab_and_line sal;
  569. sal = find_pc_line (pc, 0);
  570. *startptr = sal.pc;
  571. *endptr = sal.end;
  572. return sal.symtab != 0;
  573. }
  574. /* Parse a string that specifies a line number.
  575. Pass the address of a char * variable; that variable will be
  576. advanced over the characters actually parsed.
  577. The string can be:
  578. LINENUM -- that line number in current file. PC returned is 0.
  579. FILE:LINENUM -- that line in that file. PC returned is 0.
  580. FUNCTION -- line number of openbrace of that function.
  581. PC returned is the start of the function.
  582. FILE:FUNCTION -- likewise, but prefer functions in that file.
  583. *EXPR -- line in which address EXPR appears.
  584. FUNCTION may be an undebuggable function found in misc_function_vector.
  585. If the argument FUNFIRSTLINE is nonzero, we want the first line
  586. of real code inside a function when a function is specified.
  587. DEFAULT_SYMTAB specifies the file to use if none is specified.
  588. It defaults to current_source_symtab.
  589. DEFAULT_LINE specifies the line number to use for relative
  590. line numbers (that start with signs). Defaults to current_source_line.
  591. Note that it is possible to return zero for the symtab
  592. if no file is validly specified. Callers must check that.
  593. Also, the line number returned may be invalid. */
  594. struct symtab_and_line
  595. decode_line_1 (argptr, funfirstline, default_symtab, default_line)
  596. char **argptr;
  597. int funfirstline;
  598. struct symtab *default_symtab;
  599. int default_line;
  600. {
  601. struct symtab_and_line value;
  602. register char *p, *p1;
  603. register struct symtab *s;
  604. register struct symbol *sym;
  605. register CORE_ADDR pc;
  606. register int i;
  607. char *copy;
  608. /* Defaults have defaults. */
  609. if (default_symtab == 0)
  610. {
  611. default_symtab = current_source_symtab;
  612. default_line = current_source_line;
  613. }
  614. /* See if arg is *PC */
  615. if (**argptr == '*')
  616. {
  617. (*argptr)++;
  618. pc = parse_and_eval_address_1 (argptr);
  619. value = find_pc_line (pc, 0);
  620. value.pc = pc;
  621. return value;
  622. }
  623. /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
  624. if (symtab_list == 0)
  625. error ("No symbol table is loaded. Use the \"symbol-file\" command.");
  626. s = 0;
  627. for (p = *argptr; *p; p++)
  628. {
  629. if (p[0] == ':' || p[0] == ' ' || p[0] == '\t')
  630. break;
  631. }
  632. while (p[0] == ' ' || p[0] == '\t') p++;
  633. if (p[0] == ':')
  634. {
  635. /* Extract the file name. */
  636. p1 = p;
  637. while (p != *argptr && p[-1] == ' ') --p;
  638. copy = (char *) alloca (p - *argptr + 1);
  639. bcopy (*argptr, copy, p - *argptr);
  640. copy[p - *argptr] = 0;
  641. /* Find that file's data. */
  642. s = lookup_symtab (copy);
  643. if (s == 0)
  644. error ("No source file named %s.", copy);
  645. /* Discard the file name from the arg. */
  646. p = p1 + 1;
  647. while (*p == ' ' || *p == '\t') p++;
  648. *argptr = p;
  649. }
  650. /* s is specified file's symtab, or 0 if no file specified.
  651. arg no longer contains the file name. */
  652. /* Check whether arg is all digits (and sign) */
  653. p = *argptr;
  654. if (*p == '-' || *p == '+') p++;
  655. while (*p >= '0' && *p <= '9')
  656. p++;
  657. if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ','))
  658. {
  659. /* We found a token consisting of all digits -- at least one digit. */
  660. enum sign {none, plus, minus} sign = none;
  661. if (**argptr == '+')
  662. sign = plus, (*argptr)++;
  663. else if (**argptr == '-')
  664. sign = minus, (*argptr)++;
  665. value.line = atoi (*argptr);
  666. switch (sign)
  667. {
  668. case plus:
  669. if (p == *argptr)
  670. value.line = 5;
  671. if (s == 0)
  672. value.line = default_line + value.line;
  673. break;
  674. case minus:
  675. if (p == *argptr)
  676. value.line = 15;
  677. if (s == 0)
  678. value.line = default_line - value.line;
  679. else
  680. value.line = 1;
  681. break;
  682. }
  683. while (*p == ' ' || *p == '\t') p++;
  684. *argptr = p;
  685. if (s == 0)
  686. s = default_symtab;
  687. value.symtab = s;
  688. value.pc = 0;
  689. return value;
  690. }
  691. /* Arg token is not digits => try it as a function name
  692. Find the next token (everything up to end or next whitespace). */
  693. p = *argptr;
  694. while (*p && *p != ' ' && *p != '\t' && *p != ',') p++;
  695. copy = (char *) alloca (p - *argptr + 1);
  696. bcopy (*argptr, copy, p - *argptr);
  697. copy[p - *argptr] = 0;
  698. while (*p == ' ' || *p == '\t') p++;
  699. *argptr = p;
  700. /* Look up that token as a function.
  701. If file specified, use that file's per-file block to start with. */
  702. sym = lookup_symbol (copy, s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1) : 0,
  703. VAR_NAMESPACE);
  704. if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  705. {
  706. /* Arg is the name of a function */
  707. pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
  708. if (funfirstline)
  709. SKIP_PROLOGUE (pc);
  710. value = find_pc_line (pc, 0);
  711. value.pc = (value.end && value.pc != pc) ? value.end : pc;
  712. return value;
  713. }
  714. if (sym)
  715. error ("%s is not a function.", copy);
  716. for (i = 0; i < misc_function_count; i++)
  717. if (!strcmp (misc_function_vector[i].name, copy))
  718. {
  719. value.symtab = 0;
  720. value.line = 0;
  721. value.pc = misc_function_vector[i].address + FUNCTION_START_OFFSET;
  722. if (funfirstline)
  723. SKIP_PROLOGUE (value.pc);
  724. return value;
  725. }
  726. error ("Function %s not defined.", copy);
  727. }
  728. struct symtab_and_line
  729. decode_line_spec (string, funfirstline)
  730. char *string;
  731. int funfirstline;
  732. {
  733. struct symtab_and_line sal;
  734. if (string == 0)
  735. error ("Empty line specification.");
  736. sal = decode_line_1 (&string, funfirstline,
  737. current_source_symtab, current_source_line);
  738. if (*string)
  739. error ("Junk at end of line specification: %s", string);
  740. return sal;
  741. }
  742. static void
  743. sources_info ()
  744. {
  745. register struct symtab *s;
  746. register int column = 0;
  747. if (symtab_list == 0)
  748. {
  749. printf ("No symbol table is loaded.\n");
  750. return;
  751. }
  752. printf ("Source files for which symbol table is known:\n");
  753. for (s = symtab_list; s; s = s->next)
  754. {
  755. if (column != 0 && column + strlen (s->filename) >= 70)
  756. {
  757. printf ("\n");
  758. column = 0;
  759. }
  760. else if (column != 0)
  761. {
  762. printf (" ");
  763. column++;
  764. }
  765. printf ("%s", s->filename);
  766. column += strlen (s->filename);
  767. if (s->next)
  768. {
  769. printf (",");
  770. column++;
  771. }
  772. }
  773. printf ("\n");
  774. }
  775. /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
  776. If CLASS is zero, list all symbols except functions and type names.
  777. If CLASS is 1, list only functions.
  778. If CLASS is 2, list only type names. */
  779. #define MORE \
  780. { print_count++; \
  781. if (print_count >= 21) \
  782. { printf ("--Type Return to print more--"); \
  783. print_count = 0; \
  784. fflush (stdout); \
  785. read_line (); } }
  786. static void
  787. list_symbols (regexp, class)
  788. char *regexp;
  789. int class;
  790. {
  791. register struct symtab *s;
  792. register struct blockvector *bv;
  793. struct blockvector *prev_bv = 0;
  794. register struct block *b;
  795. register int i, j;
  796. register struct symbol *sym;
  797. char *val;
  798. int found_in_file;
  799. static char *classnames[]
  800. = {"variable", "function", "type"};
  801. int print_count = 0;
  802. if (regexp)
  803. if (val = (char *) re_comp (regexp))
  804. error ("Invalid regexp: %s", val);
  805. printf (regexp
  806. ? "All %ss matching regular expression \"%s\":\n"
  807. : "All defined %ss:\n",
  808. classnames[class],
  809. regexp);
  810. for (s = symtab_list; s; s = s->next)
  811. {
  812. found_in_file = 0;
  813. bv = BLOCKVECTOR (s);
  814. /* Often many files share a blockvector.
  815. Scan each blockvector only once so that
  816. we don't get every symbol many times.
  817. It happens that the first symtab in the list
  818. for any given blockvector is the main file. */
  819. if (bv != prev_bv)
  820. for (i = 0; i < 2; i++)
  821. {
  822. b = BLOCKVECTOR_BLOCK (bv, i);
  823. for (j = 0; j < BLOCK_NSYMS (b); j++)
  824. {
  825. QUIT;
  826. sym = BLOCK_SYM (b, j);
  827. if ((regexp == 0 || re_exec (SYMBOL_NAME (sym)))
  828. && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
  829. && SYMBOL_CLASS (sym) != LOC_BLOCK)
  830. || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
  831. || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)))
  832. {
  833. if (!found_in_file)
  834. {
  835. printf ("\nFile %s:\n", s->filename);
  836. print_count += 2;
  837. }
  838. found_in_file = 1;
  839. MORE;
  840. if (i == 1)
  841. printf ("static ");
  842. type_print (SYMBOL_TYPE (sym),
  843. (SYMBOL_CLASS (sym) == LOC_TYPEDEF
  844. ? "" : SYMBOL_NAME (sym)),
  845. stdout, 0);
  846. printf (";\n");
  847. }
  848. }
  849. }
  850. prev_bv = bv;
  851. }
  852. }
  853. static void
  854. variables_info (regexp)
  855. char *regexp;
  856. {
  857. list_symbols (regexp, 0);
  858. }
  859. static void
  860. functions_info (regexp)
  861. char *regexp;
  862. {
  863. list_symbols (regexp, 1);
  864. }
  865. static void
  866. types_info (regexp)
  867. char *regexp;
  868. {
  869. list_symbols (regexp, 2);
  870. }
  871. /* Initialize the standard C scalar types. */
  872. static
  873. struct type *
  874. init_type (code, length, uns, name)
  875. enum type_code code;
  876. int length, uns;
  877. char *name;
  878. {
  879. register struct type *type;
  880. type = (struct type *) xmalloc (sizeof (struct type));
  881. bzero (type, sizeof *type);
  882. TYPE_CODE (type) = code;
  883. TYPE_LENGTH (type) = length;
  884. TYPE_FLAGS (type) = uns ? TYPE_FLAG_UNSIGNED : 0;
  885. TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
  886. TYPE_NFIELDS (type) = 0;
  887. TYPE_NAME (type) = name;
  888. return type;
  889. }
  890. static
  891. initialize ()
  892. {
  893. add_info ("variables", variables_info,
  894. "All global and static variable names, or those matching REGEXP.");
  895. add_info ("functions", functions_info,
  896. "All function names, or those matching REGEXP.");
  897. add_info ("types", types_info,
  898. "All types names, or those matching REGEXP.");
  899. add_info ("sources", sources_info,
  900. "Source files in the program.");
  901. obstack_init (symbol_obstack);
  902. builtin_type_void = init_type (TYPE_CODE_VOID, 0, 0, "void");
  903. builtin_type_float = init_type (TYPE_CODE_FLT, sizeof (float), 0, "float");
  904. builtin_type_double = init_type (TYPE_CODE_FLT, sizeof (double), 0, "double");
  905. builtin_type_char = init_type (TYPE_CODE_INT, sizeof (char), 0, "char");
  906. builtin_type_short = init_type (TYPE_CODE_INT, sizeof (short), 0, "short");
  907. builtin_type_long = init_type (TYPE_CODE_INT, sizeof (long), 0, "long");
  908. builtin_type_int = init_type (TYPE_CODE_INT, sizeof (int), 0, "int");
  909. builtin_type_unsigned_char = init_type (TYPE_CODE_INT, sizeof (char), 1, "unsigned char");
  910. builtin_type_unsigned_short = init_type (TYPE_CODE_INT, sizeof (short), 1, "unsigned short");
  911. builtin_type_unsigned_long = init_type (TYPE_CODE_INT, sizeof (long), 1, "unsigned long");
  912. builtin_type_unsigned_int = init_type (TYPE_CODE_INT, sizeof (int), 1, "unsigned int");
  913. }
  914. END_FILE