Skip to contentSkip to conversation

libtraceevent: Makefile updates fixes and unit tests

PATCH New Steven Rostedt 1 participant 11 messages Dec 16, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover libtraceevent: Makefile updates fixes and unit tests

The libtraceevent Makefiles are a result of its several foster homes. It started at trace-cmd and then went into the kernel, and now lives in its own repository. The Makefiles were brought over from the previous homes and modified and tweaked to get them to work. This added a bunch of unnecessary logic that is not needed for just this library.

The Makefiles were rewritten to be more consistent with libtracefs and with trace-cmd, which greatly simplified them.

A unit test was added to start testing event parsing and reading to lay the ground work for more unit tests that will help keep libtraceevent from having more regressions (like one that slipped in).

The processing of the old dynamic string format (which had just he offset where the length of the string was simply determined by the size of the string at the offset) stopped working. It actually worked by shear luck, but changes to add the rel_loc parsing took away all that luck. It had to be refactored to be more specific in knowing that the format is of the old size.

Deprecate tep_print_field() as it does not give the user to pass the in the size of the data to parse, and since it could be from untrusted sources, that would make it a security issue.

Add tep_print_field_content() that requires a size also passed in.

Use the size more to verify that only the data gets looked at and does not rely on the data to stay within bounds.

*** NOTE *** there needs to be an audit of all the code to add checks to make sure all access are within the data. Even though the size is now passed in, there are many cases where what is parsed is going to be within those limits.

Steven Rostedt (VMware) (10):
libtraceevent: Fix installing of man pages in remote directory
libtraceevent: Rewrite Makefiles to be like libtracefs
libtraceevent: Have make tags and TAGS honor O= build directory
libtraceevent: Move installed headers into their own directory
libtraceevent: Add unit test framework for library commands
libtraceveent: Add testing of old dynamic string event layout
libraceevent: Add unit test to test print_fmt with dynamic strings
libtraceevent: Account for old dynamic string formats and honor size
libtraceevent: Replace tep_print_field() with
tep_print_field_content()
libtraceevent: Have print_field_raw() handle old data layout

 Documentation/Makefile                      |  16 +-
 Documentation/libtraceevent-field_print.txt |  10 +-
 Documentation/libtraceevent.txt             |   2 +-
 Makefile                                    | 215 +++++++++++---------
 build/Build.include                         | 103 ----------
 build/Makefile.build                        | 162 ---------------
 {src => include/traceevent}/event-parse.h   |   8 +-
 {src => include/traceevent}/event-utils.h   |   0
 {src => include/traceevent}/kbuffer.h       |   0
 {src => include/traceevent}/trace-seq.h     |   0
 plugins/Makefile                            |  63 ++----
 scripts/Makefile.include                    | 135 ------------
 scripts/features.mk                         |  37 ++++
 scripts/utils.mk                            | 174 ++++++++++++++++
 src/Makefile                                |  51 ++++-
 src/event-parse.c                           |  96 ++++++---
 utest/Makefile                              |  38 ++++
 utest/README                                |  17 ++
 utest/trace-utest.c                         |  86 ++++++++
 utest/trace-utest.h                         |  14 ++
 utest/traceevent-utest.c                    | 172 ++++++++++++++++
 21 files changed, 807 insertions(+), 592 deletions(-)
 delete mode 100644 build/Build.include
 delete mode 100644 build/Makefile.build
 rename {src => include/traceevent}/event-parse.h (99%)
 rename {src => include/traceevent}/event-utils.h (100%)
 rename {src => include/traceevent}/kbuffer.h (100%)
 rename {src => include/traceevent}/trace-seq.h (100%)
 delete mode 100644 scripts/Makefile.include
 create mode 100644 scripts/features.mk
 create mode 100644 scripts/utils.mk
 create mode 100644 utest/Makefile
 create mode 100644 utest/README
 create mode 100644 utest/trace-utest.c
 create mode 100644 utest/trace-utest.h
 create mode 100644 utest/traceevent-utest.c
Signature · 1 line
2.33.0
#1 lore raw

PATCH 1/10 libtraceevent: Fix installing of man pages in remote directory

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

When the building of the man pages is done in a directory other than the repo directory (with the O= option), the intermediate build file name incorrectly appends a "install-" prefix to the path and not to the file, causing the installation to fail.

Fix it by using patsubst instead of addprefix to append the "install-" name to the intermediate files for the installation.

---
 Documentation/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/Makefile +2 -2
index 73dbe2ae4d43..af124572c863 100644
@@ -154,12 +154,12 @@ ifdef missing_tools
 	$(error "You need to install $(missing_tools) for man pages")
 endif
 
-install-%.3: $(OUTPUT)%.3
+$(OUTPUT)install-%.3: $(OUTPUT)%.3
 	$(call QUIET_INSTALL, $<) \
 		$(INSTALL) -d -m 755 $(DESTDIR)$(man3dir); \
 		$(INSTALL) -m 644 $< $(DESTDIR)$(man3dir);
 
-do-install-man: man $(addprefix install-,$(wildcard $(OUTPUT)*.3))
+do-install-man: man $(patsubst $(OUTPUT)%,$(OUTPUT)install-%,$(wildcard $(OUTPUT)*.3))
 
 install-man: check-man-tools man
 	$(Q)$(MAKE) -C . do-install-man
Signature · 1 line
2.33.0
#2 lore raw

PATCH 2/10 libtraceevent: Rewrite Makefiles to be like libtracefs

In reply to Steven Rostedt

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

The libtraceevent code has moved around various homes. It originated in the trace-cmd.git repository and then moved to the Linux kernel under tools/libs. Finally it made its home in its own libtraceevent git repo. During its time at different foster homes, it took with it various ways to build it from the other homes and this was incorporated into the Makefiles. But the kernel Makefiles were rather complex as it had to build not only libtraceevent but other tooling.

Unfortunately, that complexity made it's way into libtraceevent, making it difficult to add new functionality. Instead, rewrite it to be more like libtracefs that has a much simpler Makefile structure.

