Skip to contentSkip to conversation

libtraceevent: Add way to print function prototype

PATCH New Steven Rostedt 1 participant 3 messages Feb 3 Documentation

lore
Share & tools
lore
#0 lore raw

v2 cover libtraceevent: Add way to print function prototype

From: "Steven Rostedt (Google)" <[email protected]>

In order to have trace-printk add showing of a function's prototype, add a new function that can use BTF to show the functions arguments:

tep_btf_list_args()

Changes since v1: https://lore.kernel.org/all/[email protected]/

- Fixed whitespace issue

Steven Rostedt (Google) (2):
libtraceevent: Split out btf func init code from tep_btf_print_args()
libtraceevent: Add tep_btf_list_args()

 Documentation/libtraceevent-btf-proto.txt | 124 ++++++++++++
 Documentation/libtraceevent.txt           |   1 +
 include/traceevent/event-parse.h          |   1 +
 src/trace-btf.c                           | 221 +++++++++++++++++++---
 4 files changed, 320 insertions(+), 27 deletions(-)
 create mode 100644 Documentation/libtraceevent-btf-proto.txt
Signature · 1 line
2.51.0
#1 lore raw

PATCH v2 1/2 libtraceevent: Split out btf func init code from tep_btf_print_args()

From: "Steven Rostedt (Google)" <[email protected]>

In order to use BTF for various other attributes of functions, move out the btf function initialization from the tp_btf_print_args() and into its own function.

---
 src/trace-btf.c | 70 +++++++++++++++++++++++++++++++------------------
 1 file changed, 45 insertions(+), 25 deletions(-)
src/trace-btf.c +45 -25
index d0231b49d144..0b733bf9cb75 100644
@@ -330,35 +330,13 @@ static void assign_arg(unsigned long long *arg, void *args, int size, int a)
 		*(unsigned long long *)(args + a * sizeof(long long));
 }
 
-/**
- * tep_btf_print_args - Print function arguments from BTF info
- * @tep: The tep descriptor to use
- * @s: The trace_seq to write the arguments into
- * @args: The array that holds the arguments
- * @nmem: The number of arguments
- * @size: The size of each item in args (4 or 8).
- * @func: The name of the function.
- *
- * Loads up the @s with a list of arguments for the function based on
- * the @args.
- *
- * If there's no BTF loaded or @func is not found, then it just writes
- * the @args as raw numbers. Otherwise it will pretty print the
- * arguments based on the function info in BTF.
- *
- * Returns: 0 on success and -1 on failure.
- */
-int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
-		       int nmem, int size, const char *func)
+static int init_btf_func(struct tep_btf *btf, struct trace_seq *s,
+			 void *args, int nmem, int size,
+			 const char *func, struct btf_type **p_type)
 {
-	struct tep_btf *btf = tep->btf;
 	struct btf_type *type = tep_btf_find_func(btf, func);
-	struct btf_param *param;
 	unsigned long long arg;
-	unsigned int encode;
-	const char *param_name;
 	const char *fp;
-	int a, p, x, nr;
 
 	if (size != 4 && size != 8)
 		return -1;
@@ -381,6 +359,7 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
 			if (i + 1 < nmem)
 				trace_seq_puts(s, ", ");
 		}
+		*p_type = NULL;
 		return 0;
 	}
 
@@ -391,6 +370,47 @@ int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
 		return -1;
 	}
 
