| 1 | # GDB debugging support |
| 2 | # |
| 3 | # Copyright 2017 Linaro Ltd |
| 4 | # |
| 5 | # Author: Alex Bennée <alex.bennee@linaro.org> |
| 6 | # |
| 7 | # This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 8 | # See the COPYING file in the top-level directory. |
| 9 | # |
| 10 | # SPDX-License-Identifier: GPL-2.0-or-later |
| 11 | |
| 12 | # 'qemu timers' -- display the current timerlists |
| 13 | |
| 14 | import gdb |
| 15 | |
| 16 | class TimersCommand(gdb.Command): |
| 17 | '''Display the current QEMU timers''' |
| 18 | |
| 19 | def __init__(self): |
| 20 | 'Register the class as a gdb command' |
| 21 | gdb.Command.__init__(self, 'qemu timers', gdb.COMMAND_DATA, |
| 22 | gdb.COMPLETE_NONE) |
| 23 | |
| 24 | def _format_expire_time(self, expire_time, scale): |
| 25 | "Return human-readable expiry time (ns) with scale info." |
| 26 | secs = expire_time / 1e9 |
| 27 | |
| 28 | # Select unit and compute value |
| 29 | if secs < 1: |
| 30 | val, unit = secs * 1000, "ms" |
| 31 | elif secs < 60: |
| 32 | val, unit = secs, "s" |
| 33 | elif secs < 3600: |
| 34 | val, unit = secs / 60, "min" |
| 35 | elif secs < 86400: |
| 36 | val, unit = secs / 3600, "hrs" |
| 37 | else: |
| 38 | val, unit = secs / 86400, "days" |
| 39 | |
| 40 | scale_map = {1: "ns", 1000: "us", 1000000: "ms", |
| 41 | 1000000000: "s"} |
| 42 | scale_str = scale_map.get(scale, f"scale={scale}") |
| 43 | return f"{val:.2f} {unit} [{scale_str}]" |
| 44 | |
| 45 | def _format_attribute(self, attr): |
| 46 | "Given QEMUTimer attributes value, return a human-readable string" |
| 47 | |
| 48 | # From include/qemu/timer.h |
| 49 | if attr == 0: |
| 50 | value = 'NONE' |
| 51 | elif attr == 1 << 0: |
| 52 | value = 'ATTR_EXTERNAL' |
| 53 | elif attr == int(0xffffffff): |
| 54 | value = 'ATTR_ALL' |
| 55 | else: |
| 56 | value = 'UNKNOWN' |
| 57 | return f'{attr} <{value}>' |
| 58 | |
| 59 | def dump_timers(self, timer): |
| 60 | "Follow a timer and recursively dump each one in the list." |
| 61 | # timer should be of type QemuTimer |
| 62 | scale = int(timer['scale']) |
| 63 | expire_time = int(timer['expire_time']) |
| 64 | attributes = int(timer['attributes']) |
| 65 | |
| 66 | time_str = self._format_expire_time(expire_time, scale) |
| 67 | attr_str = self._format_attribute(attributes) |
| 68 | |
| 69 | gdb.write(f" timer at {time_str} (attr:{attr_str}, " |
| 70 | f"cb:{timer['cb']}, opq:{timer['opaque']})\n") |
| 71 | |
| 72 | if int(timer['next']) > 0: |
| 73 | self.dump_timers(timer['next']) |
| 74 | |
| 75 | |
| 76 | def process_timerlist(self, tlist, ttype): |
| 77 | gdb.write("Processing %s timers\n" % (ttype)) |
| 78 | gdb.write(" clock %s is enabled:%s\n" % ( |
| 79 | tlist['clock']['type'], |
| 80 | tlist['clock']['enabled'])) |
| 81 | if int(tlist['active_timers']) > 0: |
| 82 | self.dump_timers(tlist['active_timers']) |
| 83 | |
| 84 | |
| 85 | def invoke(self, arg, from_tty): |
| 86 | 'Run the command' |
| 87 | main_timers = gdb.parse_and_eval("main_loop_tlg") |
| 88 | |
| 89 | # This will break if QEMUClockType in timer.h is redfined |
| 90 | self.process_timerlist(main_timers['tl'][0], "Realtime") |
| 91 | self.process_timerlist(main_timers['tl'][1], "Virtual") |
| 92 | self.process_timerlist(main_timers['tl'][2], "Host") |
| 93 | self.process_timerlist(main_timers['tl'][3], "Virtual RT") |