void setcor(void) { int i; if (cormap) { for (i = 0; i < cormap->nsegs; i++) if (cormap->seg[i].inuse) close(cormap->seg[i].fd); } fcor = getfile(corfil, 2, ORDWR); if (fcor <= 0) { if (cormap) free(cormap); cormap = dumbmap(-1); return; } if(pid > 0) { /* provide addressability to executing process */ cormap = attachproc(pid, kflag, fcor, &fhdr); if (!cormap) cormap = dumbmap(-1); } else { cormap = newmap(cormap, 2); if (!cormap) cormap = dumbmap(-1); setmap(cormap, fcor, fhdr.txtaddr, fhdr.txtaddr+fhdr.txtsz, fhdr.txtaddr, "text"); setmap(cormap, fcor, fhdr.dataddr, 0xffffffff, fhdr.dataddr, "data"); } kmsys(); return; }
int getthreads(void) { int i, j, curn, found; Map *curmap[nelem(map)]; int curthread[nelem(map)]; static int complained = 0; curn = procthreadpids(pid, curthread, nelem(curthread)); if(curn <= 0) return curn; if(curn > nelem(map)) { if(complained == 0) { fprint(2, "prof: too many threads; limiting to %d\n", nthread, nelem(map)); complained = 1; } curn = nelem(map); } if(curn == nthread && memcmp(thread, curthread, curn*sizeof(*thread)) == 0) return curn; // no changes // Number of threads has changed (might be the init case). // A bit expensive but rare enough not to bother being clever. for(i = 0; i < curn; i++) { found = 0; for(j = 0; j < nthread; j++) { if(curthread[i] == thread[j]) { found = 1; curmap[i] = map[j]; map[j] = nil; break; } } if(found) continue; // map new thread curmap[i] = attachproc(curthread[i], &fhdr); if(curmap[i] == nil) { fprint(2, "prof: can't attach to %d: %r\n", curthread[i]); return -1; } } for(j = 0; j < nthread; j++) if(map[j] != nil) detachproc(map[j]); nthread = curn; memmove(thread, curthread, nthread*sizeof thread[0]); memmove(map, curmap, sizeof map); return nthread; }
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); }
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); }