: QMP: Human Monitor passthrough
v3 cover : QMP: Human Monitor passthrough
Simple example:
-> { "execute": "human-monitor-command", "arguments": { "command-line": "print /i 10+25" } } <- { "return": "35\r\n" }
Please, check individual patches for details. Also note that this series depends on the script improvements one.
Also, Markus suggestion of having an assert() in qemu_chr_close() have not been added this series because I don't know what to assert(). But that's an incremental change anyway and should prevent this series from being merged.
changelog
---------
v2 -> v3
- Renamed command name to human-monitor-command
- Fixed buggy error reporting when cpu-index is invalid
- Make qemu_chr_mem_to_qs() return a string when outbuf is empty
- Introduced qemu_chr_mem_osize() along with some cleanups
v1 -> v2
- A number of small cleanups and clarifications
Thanks.
PATCH v3 1/3 qemu-char: Introduce Memory driver
This driver handles in-memory chardev operations. That's, all writes to this driver are stored in an internal buffer and it doesn't talk to the external world in any way.
Right now it's very simple: it supports only writes. But it can be easily extended to support more operations.
This is going to be used by the monitor's "HMP passthrough via QMP" feature, which needs to run monitor handlers without a backing device.
--- qemu-char.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-char.h | 7 ++++++ 2 files changed, 77 insertions(+), 0 deletions(-)
qemu-char.c +70 -0
@@ -2275,6 +2275,76 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)return NULL ; } +/***********************************************************/ +/* Memory chardev */ +typedef struct { +size_t outbuf_size; +size_t outbuf_capacity; +uint8_t *outbuf; +}MemoryDriver ; + +static int mem_chr_write (CharDriverState *chr,const uint8_t *buf,int len) +{ +MemoryDriver *d = chr->opaque; + +/* TODO: the QString implementation has the same code, we should +* introduce a generic way to do this in cutils.c */ +if (d->outbuf_capacity < d->outbuf_size + len) { +/* grow outbuf */ + d->outbuf_capacity += len; + d->outbuf_capacity *=2 ; + d->outbuf =qemu_realloc (d->outbuf, d->outbuf_capacity); + } + +memcpy (d->outbuf + d->outbuf_size, buf, len); + d->outbuf_size += len; + +return len; +} + +void qemu_chr_init_mem (CharDriverState *chr) +{ +MemoryDriver *d; + + d =qemu_malloc (sizeof (*d)); + d->outbuf_size =0 ; + d->outbuf_capacity =4096 ; + d->outbuf =qemu_mallocz (d->outbuf_capacity); + +memset (chr,0 ,sizeof (*chr)); + chr->opaque = d; + chr->chr_write = mem_chr_write; +} + +/* assumes the stored data is a string */ +QString *qemu_chr_mem_to_qs (CharDriverState *chr) +{ +MemoryDriver *d = chr->opaque; + +if (d->outbuf_size ==0 ) { +return qstring_new (); + } + +return qstring_from_substr ((char *) d->outbuf,0 , d->outbuf_size -1 ); +} + +/* NOTE: this driver can not be closed with qemu_chr_close()! */ +void qemu_chr_close_mem (CharDriverState *chr) +{ +MemoryDriver *d = chr->opaque; + +qemu_free (d->outbuf); +qemu_free (chr->opaque); + chr->opaque =NULL ; + chr->chr_write =NULL ; +} + +size_t qemu_chr_mem_osize (const CharDriverState *chr) +{ +const MemoryDriver *d = chr->opaque; +return d->outbuf_size; +} +QemuOpts *qemu_chr_parse_compat (const char *label,const char *filename) {char host[65 ], port[33 ], width[8 ], height[8 ];
qemu-char.h +7 -0
@@ -6,6 +6,7 @@#include "qemu-option.h" #include "qemu-config.h" #include "qobject.h" +#include "qstring.h" /* character device */
@@ -100,6 +101,12 @@ CharDriverState *qemu_chr_open_eventfd(int eventfd);extern int term_escape_char; +/* memory chardev */ +void qemu_chr_init_mem (CharDriverState *chr); +void qemu_chr_close_mem (CharDriverState *chr); +QString *qemu_chr_mem_to_qs (CharDriverState *chr); +size_t qemu_chr_mem_osize (const CharDriverState *chr); +/* async I/O support */ int qemu_set_fd_handler2(int fd,
Signature · 1 line
1.7.3.2.164.g6f10c
Re: [Qemu-devel] [PATCH 1/3] qemu-char: Introduce Memory driver
Quoted 57 lines · Luiz Capitulino
This driver handles in-memory chardev operations. That's, all writes to this driver are stored in an internal buffer and it doesn't talk to the external world in any way.
Right now it's very simple: it supports only writes. But it can be easily extended to support more operations.
This is going to be used by the monitor's "HMP passthrough via QMP" feature, which needs to run monitor handlers without a backing device.
Signed-off-by Luiz Capitulino <[email protected]>--- qemu-char.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-char.h | 7 ++++++ 2 files changed, 77 insertions(+), 0 deletions(-)qemu-char.c +32
-0@@ -2275,6 +2275,76 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)return NULL ; } +/***********************************************************/ +/* Memory chardev */ +typedef struct { +size_t outbuf_size; +size_t outbuf_capacity; +uint8_t *outbuf; +}MemoryDriver ; + +static int mem_chr_write (CharDriverState *chr,const uint8_t *buf,int len) +{ +MemoryDriver *d = chr->opaque; + +/* TODO: the QString implementation has the same code, we should +* introduce a generic way to do this in cutils.c */ +if (d->outbuf_capacity < d->outbuf_size + len) { +/* grow outbuf */ + d->outbuf_capacity += len; + d->outbuf_capacity *=2 ; + d->outbuf =qemu_realloc (d->outbuf, d->outbuf_capacity); + } + +memcpy (d->outbuf + d->outbuf_size, buf, len); + d->outbuf_size += len; + +return len; +} + +void qemu_chr_init_mem (CharDriverState *chr) +{ +MemoryDriver *d; + + d =qemu_malloc (sizeof (*d));
qemu-char.c static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) · line 2319
Quoted 10 lines
qemu-char.c +10
-0+ d->outbuf_size =0 ; + d->outbuf_capacity =4096 ; + d->outbuf =qemu_mallocz (d->outbuf_capacity); + +memset (chr,0 ,sizeof (*chr)); + chr->opaque = d; + chr->chr_write = mem_chr_write; +} + +/* assumes the stored data is a string */
This could indicate a problem. See my reply in the thread for v2.
qemu-char.c static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) · line 2326
Quoted 7 lines
qemu-char.c +7
-0+QString *qemu_chr_mem_to_qs (CharDriverState *chr) +{ +MemoryDriver *d = chr->opaque; + +if (d->outbuf_size ==0 ) { +return qstring_new (); + }
Why is this necessary? Is qstring_from_substr() broken for empty substrings? If it is, it ought to be fixed!
Quoted 49 lines
qemu-char.c +21
-0+ +return qstring_from_substr ((char *) d->outbuf,0 , d->outbuf_size -1 ); +} + +/* NOTE: this driver can not be closed with qemu_chr_close()! */ +void qemu_chr_close_mem (CharDriverState *chr) +{ +MemoryDriver *d = chr->opaque; + +qemu_free (d->outbuf); +qemu_free (chr->opaque); + chr->opaque =NULL ; + chr->chr_write =NULL ; +} + +size_t qemu_chr_mem_osize (const CharDriverState *chr) +{ +const MemoryDriver *d = chr->opaque; +return d->outbuf_size; +} +QemuOpts *qemu_chr_parse_compat (const char *label,const char *filename) {char host[65 ], port[33 ], width[8 ], height[8 ];qemu-char.h +7
-0@@ -6,6 +6,7 @@#include "qemu-option.h" #include "qemu-config.h" #include "qobject.h" +#include "qstring.h" /* character device */ @@ -100,6 +101,12 @@ CharDriverState *qemu_chr_open_eventfd(int eventfd);extern int term_escape_char; +/* memory chardev */ +void qemu_chr_init_mem (CharDriverState *chr); +void qemu_chr_close_mem (CharDriverState *chr); +QString *qemu_chr_mem_to_qs (CharDriverState *chr); +size_t qemu_chr_mem_osize (const CharDriverState *chr); +/* async I/O support */ int qemu_set_fd_handler2(int fd,
Re: [Qemu-devel] [PATCH 1/3] qemu-char: Introduce Memory driver
Quoted 63 lines · On Fri, 12 Nov 2010 11:21:57 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 61 lines · Luiz Capitulino
This driver handles in-memory chardev operations. That's, all writes to this driver are stored in an internal buffer and it doesn't talk to the external world in any way.
Right now it's very simple: it supports only writes. But it can be easily extended to support more operations.
This is going to be used by the monitor's "HMP passthrough via QMP" feature, which needs to run monitor handlers without a backing device.
Signed-off-by Luiz Capitulino <[email protected]>--- qemu-char.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-char.h | 7 ++++++ 2 files changed, 77 insertions(+), 0 deletions(-)qemu-char.c +36
-0@@ -2275,6 +2275,76 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)return NULL ; } +/***********************************************************/ +/* Memory chardev */ +typedef struct { +size_t outbuf_size; +size_t outbuf_capacity; +uint8_t *outbuf; +}MemoryDriver ; + +static int mem_chr_write (CharDriverState *chr,const uint8_t *buf,int len) +{ +MemoryDriver *d = chr->opaque; + +/* TODO: the QString implementation has the same code, we should +* introduce a generic way to do this in cutils.c */ +if (d->outbuf_capacity < d->outbuf_size + len) { +/* grow outbuf */ + d->outbuf_capacity += len; + d->outbuf_capacity *=2 ; + d->outbuf =qemu_realloc (d->outbuf, d->outbuf_capacity); + } + +memcpy (d->outbuf + d->outbuf_size, buf, len); + d->outbuf_size += len; + +return len; +} + +void qemu_chr_init_mem (CharDriverState *chr) +{ +MemoryDriver *d; + + d =qemu_malloc (sizeof (*d)); + d->outbuf_size =0 ; + d->outbuf_capacity =4096 ; + d->outbuf =qemu_mallocz (d->outbuf_capacity); +
Quoted 8 lines
qemu-char.c static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) · line 2319
Quoted 6 lines
qemu-char.c +6
-0+memset (chr,0 ,sizeof (*chr)); + chr->opaque = d; + chr->chr_write = mem_chr_write; +} + +/* assumes the stored data is a string */ This could indicate a problem. See my reply in the thread for v2.
Replied, but I can't see the problem.
Quoted 10 lines
qemu-char.c static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) · line 2326
Quoted 7 lines
qemu-char.c +7
-0+QString *qemu_chr_mem_to_qs (CharDriverState *chr) +{ +MemoryDriver *d = chr->opaque; + +if (d->outbuf_size ==0 ) { +return qstring_new (); + }Why is this necessary? Is
qstring_from_substr()broken for empty substrings? If it is, it ought to be fixed!
qstring_from_substr() takes a character range; outbuf_size stores a size, not a string length. So we do:
Quoted 1 line
qemu-char.c static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) · line 2328
Quoted 1 line
qemu-char.c +1
-0+return qstring_from_substr ((char *) d->outbuf,0 , d->outbuf_size -1 );
If outbuf_size is 0, we'll be passing a negative value down.
Quoted 48 lines
Quoted 47 lines
qemu-char.c +19
-0+} + +/* NOTE: this driver can not be closed with qemu_chr_close()! */ +void qemu_chr_close_mem (CharDriverState *chr) +{ +MemoryDriver *d = chr->opaque; + +qemu_free (d->outbuf); +qemu_free (chr->opaque); + chr->opaque =NULL ; + chr->chr_write =NULL ; +} + +size_t qemu_chr_mem_osize (const CharDriverState *chr) +{ +const MemoryDriver *d = chr->opaque; +return d->outbuf_size; +} +QemuOpts *qemu_chr_parse_compat (const char *label,const char *filename) {char host[65 ], port[33 ], width[8 ], height[8 ];qemu-char.h +7
-0@@ -6,6 +6,7 @@#include "qemu-option.h" #include "qemu-config.h" #include "qobject.h" +#include "qstring.h" /* character device */ @@ -100,6 +101,12 @@ CharDriverState *qemu_chr_open_eventfd(int eventfd);extern int term_escape_char; +/* memory chardev */ +void qemu_chr_init_mem (CharDriverState *chr); +void qemu_chr_close_mem (CharDriverState *chr); +QString *qemu_chr_mem_to_qs (CharDriverState *chr); +size_t qemu_chr_mem_osize (const CharDriverState *chr); +/* async I/O support */ int qemu_set_fd_handler2(int fd,
Re: [Qemu-devel] [PATCH 1/3] qemu-char: Introduce Memory driver
Quoted 4 lines · Luiz Capitulino
Quoted 1 line · On Fri, 12 Nov 2010 11:21:57 +0100 Markus Armbruster <[email protected]> wrote:
Luiz Capitulino <[email protected]> writes:
[...]
Quoted 11 lines
Quoted 10 lines
qemu-char.c static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) · line 2326
Quoted 7 lines
qemu-char.c +7
-0+QString *qemu_chr_mem_to_qs (CharDriverState *chr) +{ +MemoryDriver *d = chr->opaque; + +if (d->outbuf_size ==0 ) { +return qstring_new (); + }Why is this necessary? Is
qstring_from_substr()broken for empty substrings? If it is, it ought to be fixed!
Quoted 6 lines
qstring_from_substr()takes a character range;outbuf_sizestores a size, not a string length. So we do:Quoted 1 line
Quoted 1 line
+ return
qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);If
outbuf_sizeis 0, we'll be passing a negative value down.
What's wrong with that?
[...]
Re: [Qemu-devel] [PATCH 1/3] qemu-char: Introduce Memory driver
Quoted 18 lines · On Fri, 12 Nov 2010 15:16:33 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 4 lines · Luiz Capitulino
Quoted 1 line · On Fri, 12 Nov 2010 11:21:57 +0100 Markus Armbruster <[email protected]> wrote:
Luiz Capitulino <[email protected]> writes:
[...]
Quoted 11 lines
Quoted 10 lines
qemu-char.c static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) · line 2326
Quoted 7 lines
qemu-char.c +7
-0+QString *qemu_chr_mem_to_qs (CharDriverState *chr) +{ +MemoryDriver *d = chr->opaque; + +if (d->outbuf_size ==0 ) { +return qstring_new (); + }Why is this necessary? Is
qstring_from_substr()broken for empty substrings? If it is, it ought to be fixed!
Quoted 8 lines
Quoted 6 lines
qstring_from_substr()takes a character range;outbuf_sizestores a size, not a string length. So we do:Quoted 1 line
Quoted 1 line
+ return
qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);If
outbuf_sizeis 0, we'll be passing a negative value down.What's wrong with that?
Although it's going to work with the current QString implementation, I don't think it's it's a good idea to rely on a negative index.
Maybe, we could have:
return qstring_from_substr((char *) d->outbuf, 0,
d->outbuf_size > 0 ? d->outbuf_size - 1 : 0);
A bit harder to read, but makes the function smaller.
Re: [Qemu-devel] [PATCH 1/3] qemu-char: Introduce Memory driver
Quoted 24 lines · Luiz Capitulino
Quoted 21 lines · On Fri, 12 Nov 2010 15:16:33 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 4 lines · Luiz Capitulino
Quoted 1 line · On Fri, 12 Nov 2010 11:21:57 +0100 Markus Armbruster <[email protected]> wrote:
Luiz Capitulino <[email protected]> writes:
[...]
Quoted 14 lines
Quoted 10 lines
qemu-char.c static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) · line 2326
Quoted 7 lines
qemu-char.c +7
-0+QString *qemu_chr_mem_to_qs (CharDriverState *chr) +{ +MemoryDriver *d = chr->opaque; + +if (d->outbuf_size ==0 ) { +return qstring_new (); + }Why is this necessary? Is
qstring_from_substr()broken for empty substrings? If it is, it ought to be fixed!
qstring_from_substr()takes a character range;outbuf_sizestores a size, not a string length. So we do:
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
+ return
qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);If
outbuf_sizeis 0, we'll be passing a negative value down.What's wrong with that?
Although it's going to work with the current QString implementation, I don't think it's it's a good idea to rely on a negative index.
How should I extract the substring of S beginning at index B with length L? If I cant't do this for any B, L with interval [B,B+L-1] fully within [0,length(S)], then the API is flawed, and ought to be replaced.
Quoted 6 lines
Maybe, we could have:
return qstring_from_substr((char *)
d->outbuf, 0,d->outbuf_size > 0 ? d->outbuf_size - 1 : 0);A bit harder to read, but makes the function smaller.
Err, doesn't qstring_from_substr(s, 0, 0) extract a substring of length 1?
Re: [Qemu-devel] [PATCH 1/3] qemu-char: Introduce Memory driver
Quoted 38 lines · On Fri, 12 Nov 2010 16:04:39 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 24 lines · Luiz Capitulino
Quoted 21 lines · On Fri, 12 Nov 2010 15:16:33 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 4 lines · Luiz Capitulino
Quoted 1 line · On Fri, 12 Nov 2010 11:21:57 +0100 Markus Armbruster <[email protected]> wrote:
Luiz Capitulino <[email protected]> writes:
[...]
Quoted 14 lines
Quoted 10 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+QString *qemu_chr_mem_to_qs(CharDriverState *chr) +{ + MemoryDriver *d = chr->opaque; + + if (d->outbuf_size == 0) { + return qstring_new(); + }Why is this necessary? Is
qstring_from_substr()broken for empty substrings? If it is, it ought to be fixed!
qstring_from_substr()takes a character range;outbuf_sizestores a size, not a string length. So we do:Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
+ return
qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);If
outbuf_sizeis 0, we'll be passing a negative value down.What's wrong with that?
Although it's going to work with the current QString implementation, I don't think it's it's a good idea to rely on a negative index.
How should I extract the substring of S beginning at index B with length L? If I cant't do this for any B, L with interval [B,B+L-1] fully within [0,length(S)], then the API is flawed, and ought to be replaced.
Quoted 1 line · anymore. When you
Quoted 1 line
Quoted 1 line
What's wrong with that?
What did you mean? Did you mean 'let's not decrement outbuf_size' or did you mean 'let's pass -1 anyway'?
Both seem wrong to me: the substring [0,-1] should be invalid and not decrementing outbuf_size is wrong, because it contains the buffer size and qstring_from_substr() will consume an additional char from the buffer (which should be '\0' today, but we shouldn't count on that).
Quoted 10 lines
Quoted 7 lines
Maybe, we could have:
return qstring_from_substr((char *)
d->outbuf, 0,d->outbuf_size > 0 ? d->outbuf_size - 1 : 0);A bit harder to read, but makes the function smaller.
Err, doesn't
qstring_from_substr(s, 0, 0)extract a substring of length 1?
Yeah, it's a bug. But that doesn't change my suggestion, can we do this way?
This should fix the bug (not even compiled tested):
qstring.c +3 -3
@@ -42,10 +42,10 @@ QString *qstring_from_substr(const char *str, int start, int end) qstring =qemu_malloc (sizeof (*qstring)); - qstring->length = end - start +1 ; - qstring->capacity = qstring->length; + qstring->length = end - start; + qstring->capacity = qstring->length +1 ; - qstring->string =qemu_malloc (qstring->capacity +1 ); + qstring->string =qemu_malloc (qstring->capacity);memcpy (qstring->string, str + start, qstring->length); qstring->string[qstring->length] =0 ;
Re: [Qemu-devel] [PATCH 1/3] qemu-char: Introduce Memory driver
Quoted 42 lines · Luiz Capitulino
Quoted 30 lines · On Fri, 12 Nov 2010 16:04:39 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 28 lines · Luiz Capitulino
Quoted 25 lines · On Fri, 12 Nov 2010 15:16:33 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 4 lines · Luiz Capitulino
Quoted 1 line · On Fri, 12 Nov 2010 11:21:57 +0100 Markus Armbruster <[email protected]> wrote:
Luiz Capitulino <[email protected]> writes:
[...]
Quoted 17 lines
Quoted 10 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+QString *qemu_chr_mem_to_qs(CharDriverState *chr) +{ + MemoryDriver *d = chr->opaque; + + if (d->outbuf_size == 0) { + return qstring_new(); + }Why is this necessary? Is
qstring_from_substr()broken for empty substrings? If it is, it ought to be fixed!
qstring_from_substr()takes a character range;outbuf_sizestores a size, not a string length. So we do:Quoted 1 line
Quoted 1 line
+ return
qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);If
outbuf_sizeis 0, we'll be passing a negative value down.Quoted 8 lines
Quoted 4 lines
Quoted 1 line
What's wrong with that?
Although it's going to work with the current QString implementation, I don't think it's it's a good idea to rely on a negative index.
How should I extract the substring of S beginning at index B with length L? If I cant't do this for any B, L with interval [B,B+L-1] fully within [0,length(S)], then the API is flawed, and ought to be replaced.
Quoted 6 lines
Quoted 1 line · anymore. When you
Quoted 1 line
Quoted 1 line
What's wrong with that?
What did you mean? Did you mean 'let's not decrement
outbuf_size' or did you mean 'let's pass -1 anyway'?
Yes, what's wrong with qstring_from_substr(S, 0, -1)?
Its function comment is imprecise, it doesn't tell us whether the END-th character is included in the substring or not.
The code, however, is clear enough: it *is* included. And the unit test checks that.
Therefore, qstring_from_substr("abc", 0, 0) returns the qstring "a".
Quoted 1 line
Both seem wrong to me: the substring [0,-1] should be invalid
Why?
How do you express "the empty substring starting at 0" then?
Quoted 27 lines
and not
decrementingoutbuf_sizeis wrong, because it contains the buffer size andqstring_from_substr()will consume an additional char from the buffer (which should be '\0' today, but we shouldn't count on that).Quoted 10 lines
Quoted 7 lines
Maybe, we could have:
return qstring_from_substr((char *)
d->outbuf, 0,d->outbuf_size > 0 ? d->outbuf_size - 1 : 0);A bit harder to read, but makes the function smaller.
Err, doesn't
qstring_from_substr(s, 0, 0)extract a substring of length 1?Yeah, it's a bug. But that doesn't change my suggestion, can we do this way?
This should fix the bug (not even compiled tested):
qstring.c +0
-0@@ -42,10 +42,10 @@ QString *qstring_from_substr(const char *str, int start, int end) qstring =qemu_malloc (sizeof (*qstring));
qstring.c QString *qstring_from_substr(const char *str, int start, int end) · line 50
Quoted 10 lines
qstring.c +3
-3- qstring->length = end - start +1 ; - qstring->capacity = qstring->length; + qstring->length = end - start; + qstring->capacity = qstring->length +1 ; - qstring->string =qemu_malloc (qstring->capacity +1 ); + qstring->string =qemu_malloc (qstring->capacity);memcpy (qstring->string, str + start, qstring->length); qstring->string[qstring->length] =0 ;
I suspect this will fail your unit test.
Re: [Qemu-devel] [PATCH 1/3] qemu-char: Introduce Memory driver
Quoted 59 lines · On Fri, 12 Nov 2010 17:06:16 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 42 lines · Luiz Capitulino
Quoted 30 lines · On Fri, 12 Nov 2010 16:04:39 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 28 lines · Luiz Capitulino
Quoted 25 lines · On Fri, 12 Nov 2010 15:16:33 +0100 Markus Armbruster <[email protected]> wrote:
Quoted 4 lines · Luiz Capitulino
Quoted 1 line · On Fri, 12 Nov 2010 11:21:57 +0100 Markus Armbruster <[email protected]> wrote:
Luiz Capitulino <[email protected]> writes:
[...]
Quoted 17 lines
Quoted 10 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+QString *qemu_chr_mem_to_qs(CharDriverState *chr) +{ + MemoryDriver *d = chr->opaque; + + if (d->outbuf_size == 0) { + return qstring_new(); + }Why is this necessary? Is
qstring_from_substr()broken for empty substrings? If it is, it ought to be fixed!
qstring_from_substr()takes a character range;outbuf_sizestores a size, not a string length. So we do:Quoted 1 line
Quoted 1 line
+ return
qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);If
outbuf_sizeis 0, we'll be passing a negative value down.Quoted 8 lines
Quoted 4 lines
Quoted 1 line
What's wrong with that?
Although it's going to work with the current QString implementation, I don't think it's it's a good idea to rely on a negative index.
How should I extract the substring of S beginning at index B with length L? If I cant't do this for any B, L with interval [B,B+L-1] fully within [0,length(S)], then the API is flawed, and ought to be replaced.
Quoted 6 lines
Quoted 1 line · anymore. When you
Quoted 1 line
Quoted 1 line
What's wrong with that?
What did you mean? Did you mean 'let's not decrement
outbuf_size' or did you mean 'let's pass -1 anyway'?Yes, what's wrong with
qstring_from_substr(S, 0, -1)?Its function comment is imprecise, it doesn't tell us whether the END-th character is included in the substring or not.
The code, however, is clear enough: it *is* included. And the unit test checks that.
Quoted 7 lines
Therefore,
qstring_from_substr("abc", 0, 0)returns the qstring "a".Quoted 1 line
Both seem wrong to me: the substring [0,-1] should be invalid
Why?
How do you express "the empty substring starting at 0" then?
I didn't consider that when I wrote the code, so it's a matter a defining the behavior we want it to have.
Quoted 34 lines
Quoted 34 lines
and not
decrementingoutbuf_sizeis wrong, because it contains the buffer size andqstring_from_substr()will consume an additional char from the buffer (which should be '\0' today, but we shouldn't count on that).Quoted 10 lines
Quoted 7 lines
Maybe, we could have:
return qstring_from_substr((char *)
d->outbuf, 0,d->outbuf_size > 0 ? d->outbuf_size - 1 : 0);A bit harder to read, but makes the function smaller.
Err, doesn't
qstring_from_substr(s, 0, 0)extract a substring of length 1?Yeah, it's a bug. But that doesn't change my suggestion, can we do this way?
This should fix the bug (not even compiled tested):
qstring.c +2
-2@@ -42,10 +42,10 @@ QString *qstring_from_substr(const char *str, int start, int end) qstring =qemu_malloc (sizeof (*qstring)); - qstring->length = end - start +1 ; - qstring->capacity = qstring->length; + qstring->length = end - start; + qstring->capacity = qstring->length +1 ;
Quoted 6 lines
qstring.c QString *qstring_from_substr(const char *str, int start, int end) · line 50
Quoted 4 lines
qstring.c +1
-1- qstring->string =qemu_malloc (qstring->capacity +1 ); + qstring->string =qemu_malloc (qstring->capacity);memcpy (qstring->string, str + start, qstring->length); qstring->string[qstring->length] =0 ;I suspect this will fail your unit test.
Haven't checked it yet, but maybe it has to be fixed too.
PATCH v3 2/3 QMP: Introduce Human Monitor passthrough command
In reply to Luiz CapitulinoThis command allows QMP clients to execute HMP commands.
Please, check the documentation added to the qmp-commands.hx file for additional details about the interface and its limitations.
--- monitor.c | 38 ++++++++++++++++++++++++++++++++++++++ qmp-commands.hx | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 0 deletions(-)
monitor.c +38 -0
@@ -491,6 +491,44 @@ static int do_qmp_capabilities(Monitor *mon, const QDict *params,return 0 ; } +static int mon_set_cpu (int cpu_index); +static void handle_user_command (Monitor *mon,const char *cmdline); + +static int do_hmp_passthrough (Monitor *mon,const QDict *params, +QObject **ret_data) +{ +int ret =0 ; +Monitor *old_mon, hmp; +CharDriverState mchar; + +memset (&hmp,0 ,sizeof (hmp)); +qemu_chr_init_mem (&mchar); + hmp.chr = &mchar; + + old_mon = cur_mon; + cur_mon = &hmp; + +if (qdict_haskey (params,"cpu-index" )) { + ret =mon_set_cpu (qdict_get_int (params,"cpu-index" )); +if (ret <0 ) { + cur_mon = old_mon; +qerror_report (QERR_INVALID_PARAMETER_VALUE ,"cpu-index" ,"a CPU number" ); + goto out; + } + } + +handle_user_command (&hmp,qdict_get_str (params,"command-line" )); + cur_mon = old_mon; + +if (qemu_chr_mem_osize (hmp.chr) >0 ) { + *ret_data =QOBJECT (qemu_chr_mem_to_qs (hmp.chr)); + } + +out: +qemu_chr_close_mem (hmp.chr); +return ret; +} +static int compare_cmd (const char *name,const char *list) {const char *p, *pstart;
qmp-commands.hx +45 -0
51 lines not on this page · raw message
Signature · 1 line
1.7.3.2.164.g6f10c
PATCH v3 3/3 QMP/qmp-shell: Introduce HMP mode
In reply to Luiz CapitulinoIn which qmp-shell will exclusively use the HMP passthrough feature, this is useful for testing.
Example:
# ./qmp-shell -H qmp-sock
Welcome to the HMP shell!
Connected to QEMU 0.13.50 (QEMU) info network
VLAN 0 devices:
user.0: net=10.0.2.0, restricted=n
e1000.0: model=e1000,macaddr=52:54:00:12:34:56 Devices not on any VLAN:
(QEMU)
--- QMP/qmp-shell | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 74 insertions(+), 1 deletions(-)
QMP/qmp-shell +74 -1
@@ -145,6 +145,72 @@ class QMPShell(qmp.QEMUMonitorProtocol): else: return self._execute_cmd(cmdline) +class HMPShell(QMPShell): + def __init__(self, address): + QMPShell.__init__(self, address) + self.__cpu_index = 0 + + def __cmd_completion(self): + for cmd in self.__cmd_passthrough('help')['return'].split('\r\n'): + if cmd and cmd[0] != '[' and cmd[0] != '\t': + name = cmd.split()[0] # drop help text + if name == 'info': + continue + if name.find('|') != -1: + # Command in the form 'foobar|f' or 'f|foobar', take the + # full name + opt = name.split('|') + if len(opt[0]) == 1: + name = opt[1] + else: + name = opt[0] + self._completer.append(name) + self._completer.append('help ' + name) # help completion + + def __info_completion(self): + for cmd in self.__cmd_passthrough('info')['return'].split('\r\n'): + if cmd: + self._completer.append('info ' + cmd.split()[1]) + + def __other_completion(self): + # special cases + self._completer.append('help info') + + def _fill_completion(self): + self.__cmd_completion() + self.__info_completion() + self.__other_completion() + + def __cmd_passthrough(self, cmdline): + return self.cmd_obj({ 'execute': 'human-monitor-command', 'arguments': + { 'command-line': cmdline, + 'cpu-index': self.__cpu_index } }) + + def _execute_cmd(self, cmdline): + if cmdline.split()[0] == "cpu": + # trap the cpu command, it requires special setting + try: + self.__cpu_index = int(cmdline.split()[1]) + except ValueError: + print 'cpu command takes an integer argument' + return True + resp = self.__cmd_passthrough(cmdline) + if resp is None: + print 'Disconnected' + return False + assert 'return' in resp or 'error' in resp + if 'return' in resp: + # Success + if len(resp['return']) > 0: + print resp['return'], + else: + # Error + print '%s: %s' % (resp['error']['class'], resp['error']['desc']) + return True + + def show_banner(self): + QMPShell.show_banner(self, msg='Welcome to the HMP shell!') + def die(msg): sys.stderr.write('ERROR: %s\n' % msg) sys.exit(1)
@@ -156,9 +222,16 @@ def fail_cmdline(option=None): sys.exit(1) def main(): + addr = '' try: if len(sys.argv) == 2: qemu = QMPShell(sys.argv[1]) + addr = sys.argv[1] + elif len(sys.argv) == 3: + if sys.argv[1] != '-H': + fail_cmdline(sys.argv[1]) + qemu = HMPShell(sys.argv[2]) + addr = sys.argv[2] else: fail_cmdline() except QMPShellBadPort:
@@ -171,7 +244,7 @@ def main(): except qmp.QMPCapabilitiesError: die('Could not negotiate capabilities') except qemu.error: - die('Could not connect to %s' % sys.argv[1]) + die('Could not connect to %s' % addr) qemu.show_banner() while qemu.read_exec_command('(QEMU) '):
Signature · 1 line
1.7.3.2.164.g6f10c