[Libguestfs] [libnbd PATCH 16/23] generator/C: print_trace_enter: wrap "enter" debug() / debug_direct() call

Laszlo Ersek lersek at redhat.com
Tue Apr 25 07:11:00 UTC 2023


At this point we have ensured that "pr_wrap_cstr" can wrap the "enter"
format string, and that "pr_wrap" can wrap the arg/optarg value list.

Extract the generation of the format string to "print_format_string"
(precisely between the outer quotes), and pass it to "pr_wrap_cstr".

If there is at least one argument (arg or optarg alike), don't print a
comma-space; after the comma, return the carriage to a new line, and
properly indent it, so that it match the name of the debug() vs.
debug_direct() function that's being called. This ensures the next
"pr_wrap" has maximum width to operate with. This is why we wanted the
comma-spaces to be suffixes, rather than prefixes -- we wanted to insert
this line break between the first comma after the format string, and the
first arg (or optarg, whichever comes first).

Extract the generation of the value (arg/optarg) lists to "print_args"
(which now only produces inner commas!, see again the prefix->suffix
change), and pass "print_args" to "pr_wrap".

The longest debug("enter") invocation changes as follows [lib/api.c]:

> @@ -5469,7 +5548,10 @@ nbd_aio_pwrite (struct nbd_handle *h, co
>      char *buf_printable =
>          nbd_internal_printable_buffer (buf, count);
>      debug (h,
> -           "enter: buf=\"%s\" count=%zu offset=%"PRIu64" completion=%s flags=0x%x", buf_printable ? buf_printable : "", count, offset, CALLBACK_IS_NULL (completion_callback) ? "<fun>" : "NULL", flags);
> +           "enter: buf=\"%s\" count=%zu offset=%"PRIu64" completion=%s "
> +           "flags=0x%x",
> +           buf_printable ? buf_printable : "", count, offset,
> +           CALLBACK_IS_NULL (completion_callback) ? "<fun>" : "NULL", flags);
>      free (buf_printable);
>    }
>

The second longest one [lib/api.c]:

> @@ -5394,7 +5470,10 @@ nbd_aio_pread_structured (struct nbd_han
>    pthread_mutex_lock (&h->lock);
>    if_debug (h) {
>      debug (h,
> -           "enter: buf=<buf> count=%zu offset=%"PRIu64" chunk=%s completion=%s flags=0x%x", count, offset, "<fun>", CALLBACK_IS_NULL (completion_callback) ? "<fun>" : "NULL", flags);
> +           "enter: buf=<buf> count=%zu offset=%"PRIu64" chunk=%s "
> +           "completion=%s flags=0x%x",
> +           count, offset, "<fun>",
> +           CALLBACK_IS_NULL (completion_callback) ? "<fun>" : "NULL", flags);
>    }
>
>    if (h->pread_initialize)

There is one debug_direct("enter") invocation change [lib/api.c]:

> @@ -332,7 +335,8 @@ nbd_set_private_data (struct nbd_handle
>    /* This function must not call set_error. */
>    if_debug (h) {
>      debug_direct (h, "nbd_set_private_data",
> -                  "enter: private_data=%"PRIuPTR"", private_data);
> +                  "enter: private_data=%"PRIuPTR"",
> +                  private_data);
>    }
>
>    ret = nbd_unlocked_set_private_data (h, private_data);

This patch is easiest to view with "git show -b".

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2172516
Signed-off-by: Laszlo Ersek <lersek at redhat.com>
---
 generator/C.ml | 128 ++++++++++----------
 1 file changed, 67 insertions(+), 61 deletions(-)

diff --git a/generator/C.ml b/generator/C.ml
index 43601625bcb0..ce6ff69419a5 100644
--- a/generator/C.ml
+++ b/generator/C.ml
@@ -744,72 +744,78 @@ let
         spaces 18
       ) in
     pr "%s\"" indent;
