1 #include "proccontrol/src/int_process.h"
2 #include "proccontrol/src/irpc.h"
3 #include "proccontrol/src/procpool.h"
4 #include "proccontrol/src/int_handler.h"
5 #include "proccontrol/h/Mailbox.h"
6 #include "proccontrol/h/PCErrors.h"
7 #include "proccontrol/h/Generator.h"
8 #include "proccontrol/h/Event.h"
9 #include "proccontrol/h/Handler.h"
14 using namespace Dyninst;
17 Process::thread_mode_t threadingMode = Process::GeneratorThreading;
18 bool int_process::in_callback = false;
20 bool int_process::create()
22 ProcPool()->condvar()->lock();
24 bool result = plat_create();
26 pthrd_printf("Could not create debuggee, %s\n", executable.c_str());
27 ProcPool()->condvar()->unlock();
31 int_thread *initial_thread;
32 initial_thread = int_thread::createThread(this, NULL_THR_ID, NULL_LWP, true);
34 ProcPool()->addProcess(this);
35 setState(neonatal_intermediate);
37 ProcPool()->condvar()->broadcast();
38 ProcPool()->condvar()->unlock();
40 pthrd_printf("Created debugged %s on pid %d\n", executable.c_str(), pid);
41 result = waitfor_startup();
42 if (getState() == int_process::exited) {
43 pthrd_printf("Process %s exited during create\n", executable.c_str());
47 pthrd_printf("Error during process create for %d\n", pid);
50 result = post_create();
52 pthrd_printf("Error during post create for %d\n", pid);
56 assert(getState() == running);
60 bool int_process::waitfor_startup()
66 pthrd_printf("Waiting for startup to complete for %d\n", pid);
67 result = waitAndHandleForProc(true, this, proc_exited);
68 if (proc_exited || getState() == exited) {
69 pthrd_printf("Error. Proces exited during create/attach\n");
72 if (!result || getState() == errorstate) {
73 pthrd_printf("Error. Process %d errored during create/attach\n", pid);
76 if (getState() == running) {
77 pthrd_printf("Successfully completed create/attach on %d\n", pid);
83 bool int_process::multi_attach(std::vector<int_process *> &pids)
86 bool had_error = false;
87 std::vector<int_process *>::iterator i;
89 #define for_each_procdebug(func, err_msg) \
90 for (i = pids.begin(); i != pids.end(); i++) { \
91 int_process *pd = (*i); \
94 result = pd->func(); \
96 pthrd_printf("Could not %s to %d", err_msg, pd->pid); \
103 ProcPool()->condvar()->lock();
105 for_each_procdebug(plat_attach, "attach");
106 //MATT TODO: Add to ProcPool
108 ProcPool()->condvar()->broadcast();
109 ProcPool()->condvar()->unlock();
111 for_each_procdebug(waitfor_startup, "wait for attach");
113 for_each_procdebug(post_attach, "post attach");
118 bool int_process::attachThreads()
120 if (!needIndividualThreadAttach())
124 * This OS (linux) needs us to attach to each thread manually.
125 * Get all the thread LWPs and create new thread objects for them.
126 * After we have all the threads, check again to see if there are any
127 * new ones. We're dealing with the race condition where we could get
128 * a list of LWPs, but then new threads are created before we attach to
129 * all the existing threads.
131 bool found_new_threads;
133 found_new_threads = false;
134 vector<Dyninst::LWP> lwps;
135 bool result = getThreadLWPs(lwps);
137 perr_printf("Failed to get thread LWPs for %d\n", pid);
141 ProcPool()->condvar()->lock();
142 for (vector<Dyninst::LWP>::iterator i = lwps.begin(); i != lwps.end(); i++) {
143 int_thread *thr = threadpool->findThreadByLWP(*i);
145 pthrd_printf("Already have thread %d in process %d\n", *i, pid);
148 pthrd_printf("Creating new thread for %d/%d during attach\n", pid, *i);
149 thr = int_thread::createThread(this, NULL_THR_ID, *i, false);
150 found_new_threads = true;
152 ProcPool()->condvar()->broadcast();
153 ProcPool()->condvar()->unlock();
154 } while (found_new_threads);
158 bool int_process::attach()
160 ProcPool()->condvar()->lock();
162 pthrd_printf("Attaching to process %d\n", pid);
163 bool result = plat_attach();
165 ProcPool()->condvar()->broadcast();
166 ProcPool()->condvar()->unlock();
167 pthrd_printf("Could not attach to debuggee, %d\n", pid);
171 int_thread *initial_thread;
172 initial_thread = int_thread::createThread(this, NULL_THR_ID, NULL_LWP, true);
174 ProcPool()->addProcess(this);
176 setState(neonatal_intermediate);
178 ProcPool()->condvar()->broadcast();
179 ProcPool()->condvar()->unlock();
181 result = attachThreads();
183 pthrd_printf("Failed to attach to threads in %d\n", pid);
184 setLastError(err_internal, "Could not attach to process' threads");
188 pthrd_printf("Wait for attach from process %d\n", pid);
189 result = waitfor_startup();
191 pthrd_printf("Error waiting for attach to %d\n", pid);
192 setLastError(err_internal, "Process failed to startup");
196 result = post_attach();
198 pthrd_printf("Error on post attach for %d\n", pid);
199 setLastError(err_internal, "Process failed post-startup");
206 if (getState() == exited) {
207 setLastError(err_exited, "Process exited unexpectedly during attach\n");
210 pthrd_printf("Error during process attach for %d\n", pid);
214 bool int_process::execed()
216 ProcPool()->condvar()->lock();
218 bool should_clean = false;
219 mem->rmProc(this, should_clean);
222 mem = new mem_state(this);
224 arch = Dyninst::Arch_none;
225 exec_mem_cache.clear();
227 while (!proc_stoppers.empty()) proc_stoppers.pop();
229 int_threadPool::iterator i = threadpool->begin();
230 for (; i != threadpool->end(); i++) {
231 int_thread *thrd = *i;
232 thrd->setHandlerState(int_thread::exited);
233 thrd->setInternalState(int_thread::exited);
234 thrd->setUserState(int_thread::exited);
235 thrd->setGeneratorState(int_thread::exited);
236 ProcPool()->rmThread(thrd);
242 int_thread *initial_thread;
243 initial_thread = int_thread::createThread(this, NULL_THR_ID, NULL_LWP, true);
244 initial_thread->setGeneratorState(int_thread::stopped);
245 initial_thread->setHandlerState(int_thread::stopped);
246 initial_thread->setInternalState(int_thread::running);
247 initial_thread->setUserState(int_thread::running);
250 ProcPool()->condvar()->broadcast();
251 ProcPool()->condvar()->unlock();
253 bool result = plat_execed();
258 bool int_process::forked()
260 ProcPool()->condvar()->lock();
262 pthrd_printf("Setting up forked process %d\n", pid);
263 bool result = plat_forked();
265 pthrd_printf("Could not handle forked debuggee, %d\n", pid);
269 int_thread *initial_thread;
270 initial_thread = int_thread::createThread(this, NULL_THR_ID, NULL_LWP, true);
272 ProcPool()->addProcess(this);
274 ProcPool()->condvar()->broadcast();
275 ProcPool()->condvar()->unlock();
277 result = attachThreads();
279 pthrd_printf("Failed to attach to threads in %d\n", pid);
280 setLastError(err_internal, "Could not attach to process' threads");
284 result = post_forked();
286 pthrd_printf("Post-fork failed on %d\n", pid);
287 setLastError(err_internal, "Error handling forked process");
293 if (getState() == exited) {
294 setLastError(err_exited, "Process exited unexpectedly during attach\n");
297 pthrd_printf("Error during process attach for %d\n", pid);
301 bool int_process::post_forked()
307 bool int_process::post_attach()
309 bool result = initLibraryMechanism();
311 pthrd_printf("Error initializing library mechanism\n");
315 std::set<int_library*> added, rmd;
316 result = refresh_libraries(added, rmd);
321 bool int_process::post_create()
323 return initLibraryMechanism();
326 bool int_process::getThreadLWPs(std::vector<Dyninst::LWP> &)
331 const char *int_process::stateName(int_process::State s)
334 case neonatal: return "neonatal";
335 case neonatal_intermediate: return "neonatal_intermediate";
336 case running: return "running";
337 case exited: return "exited";
338 case errorstate: return "errorstate";
344 void int_process::setState(int_process::State s)
346 int old_state = (int) state;
347 int new_state = (int) s;
349 if (new_state < old_state) {
350 perr_printf("Regression of state %s to %s on %d\n",
351 stateName(state), stateName(s), pid);
354 pthrd_printf("Changing state of process from %s to %s on %d\n",
355 stateName(state), stateName(s), pid);
358 int_thread::State new_thr_state = int_thread::errorstate;
360 case neonatal: new_thr_state = int_thread::neonatal; break;
361 case neonatal_intermediate: new_thr_state = int_thread::neonatal_intermediate; break;
362 case running: new_thr_state = int_thread::stopped; break;
363 case exited: new_thr_state = int_thread::exited; break;
364 case errorstate: new_thr_state = int_thread::errorstate; break;
366 pthrd_printf("Setting state of all threads in %d to %s\n", pid,
367 int_thread::stateStr(new_thr_state));
368 for (int_threadPool::iterator i = threadpool->begin(); i != threadpool->end(); i++)
370 (*i)->setUserState(new_thr_state);
371 (*i)->setInternalState(new_thr_state);
372 (*i)->setHandlerState(new_thr_state);
373 (*i)->setGeneratorState(new_thr_state);
377 int_process::State int_process::getState() const
382 void int_process::setContSignal(int sig) {
386 int int_process::getContSignal() const {
390 void int_process::setPid(Dyninst::PID p)
392 pthrd_printf("Setting int_process %p to pid %d\n", this, p);
396 Dyninst::PID int_process::getPid() const
401 int_threadPool *int_process::threadPool() const
406 Process::ptr int_process::proc() const
411 struct syncRunStateRet_t {
412 bool hasRunningThread;
415 bool hasContinuePending;
418 std::vector<int_process *> readyProcStoppers;
419 syncRunStateRet_t() :
420 hasRunningThread(false),
422 hasStopPending(false),
423 hasContinuePending(false),
424 hasClearingBP(false),
425 hasProcStopRPC(false)
430 bool syncRunState(int_process *p, void *r)
432 int_threadPool *tp = p->threadPool();
433 syncRunStateRet_t *ret = (syncRunStateRet_t *) r;
436 if (p->hasQueuedProcStoppers() && p->threadPool()->allStopped()) {
437 ret->readyProcStoppers.push_back(p);
439 if (p->forceGeneratorBlock()) {
440 pthrd_printf("Process %d is forcing blocking via generator block\n", p->getPid());
441 ret->hasRunningThread = true;
444 if (dyninst_debug_proccontrol) {
445 for (int_threadPool::iterator i = tp->begin(); i != tp->end(); i++)
447 int_thread *thr = *i;
448 pthrd_printf("Pre-Thread %d/%d is in handler state %s with internal state %s (user is %s)\n",
449 p->getPid(), thr->getLWP(),
450 int_thread::stateStr(thr->getHandlerState()),
451 int_thread::stateStr(thr->getInternalState()),
452 int_thread::stateStr(thr->getUserState()));
459 bool force_leave_stopped = false;
460 for (int_threadPool::iterator i = tp->begin(); i != tp->end(); i++)
462 int_thread *thr = *i;
463 thr->handleNextPostedIRPC(false);
465 int_iRPC::ptr rpc = thr->hasRunningProcStopperRPC();
468 ret->hasProcStopRPC = true;
469 if (rpc->getState() >= int_iRPC::Prepping) {
470 pthrd_printf("Thread %d/%d has pending proc stopper RPC, leaving other threads stopped\n",
471 p->getPid(), thr->getLWP());
472 force_leave_stopped = true;
476 for (int_threadPool::iterator i = tp->begin(); i != tp->end(); i++)
478 int_thread *thr = *i;
480 if (thr->hasPendingStop()) {
481 ret->hasStopPending = true;
483 if (thr->hasPendingUserContinue()) {
484 ret->hasContinuePending = true;
486 if (thr->isClearingBreakpoint()) {
487 ret->hasClearingBP = true;
490 int_iRPC::ptr pstop_rpc = thr->hasRunningProcStopperRPC();
491 if (thr->hasPendingStop() && thr->getHandlerState() == int_thread::stopped) {
492 pthrd_printf("Continuing thread %d/%d to clear out pending stop\n",
493 thr->llproc()->getPid(), thr->getLWP());
496 else if (thr->getInternalState() == int_thread::stopped && pstop_rpc &&
497 (pstop_rpc->getState() == int_iRPC::Running ||
498 pstop_rpc->getState() == int_iRPC::Ready))
500 pthrd_printf("Continuing thread %d/%d due to pending procstop iRPC\n",
501 p->getPid(), thr->getLWP());
504 else if (pstop_rpc &&
505 thr->getInternalState() == int_thread::running &&
506 thr->getHandlerState() == int_thread::stopped &&
507 pstop_rpc == thr->runningRPC())
509 pthrd_printf("Thread %d/%d was stopped during proccstopper (maybe due to signal).",
510 p->getPid(), thr->getLWP());
513 else if (thr->getInternalState() == int_thread::running &&
514 thr->getHandlerState() == int_thread::stopped &&
515 !force_leave_stopped)
517 //The thread is stopped, but the user wants it running (we probably just finished
518 // handling a sync event). Go ahead and continue the thread.
519 pthrd_printf("Continuing thread %d/%d to match internal state after events\n",
520 p->getPid(), thr->getLWP());
524 if (thr->getInternalState() == int_thread::running ||
525 thr->getInternalState() == int_thread::neonatal_intermediate) {
526 //Keep track if any threads are running and running synchronous RPCs
527 ret->hasRunningThread = true;
528 if (thr->hasSyncRPC()) {
529 ret->hasSyncRPC = true;
533 pthrd_printf("Finished syncing runState for %d\n", p->getPid());
534 if (dyninst_debug_proccontrol) {
535 for (int_threadPool::iterator i = tp->begin(); i != tp->end(); i++)
537 int_thread *thr = *i;
538 pthrd_printf("Post-Thread %d/%d is in handler state %s with internal state %s (user is %s)\n",
539 p->getPid(), thr->getLWP(),
540 int_thread::stateStr(thr->getHandlerState()),
541 int_thread::stateStr(thr->getInternalState()),
542 int_thread::stateStr(thr->getUserState()));
549 bool doPlatContProcess(int_process *p, void * /*unused*/) {
550 bool hasPendingContinue = false;
551 int_threadPool::iterator i;
552 for(i = p->threadPool()->begin(); i != p->threadPool()->end(); ++i) {
553 if( (*i)->hasPendingContinue() ) {
554 // These continues require handler attention -- postpone until then
555 if( !(*i)->hasPendingUserContinue() ) {
556 hasPendingContinue = true;
557 pthrd_printf("Unsetting pending continue for %d/%d\n",
558 p->getPid(), (*i)->getLWP());
560 (*i)->setPendingContinue(false);
565 if( hasPendingContinue ) {
566 if( !p->plat_contProcess() ) {
567 perr_printf("Failed to continue whole process\n");
571 pthrd_printf("No pending continue found for PID %d, skipping continue\n",
578 int_process *int_process::in_waitHandleProc = NULL;
579 bool int_process::waitAndHandleForProc(bool block, int_process *proc, bool &proc_exited)
581 assert(in_waitHandleProc == NULL);
582 in_waitHandleProc = proc;
584 bool result = waitAndHandleEvents(block);
587 if (proc->getState() == int_process::exited) {
588 pthrd_printf("Deleting proc %d from waitAndHandleForProc\n", proc->getPid());
596 in_waitHandleProc = NULL;
601 bool int_process::waitAndHandleEvents(bool block)
603 bool gotEvent = false;
604 assert(!int_process::in_callback);
607 static bool recurse = false;
614 * Check status of threads
616 pthrd_printf("Updating state of each process\n");
617 syncRunStateRet_t ret;
618 ProcPool()->for_each(syncRunState, &ret);
620 if( getThreadControlMode() == HybridLWPControl ) {
621 ProcPool()->for_each(doPlatContProcess, NULL);
624 if (ret.readyProcStoppers.size()) {
625 int_process *proc = ret.readyProcStoppers[0];
626 Event::ptr ev = proc->removeProcStopper();
627 if (ev->triggersCB() &&
629 mt()->getThreadMode() == Process::HandlerThreading)
631 pthrd_printf("Handler thread sees postponed callback requiring "
632 "event '%s', not taking\n",
634 notify()->noteEvent();
638 pthrd_printf("Handling postponed proc stopper event on %d\n", proc->getPid());
639 proc->handlerpool->handleEvent(ev);
644 * Check for possible error combinations from syncRunState
646 if (!ret.hasRunningThread) {
648 //We've successfully handled an event, but no longer have any running threads
649 pthrd_printf("Returning after handling events, no threads running\n");
652 if (isHandlerThread()) {
653 //Not an error for the handler thread to get no events.
654 pthrd_printf("Returning to handler due to no running threads\n");
657 //The user called us with no running threads
658 setLastError(err_notrunning, "No running processes or threads to receive events on");
659 pthrd_printf("No running threads, returning from waitAndHandleEvents\n");
665 * The handler thread doesn't want to pick up anything that
666 * requires a callback, leaving that for the user. Peek ahead
667 * in the mailbox and abort out if we're the handler thread and
668 * the next event will require a callback.
670 if (isHandlerThread() && mt()->getThreadMode() == Process::HandlerThreading) {
671 Event::ptr ev = mbox()->peek();
672 if (ev == Event::ptr())
674 pthrd_printf("Handler thread returning due to lack of events\n");
677 if (ev->triggersCB())
679 pthrd_printf("Handler thread sees callback requiring event '%s', "
680 "not taking\n", ev->name().c_str());
681 notify()->noteEvent();
686 * Check for new events
688 bool should_block = ((block && !gotEvent) || ret.hasStopPending ||
689 ret.hasSyncRPC || ret.hasClearingBP || ret.hasProcStopRPC ||
690 ret.hasContinuePending);
691 pthrd_printf("%s for events (%d %d %d %d %d %d %d)\n",
692 should_block ? "Blocking" : "Polling",
693 (int) block, (int) gotEvent, (int) ret.hasStopPending,
694 (int) ret.hasSyncRPC, (int) ret.hasClearingBP, (int) ret.hasProcStopRPC,
695 (int) ret.hasContinuePending);
696 Event::ptr ev = mbox()->dequeue(should_block);
698 if (ev == Event::ptr())
700 if (block && gotEvent) {
701 pthrd_printf("Returning after handling events\n");
705 perr_printf("Blocking wait failed to get events\n");
706 setLastError(err_internal, "Blocking wait returned without events");
710 if (isHandlerThread()) {
711 pthrd_printf("Handler thread found nothing to do\n");
714 setLastError(err_noevents, "Poll failed to find events");
715 pthrd_printf("Poll failed to find events\n");
719 if (mt()->getThreadMode() == Process::NoThreads ||
720 mt()->getThreadMode() == Process::GeneratorThreading)
722 pthrd_printf("Clearing event from pipe after dequeue\n");
723 notify()->clearEvent();
727 ev->getProcess()->llproc()->updateSyncState(ev, false);
729 if (ev->procStopper()) {
731 * This event wants the process stopped before it gets handled.
732 * We'll start that here, and then postpone the event until it's
733 * stopped. It's up to the event to continue the process again.
735 int_process *proc = ev->getProcess()->llproc();
736 proc->threadPool()->desyncInternalState();
737 bool result = proc->threadPool()->intStop(false);
739 pthrd_printf("Failed to stop process for event.\n");
742 proc->addProcStopper(ev);
747 HandlerPool *hpool = ev->getProcess()->llproc()->handlerpool;
748 hpool->handleEvent(ev);
750 if (!ev->getProcess()->llproc())
752 //Special case event handling, the process cleaned itself
753 // under this event (likely post-exit or post-crash), but was
754 // unable to clean its handlerpool (as we were using it).
755 // Clean this for the process now.
764 bool int_process::detach(bool &should_delete)
766 should_delete = false;
767 bool had_error = true;
769 int_threadPool *tp = threadPool();
770 pthrd_printf("Detach requested on %d\n", getPid());
771 while (!tp->allStopped()) {
772 pthrd_printf("Stopping process for detach\n");
776 while (!mem->breakpoints.empty())
778 std::map<Dyninst::Address, installed_breakpoint *>::iterator i = mem->breakpoints.begin();
779 bool result = i->second->uninstall(this);
781 perr_printf("Error removing breakpoint at %lx\n", i->first);
782 setLastError(err_internal, "Error removing breakpoint before detach\n");
787 ProcPool()->condvar()->lock();
789 result = plat_detach();
791 pthrd_printf("Error performing lowlevel detach\n");
797 ProcPool()->condvar()->signal();
798 ProcPool()->condvar()->unlock();
802 should_delete = true;
806 bool int_process::terminate(bool &needs_sync)
808 pthrd_printf("Terminate requested on process %d\n", getPid());
809 bool had_error = true;
810 ProcPool()->condvar()->lock();
811 bool result = plat_terminate(needs_sync);
813 pthrd_printf("plat_terminate failed on %d\n", getPid());
816 setForceGeneratorBlock(true);
819 ProcPool()->condvar()->signal();
820 ProcPool()->condvar()->unlock();
824 int_process::int_process(Dyninst::PID p, std::string e, std::vector<std::string> a) :
829 arch(Dyninst::Arch_none),
831 up_proc(Process::ptr()),
833 hasCrashSignal(false),
836 forceGenerator(false),
841 //Put any object initialization in 'initializeProcess', below.
844 int_process::int_process(Dyninst::PID pid_, int_process *p) :
845 state(int_process::running),
847 executable(p->executable),
850 hasCrashSignal(p->hasCrashSignal),
851 crashSignal(p->crashSignal),
852 hasExitCode(p->hasExitCode),
853 forceGenerator(false),
854 exitCode(p->exitCode),
855 exec_mem_cache(exec_mem_cache),
856 continueSig(p->continueSig)
858 Process::ptr hlproc = Process::ptr(new Process());
859 mem = new mem_state(*p->mem, this);
860 initializeProcess(hlproc);
863 void int_process::initializeProcess(Process::ptr p)
867 threadpool = new int_threadPool(this);
868 handlerpool = createDefaultHandlerPool();
871 mem = new mem_state(this);
872 Generator::getDefaultGenerator(); //May create generator thread
875 int_thread *int_process::findStoppedThread()
877 ProcPool()->condvar()->lock();
878 int_thread *result = NULL;
879 for (int_threadPool::iterator i = threadpool->begin(); i != threadpool->end(); i++)
881 int_thread *thr = *i;
882 if (thr->getHandlerState() == int_thread::stopped) {
887 ProcPool()->condvar()->unlock();
889 assert(result->getGeneratorState() == int_thread::stopped);
894 bool int_process::readMem(void *local, Dyninst::Address remote, size_t size)
896 int_thread *thr = findStoppedThread();
898 setLastError(err_notstopped, "A thread must be stopped to read from memory");
899 perr_printf("Unable to find a stopped thread for read in process %d\n", getPid());
902 pthrd_printf("Reading from remote memory %lx to %p, size = %lu on %d/%d\n",
903 remote, local, (unsigned long) size, getPid(), thr->getLWP());
904 return plat_readMem(thr, local, remote, size);
907 bool int_process::writeMem(void *local, Dyninst::Address remote, size_t size)
909 int_thread *thr = findStoppedThread();
911 setLastError(err_notstopped, "A thread must be stopped to write to memory");
912 perr_printf("Unable to find a stopped thread for write in process %d\n", getPid());
915 pthrd_printf("Writing to remote memory %lx from %p, size = %lu on %d/%d\n",
916 remote, local, (unsigned long) size, getPid(), thr->getLWP());
917 return plat_writeMem(thr, local, remote, size);
920 Dyninst::Address int_process::mallocExecMemory(unsigned size)
922 Dyninst::Address max = 0;
923 std::map<Dyninst::Address, unsigned>::iterator i;
924 for (i = exec_mem_cache.begin(); i != exec_mem_cache.end(); i++) {
925 if (i->first + i->second > max)
926 max = i->first + i->second;
929 Dyninst::Address addr = plat_mallocExecMemory(max, size);
930 exec_mem_cache[addr] = size;
934 void int_process::freeExecMemory(Dyninst::Address addr)
936 std::map<Dyninst::Address, unsigned>::iterator i;
937 i = exec_mem_cache.find(addr);
938 assert(i != exec_mem_cache.end());
939 exec_mem_cache.erase(i);
942 Dyninst::Address int_process::infMalloc(unsigned long size, bool use_addr, Dyninst::Address addr)
944 pthrd_printf("Process %d is allocating memory of size %lu at 0x%lx\n", getPid(), size, addr);
945 int_iRPC::ptr rpc = rpcMgr()->createInfMallocRPC(this, size, use_addr, addr);
947 rpcMgr()->postRPCToProc(this, rpc);
949 int_thread *thr = rpc->thread();
950 bool result = thr->handleNextPostedIRPC(true);
952 pthrd_printf("Failed to handleNextPostedIRPC\n");
956 if (rpc->getState() != int_iRPC::Finished)
959 result = waitAndHandleForProc(true, this, proc_exited);
961 perr_printf("Process exited during infMalloc\n");
962 setLastError(err_exited, "Process exited during infMalloc\n");
966 pthrd_printf("Error in waitAndHandleEvents");
970 assert(rpc->getState() == int_iRPC::Finished);
972 Dyninst::Address aresult = rpc->infMallocResult();
973 pthrd_printf("Inferior malloc returning %lx\n", aresult);
974 mem->inf_malloced_memory[aresult] = size;
978 bool int_process::infFree(Dyninst::Address addr)
980 std::map<Dyninst::Address, unsigned long>::iterator i = mem->inf_malloced_memory.find(addr);
981 if (i == mem->inf_malloced_memory.end()) {
982 setLastError(err_badparam, "Unknown address passed to freeMemory");
983 perr_printf("Passed bad address, %lx, to infFree\n", addr);
986 unsigned long size = i->second;
988 int_iRPC::ptr rpc = rpcMgr()->createInfFreeRPC(this, size, addr);
990 pthrd_printf("Process %d is freeing memory of size %lu at 0x%lx with rpc %lu\n", getPid(), size, addr,
992 rpcMgr()->postRPCToProc(this, rpc);
994 int_thread *thr = rpc->thread();
995 bool result = thr->handleNextPostedIRPC(true);
997 pthrd_printf("Failed to handleNextPostedIRPC\n");
1001 if (rpc->getState() != int_iRPC::Finished)
1004 result = waitAndHandleForProc(true, this, proc_exited);
1006 perr_printf("Process exited during infFree\n");
1007 setLastError(err_exited, "Process exited during infFree\n");
1011 pthrd_printf("Error in waitAndHandleEvents");
1015 assert(rpc->getState() == int_iRPC::Finished);
1017 pthrd_printf("Inferior free returning successfully\n");
1018 mem->inf_malloced_memory.erase(i);
1022 void int_process::addProcStopper(Event::ptr ev)
1024 proc_stoppers.push(ev);
1027 bool int_process::forceGeneratorBlock() const
1029 return forceGenerator;
1032 void int_process::setForceGeneratorBlock(bool b)
1037 Event::ptr int_process::removeProcStopper()
1039 assert(proc_stoppers.size());
1040 Event::ptr ret = proc_stoppers.front();
1041 proc_stoppers.pop();
1045 bool int_process::hasQueuedProcStoppers() const
1047 return !proc_stoppers.empty();
1050 int int_process::getAddressWidth()
1052 switch (getTargetArch()) {
1065 HandlerPool *int_process::handlerPool() const
1070 bool int_process::addBreakpoint(Dyninst::Address addr, int_breakpoint *bp)
1072 if (getState() != running) {
1073 perr_printf("Attempted to add breakpoint at %lx to stopped process %d\n", addr, getPid());
1074 setLastError(err_exited, "Attempted to insert breakpoint into exited process\n");
1078 pthrd_printf("Installing new breakpoint at %lx into %d\n", addr, getPid());
1079 installed_breakpoint *ibp = NULL;
1080 map<Address, installed_breakpoint *>::iterator i = mem->breakpoints.find(addr);
1081 if (i == mem->breakpoints.end()) {
1082 pthrd_printf("Adding new breakpoint to %d\n", getPid());
1083 ibp = new installed_breakpoint(mem, addr);
1084 ibp->addBreakpoint(this, bp);
1085 bool result = ibp->install(this);
1087 pthrd_printf("Failed to install new breakpoint\n");
1093 assert(ibp && ibp->isInstalled());
1094 bool result = ibp->addBreakpoint(this, bp);
1096 pthrd_printf("Failed to install new breakpoint\n");
1103 bool int_process::rmBreakpoint(Dyninst::Address addr, int_breakpoint *bp)
1105 map<Address, installed_breakpoint *>::iterator i = mem->breakpoints.find(addr);
1106 if (i == mem->breakpoints.end()) {
1107 perr_printf("Attempted to removed breakpoint that isn't installed\n");
1110 installed_breakpoint *ibp = i->second;
1111 assert(ibp && ibp->isInstalled());
1114 bool result = ibp->rmBreakpoint(this, bp, empty);
1116 pthrd_printf("rmBreakpoint failed on breakpoint at %lx in %d\n", addr, getPid());
1126 installed_breakpoint *int_process::getBreakpoint(Dyninst::Address addr)
1128 std::map<Dyninst::Address, installed_breakpoint *>::iterator i = mem->breakpoints.find(addr);
1129 if (i == mem->breakpoints.end())
1134 int_library *int_process::getLibraryByName(std::string s) const
1136 for (set<int_library *>::iterator i = mem->libs.begin();
1137 i != mem->libs.end(); i++)
1139 if (s == (*i)->getName())
1145 size_t int_process::numLibs() const
1147 return mem->libs.size();
1150 std::string int_process::getExecutable() const
1155 bool int_process::isInCallback()
1160 mem_state::ptr int_process::memory() const
1165 void int_process::setExitCode(int c)
1167 assert(!hasCrashSignal);
1172 void int_process::setCrashSignal(int s)
1174 assert(!hasExitCode);
1175 hasCrashSignal = true;
1179 bool int_process::getExitCode(int &c)
1185 bool int_process::getCrashSignal(int &s)
1188 return hasCrashSignal;
1191 bool int_process::isInCB()
1196 void int_process::setInCB(bool b)
1198 assert(in_callback == !b);
1202 void int_process::updateSyncState(Event::ptr ev, bool gen)
1204 EventType etype = ev->getEventType();
1205 switch (ev->getSyncType()) {
1207 pthrd_printf("Event %s is asynchronous\n", etype.name().c_str());
1209 case Event::sync_thread: {
1210 int_thread *thrd = ev->getThread()->llthrd();
1214 pthrd_printf("Event %s is thread synchronous, marking thread %d stopped\n",
1215 etype.name().c_str(), thrd->getLWP());
1217 int_thread::State old_state = gen ? thrd->getGeneratorState() : thrd->getHandlerState();
1218 assert(old_state == int_thread::running ||
1219 old_state == int_thread::neonatal_intermediate);
1220 if (old_state == int_thread::errorstate)
1223 thrd->setGeneratorState(int_thread::stopped);
1225 thrd->setHandlerState(int_thread::stopped);
1229 case Event::sync_process: {
1230 pthrd_printf("Event %s is process synchronous, marking process %d stopped\n",
1231 etype.name().c_str(), getPid());
1232 int_threadPool *tp = threadPool();
1233 for (int_threadPool::iterator i = tp->begin(); i != tp->end(); i++) {
1234 int_thread *thrd = *i;
1235 int_thread::State old_state = gen ? thrd->getGeneratorState() : thrd->getHandlerState();
1236 if (old_state != int_thread::running &&
1237 old_state != int_thread::neonatal_intermediate)
1240 thrd->setGeneratorState(int_thread::stopped);
1242 thrd->setHandlerState(int_thread::stopped);
1246 case Event::unset: {
1252 int_process::~int_process()
1254 if (up_proc != Process::ptr())
1256 proc_exitstate *exitstate = new proc_exitstate();
1257 exitstate->pid = pid;
1258 exitstate->exited = hasExitCode;
1259 exitstate->exit_code = exitCode;
1260 exitstate->crashed = hasCrashSignal;
1261 exitstate->crash_signal = crashSignal;
1262 assert(!up_proc->exitstate_);
1263 up_proc->exitstate_ = exitstate;
1264 up_proc->llproc_ = NULL;
1271 //Do not delete handlerpool yet, we're currently under
1272 // an event handler. We do want to delete this if called
1275 mem->rmProc(this, should_clean);
1282 bool int_threadPool::userCont()
1287 bool int_threadPool::intCont()
1292 bool int_threadPool::cont(bool user_cont)
1294 pthrd_printf("%s continuing process %d\n", user_cont ? "User" : "Int", proc()->getPid());
1296 Dyninst::PID pid = proc()->getPid();
1297 bool had_error = false;
1298 bool cont_something = false;
1300 ProcPool()->condvar()->lock();
1302 bool needs_sync = false;
1303 for (iterator i = begin(); i != end(); i++) {
1304 int_thread *thr = *i;
1307 pthrd_printf("Continuing thread %d on process %d\n", thr->getLWP(), pid);
1308 int_thread::stopcont_ret_t ret = thr->cont(user_cont, true);
1310 case int_thread::sc_skip:
1312 case int_thread::sc_error:
1315 case int_thread::sc_success_pending:
1317 case int_thread::sc_success:
1318 cont_something = true;
1323 ProcPool()->condvar()->signal();
1324 ProcPool()->condvar()->unlock();
1326 if (!cont_something) {
1327 perr_printf("Failed to continue exited process %d\n", pid);
1328 setLastError(err_exited, "Continue attempted on exited process\n");
1332 if (user_cont && needs_sync)
1334 pthrd_printf("Attempting to handle pending user continue\n");
1336 bool result = int_process::waitAndHandleForProc(true, proc(), proc_exited);
1338 pthrd_printf("Process exited during continue\n");
1339 setLastError(err_exited, "Process exited during continue\n");
1343 perr_printf("Error waiting for events after continue on %d\n", proc()->getPid());
1351 bool int_thread::userCont()
1356 bool int_thread::intCont()
1361 bool int_thread::cont(bool user_cont)
1363 pthrd_printf("%s continuing single thread %d/%d\n", user_cont ? "User" : "Int",
1364 llproc()->getPid(), getLWP());
1366 bool result = rpcMgr()->handleThreadContinue(this, user_cont);
1368 pthrd_printf("Error handling IRPC during continue\n");
1372 if ( int_process::getThreadControlMode() == int_process::NoLWPControl ) {
1373 pthrd_printf("%s continuing entire process %d on thread operation on %d\n",
1374 user_cont ? "User" : "Int", llproc()->getPid(), getLWP());
1376 return llproc()->threadPool()->userCont();
1379 return llproc()->threadPool()->intCont();
1382 stopcont_ret_t ret = cont(user_cont, false);
1384 if (ret == sc_skip) {
1385 perr_printf("Attempted to continue exited thread\n");
1386 setLastError(err_exited, "Attempted thread continue on exited thread\n");
1389 if (ret == sc_error) {
1391 //The internal state is running, so there was an internal error during continue, but
1392 // the user state was stopped. We won't treat this as a user error and instead
1393 // just change the user state.
1394 pthrd_printf("Ignoring previous error on %d/%d\n", llproc()->getPid(), getLWP());
1397 pthrd_printf("Error continuing thread %d/%d\n", llproc()->getPid(), getLWP());
1404 if( ret == sc_success_pending ) {
1405 pthrd_printf("Attempting to handle pending user continue\n");
1407 result = int_process::waitAndHandleForProc(true, llproc(), proc_exited);
1409 pthrd_printf("Process exited during thread continue\n");
1410 setLastError(err_exited, "Process exited during continue\n");
1414 perr_printf("Error waiting for events after continue on %d\n", getLWP());
1419 bool result = setUserState(int_thread::running);
1421 setLastError(err_exited, "Attempted thread continue on exited thread\n");
1422 perr_printf("Failed to continue thread %d/%d--bad state\n", llproc()->getPid(), getLWP());
1429 int_thread::stopcont_ret_t int_thread::cont(bool user_cont, bool have_proc_lock)
1431 Dyninst::PID pid = proc()->getPid();
1433 pthrd_printf("Top level %s continue for %d/%d\n",
1434 user_cont ? "user" : "int", pid, lwp);
1436 if (getHandlerState() == errorstate) {
1437 pthrd_printf("thread %d on process %d in error state\n", getLWP(), pid);
1440 if (getHandlerState() == exited) {
1441 pthrd_printf("thread %d on process %d already exited\n", getLWP(), pid);
1446 setUserState(running);
1449 if (getHandlerState() != stopped) {
1450 perr_printf("Error. continue attempted on running thread %d/%d\n", pid, lwp);
1451 setLastError(err_notstopped, "Continue attempted on running thread\n");
1455 if (!have_proc_lock) {
1456 ProcPool()->condvar()->lock();
1459 regpool_lock.lock();
1460 cached_regpool.regs.clear();
1461 cached_regpool.full = false;
1462 regpool_lock.unlock();
1464 bool result = plat_cont(user_cont);
1465 if (result && !hasPendingUserContinue()) {
1466 setInternalState(running);
1467 setHandlerState(running);
1468 setGeneratorState(running);
1471 if (!have_proc_lock) {
1472 ProcPool()->condvar()->signal();
1473 ProcPool()->condvar()->unlock();
1477 pthrd_printf("Could not resume debugee %d, thread %d\n", pid, lwp);
1481 if( hasPendingUserContinue() ) return sc_success_pending;
1486 bool int_threadPool::userStop()
1488 return stop(true, true);
1491 bool int_threadPool::intStop(bool sync)
1493 return stop(false, sync);
1496 bool int_threadPool::stop(bool user_stop, bool sync)
1498 bool stopped_something = false;
1499 bool had_error = false;
1500 bool needs_sync = false;
1501 for (iterator i = begin(); i != end(); i++) {
1502 int_thread *thr = *i;
1504 pthrd_printf("Process %d performing %s stop on thread %d\n", proc()->getPid(),
1505 user_stop ? "user" : "int", thr->getLWP());
1506 int_thread::stopcont_ret_t ret = thr->stop(user_stop);
1508 case int_thread::sc_skip:
1509 pthrd_printf("int_thread::stop on %d/%d returned sc_skip\n",
1510 proc()->getPid(), thr->getLWP());
1512 case int_thread::sc_error:
1513 pthrd_printf("int_thread::stop on %d/%d returned sc_error\n",
1514 proc()->getPid(), thr->getLWP());
1515 if (getLastError() == err_noproc)
1516 pthrd_printf("int_thread::stop thread exit on %d/%d, skipping stop\n",
1517 proc()->getPid(), thr->getLWP());
1521 case int_thread::sc_success_pending:
1522 pthrd_printf("int_thread::stop on %d/%d return sc_success_pending\n",
1523 proc()->getPid(), thr->getLWP());
1524 stopped_something = true;
1527 case int_thread::sc_success:
1528 pthrd_printf("int_thread::stop on %d/%d returned sc_success\n",
1529 proc()->getPid(), thr->getLWP());
1530 stopped_something = true;
1536 pthrd_printf("Error while stopping threads on %d\n", proc()->getPid());
1537 setLastError(err_internal, "Could not stop process\n");
1540 if (!stopped_something) {
1541 perr_printf("No threads can be stopped on %d\n", proc()->getPid());
1542 setLastError(err_notrunning, "Attempt to stop a process that isn't running\n");
1546 if (needs_sync && sync)
1549 bool result = int_process::waitAndHandleForProc(true, proc(), proc_exited);
1551 pthrd_printf("Process exited during stop\n");
1552 setLastError(err_exited, "Process exited during stop\n");
1556 perr_printf("Error waiting for events after stop on %d\n", proc()->getPid());
1564 int_thread::stopcont_ret_t int_thread::stop(bool user_stop)
1566 Dyninst::PID pid = proc()->getPid();
1567 pthrd_printf("Top level %s thread pause for %d/%d\n",
1568 user_stop ? "user" : "int", pid, lwp);
1570 if (getHandlerState() == errorstate) {
1571 pthrd_printf("thread %d on process %d in error state\n", getLWP(), pid);
1574 if (getHandlerState() == exited) {
1575 pthrd_printf("thread %d on process %d already exited\n", getLWP(), pid);
1580 pthrd_printf("thread %d has in-progress stop on process %d\n", getLWP(), pid);
1583 if (getHandlerState() == stopped) {
1584 pthrd_printf("thread %d on process %d is already handler stopped, leaving\n",
1587 setUserState(stopped);
1588 setInternalState(stopped);
1591 if (getInternalState() == stopped) {
1592 pthrd_printf("thread %d is already stopped on process %d\n", getLWP(), pid);
1594 setUserState(stopped);
1597 if (getHandlerState() != running)
1599 perr_printf("Attempt to stop thread %d/%d in bad state %d\n",
1600 pid, lwp, getHandlerState());
1601 setLastError(err_internal, "Bad state during thread stop attempt\n");
1605 assert(!pending_stop);
1606 pending_stop = true;
1608 assert(!pending_user_stop);
1609 pending_user_stop = true;
1612 bool result = plat_stop();
1614 pthrd_printf("Could not pause debuggee %d, thr %d\n", pid, lwp);
1615 pending_stop = false;
1620 return sc_success_pending;
1625 bool int_thread::stop(bool user_stop, bool sync)
1627 if ( int_process::getThreadControlMode() == int_process::NoLWPControl ) {
1629 pthrd_printf("User stopping entire process %d on thread operation on %d\n",
1630 llproc()->getPid(), getLWP());
1631 return llproc()->threadPool()->userStop();
1634 pthrd_printf("Int stopping entire process %d on thread operation on %d\n",
1635 llproc()->getPid(), getLWP());
1636 return llproc()->threadPool()->intStop();
1640 pthrd_printf("%s stopping single thread %d/%d\n", user_stop ? "User" : "Int",
1641 llproc()->getPid(), getLWP());
1643 stopcont_ret_t ret = stop(user_stop);
1644 if (ret == sc_skip) {
1645 perr_printf("Thread %d/%d was not in a stoppable state\n",
1646 llproc()->getPid(), getLWP());
1647 setLastError(err_notrunning, "Attempt to stop a thread that isn't running\n");
1650 if (ret == sc_error) {
1651 pthrd_printf("Thread %d/%d returned error during stop\n",
1652 llproc()->getPid(), getLWP());
1655 if (ret == sc_success) {
1656 pthrd_printf("Thread %d/%d successfully stopped\n",
1657 llproc()->getPid(), getLWP());
1660 assert(ret == sc_success_pending);
1662 pthrd_printf("Thread %d/%d successfully stopped, but not sync'd\n",
1663 llproc()->getPid(), getLWP());
1668 bool result = int_process::waitAndHandleForProc(true, llproc(), proc_exited);
1670 pthrd_printf("Process exited during thread stop\n");
1671 setLastError(err_exited, "Process exited during stop\n");
1675 perr_printf("Error waiting for events after stop on %d\n", getLWP());
1681 bool int_thread::userStop()
1683 return stop(true, true);
1686 bool int_thread::intStop(bool sync)
1688 return stop(false, sync);
1691 void int_thread::setPendingUserStop(bool b)
1693 pending_user_stop = b;
1696 bool int_thread::hasPendingUserStop() const
1698 return pending_user_stop;
1701 void int_thread::setPendingStop(bool b)
1706 bool int_thread::hasPendingStop() const
1708 return pending_stop;
1711 void int_thread::setPendingUserContinue(bool b)
1713 pending_user_continue = b;
1716 bool int_thread::hasPendingUserContinue() const
1718 return pending_user_continue;
1721 void int_thread::setPendingContinue(bool b)
1723 pending_continue = b;
1726 bool int_thread::hasPendingContinue() const
1728 return pending_continue;
1731 Process::ptr int_thread::proc() const
1733 return proc_->proc();
1736 int_process *int_thread::llproc() const
1741 Dyninst::THR_ID int_thread::getTid() const
1746 Dyninst::LWP int_thread::getLWP() const
1751 int_thread::State int_thread::getHandlerState() const
1753 return handler_state;
1756 int_thread::State int_thread::getUserState() const
1761 int_thread::State int_thread::getGeneratorState() const
1763 return generator_state;
1766 int_thread::State int_thread::getInternalState() const
1768 return internal_state;
1771 const char *int_thread::stateStr(int_thread::State s)
1774 case neonatal: return "neonatal";
1775 case neonatal_intermediate: return "neonatal_intermediate";
1776 case running: return "running";
1777 case stopped: return "stopped";
1778 case exited: return "exited";
1779 case errorstate: return "errorstate";
1785 bool int_thread::setAnyState(int_thread::State *from, int_thread::State to)
1787 const char *s = NULL;
1788 if (from == &handler_state) {
1789 s = "handler state";
1791 else if (from == &user_state) {
1794 else if (from == &generator_state) {
1795 s = "generator state";
1797 else if (from == &internal_state) {
1798 s = "internal state";
1803 pthrd_printf("Leaving %s for %d in state %s\n", s, lwp, stateStr(to));
1806 if (to == errorstate) {
1807 perr_printf("Setting %s for %d from %s to errorstate\n",
1808 s, lwp, stateStr(*from));
1812 if (*from == errorstate) {
1813 perr_printf("Attempted %s reversion for %d from errorstate to %s\n",
1814 s, lwp, stateStr(to));
1817 if (*from == exited) {
1818 perr_printf("Attempted %s reversion for %d from exited to %s\n",
1819 s, lwp, stateStr(to));
1822 if (to == neonatal && *from != neonatal) {
1823 perr_printf("Attempted %s reversion for %d from %s to neonatal\n",
1824 s, lwp, stateStr(*from));
1828 pthrd_printf("Changing %s for %d/%d from %s to %s\n", s, llproc()->getPid(), lwp,
1829 stateStr(*from), stateStr(to));
1832 if (internal_state == stopped) assert(handler_state == stopped || handler_state == exited );
1833 if (handler_state == stopped) assert(generator_state == stopped || generator_state == exited);
1834 if (generator_state == running) assert(handler_state == running);
1835 if (handler_state == running) assert(internal_state == running);
1839 bool int_thread::setHandlerState(int_thread::State s)
1841 return setAnyState(&handler_state, s);
1844 bool int_thread::setUserState(int_thread::State s)
1846 return setAnyState(&user_state, s);
1849 bool int_thread::setGeneratorState(int_thread::State s)
1851 return setAnyState(&generator_state, s);
1854 bool int_thread::setInternalState(int_thread::State s)
1856 return setAnyState(&internal_state, s);
1859 void int_thread::desyncInternalState()
1861 pthrd_printf("Thread %d/%d is desyncing int from user state %d\n",
1862 llproc()->getPid(), getLWP(), num_locked_stops+1);
1866 void int_thread::restoreInternalState(bool sync)
1868 pthrd_printf("Thread %d/%d is restoring int to user state, %d\n",
1869 llproc()->getPid(), getLWP(), num_locked_stops-1);
1870 assert(num_locked_stops > 0);
1872 if (num_locked_stops > 0)
1875 pthrd_printf("Changing internal state, %s, to user state, %s.\n",
1876 int_thread::stateStr(internal_state), int_thread::stateStr(user_state));
1878 if (internal_state == user_state)
1882 else if (internal_state == int_thread::exited ||
1883 user_state == int_thread::exited)
1885 setInternalState(int_thread::exited);
1887 else if (internal_state == int_thread::stopped &&
1888 user_state == int_thread::running)
1890 bool result = intCont();
1892 perr_printf("Error continuing internal process %d/%d when resyncing\n",
1893 llproc()->getPid(), getLWP());
1897 else if (internal_state == int_thread::running &&
1898 user_state == int_thread::stopped)
1900 bool result = intStop(sync);
1902 perr_printf("Error stopping internal process %d/%d when resyncing\n",
1903 llproc()->getPid(), getLWP());
1908 setInternalState(user_state);
1912 void int_thread::setContSignal(int sig)
1917 int int_thread::getContSignal() {
1918 return continueSig_;
1921 int_thread::int_thread(int_process *p, Dyninst::THR_ID t, Dyninst::LWP l) :
1926 handler_state(neonatal),
1927 user_state(neonatal),
1928 generator_state(neonatal),
1929 internal_state(neonatal),
1931 pending_user_stop(false),
1932 pending_stop(false),
1933 pending_user_continue(false),
1934 pending_continue(false),
1935 num_locked_stops(0),
1936 user_single_step(false),
1938 clearing_breakpoint(false)
1940 Thread::ptr new_thr(new Thread());
1942 new_thr->llthread_ = this;
1943 up_thread = new_thr;
1946 int_thread::~int_thread()
1948 assert(!up_thread->exitstate_);
1950 up_thread->exitstate_ = new thread_exitstate();
1951 up_thread->exitstate_->lwp = lwp;
1952 up_thread->exitstate_->thr_id = tid;
1953 up_thread->llthread_ = NULL;
1956 int_thread *int_thread::createThread(int_process *proc,
1957 Dyninst::THR_ID thr_id,
1958 Dyninst::LWP lwp_id,
1961 int_thread *newthr = createThreadPlat(proc, thr_id, lwp_id, initial_thrd);
1962 pthrd_printf("Creating %s thread %d/%d, thr_id = %d\n",
1963 initial_thrd ? "initial" : "new",
1964 proc->getPid(), newthr->getLWP(), thr_id);
1965 proc->threadPool()->addThread(newthr);
1967 proc->threadPool()->setInitialThread(newthr);
1969 ProcPool()->addThread(proc, newthr);
1970 bool result = newthr->attach();
1972 pthrd_printf("Failed to attach to new thread %d/%d\n", proc->getPid(), lwp_id);
1975 newthr->setUserState(neonatal_intermediate);
1976 newthr->setInternalState(neonatal_intermediate);
1977 newthr->setHandlerState(neonatal_intermediate);
1978 newthr->setGeneratorState(neonatal_intermediate);
1983 Thread::ptr int_thread::thread()
1988 bool int_thread::getAllRegisters(int_registerPool &pool)
1990 bool result = false;
1992 pthrd_printf("Reading registers for thread %d\n", getLWP());
1993 regpool_lock.lock();
1994 if (cached_regpool.full) {
1995 pool = cached_regpool;
1996 pthrd_printf("Returning cached register set\n");
2000 result = plat_getAllRegisters(cached_regpool);
2002 pthrd_printf("plat_getAllRegisters returned error on %d\n", getLWP());
2005 cached_regpool.full = true;
2006 pool = cached_regpool;
2008 pthrd_printf("Successfully retrieved all registers for %d\n", getLWP());
2011 regpool_lock.unlock();
2015 bool int_thread::setAllRegisters(int_registerPool &pool)
2017 assert(getHandlerState() == int_thread::stopped);
2018 assert(getGeneratorState() == int_thread::stopped);
2019 regpool_lock.lock();
2021 pthrd_printf("Setting registers for thread %d\n", getLWP());
2022 bool result = plat_setAllRegisters(pool);
2024 pthrd_printf("plat_setAllRegisters returned error on %d\n", getLWP());
2027 cached_regpool = pool;
2028 cached_regpool.full = true;
2029 pthrd_printf("Successfully set all registers for %d\n", getLWP());
2031 regpool_lock.unlock();
2035 bool int_thread::getRegister(Dyninst::MachRegister reg, Dyninst::MachRegisterVal &val)
2037 bool result = false;
2038 pthrd_printf("Get register value for thread %d, register %s\n", lwp, reg.name());
2040 if (!llproc()->plat_individualRegAccess())
2042 pthrd_printf("Platform does not support individual register access, "
2043 "getting everything\n");
2044 int_registerPool pool;
2045 result = getAllRegisters(pool);
2047 pthrd_printf("Unable to access full register set\n");
2050 val = pool.regs[reg];
2054 regpool_lock.lock();
2056 int_registerPool::reg_map_t::iterator i = cached_regpool.regs.find(reg);
2057 if (i != cached_regpool.regs.end()) {
2059 pthrd_printf("Returning cached register value %lx for register %s on %d\n",
2060 val, reg.name(), lwp);
2065 result = plat_getRegister(reg, val);
2067 pthrd_printf("Error reading register value for %s on %d\n", reg.name(), lwp);
2071 pthrd_printf("Returning register value %lx for register %s on %d\n",
2072 val, reg.name(), lwp);
2074 regpool_lock.unlock();
2078 bool int_thread::setRegister(Dyninst::MachRegister reg, Dyninst::MachRegisterVal val)
2080 assert(getHandlerState() == int_thread::stopped);
2081 assert(getGeneratorState() == int_thread::stopped);
2083 if (!llproc()->plat_individualRegAccess())
2085 pthrd_printf("Platform does not support individual register access, "
2086 "setting everything\n");
2087 int_registerPool pool;
2088 bool result = getAllRegisters(pool);
2090 pthrd_printf("Unable to access full register set\n");
2093 pool.regs[reg] = val;
2094 result = setAllRegisters(pool);
2096 pthrd_printf("Unable to set full register set\n");
2102 regpool_lock.lock();
2104 pthrd_printf("Setting register %s for thread %d to %lx\n", reg.name(), getLWP(), val);
2105 MachRegister base_register = reg.getBaseRegister();
2106 bool result = plat_setRegister(base_register, val);
2108 pthrd_printf("Error setting register %s\n", base_register.name());
2112 cached_regpool.regs[base_register] = val;
2114 regpool_lock.unlock();
2118 void int_thread::addPostedRPC(int_iRPC::ptr rpc_)
2121 posted_rpcs.push_back(rpc_);
2124 rpc_list_t *int_thread::getPostedRPCs()
2126 return &posted_rpcs;
2129 bool int_thread::hasPostedRPCs()
2131 return (posted_rpcs.size() != 0);
2134 void int_thread::setRunningRPC(int_iRPC::ptr rpc_)
2136 assert(!running_rpc);
2140 int_iRPC::ptr int_thread::runningRPC() const
2145 void int_thread::clearRunningRPC()
2147 running_rpc = int_iRPC::ptr();
2149 bool int_thread::saveRegsForRPC()
2151 assert(!rpc_regs.full);
2152 return getAllRegisters(rpc_regs);
2155 bool int_thread::restoreRegsForRPC(bool clear)
2157 assert(rpc_regs.full);
2158 bool result = setAllRegisters(rpc_regs);
2159 if (clear && result) {
2160 rpc_regs.regs.clear();
2161 rpc_regs.full = false;
2166 bool int_thread::hasSavedRPCRegs()
2168 return rpc_regs.full;
2171 bool int_thread::runningInternalRPC() const
2173 if (runningRPC() && runningRPC()->isInternalRPC()) {
2176 if (posted_rpcs.size() && posted_rpcs.front()->isInternalRPC()) {
2182 void int_thread::incSyncRPCCount()
2187 void int_thread::decSyncRPCCount()
2189 assert(sync_rpc_count > 0);
2193 bool int_thread::hasSyncRPC()
2195 return (sync_rpc_count != 0);
2198 int_iRPC::ptr int_thread::nextPostedIRPC() const
2200 if (!posted_rpcs.size())
2201 return int_iRPC::ptr();
2202 return posted_rpcs.front();
2205 bool int_thread::handleNextPostedIRPC(bool block)
2207 int_iRPC::ptr posted_rpc = nextPostedIRPC();
2208 if (!posted_rpc || runningRPC())
2211 bool ret_result = false;
2212 pthrd_printf("Handling next postd irpc %lu on %d/%d of type %s in state %s\n",
2213 posted_rpc->id(), llproc()->getPid(), getLWP(),
2214 posted_rpc->getStrType(), posted_rpc->getStrState());
2215 assert(posted_rpc->getState() == int_iRPC::Posted ||
2216 posted_rpc->getState() == int_iRPC::Prepping ||
2217 posted_rpc->getState() == int_iRPC::Prepped);
2219 if (posted_rpc->getState() == int_iRPC::Posted) {
2221 pthrd_printf("Prepping next rpc to run on %d/%d\n", llproc()->getPid(), getLWP());
2222 bool result = rpcMgr()->prepNextRPC(this, block, error);
2223 if (!result && error) {
2224 perr_printf("Failed to prep RPC\n");
2228 if (posted_rpc->getState() == int_iRPC::Prepping) {
2229 pthrd_printf("Checking if rpc is prepped on %d/%d\n", llproc()->getPid(), getLWP());
2230 posted_rpc->isRPCPrepped();
2232 if (posted_rpc->getState() == int_iRPC::Prepped) {
2233 pthrd_printf("Running next RPC on %d/%d\n", llproc()->getPid(), getLWP());
2234 rpcMgr()->runNextRPC(this, block);
2242 int_iRPC::ptr int_thread::hasRunningProcStopperRPC() const
2244 int_iRPC::ptr running = runningRPC();
2245 if (running && running->isProcStopRPC()) {
2248 int_iRPC::ptr nextposted = nextPostedIRPC();
2249 if (!running && nextposted && nextposted->isProcStopRPC() &&
2250 nextposted->getState() != int_iRPC::Posted)
2254 return int_iRPC::ptr();
2257 bool int_thread::singleStepMode() const
2262 void int_thread::setSingleStepMode(bool s)
2267 bool int_thread::singleStepUserMode() const
2269 return user_single_step;
2272 void int_thread::setSingleStepUserMode(bool s)
2274 user_single_step = s;
2277 bool int_thread::singleStep() const
2279 return single_step || user_single_step;
2282 void int_thread::markClearingBreakpoint(installed_breakpoint *bp)
2284 assert(!clearing_breakpoint || bp == NULL);
2285 clearing_breakpoint = bp;
2288 installed_breakpoint *int_thread::isClearingBreakpoint()
2290 return clearing_breakpoint;
2293 int_thread *int_threadPool::findThreadByLWP(Dyninst::LWP lwp)
2295 std::map<Dyninst::LWP, int_thread *>::iterator i = thrds_by_lwp.find(lwp);
2296 if (i == thrds_by_lwp.end())
2301 int_thread *int_threadPool::initialThread() const
2303 return initial_thread;
2306 bool int_threadPool::allStopped()
2308 for (iterator i = begin(); i != end(); i++) {
2309 if ((*i)->getInternalState() == int_thread::running)
2315 void int_threadPool::addThread(int_thread *thrd)
2317 Dyninst::LWP lwp = thrd->getLWP();
2318 std::map<Dyninst::LWP, int_thread *>::iterator i = thrds_by_lwp.find(lwp);
2319 assert (i == thrds_by_lwp.end());
2320 thrds_by_lwp[lwp] = thrd;
2321 threads.push_back(thrd);
2322 hl_threads.push_back(thrd->thread());
2325 void int_threadPool::rmThread(int_thread *thrd)
2327 assert(thrd != initial_thread);
2328 Dyninst::LWP lwp = thrd->getLWP();
2329 std::map<Dyninst::LWP, int_thread *>::iterator i = thrds_by_lwp.find(lwp);
2330 assert (i != thrds_by_lwp.end());
2331 thrds_by_lwp.erase(i);
2333 for (unsigned i=0; i<threads.size(); i++) {
2334 if (threads[i] != thrd)
2336 threads[i] = threads[threads.size()-1];
2338 hl_threads[i] = hl_threads[hl_threads.size()-1];
2339 hl_threads.pop_back();
2343 void int_threadPool::clear()
2347 thrds_by_lwp.clear();
2348 initial_thread = NULL;
2351 void int_threadPool::desyncInternalState()
2353 for (iterator i = begin(); i != end(); i++) {
2354 (*i)->desyncInternalState();
2358 void int_threadPool::restoreInternalState(bool sync)
2360 for (iterator i = begin(); i != end(); i++) {
2361 (*i)->restoreInternalState(false);
2364 int_process::waitAndHandleEvents(false);
2367 void int_threadPool::setInitialThread(int_thread *thrd)
2369 initial_thread = thrd;
2372 int_process *int_threadPool::proc() const
2377 unsigned int_threadPool::size() const
2379 return threads.size();
2382 ThreadPool *int_threadPool::pool() const
2387 int_threadPool::int_threadPool(int_process *p) :
2390 up_pool = new ThreadPool();
2391 up_pool->threadpool = this;
2394 int_threadPool::~int_threadPool()
2399 for (vector<int_thread*>::iterator i = threads.begin(); i != threads.end(); i++)
2405 int_breakpoint::int_breakpoint(Breakpoint::ptr up) :
2408 isCtrlTransfer_(false),
2413 int_breakpoint::int_breakpoint(Dyninst::Address to_, Breakpoint::ptr up) :
2416 isCtrlTransfer_(true),
2421 int_breakpoint::~int_breakpoint()
2425 bool int_breakpoint::isCtrlTransfer() const
2427 return isCtrlTransfer_;
2430 Address int_breakpoint::toAddr() const
2435 void *int_breakpoint::getData() const
2440 void int_breakpoint::setData(void *v)
2445 Breakpoint::weak_ptr int_breakpoint::upBreakpoint() const
2450 installed_breakpoint::installed_breakpoint(mem_state::ptr memory_, Address addr_) :
2459 installed_breakpoint::installed_breakpoint(mem_state::ptr memory_,
2460 const installed_breakpoint *ip) :
2464 buffer_size(ip->buffer_size),
2465 installed(ip->installed),
2466 suspend_count(ip->suspend_count),
2469 memcpy(buffer, ip->buffer, sizeof(buffer));
2472 installed_breakpoint::~installed_breakpoint()
2476 bool installed_breakpoint::isInstalled() const
2481 bool installed_breakpoint::install(int_process *proc)
2487 bool result = plat_install(proc, true);
2489 pthrd_printf("Error installing breakpoint\n");
2492 assert(buffer && buffer_size);
2494 memory->breakpoints[addr] = this;
2498 bool installed_breakpoint::uninstall(int_process *proc)
2502 if (proc->getState() != int_process::exited)
2504 bool result = proc->writeMem(&buffer, addr, buffer_size);
2506 pthrd_printf("Failed to remove breakpoint at %lx from process %d\n",
2507 addr, proc->getPid());
2514 std::map<Dyninst::Address, installed_breakpoint *>::iterator i;
2515 i = memory->breakpoints.find(addr);
2516 if (i == memory->breakpoints.end()) {
2517 perr_printf("Failed to remove breakpoint from list\n");
2520 memory->breakpoints.erase(i);
2525 bool installed_breakpoint::suspend(int_process *proc)
2528 if (suspend_count > 1) {
2529 pthrd_printf("Breakpoint already suspended, suspend_count = %d\n",
2533 bool result = proc->writeMem(&buffer, addr, buffer_size);
2535 pthrd_printf("Failed to suspend breakpoint at %lx from process %d\n",
2536 addr, proc->getPid());
2542 bool installed_breakpoint::resume(int_process *proc)
2545 assert(suspend_count >= 0);
2546 if (suspend_count > 0) {
2547 pthrd_printf("Breakpoint remaining suspended, suspend_count = %d\n", suspend_count);
2550 bool result = plat_install(proc, false);
2552 pthrd_printf("Failed to install breakpoint at %lx in process %d\n",
2553 addr, proc->getPid());
2559 bool installed_breakpoint::addBreakpoint(int_process *proc, int_breakpoint *bp)
2561 pthrd_printf("Adding breakpoint at %lx\n", addr);
2562 if (bp->isCtrlTransfer()) {
2563 for (set<int_breakpoint *>::iterator i = bps.begin(); i != bps.end(); i++)
2565 if ((*i)->isCtrlTransfer()) {
2566 perr_printf("Error. Attempted to add to control transfer breakpoints "
2568 setLastError(err_badparam, "Attempted two control transfer breakpoints at "
2576 Breakpoint::ptr upbp = bp->upBreakpoint().lock();
2577 if (upbp != Breakpoint::ptr()) {
2578 //We keep the set of installed Breakpoints to keep the breakpoint shared
2579 // pointer reference counter from cleaning a Breakpoint while installed.
2580 hl_bps.insert(upbp);
2584 bool result = install(proc);
2586 pthrd_printf("Error failed to install breakpoint at %lx\n", addr);
2594 bool installed_breakpoint::rmBreakpoint(int_process *proc, int_breakpoint *bp, bool &empty)
2596 pthrd_printf("Removing breakpoint at %lx\n", addr);
2598 set<int_breakpoint *>::iterator i = bps.find(bp);
2599 if (i == bps.end()) {
2600 perr_printf("Attempted to remove a non-installed breakpoint\n");
2601 setLastError(err_badparam, "Breakpoint was not installed in process\n");
2607 set<Breakpoint::ptr>::iterator j = hl_bps.find(bp->upBreakpoint().lock());
2608 if (j != hl_bps.end()) {
2614 bool result = uninstall(proc);
2616 perr_printf("Failed to remove breakpoint at %lx\n", addr);
2617 setLastError(err_internal, "Could not remove breakpoint\n");
2625 Dyninst::Address installed_breakpoint::getAddr() const
2630 int_library::int_library(std::string n, Dyninst::Address load_addr) :
2632 load_address(load_addr),
2633 data_load_address(0),
2634 has_data_load(false),
2637 up_lib = new Library();
2641 int_library::int_library(std::string n, Dyninst::Address load_addr, Dyninst::Address data_load_addr) :
2643 load_address(load_addr),
2644 data_load_address(data_load_addr),
2645 has_data_load(true),
2648 up_lib = new Library();
2652 int_library::int_library(int_library *l) :
2654 load_address(l->load_address),
2655 data_load_address(l->data_load_address),
2656 has_data_load(l->has_data_load),
2659 up_lib = new Library();
2663 int_library::~int_library()
2667 std::string int_library::getName()
2672 Dyninst::Address int_library::getAddr()
2674 return load_address;
2677 Dyninst::Address int_library::getDataAddr()
2679 return data_load_address;
2682 bool int_library::hasDataAddr()
2684 return has_data_load;
2687 void int_library::setMark(bool b)
2692 bool int_library::isMarked() const
2697 Library::ptr int_library::getUpPtr() const
2702 mem_state::mem_state(int_process *proc)
2707 mem_state::mem_state(mem_state &m, int_process *p)
2709 pthrd_printf("Copying mem_state to new process %d\n", p->getPid());
2711 set<int_library *>::iterator i;
2712 for (i = m.libs.begin(); i != m.libs.end(); i++)
2714 int_library *orig_lib = *i;
2715 int_library *new_lib = new int_library(orig_lib);
2716 libs.insert(new_lib);
2718 map<Dyninst::Address, installed_breakpoint *>::iterator j;
2719 for (j = m.breakpoints.begin(); j != m.breakpoints.end(); j++)
2721 Address orig_addr = j->first;
2722 installed_breakpoint *orig_bp = j->second;
2723 installed_breakpoint *new_bp = new installed_breakpoint(this, orig_bp);
2724 breakpoints[orig_addr] = new_bp;
2726 inf_malloced_memory = m.inf_malloced_memory;
2729 mem_state::~mem_state()
2731 pthrd_printf("Destroy memory image of old process\n");
2732 set<int_library *>::iterator i;
2733 for (i = libs.begin(); i != libs.end(); i++)
2735 int_library *lib = *i;
2740 map<Dyninst::Address, installed_breakpoint *>::iterator j;
2741 for (j = breakpoints.begin(); j != breakpoints.end(); j++)
2743 installed_breakpoint *ibp = j->second;
2746 breakpoints.clear();
2749 void mem_state::addProc(int_process *p)
2751 pthrd_printf("Adding process %d as sharing a memory state with existing proc\n",
2756 void mem_state::rmProc(int_process *p, bool &should_clean)
2758 set<int_process *>::iterator i = procs.find(p);
2759 assert(i != procs.end());
2762 if (procs.empty()) {
2763 should_clean = true;
2764 pthrd_printf("Removed process %d from memory image, should clean image\n",
2768 should_clean = false;
2769 pthrd_printf("Removed process %d from memory image, others remain\n",
2775 int_notify *int_notify::the_notify = NULL;
2776 int_notify::int_notify() :
2783 up_notify = new EventNotify();
2784 up_notify->llnotify = this;
2787 int_notify *notify()
2789 if (int_notify::the_notify)
2790 return int_notify::the_notify;
2792 static Mutex init_lock;
2794 if (!int_notify::the_notify) {
2795 int_notify::the_notify = new int_notify();
2798 return int_notify::the_notify;
2801 void int_notify::noteEvent()
2803 assert(isHandlerThread());
2804 assert(events_noted == 0);
2807 pthrd_printf("noteEvent - %d\n", events_noted);
2808 set<EventNotify::notify_cb_t>::iterator i;
2809 for (i = cbs.begin(); i != cbs.end(); i++) {
2810 pthrd_printf("Calling notification CB\n");
2815 static void notifyNewEvent()
2817 notify()->noteEvent();
2821 void int_notify::clearEvent()
2823 assert(!isHandlerThread());
2825 pthrd_printf("clearEvent - %d\n", events_noted);
2826 assert(events_noted == 0);
2830 bool int_notify::hasEvents()
2832 return (events_noted > 0);
2835 void int_notify::registerCB(EventNotify::notify_cb_t cb)
2840 void int_notify::removeCB(EventNotify::notify_cb_t cb)
2842 set<EventNotify::notify_cb_t>::iterator i = cbs.find(cb);
2848 int int_notify::getPipeIn()
2863 RegisterPool::RegisterPool()
2865 llregpool = new int_registerPool();
2868 RegisterPool::RegisterPool(const RegisterPool &rp)
2870 llregpool = new int_registerPool();
2871 *llregpool = *rp.llregpool;
2874 RegisterPool::~RegisterPool()
2879 RegisterPool::iterator RegisterPool::begin()
2881 return RegisterPool::iterator(llregpool->regs.begin());
2884 RegisterPool::iterator RegisterPool::end()
2886 return RegisterPool::iterator(llregpool->regs.end());
2889 RegisterPool::iterator RegisterPool::find(MachRegister r)
2891 return RegisterPool::iterator(llregpool->regs.find(r));
2894 RegisterPool::const_iterator RegisterPool::begin() const
2896 return RegisterPool::const_iterator(llregpool->regs.begin());
2899 RegisterPool::const_iterator RegisterPool::end() const
2901 return RegisterPool::const_iterator(llregpool->regs.end());
2904 RegisterPool::const_iterator RegisterPool::find(MachRegister r) const
2906 return RegisterPool::const_iterator(llregpool->regs.find(r));
2909 MachRegisterVal& RegisterPool::operator[](MachRegister r)
2911 return llregpool->regs[r];
2914 const MachRegisterVal& RegisterPool::operator[](MachRegister r) const
2916 return llregpool->regs[r];
2919 size_t RegisterPool::size() const
2921 return llregpool->regs.size();
2924 Thread::ptr RegisterPool::getThread() const
2926 return llregpool->thread->thread();
2929 RegisterPool::iterator::iterator()
2933 RegisterPool::iterator::iterator(int_iter i_) :
2938 RegisterPool::iterator::~iterator()
2942 std::pair<Dyninst::MachRegister, Dyninst::MachRegisterVal> RegisterPool::iterator::operator*()
2947 RegisterPool::iterator RegisterPool::iterator::operator++()
2951 return RegisterPool::iterator(i);
2954 RegisterPool::iterator RegisterPool::iterator::operator++(int)
2960 RegisterPool::const_iterator::const_iterator()
2964 RegisterPool::const_iterator::const_iterator(int_iter i_) :
2969 RegisterPool::const_iterator::~const_iterator()
2973 std::pair<Dyninst::MachRegister, Dyninst::MachRegisterVal> RegisterPool::const_iterator::operator*() const
2978 RegisterPool::const_iterator RegisterPool::const_iterator::operator++()
2982 return RegisterPool::const_iterator(i);
2985 RegisterPool::const_iterator RegisterPool::const_iterator::operator++(int)
2992 int_registerPool::int_registerPool() :
2998 int_registerPool::int_registerPool(const int_registerPool &c) :
3005 int_registerPool::~int_registerPool()
3021 std::string Library::getName() const
3023 return lib->getName();
3026 Dyninst::Address Library::getLoadAddress() const
3028 return lib->getAddr();
3031 Dyninst::Address Library::getDataLoadAddress() const
3033 return lib->getDataAddr();
3036 LibraryPool::LibraryPool()
3040 LibraryPool::~LibraryPool()
3044 size_t LibraryPool::size() const
3046 return proc->numLibs();
3049 Library::ptr LibraryPool::getLibraryByName(std::string s)
3051 int_library *int_lib = proc->getLibraryByName(s);
3054 return int_lib->up_lib;
3057 const Library::ptr LibraryPool::getLibraryByName(std::string s) const
3059 int_library *int_lib = proc->getLibraryByName(s);
3062 return int_lib->up_lib;
3065 LibraryPool::iterator::iterator()
3069 LibraryPool::iterator::~iterator()
3073 Library::ptr LibraryPool::iterator::operator*() const
3075 return (*int_iter)->up_lib;
3078 LibraryPool::iterator LibraryPool::iterator::operator++()
3080 LibraryPool::iterator orig = *this;
3085 LibraryPool::iterator LibraryPool::iterator::operator++(int)
3091 LibraryPool::iterator LibraryPool::begin()
3093 LibraryPool::iterator i;
3094 i.int_iter = proc->memory()->libs.begin();
3098 LibraryPool::iterator LibraryPool::end()
3100 LibraryPool::iterator i;
3101 i.int_iter = proc->memory()->libs.end();
3105 bool LibraryPool::iterator::operator==(const LibraryPool::iterator &i) const
3107 return int_iter == i.int_iter;
3110 bool LibraryPool::iterator::operator!=(const LibraryPool::iterator &i) const
3112 return int_iter != i.int_iter;
3115 LibraryPool::const_iterator LibraryPool::begin() const
3117 LibraryPool::const_iterator i;
3118 i.int_iter = proc->memory()->libs.begin();
3122 LibraryPool::const_iterator LibraryPool::end() const
3124 LibraryPool::const_iterator i;
3125 i.int_iter = proc->memory()->libs.end();
3129 LibraryPool::const_iterator::const_iterator()
3133 LibraryPool::const_iterator::~const_iterator()
3137 const Library::const_ptr LibraryPool::const_iterator::operator*() const
3139 return (*int_iter)->up_lib;
3142 bool LibraryPool::const_iterator::operator==(const LibraryPool::const_iterator &i)
3144 return int_iter == i.int_iter;
3147 bool LibraryPool::const_iterator::operator!=(const LibraryPool::const_iterator &i)
3149 return int_iter != i.int_iter;
3152 LibraryPool::const_iterator LibraryPool::const_iterator::operator++()
3154 LibraryPool::const_iterator orig = *this;
3159 LibraryPool::const_iterator LibraryPool::const_iterator::operator++(int)
3165 bool Process::registerEventCallback(EventType evt, Process::cb_func_t cbfunc)
3167 MTLock lock_this_func(MTLock::allow_init);
3168 HandleCallbacks *cbhandler = HandleCallbacks::getCB();
3169 return cbhandler->registerCallback(evt, cbfunc);
3172 bool Process::removeEventCallback(EventType evt, cb_func_t cbfunc)
3174 MTLock lock_this_func(MTLock::allow_init);
3175 HandleCallbacks *cbhandler = HandleCallbacks::getCB();
3176 return cbhandler->removeCallback(evt, cbfunc);
3179 bool Process::removeEventCallback(EventType evt)
3181 MTLock lock_this_func(MTLock::allow_init);
3182 HandleCallbacks *cbhandler = HandleCallbacks::getCB();
3183 return cbhandler->removeCallback(evt);
3186 bool Process::removeEventCallback(cb_func_t cbfunc)
3188 MTLock lock_this_func(MTLock::allow_init);
3189 HandleCallbacks *cbhandler = HandleCallbacks::getCB();
3190 return cbhandler->removeCallback(cbfunc);
3193 bool Process::handleEvents(bool block)
3195 // One might think we should be delivering callbacks when taking this lock,
3196 // but we'll do it under waitAndHandleEvent instead.
3197 MTLock lock_this_func(MTLock::allow_init);
3199 pthrd_printf("User triggered event handling\n");
3200 if (int_process::isInCB()) {
3201 perr_printf("User attempted call on process while in CB, erroring.");
3202 setLastError(err_incallback, "Cannot handleEvents from callback\n");
3206 bool result = int_process::waitAndHandleEvents(block);
3208 pthrd_printf("Error handling events for user\n");
3214 bool Process::setThreadingMode(thread_mode_t tm)
3216 MTLock lock_this_func(MTLock::allow_init);
3217 return mt()->setThreadMode(tm);
3220 Process::ptr Process::createProcess(std::string executable, const std::vector<std::string> &argv)
3222 MTLock lock_this_func(MTLock::allow_init, MTLock::deliver_callbacks);
3224 pthrd_printf("User asked to launch executable %s\n", executable.c_str());
3225 if (int_process::isInCB()) {
3226 perr_printf("User attempted call on process create while in CB, erroring.");
3227 setLastError(err_incallback, "Cannot createProcess from callback\n");
3228 return Process::ptr();
3231 Process::ptr newproc(new Process());
3232 int_process *llproc = int_process::createProcess(executable, argv);
3233 llproc->initializeProcess(newproc);
3235 bool result = llproc->create();
3237 pthrd_printf("Unable to create process %s\n", executable.c_str());
3238 return Process::ptr();
3244 Process::ptr Process::attachProcess(Dyninst::PID pid, std::string executable)
3246 MTLock lock_this_func(MTLock::allow_init, MTLock::deliver_callbacks);
3247 pthrd_printf("User asked to attach to process %d (%s)\n", pid, executable.c_str());
3248 if (int_process::isInCB()) {
3249 perr_printf("User attempted call on process attach while in CB, erroring.\n");
3250 setLastError(err_incallback, "Cannot attachProcess from callback\n");
3251 return Process::ptr();
3253 Process::ptr newproc(new Process());
3254 int_process *llproc = int_process::createProcess(pid, executable);
3255 llproc->initializeProcess(newproc);
3257 bool result = llproc->attach();
3259 pthrd_printf("Unable to attach to process %d\n", pid);
3261 return Process::ptr();
3267 Process::Process() :
3282 Dyninst::PID Process::getPid() const
3284 MTLock lock_this_func(MTLock::allow_generator);
3287 return exitstate_->pid;
3289 return llproc_->getPid();
3292 int_process *Process::llproc() const
3297 const ThreadPool &Process::threads() const
3299 MTLock lock_this_func;
3300 static ThreadPool *err_pool;
3302 perr_printf("threads on deleted process\n");
3303 setLastError(err_exited, "Process is exited\n");
3305 err_pool = new ThreadPool();
3310 return *(llproc_->threadPool()->pool());
3313 ThreadPool &Process::threads()
3315 MTLock lock_this_func;
3316 static ThreadPool *err_pool;
3318 perr_printf("threads on deleted process\n");
3319 setLastError(err_exited, "Process is exited\n");
3321 err_pool = new ThreadPool();
3326 return *(llproc_->threadPool()->pool());
3329 const LibraryPool &Process::libraries() const
3331 MTLock lock_this_func;
3332 static LibraryPool *err_pool;
3334 perr_printf("libraries on deleted process\n");
3335 setLastError(err_exited, "Process is exited\n");
3337 err_pool = new LibraryPool();
3342 return llproc_->libpool;
3345 LibraryPool &Process::libraries()
3347 MTLock lock_this_func;
3348 static LibraryPool *err_pool;
3350 perr_printf("libraries on deleted process\n");
3351 setLastError(err_exited, "Process is exited\n");
3353 err_pool = new LibraryPool();
3358 return llproc_->libpool;
3361 bool Process::continueProc()
3363 MTLock lock_this_func(MTLock::deliver_callbacks);
3365 perr_printf("coninueProc on deleted process\n");
3366 setLastError(err_exited, "Process is exited\n");
3370 pthrd_printf("User continuing entire process %d\n", getPid());
3371 if (int_process::isInCB()) {
3372 perr_printf("User attempted call on process while in CB, erroring.");
3373 setLastError(err_incallback, "Cannot continueProc from callback\n");
3377 return llproc_->threadPool()->userCont();
3380 bool Process::isCrashed() const
3382 MTLock lock_this_func;
3385 return exitstate_->crashed;
3387 int crashSignal = 0;
3388 return (llproc_->getState() == int_process::exited && llproc_->getCrashSignal(crashSignal));
3391 bool Process::isExited() const
3393 MTLock lock_this_func;
3397 return exitstate_->exited;
3399 return (llproc_->getState() == int_process::exited && llproc_->getExitCode(exitCode));
3402 int Process::getCrashSignal() const
3404 MTLock lock_this_func;
3407 return exitstate_->crashed ? exitstate_->crash_signal : 0;
3410 int crashSignal = 0;
3411 if (!(llproc_->getState() == int_process::exited && llproc_->getCrashSignal(crashSignal)))
3416 int Process::getExitCode() const
3418 MTLock lock_this_func;
3421 return !exitstate_->crashed ? exitstate_->exit_code : 0;
3425 if (!(llproc_->getState() == int_process::exited && llproc_->getExitCode(exitCode)))
3430 bool Process::stopProc()
3432 MTLock lock_this_func(MTLock::deliver_callbacks);
3434 perr_printf("stopProc on deleted process\n");
3435 setLastError(err_exited, "Process is exited\n");
3439 pthrd_printf("User stopping entire process %d\n", getPid());
3440 if (int_process::isInCB()) {
3441 perr_printf("User attempted call on process while in CB, erroring.");
3442 setLastError(err_incallback, "Cannot continueProc from callback\n");
3446 return llproc_->threadPool()->userStop();
3449 bool Process::detach()
3451 MTLock lock_this_func(MTLock::deliver_callbacks);
3453 perr_printf("detach on deleted process\n");
3454 setLastError(err_exited, "Process is exited\n");
3458 bool result = llproc_->detach(should_delete);
3460 pthrd_printf("Failed to detach from process\n");
3463 if (should_delete) {
3464 HandlerPool *hp = llproc_->handlerPool();
3472 bool Process::terminate()
3474 MTLock lock_this_func(MTLock::deliver_callbacks);
3476 perr_printf("terminate on deleted process\n");
3477 setLastError(err_exited, "Process is exited\n");
3481 pthrd_printf("User terminating process %d\n", llproc_->getPid());
3482 bool needsSync = false;
3483 bool result = llproc_->terminate(needsSync);
3485 pthrd_printf("Terminating process %d failed\n", llproc_->getPid());
3490 bool proc_exited = false;
3491 while (!proc_exited) {
3492 bool result = int_process::waitAndHandleForProc(true, llproc(), proc_exited);
3494 perr_printf("Error waiting for process to terminate\n");
3500 HandlerPool *hp = llproc_->handlerPool();
3509 bool Process::isTerminated() const
3511 MTLock lock_this_func;
3515 return (llproc_->getState() == int_process::exited);
3518 bool Process::hasStoppedThread() const
3520 MTLock lock_this_func;
3522 perr_printf("hasStoppedThread on deleted process\n");
3523 setLastError(err_exited, "Process is exited\n");
3527 int_threadPool::iterator i;
3528 for (i = llproc_->threadPool()->begin(); i != llproc_->threadPool()->end(); i++) {
3529 if ((*i)->getUserState() == int_thread::stopped)
3535 bool Process::hasRunningThread() const
3537 MTLock lock_this_func;
3539 perr_printf("hasRunningThread on deleted process\n");
3540 setLastError(err_exited, "Process is exited\n");
3544 int_threadPool::iterator i;
3545 for (i = llproc_->threadPool()->begin(); i != llproc_->threadPool()->end(); i++) {
3546 if ((*i)->getUserState() == int_thread::running)
3552 bool Process::allThreadsStopped() const
3554 MTLock lock_this_func;
3556 perr_printf("allThreadsStopped on deleted process\n");
3557 setLastError(err_exited, "Process is exited\n");
3561 int_threadPool::iterator i;
3562 for (i = llproc_->threadPool()->begin(); i != llproc_->threadPool()->end(); i++) {
3563 if ((*i)->getUserState() == int_thread::running)
3569 bool Process::allThreadsRunning() const
3571 MTLock lock_this_func;
3573 perr_printf("allThreadsRunning on deleted process\n");
3574 setLastError(err_exited, "Process is exited\n");
3578 int_threadPool::iterator i;
3579 for (i = llproc_->threadPool()->begin(); i != llproc_->threadPool()->end(); i++) {
3580 if ((*i)->getUserState() == int_thread::stopped)
3586 Thread::ptr Process::postIRPC(IRPC::ptr irpc) const
3588 MTLock lock_this_func;
3590 perr_printf("postIRPC on deleted process\n");
3591 setLastError(err_exited, "Process is exited\n");
3592 return Thread::ptr();
3595 int_process *proc = llproc();
3596 int_iRPC::ptr rpc = irpc->llrpc()->rpc;
3597 bool result = rpcMgr()->postRPCToProc(proc, rpc);
3599 pthrd_printf("postRPCToProc failed on %d\n", proc->getPid());
3600 return Thread::ptr();
3603 if (int_process::in_callback) {
3604 pthrd_printf("Returning from postIRPC in callback\n");
3605 return rpc->thread()->thread();
3607 int_thread *thr = rpc->thread();
3608 if (thr->getInternalState() == int_thread::running) {
3609 //The thread is running, let's go ahead and start the RPC going.
3610 bool result = thr->handleNextPostedIRPC(true);
3612 pthrd_printf("handleNextPostedIRPC failed\n");
3613 return Thread::ptr();
3616 return thr->thread();
3619 bool Process::getPostedIRPCs(std::vector<IRPC::ptr> &rpcs) const
3621 MTLock lock_this_func;
3623 perr_printf("postIRPC on deleted process\n");
3624 setLastError(err_exited, "Process is exited\n");
3627 int_threadPool *tp = llproc()->threadPool();
3628 for (int_threadPool::iterator i = tp->begin(); i != tp->end(); i++)
3630 int_thread *thr = *i;
3631 rpc_list_t *rpc_list = thr->getPostedRPCs();
3632 for (rpc_list_t::iterator j = rpc_list->begin(); j != rpc_list->end(); j++) {
3633 IRPC::ptr up_rpc = (*j)->getIRPC().lock();
3634 if (up_rpc == IRPC::ptr())
3636 rpcs.push_back(up_rpc);
3642 Dyninst::Architecture Process::getArchitecture() const
3644 MTLock lock_this_func;
3646 perr_printf("getArchitecture on deleted process\n");
3647 setLastError(err_exited, "Process is exited\n");
3648 return Dyninst::Arch_none;
3650 return llproc_->getTargetArch();
3653 Dyninst::Address Process::mallocMemory(size_t size, Dyninst::Address addr)
3655 MTLock lock_this_func(MTLock::deliver_callbacks);
3657 perr_printf("mallocMemory on deleted process\n");
3658 setLastError(err_exited, "Process is exited\n");
3662 if (int_process::isInCB()) {
3663 perr_printf("User attempted call on process while in CB, erroring.");
3664 setLastError(err_incallback, "Cannot mallocMemory from callback\n");
3667 return llproc_->infMalloc(size, true, addr);
3670 Dyninst::Address Process::mallocMemory(size_t size)
3672 MTLock lock_this_func(MTLock::deliver_callbacks);
3674 perr_printf("mallocMemory on deleted process\n");
3675 setLastError(err_exited, "Process is exited\n");
3679 if (int_process::isInCB()) {
3680 perr_printf("User attempted call on process while in CB, erroring.");
3681 setLastError(err_incallback, "Cannot mallocMemory from callback\n");
3684 return llproc_->infMalloc(size, false, 0x0);
3687 bool Process::freeMemory(Dyninst::Address addr)
3689 MTLock lock_this_func(MTLock::deliver_callbacks);
3691 perr_printf("freeMemory on deleted process\n");
3692 setLastError(err_exited, "Process is exited\n");
3696 if (int_process::isInCB()) {
3697 perr_printf("User attempted call on process while in CB, erroring.");
3698 setLastError(err_incallback, "Cannot freeMemory from callback\n");
3701 return llproc_->infFree(addr);
3704 bool Process::writeMemory(Dyninst::Address addr, void *buffer, size_t size) const
3706 MTLock lock_this_func;
3708 perr_printf("writeMemory on deleted process\n");
3709 setLastError(err_exited, "Process is exited\n");
3713 pthrd_printf("User wants to write memory from 0x%lx to 0x%p of size %lu\n",
3714 addr, buffer, (unsigned long) size);
3715 return llproc_->writeMem(buffer, addr, size);
3718 bool Process::readMemory(void *buffer, Dyninst::Address addr, size_t size) const
3720 MTLock lock_this_func;
3722 perr_printf("readMemory on deleted process\n");
3723 setLastError(err_exited, "Process is exited\n");
3727 pthrd_printf("User wants to read memory from 0x%lx to 0x%p of size %lu\n",
3728 addr, buffer, (unsigned long) size);
3729 return llproc_->readMem(buffer, addr, size);
3732 bool Process::addBreakpoint(Address addr, Breakpoint::ptr bp) const
3734 MTLock lock_this_func;
3736 perr_printf("addBreakpoint on deleted process\n");
3737 setLastError(err_exited, "Process is exited\n");
3741 if (hasRunningThread()) {
3742 perr_printf("User attempted to add breakpoint to running process\n");
3743 setLastError(err_notstopped, "Attempted to insert breakpoint into running process\n");
3746 return llproc_->addBreakpoint(addr, bp->llbp());
3749 bool Process::rmBreakpoint(Dyninst::Address addr, Breakpoint::ptr bp) const
3751 MTLock lock_this_func;
3753 perr_printf("rmBreakpoint on deleted process\n");
3754 setLastError(err_exited, "Process is exited\n");
3758 if (hasRunningThread()) {
3759 perr_printf("User attempted to add breakpoint to running process\n");
3760 setLastError(err_notstopped, "Attempted to insert breakpoint into running process\n");
3763 return llproc_->rmBreakpoint(addr, bp->llbp());
3780 Process::ptr Thread::getProcess() const
3782 MTLock lock_this_func;
3785 return exitstate_->proc_ptr;
3787 return llthread_->proc();
3790 int_thread *Thread::llthrd() const
3795 bool Thread::isStopped() const
3797 MTLock lock_this_func;
3799 perr_printf("isStopped called on exited thread\n");
3800 setLastError(err_exited, "Thread is exited\n");
3803 return llthread_->getUserState() == int_thread::stopped;
3806 bool Thread::isRunning() const
3808 MTLock lock_this_func;
3810 perr_printf("isRunning called on exited thread\n");
3811 setLastError(err_exited, "Thread is exited\n");
3814 return llthread_->getUserState() == int_thread::running;
3817 bool Thread::isLive() const
3819 MTLock lock_this_func;
3823 return (llthread_->getUserState() == int_thread::stopped ||
3824 llthread_->getUserState() == int_thread::running);
3827 bool Thread::stopThread()
3829 MTLock lock_this_func(MTLock::deliver_callbacks);
3831 perr_printf("stopThread called on exited thread\n");
3832 setLastError(err_exited, "Thread is exited\n");
3836 if (int_process::isInCB()) {
3837 perr_printf("User attempted continue call on thread while in CB, erroring.");
3838 setLastError(err_incallback, "Cannot continueThread from callback\n");
3842 return llthread_->userStop();
3845 bool Thread::continueThread()
3847 MTLock lock_this_func(MTLock::deliver_callbacks);
3849 perr_printf("continueThread called on exited thread\n");
3850 setLastError(err_exited, "Thread is exited\n");
3854 if (int_process::isInCB()) {
3855 perr_printf("User attempted continue call on thread while in CB, erroring.");
3856 setLastError(err_incallback, "Cannot continueThread from callback\n");
3860 return llthread_->userCont();
3863 bool Thread::getAllRegisters(RegisterPool &pool) const
3865 MTLock lock_this_func;
3867 perr_printf("getAllRegisters called on exited thread\n");
3868 setLastError(err_exited, "Thread is exited\n");
3872 if (llthread_->getUserState() != int_thread::stopped) {
3873 setLastError(err_notstopped, "Thread must be stopped before getting registers");
3874 perr_printf("User called getAllRegisters on running thread %d\n", llthread_->getLWP());
3877 return llthread_->getAllRegisters(*pool.llregpool);
3880 bool Thread::setAllRegisters(RegisterPool &pool) const
3882 MTLock lock_this_func;
3884 perr_printf("setAllRegisters called on exited thread\n");
3885 setLastError(err_exited, "Thread is exited\n");
3888 if (llthread_->getUserState() != int_thread::stopped) {
3889 setLastError(err_notstopped, "Thread must be stopped before setting registers");
3890 perr_printf("User called setAllRegisters on running thread %d\n", llthread_->getLWP());
3893 return llthread_->setAllRegisters(*pool.llregpool);
3896 bool Thread::getRegister(Dyninst::MachRegister reg, Dyninst::MachRegisterVal &val) const
3898 MTLock lock_this_func;
3900 perr_printf("getRegister called on exited thread\n");
3901 setLastError(err_exited, "Thread is exited\n");
3904 if (llthread_->getUserState() != int_thread::stopped) {
3905 setLastError(err_notstopped, "Thread must be stopped before getting registers");
3906 perr_printf("User called getRegister on running thread %d\n", llthread_->getLWP());
3909 return llthread_->getRegister(reg, val);
3912 bool Thread::setRegister(Dyninst::MachRegister reg, Dyninst::MachRegisterVal val) const
3914 MTLock lock_this_func;
3916 perr_printf("setRegister called on exited thread\n");
3917 setLastError(err_exited, "Thread is exited\n");
3920 if (llthread_->getUserState() != int_thread::stopped) {
3921 setLastError(err_notstopped, "Thread must be stopped before setting registers");
3922 perr_printf("User called setRegister on running thread %d\n", llthread_->getLWP());
3925 return llthread_->setRegister(reg, val);
3928 bool Thread::isInitialThread() const
3930 MTLock lock_this_func;
3932 perr_printf("isInitialThrad called on exited thread\n");
3933 setLastError(err_exited, "Thread is exited\n");
3936 return llthread_->llproc()->threadPool()->initialThread() == llthread_;
3939 void Thread::setSingleStepMode(bool