From patchwork Sat Apr 13 08:56:43 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 236320 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 332C82C00B5 for ; Sat, 13 Apr 2013 19:15:12 +1000 (EST) Received: from localhost ([::1]:33390 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UQwNQ-0005xq-IQ for incoming@patchwork.ozlabs.org; Sat, 13 Apr 2013 05:04:04 -0400 Received: from eggs.gnu.org ([208.118.235.92]:49808) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UQwKV-0001Zu-2D for qemu-devel@nongnu.org; Sat, 13 Apr 2013 05:01:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UQwKT-0000zf-GW for qemu-devel@nongnu.org; Sat, 13 Apr 2013 05:01:02 -0400 Received: from e23smtp03.au.ibm.com ([202.81.31.145]:35991) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UQwKS-0000xN-M1 for qemu-devel@nongnu.org; Sat, 13 Apr 2013 05:01:01 -0400 Received: from /spool/local by e23smtp03.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Sat, 13 Apr 2013 18:53:33 +1000 Received: from d23dlp02.au.ibm.com (202.81.31.213) by e23smtp03.au.ibm.com (202.81.31.209) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Sat, 13 Apr 2013 18:53:15 +1000 Received: from d23relay04.au.ibm.com (d23relay04.au.ibm.com [9.190.234.120]) by d23dlp02.au.ibm.com (Postfix) with ESMTP id 7CFEC2BB0052 for ; Sat, 13 Apr 2013 19:00:40 +1000 (EST) Received: from d23av03.au.ibm.com (d23av03.au.ibm.com [9.190.234.97]) by d23relay04.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r3D8lI4G47513816 for ; Sat, 13 Apr 2013 18:47:18 +1000 Received: from d23av03.au.ibm.com (loopback [127.0.0.1]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r3D90dp2029627 for ; Sat, 13 Apr 2013 19:00:40 +1000 Received: from RH63Wenchao (wenchaox.cn.ibm.com [9.115.122.157]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r3D8u24R020091; Sat, 13 Apr 2013 19:00:37 +1000 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Sat, 13 Apr 2013 16:56:43 +0800 Message-Id: <1365843407-16504-15-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1365843407-16504-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1365843407-16504-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13041308-6102-0000-0000-0000034F2C20 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 202.81.31.145 Cc: kwolf@redhat.com, phrdina@redhat.com, stefanha@gmail.com, armbru@redhat.com, lcapitulino@redhat.com, pbonzini@redhat.com, Wenchao Xia Subject: [Qemu-devel] [PATCH V12 14/18] block: dump to monitor for bdrv_snapshot_dump() and bdrv_image_info_dump() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch introduce a new print function, which will output message to monitor when it present. With it, bdrv_snapshot_dump() need no more buffer and can avoid string truncation, bdrv_image_info_dump() can also be used by hmp code later, besides qemu-img code. Signed-off-by: Wenchao Xia --- block/qapi.c | 58 +++++++++++++++++++++--------------------- include/block/qapi.h | 2 +- include/qemu/error-report.h | 1 + qemu-img.c | 7 +++-- savevm.c | 7 +++-- util/qemu-error.c | 18 +++++++++++++ 6 files changed, 57 insertions(+), 36 deletions(-) diff --git a/block/qapi.c b/block/qapi.c index dc72adc..6e04a7f 100644 --- a/block/qapi.c +++ b/block/qapi.c @@ -351,7 +351,7 @@ BlockInfoList *qmp_query_block(Error **errp) return NULL; } -char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) +void bdrv_snapshot_dump(QEMUSnapshotInfo *sn) { char buf1[128], date_buf[128], clock_buf[128]; struct tm tm; @@ -359,9 +359,8 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) int64_t secs; if (!sn) { - snprintf(buf, buf_size, - "%-10s%-20s%7s%20s%15s", - "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); + message_printf("%-10s%-20s%7s%20s%15s", + "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); } else { ti = sn->date_sec; localtime_r(&ti, &tm); @@ -374,14 +373,13 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) (int)((secs / 60) % 60), (int)(secs % 60), (int)((sn->vm_clock_nsec / 1000000) % 1000)); - snprintf(buf, buf_size, - "%-10s%-20s%7s%20s%15s", - sn->id_str, sn->name, - get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), - date_buf, - clock_buf); + message_printf("%-10s%-20s%7s%20s%15s", + sn->id_str, sn->name, + get_human_readable_size(buf1, sizeof(buf1), + sn->vm_state_size), + date_buf, + clock_buf); } - return buf; } void bdrv_image_info_dump(ImageInfo *info) @@ -394,43 +392,44 @@ void bdrv_image_info_dump(ImageInfo *info) info->actual_size); } get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size); - printf("image: %s\n" - "file format: %s\n" - "virtual size: %s (%" PRId64 " bytes)\n" - "disk size: %s\n", - info->filename, info->format, size_buf, - info->virtual_size, - dsize_buf); + message_printf("image: %s\n" + "file format: %s\n" + "virtual size: %s (%" PRId64 " bytes)\n" + "disk size: %s\n", + info->filename, info->format, size_buf, + info->virtual_size, + dsize_buf); if (info->has_encrypted && info->encrypted) { - printf("encrypted: yes\n"); + message_printf("encrypted: yes\n"); } if (info->has_cluster_size) { - printf("cluster_size: %" PRId64 "\n", info->cluster_size); + message_printf("cluster_size: %" PRId64 "\n", info->cluster_size); } if (info->has_dirty_flag && info->dirty_flag) { - printf("cleanly shut down: no\n"); + message_printf("cleanly shut down: no\n"); } if (info->has_backing_filename) { - printf("backing file: %s", info->backing_filename); + message_printf("backing file: %s", info->backing_filename); if (info->has_full_backing_filename) { - printf(" (actual path: %s)", info->full_backing_filename); + message_printf(" (actual path: %s)", info->full_backing_filename); } - putchar('\n'); + message_printf("\n"); if (info->has_backing_filename_format) { - printf("backing file format: %s\n", info->backing_filename_format); + message_printf("backing file format: %s\n", + info->backing_filename_format); } } if (info->has_snapshots) { SnapshotInfoList *elem; - char buf[256]; - printf("Snapshot list:\n"); - printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); + message_printf("Snapshot list:\n"); + bdrv_snapshot_dump(NULL); + message_printf("\n"); /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but * we convert to the block layer's native QEMUSnapshotInfo for now. @@ -446,7 +445,8 @@ void bdrv_image_info_dump(ImageInfo *info) pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id); pstrcpy(sn.name, sizeof(sn.name), elem->value->name); - printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn)); + bdrv_snapshot_dump(&sn); + message_printf("\n"); } } } diff --git a/include/block/qapi.h b/include/block/qapi.h index 237660f..f7eda89 100644 --- a/include/block/qapi.h +++ b/include/block/qapi.h @@ -38,6 +38,6 @@ int bdrv_query_image_info(BlockDriverState *bs, void bdrv_query_info(BlockDriverState *bs, BlockInfo **p_info, Error **errp); -char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn); +void bdrv_snapshot_dump(QEMUSnapshotInfo *sn); void bdrv_image_info_dump(ImageInfo *info); #endif diff --git a/include/qemu/error-report.h b/include/qemu/error-report.h index c902cc1..90341bd 100644 --- a/include/qemu/error-report.h +++ b/include/qemu/error-report.h @@ -40,4 +40,5 @@ void error_set_progname(const char *argv0); void error_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2); const char *error_get_progname(void); +void message_printf(const char *fmt, ...) GCC_FMT_ATTR(1, 2); #endif diff --git a/qemu-img.c b/qemu-img.c index 5b229a9..8cbcb0b 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1558,16 +1558,17 @@ static void dump_snapshots(BlockDriverState *bs) { QEMUSnapshotInfo *sn_tab, *sn; int nb_sns, i; - char buf[256]; nb_sns = bdrv_snapshot_list(bs, &sn_tab); if (nb_sns <= 0) return; printf("Snapshot list:\n"); - printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); + bdrv_snapshot_dump(NULL); + printf("\n"); for(i = 0; i < nb_sns; i++) { sn = &sn_tab[i]; - printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); + bdrv_snapshot_dump(sn); + printf("\n"); } g_free(sn_tab); } diff --git a/savevm.c b/savevm.c index 1c8fbee..e125567 100644 --- a/savevm.c +++ b/savevm.c @@ -2466,7 +2466,6 @@ void do_info_snapshots(Monitor *mon, const QDict *qdict) int nb_sns, i, ret, available; int total; int *available_snapshots; - char buf[256]; bs = bdrv_snapshots(); if (!bs) { @@ -2509,10 +2508,12 @@ void do_info_snapshots(Monitor *mon, const QDict *qdict) } if (total > 0) { - monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); + bdrv_snapshot_dump(NULL); + monitor_printf(mon, "\n"); for (i = 0; i < total; i++) { sn = &sn_tab[available_snapshots[i]]; - monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); + bdrv_snapshot_dump(sn); + monitor_printf(mon, "\n"); } } else { monitor_printf(mon, "There is no suitable snapshot available\n"); diff --git a/util/qemu-error.c b/util/qemu-error.c index 08a36f4..a47bf32 100644 --- a/util/qemu-error.c +++ b/util/qemu-error.c @@ -213,3 +213,21 @@ void error_report(const char *fmt, ...) va_end(ap); error_printf("\n"); } + +/* + * Print to current monitor if we have one, else to stdout. It is similar with + * error_printf(). + * TODO just like error_vprintf() + */ +void message_printf(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + if (cur_mon) { + monitor_vprintf(cur_mon, fmt, ap); + } else { + vfprintf(stdout, fmt, ap); + } + va_end(ap); +}