Merge branch 'master' into bgq_ramdisk_io
[dyninst.git] / proccontrol / src / event.C
1 /*
2  * See the dyninst/COPYRIGHT file for copyright information.
3  * 
4  * We provide the Paradyn Tools (below described as "Paradyn")
5  * on an AS IS basis, and do not warrant its validity or performance.
6  * We reserve the right to update, modify, or discontinue this
7  * software at any time.  We shall have no obligation to supply such
8  * updates or modifications or any other form of support to you.
9  * 
10  * By your use of Paradyn, you understand and agree that we (or any
11  * other person or entity with proprietary rights in Paradyn) are
12  * under no obligation to provide either maintenance services,
13  * update services, notices of latent defects, or correction of
14  * defects for Paradyn.
15  * 
16  * This library is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU Lesser General Public
18  * License as published by the Free Software Foundation; either
19  * version 2.1 of the License, or (at your option) any later version.
20  * 
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * Lesser General Public License for more details.
25  * 
26  * You should have received a copy of the GNU Lesser General Public
27  * License along with this library; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29  */
30
31 #include "dynutil/h/dyntypes.h"
32 #include "proccontrol/src/int_process.h"
33 #include "proccontrol/src/int_handler.h"
34 #include "proccontrol/src/int_event.h"
35 #include "proccontrol/src/procpool.h"
36 #include "proccontrol/src/irpc.h"
37 #include "proccontrol/h/Event.h"
38 #include "proccontrol/h/PCProcess.h"
39 #include "proccontrol/h/PCErrors.h"
40 #include "proccontrol/h/Generator.h"
41 #include <assert.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 using namespace std;
46
47 ArchEvent::ArchEvent(std::string name_) :
48    name(name_)
49 {
50 }
51
52 ArchEvent::~ArchEvent()
53 {
54 }
55
56 std::string ArchEvent::getName()
57 {
58    return name;
59 }
60
61 Event::Event(EventType etype_, Thread::ptr thread_) :
62    etype(etype_),
63    thread(thread_),
64    proc(thread_ ? thread_->getProcess() : Process::ptr()),
65    stype(unset),
66    master_event(Event::ptr()),
67    suppress_cb(false),
68    user_event(false),
69    handling_started(false),
70    noted_event(false)
71 {
72 }
73
74 EventType Event::getEventType() const {
75    return etype;
76 }
77
78 Thread::const_ptr Event::getThread() const {
79    return thread;
80 }
81
82 Process::const_ptr Event::getProcess() const {
83    return proc;
84 }
85
86 void Event::setThread(Thread::const_ptr t) {
87    thread = t;
88 }
89
90 void Event::setProcess(Process::const_ptr p) {
91         proc = p;
92 }
93
94 void Event::setUserEvent(bool b) {
95     user_event = b;
96 }
97
98 bool Event::userEvent() const {
99     return user_event;
100 }
101
102 bool Event::canFastHandle() const
103 {
104    return false;
105 }
106
107 void Event::setSyncType(SyncType s)
108 {
109    stype = s;
110 }
111
112 bool Event::suppressCB() const
113 {
114    return suppress_cb;
115 }
116
117 bool Event::triggersCB() const
118 {
119    HandleCallbacks *cbhandler = HandleCallbacks::getCB();
120    if (cbhandler->requiresCB(shared_from_this()))
121      return true;
122    std::vector<Event::ptr>::const_iterator i = subservient_events.begin();
123    for (; i != subservient_events.end(); ++i) {
124       if ((*i)->triggersCB())
125          return true;
126    }
127    return false;
128 }
129
130 void Event::setSuppressCB(bool b)
131 {
132   pthrd_printf("Setting callback suppress for %s\n", name().c_str());
133    suppress_cb = b;
134 }
135
136 Event::SyncType Event::getSyncType() const
137 {
138    return stype;
139 }
140
141 std::string Event::name() const
142 {
143    return etype.name();
144 }
145
146 Event::weak_ptr Event::subservientTo() const
147 {
148    return master_event;
149 }
150
151 void Event::addSubservientEvent(Event::ptr ev)
152 {
153    subservient_events.push_back(ev);
154    ev->master_event = shared_from_this();
155 }
156
157 std::string EventType::name() const
158 {
159    std::string prefix;
160    if (etime == Pre)
161       prefix = "pre-";
162    if (etime == Post)
163       prefix = "post-";
164
165 #define STR_CASE(ET) case ET: return prefix + std::string(#ET)
166    switch (ecode) {
167       STR_CASE(Error);
168       STR_CASE(Unset);
169       STR_CASE(Exit);
170       STR_CASE(Crash);
171       STR_CASE(Fork);
172       STR_CASE(Exec);
173       STR_CASE(ThreadCreate);
174       STR_CASE(UserThreadCreate);
175       STR_CASE(LWPCreate);
176       STR_CASE(ThreadDestroy);
177       STR_CASE(UserThreadDestroy);
178       STR_CASE(LWPDestroy);
179       STR_CASE(Stop);
180       STR_CASE(Signal);
181       STR_CASE(LibraryLoad);
182       STR_CASE(LibraryUnload);
183       STR_CASE(Bootstrap);
184       STR_CASE(Breakpoint);
185       STR_CASE(RPC);
186       STR_CASE(SingleStep);
187       STR_CASE(Library);
188       STR_CASE(BreakpointClear);
189       STR_CASE(BreakpointRestore);
190       STR_CASE(RPCLaunch);
191       STR_CASE(Async);
192       STR_CASE(ChangePCStop);
193       STR_CASE(Detach);
194       STR_CASE(IntBootstrap);
195       STR_CASE(ForceTerminate);
196       STR_CASE(PreBootstrap);
197       STR_CASE(Nop);
198       STR_CASE(ThreadDB);
199       STR_CASE(ThreadInfo);
200       STR_CASE(ControlAuthority);
201       STR_CASE(AsyncRead);
202       STR_CASE(AsyncWrite);
203       STR_CASE(AsyncReadAllRegs);
204       STR_CASE(AsyncSetAllRegs);
205       STR_CASE(AsyncFileRead);
206       default: return prefix + std::string("Unknown");
207    }
208 }
209
210 bool Event::procStopper() const
211 {
212    return false;
213 }
214
215 Event::~Event()
216 {
217 }
218
219 void Event::setLastError(err_t ec, const char *es) {
220    if (proc) {
221       proc->setLastError(ec, es);
222    }
223 }
224
225 EventTerminate::EventTerminate(EventType type_) :
226    Event(type_)
227 {
228 }
229
230 EventTerminate::~EventTerminate()
231 {
232 }
233
234 EventExit::EventExit(EventType::Time eventtime, int exitcode_) :
235    EventTerminate(EventType(eventtime, EventType::Exit)),
236    exitcode(exitcode_)
237 {
238 }
239
240 int EventExit::getExitCode() const
241 {
242    return exitcode;
243 }
244
245 EventExit::~EventExit()
246 {
247 }
248
249 EventCrash::EventCrash(int termsig_) :
250    EventTerminate(EventType(EventType::None, EventType::Crash)),
251    termsig(termsig_)
252 {
253 }
254
255 int EventCrash::getTermSignal() const
256 {
257    return termsig;
258 }
259          
260 EventCrash::~EventCrash()
261 {
262 }
263
264 EventForceTerminate::EventForceTerminate(int termsig_) :
265     EventTerminate(EventType(EventType::Post, EventType::ForceTerminate)),
266     termsig(termsig_)
267 {
268 }
269
270 int EventForceTerminate::getTermSignal() const
271 {
272    return termsig;
273 }
274
275 EventForceTerminate::~EventForceTerminate()
276 {
277 }
278
279 EventExec::EventExec(EventType::Time etime_, std::string path) :
280    Event(EventType(etime_, EventType::Exec)),
281    execpath(path)
282 {
283 }
284
285 EventExec::~EventExec()
286 {
287 }
288
289 std::string EventExec::getExecPath() const
290 {
291    return execpath;
292 }
293
294 void EventExec::setExecPath(std::string e)
295 {
296    execpath = e;
297 }
298
299 EventStop::EventStop() :
300    Event(EventType(EventType::None, EventType::Stop))
301 {
302 }
303
304 EventStop::~EventStop()
305 {
306 }
307
308
309 EventBreakpoint::EventBreakpoint(int_eventBreakpoint *ibp_) :
310    Event(EventType(EventType::None, EventType::Breakpoint)),
311    int_bp(ibp_)
312 {
313 }
314
315 EventBreakpoint::~EventBreakpoint()
316 {
317    if (int_bp) {
318       delete int_bp;
319       int_bp = NULL;
320    }
321    
322 }
323
324 Dyninst::Address EventBreakpoint::getAddress() const
325 {
326    return int_bp->addr;
327 }
328
329 void EventBreakpoint::getBreakpoints(std::vector<Breakpoint::const_ptr> &bps) const
330 {
331    if (!int_bp)
332       return;
333    bp_instance *ibp = int_bp->lookupInstalledBreakpoint();
334    std::set<Breakpoint::ptr>::iterator i;
335    for (i = ibp->hl_bps.begin(); i != ibp->hl_bps.end(); ++i) {
336       bps.push_back(*i);
337    }
338 }
339
340 void EventBreakpoint::getBreakpoints(std::vector<Breakpoint::ptr> &bps)
341 {
342    if (!int_bp)
343       return;
344    bp_instance *ibp = int_bp->lookupInstalledBreakpoint();
345    std::set<Breakpoint::ptr>::iterator i;
346    for (i = ibp->hl_bps.begin(); i != ibp->hl_bps.end(); i++) {
347       bps.push_back(*i);
348    }
349 }
350
351 bool EventBreakpoint::suppressCB() const
352 {
353
354   if (Event::suppressCB()) {
355     pthrd_printf("Suppressing CB on EventBreakpoint: All event suppress\n");
356       return true;
357   }
358    bp_instance *ibp = int_bp->lookupInstalledBreakpoint();
359    if (!ibp) {
360      pthrd_printf("Suppressing CB on EventBreakpoint: no bp_instance\n");
361      return true;
362    }
363    if (ibp->hl_bps.empty()) {
364      pthrd_printf("Suppressing CB on EventBreakpoint:: no hl_bps in bp_instance\n");
365      return true;
366    }
367    for (bp_instance::iterator iter = ibp->begin();
368         iter != ibp->end(); ++iter) {
369      pthrd_printf("Suppressing CB on EventBreakpoint: checking for unsuppressed HL BP: %s\n",
370                   (*iter)->suppressCallbacks() ? "<suppress>" : "<no suppress>");
371      if (!(*iter)->suppressCallbacks()) return false;
372    }
373    pthrd_printf("Suppressing CB on EventBreakpoint: default case\n");
374    return true;
375 }
376
377 int_eventBreakpoint *EventBreakpoint::getInternal() const
378 {
379    return int_bp;
380 }
381
382 bool EventBreakpoint::procStopper() const
383 {
384    int num_proc_stoppers = 0;
385    bp_instance *bp = int_bp->lookupInstalledBreakpoint();
386    if (!bp) {
387       return false;
388    }
389
390    for (sw_breakpoint::iterator i = bp->begin(); i != bp->end(); i++) {
391       if (!(*i)->isProcessStopper())
392          continue;
393       if (isGeneratorThread()) {
394          //We can call this during decode--don't set the states then.
395          return true;
396       }
397       num_proc_stoppers++;
398    }
399
400    if (!handled_by.empty())
401       return false;
402
403    if (!num_proc_stoppers) {
404       //The breakpoint is not a proc stopper.
405       return false;
406    }
407
408    int_process *proc = getProcess()->llproc();
409    int_thread *thrd = getThread()->llthrd();
410    if (!int_bp->stopped_proc) {
411       //Move the internal state of the process to be stopped.
412       thrd->getInternalState().desyncStateProc(int_thread::stopped);
413       int_bp->stopped_proc = true;
414    }
415    
416    //We return true if the event isn't ready
417    return !proc->getProcStopManager().processStoppedTo(int_thread::InternalStateID);
418 }
419
420 EventSignal::EventSignal(int sig_) :
421    Event(EventType(EventType::None, EventType::Signal)),
422    sig(sig_)
423 {
424 }
425
426 EventSignal::~EventSignal()
427 {
428 }
429
430 int EventSignal::getSignal() const
431 {
432    return sig;
433 }
434
435 void EventSignal::clearThreadSignal() const
436 {
437     int_thread *thr = getThread()->llthrd();
438     thr->setContSignal(0);
439 }
440
441 void EventSignal::setThreadSignal(int newSignal) const 
442 {
443     int_thread *thr = getThread()->llthrd();
444     thr->setContSignal(newSignal);
445 }
446
447 EventBootstrap::EventBootstrap() :
448    Event(EventType(EventType::None, EventType::Bootstrap))
449 {
450 }
451
452 EventBootstrap::~EventBootstrap()
453 {
454 }
455
456 EventPreBootstrap::EventPreBootstrap() :
457    Event(EventType(EventType::None, EventType::PreBootstrap))
458 {
459 }
460
461 EventPreBootstrap::~EventPreBootstrap()
462 {
463 }
464
465
466 EventNewThread::EventNewThread(EventType et) : 
467    Event(et)
468 {
469 }
470
471 EventNewThread::~EventNewThread()
472 {
473 }
474
475 EventNewUserThread::EventNewUserThread() :
476    EventNewThread(EventType(EventType::None, EventType::UserThreadCreate))
477 {
478    iev = new int_eventNewUserThread();
479 }
480
481 EventNewUserThread::~EventNewUserThread()
482 {
483    if (iev) 
484       delete iev;
485    iev = NULL;
486 }
487
488 Dyninst::LWP EventNewUserThread::getLWP() const
489 {
490    if (iev->lwp != NULL_LWP)
491       return iev->lwp;
492    return iev->thr ? iev->thr->getLWP() : NULL_LWP;
493 }
494
495 Thread::const_ptr EventNewUserThread::getNewThread() const
496 {
497    if (iev->thr)
498       return iev->thr->thread();
499    if (iev->lwp == NULL_LWP)
500       return Thread::const_ptr();
501
502    iev->thr = getProcess()->llproc()->threadPool()->findThreadByLWP(iev->lwp);
503    assert(iev->thr);
504    return iev->thr->thread();
505 }
506
507 int_eventNewUserThread *EventNewUserThread::getInternalEvent() const
508 {
509    return iev;
510 }
511
512 EventNewLWP::EventNewLWP(Dyninst::LWP lwp_, int attach_status) :
513    EventNewThread(EventType(EventType::None, EventType::LWPCreate)),
514    lwp(lwp_)
515 {
516    iev = new int_eventNewLWP();
517    iev->lwp = lwp;
518    iev->attach_status = (int_thread::attach_status_t) attach_status;
519 }
520
521 EventNewLWP::~EventNewLWP()
522 {
523    delete iev;
524 }
525
526 Dyninst::LWP EventNewLWP::getLWP() const
527 {
528    return iev->lwp;
529 }
530
531 Thread::const_ptr EventNewLWP::getNewThread() const
532 {
533    int_thread *thr = getProcess()->llproc()->threadPool()->findThreadByLWP(lwp);
534    assert(thr);
535    return thr->thread();
536 }
537
538 int_eventNewLWP *EventNewLWP::getInternalEvent()
539 {
540    return iev;
541 }
542
543 EventThreadDestroy::EventThreadDestroy(EventType et) :
544    Event(et)
545 {
546 }
547
548 EventThreadDestroy::~EventThreadDestroy()
549 {
550 }
551
552 EventUserThreadDestroy::EventUserThreadDestroy(EventType::Time time_) :
553    EventThreadDestroy(EventType(time_, EventType::UserThreadDestroy))
554 {
555 }
556
557 EventUserThreadDestroy::~EventUserThreadDestroy()
558 {
559 }
560
561 EventLWPDestroy::EventLWPDestroy(EventType::Time time_) :
562    EventThreadDestroy(EventType(time_, EventType::LWPDestroy))
563 {
564 }
565
566 EventLWPDestroy::~EventLWPDestroy()
567 {
568 }
569
570 EventFork::EventFork(EventType::Time time_, Dyninst::PID pid_) :
571    Event(EventType(time_, EventType::Fork)),
572    pid(pid_)
573 {
574 }
575
576 EventFork::~EventFork()
577 {
578 }
579
580 Dyninst::PID EventFork::getPID() const
581 {
582    return pid;
583 }
584
585 Process::const_ptr EventFork::getChildProcess() const
586 {
587    int_process *iproc = ProcPool()->findProcByPid(pid);
588    assert(iproc);
589    return iproc->proc();
590 }
591
592 EventRPC::EventRPC(rpc_wrapper *wrapper_) :
593    Event(EventType(EventType::None, EventType::RPC)),
594    wrapper(new rpc_wrapper(wrapper_))
595 {
596    int_rpc = new int_eventRPC();
597 }
598
599 EventRPC::~EventRPC()
600 {
601    memset(wrapper, 0, sizeof(wrapper));
602    delete wrapper;
603    wrapper = NULL;
604
605    if (int_rpc) {
606       delete int_rpc;
607       int_rpc = NULL;
608    }
609 }
610
611 IRPC::const_ptr EventRPC::getIRPC() const
612
613    return wrapper->rpc->getIRPC().lock();
614 }
615
616 bool EventRPC::suppressCB() const
617 {
618    if (Event::suppressCB()) return true;
619    return (wrapper->rpc->getIRPC().lock() == IRPC::ptr());
620 }
621
622 rpc_wrapper *EventRPC::getllRPC()
623 {
624    return wrapper;
625 }
626
627 int_eventRPC *EventRPC::getInternal() const
628 {
629    return int_rpc;
630 }
631
632 bool EventRPCLaunch::procStopper() const
633 {
634    int_process *proc = getProcess()->llproc();
635    int_thread *thrd = getThread()->llthrd();
636
637    if (!handled_by.empty())
638       return false;
639
640    int_iRPC::ptr rpc = thrd->nextPostedIRPC();
641    assert(rpc);
642
643    if (proc->plat_threadOpsNeedProcStop()) {
644       return !proc->getProcStopManager().processStoppedTo(int_thread::IRPCSetupStateID);
645    }
646    if (rpc->isProcStopRPC()) {
647       return !rpc->isRPCPrepped();
648    }
649    return !proc->getProcStopManager().threadStoppedTo(thrd, int_thread::IRPCSetupStateID);
650 }
651
652 EventRPCLaunch::EventRPCLaunch() :
653    Event(EventType(EventType::None, EventType::RPCLaunch))
654 {
655 }
656
657 EventRPCLaunch::~EventRPCLaunch()
658 {
659 }
660
661 EventSingleStep::EventSingleStep() :
662    Event(EventType(EventType::None, EventType::SingleStep))
663 {
664 }
665
666 EventSingleStep::~EventSingleStep()
667 {
668 }
669
670 EventBreakpointClear::EventBreakpointClear() :
671   Event(EventType(EventType::None, EventType::BreakpointClear))
672 {
673    int_bpc = new int_eventBreakpointClear();
674 }
675
676 EventBreakpointClear::~EventBreakpointClear()
677 {
678    assert(int_bpc);
679    delete int_bpc;
680    int_bpc = NULL;
681 }
682
683 int_eventBreakpointClear *EventBreakpointClear::getInternal() const
684 {
685    return int_bpc;
686 }
687
688 bool EventBreakpointClear::procStopper() const
689 {
690    if (!int_bpc->stopped_proc)
691       return false;
692
693    if (!handled_by.empty())
694       return false;
695
696    int_process *proc = getProcess()->llproc();
697    return !proc->getProcStopManager().processStoppedTo(int_thread::BreakpointStateID);
698 }
699
700 EventBreakpointRestore::EventBreakpointRestore(int_eventBreakpointRestore *iebpr) :
701    Event(EventType(EventType::None, EventType::BreakpointRestore)),
702    int_bpr(iebpr)
703 {
704 }
705
706 EventBreakpointRestore::~EventBreakpointRestore()
707 {
708    assert(int_bpr);
709    delete int_bpr;
710    int_bpr = NULL;
711 }
712
713 int_eventBreakpointRestore *EventBreakpointRestore::getInternal() const
714 {
715    return int_bpr;
716 }
717
718
719 EventLibrary::EventLibrary(const std::set<Library::ptr> &added_libs_,
720                            const std::set<Library::ptr> &rmd_libs_) :
721    Event(EventType(EventType::None, EventType::Library)),
722    added_libs(added_libs_),
723    rmd_libs(rmd_libs_)
724 {
725 }
726
727 EventLibrary::EventLibrary() :
728    Event(EventType(EventType::None, EventType::Library))
729 {
730 }
731
732 EventLibrary::~EventLibrary()
733 {
734 }
735
736 void EventLibrary::setLibs(const std::set<Library::ptr> &added_libs_,
737                            const std::set<Library::ptr> &rmd_libs_)
738 {
739    added_libs = added_libs_;
740    rmd_libs = rmd_libs_;
741 }
742
743 const std::set<Library::ptr> &EventLibrary::libsAdded() const
744 {
745    return added_libs;
746 }
747
748 const std::set<Library::ptr> &EventLibrary::libsRemoved() const
749 {
750    return rmd_libs;
751 }
752
753 EventAsync::EventAsync(int_eventAsync *ievent) :
754    Event(EventType(EventType::None, EventType::Async)),
755    internal(ievent)
756 {
757 }
758
759 EventAsync::~EventAsync()
760 {
761    if (internal) {
762       delete internal;
763       internal = NULL;
764    }
765 }
766
767 int_eventAsync *EventAsync::getInternal() const
768 {
769    return internal;
770 }
771
772 EventChangePCStop::EventChangePCStop() :
773    Event(EventType(EventType::None, EventType::ChangePCStop))
774 {
775 }
776
777 EventChangePCStop::~EventChangePCStop()
778 {
779 }
780
781 EventDetach::EventDetach() :
782    Event(EventType(EventType::None, EventType::Detach))
783 {
784    int_detach = new int_eventDetach();
785 }
786
787 EventDetach::~EventDetach()
788 {
789    if (int_detach)
790       delete int_detach;
791    int_detach = NULL;
792 }
793
794 int_eventDetach *EventDetach::getInternal() const
795 {
796    return int_detach;
797 }
798
799 bool EventDetach::procStopper() const
800 {
801    if (!handled_by.empty())
802       return false;
803
804    if (getInternal()->removed_bps)
805       return false;
806
807    int_process *proc = getProcess()->llproc();
808    return !proc->getProcStopManager().processStoppedTo(int_thread::DetachStateID);
809 }
810
811 EventIntBootstrap::EventIntBootstrap(void *d) :
812    Event(EventType(EventType::None, EventType::IntBootstrap)),
813    data(d)
814 {
815 }
816
817 EventIntBootstrap::~EventIntBootstrap()
818 {
819 }
820
821 void *EventIntBootstrap::getData() const
822 {
823    return data;
824 }
825
826 void EventIntBootstrap::setData(void *d)
827 {
828    data = d;
829 }
830
831 EventAsyncIO::EventAsyncIO(EventType et, int_eventAsyncIO *iev_) :
832    Event(et),
833    iev(iev_)
834 {
835 }
836
837 EventAsyncIO::~EventAsyncIO()
838 {
839    if (iev) {
840       delete iev;
841       iev = NULL;
842    }
843 }
844
845 bool EventAsyncIO::hadError() const
846 {
847    return iev->resp->hasError();
848 }
849
850 void *EventAsyncIO::getOpaqueVal() const
851 {
852    return iev->opaque_value;
853 }
854
855 int_eventAsyncIO *EventAsyncIO::getInternalEvent() const
856 {
857    return iev;
858 }
859
860 EventAsyncRead::EventAsyncRead(int_eventAsyncIO *iev_) :
861    EventAsyncIO(EventType(EventType::None, EventType::AsyncRead), iev_)
862 {
863 }
864
865 EventAsyncRead::~EventAsyncRead()
866 {
867 }
868
869 void *EventAsyncRead::getMemory() const
870 {
871    return iev->local_memory;
872 }
873
874 size_t EventAsyncRead::getSize() const
875 {
876    return iev->size;
877 }
878
879 Address EventAsyncRead::getAddress() const
880 {
881    return iev->remote_addr;
882 }
883
884 EventAsyncWrite::EventAsyncWrite(int_eventAsyncIO *iev_) :
885    EventAsyncIO(EventType(EventType::None, EventType::AsyncWrite), iev_)
886 {
887 }
888
889 EventAsyncWrite::~EventAsyncWrite()
890 {
891 }
892
893 size_t EventAsyncWrite::getSize() const
894 {
895    return iev->size;
896 }
897
898 Address EventAsyncWrite::getAddress() const
899 {
900    return iev->remote_addr;
901 }
902
903 EventAsyncReadAllRegs::EventAsyncReadAllRegs(int_eventAsyncIO *iev_) :
904    EventAsyncIO(EventType(EventType::None, EventType::AsyncReadAllRegs), iev_)
905 {
906 }
907
908 EventAsyncReadAllRegs::~EventAsyncReadAllRegs()
909 {
910 }
911
912 const RegisterPool &EventAsyncReadAllRegs::getRegisters() const
913 {
914    return *iev->rpool;
915 }
916
917 EventAsyncSetAllRegs::EventAsyncSetAllRegs(int_eventAsyncIO *iev_) :
918    EventAsyncIO(EventType(EventType::None, EventType::AsyncSetAllRegs), iev_)
919 {
920 }
921
922 EventAsyncSetAllRegs::~EventAsyncSetAllRegs()
923 {
924 }
925
926 EventNop::EventNop() :
927    Event(EventType(EventType::None, EventType::Nop))
928 {
929 }
930
931 EventNop::~EventNop()
932 {
933 }
934
935 EventThreadDB::EventThreadDB() :
936    Event(EventType(EventType::None, EventType::ThreadDB))
937 {
938    int_etdb = new int_eventThreadDB();
939 }
940
941 EventThreadDB::~EventThreadDB()
942 {
943    delete int_etdb;
944    int_etdb = NULL;
945 }
946
947 EventWinStopThreadDestroy::EventWinStopThreadDestroy(EventType::Time time_) :
948 EventThreadDestroy(EventType(time_, EventType::WinStopThreadDestroy)) {};
949
950 EventWinStopThreadDestroy::~EventWinStopThreadDestroy() {}
951
952 int_eventThreadDB *EventThreadDB::getInternal() const
953 {
954    return int_etdb;
955 }
956
957 bool EventThreadDB::triggersCB() const
958 {
959    EventType::Time ev_times[] = { EventType::None, EventType::Pre, EventType::Post };
960    int ev_types[] = { EventType::UserThreadCreate, EventType::UserThreadDestroy, 
961                       EventType::LWPCreate, EventType::LWPDestroy };
962    HandleCallbacks *cbhandler = HandleCallbacks::getCB();
963    for (unsigned i = 0; i < 3; i++)
964       for (unsigned j = 0; j < 4; j++)
965          if (cbhandler->hasCBs(EventType(ev_times[i], ev_types[j])))
966             return true;
967    return false;
968 }
969
970 EventControlAuthority::EventControlAuthority(EventType::Time t, int_eventControlAuthority *iev_) :
971    Event(EventType(t, EventType::ControlAuthority)),
972    iev(iev_)
973 {
974 }
975
976 EventControlAuthority::~EventControlAuthority()
977 {
978    if (iev && !iev->dont_delete) {
979       delete iev;
980    }
981    iev = NULL;
982 }
983
984 bool EventControlAuthority::procStopper() const
985 {
986    if (eventTrigger() == ControlUnset)
987       return false;
988    if (eventTrigger() == ControlGained)
989       return false;
990    if (iev->unset_desync)
991       return false;
992
993    int_process *proc = getProcess()->llproc();
994    int_thread *thr = getThread()->llthrd();
995
996    if (!iev->did_desync) {
997       pthrd_printf("Desyncing control authority state for EventControlAuthority\n");
998       thr->getControlAuthorityState().desyncStateProc(int_thread::stopped);
999       iev->did_desync = true;
1000    }
1001    return !proc->getProcStopManager().processStoppedTo(int_thread::ControlAuthorityStateID);
1002 }
1003
1004 std::string EventControlAuthority::otherToolName() const
1005 {
1006    return iev->toolname;
1007 }
1008  
1009 unsigned int EventControlAuthority::otherToolID() const
1010 {
1011    return iev->toolid;
1012 }
1013
1014 int EventControlAuthority::otherToolPriority() const
1015 {
1016    return iev->priority;
1017 }
1018
1019 EventControlAuthority::Trigger EventControlAuthority::eventTrigger() const
1020 {
1021    return iev->trigger;
1022 }
1023
1024 EventAsyncFileRead::EventAsyncFileRead(int_eventAsyncFileRead *iev_) :
1025    Event(EventType(EventType::None, EventType::AsyncFileRead)),
1026    iev(iev_)
1027 {
1028 }
1029
1030 EventAsyncFileRead::~EventAsyncFileRead()
1031 {
1032    delete iev;
1033 }
1034
1035 std::string EventAsyncFileRead::getFilename() const
1036 {
1037    return iev->filename;
1038 }
1039
1040 size_t EventAsyncFileRead::getReadSize() const
1041 {
1042    return iev->orig_size;
1043 }
1044
1045 Dyninst::Offset EventAsyncFileRead::getReadOffset() const
1046 {
1047    return iev->offset;
1048 }
1049
1050 void *EventAsyncFileRead::getBuffer() const
1051 {
1052    return iev->data;
1053 }
1054
1055 size_t EventAsyncFileRead::getBufferSize() const
1056 {
1057    return iev->size;
1058 }
1059
1060 int_eventAsyncFileRead *EventAsyncFileRead::getInternal()
1061 {
1062    return iev;
1063 }
1064
1065 int_eventControlAuthority *EventControlAuthority::getInternalEvent() const 
1066 {
1067    return iev;
1068 }
1069
1070 int_eventBreakpoint::int_eventBreakpoint(Address a, sw_breakpoint *, int_thread *thr) :
1071    addr(a),
1072    hwbp(NULL),
1073    thrd(thr),
1074    stopped_proc(false)
1075 {
1076 }
1077
1078 int_eventBreakpoint::int_eventBreakpoint(hw_breakpoint *i, int_thread *thr) :
1079    addr(i->getAddr()),
1080    hwbp(i),
1081    thrd(thr),
1082    stopped_proc(false)
1083 {
1084 }
1085
1086 int_eventBreakpoint::~int_eventBreakpoint()
1087 {
1088 }
1089
1090 bp_instance *int_eventBreakpoint::lookupInstalledBreakpoint()
1091 {
1092    if (hwbp)
1093       return static_cast<bp_instance *>(hwbp);
1094    else
1095       return static_cast<bp_instance *>(thrd->llproc()->getBreakpoint(addr));
1096 }
1097
1098 int_eventBreakpointClear::int_eventBreakpointClear() :
1099    started_bp_suspends(false),
1100    cached_bp_sets(false),
1101    set_singlestep(false),
1102    stopped_proc(false)
1103 {
1104 }
1105
1106 int_eventBreakpointClear::~int_eventBreakpointClear()
1107 {
1108 }
1109
1110 int_eventBreakpointRestore::int_eventBreakpointRestore(bp_instance *breakpoint_) :
1111    set_states(false),
1112    bp_resume_started(false),
1113    bp(breakpoint_)
1114 {
1115 }
1116
1117 int_eventBreakpointRestore::~int_eventBreakpointRestore()
1118 {
1119 }
1120
1121 int_eventNewLWP::int_eventNewLWP()
1122 {
1123 }
1124
1125 int_eventNewLWP::~int_eventNewLWP()
1126 {
1127 }
1128
1129 int_eventRPC::int_eventRPC()
1130 {
1131 }
1132
1133 int_eventRPC::~int_eventRPC()
1134 {
1135 }
1136
1137 void int_eventRPC::getPendingAsyncs(std::set<response::ptr> &pending)
1138 {
1139    if (alloc_regresult && !alloc_regresult->isReady()) {
1140       pending.insert(alloc_regresult);
1141    }
1142    if (memrestore_response && !memrestore_response->isReady()) {
1143       pending.insert(memrestore_response);
1144    }
1145    if (regrestore_response && !regrestore_response->isReady()) {
1146       pending.insert(regrestore_response);
1147    }
1148 }
1149
1150 int_eventAsync::int_eventAsync(response::ptr r)
1151 {
1152    resp.insert(r);
1153 }
1154
1155 int_eventAsync::~int_eventAsync()
1156 {
1157 }
1158
1159 set<response::ptr> &int_eventAsync::getResponses()
1160 {
1161    return resp;
1162 }
1163
1164 void int_eventAsync::addResp(response::ptr r)
1165 {
1166    resp.insert(r);
1167 }
1168
1169 int_eventNewUserThread::int_eventNewUserThread() :
1170    thr(NULL),
1171    lwp(NULL_LWP),
1172    raw_data(NULL),
1173    needs_update(true)
1174 {   
1175 }
1176
1177 int_eventNewUserThread::~int_eventNewUserThread()
1178 {
1179    if (raw_data)
1180       free(raw_data);
1181 }
1182
1183 int_eventThreadDB::int_eventThreadDB() :
1184    completed_new_evs(false)
1185 {
1186 }
1187
1188 int_eventThreadDB::~int_eventThreadDB()
1189 {
1190 }
1191
1192 int_eventDetach::int_eventDetach() :
1193    temporary_detach(false),
1194    removed_bps(false),
1195    done(false),
1196    had_error(false)
1197 {
1198 }
1199
1200 int_eventDetach::~int_eventDetach()
1201 {
1202 }
1203
1204 int_eventControlAuthority::int_eventControlAuthority(string toolname_, unsigned int toolid_,
1205                                                      int priority_, EventControlAuthority::Trigger trigger_) :
1206    toolname(toolname_),
1207    toolid(toolid_),
1208    priority(priority_),
1209    trigger(trigger_),
1210    control_lost(false),
1211    handled_bps(false),
1212    took_ca(false),
1213    did_desync(false),
1214    unset_desync(false),
1215    dont_delete(false),
1216    waiting_on_stop(false)
1217 {
1218 }
1219
1220 int_eventControlAuthority::int_eventControlAuthority()
1221 {   
1222 }
1223
1224 int_eventControlAuthority::~int_eventControlAuthority()
1225 {
1226 }
1227
1228 int_eventAsyncIO::int_eventAsyncIO(response::ptr resp_, asyncio_type iot_) : 
1229    resp(resp_),
1230    iot(iot_),
1231    rpool(NULL)
1232 {
1233    pthrd_printf("Creating int_eventAsyncIO at %p\n", this);
1234 }
1235
1236 int_eventAsyncIO::~int_eventAsyncIO()
1237 {
1238    pthrd_printf("Deleting int_eventAsyncIO at %p\n", this);
1239 }
1240
1241 int_eventAsyncFileRead::int_eventAsyncFileRead() :
1242    data(NULL),
1243    size(0),
1244    orig_size(0),
1245    to_free(NULL),
1246    offset(0),
1247    errorcode(0),
1248    whole_file(false),
1249    resp(NULL)
1250 {
1251 }
1252
1253 int_eventAsyncFileRead::~int_eventAsyncFileRead()
1254 {
1255    if (to_free)
1256       free(to_free);
1257 }
1258
1259 bool int_eventAsyncFileRead::isComplete()
1260 {
1261    if (!whole_file)
1262       return true;
1263    return (size != orig_size);
1264 }
1265
1266 #define DEFN_EVENT_CAST(NAME, TYPE) \
1267    NAME::ptr Event::get ## NAME() {  \
1268      if (etype.code() != EventType::TYPE) return NAME::ptr();  \
1269      return boost::static_pointer_cast<NAME>(shared_from_this()); \
1270    } \
1271    NAME::const_ptr Event::get ## NAME() const { \
1272      if (etype.code() != EventType::TYPE) return NAME::const_ptr();  \
1273      return boost::static_pointer_cast<const NAME>(shared_from_this()); \
1274    }
1275
1276 #define DEFN_EVENT_CAST2(NAME, TYPE, TYPE2) \
1277    NAME::ptr Event::get ## NAME() {  \
1278      if (etype.code() != EventType::TYPE && etype.code() != EventType::TYPE2) return NAME::ptr(); \
1279      return boost::static_pointer_cast<NAME>(shared_from_this()); \
1280    } \
1281    NAME::const_ptr Event::get ## NAME() const { \
1282      if (etype.code() != EventType::TYPE && etype.code() != EventType::TYPE2) return NAME::const_ptr(); \
1283      return boost::static_pointer_cast<const NAME>(shared_from_this()); \
1284    }
1285
1286 #define DEFN_EVENT_CAST3(NAME, TYPE, TYPE2, TYPE3) \
1287    NAME::ptr Event::get ## NAME() {  \
1288      if (etype.code() != EventType::TYPE && etype.code() != EventType::TYPE2 && etype.code() != EventType::TYPE3) return NAME::ptr(); \
1289      return boost::static_pointer_cast<NAME>(shared_from_this()); \
1290    } \
1291    NAME::const_ptr Event::get ## NAME() const { \
1292      if (etype.code() != EventType::TYPE && etype.code() != EventType::TYPE2 && etype.code() != EventType::TYPE3) return NAME::const_ptr(); \
1293      return boost::static_pointer_cast<const NAME>(shared_from_this()); \
1294    }
1295
1296 #define DEFN_EVENT_CAST4(NAME, TYPE, TYPE2, TYPE3, TYPE4)   \
1297    NAME::ptr Event::get ## NAME() {  \
1298       if (etype.code() != EventType::TYPE && etype.code() != EventType::TYPE2 && etype.code() != EventType::TYPE3 && etype.code() != EventType::TYPE4) return NAME::ptr(); \
1299      return boost::static_pointer_cast<NAME>(shared_from_this()); \
1300    } \
1301    NAME::const_ptr Event::get ## NAME() const { \
1302       if (etype.code() != EventType::TYPE && etype.code() != EventType::TYPE2 && etype.code() != EventType::TYPE3 && etype.code() != EventType::TYPE4) return NAME::const_ptr(); \
1303      return boost::static_pointer_cast<const NAME>(shared_from_this()); \
1304    }
1305
1306
1307 DEFN_EVENT_CAST4(EventAsyncIO, AsyncRead, AsyncWrite, AsyncReadAllRegs, AsyncSetAllRegs)
1308 DEFN_EVENT_CAST3(EventTerminate, Exit, Crash, ForceTerminate)
1309 DEFN_EVENT_CAST2(EventNewThread, UserThreadCreate, LWPCreate)
1310 DEFN_EVENT_CAST2(EventThreadDestroy, UserThreadDestroy, LWPDestroy)
1311 DEFN_EVENT_CAST(EventExit, Exit)
1312 DEFN_EVENT_CAST(EventCrash, Crash)
1313 DEFN_EVENT_CAST(EventForceTerminate, ForceTerminate)
1314 DEFN_EVENT_CAST(EventExec, Exec)
1315 DEFN_EVENT_CAST(EventStop, Stop)
1316 DEFN_EVENT_CAST(EventBreakpoint, Breakpoint)
1317 DEFN_EVENT_CAST(EventNewUserThread, UserThreadCreate)
1318 DEFN_EVENT_CAST(EventNewLWP, LWPCreate)
1319 DEFN_EVENT_CAST(EventUserThreadDestroy, UserThreadDestroy)
1320 DEFN_EVENT_CAST(EventLWPDestroy, LWPDestroy)
1321 DEFN_EVENT_CAST(EventFork, Fork)
1322 DEFN_EVENT_CAST(EventSignal, Signal)
1323 DEFN_EVENT_CAST(EventBootstrap, Bootstrap)
1324 DEFN_EVENT_CAST(EventRPC, RPC)
1325 DEFN_EVENT_CAST(EventSingleStep, SingleStep)
1326 DEFN_EVENT_CAST(EventBreakpointClear, BreakpointClear)
1327 DEFN_EVENT_CAST(EventBreakpointRestore, BreakpointRestore)
1328 DEFN_EVENT_CAST(EventLibrary, Library)
1329 DEFN_EVENT_CAST(EventRPCLaunch, RPCLaunch)
1330 DEFN_EVENT_CAST(EventAsync, Async)
1331 DEFN_EVENT_CAST(EventChangePCStop, ChangePCStop)
1332 DEFN_EVENT_CAST(EventPreBootstrap, PreBootstrap)
1333 DEFN_EVENT_CAST(EventDetach, Detach)
1334 DEFN_EVENT_CAST(EventIntBootstrap, IntBootstrap)
1335 DEFN_EVENT_CAST(EventNop, Nop)
1336 DEFN_EVENT_CAST(EventThreadDB, ThreadDB)
1337 DEFN_EVENT_CAST(EventWinStopThreadDestroy, WinStopThreadDestroy)
1338 DEFN_EVENT_CAST(EventControlAuthority, ControlAuthority)
1339 DEFN_EVENT_CAST(EventAsyncRead, AsyncRead)
1340 DEFN_EVENT_CAST(EventAsyncWrite, AsyncWrite)
1341 DEFN_EVENT_CAST(EventAsyncReadAllRegs, AsyncReadAllRegs)
1342 DEFN_EVENT_CAST(EventAsyncSetAllRegs, AsyncSetAllRegs)
1343 DEFN_EVENT_CAST(EventAsyncFileRead, AsyncFileRead)