[CRIU] [PATCH] protobuf: add generic ability for vertical objects show

Kinsbursky Stanislav skinsbursky at openvz.org
Mon Jul 23 06:15:08 EDT 2012


From: Stanislav Kinsbursky <skinsbursky at openvz.org>

New helper pb_show_vertical() prints only one object line by line.

Signed-off-by: Stanislav Kinsbursky <skinsbursky at openvz.org>
---
 include/protobuf.h |    9 +++++++--
 protobuf.c         |   44 +++++++++++++++++++++++++-------------------
 2 files changed, 32 insertions(+), 21 deletions(-)
-------------- next part --------------
diff --git a/include/protobuf.h b/include/protobuf.h
index 2818ae4..5edb7cb 100644
--- a/include/protobuf.h
+++ b/include/protobuf.h
@@ -59,12 +59,17 @@ extern int pb_write_object_with_header(int fd, void *obj,
 #include <google/protobuf-c/protobuf-c.h>
 
 extern void do_pb_show_plain(int fd, const ProtobufCMessageDescriptor *d,
-		pb_unpack_t unpack, pb_free_t free);
+		pb_unpack_t unpack, pb_free_t free, int single_entry);
 
 /* Don't have objects at hands to also do typechecking here */
 #define pb_show_plain(__fd, __proto_message_name)			\
 	do_pb_show_plain(__fd, &__proto_message_name##__descriptor,	\
 			(pb_unpack_t)__proto_message_name##__unpack,			\
-			(pb_free_t)__proto_message_name##__free_unpacked)
+			(pb_free_t)__proto_message_name##__free_unpacked, 0)
+
+#define pb_show_vertical(__fd, __proto_message_name)			\
+	do_pb_show_plain(__fd, &__proto_message_name##__descriptor,	\
+			(pb_unpack_t)__proto_message_name##__unpack,			\
+			(pb_free_t)__proto_message_name##__free_unpacked, 1)
 
 #endif /* PROTOBUF_H__ */
diff --git a/protobuf.c b/protobuf.c
index ff6c937..82f3730 100644
--- a/protobuf.c
+++ b/protobuf.c
@@ -20,38 +20,39 @@
  */
 #define PB_PKOBJ_LOCAL_SIZE	1024
 
-typedef void (pb_pr_field_t)(void *obj, void *arg);
+typedef void (pb_pr_field_t)(void *obj, void *arg, int single_entry);
 
-static void pb_msg_int32x(void *obj, void *arg)
+static void pb_msg_int32x(void *obj, void *arg, int single_entry)
 {
 	pr_msg("0x%08x", *(int *)obj);
 }
 
-static void pb_msg_int64x(void *obj, void *arg)
+static void pb_msg_int64x(void *obj, void *arg, int single_entry)
 {
 	pr_msg("0x%016lx", *(long *)obj);
 }
 
-static void pb_msg_string(void *obj, void *arg)
+static void pb_msg_string(void *obj, void *arg, int single_entry)
 {
 	pr_msg("\"%s\"",	*(char **)obj);
 }
 
-static void pb_msg_unk(void *obj, void *arg)
+static void pb_msg_unk(void *obj, void *arg, int single_entry)
 {
 	pr_msg("unknown object %p\n", obj);
 }
 
-static void pb_show_msg(const void *msg, const ProtobufCMessageDescriptor *md);
+static void pb_show_msg(const void *msg, const ProtobufCMessageDescriptor *md,
+			int single_entry);
 
-static void show_nested_message(void *msg, void *md)
+static void show_nested_message(void *msg, void *md, int single_entry)
 {
 	pr_msg("[ ");
-	pb_show_msg(msg, md);
+	pb_show_msg(msg, md, single_entry);
 	pr_msg(" ] ");
 }
 
-static void show_enum(void *msg, void *md)
+static void show_enum(void *msg, void *md, int single_entry)
 {
 	ProtobufCEnumDescriptor *d = md;
 	const char *val_name = NULL;
@@ -71,7 +72,7 @@ static void show_enum(void *msg, void *md)
 }
 
 static void pb_show_field(const ProtobufCFieldDescriptor *fd, void *where,
-			  unsigned long nr_fields)
+			  unsigned long nr_fields, int single_entry)
 {
 	pb_pr_field_t *show;
 	unsigned long counter;
@@ -121,18 +122,21 @@ static void pb_show_field(const ProtobufCFieldDescriptor *fd, void *where,
 			break;
 	}
 
-	show(where, arg);
+	show(where, arg, single_entry);
 	where += fsize;
 
 	for (counter = 0; counter < nr_fields - 1; counter++, where += fsize) {
 		pr_msg(":");
-		show(where, arg);
+		show(where, arg, single_entry);
 	}
-
-	pr_msg(" ");
+	if (single_entry)
+		pr_msg("\n");
+	else
+		pr_msg(" ");
 }
 
-static void pb_show_msg(const void *msg, const ProtobufCMessageDescriptor *md)
+static void pb_show_msg(const void *msg, const ProtobufCMessageDescriptor *md,
+			int single_entry)
 {
 	int i;
 
@@ -154,12 +158,12 @@ static void pb_show_msg(const void *msg, const ProtobufCMessageDescriptor *md)
 			data = (unsigned long *)*data;
 		}
 
-		pb_show_field(&fd, data, nr_fields);
+		pb_show_field(&fd, data, nr_fields, single_entry);
 	}
 }
 
 void do_pb_show_plain(int fd, const ProtobufCMessageDescriptor *md,
-		pb_unpack_t unpack, pb_free_t free)
+		pb_unpack_t unpack, pb_free_t free, int single_entry)
 {
 	while (1) {
 		void *obj;
@@ -167,9 +171,11 @@ void do_pb_show_plain(int fd, const ProtobufCMessageDescriptor *md,
 		if (pb_read_object_with_header(fd, &obj, unpack, true) <= 0)
 			break;
 
-		pb_show_msg(obj, md);
-		pr_msg("\n");
+		pb_show_msg(obj, md, single_entry);
 		free(obj, NULL);
+		if (single_entry)
+			break;
+		pr_msg("\n");
 	}
 }
 


More information about the CRIU mailing list