Skip to contentSkip to conversation

Initial trace-cmd support for ftrace uprobes

PATCH In review Tzvetomir Stoyanov (VMware) 2 participants 5 messages Nov 25, 2020 – Dec 3, 2020 Kbuild

lore
Share & tools
lore
#0 lore raw

v3 cover Initial trace-cmd support for ftrace uprobes

Ftrace supports uprobes - trace events on user space applications. Trace-cmd cannot configure uprobes, in order to support it this new functionality is introduced:
- Function name to file offset mapping, this information is extracted from the dwarf and ELF headers in the binary file.
- New options are added to "trace-cmd record" for setting a uprobe on given file and the libraries it depends on:

    --uprobe file:function
    --uprobe-ret file:function
    --libs
    --no-libs

[
v3 changes:
- Added DEBUG_INTERNALS local define in trace-obj-debug.c, for printing debug information about resolving process.
- Replaced function name regex match with simple wildcards match.
- Implemented functionality to search for functions in the libraries that the given application depends on.
- Added --libs and --no-libs arguments to trace-cmd record.
v2 changes:
- Renamed some internal trace-cmd functions, to not overlap the bfd library namespace. Suggested by Steven Rostedt.
]

Tzvetomir Stoyanov (VMware) (3):
trace-cmd: Internal refactoring of pid address map logic
trace-cmd: New internal APIs for reading ELF header
trace-cmd: [POC] Add support for uprobes

 Makefile                       |  10 +
 tracecmd/Makefile              |   2 +
 tracecmd/include/trace-local.h |  56 +-
 tracecmd/trace-obj-debug.c     | 908 +++++++++++++++++++++++++++++++++
 tracecmd/trace-record.c        | 204 ++++----
 tracecmd/trace-uprobes.c       | 223 ++++++++
 tracecmd/trace-usage.c         |   8 +
 7 files changed, 1297 insertions(+), 114 deletions(-)
 create mode 100644 tracecmd/trace-obj-debug.c
 create mode 100644 tracecmd/trace-uprobes.c
Signature · 1 line
2.28.0
#1 lore raw

PATCH v3 1/3 trace-cmd: Internal refactoring of pid address map logic

Make functions for collecting file to memory map of a PID non static, so they can be reused in the trace-cmd application context. Created new file trace-obj-debug.c with these functions.

---
 tracecmd/Makefile              |   1 +
 tracecmd/include/trace-local.h |  19 +++--
 tracecmd/trace-obj-debug.c     | 138 +++++++++++++++++++++++++++++++++
 tracecmd/trace-record.c        | 107 +------------------------
 4 files changed, 152 insertions(+), 113 deletions(-)
 create mode 100644 tracecmd/trace-obj-debug.c
tracecmd/Makefile +1 -0
index 5e59adf8..f9435558 100644
@@ -31,6 +31,7 @@ TRACE_CMD_OBJS += trace-show.o
 TRACE_CMD_OBJS += trace-list.o
 TRACE_CMD_OBJS += trace-usage.o
 TRACE_CMD_OBJS += trace-dump.o
+TRACE_CMD_OBJS += trace-obj-debug.o
 ifeq ($(VSOCK_DEFINED), 1)
 TRACE_CMD_OBJS += trace-tsync.o
 endif
tracecmd/include/trace-local.h +11 -8
index 28d1b4e0..6b469e63 100644
@@ -178,14 +178,6 @@ struct func_list {
 	const char *mod;
 };
 
