sym.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <mach.h>
  5. #define HUGEINT 0x7fffffff
  6. #define NNAME 20 /* a relic of the past */
  7. typedef struct txtsym Txtsym;
  8. typedef struct file File;
  9. typedef struct hist Hist;
  10. struct txtsym { /* Text Symbol table */
  11. int n; /* number of local vars */
  12. Sym **locals; /* array of ptrs to autos */
  13. Sym *sym; /* function symbol entry */
  14. };
  15. struct hist { /* Stack of include files & #line directives */
  16. char *name; /* Assumes names Null terminated in file */
  17. long line; /* line # where it was included */
  18. long offset; /* line # of #line directive */
  19. };
  20. struct file { /* Per input file header to history stack */
  21. long addr; /* address of first text sym */
  22. union {
  23. Txtsym *txt; /* first text symbol */
  24. Sym *sym; /* only during initilization */
  25. };
  26. int n; /* size of history stack */
  27. Hist *hist; /* history stack */
  28. };
  29. static int debug = 0;
  30. static Sym **autos; /* Base of auto variables */
  31. static File *files; /* Base of file arena */
  32. static int fmax; /* largest file path index */
  33. static Sym **fnames; /* file names path component table */
  34. static Sym **globals; /* globals by addr table */
  35. static Hist *hist; /* base of history stack */
  36. static int isbuilt; /* internal table init flag */
  37. static long nauto; /* number of automatics */
  38. static long nfiles; /* number of files */
  39. static long nglob; /* number of globals */
  40. static long nhist; /* number of history stack entries */
  41. static long nsym; /* number of symbols */
  42. static int ntxt; /* number of text symbols */
  43. static uchar *pcline; /* start of pc-line state table */
  44. static uchar *pclineend; /* end of pc-line table */
  45. static uchar *spoff; /* start of pc-sp state table */
  46. static uchar *spoffend; /* end of pc-sp offset table */
  47. static Sym *symbols; /* symbol table */
  48. static Txtsym *txt; /* Base of text symbol table */
  49. static long txtstart; /* start of text segment */
  50. static long txtend; /* end of text segment */
  51. static void cleansyms(void);
  52. static int decodename(Biobuf*, Sym*);
  53. static short *encfname(char*);
  54. static int fline(char*, int, long, Hist*, Hist**);
  55. static void fillsym(Sym*, Symbol*);
  56. static int findglobal(char*, Symbol*);
  57. static int findlocvar(Symbol*, char *, Symbol*);
  58. static int findtext(char*, Symbol*);
  59. static int hcomp(Hist*, short*);
  60. static int hline(File*, short*, ulong*);
  61. static void printhist(char*, Hist*, int);
  62. static int buildtbls(void);
  63. static int symcomp(void*, void*);
  64. static int symerrmsg(int, char*);
  65. static int txtcomp(void*, void*);
  66. static int filecomp(void*, void*);
  67. /*
  68. * initialize the symbol tables
  69. */
  70. int
  71. syminit(int fd, Fhdr *fp)
  72. {
  73. Sym *p;
  74. int i, size;
  75. Biobuf b;
  76. if(fp->symsz == 0)
  77. return 0;
  78. if(fp->type == FNONE)
  79. return 0;
  80. cleansyms();
  81. textseg(fp->txtaddr, fp);
  82. /* minimum symbol record size = 4+1+2 bytes */
  83. symbols = malloc((fp->symsz/(4+1+2)+1)*sizeof(Sym));
  84. if(symbols == 0) {
  85. werrstr("can't malloc %ld bytes", fp->symsz);
  86. return -1;
  87. }
  88. Binit(&b, fd, OREAD);
  89. Bseek(&b, fp->symoff, 0);
  90. nsym = 0;
  91. size = 0;
  92. for(p = symbols; size < fp->symsz; p++, nsym++) {
  93. if(Bread(&b, &p->value, sizeof(p->value)) != sizeof(p->value))
  94. return symerrmsg(sizeof(p->value), "symbol");
  95. p->value = beswal(p->value);
  96. if(Bread(&b, &p->type, sizeof(p->type)) != sizeof(p->type))
  97. return symerrmsg(sizeof(p->value), "symbol");
  98. i = decodename(&b, p);
  99. if(i < 0)
  100. return -1;
  101. size += i+sizeof(p->value)+sizeof(p->type);
  102. /* count global & auto vars, text symbols, and file names */
  103. switch (p->type) {
  104. case 'l':
  105. case 'L':
  106. case 't':
  107. case 'T':
  108. ntxt++;
  109. break;
  110. case 'd':
  111. case 'D':
  112. case 'b':
  113. case 'B':
  114. nglob++;
  115. break;
  116. case 'f':
  117. if(strcmp(p->name, ".frame") == 0) {
  118. p->type = 'm';
  119. nauto++;
  120. }
  121. else if(p->value > fmax)
  122. fmax = p->value; /* highest path index */
  123. break;
  124. case 'a':
  125. case 'p':
  126. case 'm':
  127. nauto++;
  128. break;
  129. case 'z':
  130. if(p->value == 1) { /* one extra per file */
  131. nhist++;
  132. nfiles++;
  133. }
  134. nhist++;
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. if (debug)
  141. print("NG: %ld NT: %d NF: %d\n", nglob, ntxt, fmax);
  142. if (fp->sppcsz) { /* pc-sp offset table */
  143. spoff = (uchar *)malloc(fp->sppcsz);
  144. if(spoff == 0) {
  145. werrstr("can't malloc %ld bytes", fp->sppcsz);
  146. return -1;
  147. }
  148. Bseek(&b, fp->sppcoff, 0);
  149. i = Bread(&b, spoff, fp->sppcsz);
  150. if(i != fp->sppcsz){
  151. spoff = 0;
  152. return symerrmsg(fp->sppcsz, "sp-pc");
  153. }
  154. spoffend = spoff+fp->sppcsz;
  155. }
  156. if (fp->lnpcsz) { /* pc-line number table */
  157. pcline = (uchar *)malloc(fp->lnpcsz);
  158. if(pcline == 0) {
  159. werrstr("can't malloc %ld bytes", fp->lnpcsz);
  160. return -1;
  161. }
  162. Bseek(&b, fp->lnpcoff, 0);
  163. i = Bread(&b, pcline, fp->lnpcsz);
  164. if(i != fp->lnpcsz){
  165. pcline = 0;
  166. return symerrmsg(fp->lnpcsz, "pc-line");
  167. }
  168. pclineend = pcline+fp->lnpcsz;
  169. }
  170. return nsym;
  171. }
  172. static int
  173. symerrmsg(int n, char *table)
  174. {
  175. werrstr("can't read %d bytes of %s table", n, table);
  176. return -1;
  177. }
  178. static int
  179. decodename(Biobuf *bp, Sym *p)
  180. {
  181. char *cp;
  182. int c1, c2;
  183. int n;
  184. if((p->type & 0x80) == 0) { /* old-style, fixed length names */
  185. p->name = malloc(NNAME);
  186. if(p->name == 0) {
  187. werrstr("can't malloc %d bytes", NNAME);
  188. return -1;
  189. }
  190. if(Bread(bp, p->name, NNAME) != NNAME)
  191. return symerrmsg(NNAME, "symbol");
  192. Bseek(bp, 3, 1);
  193. return NNAME+3;
  194. }
  195. p->type &= ~0x80;
  196. if(p->type == 'z' || p->type == 'Z') {
  197. n = Bseek(bp, 0, 1);
  198. if(Bgetc(bp) < 0) {
  199. werrstr("can't read symbol name");
  200. return -1;
  201. }
  202. for(;;) {
  203. c1 = Bgetc(bp);
  204. c2 = Bgetc(bp);
  205. if(c1 < 0 || c2 < 0) {
  206. werrstr("can't read symbol name");
  207. return -1;
  208. }
  209. if(c1 == 0 && c2 == 0)
  210. break;
  211. }
  212. n = Bseek(bp, 0, 1)-n;
  213. p->name = malloc(n);
  214. if(p->name == 0) {
  215. werrstr("can't malloc %d bytes", n);
  216. return -1;
  217. }
  218. Bseek(bp, -n, 1);
  219. if(Bread(bp, p->name, n) != n) {
  220. werrstr("can't read %d bytes of symbol name", n);
  221. return -1;
  222. }
  223. } else {
  224. cp = Brdline(bp, '\0');
  225. if(cp == 0) {
  226. werrstr("can't read symbol name");
  227. return -1;
  228. }
  229. n = Blinelen(bp);
  230. p->name = malloc(n);
  231. if(p->name == 0) {
  232. werrstr("can't malloc %d bytes", n);
  233. return -1;
  234. }
  235. strcpy(p->name, cp);
  236. }
  237. return n;
  238. }
  239. /*
  240. * free any previously loaded symbol tables
  241. */
  242. static void
  243. cleansyms(void)
  244. {
  245. if(globals)
  246. free(globals);
  247. globals = 0;
  248. nglob = 0;
  249. if(txt)
  250. free(txt);
  251. txt = 0;
  252. ntxt = 0;
  253. if(fnames)
  254. free(fnames);
  255. fnames = 0;
  256. fmax = 0;
  257. if(files)
  258. free(files);
  259. files = 0;
  260. nfiles = 0;
  261. if(hist)
  262. free(hist);
  263. hist = 0;
  264. nhist = 0;
  265. if(autos)
  266. free(autos);
  267. autos = 0;
  268. nauto = 0;
  269. isbuilt = 0;
  270. if(symbols)
  271. free(symbols);
  272. symbols = 0;
  273. nsym = 0;
  274. if(spoff)
  275. free(spoff);
  276. spoff = 0;
  277. if(pcline)
  278. free(pcline);
  279. pcline = 0;
  280. }
  281. /*
  282. * delimit the text segment
  283. */
  284. void
  285. textseg(ulong base, Fhdr *fp)
  286. {
  287. txtstart = base;
  288. txtend = base+fp->txtsz;
  289. }
  290. /*
  291. * symbase: return base and size of raw symbol table
  292. * (special hack for high access rate operations)
  293. */
  294. Sym *
  295. symbase(long *n)
  296. {
  297. *n = nsym;
  298. return symbols;
  299. }
  300. /*
  301. * Get the ith symbol table entry
  302. */
  303. Sym *
  304. getsym(int index)
  305. {
  306. if(index < nsym)
  307. return &symbols[index];
  308. return 0;
  309. }
  310. /*
  311. * initialize internal symbol tables
  312. */
  313. static int
  314. buildtbls(void)
  315. {
  316. int i, j, nh, ng, nt;
  317. File *f;
  318. Txtsym *tp;
  319. Hist *hp;
  320. Sym *p, **ap;
  321. if(isbuilt)
  322. return 1;
  323. isbuilt = 1;
  324. /* allocate the tables */
  325. if(nglob) {
  326. globals = malloc(nglob*sizeof(*globals));
  327. if(!globals) {
  328. werrstr("can't malloc global symbol table");
  329. return 0;
  330. }
  331. }
  332. if(ntxt) {
  333. txt = malloc(ntxt*sizeof(*txt));
  334. if (!txt) {
  335. werrstr("can't malloc text symbol table");
  336. return 0;
  337. }
  338. }
  339. fmax++;
  340. fnames = malloc(fmax*sizeof(*fnames));
  341. if (!fnames) {
  342. werrstr("can't malloc file name table");
  343. return 0;
  344. }
  345. memset(fnames, 0, fmax*sizeof(*fnames));
  346. files = malloc(nfiles*sizeof(*files));
  347. if(!files) {
  348. werrstr("can't malloc file table");
  349. return 0;
  350. }
  351. hist = malloc(nhist*sizeof(Hist));
  352. if(hist == 0) {
  353. werrstr("can't malloc history stack");
  354. return 0;
  355. }
  356. autos = malloc(nauto*sizeof(Sym*));
  357. if(autos == 0) {
  358. werrstr("can't malloc auto symbol table");
  359. return 0;
  360. }
  361. /* load the tables */
  362. ng = nt = nh = 0;
  363. f = 0;
  364. tp = 0;
  365. i = nsym;
  366. hp = hist;
  367. ap = autos;
  368. for(p = symbols; i-- > 0; p++) {
  369. switch(p->type) {
  370. case 'D':
  371. case 'd':
  372. case 'B':
  373. case 'b':
  374. if(debug)
  375. print("Global: %s %lux\n", p->name, p->value);
  376. globals[ng++] = p;
  377. break;
  378. case 'z':
  379. if(p->value == 1) { /* New file */
  380. if(f) {
  381. f->n = nh;
  382. f->hist[nh].name = 0; /* one extra */
  383. hp += nh+1;
  384. f++;
  385. }
  386. else
  387. f = files;
  388. f->hist = hp;
  389. f->sym = 0;
  390. f->addr = 0;
  391. nh = 0;
  392. }
  393. /* alloc one slot extra as terminator */
  394. f->hist[nh].name = p->name;
  395. f->hist[nh].line = p->value;
  396. f->hist[nh].offset = 0;
  397. if(debug)
  398. printhist("-> ", &f->hist[nh], 1);
  399. nh++;
  400. break;
  401. case 'Z':
  402. if(f && nh > 0)
  403. f->hist[nh-1].offset = p->value;
  404. break;
  405. case 'T':
  406. case 't': /* Text: terminate history if first in file */
  407. case 'L':
  408. case 'l':
  409. tp = &txt[nt++];
  410. tp->n = 0;
  411. tp->sym = p;
  412. tp->locals = ap;
  413. if(debug)
  414. print("TEXT: %s at %lux\n", p->name, p->value);
  415. if(f && !f->sym) { /* first */
  416. f->sym = p;
  417. f->addr = p->value;
  418. }
  419. break;
  420. case 'a':
  421. case 'p':
  422. case 'm': /* Local Vars */
  423. if(!tp)
  424. print("Warning: Free floating local var: %s\n",
  425. p->name);
  426. else {
  427. if(debug)
  428. print("Local: %s %lux\n", p->name, p->value);
  429. tp->locals[tp->n] = p;
  430. tp->n++;
  431. ap++;
  432. }
  433. break;
  434. case 'f': /* File names */
  435. if(debug)
  436. print("Fname: %s\n", p->name);
  437. fnames[p->value] = p;
  438. break;
  439. default:
  440. break;
  441. }
  442. }
  443. /* sort global and text tables into ascending address order */
  444. qsort(globals, nglob, sizeof(Sym*), symcomp);
  445. qsort(txt, ntxt, sizeof(Txtsym), txtcomp);
  446. qsort(files, nfiles, sizeof(File), filecomp);
  447. tp = txt;
  448. for(i = 0, f = files; i < nfiles; i++, f++) {
  449. for(j = 0; j < ntxt; j++) {
  450. if(f->sym == tp->sym) {
  451. if(debug) {
  452. print("LINK: %s to at %lux", f->sym->name, f->addr);
  453. printhist("... ", f->hist, 1);
  454. }
  455. f->txt = tp++;
  456. break;
  457. }
  458. if(++tp >= txt+ntxt) /* wrap around */
  459. tp = txt;
  460. }
  461. }
  462. return 1;
  463. }
  464. /*
  465. * find symbol function.var by name.
  466. * fn != 0 && var != 0 => look for fn in text, var in data
  467. * fn != 0 && var == 0 => look for fn in text
  468. * fn == 0 && var != 0 => look for var first in text then in data space.
  469. */
  470. int
  471. lookup(char *fn, char *var, Symbol *s)
  472. {
  473. int found;
  474. if(buildtbls() == 0)
  475. return 0;
  476. if(fn) {
  477. found = findtext(fn, s);
  478. if(var == 0) /* case 2: fn not in text */
  479. return found;
  480. else if(!found) /* case 1: fn not found */
  481. return 0;
  482. } else if(var) {
  483. found = findtext(var, s);
  484. if(found)
  485. return 1; /* case 3: var found in text */
  486. } else return 0; /* case 4: fn & var == zero */
  487. if(found)
  488. return findlocal(s, var, s); /* case 1: fn found */
  489. return findglobal(var, s); /* case 3: var not found */
  490. }
  491. /*
  492. * find a function by name
  493. */
  494. static int
  495. findtext(char *name, Symbol *s)
  496. {
  497. int i;
  498. for(i = 0; i < ntxt; i++) {
  499. if(strcmp(txt[i].sym->name, name) == 0) {
  500. fillsym(txt[i].sym, s);
  501. s->handle = (void *) &txt[i];
  502. return 1;
  503. }
  504. }
  505. return 0;
  506. }
  507. /*
  508. * find global variable by name
  509. */
  510. static int
  511. findglobal(char *name, Symbol *s)
  512. {
  513. int i;
  514. for(i = 0; i < nglob; i++) {
  515. if(strcmp(globals[i]->name, name) == 0) {
  516. fillsym(globals[i], s);
  517. return 1;
  518. }
  519. }
  520. return 0;
  521. }
  522. /*
  523. * find the local variable by name within a given function
  524. */
  525. int
  526. findlocal(Symbol *s1, char *name, Symbol *s2)
  527. {
  528. if(s1 == 0)
  529. return 0;
  530. if(buildtbls() == 0)
  531. return 0;
  532. return findlocvar(s1, name, s2);
  533. }
  534. /*
  535. * find the local variable by name within a given function
  536. * (internal function - does no parameter validation)
  537. */
  538. static int
  539. findlocvar(Symbol *s1, char *name, Symbol *s2)
  540. {
  541. Txtsym *tp;
  542. int i;
  543. tp = (Txtsym *)s1->handle;
  544. if(tp && tp->locals) {
  545. for(i = 0; i < tp->n; i++)
  546. if (strcmp(tp->locals[i]->name, name) == 0) {
  547. fillsym(tp->locals[i], s2);
  548. s2->handle = (void *)tp;
  549. return 1;
  550. }
  551. }
  552. return 0;
  553. }
  554. /*
  555. * Get ith text symbol
  556. */
  557. int
  558. textsym(Symbol *s, int index)
  559. {
  560. if(buildtbls() == 0)
  561. return 0;
  562. if(index >= ntxt)
  563. return 0;
  564. fillsym(txt[index].sym, s);
  565. s->handle = (void *)&txt[index];
  566. return 1;
  567. }
  568. /*
  569. * Get ith file name
  570. */
  571. int
  572. filesym(int index, char *buf, int n)
  573. {
  574. Hist *hp;
  575. if(buildtbls() == 0)
  576. return 0;
  577. if(index >= nfiles)
  578. return 0;
  579. hp = files[index].hist;
  580. if(!hp || !hp->name)
  581. return 0;
  582. return fileelem(fnames, (uchar*)hp->name, buf, n);
  583. }
  584. /*
  585. * Lookup name of local variable located at an offset into the frame.
  586. * The type selects either a parameter or automatic.
  587. */
  588. int
  589. getauto(Symbol *s1, int off, int type, Symbol *s2)
  590. {
  591. Txtsym *tp;
  592. Sym *p;
  593. int i, t;
  594. if(s1 == 0)
  595. return 0;
  596. if(type == CPARAM)
  597. t = 'p';
  598. else if(type == CAUTO)
  599. t = 'a';
  600. else
  601. return 0;
  602. if(buildtbls() == 0)
  603. return 0;
  604. tp = (Txtsym *)s1->handle;
  605. if(tp == 0)
  606. return 0;
  607. for(i = 0; i < tp->n; i++) {
  608. p = tp->locals[i];
  609. if(p->type == t && p->value == off) {
  610. fillsym(p, s2);
  611. s2->handle = s1->handle;
  612. return 1;
  613. }
  614. }
  615. return 0;
  616. }
  617. /*
  618. * Find text symbol containing addr; binary search assumes text array is sorted by addr
  619. */
  620. static int
  621. srchtext(long addr)
  622. {
  623. ulong val;
  624. int top, bot, mid;
  625. Sym *sp;
  626. val = addr;
  627. bot = 0;
  628. top = ntxt;
  629. for (mid = (bot+top)/2; mid < top; mid = (bot+top)/2) {
  630. sp = txt[mid].sym;
  631. if(val < (ulong)sp->value)
  632. top = mid;
  633. else if(mid != ntxt-1 && val >= (ulong)txt[mid+1].sym->value)
  634. bot = mid;
  635. else
  636. return mid;
  637. }
  638. return -1;
  639. }
  640. /*
  641. * Find data symbol containing addr; binary search assumes data array is sorted by addr
  642. */
  643. static
  644. int srchdata(long addr)
  645. {
  646. ulong val;
  647. int top, bot, mid;
  648. Sym *sp;
  649. bot = 0;
  650. top = nglob;
  651. val = addr;
  652. for(mid = (bot+top)/2; mid < top; mid = (bot+top)/2) {
  653. sp = globals[mid];
  654. if(val < (ulong)sp->value)
  655. top = mid;
  656. else if(mid < nglob-1 && val >= (ulong)globals[mid+1]->value)
  657. bot = mid;
  658. else
  659. return mid;
  660. }
  661. return -1;
  662. }
  663. /*
  664. * Find symbol containing val in specified search space
  665. * There is a special case when a value falls beyond the end
  666. * of the text segment; if the search space is CTEXT, that value
  667. * (usually etext) is returned. If the search space is CANY, symbols in the
  668. * data space are searched for a match.
  669. */
  670. int
  671. findsym(long w, int type, Symbol *s)
  672. {
  673. int i;
  674. if(buildtbls() == 0)
  675. return 0;
  676. if(type == CTEXT || type == CANY) {
  677. i = srchtext(w);
  678. if(i >= 0) {
  679. if(type == CTEXT || i != ntxt-1) {
  680. fillsym(txt[i].sym, s);
  681. s->handle = (void *) &txt[i];
  682. return 1;
  683. }
  684. }
  685. }
  686. if(type == CDATA || type == CANY) {
  687. i = srchdata(w);
  688. if(i >= 0) {
  689. fillsym(globals[i], s);
  690. return 1;
  691. }
  692. }
  693. return 0;
  694. }
  695. /*
  696. * Find the start and end address of the function containing addr
  697. */
  698. int
  699. fnbound(long addr, ulong *bounds)
  700. {
  701. int i;
  702. if(buildtbls() == 0)
  703. return 0;
  704. i = srchtext(addr);
  705. if(0 <= i && i < ntxt-1) {
  706. bounds[0] = txt[i].sym->value;
  707. bounds[1] = txt[i+1].sym->value;
  708. return 1;
  709. }
  710. return 0;
  711. }
  712. /*
  713. * get the ith local symbol for a function
  714. * the input symbol table is reverse ordered, so we reverse
  715. * accesses here to maintain approx. parameter ordering in a stack trace.
  716. */
  717. int
  718. localsym(Symbol *s, int index)
  719. {
  720. Txtsym *tp;
  721. if(s == 0)
  722. return 0;
  723. if(buildtbls() == 0)
  724. return 0;
  725. tp = (Txtsym *)s->handle;
  726. if(tp && tp->locals && index < tp->n) {
  727. fillsym(tp->locals[tp->n-index-1], s); /* reverse */
  728. s->handle = (void *)tp;
  729. return 1;
  730. }
  731. return 0;
  732. }
  733. /*
  734. * get the ith global symbol
  735. */
  736. int
  737. globalsym(Symbol *s, int index)
  738. {
  739. if(s == 0)
  740. return 0;
  741. if(buildtbls() == 0)
  742. return 0;
  743. if(index < nglob) {
  744. fillsym(globals[index], s);
  745. return 1;
  746. }
  747. return 0;
  748. }
  749. /*
  750. * find the pc given a file name and line offset into it.
  751. */
  752. long
  753. file2pc(char *file, ulong line)
  754. {
  755. File *fp;
  756. int i;
  757. long pc;
  758. ulong start, end;
  759. short *name;
  760. if(buildtbls() == 0 || files == 0)
  761. return -1;
  762. name = encfname(file);
  763. if(name == 0) { /* encode the file name */
  764. werrstr("file %s not found", file);
  765. return -1;
  766. }
  767. /* find this history stack */
  768. for(i = 0, fp = files; i < nfiles; i++, fp++)
  769. if (hline(fp, name, &line))
  770. break;
  771. free(name);
  772. if(i >= nfiles) {
  773. werrstr("line %ld in file %s not found", line, file);
  774. return -1;
  775. }
  776. start = fp->addr; /* first text addr this file */
  777. if(i < nfiles-1)
  778. end = (fp+1)->addr; /* first text addr next file */
  779. else
  780. end = 0; /* last file in load module */
  781. /*
  782. * At this point, line contains the offset into the file.
  783. * run the state machine to locate the pc closest to that value.
  784. */
  785. if(debug)
  786. print("find pc for %ld - between: %lux and %lux\n", line, start, end);
  787. pc = line2addr(line, start, end);
  788. if(pc == -1) {
  789. werrstr("line %ld not in file %s", line, file);
  790. return -1;
  791. }
  792. return pc;
  793. }
  794. /*
  795. * search for a path component index
  796. */
  797. static int
  798. pathcomp(char *s, int n)
  799. {
  800. int i;
  801. for(i = 0; i <= fmax; i++)
  802. if(fnames[i] && strncmp(s, fnames[i]->name, n) == 0)
  803. return i;
  804. return -1;
  805. }
  806. /*
  807. * Encode a char file name as a sequence of short indices
  808. * into the file name dictionary.
  809. */
  810. static short*
  811. encfname(char *file)
  812. {
  813. int i, j;
  814. char *cp, *cp2;
  815. short *dest;
  816. if(*file == '/') /* always check first '/' */
  817. cp2 = file+1;
  818. else {
  819. cp2 = strchr(file, '/');
  820. if(!cp2)
  821. cp2 = strchr(file, 0);
  822. }
  823. cp = file;
  824. dest = 0;
  825. for(i = 0; *cp; i++) {
  826. j = pathcomp(cp, cp2-cp);
  827. if(j < 0)
  828. return 0; /* not found */
  829. dest = realloc(dest, (i+1)*sizeof(short));
  830. dest[i] = j;
  831. cp = cp2;
  832. while(*cp == '/') /* skip embedded '/'s */
  833. cp++;
  834. cp2 = strchr(cp, '/');
  835. if(!cp2)
  836. cp2 = strchr(cp, 0);
  837. }
  838. dest = realloc(dest, (i+1)*sizeof(short));
  839. dest[i] = 0;
  840. return dest;
  841. }
  842. /*
  843. * Search a history stack for a matching file name accumulating
  844. * the size of intervening files in the stack.
  845. */
  846. static int
  847. hline(File *fp, short *name, ulong *line)
  848. {
  849. Hist *hp;
  850. int offset, depth;
  851. long ln;
  852. for(hp = fp->hist; hp->name; hp++) /* find name in stack */
  853. if(hp->name[1] || hp->name[2]) {
  854. if(hcomp(hp, name))
  855. break;
  856. }
  857. if(!hp->name) /* match not found */
  858. return 0;
  859. if(debug)
  860. printhist("hline found ... ", hp, 1);
  861. /*
  862. * unwind the stack until empty or we hit an entry beyond our line
  863. */
  864. ln = *line;
  865. offset = hp->line-1;
  866. depth = 1;
  867. for(hp++; depth && hp->name; hp++) {
  868. if(debug)
  869. printhist("hline inspect ... ", hp, 1);
  870. if(hp->name[1] || hp->name[2]) {
  871. if(hp->offset){ /* Z record */
  872. offset = 0;
  873. if(hcomp(hp, name)) {
  874. if(*line <= hp->offset)
  875. break;
  876. ln = *line+hp->line-hp->offset;
  877. depth = 1; /* implicit pop */
  878. } else
  879. depth = 2; /* implicit push */
  880. } else if(depth == 1 && ln < hp->line-offset)
  881. break; /* Beyond our line */
  882. else if(depth++ == 1) /* push */
  883. offset -= hp->line;
  884. } else if(--depth == 1) /* pop */
  885. offset += hp->line;
  886. }
  887. *line = ln+offset;
  888. return 1;
  889. }
  890. /*
  891. * compare two encoded file names
  892. */
  893. static int
  894. hcomp(Hist *hp, short *sp)
  895. {
  896. uchar *cp;
  897. int i, j;
  898. short *s;
  899. cp = (uchar *)hp->name;
  900. s = sp;
  901. if (*s == 0)
  902. return 0;
  903. for (i = 1; j = (cp[i]<<8)|cp[i+1]; i += 2) {
  904. if(j == 0)
  905. break;
  906. if(*s == j)
  907. s++;
  908. else
  909. s = sp;
  910. }
  911. return *s == 0;
  912. }
  913. /*
  914. * Convert a pc to a "file:line {file:line}" string.
  915. */
  916. int
  917. fileline(char *str, int n, ulong dot)
  918. {
  919. long line;
  920. int top, bot, mid;
  921. File *f;
  922. *str = 0;
  923. if(buildtbls() == 0)
  924. return 0;
  925. /* binary search assumes file list is sorted by addr */
  926. bot = 0;
  927. top = nfiles;
  928. for (mid = (bot+top)/2; mid < top; mid = (bot+top)/2) {
  929. f = &files[mid];
  930. if(dot < f->addr)
  931. top = mid;
  932. else if(mid < nfiles-1 && dot >= (f+1)->addr)
  933. bot = mid;
  934. else {
  935. line = pc2line(dot);
  936. if(line > 0 && fline(str, n, line, f->hist, 0) >= 0)
  937. return 1;
  938. break;
  939. }
  940. }
  941. return 0;
  942. }
  943. /*
  944. * Convert a line number within a composite file to relative line
  945. * number in a source file. A composite file is the source
  946. * file with included files inserted in line.
  947. */
  948. static int
  949. fline(char *str, int n, long line, Hist *base, Hist **ret)
  950. {
  951. Hist *start; /* start of current level */
  952. Hist *h; /* current entry */
  953. int delta; /* sum of size of files this level */
  954. int k;
  955. start = base;
  956. h = base;
  957. delta = h->line;
  958. while(h && h->name && line > h->line) {
  959. if(h->name[1] || h->name[2]) {
  960. if(h->offset != 0) { /* #line Directive */
  961. delta = h->line-h->offset+1;
  962. start = h;
  963. base = h++;
  964. } else { /* beginning of File */
  965. if(start == base)
  966. start = h++;
  967. else {
  968. k = fline(str, n, line, start, &h);
  969. if(k <= 0)
  970. return k;
  971. }
  972. }
  973. } else {
  974. if(start == base && ret) { /* end of recursion level */
  975. *ret = h;
  976. return 1;
  977. } else { /* end of included file */
  978. delta += h->line-start->line;
  979. h++;
  980. start = base;
  981. }
  982. }
  983. }
  984. if(!h)
  985. return -1;
  986. if(start != base)
  987. line = line-start->line+1;
  988. else
  989. line = line-delta+1;
  990. if(!h->name)
  991. strncpy(str, "<eof>", n);
  992. else {
  993. k = fileelem(fnames, (uchar*)start->name, str, n);
  994. if(k+8 < n)
  995. sprint(str+k, ":%ld", line);
  996. }
  997. /**********Remove comments for complete back-trace of include sequence
  998. * if(start != base) {
  999. * k = strlen(str);
  1000. * if(k+2 < n) {
  1001. * str[k++] = ' ';
  1002. * str[k++] = '{';
  1003. * }
  1004. * k += fileelem(fnames, (uchar*) base->name, str+k, n-k);
  1005. * if(k+10 < n)
  1006. * sprint(str+k, ":%ld}", start->line-delta);
  1007. * }
  1008. ********************/
  1009. return 0;
  1010. }
  1011. /*
  1012. * convert an encoded file name to a string.
  1013. */
  1014. int
  1015. fileelem(Sym **fp, uchar *cp, char *buf, int n)
  1016. {
  1017. int i, j;
  1018. char *c, *bp, *end;
  1019. bp = buf;
  1020. end = buf+n-1;
  1021. for(i = 1; j = (cp[i]<<8)|cp[i+1]; i+=2){
  1022. c = fp[j]->name;
  1023. if(bp != buf && bp[-1] != '/' && bp < end)
  1024. *bp++ = '/';
  1025. while(bp < end && *c)
  1026. *bp++ = *c++;
  1027. }
  1028. *bp = 0;
  1029. i = bp-buf;
  1030. if(i > 1) {
  1031. cleanname(buf);
  1032. i = strlen(buf);
  1033. }
  1034. return i;
  1035. }
  1036. /*
  1037. * compare the values of two symbol table entries.
  1038. */
  1039. static int
  1040. symcomp(void *a, void *b)
  1041. {
  1042. return (*(Sym**)a)->value - (*(Sym**)b)->value;
  1043. }
  1044. /*
  1045. * compare the values of the symbols referenced by two text table entries
  1046. */
  1047. static int
  1048. txtcomp(void *a, void *b)
  1049. {
  1050. return ((Txtsym*)a)->sym->value - ((Txtsym*)b)->sym->value;
  1051. }
  1052. /*
  1053. * compare the values of the symbols referenced by two file table entries
  1054. */
  1055. static int
  1056. filecomp(void *a, void *b)
  1057. {
  1058. return ((File*)a)->addr - ((File*)b)->addr;
  1059. }
  1060. /*
  1061. * fill an interface Symbol structure from a symbol table entry
  1062. */
  1063. static void
  1064. fillsym(Sym *sp, Symbol *s)
  1065. {
  1066. s->type = sp->type;
  1067. s->value = sp->value;
  1068. s->name = sp->name;
  1069. switch(sp->type) {
  1070. case 'b':
  1071. case 'B':
  1072. case 'D':
  1073. case 'd':
  1074. s->class = CDATA;
  1075. break;
  1076. case 't':
  1077. case 'T':
  1078. case 'l':
  1079. case 'L':
  1080. s->class = CTEXT;
  1081. break;
  1082. case 'a':
  1083. s->class = CAUTO;
  1084. break;
  1085. case 'p':
  1086. s->class = CPARAM;
  1087. break;
  1088. case 'm':
  1089. s->class = CSTAB;
  1090. break;
  1091. default:
  1092. s->class = CNONE;
  1093. break;
  1094. }
  1095. s->handle = 0;
  1096. }
  1097. /*
  1098. * find the stack frame, given the pc
  1099. */
  1100. long
  1101. pc2sp(ulong pc)
  1102. {
  1103. uchar *c;
  1104. uchar u;
  1105. ulong currpc;
  1106. long currsp;
  1107. if(spoff == 0)
  1108. return -1;
  1109. currsp = 0;
  1110. currpc = txtstart - mach->pcquant;
  1111. if(pc<currpc || pc>txtend)
  1112. return -1;
  1113. for(c = spoff; c < spoffend; c++) {
  1114. if (currpc >= pc)
  1115. return currsp;
  1116. u = *c;
  1117. if (u == 0) {
  1118. currsp += (c[1]<<24)|(c[2]<<16)|(c[3]<<8)|c[4];
  1119. c += 4;
  1120. }
  1121. else if (u < 65)
  1122. currsp += 4*u;
  1123. else if (u < 129)
  1124. currsp -= 4*(u-64);
  1125. else
  1126. currpc += mach->pcquant*(u-129);
  1127. currpc += mach->pcquant;
  1128. }
  1129. return -1;
  1130. }
  1131. /*
  1132. * find the source file line number for a given value of the pc
  1133. */
  1134. long
  1135. pc2line(ulong pc)
  1136. {
  1137. uchar *c;
  1138. uchar u;
  1139. ulong currpc;
  1140. long currline;
  1141. if(pcline == 0)
  1142. return -1;
  1143. currline = 0;
  1144. currpc = txtstart-mach->pcquant;
  1145. if(pc<currpc || pc>txtend)
  1146. return -1;
  1147. for(c = pcline; c < pclineend; c++) {
  1148. if(currpc >= pc)
  1149. return currline;
  1150. u = *c;
  1151. if(u == 0) {
  1152. currline += (c[1]<<24)|(c[2]<<16)|(c[3]<<8)|c[4];
  1153. c += 4;
  1154. }
  1155. else if(u < 65)
  1156. currline += u;
  1157. else if(u < 129)
  1158. currline -= (u-64);
  1159. else
  1160. currpc += mach->pcquant*(u-129);
  1161. currpc += mach->pcquant;
  1162. }
  1163. return -1;
  1164. }
  1165. /*
  1166. * find the pc associated with a line number
  1167. * basepc and endpc are text addresses bounding the search.
  1168. * if endpc == 0, the end of the table is used (i.e., no upper bound).
  1169. * usually, basepc and endpc contain the first text address in
  1170. * a file and the first text address in the following file, respectively.
  1171. */
  1172. long
  1173. line2addr(ulong line, ulong basepc, ulong endpc)
  1174. {
  1175. uchar *c;
  1176. uchar u;
  1177. ulong currpc;
  1178. long currline;
  1179. long delta, d;
  1180. long pc, found;
  1181. if(pcline == 0 || line == 0)
  1182. return -1;
  1183. currline = 0;
  1184. currpc = txtstart-mach->pcquant;
  1185. pc = -1;
  1186. found = 0;
  1187. delta = HUGEINT;
  1188. for(c = pcline; c < pclineend; c++) {
  1189. if(endpc && currpc >= endpc) /* end of file of interest */
  1190. break;
  1191. if(currpc >= basepc) { /* proper file */
  1192. if(currline >= line) {
  1193. d = currline-line;
  1194. found = 1;
  1195. } else
  1196. d = line-currline;
  1197. if(d < delta) {
  1198. delta = d;
  1199. pc = currpc;
  1200. }
  1201. }
  1202. u = *c;
  1203. if(u == 0) {
  1204. currline += (c[1]<<24)|(c[2]<<16)|(c[3]<<8)|c[4];
  1205. c += 4;
  1206. }
  1207. else if(u < 65)
  1208. currline += u;
  1209. else if(u < 129)
  1210. currline -= (u-64);
  1211. else
  1212. currpc += mach->pcquant*(u-129);
  1213. currpc += mach->pcquant;
  1214. }
  1215. if(found)
  1216. return pc;
  1217. return -1;
  1218. }
  1219. /*
  1220. * Print a history stack (debug). if count is 0, prints the whole stack
  1221. */
  1222. static void
  1223. printhist(char *msg, Hist *hp, int count)
  1224. {
  1225. int i;
  1226. uchar *cp;
  1227. char buf[128];
  1228. i = 0;
  1229. while(hp->name) {
  1230. if(count && ++i > count)
  1231. break;
  1232. print("%s Line: %lx (%ld) Offset: %lx (%ld) Name: ", msg,
  1233. hp->line, hp->line, hp->offset, hp->offset);
  1234. for(cp = (uchar *)hp->name+1; (*cp<<8)|cp[1]; cp += 2) {
  1235. if (cp != (uchar *)hp->name+1)
  1236. print("/");
  1237. print("%x", (*cp<<8)|cp[1]);
  1238. }
  1239. fileelem(fnames, (uchar *) hp->name, buf, sizeof(buf));
  1240. print(" (%s)\n", buf);
  1241. hp++;
  1242. }
  1243. }
  1244. #ifdef DEBUG
  1245. /*
  1246. * print the history stack for a file. (debug only)
  1247. * if (name == 0) => print all history stacks.
  1248. */
  1249. void
  1250. dumphist(char *name)
  1251. {
  1252. int i;
  1253. File *f;
  1254. short *fname;
  1255. if(buildtbls() == 0)
  1256. return;
  1257. if(name)
  1258. fname = encfname(name);
  1259. for(i = 0, f = files; i < nfiles; i++, f++)
  1260. if(fname == 0 || hcomp(f->hist, fname))
  1261. printhist("> ", f->hist, f->n);
  1262. if(fname)
  1263. free(fname);
  1264. }
  1265. #endif