def test_trace_filtering(self): """Test that trace hook is triggering event callbacks with filtering.""" global ignore_filter ignore_filter = tracefilter.TraceFilter() tracer.clear_hooks_and_stop() self.assertEqual( 1, tracer.add_hook(my_trace_dispatch, { 'start': True, 'event_set': frozenset(('call', )) })) def foo(): pass foo() tracer.stop() global trace_lines import pprint # for entry in trace_lines: # print entry.event, entry.filename, entry.lineno, entry.name self.assertTrue( len(trace_lines) >= 2, 'Should have captured some trace output') for i, right in [(-1, ( 'call', 'stop', )), (-2, ( 'call', 'foo', ))]: self.assertEqual(right, ( trace_lines[i].event, trace_lines[i].name, )) return
def test_basic(self): """Basic sanity and status testing.""" tracer.HOOKS = [] self.assertEqual(0, tracer.size()) self.assertEqual(False, tracer.is_started()) tracer.start() self.assertEqual(True, tracer.is_started()) tracer.stop() self.assertEqual(False, tracer.is_started()) self.assertEqual(1, tracer.add_hook(my_trace_dispatch, {'backlevel': 1})) self.assertEqual(0, len(trace_lines)) tracer.start() self.assertEqual(0, len(trace_lines)) self.assertEqual(True, tracer.is_started()) self.assertEqual( 0, tracer.remove_hook(my_trace_dispatch, stop_if_empty=True)) self.assertEqual(False, tracer.is_started()) self.assertEqual( 1, tracer.add_hook(my_trace_dispatch, { 'start': True, 'backlevel': 1 })) self.assertEqual(True, tracer.is_started()) tracer.clear_hooks_and_stop() return
def run(globalOptions: dict): global pyProc signal.signal(signal.SIGINT, handler) signal.signal(signal.SIGTERM, handler) options = globalOptions if options.get('cmdline_args').get('bypass', False): cmd = options.get('control').get('cmd') logging.info("Bypass vaitrace, just run cmd") pyCmd = options.get('control').get('cmd') pyRunCtx(pyCmd) exit(0) """Preparing""" tracer.prepare(options) tracer.start() """requirememt format: ["tracerName", "tracerName1", "hwInfo", ...]""" collector.prepare(options, tracer.getSourceRequirement()) collector.start() """Start Running""" pyCmd = options.get('control').get('cmd') timeout = options.get('control').get('timeout') # pyRunCtx(pyCmd) pyProc = Thread(target=pyRunCtx, args=(pyCmd,)) pyProc.start() options['control']['launcher'] = "python" options['control']['pid'] = os.getpid() options['control']['time'] = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()) if timeout <= 0: pyProc.join() else: while timeout > 0: time.sleep(1) timeout -= 1 p = pyProc.is_alive() if p == False: break logging.info("vaitrace timeout, waiting for Python thread terminated") pyProc.join() collector.stop() tracer.stop() tracer.process(collector.getData())
def run(globalOptions: dict): signal.signal(signal.SIGINT, handler) signal.signal(signal.SIGTERM, handler) options = globalOptions """Help find the path of cmd in PATH""" cmd = options.get('control').get('cmd')[0] options['control']['cmd'][0] = shell_find_exec_path(cmd) if options.get('cmdline_args').get('bypass', False): cmd = options.get('control').get('cmd') logging.info("Bypass vaitrace, just run cmd") proc = Popen(cmd) proc.wait() exit(0) """Preparing""" tracer.prepare(options) tracer.start() """requirememt format: ["tracerName", "tracerName1", "hwInfo", ...]""" collector.prepare(options, tracer.getSourceRequirement()) collector.start() """Start Running""" cmd = options.get('control').get('cmd') timeout = options.get('control').get('timeout') proc = Popen(cmd) options['control']['launcher'] = "cpp" options['control']['pid'] = proc.pid options['control']['time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) if timeout <= 0: proc.wait() else: while timeout > 0: time.sleep(1) timeout -= 1 p = proc.poll() if p is not None: break logging.info("vaitrace time out, stopping process...") proc.send_signal(signal.SIGINT) proc.wait() collector.stop() tracer.stop() tracer.process(collector.getData())
def test_trace(self): """Test that trace hook is triggering event callbacks without filtering.""" tracer.clear_hooks_and_stop() self.assertEqual( 1, tracer.add_hook(my_trace_dispatch, { 'start': True, 'backlevel': 1 })) def squares(): j = 1 for i in range(5): j += j + 2 pass return squares() tracer.stop() global trace_lines import pprint # for entry in trace_lines: # print entry.event, entry.filename, entry.lineno, entry.name self.assertTrue( len(trace_lines) >= 5, 'Should have captured some trace output') for i, right in [(-1, ( 'return', 'squares', )), (-2, ( 'line', 'squares', ))]: self.assertEqual(right, ( trace_lines[i].event, trace_lines[i].name, )) return