| 1 | /* |
| 2 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | * Copyright (C) 2025, Julian Ganz <neither@nut.email> |
| 4 | * |
| 5 | * Traps - count traps |
| 6 | * |
| 7 | * Count the number of interrupts (asynchronous events), exceptions (synchronous |
| 8 | * events) and host calls (e.g. semihosting) per cpu and report those counts on |
| 9 | * exit. |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | |
| 14 | #include <qemu-plugin.h> |
| 15 | |
| 16 | QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION; |
| 17 | |
| 18 | typedef struct { |
| 19 | uint64_t interrupts; |
| 20 | uint64_t exceptions; |
| 21 | uint64_t hostcalls; |
| 22 | } TrapCounters; |
| 23 | |
| 24 | static struct qemu_plugin_scoreboard *traps; |
| 25 | |
| 26 | static void vcpu_discon(unsigned int vcpu_index, |
| 27 | enum qemu_plugin_discon_type type, uint64_t from_pc, |
| 28 | uint64_t to_pc, void *userdata) |
| 29 | { |
| 30 | TrapCounters *rec = qemu_plugin_scoreboard_find(traps, vcpu_index); |
| 31 | switch (type) { |
| 32 | case QEMU_PLUGIN_DISCON_INTERRUPT: |
| 33 | rec->interrupts++; |
| 34 | break; |
| 35 | case QEMU_PLUGIN_DISCON_EXCEPTION: |
| 36 | rec->exceptions++; |
| 37 | break; |
| 38 | case QEMU_PLUGIN_DISCON_HOSTCALL: |
| 39 | rec->hostcalls++; |
| 40 | break; |
| 41 | default: |
| 42 | g_assert_not_reached(); |
| 43 | break; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | static void plugin_exit(void *p) |
| 48 | { |
| 49 | g_autoptr(GString) report; |
| 50 | report = g_string_new("VCPU, interrupts, exceptions, hostcalls\n"); |
| 51 | int max_vcpus = qemu_plugin_num_vcpus(); |
| 52 | int vcpu; |
| 53 | |
| 54 | for (vcpu = 0; vcpu < max_vcpus; vcpu++) { |
| 55 | TrapCounters *rec = qemu_plugin_scoreboard_find(traps, vcpu); |
| 56 | g_string_append_printf(report, |
| 57 | "% 4d, % 10"PRId64", % 10"PRId64", % 10"PRId64 |
| 58 | "\n", vcpu, rec->interrupts, rec->exceptions, |
| 59 | rec->hostcalls); |
| 60 | } |
| 61 | |
| 62 | qemu_plugin_outs(report->str); |
| 63 | qemu_plugin_scoreboard_free(traps); |
| 64 | } |
| 65 | |
| 66 | QEMU_PLUGIN_EXPORT |
| 67 | int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info, |
| 68 | int argc, char **argv) |
| 69 | { |
| 70 | if (!info->system_emulation) { |
| 71 | qemu_plugin_outs("Note: interrupts are only reported in system" |
| 72 | " emulation mode."); |
| 73 | } |
| 74 | |
| 75 | traps = qemu_plugin_scoreboard_new(sizeof(TrapCounters)); |
| 76 | |
| 77 | qemu_plugin_register_vcpu_discon_cb(id, QEMU_PLUGIN_DISCON_ALL, |
| 78 | vcpu_discon, |
| 79 | NULL); |
| 80 | |
| 81 | qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); |
| 82 | |
| 83 | return 0; |
| 84 | } |