---
 Documentation/Makefile   |  12 ++-
 Makefile                 | 185 ++++++++++++++++++++-------------------
 build/Build.include      | 103 ----------------------
 build/Makefile.build     | 162 ----------------------------------
 plugins/Makefile         |  61 +++----------
 scripts/Makefile.include | 135 ----------------------------
 scripts/features.mk      |  37 ++++++++
 scripts/utils.mk         | 174 ++++++++++++++++++++++++++++++++++++
 src/Makefile             |  51 ++++++++++-
 9 files changed, 371 insertions(+), 549 deletions(-)
 delete mode 100644 build/Build.include
 delete mode 100644 build/Makefile.build
 delete mode 100644 scripts/Makefile.include
 create mode 100644 scripts/features.mk
 create mode 100644 scripts/utils.mk
Documentation/Makefile +5 -7
index af124572c863..5904b798b786 100644
@@ -1,5 +1,5 @@
-include ../scripts/Makefile.include
 include ../scripts/utilities.mak
+include ../scripts/utils.mk
 
 # This Makefile and manpage XSL files were taken from tools/perf/Documentation
 # and modified for libtraceevent.
@@ -13,6 +13,8 @@ _MAN_XML=$(patsubst %.txt,%.xml,$(MAN_TXT))
 _MAN_HTML=$(patsubst %.txt,%.html,$(MAN_TXT))
 _DOC_MAN3=$(patsubst %.txt,%.m,$(MAN3_TXT))
 
+OUTPUT		:= $(obj)/Documentation/
+
 MAN_XML=$(addprefix $(OUTPUT),$(_MAN_XML))
 MAN_HTML=$(addprefix $(OUTPUT),$(_MAN_HTML))
 DOC_MAN3=$(addprefix $(OUTPUT),$(_DOC_MAN3))
@@ -155,9 +157,7 @@ ifdef missing_tools
 endif
 
 $(OUTPUT)install-%.3: $(OUTPUT)%.3
-	$(call QUIET_INSTALL, $<) \
-		$(INSTALL) -d -m 755 $(DESTDIR)$(man3dir); \
-		$(INSTALL) -m 644 $< $(DESTDIR)$(man3dir);
+	$(Q)$(call do_install,$<,$(man3dir),644)
 
 do-install-man: man $(patsubst $(OUTPUT)%,$(OUTPUT)install-%,$(wildcard $(OUTPUT)*.3))
 
@@ -165,9 +165,7 @@ install-man: check-man-tools man
 	$(Q)$(MAKE) -C . do-install-man
 
 install-%.txt: $(OUTPUT)%.html
-	$(call QUIET_INSTALL, $<) \
-		$(INSTALL) -d -m 755 $(DESTDIR)$(htmldir); \
-		$(INSTALL) -m 644 $< $(DESTDIR)$(htmldir);
+	$(Q)$(call do_install,$<,$(htmldir),644)
 
 do-install-html: html $(addprefix install-,$(wildcard *.txt))
 
Makefile +94 -91

316 lines not on this page · raw message

build/Build.include deleted +0 -103

103 lines not on this page · raw message

build/Makefile.build deleted +0 -162

162 lines not on this page · raw message

plugins/Makefile +14 -47

107 lines not on this page · raw message

scripts/Makefile.include deleted +0 -135

135 lines not on this page · raw message

scripts/features.mk new file +37 -0
new file mode 100644
index 000000000000..9c7f8c3076b3
@@ -0,0 +1,37 @@
+# SPDX-License-Identifier: GPL-2.0
+
+# taken from perf which was based on Linux Kbuild
+# try-cc
+# Usage: option = $(call try-cc, source-to-build, cc-options)
+try-cc = $(shell sh -c							\
+	'TMP="$(BUILD_OUTPUT)$(TMPOUT).$$$$";						\
+	echo "$(1)" |							\
+	$(CC) -x c - $(2) -o "$$TMP" > /dev/null 2>&1 && echo y;	\
+	rm -f "$$TMP"')
+
+define SOURCE_PTRACE
+#include <stdio.h>
+#include <sys/ptrace.h>
+
+int main (void)
+{
+	int ret;
+	ret = ptrace(PTRACE_ATTACH, 0, NULL, 0);
+	ptrace(PTRACE_TRACEME, 0, NULL, 0);
+	ptrace(PTRACE_GETSIGINFO, 0, NULL, NULL);
+	ptrace(PTRACE_GETEVENTMSG, 0, NULL, NULL);
+	ptrace(PTRACE_SETOPTIONS, NULL, NULL,
+		       PTRACE_O_TRACEFORK |
+		       PTRACE_O_TRACEVFORK |
+		       PTRACE_O_TRACECLONE |
+		       PTRACE_O_TRACEEXIT);
+	ptrace(PTRACE_CONT, NULL, NULL, 0);
+	ptrace(PTRACE_DETACH, 0, NULL, NULL);
+	ptrace(PTRACE_SETOPTIONS, 0, NULL,
+	       PTRACE_O_TRACEFORK |
+	       PTRACE_O_TRACEVFORK |
+	       PTRACE_O_TRACECLONE |
+	       PTRACE_O_TRACEEXIT);
+	return ret;
+}
+endef
scripts/utils.mk new file +174 -0

174 lines not on this page · raw message

src/Makefile +47 -4

59 lines not on this page · raw message

Signature · 1 line
2.33.0
#3 lore raw

PATCH 3/10 libtraceevent: Have make tags and TAGS honor O= build directory

In reply to Steven Rostedt

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

Currently if one does a "make O=/tmp/build tags" the tags are still built in the source directory. Although this is uncommon to do, the tags (and TAGS) target should still honor the build directory.

---
 Makefile | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
Makefile +16 -9
index 4363e9b73dc3..8b3b5cc1ea35 100644
@@ -246,15 +246,22 @@ define update_dir
    fi);
 endef
 
