static void get_file(char *ifile) { int h; int d; Ifd = open(ifile, OREAD); if (Ifd < 0) { fprint(2, "5cv: open %s: %r\n", ifile); exits("open"); } h = crackhdr(Ifd, &Ihdr); if (!h || Debug){ fprint(2, "Crackhdr: %d, type: %d, name: %s\n", h, Ihdr.type, Ihdr.name); fprint(2, "txt %lux, ent %lux, txtsz %lux, dataddr %lux\n", Ihdr.txtaddr, Ihdr.entry, Ihdr.txtsz, Ihdr.dataddr); } if (!h) Usage("File type not recognized"); machbytype(Ihdr.type); if (Debug) fprint(2, "name: <%s> pgsize:%ux\n", mach->name, mach->pgsize); if (Txtaddr != -1){ d = Txtaddr - Ihdr.txtaddr; Ihdr.txtaddr += d; Ihdr.dataddr = Ihdr.txtaddr + Ihdr.txtsz; } }
void setsym(void) { Symbol s; if((fsym = getfile(symfil, 1, wtflag)) < 0) { symmap = dumbmap(-1); return; } if (crackhdr(fsym, &fhdr)) { machbytype(fhdr.type); symmap = loadmap(symmap, fsym, &fhdr); if (symmap == 0) symmap = dumbmap(fsym); if (syminit(fsym, &fhdr) < 0) dprint("%r\n"); if (mach->sbreg && lookup(0, mach->sbreg, &s)) mach->sb = s.value; } else symmap = dumbmap(fsym); }
void main(int argc, char *argv[]) { int fd; long i, j, k, n; Dir *d; char *name; ulong *data; ulong tbase, sum; long delta; Symbol s; Biobuf outbuf; Fhdr f; struct COUNTER *cp; char filebuf[128], *file; if(argc != 2 && argc != 3) error(0, "usage: tprof pid [binary]"); /* * Read symbol table */ if(argc == 2){ file = filebuf; snprint(filebuf, sizeof filebuf, "/proc/%s/text", argv[1]); }else file = argv[2]; fd = open(file, OREAD); if(fd < 0) error(1, file); if (!crackhdr(fd, &f)) error(1, "read text header"); if (f.type == FNONE) error(0, "text file not an a.out"); machbytype(f.type); if (syminit(fd, &f) < 0) error(1, "syminit"); close(fd); /* * Read timing data */ file = smprint("/proc/%s/profile", argv[1]); fd = open(file, OREAD); if(fd < 0) error(1, file); free(file); d = dirfstat(fd); if(d == nil) error(1, "stat"); n = d->length/sizeof(data[0]); if(n < 2) error(0, "data file too short"); data = malloc(d->length); if(data == 0) error(1, "malloc"); if(read(fd, data, d->length) < 0) error(1, "text read"); close(fd); for(i=0; i<n; i++) data[i] = machdata->swal(data[i]); delta = data[0]-data[1]; print("total: %ld\n", data[0]); if(data[0] == 0) exits(0); if (!textsym(&s, 0)) error(0, "no text symbols"); tbase = s.value & ~(mach->pgsize-1); /* align down to page */ print("TEXT %.8lux\n", tbase); /* * Accumulate counts for each function */ cp = 0; k = 0; for (i = 0, j = (s.value-tbase)/PCRES+2; j < n; i++) { name = s.name; /* save name */ if (!textsym(&s, i)) /* get next symbol */ break; sum = 0; while (j < n && j*PCRES < s.value-tbase) sum += data[j++]; if (sum) { cp = realloc(cp, (k+1)*sizeof(struct COUNTER)); if (cp == 0) error(1, "realloc"); cp[k].name = name; cp[k].time = sum; k++; } } if (!k) error(0, "no counts"); cp[k].time = 0; /* "etext" can take no time */ /* * Sort by time and print */ qsort(cp, k, sizeof(struct COUNTER), compar); Binit(&outbuf, 1, OWRITE); Bprint(&outbuf, " ms %% sym\n"); while(--k>=0) Bprint(&outbuf, "%6ld\t%3lld.%lld\t%s\n", cp[k].time, 100LL*cp[k].time/delta, (1000LL*cp[k].time/delta)%10, cp[k].name); exits(0); }
void readtext(char *s) { Dir *d; Lsym *l; Value *v; Symbol sym; ulong length; extern Machdata mipsmach; if(mtype != 0){ symmap = newmap(0, 1); if(symmap == 0) print("%s: (error) loadmap: cannot make symbol map\n", argv0); length = 1<<24; d = dirfstat(text); if(d != nil) { length = d->length; free(d); } setmap(symmap, text, 0, length, 0, "binary"); free(d); return; } machdata = &mipsmach; if(!crackhdr(text, &fhdr)) { print("can't decode file header\n"); return; } symmap = loadmap(0, text, &fhdr); if(symmap == 0) print("%s: (error) loadmap: cannot make symbol map\n", argv0); if(syminit(text, &fhdr) < 0) { print("%s: (error) syminit: %r\n", argv0); return; } print("%s:%s\n\n", s, fhdr.name); if(mach->sbreg && lookup(0, mach->sbreg, &sym)) { mach->sb = sym.value; l = enter("SB", Tid); l->v->vstore.fmt = 'X'; l->v->vstore.u0.sival = mach->sb; l->v->type = TINT; l->v->set = 1; } l = mkvar("objtype"); v = l->v; v->vstore.fmt = 's'; v->set = 1; v->vstore.u0.sstring = strnode(mach->name); v->type = TSTRING; l = mkvar("textfile"); v = l->v; v->vstore.fmt = 's'; v->set = 1; v->vstore.u0.sstring = strnode(s); v->type = TSTRING; machbytype(fhdr.type); varreg(); }