Package vtrace :: Module notifiers
[hide private]
[frames] | no frames]

Source Code for Module vtrace.notifiers

  1  """ 
  2  Vtrace notitifers base classes and examples 
  3   
  4  Vtrace supports the idea of callback notifiers which 
  5  get called whenever particular events occur in the target 
  6  process.  Notifiers may be registered to recieve a callback 
  7  on any of the vtrace.NOTIFY_FOO events from vtrace.  One notifier 
  8  *may* be registered with more than one trace, as the "notify" 
  9  method is passed a reference to the trace for which an event 
 10  has occured... 
 11   
 12  """ 
 13  # Copyright (C) 2007 Invisigoth - See LICENSE file for details 
 14   
 15  import vtrace 
 16  import traceback 
 17   
18 -class Notifier(object):
19 """ 20 The top level example notifier... Anything which registers 21 itself for trace events or tracegroup events should implement 22 the notify method as shown here. 23 """ 24
25 - def __init__(self):
26 """ 27 All extenders *must* call this. Mostly because all the 28 goop necissary for the remote debugging stuff... 29 (if notifier is instantiated on server, all is well, if it's 30 on the client it needs a proxy...) 31 """ 32 pass
33
34 - def handleEvent(self, event, trace):
35 """ 36 An "internal" handler so if we need to do something 37 from an API perspective before calling the notify method 38 we can have a good "all at once" hook 39 """ 40 self.notify(event, trace)
41
42 - def notify(self, event, trace):
43 print "Got event: %d from pid %d" % (event, trace.getPid())
44
45 -class VerboseNotifier(Notifier):
46 - def notify(self, event, trace):
47 print "PID %d thread(%d) got" % (trace.getPid(), trace.getMeta("ThreadId")), 48 if event == vtrace.NOTIFY_ALL: 49 print "WTF, how did we get a vtrace.NOTIFY_ALL event?!?!" 50 elif event == vtrace.NOTIFY_SIGNAL: 51 signo = trace.getCurrentSignal() 52 print "vtrace.NOTIFY_SIGNAL %d (0x%.8x)" % (signo, signo) 53 if trace.getMeta("Platform") == "Windows": 54 print repr(trace.getMeta("Win32Event")) 55 elif event == vtrace.NOTIFY_BREAK: 56 print "vtrace.NOTIFY_BREAK" 57 elif event == vtrace.NOTIFY_SYSCALL: 58 print "vtrace.NOTIFY_SYSCALL" 59 elif event == vtrace.NOTIFY_CONTINUE: 60 print "vtrace.NOTIFY_CONTINUE" 61 elif event == vtrace.NOTIFY_EXIT: 62 print "vtrace.NOTIFY_EXIT" 63 print "ExitCode",trace.getMeta("ExitCode") 64 elif event == vtrace.NOTIFY_ATTACH: 65 print "vtrace.NOTIFY_ATTACH" 66 elif event == vtrace.NOTIFY_DETACH: 67 print "vtrace.NOTIFY_DETACH" 68 elif event == vtrace.NOTIFY_LOAD_LIBRARY: 69 print "vtrace.NOTIFY_LOAD_LIBRARY" 70 elif event == vtrace.NOTIFY_UNLOAD_LIBRARY: 71 print "vtrace.NOTIFY_UNLOAD_LIBRARY" 72 elif event == vtrace.NOTIFY_CREATE_THREAD: 73 print "vtrace.NOTIFY_CREATE_THREAD" 74 elif event == vtrace.NOTIFY_EXIT_THREAD: 75 print "vtrace.NOTIFY_EXIT_THREAD" 76 print "ExitThread",trace.getMeta("ExitThread", -1) 77 elif event == vtrace.NOTIFY_STEP: 78 print "vtrace.NOTIFY_STEP" 79 else: 80 print "vtrace.NOTIFY_WTF_HUH?"
81
82 -class DistributedNotifier(Notifier):
83 """ 84 A notifier which will distributed notifications out to 85 locally registered notifiers so that remote tracer's notifier 86 callbacks only require once across the wire. 87 """ 88 # NOTE: once you turn on vtrace.NOTIFY_ALL it can't be turned back off yet.
89 - def __init__(self):
90 Notifier.__init__(self) 91 self.shared = False 92 self.events = [] 93 self.notifiers = {} 94 for i in range(vtrace.NOTIFY_MAX): 95 self.notifiers[i] = []
96
97 - def getProxy(self, trace):
98 host,nothing = cobra.getCobraSocket(trace).getLocalName()
99
100 - def notify(self, event, trace):
101 self.fireNotifiers(event, trace)
102
103 - def fireNotifiers(self, event, trace):
104 """ 105 Fire all our registerd local-notifiers 106 """ 107 nlist = self.notifiers.get(vtrace.NOTIFY_ALL, []) 108 for notifier in nlist: 109 try: 110 notifier.handleEvent(event, trace) 111 except: 112 print "ERROR - Exception in notifier:",traceback.format_exc() 113 114 nlist = self.notifiers.get(event, []) 115 for notifier in nlist: 116 try: 117 notifier.handleEvent(event, trace) 118 except: 119 print "ERROR - Exception in notifier:",traceback.format_exc()
120
121 - def registerNotifier(self, event, notif):
122 """ 123 Register a sub-notifier to get the remote callback's via 124 our local delivery. 125 """ 126 nlist = self.notifiers.get(event) 127 nlist.append(notif)
128
129 - def deregisterNotifier(self, event, notif):
130 nlist = self.notifiers.get(event) 131 nlist.remove(notif)
132