6 #define HUGEINT 0x7fffffff
7 #define NNAME 20 /* a relic of the past */
9 typedef struct txtsym Txtsym;
10 typedef struct file File;
11 typedef struct hist Hist;
13 struct txtsym { /* Text Symbol table */
14 int n; /* number of local vars */
15 Sym **locals; /* array of ptrs to autos */
16 Sym *sym; /* function symbol entry */
19 struct hist { /* Stack of include files & #line directives */
20 char *name; /* Assumes names Null terminated in file */
21 long line; /* line # where it was included */
22 long offset; /* line # of #line directive */
25 struct file { /* Per input file header to history stack */
26 uvlong addr; /* address of first text sym */
28 Txtsym *txt; /* first text symbol */
29 Sym *sym; /* only during initilization */
31 int n; /* size of history stack */
32 Hist *hist; /* history stack */
37 static Sym **autos; /* Base of auto variables */
38 static File *files; /* Base of file arena */
39 static int fmax; /* largest file path index */
40 static Sym **fnames; /* file names path component table */
41 static Sym **globals; /* globals by addr table */
42 static Hist *hist; /* base of history stack */
43 static int isbuilt; /* internal table init flag */
44 static long nauto; /* number of automatics */
45 static long nfiles; /* number of files */
46 static long nglob; /* number of globals */
47 static long nhist; /* number of history stack entries */
48 static long nsym; /* number of symbols */
49 static int ntxt; /* number of text symbols */
50 static uchar *pcline; /* start of pc-line state table */
51 static uchar *pclineend; /* end of pc-line table */
52 static uchar *spoff; /* start of pc-sp state table */
53 static uchar *spoffend; /* end of pc-sp offset table */
54 static Sym *symbols; /* symbol table */
55 static Txtsym *txt; /* Base of text symbol table */
56 static uvlong txtstart; /* start of text segment */
57 static uvlong txtend; /* end of text segment */
59 static void cleansyms(void);
60 static long decodename(Biobuf*, Sym*);
61 static short *encfname(char*);
62 static int fline(char*, int, long, 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*, short*);
68 static int hline(File*, short*, long*);
69 static void printhist(char*, Hist*, int);
70 static int buildtbls(void);
71 static int symcomp(void*, void*);
72 static int symerrmsg(int, char*);
73 static int txtcomp(void*, void*);
74 static int filecomp(void*, void*);
77 * initialize the symbol tables
80 syminit(int fd, Fhdr *fp)
94 textseg(fp->txtaddr, fp);
95 /* minimum symbol record size = 4+1+2 bytes */
96 symbols = malloc((fp->symsz/(4+1+2)+1)*sizeof(Sym));
98 werrstr("can't malloc %ld bytes", fp->symsz);
101 Binit(&b, fd, OREAD);
102 Bseek(&b, fp->symoff, 0);
105 for(p = symbols; size < fp->symsz; p++, nsym++) {
106 if(fp->_magic && (fp->magic & HDR_MAGIC)){
108 if(Bread(&b, &vl, 8) != 8)
109 return symerrmsg(8, "symbol");
110 p->value = beswav(vl);
114 if(Bread(&b, &l, 4) != 4)
115 return symerrmsg(4, "symbol");
116 p->value = (u32int)beswal(l);
118 if(Bread(&b, &p->type, sizeof(p->type)) != sizeof(p->type))
119 return symerrmsg(sizeof(p->value), "symbol");
121 i = decodename(&b, p);
124 size += i+svalsz+sizeof(p->type);
126 /* count global & auto vars, text symbols, and file names */
141 if(strcmp(p->name, ".frame") == 0) {
145 else if(p->value > fmax)
146 fmax = p->value; /* highest path index */
154 if(p->value == 1) { /* one extra per file */
165 print("NG: %ld NT: %d NF: %d\n", nglob, ntxt, fmax);
166 if (fp->sppcsz) { /* pc-sp offset table */
167 spoff = (uchar *)malloc(fp->sppcsz);
169 werrstr("can't malloc %ld bytes", fp->sppcsz);
172 Bseek(&b, fp->sppcoff, 0);
173 if(Bread(&b, spoff, fp->sppcsz) != fp->sppcsz){
175 return symerrmsg(fp->sppcsz, "sp-pc");
177 spoffend = spoff+fp->sppcsz;
179 if (fp->lnpcsz) { /* pc-line number table */
180 pcline = (uchar *)malloc(fp->lnpcsz);
182 werrstr("can't malloc %ld bytes", fp->lnpcsz);
185 Bseek(&b, fp->lnpcoff, 0);
186 if(Bread(&b, pcline, fp->lnpcsz) != fp->lnpcsz){
188 return symerrmsg(fp->lnpcsz, "pc-line");
190 pclineend = pcline+fp->lnpcsz;
196 symerrmsg(int n, char *table)
198 werrstr("can't read %d bytes of %s table", n, table);
203 decodename(Biobuf *bp, Sym *p)
210 if((p->type & 0x80) == 0) { /* old-style, fixed length names */
211 p->name = malloc(NNAME);
213 werrstr("can't malloc %d bytes", NNAME);
216 if(Bread(bp, p->name, NNAME) != NNAME)
217 return symerrmsg(NNAME, "symbol");
223 if(p->type == 'z' || p->type == 'Z') {
226 werrstr("can't read symbol name");
232 if(c1 < 0 || c2 < 0) {
233 werrstr("can't read symbol name");
236 if(c1 == 0 && c2 == 0)
239 n = Bseek(bp, 0, 1)-o;
242 werrstr("can't malloc %ld bytes", n);
246 if(Bread(bp, p->name, n) != n) {
247 werrstr("can't read %ld bytes of symbol name", n);
251 cp = Brdline(bp, '\0');
253 werrstr("can't read symbol name");
259 werrstr("can't malloc %ld bytes", n);
268 * free any previously loaded symbol tables
312 * delimit the text segment
315 textseg(uvlong base, Fhdr *fp)
318 txtend = base+fp->txtsz;
322 * symbase: return base and size of raw symbol table
323 * (special hack for high access rate operations)
333 * Get the ith symbol table entry
338 if(index >= 0 && index < nsym)
339 return &symbols[index];
344 * initialize internal symbol tables
359 /* allocate the tables */
361 globals = malloc(nglob*sizeof(*globals));
363 werrstr("can't malloc global symbol table");
368 txt = malloc(ntxt*sizeof(*txt));
370 werrstr("can't malloc text symbol table");
374 fnames = malloc((fmax+1)*sizeof(*fnames));
376 werrstr("can't malloc file name table");
379 memset(fnames, 0, (fmax+1)*sizeof(*fnames));
380 files = malloc(nfiles*sizeof(*files));
382 werrstr("can't malloc file table");
385 hist = malloc(nhist*sizeof(Hist));
387 werrstr("can't malloc history stack");
390 autos = malloc(nauto*sizeof(Sym*));
392 werrstr("can't malloc auto symbol table");
395 /* load the tables */
402 for(p = symbols; i-- > 0; p++) {
409 print("Global: %s %llux\n", p->name, p->value);
413 if(p->value == 1) { /* New file */
416 f->hist[nh].name = 0; /* one extra */
427 /* alloc one slot extra as terminator */
428 f->hist[nh].name = p->name;
429 f->hist[nh].line = p->value;
430 f->hist[nh].offset = 0;
432 printhist("-> ", &f->hist[nh], 1);
437 f->hist[nh-1].offset = p->value;
440 case 't': /* Text: terminate history if first in file */
448 print("TEXT: %s at %llux\n", p->name, p->value);
449 if(f && !f->sym) { /* first */
456 case 'm': /* Local Vars */
458 print("Warning: Free floating local var: %s\n",
462 print("Local: %s %llux\n", p->name, p->value);
463 tp->locals[tp->n] = p;
468 case 'f': /* File names */
470 print("Fname: %s\n", p->name);
471 fnames[p->value] = p;
477 /* sort global and text tables into ascending address order */
478 qsort(globals, nglob, sizeof(Sym*), symcomp);
479 qsort(txt, ntxt, sizeof(Txtsym), txtcomp);
480 qsort(files, nfiles, sizeof(File), filecomp);
482 for(i = 0, f = files; i < nfiles; i++, f++) {
483 for(j = 0; j < ntxt; j++) {
484 if(f->sym == tp->sym) {
486 print("LINK: %s to at %llux", f->sym->name, f->addr);
487 printhist("... ", f->hist, 1);
492 if(++tp >= txt+ntxt) /* wrap around */
500 * find symbol function.var by name.
501 * fn != 0 && var != 0 => look for fn in text, var in data
502 * fn != 0 && var == 0 => look for fn in text
503 * fn == 0 && var != 0 => look for var first in text then in data space.
506 lookup(char *fn, char *var, Symbol *s)
513 found = findtext(fn, s);
514 if(var == 0) /* case 2: fn not in text */
516 else if(!found) /* case 1: fn not found */
519 found = findtext(var, s);
521 return 1; /* case 3: var found in text */
522 } else return 0; /* case 4: fn & var == zero */
525 return findlocal(s, var, s); /* case 1: fn found */
526 return findglobal(var, s); /* case 3: var not found */
531 * find a function by name
534 findtext(char *name, Symbol *s)
538 for(i = 0; i < ntxt; i++) {
539 if(strcmp(txt[i].sym->name, name) == 0) {
540 fillsym(txt[i].sym, s);
541 s->handle = (void *) &txt[i];
549 * find global variable by name
552 findglobal(char *name, Symbol *s)
556 for(i = 0; i < nglob; i++) {
557 if(strcmp(globals[i]->name, name) == 0) {
558 fillsym(globals[i], s);
567 * find the local variable by name within a given function
570 findlocal(Symbol *s1, char *name, Symbol *s2)
576 return findlocvar(s1, name, s2);
580 * find the local variable by name within a given function
581 * (internal function - does no parameter validation)
584 findlocvar(Symbol *s1, char *name, Symbol *s2)
589 tp = (Txtsym *)s1->handle;
590 if(tp && tp->locals) {
591 for(i = 0; i < tp->n; i++)
592 if (strcmp(tp->locals[i]->name, name) == 0) {
593 fillsym(tp->locals[i], s2);
594 s2->handle = (void *)tp;
595 s2->index = tp->n-1 - i;
603 * Get ith text symbol
606 textsym(Symbol *s, int index)
611 if(index < 0 || index >= ntxt)
613 fillsym(txt[index].sym, s);
614 s->handle = (void *)&txt[index];
623 filesym(int index, char *buf, int n)
629 if(index < 0 || index >= nfiles)
631 hp = files[index].hist;
634 return fileelem(fnames, (uchar*)hp->name, buf, n);
638 * Lookup name of local variable located at an offset into the frame.
639 * The type selects either a parameter or automatic.
642 getauto(Symbol *s1, int off, int type, Symbol *s2)
652 else if(type == CAUTO)
658 tp = (Txtsym *)s1->handle;
661 for(i = 0; i < tp->n; i++) {
663 if(p->type == t && p->value == off) {
665 s2->handle = s1->handle;
666 s2->index = tp->n-1 - i;
674 * Find text symbol containing addr; binary search assumes text array is sorted by addr
677 srchtext(uvlong addr)
686 for (mid = (bot+top)/2; mid < top; mid = (bot+top)/2) {
690 else if(mid != ntxt-1 && val >= txt[mid+1].sym->value)
699 * Find data symbol containing addr; binary search assumes data array is sorted by addr
702 srchdata(uvlong addr)
711 for(mid = (bot+top)/2; mid < top; mid = (bot+top)/2) {
715 else if(mid < nglob-1 && val >= globals[mid+1]->value)
724 * Find symbol containing val in specified search space
725 * There is a special case when a value falls beyond the end
726 * of the text segment; if the search space is CTEXT, that value
727 * (usually etext) is returned. If the search space is CANY, symbols in the
728 * data space are searched for a match.
731 findsym(uvlong val, int type, Symbol *s)
738 if(type == CTEXT || type == CANY) {
741 if(type == CTEXT || i != ntxt-1) {
742 fillsym(txt[i].sym, s);
743 s->handle = (void *) &txt[i];
749 if(type == CDATA || type == CANY) {
752 fillsym(globals[i], s);
761 * Find the start and end address of the function containing addr
764 fnbound(uvlong addr, uvlong *bounds)
772 if(0 <= i && i < ntxt-1) {
773 bounds[0] = txt[i].sym->value;
774 bounds[1] = txt[i+1].sym->value;
781 * get the ith local symbol for a function
782 * the input symbol table is reverse ordered, so we reverse
783 * accesses here to maintain approx. parameter ordering in a stack trace.
786 localsym(Symbol *s, int index)
790 if(s == 0 || index < 0)
795 tp = (Txtsym *)s->handle;
796 if(tp && tp->locals && index < tp->n) {
797 fillsym(tp->locals[tp->n-index-1], s); /* reverse */
798 s->handle = (void *)tp;
806 * get the ith global symbol
809 globalsym(Symbol *s, int index)
816 if(index >=0 && index < nglob) {
817 fillsym(globals[index], s);
825 * find the pc given a file name and line offset into it.
828 file2pc(char *file, long line)
832 uvlong pc, start, end;
835 if(buildtbls() == 0 || files == 0)
837 name = encfname(file);
838 if(name == 0) { /* encode the file name */
839 werrstr("file %s not found", file);
842 /* find this history stack */
843 for(i = 0, fp = files; i < nfiles; i++, fp++)
844 if (hline(fp, name, &line))
848 werrstr("line %ld in file %s not found", line, file);
851 start = fp->addr; /* first text addr this file */
853 end = (fp+1)->addr; /* first text addr next file */
855 end = 0; /* last file in load module */
857 * At this point, line contains the offset into the file.
858 * run the state machine to locate the pc closest to that value.
861 print("find pc for %ld - between: %llux and %llux\n", line, start, end);
862 pc = line2addr(line, start, end);
864 werrstr("line %ld not in file %s", line, file);
871 * search for a path component index
874 pathcomp(char *s, int n)
878 for(i = 0; i <= fmax; i++)
879 if(fnames[i] && strncmp(s, fnames[i]->name, n) == 0)
885 * Encode a char file name as a sequence of short indices
886 * into the file name dictionary.
895 if(*file == '/') /* always check first '/' */
898 cp2 = strchr(file, '/');
900 cp2 = strchr(file, 0);
904 for(i = 0; *cp; i++) {
905 j = pathcomp(cp, cp2-cp);
907 return 0; /* not found */
908 dest = realloc(dest, (i+1)*sizeof(short));
911 while(*cp == '/') /* skip embedded '/'s */
913 cp2 = strchr(cp, '/');
917 dest = realloc(dest, (i+1)*sizeof(short));
923 * Search a history stack for a matching file name accumulating
924 * the size of intervening files in the stack.
927 hline(File *fp, short *name, long *line)
933 for(hp = fp->hist; hp->name; hp++) /* find name in stack */
934 if(hp->name[1] || hp->name[2]) {
938 if(!hp->name) /* match not found */
941 printhist("hline found ... ", hp, 1);
943 * unwind the stack until empty or we hit an entry beyond our line
948 for(hp++; depth && hp->name; hp++) {
950 printhist("hline inspect ... ", hp, 1);
951 if(hp->name[1] || hp->name[2]) {
952 if(hp->offset){ /* Z record */
954 if(hcomp(hp, name)) {
955 if(*line <= hp->offset)
957 ln = *line+hp->line-hp->offset;
958 depth = 1; /* implicit pop */
960 depth = 2; /* implicit push */
961 } else if(depth == 1 && ln < hp->line-offset)
962 break; /* Beyond our line */
963 else if(depth++ == 1) /* push */
965 } else if(--depth == 1) /* pop */
973 * compare two encoded file names
976 hcomp(Hist *hp, short *sp)
982 cp = (uchar *)hp->name;
986 for (i = 1; j = (cp[i]<<8)|cp[i+1]; i += 2) {
998 * Convert a pc to a "file:line {file:line}" string.
1001 fileline(char *str, int n, uvlong dot)
1003 long line, top, bot, mid;
1007 if(buildtbls() == 0)
1009 /* binary search assumes file list is sorted by addr */
1012 for (mid = (bot+top)/2; mid < top; mid = (bot+top)/2) {
1016 else if(mid < nfiles-1 && dot >= (f+1)->addr)
1019 line = pc2line(dot);
1020 if(line > 0 && fline(str, n, line, f->hist, 0) >= 0)
1029 * Convert a line number within a composite file to relative line
1030 * number in a source file. A composite file is the source
1031 * file with included files inserted in line.
1034 fline(char *str, int n, long line, Hist *base, Hist **ret)
1036 Hist *start; /* start of current level */
1037 Hist *h; /* current entry */
1038 long delta; /* sum of size of files this level */
1044 while(h && h->name && line > h->line) {
1045 if(h->name[1] || h->name[2]) {
1046 if(h->offset != 0) { /* #line Directive */
1047 delta = h->line-h->offset+1;
1050 } else { /* beginning of File */
1054 k = fline(str, n, line, start, &h);
1060 if(start == base && ret) { /* end of recursion level */
1063 } else { /* end of included file */
1064 delta += h->line-start->line;
1073 line = line-start->line+1;
1075 line = line-delta+1;
1077 strncpy(str, "<eof>", n);
1079 k = fileelem(fnames, (uchar*)start->name, str, n);
1081 sprint(str+k, ":%ld", line);
1083 /**********Remove comments for complete back-trace of include sequence
1084 * if(start != base) {
1090 * k += fileelem(fnames, (uchar*) base->name, str+k, n-k);
1092 * sprint(str+k, ":%ld}", start->line-delta);
1094 ********************/
1099 * convert an encoded file name to a string.
1102 fileelem(Sym **fp, uchar *cp, char *buf, int n)
1109 for(i = 1; j = (cp[i]<<8)|cp[i+1]; i+=2){
1111 if(bp != buf && bp[-1] != '/' && bp < end)
1113 while(bp < end && *c)
1126 * compare the values of two symbol table entries.
1129 symcomp(void *a, void *b)
1133 i = (*(Sym**)a)->value - (*(Sym**)b)->value;
1136 return strcmp((*(Sym**)a)->name, (*(Sym**)b)->name);
1140 * compare the values of the symbols referenced by two text table entries
1143 txtcomp(void *a, void *b)
1145 return ((Txtsym*)a)->sym->value - ((Txtsym*)b)->sym->value;
1149 * compare the values of the symbols referenced by two file table entries
1152 filecomp(void *a, void *b)
1154 return ((File*)a)->addr - ((File*)b)->addr;
1158 * fill an interface Symbol structure from a symbol table entry
1161 fillsym(Sym *sp, Symbol *s)
1164 s->value = sp->value;
1197 * find the stack frame, given the pc
1203 uvlong currpc, currsp;
1208 currpc = txtstart - mach->pcquant;
1210 if(pc<currpc || pc>txtend)
1212 for(c = spoff; c < spoffend; c++) {
1217 currsp += (c[1]<<24)|(c[2]<<16)|(c[3]<<8)|c[4];
1225 currpc += mach->pcquant*(u-129);
1226 currpc += mach->pcquant;
1232 * find the source file line number for a given value of the pc
1244 currpc = txtstart-mach->pcquant;
1245 if(pc<currpc || pc>txtend)
1248 for(c = pcline; c < pclineend; c++) {
1253 currline += (c[1]<<24)|(c[2]<<16)|(c[3]<<8)|c[4];
1261 currpc += mach->pcquant*(u-129);
1262 currpc += mach->pcquant;
1268 * find the pc associated with a line number
1269 * basepc and endpc are text addresses bounding the search.
1270 * if endpc == 0, the end of the table is used (i.e., no upper bound).
1271 * usually, basepc and endpc contain the first text address in
1272 * a file and the first text address in the following file, respectively.
1275 line2addr(long line, uvlong basepc, uvlong endpc)
1283 if(pcline == 0 || line == 0)
1287 currpc = txtstart-mach->pcquant;
1292 for(c = pcline; c < pclineend; c++) {
1293 if(endpc && currpc >= endpc) /* end of file of interest */
1295 if(currpc >= basepc) { /* proper file */
1296 if(currline >= line) {
1308 currline += (c[1]<<24)|(c[2]<<16)|(c[3]<<8)|c[4];
1316 currpc += mach->pcquant*(u-129);
1317 currpc += mach->pcquant;
1325 * Print a history stack (debug). if count is 0, prints the whole stack
1328 printhist(char *msg, Hist *hp, int count)
1336 if(count && ++i > count)
1338 print("%s Line: %lx (%ld) Offset: %lx (%ld) Name: ", msg,
1339 hp->line, hp->line, hp->offset, hp->offset);
1340 for(cp = (uchar *)hp->name+1; (*cp<<8)|cp[1]; cp += 2) {
1341 if (cp != (uchar *)hp->name+1)
1343 print("%x", (*cp<<8)|cp[1]);
1345 fileelem(fnames, (uchar *) hp->name, buf, sizeof(buf));
1346 print(" (%s)\n", buf);
1353 * print the history stack for a file. (debug only)
1354 * if (name == 0) => print all history stacks.
1357 dumphist(char *name)
1363 if(buildtbls() == 0)
1366 fname = encfname(name);
1367 for(i = 0, f = files; i < nfiles; i++, f++)
1368 if(fname == 0 || hcomp(f->hist, fname))
1369 printhist("> ", f->hist, f->n);