Initial trace-cmd support for ftrace uprobes
PATCH v2 3/3 trace-cmd: [POC] Add support for uprobes
In reply to Tzvetomir Stoyanov (VMware)Initial implementaton of trace-cmd support for ftrace uprobes. Two new trace-cmd record / set argumemnts are introduced:
--uprobe file:function
--uprobe-ret file:function
The ftrace (return) probe is set on given function from the file. Wildcards are supported in the function name:
--uprobe file:*
will set uprobes on all functions from the given file.
--- include/trace-cmd/trace-cmd.h | 1 + tracecmd/Makefile | 1 + tracecmd/include/trace-local.h | 18 +++ tracecmd/trace-record.c | 86 ++++++++++++- tracecmd/trace-uprobes.c | 221 +++++++++++++++++++++++++++++++++ tracecmd/trace-usage.c | 4 + 6 files changed, 330 insertions(+), 1 deletion(-) create mode 100644 tracecmd/trace-uprobes.c
include/trace-cmd/trace-cmd.h +1 -0
@@ -7,6 +7,7 @@#define _TRACE_CMD_H#include "traceevent/event-parse.h" +#include "tracefs.h" #define TRACECMD_MAGIC { 23, 8, 68 }
tracecmd/Makefile +1 -0
@@ -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
@@ -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); +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;struct tracefs_instance *tracefs;
@@ -208,6 +223,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 +85 -1
134 lines not on this page · raw message
tracecmd/trace-uprobes.c new file +221 -0
221 lines not on this page · raw message
tracecmd/trace-usage.c +4 -0
@@ -65,6 +65,8 @@ 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" }, { "set",
@@ -101,6 +103,8 @@ 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" }, { "start",
Signature · 1 line
2.26.2
Quoted 8 lines · On Wed, 16 Sep 2020 14:47:06 +0300 "Tzvetomir Stoyanov (VMware)"…
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:
--uprobe file:function
--uprobe-ret file:function
Thanks Tzvetomir,
I'll take a look at this.
-- Steve
Quoted 18 lines
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 uprobesMakefile | 10 + include/trace-cmd/trace-cmd.h | 9 + tracecmd/Makefile | 2 + tracecmd/include/trace-local.h | 49 +- tracecmd/trace-obj-debug.c | 823 +++++++++++++++++++++++++++++++++ tracecmd/trace-record.c | 193 ++++---- tracecmd/trace-uprobes.c | 221 +++++++++ tracecmd/trace-usage.c | 4 + 8 files changed, 1197 insertions(+), 114 deletions(-) create mode 100644 tracecmd/trace-obj-debug.c create mode 100644 tracecmd/trace-uprobes.c
v2 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:
--uprobe file:function
--uprobe-ret file:function
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 + include/trace-cmd/trace-cmd.h | 9 + tracecmd/Makefile | 2 + tracecmd/include/trace-local.h | 49 +- tracecmd/trace-obj-debug.c | 823 +++++++++++++++++++++++++++++++++ tracecmd/trace-record.c | 193 ++++---- tracecmd/trace-uprobes.c | 221 +++++++++ tracecmd/trace-usage.c | 4 + 8 files changed, 1197 insertions(+), 114 deletions(-) create mode 100644 tracecmd/trace-obj-debug.c create mode 100644 tracecmd/trace-uprobes.c
Signature · 1 line
2.26.2
PATCH v2 2/3 trace-cmd: New internal APIs for reading ELF header
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 + include/trace-cmd/trace-cmd.h | 8 + tracecmd/include/trace-local.h | 12 + tracecmd/trace-obj-debug.c | 685 +++++++++++++++++++++++++++++++++ 4 files changed, 715 insertions(+)
Makefile +10 -0
@@ -245,6 +245,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 "\#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
include/trace-cmd/trace-cmd.h +8 -0
@@ -135,6 +135,14 @@ struct tracecmd_proc_addr_map {char *lib_name; }; +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*/ +}; +typedef void (*tracecmd_show_data_func )(struct tracecmd_input *handle,struct tep_record *record); typedef void (*tracecmd_handle_init_func)(struct tracecmd_input *handle,
tracecmd/include/trace-local.h +12 -0
@@ -317,4 +317,16 @@ 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 trace_debug_object ; +struct trace_debug_object *trace_debug_obj_create_file (char *file); +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 +685 -0
695 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH v2 1/3 trace-cmd: Internal refactoring of pid address map logic
In reply to Tzvetomir Stoyanov (VMware)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
@@ -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
@@ -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.26.2
Hi Tzvetomir,
I really like this patch set. Great job!
To get it working on my laptop, I had to make the following changes. I ran this on a new VM which didn't have libbfd and it failed to build. Then the check wasn't quite working, and it was saying that libbfd wasn't installed even though it was. Finally, I tested it against a small program that had numbers in the function name, and I had to change your sanitizer to not convert numbers into underscores.
Also, all strdup()s need to be tested. But other than that, it looks great!
Below is the changes I made.
-- Steve
Makefile +1 -1
@@ -245,7 +245,7 @@ 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 "\#include <bfd.h>\n void main(){bfd_init();}" | $(CC) -xc - -lbfd >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi) +BFD_INSTALLED := $(shell if (printf "$(pound)include <bfd.h>\n void main(){bfd_init();}" | $(CC) -x c - -lbfd >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi) export BFD_INSTALLED ifeq ($(BFD_INSTALLED), 1)
tracecmd/trace-obj-debug.c +1 -1
@@ -8,13 +8,13 @@#include <string.h> #include <limits.h> #include <errno.h> -#include <bfd.h> #include <unistd.h> #include "trace-local.h" #include "trace-cmd.h" #ifdef BFD_INSTALLED +#include <bfd.h> struct trace_debug_handle { bfd *bfd;
tracecmd/trace-uprobes.c +1 -1
@@ -31,7 +31,7 @@ static char *uprobe_event_name(char *file, char *func, bool pret)asprintf (&event,"%c_%.*s_%.10s" , pret ?'r' :'p' ,10 , fname, func);if (event) {for (i =0 ; event[i]; i++) { -if (!isalpha (event[i])) +if (!isalnum (event[i])) event[i] ='_' ; } }
Re: [PATCH v2 2/3] trace-cmd: New internal APIs for reading ELF header
In reply to Tzvetomir Stoyanov (VMware)Quoted 5 lines · On Wed, 16 Sep 2020 14:47:08 +0300 "Tzvetomir Stoyanov (VMware)"…
tracecmd/trace-obj-debug.c +5
-0+#define RESOLVE_NAME (1 << 0) +#define RESOLVE_VMA (1 << 1) +#define RESOLVE_FOFFSET (1 << 2) +struct trace_obj_job { +unsigned int flags;
tracecmd/trace-obj-debug.c line 72
Quoted 10 lines
tracecmd/trace-obj-debug.c +10
-0+unsigned long long addr_offset; +struct debug_symbols *symbols; +}; + +struct dwarf_bfd_context { +asymbol **table; +struct trace_obj_job *job; +}; + +static void bfd_dwarf_section (bfd *abfd,asection *section,void *param)
Another small comment. Do not name any function that starts with "bfd_" as that makes it hard to know the difference between these functions and functions from the bfd library. The only functions that should have bfd_ as the start are those in the library.
Could call this: dwarf_bfd_section()?
Quoted 77 lines
tracecmd/trace-obj-debug.c +77
-0+{ +struct dwarf_bfd_context *context = (struct dwarf_bfd_context *)param; +unsigned int discriminator; +const char *functionname; +struct debug_symbols *s; +unsigned long long vma; +const char *filename; +unsigned int line; +bfd_boolean found; + +if (!(section->flags &SEC_CODE )) +return ; + +for (s = context->job->symbols; s; s = s->next) { +if (s->symbol.vma_near) + vma = s->symbol.vma_near; +else if (s->symbol.vma_start) + vma = s->symbol.vma_start; +else +continue ; + +if (abfd->flags &DYNAMIC ) + vma -= context->job->addr_offset; +if (vma && section->vma <= vma && + (section->vma + section->size) > vma) { +if (!s->symbol.fname) + s->symbol.fname =strdup (abfd->filename); +if (context->job->flags &RESOLVE_FOFFSET ) + s->symbol.foffset = section->filepos + (vma - section->vma); +if (!s->symbol.name && (context->job->flags &RESOLVE_NAME )) { + found =bfd_find_nearest_line_discriminator (abfd, section, context->table, + vma - section->vma, &filename, + &functionname, &line, &discriminator); +if (found) + s->symbol.name =strdup (functionname); + } + } + } +} + +static asymbol **get_sym_table (bfd *handle) +{ +bfd_boolean dyn = FALSE; +asymbol **symtable; +long count; +long size; + +if ((bfd_get_file_flags (handle) &HAS_SYMS ) ==0 ) +return NULL ; + size =bfd_get_symtab_upper_bound (handle); +if (size ==0 ) { + size =bfd_get_dynamic_symtab_upper_bound (handle); + dyn = TRUE; + } +if (size <=0 ) +return NULL ; + + symtable = (asymbol **)calloc (1 , size); +if (!symtable) +return NULL ; +if (dyn) + count =bfd_canonicalize_dynamic_symtab (handle, symtable); +else + count =bfd_canonicalize_symtab (handle, symtable); +if (count <=0 ) { +free (symtable); +return NULL ; + } +/* +* alloc = bfd_demangle(cur_bfd, name, demangle_flags); +*/ + +return symtable; +} + +static int sym_match (char *pattern,regex_t *regex,const char *symbol) +{
tracecmd/trace-obj-debug.c line 159
Quoted 10 lines
tracecmd/trace-obj-debug.c +10
-0+if (strlen (pattern) ==strlen (symbol) && + !strcmp (pattern, symbol)) +return 0 ; +if (regex && !regexec (regex, symbol,0 ,NULL ,0 )) +return 1 ; + +return -1 ; +} + +static int bfd_symlookup (struct dwarf_bfd_context *context)
Same here: lookup_bfd_sym()?
Quoted 34 lines
tracecmd/trace-obj-debug.c +34
-0+{ +struct debug_symbols *s, *last =NULL ; +struct debug_symbols *new, *new_list =NULL ; +unsigned long long vma; +asymbol **sp; +int res =0 ; +int ret; + +for (sp = context->table; *sp !=NULL ; sp++) { +if (!((*sp)->flags &BSF_FUNCTION )) +continue ; +for (s = context->job->symbols; s; s = s->next) { + last = s; + ret =sym_match (s->symbol.name, s->regex, (*sp)->name); +if (ret <0 ) +continue ; + vma = (*sp)->value + (*sp)->section->vma; +if ((*sp)->the_bfd->flags &DYNAMIC ) + vma += context->job->addr_offset; +if (ret ==0 ) {/* exact match */ + s->symbol.vma_start = vma; + }else {/* regex pattern match */ + new =calloc (1 ,sizeof (struct debug_symbols )); +if (!new) +break ; + new->symbol.name =strdup ((*sp)->name); + new->symbol.vma_start = vma; + new->symbol.vma_near = s->symbol.vma_near; + new->symbol.foffset = s->symbol.foffset; +if (s->symbol.fname) + new->symbol.fname =strdup (s->symbol.fname); + new->next = new_list; + new_list = new; + }
tracecmd/trace-obj-debug.c line 203
Quoted 10 lines
tracecmd/trace-obj-debug.c +10
-0+ res++; + } + } +if (last && !last->next) + last->next = new_list; + +return res; +} + +static int bfd_process_object (bfd *abfd,struct trace_obj_job *job)
find_bfd_process_object()?
Quoted 13 lines
tracecmd/trace-obj-debug.c +13
-0+{ +struct dwarf_bfd_context context; +int ret =0 ; + +memset (&context,0 ,sizeof (context)); + context.job = job; + +if (bfd_check_format_matches (abfd, bfd_object,NULL ) || +bfd_check_format_matches (abfd, bfd_core,NULL )) { + context.table =get_sym_table (abfd); +if (job->flags &RESOLVE_VMA ) +bfd_symlookup (&context); +if ((job->flags &RESOLVE_NAME ) || (job->flags &RESOLVE_FOFFSET ))
tracecmd/trace-obj-debug.c line 226
Quoted 10 lines
tracecmd/trace-obj-debug.c +10
-0+bfd_map_over_sections (abfd, bfd_dwarf_section, &context); +free (context.table); + }else { + ret =-1 ; + } + +return ret; +} + +static int bfd_read_all (bfd *abfd,struct trace_obj_job *job)
read_all_bfd()?
-- Steve
Quoted 26 lines
tracecmd/trace-obj-debug.c +26
-0+{ +bfd *last_arfile =NULL ; +bfd *arfile =NULL ; +int ret =0 ; + +if (bfd_check_format (abfd, bfd_archive)) { +for (;;) { +bfd_set_error (bfd_error_no_error); + arfile =bfd_openr_next_archived_file (abfd, arfile); +if (!arfile) { +if (bfd_get_error () != bfd_error_no_more_archived_files) +break ; + } + ret =bfd_read_all (arfile, job); +if (last_arfile) +bfd_close (last_arfile); + last_arfile = arfile; + } +if (last_arfile) +bfd_close (last_arfile); + }else + ret =bfd_process_object (abfd, job); + +return ret; +} +