void main(int argc, char **argv) { ElfSym esym; Fhdr *fp; int i, j; ARGBEGIN{ default: usage(); }ARGEND if(argc == 0) usage(); for(i=0; i<argc; i++){ if((fp = crackhdr(argv[i], OREAD)) == nil){ fprint(2, "%s: %r\n", argv[i]); continue; } for(j=0; elfsym(fp->elf, j, &esym)>=0; j++) print("%s 0x%lux\n", esym.name, esym.value); uncrackhdr(fp); } exits(0); }
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 corefile(char *name, int explicit) { Fhdr *hdr; char t[100]; Dir *d; if((d = dirstat(name)) == nil){ if(explicit) fprint(2, "%s; %r\n", name); return; } strcpy(t, ctime(d->mtime)); t[strlen(t)-1] = 0; /* newline */ if((hdr = crackhdr(name, OREAD)) == nil){ if(explicit) fprint(2, "%s: %r\n", name); return; } if(hdr->ftype != FCORE){ uncrackhdr(hdr); if(explicit) fprint(2, "%s: not a core file\n", name); return; }
static void inithdr(int fd) { seek(fd, 0, 0); if(!crackhdr(fd, &fhdr)) fatal("read text header"); if(syminit(fd, &fhdr) < 0) fatal("%r\n"); }
/* * get the symbol table from an executable file, if it has one */ void execsyms(int fd) { Fhdr f; Sym *s; int32_t n; seek(fd, 0, 0); if (crackhdr(fd, &f) == 0) { error("Can't read header for %s", filename); return; } if (syminit(fd, &f) < 0) return; s = symbase(&n); nsym = 0; while(n--) psym(s++, 0); printsyms(symptr, nsym); }
int size(char *file) { int fd; Fhdr f; if((fd = open(file, OREAD)) < 0){ fprint(2, "size: "); perror(file); return 1; } if(crackhdr(fd, &f)) { print("%ldt + %ldd + %ldb = %ld\t%s\n", f.txtsz, f.datsz, f.bsssz, f.txtsz+f.datsz+f.bsssz, file); close(fd); return 0; } fprint(2, "size: %s not an a.out\n", file); close(fd); return 1; }
void inithdr(int fd) { Symbol s; extern Machdata powermach; seek(fd, 0, 0); if (!crackhdr(fd, &fhdr)) fatal(0, "read text header"); if(fhdr.type != FPOWER) fatal(0, "bad magic number"); if(syminit(fd, &fhdr) < 0) fatal(0, "%r\n"); symmap = loadmap(symmap, fd, &fhdr); if (mach->sbreg && lookup(0, mach->sbreg, &s)) mach->sb = s.value; machdata = &powermach; }
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 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; if(argc != 3) error(0, "usage: kprof text data"); /* * Read symbol table */ fd = open(argv[1], OREAD); if(fd < 0) error(1, argv[1]); if (!crackhdr(fd, &f)) error(1, "read text header"); if (f.type == FNONE) error(0, "text file not an a.out"); if (syminit(fd, &f) < 0) error(1, "syminit"); close(fd); /* * Read timing data */ fd = open(argv[2], OREAD); if(fd < 0) error(1, argv[2]); if((d = dirfstat(fd)) == 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); free(d); for(i=0; i<n; i++) data[i] = beswal(data[i]); pcres = 1 << data[SpecialSampleLogBucketSize]; uspertick = data[SpecialMicroSecondsPerTick]; if (data[SpecialSampleSize] != sizeof(data[0])) error(0, "only sample size 4 supported\n"); delta = data[SpecialTotalTicks] - data[SpecialOutsideTicks]; print("total: %lud in kernel text: %lud outside kernel text: %lud\n", data[0], delta, data[1]); 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("KTZERO %.8lux\n", tbase); /* * Accumulate counts for each function */ cp = 0; k = 0; for (i = 0, j = (s.value-tbase)/pcres+SpecialMax; 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, "%lud\t%3lud.%ld\t%s\n", tickstoms(cp[k].time), 100*cp[k].time/delta, (1000*cp[k].time/delta)%10, cp[k].name); exits(0); }
void main(int argc, char *argv[]) { int fd; int32_t i, j, k, n; char *name; uint32_t *data; int64_t tbase; uint32_t sum; int32_t delta; Symbol s; Biobuf outbuf; Fhdr f; Dir *d; struct COUNTER *cp; if(argc != 3) error(0, "usage: kprof text data"); /* * Read symbol table */ fd = open(argv[1], OREAD); if(fd < 0) error(1, argv[1]); if (!crackhdr(fd, &f)) error(1, "read text header"); if (f.type == FNONE) error(0, "text file not an a.out"); if (syminit(fd, &f) < 0) error(1, "syminit"); close(fd); /* * Read timing data */ fd = open(argv[2], OREAD); if(fd < 0) error(1, argv[2]); 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] = beswal(data[i]); delta = data[0]-data[1]; print("total: %ld in kernel text: %ld outside kernel text: %ld\n", data[0], delta, data[1]); if(data[0] == 0) exits(0); if (!textsym(&s, 0)) error(0, "no text symbols"); tbase = mach->kbase; if(tbase != s.value & ~0xFFF) print("warning: kbase %.8llux != tbase %.8llux\n", tbase, s.value&~0xFFF); print("KTZERO %.8llux PGSIZE %dKb\n", tbase, mach->pgsize/1024); /* * Accumulate counts for each function */ cp = 0; k = 0; for (i = 0, j = 2; j < n; i++) { name = s.name; /* save name */ if (!textsym(&s, i)) /* get next symbol */ break; s.value -= tbase; s.value /= PCRES; sum = 0; while (j < n && j < s.value) 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, "%ld\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); }
static int strip(char* file, char* out) { Dir *dir; int fd, i; Fhdr fhdr; Exec *exec; ulong mode; void *data; vlong length; if((fd = open(file, OREAD)) < 0){ error("%s: open: %r", file); return 1; } if(!crackhdr(fd, &fhdr)){ error("%s: %r", file); close(fd); return 1; } for(i = MIN_MAGIC; i <= MAX_MAGIC; i++){ if(fhdr.magic == _MAGIC(0, i) || fhdr.magic == _MAGIC(HDR_MAGIC, i)) break; } if(i > MAX_MAGIC){ error("%s: not a recognizeable binary", file); close(fd); return 1; } if((dir = dirfstat(fd)) == nil){ error("%s: stat: %r", file); close(fd); return 1; } length = fhdr.datoff+fhdr.datsz; if(length == dir->length){ if(out == nil){ /* nothing to do */ error("%s: already stripped", file); free(dir); close(fd); return 0; } } if(length > dir->length){ error("%s: strange length", file); close(fd); free(dir); return 1; } mode = dir->mode; free(dir); if((data = malloc(length)) == nil){ error("%s: malloc failure", file); close(fd); return 1; } seek(fd, 0LL, 0); if(read(fd, data, length) != length){ error("%s: read: %r", file); close(fd); free(data); return 1; } close(fd); exec = data; exec->syms = 0; exec->spsz = 0; exec->pcsz = 0; if(out == nil){ if(remove(file) < 0) { error("%s: remove: %r", file); free(data); return 1; } out = file; } if((fd = create(out, OWRITE, mode)) < 0){ error("%s: create: %r", out); free(data); return 1; } if(write(fd, data, length) != length){ error("%s: write: %r", out); close(fd); free(data); return 1; } close(fd); free(data); return 0; }
int main(int argc, char *argv[]) { int i; char *ppfile; ARGBEGIN{ case 'P': pprof =1; ppfile = EARGF(Usage()); pproffd = Bopen(ppfile, OWRITE); if(pproffd == nil) { fprint(2, "prof: cannot open %s: %r\n", ppfile); exit(2); } break; case 'd': delta_msec = atoi(EARGF(Usage())); break; case 't': total_sec = atoi(EARGF(Usage())); break; case 'p': pid = atoi(EARGF(Usage())); break; case 'f': functions = 1; break; case 'h': histograms = 1; break; case 'l': linenums = 1; break; case 'r': registers = 1; break; case 's': stacks++; break; default: Usage(); }ARGEND if(pid <= 0 && argc == 0) Usage(); if(functions+linenums+registers+stacks+pprof == 0) histograms = 1; if(!machbyname("amd64")) { fprint(2, "prof: no amd64 support\n", pid); exit(1); } if(argc > 0) file = argv[0]; else if(pid) { file = proctextfile(pid); if (file == NULL) { fprint(2, "prof: can't find file for pid %d: %r\n", pid); fprint(2, "prof: on Darwin, need to provide file name explicitly\n"); exit(1); } } fd = open(file, 0); if(fd < 0) { fprint(2, "prof: can't open %s: %r\n", file); exit(1); } if(crackhdr(fd, &fhdr)) { have_syms = syminit(fd, &fhdr); if(!have_syms) { fprint(2, "prof: no symbols for %s: %r\n", file); } } else { fprint(2, "prof: crack header for %s: %r\n", file); exit(1); } if(pid <= 0) pid = startprocess(argv); attachproc(pid, &fhdr); // initializes thread list if(setarch() < 0) { detach(); fprint(2, "prof: can't identify binary architecture for pid %d\n", pid); exit(1); } if(getthreads() <= 0) { detach(); fprint(2, "prof: can't find threads for pid %d\n", pid); exit(1); } for(i = 0; i < nthread; i++) ctlproc(thread[i], "start"); samples(); detach(); dumphistogram(); dumppprof(); exit(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(); }
int main(int argc, char *argv[]) { int i; ARGBEGIN{ case 'd': delta_msec = atoi(EARGF(Usage())); break; case 't': total_sec = atoi(EARGF(Usage())); break; case 'p': pid = atoi(EARGF(Usage())); break; case 'f': functions = 1; break; case 'h': histograms = 1; break; case 'l': linenums = 1; break; case 'r': registers = 1; break; case 's': stacks++; break; }ARGEND if(pid <= 0 && argc == 0) Usage(); if(functions+linenums+registers+stacks == 0) histograms = 1; if(!machbyname("amd64")) { fprint(2, "prof: no amd64 support\n", pid); exit(1); } if(argc > 0) file = argv[0]; else if(pid) file = proctextfile(pid); fd = open(file, 0); if(fd < 0) { fprint(2, "prof: can't open %s: %r\n", file); exit(1); } if(crackhdr(fd, &fhdr)) { have_syms = syminit(fd, &fhdr); if(!have_syms) { fprint(2, "prof: no symbols for %s: %r\n", file); } } else { fprint(2, "prof: crack header for %s: %r\n", file); exit(1); } if(pid <= 0) pid = startprocess(argv); attachproc(pid, &fhdr); // initializes thread list if(getthreads() <= 0) { detach(); fprint(2, "prof: can't find threads for pid %d\n", pid); exit(1); } for(i = 0; i < nthread; i++) ctlproc(thread[i], "start"); samples(); detach(); dumphistogram(); exit(0); }