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