Re: [PATCH 3/5] target/i386: convert to use format_state instead of dump_state
On Wed, Sep 08, 2021 at 11:06:33PM +0100, Daniel P. Berrangé wrote: > On Wed, Sep 08, 2021 at 01:05:13PM -0500, Eric Blake wrote: > > On Wed, Sep 08, 2021 at 11:37:09AM +0100, Daniel P. Berrangé wrote: > > > Signed-off-by: Daniel P. Berrangé > > > --- > > > target/i386/cpu-dump.c | 325 ++--- > > > target/i386/cpu.c | 2 +- > > > target/i386/cpu.h | 2 +- > > > 3 files changed, 174 insertions(+), 155 deletions(-) > > > > > > diff --git a/target/i386/cpu-dump.c b/target/i386/cpu-dump.c > > > index 02b635a52c..8e19485a20 100644 > > > --- a/target/i386/cpu-dump.c > > > +++ b/target/i386/cpu-dump.c > > > @@ -94,41 +94,45 @@ static const char *cc_op_str[CC_OP_NB] = { > > > }; > > > > > > static void > > > -cpu_x86_dump_seg_cache(CPUX86State *env, FILE *f, > > > +cpu_x86_dump_seg_cache(CPUX86State *env, GString *buf, > > > const char *name, struct SegmentCache *sc) > > > { > > > #ifdef TARGET_X86_64 > > > if (env->hflags & HF_CS64_MASK) { > > > -qemu_fprintf(f, "%-3s=%04x %016" PRIx64 " %08x %08x", name, > > > - sc->selector, sc->base, sc->limit, > > > - sc->flags & 0x0000); > > > +g_string_append_printf(buf, "%-3s=%04x %016" PRIx64 " %08x > > > %08x", name, > > > + sc->selector, sc->base, sc->limit, > > > + sc->flags & 0x0000); > > > > Did you consider using open_memstream() to get a FILE* that can then > > be passed into these callbacks unchanged, rather than rewriting all > > the callbacks to a new signature? > > That is certainly an option, but it wouldn't eliminate the need to do > a rewrite. I would still want to replace qemu_fprintf with fprintf in > that scenario. It is desirable to be able to eliminate the QEMU > specific printf wrappers which only exist because they need to treat > a NULL FILE* object as an indication to output to the HMP chardev. > Admittedly that would result in shorter lines than today. > > > Then again, I like the GString signature better than FILE*, even if it > > makes for longer lines. > > Yes, the verbosity is not ideal. I like the GString API as a general > purpose API for formatting text output to a buffer overall. > > I don't feel too strongly either way though, as long as we get to a place > where we eliminate the custom QEMU printf wrappers that integrate with > the monitor APIs. I forgot that open_memstream is not portable. The portable alternative is fmemopen but that needs to know the buffer size upfront which is too unpleasant to use. So GString is the better portable option. Regards, Daniel -- |: https://berrange.com -o-https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o-https://fstop138.berrange.com :| |: https://entangle-photo.org-o-https://www.instagram.com/dberrange :|
Re: [PATCH 3/5] target/i386: convert to use format_state instead of dump_state
On Wed, Sep 08, 2021 at 01:05:13PM -0500, Eric Blake wrote: > On Wed, Sep 08, 2021 at 11:37:09AM +0100, Daniel P. Berrangé wrote: > > Signed-off-by: Daniel P. Berrangé > > --- > > target/i386/cpu-dump.c | 325 ++--- > > target/i386/cpu.c | 2 +- > > target/i386/cpu.h | 2 +- > > 3 files changed, 174 insertions(+), 155 deletions(-) > > > > diff --git a/target/i386/cpu-dump.c b/target/i386/cpu-dump.c > > index 02b635a52c..8e19485a20 100644 > > --- a/target/i386/cpu-dump.c > > +++ b/target/i386/cpu-dump.c > > @@ -94,41 +94,45 @@ static const char *cc_op_str[CC_OP_NB] = { > > }; > > > > static void > > -cpu_x86_dump_seg_cache(CPUX86State *env, FILE *f, > > +cpu_x86_dump_seg_cache(CPUX86State *env, GString *buf, > > const char *name, struct SegmentCache *sc) > > { > > #ifdef TARGET_X86_64 > > if (env->hflags & HF_CS64_MASK) { > > -qemu_fprintf(f, "%-3s=%04x %016" PRIx64 " %08x %08x", name, > > - sc->selector, sc->base, sc->limit, > > - sc->flags & 0x0000); > > +g_string_append_printf(buf, "%-3s=%04x %016" PRIx64 " %08x %08x", > > name, > > + sc->selector, sc->base, sc->limit, > > + sc->flags & 0x0000); > > Did you consider using open_memstream() to get a FILE* that can then > be passed into these callbacks unchanged, rather than rewriting all > the callbacks to a new signature? That is certainly an option, but it wouldn't eliminate the need to do a rewrite. I would still want to replace qemu_fprintf with fprintf in that scenario. It is desirable to be able to eliminate the QEMU specific printf wrappers which only exist because they need to treat a NULL FILE* object as an indication to output to the HMP chardev. Admittedly that would result in shorter lines than today. > Then again, I like the GString signature better than FILE*, even if it > makes for longer lines. Yes, the verbosity is not ideal. I like the GString API as a general purpose API for formatting text output to a buffer overall. I don't feel too strongly either way though, as long as we get to a place where we eliminate the custom QEMU printf wrappers that integrate with the monitor APIs. Regards, Daniel -- |: https://berrange.com -o-https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o-https://fstop138.berrange.com :| |: https://entangle-photo.org-o-https://www.instagram.com/dberrange :|
Re: [PATCH 3/5] target/i386: convert to use format_state instead of dump_state
On Wed, Sep 08, 2021 at 11:37:09AM +0100, Daniel P. Berrangé wrote: > Signed-off-by: Daniel P. Berrangé > --- > target/i386/cpu-dump.c | 325 ++--- > target/i386/cpu.c | 2 +- > target/i386/cpu.h | 2 +- > 3 files changed, 174 insertions(+), 155 deletions(-) > > diff --git a/target/i386/cpu-dump.c b/target/i386/cpu-dump.c > index 02b635a52c..8e19485a20 100644 > --- a/target/i386/cpu-dump.c > +++ b/target/i386/cpu-dump.c > @@ -94,41 +94,45 @@ static const char *cc_op_str[CC_OP_NB] = { > }; > > static void > -cpu_x86_dump_seg_cache(CPUX86State *env, FILE *f, > +cpu_x86_dump_seg_cache(CPUX86State *env, GString *buf, > const char *name, struct SegmentCache *sc) > { > #ifdef TARGET_X86_64 > if (env->hflags & HF_CS64_MASK) { > -qemu_fprintf(f, "%-3s=%04x %016" PRIx64 " %08x %08x", name, > - sc->selector, sc->base, sc->limit, > - sc->flags & 0x0000); > +g_string_append_printf(buf, "%-3s=%04x %016" PRIx64 " %08x %08x", > name, > + sc->selector, sc->base, sc->limit, > + sc->flags & 0x0000); Did you consider using open_memstream() to get a FILE* that can then be passed into these callbacks unchanged, rather than rewriting all the callbacks to a new signature? Then again, I like the GString signature better than FILE*, even if it makes for longer lines. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH 3/5] target/i386: convert to use format_state instead of dump_state
On a Wednesday in 2021, Daniel P. Berrangé wrote: Signed-off-by: Daniel P. Berrangé --- target/i386/cpu-dump.c | 325 ++--- target/i386/cpu.c | 2 +- target/i386/cpu.h | 2 +- 3 files changed, 174 insertions(+), 155 deletions(-) diff --git a/target/i386/cpu-dump.c b/target/i386/cpu-dump.c index 02b635a52c..8e19485a20 100644 --- a/target/i386/cpu-dump.c +++ b/target/i386/cpu-dump.c [...] @@ -355,107 +359,116 @@ void x86_cpu_dump_state(CPUState *cs, FILE *f, int flags) [...] { -qemu_fprintf(f, "GDT= %08x %08x\n", - (uint32_t)env->gdt.base, env->gdt.limit); -qemu_fprintf(f, "IDT= %08x %08x\n", - (uint32_t)env->idt.base, env->idt.limit); -qemu_fprintf(f, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n", - (uint32_t)env->cr[0], - (uint32_t)env->cr[2], - (uint32_t)env->cr[3], - (uint32_t)env->cr[4]); +g_string_append_printf(buf, "GDT= %08x %08x\n", + (uint32_t)env->gdt.base, env->gdt.limit); +g_string_append_printf(buf, "IDT= %08x %08x\n", + (uint32_t)env->idt.base, env->idt.limit); +g_string_append_printf(buf, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n", + (uint32_t)env->cr[0], + (uint32_t)env->cr[2], + (uint32_t)env->cr[3], + (uint32_t)env->cr[4]); for(i = 0; i < 4; i++) { -qemu_fprintf(f, "DR%d=" TARGET_FMT_lx " ", i, env->dr[i]); +g_string_append_printf(buf, "DR%d=" TARGET_FMT_lx + " ", i, env->dr[i]); The formatting string can comfortably fit on the first line here. Jano } -qemu_fprintf(f, "\nDR6=" TARGET_FMT_lx " DR7=" TARGET_FMT_lx "\n", - env->dr[6], env->dr[7]); +g_string_append_printf(buf, "\nDR6=" TARGET_FMT_lx + " DR7=" TARGET_FMT_lx "\n", + env->dr[6], env->dr[7]); signature.asc Description: PGP signature
[PATCH 3/5] target/i386: convert to use format_state instead of dump_state
Signed-off-by: Daniel P. Berrangé --- target/i386/cpu-dump.c | 325 ++--- target/i386/cpu.c | 2 +- target/i386/cpu.h | 2 +- 3 files changed, 174 insertions(+), 155 deletions(-) diff --git a/target/i386/cpu-dump.c b/target/i386/cpu-dump.c index 02b635a52c..8e19485a20 100644 --- a/target/i386/cpu-dump.c +++ b/target/i386/cpu-dump.c @@ -94,41 +94,45 @@ static const char *cc_op_str[CC_OP_NB] = { }; static void -cpu_x86_dump_seg_cache(CPUX86State *env, FILE *f, +cpu_x86_dump_seg_cache(CPUX86State *env, GString *buf, const char *name, struct SegmentCache *sc) { #ifdef TARGET_X86_64 if (env->hflags & HF_CS64_MASK) { -qemu_fprintf(f, "%-3s=%04x %016" PRIx64 " %08x %08x", name, - sc->selector, sc->base, sc->limit, - sc->flags & 0x0000); +g_string_append_printf(buf, "%-3s=%04x %016" PRIx64 " %08x %08x", name, + sc->selector, sc->base, sc->limit, + sc->flags & 0x0000); } else #endif { -qemu_fprintf(f, "%-3s=%04x %08x %08x %08x", name, sc->selector, - (uint32_t)sc->base, sc->limit, - sc->flags & 0x0000); +g_string_append_printf(buf, "%-3s=%04x %08x %08x %08x", + name, sc->selector, + (uint32_t)sc->base, sc->limit, + sc->flags & 0x0000); } if (!(env->hflags & HF_PE_MASK) || !(sc->flags & DESC_P_MASK)) goto done; -qemu_fprintf(f, " DPL=%d ", +g_string_append_printf(buf, " DPL=%d ", (sc->flags & DESC_DPL_MASK) >> DESC_DPL_SHIFT); if (sc->flags & DESC_S_MASK) { if (sc->flags & DESC_CS_MASK) { -qemu_fprintf(f, (sc->flags & DESC_L_MASK) ? "CS64" : - ((sc->flags & DESC_B_MASK) ? "CS32" : "CS16")); -qemu_fprintf(f, " [%c%c", (sc->flags & DESC_C_MASK) ? 'C' : '-', - (sc->flags & DESC_R_MASK) ? 'R' : '-'); +g_string_append_printf(buf, (sc->flags & DESC_L_MASK) ? "CS64" : + ((sc->flags & DESC_B_MASK) ? "CS32" : "CS16")); +g_string_append_printf(buf, " [%c%c", + (sc->flags & DESC_C_MASK) ? 'C' : '-', + (sc->flags & DESC_R_MASK) ? 'R' : '-'); } else { -qemu_fprintf(f, (sc->flags & DESC_B_MASK +g_string_append_printf(buf, (sc->flags & DESC_B_MASK || env->hflags & HF_LMA_MASK) ? "DS " : "DS16"); -qemu_fprintf(f, " [%c%c", (sc->flags & DESC_E_MASK) ? 'E' : '-', - (sc->flags & DESC_W_MASK) ? 'W' : '-'); +g_string_append_printf(buf, " [%c%c", + (sc->flags & DESC_E_MASK) ? 'E' : '-', + (sc->flags & DESC_W_MASK) ? 'W' : '-'); } -qemu_fprintf(f, "%c]", (sc->flags & DESC_A_MASK) ? 'A' : '-'); +g_string_append_printf(buf, "%c]", + (sc->flags & DESC_A_MASK) ? 'A' : '-'); } else { static const char *sys_type_name[2][16] = { { /* 32 bit mode */ @@ -144,12 +148,12 @@ cpu_x86_dump_seg_cache(CPUX86State *env, FILE *f, "Reserved", "IntGate64", "TrapGate64" } }; -qemu_fprintf(f, "%s", - sys_type_name[(env->hflags & HF_LMA_MASK) ? 1 : 0] - [(sc->flags & DESC_TYPE_MASK) >> DESC_TYPE_SHIFT]); +g_string_append_printf(buf, "%s", + sys_type_name[(env->hflags & HF_LMA_MASK) ? 1 : 0] + [(sc->flags & DESC_TYPE_MASK) >> DESC_TYPE_SHIFT]); } done: -qemu_fprintf(f, "\n"); +g_string_append_printf(buf, "\n"); } #ifndef CONFIG_USER_ONLY @@ -344,7 +348,7 @@ void x86_cpu_dump_local_apic_state(CPUState *cs, int flags) #define DUMP_CODE_BYTES_TOTAL50 #define DUMP_CODE_BYTES_BACKWARD 20 -void x86_cpu_dump_state(CPUState *cs, FILE *f, int flags) +void x86_cpu_format_state(CPUState *cs, GString *buf, int flags) { X86CPU *cpu = X86_CPU(cs); CPUX86State *env = >env; @@ -355,107 +359,116 @@ void x86_cpu_dump_state(CPUState *cs, FILE *f, int flags) eflags = cpu_compute_eflags(env); #ifdef TARGET_X86_64 if (env->hflags & HF_CS64_MASK) { -qemu_fprintf(f, "RAX=%016" PRIx64 " RBX=%016" PRIx64 " RCX=%016" PRIx64 " RDX=%016" PRIx64 "\n" - "RSI=%016" PRIx64 " RDI=%016" PRIx64 " RBP=%016" PRIx64 " RSP=%016" PRIx64 "\n" - "R8 =%016" PRIx64 " R9 =%016" PRIx64 " R10=%016" PRIx64 " R11=%016" PRIx64 "\n" - "R12=%016" PRIx64 " R13=%016" PRIx64 " R14=%016" PRIx64 " R15=%016" PRIx64 "\n" -