A first pass at implementing thread stop and continue on FreeBSD.
[dyninst.git] / proccontrol / src / handler.C
1 #include "proccontrol/h/Handler.h"
2 #include "proccontrol/h/PCErrors.h"
3 #include "proccontrol/h/Process.h"
4 #include "proccontrol/src/int_process.h"
5 #include "proccontrol/src/int_handler.h"
6 #include "proccontrol/src/procpool.h"
7 #include "proccontrol/src/irpc.h"
8 #include "dynutil/h/dyn_regs.h"
9
10 using namespace Dyninst;
11
12 #include <assert.h>
13
14 Handler::Handler(std::string name_) :
15    name(name_)
16 {
17 }
18
19 Handler::~Handler()
20 {
21 }
22
23 int Handler::getPriority() const
24 {
25    return DefaultPriority;
26 }
27
28 Event::ptr Handler::convertEventForCB(Event::ptr /*orig*/)
29 {
30    return Event::ptr();
31 }
32
33 std::string Handler::getName() const
34 {
35    return name;
36 }
37
38 HandlerPool::HandlerPool()
39 {
40 }
41
42 HandlerPool::~HandlerPool()
43 {
44    for (HandlerMap_t::iterator i = handlers.begin(); i != handlers.end(); i++) {
45       delete (*i).second;
46    }
47    //Do not delete actual Handler* objects.
48    handlers.clear();
49 }
50
51 void HandlerPool::addHandlerInt(EventType etype, Handler *handler)
52 {
53    pthrd_printf("Handler %s will handle event %s\n", handler->getName().c_str(), 
54                 etype.name().c_str());
55    assert(etype.time() != EventType::Any);
56    HandlerMap_t::iterator i = handlers.find(etype);
57    HandlerSet_t *theset = NULL;
58    if (i == handlers.end()) {
59       theset = new HandlerSet_t();
60       handlers[etype] = theset;
61    }
62    else {
63       theset = (*i).second;
64    }   
65    theset->insert(handler);
66
67    HandleCallbacks *cb = HandleCallbacks::getCB();
68    if (cb != handler) {
69       cb->alleventtypes.insert(etype);
70       if (!(etype.code() >= EventType::InternalEvents && etype.code() < EventType::MaxProcCtrlEvent))
71          addHandlerInt(etype, cb);
72    }
73 }
74
75 void HandlerPool::addHandler(Handler *handler)
76 {
77    std::vector<EventType> etypes;
78    handler->getEventTypesHandled(etypes);
79
80    for (std::vector<EventType>::iterator i = etypes.begin(); i != etypes.end(); i++)
81    {
82       if ((*i).time() == EventType::Any) {
83          addHandlerInt(EventType(EventType::Pre, (*i).code()), handler);
84          addHandlerInt(EventType(EventType::Post, (*i).code()), handler);
85          addHandlerInt(EventType(EventType::None, (*i).code()), handler);
86       }
87       else {
88          addHandlerInt(*i, handler);
89       }
90    }
91 }
92
93 struct eh_cmp_func
94 {
95    bool operator()(const pair<Event::ptr, Handler*> &a,
96                    const pair<Event::ptr, Handler*> &b)
97    {
98       if (a.second->getPriority() != b.second->getPriority())
99          return a.second->getPriority() < b.second->getPriority();
100       if (a.first->subservientTo().lock() == b.first)
101          return false;
102       if (b.first->subservientTo().lock() == a.first)
103          return true;
104       eventtype_cmp cmp;
105       return cmp(a.first->getEventType(), b.first->getEventType());
106    }
107 };
108
109 bool HandlerPool::handleEvent(Event::ptr ev)
110 {
111    EventType etype = ev->getEventType();
112    Event::ptr cb_replacement_ev = Event::ptr();
113    
114    /**
115     * An event and its subservient events are a set of events that
116     * are processed at the same time.  As an example, on SysV systems
117     * a Library event is triggered by a breakpoint.  We want to do both the
118     * breakpoint handling and the library handling at the same time,
119     * but don't want to seperate the handling of these events.  In this
120     * example the Library event is subservient to the Breakpoint event.
121     *
122     * We'll take the event and all its subservient set and run handlers
123     * for all of them.
124     **/
125    set<Event::ptr> all_events;
126    all_events.insert(ev);      
127    for (vector<Event::ptr>::iterator i = ev->subservient_events.begin();
128         i != ev->subservient_events.end(); i++)
129    {
130       all_events.insert(*i);
131    }
132
133    typedef set<pair<Event::ptr, Handler *>, eh_cmp_func > ev_hndler_set_t;
134    ev_hndler_set_t events_and_handlers;      
135    for (set<Event::ptr>::iterator i = all_events.begin(); i != all_events.end(); i++)
136    {
137       Event::ptr ev = *i;
138       EventType etype = ev->getEventType();
139       HandlerMap_t::iterator j = handlers.find(etype);
140       if (j == handlers.end()) {
141          perr_printf("Event %s has no handlers registered\n", etype.name().c_str());
142          continue;
143       }
144       HandlerSet_t *hset = j->second;
145       for (HandlerSet_t::iterator k = hset->begin(); k != hset->end(); k++)
146       {
147          Handler *hnd = *k;
148          events_and_handlers.insert(pair<Event::ptr, Handler*>(ev, hnd));
149       }
150    }
151
152    /**
153     * We should have all events and handlers properly sorted into the
154     * events_and_handlers set in the order we want to run them in.
155     * Now it's finally time to run the handlers.
156     **/
157    bool handled_something = false;
158    ev_hndler_set_t::iterator i;
159    for (i = events_and_handlers.begin(); i != events_and_handlers.end(); i++)
160    {
161       handled_something = true;
162       Event::ptr event = i->first;
163       Handler *handler = i->second;
164       EventType etype = ev->getEventType();
165
166       pthrd_printf("Handling event '%s' with handler '%s'\n", etype.name().c_str(), 
167                    handler->getName().c_str());
168       
169       bool result = handler->handleEvent(event);
170       if (!result) {
171          pthrd_printf("Error handling event %s with %s\n", etype.name().c_str(), 
172                       handler->getName().c_str());
173          return false;
174       }
175    }
176    return handled_something;
177 }
178
179 HandleBootstrap::HandleBootstrap() :
180    Handler(std::string("Bootstrap"))
181 {
182 }
183
184 HandleBootstrap::~HandleBootstrap()
185 {
186 }
187
188 void HandleBootstrap::getEventTypesHandled(std::vector<EventType> &etypes)
189 {
190    etypes.push_back(EventType(EventType::None, EventType::Bootstrap));
191 }
192
193 bool HandleBootstrap::handleEvent(Event::ptr ev)
194 {
195    int_process *p = ev->getProcess()->llproc();
196    assert(p);
197    pthrd_printf("Handling bootstrap for %d\n", p->getPid());
198
199    if (p->getState() != int_process::neonatal_intermediate)
200       return true;
201    
202    bool all_bootstrapped = true;
203    int_threadPool *tp = ev->getProcess()->llproc()->threadPool();
204    for (int_threadPool::iterator i = tp->begin(); i != tp->end(); i++) {
205       int_thread *thr = *i;
206       if (thr->getHandlerState() == int_thread::neonatal_intermediate) {
207          pthrd_printf("Thread %d is not yet bootstrapped\n", thr->getLWP());
208          all_bootstrapped = false;
209          break;
210       }
211    }
212
213    if (all_bootstrapped) {
214       pthrd_printf("All threads are bootstrapped, marking process bootstrapped\n");
215       p->setState(int_process::running);
216    }
217
218    return true;
219 }
220
221 HandleSignal::HandleSignal() :
222    Handler(std::string("Signal"))
223 {
224 }
225
226 HandleSignal::~HandleSignal()
227 {
228 }
229
230 void HandleSignal::getEventTypesHandled(std::vector<EventType> &etypes)
231 {
232    etypes.push_back(EventType(EventType::None, EventType::Signal));
233 }
234
235 bool HandleSignal::handleEvent(Event::ptr ev)
236 {
237    int_thread *thrd = ev->getThread()->llthrd();
238    
239    EventSignal *sigev = static_cast<EventSignal *>(ev.get());
240    thrd->setContSignal(sigev->getSignal());
241
242    return true;
243 }
244
245 HandlePostExit::HandlePostExit() :
246    Handler("Post Exit")
247 {
248 }
249
250 HandlePostExit::~HandlePostExit()
251 {
252 }
253
254 void HandlePostExit::getEventTypesHandled(std::vector<EventType> &etypes)
255 {
256    etypes.push_back(EventType(EventType::Post, EventType::Exit));
257 }
258
259 bool HandlePostExit::handleEvent(Event::ptr ev)
260 {
261    int_process *proc = ev->getProcess()->llproc();
262    int_thread *thrd = ev->getThread()->llthrd();
263    assert(proc);
264    assert(thrd);
265    EventExit *event = static_cast<EventExit *>(ev.get());
266    pthrd_printf("Handling post-exit for process %d on thread %d\n",
267                 proc->getPid(), thrd->getLWP());
268    proc->setExitCode(event->getExitCode());
269    
270    ProcPool()->condvar()->lock();
271
272    proc->setState(int_process::exited);
273    ProcPool()->rmProcess(proc);
274    ProcPool()->rmThread(thrd);
275
276    ProcPool()->condvar()->signal();
277    ProcPool()->condvar()->unlock();
278
279    if (int_process::in_waitHandleProc == proc) {
280       pthrd_printf("Postponing delete due to being in waitAndHandleForProc\n");
281    }else{
282       delete proc;
283    }
284
285    return true;
286 }
287
288 HandleCrash::HandleCrash() :
289    Handler("Crash")
290 {
291 }
292
293 HandleCrash::~HandleCrash()
294 {
295 }
296
297 void HandleCrash::getEventTypesHandled(std::vector<EventType> &etypes)
298 {
299    etypes.push_back(EventType(EventType::Post, EventType::Crash));
300 }
301
302 bool HandleCrash::handleEvent(Event::ptr ev)
303 {
304    int_process *proc = ev->getProcess()->llproc();
305    int_thread *thrd = ev->getThread()->llthrd();
306    assert(proc);
307    assert(thrd);
308    pthrd_printf("Handling crash for process %d on thread %d\n",
309                 proc->getPid(), thrd->getLWP());
310    EventCrash *event = static_cast<EventCrash *>(ev.get());
311    proc->setCrashSignal(event->getTermSignal());
312    
313    ProcPool()->condvar()->lock();
314
315    proc->setState(int_process::exited);
316    ProcPool()->rmProcess(proc);
317
318    ProcPool()->condvar()->signal();
319    ProcPool()->condvar()->unlock();
320
321    delete proc;
322
323    return true;
324 }
325
326 HandlePreExit::HandlePreExit() :
327    Handler("Pre Exit")
328 {
329 }
330
331 HandlePreExit::~HandlePreExit()
332 {
333 }
334
335 void HandlePreExit::getEventTypesHandled(std::vector<EventType> &etypes)
336 {
337    etypes.push_back(EventType(EventType::Pre, EventType::Exit));
338 }
339
340 bool HandlePreExit::handleEvent(Event::ptr ev)
341 {
342    pthrd_printf("Handling pre-exit for process %d on thread %d\n",
343                 ev->getProcess()->llproc()->getPid(), 
344                 ev->getThread()->llthrd()->getLWP());
345
346    return true;
347 }
348
349 HandleThreadCreate::HandleThreadCreate() :
350    Handler("Thread Create")
351 {
352 }
353
354 HandleThreadCreate::~HandleThreadCreate()
355 {
356 }
357
358 void HandleThreadCreate::getEventTypesHandled(std::vector<EventType> &etypes)
359 {
360    etypes.push_back(EventType(EventType::None, EventType::ThreadCreate));
361 }
362
363 bool HandleThreadCreate::handleEvent(Event::ptr ev)
364 {
365    int_process *proc = ev->getProcess()->llproc();
366    int_thread *thrd = ev->getThread()->llthrd();
367    EventNewThread *threadev = static_cast<EventNewThread *>(ev.get());
368
369    pthrd_printf("Handle thread create for %d/%d with new thread %d\n",
370                 proc->getPid(), thrd ? thrd->getLWP() : -1, threadev->getLWP());
371
372    ProcPool()->condvar()->lock();
373    
374    int_thread *newthr = int_thread::createThread(proc, 0, threadev->getLWP(), false);
375    //New threads start stopped, but inherit the user state of the creating
376    // thread (which should be 'running').
377    newthr->setGeneratorState(int_thread::stopped);
378    newthr->setHandlerState(int_thread::stopped);
379    newthr->setInternalState(thrd->getUserState());
380    newthr->setUserState(thrd->getUserState());
381    
382    ProcPool()->condvar()->signal();
383    ProcPool()->condvar()->unlock();
384
385    return true;
386 }
387
388 HandleThreadDestroy::HandleThreadDestroy() :
389    Handler("Thread Destroy")
390 {
391 }
392
393 HandleThreadDestroy::~HandleThreadDestroy()
394 {
395 }
396
397 void HandleThreadDestroy::getEventTypesHandled(std::vector<EventType> &etypes)
398 {
399    etypes.push_back(EventType(EventType::Any, EventType::ThreadDestroy));
400 }
401
402 bool HandleThreadDestroy::handleEvent(Event::ptr ev)
403 {
404    int_thread *thrd = ev->getThread()->llthrd();
405    int_process *proc = ev->getProcess()->llproc();
406    if (ev->getEventType().time() == EventType::Pre) {
407       pthrd_printf("Handling pre-thread destroy for %d\n", thrd->getLWP());
408       return true;
409    }
410
411    pthrd_printf("Handling post-thread destroy for %d\n", thrd->getLWP());
412    ProcPool()->condvar()->lock();
413
414    thrd->setHandlerState(int_thread::exited);
415    thrd->setInternalState(int_thread::exited);
416    thrd->setUserState(int_thread::exited);
417    ProcPool()->rmThread(thrd);
418    proc->threadPool()->rmThread(thrd);
419
420    delete thrd;
421
422    ProcPool()->condvar()->signal();
423    ProcPool()->condvar()->unlock();
424    return true;
425 }
426
427 HandleThreadStop::HandleThreadStop() :
428    Handler(std::string("Thread Stop"))
429 {
430 }
431
432 HandleThreadStop::~HandleThreadStop()
433 {
434 }
435
436 void HandleThreadStop::getEventTypesHandled(std::vector<EventType> &etypes)
437 {
438    etypes.push_back(EventType(EventType::None, EventType::Stop));
439 }
440
441 bool HandleThreadStop::handleEvent(Event::ptr ev)
442 {
443    int_thread *thrd = ev->getThread()->llthrd();
444    int_process *proc = ev->getProcess()->llproc();
445    pthrd_printf("Handling thread stop for %d/%d\n", proc->getPid(), thrd->getLWP());
446
447    assert(thrd->hasPendingStop());
448    thrd->setPendingStop(false);
449
450    thrd->setInternalState(int_thread::stopped);
451    if (thrd->hasPendingUserStop()) {
452       thrd->setUserState(int_thread::stopped);
453       thrd->setPendingUserStop(false);
454    }
455
456    return true;
457 }
458
459 HandlePostFork::HandlePostFork() :
460    Handler("Post Fork")
461 {
462 }
463
464 HandlePostFork::~HandlePostFork()
465 {
466 }
467
468 void HandlePostFork::getEventTypesHandled(std::vector<EventType> &etypes)
469 {
470    etypes.push_back(EventType(EventType::Post, EventType::Fork));
471 }
472
473 bool HandlePostFork::handleEvent(Event::ptr ev)
474 {
475    EventFork *efork = static_cast<EventFork *>(ev.get());
476    Dyninst::PID child_pid = efork->getPID();
477    int_process *parent_proc = ev->getProcess()->llproc();
478    pthrd_printf("Handling fork for parent %d to child %d\n",
479                 parent_proc->getPid(), child_pid);
480
481    int_process *child_proc = int_process::createProcess(child_pid, parent_proc);
482    assert(child_proc);
483    return child_proc->forked();
484 }
485
486 HandlePostExec::HandlePostExec() :
487    Handler("Post Exec")
488 {
489 }
490
491 HandlePostExec::~HandlePostExec()
492 {
493 }
494
495 void HandlePostExec::getEventTypesHandled(std::vector<EventType> &etypes)
496 {
497    etypes.push_back(EventType(EventType::Post, EventType::Exec));
498 }
499
500 bool HandlePostExec::handleEvent(Event::ptr ev)
501 {
502    EventExec *eexec = static_cast<EventExec *>(ev.get());
503    int_process *proc = ev->getProcess()->llproc();
504    pthrd_printf("Handling exec for process %d\n",
505                 proc->getPid());
506
507    bool result = proc->execed();
508    if (!result) 
509       return false;
510    
511    eexec->setExecPath(proc->getExecutable());
512    eexec->setThread(proc->threadPool()->initialThread()->thread());
513    return true;
514 }
515
516 HandleSingleStep::HandleSingleStep() :
517    Handler("Single Step")
518 {
519 }
520
521 HandleSingleStep::~HandleSingleStep()
522 {
523 }
524
525 void HandleSingleStep::getEventTypesHandled(vector<EventType> &etypes)
526 {
527    etypes.push_back(EventType(EventType::None, EventType::SingleStep));
528 }
529
530 bool HandleSingleStep::handleEvent(Event::ptr ev)
531 {
532    pthrd_printf("Handling event single step on %d/%d\n", 
533                 ev->getProcess()->llproc()->getPid(),
534                 ev->getThread()->llthrd()->getLWP());
535    ev->getThread()->llthrd()->setUserState(int_thread::stopped);
536    ev->getThread()->llthrd()->setInternalState(int_thread::stopped);
537    return true;
538 }
539
540 HandleBreakpoint::HandleBreakpoint() :
541    Handler("Breakpoint")
542 {
543 }
544
545 HandleBreakpoint::~HandleBreakpoint()
546 {
547 }
548
549 void HandleBreakpoint::getEventTypesHandled(vector<EventType> &etypes)
550 {
551    etypes.push_back(EventType(EventType::None, EventType::Breakpoint));
552 }
553
554 bool HandleBreakpoint::handleEvent(Event::ptr ev)
555 {
556    pthrd_printf("Handling breakpoint\n");
557    int_process *proc = ev->getProcess()->llproc();
558
559    EventBreakpoint *ebp = static_cast<EventBreakpoint *>(ev.get());
560    std::vector<Breakpoint::ptr> hl_bps;
561    ebp->getBreakpoints(hl_bps);
562    bool has_user_breakpoints = !hl_bps.empty();
563
564    if (has_user_breakpoints) 
565    {
566       int_threadPool *pool = proc->threadPool();
567       for (int_threadPool::iterator i = pool->begin(); i != pool->end(); i++) {
568          (*i)->setUserState(int_thread::stopped);
569       }
570    }
571
572    return true;
573 }
574
575 HandlePostBreakpoint::HandlePostBreakpoint() :
576    Handler("Post Breakpoint")
577 {
578 }
579
580 HandlePostBreakpoint::~HandlePostBreakpoint()
581 {
582 }
583
584 void HandlePostBreakpoint::getEventTypesHandled(vector<EventType> &etypes)
585 {
586    etypes.push_back(EventType(EventType::None, EventType::Breakpoint));
587 }
588
589 bool HandlePostBreakpoint::handleEvent(Event::ptr ev)
590 {
591    int_process *proc = ev->getProcess()->llproc();
592    int_thread *thrd = ev->getThread()->llthrd();
593
594    /**
595     * TODO: Ctrl transfer breakpoints
596     **/
597
598    /**
599     * Stop all other threads in the job while we remove the breakpoint, single step 
600     * through the instruction and then resume the other threads
601     **/
602    pthrd_printf("Marking all threads in %d stopped for internal breakpoint handling\n",
603                 proc->getPid());
604    int_threadPool *pool = proc->threadPool();
605    for (int_threadPool::iterator i = pool->begin(); i != pool->end(); i++) {
606       if ((*i)->getInternalState() == int_thread::running) {
607          (*i)->setInternalState(int_thread::stopped);
608       }
609    }
610
611    EventBreakpoint *evbp = static_cast<EventBreakpoint *>(ev.get());
612    installed_breakpoint *bp = evbp->installedbp();
613
614    pthrd_printf("Setting breakpoint thread to single step mode\n");
615    thrd->setSingleStepMode(true);
616    thrd->markClearingBreakpoint(bp);
617
618    pthrd_printf("Removing breakpoint from memory\n");
619    bool result = bp->suspend(proc);
620    assert(result);
621
622    pthrd_printf("Restoring PC to original location location at %lx\n", bp->getAddr());
623    MachRegister pcreg = MachRegister::getPC(proc->getTargetArch());
624    result = thrd->setRegister(pcreg, (MachRegisterVal) bp->getAddr());
625    assert(result);
626
627    thrd->setInternalState(int_thread::running);
628
629    return true;
630 }
631
632 HandleBreakpointClear::HandleBreakpointClear() :
633    Handler("Breakpoint Clear")
634 {
635 }
636
637 HandleBreakpointClear::~HandleBreakpointClear()
638 {
639 }
640
641 void HandleBreakpointClear::getEventTypesHandled(vector<EventType> &etypes)
642 {
643    etypes.push_back(EventType(EventType::None, EventType::BreakpointClear));
644 }
645
646 bool HandleBreakpointClear::handleEvent(Event::ptr ev)
647 {
648    int_process *proc = ev->getProcess()->llproc();
649    int_thread *thrd = ev->getThread()->llthrd();
650    EventBreakpointClear *bpc = static_cast<EventBreakpointClear *>(ev.get());
651    installed_breakpoint *bp = bpc->bp();
652
653    pthrd_printf("Resuming breakpoint at %lx\n", bp->getAddr());
654    bp->resume(proc);
655   
656    pthrd_printf("Restoring process state\n");
657    thrd->setSingleStepMode(false);
658    thrd->markClearingBreakpoint(NULL);
659    thrd->setInternalState(int_thread::stopped);
660   
661    proc->threadPool()->restoreInternalState(false);
662
663    return true;
664 }
665
666 int HandlePostBreakpoint::getPriority() const
667 {
668    return Handler::PostCallbackPriority;
669 }
670
671 HandleLibrary::HandleLibrary() :
672    Handler("SysV Library Handler")
673 {
674 }
675
676 HandleLibrary::~HandleLibrary()
677 {
678 }
679
680 bool HandleLibrary::handleEvent(Event::ptr ev)
681 {
682    pthrd_printf("Handling library load/unload\n");
683    EventLibrary *lev = static_cast<EventLibrary *>(ev.get());
684
685    int_process *proc = ev->getProcess()->llproc();
686    set<int_library *> ll_added, ll_rmd;
687    bool result = proc->refresh_libraries(ll_added, ll_rmd);
688    if (!result) {
689       pthrd_printf("Failed to refresh library list\n");
690       return false;
691    }
692    if (ll_added.empty() && ll_rmd.empty()) {
693       pthrd_printf("Could not find actual changes in lib state\n");
694       return true;
695    }
696
697    set<Library::ptr> added, rmd;
698    for (set<int_library*>::iterator i = ll_added.begin(); i != ll_added.end(); i++) {
699       added.insert((*i)->getUpPtr());
700    }
701    for (set<int_library*>::iterator i = ll_rmd.begin(); i != ll_rmd.end(); i++) {
702       rmd.insert((*i)->getUpPtr());
703    }
704    lev->setLibs(added, rmd);
705    return true;
706 }
707
708 void HandleLibrary::getEventTypesHandled(std::vector<EventType> &etypes)
709 {
710    etypes.push_back(EventType(EventType::None, EventType::Library));
711 }
712
713 HandleCallbacks::HandleCallbacks() : 
714    Handler("Callback")
715 {
716 }
717
718 HandleCallbacks::~HandleCallbacks()
719 {
720 }
721
722 HandleCallbacks *HandleCallbacks::getCB()
723 {
724    static HandleCallbacks *cb = NULL;
725    if (!cb) {
726       cb = new HandleCallbacks();
727       assert(cb);
728    }
729    return cb;
730 }
731
732 int HandleCallbacks::getPriority() const
733 {
734    return CallbackPriority;
735 }
736
737 void HandleCallbacks::getEventTypesHandled(std::vector<EventType> & /*etypes*/)
738 {
739    //Callbacks are special cased, they respond to all event types.
740 }
741
742 bool HandleCallbacks::hasCBs(Event::const_ptr ev)
743 {
744    return cbfuncs.find(ev->getEventType()) != cbfuncs.end();
745 }
746
747 bool HandleCallbacks::requiresCB(Event::const_ptr ev)
748 {
749    return hasCBs(ev) && !ev->suppressCB();
750 }
751
752 bool HandleCallbacks::handleEvent(Event::ptr ev)
753 {
754    EventType evtype = ev->getEventType();
755    std::map<EventType, std::set<Process::cb_func_t>, eventtype_cmp>::iterator i = cbfuncs.find(evtype);
756    if (i == cbfuncs.end()) {
757       pthrd_printf("No callback registered for event type '%s'\n", ev->name().c_str());
758       return true;
759    }
760    int_process *proc = ev->getProcess()->llproc();
761    if (proc &&
762        (proc->getState() == int_process::neonatal ||
763         proc->getState() == int_process::neonatal_intermediate))
764    {
765       pthrd_printf("No callback for neonatal process %d\n", proc->getPid());
766       return true;
767    }
768    const std::set<Process::cb_func_t> &cbs = i->second;
769
770    return deliverCallback(ev, cbs);
771 }
772
773 static const char *action_str(Process::cb_action_t action)
774 {
775    switch (action) {
776       case Process::cbThreadContinue:
777          return "cbThreadContinue";
778       case Process::cbThreadStop:
779          return "cbThreadStop";
780       case Process::cbProcContinue:
781          return "cbProcContinue";
782       case Process::cbProcStop:
783          return "cbProcStop";
784       case Process::cbDefault:
785          return "cbDefault";
786       default:
787          assert(0);
788    }
789
790    return NULL;
791 }
792
793 bool HandleCallbacks::handleCBReturn(Process::const_ptr proc, Thread::const_ptr thrd, 
794                                      Process::cb_action_t ret)
795 {
796    switch (ret) {
797       case Process::cbThreadContinue:
798          if (thrd == Thread::const_ptr()) {
799             perr_printf("User returned invalid action %s for event\n", 
800                         action_str(ret));
801             return false;
802          }
803          pthrd_printf("Callbacks returned thread continue\n");
804          thrd->llthrd()->setUserState(int_thread::running);
805          thrd->llthrd()->setInternalState(int_thread::running);
806          break;
807       case Process::cbThreadStop:
808          if (thrd == Thread::const_ptr()) {
809             perr_printf("User returned invalid action %s for event\n", 
810                         action_str(ret));
811             return false;
812          }
813          pthrd_printf("Callbacks returned thread stop\n");
814          thrd->llthrd()->setUserState(int_thread::stopped);
815          thrd->llthrd()->setInternalState(int_thread::stopped);
816          break;
817       case Process::cbProcContinue: {
818          if (proc == Process::const_ptr()) {
819             perr_printf("User returned invalid action %s for event\n", 
820                         action_str(ret));
821             return false;
822          }
823          pthrd_printf("Callbacks returned process continue\n");
824          int_threadPool *tp = proc->llproc()->threadPool();
825          for (int_threadPool::iterator j = tp->begin(); j != tp->end(); j++) {
826             (*j)->setUserState(int_thread::running);
827             (*j)->setInternalState(int_thread::running);
828          }
829          break;
830       }
831       case Process::cbProcStop: {
832          if (proc == Process::const_ptr()) {
833             perr_printf("User returned invalid action %s for event\n", 
834                         action_str(ret));
835             return false;
836          }
837          pthrd_printf("Callbacks returned process stop\n");
838          int_threadPool *tp = proc->llproc()->threadPool();
839          for (int_threadPool::iterator j = tp->begin(); j != tp->end(); j++) {
840             (*j)->setUserState(int_thread::stopped);
841             (*j)->setInternalState(int_thread::stopped);
842          }
843          break;
844       }
845       case Process::cbDefault:
846          pthrd_printf("Callbacks returned default\n");
847          break;
848    }
849    return true;
850 }
851
852 bool HandleCallbacks::deliverCallback(Event::ptr ev, const set<Process::cb_func_t> &cbset)
853 {
854    if (ev->suppressCB()) {
855       pthrd_printf("Suppressing callbacks for event %s\n", ev->name().c_str());
856       if (mt()->getThreadMode() == Process::HandlerThreading && 
857           notify()->hasEvents()) 
858       {
859          notify()->clearEvent();
860       }
861       return true;
862    }
863
864    //The following code loops over each callback registered for this event type
865    // and triggers the callback for the user.  Return results are aggregated.
866    assert(!(isHandlerThread() && mt()->getThreadMode() != Process::CallbackThreading));
867    unsigned k = 0;
868    pthrd_printf("Triggering callback for event '%s'\n", ev->name().c_str());
869    Process::cb_action_t parent_result = Process::cbDefault;
870    Process::cb_action_t child_result = Process::cbDefault;
871    std::set<Process::cb_func_t>::iterator j;
872    for (j = cbset.begin(); j != cbset.end(); j++, k++) {
873       pthrd_printf("Triggering callback #%u for event '%s'\n", k, ev->name().c_str());
874       int_process::setInCB(true);
875       Process::cb_ret_t ret = (*j)(ev);
876       int_process::setInCB(false);
877
878       if (ret.parent != Process::cbDefault)
879          parent_result = ret.parent;
880       if (ret.child != Process::cbDefault)
881          child_result = ret.child;
882
883       pthrd_printf("Callback #%u return %s/%s\n", k, action_str(ret.parent),
884                    action_str(ret.child));
885    }
886
887    //Given the callback return result, change the user state to the appropriate
888    // setting.
889    pthrd_printf("Handling return value for main process\n");
890    handleCBReturn(ev->getProcess(), ev->getThread(), parent_result);
891
892    pthrd_printf("Handling return value for child process/thread\n");
893    Process::const_ptr child_proc = Process::const_ptr();
894    Thread::const_ptr child_thread = Thread::const_ptr();
895    bool event_has_child = false;
896    switch (ev->getEventType().code()) {
897       case EventType::Fork:
898          event_has_child = true;
899          child_proc = static_cast<EventFork *>(ev.get())->getChildProcess();
900          break;
901       case EventType::ThreadCreate:
902          event_has_child = true;
903          child_thread = static_cast<EventNewThread *>(ev.get())->getNewThread();
904          break;
905    }
906    if (event_has_child)
907       handleCBReturn(child_proc, child_thread, child_result);
908
909    if (mt()->getThreadMode() == Process::HandlerThreading && 
910        notify()->hasEvents())
911    {
912       notify()->clearEvent();
913    }
914
915    return true;
916 }
917
918 bool HandleCallbacks::registerCallback_int(EventType ev, Process::cb_func_t func)
919 {
920    pthrd_printf("Registering event %s with callback function %p\n", ev.name().c_str(), func);
921    std::set<EventType>::iterator i = alleventtypes.find(ev);
922    if (i == alleventtypes.end()) {
923       pthrd_printf("Event %s does not have any handler\n", ev.name().c_str());
924       return false;
925    }
926    cbfuncs[ev].insert(func);
927    return true;
928 }
929
930 bool HandleCallbacks::registerCallback(EventType ev, Process::cb_func_t func)
931 {
932    switch (ev.time()) {
933       case EventType::Pre:
934       case EventType::Post:
935       case EventType::None: {
936          bool result = registerCallback_int(ev, func);
937          if (!result) {
938             pthrd_printf("Did not register any callbacks for %s\n", ev.name().c_str());
939             setLastError(err_noevents, "EventType does not exist");
940             return false;
941          }
942          break;
943       }
944       case EventType::Any: {
945          bool result1 = registerCallback_int(EventType(EventType::Pre, ev.code()), func);
946          bool result2 = registerCallback_int(EventType(EventType::Post, ev.code()), func);
947          bool result3 = registerCallback_int(EventType(EventType::None, ev.code()), func);
948          if (!result1 && !result2 && !result3) {
949             pthrd_printf("Did not register any callbacks for %s\n", ev.name().c_str());
950             setLastError(err_noevents, "EventType does not exist");
951             return false;
952          }
953          break;
954       }
955    }
956    return true;
957 }
958
959 bool HandleCallbacks::removeCallback_int(EventType et, Process::cb_func_t func)
960 {
961    cbfuncs_t::iterator i = cbfuncs.find(et);
962    if (i == cbfuncs.end()) {
963       return false;
964    }
965    set<Process::cb_func_t> &func_set = i->second;
966    set<Process::cb_func_t>::iterator j = func_set.find(func);
967    if (j == func_set.end()) {
968       return false;
969    }
970    func_set.erase(j);
971    return true;
972 }
973
974 bool HandleCallbacks::removeCallback(EventType et, Process::cb_func_t func)
975 {
976    bool result = false;
977    switch (et.time()) {
978       case EventType::Pre:
979       case EventType::Post:
980       case EventType::None: {
981          result = removeCallback_int(et, func);
982       }
983       case EventType::Any: {
984          bool result1 = removeCallback_int(EventType(EventType::Pre, et.code()), func);
985          bool result2 = removeCallback_int(EventType(EventType::Post,et.code()), func);
986          bool result3 = removeCallback_int(EventType(EventType::None,et.code()), func);
987          result = (result1 || result2 || result3);
988       }
989    }
990    if (!result) {
991       perr_printf("Attempted to remove non-existant callback %s\n", 
992                   et.name().c_str());
993       setLastError(err_badparam, "Callback does not exist");
994       return false;
995    }
996    return true;
997 }
998
999 bool HandleCallbacks::removeCallback_int(EventType et)
1000 {
1001    cbfuncs_t::iterator i = cbfuncs.find(et);
1002    if (i == cbfuncs.end()) {
1003       return false;
1004    }
1005    cbfuncs.erase(i);
1006    return true;
1007 }
1008
1009 bool HandleCallbacks::removeCallback(EventType et)
1010 {
1011    bool result = false;
1012    switch (et.time()) {
1013       case EventType::Pre:
1014       case EventType::Post:
1015       case EventType::None: {
1016          result = removeCallback_int(et);
1017       }
1018       case EventType::Any: {
1019          bool result1 = removeCallback_int(EventType(EventType::Pre, et.code()));
1020          bool result2 = removeCallback_int(EventType(EventType::Post,et.code()));
1021          bool result3 = removeCallback_int(EventType(EventType::None,et.code()));
1022          result = (result1 || result2 || result3);
1023       }
1024    }
1025    if (!result) {
1026       perr_printf("Attempted to remove non-existant callback %s\n", 
1027                   et.name().c_str());
1028       setLastError(err_badparam, "Callback does not exist");
1029       return false;
1030    }
1031    return true;
1032 }
1033
1034 bool HandleCallbacks::removeCallback(Process::cb_func_t func)
1035 {
1036    bool rmd_something = false;
1037    for (cbfuncs_t::iterator i = cbfuncs.begin(); i != cbfuncs.end(); i++)
1038    {
1039       EventType et = i->first;
1040       bool result = removeCallback_int(et, func);
1041       if (result) 
1042          rmd_something = true;
1043    }
1044    if (!rmd_something) {
1045       perr_printf("Attempted to remove non-existant callback %p\n", func);
1046       setLastError(err_badparam, "Callback does not exist");
1047       return false;
1048    }
1049    return true;
1050 }
1051
1052 HandlerPool *createDefaultHandlerPool()
1053 {
1054    static bool initialized = false;
1055    static HandleBootstrap *hbootstrap = NULL;
1056    static HandleSignal *hsignal = NULL;
1057    static HandlePostExit *hpostexit = NULL;
1058    static HandlePreExit *hpreexit = NULL;
1059    static HandleThreadCreate *hthreadcreate = NULL;
1060    static HandleThreadDestroy *hthreaddestroy = NULL;
1061    static HandleThreadStop *hthreadstop = NULL;
1062    static HandleSingleStep *hsinglestep = NULL;
1063    static HandleCrash *hcrash = NULL;
1064    static HandleBreakpoint *hbpoint = NULL;
1065    static HandlePostBreakpoint *hpost_bpoint = NULL;
1066    static HandleBreakpointClear *hbpclear = NULL;
1067    static HandleLibrary *hlibrary = NULL;
1068    static HandlePostFork *hpostfork = NULL;
1069    static HandlePostExec *hpostexec = NULL;
1070    static iRPCHandler *hrpc = NULL;
1071    if (!initialized) {
1072       hbootstrap = new HandleBootstrap();
1073       hsignal = new HandleSignal();
1074       hpostexit = new HandlePostExit();
1075       hpreexit = new HandlePreExit();
1076       hthreadcreate = new HandleThreadCreate();
1077       hthreaddestroy = new HandleThreadDestroy();
1078       hthreadstop = new HandleThreadStop();
1079       hsinglestep = new HandleSingleStep();
1080       hcrash = new HandleCrash();
1081       hbpoint = new HandleBreakpoint();
1082       hpost_bpoint = new HandlePostBreakpoint();
1083       hbpclear = new HandleBreakpointClear();
1084       hrpc = new iRPCHandler();
1085       hlibrary = new HandleLibrary();
1086       hpostfork = new HandlePostFork();
1087       hpostexec = new HandlePostExec();
1088       initialized = true;
1089    }
1090    HandlerPool *hpool = new HandlerPool();
1091    hpool->addHandler(hbootstrap);
1092    hpool->addHandler(hsignal);
1093    hpool->addHandler(hpostexit);
1094    hpool->addHandler(hpreexit);
1095    hpool->addHandler(hthreadcreate);
1096    hpool->addHandler(hthreaddestroy);
1097    hpool->addHandler(hthreadstop);
1098    hpool->addHandler(hsinglestep);
1099    hpool->addHandler(hcrash);
1100    hpool->addHandler(hbpoint);
1101    hpool->addHandler(hpost_bpoint);
1102    hpool->addHandler(hbpclear);
1103    hpool->addHandler(hrpc);
1104    hpool->addHandler(hlibrary);
1105    hpool->addHandler(hpostfork);
1106    hpool->addHandler(hpostexec);
1107    plat_createDefaultHandlerPool(hpool);
1108    return hpool;
1109 }
1110