simpletrace: define exception and add handling

Define `SimpleException` to differentiate our exceptions from generic
exceptions (IOError, etc.). Adapted simpletrace to support this and
output to stderr.

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Mads Ynddal <m.ynddal@samsung.com>
Message-id: 20230926103436.25700-8-mads@ynddal.dk
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
Mads Ynddal 2023-09-26 12:34:29 +02:00 committed by Stefan Hajnoczi
parent d1f9259014
commit 1990fb9893

View file

@ -32,12 +32,15 @@
log_header_fmt = '=QQQ' log_header_fmt = '=QQQ'
rec_header_fmt = '=QQII' rec_header_fmt = '=QQII'
class SimpleException(Exception):
pass
def read_header(fobj, hfmt): def read_header(fobj, hfmt):
'''Read a trace record header''' '''Read a trace record header'''
hlen = struct.calcsize(hfmt) hlen = struct.calcsize(hfmt)
hdr = fobj.read(hlen) hdr = fobj.read(hlen)
if len(hdr) != hlen: if len(hdr) != hlen:
raise ValueError('Error reading header. Wrong filetype provided?') raise SimpleException('Error reading header. Wrong filetype provided?')
return struct.unpack(hfmt, hdr) return struct.unpack(hfmt, hdr)
def get_record(event_mapping, event_id_to_name, rechdr, fobj): def get_record(event_mapping, event_id_to_name, rechdr, fobj):
@ -49,10 +52,10 @@ def get_record(event_mapping, event_id_to_name, rechdr, fobj):
try: try:
event = event_mapping[name] event = event_mapping[name]
except KeyError as e: except KeyError as e:
sys.stderr.write(f'{e} event is logged but is not declared ' \ raise SimpleException(
'in the trace events file, try using ' \ f'{e} event is logged but is not declared in the trace events'
'trace-events-all instead.\n') 'file, try using trace-events-all instead.'
sys.exit(1) )
rec = (name, timestamp_ns, pid) rec = (name, timestamp_ns, pid)
for type, name in event.args: for type, name in event.args:
@ -247,8 +250,7 @@ def run(analyzer):
*no_header, trace_event_path, trace_file_path = sys.argv[1:] *no_header, trace_event_path, trace_file_path = sys.argv[1:]
assert no_header == [] or no_header == ['--no-header'], 'Invalid no-header argument' assert no_header == [] or no_header == ['--no-header'], 'Invalid no-header argument'
except (AssertionError, ValueError): except (AssertionError, ValueError):
sys.stderr.write(f'usage: {sys.argv[0]} [--no-header] <trace-events> <trace-file>\n') raise SimpleException(f'usage: {sys.argv[0]} [--no-header] <trace-events> <trace-file>\n')
sys.exit(1)
with open(trace_event_path, 'r') as events_fobj, open(trace_file_path, 'rb') as log_fobj: with open(trace_event_path, 'r') as events_fobj, open(trace_file_path, 'rb') as log_fobj:
process(events_fobj, log_fobj, analyzer, read_header=not no_header) process(events_fobj, log_fobj, analyzer, read_header=not no_header)
@ -276,4 +278,8 @@ def catchall(self, event, rec):
i += 1 i += 1
print(' '.join(fields)) print(' '.join(fields))
run(Formatter()) try:
run(Formatter())
except SimpleException as e:
sys.stderr.write(str(e) + "\n")
sys.exit(1)