-struct pid_addr_maps {
-	struct pid_addr_maps		*next;
-	struct tracecmd_proc_addr_map	*lib_maps;
-	unsigned int			nr_lib_maps;
-	char				*proc_name;
-	int				pid;
-};
-
 struct opt_list {
 	struct opt_list *next;
 	const char	*option;
@@ -314,4 +306,15 @@ void *malloc_or_die(unsigned int size); /* Can be overridden */
 void __noreturn __die(const char *fmt, ...);
 void __noreturn _vdie(const char *fmt, va_list ap);
 
+/* --- Debug symbols--- */
+struct pid_addr_maps {
+	struct pid_addr_maps	*next;
+	struct tracecmd_proc_addr_map	*lib_maps;
+	unsigned int			nr_lib_maps;
+	char				*proc_name;
+	int				pid;
+};
+int trace_debug_get_filemap(struct pid_addr_maps **file_maps, int pid);
+void trace_debug_free_filemap(struct pid_addr_maps *maps);
+
 #endif /* __TRACE_LOCAL_H */
tracecmd/trace-obj-debug.c new file +138 -0

138 lines not on this page · raw message

tracecmd/trace-record.c +2 -105

125 lines not on this page · raw message

Signature · 1 line
2.28.0
#2 lore raw

PATCH v3 2/3 trace-cmd: New internal APIs for reading ELF header

In reply to Tzvetomir Stoyanov (VMware)

Implemented new trace-cmd internal APIs for parsing ELF header and resolving VMA to function name and function name to VMA and file offset. The bfd library is used to read and parse the binary file. The new APIs are part of trace-cmd application and are visible only inside its context.

---
 Makefile                       |  10 +
 tracecmd/include/trace-local.h |  19 +
 tracecmd/trace-obj-debug.c     | 770 +++++++++++++++++++++++++++++++++
 3 files changed, 799 insertions(+)
Makefile +10 -0
index 8626b91b..a59b586d 100644
@@ -246,6 +246,16 @@ endif
 CUNIT_INSTALLED := $(shell if (printf "$(pound)include <CUnit/Basic.h>\n void main(){CU_initialize_registry();}" | $(CC) -x c - -lcunit >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi)
 export CUNIT_INSTALLED
 
+BFD_INSTALLED := $(shell if (echo -e "$(pound)include <bfd.h>\n void main(){bfd_init();}" | $(CC) -xc  - -lbfd >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi)
+
+export BFD_INSTALLED
+ifeq ($(BFD_INSTALLED), 1)
+CFLAGS += -DBFD_INSTALLED
+LIBS += -lbfd
+else
+$(warning libbfd is not installed)
+endif
+
 export CFLAGS
 export INCLUDES
 
tracecmd/include/trace-local.h +19 -0
index 6b469e63..933360a5 100644
@@ -317,4 +317,23 @@ struct pid_addr_maps {
 int trace_debug_get_filemap(struct pid_addr_maps **file_maps, int pid);
 void trace_debug_free_filemap(struct pid_addr_maps *maps);
 
+struct tracecmd_debug_symbols {
+	char *name;			/* symbol's name */
+	char *fname;			/* symbol's file */
+	unsigned long long vma_start;	/* symbol's start VMA */
+	unsigned long long vma_near;	/* symbol's requested VMA */
+	unsigned long long foffset;	/* symbol's offset in the binary file*/
+};
+struct trace_debug_object;
+struct trace_debug_object *trace_debug_obj_create_file(char *file, bool libs);
+struct trace_debug_object *trace_debug_obj_create_pid(int pid);
+void trace_debug_obj_destroy(struct trace_debug_object *debug);
+
+int trace_debug_resolve_symbols(struct trace_debug_object *obj);
+int trace_debug_add_resolve_symbol(struct trace_debug_object *obj,
+				   unsigned long long vma, char *name);
+
+void trace_debug_walk_resolved_symbols(struct trace_debug_object *obj,
+				       int (*callback)(struct tracecmd_debug_symbols *, void *),
+				       void *context);
 #endif /* __TRACE_LOCAL_H */
tracecmd/trace-obj-debug.c +770 -0

780 lines not on this page · raw message

Signature · 1 line
2.28.0
#3 lore raw

Re: [PATCH v3 2/3] trace-cmd: New internal APIs for reading ELF header

Makefile endif · line 250

Quoted 7 lines · On Wed, 25 Nov 2020 12:44:21 +0200 "Tzvetomir Stoyanov (VMware)"…

+++ b/Makefile

Makefile +2 -0
@@ -246,6 +246,16 @@ endif
 CUNIT_INSTALLED := $(shell if (printf "$(pound)include <CUnit/Basic.h>\n void main(){CU_initialize_registry();}" | $(CC) -x c - -lcunit >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi)
 export CUNIT_INSTALLED
 
+BFD_INSTALLED := $(shell if (echo -e "$(pound)include <bfd.h>\n void main(){bfd_init();}" | $(CC) -xc  - -lbfd >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi)
+

Note, you need to include a "-o /dev/null" in the "| $(CC) -o /dev/null -xc - "

otherwise you end up with an "a.out" in the directory.

-- Steve

#4 lore raw

PATCH v3 3/3 trace-cmd: [POC] Add support for uprobes

In reply to Tzvetomir Stoyanov (VMware)

Initial implementaton of trace-cmd support for ftrace uprobes. New trace-cmd record / set arguments are introduced:
--uprobe file:function
--uprobe-ret file:function
The ftrace (return) probe is set on the given function from the file. Wildcards '*' and '?' are supported in the function name:
--uprobe file:*
will set uprobes on all functions from the given file.
Set uprobes on related libraries:
--libs
--no-libs
When set before --uprobe/--uprobe-ret argument, specifies whether to search or not for the given function in the libraries, that the given executable file depends on. By default, uprobes are set only for the matched functions in the given executable, no search in related libraries. The --libs/--no-libs argument affects all --uprobe/--uprobe-ret argumenst, specified after it.

---
 tracecmd/Makefile              |   1 +
 tracecmd/include/trace-local.h |  18 +++
 tracecmd/trace-record.c        |  97 +++++++++++++-
 tracecmd/trace-uprobes.c       | 223 +++++++++++++++++++++++++++++++++
 tracecmd/trace-usage.c         |   8 ++
 5 files changed, 346 insertions(+), 1 deletion(-)
 create mode 100644 tracecmd/trace-uprobes.c
tracecmd/Makefile +1 -0
index f9435558..37a5c42b 100644
@@ -32,6 +32,7 @@ TRACE_CMD_OBJS += trace-list.o
 TRACE_CMD_OBJS += trace-usage.o
 TRACE_CMD_OBJS += trace-dump.o
 TRACE_CMD_OBJS += trace-obj-debug.o
+TRACE_CMD_OBJS += trace-uprobes.o
 ifeq ($(VSOCK_DEFINED), 1)
 TRACE_CMD_OBJS += trace-tsync.o
 endif
tracecmd/include/trace-local.h +18 -0
index 933360a5..d3b767cf 100644
@@ -189,6 +189,21 @@ struct filter_pids {
 	int exclude;
 };
 
+struct tracecmd_uprobe {
+	struct tracecmd_uprobe *next;
+
+	char *file;
+	int pcount;
+	struct trace_debug_object *debug;
+	char **events;
+	int ecount;
+	int esize;
+};
+int tracecmd_uprobe_new(struct tracecmd_uprobe **list, char *file, char *func, bool libs);
+int tracecmd_uprobe_remove(struct tracecmd_uprobe *probes);
+void tracecmd_uprobe_free(struct tracecmd_uprobe *probes);
+int tracecmd_uprobe_create(struct tracecmd_uprobe *probes, bool uret);
+
 struct buffer_instance {
 	struct buffer_instance	*next;
 	char			*name;
@@ -209,6 +224,9 @@ struct buffer_instance {
 	struct func_list	*filter_funcs;
 	struct func_list	*notrace_funcs;
 
+	struct tracecmd_uprobe	*uprobes;
+	struct tracecmd_uprobe	*uretprobes;
+
 	struct opt_list		*options;
 	struct filter_pids	*filter_pids;
 	struct filter_pids	*process_pids;
tracecmd/trace-record.c +96 -1

151 lines not on this page · raw message

tracecmd/trace-uprobes.c new file +223 -0

223 lines not on this page · raw message

tracecmd/trace-usage.c +8 -0
index 3f0b2d07..49628d6b 100644
@@ -65,6 +65,10 @@ static struct usage_help usage_help[] = {
 		"               If a negative number is specified, timestamps synchronization is disabled"
 		"               If 0 is specified, no loop is performed - timestamps offset is calculated only twice,"
 		"                                                         at the beginnig and at the end of the trace\n"
+		"          --uprobe set the specified [file:function] as uprobe\n"
+		"          --uprobe-ret set the specified [file:function] as return uprobe\n"
+		"          --libs affects all --uprobe/--uprobe-ret after it: set uprobes on the libraries, that the application depends on\n"
+		"          --no-libs affects all --uprobe/--uprobe-ret after it: do not set uprobes on the libraries, that the application depends on\n"
 	},
 	{
 		"set",
@@ -101,6 +105,10 @@ static struct usage_help usage_help[] = {
 		"          --cmdlines-size change kernel saved_cmdlines_size\n"
 		"          --user execute the specified [command ...] as given user\n"
 		"          --fork return immediately if a command is specified\n"
+		"          --uprobe set the specified [file:function] as uprobe\n"
+		"          --uprobe-ret set the specified [file:function] as return uprobe\n"
+		"          --libs affects all --uprobe/--uprobe-ret after it: set uprobes on the libraries, that the application depends on\n"
+		"          --no-libs affects all --uprobe/--uprobe-ret after it: do not set uprobes on the libraries, that the application depends on\n"
 	},
 	{
 		"start",
Signature · 1 line
2.28.0