Skip to contentSkip to conversation

libtraceevent: Pretty print function parameters of enum-type

PATCH RESEND Superseded Donglin Peng 2 participants 6 messages Feb 6 – Feb 8

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 libtraceevent: Pretty print function parameters of enum-type

From: Donglin Peng <[email protected]>

The tep_btf_print_args() function currently prints values of enum parameters in decimal format, which reduces readability.

Utilize the BTF information to resolve enum type parameters and print their symbolic names where possible. This enhances the trace log readability by displaying meaningful identifiers instead of raw numbers.

Before:
mod_memcg_lruvec_state(lruvec=0xffff88800c18a540, idx=5, val=320)

After:
mod_memcg_lruvec_state(lruvec=0xffff88800c18a540, idx=5 [NR_SLAB_RECLAIMABLE_B], val=320)

Cc Steven Rostedt <[email protected]>
Cc Masami Hiramatsu <[email protected]>
---
 src/trace-btf.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
src/trace-btf.c +19 -1
index 859b0860ce2a..85f5fefb4d3b 100644
@@ -32,6 +32,16 @@ struct tep_btf {
 #define REALLOC_SIZE (1 << 10)
 #define REALLOC_MASK (REALLOC_SIZE - 1)
 
+#define for_each_enum(i, enum_type, member)			\
+	for (i = 0, member = btf_enum(enum_type);		\
+	     i < BTF_INFO_VLEN(enum_type->info);		\
+	     i++, member++)
+
+static inline struct btf_enum *btf_enum(const struct btf_type *t)
+{
+	return (struct btf_enum *)(t + 1);
+}
+
 static const char *btf_name(struct tep_btf *btf, int off)
 {
 	if (off < btf->hdr->str_len)
@@ -546,10 +556,11 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
 	struct tep_btf *btf = tep->btf;
 	struct btf_type *type = tep_btf_find_func(btf, func);
 	struct btf_param *param;
+	struct btf_enum *enump;
 	unsigned long long arg;
 	unsigned int encode;
 	const char *param_name;
-	int a, p, x, nr;
+	int a, p, x, nr, i;
 
 	if (!func)
 		return -1;
@@ -628,6 +639,13 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
 			break;
 		case BTF_KIND_ENUM:
 			trace_seq_printf(s, "%lld", arg);
+			for_each_enum(i, t, enump) {
+				if (arg == enump->val) {
+					trace_seq_printf(s, " [%s]",
+						btf_name(btf, enump->name_off));
+					break;
+				}
+			}
 			break;
 		default:
 			/* This does not handle complex arguments */
Signature · 1 line
2.34.1
#1 lore raw
Quoted 1 line · On Fri, 6 Feb 2026 17:15:12 +0800 Donglin Peng <[email protected]> wrote:

From: Donglin Peng <[email protected]>

Hi Donglin,

Quoted 27 lines

The tep_btf_print_args() function currently prints values of enum parameters in decimal format, which reduces readability.

Utilize the BTF information to resolve enum type parameters and print their symbolic names where possible. This enhances the trace log readability by displaying meaningful identifiers instead of raw numbers.

Before:
mod_memcg_lruvec_state(lruvec=0xffff88800c18a540, idx=5, val=320)

After:
mod_memcg_lruvec_state(lruvec=0xffff88800c18a540, idx=5 [NR_SLAB_RECLAIMABLE_B], val=320)

Cc Steven Rostedt <[email protected]>
Cc Masami Hiramatsu <[email protected]>
---
 src/trace-btf.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
src/trace-btf.c +0 -0
index 859b0860ce2a..85f5fefb4d3b 100644

src/trace-btf.c struct tep_btf { · line 38

Quoted 7 lines
src/trace-btf.c +4 -0
@@ -32,6 +32,16 @@ struct tep_btf {
 #define REALLOC_SIZE (1 << 10)
 #define REALLOC_MASK (REALLOC_SIZE - 1)
 
+#define for_each_enum(i, enum_type, member)			\
+	for (i = 0, member = btf_enum(enum_type);		\
+	     i < BTF_INFO_VLEN(enum_type->info);		\
+	     i++, member++)

You can make the above into:

 #define for_each_enum(enum_type, member)			\
	for (int __i = 0, member = btf_enum(enum_type);		\
	     __i < BTF_INFO_VLEN((enum_type)->info);		\
	     __i++, member++)

Then you don't need to pass in the counter "i".

(also it's good to put parenthesis around "enum_type").

Quoted 10 lines
src/trace-btf.c +6 -0
+
+static inline struct btf_enum *btf_enum(const struct btf_type *t)
+{
+	return (struct btf_enum *)(t + 1);
+}
+
 static const char *btf_name(struct tep_btf *btf, int off)
 {
 	if (off < btf->hdr->str_len)

src/trace-btf.c int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args, · line 563

Quoted 9 lines
src/trace-btf.c +2 -1
@@ -546,10 +556,11 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
 	struct tep_btf *btf = tep->btf;
 	struct btf_type *type = tep_btf_find_func(btf, func);
 	struct btf_param *param;
+	struct btf_enum *enump;
 	unsigned long long arg;
 	unsigned int encode;
 	const char *param_name;
-	int a, p, x, nr;
+	int a, p, x, nr, i;

And the above doesn't need to be changed.

Thanks,

-- Steve

Quoted 17 lines
src/trace-btf.c +7 -0
 	if (!func)
 		return -1;
@@ -628,6 +639,13 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
 			break;
 		case BTF_KIND_ENUM:
 			trace_seq_printf(s, "%lld", arg);
+			for_each_enum(i, t, enump) {
+				if (arg == enump->val) {
+					trace_seq_printf(s, " [%s]",
+						btf_name(btf, enump->name_off));
+					break;
+				}
+			}
 			break;
 		default:
 			/* This does not handle complex arguments */
#2 lore raw
Quoted 46 lines · Steven Rostedt
Quoted 1 line · On Fri, 6 Feb 2026 17:15:12 +0800 Donglin Peng <[email protected]> wrote:

From: Donglin Peng <[email protected]>

Hi Donglin,

Quoted 27 lines

The tep_btf_print_args() function currently prints values of enum parameters in decimal format, which reduces readability.

Utilize the BTF information to resolve enum type parameters and print their symbolic names where possible. This enhances the trace log readability by displaying meaningful identifiers instead of raw numbers.

Before:
mod_memcg_lruvec_state(lruvec=0xffff88800c18a540, idx=5, val=320)

After:
mod_memcg_lruvec_state(lruvec=0xffff88800c18a540, idx=5 [NR_SLAB_RECLAIMABLE_B], val=320)

Cc Steven Rostedt <[email protected]>
Cc Masami Hiramatsu <[email protected]>
---
 src/trace-btf.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
src/trace-btf.c +0 -0
index 859b0860ce2a..85f5fefb4d3b 100644

src/trace-btf.c struct tep_btf { · line 38

Quoted 7 lines
src/trace-btf.c +4 -0
@@ -32,6 +32,16 @@ struct tep_btf {
 #define REALLOC_SIZE (1 << 10)
 #define REALLOC_MASK (REALLOC_SIZE - 1)
 
+#define for_each_enum(i, enum_type, member)                  \
+     for (i = 0, member = btf_enum(enum_type);               \
+          i < BTF_INFO_VLEN(enum_type->info);                \
+          i++, member++)

You can make the above into:

Quoted 6 lines
 #define for_each_enum(enum_type, member)                       \
        for (int __i = 0, member = btf_enum(enum_type);         \
             __i < BTF_INFO_VLEN((enum_type)->info);            \
             __i++, member++)

Then you don't need to pass in the counter "i".

Good, I will fix it in the next version.

Quoted 2 lines

(also it's good to put parenthesis around "enum_type").

Thanks.

Quoted 14 lines
Quoted 14 lines
src/trace-btf.c +6 -0
+
+static inline struct btf_enum *btf_enum(const struct btf_type *t)
+{
+     return (struct btf_enum *)(t + 1);
+}
+
 static const char *btf_name(struct tep_btf *btf, int off)
 {
      if (off < btf->hdr->str_len)
@@ -546,10 +556,11 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
      struct tep_btf *btf = tep->btf;
      struct btf_type *type = tep_btf_find_func(btf, func);
      struct btf_param *param;
Quoted 8 lines

src/trace-btf.c int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args, · line 563

Quoted 6 lines
src/trace-btf.c +2 -1
+     struct btf_enum *enump;
      unsigned long long arg;
      unsigned int encode;
      const char *param_name;
-     int a, p, x, nr;
+     int a, p, x, nr, i;

And the above doesn't need to be changed.

Thanks, I understood.

Quoted 23 lines

Thanks,

-- Steve

Quoted 17 lines
src/trace-btf.c +7 -0
      if (!func)
              return -1;
@@ -628,6 +639,13 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
                      break;
              case BTF_KIND_ENUM:
                      trace_seq_printf(s, "%lld", arg);
+                     for_each_enum(i, t, enump) {
+                             if (arg == enump->val) {
+                                     trace_seq_printf(s, " [%s]",
+                                             btf_name(btf, enump->name_off));
+                                     break;
+                             }
+                     }
                      break;
              default:
                      /* This does not handle complex arguments */
#3 lore raw
In reply to Steven Rostedt
Quoted 46 lines · Steven Rostedt
Quoted 1 line · On Fri, 6 Feb 2026 17:15:12 +0800 Donglin Peng <[email protected]> wrote:

From: Donglin Peng <[email protected]>

Hi Donglin,

Quoted 27 lines

The tep_btf_print_args() function currently prints values of enum parameters in decimal format, which reduces readability.

Utilize the BTF information to resolve enum type parameters and print their symbolic names where possible. This enhances the trace log readability by displaying meaningful identifiers instead of raw numbers.

Before:
mod_memcg_lruvec_state(lruvec=0xffff88800c18a540, idx=5, val=320)

After:
mod_memcg_lruvec_state(lruvec=0xffff88800c18a540, idx=5 [NR_SLAB_RECLAIMABLE_B], val=320)

Cc Steven Rostedt <[email protected]>
Cc Masami Hiramatsu <[email protected]>
---
 src/trace-btf.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
src/trace-btf.c +0 -0
index 859b0860ce2a..85f5fefb4d3b 100644

src/trace-btf.c struct tep_btf { · line 38

Quoted 7 lines
src/trace-btf.c +4 -0
@@ -32,6 +32,16 @@ struct tep_btf {
 #define REALLOC_SIZE (1 << 10)
 #define REALLOC_MASK (REALLOC_SIZE - 1)
 
+#define for_each_enum(i, enum_type, member)                  \
+     for (i = 0, member = btf_enum(enum_type);               \
+          i < BTF_INFO_VLEN(enum_type->info);                \
+          i++, member++)

You can make the above into:

Quoted 6 lines
 #define for_each_enum(enum_type, member)                       \
        for (int __i = 0, member = btf_enum(enum_type);         \
             __i < BTF_INFO_VLEN((enum_type)->info);            \
             __i++, member++)

Then you don't need to pass in the counter "i".

Sorry, I found that the previous change causes a build failure.

The error occurs in trace-btf.c:36:
warning: initialization of 'int' from 'struct btf_enum *' makes integer from pointer without a cast [-Wint-conversion]
36 | for (int __i = 0, member = btf_enum(enum_type); \

This is due to the member variable being redefined as an int type, conflicting with its assignment of a struct btf_enum *pointer from btf_enum(enum_type).

Quoted 47 lines

(also it's good to put parenthesis around "enum_type").

Quoted 10 lines
src/trace-btf.c +6 -0
+
+static inline struct btf_enum *btf_enum(const struct btf_type *t)
+{
+     return (struct btf_enum *)(t + 1);
+}
+
 static const char *btf_name(struct tep_btf *btf, int off)
 {
      if (off < btf->hdr->str_len)

src/trace-btf.c int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args, · line 563

Quoted 9 lines
src/trace-btf.c +2 -1
@@ -546,10 +556,11 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
      struct tep_btf *btf = tep->btf;
      struct btf_type *type = tep_btf_find_func(btf, func);
      struct btf_param *param;
+     struct btf_enum *enump;
      unsigned long long arg;
      unsigned int encode;
      const char *param_name;
-     int a, p, x, nr;
+     int a, p, x, nr, i;

And the above doesn't need to be changed.

Thanks,

-- Steve

Quoted 17 lines
src/trace-btf.c +7 -0
      if (!func)
              return -1;
@@ -628,6 +639,13 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
                      break;
              case BTF_KIND_ENUM:
                      trace_seq_printf(s, "%lld", arg);
+                     for_each_enum(i, t, enump) {
+                             if (arg == enump->val) {
+                                     trace_seq_printf(s, " [%s]",
+                                             btf_name(btf, enump->name_off));
+                                     break;
+                             }
+                     }
                      break;
              default:
                      /* This does not handle complex arguments */
#4 lore raw
Quoted 17 lines · On Sat, 7 Feb 2026 13:04:02 +0800 Donglin Peng <[email protected]> wrote:
Quoted 6 lines

src/trace-btf.c struct tep_btf { · line 38

Quoted 4 lines
src/trace-btf.c +4 -0
+#define for_each_enum(i, enum_type, member)                  \
+     for (i = 0, member = btf_enum(enum_type);               \
+          i < BTF_INFO_VLEN(enum_type->info);                \
+          i++, member++)  
Quoted 8 lines

You can make the above into:

 #define for_each_enum(enum_type, member)                       \
        for (int __i = 0, member = btf_enum(enum_type);         \
             __i < BTF_INFO_VLEN((enum_type)->info);            \
             __i++, member++)

Then you don't need to pass in the counter "i".

Sorry, I found that the previous change causes a build failure.

Quoted 8 lines

The error occurs in trace-btf.c:36:
warning: initialization of 'int' from 'struct btf_enum *' makes integer from pointer without a cast [-Wint-conversion]
36 | for (int __i = 0, member = btf_enum(enum_type); \

This is due to the member variable being redefined as an int type, conflicting with its assignment of a struct btf_enum *pointer from btf_enum(enum_type).

Ah, I had that issue before. But this should work:

 #define for_each_enum(enum_type, member)				\
	member = btf_enum(enum_type);					\
	for (int __i = 0; __i < BTF_INFO_VLEN((enum_type)->info);	\
	     __i++, member++)

Just can't use it as a single line for if statements.

	if (x)
		for_each_enum() {
		}

But we shouldn't be doing that anyway.

-- Steve

#5 lore raw
Quoted 37 lines · Steven Rostedt
Quoted 17 lines · On Sat, 7 Feb 2026 13:04:02 +0800 Donglin Peng <[email protected]> wrote:
Quoted 6 lines

src/trace-btf.c struct tep_btf { · line 38

Quoted 4 lines
src/trace-btf.c +4 -0
+#define for_each_enum(i, enum_type, member)                  \
+     for (i = 0, member = btf_enum(enum_type);               \
+          i < BTF_INFO_VLEN(enum_type->info);                \
+          i++, member++)
Quoted 8 lines

You can make the above into:

 #define for_each_enum(enum_type, member)                       \
        for (int __i = 0, member = btf_enum(enum_type);         \
             __i < BTF_INFO_VLEN((enum_type)->info);            \
             __i++, member++)

Then you don't need to pass in the counter "i".

Sorry, I found that the previous change causes a build failure.

Quoted 8 lines

The error occurs in trace-btf.c:36:
warning: initialization of 'int' from 'struct btf_enum *' makes integer from pointer without a cast [-Wint-conversion]
36 | for (int __i = 0, member = btf_enum(enum_type); \

This is due to the member variable being redefined as an int type, conflicting with its assignment of a struct btf_enum *pointer from btf_enum(enum_type).

Ah, I had that issue before. But this should work:

 #define for_each_enum(enum_type, member)                               \
        member = btf_enum(enum_type);                                   \
        for (int __i = 0; __i < BTF_INFO_VLEN((enum_type)->info);       \
             __i++, member++)
Quoted 7 lines

Just can't use it as a single line for if statements.

        if (x)
                for_each_enum() {
                }

But we shouldn't be doing that anyway.

Thanks. I will do it in v2.

Quoted 2 lines

-- Steve