block/qapi: Clean up how we print to monitor or stdout

bdrv_snapshot_dump(), bdrv_image_info_specific_dump(),
bdrv_image_info_dump() and their helpers take an fprintf()-like
callback and a FILE * to pass to it.

hmp.c passes monitor_printf() cast to fprintf_function and the current
monitor cast to FILE *.

qemu-img.c and qemu-io-cmds.c pass fprintf and stdout.

The type-punning is technically undefined behaviour, but works in
practice.  Clean up: drop the callback, and call qemu_printf()
instead.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Message-Id: <20190417191805.28198-8-armbru@redhat.com>
This commit is contained in:
Markus Armbruster 2019-04-17 21:17:55 +02:00
parent ac7ff4cf5f
commit e1ce7d747b
5 changed files with 67 additions and 83 deletions

View File

@ -36,6 +36,7 @@
#include "qapi/qmp/qlist.h" #include "qapi/qmp/qlist.h"
#include "qapi/qmp/qnum.h" #include "qapi/qmp/qnum.h"
#include "qapi/qmp/qstring.h" #include "qapi/qmp/qstring.h"
#include "qemu/qemu-print.h"
#include "sysemu/block-backend.h" #include "sysemu/block-backend.h"
#include "qemu/cutils.h" #include "qemu/cutils.h"
@ -660,8 +661,7 @@ static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
return buf; return buf;
} }
void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f, void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
QEMUSnapshotInfo *sn)
{ {
char buf1[128], date_buf[128], clock_buf[128]; char buf1[128], date_buf[128], clock_buf[128];
struct tm tm; struct tm tm;
@ -669,9 +669,8 @@ void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
int64_t secs; int64_t secs;
if (!sn) { if (!sn) {
func_fprintf(f, qemu_printf("%-10s%-20s%7s%20s%15s",
"%-10s%-20s%7s%20s%15s", "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
"ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
} else { } else {
ti = sn->date_sec; ti = sn->date_sec;
localtime_r(&ti, &tm); localtime_r(&ti, &tm);
@ -684,50 +683,46 @@ void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
(int)((secs / 60) % 60), (int)((secs / 60) % 60),
(int)(secs % 60), (int)(secs % 60),
(int)((sn->vm_clock_nsec / 1000000) % 1000)); (int)((sn->vm_clock_nsec / 1000000) % 1000));
func_fprintf(f, qemu_printf("%-10s%-20s%7s%20s%15s",
"%-10s%-20s%7s%20s%15s", sn->id_str, sn->name,
sn->id_str, sn->name, get_human_readable_size(buf1, sizeof(buf1),
get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
sn->vm_state_size), date_buf,
date_buf, clock_buf);
clock_buf);
} }
} }
static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation, static void dump_qdict(int indentation, QDict *dict);
QDict *dict); static void dump_qlist(int indentation, QList *list);
static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
QList *list);
static void dump_qobject(fprintf_function func_fprintf, void *f, static void dump_qobject(int comp_indent, QObject *obj)
int comp_indent, QObject *obj)
{ {
switch (qobject_type(obj)) { switch (qobject_type(obj)) {
case QTYPE_QNUM: { case QTYPE_QNUM: {
QNum *value = qobject_to(QNum, obj); QNum *value = qobject_to(QNum, obj);
char *tmp = qnum_to_string(value); char *tmp = qnum_to_string(value);
func_fprintf(f, "%s", tmp); qemu_printf("%s", tmp);
g_free(tmp); g_free(tmp);
break; break;
} }
case QTYPE_QSTRING: { case QTYPE_QSTRING: {
QString *value = qobject_to(QString, obj); QString *value = qobject_to(QString, obj);
func_fprintf(f, "%s", qstring_get_str(value)); qemu_printf("%s", qstring_get_str(value));
break; break;
} }
case QTYPE_QDICT: { case QTYPE_QDICT: {
QDict *value = qobject_to(QDict, obj); QDict *value = qobject_to(QDict, obj);
dump_qdict(func_fprintf, f, comp_indent, value); dump_qdict(comp_indent, value);
break; break;
} }
case QTYPE_QLIST: { case QTYPE_QLIST: {
QList *value = qobject_to(QList, obj); QList *value = qobject_to(QList, obj);
dump_qlist(func_fprintf, f, comp_indent, value); dump_qlist(comp_indent, value);
break; break;
} }
case QTYPE_QBOOL: { case QTYPE_QBOOL: {
QBool *value = qobject_to(QBool, obj); QBool *value = qobject_to(QBool, obj);
func_fprintf(f, "%s", qbool_get_bool(value) ? "true" : "false"); qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
break; break;
} }
default: default:
@ -735,8 +730,7 @@ static void dump_qobject(fprintf_function func_fprintf, void *f,
} }
} }
static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation, static void dump_qlist(int indentation, QList *list)
QList *list)
{ {
const QListEntry *entry; const QListEntry *entry;
int i = 0; int i = 0;
@ -744,17 +738,16 @@ static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) { for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
QType type = qobject_type(entry->value); QType type = qobject_type(entry->value);
bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i, qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
composite ? '\n' : ' '); composite ? '\n' : ' ');
dump_qobject(func_fprintf, f, indentation + 1, entry->value); dump_qobject(indentation + 1, entry->value);
if (!composite) { if (!composite) {
func_fprintf(f, "\n"); qemu_printf("\n");
} }
} }
} }
static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation, static void dump_qdict(int indentation, QDict *dict)
QDict *dict)
{ {
const QDictEntry *entry; const QDictEntry *entry;
@ -769,18 +762,17 @@ static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
key[i] = entry->key[i] == '-' ? ' ' : entry->key[i]; key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
} }
key[i] = 0; key[i] = 0;
func_fprintf(f, "%*s%s:%c", indentation * 4, "", key, qemu_printf("%*s%s:%c", indentation * 4, "", key,
composite ? '\n' : ' '); composite ? '\n' : ' ');
dump_qobject(func_fprintf, f, indentation + 1, entry->value); dump_qobject(indentation + 1, entry->value);
if (!composite) { if (!composite) {
func_fprintf(f, "\n"); qemu_printf("\n");
} }
g_free(key); g_free(key);
} }
} }
void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f, void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec)
ImageInfoSpecific *info_spec)
{ {
QObject *obj, *data; QObject *obj, *data;
Visitor *v = qobject_output_visitor_new(&obj); Visitor *v = qobject_output_visitor_new(&obj);
@ -788,13 +780,12 @@ void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort); visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
visit_complete(v, &obj); visit_complete(v, &obj);
data = qdict_get(qobject_to(QDict, obj), "data"); data = qdict_get(qobject_to(QDict, obj), "data");
dump_qobject(func_fprintf, f, 1, data); dump_qobject(1, data);
qobject_unref(obj); qobject_unref(obj);
visit_free(v); visit_free(v);
} }
void bdrv_image_info_dump(fprintf_function func_fprintf, void *f, void bdrv_image_info_dump(ImageInfo *info)
ImageInfo *info)
{ {
char size_buf[128], dsize_buf[128]; char size_buf[128], dsize_buf[128];
if (!info->has_actual_size) { if (!info->has_actual_size) {
@ -804,49 +795,48 @@ void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
info->actual_size); info->actual_size);
} }
get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size); get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
func_fprintf(f, qemu_printf("image: %s\n"
"image: %s\n" "file format: %s\n"
"file format: %s\n" "virtual size: %s (%" PRId64 " bytes)\n"
"virtual size: %s (%" PRId64 " bytes)\n" "disk size: %s\n",
"disk size: %s\n", info->filename, info->format, size_buf,
info->filename, info->format, size_buf, info->virtual_size,
info->virtual_size, dsize_buf);
dsize_buf);
if (info->has_encrypted && info->encrypted) { if (info->has_encrypted && info->encrypted) {
func_fprintf(f, "encrypted: yes\n"); qemu_printf("encrypted: yes\n");
} }
if (info->has_cluster_size) { if (info->has_cluster_size) {
func_fprintf(f, "cluster_size: %" PRId64 "\n", qemu_printf("cluster_size: %" PRId64 "\n",
info->cluster_size); info->cluster_size);
} }
if (info->has_dirty_flag && info->dirty_flag) { if (info->has_dirty_flag && info->dirty_flag) {
func_fprintf(f, "cleanly shut down: no\n"); qemu_printf("cleanly shut down: no\n");
} }
if (info->has_backing_filename) { if (info->has_backing_filename) {
func_fprintf(f, "backing file: %s", info->backing_filename); qemu_printf("backing file: %s", info->backing_filename);
if (!info->has_full_backing_filename) { if (!info->has_full_backing_filename) {
func_fprintf(f, " (cannot determine actual path)"); qemu_printf(" (cannot determine actual path)");
} else if (strcmp(info->backing_filename, } else if (strcmp(info->backing_filename,
info->full_backing_filename) != 0) { info->full_backing_filename) != 0) {
func_fprintf(f, " (actual path: %s)", info->full_backing_filename); qemu_printf(" (actual path: %s)", info->full_backing_filename);
} }
func_fprintf(f, "\n"); qemu_printf("\n");
if (info->has_backing_filename_format) { if (info->has_backing_filename_format) {
func_fprintf(f, "backing file format: %s\n", qemu_printf("backing file format: %s\n",
info->backing_filename_format); info->backing_filename_format);
} }
} }
if (info->has_snapshots) { if (info->has_snapshots) {
SnapshotInfoList *elem; SnapshotInfoList *elem;
func_fprintf(f, "Snapshot list:\n"); qemu_printf("Snapshot list:\n");
bdrv_snapshot_dump(func_fprintf, f, NULL); bdrv_snapshot_dump(NULL);
func_fprintf(f, "\n"); qemu_printf("\n");
/* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
* we convert to the block layer's native QEMUSnapshotInfo for now. * we convert to the block layer's native QEMUSnapshotInfo for now.
@ -862,13 +852,13 @@ void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id); pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
pstrcpy(sn.name, sizeof(sn.name), elem->value->name); pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
bdrv_snapshot_dump(func_fprintf, f, &sn); bdrv_snapshot_dump(&sn);
func_fprintf(f, "\n"); qemu_printf("\n");
} }
} }
if (info->has_format_specific) { if (info->has_format_specific) {
func_fprintf(f, "Format specific information:\n"); qemu_printf("Format specific information:\n");
bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific); bdrv_image_info_specific_dump(info->format_specific);
} }
} }

12
hmp.c
View File

@ -580,8 +580,7 @@ static void print_block_info(Monitor *mon, BlockInfo *info,
monitor_printf(mon, "\nImages:\n"); monitor_printf(mon, "\nImages:\n");
image_info = inserted->image; image_info = inserted->image;
while (1) { while (1) {
bdrv_image_info_dump((fprintf_function)monitor_printf, bdrv_image_info_dump(image_info);
mon, image_info);
if (image_info->has_backing_image) { if (image_info->has_backing_image) {
image_info = image_info->backing_image; image_info = image_info->backing_image;
} else { } else {
@ -1586,7 +1585,7 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
monitor_printf(mon, "List of snapshots present on all disks:\n"); monitor_printf(mon, "List of snapshots present on all disks:\n");
if (total > 0) { if (total > 0) {
bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL); bdrv_snapshot_dump(NULL);
monitor_printf(mon, "\n"); monitor_printf(mon, "\n");
for (i = 0; i < total; i++) { for (i = 0; i < total; i++) {
sn = &sn_tab[global_snapshots[i]]; sn = &sn_tab[global_snapshots[i]];
@ -1594,7 +1593,7 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
* overwrite it. * overwrite it.
*/ */
pstrcpy(sn->id_str, sizeof(sn->id_str), "--"); pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn); bdrv_snapshot_dump(sn);
monitor_printf(mon, "\n"); monitor_printf(mon, "\n");
} }
} else { } else {
@ -1608,11 +1607,10 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
monitor_printf(mon, monitor_printf(mon,
"\nList of partial (non-loadable) snapshots on '%s':\n", "\nList of partial (non-loadable) snapshots on '%s':\n",
image_entry->imagename); image_entry->imagename);
bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL); bdrv_snapshot_dump(NULL);
monitor_printf(mon, "\n"); monitor_printf(mon, "\n");
QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) { QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, bdrv_snapshot_dump(&snapshot_entry->sn);
&snapshot_entry->sn);
monitor_printf(mon, "\n"); monitor_printf(mon, "\n");
} }
} }

