def get_function_lineno(cls, fn, path, last=False): if last: out = run_cmd.Cmd().run_cmd("wc -l %s | awk '{print ($1);}'" % (path)) return long(out) out = run_cmd.Cmd().run_cmd("egrep -no ' > %s( |$)' %s" % (fn, path)) if len(out) == 0: return None else: return long(out.split(":")[0])
def go(): parser = process_args.FiddleArgParser("Fiddle test suite") args = parser.args shell = run_cmd.Cmd() task_mgr = parser.task_manager() if args.create: task_mgr.create_test_instance() elif args.import_policy: task_mgr.import_policy() elif args.run_new_trace: task_mgr.run_trace() elif args.postprocess_trace: task_mgr.postprocess_trace() # elif args.run_trace: # task_mgr.run_trace() if args.print_trace_commands: task_mgr.rt.do_print_cmds() #if args.buildcommands or if args.print_build_commands or args.build_software or args.print_build_commands: targets = args.build_software if args.build_software else args.print_build_commands ret = task_mgr.build(targets, True if args.build_software else False) if args.print_build_commands: for r in ret: for task in r.tasks: print "to %s %s:" % (task.name, task.basename) for action in task.list_tasks()['actions']: if isinstance(action, CmdAction): print "cd %s" % task.root_dir print action.expand_action() print "\n"
def __init__(self, kw, name=None, default=False): global registry global defaults self.default = default v = None if name is not None: self.name = name else: self.name = "" if "name" in kw.keys(): self.name = kw["name"] del kw["name"] if "name" in self.required_fields: self.required_fields.remove("name") self._update_raw("name", self.name) fields = kw.keys() for f in self.required_fields: if f not in fields: default_location = getattr(Main.default_raw, self.__class__.__name__) v = getattr(default_location, f, None) if v is None: raise ConfigException("Missing field '%s' in " "configuration %s" % (f, self.__class__.__name__)) kw[f] = v self._files = {} self._configs = [] self._GDB_configs = [] for (k, v) in kw.iteritems(): if k == "Files": for (name, info) in v.iteritems(): if name in self._files.keys(): raise ConfigException( "A there is already a file named '%s' in the configuration for software '%s'" % (name, self.name)) self._files[name] = ConfigFile(name, info, self) elif k == "ExecConfig": self._configs.append(ExecConfig(v, self, "exec")) elif k == "GDBConfig": self._GDB_configs.append(ExecConfig(v, self, "gdb")) else: setattr(self, k, v) self._update_raw(k, v) cls = self.__class__.__name__ if self.default: if cls not in defaults: defaults[cls] = [] defaults[cls].append(self) else: if cls not in registry.keys(): registry[cls] = [] registry[cls].append(self) self.shell = run_cmd.Cmd()
def __init__(self, kw): fields = kw.keys() for f in self.required_fields: if f not in fields: raise Exception("Missing field '%s' in " "configuration %s" % (f, str(self.__class__))) global registry for (k, v) in kw.iteritems(): setattr(self, k, v) cls = str(self.__class__.__name__) if cls not in registry.keys(): registry[cls] = [] registry[cls].append(self) self.shell = run_cmd.Cmd()
def go(): parser = argparse.ArgumentParser("Target test suite") cmds = parser.add_mutually_exclusive_group() class SubstageFileAction(argparse.Action): def __init__(self, option_strings, dest, **kwargs): self.stages = Main.stages self.stagenames = [s.stagename for s in self.stages] if len(self.stages) == 1: self.nargs = 2 else: self.nargs = 3 name = Main.get_hardwareclass_config().name path = Main.get_hardwareclass_config().hw_info_path defaultdir = os.path.join( Main.get_hardwareclass_config().hw_info_path, name) self.sdefaults = {} kwargs['default'] = self.sdefaults super(SubstageFileAction, self).__init__(option_strings, dest, **kwargs) def __call__(self, parser, namespace, values, option_string=None): stagename = values[0] f = os.path.abspath(values[1]) d = os.path.abspath(values[2]) if stagename not in self.stagenames: raise argparse.ArgumentError( self, "%s not a valid stage, must be one of %s" % (stagename, str(self.stagenames))) if not os.path.exists(f): raise argparse.ArgumentError( "Substage definition file I am trying to import from '%s' not found" % f) if not os.path.exists(d): raise argparse.ArgumentError( "Region definition file I am trying to import from '%s' not found" % d) getattr(namespace, self.dest)[stagename] = (f, d) class SubstageNameAction(argparse.Action): def __init__(self, option_strings, dest, **kwargs): stages = Main.stages self.stagenames = [s.stagename for s in stages] self.nargs = 2 defaults = {} kwargs['default'] = defaults super(SubstageNameAction, self).__init__(option_strings, dest, **kwargs) def __call__(self, parser, namespace, values, option_string=None): stagename = values[0] policy_name = values[1] if stagename not in self.stagenames: raise argparse.ArgumentError( self, "%s not a valid stage, must be one of %s" % (stagename, str(self.stagenames))) getattr(namespace, self.dest)[stagename] = policy_name cmds.add_argument('-c', '--create', help='Create new result directory for current instance of '\ 'software specified in configuration', action='store_true', default=False) cmds.add_argument( '-I', '--import_policy', help="Import policy (substage and region definitions) into instance. ' \ 'Requires stage name, path to file containing proposed substages, " "and path to file containing region info", nargs="*", default={}, action=SubstageFileAction) cmds.add_argument( '-L', '--list_policies', default=False, action="store_true", help="Lists instance's imported policies", ) cmds.add_argument('-B', '--build_software', help="Name of software to clean, update git tree, " "and build (openocd, u-boot, qemu))", action="append", default=[]) cmds.add_argument('-b', '--print_build_commands', help="Print build commands for software", default=[], action="append") cmds.add_argument('-S', '--setup_trace', action="store_true", default=False) cmds.add_argument('-R', '--run_new_trace', action="store_true", default=False) cmds.add_argument('-D', '--do_trace', action="store_true", help="Performs trace if not already performed", default=False) cmds.add_argument('--list_instances', action="store_true", default=False) cmds.add_argument('-l', '--list_test_runs', action="store_true", default=False) cmds.add_argument('-p', '--postprocess_trace', default=[], action="append", choices=instrumentation_results_manager.PostTraceLoader. supported_types, help="Run trace postprocessing command") cmds.add_argument('-P', '--print_trace_commands', action='store_true', help='Prints commands used to produce trace') parser.add_argument('-i', '--select_instance', help='Name test instance use, " \ "by default we use newest', action='store', default=None) c = Main.get_hardwareclass_config() if not isinstance(c.default_host, str): raise Exception( "check your configuration, default host should be a string") host = Main.object_config_lookup("HostConfig", c.default_host) parser.add_argument('-t', '--select_trace', action="store", help="Select existing instance's trace by name") parser.add_argument( "-T", "--trace_methods", default=host.default_tracing, action="append", choices=[m.name for m in Main.object_config_lookup("TraceMethod")]) parser.add_argument('-s', '--stages', action='append', default=[s.stagename for s in Main.default_stages]) parser.add_argument('-n', '--select_policy', action=SubstageNameAction, nargs=2) parser.add_argument( '-H', '--host', action="store", default=host.name, choices=[m.name for m in Main.object_config_lookup("HostConfig")]) parser.add_argument('-k', '--keep_temp_files', action="store_true") parser.add_argument('-q', '--quick', help='Try to skip some steps to be faster', action='store_true', default=False) args = parser.parse_args() args.hook = "" other = None shell = run_cmd.Cmd() cmd = None for c in list(doit_manager.cmds): n = c._name_ if getattr(args, n): cmd = c break if "all" in args.stages: args.stages = Main.stages if args.print_build_commands or args.build_software: other = args.print_build_commands + args.build_software if args.import_policy: policy = args.import_policy else: policy = args.import_policy task_mgr = doit_manager.TaskManager(cmd, args.select_instance, args.select_trace, args.host, args.trace_methods, args.stages, policy, args.postprocess_trace, not args.keep_temp_files, args.quick, other) # task_mgr = doit_manager.TaskManager(args.print_build_commands, # args.build_software, # args.create, # args.stages, # policies, # args.quick, # args.run_trace, # args.select_trace, # import_policies, # args.postprocess_trace, # args.testcfginstance, run, # args.print_cmds, # rm_dir=not args.keep_temp_files) # if args.create or import_policies or args.print_cmds: # if args.create: task_mgr.create_test_instance() elif args.import_policy: task_mgr.import_policy() elif args.run_new_trace: task_mgr.run_trace() elif args.postprocess_trace: task_mgr.postprocess_trace() # elif args.run_trace: # task_mgr.run_trace() if args.print_trace_commands: task_mgr.rt.do_print_cmds() #if args.buildcommands or if args.print_build_commands or args.build_software or args.print_build_commands: targets = args.build_software if args.build_software else args.print_build_commands ret = task_mgr.build(targets, True if args.build_software else False) if args.print_build_commands: for r in ret: for task in r.tasks: print "to %s %s:" % (task.name, task.basename) for action in task.list_tasks()['actions']: if isinstance(action, CmdAction): print "cd %s" % task.root_dir print action.expand_action() print "\n"
class Main(ConfigObject): required_fields = ["name"] shell = run_cmd.Cmd() test_suite_dir = os.path.realpath( os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) configs = {} supported_tracing_methods = set() cc = "" runtime = {} verbose = False def __init__(self, kw, name=None, default=False): self.default = default self._target = None self._target_checked = None self._target_software = None self._target_software_checked = None self._target = None self._target_checked = None self._stages = None self._stages_checked = None self._traces = None self.test_suite_path = Main.test_suite_dir self._update_raw("test_suite_path", self.test_suite_path) super(Main, self).__init__(kw, name, default) self.root = self.populate_path(self.root, isroot=True) self._check_path("root") self._update_raw("root", self.root) self.hw_info_path = self.populate_path_from_name("hw_info_path", True) self._check_path("hw_info_path") self._update_raw("hw_info_path", self.hw_info_path) self.cc = self.populate_path_from_name("cc", or_default=True) self.cc = os.path.expanduser(self.cc) if not os.path.exists(self.cc + "gcc"): raise ConfigException( "There does not appear to be a gcc compiler (Main.cc) at %sgcc" % Main.cc) self._update_raw("cc", self.cc) self.test_data_path = self.populate_path(self.test_data_path) self._update_raw("test_data_path", self.test_data_path) def setup(self): if self.attr_exists("setup_done"): return self.setup_done = True @classmethod def populate_from_config(cls, value, use_default=True, is_default=False): if (not isinstance(value, str)) or (not (("{" in value) and ("}" in value)) or (len(value) < 3)): return value if not cls.raw: is_default = True if is_default: return value else: r = cls._do_format(value, cls.raw) if isinstance(r, Exception): if use_default: r2 = cls._do_format(value, cls.default_raw) if isinstance(r2, Exception): raise ConfigException( "Unable to lookup template name '%s' in %s of config file (also: %s: %s)" % (r.args, value, r2.message, r2.args)) else: r = r2 return r @classmethod def set_config(cls, key, value): cls.configs[key] = value @classmethod def has_config(cls, key, *args): try: cls.get_config(cls, key, *args) return True except KeyError: return False @classmethod def get_config(cls, key, *args): if key in cls.configs.iterkeys(): v = cls.configs[key] if callable(v): return v(*args) else: return v else: raise KeyError("Config value %s not set" % key) def _get_generic_config(self, name, key, stage="", catch_except=False): if not stage: s = "" elif type(stage) == str: s = stage else: s = stage.stagename o = Main.raw if name: o = getattr(o, name) attrs = key.split(".") for a in attrs: o = getattr(o, a) if s: o = getattr(o, s) return o def _set_generic_config(self, name, key, value, stage=""): if not stage: s = "" elif type(stage) == str: s = stage else: s = stage.stagename key = "%s.%s" % (name, key) if stage: key += ".%s" % s self._update_raw(key, value) def set_runtime_config(self, key, value, stage=None, catch_except=False): self._set_generic_config("runtime", key, value, stage) def get_runtime_config(self, key, stage=None, catch_except=False): return self._get_generic_config("runtime", key, stage) def get_policy_config(self, key, stage=None, catch_except=False): return self._get_generic_config("policies", key, stage, catch_except) def get_target_config(self, key="", stage=None, catch_except=False): return self._get_generic_config("runtime", "target." + key, stage, catch_except) def get_static_analysis_config(self, key="", stage=None, catch_except=False): return self._get_generic_config("static_analysis", key, stage, catch_except) def stage_from_name(self, stagename): s = None for stage in self.object_config_lookup("TargetStage"): if stage.stagename == stagename: s = stage break return s @property def traces(self): if not self._traces: self._traces = self.object_config_lookup("TraceMethod") return self._traces @property def postprocesses(self): if not self._pps: self._pps = self.object_config_lookup("PostProcess") return self._pps @property def stages(self): if not self._stages: self._stages = self.object_config_lookup("TargetStage") if 0 == len(self._stages): self._stages = defaults["TargetStage"] return self._stages @property def target_software(self): if not self._target_software: target_software_name = self.target.software if isinstance(target_software_name, str): self._target_software = self.object_config_lookup( "Software", target_software_name) else: self._target_software = target_software_name return self._target_software @property def target(self): if not self._target: for s in self.object_config_lookup("Target"): self._target = s break return self._target def get_hardwareclass_config(self): return self.object_config_lookup("HardwareClass", Main.hardwareclass)
# The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import hashlib import run_cmd import re import r2_keeper as r2 shell = run_cmd.Cmd() def file_md5(filename): m = hashlib.md5() with open(filename, "rb") as f: for block in iter(lambda: f.read(65536), b""): m.update(block) return m.hexdigest() def get_entrypoint(elf): return r2.entrypoint(elf) # cmd = "%sreadelf -W -h %s" % (cc, elf) # output = shell.run_multiline_cmd(cmd) # ere = re.compile("Entry point address:[\s]+(0x[a-fA-F0-9]{1,16})")
class Main(ConfigObject): required_fields = [ "root", "cc", "test_data_path", "python", "bootloader", "test_suite_path", "hw_info_path", "task_handlers" ] shell = run_cmd.Cmd() hardware_instances = {} test_suite_dir = os.path.dirname(os.path.realpath(__file__)) configs = {} supported_tracing_methods = set() @classmethod def set_config(cls, key, value): cls.configs[key] = value @classmethod def get_config(cls, key, *args): if key in cls.configs.iterkeys(): v = cls.configs[key] if callable(v): return v(*args) else: return v else: return None def stage_from_name(self, stagename): s = None for stage in self.get_bootloader_cfg().supported_stages.itervalues(): if stage.stagename == stagename: s = stage break return s def hw_from_name(self, tracename): hw = None for h in self.hardware_instances.itervalues(): if h.tracename == tracename: hw = h break return hw def get_bootloader_cfg(self): return self.object_config_lookup("Bootloader", self.bootloader) def get_bootloader_root(self): return self.get_bootloader_cfg().software_cfg.root def get_hardwareclass_config(self): return self.object_config_lookup("HardwareClass", self.hardwareclass) @property def test_suite_path(self): return os.path.join(self.root, self.__ts_path) @test_suite_path.setter def test_suite_path(self, ts_path): self.__ts_path = ts_path @property def test_data_path(self): return os.path.join(self.root, self.__td_path) @test_data_path.setter def test_data_path(self, td_path): self.__td_path = td_path @property def hw_info_path(self): return os.path.join(self.root, self.__hw_path) @hw_info_path.setter def hw_info_path(self, hw_path): self.__hw_path = hw_path