[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 05/15] QMP: chardev handling
From: |
Luiz Capitulino |
Subject: |
[Qemu-devel] [PATCH 05/15] QMP: chardev handling |
Date: |
Thu, 19 Nov 2009 13:13:33 -0200 |
This patch adds initial QMP support in QEMU. It's important
to notice that most QMP code will be part of the Monitor.
Input will be handled by monitor_control_read(). Currently it
reads the input and discards it, next patches add proper input
support.
The function monitor_json_emitter(), as its name implies, is
used by the Monitor to emit JSON output. In this commit it's
used by monitor_control_event() to print our greeting message.
Finally, control mode support is also added to monitor_init(),
allowing QMP to be really enabled.
Signed-off-by: Luiz Capitulino <address@hidden>
---
monitor.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 76 insertions(+), 2 deletions(-)
diff --git a/monitor.c b/monitor.c
index 07bd21c..be68dd5 100644
--- a/monitor.c
+++ b/monitor.c
@@ -50,6 +50,7 @@
#include "qdict.h"
#include "qstring.h"
#include "qerror.h"
+#include "qjson.h"
//#define DEBUG
//#define DEBUG_COMPLETION
@@ -92,6 +93,11 @@ struct mon_fd_t {
QLIST_ENTRY(mon_fd_t) next;
};
+typedef struct MonitorControl {
+ uint8_t buf[1024];
+ int size;
+} MonitorControl;
+
struct Monitor {
CharDriverState *chr;
int mux_out;
@@ -101,6 +107,7 @@ struct Monitor {
uint8_t outbuf[1024];
int outbuf_index;
ReadLineState *rs;
+ MonitorControl *mc;
CPUState *mon_cpu;
BlockDriverCompletionFunc *password_completion_cb;
void *password_opaque;
@@ -255,6 +262,17 @@ static void monitor_print_qobject(Monitor *mon, const
QObject *data)
monitor_puts(mon, "\n");
}
+static void monitor_json_emitter(Monitor *mon, const QObject *data)
+{
+ QString *json;
+
+ json = qobject_to_json(data);
+ assert(json != NULL);
+
+ monitor_printf(mon, "%s\n", qstring_get_str(json));
+ QDECREF(json);
+}
+
static int compare_cmd(const char *name, const char *list)
{
const char *p, *pstart;
@@ -3530,6 +3548,37 @@ static int monitor_can_read(void *opaque)
return (mon->suspend_cnt == 0) ? 128 : 0;
}
+/**
+ * monitor_control_read(): Read and handle QMP input
+ */
+static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
+{
+ Monitor *old_mon = cur_mon;
+ int i;
+
+ cur_mon = opaque;
+
+ for (i = 0; i < size; i++) {
+ if (buf[i] == '\r' || buf[i] == '\n') {
+ cur_mon->mc->buf[cur_mon->mc->size] = '\0';
+ cur_mon->mc->size = 0;
+
+ /* TODO: parse QMP input */
+ break;
+ } else {
+ cur_mon->mc->buf[cur_mon->mc->size++] = buf[i];
+ if (cur_mon->mc->size == sizeof(cur_mon->mc->buf)) {
+ /* FIXME: qemu_error_new() */
+ monitor_printf(cur_mon, "Command too long\n");
+ cur_mon->mc->size = 0;
+ break;
+ }
+ }
+ }
+
+ cur_mon = old_mon;
+}
+
static void monitor_read(void *opaque, const uint8_t *buf, int size)
{
Monitor *old_mon = cur_mon;
@@ -3573,6 +3622,24 @@ void monitor_resume(Monitor *mon)
readline_show_prompt(mon->rs);
}
+/**
+ * monitor_control_event(): Print QMP gretting on RESET
+ */
+static void monitor_control_event(void *opaque, int event)
+{
+ if (event == CHR_EVENT_OPENED) {
+ QObject *data;
+ Monitor *mon = opaque;
+
+ data = qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }");
+ assert(data != NULL);
+ monitor_json_emitter(mon, data);
+ qobject_decref(data);
+
+ mon->mc->size = 0;
+ }
+}
+
static void monitor_event(void *opaque, int event)
{
Monitor *mon = opaque;
@@ -3659,8 +3726,15 @@ void monitor_init(CharDriverState *chr, int flags)
monitor_read_command(mon, 0);
}
- qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
- mon);
+ if (monitor_ctrl_mode(mon)) {
+ mon->mc = qemu_mallocz(sizeof(MonitorControl));
+ /* Control mode requires special handlers */
+ qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
+ monitor_control_event, mon);
+ } else {
+ qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
+ monitor_event, mon);
+ }
QLIST_INSERT_HEAD(&mon_list, mon, entry);
if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
--
1.6.5.3.148.g785c5
- [Qemu-devel] [RFC v0 00/15] QEMU Monitor Protocol, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 01/15] monitor: Introduce MONITOR_USE_CONTROL flag, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 02/15] monitor: Command-line flag to enable control mode, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 03/15] monitor: Move handler calling code to its own function, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 04/15] QError: Add errors used by QMP, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 05/15] QMP: chardev handling,
Luiz Capitulino <=
- [Qemu-devel] [PATCH 06/15] QMP: Output support, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 07/15] QMP: Input support, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 08/15] QMP: Asynchronous events infrastructure, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 09/15] QMP: Introduce basic asynchronous events, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 10/15] QMP: Disable monitor print functions, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 11/15] QMP: Introduce README file, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 12/15] QMP: Introduce specification, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 13/15] QMP: Introduce qmp-events.txt, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 14/15] QMP: Introduce qmp-shell, Luiz Capitulino, 2009/11/19
- [Qemu-devel] [PATCH 15/15] QMP: Introduce vm-info, Luiz Capitulino, 2009/11/19