def _start_process(self, args, kwargs): code, data_type, data, regs = self._resolve_args(args, kwargs) if code is None: code = self.code if data_type is None: data_type = self.data_type data = self.data if regs is None: regs = self.regs if code is None: raise ValueError("No code to run") if regs is None: regs = ptrace_registers_t() prog = ReAsmProgram(code, data_type) return ReAsmProcess(prog, data, regs)
def _resolve_args(self, args, kwargs): code = None data_type = None data = None regs = None for arg in args: if isinstance(arg, str): if code is not None: raise ValueError("Multiple code arguments found") code = arg elif isinstance(arg, Structure): if data_type is not None: raise ValueError("Multiple data type definitions found") data_type = type(arg) data = arg elif isinstance(arg, TypeType) and issubclass(arg, Structure): if data_type is not None: raise ValueError("Multiple data type definitions found") data_type = arg data = arg() elif isinstance(arg, ptrace_registers_t): if regs is not None: raise ValueError("Registers can only be supplied once") regs = arg else: raise ValueError("Unknown argument %r" % arg) for kw, val in kwargs.iteritems(): if hasattr(ptrace_registers_t, kw): if regs is None: regs = ptrace_registers_t() setattr(regs, kw, val) elif data is not None and hasattr(data, kw): setattr(data, kw, val) else: raise ValueError("Unknown keyword %r" % kw) return code, data_type, data, regs