View File

@ -27,7 +27,6 @@
#include "block/block.h" #include "block/block.h"
#include "block/snapshot.h" #include "block/snapshot.h"
#include "qemu/fprintf-fn.h"
BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk, BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
BlockDriverState *bs, Error **errp); BlockDriverState *bs, Error **errp);
@ -38,10 +37,7 @@ void bdrv_query_image_info(BlockDriverState *bs,
ImageInfo **p_info, ImageInfo **p_info,
Error **errp); Error **errp);
void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f, void bdrv_snapshot_dump(QEMUSnapshotInfo *sn);
QEMUSnapshotInfo *sn); void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec);
void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f, void bdrv_image_info_dump(ImageInfo *info);
ImageInfoSpecific *info_spec);
void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
ImageInfo *info);
#endif #endif

View File

@ -2483,11 +2483,11 @@ static void dump_snapshots(BlockDriverState *bs)
if (nb_sns <= 0) if (nb_sns <= 0)
return; return;
printf("Snapshot list:\n"); printf("Snapshot list:\n");
bdrv_snapshot_dump(fprintf, stdout, NULL); bdrv_snapshot_dump(NULL);
printf("\n"); printf("\n");
for(i = 0; i < nb_sns; i++) { for(i = 0; i < nb_sns; i++) {
sn = &sn_tab[i]; sn = &sn_tab[i];
bdrv_snapshot_dump(fprintf, stdout, sn); bdrv_snapshot_dump(sn);
printf("\n"); printf("\n");
} }
g_free(sn_tab); g_free(sn_tab);
@ -2536,7 +2536,7 @@ static void dump_human_image_info_list(ImageInfoList *list)
} }
delim = true; delim = true;
bdrv_image_info_dump(fprintf, stdout, elem->value); bdrv_image_info_dump(elem->value);
} }
} }

View File

@ -1699,7 +1699,7 @@ static int info_f(BlockBackend *blk, int argc, char **argv)
} }
if (spec_info) { if (spec_info) {
printf("Format specific information:\n"); printf("Format specific information:\n");
bdrv_image_info_specific_dump(fprintf, stdout, spec_info); bdrv_image_info_specific_dump(spec_info);
qapi_free_ImageInfoSpecific(spec_info); qapi_free_ImageInfoSpecific(spec_info);
} }