The 'dump_state' callback assumes it will be outputting to a FILE object. This is fine for HMP, but not so useful for QMP. Introduce a new 'format_state' callback that returns a formatted GString instead.
Signed-off-by: Daniel P. Berrangé <berra...@redhat.com> --- hw/core/cpu-common.c | 15 +++++++++++++++ include/hw/core/cpu.h | 13 ++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/hw/core/cpu-common.c b/hw/core/cpu-common.c index e2f5a64604..c2cd33a817 100644 --- a/hw/core/cpu-common.c +++ b/hw/core/cpu-common.c @@ -106,6 +106,21 @@ void cpu_dump_state(CPUState *cpu, FILE *f, int flags) if (cc->dump_state) { cpu_synchronize_state(cpu); cc->dump_state(cpu, f, flags); + } else if (cc->format_state) { + g_autoptr(GString) buf = g_string_new(""); + cpu_synchronize_state(cpu); + cc->format_state(cpu, buf, flags); + qemu_fprintf(f, "%s", buf->str); + } +} + +void cpu_format_state(CPUState *cpu, GString *buf, int flags) +{ + CPUClass *cc = CPU_GET_CLASS(cpu); + + if (cc->format_state) { + cpu_synchronize_state(cpu); + cc->format_state(cpu, buf, flags); } } diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index bc864564ce..1599ef9df3 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -91,7 +91,8 @@ struct SysemuCPUOps; * @reset_dump_flags: #CPUDumpFlags to use for reset logging. * @has_work: Callback for checking if there is work to do. * @memory_rw_debug: Callback for GDB memory access. - * @dump_state: Callback for dumping state. + * @dump_state: Callback for dumping state. Deprecated, use @format_state. + * @format_state: Callback for formatting state. * @get_arch_id: Callback for getting architecture-dependent CPU ID. * @set_pc: Callback for setting the Program Counter register. This * should have the semantics used by the target architecture when @@ -136,6 +137,7 @@ struct CPUClass { int (*memory_rw_debug)(CPUState *cpu, vaddr addr, uint8_t *buf, int len, bool is_write); void (*dump_state)(CPUState *cpu, FILE *, int flags); + void (*format_state)(CPUState *cpu, GString *buf, int flags); int64_t (*get_arch_id)(CPUState *cpu); void (*set_pc)(CPUState *cpu, vaddr value); int (*gdb_read_register)(CPUState *cpu, GByteArray *buf, int reg); @@ -537,6 +539,15 @@ enum CPUDumpFlags { */ void cpu_dump_state(CPUState *cpu, FILE *f, int flags); +/** + * cpu_format_state: + * @cpu: The CPU whose state is to be formatted. + * @buf: buffer to format state into + * + * Formats the CPU state. + */ +void cpu_format_state(CPUState *cpu, GString *buf, int flags); + #ifndef CONFIG_USER_ONLY /** * cpu_get_phys_page_attrs_debug: -- 2.31.1