Skip to contentSkip to conversation

string-output-visitor: show structs as "<omitted>"

RFC Superseded Stefan Hajnoczi 2 participants 2 messages Dec 11, 2023 – Dec 12, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC 1/1 string-output-visitor: show structs as "<omitted>"

StringOutputVisitor crashes when it visits a struct because ->start_struct() is NULL.

Show "<omitted>" instead of crashing. This is necessary because the virtio-blk-pci iothread-vq-mapping parameter that I'd like to introduce soon is a list of IOThreadMapping structs.

Cc Markus Armbruster <[email protected]>

---
Can we do better?

I am unfamiliar with StringOutputVisitor, so I wasn't sure how to proceed. Is the format or at least the intended use of StringOutputVisitor's output defined somewhere? Does it need to be a single line or can the output be multiple lines?

Or maybe I shouldn't introduce a qdev property with IOThreadMappingList as its type in
https://lore.kernel.org/qemu-devel/[email protected]/?

---
 include/qapi/string-output-visitor.h |  6 +++---
 qapi/string-output-visitor.c         | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 3 deletions(-)
include/qapi/string-output-visitor.h +3 -3
index 268dfe9986..762fe3f705 100644
@@ -26,9 +26,9 @@ typedef struct StringOutputVisitor StringOutputVisitor;
  * If everything else succeeds, pass @result to visit_complete() to
  * collect the result of the visit.
  *
- * The string output visitor does not implement support for visiting
- * QAPI structs, alternates, null, or arbitrary QTypes.  It also
- * requires a non-null list argument to visit_start_list().
+ * The string output visitor does not implement support for alternates, null,
+ * or arbitrary QTypes.  It also requires a non-null list argument to
+ * visit_start_list().
  */
 Visitor *string_output_visitor_new(bool human, char **result);
 
qapi/string-output-visitor.c +14 -0
index c0cb72dbe4..363dac00fe 100644
@@ -292,6 +292,18 @@ static bool print_type_null(Visitor *v, const char *name, QNull **obj,
     return true;
 }
 
+static bool start_struct(Visitor *v, const char *name, void **obj,
+                         size_t size, Error **errp)
+{
+    return true;
+}
+
+static void end_struct(Visitor *v, void **obj)
+{
+    StringOutputVisitor *sov = to_sov(v);
+    string_output_set(sov, g_strdup("<omitted>"));
+}
+
 static bool
 start_list(Visitor *v, const char *name, GenericList **list, size_t size,
            Error **errp)
@@ -379,6 +391,8 @@ Visitor *string_output_visitor_new(bool human, char **result)
     v->visitor.type_str = print_type_str;
     v->visitor.type_number = print_type_number;
     v->visitor.type_null = print_type_null;
+    v->visitor.start_struct = start_struct;
+    v->visitor.end_struct = end_struct;
     v->visitor.start_list = start_list;
     v->visitor.next_list = next_list;
     v->visitor.end_list = end_list;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 7 lines · Stefan Hajnoczi

StringOutputVisitor crashes when it visits a struct because ->start_struct() is NULL.

Show "<omitted>" instead of crashing. This is necessary because the virtio-blk-pci iothread-vq-mapping parameter that I'd like to introduce soon is a list of IOThreadMapping structs.

Quoted 8 lines
Cc Markus Armbruster <[email protected]>

---
Can we do better?

I am unfamiliar with StringOutputVisitor, so I wasn't sure how to proceed. Is the format or at least the intended use of StringOutputVisitor's output defined somewhere?

Bwuahahahaha!

SCNR

Quoted 2 lines

Does it need to be a
single line or can the output be multiple lines?

I'm afraid we need to review its users to be sure.

Quoted 3 lines

Or maybe I shouldn't introduce a qdev property with IOThreadMappingList as its type in
https://lore.kernel.org/qemu-devel/[email protected]/?

QOM initially supported only scalar properties.

I think maintaining this restriction will lead to awkward interfaces. Lists are clearly useful. And then we'll likely want list of struct, not multiple lists.

Lifting the restriction will take some work.

On the string visitors, see also my musings in

    Subject: Re: [PATCH v2 1/2] qdev: add IOThreadVirtQueueMappingList property type 
    Date: Mon, 11 Dec 2023 16:32:06 +0100
    Message-ID: <[email protected]>
Quoted 10 lines
---
 include/qapi/string-output-visitor.h |  6 +++---
 qapi/string-output-visitor.c         | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 3 deletions(-)
include/qapi/string-output-visitor.h +0 -0
index 268dfe9986..762fe3f705 100644

include/qapi/string-output-visitor.h typedef struct StringOutputVisitor StringOutputVisitor; · line 31

Quoted 9 lines
include/qapi/string-output-visitor.h +3 -3
@@ -26,9 +26,9 @@ typedef struct StringOutputVisitor StringOutputVisitor;
  * If everything else succeeds, pass @result to visit_complete() to
  * collect the result of the visit.
  *
- * The string output visitor does not implement support for visiting
- * QAPI structs, alternates, null, or arbitrary QTypes.  It also
- * requires a non-null list argument to visit_start_list().
+ * The string output visitor does not implement support for alternates, null,
+ * or arbitrary QTypes.  It also requires a non-null list argument to
+ * visit_start_list().

Mention output for structs is information-free?

Quoted 11 lines
include/qapi/string-output-visitor.h +0 -0
  */
 Visitor *string_output_visitor_new(bool human, char **result);
 
qapi/string-output-visitor.c +0 -0
index c0cb72dbe4..363dac00fe 100644
@@ -292,6 +292,18 @@ static bool print_type_null(Visitor *v, const char *name, QNull **obj,
     return true;
 }
 

qapi/string-output-visitor.c static bool print_type_null(Visitor *v, const char *name, QNull **obj, · line 304

Quoted 10 lines
qapi/string-output-visitor.c +10 -0
+static bool start_struct(Visitor *v, const char *name, void **obj,
+                         size_t size, Error **errp)
+{
+    return true;
+}
+
+static void end_struct(Visitor *v, void **obj)
+{
+    StringOutputVisitor *sov = to_sov(v);
+    string_output_set(sov, g_strdup("<omitted>"));

TODO comment?

Quoted 14 lines
qapi/string-output-visitor.c +4 -0
+}
+
 static bool
 start_list(Visitor *v, const char *name, GenericList **list, size_t size,
            Error **errp)
@@ -379,6 +391,8 @@ Visitor *string_output_visitor_new(bool human, char **result)
     v->visitor.type_str = print_type_str;
     v->visitor.type_number = print_type_number;
     v->visitor.type_null = print_type_null;
+    v->visitor.start_struct = start_struct;
+    v->visitor.end_struct = end_struct;
     v->visitor.start_list = start_list;
     v->visitor.next_list = next_list;
     v->visitor.end_list = end_list;