void samples(void) { int i, pid, msec; struct timespec req; req.tv_sec = delta_msec/1000; req.tv_nsec = 1000000*(delta_msec % 1000); for(msec = 0; total_sec <= 0 || msec < 1000*total_sec; msec += delta_msec) { nsample++; nsamplethread += nthread; for(i = 0; i < nthread; i++) { pid = thread[i]; if(ctlproc(pid, "stop") < 0) return; if(!sample(map[i])) { ctlproc(pid, "start"); return; } printpc(map[i], ureg.ip, ureg.sp); ctlproc(pid, "start"); } nanosleep(&req, NULL); getthreads(); if(nthread == 0) break; } }
void samples(void) { int i, pid, msec; struct timespec req; int getmaps; req.tv_sec = delta_msec/1000; req.tv_nsec = 1000000*(delta_msec % 1000); getmaps = 0; if(pprof) getmaps= 1; for(msec = 0; total_sec <= 0 || msec < 1000*total_sec; msec += delta_msec) { nsample++; nsamplethread += nthread; for(i = 0; i < nthread; i++) { pid = thread[i]; if(ctlproc(pid, "stop") < 0) return; if(!sample(map[i])) { ctlproc(pid, "start"); return; } printpc(map[i], arch->uregPC(), arch->uregSP()); ctlproc(pid, "start"); } nanosleep(&req, NULL); getthreads(); if(nthread == 0) break; if(getmaps) { getmaps = 0; ppmaps(); } } }
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); }