trace-cmd: [POC] Add APIs for extracting DWARF information from a file
PATCH 1/1 trace-cmd: [POC] Add APIs for extracting DWARF information from a file
Added initial infrastructure and trace-cmd library APIs for working with DWARF debug information:
trace_obj_debug_create()
trace_obj_debug_destroy()
trace_obj_debug_get_fileoffset()
These APIs utilize libdwarf and libbfd for parsing ELF and DWARF headers of the files. The trace_obj_debug_get_fileoffset() API retrieves the offset in the file of the given functions. These offsets can be used to set a uprobes to the functions.
--- Makefile | 12 ++ include/trace-cmd/trace-cmd.h | 14 ++ lib/trace-cmd/Makefile | 17 ++ lib/trace-cmd/trace-obj-debug.c | 340 ++++++++++++++++++++++++++++++++ 4 files changed, 383 insertions(+) create mode 100644 lib/trace-cmd/trace-obj-debug.c
Makefile +12 -0
@@ -243,6 +243,18 @@ endif CUNIT_INSTALLED := $(shell if (echo -e "\#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 +DWARF_INSTALLED := $(shell if (echo -e "\#include <libdwarf/libdwarf.h>\n void main(){dwarf_init(-1, 0, 0, 0, 0, 0);}" | $(CC) -xc - -ldwarf >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi) +export DWARF_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) +LIBS += -lbfd +endif +ifeq ($(DWARF_INSTALLED), 1) +LIBS += -ldwarf +endif + export CFLAGS export INCLUDES
include/trace-cmd/trace-cmd.h +14 -0
@@ -483,6 +483,20 @@ void tracecmd_plog(const char *fmt, ...);void tracecmd_plog_error (const char *fmt, ...);int tracecmd_set_logfile (char *logfile); +/* --- Object debug --- */ +struct trace_obj_debug ; +struct trace_obj_symbols { +struct trace_obj_symbols *next; +char *name;/* symbol name */ +unsigned long long vma;/* symbol virtual memory address */ +unsigned long long foffset;/* symbol file offset */ +}; + +struct trace_obj_debug *trace_obj_debug_create (char *file); +void trace_obj_debug_destroy (struct trace_obj_debug *obj); +int trace_obj_debug_get_fileoffset (struct trace_obj_debug *obj, +struct trace_obj_symbols *symbols); +/* --- System --- */ unsigned long long tracecmd_generate_traceid (void );int tracecmd_count_cpus (void );
lib/trace-cmd/Makefile +17 -0
@@ -22,6 +22,16 @@ OBJS += trace-timesync.o OBJS += trace-blk-hack.o OBJS += trace-ftrace.o +ifeq ($(BFD_INSTALLED), 1) +ifeq ($(DWARF_INSTALLED), 1) +OBJS += trace-obj-debug.o +else +$(warning libdwarf is not installed) +endif +else +$(warning libbfd is not installed) +endif + OBJS := $(OBJS:%.o=$(bdir)/%.o) DEPS := $(OBJS:$(bdir)/%.o=$(bdir)/.%.d)
@@ -38,6 +48,13 @@ $(bdir)/libtracecmd.a: $(OBJS) LIBS = -L$(obj)/lib/traceevent -ltraceevent +ifeq ($(BFD_INSTALLED), 1) +LIBS += -lbfd +endif +ifeq ($(DWARF_INSTALLED), 1) +LIBS += -ldwarf +endif + $(bdir)/libtracecmd.so: $(OBJS) $(Q)$(call do_compile_shared_library)
lib/trace-cmd/trace-obj-debug.c new file +340 -0
340 lines not on this page · raw message
Signature · 1 line
2.26.2
On Thu, 21 May 2020 13:55:19 +0300
"Tzvetomir Stoyanov (VMware)" <[email protected]> wrote:
Hi Tzvetomir,
Thanks for doing this.
Quoted 47 lines
Added initial infrastructure and trace-cmd library APIs for working with DWARF debug information:
trace_obj_debug_create()
trace_obj_debug_destroy()
trace_obj_debug_get_fileoffset()
These APIs utilize libdwarf and libbfd for parsing ELF and DWARF headers of the files. Thetrace_obj_debug_get_fileoffset()API retrieves the offset in the file of the given functions. These offsets can be used to set a uprobes to the functions.Signed-off-by Tzvetomir Stoyanov (VMware) <[email protected]>--- Makefile | 12 ++ include/trace-cmd/trace-cmd.h | 14 ++ lib/trace-cmd/Makefile | 17 ++ lib/trace-cmd/trace-obj-debug.c | 340 ++++++++++++++++++++++++++++++++ 4 files changed, 383 insertions(+) create mode 100644 lib/trace-cmd/trace-obj-debug.cMakefile +12
-0@@ -243,6 +243,18 @@ endif CUNIT_INSTALLED := $(shell if (echo -e "\#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 +DWARF_INSTALLED := $(shell if (echo -e "\#include <libdwarf/libdwarf.h>\n void main(){dwarf_init(-1, 0, 0, 0, 0, 0);}" | $(CC) -xc - -ldwarf >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi) +export DWARF_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) +LIBS += -lbfd +endif +ifeq ($(DWARF_INSTALLED), 1) +LIBS += -ldwarf +endif + export CFLAGS export INCLUDESinclude/trace-cmd/trace-cmd.h +0
-0
include/trace-cmd/trace-cmd.h void tracecmd_plog(const char *fmt, ...); · line 487
Quoted 5 lines
include/trace-cmd/trace-cmd.h +2
-0@@ -483,6 +483,20 @@ void tracecmd_plog(const char *fmt, ...);void tracecmd_plog_error (const char *fmt, ...);int tracecmd_set_logfile (char *logfile); +/* --- Object debug --- */ +struct trace_obj_debug ;
Add a space here. It's fine to butt single lines declarations together, but lets keep full struct declarations separated.
include/trace-cmd/trace-cmd.h void tracecmd_plog(const char *fmt, ...); · line 498
Quoted 11 lines
include/trace-cmd/trace-cmd.h +11
-0+struct trace_obj_symbols { +struct trace_obj_symbols *next; +char *name;/* symbol name */ +unsigned long long vma;/* symbol virtual memory address */ +unsigned long long foffset;/* symbol file offset */ +}; + +struct trace_obj_debug *trace_obj_debug_create (char *file); +void trace_obj_debug_destroy (struct trace_obj_debug *obj); +int trace_obj_debug_get_fileoffset (struct trace_obj_debug *obj, +struct trace_obj_symbols *symbols);
Are these something we want in the tracecmd library? If so they should have a "tracecmd_" prefix and not "trace_". We use the "trace_" prefix for internal functions that are not part of the libraries API.
Quoted 45 lines
include/trace-cmd/trace-cmd.h +1
-0+/* --- System --- */ unsigned long long tracecmd_generate_traceid (void );int tracecmd_count_cpus (void );lib/trace-cmd/Makefile +17
-0@@ -22,6 +22,16 @@ OBJS += trace-timesync.o OBJS += trace-blk-hack.o OBJS += trace-ftrace.o +ifeq ($(BFD_INSTALLED), 1) +ifeq ($(DWARF_INSTALLED), 1) +OBJS += trace-obj-debug.o +else +$(warning libdwarf is not installed) +endif +else +$(warning libbfd is not installed) +endif + OBJS := $(OBJS:%.o=$(bdir)/%.o) DEPS := $(OBJS:$(bdir)/%.o=$(bdir)/.%.d)@@ -38,6 +48,13 @@ $(bdir)/libtracecmd.a: $(OBJS) LIBS = -L$(obj)/lib/traceevent -ltraceevent +ifeq ($(BFD_INSTALLED), 1) +LIBS += -lbfd +endif +ifeq ($(DWARF_INSTALLED), 1) +LIBS += -ldwarf +endif + $(bdir)/libtracecmd.so: $(OBJS) $(Q)$(call do_compile_shared_library)lib/trace-cmd/trace-obj-debug.c new file +0
-0
lib/trace-cmd/trace-obj-debug.c line 3
Quoted 3 lines
lib/trace-cmd/trace-obj-debug.c new file +3
-0@@ -0,0 +1,340 @@ +// SPDX-License-Identifier: LGPL-2.1 +/* +* Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <[email protected]>
This file looks completely written by you. You can remove my name from it.
Quoted 22 lines
lib/trace-cmd/trace-obj-debug.c new file +22
-0+* Copyright (C) 2020, VMware, Tzvetomir Stoyanov <[email protected]> +* +*/ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <dwarf.h> +#include <libdwarf/libdwarf.h> +#include <bfd.h> +#include "trace-cmd-local.h" +#include "trace-cmd.h" + +struct trace_obj_debug { +Dwarf_Debug dwarf; +bfd *bfd; +}; + +static void dwarf_error_handler (Dwarf_Error error,Dwarf_Ptr errarg) +{ +warning ("\nlibdwarf error detected: 0x%" DW_PR_DUx" %s\n" , +dwarf_errno (error),dwarf_errmsg (error)); +}
lib/trace-cmd/trace-obj-debug.c line 35
Quoted 10 lines
lib/trace-cmd/trace-obj-debug.c new file +10
-0+ +static void get_addr (Dwarf_Attribute attr,Dwarf_Addr *val) +{ +Dwarf_Error error =0 ; +Dwarf_Addr uval =0 ; +int res; + + res =dwarf_formaddr (attr, &uval, &error); +if (res ==DW_DLV_OK ) + *val = uval;
Hmm, shouldn't this function return a success or failure? Or does it not matter?
Quoted 6 lines
lib/trace-cmd/trace-obj-debug.c new file +6
-0+} + +static void dwarf_get_subprog (Dwarf_Debug dbg,Dwarf_Die die, +struct trace_obj_symbols *data,const char *name) +{ +Dwarf_Attribute *attrbuf =0 ;
lib/trace-cmd/trace-obj-debug.c line 51
Quoted 10 lines
lib/trace-cmd/trace-obj-debug.c new file +10
-0+Dwarf_Signed attrcount =0 ; +Dwarf_Error error =0 ; +Dwarf_Addr lowpc =0 ; +Dwarf_Half aform; +Dwarf_Signed i; +int res; + + res =dwarf_attrlist (die, &attrbuf, &attrcount, &error); +if (res !=DW_DLV_OK ) +return ;
This function should probably return success or failure.
lib/trace-cmd/trace-obj-debug.c line 56
Quoted 5 lines
lib/trace-cmd/trace-obj-debug.c new file +5
-0+for (i =0 ; i < attrcount ; ++i) { + res =dwarf_whatattr (attrbuf[i], &aform, &error); +if (res ==DW_DLV_OK ) { +if (aform == DW_AT_low_pc) +get_addr (attrbuf[i], &lowpc);
Hmm, what if we get more than one match? Should we break out of the loop on a match?
lib/trace-cmd/trace-obj-debug.c line 63
Quoted 7 lines
lib/trace-cmd/trace-obj-debug.c new file +7
-0+ } +dwarf_dealloc (dbg, attrbuf[i],DW_DLA_ATTR ); + } + +while (data) { +if (lowpc && !strcmp (name, data->name)) + data->vma = lowpc;
Is it possible to have more than one match here too?
lib/trace-cmd/trace-obj-debug.c line 72
Quoted 9 lines
lib/trace-cmd/trace-obj-debug.c new file +9
-0+ data = data->next; + } + +dwarf_dealloc (dbg, attrbuf,DW_DLA_LIST ); +} + + +static void dwarf_get_die_data (Dwarf_Debug dbg,Dwarf_Die die, +struct trace_obj_symbols *data)
This should probably return a status as well.
lib/trace-cmd/trace-obj-debug.c line 80
Quoted 8 lines
lib/trace-cmd/trace-obj-debug.c new file +8
-0+{ +const char *formname =0 ; +Dwarf_Attribute attr =0 ; +const char *tagname =0 ; +Dwarf_Half formnum =0 ; +Dwarf_Error error =0 ; +Dwarf_Half tag =0 ; +char *name =0 ;
Pointers should be assigned to NULL not 0.
Quoted 29 lines
lib/trace-cmd/trace-obj-debug.c new file +29
-0+int res =0 ; + + res =dwarf_diename (die, &name, &error); +if (res ==DW_DLV_ERROR ) { +warning ("Error in dwarf_diename: %s\n" ,dwarf_errmsg (error)); +return ; + } +if (res ==DW_DLV_NO_ENTRY ) + name ="<no DW_AT_name attr>" ; + + res =dwarf_tag (die, &tag, &error); +if (res !=DW_DLV_OK ) { +warning ("Error in dwarf_tag: %s n" ,dwarf_errmsg (error)); +return ; + } + res =dwarf_get_TAG_name (tag, &tagname); +if (res !=DW_DLV_OK ) { +warning ("Error in dwarf_get_TAG_name: %s\n" ,dwarf_errmsg (error)); +return ; + } + + res =dwarf_attr (die, DW_AT_name, &attr, &error); +if (res ==DW_DLV_OK ) { + res =dwarf_whatform (attr, &formnum, &error); +if (res !=DW_DLV_OK ) { +warning ("Error in dwarf_whatform: %s\n" , +dwarf_errmsg (error)); +return ; + }
lib/trace-cmd/trace-obj-debug.c line 119
Quoted 10 lines
lib/trace-cmd/trace-obj-debug.c new file +10
-0+ formname ="form-name-unavailable" ; + res =dwarf_get_FORM_name (formnum, &formname); +if (res !=DW_DLV_OK ) + formname ="UNKNoWn FORM!" ; +dwarf_dealloc (dbg, attr,DW_DLA_ATTR ); + } + +if (tag == DW_TAG_subprogram) +dwarf_get_subprog (dbg, die, data, name); +}
The rest looks good. I'll play a little more with it.
-- Steve
Quoted 7 lines · On Thu, 4 Jun 2020 15:20:13 -0400 Steven Rostedt <[email protected]> wrote:
On Thu, 21 May 2020 13:55:19 +0300
"Tzvetomir Stoyanov (VMware)" <[email protected]> wrote:Hi Tzvetomir,
Thanks for doing this.
I just realized there's no interface into this code. Do you have a patch that executes it? Can you post that too, or have you already and I'm just missing it?
Thanks!
-- Steve