int crackhdr(int fd, Fhdr *fp) { ExecTable *mp; ExecHdr d; int nb, magic, ret; fp->type = FNONE; nb = read(fd, (char *)&d.e, sizeof(d.e)); if (nb <= 0) return 0; ret = 0; fp->magic = magic = beswal(d.e.exec.magic); /* big-endian */ for (mp = exectab; mp->magic; mp++) { if (mp->magic == magic && nb >= mp->hsize) { //if(mp->magic == V_MAGIC) // mp = couldbe4k(mp); hswal((long *) &d, sizeof(d.e)/sizeof(long), mp->swal); fp->type = mp->type; fp->name = mp->name; fp->hdrsz = mp->hsize; /* zero on bootables */ mach = mp->mach; ret = mp->hparse(fd, fp, &d); seek(fd, mp->hsize, 0); /* seek to end of header */ break; } } if(mp->magic == 0) werrstr("unknown header type"); return ret; }
int crackhdr(int fd, Fhdr *fp) { ExecTable *mp; ExecHdr d; int nb, ret; ulong magic; fp->type = FNONE; nb = read(fd, (char *)&d.e, sizeof(d.e)); if (nb <= 0) return 0; ret = 0; magic = beswal(d.e.magic); /* big-endian */ for (mp = exectab; mp->magic; mp++) { if (nb < mp->hsize) continue; /* * The magic number has morphed into something * with fields (the straw was DYN_MAGIC) so now * a flag is needed in Fhdr to distinguish _MAGIC() * magic numbers from foreign magic numbers. * * This code is creaking a bit and if it has to * be modified/extended much more it's probably * time to step back and redo it all. */ if(mp->_magic){ if(mp->magic != (magic & ~DYN_MAGIC)) continue; if(mp->magic == V_MAGIC) mp = couldbe4k(mp); if ((magic & DYN_MAGIC) && mp->dlmname != nil) fp->name = mp->dlmname; else fp->name = mp->name; } else{ if(mp->magic != magic) continue; fp->name = mp->name; } fp->type = mp->type; fp->hdrsz = mp->hsize; /* will be zero on bootables */ fp->_magic = mp->_magic; fp->magic = magic; mach = mp->mach; if(mp->swal != nil) hswal(&d, sizeof(d.e)/sizeof(ulong), mp->swal); ret = mp->hparse(fd, fp, &d); seek(fd, mp->hsize, 0); /* seek to end of header */ break; } if(mp->magic == 0) werrstr("unknown header type"); return ret; }
void main(int argc, char *argv[]) { int fd; long i, j, k, n; char *name; ulong *data; vlong tbase; ulong sum; long 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 = s.value & ~(mach->pgsize-1); /* align down to page */ print("KTZERO %.8llux\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, "%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); }
int 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.%d\t%s\n", tickstoms(cp[k].time), 100*cp[k].time/delta, (1000*cp[k].time/delta)%10, cp[k].name); exits(0); }
/* * Read from Windows PE/COFF .exe file image. */ static int pedotout(int fd, Fhdr *fp, ExecHdr *hp) { uint32 start, magic; uint32 symtab, esymtab, pclntab, epclntab; IMAGE_FILE_HEADER fh; IMAGE_SECTION_HEADER sh; IMAGE_OPTIONAL_HEADER oh; PE64_IMAGE_OPTIONAL_HEADER oh64; uint8 sym[18]; uint32 *valp, ib, entry; int i, ohoffset; USED(hp); seek(fd, 0x3c, 0); if (readn(fd, &start, sizeof(start)) != sizeof(start)) { werrstr("crippled PE MSDOS header"); return 0; } start = leswal(start); seek(fd, start, 0); if (readn(fd, &magic, sizeof(magic)) != sizeof(magic)) { werrstr("no PE magic number found"); return 0; } if (beswal(magic) != 0x50450000) { /* "PE\0\0" */ werrstr("incorrect PE magic number"); return 0; } if (readn(fd, &fh, sizeof(fh)) != sizeof(fh)) { werrstr("crippled PE File Header"); return 0; } if (fh.PointerToSymbolTable == 0) { werrstr("zero pointer to COFF symbol table"); return 0; } ohoffset = seek(fd, 0, 1); if (readn(fd, &oh, sizeof(oh)) != sizeof(oh)) { werrstr("crippled PE Optional Header"); return 0; } switch(oh.Magic) { case 0x10b: // PE32 fp->type = FI386; ib = leswal(oh.ImageBase); entry = leswal(oh.AddressOfEntryPoint); break; case 0x20b: // PE32+ fp->type = FAMD64; seek(fd, ohoffset, 0); if (readn(fd, &oh64, sizeof(oh64)) != sizeof(oh64)) { werrstr("crippled PE32+ Optional Header"); return 0; } ib = leswal(oh64.ImageBase); entry = leswal(oh64.AddressOfEntryPoint); break; default: werrstr("invalid PE Optional Header magic number"); return 0; } fp->txtaddr = 0; fp->dataddr = 0; for (i=0; i<leswab(fh.NumberOfSections); i++) { if (readn(fd, &sh, sizeof(sh)) != sizeof(sh)) { werrstr("could not read Section Header %d", i+1); return 0; } if (match8(sh.Name, ".text")) settext(fp, ib+entry, ib+leswal(sh.VirtualAddress), leswal(sh.VirtualSize), leswal(sh.PointerToRawData)); if (match8(sh.Name, ".data")) setdata(fp, ib+leswal(sh.VirtualAddress), leswal(sh.SizeOfRawData), leswal(sh.PointerToRawData), leswal(sh.VirtualSize)-leswal(sh.SizeOfRawData)); } if (fp->txtaddr==0 || fp->dataddr==0) { werrstr("no .text or .data"); return 0; } seek(fd, leswal(fh.PointerToSymbolTable), 0); symtab = esymtab = pclntab = epclntab = 0; for (i=0; i<leswal(fh.NumberOfSymbols); i++) { if (readn(fd, sym, sizeof(sym)) != sizeof(sym)) { werrstr("crippled COFF symbol %d", i); return 0; } valp = (uint32 *)&sym[8]; if (match8(sym, "symtab")) symtab = leswal(*valp); if (match8(sym, "esymtab")) esymtab = leswal(*valp); if (match8(sym, "pclntab")) pclntab = leswal(*valp); if (match8(sym, "epclntab")) epclntab = leswal(*valp); } if (symtab==0 || esymtab==0 || pclntab==0 || epclntab==0) { werrstr("no symtab or esymtab or pclntab or epclntab in COFF symbol table"); return 0; } setsym(fp, symtab, esymtab-symtab, 0, 0, pclntab, epclntab-pclntab); return 1; }