2 * Copyright (c) 1996 Barton P. Miller
4 * We provide the Paradyn Parallel Performance Tools (below
5 * described as Paradyn") on an AS IS basis, and do not warrant its
6 * validity or performance. We reserve the right to update, modify,
7 * or discontinue this software at any time. We shall have no
8 * obligation to supply such updates or modifications or any other
9 * form of support to you.
11 * This license is for research uses. For such uses, there is no
12 * charge. We define "research use" to mean you may freely use it
13 * inside your organization for whatever purposes you see fit. But you
14 * may not re-distribute Paradyn or parts of Paradyn, in any form
15 * source or binary (including derivatives), electronic or otherwise,
16 * to any other organization or entity without our permission.
18 * (for other uses, please contact us at paradyn@cs.wisc.edu)
20 * All warranties, including without limitation, any warranty of
21 * merchantability or fitness for a particular purpose, are hereby
24 * By your use of Paradyn, you understand and agree that we (or any
25 * other person or entity with proprietary rights in Paradyn) are
26 * under no obligation to provide either maintenance services,
27 * update services, notices of latent defects, or correction of
28 * defects for Paradyn.
30 * Even if advised of the possibility of such damages, under no
31 * circumstances shall we (or any other person or entity with
32 * proprietary rights in the software licensed hereunder) be liable
33 * to you or any third party for direct, indirect, or consequential
34 * damages of any character regardless of type of action, including,
35 * without limitation, loss of profits, loss of use, loss of good
36 * will, or computer failure or malfunction. You agree to indemnify
37 * us (and any other person or entity with proprietary rights in the
38 * software licensed hereunder) for any and all liability it may
39 * incur to third parties resulting from your use of Paradyn.
44 * Revision 1.30 1997/07/08 19:15:18 buck
45 * Added support for the x86 Solaris platform and dynamically linked
46 * executables to the dyninst API library.
48 * Revision 1.29 1997/07/01 16:54:56 tamches
49 * dummy set_breakpoint_for_syscall_completion
51 * Revision 1.28 1997/06/23 19:16:03 buck
52 * Added features to the dyninst API library, including an optional "else"
53 * in a BPatch_ifExpr; the BPatch_setMutationsActive call to temporarily
54 * disable all snippets; and the replaceFunctionCall and removeFunctionCall
55 * member functions of BPatch_thread to retarget or NOOP out a function
58 * Revision 1.1.1.3 1997/06/11 17:33:02 buck
59 * Update Maryland repository with latest changes from Wisconsin.
61 * Revision 1.27 1997/05/16 18:48:26 naim
62 * Fixing problem when inferiorRPC was launched and the application was in
63 * the middle of a system call - naim
65 * Revision 1.26 1997/04/14 00:22:26 newhall
66 * removed class pdFunction and replaced it with base class function_base and
67 * derived class pd_Function
69 * Revision 1.25 1997/03/18 19:44:25 buck
70 * first commit of dyninst library. Also includes:
71 * moving templates from paradynd to dyninstAPI
72 * converting showError into a function (in showerror.C)
73 * many ifdefs for BPATCH_LIBRARY in dyinstAPI/src.
75 * Revision 1.24 1997/02/26 23:43:03 mjrg
76 * First part on WindowsNT port: changes for compiling with Visual C++;
77 * moved unix specific code to unix.C
79 * Revision 1.23 1997/02/21 20:13:53 naim
80 * Moving files from paradynd to dyninstAPI + moving references to dataReqNode
81 * out of the ast class. The is the first pre-dyninstAPI commit! - naim
83 * Revision 1.22 1997/01/30 18:14:12 tamches
84 * skeleton isRunning, tryToFindExecutable, and read_inferiorRPC_result_register
86 * Revision 1.21 1996/11/23 22:46:41 lzheng
87 * Finished the implementation of inferiorPRC on HPUX platfrom
89 * Revision 1.20 1996/11/19 16:28:24 newhall
90 * Fix to stack walking on Solaris: find leaf functions in stack (these can occur
91 * on top of stack or in middle of stack if the signal handler is on the stack)
92 * Fix to relocated functions: new instrumentation points are kept on a per
93 * process basis. Cleaned up some of the code.
95 * Revision 1.19 1996/11/05 20:35:34 tamches
96 * changed some OS:: methods to process:: methods
98 * Revision 1.18 1996/10/31 08:55:11 tamches
99 * the shm-sampling commit; routines to do inferiorRPC; removed some warnings.
101 * Revision 1.17 1996/08/20 19:18:02 lzheng
102 * Implementation of moving multiple instructions sequence and
103 * splitting the instrumentation into two phases
105 * Revision 1.16 1996/08/16 21:19:54 tamches
106 * updated copyright for release 1.1
108 * Revision 1.15 1996/05/08 23:55:09 mjrg
109 * added support for handling fork and exec by an application
110 * use /proc instead of ptrace on solaris
113 * Revision 1.14 1996/04/03 14:27:58 naim
114 * Implementation of deallocation of instrumentation for solaris and sunos - naim
116 * Revision 1.13 1996/03/12 20:48:40 mjrg
117 * Improved handling of process termination
118 * New version of aggregateSample to support adding and removing components
120 * Added error messages
122 * Revision 1.12 1996/02/12 16:46:19 naim
123 * Updating the way we compute number_of_cpus. On solaris we will return the
124 * number of cpus; on sunos, hp, aix 1 and on the CM-5 the number of processes,
125 * which should be equal to the number of cpus - naim
127 * Revision 1.11 1996/02/09 23:53:48 naim
128 * Adding new internal metric number_of_nodes - naim
130 * Revision 1.10 1995/11/22 00:02:23 mjrg
131 * Updates for paradyndPVM on solaris
132 * Fixed problem with wrong daemon getting connection to paradyn
133 * Removed -f and -t arguments to paradyn
134 * Added cleanUpAndExit to clean up and exit from pvm before we exit paradynd
135 * Fixed bug in my previous commit
140 #include "dyninstAPI/src/process.h"
143 extern int ioctl(int, int, ...);
144 extern int getrusage(int, struct rusage*);
146 #include <sys/exec.h>
148 extern struct rusage *mapUarea();
150 #include <machine/reg.h> // for ptrace_getregs call
153 #include <sys/ioctl.h>
155 #include <machine/reg.h>
156 #include <sys/user.h> // for u-area stuff
158 #include "dyninstAPI/src/symtab.h"
159 #include "util/h/headers.h"
160 #include "dyninstAPI/src/os.h"
161 #include "dyninstAPI/src/stats.h"
162 #include "util/h/Types.h"
163 #include "paradynd/src/showerror.h"
164 #include "dyninstAPI/src/util.h" // getCurrWallTime
165 #include "util/h/pathName.h"
169 #include <sys/user.h>
172 // #include <sys/termios.h>
174 extern bool isValidAddress(process *proc, Address where);
176 extern struct rusage *mapUarea();
178 /* ********************************************************************** */
182 static bool haltProcess(process *p);
183 static void continueProcess(process *p, const bool halted);
186 static bool deliverPtrace(process *p, enum ptracereq req, void *addr,
187 int data, void *addr2);
188 // static bool deliverPtraceFast(process *p, enum ptracereq req, void *addr,
189 // int data, void *addr2);
190 // // like deliverPtrace() but assumes process is paused.
193 bool ptraceKludge::haltProcess(process *p) {
194 bool wasStopped = (p->status() == stopped);
195 if (p->status() != neonatal && !wasStopped) {
196 if (!p->loopUntilStopped()) {
197 cerr << "error in loopUntilStopped\n";
204 void ptraceKludge::continueProcess(process *p, const bool wasStopped) {
205 // First handle the cases where we shouldn't issue a PTRACE_CONT:
206 if (p->status() == neonatal) return;
207 if (wasStopped) return;
209 // Choose either one of the following methods to continue a process.
210 // The choice must be consistent with that in process::continueProc_ and stop_
212 #ifndef PTRACE_ATTACH_DETACH
213 if (P_ptrace(PTRACE_CONT, p->pid, (caddr_t) 1, SIGCONT, NULL) == -1) {
215 if (P_ptrace(PTRACE_DETACH, p->pid, (caddr_t) 1, SIGCONT, NULL) == -1) {
217 perror("error in continueProcess");
222 bool ptraceKludge::deliverPtrace(process *p, enum ptracereq req, void *addr,
223 int data, void *addr2) {
226 if (req != PTRACE_DETACH)
227 halted = haltProcess(p);
229 bool ret = (P_ptrace(req, p->getPid(), (char*)addr, data, (char*)addr2) != -1);
231 if (req != PTRACE_DETACH)
232 continueProcess(p, halted);
237 /* ********************************************************************** */
239 void *process::getRegisters() {
240 // ptrace - GETREGS call
241 // assumes the process is stopped (ptrace requires it)
242 assert(status_ == stopped);
244 // See <machine/reg.h> and <machine/fp.h>
245 const int numbytesPart1 = sizeof(struct regs);
246 const int numbytesPart2 = sizeof(struct fp_status);
247 assert(numbytesPart1 % 4 == 0);
248 assert(numbytesPart2 % 4 == 0);
250 void *buffer = new char[numbytesPart1 + numbytesPart2];
253 struct regs theIntRegs;
254 int result = P_ptrace(PTRACE_GETREGS, pid, (char*)&theIntRegs, 0, 0);
255 assert(result != -1);
257 struct fp_status theFpRegs;
258 result = P_ptrace(PTRACE_GETFPREGS, pid, (char*)&theFpRegs, 0, 0);
259 assert(result != -1);
261 memcpy(buffer, &theIntRegs, sizeof(theIntRegs));
262 memcpy((char*)buffer + sizeof(theIntRegs), &theFpRegs, sizeof(theFpRegs));
267 static bool changePC(int pid, struct regs &theIntRegs, unsigned loc) {
268 assert(loc % 4 == 0);
270 theIntRegs.r_pc = loc;
271 theIntRegs.r_npc = loc+4;
273 if (0 != P_ptrace(PTRACE_SETREGS, pid, (char*)&theIntRegs, 0, 0)) {
274 cerr << "process::changePC failed" << endl;
281 bool process::executingSystemCall() {
282 // this is not implemented yet - naim 5/15/97
286 bool process::changePC(unsigned loc, const void *savedRegs) {
287 assert(status_ == stopped);
288 struct regs theIntRegs = *(const struct regs*)savedRegs; // makes a copy (on purpose)
290 return ::changePC(pid, theIntRegs, loc);
293 bool process::changePC(unsigned loc) {
294 assert(status_ == stopped);
295 struct regs theIntRegs;
296 int result = P_ptrace(PTRACE_GETREGS, pid, (char*)&theIntRegs, 0, 0);
297 assert(result != -1);
299 return ::changePC(pid, theIntRegs, loc);
303 bool process::restoreRegisters(void *buffer) {
304 // two ptrace - SETREGS calls
305 // assumes process is stopped (ptrace requires it)
306 assert(status_ == stopped);
308 struct regs theIntRegs = *(struct regs *)buffer;
309 struct fp_status theFpRegs = *(struct fp_status *)((char *)buffer + sizeof(struct regs));
311 int result = P_ptrace(PTRACE_SETREGS, pid, (char *)&theIntRegs, 0, 0);
313 perror("process::restoreRegisters PTRACE_SETREGS failed");
317 result = P_ptrace(PTRACE_SETFPREGS, pid, (char *)&theFpRegs, 0, 0);
319 perror("process::restoreRegisters PTRACE_SETFPREGS failed");
326 bool process::getActiveFrame(int *fp, int *pc)
329 if (ptraceKludge::deliverPtrace(this,PTRACE_GETREGS,(char *)®s,0,0)) {
338 // TODO: implement this
339 bool process::needToAddALeafFrame(Frame,unsigned int &){
343 bool process::readDataFromFrame(int currentFP, int *fp, int *rtn, bool uppermost)
356 func = symbols -> findFunctionIn(pc,this);
358 if (func ->isLeafFunc()) {
359 if (ptraceKludge::deliverPtrace(this,PTRACE_GETREGS,
360 (char *)®s,0,0)) {
361 *rtn = regs.r_o7 + 8;
369 // For the sparc, register %i7 is the return address - 8 and the fp is
370 // register %i6. These registers can be located in currentFP+14*5 and
371 // currentFP+14*4 respectively, but to avoid two calls to readDataSpace,
372 // we bring both together (i.e. 8 bytes of memory starting at currentFP+14*4
376 if (readDataSpace((caddr_t) (currentFP + 56),
377 sizeof(int)*2, (caddr_t) &addrs, true)) {
378 // this is the previous frame pointer
381 *rtn = addrs.rtn + 8;
383 // if pc==0, then we are in the outermost frame and we should stop. We
384 // do this by making fp=0.
386 if ( (addrs.rtn == 0) || !isValidAddress(this,(Address) addrs.rtn) ) {
397 // already setup on this FD.
398 // disconnect from controlling terminal
399 void OS::osDisconnect(void) {
400 int ttyfd = open ("/dev/tty", O_RDONLY);
401 ioctl (ttyfd, TIOCNOTTY, NULL);
405 bool process::stop_() {
406 // formerly OS::osStop()
408 /* Choose either one of the following methods for stopping a process, but not both.
409 * The choice must be consistent with that in process::continueProc_
410 * and ptraceKludge::continueProcess
413 #ifndef PTRACE_ATTACH_DETACH
414 return (P_kill(pid, SIGSTOP) != -1);
420 bool process::continueWithForwardSignal(int sig) {
421 // formerly OS::osForwardSignal
422 return (P_ptrace(PTRACE_CONT, pid, (char*)1, sig, 0) != -1);
425 void OS::osTraceMe(void) { P_ptrace(PTRACE_TRACEME, 0, 0, 0, 0); }
428 // wait for a process to terminate or stop
429 int process::waitProcs(int *status) {
430 return waitpid(0, status, WNOHANG);
433 // attach to an inferior process.
434 bool process::attach() {
435 // we only need to attach to a process that is not our direct children.
442 bool process::attach_() {
443 return (P_ptrace(PTRACE_ATTACH, getPid(), 0, 0, 0) != -1);
446 bool process::isRunning_() const {
447 // determine if a process is running by doing low-level system checks, as
448 // opposed to checking the 'status_' member vrble. May assume that attach()
449 // has run, but can't assume anything else.
451 assert(false); // not yet implemented!
455 // TODO is this safe here ?
456 bool process::continueProc_() {
462 ptraceOps++; ptraceOtherOps++;
464 /* choose either one of the following ptrace calls, but not both.
465 * The choice must be consistent with that in stop_ and
466 * ptraceKludge::continueProcess.
468 #ifndef PTRACE_ATTACH_DETACH
469 ret = P_ptrace(PTRACE_CONT, pid, (char*)1, 0, (char*)NULL);
471 ret = P_ptrace(PTRACE_DETACH, pid, (char*)1, SIGCONT, (char*)NULL);
475 perror("continueProc_()");
480 #ifdef BPATCH_LIBRARY
481 bool process::terminateProc_()
486 if (P_ptrace(PTRACE_KILL, pid, NULL, NULL, NULL) != 0)
494 bool process::pause_() {
497 ptraceOps++; ptraceOtherOps++;
498 bool wasStopped = (status() == stopped);
499 if (status() != neonatal && !wasStopped)
500 return (loopUntilStopped());
505 bool process::detach_() {
508 ptraceOps++; ptraceOtherOps++;
509 return (ptraceKludge::deliverPtrace(this, PTRACE_DETACH, (char*)1, SIGCONT, NULL));
512 #ifdef BPATCH_LIBRARY
513 bool process::API_detach_(const bool cont) {
517 ptraceOps++; ptraceOtherOps++;
518 if (!cont) P_kill(pid, SIGSTOP);
519 return (ptraceKludge::deliverPtrace(this, PTRACE_DETACH, (char*)1, SIGCONT, NULL));
523 // temporarily unimplemented, PTRACE_DUMPCORE is specific to sunos4.1
524 bool process::dumpCore_(const string coreFile) {
527 ptraceOps++; ptraceOtherOps++;
531 if (coreFile.length() > 0)
532 ret = P_ptrace(PTRACE_DUMPCORE, pid, P_strdup(coreFile.string_of()), 0, (char*) NULL);
534 ret = P_ptrace(PTRACE_DUMPCORE, pid, (char *) NULL, 0 , (char *) NULL);
539 bool process::writeTextWord_(caddr_t inTraced, int data) {
542 ptraceBytes += sizeof(int); ptraceOps++;
544 // cerr << "writeTextWord @ " << (void *)inTraced << endl; cerr.flush();
546 return (ptraceKludge::deliverPtrace(this, PTRACE_POKETEXT, inTraced, data, NULL));
549 bool process::writeTextSpace_(void *inTraced, int amount, const void *inSelf) {
552 ptraceBytes += amount; ptraceOps++;
554 // cerr << "writeTextSpace pid=" << getPid() << ", @ " << (void *)inTraced << " len=" << amount << endl; cerr.flush();
556 return (ptraceKludge::deliverPtrace(this, PTRACE_WRITETEXT, inTraced, amount, (void *)inSelf));
559 #ifdef BPATCH_SET_MUTATIONS_ACTIVE
560 bool process::readTextSpace_(void *inTraced, int amount, const void *inSelf) {
565 ptraceOps++; ptraceBytes += amount;
566 result = ptraceKludge::deliverPtrace(this, PTRACE_READTEXT,
567 (void *)inTraced, amount, inSelf);
574 bool process::writeDataSpace_(void *inTraced, int amount, const void *inSelf) {
578 ptraceOps++; ptraceBytes += amount;
580 // cerr << "process::writeDataSpace_ pid " << getPid() << " writing " << amount << " bytes at loc " << inTraced << endl;
582 bool result = ptraceKludge::deliverPtrace(this, PTRACE_WRITEDATA, inTraced, amount, (void*)inSelf);
587 bool process::readDataSpace_(const void *inTraced, int amount, void *inSelf) {
592 ptraceOps++; ptraceBytes += amount;
593 result = ptraceKludge::deliverPtrace(this, PTRACE_READDATA,
594 (void *)inTraced, amount, inSelf);
601 time64 process::getInferiorProcessCPUtime() const {
602 // kvm_getproc returns a ptr to a _copy_ of the proc structure
604 // time64 wall1 = getCurrWallTime();
605 proc *p = kvm_getproc(kvmHandle, getPid());
607 perror("could not getInferiorProcessCPUtime because kvm_getproc failed");
610 // time64 wall2 = getCurrWallTime();
611 // time64 difference = wall2-wall1;
612 // unsigned long difference_long = difference;
613 // cout << "took " << difference_long << " usecs to kvm_getproc" << endl;
615 // kvm_getu returns a copy to a _copy_ of the process' uarea
617 user *u = kvm_getu(kvmHandle, p);
619 perror("could not kvm_getu()");
623 return userAndSysTime2uSecs(u->u_ru.ru_utime,
632 if (childUareaPtr == NULL) {
633 cout << "cannot read inferior proc cpu time since unmapped...try to implement kvm_getproc instead" << endl;
637 static time64 prevResult = 0; // to check for rollback
640 time64 result = userAndSysTime2uSecs(childUareaPtr->u_ru.ru_utime,
641 childUareaPtr->u_ru.ru_stime);
642 if (result < prevResult) {
643 cout << "process::getInferiorProcessCPUtime() retrying due to rollback!" << endl;
647 cout << "sunos done getting virtual time." << endl; cout.flush();
654 user *process::tryToMapChildUarea(int childpid) {
655 // a static member fn
656 // see DYNINSTprobeUarea of rtinst/src/sunos.c
660 kvm_t *kvmfd = kvm_open(0, 0, 0, O_RDONLY, 0);
662 perror("could not map child's uarea because kvm_open failed");
666 // kvm_getproc returns a ptr to a _copy_ of the proc structure
668 time64 wall1 = getCurrWallTime();
669 proc *p = kvm_getproc(kvmfd, childpid);
671 perror("could not map child's uarea because kvm_getproc failed");
674 time64 wall2 = getCurrWallTime();
675 time64 difference = wall2-wall1;
676 unsigned long difference_long = difference;
677 cout << "took " << difference_long << " usecs to kvm_getproc" << endl;
679 // kvm_getu returns a copy to a _copy_ of the process' uarea
681 user *u = kvm_getu(kvmfd, p);
683 perror("could not map child's uarea because kvm_getu failed");
689 void *uareaPtr = p->p_uarea;
691 int kmemfd = open("/dev/kmem", O_RDONLY, 0);
693 perror("could not map child's uarea because could not open /dev/kmem for reading");
697 void *result = P_mmap(NULL, sizeof(user), PROT_READ, MAP_SHARED, kmemfd,
699 if (result == (void *)-1) {
700 perror("could not map child's uarea because could not mmap /dev/kmem");
705 cout << "mmap of child's uarea succeeded!" << endl;
707 return (user *)result;
711 //time64 process::getInferiorProcessCPUtime() const {
712 // // We get the inferior process's cpu time via a ptrace() of the u-area
713 // // of the inferior process, though it should be possible to mmap()
714 // // the inferior process's uarea into paradynd if we really want to...
716 // // UH OH: this will only work if the inferior process has been stopped!!!
718 // static time64 prevResult = 0;
721 // // assumes child process has been stopped
722 // user childUareaPtr; // just a dummy
723 // unsigned addrOffset = (void *)&childUareaPtr.u_ru.ru_utime - (void *)&childUareaPtr;
724 // unsigned numBytesNeeded = (void *)&childUareaPtr.u_ru.ru_maxrss -
725 // (void *)&childUareaPtr.u_ru.ru_utime;
726 // assert(numBytesNeeded % 4 == 0);
728 // rusage theUsage; // we'll write into the first few bytes of this structure
729 // void *ptr = &theUsage.ru_utime;
731 // cout << "peeking from uarea for pid " << this->getPid() << endl; cout.flush();
732 // while (numBytesNeeded) {
734 // unsigned result = P_ptrace(PTRACE_PEEKUSER, this->getPid(),
735 // (char*)addrOffset, 0, NULL);
737 // perror("could not getChildCPUtimeViaPtraceOfUarea: ptrace()");
741 // memcpy(ptr, &result, 4);
744 // numBytesNeeded -= 4;
747 // time64 result = userAndSysTime2uSecs(theUsage.ru_utime, theUsage.ru_stime);
748 // if (result < prevResult) {
749 // cout << "process::getInferiorProcessCPUtime() retrying due to rollback!" << endl;
753 // prevResult = result;
759 bool process::loopUntilStopped() {
760 /* make sure the process is stopped in the eyes of ptrace */
761 stop_(); // sends SIGSTOP signal to the process
765 int ret = P_waitpid(pid, &waitStatus, WUNTRACED);
766 if ((ret == -1 && errno == ECHILD) || (WIFEXITED(waitStatus))) {
767 // the child is gone.
768 handleProcessExit(this, WEXITSTATUS(waitStatus));
770 } else if (WIFSIGNALED(waitStatus)) {
771 handleProcessExit(this, WTERMSIG(waitStatus));
773 } else if (WIFSTOPPED(waitStatus)) {
774 int sig = WSTOPSIG(waitStatus);
775 if (sig == SIGSTOP) {
778 extern int handleSigChild(int, int);
779 handleSigChild(pid, waitStatus);
783 logLine("Problem stopping process\n");
790 bool process::dumpImage() {
791 const string &imageFileName = symbols->file();
792 const Address codeOff = symbols->codeOffset();
806 ifd = P_open(imageFileName.string_of(), O_RDONLY, 0);
808 string msg = string("Dump core failed: unable to open file '") + imageFileName
809 + string("': ") + string(sys_errlist[errno]);
810 showErrorCallback(47, msg);
814 rd = P_read(ifd, (void *) &my_exec, sizeof(struct exec));
815 if (rd != sizeof(struct exec)) {
816 string msg = string("Dump core failed: read failed '") + imageFileName
817 + string("': ") + string(sys_errlist[errno]);
818 showErrorCallback(47, msg);
823 rd = P_fstat(ifd, &statBuf);
825 string msg = string("Dump core failed: fstat failed: ") + string(sys_errlist[errno]);
826 showErrorCallback(47, msg);
830 length = statBuf.st_size;
831 sprintf(outFile, "%s.real", imageFileName.string_of());
832 sprintf(errorLine, "saving program to %s\n", outFile);
835 ofd = P_open(outFile, O_WRONLY|O_CREAT, 0777);
837 string msg = string("Dump core failed: unable to open file '") + string(outFile)
838 + string("': ") + string(sys_errlist[errno]);
839 showErrorCallback(47, msg);
843 //cleanUpAndExit(-1);
845 /* now copy the rest */
847 P_lseek(ofd, 0, SEEK_SET);
848 P_write(ofd, (void*) &my_exec, sizeof(struct exec));
851 // make sure it is stopped.
853 P_waitpid(pid, NULL, WUNTRACED);
856 P_lseek(ofd, N_TXTOFF(my_exec), SEEK_SET);
857 for (i=0; i < my_exec.a_text; i+= 4096) {
859 P_ptrace(PTRACE_READTEXT, pid, (char*) (codeOff + i), 4096, buffer);
861 string msg = string("Dump core failed: ptrace failed: ")
862 + string(sys_errlist[errno]);
863 showErrorCallback(47, msg);
864 P_close(ofd); P_close(ifd);
867 P_write(ofd, buffer, 4096);
870 P_ptrace(PTRACE_CONT, pid, (char*) 1, SIGCONT, 0);
872 rd = P_lseek(ofd, N_DATOFF(my_exec), SEEK_SET);
873 if (rd != N_DATOFF(my_exec)) {
874 string msg = string("Dump core failed: lseek failed: ")
875 + string(sys_errlist[errno]);
876 showErrorCallback(47, msg);
877 P_close(ofd); P_close(ifd);
880 //cleanUpAndExit(-1);
883 rd = P_lseek(ifd, N_DATOFF(my_exec), SEEK_SET);
884 if (rd != N_DATOFF(my_exec)) {
885 string msg = string("Dump core failed: lseek failed: ")
886 + string(sys_errlist[errno]);
887 showErrorCallback(47, msg);
888 P_close(ofd); P_close(ifd);
891 //cleanUpAndExit(-1);
894 total = N_DATOFF(my_exec);
895 for (i=N_DATOFF(my_exec); i < length; i += 4096) {
896 rd = P_read(ifd, buffer, 4096);
897 P_write(ofd, buffer, rd);
900 if (total != length) {
901 string msg = string("Dump core failed: tried to write ") + string(length) +
902 string(" bytes, only ") + string(total) + string("written");
903 showErrorCallback(47, msg);
904 P_close(ofd); P_close(ifd);
906 //sprintf(errorLine, "Tried to write %d bytes, only %d written\n",
908 //logLine(errorLine);
909 //showErrorCallback(57, (const char *) errorLine);
917 // TODO -- only call getrusage once per round
918 static struct rusage *get_usage_data() {
919 static bool init = false;
920 static struct rusage *mapped = NULL;
921 static struct rusage other;
929 if (!getrusage(RUSAGE_SELF, &other))
937 float OS::compute_rusage_cpu() {
938 struct rusage *ru = get_usage_data();
940 return ((float)ru->ru_utime.tv_sec + (float)ru->ru_utime.tv_usec / 1000000.0);
945 float OS::compute_rusage_sys() {
946 struct rusage *ru = get_usage_data();
948 return ((float)ru->ru_stime.tv_sec + (float)ru->ru_stime.tv_usec / 1000000.0);
953 float OS::compute_rusage_min() {
954 struct rusage *ru = get_usage_data();
956 return ((float) ru->ru_minflt);
960 float OS::compute_rusage_maj() {
961 struct rusage *ru = get_usage_data();
963 return ((float) ru->ru_majflt);
967 float OS::compute_rusage_swap() {
968 struct rusage *ru = get_usage_data();
970 return ((float) ru->ru_nswap);
974 float OS::compute_rusage_io_in() {
975 struct rusage *ru = get_usage_data();
977 return ((float) ru->ru_inblock);
981 float OS::compute_rusage_io_out() {
982 struct rusage *ru = get_usage_data();
984 return ((float) ru->ru_oublock);
988 float OS::compute_rusage_msg_send() {
989 struct rusage *ru = get_usage_data();
991 return ((float) ru->ru_msgsnd);
995 float OS::compute_rusage_msg_recv() {
996 struct rusage *ru = get_usage_data();
998 return ((float) ru->ru_msgrcv);
1002 float OS::compute_rusage_sigs() {
1003 struct rusage *ru = get_usage_data();
1005 return ((float) ru->ru_nsignals);
1009 float OS::compute_rusage_vol_cs() {
1010 struct rusage *ru = get_usage_data();
1012 return ((float) ru->ru_nvcsw);
1016 float OS::compute_rusage_inv_cs() {
1017 struct rusage *ru = get_usage_data();
1019 return ((float) ru->ru_nivcsw);
1024 int getNumberOfCPUs()
1029 string process::tryToFindExecutable(const string &progpath, int pid) {
1030 // returns empty string on failure
1032 if (progpath.length() == 0)
1035 if (exists_executable(progpath))
1038 return ""; // failure
1041 unsigned process::read_inferiorRPC_result_register(reg) {
1042 // on sparc, the result register is always in o0, so no need to use the input arg.
1044 if (!ptraceKludge::deliverPtrace(this, PTRACE_GETREGS, (char*)®s, 0, 0))
1049 bool process::set_breakpoint_for_syscall_completion() {
1050 // SUNos can't do this (as far as I know)