-tags:	force
-	$(RM) tags
-	find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \
-	--regex-c++='/_PE\(([^,)]*).*/TEP_ERRNO__\1/'
-
-TAGS:	force
-	$(RM) TAGS
-	find . -name '*.[ch]' | xargs etags \
-	--regex='/_PE(\([^,)]*\).*/TEP_ERRNO__\1/'
+VIM_TAGS = $(obj)/tags
+EMACS_TAGS = $(obj)/TAGS
+
+$(VIM_TAGS): force
+	$(RM) $(VIM_TAGS)
+	find $(src) -name '*.[ch]' | (cd $(obj) && xargs ctags --extra=+f --c-kinds=+px \
+	--regex-c++='/_PE\(([^,)]*).*/TEP_ERRNO__\1/')
+
+tags: $(VIM_TAGS)
+
+$(EMACS_TAGS): force
+	$(RM) $(EMACS_TAGS)
+	find $(src) -name '*.[ch]' | (cd $(obj) && xargs etags \
+	--regex='/_PE(\([^,)]*\).*/TEP_ERRNO__\1/')
+
+TAGS: $(EMACS_TAGS)
 
 define build_prefix
 	(echo $1 > [email protected];	\
Signature · 1 line
2.33.0
#4 lore raw

PATCH 4/10 libtraceevent: Move installed headers into their own directory

In reply to Steven Rostedt

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

It was noted that it is confusing that the headers that get installed and are API are in the same directory as the source code. Create a include/traceevent directory and move the headers that get installed onto the system into that directory.

---
 Makefile                                  | 14 ++++++++------
 {src => include/traceevent}/event-parse.h |  0
 {src => include/traceevent}/event-utils.h |  0
 {src => include/traceevent}/kbuffer.h     |  0
 {src => include/traceevent}/trace-seq.h   |  0
 plugins/Makefile                          |  2 +-
 6 files changed, 9 insertions(+), 7 deletions(-)
 rename {src => include/traceevent}/event-parse.h (100%)
 rename {src => include/traceevent}/event-utils.h (100%)
 rename {src => include/traceevent}/kbuffer.h (100%)
 rename {src => include/traceevent}/trace-seq.h (100%)
Makefile +8 -6
index 8b3b5cc1ea35..965eb56a8ea0 100644
@@ -117,9 +117,11 @@ N		=
 LIBTRACEEVENT_STATIC = $(bdir)/libtraceevent.a
 LIBTRACEEVENT_SHARED = $(bdir)/libtraceevent.so.$(EVENT_PARSE_VERSION)
 
-INCLUDES = -I. -I $(srctree)/include $(CONFIG_INCLUDES)
+EP_HEADERS_DIR = $(src)/include/traceevent
 
-export LIBTRACEEVENT_STATIC LIBTRACEEVENT_SHARED
+INCLUDES = -I. -I $(srctree)/include -I $(EP_HEADERS_DIR) $(CONFIG_INCLUDES)
+
+export LIBTRACEEVENT_STATIC LIBTRACEEVENT_SHARED EP_HEADERS_DIR
 
 # Set compile option CFLAGS
 ifdef EXTRA_CFLAGS
@@ -334,10 +336,10 @@ install_pkgconfig: $(PKG_CONFIG_FILE)
 	$(Q)$(call do_install_pkgconfig_file,$(prefix))
 
 install_headers:
-	$(Q)$(call do_install,src/event-parse.h,$(includedir_SQ),644);
-	$(Q)$(call do_install,src/event-utils.h,$(includedir_SQ),644);
-	$(Q)$(call do_install,src/trace-seq.h,$(includedir_SQ),644);
-	$(Q)$(call do_install,src/kbuffer.h,$(includedir_SQ),644)
+	$(Q)$(call do_install,$(EP_HEADERS_DIR)/event-parse.h,$(includedir_SQ),644);
+	$(Q)$(call do_install,$(EP_HEADERS_DIR)/event-utils.h,$(includedir_SQ),644);
+	$(Q)$(call do_install,$(EP_HEADERS_DIR)/trace-seq.h,$(includedir_SQ),644);
+	$(Q)$(call do_install,$(EP_HEADERS_DIR)/kbuffer.h,$(includedir_SQ),644)
 
 install: install_libs
 
include/traceevent/event-parse.h renamed from src/event-parse.h +0 -0
similarity index 100%
rename from src/event-parse.h
rename to include/traceevent/event-parse.h
include/traceevent/event-utils.h renamed from src/event-utils.h +0 -0
similarity index 100%
rename from src/event-utils.h
rename to include/traceevent/event-utils.h
include/traceevent/kbuffer.h renamed from src/kbuffer.h +0 -0
similarity index 100%
rename from src/kbuffer.h
rename to include/traceevent/kbuffer.h
include/traceevent/trace-seq.h renamed from src/trace-seq.h +0 -0
similarity index 100%
rename from src/trace-seq.h
rename to include/traceevent/trace-seq.h
plugins/Makefile +1 -1
index 8b1dbf68757c..4c8cb177b668 100644
@@ -62,7 +62,7 @@ CONFIG_FLAGS   =
 OBJ            = $@
 N              =
 
-INCLUDES = -I. -I.. -I../src -I $(srctree)/include $(CONFIG_INCLUDES)
+INCLUDES = -I. -I.. -I../src -I $(srctree)/include -I $(EP_HEADERS_DIR) $(CONFIG_INCLUDES)
 
 # Set compile option CFLAGS
 ifdef EXTRA_CFLAGS
Signature · 1 line
2.33.0
#5 lore raw

PATCH 5/10 libtraceevent: Add unit test framework for library commands

In reply to Steven Rostedt

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

Add the frame work to start adding CUnit test framework. This also adds one test to test dynamic strings, but more should be added.

---
 Makefile                 |   5 ++
 utest/Makefile           |  38 +++++++++++++
 utest/README             |  17 ++++++
 utest/trace-utest.c      |  86 +++++++++++++++++++++++++++++
 utest/trace-utest.h      |  14 +++++
 utest/traceevent-utest.c | 114 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 274 insertions(+)
 create mode 100644 utest/Makefile
 create mode 100644 utest/README
 create mode 100644 utest/trace-utest.c
 create mode 100644 utest/trace-utest.h
 create mode 100644 utest/traceevent-utest.c
Makefile +5 -0
index 965eb56a8ea0..b26b0d5804ce 100644
@@ -248,6 +248,11 @@ define update_dir
    fi);
 endef
 
