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.33 1997/07/16 19:24:08 naim
45 * Minor change to my previous commit - naim
47 * Revision 1.32 1997/07/16 19:13:48 naim
48 * Fixing fork on sunos - naim
50 * Revision 1.31 1997/07/09 19:30:32 tamches
51 * isLeafFunc() --> hasNoStackFrame()
53 * Revision 1.30 1997/07/08 19:15:18 buck
54 * Added support for the x86 Solaris platform and dynamically linked
55 * executables to the dyninst API library.
57 * Revision 1.29 1997/07/01 16:54:56 tamches
58 * dummy set_breakpoint_for_syscall_completion
60 * Revision 1.28 1997/06/23 19:16:03 buck
61 * Added features to the dyninst API library, including an optional "else"
62 * in a BPatch_ifExpr; the BPatch_setMutationsActive call to temporarily
63 * disable all snippets; and the replaceFunctionCall and removeFunctionCall
64 * member functions of BPatch_thread to retarget or NOOP out a function
67 * Revision 1.1.1.3 1997/06/11 17:33:02 buck
68 * Update Maryland repository with latest changes from Wisconsin.
70 * Revision 1.27 1997/05/16 18:48:26 naim
71 * Fixing problem when inferiorRPC was launched and the application was in
72 * the middle of a system call - naim
74 * Revision 1.26 1997/04/14 00:22:26 newhall
75 * removed class pdFunction and replaced it with base class function_base and
76 * derived class pd_Function
78 * Revision 1.25 1997/03/18 19:44:25 buck
79 * first commit of dyninst library. Also includes:
80 * moving templates from paradynd to dyninstAPI
81 * converting showError into a function (in showerror.C)
82 * many ifdefs for BPATCH_LIBRARY in dyinstAPI/src.
84 * Revision 1.24 1997/02/26 23:43:03 mjrg
85 * First part on WindowsNT port: changes for compiling with Visual C++;
86 * moved unix specific code to unix.C
88 * Revision 1.23 1997/02/21 20:13:53 naim
89 * Moving files from paradynd to dyninstAPI + moving references to dataReqNode
90 * out of the ast class. The is the first pre-dyninstAPI commit! - naim
92 * Revision 1.22 1997/01/30 18:14:12 tamches
93 * skeleton isRunning, tryToFindExecutable, and read_inferiorRPC_result_register
95 * Revision 1.21 1996/11/23 22:46:41 lzheng
96 * Finished the implementation of inferiorPRC on HPUX platfrom
98 * Revision 1.20 1996/11/19 16:28:24 newhall
99 * Fix to stack walking on Solaris: find leaf functions in stack (these can occur
100 * on top of stack or in middle of stack if the signal handler is on the stack)
101 * Fix to relocated functions: new instrumentation points are kept on a per
102 * process basis. Cleaned up some of the code.
104 * Revision 1.19 1996/11/05 20:35:34 tamches
105 * changed some OS:: methods to process:: methods
107 * Revision 1.18 1996/10/31 08:55:11 tamches
108 * the shm-sampling commit; routines to do inferiorRPC; removed some warnings.
110 * Revision 1.17 1996/08/20 19:18:02 lzheng
111 * Implementation of moving multiple instructions sequence and
112 * splitting the instrumentation into two phases
114 * Revision 1.16 1996/08/16 21:19:54 tamches
115 * updated copyright for release 1.1
117 * Revision 1.15 1996/05/08 23:55:09 mjrg
118 * added support for handling fork and exec by an application
119 * use /proc instead of ptrace on solaris
122 * Revision 1.14 1996/04/03 14:27:58 naim
123 * Implementation of deallocation of instrumentation for solaris and sunos - naim
125 * Revision 1.13 1996/03/12 20:48:40 mjrg
126 * Improved handling of process termination
127 * New version of aggregateSample to support adding and removing components
129 * Added error messages
131 * Revision 1.12 1996/02/12 16:46:19 naim
132 * Updating the way we compute number_of_cpus. On solaris we will return the
133 * number of cpus; on sunos, hp, aix 1 and on the CM-5 the number of processes,
134 * which should be equal to the number of cpus - naim
136 * Revision 1.11 1996/02/09 23:53:48 naim
137 * Adding new internal metric number_of_nodes - naim
139 * Revision 1.10 1995/11/22 00:02:23 mjrg
140 * Updates for paradyndPVM on solaris
141 * Fixed problem with wrong daemon getting connection to paradyn
142 * Removed -f and -t arguments to paradyn
143 * Added cleanUpAndExit to clean up and exit from pvm before we exit paradynd
144 * Fixed bug in my previous commit
149 #include "dyninstAPI/src/process.h"
152 extern int ioctl(int, int, ...);
153 extern int getrusage(int, struct rusage*);
155 #include <sys/exec.h>
157 extern struct rusage *mapUarea();
159 #include <machine/reg.h> // for ptrace_getregs call
162 #include <sys/ioctl.h>
164 #include <machine/reg.h>
165 #include <sys/user.h> // for u-area stuff
167 #include "dyninstAPI/src/symtab.h"
168 #include "util/h/headers.h"
169 #include "dyninstAPI/src/os.h"
170 #include "dyninstAPI/src/stats.h"
171 #include "util/h/Types.h"
172 #include "paradynd/src/showerror.h"
173 #include "dyninstAPI/src/util.h" // getCurrWallTime
174 #include "util/h/pathName.h"
178 #include <sys/user.h>
181 // #include <sys/termios.h>
183 extern bool isValidAddress(process *proc, Address where);
185 extern struct rusage *mapUarea();
187 /* ********************************************************************** */
191 static bool haltProcess(process *p);
192 static void continueProcess(process *p, const bool halted);
195 static bool deliverPtrace(process *p, enum ptracereq req, void *addr,
196 int data, void *addr2);
197 // static bool deliverPtraceFast(process *p, enum ptracereq req, void *addr,
198 // int data, void *addr2);
199 // // like deliverPtrace() but assumes process is paused.
202 bool ptraceKludge::haltProcess(process *p) {
203 bool wasStopped = (p->status() == stopped);
204 if (p->status() != neonatal && !wasStopped) {
205 if (!p->loopUntilStopped()) {
206 cerr << "error in loopUntilStopped\n";
213 void ptraceKludge::continueProcess(process *p, const bool wasStopped) {
214 // First handle the cases where we shouldn't issue a PTRACE_CONT:
215 if (p->status() == neonatal) return;
216 if (wasStopped) return;
218 // Choose either one of the following methods to continue a process.
219 // The choice must be consistent with that in process::continueProc_ and stop_
221 #ifndef PTRACE_ATTACH_DETACH
222 if (P_ptrace(PTRACE_CONT, p->pid, (caddr_t) 1, SIGCONT, NULL) == -1) {
224 if (P_ptrace(PTRACE_DETACH, p->pid, (caddr_t) 1, SIGCONT, NULL) == -1) {
226 perror("error in continueProcess");
231 bool ptraceKludge::deliverPtrace(process *p, enum ptracereq req, void *addr,
232 int data, void *addr2) {
235 if (req != PTRACE_DETACH)
236 halted = haltProcess(p);
238 bool ret = (P_ptrace(req, p->getPid(), (char*)addr, data, (char*)addr2) != -1);
240 if (req != PTRACE_DETACH)
241 continueProcess(p, halted);
246 /* ********************************************************************** */
248 void *process::getRegisters() {
249 // ptrace - GETREGS call
250 // assumes the process is stopped (ptrace requires it)
251 assert(status_ == stopped);
253 // See <machine/reg.h> and <machine/fp.h>
254 const int numbytesPart1 = sizeof(struct regs);
255 const int numbytesPart2 = sizeof(struct fp_status);
256 assert(numbytesPart1 % 4 == 0);
257 assert(numbytesPart2 % 4 == 0);
259 void *buffer = new char[numbytesPart1 + numbytesPart2];
262 struct regs theIntRegs;
263 int result = P_ptrace(PTRACE_GETREGS, pid, (char*)&theIntRegs, 0, 0);
264 assert(result != -1);
266 struct fp_status theFpRegs;
267 result = P_ptrace(PTRACE_GETFPREGS, pid, (char*)&theFpRegs, 0, 0);
268 assert(result != -1);
270 memcpy(buffer, &theIntRegs, sizeof(theIntRegs));
271 memcpy((char*)buffer + sizeof(theIntRegs), &theFpRegs, sizeof(theFpRegs));
276 static bool changePC(int pid, struct regs &theIntRegs, unsigned loc) {
277 assert(loc % 4 == 0);
279 theIntRegs.r_pc = loc;
280 theIntRegs.r_npc = loc+4;
282 if (0 != P_ptrace(PTRACE_SETREGS, pid, (char*)&theIntRegs, 0, 0)) {
283 cerr << "process::changePC failed" << endl;
290 bool process::executingSystemCall() {
291 // this is not implemented yet - naim 5/15/97
295 bool process::changePC(unsigned loc, const void *savedRegs) {
296 assert(status_ == stopped);
297 struct regs theIntRegs = *(const struct regs*)savedRegs; // makes a copy (on purpose)
299 return ::changePC(pid, theIntRegs, loc);
302 bool process::changePC(unsigned loc) {
303 assert(status_ == stopped);
304 struct regs theIntRegs;
305 int result = P_ptrace(PTRACE_GETREGS, pid, (char*)&theIntRegs, 0, 0);
306 assert(result != -1);
308 return ::changePC(pid, theIntRegs, loc);
312 bool process::restoreRegisters(void *buffer) {
313 // two ptrace - SETREGS calls
314 // assumes process is stopped (ptrace requires it)
315 assert(status_ == stopped);
317 struct regs theIntRegs = *(struct regs *)buffer;
318 struct fp_status theFpRegs = *(struct fp_status *)((char *)buffer + sizeof(struct regs));
320 int result = P_ptrace(PTRACE_SETREGS, pid, (char *)&theIntRegs, 0, 0);
322 perror("process::restoreRegisters PTRACE_SETREGS failed");
326 result = P_ptrace(PTRACE_SETFPREGS, pid, (char *)&theFpRegs, 0, 0);
328 perror("process::restoreRegisters PTRACE_SETFPREGS failed");
335 bool process::getActiveFrame(int *fp, int *pc)
338 if (ptraceKludge::deliverPtrace(this,PTRACE_GETREGS,(char *)®s,0,0)) {
347 // TODO: implement this
348 bool process::needToAddALeafFrame(Frame,unsigned int &){
352 bool process::readDataFromFrame(int currentFP, int *fp, int *rtn, bool uppermost)
365 func = symbols -> findFunctionIn(pc,this);
367 if (func ->hasNoStackFrame()) {
368 if (ptraceKludge::deliverPtrace(this,PTRACE_GETREGS,
369 (char *)®s,0,0)) {
370 *rtn = regs.r_o7 + 8;
378 // For the sparc, register %i7 is the return address - 8 and the fp is
379 // register %i6. These registers can be located in currentFP+14*5 and
380 // currentFP+14*4 respectively, but to avoid two calls to readDataSpace,
381 // we bring both together (i.e. 8 bytes of memory starting at currentFP+14*4
385 if (readDataSpace((caddr_t) (currentFP + 56),
386 sizeof(int)*2, (caddr_t) &addrs, true)) {
387 // this is the previous frame pointer
390 *rtn = addrs.rtn + 8;
392 // if pc==0, then we are in the outermost frame and we should stop. We
393 // do this by making fp=0.
395 if ( (addrs.rtn == 0) || !isValidAddress(this,(Address) addrs.rtn) ) {
406 // already setup on this FD.
407 // disconnect from controlling terminal
408 void OS::osDisconnect(void) {
409 int ttyfd = open ("/dev/tty", O_RDONLY);
410 ioctl (ttyfd, TIOCNOTTY, NULL);
414 bool process::stop_() {
415 // formerly OS::osStop()
417 /* Choose either one of the following methods for stopping a process, but not both.
418 * The choice must be consistent with that in process::continueProc_
419 * and ptraceKludge::continueProcess
422 #ifndef PTRACE_ATTACH_DETACH
423 return (P_kill(pid, SIGSTOP) != -1);
429 bool process::continueWithForwardSignal(int sig) {
430 // formerly OS::osForwardSignal
431 return (P_ptrace(PTRACE_CONT, pid, (char*)1, sig, 0) != -1);
434 void OS::osTraceMe(void) { P_ptrace(PTRACE_TRACEME, 0, 0, 0, 0); }
437 // wait for a process to terminate or stop
438 int process::waitProcs(int *status) {
439 return waitpid(0, status, WNOHANG);
442 // attach to an inferior process.
443 bool process::attach() {
444 // we only need to attach to a process that is not our direct children.
451 bool process::attach_() {
452 return (P_ptrace(PTRACE_ATTACH, getPid(), 0, 0, 0) != -1);
455 bool process::isRunning_() const {
456 // determine if a process is running by doing low-level system checks, as
457 // opposed to checking the 'status_' member vrble. May assume that attach()
458 // has run, but can't assume anything else.
460 assert(false); // not yet implemented!
464 // TODO is this safe here ?
465 bool process::continueProc_() {
471 ptraceOps++; ptraceOtherOps++;
473 /* choose either one of the following ptrace calls, but not both.
474 * The choice must be consistent with that in stop_ and
475 * ptraceKludge::continueProcess.
477 #ifndef PTRACE_ATTACH_DETACH
478 if (!ptraceKludge::deliverPtrace(this, PTRACE_CONT, (char*)1, 0, NULL))
482 //ret = P_ptrace(PTRACE_CONT, pid, (char*)1, 0, (char*)NULL);
484 ret = P_ptrace(PTRACE_DETACH, pid, (char*)1, SIGCONT, (char*)NULL);
488 perror("continueProc_()");
493 #ifdef BPATCH_LIBRARY
494 bool process::terminateProc_()
499 if (P_ptrace(PTRACE_KILL, pid, NULL, NULL, NULL) != 0)
507 bool process::pause_() {
510 ptraceOps++; ptraceOtherOps++;
511 bool wasStopped = (status() == stopped);
512 if (status() != neonatal && !wasStopped)
513 return (loopUntilStopped());
518 bool process::detach_() {
521 ptraceOps++; ptraceOtherOps++;
522 return (ptraceKludge::deliverPtrace(this, PTRACE_DETACH, (char*)1, SIGCONT, NULL));
525 #ifdef BPATCH_LIBRARY
526 bool process::API_detach_(const bool cont) {
530 ptraceOps++; ptraceOtherOps++;
531 if (!cont) P_kill(pid, SIGSTOP);
532 return (ptraceKludge::deliverPtrace(this, PTRACE_DETACH, (char*)1, SIGCONT, NULL));
536 // temporarily unimplemented, PTRACE_DUMPCORE is specific to sunos4.1
537 bool process::dumpCore_(const string coreFile) {
540 ptraceOps++; ptraceOtherOps++;
544 if (coreFile.length() > 0)
545 ret = P_ptrace(PTRACE_DUMPCORE, pid, P_strdup(coreFile.string_of()), 0, (char*) NULL);
547 ret = P_ptrace(PTRACE_DUMPCORE, pid, (char *) NULL, 0 , (char *) NULL);
552 bool process::writeTextWord_(caddr_t inTraced, int data) {
555 ptraceBytes += sizeof(int); ptraceOps++;
557 // cerr << "writeTextWord @ " << (void *)inTraced << endl; cerr.flush();
559 return (ptraceKludge::deliverPtrace(this, PTRACE_POKETEXT, inTraced, data, NULL));
562 bool process::writeTextSpace_(void *inTraced, int amount, const void *inSelf) {
565 ptraceBytes += amount; ptraceOps++;
567 // cerr << "writeTextSpace pid=" << getPid() << ", @ " << (void *)inTraced << " len=" << amount << endl; cerr.flush();
569 return (ptraceKludge::deliverPtrace(this, PTRACE_WRITETEXT, inTraced, amount, (void *)inSelf));
572 #ifdef BPATCH_SET_MUTATIONS_ACTIVE
573 bool process::readTextSpace_(void *inTraced, int amount, const void *inSelf) {
578 ptraceOps++; ptraceBytes += amount;
579 result = ptraceKludge::deliverPtrace(this, PTRACE_READTEXT,
580 (void *)inTraced, amount, inSelf);
587 bool process::writeDataSpace_(void *inTraced, int amount, const void *inSelf) {
591 ptraceOps++; ptraceBytes += amount;
593 // cerr << "process::writeDataSpace_ pid " << getPid() << " writing " << amount << " bytes at loc " << inTraced << endl;
595 bool result = ptraceKludge::deliverPtrace(this, PTRACE_WRITEDATA, inTraced, amount, (void*)inSelf);
600 bool process::readDataSpace_(const void *inTraced, int amount, void *inSelf) {
605 ptraceOps++; ptraceBytes += amount;
606 result = ptraceKludge::deliverPtrace(this, PTRACE_READDATA,
607 (void *)inTraced, amount, inSelf);
614 time64 process::getInferiorProcessCPUtime() const {
615 // kvm_getproc returns a ptr to a _copy_ of the proc structure
617 // time64 wall1 = getCurrWallTime();
618 proc *p = kvm_getproc(kvmHandle, getPid());
620 perror("could not getInferiorProcessCPUtime because kvm_getproc failed");
623 // time64 wall2 = getCurrWallTime();
624 // time64 difference = wall2-wall1;
625 // unsigned long difference_long = difference;
626 // cout << "took " << difference_long << " usecs to kvm_getproc" << endl;
628 // kvm_getu returns a copy to a _copy_ of the process' uarea
630 user *u = kvm_getu(kvmHandle, p);
632 perror("could not kvm_getu()");
636 return userAndSysTime2uSecs(u->u_ru.ru_utime,
645 if (childUareaPtr == NULL) {
646 cout << "cannot read inferior proc cpu time since unmapped...try to implement kvm_getproc instead" << endl;
650 static time64 prevResult = 0; // to check for rollback
653 time64 result = userAndSysTime2uSecs(childUareaPtr->u_ru.ru_utime,
654 childUareaPtr->u_ru.ru_stime);
655 if (result < prevResult) {
656 cout << "process::getInferiorProcessCPUtime() retrying due to rollback!" << endl;
660 cout << "sunos done getting virtual time." << endl; cout.flush();
667 user *process::tryToMapChildUarea(int childpid) {
668 // a static member fn
669 // see DYNINSTprobeUarea of rtinst/src/sunos.c
673 kvm_t *kvmfd = kvm_open(0, 0, 0, O_RDONLY, 0);
675 perror("could not map child's uarea because kvm_open failed");
679 // kvm_getproc returns a ptr to a _copy_ of the proc structure
681 time64 wall1 = getCurrWallTime();
682 proc *p = kvm_getproc(kvmfd, childpid);
684 perror("could not map child's uarea because kvm_getproc failed");
687 time64 wall2 = getCurrWallTime();
688 time64 difference = wall2-wall1;
689 unsigned long difference_long = difference;
690 cout << "took " << difference_long << " usecs to kvm_getproc" << endl;
692 // kvm_getu returns a copy to a _copy_ of the process' uarea
694 user *u = kvm_getu(kvmfd, p);
696 perror("could not map child's uarea because kvm_getu failed");
702 void *uareaPtr = p->p_uarea;
704 int kmemfd = open("/dev/kmem", O_RDONLY, 0);
706 perror("could not map child's uarea because could not open /dev/kmem for reading");
710 void *result = P_mmap(NULL, sizeof(user), PROT_READ, MAP_SHARED, kmemfd,
712 if (result == (void *)-1) {
713 perror("could not map child's uarea because could not mmap /dev/kmem");
718 cout << "mmap of child's uarea succeeded!" << endl;
720 return (user *)result;
724 //time64 process::getInferiorProcessCPUtime() const {
725 // // We get the inferior process's cpu time via a ptrace() of the u-area
726 // // of the inferior process, though it should be possible to mmap()
727 // // the inferior process's uarea into paradynd if we really want to...
729 // // UH OH: this will only work if the inferior process has been stopped!!!
731 // static time64 prevResult = 0;
734 // // assumes child process has been stopped
735 // user childUareaPtr; // just a dummy
736 // unsigned addrOffset = (void *)&childUareaPtr.u_ru.ru_utime - (void *)&childUareaPtr;
737 // unsigned numBytesNeeded = (void *)&childUareaPtr.u_ru.ru_maxrss -
738 // (void *)&childUareaPtr.u_ru.ru_utime;
739 // assert(numBytesNeeded % 4 == 0);
741 // rusage theUsage; // we'll write into the first few bytes of this structure
742 // void *ptr = &theUsage.ru_utime;
744 // cout << "peeking from uarea for pid " << this->getPid() << endl; cout.flush();
745 // while (numBytesNeeded) {
747 // unsigned result = P_ptrace(PTRACE_PEEKUSER, this->getPid(),
748 // (char*)addrOffset, 0, NULL);
750 // perror("could not getChildCPUtimeViaPtraceOfUarea: ptrace()");
754 // memcpy(ptr, &result, 4);
757 // numBytesNeeded -= 4;
760 // time64 result = userAndSysTime2uSecs(theUsage.ru_utime, theUsage.ru_stime);
761 // if (result < prevResult) {
762 // cout << "process::getInferiorProcessCPUtime() retrying due to rollback!" << endl;
766 // prevResult = result;
772 bool process::loopUntilStopped() {
773 /* make sure the process is stopped in the eyes of ptrace */
774 stop_(); // sends SIGSTOP signal to the process
778 int ret = P_waitpid(pid, &waitStatus, WUNTRACED);
779 if ((ret == -1 && errno == ECHILD) || (WIFEXITED(waitStatus))) {
780 // the child is gone.
781 handleProcessExit(this, WEXITSTATUS(waitStatus));
783 } else if (WIFSIGNALED(waitStatus)) {
784 handleProcessExit(this, WTERMSIG(waitStatus));
786 } else if (WIFSTOPPED(waitStatus)) {
787 int sig = WSTOPSIG(waitStatus);
788 if (sig == SIGSTOP) {
791 extern int handleSigChild(int, int);
792 handleSigChild(pid, waitStatus);
796 logLine("Problem stopping process\n");
803 bool process::dumpImage() {
804 const string &imageFileName = symbols->file();
805 const Address codeOff = symbols->codeOffset();
819 ifd = P_open(imageFileName.string_of(), O_RDONLY, 0);
821 string msg = string("Dump core failed: unable to open file '") + imageFileName
822 + string("': ") + string(sys_errlist[errno]);
823 showErrorCallback(47, msg);
827 rd = P_read(ifd, (void *) &my_exec, sizeof(struct exec));
828 if (rd != sizeof(struct exec)) {
829 string msg = string("Dump core failed: read failed '") + imageFileName
830 + string("': ") + string(sys_errlist[errno]);
831 showErrorCallback(47, msg);
836 rd = P_fstat(ifd, &statBuf);
838 string msg = string("Dump core failed: fstat failed: ") + string(sys_errlist[errno]);
839 showErrorCallback(47, msg);
843 length = statBuf.st_size;
844 sprintf(outFile, "%s.real", imageFileName.string_of());
845 sprintf(errorLine, "saving program to %s\n", outFile);
848 ofd = P_open(outFile, O_WRONLY|O_CREAT, 0777);
850 string msg = string("Dump core failed: unable to open file '") + string(outFile)
851 + string("': ") + string(sys_errlist[errno]);
852 showErrorCallback(47, msg);
856 //cleanUpAndExit(-1);
858 /* now copy the rest */
860 P_lseek(ofd, 0, SEEK_SET);
861 P_write(ofd, (void*) &my_exec, sizeof(struct exec));
864 // make sure it is stopped.
866 P_waitpid(pid, NULL, WUNTRACED);
869 P_lseek(ofd, N_TXTOFF(my_exec), SEEK_SET);
870 for (i=0; i < my_exec.a_text; i+= 4096) {
872 P_ptrace(PTRACE_READTEXT, pid, (char*) (codeOff + i), 4096, buffer);
874 string msg = string("Dump core failed: ptrace failed: ")
875 + string(sys_errlist[errno]);
876 showErrorCallback(47, msg);
877 P_close(ofd); P_close(ifd);
880 P_write(ofd, buffer, 4096);
883 P_ptrace(PTRACE_CONT, pid, (char*) 1, SIGCONT, 0);
885 rd = P_lseek(ofd, N_DATOFF(my_exec), SEEK_SET);
886 if (rd != N_DATOFF(my_exec)) {
887 string msg = string("Dump core failed: lseek failed: ")
888 + string(sys_errlist[errno]);
889 showErrorCallback(47, msg);
890 P_close(ofd); P_close(ifd);
893 //cleanUpAndExit(-1);
896 rd = P_lseek(ifd, N_DATOFF(my_exec), SEEK_SET);
897 if (rd != N_DATOFF(my_exec)) {
898 string msg = string("Dump core failed: lseek failed: ")
899 + string(sys_errlist[errno]);
900 showErrorCallback(47, msg);
901 P_close(ofd); P_close(ifd);
904 //cleanUpAndExit(-1);
907 total = N_DATOFF(my_exec);
908 for (i=N_DATOFF(my_exec); i < length; i += 4096) {
909 rd = P_read(ifd, buffer, 4096);
910 P_write(ofd, buffer, rd);
913 if (total != length) {
914 string msg = string("Dump core failed: tried to write ") + string(length) +
915 string(" bytes, only ") + string(total) + string("written");
916 showErrorCallback(47, msg);
917 P_close(ofd); P_close(ifd);
919 //sprintf(errorLine, "Tried to write %d bytes, only %d written\n",
921 //logLine(errorLine);
922 //showErrorCallback(57, (const char *) errorLine);
930 // TODO -- only call getrusage once per round
931 static struct rusage *get_usage_data() {
932 static bool init = false;
933 static struct rusage *mapped = NULL;
934 static struct rusage other;
942 if (!getrusage(RUSAGE_SELF, &other))
950 float OS::compute_rusage_cpu() {
951 struct rusage *ru = get_usage_data();
953 return ((float)ru->ru_utime.tv_sec + (float)ru->ru_utime.tv_usec / 1000000.0);
958 float OS::compute_rusage_sys() {
959 struct rusage *ru = get_usage_data();
961 return ((float)ru->ru_stime.tv_sec + (float)ru->ru_stime.tv_usec / 1000000.0);
966 float OS::compute_rusage_min() {
967 struct rusage *ru = get_usage_data();
969 return ((float) ru->ru_minflt);
973 float OS::compute_rusage_maj() {
974 struct rusage *ru = get_usage_data();
976 return ((float) ru->ru_majflt);
980 float OS::compute_rusage_swap() {
981 struct rusage *ru = get_usage_data();
983 return ((float) ru->ru_nswap);
987 float OS::compute_rusage_io_in() {
988 struct rusage *ru = get_usage_data();
990 return ((float) ru->ru_inblock);
994 float OS::compute_rusage_io_out() {
995 struct rusage *ru = get_usage_data();
997 return ((float) ru->ru_oublock);
1001 float OS::compute_rusage_msg_send() {
1002 struct rusage *ru = get_usage_data();
1004 return ((float) ru->ru_msgsnd);
1008 float OS::compute_rusage_msg_recv() {
1009 struct rusage *ru = get_usage_data();
1011 return ((float) ru->ru_msgrcv);
1015 float OS::compute_rusage_sigs() {
1016 struct rusage *ru = get_usage_data();
1018 return ((float) ru->ru_nsignals);
1022 float OS::compute_rusage_vol_cs() {
1023 struct rusage *ru = get_usage_data();
1025 return ((float) ru->ru_nvcsw);
1029 float OS::compute_rusage_inv_cs() {
1030 struct rusage *ru = get_usage_data();
1032 return ((float) ru->ru_nivcsw);
1037 int getNumberOfCPUs()
1042 string process::tryToFindExecutable(const string &progpath, int pid) {
1043 // returns empty string on failure
1045 if (progpath.length() == 0)
1048 if (exists_executable(progpath))
1051 return ""; // failure
1054 unsigned process::read_inferiorRPC_result_register(reg) {
1055 // on sparc, the result register is always in o0, so no need to use the input arg.
1057 if (!ptraceKludge::deliverPtrace(this, PTRACE_GETREGS, (char*)®s, 0, 0))
1062 bool process::set_breakpoint_for_syscall_completion() {
1063 // SUNos can't do this (as far as I know)