diff mbox

[4/4] HMP: Introduce console command

Message ID 1358759628-18278-5-git-send-email-lilei@linux.vnet.ibm.com
State New
Headers show

Commit Message

Lei Li Jan. 21, 2013, 9:13 a.m. UTC
Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
---
 hmp-commands.hx           |   21 ++++++++++++++++++
 hmp.c                     |   52 +++++++++++++++++++++++++++++++++++++++++++++
 hmp.h                     |    1 +
 include/monitor/monitor.h |    3 ++
 monitor.c                 |   15 +++++++++++++
 5 files changed, 92 insertions(+), 0 deletions(-)

Comments

Anthony Liguori Jan. 21, 2013, 7:52 p.m. UTC | #1
Why don't you resubmit the series without this patch.  I think this
needs a bit of work...

Lei Li <lilei@linux.vnet.ibm.com> writes:

> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
> ---
>  hmp-commands.hx           |   21 ++++++++++++++++++
>  hmp.c                     |   52 +++++++++++++++++++++++++++++++++++++++++++++
>  hmp.h                     |    1 +
>  include/monitor/monitor.h |    3 ++
>  monitor.c                 |   15 +++++++++++++
>  5 files changed, 92 insertions(+), 0 deletions(-)
>
> diff --git a/hmp-commands.hx b/hmp-commands.hx
> index 52ead10..7f7a54a 100644
> --- a/hmp-commands.hx
> +++ b/hmp-commands.hx
> @@ -875,6 +875,27 @@ if the requested size is larger than it.
>  ETEXI
>  
>      {
> +        .name       = "console",
> +        .args_type  = "chardev:s",
> +        .params     = "chardev",
> +        .mhandler.cmd = hmp_console,
> +    },
> +
> +STEXI
> +@item console @var{device}
> +@findex console
> +Connect to the serial console from within the monitor, allow to write data
> +to memchardev @var{chardev}. Exit from the console and return back to
> +monitor by 'ctrl-]' or enter.
> +
> +@example
> +(qemu) console foo
> +foo: data string...
> +@end example
> +
> +ETEXI
> +
> +    {
>          .name       = "migrate",
>          .args_type  = "detach:-d,blk:-b,inc:-i,uri:s",
>          .params     = "[-d] [-b] [-i] uri",
> diff --git a/hmp.c b/hmp.c
> index 7e86c24..fab5090 100644
> --- a/hmp.c
> +++ b/hmp.c
> @@ -1389,3 +1389,55 @@ void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
>      qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
>      hmp_handle_error(mon, &local_err);
>  }
> +
> +enum escape_char
> +{
> +    ESCAPE_CHAR_CTRL_GS = 0x1d  /* ctrl-] used for escape */
> +};
> +
> +static void hmp_read_console(Monitor *mon, const char *data,
> +                             void *opaque)
> +{
> +    CharDriverState *chr = opaque;
> +    uint32_t size = strlen(data);
> +    enum escape_char console_escape = ESCAPE_CHAR_CTRL_GS;
> +
> +    Error *err = NULL;
> +
> +    if (*data == console_escape) {
> +        monitor_resume(mon);
> +        return;
> +    }
> +
> +    qmp_memchar_write(chr->label, size, data, 0, 0, &err);

So how would the user see data that gets read from the console?

There should be a qmp_memchar_read() call somewhere, no?  I think we
previously discussed doing this based on a timer callback...  It's a
little hackish but it should work well enough in practice.

Regards,

Anthony Liguori

> +
> +    if (err) {
> +        monitor_printf(mon, "%s\n", error_get_pretty(err));
> +        monitor_read_command(mon,1);
> +        error_free(err);
> +        return;
> +    }
> +
> +    monitor_read_command(mon, 1);
> +}
> +
> +void hmp_console(Monitor *mon, const QDict *qdict)
> +{
> +    const char *device = qdict_get_str(qdict, "chardev");
> +    CharDriverState *chr;
> +    Error *err = NULL;
> +
> +    chr = qemu_chr_find(device);
> +
> +    if (!chr) {
> +        error_set(&err, QERR_DEVICE_NOT_FOUND, device);
> +        goto out;
> +    }
> +
> +    if (monitor_read_console(mon, device, hmp_read_console, chr) < 0) {
> +        monitor_printf(mon, "Connect to console %s failed\n", device);
> +    }
> +
> +out:
> +    hmp_handle_error(mon, &err);
> +}
> diff --git a/hmp.h b/hmp.h
> index 076d8cf..a01268e 100644
> --- a/hmp.h
> +++ b/hmp.h
> @@ -84,5 +84,6 @@ void hmp_nbd_server_add(Monitor *mon, const QDict *qdict);
>  void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict);
>  void hmp_chardev_add(Monitor *mon, const QDict *qdict);
>  void hmp_chardev_remove(Monitor *mon, const QDict *qdict);
> +void hmp_console(Monitor *mon, const QDict *qdict);
>  
>  #endif
> diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h
> index 87fb49c..a14e965 100644
> --- a/include/monitor/monitor.h
> +++ b/include/monitor/monitor.h
> @@ -86,6 +86,9 @@ ReadLineState *monitor_get_rs(Monitor *mon);
>  int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
>                            void *opaque);
>  
> +int monitor_read_console(Monitor *mon, const char *device,
> +                         ReadLineFunc *readline_func, void *opaque);
> +
>  int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret);
>  
>  int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret);
> diff --git a/monitor.c b/monitor.c
> index 20bd19b..6f6155d 100644
> --- a/monitor.c
> +++ b/monitor.c
> @@ -260,6 +260,21 @@ int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
>      }
>  }
>  
> +int monitor_read_console(Monitor *mon, const char *device,
> +                         ReadLineFunc *readline_func, void *opaque)
> +{
> +    char prompt[60];
> +
> +    if (!mon->rs) {
> +        return -1;
> +    }
> +
> +    snprintf(prompt, sizeof(prompt), "%s: ", device);
> +    readline_start(mon->rs, prompt, 0, readline_func, opaque);
> +
> +    return 0;
> +}
> +
>  void monitor_flush(Monitor *mon)
>  {
>      if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
> -- 
> 1.7.7.6
Lei Li Jan. 22, 2013, 7:43 a.m. UTC | #2
On 01/22/2013 03:52 AM, Anthony Liguori wrote:
> Why don't you resubmit the series without this patch.  I think this
> needs a bit of work...

Sure, I will resubmit the series without this patch and try to implement
this console command as your suggestion in another thread later.

Thanks for your time!

>
> Lei Li <lilei@linux.vnet.ibm.com> writes:
>
>> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
>> ---
>>   hmp-commands.hx           |   21 ++++++++++++++++++
>>   hmp.c                     |   52 +++++++++++++++++++++++++++++++++++++++++++++
>>   hmp.h                     |    1 +
>>   include/monitor/monitor.h |    3 ++
>>   monitor.c                 |   15 +++++++++++++
>>   5 files changed, 92 insertions(+), 0 deletions(-)
>>
>> diff --git a/hmp-commands.hx b/hmp-commands.hx
>> index 52ead10..7f7a54a 100644
>> --- a/hmp-commands.hx
>> +++ b/hmp-commands.hx
>> @@ -875,6 +875,27 @@ if the requested size is larger than it.
>>   ETEXI
>>   
>>       {
>> +        .name       = "console",
>> +        .args_type  = "chardev:s",
>> +        .params     = "chardev",
>> +        .mhandler.cmd = hmp_console,
>> +    },
>> +
>> +STEXI
>> +@item console @var{device}
>> +@findex console
>> +Connect to the serial console from within the monitor, allow to write data
>> +to memchardev @var{chardev}. Exit from the console and return back to
>> +monitor by 'ctrl-]' or enter.
>> +
>> +@example
>> +(qemu) console foo
>> +foo: data string...
>> +@end example
>> +
>> +ETEXI
>> +
>> +    {
>>           .name       = "migrate",
>>           .args_type  = "detach:-d,blk:-b,inc:-i,uri:s",
>>           .params     = "[-d] [-b] [-i] uri",
>> diff --git a/hmp.c b/hmp.c
>> index 7e86c24..fab5090 100644
>> --- a/hmp.c
>> +++ b/hmp.c
>> @@ -1389,3 +1389,55 @@ void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
>>       qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
>>       hmp_handle_error(mon, &local_err);
>>   }
>> +
>> +enum escape_char
>> +{
>> +    ESCAPE_CHAR_CTRL_GS = 0x1d  /* ctrl-] used for escape */
>> +};
>> +
>> +static void hmp_read_console(Monitor *mon, const char *data,
>> +                             void *opaque)
>> +{
>> +    CharDriverState *chr = opaque;
>> +    uint32_t size = strlen(data);
>> +    enum escape_char console_escape = ESCAPE_CHAR_CTRL_GS;
>> +
>> +    Error *err = NULL;
>> +
>> +    if (*data == console_escape) {
>> +        monitor_resume(mon);
>> +        return;
>> +    }
>> +
>> +    qmp_memchar_write(chr->label, size, data, 0, 0, &err);
> So how would the user see data that gets read from the console?

Yeah, I did not consider this.

> There should be a qmp_memchar_read() call somewhere, no?  I think we
> previously discussed doing this based on a timer callback...  It's a
> little hackish but it should work well enough in practice.

Okay, I need to think about this...

>
> Regards,
>
> Anthony Liguori
>
>> +
>> +    if (err) {
>> +        monitor_printf(mon, "%s\n", error_get_pretty(err));
>> +        monitor_read_command(mon,1);
>> +        error_free(err);
>> +        return;
>> +    }
>> +
>> +    monitor_read_command(mon, 1);
>> +}
>> +
>> +void hmp_console(Monitor *mon, const QDict *qdict)
>> +{
>> +    const char *device = qdict_get_str(qdict, "chardev");
>> +    CharDriverState *chr;
>> +    Error *err = NULL;
>> +
>> +    chr = qemu_chr_find(device);
>> +
>> +    if (!chr) {
>> +        error_set(&err, QERR_DEVICE_NOT_FOUND, device);
>> +        goto out;
>> +    }
>> +
>> +    if (monitor_read_console(mon, device, hmp_read_console, chr) < 0) {
>> +        monitor_printf(mon, "Connect to console %s failed\n", device);
>> +    }
>> +
>> +out:
>> +    hmp_handle_error(mon, &err);
>> +}
>> diff --git a/hmp.h b/hmp.h
>> index 076d8cf..a01268e 100644
>> --- a/hmp.h
>> +++ b/hmp.h
>> @@ -84,5 +84,6 @@ void hmp_nbd_server_add(Monitor *mon, const QDict *qdict);
>>   void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict);
>>   void hmp_chardev_add(Monitor *mon, const QDict *qdict);
>>   void hmp_chardev_remove(Monitor *mon, const QDict *qdict);
>> +void hmp_console(Monitor *mon, const QDict *qdict);
>>   
>>   #endif
>> diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h
>> index 87fb49c..a14e965 100644
>> --- a/include/monitor/monitor.h
>> +++ b/include/monitor/monitor.h
>> @@ -86,6 +86,9 @@ ReadLineState *monitor_get_rs(Monitor *mon);
>>   int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
>>                             void *opaque);
>>   
>> +int monitor_read_console(Monitor *mon, const char *device,
>> +                         ReadLineFunc *readline_func, void *opaque);
>> +
>>   int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret);
>>   
>>   int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret);
>> diff --git a/monitor.c b/monitor.c
>> index 20bd19b..6f6155d 100644
>> --- a/monitor.c
>> +++ b/monitor.c
>> @@ -260,6 +260,21 @@ int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
>>       }
>>   }
>>   
>> +int monitor_read_console(Monitor *mon, const char *device,
>> +                         ReadLineFunc *readline_func, void *opaque)
>> +{
>> +    char prompt[60];
>> +
>> +    if (!mon->rs) {
>> +        return -1;
>> +    }
>> +
>> +    snprintf(prompt, sizeof(prompt), "%s: ", device);
>> +    readline_start(mon->rs, prompt, 0, readline_func, opaque);
>> +
>> +    return 0;
>> +}
>> +
>>   void monitor_flush(Monitor *mon)
>>   {
>>       if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
>> -- 
>> 1.7.7.6
diff mbox

Patch

diff --git a/hmp-commands.hx b/hmp-commands.hx
index 52ead10..7f7a54a 100644
--- a/hmp-commands.hx
+++ b/hmp-commands.hx
@@ -875,6 +875,27 @@  if the requested size is larger than it.
 ETEXI
 
     {
+        .name       = "console",
+        .args_type  = "chardev:s",
+        .params     = "chardev",
+        .mhandler.cmd = hmp_console,
+    },
+
+STEXI
+@item console @var{device}
+@findex console
+Connect to the serial console from within the monitor, allow to write data
+to memchardev @var{chardev}. Exit from the console and return back to
+monitor by 'ctrl-]' or enter.
+
+@example
+(qemu) console foo
+foo: data string...
+@end example
+
+ETEXI
+
+    {
         .name       = "migrate",
         .args_type  = "detach:-d,blk:-b,inc:-i,uri:s",
         .params     = "[-d] [-b] [-i] uri",
diff --git a/hmp.c b/hmp.c
index 7e86c24..fab5090 100644
--- a/hmp.c
+++ b/hmp.c
@@ -1389,3 +1389,55 @@  void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
     qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
     hmp_handle_error(mon, &local_err);
 }
+
+enum escape_char
+{
+    ESCAPE_CHAR_CTRL_GS = 0x1d  /* ctrl-] used for escape */
+};
+
+static void hmp_read_console(Monitor *mon, const char *data,
+                             void *opaque)
+{
+    CharDriverState *chr = opaque;
+    uint32_t size = strlen(data);
+    enum escape_char console_escape = ESCAPE_CHAR_CTRL_GS;
+
+    Error *err = NULL;
+
+    if (*data == console_escape) {
+        monitor_resume(mon);
+        return;
+    }
+
+    qmp_memchar_write(chr->label, size, data, 0, 0, &err);
+
+    if (err) {
+        monitor_printf(mon, "%s\n", error_get_pretty(err));
+        monitor_read_command(mon,1);
+        error_free(err);
+        return;
+    }
+
+    monitor_read_command(mon, 1);
+}
+
+void hmp_console(Monitor *mon, const QDict *qdict)
+{
+    const char *device = qdict_get_str(qdict, "chardev");
+    CharDriverState *chr;
+    Error *err = NULL;
+
+    chr = qemu_chr_find(device);
+
+    if (!chr) {
+        error_set(&err, QERR_DEVICE_NOT_FOUND, device);
+        goto out;
+    }
+
+    if (monitor_read_console(mon, device, hmp_read_console, chr) < 0) {
+        monitor_printf(mon, "Connect to console %s failed\n", device);
+    }
+
+out:
+    hmp_handle_error(mon, &err);
+}
diff --git a/hmp.h b/hmp.h
index 076d8cf..a01268e 100644
--- a/hmp.h
+++ b/hmp.h
@@ -84,5 +84,6 @@  void hmp_nbd_server_add(Monitor *mon, const QDict *qdict);
 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict);
 void hmp_chardev_add(Monitor *mon, const QDict *qdict);
 void hmp_chardev_remove(Monitor *mon, const QDict *qdict);
+void hmp_console(Monitor *mon, const QDict *qdict);
 
 #endif
diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h
index 87fb49c..a14e965 100644
--- a/include/monitor/monitor.h
+++ b/include/monitor/monitor.h
@@ -86,6 +86,9 @@  ReadLineState *monitor_get_rs(Monitor *mon);
 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
                           void *opaque);
 
+int monitor_read_console(Monitor *mon, const char *device,
+                         ReadLineFunc *readline_func, void *opaque);
+
 int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret);
 
 int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret);
diff --git a/monitor.c b/monitor.c
index 20bd19b..6f6155d 100644
--- a/monitor.c
+++ b/monitor.c
@@ -260,6 +260,21 @@  int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
     }
 }
 
+int monitor_read_console(Monitor *mon, const char *device,
+                         ReadLineFunc *readline_func, void *opaque)
+{
+    char prompt[60];
+
+    if (!mon->rs) {
+        return -1;
+    }
+
+    snprintf(prompt, sizeof(prompt), "%s: ", device);
+    readline_start(mon->rs, prompt, 0, readline_func, opaque);
+
+    return 0;
+}
+
 void monitor_flush(Monitor *mon)
 {
     if (mon && mon->outbuf_index != 0 && !mon->mux_out) {