sym.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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 >= 0 && 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. s->index = i;
  503. return 1;
  504. }
  505. }
  506. return 0;
  507. }
  508. /*
  509. * find global variable by name
  510. */
  511. static int
  512. findglobal(char *name, Symbol *s)
  513. {
  514. int i;
  515. for(i = 0; i < nglob; i++) {
  516. if(strcmp(globals[i]->name, name) == 0) {
  517. fillsym(globals[i], s);
  518. s->index = i;
  519. return 1;
  520. }
  521. }
  522. return 0;
  523. }
  524. /*
  525. * find the local variable by name within a given function
  526. */
  527. int
  528. findlocal(Symbol *s1, char *name, Symbol *s2)
  529. {
  530. if(s1 == 0)
  531. return 0;
  532. if(buildtbls() == 0)
  533. return 0;
  534. return findlocvar(s1, name, s2);
  535. }
  536. /*
  537. * find the local variable by name within a given function
  538. * (internal function - does no parameter validation)
  539. */
  540. static int
  541. findlocvar(Symbol *s1, char *name, Symbol *s2)
  542. {
  543. Txtsym *tp;
  544. int i;
  545. tp = (Txtsym *)s1->handle;
  546. if(tp && tp->locals) {
  547. for(i = 0; i < tp->n; i++)
  548. if (strcmp(tp->locals[i]->name, name) == 0) {
  549. fillsym(tp->locals[i], s2);
  550. s2->handle = (void *)tp;
  551. s2->index = tp->n-1 - i;
  552. return 1;
  553. }
  554. }
  555. return 0;
  556. }
  557. /*
  558. * Get ith text symbol
  559. */
  560. int
  561. textsym(Symbol *s, int index)
  562. {
  563. if(buildtbls() == 0)
  564. return 0;
  565. if(index < 0 || index >= ntxt)
  566. return 0;
  567. fillsym(txt[index].sym, s);
  568. s->handle = (void *)&txt[index];
  569. s->index = index;
  570. return 1;
  571. }
  572. /*
  573. * Get ith file name
  574. */
  575. int
  576. filesym(int index, char *buf, int n)
  577. {
  578. Hist *hp;
  579. if(buildtbls() == 0)
  580. return 0;
  581. if(index < 0 || index >= nfiles)
  582. return 0;
  583. hp = files[index].hist;
  584. if(!hp || !hp->name)
  585. return 0;
  586. return fileelem(fnames, (uchar*)hp->name, buf, n);
  587. }
  588. /*
  589. * Lookup name of local variable located at an offset into the frame.
  590. * The type selects either a parameter or automatic.
  591. */
  592. int
  593. getauto(Symbol *s1, int off, int type, Symbol *s2)
  594. {
  595. Txtsym *tp;
  596. Sym *p;
  597. int i, t;
  598. if(s1 == 0)
  599. return 0;
  600. if(type == CPARAM)
  601. t = 'p';
  602. else if(type == CAUTO)
  603. t = 'a';
  604. else
  605. return 0;
  606. if(buildtbls() == 0)
  607. return 0;
  608. tp = (Txtsym *)s1->handle;
  609. if(tp == 0)
  610. return 0;
  611. for(i = 0; i < tp->n; i++) {
  612. p = tp->locals[i];
  613. if(p->type == t && p->value == off) {
  614. fillsym(p, s2);
  615. s2->handle = s1->handle;
  616. s2->index = tp->n-1 - i;
  617. return 1;
  618. }
  619. }
  620. return 0;
  621. }
  622. /*
  623. * Find text symbol containing addr; binary search assumes text array is sorted by addr
  624. */
  625. static int
  626. srchtext(long addr)
  627. {
  628. ulong val;
  629. int top, bot, mid;
  630. Sym *sp;
  631. val = addr;
  632. bot = 0;
  633. top = ntxt;
  634. for (mid = (bot+top)/2; mid < top; mid = (bot+top)/2) {
  635. sp = txt[mid].sym;
  636. if(val < (ulong)sp->value)
  637. top = mid;
  638. else if(mid != ntxt-1 && val >= (ulong)txt[mid+1].sym->value)
  639. bot = mid;
  640. else
  641. return mid;
  642. }
  643. return -1;
  644. }
  645. /*
  646. * Find data symbol containing addr; binary search assumes data array is sorted by addr
  647. */
  648. static
  649. int srchdata(long addr)
  650. {
  651. ulong val;
  652. int top, bot, mid;
  653. Sym *sp;
  654. bot = 0;
  655. top = nglob;
  656. val = addr;
  657. for(mid = (bot+top)/2; mid < top; mid = (bot+top)/2) {
  658. sp = globals[mid];
  659. if(val < (ulong)sp->value)
  660. top = mid;
  661. else if(mid < nglob-1 && val >= (ulong)globals[mid+1]->value)
  662. bot = mid;
  663. else
  664. return mid;
  665. }
  666. return -1;
  667. }
  668. /*
  669. * Find symbol containing val in specified search space
  670. * There is a special case when a value falls beyond the end
  671. * of the text segment; if the search space is CTEXT, that value
  672. * (usually etext) is returned. If the search space is CANY, symbols in the
  673. * data space are searched for a match.
  674. */
  675. int
  676. findsym(long w, int type, Symbol *s)
  677. {
  678. int i;
  679. if(buildtbls() == 0)
  680. return 0;
  681. if(type == CTEXT || type == CANY) {
  682. i = srchtext(w);
  683. if(i >= 0) {
  684. if(type == CTEXT || i != ntxt-1) {
  685. fillsym(txt[i].sym, s);
  686. s->handle = (void *) &txt[i];
  687. s->index = i;
  688. return 1;
  689. }
  690. }
  691. }
  692. if(type == CDATA || type == CANY) {
  693. i = srchdata(w);
  694. if(i >= 0) {
  695. fillsym(globals[i], s);
  696. s->index = i;
  697. return 1;
  698. }
  699. }
  700. return 0;
  701. }
  702. /*
  703. * Find the start and end address of the function containing addr
  704. */
  705. int
  706. fnbound(long addr, ulong *bounds)
  707. {
  708. int i;
  709. if(buildtbls() == 0)
  710. return 0;
  711. i = srchtext(addr);
  712. if(0 <= i && i < ntxt-1) {
  713. bounds[0] = txt[i].sym->value;
  714. bounds[1] = txt[i+1].sym->value;
  715. return 1;
  716. }
  717. return 0;
  718. }
  719. /*
  720. * get the ith local symbol for a function
  721. * the input symbol table is reverse ordered, so we reverse
  722. * accesses here to maintain approx. parameter ordering in a stack trace.
  723. */
  724. int
  725. localsym(Symbol *s, int index)
  726. {
  727. Txtsym *tp;
  728. if(s == 0 || index < 0)
  729. return 0;
  730. if(buildtbls() == 0)
  731. return 0;
  732. tp = (Txtsym *)s->handle;
  733. if(tp && tp->locals && index < tp->n) {
  734. fillsym(tp->locals[tp->n-index-1], s); /* reverse */
  735. s->handle = (void *)tp;
  736. s->index = index;
  737. return 1;
  738. }
  739. return 0;
  740. }
  741. /*
  742. * get the ith global symbol
  743. */
  744. int
  745. globalsym(Symbol *s, int index)
  746. {
  747. if(s == 0)
  748. return 0;
  749. if(buildtbls() == 0)
  750. return 0;
  751. if(index >=0 && index < nglob) {
  752. fillsym(globals[index], s);
  753. s->index = index;
  754. return 1;
  755. }
  756. return 0;
  757. }
  758. /*
  759. * find the pc given a file name and line offset into it.
  760. */
  761. long
  762. file2pc(char *file, ulong line)
  763. {
  764. File *fp;
  765. int i;
  766. long pc;
  767. ulong start, end;
  768. short *name;
  769. if(buildtbls() == 0 || files == 0)
  770. return -1;
  771. name = encfname(file);
  772. if(name == 0) { /* encode the file name */
  773. werrstr("file %s not found", file);
  774. return -1;
  775. }
  776. /* find this history stack */
  777. for(i = 0, fp = files; i < nfiles; i++, fp++)
  778. if (hline(fp, name, &line))
  779. break;
  780. free(name);
  781. if(i >= nfiles) {
  782. werrstr("line %ld in file %s not found", line, file);
  783. return -1;
  784. }
  785. start = fp->addr; /* first text addr this file */
  786. if(i < nfiles-1)
  787. end = (fp+1)->addr; /* first text addr next file */
  788. else
  789. end = 0; /* last file in load module */
  790. /*
  791. * At this point, line contains the offset into the file.
  792. * run the state machine to locate the pc closest to that value.
  793. */
  794. if(debug)
  795. print("find pc for %ld - between: %lux and %lux\n", line, start, end);
  796. pc = line2addr(line, start, end);
  797. if(pc == -1) {
  798. werrstr("line %ld not in file %s", line, file);
  799. return -1;
  800. }
  801. return pc;
  802. }
  803. /*
  804. * search for a path component index
  805. */
  806. static int
  807. pathcomp(char *s, int n)
  808. {
  809. int i;
  810. for(i = 0; i <= fmax; i++)
  811. if(fnames[i] && strncmp(s, fnames[i]->name, n) == 0)
  812. return i;
  813. return -1;
  814. }
  815. /*
  816. * Encode a char file name as a sequence of short indices
  817. * into the file name dictionary.
  818. */
  819. static short*
  820. encfname(char *file)
  821. {
  822. int i, j;
  823. char *cp, *cp2;
  824. short *dest;
  825. if(*file == '/') /* always check first '/' */
  826. cp2 = file+1;
  827. else {
  828. cp2 = strchr(file, '/');
  829. if(!cp2)
  830. cp2 = strchr(file, 0);
  831. }
  832. cp = file;
  833. dest = 0;
  834. for(i = 0; *cp; i++) {
  835. j = pathcomp(cp, cp2-cp);
  836. if(j < 0)
  837. return 0; /* not found */
  838. dest = realloc(dest, (i+1)*sizeof(short));
  839. dest[i] = j;
  840. cp = cp2;
  841. while(*cp == '/') /* skip embedded '/'s */
  842. cp++;
  843. cp2 = strchr(cp, '/');
  844. if(!cp2)
  845. cp2 = strchr(cp, 0);
  846. }
  847. dest = realloc(dest, (i+1)*sizeof(short));
  848. dest[i] = 0;
  849. return dest;
  850. }
  851. /*
  852. * Search a history stack for a matching file name accumulating
  853. * the size of intervening files in the stack.
  854. */
  855. static int
  856. hline(File *fp, short *name, ulong *line)
  857. {
  858. Hist *hp;
  859. int offset, depth;
  860. long ln;
  861. for(hp = fp->hist; hp->name; hp++) /* find name in stack */
  862. if(hp->name[1] || hp->name[2]) {
  863. if(hcomp(hp, name))
  864. break;
  865. }
  866. if(!hp->name) /* match not found */
  867. return 0;
  868. if(debug)
  869. printhist("hline found ... ", hp, 1);
  870. /*
  871. * unwind the stack until empty or we hit an entry beyond our line
  872. */
  873. ln = *line;
  874. offset = hp->line-1;
  875. depth = 1;
  876. for(hp++; depth && hp->name; hp++) {
  877. if(debug)
  878. printhist("hline inspect ... ", hp, 1);
  879. if(hp->name[1] || hp->name[2]) {
  880. if(hp->offset){ /* Z record */
  881. offset = 0;
  882. if(hcomp(hp, name)) {
  883. if(*line <= hp->offset)
  884. break;
  885. ln = *line+hp->line-hp->offset;
  886. depth = 1; /* implicit pop */
  887. } else
  888. depth = 2; /* implicit push */
  889. } else if(depth == 1 && ln < hp->line-offset)
  890. break; /* Beyond our line */
  891. else if(depth++ == 1) /* push */
  892. offset -= hp->line;
  893. } else if(--depth == 1) /* pop */
  894. offset += hp->line;
  895. }
  896. *line = ln+offset;
  897. return 1;
  898. }
  899. /*
  900. * compare two encoded file names
  901. */
  902. static int
  903. hcomp(Hist *hp, short *sp)
  904. {
  905. uchar *cp;
  906. int i, j;
  907. short *s;
  908. cp = (uchar *)hp->name;
  909. s = sp;
  910. if (*s == 0)
  911. return 0;
  912. for (i = 1; j = (cp[i]<<8)|cp[i+1]; i += 2) {
  913. if(j == 0)
  914. break;
  915. if(*s == j)
  916. s++;
  917. else
  918. s = sp;
  919. }
  920. return *s == 0;
  921. }
  922. /*
  923. * Convert a pc to a "file:line {file:line}" string.
  924. */
  925. int
  926. fileline(char *str, int n, ulong dot)
  927. {
  928. long line;
  929. int top, bot, mid;
  930. File *f;
  931. *str = 0;
  932. if(buildtbls() == 0)
  933. return 0;
  934. /* binary search assumes file list is sorted by addr */
  935. bot = 0;
  936. top = nfiles;
  937. for (mid = (bot+top)/2; mid < top; mid = (bot+top)/2) {
  938. f = &files[mid];
  939. if(dot < f->addr)
  940. top = mid;
  941. else if(mid < nfiles-1 && dot >= (f+1)->addr)
  942. bot = mid;
  943. else {
  944. line = pc2line(dot);
  945. if(line > 0 && fline(str, n, line, f->hist, 0) >= 0)
  946. return 1;
  947. break;
  948. }
  949. }
  950. return 0;
  951. }
  952. /*
  953. * Convert a line number within a composite file to relative line
  954. * number in a source file. A composite file is the source
  955. * file with included files inserted in line.
  956. */
  957. static int
  958. fline(char *str, int n, long line, Hist *base, Hist **ret)
  959. {
  960. Hist *start; /* start of current level */
  961. Hist *h; /* current entry */
  962. int delta; /* sum of size of files this level */
  963. int k;
  964. start = base;
  965. h = base;
  966. delta = h->line;
  967. while(h && h->name && line > h->line) {
  968. if(h->name[1] || h->name[2]) {
  969. if(h->offset != 0) { /* #line Directive */
  970. delta = h->line-h->offset+1;
  971. start = h;
  972. base = h++;
  973. } else { /* beginning of File */
  974. if(start == base)
  975. start = h++;
  976. else {
  977. k = fline(str, n, line, start, &h);
  978. if(k <= 0)
  979. return k;
  980. }
  981. }
  982. } else {
  983. if(start == base && ret) { /* end of recursion level */
  984. *ret = h;
  985. return 1;
  986. } else { /* end of included file */
  987. delta += h->line-start->line;
  988. h++;
  989. start = base;
  990. }
  991. }
  992. }
  993. if(!h)
  994. return -1;
  995. if(start != base)
  996. line = line-start->line+1;
  997. else
  998. line = line-delta+1;
  999. if(!h->name)
  1000. strncpy(str, "<eof>", n);
  1001. else {
  1002. k = fileelem(fnames, (uchar*)start->name, str, n);
  1003. if(k+8 < n)
  1004. sprint(str+k, ":%ld", line);
  1005. }
  1006. /**********Remove comments for complete back-trace of include sequence
  1007. * if(start != base) {
  1008. * k = strlen(str);
  1009. * if(k+2 < n) {
  1010. * str[k++] = ' ';
  1011. * str[k++] = '{';
  1012. * }
  1013. * k += fileelem(fnames, (uchar*) base->name, str+k, n-k);
  1014. * if(k+10 < n)
  1015. * sprint(str+k, ":%ld}", start->line-delta);
  1016. * }
  1017. ********************/
  1018. return 0;
  1019. }
  1020. /*
  1021. * convert an encoded file name to a string.
  1022. */
  1023. int
  1024. fileelem(Sym **fp, uchar *cp, char *buf, int n)
  1025. {
  1026. int i, j;
  1027. char *c, *bp, *end;
  1028. bp = buf;
  1029. end = buf+n-1;
  1030. for(i = 1; j = (cp[i]<<8)|cp[i+1]; i+=2){
  1031. c = fp[j]->name;
  1032. if(bp != buf && bp[-1] != '/' && bp < end)
  1033. *bp++ = '/';
  1034. while(bp < end && *c)
  1035. *bp++ = *c++;
  1036. }
  1037. *bp = 0;
  1038. i = bp-buf;
  1039. if(i > 1) {
  1040. cleanname(buf);
  1041. i = strlen(buf);
  1042. }
  1043. return i;
  1044. }
  1045. /*
  1046. * compare the values of two symbol table entries.
  1047. */
  1048. static int
  1049. symcomp(void *a, void *b)
  1050. {
  1051. int i;
  1052. i = (*(Sym**)a)->value - (*(Sym**)b)->value;
  1053. if (i)
  1054. return i;
  1055. return strcmp((*(Sym**)a)->name, (*(Sym**)b)->name);
  1056. }
  1057. /*
  1058. * compare the values of the symbols referenced by two text table entries
  1059. */
  1060. static int
  1061. txtcomp(void *a, void *b)
  1062. {
  1063. return ((Txtsym*)a)->sym->value - ((Txtsym*)b)->sym->value;
  1064. }
  1065. /*
  1066. * compare the values of the symbols referenced by two file table entries
  1067. */
  1068. static int
  1069. filecomp(void *a, void *b)
  1070. {
  1071. return ((File*)a)->addr - ((File*)b)->addr;
  1072. }
  1073. /*
  1074. * fill an interface Symbol structure from a symbol table entry
  1075. */
  1076. static void
  1077. fillsym(Sym *sp, Symbol *s)
  1078. {
  1079. s->type = sp->type;
  1080. s->value = sp->value;
  1081. s->name = sp->name;
  1082. s->index = 0;
  1083. switch(sp->type) {
  1084. case 'b':
  1085. case 'B':
  1086. case 'D':
  1087. case 'd':
  1088. s->class = CDATA;
  1089. break;
  1090. case 't':
  1091. case 'T':
  1092. case 'l':
  1093. case 'L':
  1094. s->class = CTEXT;
  1095. break;
  1096. case 'a':
  1097. s->class = CAUTO;
  1098. break;
  1099. case 'p':
  1100. s->class = CPARAM;
  1101. break;
  1102. case 'm':
  1103. s->class = CSTAB;
  1104. break;
  1105. default:
  1106. s->class = CNONE;
  1107. break;
  1108. }
  1109. s->handle = 0;
  1110. }
  1111. /*
  1112. * find the stack frame, given the pc
  1113. */
  1114. long
  1115. pc2sp(ulong pc)
  1116. {
  1117. uchar *c;
  1118. uchar u;
  1119. ulong currpc;
  1120. long currsp;
  1121. if(spoff == 0)
  1122. return -1;
  1123. currsp = 0;
  1124. currpc = txtstart - mach->pcquant;
  1125. if(pc<currpc || pc>txtend)
  1126. return -1;
  1127. for(c = spoff; c < spoffend; c++) {
  1128. if (currpc >= pc)
  1129. return currsp;
  1130. u = *c;
  1131. if (u == 0) {
  1132. currsp += (c[1]<<24)|(c[2]<<16)|(c[3]<<8)|c[4];
  1133. c += 4;
  1134. }
  1135. else if (u < 65)
  1136. currsp += 4*u;
  1137. else if (u < 129)
  1138. currsp -= 4*(u-64);
  1139. else
  1140. currpc += mach->pcquant*(u-129);
  1141. currpc += mach->pcquant;
  1142. }
  1143. return -1;
  1144. }
  1145. /*
  1146. * find the source file line number for a given value of the pc
  1147. */
  1148. long
  1149. pc2line(ulong pc)
  1150. {
  1151. uchar *c;
  1152. uchar u;
  1153. ulong currpc;
  1154. long currline;
  1155. if(pcline == 0)
  1156. return -1;
  1157. currline = 0;
  1158. currpc = txtstart-mach->pcquant;
  1159. if(pc<currpc || pc>txtend)
  1160. return -1;
  1161. for(c = pcline; c < pclineend; c++) {
  1162. if(currpc >= pc)
  1163. return currline;
  1164. u = *c;
  1165. if(u == 0) {
  1166. currline += (c[1]<<24)|(c[2]<<16)|(c[3]<<8)|c[4];
  1167. c += 4;
  1168. }
  1169. else if(u < 65)
  1170. currline += u;
  1171. else if(u < 129)
  1172. currline -= (u-64);
  1173. else
  1174. currpc += mach->pcquant*(u-129);
  1175. currpc += mach->pcquant;
  1176. }
  1177. return -1;
  1178. }
  1179. /*
  1180. * find the pc associated with a line number
  1181. * basepc and endpc are text addresses bounding the search.
  1182. * if endpc == 0, the end of the table is used (i.e., no upper bound).
  1183. * usually, basepc and endpc contain the first text address in
  1184. * a file and the first text address in the following file, respectively.
  1185. */
  1186. long
  1187. line2addr(ulong line, ulong basepc, ulong endpc)
  1188. {
  1189. uchar *c;
  1190. uchar u;
  1191. ulong currpc;
  1192. long currline;
  1193. long delta, d;
  1194. long pc, found;
  1195. if(pcline == 0 || line == 0)
  1196. return -1;
  1197. currline = 0;
  1198. currpc = txtstart-mach->pcquant;
  1199. pc = -1;
  1200. found = 0;
  1201. delta = HUGEINT;
  1202. for(c = pcline; c < pclineend; c++) {
  1203. if(endpc && currpc >= endpc) /* end of file of interest */
  1204. break;
  1205. if(currpc >= basepc) { /* proper file */
  1206. if(currline >= line) {
  1207. d = currline-line;
  1208. found = 1;
  1209. } else
  1210. d = line-currline;
  1211. if(d < delta) {
  1212. delta = d;
  1213. pc = currpc;
  1214. }
  1215. }
  1216. u = *c;
  1217. if(u == 0) {
  1218. currline += (c[1]<<24)|(c[2]<<16)|(c[3]<<8)|c[4];
  1219. c += 4;
  1220. }
  1221. else if(u < 65)
  1222. currline += u;
  1223. else if(u < 129)
  1224. currline -= (u-64);
  1225. else
  1226. currpc += mach->pcquant*(u-129);
  1227. currpc += mach->pcquant;
  1228. }
  1229. if(found)
  1230. return pc;
  1231. return -1;
  1232. }
  1233. /*
  1234. * Print a history stack (debug). if count is 0, prints the whole stack
  1235. */
  1236. static void
  1237. printhist(char *msg, Hist *hp, int count)
  1238. {
  1239. int i;
  1240. uchar *cp;
  1241. char buf[128];
  1242. i = 0;
  1243. while(hp->name) {
  1244. if(count && ++i > count)
  1245. break;
  1246. print("%s Line: %lx (%ld) Offset: %lx (%ld) Name: ", msg,
  1247. hp->line, hp->line, hp->offset, hp->offset);
  1248. for(cp = (uchar *)hp->name+1; (*cp<<8)|cp[1]; cp += 2) {
  1249. if (cp != (uchar *)hp->name+1)
  1250. print("/");
  1251. print("%x", (*cp<<8)|cp[1]);
  1252. }
  1253. fileelem(fnames, (uchar *) hp->name, buf, sizeof(buf));
  1254. print(" (%s)\n", buf);
  1255. hp++;
  1256. }
  1257. }
  1258. #ifdef DEBUG
  1259. /*
  1260. * print the history stack for a file. (debug only)
  1261. * if (name == 0) => print all history stacks.
  1262. */
  1263. void
  1264. dumphist(char *name)
  1265. {
  1266. int i;
  1267. File *f;
  1268. short *fname;
  1269. if(buildtbls() == 0)
  1270. return;
  1271. if(name)
  1272. fname = encfname(name);
  1273. for(i = 0, f = files; i < nfiles; i++, f++)
  1274. if(fname == 0 || hcomp(f->hist, fname))
  1275. printhist("> ", f->hist, f->n);
  1276. if(fname)
  1277. free(fname);
  1278. }
  1279. #endif