monitor: tighten monitor_set_cpu()/get_cpu()
These functions access mon_cpu_path, which is a MonitorHMP-specific field. Narrowing their signatures from Monitor* to MonitorHMP* makes the type system enforce what was already true at runtime: every caller is in an HMP context. The expression parser's MONITOR_HMP() casts are safe because it is only reachable from handle_hmp_command(), they will be dropped with later patches. Reviewed-by: Dr. David Alan Gilbert <dave@treblig.org> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-ID: <20260828-qemu-no-hmp-v5-36-9227de146347@redhat.com>
Marc-André Lureau committed
Aug 28, 2026 at 16:04 UTC
bcf97f76064885730f491b63d4543b60c87c09fd
17 files changed
+47
-50
hw/core/machine-hmp-cmds.c
+2
-2
@@ -36,7 +36,7 @@ void hmp_info_cpus(MonitorHMP *hmp, const QDict *qdict)
36
g_autofree char *cpu_model = cpu_model_from_type(cpu->value->qom_type);
37
int active = ' ';
38
39
- if (cpu->value->cpu_index == monitor_get_cpu_index(mon)) {
39
+ if (cpu->value->cpu_index == monitor_hmp_get_cpu_index(hmp)) {
40
active = '*';
41
}
42
@@ -228,7 +228,7 @@ void hmp_memsave(MonitorHMP *hmp, const QDict *qdict)
228
const char *filename = qdict_get_str(qdict, "filename");
229
uint64_t addr = qdict_get_int(qdict, "val");
230
Error *err = NULL;
231
- int cpu_index = monitor_get_cpu_index(mon);
231
+ int cpu_index = monitor_hmp_get_cpu_index(hmp);
232
233
if (cpu_index < 0) {
234
monitor_printf(mon, "No CPU available\n");
include/monitor/hmp.h
+4
-3
@@ -30,7 +30,7 @@ OBJECT_DECLARE_TYPE(MonitorHMP, MonitorHMPClass, MONITOR_HMP);
30
struct MonitorDef {
31
const char *name;
32
int offset;
33
- int64_t (*get_value)(Monitor *mon, const MonitorDef *md, int offset);
33
+ int64_t (*get_value)(MonitorHMP *hmp, const MonitorDef *md, int offset);
34
};
35
36
void monitor_new_hmp(const char *id, const char *chardev_id,
@@ -53,8 +53,9 @@ void monitor_register_hmp_info_hrt(const char *name,
53
HumanReadableText *(*handler)(Error **errp));
54
55
56
-CPUArchState *mon_get_cpu_env(Monitor *mon);
57
-CPUState *mon_get_cpu(Monitor *mon);
56
+CPUArchState *monitor_hmp_get_cpu_env(MonitorHMP *hmp);
57
+CPUState *monitor_hmp_get_cpu(MonitorHMP *hmp);
58
+int monitor_hmp_get_cpu_index(MonitorHMP *hmp);
59
60
bool hmp_handle_error(MonitorHMP *hmp, Error *err);
61
void hmp_help_cmd(Monitor *mon, const char *name);
include/monitor/monitor.h
-1
@@ -39,7 +39,6 @@ int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp);
39
40
int monitor_puts(Monitor *mon, const char *str);
41
void monitor_flush(Monitor *mon);
42
-int monitor_get_cpu_index(Monitor *mon);
42
43
int monitor_puts_locked(Monitor *mon, const char *str);
44
void monitor_flush_locked(Monitor *mon);
monitor/hmp-cmds.c
+18
-21
@@ -182,10 +182,10 @@ void hmp_cpu(MonitorHMP *hmp, const QDict *qdict)
182
Monitor *mon = MONITOR(hmp);
183
int64_t cpu_index;
184
185
- /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
185
+ /* XXX: drop the monitor_hmp_set_cpu() usage when all HMP commands that
186
use it are converted to the QAPI */
187
cpu_index = qdict_get_int(qdict, "index");
188
- if (monitor_set_cpu(mon, cpu_index) < 0) {
188
+ if (monitor_hmp_set_cpu(hmp, cpu_index) < 0) {
189
monitor_printf(mon, "invalid CPU index\n");
190
}
191
}
@@ -512,9 +512,8 @@ void hmp_dumpdtb(MonitorHMP *hmp, const QDict *qdict)
512
#endif
513
514
/* Set the current CPU defined by the user. Callers must hold BQL. */
515
-int monitor_set_cpu(Monitor *mon, int cpu_index)
515
+int monitor_hmp_set_cpu(MonitorHMP *hmp, int cpu_index)
516
{
517
- MonitorHMP *hmp = MONITOR_HMP(mon);
517
CPUState *cpu;
518
519
cpu = qemu_get_cpu(cpu_index);
@@ -527,9 +526,8 @@ int monitor_set_cpu(Monitor *mon, int cpu_index)
526
}
527
528
/* Callers must hold BQL. */
530
-static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
529
+static CPUState *monitor_hmp_get_cpu_sync(MonitorHMP *hmp, bool synchronize)
530
{
532
- MonitorHMP *hmp = MONITOR_HMP(mon);
531
CPUState *cpu = NULL;
532
533
if (hmp->mon_cpu_path) {
@@ -544,7 +542,7 @@ static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
542
if (!first_cpu) {
543
return NULL;
544
}
547
- monitor_set_cpu(mon, first_cpu->cpu_index);
545
+ monitor_hmp_set_cpu(hmp, first_cpu->cpu_index);
546
cpu = first_cpu;
547
}
548
assert(cpu != NULL);
@@ -554,21 +552,21 @@ static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
552
return cpu;
553
}
554
557
-CPUState *mon_get_cpu(Monitor *mon)
555
+CPUState *monitor_hmp_get_cpu(MonitorHMP *hmp)
556
{
559
- return mon_get_cpu_sync(mon, true);
557
+ return monitor_hmp_get_cpu_sync(hmp, true);
558
}
559
562
-CPUArchState *mon_get_cpu_env(Monitor *mon)
560
+CPUArchState *monitor_hmp_get_cpu_env(MonitorHMP *hmp)
561
{
564
- CPUState *cs = mon_get_cpu(mon);
562
+ CPUState *cs = monitor_hmp_get_cpu(hmp);
563
564
return cs ? cpu_env(cs) : NULL;
565
}
566
569
-int monitor_get_cpu_index(Monitor *mon)
567
+int monitor_hmp_get_cpu_index(MonitorHMP *hmp)
568
{
571
- CPUState *cs = mon_get_cpu_sync(mon, false);
569
+ CPUState *cs = monitor_hmp_get_cpu_sync(hmp, false);
570
571
return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
572
}
@@ -586,7 +584,7 @@ void hmp_info_registers(MonitorHMP *hmp, const QDict *qdict)
584
cpu_dump_state(cs, NULL, CPU_DUMP_FPU | CPU_DUMP_VPU);
585
}
586
} else {
589
- cs = vcpu >= 0 ? qemu_get_cpu(vcpu) : mon_get_cpu(mon);
587
+ cs = vcpu >= 0 ? qemu_get_cpu(vcpu) : monitor_hmp_get_cpu(hmp);
588
589
if (!cs) {
590
if (vcpu >= 0) {
@@ -602,13 +600,14 @@ void hmp_info_registers(MonitorHMP *hmp, const QDict *qdict)
600
}
601
}
602
605
-static void memory_dump(Monitor *mon, int count, int format, int wsize,
603
+static void memory_dump(MonitorHMP *hmp, int count, int format, int wsize,
604
uint64_t addr, bool is_physical)
605
{
606
+ Monitor *mon = MONITOR(hmp);
607
int l, line_size, i, max_digits, len;
608
uint8_t buf[16];
609
uint64_t v;
611
- CPUState *cs = mon_get_cpu(mon);
610
+ CPUState *cs = monitor_hmp_get_cpu(hmp);
611
const unsigned int addr_width = is_physical ? 8 : (target_long_bits() / 4);
612
const bool big_endian = target_big_endian();
613
@@ -712,24 +711,22 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
711
712
void hmp_memory_dump(MonitorHMP *hmp, const QDict *qdict)
713
{
715
- Monitor *mon = MONITOR(hmp);
714
int count = qdict_get_int(qdict, "count");
715
int format = qdict_get_int(qdict, "format");
716
int size = qdict_get_int(qdict, "size");
717
vaddr addr = qdict_get_int(qdict, "addr");
718
721
- memory_dump(mon, count, format, size, addr, false);
719
+ memory_dump(hmp, count, format, size, addr, false);
720
}
721
722
void hmp_physical_memory_dump(MonitorHMP *hmp, const QDict *qdict)
723
{
726
- Monitor *mon = MONITOR(hmp);
724
int count = qdict_get_int(qdict, "count");
725
int format = qdict_get_int(qdict, "format");
726
int size = qdict_get_int(qdict, "size");
727
hwaddr addr = qdict_get_int(qdict, "addr");
728
732
- memory_dump(mon, count, format, size, addr, true);
729
+ memory_dump(hmp, count, format, size, addr, true);
730
}
731
732
void hmp_gpa2hva(MonitorHMP *hmp, const QDict *qdict)
@@ -757,7 +754,7 @@ void hmp_gva2gpa(MonitorHMP *hmp, const QDict *qdict)
754
{
755
Monitor *mon = MONITOR(hmp);
756
vaddr addr = qdict_get_int(qdict, "addr");
760
- CPUState *cs = mon_get_cpu(mon);
757
+ CPUState *cs = monitor_hmp_get_cpu(hmp);
758
TranslateForDebugResult tres;
759
760
if (!cs) {
monitor/hmp.c
+9
-9
@@ -411,10 +411,10 @@ void hmp_help_cmd(Monitor *mon, const char *name)
411
* Set @pval to the value in the register identified by @name.
412
* return %true if the register is found, %false otherwise.
413
*/
414
-static bool gdb_get_register(Monitor *mon, int64_t *pval, const char *name)
414
+static bool gdb_get_register(MonitorHMP *hmp, int64_t *pval, const char *name)
415
{
416
g_autoptr(GArray) regs = NULL;
417
- CPUState *cs = mon_get_cpu(mon);
417
+ CPUState *cs = monitor_hmp_get_cpu(hmp);
418
419
if (cs == NULL) {
420
return false;
@@ -452,7 +452,7 @@ static bool gdb_get_register(Monitor *mon, int64_t *pval, const char *name)
452
static const char *pch;
453
static sigjmp_buf expr_env;
454
455
-static int get_monitor_def(Monitor *mon, int64_t *pval, const char *name);
455
+static int get_monitor_def(MonitorHMP *mon, int64_t *pval, const char *name);
456
457
static G_NORETURN G_GNUC_PRINTF(2, 3)
458
void expr_error(Monitor *mon, const char *fmt, ...)
@@ -535,8 +535,8 @@ static int64_t expr_unary(Monitor *mon)
535
pch++;
536
}
537
*q = 0;
538
- if (!gdb_get_register(mon, ®, buf)
539
- && get_monitor_def(mon, ®, buf) < 0) {
538
+ if (!gdb_get_register(MONITOR_HMP(mon), ®, buf)
539
+ && get_monitor_def(MONITOR_HMP(mon), ®, buf) < 0) {
540
expr_error(mon, "unknown register");
541
}
542
n = reg;
@@ -1733,9 +1733,9 @@ void monitor_register_hmp_info_hrt(const char *name,
1733
* Set @pval to the value in the register identified by @name.
1734
* return 0 if OK, -1 if not found
1735
*/
1736
-static int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
1736
+static int get_monitor_def(MonitorHMP *hmp, int64_t *pval, const char *name)
1737
{
1738
- CPUState *cs = mon_get_cpu(mon);
1738
+ CPUState *cs = monitor_hmp_get_cpu(hmp);
1739
const MonitorDef *md;
1740
void *ptr;
1741
@@ -1750,9 +1750,9 @@ static int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
1750
for (; md->name != NULL; md++) {
1751
if (hmp_compare_cmd(name, md->name)) {
1752
if (md->get_value) {
1753
- *pval = md->get_value(mon, md, md->offset);
1753
+ *pval = md->get_value(hmp, md, md->offset);
1754
} else {
1755
- CPUArchState *env = mon_get_cpu_env(mon);
1755
+ CPUArchState *env = monitor_hmp_get_cpu_env(hmp);
1756
ptr = (uint8_t *)env + md->offset;
1757
*pval = *(int32_t *)ptr;
1758
}
monitor/monitor-internal.h
+1
-1
@@ -215,7 +215,7 @@ int monitor_can_read(void *opaque);
215
void monitor_cancel_out_watch(Monitor *mon);
216
void monitor_list_append(Monitor *mon);
217
void monitor_fdsets_cleanup(void);
218
-int monitor_set_cpu(Monitor *mon, int cpu_index);
218
+int monitor_hmp_set_cpu(MonitorHMP *mon, int cpu_index);
219
220
void qmp_send_response(MonitorQMP *mon, const QDict *rsp);
221
void monitor_data_destroy_qmp(MonitorQMP *mon);
monitor/qmp-cmds.c
+1
-1
@@ -169,7 +169,7 @@ char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
169
MonitorHMP *hmp = MONITOR_HMP(object_new(TYPE_MONITOR_HMP));
170
171
if (has_cpu_index) {
172
- int ret = monitor_set_cpu(&hmp->parent_obj, cpu_index);
172
+ int ret = monitor_hmp_set_cpu(hmp, cpu_index);
173
if (ret < 0) {
174
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
175
"a CPU number");
stats/stats-hmp-cmds.c
+1
-1
@@ -226,7 +226,7 @@ void hmp_info_stats(MonitorHMP *hmp, const QDict *qdict)
226
filter = stats_filter(target, names, -1, provider);
227
break;
228
case STATS_TARGET_VCPU: {}
229
- int cpu_index = monitor_get_cpu_index(mon);
229
+ int cpu_index = monitor_hmp_get_cpu_index(hmp);
230
filter = stats_filter(target, names, cpu_index, provider);
231
break;
232
case STATS_TARGET_CRYPTODEV:
target/i386/cpu-apic.c
+1
-1
@@ -96,7 +96,7 @@ void hmp_info_local_apic(MonitorHMP *hmp, const QDict *qdict)
96
cpu_synchronize_state(cs);
97
}
98
} else {
99
- cs = mon_get_cpu(mon);
99
+ cs = monitor_hmp_get_cpu(hmp);
100
}
101
102
target/i386/cpu.c
+2
-2
@@ -10853,10 +10853,10 @@ static const Property x86_cpu_properties[] = {
10853
10854
#ifndef CONFIG_USER_ONLY
10855
10856
-static int64_t monitor_get_pc(Monitor *mon, const struct MonitorDef *md,
10856
+static int64_t monitor_get_pc(MonitorHMP *hmp, const struct MonitorDef *md,
10857
int offset)
10858
{
10859
- CPUArchState *env = mon_get_cpu_env(mon);
10859
+ CPUArchState *env = monitor_hmp_get_cpu_env(hmp);
10860
int64_t ret = env->eip + env->segs[R_CS].base;
10861
10862
if (!(env->hflags & HF_CS64_MASK)) {
target/i386/monitor.c
+2
-2
@@ -215,7 +215,7 @@ void hmp_info_tlb(MonitorHMP *hmp, const QDict *qdict)
215
CPUArchState *env;
216
AddressSpace *as;
217
218
- env = mon_get_cpu_env(mon);
218
+ env = monitor_hmp_get_cpu_env(hmp);
219
if (!env) {
220
monitor_printf(mon, "No CPU available\n");
221
return;
@@ -542,7 +542,7 @@ void hmp_info_mem(MonitorHMP *hmp, const QDict *qdict)
542
CPUArchState *env;
543
AddressSpace *as;
544
545
- env = mon_get_cpu_env(mon);
545
+ env = monitor_hmp_get_cpu_env(hmp);
546
if (!env) {
547
monitor_printf(mon, "No CPU available\n");
548
return;
target/m68k/monitor.c
+1
-1
@@ -13,7 +13,7 @@
13
void hmp_info_tlb(MonitorHMP *hmp, const QDict *qdict)
14
{
15
Monitor *mon = MONITOR(hmp);
16
- CPUArchState *env1 = mon_get_cpu_env(mon);
16
+ CPUArchState *env1 = monitor_hmp_get_cpu_env(hmp);
17
18
if (!env1) {
19
monitor_printf(mon, "No CPU available\n");
target/ppc/monitor.c
+1
-1
@@ -14,7 +14,7 @@
14
void hmp_info_tlb(MonitorHMP *hmp, const QDict *qdict)
15
{
16
Monitor *mon = MONITOR(hmp);
17
- CPUArchState *env1 = mon_get_cpu_env(mon);
17
+ CPUArchState *env1 = monitor_hmp_get_cpu_env(hmp);
18
19
if (!env1) {
20
monitor_printf(mon, "No CPU available\n");
target/riscv/monitor.c
+1
-1
@@ -220,7 +220,7 @@ void hmp_info_mem(MonitorHMP *hmp, const QDict *qdict)
220
Monitor *mon = MONITOR(hmp);
221
CPUArchState *env;
222
223
- env = mon_get_cpu_env(mon);
223
+ env = monitor_hmp_get_cpu_env(hmp);
224
if (!env) {
225
monitor_printf(mon, "No CPU available\n");
226
return;
target/sh4/monitor.c
+1
-1
@@ -41,7 +41,7 @@ static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
41
void hmp_info_tlb(MonitorHMP *hmp, const QDict *qdict)
42
{
43
Monitor *mon = MONITOR(hmp);
44
- CPUArchState *env = mon_get_cpu_env(mon);
44
+ CPUArchState *env = monitor_hmp_get_cpu_env(hmp);
45
int i;
46
47
if (!env) {
target/sparc/monitor.c
+1
-1
@@ -30,7 +30,7 @@
30
void hmp_info_tlb(MonitorHMP *hmp, const QDict *qdict)
31
{
32
Monitor *mon = MONITOR(hmp);
33
- CPUArchState *env1 = mon_get_cpu_env(mon);
33
+ CPUArchState *env1 = monitor_hmp_get_cpu_env(hmp);
34
35
if (!env1) {
36
monitor_printf(mon, "No CPU available\n");
target/xtensa/monitor.c
+1
-1
@@ -29,7 +29,7 @@
29
void hmp_info_tlb(MonitorHMP *hmp, const QDict *qdict)
30
{
31
Monitor *mon = MONITOR(hmp);
32
- CPUArchState *env1 = mon_get_cpu_env(mon);
32
+ CPUArchState *env1 = monitor_hmp_get_cpu_env(hmp);
33
34
if (!env1) {
35
monitor_printf(mon, "No CPU available\n");