1import os 2import sys 3import errno 4import datetime 5import itertools 6from .commands import runCmd 7 8class BaseDumper(object): 9 """ Base class to dump commands from host/target """ 10 11 def __init__(self, cmds, parent_dir): 12 self.cmds = [] 13 # Some testing doesn't inherit testimage, so it is needed 14 # to set some defaults. 15 self.parent_dir = parent_dir or "/tmp/oe-saved-tests" 16 dft_cmds = """ top -bn1 17 iostat -x -z -N -d -p ALL 20 2 18 ps -ef 19 free 20 df 21 memstat 22 dmesg 23 ip -s link 24 netstat -an""" 25 if not cmds: 26 cmds = dft_cmds 27 for cmd in cmds.split('\n'): 28 cmd = cmd.lstrip() 29 if not cmd or cmd[0] == '#': 30 continue 31 self.cmds.append(cmd) 32 33 def create_dir(self, dir_suffix): 34 dump_subdir = ("%s_%s" % ( 35 datetime.datetime.now().strftime('%Y%m%d%H%M'), 36 dir_suffix)) 37 dump_dir = os.path.join(self.parent_dir, dump_subdir) 38 try: 39 os.makedirs(dump_dir) 40 except OSError as err: 41 if err.errno != errno.EEXIST: 42 raise err 43 self.dump_dir = dump_dir 44 45 def _write_dump(self, command, output): 46 if isinstance(self, HostDumper): 47 prefix = "host" 48 elif isinstance(self, TargetDumper): 49 prefix = "target" 50 else: 51 prefix = "unknown" 52 for i in itertools.count(): 53 filename = "%s_%02d_%s" % (prefix, i, command) 54 fullname = os.path.join(self.dump_dir, filename) 55 if not os.path.exists(fullname): 56 break 57 with open(fullname, 'w') as dump_file: 58 dump_file.write(output) 59 60 61class HostDumper(BaseDumper): 62 """ Class to get dumps from the host running the tests """ 63 64 def __init__(self, cmds, parent_dir): 65 super(HostDumper, self).__init__(cmds, parent_dir) 66 67 def dump_host(self, dump_dir=""): 68 if dump_dir: 69 self.dump_dir = dump_dir 70 for cmd in self.cmds: 71 result = runCmd(cmd, ignore_status=True) 72 self._write_dump(cmd.split()[0], result.output) 73 74class TargetDumper(BaseDumper): 75 """ Class to get dumps from target, it only works with QemuRunner """ 76 77 def __init__(self, cmds, parent_dir, runner): 78 super(TargetDumper, self).__init__(cmds, parent_dir) 79 self.runner = runner 80 81 def dump_target(self, dump_dir=""): 82 if dump_dir: 83 self.dump_dir = dump_dir 84 for cmd in self.cmds: 85 # We can continue with the testing if serial commands fail 86 try: 87 (status, output) = self.runner.run_serial(cmd) 88 self._write_dump(cmd.split()[0], output) 89 except: 90 print("Tried to dump info from target but " 91 "serial console failed") 92