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::setPid(Dyninst::PID p)
384 pthrd_printf("Setting int_process %p to pid %d\n", this, p);
388 Dyninst::PID int_process::getPid() const
393 int_threadPool *int_process::threadPool() const
398 Process::ptr int_process::proc() const
403 struct syncRunStateRet_t {
404 bool hasRunningThread;
409 std::vector<int_process *> readyProcStoppers;
410 syncRunStateRet_t() :
411 hasRunningThread(false),
413 hasStopPending(false),
414 hasClearingBP(false),
415 hasProcStopRPC(false)
420 bool syncRunState(int_process *p, void *r)
422 int_threadPool *tp = p->threadPool();
423 syncRunStateRet_t *ret = (syncRunStateRet_t *) r;
426 if (p->hasQueuedProcStoppers() && p->threadPool()->allStopped()) {
427 ret->readyProcStoppers.push_back(p);
429 if (p->forceGeneratorBlock()) {
430 pthrd_printf("Process %d is forcing blocking via generator block\n", p->getPid());
431 ret->hasRunningThread = true;
434 if (dyninst_debug_proccontrol) {
435 for (int_threadPool::iterator i = tp->begin(); i != tp->end(); i++)
437 int_thread *thr = *i;
438 pthrd_printf("Pre-Thread %d/%d is in handler state %s with internal state %s (user is %s)\n",
439 p->getPid(), thr->getLWP(),
440 int_thread::stateStr(thr->getHandlerState()),
441 int_thread::stateStr(thr->getInternalState()),
442 int_thread::stateStr(thr->getUserState()));
449 bool force_leave_stopped = false;
450 for (int_threadPool::iterator i = tp->begin(); i != tp->end(); i++)
452 int_thread *thr = *i;
453 thr->handleNextPostedIRPC(false);
455 int_iRPC::ptr rpc = thr->hasRunningProcStopperRPC();
458 ret->hasProcStopRPC = true;
459 if (rpc->getState() >= int_iRPC::Prepping) {
460 pthrd_printf("Thread %d/%d has pending proc stopper RPC, leaving other threads stopped\n",
461 p->getPid(), thr->getLWP());
462 force_leave_stopped = true;
466 for (int_threadPool::iterator i = tp->begin(); i != tp->end(); i++)
468 int_thread *thr = *i;
470 if (thr->hasPendingStop()) {
471 ret->hasStopPending = true;
473 if (thr->isClearingBreakpoint()) {
474 ret->hasClearingBP = true;
477 int_iRPC::ptr pstop_rpc = thr->hasRunningProcStopperRPC();
478 if (thr->hasPendingStop() && thr->getHandlerState() == int_thread::stopped) {
479 pthrd_printf("Continuing thread %d/%d to clear out pending stop\n",
480 thr->llproc()->getPid(), thr->getLWP());
483 else if (thr->getInternalState() == int_thread::stopped && pstop_rpc &&
484 (pstop_rpc->getState() == int_iRPC::Running ||
485 pstop_rpc->getState() == int_iRPC::Ready))
487 pthrd_printf("Continuing thread %d/%d due to pending procstop iRPC\n",
488 p->getPid(), thr->getLWP());
491 else if (thr->getInternalState() == int_thread::running &&
492 thr->getHandlerState() == int_thread::stopped &&
493 !force_leave_stopped)
495 //The thread is stopped, but the user wants it running (we probably just finished
496 // handling a sync event). Go ahead and continue the thread.
497 pthrd_printf("Continuing thread %d/%d to match internal state after events\n",
498 p->getPid(), thr->getLWP());
502 if (thr->getInternalState() == int_thread::running ||
503 thr->getInternalState() == int_thread::neonatal_intermediate) {
504 //Keep track if any threads are running and running synchronous RPCs
505 ret->hasRunningThread = true;
506 if (thr->hasSyncRPC()) {
507 ret->hasSyncRPC = true;
511 pthrd_printf("Finished syncing runState for %d\n", p->getPid());
512 if (dyninst_debug_proccontrol) {
513 for (int_threadPool::iterator i = tp->begin(); i != tp->end(); i++)
515 int_thread *thr = *i;
516 pthrd_printf("Post-Thread %d/%d is in handler state %s with internal state %s (user is %s)\n",
517 p->getPid(), thr->getLWP(),
518 int_thread::stateStr(thr->getHandlerState()),
519 int_thread::stateStr(thr->getInternalState()),
520 int_thread::stateStr(thr->getUserState()));
526 int_process *int_process::in_waitHandleProc = NULL;
527 bool int_process::waitAndHandleForProc(bool block, int_process *proc, bool &proc_exited)
529 assert(in_waitHandleProc == NULL);
530 in_waitHandleProc = proc;
532 bool result = waitAndHandleEvents(block);
535 if (proc->getState() == int_process::exited) {
536 pthrd_printf("Deleting proc %d from waitAndHandleForProc\n", proc->getPid());
544 in_waitHandleProc = NULL;
549 bool int_process::waitAndHandleEvents(bool block)
551 bool gotEvent = false;
552 assert(!int_process::in_callback);
555 static bool recurse = false;
562 * Check status of threads
564 pthrd_printf("Updating state of each process\n");
565 syncRunStateRet_t ret;
566 ProcPool()->for_each(syncRunState, &ret);
568 if (ret.readyProcStoppers.size()) {
569 int_process *proc = ret.readyProcStoppers[0];
570 Event::ptr ev = proc->removeProcStopper();
571 if (ev->triggersCB() &&
573 mt()->getThreadMode() == Process::HandlerThreading)
575 pthrd_printf("Handler thread sees postponed callback requiring "
576 "event '%s', not taking\n",
578 notify()->noteEvent();
582 pthrd_printf("Handling postponed proc stopper event on %d\n", proc->getPid());
583 proc->handlerpool->handleEvent(ev);
588 * Check for possible error combinations from syncRunState
590 if (!ret.hasRunningThread) {
592 //We've successfully handled an event, but no longer have any running threads
593 pthrd_printf("Returning after handling events, no threads running\n");
596 if (isHandlerThread()) {
597 //Not an error for the handler thread to get no events.
598 pthrd_printf("Returning to handler due to no running threads\n");
601 //The user called us with no running threads
602 setLastError(err_notrunning, "No running processes or threads to receive events on");
603 pthrd_printf("No running threads, returning from waitAndHandleEvents\n");
609 * The handler thread doesn't want to pick up anything that
610 * requires a callback, leaving that for the user. Peek ahead
611 * in the mailbox and abort out if we're the handler thread and
612 * the next event will require a callback.
614 if (isHandlerThread() && mt()->getThreadMode() == Process::HandlerThreading) {
615 Event::ptr ev = mbox()->peek();
616 if (ev == Event::ptr())
618 pthrd_printf("Handler thread returning due to lack of events\n");
621 if (ev->triggersCB())
623 pthrd_printf("Handler thread sees callback requiring event '%s', "
624 "not taking\n", ev->name().c_str());
625 notify()->noteEvent();
630 * Check for new events
632 bool should_block = ((block && !gotEvent) || ret.hasStopPending ||
633 ret.hasSyncRPC || ret.hasClearingBP || ret.hasProcStopRPC);
634 pthrd_printf("%s for events (%d %d %d %d %d %d)\n",
635 should_block ? "Blocking" : "Polling",
636 (int) block, (int) gotEvent, (int) ret.hasStopPending,
637 (int) ret.hasSyncRPC, (int) ret.hasClearingBP, (int) ret.hasProcStopRPC);
638 Event::ptr ev = mbox()->dequeue(should_block);
640 if (ev == Event::ptr())
642 if (block && gotEvent) {
643 pthrd_printf("Returning after handling events\n");
647 perr_printf("Blocking wait failed to get events\n");
648 setLastError(err_internal, "Blocking wait returned without events");
652 if (isHandlerThread()) {
653 pthrd_printf("Handler thread found nothing to do\n");
656 setLastError(err_noevents, "Poll failed to find events");
657 pthrd_printf("Poll failed to find events\n");
661 if (mt()->getThreadMode() == Process::NoThreads ||
662 mt()->getThreadMode() == Process::GeneratorThreading)
664 pthrd_printf("Clearing event from pipe after dequeue\n");
665 notify()->clearEvent();
669 ev->getProcess()->llproc()->updateSyncState(ev, false);
671 if (ev->procStopper()) {
673 * This event wants the process stopped before it gets handled.
674 * We'll start that here, and then postpone the event until it's
675 * stopped. It's up to the event to continue the process again.
677 int_process *proc = ev->getProcess()->llproc();
678 proc->threadPool()->desyncInternalState();
679 bool result = proc->threadPool()->intStop(false);
681 pthrd_printf("Failed to stop process for event.\n");
684 proc->addProcStopper(ev);
689 HandlerPool *hpool = ev->getProcess()->llproc()->handlerpool;
690 hpool->handleEvent(ev);
692 if (!ev->getProcess()->llproc())
694 //Special case event handling, the process cleaned itself
695 // under this event (likely post-exit or post-crash), but was
696 // unable to clean its handlerpool (as we were using it).
697 // Clean this for the process now.
706 bool int_process::detach(bool &should_delete)
708 should_delete = false;
709 bool had_error = true;
711 int_threadPool *tp = threadPool();
712 pthrd_printf("Detach requested on %d\n", getPid());
713 while (!tp->allStopped()) {
714 pthrd_printf("Stopping process for detach\n");
718 while (!mem->breakpoints.empty())
720 std::map<Dyninst::Address, installed_breakpoint *>::iterator i = mem->breakpoints.begin();
721 bool result = i->second->uninstall(this);
723 perr_printf("Error removing breakpoint at %lx\n", i->first);
724 setLastError(err_internal, "Error removing breakpoint before detach\n");
729 ProcPool()->condvar()->lock();
731 result = plat_detach();
733 pthrd_printf("Error performing lowlevel detach\n");
739 ProcPool()->condvar()->signal();
740 ProcPool()->condvar()->unlock();
744 should_delete = true;
748 bool int_process::terminate(bool &needs_sync)
750 pthrd_printf("Terminate requested on process %d\n", getPid());
751 bool had_error = true;
752 ProcPool()->condvar()->lock();
753 bool result = plat_terminate(needs_sync);
755 pthrd_printf("plat_terminate failed on %d\n", getPid());
758 setForceGeneratorBlock(true);
761 ProcPool()->condvar()->signal();
762 ProcPool()->condvar()->unlock();
766 int_process::int_process(Dyninst::PID p, std::string e, std::vector<std::string> a) :
771 arch(Dyninst::Arch_none),
773 up_proc(Process::ptr()),
775 hasCrashSignal(false),
778 forceGenerator(false),
782 //Put any object initialization in 'initializeProcess', below.
785 int_process::int_process(Dyninst::PID pid_, int_process *p) :
786 state(int_process::running),
788 executable(p->executable),
791 hasCrashSignal(p->hasCrashSignal),
792 crashSignal(p->crashSignal),
793 hasExitCode(p->hasExitCode),
794 forceGenerator(false),
795 exitCode(p->exitCode),
796 exec_mem_cache(exec_mem_cache)
798 Process::ptr hlproc = Process::ptr(new Process());
799 mem = new mem_state(*p->mem, this);
800 initializeProcess(hlproc);
803 void int_process::initializeProcess(Process::ptr p)
807 threadpool = new int_threadPool(this);
808 handlerpool = createDefaultHandlerPool();
811 mem = new mem_state(this);
812 Generator::getDefaultGenerator(); //May create generator thread
815 int_thread *int_process::findStoppedThread()
817 ProcPool()->condvar()->lock();
818 int_thread *result = NULL;
819 for (int_threadPool::iterator i = threadpool->begin(); i != threadpool->end(); i++)
821 int_thread *thr = *i;
822 if (thr->getHandlerState() == int_thread::stopped) {
827 ProcPool()->condvar()->unlock();
829 assert(result->getGeneratorState() == int_thread::stopped);
834 bool int_process::readMem(void *local, Dyninst::Address remote, size_t size)
836 int_thread *thr = findStoppedThread();
838 setLastError(err_notstopped, "A thread must be stopped to read from memory");
839 perr_printf("Unable to find a stopped thread for read in process %d\n", getPid());
842 pthrd_printf("Reading from remote memory %lx to %p, size = %lu on %d/%d\n",
843 remote, local, (unsigned long) size, getPid(), thr->getLWP());
844 return plat_readMem(thr, local, remote, size);
847 bool int_process::writeMem(void *local, Dyninst::Address remote, size_t size)
849 int_thread *thr = findStoppedThread();
851 setLastError(err_notstopped, "A thread must be stopped to write to memory");
852 perr_printf("Unable to find a stopped thread for write in process %d\n", getPid());
855 pthrd_printf("Writing to remote memory %lx from %p, size = %lu on %d/%d\n",
856 remote, local, (unsigned long) size, getPid(), thr->getLWP());
857 return plat_writeMem(thr, local, remote, size);
860 Dyninst::Address int_process::mallocExecMemory(unsigned size)
862 Dyninst::Address max = 0;
863 std::map<Dyninst::Address, unsigned>::iterator i;
864 for (i = exec_mem_cache.begin(); i != exec_mem_cache.end(); i++) {
865 if (i->first + i->second > max)
866 max = i->first + i->second;
869 Dyninst::Address addr = plat_mallocExecMemory(max, size);
870 exec_mem_cache[addr] = size;
874 void int_process::freeExecMemory(Dyninst::Address addr)
876 std::map<Dyninst::Address, unsigned>::iterator i;
877 i = exec_mem_cache.find(addr);
878 assert(i != exec_mem_cache.end());
879 exec_mem_cache.erase(i);
882 Dyninst::Address int_process::infMalloc(unsigned long size, bool use_addr, Dyninst::Address addr)
884 pthrd_printf("Process %d is allocating memory of size %lu at 0x%lx\n", getPid(), size, addr);
885 int_iRPC::ptr rpc = rpcMgr()->createInfMallocRPC(this, size, use_addr, addr);
887 rpcMgr()->postRPCToProc(this, rpc);
889 int_thread *thr = rpc->thread();
890 bool result = thr->handleNextPostedIRPC(true);
892 pthrd_printf("Failed to handleNextPostedIRPC\n");
896 if (rpc->getState() != int_iRPC::Finished)
899 result = waitAndHandleForProc(true, this, proc_exited);
901 perr_printf("Process exited during infMalloc\n");
902 setLastError(err_exited, "Process exited during infMalloc\n");
906 pthrd_printf("Error in waitAndHandleEvents");
910 assert(rpc->getState() == int_iRPC::Finished);
912 Dyninst::Address aresult = rpc->infMallocResult();
913 pthrd_printf("Inferior malloc returning %lx\n", aresult);
914 mem->inf_malloced_memory[aresult] = size;
918 bool int_process::infFree(Dyninst::Address addr)
920 std::map<Dyninst::Address, unsigned long>::iterator i = mem->inf_malloced_memory.find(addr);
921 if (i == mem->inf_malloced_memory.end()) {
922 setLastError(err_badparam, "Unknown address passed to freeMemory");
923 perr_printf("Passed bad address, %lx, to infFree\n", addr);
926 unsigned long size = i->second;
928 int_iRPC::ptr rpc = rpcMgr()->createInfFreeRPC(this, size, addr);
930 pthrd_printf("Process %d is freeing memory of size %lu at 0x%lx with rpc %lu\n", getPid(), size, addr,
932 rpcMgr()->postRPCToProc(this, rpc);
934 int_thread *thr = rpc->thread();
935 bool result = thr->handleNextPostedIRPC(true);
937 pthrd_printf("Failed to handleNextPostedIRPC\n");
941 if (rpc->getState() != int_iRPC::Finished)
944 result = waitAndHandleForProc(true, this, proc_exited);
946 perr_printf("Process exited during infFree\n");
947 setLastError(err_exited, "Process exited during infFree\n");
951 pthrd_printf("Error in waitAndHandleEvents");
955 assert(rpc->getState() == int_iRPC::Finished);
957 pthrd_printf("Inferior free returning successfully\n");
958 mem->inf_malloced_memory.erase(i);
962 void int_process::addProcStopper(Event::ptr ev)
964 proc_stoppers.push(ev);
967 bool int_process::forceGeneratorBlock() const
969 return forceGenerator;
972 void int_process::setForceGeneratorBlock(bool b)
977 Event::ptr int_process::removeProcStopper()
979 assert(proc_stoppers.size());
980 Event::ptr ret = proc_stoppers.front();
985 bool int_process::hasQueuedProcStoppers() const
987 return !proc_stoppers.empty();
990 int int_process::getAddressWidth()
992 switch (getTargetArch()) {
1005 HandlerPool *int_process::handlerPool() const
1010 bool int_process::addBreakpoint(Dyninst::Address addr, int_breakpoint *bp)
1012 if (getState() != running) {
1013 perr_printf("Attempted to add breakpoint at %lx to stopped process %d\n", addr, getPid());
1014 setLastError(err_exited, "Attempted to insert breakpoint into exited process\n");
1018 pthrd_printf("Installing new breakpoint at %lx into %d\n", addr, getPid());
1019 installed_breakpoint *ibp = NULL;
1020 map<Address, installed_breakpoint *>::iterator i = mem->breakpoints.find(addr);
1021 if (i == mem->breakpoints.end()) {
1022 pthrd_printf("Adding new breakpoint to %d\n", getPid());
1023 ibp = new installed_breakpoint(mem, addr);
1024 ibp->addBreakpoint(this, bp);
1025 bool result = ibp->install(this);
1027 pthrd_printf("Failed to install new breakpoint\n");
1033 assert(ibp && ibp->isInstalled());
1034 bool result = ibp->addBreakpoint(this, bp);
1036 pthrd_printf("Failed to install new breakpoint\n");
1043 bool int_process::rmBreakpoint(Dyninst::Address addr, int_breakpoint *bp)
1045 map<Address, installed_breakpoint *>::iterator i = mem->breakpoints.find(addr);
1046 if (i == mem->breakpoints.end()) {
1047 perr_printf("Attempted to removed breakpoint that isn't installed\n");
1050 installed_breakpoint *ibp = i->second;
1051 assert(ibp && ibp->isInstalled());
1054 bool result = ibp->rmBreakpoint(this, bp, empty);
1056 pthrd_printf("rmBreakpoint failed on breakpoint at %lx in %d\n", addr, getPid());
1066 installed_breakpoint *int_process::getBreakpoint(Dyninst::Address addr)
1068 std::map<Dyninst::Address, installed_breakpoint *>::iterator i = mem->breakpoints.find(addr);
1069 if (i == mem->breakpoints.end())
1074 int_library *int_process::getLibraryByName(std::string s) const
1076 for (set<int_library *>::iterator i = mem->libs.begin();
1077 i != mem->libs.end(); i++)
1079 if (s == (*i)->getName())
1085 size_t int_process::numLibs() const
1087 return mem->libs.size();
1090 std::string int_process::getExecutable() const
1095 bool int_process::isInCallback()
1100 mem_state::ptr int_process::memory() const
1105 void int_process::setExitCode(int c)
1107 assert(!hasCrashSignal);
1112 void int_process::setCrashSignal(int s)
1114 assert(!hasExitCode);
1115 hasCrashSignal = true;
1119 bool int_process::getExitCode(int &c)
1125 bool int_process::getCrashSignal(int &s)
1128 return hasCrashSignal;
1131 bool int_process::isInCB()
1136 void int_process::setInCB(bool b)
1138 assert(in_callback == !b);
1142 void int_process::updateSyncState(Event::ptr ev, bool gen)
1144 EventType etype = ev->getEventType();
1145 switch (ev->getSyncType()) {
1147 pthrd_printf("Event %s is asynchronous\n", etype.name().c_str());
1149 case Event::sync_thread: {
1150 int_thread *thrd = ev->getThread()->llthrd();
1153 pthrd_printf("Event %s is thread synchronous, marking thread %d stopped\n",
1154 etype.name().c_str(), thrd->getLWP());
1155 int_thread::State old_state = gen ? thrd->getGeneratorState() : thrd->getHandlerState();
1156 assert(old_state == int_thread::running ||
1157 old_state == int_thread::neonatal_intermediate);
1158 if (old_state == int_thread::errorstate)
1161 thrd->setGeneratorState(int_thread::stopped);
1163 thrd->setHandlerState(int_thread::stopped);
1166 case Event::sync_process: {
1167 pthrd_printf("Event %s is process synchronous, marking process %d stopped\n",
1168 etype.name().c_str(), getPid());
1169 int_threadPool *tp = threadPool();
1170 for (int_threadPool::iterator i = tp->begin(); i != tp->end(); i++) {
1171 int_thread *thrd = *i;
1172 int_thread::State old_state = gen ? thrd->getGeneratorState() : thrd->getHandlerState();
1173 if (old_state != int_thread::running &&
1174 old_state != int_thread::neonatal_intermediate)
1177 thrd->setGeneratorState(int_thread::stopped);
1179 thrd->setHandlerState(int_thread::stopped);
1183 case Event::unset: {
1189 int_process::~int_process()
1191 if (up_proc != Process::ptr())
1193 proc_exitstate *exitstate = new proc_exitstate();
1194 exitstate->pid = pid;
1195 exitstate->exited = hasExitCode;
1196 exitstate->exit_code = exitCode;
1197 exitstate->crashed = hasCrashSignal;
1198 exitstate->crash_signal = crashSignal;
1199 assert(!up_proc->exitstate_);
1200 up_proc->exitstate_ = exitstate;
1201 up_proc->llproc_ = NULL;
1208 //Do not delete handlerpool yet, we're currently under
1209 // an event handler. We do want to delete this if called
1212 mem->rmProc(this, should_clean);
1219 bool int_threadPool::userCont()
1224 bool int_threadPool::intCont()
1229 bool int_threadPool::cont(bool user_cont)
1231 pthrd_printf("%s continuing process %d\n", user_cont ? "User" : "Int", proc()->getPid());
1233 Dyninst::PID pid = proc()->getPid();
1234 bool had_error = false;
1235 bool cont_something = false;
1237 ProcPool()->condvar()->lock();
1239 for (iterator i = begin(); i != end(); i++) {
1240 int_thread *thr = *i;
1243 pthrd_printf("Continuing thread %d on process %d\n", thr->getLWP(), pid);
1244 int_thread::stopcont_ret_t ret = thr->cont(user_cont, true);
1246 case int_thread::sc_skip:
1248 case int_thread::sc_error:
1251 case int_thread::sc_success:
1252 case int_thread::sc_success_pending:
1253 cont_something = true;
1258 ProcPool()->condvar()->signal();
1259 ProcPool()->condvar()->unlock();
1261 if (!cont_something) {
1262 perr_printf("Failed to continue exited process %d\n", pid);
1263 setLastError(err_exited, "Continue attempted on exited process\n");
1270 bool int_thread::userCont()
1275 bool int_thread::intCont()
1280 bool int_thread::cont(bool user_cont)
1282 pthrd_printf("%s continuing single thread %d/%d\n", user_cont ? "User" : "Int",
1283 llproc()->getPid(), getLWP());
1285 bool result = rpcMgr()->handleThreadContinue(this, user_cont);
1287 pthrd_printf("Error handling IRPC during continue\n");
1291 if (!llproc()->independentLWPControl()) {
1292 pthrd_printf("%s continuing entire process %d on thread operation on %d\n",
1293 user_cont ? "User" : "Int", llproc()->getPid(), getLWP());
1295 return llproc()->threadPool()->userCont();
1298 return llproc()->threadPool()->intCont();
1301 stopcont_ret_t ret = cont(user_cont, false);
1303 if (ret == sc_skip) {
1304 perr_printf("Attempted to continue exited thread\n");
1305 setLastError(err_exited, "Attempted thread continue on exited thread\n");
1308 if (ret == sc_error) {
1309 pthrd_printf("Error continuing thread %d/%d\n", llproc()->getPid(), getLWP());
1312 assert(ret == sc_success || ret == sc_success_pending);
1316 bool result = setUserState(int_thread::running);
1318 setLastError(err_exited, "Attempted thread continue on exited thread\n");
1319 perr_printf("Failed to continue thread %d/%d--bad state\n", llproc()->getPid(), getLWP());
1327 int_thread::stopcont_ret_t int_thread::cont(bool user_cont, bool have_proc_lock)
1329 Dyninst::PID pid = proc()->getPid();
1331 pthrd_printf("Top level %s continue for %d/%d\n",
1332 user_cont ? "user" : "int", pid, lwp);
1334 if (getHandlerState() == errorstate) {
1335 pthrd_printf("thread %d on process %d in error state\n", getLWP(), pid);
1338 if (getHandlerState() == exited) {
1339 pthrd_printf("thread %d on process %d already exited\n", getLWP(), pid);
1344 setUserState(running);
1347 if (getHandlerState() != stopped) {
1348 perr_printf("Error. continue attempted on running thread %d/%d\n", pid, lwp);
1349 setLastError(err_notstopped, "Continue attempted on running thread\n");
1353 if (!have_proc_lock) {
1354 ProcPool()->condvar()->lock();
1357 regpool_lock.lock();
1358 cached_regpool.regs.clear();
1359 cached_regpool.full = false;
1360 regpool_lock.unlock();
1362 bool result = plat_cont();
1364 setInternalState(running);
1365 setHandlerState(running);
1366 setGeneratorState(running);
1369 if (!have_proc_lock) {
1370 ProcPool()->condvar()->signal();
1371 ProcPool()->condvar()->unlock();
1375 pthrd_printf("Could not resume debugee %d, thread %d\n", pid, lwp);
1382 bool int_threadPool::userStop()
1384 return stop(true, true);
1387 bool int_threadPool::intStop(bool sync)
1389 return stop(false, sync);
1392 bool int_threadPool::stop(bool user_stop, bool sync)
1394 bool stopped_something = false;
1395 bool had_error = false;
1396 bool needs_sync = false;
1397 for (iterator i = begin(); i != end(); i++) {
1398 int_thread *thr = *i;
1400 pthrd_printf("Process %d performing %s stop on thread %d\n", proc()->getPid(),
1401 user_stop ? "user" : "int", thr->getLWP());
1402 int_thread::stopcont_ret_t ret = thr->stop(user_stop);
1404 case int_thread::sc_skip:
1405 pthrd_printf("int_thread::stop on %d/%d returned sc_skip\n",
1406 proc()->getPid(), thr->getLWP());
1408 case int_thread::sc_error:
1409 pthrd_printf("int_thread::stop on %d/%d returned sc_error\n",
1410 proc()->getPid(), thr->getLWP());
1411 if (getLastError() == err_noproc)
1412 pthrd_printf("int_thread::stop thread exit on %d/%d, skipping stop\n",
1413 proc()->getPid(), thr->getLWP());
1417 case int_thread::sc_success_pending:
1418 pthrd_printf("int_thread::stop on %d/%d return sc_success_pending\n",
1419 proc()->getPid(), thr->getLWP());
1420 stopped_something = true;
1423 case int_thread::sc_success:
1424 pthrd_printf("int_thread::stop on %d/%d returned sc_success\n",
1425 proc()->getPid(), thr->getLWP());
1426 stopped_something = true;
1432 pthrd_printf("Error while stopping threads on %d\n", proc()->getPid());
1433 setLastError(err_internal, "Could not stop process\n");
1436 if (!stopped_something) {
1437 perr_printf("No threads can be stopped on %d\n", proc()->getPid());
1438 setLastError(err_notrunning, "Attempt to stop a process that isn't running\n");
1442 if (needs_sync && sync)
1445 bool result = int_process::waitAndHandleForProc(true, proc(), proc_exited);
1447 pthrd_printf("Process exited during stop\n");
1448 setLastError(err_exited, "Process exited during stop\n");
1452 perr_printf("Error waiting for events after stop on %d\n", proc()->getPid());
1460 int_thread::stopcont_ret_t int_thread::stop(bool user_stop)
1462 Dyninst::PID pid = proc()->getPid();
1463 pthrd_printf("Top level %s thread pause for %d/%d\n",
1464 user_stop ? "user" : "int", pid, lwp);
1466 if (getHandlerState() == errorstate) {
1467 pthrd_printf("thread %d on process %d in error state\n", getLWP(), pid);
1470 if (getHandlerState() == exited) {
1471 pthrd_printf("thread %d on process %d already exited\n", getLWP(), pid);
1476 pthrd_printf("thread %d has in-progress stop on process %d\n", getLWP(), pid);
1479 if (getHandlerState() == stopped) {
1480 pthrd_printf("thread %d on process %d is already handler stopped, leaving\n",
1483 setUserState(stopped);
1484 setInternalState(stopped);
1487 if (getInternalState() == stopped) {
1488 pthrd_printf("thread %d is already stopped on process %d\n", getLWP(), pid);
1490 setUserState(stopped);
1493 if (getHandlerState() != running)
1495 perr_printf("Attempt to stop thread %d/%d in bad state %d\n",
1496 pid, lwp, getHandlerState());
1497 setLastError(err_internal, "Bad state during thread stop attempt\n");
1501 assert(!pending_stop);
1502 pending_stop = true;
1504 assert(!pending_user_stop);
1505 pending_user_stop = true;
1508 bool result = plat_stop();
1510 pthrd_printf("Could not pause debuggee %d, thr %d\n", pid, lwp);
1511 pending_stop = false;
1516 return sc_success_pending;
1521 bool int_thread::stop(bool user_stop, bool sync)
1523 if (!llproc()->independentLWPControl()) {
1525 pthrd_printf("User stopping entire process %d on thread operation on %d\n",
1526 llproc()->getPid(), getLWP());
1527 return llproc()->threadPool()->userStop();
1530 pthrd_printf("Int stopping entire process %d on thread operation on %d\n",
1531 llproc()->getPid(), getLWP());
1532 return llproc()->threadPool()->intStop();
1536 pthrd_printf("%s stopping single thread %d/%d\n", user_stop ? "User" : "Int",
1537 llproc()->getPid(), getLWP());
1539 stopcont_ret_t ret = stop(user_stop);
1540 if (ret == sc_skip) {
1541 perr_printf("Thread %d/%d was not in a stoppable state\n",
1542 llproc()->getPid(), getLWP());
1543 setLastError(err_notrunning, "Attempt to stop a thread that isn't running\n");
1546 if (ret == sc_error) {
1547 pthrd_printf("Thread %d/%d returned error during stop\n",
1548 llproc()->getPid(), getLWP());
1551 if (ret == sc_success) {
1552 pthrd_printf("Thread %d/%d successfully stopped\n",
1553 llproc()->getPid(), getLWP());
1556 assert(ret == sc_success_pending);
1558 pthrd_printf("Thread %d/%d successfully stopped, but not sync'd\n",
1559 llproc()->getPid(), getLWP());
1564 bool result = int_process::waitAndHandleForProc(true, llproc(), proc_exited);
1566 pthrd_printf("Process exited during thread stop\n");
1567 setLastError(err_exited, "Process exited during stop\n");
1571 perr_printf("Error waiting for events after stop on %d\n", getLWP());
1577 bool int_thread::userStop()
1579 return stop(true, true);
1582 bool int_thread::intStop(bool sync)
1584 return stop(false, sync);
1587 void int_thread::setPendingUserStop(bool b)
1589 pending_user_stop = b;
1592 bool int_thread::hasPendingUserStop() const
1594 return pending_user_stop;
1597 void int_thread::setPendingStop(bool b)
1602 bool int_thread::hasPendingStop() const
1604 return pending_stop;
1607 Process::ptr int_thread::proc() const
1609 return proc_->proc();
1612 int_process *int_thread::llproc() const
1617 Dyninst::THR_ID int_thread::getTid() const
1622 Dyninst::LWP int_thread::getLWP() const
1627 int_thread::State int_thread::getHandlerState() const
1629 return handler_state;
1632 int_thread::State int_thread::getUserState() const
1637 int_thread::State int_thread::getGeneratorState() const
1639 return generator_state;
1642 int_thread::State int_thread::getInternalState() const
1644 return internal_state;
1647 const char *int_thread::stateStr(int_thread::State s)
1650 case neonatal: return "neonatal";
1651 case neonatal_intermediate: return "neonatal_intermediate";
1652 case running: return "running";
1653 case stopped: return "stopped";
1654 case exited: return "exited";
1655 case errorstate: return "errorstate";
1661 bool int_thread::setAnyState(int_thread::State *from, int_thread::State to)
1663 const char *s = NULL;
1664 if (from == &handler_state) {
1665 s = "handler state";
1667 else if (from == &user_state) {
1670 else if (from == &generator_state) {
1671 s = "generator state";
1673 else if (from == &internal_state) {
1674 s = "internal state";
1679 pthrd_printf("Leaving %s for %d in state %s\n", s, lwp, stateStr(to));
1682 if (to == errorstate) {
1683 perr_printf("Setting %s for %d from %s to errorstate\n",
1684 s, lwp, stateStr(*from));
1688 if (*from == errorstate) {
1689 perr_printf("Attempted %s reversion for %d from errorstate to %s\n",
1690 s, lwp, stateStr(to));
1693 if (*from == exited) {
1694 perr_printf("Attempted %s reversion for %d from exited to %s\n",
1695 s, lwp, stateStr(to));
1698 if (to == neonatal && *from != neonatal) {
1699 perr_printf("Attempted %s reversion for %d from %s to neonatal\n",
1700 s, lwp, stateStr(*from));
1704 pthrd_printf("Changing %s for %d/%d from %s to %s\n", s, llproc()->getPid(), lwp,
1705 stateStr(*from), stateStr(to));
1708 if (internal_state == stopped) assert(handler_state == stopped);
1709 if (handler_state == stopped) assert(generator_state == stopped || generator_state == exited);
1710 if (generator_state == running) assert(handler_state == running);
1711 if (handler_state == running) assert(internal_state == running);
1715 bool int_thread::setHandlerState(int_thread::State s)
1717 return setAnyState(&handler_state, s);
1720 bool int_thread::setUserState(int_thread::State s)
1722 return setAnyState(&user_state, s);
1725 bool int_thread::setGeneratorState(int_thread::State s)
1727 return setAnyState(&generator_state, s);
1730 bool int_thread::setInternalState(int_thread::State s)
1732 return setAnyState(&internal_state, s);
1735 void int_thread::desyncInternalState()
1737 pthrd_printf("Thread %d/%d is desyncing int from user state %d\n",
1738 llproc()->getPid(), getLWP(), num_locked_stops+1);
1742 void int_thread::restoreInternalState(bool sync)
1744 pthrd_printf("Thread %d/%d is restoring int to user state, %d\n",
1745 llproc()->getPid(), getLWP(), num_locked_stops-1);
1746 assert(num_locked_stops > 0);
1748 if (num_locked_stops > 0)
1751 pthrd_printf("Changing internal state, %s, to user state, %s.\n",
1752 int_thread::stateStr(internal_state), int_thread::stateStr(user_state));
1754 if (internal_state == user_state)
1758 else if (internal_state == int_thread::exited ||
1759 user_state == int_thread::exited)
1761 setInternalState(int_thread::exited);
1763 else if (internal_state == int_thread::stopped &&
1764 user_state == int_thread::running)
1766 bool result = intCont();
1768 perr_printf("Error continuing internal process %d/%d when resyncing\n",
1769 llproc()->getPid(), getLWP());
1773 else if (internal_state == int_thread::running &&
1774 user_state == int_thread::stopped)
1776 bool result = intStop(sync);
1778 perr_printf("Error stopping internal process %d/%d when resyncing\n",
1779 llproc()->getPid(), getLWP());
1784 setInternalState(user_state);
1788 void int_thread::setContSignal(int sig)
1793 int_thread::int_thread(int_process *p, Dyninst::THR_ID t, Dyninst::LWP l) :
1798 handler_state(neonatal),
1799 user_state(neonatal),
1800 generator_state(neonatal),
1801 internal_state(neonatal),
1803 pending_user_stop(false),
1804 pending_stop(false),
1805 num_locked_stops(0),
1806 user_single_step(false),
1808 clearing_breakpoint(false)
1810 Thread::ptr new_thr(new Thread());
1812 new_thr->llthread_ = this;
1813 up_thread = new_thr;
1816 int_thread::~int_thread()
1818 assert(!up_thread->exitstate_);
1820 up_thread->exitstate_ = new thread_exitstate();
1821 up_thread->exitstate_->lwp = lwp;
1822 up_thread->exitstate_->thr_id = tid;
1823 up_thread->llthread_ = NULL;
1826 int_thread *int_thread::createThread(int_process *proc,
1827 Dyninst::THR_ID thr_id,
1828 Dyninst::LWP lwp_id,
1831 int_thread *newthr = createThreadPlat(proc, thr_id, lwp_id, initial_thrd);
1832 pthrd_printf("Creating %s thread %d/%d, thr_id = %d\n",
1833 initial_thrd ? "initial" : "new",
1834 proc->getPid(), newthr->getLWP(), thr_id);
1835 proc->threadPool()->addThread(newthr);
1837 proc->threadPool()->setInitialThread(newthr);
1839 ProcPool()->addThread(proc, newthr);
1840 bool result = newthr->attach();
1842 pthrd_printf("Failed to attach to new thread %d/%d\n", proc->getPid(), lwp_id);
1845 newthr->setUserState(neonatal_intermediate);
1846 newthr->setInternalState(neonatal_intermediate);
1847 newthr->setHandlerState(neonatal_intermediate);
1848 newthr->setGeneratorState(neonatal_intermediate);
1853 Thread::ptr int_thread::thread()
1858 bool int_thread::getAllRegisters(int_registerPool &pool)
1860 bool result = false;
1862 pthrd_printf("Reading registers for thread %d\n", getLWP());
1863 regpool_lock.lock();
1864 if (cached_regpool.full) {
1865 pool = cached_regpool;
1866 pthrd_printf("Returning cached register set\n");
1870 result = plat_getAllRegisters(cached_regpool);
1872 pthrd_printf("plat_getAllRegisters returned error on %d\n", getLWP());
1875 cached_regpool.full = true;
1876 pool = cached_regpool;
1878 pthrd_printf("Successfully retrieved all registers for %d\n", getLWP());
1881 regpool_lock.unlock();
1885 bool int_thread::setAllRegisters(int_registerPool &pool)
1887 assert(getHandlerState() == int_thread::stopped);
1888 assert(getGeneratorState() == int_thread::stopped);
1889 regpool_lock.lock();
1891 pthrd_printf("Setting registers for thread %d\n", getLWP());
1892 bool result = plat_setAllRegisters(pool);
1894 pthrd_printf("plat_setAllRegisters returned error on %d\n", getLWP());
1897 cached_regpool = pool;
1898 cached_regpool.full = true;
1899 pthrd_printf("Successfully set all registers for %d\n", getLWP());
1901 regpool_lock.unlock();
1905 bool int_thread::getRegister(Dyninst::MachRegister reg, Dyninst::MachRegisterVal &val)
1907 bool result = false;
1908 pthrd_printf("Get register value for thread %d, register %s\n", lwp, reg.name());
1910 if (!llproc()->plat_individualRegAccess())
1912 pthrd_printf("Platform does not support individual register access, "
1913 "getting everyting\n");
1914 int_registerPool pool;
1915 result = getAllRegisters(pool);
1917 pthrd_printf("Unable to access full register set\n");
1920 val = pool.regs[reg];
1924 regpool_lock.lock();
1926 int_registerPool::reg_map_t::iterator i = cached_regpool.regs.find(reg);
1927 if (i != cached_regpool.regs.end()) {
1929 pthrd_printf("Returning cached register value %lx for register %s on %d\n",
1930 val, reg.name(), lwp);
1935 result = plat_getRegister(reg, val);
1937 pthrd_printf("Error reading register value for %s on %d\n", reg.name(), lwp);
1941 pthrd_printf("Returning register value %lx for register %s on %d\n",
1942 val, reg.name(), lwp);
1944 regpool_lock.unlock();
1948 bool int_thread::setRegister(Dyninst::MachRegister reg, Dyninst::MachRegisterVal val)
1950 assert(getHandlerState() == int_thread::stopped);
1951 assert(getGeneratorState() == int_thread::stopped);
1953 if (!llproc()->plat_individualRegAccess())
1955 pthrd_printf("Platform does not support individual register access, "
1956 "setting everyting\n");
1957 int_registerPool pool;
1958 bool result = getAllRegisters(pool);
1960 pthrd_printf("Unable to access full register set\n");
1963 pool.regs[reg] = val;
1964 result = setAllRegisters(pool);
1966 pthrd_printf("Unable to set full register set\n");
1972 regpool_lock.lock();
1974 pthrd_printf("Setting register %s for thread %d to %lx\n", reg.name(), getLWP(), val);
1975 MachRegister base_register = reg.getBaseRegister();
1976 bool result = plat_setRegister(base_register, val);
1978 pthrd_printf("Error setting register %s\n", base_register.name());
1982 cached_regpool.regs[base_register] = val;
1984 regpool_lock.unlock();
1988 void int_thread::addPostedRPC(int_iRPC::ptr rpc_)
1991 posted_rpcs.push_back(rpc_);
1994 rpc_list_t *int_thread::getPostedRPCs()
1996 return &posted_rpcs;
1999 bool int_thread::hasPostedRPCs()
2001 return (posted_rpcs.size() != 0);
2004 void int_thread::setRunningRPC(int_iRPC::ptr rpc_)
2006 assert(!running_rpc);
2010 int_iRPC::ptr int_thread::runningRPC() const
2015 void int_thread::clearRunningRPC()
2017 running_rpc = int_iRPC::ptr();
2019 bool int_thread::saveRegsForRPC()
2021 assert(!rpc_regs.full);
2022 return getAllRegisters(rpc_regs);
2025 bool int_thread::restoreRegsForRPC(bool clear)
2027 assert(rpc_regs.full);
2028 bool result = setAllRegisters(rpc_regs);
2029 if (clear && result) {
2030 rpc_regs.regs.clear();
2031 rpc_regs.full = false;
2036 bool int_thread::hasSavedRPCRegs()
2038 return rpc_regs.full;
2041 bool int_thread::runningInternalRPC() const
2043 if (runningRPC() && runningRPC()->isInternalRPC()) {
2046 if (posted_rpcs.size() && posted_rpcs.front()->isInternalRPC()) {
2052 void int_thread::incSyncRPCCount()
2057 void int_thread::decSyncRPCCount()
2059 assert(sync_rpc_count > 0);
2063 bool int_thread::hasSyncRPC()
2065 return (sync_rpc_count != 0);
2068 int_iRPC::ptr int_thread::nextPostedIRPC() const
2070 if (!posted_rpcs.size())
2071 return int_iRPC::ptr();
2072 return posted_rpcs.front();
2075 bool int_thread::handleNextPostedIRPC(bool block)
2077 int_iRPC::ptr posted_rpc = nextPostedIRPC();
2078 if (!posted_rpc || runningRPC())
2081 bool ret_result = false;
2082 pthrd_printf("Handling next postd irpc %lu on %d/%d of type %s in state %s\n",
2083 posted_rpc->id(), llproc()->getPid(), getLWP(),
2084 posted_rpc->getStrType(), posted_rpc->getStrState());
2085 assert(posted_rpc->getState() == int_iRPC::Posted ||
2086 posted_rpc->getState() == int_iRPC::Prepping ||
2087 posted_rpc->getState() == int_iRPC::Prepped);
2089 if (posted_rpc->getState() == int_iRPC::Posted) {
2091 pthrd_printf("Prepping next rpc to run on %d/%d\n", llproc()->getPid(), getLWP());
2092 bool result = rpcMgr()->prepNextRPC(this, block, error);
2093 if (!result && error) {
2094 perr_printf("Failed to prep RPC\n");
2098 if (posted_rpc->getState() == int_iRPC::Prepping) {
2099 pthrd_printf("Checking if rpc is prepped on %d/%d\n", llproc()->getPid(), getLWP());
2100 posted_rpc->isRPCPrepped();
2102 if (posted_rpc->getState() == int_iRPC::Prepped) {
2103 pthrd_printf("Running next RPC on %d/%d\n", llproc()->getPid(), getLWP());
2104 rpcMgr()->runNextRPC(this, block);
2112 int_iRPC::ptr int_thread::hasRunningProcStopperRPC() const
2114 int_iRPC::ptr running = runningRPC();
2115 if (running && running->isProcStopRPC()) {
2118 int_iRPC::ptr nextposted = nextPostedIRPC();
2119 if (!running && nextposted && nextposted->isProcStopRPC() &&
2120 nextposted->getState() != int_iRPC::Posted)
2124 return int_iRPC::ptr();
2127 bool int_thread::singleStepMode() const
2132 void int_thread::setSingleStepMode(bool s)
2137 bool int_thread::singleStepUserMode() const
2139 return user_single_step;
2142 void int_thread::setSingleStepUserMode(bool s)
2144 user_single_step = s;
2147 bool int_thread::singleStep() const
2149 return single_step || user_single_step;
2152 void int_thread::markClearingBreakpoint(installed_breakpoint *bp)
2154 assert(!clearing_breakpoint || bp == NULL);
2155 clearing_breakpoint = bp;
2158 installed_breakpoint *int_thread::isClearingBreakpoint()
2160 return clearing_breakpoint;
2163 int_thread *int_threadPool::findThreadByLWP(Dyninst::LWP lwp)
2165 std::map<Dyninst::LWP, int_thread *>::iterator i = thrds_by_lwp.find(lwp);
2166 if (i == thrds_by_lwp.end())
2171 int_thread *int_threadPool::initialThread() const
2173 return initial_thread;
2176 bool int_threadPool::allStopped()
2178 for (iterator i = begin(); i != end(); i++) {
2179 if ((*i)->getInternalState() == int_thread::running)
2185 void int_threadPool::addThread(int_thread *thrd)
2187 Dyninst::LWP lwp = thrd->getLWP();
2188 std::map<Dyninst::LWP, int_thread *>::iterator i = thrds_by_lwp.find(lwp);
2189 assert (i == thrds_by_lwp.end());
2190 thrds_by_lwp[lwp] = thrd;
2191 threads.push_back(thrd);
2192 hl_threads.push_back(thrd->thread());
2195 void int_threadPool::rmThread(int_thread *thrd)
2197 assert(thrd != initial_thread);
2198 Dyninst::LWP lwp = thrd->getLWP();
2199 std::map<Dyninst::LWP, int_thread *>::iterator i = thrds_by_lwp.find(lwp);
2200 assert (i != thrds_by_lwp.end());
2201 thrds_by_lwp.erase(i);
2203 for (unsigned i=0; i<threads.size(); i++) {
2204 if (threads[i] != thrd)
2206 threads[i] = threads[threads.size()-1];
2208 hl_threads[i] = hl_threads[hl_threads.size()-1];
2209 hl_threads.pop_back();
2213 void int_threadPool::clear()
2217 thrds_by_lwp.clear();
2218 initial_thread = NULL;
2221 void int_threadPool::desyncInternalState()
2223 for (iterator i = begin(); i != end(); i++) {
2224 (*i)->desyncInternalState();
2228 void int_threadPool::restoreInternalState(bool sync)
2230 for (iterator i = begin(); i != end(); i++) {
2231 (*i)->restoreInternalState(false);
2234 int_process::waitAndHandleEvents(false);
2237 void int_threadPool::setInitialThread(int_thread *thrd)
2239 initial_thread = thrd;
2242 int_process *int_threadPool::proc() const
2247 unsigned int_threadPool::size() const
2249 return threads.size();
2252 ThreadPool *int_threadPool::pool() const
2257 int_threadPool::int_threadPool(int_process *p) :
2260 up_pool = new ThreadPool();
2261 up_pool->threadpool = this;
2264 int_threadPool::~int_threadPool()
2269 for (vector<int_thread*>::iterator i = threads.begin(); i != threads.end(); i++)
2275 int_breakpoint::int_breakpoint(Breakpoint::ptr up) :
2278 isCtrlTransfer_(false),
2283 int_breakpoint::int_breakpoint(Dyninst::Address to_, Breakpoint::ptr up) :
2286 isCtrlTransfer_(true),
2291 int_breakpoint::~int_breakpoint()
2295 bool int_breakpoint::isCtrlTransfer() const
2297 return isCtrlTransfer_;
2300 Address int_breakpoint::toAddr() const
2305 void *int_breakpoint::getData() const
2310 void int_breakpoint::setData(void *v)
2315 Breakpoint::weak_ptr int_breakpoint::upBreakpoint() const
2320 installed_breakpoint::installed_breakpoint(mem_state::ptr memory_, Address addr_) :
2329 installed_breakpoint::installed_breakpoint(mem_state::ptr memory_,
2330 const installed_breakpoint *ip) :
2334 buffer_size(ip->buffer_size),
2335 installed(ip->installed),
2336 suspend_count(ip->suspend_count),
2339 memcpy(buffer, ip->buffer, sizeof(buffer));
2342 installed_breakpoint::~installed_breakpoint()
2346 bool installed_breakpoint::isInstalled() const
2351 bool installed_breakpoint::install(int_process *proc)
2357 bool result = plat_install(proc, true);
2359 pthrd_printf("Error installing breakpoint\n");
2362 assert(buffer && buffer_size);
2364 memory->breakpoints[addr] = this;
2368 bool installed_breakpoint::uninstall(int_process *proc)
2372 if (proc->getState() != int_process::exited)
2374 bool result = proc->writeMem(&buffer, addr, buffer_size);
2376 pthrd_printf("Failed to remove breakpoint at %lx from process %d\n",
2377 addr, proc->getPid());
2384 std::map<Dyninst::Address, installed_breakpoint *>::iterator i;
2385 i = memory->breakpoints.find(addr);
2386 if (i == memory->breakpoints.end()) {
2387 perr_printf("Failed to remove breakpoint from list\n");
2390 memory->breakpoints.erase(i);
2395 bool installed_breakpoint::suspend(int_process *proc)
2398 if (suspend_count > 1) {
2399 pthrd_printf("Breakpoint already suspended, suspend_count = %d\n",
2403 bool result = proc->writeMem(&buffer, addr, buffer_size);
2405 pthrd_printf("Failed to suspend breakpoint at %lx from process %d\n",
2406 addr, proc->getPid());
2412 bool installed_breakpoint::resume(int_process *proc)
2415 assert(suspend_count >= 0);
2416 if (suspend_count > 0) {
2417 pthrd_printf("Breakpoint remaining suspended, suspend_count = %d\n", suspend_count);
2420 bool result = plat_install(proc, false);
2422 pthrd_printf("Failed to install breakpoint at %lx in process %d\n",
2423 addr, proc->getPid());
2429 bool installed_breakpoint::addBreakpoint(int_process *proc, int_breakpoint *bp)
2431 pthrd_printf("Adding breakpoint at %lx\n", addr);
2432 if (bp->isCtrlTransfer()) {
2433 for (set<int_breakpoint *>::iterator i = bps.begin(); i != bps.end(); i++)
2435 if ((*i)->isCtrlTransfer()) {
2436 perr_printf("Error. Attempted to add to control transfer breakpoints "
2438 setLastError(err_badparam, "Attempted two control transfer breakpoints at "
2446 Breakpoint::ptr upbp = bp->upBreakpoint().lock();
2447 if (upbp != Breakpoint::ptr()) {
2448 //We keep the set of installed Breakpoints to keep the breakpoint shared
2449 // pointer reference counter from cleaning a Breakpoint while installed.
2450 hl_bps.insert(upbp);
2454 bool result = install(proc);
2456 pthrd_printf("Error failed to install breakpoint at %lx\n", addr);
2464 bool installed_breakpoint::rmBreakpoint(int_process *proc, int_breakpoint *bp, bool &empty)
2466 pthrd_printf("Removing breakpoint at %lx\n", addr);
2468 set<int_breakpoint *>::iterator i = bps.find(bp);
2469 if (i == bps.end()) {
2470 perr_printf("Attempted to remove a non-installed breakpoint\n");
2471 setLastError(err_badparam, "Breakpoint was not installed in process\n");
2477 set<Breakpoint::ptr>::iterator j = hl_bps.find(bp->upBreakpoint().lock());
2478 if (j != hl_bps.end()) {
2484 bool result = uninstall(proc);
2486 perr_printf("Failed to remove breakpoint at %lx\n", addr);
2487 setLastError(err_internal, "Could not remove breakpoint\n");
2495 Dyninst::Address installed_breakpoint::getAddr() const
2500 int_library::int_library(std::string n, Dyninst::Address load_addr) :
2502 load_address(load_address),
2503 data_load_address(0),
2504 has_data_load(false),
2507 up_lib = new Library();
2511 int_library::int_library(std::string n, Dyninst::Address load_addr, Dyninst::Address data_load_addr) :
2513 load_address(load_addr),
2514 data_load_address(data_load_addr),
2515 has_data_load(true),
2518 up_lib = new Library();
2522 int_library::int_library(int_library *l) :
2524 load_address(l->load_address),
2525 data_load_address(l->data_load_address),
2526 has_data_load(l->has_data_load),
2529 up_lib = new Library();
2533 int_library::~int_library()
2537 std::string int_library::getName()
2542 Dyninst::Address int_library::getAddr()
2544 return load_address;
2547 Dyninst::Address int_library::getDataAddr()
2549 return data_load_address;
2552 bool int_library::hasDataAddr()
2554 return has_data_load;
2557 void int_library::setMark(bool b)
2562 bool int_library::isMarked() const
2567 Library::ptr int_library::getUpPtr() const
2572 mem_state::mem_state(int_process *proc)
2577 mem_state::mem_state(mem_state &m, int_process *p)
2579 pthrd_printf("Copying mem_state to new process %d\n", p->getPid());
2581 set<int_library *>::iterator i;
2582 for (i = m.libs.begin(); i != m.libs.end(); i++)
2584 int_library *orig_lib = *i;
2585 int_library *new_lib = new int_library(orig_lib);
2586 libs.insert(new_lib);
2588 map<Dyninst::Address, installed_breakpoint *>::iterator j;
2589 for (j = m.breakpoints.begin(); j != m.breakpoints.end(); j++)
2591 Address orig_addr = j->first;
2592 installed_breakpoint *orig_bp = j->second;
2593 installed_breakpoint *new_bp = new installed_breakpoint(this, orig_bp);
2594 breakpoints[orig_addr] = new_bp;
2596 inf_malloced_memory = m.inf_malloced_memory;
2599 mem_state::~mem_state()
2601 pthrd_printf("Destroy memory image of old process\n");
2602 set<int_library *>::iterator i;
2603 for (i = libs.begin(); i != libs.end(); i++)
2605 int_library *lib = *i;
2610 map<Dyninst::Address, installed_breakpoint *>::iterator j;
2611 for (j = breakpoints.begin(); j != breakpoints.end(); j++)
2613 installed_breakpoint *ibp = j->second;
2616 breakpoints.clear();
2619 void mem_state::addProc(int_process *p)
2621 pthrd_printf("Adding process %d as sharing a memory state with existing proc\n",
2626 void mem_state::rmProc(int_process *p, bool &should_clean)
2628 set<int_process *>::iterator i = procs.find(p);
2629 assert(i != procs.end());
2632 if (procs.empty()) {
2633 should_clean = true;
2634 pthrd_printf("Removed process %d from memory image, should clean image\n",
2638 should_clean = false;
2639 pthrd_printf("Removed process %d from memory image, others remain\n",
2645 int_notify *int_notify::the_notify = NULL;
2646 int_notify::int_notify() :
2653 up_notify = new EventNotify();
2654 up_notify->llnotify = this;
2657 int_notify *notify()
2659 if (int_notify::the_notify)
2660 return int_notify::the_notify;
2662 static Mutex init_lock;
2664 if (!int_notify::the_notify) {
2665 int_notify::the_notify = new int_notify();
2668 return int_notify::the_notify;
2671 void int_notify::noteEvent()
2673 assert(isHandlerThread());
2674 assert(events_noted == 0);
2677 pthrd_printf("noteEvent - %d\n", events_noted);
2678 set<EventNotify::notify_cb_t>::iterator i;
2679 for (i = cbs.begin(); i != cbs.end(); i++) {
2680 pthrd_printf("Calling notification CB\n");
2685 static void notifyNewEvent()
2687 notify()->noteEvent();
2691 void int_notify::clearEvent()
2693 assert(!isHandlerThread());
2695 pthrd_printf("clearEvent - %d\n", events_noted);
2696 assert(events_noted == 0);
2700 bool int_notify::hasEvents()
2702 return (events_noted > 0);
2705 void int_notify::registerCB(EventNotify::notify_cb_t cb)
2710 void int_notify::removeCB(EventNotify::notify_cb_t cb)
2712 set<EventNotify::notify_cb_t>::iterator i = cbs.find(cb);
2718 int int_notify::getPipeIn()
2733 RegisterPool::RegisterPool()
2735 llregpool = new int_registerPool();
2738 RegisterPool::RegisterPool(const RegisterPool &rp)
2740 llregpool = new int_registerPool();
2741 *llregpool = *rp.llregpool;
2744 RegisterPool::~RegisterPool()
2749 RegisterPool::iterator RegisterPool::begin()
2751 return RegisterPool::iterator(llregpool->regs.begin());
2754 RegisterPool::iterator RegisterPool::end()
2756 return RegisterPool::iterator(llregpool->regs.end());
2759 RegisterPool::iterator RegisterPool::find(MachRegister r)
2761 return RegisterPool::iterator(llregpool->regs.find(r));
2764 RegisterPool::const_iterator RegisterPool::begin() const
2766 return RegisterPool::const_iterator(llregpool->regs.begin());
2769 RegisterPool::const_iterator RegisterPool::end() const
2771 return RegisterPool::const_iterator(llregpool->regs.end());
2774 RegisterPool::const_iterator RegisterPool::find(MachRegister r) const
2776 return RegisterPool::const_iterator(llregpool->regs.find(r));
2779 MachRegisterVal& RegisterPool::operator[](MachRegister r)
2781 return llregpool->regs[r];
2784 const MachRegisterVal& RegisterPool::operator[](MachRegister r) const
2786 return llregpool->regs[r];
2789 size_t RegisterPool::size() const
2791 return llregpool->regs.size();
2794 Thread::ptr RegisterPool::getThread() const
2796 return llregpool->thread->thread();
2799 RegisterPool::iterator::iterator()
2803 RegisterPool::iterator::iterator(int_iter i_) :
2808 RegisterPool::iterator::~iterator()
2812 std::pair<Dyninst::MachRegister, Dyninst::MachRegisterVal> RegisterPool::iterator::operator*()
2817 RegisterPool::iterator RegisterPool::iterator::operator++()
2821 return RegisterPool::iterator(i);
2824 RegisterPool::iterator RegisterPool::iterator::operator++(int)
2830 RegisterPool::const_iterator::const_iterator()
2834 RegisterPool::const_iterator::const_iterator(int_iter i_) :
2839 RegisterPool::const_iterator::~const_iterator()
2843 std::pair<Dyninst::MachRegister, Dyninst::MachRegisterVal> RegisterPool::const_iterator::operator*() const
2848 RegisterPool::const_iterator RegisterPool::const_iterator::operator++()
2852 return RegisterPool::const_iterator(i);
2855 RegisterPool::const_iterator RegisterPool::const_iterator::operator++(int)
2862 int_registerPool::int_registerPool() :
2868 int_registerPool::int_registerPool(const int_registerPool &c) :
2875 int_registerPool::~int_registerPool()
2891 std::string Library::getName() const
2893 return lib->getName();
2896 Dyninst::Address Library::getLoadAddress() const
2898 return lib->getAddr();
2901 Dyninst::Address Library::getDataLoadAddress() const
2903 return lib->getDataAddr();
2906 LibraryPool::LibraryPool()
2910 LibraryPool::~LibraryPool()
2914 size_t LibraryPool::size() const
2916 return proc->numLibs();
2919 Library::ptr LibraryPool::getLibraryByName(std::string s)
2921 int_library *int_lib = proc->getLibraryByName(s);
2924 return int_lib->up_lib;
2927 const Library::ptr LibraryPool::getLibraryByName(std::string s) const
2929 int_library *int_lib = proc->getLibraryByName(s);
2932 return int_lib->up_lib;
2935 LibraryPool::iterator::iterator()
2939 LibraryPool::iterator::~iterator()
2943 Library::ptr LibraryPool::iterator::operator*() const
2945 return (*int_iter)->up_lib;
2948 LibraryPool::iterator LibraryPool::iterator::operator++()
2950 LibraryPool::iterator orig = *this;
2955 LibraryPool::iterator LibraryPool::iterator::operator++(int)
2961 LibraryPool::iterator LibraryPool::begin()
2963 LibraryPool::iterator i;
2964 i.int_iter = proc->memory()->libs.begin();
2968 LibraryPool::iterator LibraryPool::end()
2970 LibraryPool::iterator i;
2971 i.int_iter = proc->memory()->libs.end();
2975 bool LibraryPool::iterator::operator==(const LibraryPool::iterator &i) const
2977 return int_iter == i.int_iter;
2980 bool LibraryPool::iterator::operator!=(const LibraryPool::iterator &i) const
2982 return int_iter != i.int_iter;
2985 LibraryPool::const_iterator LibraryPool::begin() const
2987 LibraryPool::const_iterator i;
2988 i.int_iter = proc->memory()->libs.begin();
2992 LibraryPool::const_iterator LibraryPool::end() const
2994 LibraryPool::const_iterator i;
2995 i.int_iter = proc->memory()->libs.end();
2999 LibraryPool::const_iterator::const_iterator()
3003 LibraryPool::const_iterator::~const_iterator()
3007 const Library::const_ptr LibraryPool::const_iterator::operator*() const
3009 return (*int_iter)->up_lib;
3012 bool LibraryPool::const_iterator::operator==(const LibraryPool::const_iterator &i)
3014 return int_iter == i.int_iter;
3017 bool LibraryPool::const_iterator::operator!=(const LibraryPool::const_iterator &i)
3019 return int_iter != i.int_iter;
3022 LibraryPool::const_iterator LibraryPool::const_iterator::operator++()
3024 LibraryPool::const_iterator orig = *this;
3029 LibraryPool::const_iterator LibraryPool::const_iterator::operator++(int)
3035 bool Process::registerEventCallback(EventType evt, Process::cb_func_t cbfunc)
3037 MTLock lock_this_func(MTLock::allow_init);
3038 HandleCallbacks *cbhandler = HandleCallbacks::getCB();
3039 return cbhandler->registerCallback(evt, cbfunc);
3042 bool Process::removeEventCallback(EventType evt, cb_func_t cbfunc)
3044 MTLock lock_this_func(MTLock::allow_init);
3045 HandleCallbacks *cbhandler = HandleCallbacks::getCB();
3046 return cbhandler->removeCallback(evt, cbfunc);
3049 bool Process::removeEventCallback(EventType evt)
3051 MTLock lock_this_func(MTLock::allow_init);
3052 HandleCallbacks *cbhandler = HandleCallbacks::getCB();
3053 return cbhandler->removeCallback(evt);
3056 bool Process::removeEventCallback(cb_func_t cbfunc)
3058 MTLock lock_this_func(MTLock::allow_init);
3059 HandleCallbacks *cbhandler = HandleCallbacks::getCB();
3060 return cbhandler->removeCallback(cbfunc);
3063 bool Process::handleEvents(bool block)
3065 // One might think we should be delivering callbacks when taking this lock,
3066 // but we'll do it under waitAndHandleEvent instead.
3067 MTLock lock_this_func(MTLock::allow_init);
3069 pthrd_printf("User triggered event handling\n");
3070 if (int_process::isInCB()) {
3071 perr_printf("User attempted call on process while in CB, erroring.");
3072 setLastError(err_incallback, "Cannot handleEvents from callback\n");
3076 bool result = int_process::waitAndHandleEvents(block);
3078 pthrd_printf("Error handling events for user\n");
3084 bool Process::setThreadingMode(thread_mode_t tm)
3086 MTLock lock_this_func(MTLock::allow_init);
3087 return mt()->setThreadMode(tm);
3090 Process::ptr Process::createProcess(std::string executable, const std::vector<std::string> &argv)
3092 MTLock lock_this_func(MTLock::allow_init, MTLock::deliver_callbacks);
3094 pthrd_printf("User asked to launch executable %s\n", executable.c_str());
3095 if (int_process::isInCB()) {
3096 perr_printf("User attempted call on process create while in CB, erroring.");
3097 setLastError(err_incallback, "Cannot createProcess from callback\n");
3098 return Process::ptr();
3101 Process::ptr newproc(new Process());
3102 int_process *llproc = int_process::createProcess(executable, argv);
3103 llproc->initializeProcess(newproc);
3105 bool result = llproc->create();
3107 pthrd_printf("Unable to create process %s\n", executable.c_str());
3108 return Process::ptr();
3114 Process::ptr Process::attachProcess(Dyninst::PID pid, std::string executable)
3116 MTLock lock_this_func(MTLock::allow_init, MTLock::deliver_callbacks);
3117 pthrd_printf("User asked to attach to process %d (%s)\n", pid, executable.c_str());
3118 if (int_process::isInCB()) {
3119 perr_printf("User attempted call on process attach while in CB, erroring.\n");
3120 setLastError(err_incallback, "Cannot attachProcess from callback\n");
3121 return Process::ptr();
3123 Process::ptr newproc(new Process());
3124 int_process *llproc = int_process::createProcess(pid, executable);
3125 llproc->initializeProcess(newproc);
3127 bool result = llproc->attach();
3129 pthrd_printf("Unable to attach to process %d\n", pid);
3131 return Process::ptr();
3137 Process::Process() :
3152 Dyninst::PID Process::getPid() const
3154 MTLock lock_this_func(MTLock::allow_generator);
3157 return exitstate_->pid;
3159 return llproc_->getPid();
3162 int_process *Process::llproc() const
3167 const ThreadPool &Process::threads() const
3169 MTLock lock_this_func;
3170 static ThreadPool *err_pool;
3172 perr_printf("threads on deleted process\n");
3173 setLastError(err_exited, "Process is exited\n");
3175 err_pool = new ThreadPool();
3180 return *(llproc_->threadPool()->pool());
3183 ThreadPool &Process::threads()
3185 MTLock lock_this_func;
3186 static ThreadPool *err_pool;
3188 perr_printf("threads on deleted process\n");
3189 setLastError(err_exited, "Process is exited\n");
3191 err_pool = new ThreadPool();
3196 return *(llproc_->threadPool()->pool());
3199 const LibraryPool &Process::libraries() const
3201 MTLock lock_this_func;
3202 static LibraryPool *err_pool;
3204 perr_printf("libraries on deleted process\n");
3205 setLastError(err_exited, "Process is exited\n");
3207 err_pool = new LibraryPool();
3212 return llproc_->libpool;
3215 LibraryPool &Process::libraries()
3217 MTLock lock_this_func;
3218 static LibraryPool *err_pool;
3220 perr_printf("libraries on deleted process\n");
3221 setLastError(err_exited, "Process is exited\n");
3223 err_pool = new LibraryPool();
3228 return llproc_->libpool;
3231 bool Process::continueProc()
3233 MTLock lock_this_func(MTLock::deliver_callbacks);
3235 perr_printf("coninueProc on deleted process\n");
3236 setLastError(err_exited, "Process is exited\n");
3240 pthrd_printf("User continuing entire process %d\n", getPid());
3241 if (int_process::isInCB()) {
3242 perr_printf("User attempted call on process while in CB, erroring.");
3243 setLastError(err_incallback, "Cannot continueProc from callback\n");
3247 return llproc_->threadPool()->userCont();
3250 bool Process::isCrashed() const
3252 MTLock lock_this_func;
3255 return exitstate_->crashed;
3257 int crashSignal = 0;
3258 return (llproc_->getState() == int_process::exited && llproc_->getCrashSignal(crashSignal));
3261 bool Process::isExited() const
3263 MTLock lock_this_func;
3267 return exitstate_->exited;
3269 return (llproc_->getState() == int_process::exited && llproc_->getExitCode(exitCode));
3272 int Process::getCrashSignal() const
3274 MTLock lock_this_func;
3277 return exitstate_->crashed ? exitstate_->crash_signal : 0;
3280 int crashSignal = 0;
3281 if (!(llproc_->getState() == int_process::exited && llproc_->getCrashSignal(crashSignal)))
3286 int Process::getExitCode() const
3288 MTLock lock_this_func;
3291 return !exitstate_->crashed ? exitstate_->exit_code : 0;
3295 if (!(llproc_->getState() == int_process::exited && llproc_->getExitCode(exitCode)))
3300 bool Process::stopProc()
3302 MTLock lock_this_func(MTLock::deliver_callbacks);
3304 perr_printf("stopProc on deleted process\n");
3305 setLastError(err_exited, "Process is exited\n");
3309 pthrd_printf("User stopping entire process %d\n", getPid());
3310 if (int_process::isInCB()) {
3311 perr_printf("User attempted call on process while in CB, erroring.");
3312 setLastError(err_incallback, "Cannot continueProc from callback\n");
3316 return llproc_->threadPool()->userStop();
3319 bool Process::detach()
3321 MTLock lock_this_func(MTLock::deliver_callbacks);
3323 perr_printf("detach on deleted process\n");
3324 setLastError(err_exited, "Process is exited\n");
3328 bool result = llproc_->detach(should_delete);
3330 pthrd_printf("Failed to detach from process\n");
3333 if (should_delete) {
3334 HandlerPool *hp = llproc_->handlerPool();
3342 bool Process::terminate()
3344 MTLock lock_this_func(MTLock::deliver_callbacks);
3346 perr_printf("terminate on deleted process\n");
3347 setLastError(err_exited, "Process is exited\n");
3351 pthrd_printf("User terminating process %d\n", llproc_->getPid());
3352 bool needsSync = false;
3353 bool result = llproc_->terminate(needsSync);
3355 pthrd_printf("Terminating process %d failed\n", llproc_->getPid());
3360 bool proc_exited = false;
3361 while (!proc_exited) {
3362 bool result = int_process::waitAndHandleForProc(true, llproc(), proc_exited);
3364 perr_printf("Error waiting for process to terminate\n");
3370 HandlerPool *hp = llproc_->handlerPool();
3379 bool Process::isTerminated() const
3381 MTLock lock_this_func;
3385 return (llproc_->getState() == int_process::exited);
3388 bool Process::hasStoppedThread() const
3390 MTLock lock_this_func;
3392 perr_printf("hasStoppedThread on deleted process\n");
3393 setLastError(err_exited, "Process is exited\n");
3397 int_threadPool::iterator i;
3398 for (i = llproc_->threadPool()->begin(); i != llproc_->threadPool()->end(); i++) {
3399 if ((*i)->getUserState() == int_thread::stopped)
3405 bool Process::hasRunningThread() const
3407 MTLock lock_this_func;
3409 perr_printf("hasRunningThread on deleted process\n");
3410 setLastError(err_exited, "Process is exited\n");
3414 int_threadPool::iterator i;
3415 for (i = llproc_->threadPool()->begin(); i != llproc_->threadPool()->end(); i++) {
3416 if ((*i)->getUserState() == int_thread::running)
3422 bool Process::allThreadsStopped() const
3424 MTLock lock_this_func;
3426 perr_printf("allThreadsStopped on deleted process\n");
3427 setLastError(err_exited, "Process is exited\n");
3431 int_threadPool::iterator i;
3432 for (i = llproc_->threadPool()->begin(); i != llproc_->threadPool()->end(); i++) {
3433 if ((*i)->getUserState() == int_thread::running)
3439 bool Process::allThreadsRunning() const
3441 MTLock lock_this_func;
3443 perr_printf("allThreadsRunning on deleted process\n");
3444 setLastError(err_exited, "Process is exited\n");
3448 int_threadPool::iterator i;
3449 for (i = llproc_->threadPool()->begin(); i != llproc_->threadPool()->end(); i++) {
3450 if ((*i)->getUserState() == int_thread::stopped)
3456 Thread::ptr Process::postIRPC(IRPC::ptr irpc) const
3458 MTLock lock_this_func;
3460 perr_printf("postIRPC on deleted process\n");
3461 setLastError(err_exited, "Process is exited\n");
3462 return Thread::ptr();
3465 int_process *proc = llproc();
3466 int_iRPC::ptr rpc = irpc->llrpc()->rpc;
3467 bool result = rpcMgr()->postRPCToProc(proc, rpc);
3469 pthrd_printf("postRPCToProc failed on %d\n", proc->getPid());
3470 return Thread::ptr();
3473 if (int_process::in_callback) {
3474 pthrd_printf("Returning from postIRPC in callback\n");
3475 return rpc->thread()->thread();
3477 int_thread *thr = rpc->thread();
3478 if (thr->getInternalState() == int_thread::running) {
3479 //The thread is running, let's go ahead and start the RPC going.
3480 bool result = thr->handleNextPostedIRPC(true);
3482 pthrd_printf("handleNextPostedIRPC failed\n");
3483 return Thread::ptr();
3486 return thr->thread();
3489 bool Process::getPostedIRPCs(std::vector<IRPC::ptr> &rpcs) const
3491 MTLock lock_this_func;
3493 perr_printf("postIRPC on deleted process\n");
3494 setLastError(err_exited, "Process is exited\n");
3497 int_threadPool *tp = llproc()->threadPool();
3498 for (int_threadPool::iterator i = tp->begin(); i != tp->end(); i++)
3500 int_thread *thr = *i;
3501 rpc_list_t *rpc_list = thr->getPostedRPCs();
3502 for (rpc_list_t::iterator j = rpc_list->begin(); j != rpc_list->end(); j++) {
3503 IRPC::ptr up_rpc = (*j)->getIRPC().lock();
3504 if (up_rpc == IRPC::ptr())
3506 rpcs.push_back(up_rpc);
3512 Dyninst::Architecture Process::getArchitecture() const
3514 MTLock lock_this_func;
3516 perr_printf("getArchitecture on deleted process\n");
3517 setLastError(err_exited, "Process is exited\n");
3518 return Dyninst::Arch_none;
3520 return llproc_->getTargetArch();
3523 Dyninst::Address Process::mallocMemory(size_t size, Dyninst::Address addr)
3525 MTLock lock_this_func(MTLock::deliver_callbacks);
3527 perr_printf("mallocMemory on deleted process\n");
3528 setLastError(err_exited, "Process is exited\n");
3532 if (int_process::isInCB()) {
3533 perr_printf("User attempted call on process while in CB, erroring.");
3534 setLastError(err_incallback, "Cannot mallocMemory from callback\n");
3537 return llproc_->infMalloc(size, true, addr);
3540 Dyninst::Address Process::mallocMemory(size_t size)
3542 MTLock lock_this_func(MTLock::deliver_callbacks);
3544 perr_printf("mallocMemory on deleted process\n");
3545 setLastError(err_exited, "Process is exited\n");
3549 if (int_process::isInCB()) {
3550 perr_printf("User attempted call on process while in CB, erroring.");
3551 setLastError(err_incallback, "Cannot mallocMemory from callback\n");
3554 return llproc_->infMalloc(size, false, 0x0);
3557 bool Process::freeMemory(Dyninst::Address addr)
3559 MTLock lock_this_func(MTLock::deliver_callbacks);
3561 perr_printf("freeMemory on deleted process\n");
3562 setLastError(err_exited, "Process is exited\n");
3566 if (int_process::isInCB()) {
3567 perr_printf("User attempted call on process while in CB, erroring.");
3568 setLastError(err_incallback, "Cannot freeMemory from callback\n");
3571 return llproc_->infFree(addr);
3574 bool Process::writeMemory(Dyninst::Address addr, void *buffer, size_t size) const
3576 MTLock lock_this_func;
3578 perr_printf("writeMemory on deleted process\n");
3579 setLastError(err_exited, "Process is exited\n");
3583 pthrd_printf("User wants to write memory from 0x%lx to 0x%p of size %lu\n",
3584 addr, buffer, (unsigned long) size);
3585 return llproc_->writeMem(buffer, addr, size);
3588 bool Process::readMemory(void *buffer, Dyninst::Address addr, size_t size) const
3590 MTLock lock_this_func;
3592 perr_printf("readMemory on deleted process\n");
3593 setLastError(err_exited, "Process is exited\n");
3597 pthrd_printf("User wants to read memory from 0x%lx to 0x%p of size %lu\n",
3598 addr, buffer, (unsigned long) size);
3599 return llproc_->readMem(buffer, addr, size);
3602 bool Process::addBreakpoint(Address addr, Breakpoint::ptr bp) const
3604 MTLock lock_this_func;
3606 perr_printf("addBreakpoint on deleted process\n");
3607 setLastError(err_exited, "Process is exited\n");
3611 if (hasRunningThread()) {
3612 perr_printf("User attempted to add breakpoint to running process\n");
3613 setLastError(err_notstopped, "Attempted to insert breakpoint into running process\n");
3616 return llproc_->addBreakpoint(addr, bp->llbp());
3619 bool Process::rmBreakpoint(Dyninst::Address addr, Breakpoint::ptr bp) const
3621 MTLock lock_this_func;
3623 perr_printf("rmBreakpoint on deleted process\n");
3624 setLastError(err_exited, "Process is exited\n");
3628 if (hasRunningThread()) {
3629 perr_printf("User attempted to add breakpoint to running process\n");
3630 setLastError(err_notstopped, "Attempted to insert breakpoint into running process\n");
3633 return llproc_->rmBreakpoint(addr, bp->llbp());
3650 Process::ptr Thread::getProcess() const
3652 MTLock lock_this_func;
3655 return exitstate_->proc_ptr;
3657 return llthread_->proc();
3660 int_thread *Thread::llthrd() const
3665 bool Thread::isStopped() const
3667 MTLock lock_this_func;
3669 perr_printf("isStopped called on exited thread\n");
3670 setLastError(err_exited, "Thread is exited\n");
3673 return llthread_->getUserState() == int_thread::stopped;
3676 bool Thread::isRunning() const
3678 MTLock lock_this_func;
3680 perr_printf("isRunning called on exited thread\n");
3681 setLastError(err_exited, "Thread is exited\n");
3684 return llthread_->getUserState() == int_thread::running;
3687 bool Thread::isLive() const
3689 MTLock lock_this_func;
3693 return (llthread_->getUserState() == int_thread::stopped ||
3694 llthread_->getUserState() == int_thread::running);
3697 bool Thread::stopThread()
3699 MTLock lock_this_func(MTLock::deliver_callbacks);
3701 perr_printf("stopThread called on exited thread\n");
3702 setLastError(err_exited, "Thread is exited\n");
3706 if (int_process::isInCB()) {
3707 perr_printf("User attempted continue call on thread while in CB, erroring.");
3708 setLastError(err_incallback, "Cannot continueThread from callback\n");
3712 return llthread_->userStop();
3715 bool Thread::continueThread()
3717 MTLock lock_this_func(MTLock::deliver_callbacks);
3719 perr_printf("continueThread called on exited thread\n");
3720 setLastError(err_exited, "Thread is exited\n");
3724 if (int_process::isInCB()) {
3725 perr_printf("User attempted continue call on thread while in CB, erroring.");
3726 setLastError(err_incallback, "Cannot continueThread from callback\n");
3730 return llthread_->userCont();
3733 bool Thread::getAllRegisters(RegisterPool &pool) const
3735 MTLock lock_this_func;
3737 perr_printf("getAllRegisters called on exited thread\n");
3738 setLastError(err_exited, "Thread is exited\n");
3742 if (llthread_->getUserState() != int_thread::stopped) {
3743 setLastError(err_notstopped, "Thread must be stopped before getting registers");
3744 perr_printf("User called getAllRegisters on running thread %d\n", llthread_->getLWP());
3747 return llthread_->getAllRegisters(*pool.llregpool);
3750 bool Thread::setAllRegisters(RegisterPool &pool) const
3752 MTLock lock_this_func;
3754 perr_printf("setAllRegisters called on exited thread\n");
3755 setLastError(err_exited, "Thread is exited\n");
3758 if (llthread_->getUserState() != int_thread::stopped) {
3759 setLastError(err_notstopped, "Thread must be stopped before setting registers");
3760 perr_printf("User called setAllRegisters on running thread %d\n", llthread_->getLWP());
3763 return llthread_->setAllRegisters(*pool.llregpool);
3766 bool Thread::getRegister(Dyninst::MachRegister reg, Dyninst::MachRegisterVal &val) const
3768 MTLock lock_this_func;
3770 perr_printf("getRegister called on exited thread\n");
3771 setLastError(err_exited, "Thread is exited\n");
3774 if (llthread_->getUserState() != int_thread::stopped) {
3775 setLastError(err_notstopped, "Thread must be stopped before getting registers");
3776 perr_printf("User called getRegister on running thread %d\n", llthread_->getLWP());
3779 return llthread_->getRegister(reg, val);
3782 bool Thread::setRegister(Dyninst::MachRegister reg, Dyninst::MachRegisterVal val) const
3784 MTLock lock_this_func;
3786 perr_printf("setRegister called on exited thread\n");
3787 setLastError(err_exited, "Thread is exited\n");
3790 if (llthread_->getUserState() != int_thread::stopped) {
3791 setLastError(err_notstopped, "Thread must be stopped before setting registers");
3792 perr_printf("User called setRegister on running thread %d\n", llthread_->getLWP());
3795 return llthread_->setRegister(reg, val);
3798 bool Thread::isInitialThread() const
3800 MTLock lock_this_func;
3802 perr_printf("isInitialThrad called on exited thread\n");
3803 setLastError(err_exited, "Thread is exited\n");
3806 return llthread_->llproc()->threadPool()->initialThread() == llthread_;
3809 void Thread::setSingleStepMode(bool s) const
3811 MTLock lock_this_func;
3813 perr_printf("setSingleStepMode called on exited thread\n");
3814 setLastError(err_exited, "Thread is exited\n");
3817 llthread_->setSingleStepUserMode(s);
3820 bool Thread::getSingleStepMode() const
3822 MTLock lock_this_func;
3824 perr_printf("getSingleStepMode called on exited thread\n");
3825 setLastError(err_exited, "Thread is exited\n");
3828 return llthread_->singleStepUserMode();
3831 Dyninst::LWP Thread::getLWP() const
3833 MTLock lock_this_func;
3836 return exitstate_->lwp;
3838 return llthread_->getLWP();
3841 bool Thread::postIRPC(IRPC::ptr irpc) const
3843 MTLock lock_this_func;
3845 perr_printf("postIRPC on deleted thread\n");
3846 setLastError(err_exited, "Thread is exited\n");
3850 int_thread *thr = llthread_;
3851 int_process *proc = thr->llproc();
3852 int_iRPC::ptr rpc = irpc->llrpc()->rpc;
3853 bool result = rpcMgr()->postRPCToThread(thr, rpc);
3855 pthrd_printf("postRPCToThread failed on %d\n", proc->getPid());
3859 if (int_process::isInCallback()) {
3860 pthrd_printf("Returning from postIRPC in callback\n");
3863 if (thr->getInternalState() == int_thread::running) {
3864 //The thread is running, let's go ahead and start the RPC going.
3865 bool result = thr->handleNextPostedIRPC(true);
3867 pthrd_printf("handleNextPostedIRPC failed\n");
3874 bool Thread::getPostedIRPCs(std::vector<IRPC::ptr> &rpcs) const
3876 MTLock lock_this_func;
3878 perr_printf("postIRPC on deleted thread\n");
3879 setLastError(err_exited, "Thread is exited\n");
3882 rpc_list_t *rpc_list = llthread_->getPostedRPCs();
3883 for (rpc_list_t::iterator j = rpc_list->begin(); j != rpc_list->end(); j++) {
3884 IRPC::ptr up_rpc = (*j)->getIRPC().lock();
3885 if (up_rpc == IRPC::ptr())
3887 rpcs.push_back(up_rpc);
3892 IRPC::const_ptr Thread::getRunningIRPC() const
3894 MTLock lock_this_func;
3896 perr_printf("getRunningIRPC on deleted thread\n");
3897 setLastError(err_exited, "Thread is exited\n");
3898 return IRPC::const_ptr();
3900 int_iRPC::ptr running = llthread_->runningRPC();
3901 if (running == int_iRPC::ptr())
3902 return IRPC::const_ptr();
3903 IRPC::ptr irpc = running->getIRPC().lock();
3907 ThreadPool::ThreadPool()
3911 ThreadPool::~ThreadPool()
3915 Thread::ptr ThreadPool::getInitialThread()
3917 return threadpool->initialThread()->thread();
3920 const Thread::ptr ThreadPool::getInitialThread() const
3922 return threadpool->initialThread()->thread();
3925 ThreadPool::iterator::iterator()
3929 curh = Thread::ptr();
3932 ThreadPool::iterator::~iterator()
3936 bool ThreadPool::iterator::operator==(const iterator &i)
3938 return (i.curh == curh);
3941 bool ThreadPool::iterator::operator!=(const iterator &i)
3943 return (i.curh != curh);
3946 Thread::ptr ThreadPool::iterator::operator*() const
3948 MTLock lock_this_func;
3950 assert(curi >= 0 && curi < (signed) curp->hl_threads.size());