sym.c 27 KB

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