blob: bcee03b5768bd7d18dea957f88f49505b7992571 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: MIT
5#
6
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007import os
8import sys
Andrew Geisslerc926e172021-05-07 16:11:35 -05009import json
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010import errno
11import datetime
12import itertools
Patrick Williamsc0f7c042017-02-23 20:41:17 -060013from .commands import runCmd
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015class BaseDumper(object):
16 """ Base class to dump commands from host/target """
17
18 def __init__(self, cmds, parent_dir):
19 self.cmds = []
Patrick Williamsf1e5d692016-03-30 15:21:19 -050020 # Some testing doesn't inherit testimage, so it is needed
21 # to set some defaults.
Brad Bishop977dc1a2019-02-06 16:01:43 -050022 self.parent_dir = parent_dir
Andrew Geissler5f350902021-07-23 13:09:54 -040023 self.dump_dir = parent_dir
Patrick Williamsf1e5d692016-03-30 15:21:19 -050024 dft_cmds = """ top -bn1
25 iostat -x -z -N -d -p ALL 20 2
26 ps -ef
27 free
28 df
29 memstat
30 dmesg
31 ip -s link
32 netstat -an"""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033 if not cmds:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050034 cmds = dft_cmds
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035 for cmd in cmds.split('\n'):
36 cmd = cmd.lstrip()
37 if not cmd or cmd[0] == '#':
38 continue
39 self.cmds.append(cmd)
40
41 def create_dir(self, dir_suffix):
42 dump_subdir = ("%s_%s" % (
43 datetime.datetime.now().strftime('%Y%m%d%H%M'),
44 dir_suffix))
45 dump_dir = os.path.join(self.parent_dir, dump_subdir)
46 try:
47 os.makedirs(dump_dir)
48 except OSError as err:
49 if err.errno != errno.EEXIST:
50 raise err
51 self.dump_dir = dump_dir
52
Andrew Geissler5f350902021-07-23 13:09:54 -040053 def _construct_filename(self, command):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054 if isinstance(self, HostDumper):
55 prefix = "host"
56 elif isinstance(self, TargetDumper):
57 prefix = "target"
Andrew Geisslerc926e172021-05-07 16:11:35 -050058 elif isinstance(self, MonitorDumper):
59 prefix = "qmp"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060 else:
61 prefix = "unknown"
62 for i in itertools.count():
63 filename = "%s_%02d_%s" % (prefix, i, command)
64 fullname = os.path.join(self.dump_dir, filename)
65 if not os.path.exists(fullname):
66 break
Andrew Geissler5f350902021-07-23 13:09:54 -040067 return fullname
68
69 def _write_dump(self, command, output):
70 fullname = self._construct_filename(command)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000071 os.makedirs(os.path.dirname(fullname), exist_ok=True)
Andrew Geisslerc926e172021-05-07 16:11:35 -050072 if isinstance(self, MonitorDumper):
73 with open(fullname, 'w') as json_file:
74 json.dump(output, json_file, indent=4)
75 else:
76 with open(fullname, 'w') as dump_file:
77 dump_file.write(output)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078
79class HostDumper(BaseDumper):
80 """ Class to get dumps from the host running the tests """
81
82 def __init__(self, cmds, parent_dir):
83 super(HostDumper, self).__init__(cmds, parent_dir)
84
85 def dump_host(self, dump_dir=""):
86 if dump_dir:
87 self.dump_dir = dump_dir
Andrew Geissler82c905d2020-04-13 13:39:40 -050088 env = os.environ.copy()
89 env['PATH'] = '/usr/sbin:/sbin:/usr/bin:/bin'
90 env['COLUMNS'] = '9999'
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091 for cmd in self.cmds:
Andrew Geissler82c905d2020-04-13 13:39:40 -050092 result = runCmd(cmd, ignore_status=True, env=env)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093 self._write_dump(cmd.split()[0], result.output)
94
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095class TargetDumper(BaseDumper):
96 """ Class to get dumps from target, it only works with QemuRunner """
97
Brad Bishop6e60e8b2018-02-01 10:27:11 -050098 def __init__(self, cmds, parent_dir, runner):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099 super(TargetDumper, self).__init__(cmds, parent_dir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500100 self.runner = runner
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101
102 def dump_target(self, dump_dir=""):
103 if dump_dir:
104 self.dump_dir = dump_dir
105 for cmd in self.cmds:
106 # We can continue with the testing if serial commands fail
107 try:
108 (status, output) = self.runner.run_serial(cmd)
109 self._write_dump(cmd.split()[0], output)
110 except:
111 print("Tried to dump info from target but "
112 "serial console failed")
Andrew Geisslerc926e172021-05-07 16:11:35 -0500113 print("Failed CMD: %s" % (cmd))
114
115class MonitorDumper(BaseDumper):
116 """ Class to get dumps via the Qemu Monitor, it only works with QemuRunner """
117
118 def __init__(self, cmds, parent_dir, runner):
119 super(MonitorDumper, self).__init__(cmds, parent_dir)
120 self.runner = runner
121
122 def dump_monitor(self, dump_dir=""):
123 if self.runner is None:
124 return
125 if dump_dir:
126 self.dump_dir = dump_dir
127 for cmd in self.cmds:
Andrew Geissler5f350902021-07-23 13:09:54 -0400128 cmd_name = cmd.split()[0]
Andrew Geisslerc926e172021-05-07 16:11:35 -0500129 try:
Andrew Geissler5f350902021-07-23 13:09:54 -0400130 if len(cmd.split()) > 1:
131 cmd_args = cmd.split()[1]
132 if "%s" in cmd_args:
133 filename = self._construct_filename(cmd_name)
134 cmd_data = json.loads(cmd_args % (filename))
135 output = self.runner.run_monitor(cmd_name, cmd_data)
136 else:
137 output = self.runner.run_monitor(cmd_name)
138 self._write_dump(cmd_name, output)
139 except Exception as e:
Andrew Geissler595f6302022-01-24 19:11:47 +0000140 print("Failed to dump QMP CMD: %s with\nException: %s" % (cmd_name, e))