+UTEST_DIR = utest
+
+test:	force $(LIBTRACEEVENT_STATIC)
+	$(Q)$(call descend,$(UTEST_DIR),test)
+
 VIM_TAGS = $(obj)/tags
 EMACS_TAGS = $(obj)/TAGS
 
utest/Makefile new file +38 -0
new file mode 100644
index 000000000000..160bdb434653
@@ -0,0 +1,38 @@
+# SPDX-License-Identifier: LGPL-2.1
+
+include $(src)/scripts/utils.mk
+
+TARGETS = $(bdir)/trace-utest
+
+OBJS =
+OBJS += trace-utest.o
+OBJS += traceevent-utest.o
+
+LIBS += -lcunit				\
+	-ldl				\
+	$(LIBTRACEEVENT_STATIC)
+
+OBJS := $(OBJS:%.o=$(bdir)/%.o)
+DEPS := $(OBJS:$(bdir)/%.o=$(bdir)/.%.d)
+
+$(OBJS): | $(bdir)
+$(DEPS): | $(bdir)
+
+$(bdir)/trace-utest: $(OBJS) $(LIBTRACEEVENT_STATIC)
+	$(Q)$(do_app_build)
+
+$(bdir)/%.o: %.c
+	$(Q)$(call do_fpic_compile)
+
+$(DEPS): $(bdir)/.%.d: %.c
+	$(Q)$(CC) -M $(CPPFLAGS) $(CFLAGS) $< > $@
+	$(Q)$(CC) -M -MT $(bdir)/$*.o $(CPPFLAGS) $(CFLAGS) $< > $@
+
+$(OBJS): $(bdir)/%.o : $(bdir)/.%.d
+
+dep_includes := $(wildcard $(DEPS))
+
+test: $(TARGETS)
+
+clean:
+	$(Q)$(call do_clean,$(TARGETS) $(bdir)/*.o $(bdir)/.*.d)
utest/README new file +17 -0
new file mode 100644
index 000000000000..87d6bd9b82dc
@@ -0,0 +1,17 @@
+
+Unit tests for libtraceevent library. The tests use CUnit framework:
+
+ http://cunit.sourceforge.net/
+
+which must be pre installed on the system, before building the unit tests.
+The framework can be downloaded, compiled and installed manually, or
+using a precompiled distro package:
+
+ Fedora:
+	 CUnit
+	 CUnit-devel
+
+ Ubuntu and Debian:
+	libcunit1
+	libcunit1-doc
+	libcunit1-dev
utest/trace-utest.c new file +86 -0

86 lines not on this page · raw message

utest/trace-utest.h new file +14 -0
new file mode 100644
index 000000000000..2aa3455e7696
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: LGPL-2.1 */
+/*
+ * Copyright (C) 2020, VMware, Tzvetomir Stoyanov <[email protected]>
+ *
+ * Modified from libtracefs to libtraceevent:
+ *   Copyright (C) 2021, VMware, Steven Rostedt <[email protected]>
+ *
+ */
+#ifndef _TRACE_UTEST_H_
+#define _TRACE_UTEST_H_
+
+void test_traceevent_lib(void);
+
+#endif /* _TRACE_UTEST_H_ */
utest/traceevent-utest.c new file +114 -0

114 lines not on this page · raw message

Signature · 1 line
2.33.0
#6 lore raw

PATCH 6/10 libtraceveent: Add testing of old dynamic string event layout

In reply to Steven Rostedt

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

When running against old trace.dat files, the output would not print strings. This was due to a change that did not take into account the old dynamic format that did not have strings as an array.

Add a unit test to cover this case so that it does not break again.

---
 utest/traceevent-utest.c | 54 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 51 insertions(+), 3 deletions(-)
utest/traceevent-utest.c +51 -3
index 2910b95d01e6..f4fdc86664ca 100644
@@ -63,26 +63,72 @@ static char dyn_str_data[] = {
 };
 static void *dyn_str_event_data = (void *)dyn_str_data;
 
+static const char dyn_str_old_event[] =
+	"name: irq_handler_entry\n"
+	"ID: 2\n"
+	"format:\n"
+	"\tfield:unsigned short common_type;\toffset:0;\tsize:2;\n"
+	"\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\n"
+	"\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;\n"
+	"\tfield:int common_pid;\toffset:4;\tsize:4;\n"
+	"\n"
+        "\tfield:int irq;\toffset:8;\tsize:4;\n"
+        "\tfield:__data_loc name;\toffset:12;\tsize:2;\n"
+	"\n"
+	"print fmt: \"irq=%d handler=%s\", REC->irq, __get_str(name)\n";
+
+static char dyn_str_old_data[] = {
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+	/* common type */		2, 0x00,
+#else
+	/* common type */		0x00, 2
+#endif
+	/* common flags */		0x00,
+	/* common_preempt_count */	0x00,
+	/* common_pid */		0x00, 0x00, 0x00, 0x00,
+	/* irq */			0x00, 0x00, 0x00, 0x00,
+
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+	/* name : offset */		16, 0x00,
+#else
+	/* name : offset */		0x00, 16,
+#endif
+	/* padding */			0x00, 0x00,
+	/* name */			'h', 'e', 'l', 'l', 'o', '\0',
+	/* padding */			0x00, 0x00, 0x00
+};
+static void *dyn_str_old_event_data = (void *)dyn_str_old_data;
+
 static struct tep_handle *test_tep;
 static struct trace_seq *test_seq;
 static struct trace_seq seq_storage;
 
-static void test_parse_dyn_str_event(void)
+static void parse_dyn_str(const char *dyn_str, void *data)
 {
 	struct tep_format_field *field;
 	struct tep_event *event;
 
 	CU_TEST(tep_parse_format(test_tep, &event,
-				 dyn_str_event, strlen(dyn_str_event),
+				 dyn_str, strlen(dyn_str),
 				DYN_STR_EVENT_SYSTEM) == TEP_ERRNO__SUCCESS);
 
 	field = tep_find_any_field(event, DYN_STR_FIELD);
 	CU_TEST(field != NULL);
 	trace_seq_reset(test_seq);
-	tep_print_field(test_seq, dyn_str_event_data, field);
+	tep_print_field(test_seq, data, field);
 	CU_TEST(strcmp(test_seq->buffer, DYN_STRING) == 0);
 }
 
+static void test_parse_dyn_str_event(void)
+{
+	parse_dyn_str(dyn_str_event, dyn_str_event_data);
+}
+
+static void test_parse_dyn_str_old_event(void)
+{
+	parse_dyn_str(dyn_str_old_event, dyn_str_old_event_data);
+}
+
 static int test_suite_destroy(void)
 {
 	tep_free(test_tep);
@@ -111,4 +157,6 @@ void test_traceevent_lib(void)
 	}
 	CU_add_test(suite, "parse dynamic string event",
 		    test_parse_dyn_str_event);
+	CU_add_test(suite, "parse old dynamic string event",
+		    test_parse_dyn_str_old_event);
 }