+	*p_type = type;
+
+	return 0;
+}
+
+/**
+ * tep_btf_print_args - Print function arguments from BTF info
+ * @tep: The tep descriptor to use
+ * @s: The trace_seq to write the arguments into
+ * @args: The array that holds the arguments
+ * @nmem: The number of arguments
+ * @size: The size of each item in args (4 or 8).
+ * @func: The name of the function.
+ *
+ * Loads up the @s with a list of arguments for the function based on
+ * the @args.
+ *
+ * If there's no BTF loaded or @func is not found, then it just writes
+ * the @args as raw numbers. Otherwise it will pretty print the
+ * arguments based on the function info in BTF.
+ *
+ * Returns: 0 on success and -1 on failure.
+ */
+int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
+		       int nmem, int size, const char *func)
+{
+	struct tep_btf *btf = tep->btf;
+	struct btf_type *type = tep_btf_find_func(btf, func);
+	struct btf_param *param;
+	unsigned long long arg;
+	unsigned int encode;
+	const char *param_name;
+	int a, p, x, nr;
+
+	if (init_btf_func(btf, s, args, nmem, size, func, &type) < 0)
+		return -1;
+
+	/* Type is NULL if function wasn't found */
+	if (!type)
+		return 0;
+
 	/* Get the function proto */
 	type = btf_get_type(btf, type->type);
 
Signature · 1 line
2.51.0
#2 lore raw

PATCH v2 2/2 libtraceevent: Add tep_btf_list_args()

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

Add tep_btf_list_args() to display a function's prototype:

struct trace_seq s;

trace_seq_init(&s);
tep_btf_list_args(tep, &s, "proc_scheduler");
trace_seq_do_printf(&s);

produces:

const struct ctl_table *ctl, int write, void *buffer, size_t *lenp, loff_t *ppos

Where the prototype of proc_scheduler() is:

static int proc_scheduler(const struct ctl_table *ctl, int write,
void *buffer, size_t *lenp, loff_t *ppos)

---
Changes since v1: https://lore.kernel.org/[email protected]

- Fixed whitespace issue.

 Documentation/libtraceevent-btf-proto.txt | 124 ++++++++++++++++++
 Documentation/libtraceevent.txt           |   1 +
 include/traceevent/event-parse.h          |   1 +
 src/trace-btf.c                           | 151 +++++++++++++++++++++-
 4 files changed, 275 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/libtraceevent-btf-proto.txt
Documentation/libtraceevent-btf-proto.txt new file +124 -0
new file mode 100644
index 000000000000..9a4ce15af1c9
@@ -0,0 +1,124 @@
+libtraceevent(3)
+================
+
+NAME
+----
+tep_btf_list_args - Show the arguments of a function from BTF
+
+SYNOPSIS
+--------
+[verse]
+--
+*#include <event-parse.h>*
+
+int *tep_btf_list_args*(struct tep_handle pass:[*]_tep_, struct trace_seq pass:[*]_s_, const char pass:[*]_func_);
+--
+
+DESCRIPTION
+-----------
+If the Linux kernel has BTF configured, then a binary file will exist
+in the path of */sys/kernel/btf/vmlinux*. If this file is read into memory
+and passed to *tep_load_btf()* function, then it can be used to read the arguments
+of a given function, if that function data is found within the BTF file.
+
+The *tep_btf_print_args()* takes a _tep_ handle, a trace_seq _s_ pointer
+(that was initialized by *trace_seq_init(3)*), and a _func_ string that is
+the name of the function to find the BTF information to use to print the function's prototype.
+If BTF is not loaded or the _func_ name is not found it will return a negative.
+
+RETURN VALUE
+------------
+*tep_btf_list_args()* returns the number of arguments read on success and -1 on failure
+(for example, if the function is not found).
+
+EXAMPLE
+-------
+[source,c]
+--
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <event-parse.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define BTF_FILE "/sys/kernel/btf/vmlinux"
+
+int main(int argc, char **argv)
+{
+	struct tep_handle *tep;
+	struct trace_seq s;
+	struct stat st;
+	char *buf;
+	int fd, r, z;
+
+	if (argc < 2)
+		exit(-1);
+
+	if (stat(BTF_FILE, &st) < 0) {
+		perror(BTF_FILE);
+		exit(-1);
+	}
+
+	buf = malloc(st.st_size);
+	if (!buf)
+		exit(-1);
+	fd = open(BTF_FILE, O_RDONLY);
+	if (fd < 0) {
+		perror(BTF_FILE);
+		exit(-1);
+	}
+	for (z = 0; z < st.st_size; ) {
+		r = read(fd, buf + z, st.st_size - z);
+		if (r <= 0)
+			break;
+		z += r;
+	}
+	close(fd);
+	tep = tep_alloc();
+	if (!tep)
+		exit(-1);
+
+	tep_load_btf(tep, buf, z);
+	free(buf);
+
+	trace_seq_init(&s);
+	tep_btf_list_args(tep, &s, argv[1]);
+	printf("%s(", argv[1]);
+	trace_seq_do_printf(&s);
+	printf(")\n");
+	exit(0);
+}
+
+--
+FILES
+-----
+[verse]
+--
+*event-parse.h*
+	Header file to include in order to have access to the library APIs.
+*-ltraceevent*
+	Linker switch to add when building a program that uses the library.
+--
+
+SEE ALSO
+--------
+*tep_btf_load*(3), *tep_btf_print_args*(3), *libtraceevent*(3), *trace-cmd*(1)
+
+AUTHOR
+------
+[verse]
+--
+*Steven Rostedt* <[email protected]>, author of *libtraceevent*.
+--
+REPORTING BUGS
+--------------
+Report bugs to  <[email protected]>
+
+LICENSE
+-------
+libtraceevent is Free Software licensed under the GNU LGPL 2.1
+
+RESOURCES
+---------
+https://git.kernel.org/pub/scm/libs/libtrace/libtraceevent.git/
Documentation/libtraceevent.txt +1 -0
index 5fb5fc19ffc1..6e5fdbc6e886 100644
@@ -170,6 +170,7 @@ BTF parsing:
 	int *tep_load_btf*(struct tep_handle pass:[*]_tep_, void pass:[*]_raw_data_, size_t _data_size_);
 	int *tep_btf_print_args*(struct tep_handle pass:[*]_tep_, struct trace_seq pass:[*]_s_, void pass:[*]_args_,
 		       int nmem, int size, const char pass:[*]_func_);
+	int *tep_btf_list_args*(struct tep_handle pass:[*]_tep_, struct trace_seq pass:[*]_s_, const char pass:[*]_func_);
 
 Trace sequences:
 *#include <trace-seq.h>*
include/traceevent/event-parse.h +1 -0
index 9c1abfae28d9..0070e21eb952 100644
@@ -589,6 +589,7 @@ struct kbuffer *tep_kbuffer(struct tep_handle *tep);
 
 /* BTF */
 int tep_load_btf(struct tep_handle *tep, void *raw_data, size_t data_size);
+int tep_btf_list_args(struct tep_handle *tep, struct trace_seq *s, const char *func);
 int tep_btf_print_args(struct tep_handle *tep, struct trace_seq *s, void *args,
 		       int nmem, int size, const char *func);
 
src/trace-btf.c +149 -2

175 lines not on this page · raw message

Signature · 1 line
2.51.0