-    pr "enter:";
-    List.iter (
-      function
-      | Bool n -> pr " %s=%%s" n
-      | BytesOut (n, count)
-      | BytesPersistOut (n, count) -> pr " %s=<buf> %s=%%zu" n count
-      | BytesIn (n, count)
-      | BytesPersistIn (n, count) ->
-         pr " %s=\\\"%%s\\\" %s=%%zu" n count
-      | Closure { cbname } -> pr " %s=%%s" cbname
-      | Enum (n, _) -> pr " %s=%%d" n
-      | Flags (n, _) -> pr " %s=0x%%x" n
-      | Fd n | Int n -> pr " %s=%%d" n
-      | Int64 n -> pr " %s=%%\"PRIi64\"" n
-      | SizeT n -> pr " %s=%%zu" n
-      | SockAddrAndLen (n, len) -> pr " %s=<sockaddr> %s=%%d" n len
-      | Path n
-      | String n -> pr " %s=%%s" n
-      | StringList n -> pr " %s=%%s" n
-      | UInt n -> pr " %s=%%u" n
-      | UInt32 n -> pr " %s=%%\"PRIu32\"" n
-      | UInt64 n -> pr " %s=%%\"PRIu64\"" n
-      | UIntPtr n -> pr " %s=%%\"PRIuPTR\"" n
-    ) args;
-    List.iter (
-      function
-      | OClosure { cbname } -> pr " %s=%%s" cbname
-      | OFlags (n, _, _) -> pr " %s=0x%%x" n
-    ) optargs;
+    let print_format_string () =
+      pr "enter:";
+      List.iter (
+        function
+        | Bool n -> pr " %s=%%s" n
+        | BytesOut (n, count)
+        | BytesPersistOut (n, count) -> pr " %s=<buf> %s=%%zu" n count
+        | BytesIn (n, count)
+        | BytesPersistIn (n, count) ->
+           pr " %s=\\\"%%s\\\" %s=%%zu" n count
+        | Closure { cbname } -> pr " %s=%%s" cbname
+        | Enum (n, _) -> pr " %s=%%d" n
+        | Flags (n, _) -> pr " %s=0x%%x" n
+        | Fd n | Int n -> pr " %s=%%d" n
+        | Int64 n -> pr " %s=%%\"PRIi64\"" n
+        | SizeT n -> pr " %s=%%zu" n
+        | SockAddrAndLen (n, len) -> pr " %s=<sockaddr> %s=%%d" n len
+        | Path n
+        | String n -> pr " %s=%%s" n
+        | StringList n -> pr " %s=%%s" n
+        | UInt n -> pr " %s=%%u" n
+        | UInt32 n -> pr " %s=%%\"PRIu32\"" n
+        | UInt64 n -> pr " %s=%%\"PRIu64\"" n
+        | UIntPtr n -> pr " %s=%%\"PRIuPTR\"" n
+      ) args;
+      List.iter (
+        function
+        | OClosure { cbname } -> pr " %s=%%s" cbname
+        | OFlags (n, _, _) -> pr " %s=0x%%x" n
+      ) optargs
+    in
+    pr_wrap_cstr print_format_string;
     pr "\"";
     let num_args = List.length args
     and num_optargs = List.length optargs in
     let num_allargs = num_args + num_optargs in
     if num_allargs > 0 then
-      pr ", ";
-    List.iteri (
-      fun i arg ->
-        (match arg with
-         | Bool n -> pr "%s ? \"true\" : \"false\"" n
-         | BytesOut (n, count)
-         | BytesPersistOut (n, count) -> pr "%s" count
-         | BytesIn (n, count)
-         | BytesPersistIn (n, count) ->
-            pr "%s_printable ? %s_printable : \"\", %s" n n count
-         | Closure _ -> pr "\"<fun>\""
-         | Enum (n, _) -> pr "%s" n
-         | Flags (n, _) -> pr "%s" n
-         | Fd n | Int n | Int64 n | SizeT n -> pr "%s" n
-         | SockAddrAndLen (_, len) -> pr "(int) %s" len
-         | Path n | String n | StringList n ->
-            pr "%s_printable ? %s_printable : \"\"" n n
-         | UInt n | UInt32 n | UInt64 n | UIntPtr n -> pr "%s" n
-        );
-        if i < num_allargs - 1 then
-          pr ", "
-    ) args;
-    List.iteri (
-      fun i optarg ->
-        (match optarg with
-         | OClosure { cbname } ->
-            pr "CALLBACK_IS_NULL (%s_callback) ? \"<fun>\" : \"NULL\"" cbname
-         | OFlags (n, _, _) -> pr "%s" n
-        );
-        if num_args + i < num_allargs - 1 then
-          pr ", "
-    ) optargs;
+      pr ",\n%s" indent;
+    let print_args () =
+      List.iteri (
+        fun i arg ->
+          (match arg with
+           | Bool n -> pr "%s ? \"true\" : \"false\"" n
+           | BytesOut (n, count)
+           | BytesPersistOut (n, count) -> pr "%s" count
+           | BytesIn (n, count)
+           | BytesPersistIn (n, count) ->
+              pr "%s_printable ? %s_printable : \"\", %s" n n count
+           | Closure _ -> pr "\"<fun>\""
+           | Enum (n, _) -> pr "%s" n
+           | Flags (n, _) -> pr "%s" n
+           | Fd n | Int n | Int64 n | SizeT n -> pr "%s" n
+           | SockAddrAndLen (_, len) -> pr "(int) %s" len
+           | Path n | String n | StringList n ->
+              pr "%s_printable ? %s_printable : \"\"" n n
+           | UInt n | UInt32 n | UInt64 n | UIntPtr n -> pr "%s" n
+          );
+          if i < num_allargs - 1 then
+            pr ", "
+      ) args;
+      List.iteri (
+        fun i optarg ->
+          (match optarg with
+           | OClosure { cbname } ->
+              pr "CALLBACK_IS_NULL (%s_callback) ? \"<fun>\" : \"NULL\"" cbname
+           | OFlags (n, _, _) -> pr "%s" n
+          );
+          if num_args + i < num_allargs - 1 then
+            pr ", "
+      ) optargs
+    in
+    pr_wrap ',' print_args;
     pr ");\n";
     List.iter (
       function



More information about the Libguestfs mailing list