Signature · 1 line
2.33.0
#7 lore raw

PATCH 7/10 libraceevent: Add unit test to test print_fmt with dynamic strings

In reply to Steven Rostedt

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

Add a unit test that calls tep_print_event() with the TEP_PRINT_INFO with an event that has dynamic strings (with both the old and new formats).

---
 utest/traceevent-utest.c | 9 +++++++++
 1 file changed, 9 insertions(+)
utest/traceevent-utest.c +9 -0
index f4fdc86664ca..a0d808e5c1a5 100644
@@ -26,6 +26,7 @@
 #define DYN_STR_EVENT_SYSTEM		"irq"
 #define DYN_STR_FIELD			"name"
 #define DYN_STRING			"hello"
+#define DYN_STRING_FMT			"irq=0 handler=hello"
 static const char dyn_str_event[] =
 	"name: irq_handler_entry\n"
 	"ID: 1\n"
@@ -107,6 +108,9 @@ static void parse_dyn_str(const char *dyn_str, void *data)
 {
 	struct tep_format_field *field;
 	struct tep_event *event;
+	struct tep_record record;
+
+	record.data = data;
 
 	CU_TEST(tep_parse_format(test_tep, &event,
 				 dyn_str, strlen(dyn_str),
@@ -117,6 +121,11 @@ static void parse_dyn_str(const char *dyn_str, void *data)
 	trace_seq_reset(test_seq);
 	tep_print_field(test_seq, data, field);
 	CU_TEST(strcmp(test_seq->buffer, DYN_STRING) == 0);
+
+	trace_seq_reset(test_seq);
+	tep_print_event(test_tep, test_seq, &record, "%s", TEP_PRINT_INFO);
+	trace_seq_do_printf(test_seq);
+	CU_TEST(strcmp(test_seq->buffer, DYN_STRING_FMT) == 0);
 }
 
 static void test_parse_dyn_str_event(void)
Signature · 1 line
2.33.0
#8 lore raw

PATCH 8/10 libtraceevent: Account for old dynamic string formats and honor size

In reply to Steven Rostedt

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

Old kernels that started with dynamic strings did not place the length of the dynamic string into the place holder. Instead, it only had a two byte offset to where the string was located in the data payload.

It just worked by shear luck that the reading of the string would look at he length (which was just random numbers) and if it wasn't zero, it would print the string normally. With the update to include rel_loc, the size of the place holder is no longer hard coded as 4, and the actual size is used (2 for the old format). This caused the length of the string to always be zero, which triggered the check to not bother printing strings of size zero. Hence the strings stopped being printed.

Start using the size of the data payload to limit the string parsing. More work needs to be done in this regard, but having an application trust the size of the payload of unknown data can be dangerous. Use this as an opportunity to parse by size. Make the length of the string from the old format the rest of the data payload, and not depend on it ending with '\0'.

Note, tep_print_field() does not allow the user to pass in the size of the data to parse. Currently use 4096, but this will need to be deprecated.

---
 src/event-parse.c        | 67 ++++++++++++++++++++++++++--------------
 utest/traceevent-utest.c |  7 +++--
 2 files changed, 47 insertions(+), 27 deletions(-)
src/event-parse.c +43 -24
index 34d6c9616f08..62afe86f2c6d 100644
@@ -3878,9 +3878,10 @@ static unsigned long long test_for_symbol(struct tep_handle *tep,
 #define TEP_LEN_SHIFT			16
 
 static void dynamic_offset(struct tep_handle *tep, int size, void *data,
-			   unsigned int *offset, unsigned int *len)
+			   int data_size, unsigned int *offset, unsigned int *len)
 {
 	unsigned long long val;
+	unsigned int o, l;
 
 	/*
 	 * The total allocated length of the dynamic array is
@@ -3889,19 +3890,36 @@ static void dynamic_offset(struct tep_handle *tep, int size, void *data,
 	 */
 	val = tep_read_number(tep, data, size);
 
+	/* Check for overflows */
+	o = (unsigned int)(val & TEP_OFFSET_LEN_MASK);
+
+	/* If there's no length, then just make the length the size of the data */
+	if (size == 2)
+		l = data_size - o;
+	else
+		l = (unsigned int)((val >> TEP_LEN_SHIFT) & TEP_OFFSET_LEN_MASK);
+
 	if (offset)
-		*offset = (unsigned int)(val & TEP_OFFSET_LEN_MASK);
+		*offset = o > data_size ? 0 : o;
 	if (len)
-		*len = (unsigned int)((val >> TEP_LEN_SHIFT) & TEP_OFFSET_LEN_MASK);
+		*len = o + l > data_size ? 0 : l;
 }
 
 static inline void dynamic_offset_field(struct tep_handle *tep,
 					struct tep_format_field *field,
-					void *data,
+					void *data, int size,
 					unsigned int *offset,
 					unsigned int *len)
 {
-	dynamic_offset(tep, field->size, data + field->offset, offset, len);
+	/* Test for overflow */
+	if (field->offset + field->size > size) {
+		if (*offset)
+			*offset = 0;
+		if (*len)
+			*len = 0;
+		return;
+	}
+	dynamic_offset(tep, field->size, data + field->offset, size, offset, len);
 	if (field->flags & TEP_FIELD_IS_RELATIVE)
 		*offset += field->offset + field->size;
 }
@@ -3978,7 +3996,7 @@ eval_num_arg(void *data, int size, struct tep_event *event, struct tep_print_arg
 			switch (larg->type) {
 			case TEP_PRINT_DYNAMIC_ARRAY:
 				dynamic_offset_field(tep, larg->dynarray.field, data,
-						     &offset, NULL);
+						     size, &offset, NULL);
 				offset += right;
 				if (larg->dynarray.field->elementsize)
 					field_size = larg->dynarray.field->elementsize;
@@ -4100,13 +4118,13 @@ eval_num_arg(void *data, int size, struct tep_event *event, struct tep_print_arg
 		}
 		break;
 	case TEP_PRINT_DYNAMIC_ARRAY_LEN:
-		dynamic_offset_field(tep, arg->dynarray.field, data,
+		dynamic_offset_field(tep, arg->dynarray.field, data, size,
 				     NULL, &field_size);
 		val = field_size;
 		break;
 	case TEP_PRINT_DYNAMIC_ARRAY:
 		/* Without [], we pass the address to the dynamic data */
-		dynamic_offset_field(tep, arg->dynarray.field, data,
+		dynamic_offset_field(tep, arg->dynarray.field, data, size,
 				     &offset, NULL);
 		val = (unsigned long long)((unsigned long)data + offset);
 		val = (unsigned long)data + offset;
@@ -4348,7 +4366,7 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,
 	case TEP_PRINT_HEX_STR:
 		if (arg->hex.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
 			dynamic_offset_field(tep, arg->hex.field->dynarray.field, data,
-				             &offset, NULL);
+				             size, &offset, NULL);
 			hex = data + offset;
 		} else {
 			field = arg->hex.field->field.field;
@@ -4375,7 +4393,7 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,
 
 		if (arg->int_array.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
 			dynamic_offset_field(tep, arg->int_array.field->dynarray.field, data,
-					     &offset, NULL);
+					     size, &offset, NULL);
 			num = data + offset;
 		} else {
 			field = arg->int_array.field->field.field;
@@ -4420,7 +4438,7 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,
 			arg->string.field = tep_find_any_field(event, arg->string.string);
 		if (!arg->string.field)
 			break;
-		dynamic_offset_field(tep, arg->string.field, data, &offset, &len);
+		dynamic_offset_field(tep, arg->string.field, data, size, &offset, &len);
 		/* Do not attempt to save zero length dynamic strings */
 		if (!len)
 			break;
@@ -4435,7 +4453,7 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,
 			arg->bitmask.field = tep_find_any_field(event, arg->bitmask.bitmask);
 		if (!arg->bitmask.field)
 			break;
-		dynamic_offset_field(tep, arg->bitmask.field, data, &offset, &len);
+		dynamic_offset_field(tep, arg->bitmask.field, data, size, &offset, &len);
 		print_bitmask_to_seq(tep, s, format, len_arg,
 				     data + offset, len);
 		break;
@@ -5312,7 +5330,7 @@ static int print_raw_buff_arg(struct trace_seq *s, const char *ptr,
 		return ret;
 	}
 
-	dynamic_offset_field(event->tep, arg->dynarray.field, data,
+	dynamic_offset_field(event->tep, arg->dynarray.field, data, size,
 			     &offset, &arr_len);
 	buf = data + offset;
 
@@ -5339,7 +5357,7 @@ static int is_printable_array(char *p, unsigned int len)
 	return 1;
 }
 
-static void print_field_raw(struct trace_seq *s, void *data,
+static void print_field_raw(struct trace_seq *s, void *data, int size,
 			     struct tep_format_field *field)
 {
 	struct tep_handle *tep = field->event->tep;
@@ -5348,7 +5366,7 @@ static void print_field_raw(struct trace_seq *s, void *data,
 
 	if (field->flags & TEP_FIELD_IS_ARRAY) {
 		if (field->flags & TEP_FIELD_IS_DYNAMIC) {
-			dynamic_offset_field(tep, field, data, &offset, &len);
+			dynamic_offset_field(tep, field, data, size, &offset, &len);
 		} else {
 			offset = field->offset;
 			len = field->size;
@@ -5405,7 +5423,7 @@ static void print_field_raw(struct trace_seq *s, void *data,
 static int print_parse_data(struct tep_print_parse *parse, struct trace_seq *s,
 			    void *data, int size, struct tep_event *event);
 
-void static inline print_field(struct trace_seq *s, void *data,
+void static inline print_field(struct trace_seq *s, void *data, int size,
 				    struct tep_format_field *field,
 				    struct tep_print_parse **parse_ptr)
 {
@@ -5460,17 +5478,18 @@ void static inline print_field(struct trace_seq *s, void *data,
 
  out:
 	/* Not found. */
-	print_field_raw(s, data, field);
+	print_field_raw(s, data, size, field);
 }
 
 void tep_print_field(struct trace_seq *s, void *data,
 		     struct tep_format_field *field)
 {
-	print_field(s, data, field, NULL);
+	/* unsafe to use, should pass in size */
+	print_field(s, data, 4096, field, NULL);
 }
 
 static inline void
-print_selected_fields(struct trace_seq *s, void *data,
+print_selected_fields(struct trace_seq *s, void *data, int size,
 		      struct tep_event *event,
 		      unsigned long long ignore_mask)
 {
@@ -5484,14 +5503,14 @@ print_selected_fields(struct trace_seq *s, void *data,
 			continue;
 
 		trace_seq_printf(s, " %s=", field->name);
-		print_field(s, data, field, &parse);
+		print_field(s, data, size, field, &parse);
 	}
 }
 
 void tep_print_fields(struct trace_seq *s, void *data,
-		      int size __maybe_unused, struct tep_event *event)
+		      int size, struct tep_event *event)
 {
-	print_selected_fields(s, data, event, 0);
+	print_selected_fields(s, data, size, event, 0);
 }
 
 /**
@@ -5505,7 +5524,7 @@ void tep_record_print_fields(struct trace_seq *s,
 			     struct tep_record *record,
 			     struct tep_event *event)
 {
-	print_selected_fields(s, record->data, event, 0);
+	print_selected_fields(s, record->data, record->size, event, 0);
 }
 
 /**
@@ -5523,7 +5542,7 @@ void tep_record_print_selected_fields(struct trace_seq *s,
 {
 	unsigned long long ignore_mask = ~select_mask;
 
-	print_selected_fields(s, record->data, event, ignore_mask);
+	print_selected_fields(s, record->data, record->size, event, ignore_mask);
 }
 
 static int print_function(struct trace_seq *s, const char *format,
utest/traceevent-utest.c +4 -3
index a0d808e5c1a5..0f4f17e655ae 100644
@@ -104,13 +104,14 @@ static struct tep_handle *test_tep;
 static struct trace_seq *test_seq;
 static struct trace_seq seq_storage;
 
-static void parse_dyn_str(const char *dyn_str, void *data)
+static void parse_dyn_str(const char *dyn_str, void *data, int size)
 {
 	struct tep_format_field *field;
 	struct tep_event *event;
 	struct tep_record record;
 
 	record.data = data;
+	record.size = size;
 
 	CU_TEST(tep_parse_format(test_tep, &event,
 				 dyn_str, strlen(dyn_str),
@@ -130,12 +131,12 @@ static void parse_dyn_str(const char *dyn_str, void *data)
 
 static void test_parse_dyn_str_event(void)
 {
-	parse_dyn_str(dyn_str_event, dyn_str_event_data);
+	parse_dyn_str(dyn_str_event, dyn_str_event_data, sizeof(dyn_str_data));
 }
 
 static void test_parse_dyn_str_old_event(void)
 {
-	parse_dyn_str(dyn_str_old_event, dyn_str_old_event_data);
+	parse_dyn_str(dyn_str_old_event, dyn_str_old_event_data, sizeof(dyn_str_old_data));
 }
 
 static int test_suite_destroy(void)
Signature · 1 line
2.33.0
#9 lore raw

PATCH 9/10 libtraceevent: Replace tep_print_field() with tep_print_field_content()

In reply to Steven Rostedt

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

The API tep_print_field() does not let the user define the size of the payload that is being parsed. This can lead to dangerous code if the data itself is untrusted. It could define a string and offset to read other parts of the application.

Add a new tep_print_field_content() that adds the size variable that can be used to prevent accesses outside the data.

Note, there is still many ways to circumvent the size limitation that needs to be fixed, but having an API that will not let you fix it is a problem.

Also fixed at formatting issue with print_field() that had void before the "static inline".

---
 Documentation/libtraceevent-field_print.txt | 10 +++++-----
 Documentation/libtraceevent.txt             |  2 +-
 include/traceevent/event-parse.h            |  8 ++++++--
 src/event-parse.c                           | 21 ++++++++++++++++++++-
 utest/traceevent-utest.c                    |  2 +-
 5 files changed, 33 insertions(+), 10 deletions(-)
Documentation/libtraceevent-field_print.txt +5 -5
index c201c134c42e..05b458b29e8e 100644
@@ -3,7 +3,7 @@ libtraceevent(3)
 
 NAME
 ----
-tep_print_field, tep_print_fields, tep_print_num_field, tep_print_func_field, tep_record_print_fields, tep_record_print_selected_fields -
+tep_print_field_content, tep_print_fields, tep_print_num_field, tep_print_func_field, tep_record_print_fields, tep_record_print_selected_fields -
 Print the field content.
 
 SYNOPSIS
@@ -13,7 +13,7 @@ SYNOPSIS
 *#include <event-parse.h>*
 *#include <trace-seq.h>*
 
-void *tep_print_field*(struct trace_seq pass:[*]_s_, void pass:[*]_data_, struct tep_format_field pass:[*]_field_);
+void *tep_print_field_content*(struct trace_seq pass:[*]_s_, void pass:[*]_data_, int size, struct tep_format_field pass:[*]_field_);
 void *tep_print_fields*(struct trace_seq pass:[*]_s_, void pass:[*]_data_, int _size_, struct tep_event pass:[*]_event_);
 int *tep_print_num_field*(struct trace_seq pass:[*]_s_, const char pass:[*]_fmt_, struct tep_event pass:[*]_event_, const char pass:[*]_name_, struct tep_record pass:[*]_record_, int _err_);
 int *tep_print_func_field*(struct trace_seq pass:[*]_s_, const char pass:[*]_fmt_, struct tep_event pass:[*]_event_, const char pass:[*]_name_, struct tep_record pass:[*]_record_, int _err_);
@@ -25,7 +25,7 @@ DESCRIPTION
 -----------
 These functions print recorded field's data, according to the field's type.
 
-The _tep_print_field()_ function extracts from the recorded raw _data_ value of
+The _tep_print_field_content()_ function extracts from the recorded raw _data_ value of
 the _field_ and prints it into _s_, according to the field type.
 
 The _tep_print_fields()_ prints each field name followed by the record's field
@@ -34,7 +34,7 @@ value according to the field's type:
 --
 "field1_name=field1_value field2_name=field2_value ..."
 --
-It iterates all fields of the _event_, and calls _tep_print_field()_ for each of
+It iterates all fields of the _event_, and calls _tep_print_field_content()_ for each of
 them.
 
 The _tep_print_num_field()_ function prints a numeric field with given format
@@ -83,7 +83,7 @@ void process_record(struct tep_record *record)
 	trace_seq_reset(&seq);
 
 	/* Print the value of "common_pid" */
-	tep_print_field(&seq, record->data, field_pid);
+	tep_print_field_content(&seq, record->data, record->size, field_pid);
 
 	/* Print all fields of the "hrtimer_start" event */
 	tep_print_fields(&seq, record->data, record->size, event);
Documentation/libtraceevent.txt +1 -1
index 01014b7ca719..95465521bcf4 100644
@@ -76,7 +76,7 @@ APIs related to fields from event's format files:
 	int *tep_read_number_field*(struct tep_format_field pass:[*]_field_, const void pass:[*]_data_, unsigned long long pass:[*]_value_);
 
 Event fields printing:
-	void *tep_print_field*(struct trace_seq pass:[*]_s_, void pass:[*]_data_, struct tep_format_field pass:[*]_field_);
+	void *tep_print_field_content*(struct trace_seq pass:[*]_s_, void pass:[*]_data_, int size, struct tep_format_field pass:[*]_field_);
 	void *tep_print_fields*(struct trace_seq pass:[*]_s_, void pass:[*]_data_, int _size_, struct tep_event pass:[*]_event_);
 	int *tep_print_num_field*(struct trace_seq pass:[*]_s_, const char pass:[*]_fmt_, struct tep_event pass:[*]_event_, const char pass:[*]_name_, struct tep_record pass:[*]_record_, int _err_);
 	int *tep_print_func_field*(struct trace_seq pass:[*]_s_, const char pass:[*]_fmt_, struct tep_event pass:[*]_event_, const char pass:[*]_name_, struct tep_record pass:[*]_record_, int _err_);
include/traceevent/event-parse.h +6 -2
index 97234062a057..27cba06f55cb 100644
@@ -544,8 +544,8 @@ struct tep_cmdline *tep_data_pid_from_comm(struct tep_handle *tep, const char *c
 					   struct tep_cmdline *next);
 int tep_cmdline_pid(struct tep_handle *tep, struct tep_cmdline *cmdline);
 
-void tep_print_field(struct trace_seq *s, void *data,
-		     struct tep_format_field *field);
+void tep_print_field_content(struct trace_seq *s, void *data, int size,
+			     struct tep_format_field *field);
 void tep_record_print_fields(struct trace_seq *s,
 			     struct tep_record *record,
 			     struct tep_event *event);
@@ -770,4 +770,8 @@ enum tep_loglevel {
 };
 void tep_set_loglevel(enum tep_loglevel level);
 
+/* DEPRECATED */
+void tep_print_field(struct trace_seq *s, void *data,
+		     struct tep_format_field *field);
+
 #endif /* _PARSE_EVENTS_H */
src/event-parse.c +20 -1
index 62afe86f2c6d..40af92f4295f 100644
@@ -5423,7 +5423,7 @@ static void print_field_raw(struct trace_seq *s, void *data, int size,
 static int print_parse_data(struct tep_print_parse *parse, struct trace_seq *s,
 			    void *data, int size, struct tep_event *event);
 
-void static inline print_field(struct trace_seq *s, void *data, int size,
+static inline void print_field(struct trace_seq *s, void *data, int size,
 				    struct tep_format_field *field,
 				    struct tep_print_parse **parse_ptr)
 {
@@ -5481,6 +5481,25 @@ void static inline print_field(struct trace_seq *s, void *data, int size,
 	print_field_raw(s, data, size, field);
 }
 
+/**
+ * tep_print_field_content - write out the raw content of a field
+ * @s:    The trace_seq to write the content into
+ * @data: The payload to extract the field from.
+ * @size: The size of the payload.
+ * @field: The field to extract
+ *
+ * Use @field to find the field content from within @data and write it
+ * in human readable format into @s.
+ *
+ * It will not write anything on error (s->len will not move)
+ */
+void tep_print_field_content(struct trace_seq *s, void *data, int size,
+			     struct tep_format_field *field)
+{
+	print_field(s, data, size, field, NULL);
+}
+
+/** DEPRECATED **/
 void tep_print_field(struct trace_seq *s, void *data,
 		     struct tep_format_field *field)
 {
utest/traceevent-utest.c +1 -1
index 0f4f17e655ae..99900de50355 100644
@@ -120,7 +120,7 @@ static void parse_dyn_str(const char *dyn_str, void *data, int size)
 	field = tep_find_any_field(event, DYN_STR_FIELD);
 	CU_TEST(field != NULL);
 	trace_seq_reset(test_seq);
-	tep_print_field(test_seq, data, field);
+	tep_print_field_content(test_seq, data, size, field);
 	CU_TEST(strcmp(test_seq->buffer, DYN_STRING) == 0);
 
 	trace_seq_reset(test_seq);
Signature · 1 line
2.33.0
#10 lore raw

PATCH 10/10 libtraceevent: Have print_field_raw() handle old data layout

In reply to Steven Rostedt

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

Old kernels would produce dynamic strings with only a two byte offset. The length of the array was determined by the string length. When parsing, if the field is dynamic with a size of 2, then it is the old dynamic string layout. Set the field to both a string and an array.

---
 src/event-parse.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
src/event-parse.c +10 -0
index 40af92f4295f..9bd605d74b1f 100644
@@ -1828,6 +1828,16 @@ static int event_read_fields(struct tep_event *event, struct tep_format_field **
 		field->size = strtoul(token, NULL, 0);
 		free_token(token);
 
+		/*
+		 * The old data format before dynamic arrays had dynamic
+		 * strings defined with just a 2 byte offset (the length
+		 * is defined by the strlen() of the string. To process them
+		 * correctly, check if the field is dynamic and has a size of
+		 * 2 bytes. All current dynamic events have a size of 4.
+		 */
+		if ((field->flags & TEP_FIELD_IS_DYNAMIC) && field->size == 2)
+			field->flags |= TEP_FIELD_IS_STRING | TEP_FIELD_IS_ARRAY;
+
 		if (read_expected(TEP_EVENT_OP, ";") < 0)
 			goto fail_expect;
 
Signature · 1 line
2.33.0