def doneGetContext(self, token, error, ctxs): if error: protocol.log("Error from StackTrace.getContext", error) return if ctxs: for ctx in ctxs: print ctx
def doneGet(self, token, error, mmap): if error: protocol.log("Error from PathMap.get", error) else: print(mmap) with lock: lock.notify()
def doneGet(self, token, error, mmap): if error: protocol.log("Error from PathMap.get", error) else: print mmap with lock: lock.notify()
def doneGetChildren(self, token, error, context_ids): if error: protocol.log("Error from Processes.GetChildren", error) else: print("Processes: " + str(context_ids)) with lock: lock.notify()
def __init__(self, service, name, data): self.service = service self.name = name try: self.args = channel.fromJSONSequence(data) except Exception as x: protocol.log("Error decoding event data", x)
def _log(self, msg, x): if not self._alive: return # Don't report same error multiple times to avoid filling up the log file. with self._error_log_lock: if msg in self.error_log: return self.error_log.add(msg) protocol.log(msg, x)
def doneCommand(self, token, error): if error: protocol.log("Error from RunControl.resume", error) else: context.suspend(runcontrol.DoneCommand()) with lock: lock.notify()
def doneGetContext(self, token, error, ctx): pending.remove(token) if error: protocol.log("Error from Registers.getContext", error) else: print ctx if ctx.isReadable() and not ctx.isReadOnce() and ctx.getSize() >= 2: locs = [] locs.append(registers.Location(ctx.getID(), 0, 1)) locs.append(registers.Location(ctx.getID(), 1, 1)) class DoneGetM(registers.DoneGet): def doneGet(self, token, error, value): pending.remove(token) if error: protocol.log("Error from Registers.getm", error) else: print "getm", ctx.getID(), map(ord, value) if not pending: onDone() pending.append(regs.getm(locs, DoneGetM())) if ctx.isWriteable() and not ctx.isWriteOnce() and ctx.getSize() >= 2: locs = [] locs.append(registers.Location(ctx.getID(), 0, 1)) locs.append(registers.Location(ctx.getID(), 1, 1)) class DoneSetM(registers.DoneSet): def doneGet(self, token, error): pending.remove(token) if error: protocol.log("Error from Registers.setm", error) if not pending: onDone() pending.append(regs.setm(locs, (255, 255), DoneSetM())) if not pending: onDone()
def doneGetContext(self, token, error, context): pending.remove(token) if error: protocol.log("Error from RunControl.getContext", error) else: print(context) class DoneGetState(runcontrol.DoneGetState): def doneGetState(self, token, error, suspended, pc, reason, params): pending.remove(token) if error: protocol.log("Error from RunControl.getState", error) else: print("suspended: " + str(suspended)) print("pc: " + str(pc)) print("reason: " + str(reason)) print("params: " + str(params)) if suspended: _suspended.append(context.getID()) if len(pending) == 0: with lock: lock.notify() if context and context.hasState(): pending.append(context.getState(DoneGetState())) if len(pending) == 0: with lock: lock.notify()
def doneGetChildren(self, token, error, context_ids): if error: protocol.log("Error from Processes.GetChildren", error) else: print "Processes:", context_ids with lock: lock.notify()
def doneGetContext(self, token, error, context): pending.remove(token) if error: protocol.log("Error from RunControl.getContext", error) else: print context class DoneGetState(runcontrol.DoneGetState): def doneGetState(self, token, error, suspended, pc, reason, params): pending.remove(token) if error: protocol.log( "Error from RunControl.getState", error) else: print "suspended: ", suspended print "pc: ", pc print "reason: ", reason print "params: ", params if suspended: _suspended.append(context.getID()) if len(pending) == 0: with lock: lock.notify() if context and context.hasState(): pending.append(context.getState(DoneGetState())) if len(pending) == 0: with lock: lock.notify()
def doneGet(self, token, error, value): pending.remove(token) if error: protocol.log("Error from Registers.getm", error) else: print "getm", ctx.getID(), map(ord, value) if not pending: onDone()
def doneGet(self, token, error): pending.remove(token) if error: protocol.log( "Error from Registers.setm", error) if not pending: onDone()
def doneGetContext(self, token, error, ctxs): if error: protocol.log("Error from StackTrace.getContext", error) return if ctxs: for ctx in ctxs: print(ctx)
def __traceMessageReceived(self, m): for l in self.trace_listeners: try: id = None if m.token is not None: id = m.token.getID() l.onMessageReceived(m.type, id, m.service, m.name, m.data) except Exception as x: protocol.log("Exception in channel listener", x)
def doneGetContext(self, token, error, context): pending.remove(token) if error: protocol.log("Error from SysMonitor.getContext", error) else: processes.append(context) if not pending: with lock: lock.notify()
def doneGetContext(self, token, error, context): pending.remove(token) if error: protocol.log("Error from Memory.getContext", error) else: print context if len(pending) == 0: with lock: lock.notify()
def doneGetContext(self, token, error, context): pending.remove(token) if error: protocol.log("Error from Memory.getContext", error) else: print(context) if len(pending) == 0: with lock: lock.notify()
def doneCommand(self, token, error): """Called when run control command execution is complete. :param token: pending command handle. :param error: command execution error or **None**. """ if error: protocol.log("Error from RunContext.resume", error) with condition: condition.notify()
def doneGetChildren(self, token, error, context_ids): pending.remove(token) if error: protocol.log("Error from RunControl.GetChildren", error) else: for c in context_ids: contexts.append(c) pending.append(rc.getChildren(c, self)) if len(pending) == 0: cache.set(None, None, contexts)
def doneGetContext(self, token, error, context): pending.remove(token) if error: protocol.log( "Error from SysMonitor.getContext", error) else: processes.append(context) if not pending: with lock: lock.notify()
def doneSubscribe(self, token, error): """Called when stream subscription is done. :param token: pending command handle :param error: error description if operation failed, **None** if succeeded. """ if error: protocol.log("Error from streams.subscribe()", error) with condition: condition.notify()
def trace(msg): """ Trace hooks should use this method to log a message. It prepends the message with a timestamp and sends it to the TCF logger facility. The logger implementation may or may not inject its own timestamp. For tracing, we definitely need one, so we introduce a minimal, relative-time stamp. @param msg the trace message """ protocol.log('%s %s' % (getDebugTime(), msg))
def sendPeerRemovedEvent(self): for l in protocol.getLocator().getListeners(): try: l.peerRemoved(self.rw_attrs.get(ATTR_ID)) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) try: args = [self.rw_attrs.get(ATTR_ID)] protocol.sendEvent(locator.NAME, "peerRemoved", json.dumps(args)) except IOError as x: protocol.log("Locator: failed to send 'peerRemoved' event", x)
def doneGetChildren(self, token, error, context_ids): pending.remove(token) if error: protocol.log("Error from RunControl.GetChildren", error) else: for c in context_ids: pending.append(rctrl.getContext(c, DoneGetContext())) pending.append(rctrl.getChildren(c, self)) if len(pending) == 0: with lock: lock.notify()
def sendPeerAddedEvent(self): for l in protocol.getLocator().getListeners(): try: l.peerAdded(self) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) try: args = [self.rw_attrs] protocol.sendEvent(locator.NAME, "peerAdded", json.dumps(args)) except IOError as x: protocol.log("Locator: failed to send 'peerAdded' event", x) self.last_heart_beat_time = int(time.time() * 1000)
def doneGet(self, token, error, value): pending.remove(token) if error: protocol.log( "Error from Registers.getm", error) else: print("getm " + str(ctx.getID()) + " " + str(list(map(int, value)))) if not pending: onDone()
def _close(self, error): assert self.state != STATE_CLOSED self.state = STATE_CLOSED # Closing channel underlying streams can block for a long time, # so it needs to be done by a background thread. thread = threading.Thread(target=self.stop, name="TCF Channel Cleanup") thread.daemon = True thread.start() if error and isinstance(self.remote_peer, peer.AbstractPeer): self.remote_peer.onChannelTerminated() if self.registered_with_trasport: self.registered_with_trasport = False transport.channelClosed(self, error) if self.proxy: try: self.proxy.onChannelClosed(error) except Exception as x: protocol.log("Exception in channel listener", x) channel = self class Runnable(object): def __call__(self): if channel.out_tokens: x = None if isinstance(error, Exception): x = error elif error: x = Exception(error) else: x = IOError("Channel is closed") for msg in channel.out_tokens.values(): try: s = str(msg) if len(s) > 72: s = s[:72] + "...]" y = IOError("Command " + s + " aborted") # y.initCause(x) msg.token.getListener().terminated(msg.token, y) except Exception as e: protocol.log("Exception in command listener", e) channel.out_tokens.clear() if channel.channel_listeners: for l in channel.channel_listeners: if not l: break try: l.onChannelClosed(error) except Exception as x: protocol.log("Exception in channel listener", x) elif error: protocol.log("TCF channel terminated", error) if channel.trace_listeners: for l in channel.trace_listeners: try: l.onChannelClosed(error) except Exception as x: protocol.log("Exception in channel listener", x) protocol.invokeLater(Runnable())
def doneGetContext(self, token, error, ctx): pending.remove(token) if error: protocol.log("Error from Registers.getContext", error) else: print(ctx) if ctx.isReadable() and not ctx.isReadOnce() \ and ctx.getSize() >= 2: locs = [] locs.append( registers.Location(ctx.getID(), 0, 1)) locs.append( registers.Location(ctx.getID(), 1, 1)) class DoneGetM(registers.DoneGet): def doneGet(self, token, error, value): pending.remove(token) if error: protocol.log( "Error from Registers.getm", error) else: print("getm " + str(ctx.getID()) + " " + str(list(map(int, value)))) if not pending: onDone() pending.append(regs.getm(locs, DoneGetM())) if ctx.isWriteable() and not ctx.isWriteOnce() \ and ctx.getSize() >= 2: locs = [] locs.append( registers.Location(ctx.getID(), 0, 1)) locs.append( registers.Location(ctx.getID(), 1, 1)) class DoneSetM(registers.DoneSet): def doneGet(self, token, error): pending.remove(token) if error: protocol.log( "Error from Registers.setm", error) if not pending: onDone() pending.append( regs.setm(locs, (255, 255), DoneSetM())) if not pending: onDone()
def event(self, name, data): try: args = channel.fromJSONSequence(data) if name == "peerAdded": assert len(args) == 1 _peer = Peer(self.channel.getRemotePeer(), args[0]) if self.proxy.peers.get(_peer.getID()): protocol.log("Invalid peerAdded event", Exception()) return self.proxy.peers[_peer.getID()] = _peer for l in self.proxy.listeners: try: l.peerAdded(_peer) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) elif name == "peerChanged": assert len(args) == 1 m = args[0] if not m: raise Exception("Locator service: invalid peerChanged event - no peer ID") _peer = self.proxy.peers.get(m.get(peer.ATTR_ID)) if not _peer: return self.proxy.peers[_peer.getID()] = _peer for l in self.proxy.listeners: try: l.peerChanged(_peer) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) elif name == "peerRemoved": assert len(args) == 1 id = args[0] _peer = self.proxy.peers.get(id) if not _peer: return del self.proxy.peers[id] for l in self.proxy.listeners: try: l.peerRemoved(id) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) elif name == "peerHeartBeat": assert len(args) == 1 id = args[0] _peer = self.proxy.peers.get(id) if not _peer: return for l in self.proxy.listeners: try: l.peerHeartBeat(id) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) else: raise IOError("Locator service: unknown event: " + name) except Exception as x: self.channel.terminate(x)
def doneGetChildren(self, token, error, ctx_ids): if error: protocol.log("Error from StackTrace.getChildren", error) return class DoneGetContext(stacktrace.DoneGetContext): def doneGetContext(self, token, error, ctxs): if error: protocol.log("Error from StackTrace.getContext", error) return if ctxs: for ctx in ctxs: print ctx stack.getContext(ctx_ids, DoneGetContext())
def onChannelOpened(channel, service_names, services_by_name): with _lock: for name in service_names: for provider in _providers: try: service = provider.getServiceProxy(channel, name) if not service: continue services_by_name[name] = service break except Exception as x: protocol.log("Error calling TCF service provider", x) if name in services_by_name: continue services_by_name[name] = GenericProxy(channel, name)
def _onSocketConnected(self, x): if x: self.terminate(x) self.closed = True if self.closed: try: if self.socket: self.socket.close() except socket.error as y: protocol.log("Cannot close socket", y) else: self.started = True self.start()
def doneList(self, token, error, ctx_ids): if error: protocol.log("Error from Symbols.list", error) return class DoneGetContext(symbols.DoneGetContext): def doneGetContext(self, token, error, ctx): if error: protocol.log("Error from Symbols.getContext", error) return print ctx if ctx_ids: for ctx_id in ctx_ids: syms.getContext(ctx_id, DoneGetContext())
def doneGetContext(self, token, error, context): if error: protocol.log("Error from RunControl.getContext", error) with lock: lock.notify() return class DoneResume(runcontrol.DoneCommand): def doneCommand(self, token, error): if error: protocol.log("Error from RunControl.resume", error) else: context.suspend(runcontrol.DoneCommand()) with lock: lock.notify() context.resume(runcontrol.RM_RESUME, 1, None, DoneResume())
def onChannelCreated(channel, services_by_name): with _lock: # TODO ZeroCopy support is incomplete # zero_copy = ZeroCopy() # services_by_name[zero_copy.getName()] = zero_copy for provider in _providers: try: arr = provider.getLocalService(channel) if not arr: continue for service in arr: if service.getName() in services_by_name: continue services_by_name[service.getName()] = service except Exception as x: protocol.log("Error calling TCF service provider", x);
def getServiceProxy(self, channel, service_name): service = None try: clsName = service_name + "Proxy" package = self.package_base + "." + clsName clsModule = __import__(package, fromlist=[clsName], globals=globals()) cls = clsModule.__dict__.get(clsName) service = cls(channel) assert service_name == service.getName() except ImportError: pass except Exception as x: protocol.log("Cannot instantiate service proxy for "+service_name, x) return service
def doneStart(self, token, error, process): """Called when process start is done. :param token: pending command handle. :param error: error description if operation failed, **None** if succeeded. :param process: ProcessContext object representing the started process. """ if error: protocol.log("Error from Processes.start", error) else: val.setValue(process) with condition: condition.notify()
def doneGetChildren(self, token, error, ctx_ids): if error: protocol.log("Error from StackTrace.getChildren", error) return class DoneGetContext(stacktrace.DoneGetContext): def doneGetContext(self, token, error, ctxs): if error: protocol.log("Error from StackTrace.getContext", error) return if ctxs: for ctx in ctxs: print(ctx) stack.getContext(ctx_ids, DoneGetContext())
def doneList(self, token, error, ctx_ids): if error: protocol.log("Error from Symbols.list", error) return class DoneGetContext(symbols.DoneGetContext): def doneGetContext(self, token, error, ctx): if error: protocol.log("Error from Symbols.getContext", error) return print(ctx) if ctx_ids: for ctx_id in ctx_ids: syms.getContext(ctx_id, DoneGetContext())
def doneGetChildren(self, token, error, ids): """Called when context list retrieval is done. :param token: pending command handle. :param error: error description if operation failed, **None** if succeeded. :param context_ids: array of available context IDs. """ if error: protocol.log( "Error from " + service.getName() + ".getContext()", error) else: val.setValue(ids) with condition: condition.notify()
def doneGetContext(self, token, error, context): """Called when context data retrieval is done. :param token: pending command handle. :param error: error description if operation failed, **None** if succeeded. :param context: context data. """ if error: protocol.log( "Error from " + service.getName() + ".getContext()", error) else: val.setValue(context) with condition: condition.notify()
def doneGetState(self, token, error, suspended, pc, reason, params): """Called when RunControlContext.getState() command execution is complete. :param token: pending command handle. :param error: command execution error or None. :param suspended: true if the context is suspended :param pc: program counter of the context (if suspended). :param reason: suspend reason (if suspended), see REASON_*. :param params: additional target specific data about context state, see STATE_*. """ if error: protocol.log("Error from runcontrol.getState()", error) else: val.setValue((suspended, pc, reason, params)) with condition: condition.notify()
def doneGetIDs(token, error, ids): if error: protocol.log("Error from Breakpoints.getIDs", error) return print("Breakpoints : " + str(ids)) def doneGetProperties(token, error, props): if error: protocol.log("Error from Breakpoints.getProperties", error) return print("Breakpoint Properties: " + str(props)) def doneGetStatus(token, error, props): if error: protocol.log("Error from Breakpoints.getStatus", error) return print("Breakpoint Status: " + str(props)) for bpid in ids: bps.getProperties(bpid, doneGetProperties) bps.getStatus(bpid, doneGetStatus)
def doneGetChildren(self, token, error, context_ids): pending.remove(token) if error: protocol.log("Error from SysMonitor.getChildren", error) else: class DoneGetContext(sysmonitor.DoneGetContext): def doneGetContext(self, token, error, context): pending.remove(token) if error: protocol.log( "Error from SysMonitor.getContext", error) else: processes.append(context) if not pending: with lock: lock.notify() for ctx_id in context_ids: pending.append(sm.getContext(ctx_id, DoneGetContext())) if not pending: with lock: lock.notify()
def test(): global _services protocol.startEventQueue() atexit.register(protocol.getEventQueue().shutdown) # testTimer() try: c = tcf.connect("TCP:127.0.0.1:1534") except Exception as e: protocol.log(e) sys.exit() assert c.state == channel.STATE_OPEN if __TRACE: protocol.invokeAndWait(c.addTraceListener, TraceListener()) _services = sorted(protocol.invokeAndWait(c.getRemoteServices)) print("services=" + str(_services)) if "RunControl" in _services: # RunControl must be first _services.remove("RunControl") _services.insert(0, "RunControl") for service in _services: testFct = globals().get("test" + service) if testFct: print("Testing service '%s'..." % service) try: testFct(c) print("Completed test of service '%s'." % service) except Exception as e: protocol.log("Exception testing %s" % service, e) else: print("No test for service '%s' found." % service) try: testSyncCommands(c) testTasks(c) testEvents(c) testDataCache(c) except Exception as e: protocol.log(e) if c.state == channel.STATE_OPEN: time.sleep(5) protocol.invokeLater(c.close) time.sleep(2)
def doneGetContext(self, token, error, ctx): if error: protocol.log("Error from Symbols.getContext", error) return print(ctx)
condition=lock) lock.wait(10) # --------------------------------------------------------------------------- # # TCF initialisation protocol.startEventQueue() protocol.setLogger(TcfProtocolLogger()) atexit.register(protocol.getEventQueue().shutdown) try: c = tcf.connect("TCP:127.0.0.1:1534") except Exception as e: protocol.log(e) sys.exit() # If there is a streams service, listen to it streamsSvc = getService(c, streams.NAME) if streamsSvc: subscribe(streamsSvc, 'ProcessesV1', StreamsListener(streamsSvc)) p = start(c, '/bin/ls', '-l', '-a') # this part is a bit tricky, the runcontrol contexts which accept a resume # command are the contexts which have a state. Recursively try to find the # runcontrol context which has a state, and resume it. rcSvc = getService(c, runcontrol.NAME)
def doneSet(self, token, error): if error: protocol.log("Error from PathMap.set", error) with lock: lock.notify()
def doneSet(token, error): if error: protocol.log("Error from Breakpoints.set", error) return
def doneGetStatus(token, error, props): if error: protocol.log("Error from Breakpoints.getStatus", error) return print("Breakpoint Status: " + str(props))