Example #1
0
    def go(self) -> ReturnCode:
        options = self.context.options

        with Timer('loading debugger'):
            debugger = Debuggers(self.context).load(options.debugger,
                                                    self.dextIR)
            self.dextIR.debugger = debugger.debugger_info

        with Timer('running debugger'):
            if not debugger.is_available:
                msg = '<d>could not load {}</> ({})\n'.format(
                    debugger.name, debugger.loading_error)
                if options.verbose:
                    msg = '{}\n    {}'.format(
                        msg, '    '.join(debugger.loading_error_trace))
                raise Error(msg)

            with debugger:
                try:
                    debugger.start()
                except DebuggerException as e:
                    raise Error(e)

        with open(self.context.options.dextIR_path, 'wb') as fp:
            pickle.dump(self.dextIR, fp)
        return ReturnCode.OK
Example #2
0
def tool_main(context, tool, args):
    with Timer(tool.name):
        options, defaults = tool.parse_command_line(args)
        Timer.display = options.time_report
        Timer.indent = options.indent_timer_level
        Timer.fn = context.o.blue
        context.options = options
        context.version = version(tool.name)

        if options.version:
            context.o.green('{}\n'.format(context.version))
            return ReturnCode.OK

        if (options.unittest != 'off' and not unit_tests_ok(context)):
            raise Error('<d>unit test failures</>')

        if options.colortest:
            context.o.colortest()
            return ReturnCode.OK

        try:
            tool.handle_base_options(defaults)
        except ToolArgumentError as e:
            raise Error(e)

        dir_ = context.options.working_directory
        with WorkingDirectory(context, dir=dir_) as context.working_directory:
            return_code = tool.go()

        return return_code
Example #3
0
 def go(self) -> ReturnCode:
     with Timer('list debuggers'):
         try:
             Debuggers(self.context).list()
         except DebuggerException as e:
             raise Error(e)
     return ReturnCode.OK
Example #4
0
    def go(self) -> ReturnCode:
        with Timer('loading debugger'):
            debugger = Debuggers(self.context).load(self.options.debugger)

        with Timer('running debugger'):
            if not debugger.is_available:
                self.raise_debugger_error('load', debugger)

            self.debugger_controller.run_debugger(debugger)

            if debugger.loading_error:
                self.raise_debugger_error('run', debugger)

        with open(self.controller_path, 'wb') as fp:
            pickle.dump(self.debugger_controller, fp)
        return ReturnCode.OK
Example #5
0
def run_debugger_subprocess(debugger_controller, working_dir_path):
    with NamedTemporaryFile(
            dir=working_dir_path, delete=False, mode='wb') as fp:
        pickle.dump(debugger_controller, fp, protocol=pickle.HIGHEST_PROTOCOL)
        controller_path = fp.name

    dexter_py = os.path.basename(sys.argv[0])
    if not os.path.isfile(dexter_py):
        dexter_py = os.path.join(get_root_directory(), '..', dexter_py)
    assert os.path.isfile(dexter_py)

    with NamedTemporaryFile(dir=working_dir_path) as fp:
        args = [
            sys.executable,
            dexter_py,
            'run-debugger-internal-',
            controller_path,
            '--working-directory={}'.format(working_dir_path),
            '--unittest=off',
            '--indent-timer-level={}'.format(Timer.indent + 2)
        ]
        try:
            with Timer('running external debugger process'):
                subprocess.check_call(args)
        except subprocess.CalledProcessError as e:
            raise DebuggerException(e)

    with open(controller_path, 'rb') as fp:
        debugger_controller = pickle.load(fp)

    return debugger_controller
Example #6
0
    def go(self) -> ReturnCode:
        with Timer('loading debugger'):
            debugger = Debuggers(self.context).load(self.options.debugger)

        with Timer('running debugger'):
            if not debugger.is_available:
                msg = '<d>could not load {}</> ({})\n'.format(
                    debugger.name, debugger.loading_error)
                if self.options.verbose:
                    msg = '{}\n    {}'.format(
                        msg, '    '.join(debugger.loading_error_trace))
                raise Error(msg)

        self.debugger_controller.run_debugger(debugger)

        with open(self.controller_path, 'wb') as fp:
            pickle.dump(self.debugger_controller, fp)
        return ReturnCode.OK
