blob: 5a7edc1a863ea58dfdb5eccb0cd9e6413eae5189 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001import os
2import sys
3import errno
4import datetime
5import itertools
Patrick Williamsc0f7c042017-02-23 20:41:17 -06006from .commands import runCmd
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008class BaseDumper(object):
9 """ Base class to dump commands from host/target """
10
11 def __init__(self, cmds, parent_dir):
12 self.cmds = []
Patrick Williamsf1e5d692016-03-30 15:21:19 -050013 # 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"""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025 if not cmds:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050026 cmds = dft_cmds
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074class TargetDumper(BaseDumper):
75 """ Class to get dumps from target, it only works with QemuRunner """
76
Brad Bishop6e60e8b2018-02-01 10:27:11 -050077 def __init__(self, cmds, parent_dir, runner):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 super(TargetDumper, self).__init__(cmds, parent_dir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050079 self.runner = runner
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080
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")