Example #7
0
def get_debugger_steps(context):
    step_collection = empty_debugger_steps(context)

    with Timer('parsing commands'):
        try:
            step_collection.commands = _get_command_infos(context)
        except CommandParseError as e:
            msg = 'parser error: <d>{}({}):</> {}\n{}\n{}\n'.format(
                e.filename, e.lineno, e.info, e.src, e.caret)
            raise DebuggerException(msg)

    with NamedTemporaryFile(dir=context.working_directory.path,
                            delete=False) as fp:
        pickle.dump(step_collection, fp, protocol=pickle.HIGHEST_PROTOCOL)
        steps_path = fp.name

    with NamedTemporaryFile(dir=context.working_directory.path,
                            delete=False,
                            mode='wb') as fp:
        pickle.dump(context.options, fp, protocol=pickle.HIGHEST_PROTOCOL)
        options_path = fp.name

    dexter_py = sys.argv[0]
    if not os.path.isfile(dexter_py):
        dexter_py = os.path.join(get_root_directory(), '..', dexter_py)
    assert os.path.isfile(dexter_py)

    with NamedTemporaryFile(dir=context.working_directory.path) as fp:
        args = [
            sys.executable, dexter_py, 'run-debugger-internal-', steps_path,
            options_path, '--working-directory',
            context.working_directory.path, '--unittest=off',
            '--indent-timer-level={}'.format(Timer.indent + 2)
        ]
        try:
            with Timer('running external debugger process'):
                subprocess.check_call(args)
        except subprocess.CalledProcessError as e:
            raise DebuggerException(e)

    with open(steps_path, 'rb') as fp:
        step_collection = pickle.load(fp)

    return step_collection
Example #8
0
def get_command_infos(source_files):
    with Timer('parsing commands'):
        try:
            commands = _find_all_commands(source_files)
            command_infos = OrderedDict()
            for command_type in commands:
                for command in commands[command_type].values():
                    if command_type not in command_infos:
                        command_infos[command_type] = []
                    command_infos[command_type].append(command)
            return OrderedDict(command_infos)
        except CommandParseError as e:
            msg = 'parser error: <d>{}({}):</> {}\n{}\n{}\n'.format(
                e.filename, e.lineno, e.info, e.src, e.caret)
            raise DebuggerException(msg)
Example #9
0
def run_external_build_script(context, script_path, source_files,
                              compiler_options, linker_options,
                              executable_file):
    """Build an executable using a builder script.

    The executable is saved to `context.working_directory.path`.

    Returns:
        ( stdout (str), stderr (str), builder (BuilderIR) )
    """

    builderIR = BuilderIR(
        name=context.options.builder,
        cflags=compiler_options,
        ldflags=linker_options,
    )
    assert len(source_files) == len(compiler_options), (source_files,
                                                        compiler_options)

    script_environ = _get_script_environment(source_files, compiler_options,
                                             linker_options, executable_file)
    env = dict(os.environ)
    env.update(script_environ)
    try:
        with Timer('running build script'):
            process = subprocess.Popen([script_path],
                                       cwd=context.working_directory.path,
                                       env=env,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE)
            out, err = process.communicate()
            returncode = process.returncode
        out = out.decode('utf-8')
        err = err.decode('utf-8')
        if returncode != 0:
            raise BuildScriptException(
                '{}: failed with returncode {}.\nstdout:\n{}\n\nstderr:\n{}\n'.
                format(script_path, returncode, out, err),
                script_error=err)
        return out, err, builderIR
    except OSError as e:
        raise BuildScriptException('{}: {}'.format(e.strerror, script_path))
Example #10
0
def unit_tests_ok(context):
    unittest.TestCase.maxDiff = None  # remove size limit from diff output.

    with Timer('unit tests'):
        suite = DexTestLoader().discover(context.root_directory,
                                         pattern='*.py')
        stream = StringIO()
        result = unittest.TextTestRunner(verbosity=2, stream=stream).run(suite)

        ok = result.wasSuccessful()
        if not ok or context.options.unittest == 'show-all':
            with PreserveAutoColors(context.o):
                context.o.auto_reds.extend(
                    [r'FAIL(ED|\:)', r'\.\.\.\s(FAIL|ERROR)$'])
                context.o.auto_greens.extend([r'^OK$', r'\.\.\.\sok$'])
                context.o.auto_blues.extend([r'^Ran \d+ test'])
                context.o.default('\n')
                for line in stream.getvalue().splitlines(True):
                    context.o.auto(line, stream=PrettyOutput.stderr)

        return ok
Example #11
0
 def load(self, key, step_collection=None):
     with Timer('load {}'.format(key)):
         return Debuggers.potential_debuggers()[key](self.context,
                                                     step_collection)
Example #12
0
 def load(self, key):
     with Timer('load {}'.format(key)):
         return Debuggers.potential_debuggers()[key](self.context)