Skip to contentSkip to conversation

tracing: Add dynamic strings for synthetic events

PATCH In review Tom Zanussi 3 participants 16 messages Oct 4, 2020 – Oct 9, 2020

Subsystems (3)
lore
Share & tools
lore
#1 lore raw

PATCH v4 1/7 tracing: Change STR_VAR_MAX_LEN

In reply to Tom Zanussi

32 is too small for this value, and anyway it makes more sense to use MAX_FILTER_STR_VAL, as this is also the value used for variable-length __strings.

---
 kernel/trace/trace_events_hist.c | 2 ++
 kernel/trace/trace_synth.h       | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
kernel/trace/trace_events_hist.c +2 -0
index 1b2ef6490229..3b22e2122d1a 100644
@@ -1398,6 +1398,8 @@ static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
 
 	n_str = hist_data->n_field_var_str + hist_data->n_save_var_str;
 
+	BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1));
+
 	size = STR_VAR_LEN_MAX;
 
 	for (i = 0; i < n_str; i++) {
kernel/trace/trace_synth.h +1 -1
index ac35c45207c4..5166705d1556 100644
@@ -7,7 +7,7 @@
 #define SYNTH_SYSTEM		"synthetic"
 #define SYNTH_FIELDS_MAX	32
 
-#define STR_VAR_LEN_MAX		32 /* must be multiple of sizeof(u64) */
+#define STR_VAR_LEN_MAX		MAX_FILTER_STR_VAL /* must be multiple of sizeof(u64) */
 
 struct synth_field {
 	char *type;
Signature · 1 line
2.17.1
#2 lore raw

PATCH v4 2/7 tracing: Fix parse_synth_field() error handling

In reply to Tom Zanussi

synth_field_size() returns either a positive size or an error (zero or a negative value). However, the existing code assumes the only error value is 0. It doesn't handle negative error codes, as it assigns directly to field->size (a size_t; unsigned), thereby interpreting the error code as a valid size instead.

Do the test before assignment to field->size.

[ [email protected]: changelog addition, first paragraph above ]

Fixes 4b147936fa50 (tracing: Add support for 'synthetic' events)
---
 kernel/trace/trace_events_synth.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/trace/trace_events_synth.c +4 -2
index a9cd7793f7ea..fa8a99828f41 100644
@@ -465,6 +465,7 @@ static struct synth_field *parse_synth_field(int argc, const char **argv,
 	struct synth_field *field;
 	const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
 	int len, ret = 0;
+	ssize_t size;
 
 	if (field_type[0] == ';')
 		field_type++;
@@ -520,11 +521,12 @@ static struct synth_field *parse_synth_field(int argc, const char **argv,
 			field->type[len - 1] = '\0';
 	}
 
-	field->size = synth_field_size(field->type);
-	if (!field->size) {
+	size = synth_field_size(field->type);
+	if (size <= 0) {
 		ret = -EINVAL;
 		goto free;
 	}
+	field->size = size;
 
 	if (synth_field_is_string(field->type))
 		field->is_string = true;
Signature · 1 line
2.17.1
#9 lore raw

PATCH v4 5/7 tracing: Add README information for synthetic_events file

In reply to Tom Zanussi

Add an entry with a basic description of events/synthetic_events along with a simple example.

---
 kernel/trace/trace.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/trace/trace.c +6 -1
index 3f2533adae72..73fd0e0c0f39 100644
@@ -5249,7 +5249,12 @@ static const char readme_msg[] =
 	"\t        trace(<synthetic_event>,param list)  - generate synthetic event\n"
 	"\t        save(field,...)                      - save current event fields\n"
 #ifdef CONFIG_TRACER_SNAPSHOT
-	"\t        snapshot()                           - snapshot the trace buffer\n"
+	"\t        snapshot()                           - snapshot the trace buffer\n\n"
+#endif
+#ifdef CONFIG_SYNTH_EVENTS
+	"  events/synthetic_events\t- Create/append/remove/show synthetic events\n"
+	"\t  Write into this file to define/undefine new synthetic events.\n"
+	"\t     example: echo 'myevent u64 lat; char name[]' >> synthetic_events\n"
 #endif
 #endif
 ;
Signature · 1 line
2.17.1
#3 lore raw

PATCH v4 3/7 tracing: Save normal string variables

In reply to Tom Zanussi

String variables created as field variables and save variables are already handled properly by having their values copied when set. The same isn't done for normal variables, but needs to be - simply saving a pointer to a string contained in an old event isn't sufficient, since that event's data may quickly become overwritten and therefore a string pointer to it could yield garbage.

This change uses the same mechanism as field variables and simply appends the new strings to the existing per-element field_var_str[] array allocated for that purpose.

Fixes 02205a6752f2 (tracing: Add support for 'field variables')
---
 kernel/trace/trace_events_hist.c | 34 ++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
kernel/trace/trace_events_hist.c +32 -2
index 3b22e2122d1a..812bc5f94b5c 100644
@@ -147,6 +147,8 @@ struct hist_field {
 	 */
 	unsigned int			var_ref_idx;
 	bool                            read_once;
+
+	unsigned int			var_str_idx;
 };
 
 static u64 hist_field_none(struct hist_field *field,
@@ -349,6 +351,7 @@ struct hist_trigger_data {
 	unsigned int			n_keys;
 	unsigned int			n_fields;
 	unsigned int			n_vars;
+	unsigned int			n_var_str;
 	unsigned int			key_size;
 	struct tracing_map_sort_key	sort_keys[TRACING_MAP_SORT_KEYS_MAX];
 	unsigned int			n_sort_keys;
@@ -1396,7 +1399,12 @@ static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
 		}
 	}
 
-	n_str = hist_data->n_field_var_str + hist_data->n_save_var_str;
+	n_str = hist_data->n_field_var_str + hist_data->n_save_var_str +
+		hist_data->n_var_str;
+	if (n_str > SYNTH_FIELDS_MAX) {
+		hist_elt_data_free(elt_data);
+		return -EINVAL;
+	}
 
 	BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1));
 
@@ -3653,6 +3661,7 @@ static int create_var_field(struct hist_trigger_data *hist_data,
 {
 	struct trace_array *tr = hist_data->event_file->tr;
 	unsigned long flags = 0;
+	int ret;
 
 	if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
 		return -EINVAL;
@@ -3667,7 +3676,12 @@ static int create_var_field(struct hist_trigger_data *hist_data,
 	if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
 		return -EINVAL;
 
-	return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
+	ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
+
+	if (hist_data->fields[val_idx]->flags & HIST_FIELD_FL_STRING)
+		hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
+
+	return ret;
 }
 
 static int create_val_fields(struct hist_trigger_data *hist_data,
@@ -4394,6 +4408,22 @@ static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
 		hist_val = hist_field->fn(hist_field, elt, rbe, rec);
 		if (hist_field->flags & HIST_FIELD_FL_VAR) {
 			var_idx = hist_field->var.idx;
+
+			if (hist_field->flags & HIST_FIELD_FL_STRING) {
+				unsigned int str_start, var_str_idx, idx;
+				char *str, *val_str;
+
+				str_start = hist_data->n_field_var_str +
+					hist_data->n_save_var_str;
+				var_str_idx = hist_field->var_str_idx;
+				idx = str_start + var_str_idx;
+
+				str = elt_data->field_var_str[idx];
+				val_str = (char *)(uintptr_t)hist_val;
+				strscpy(str, val_str, STR_VAR_LEN_MAX);
+
+				hist_val = (u64)(uintptr_t)str;
+			}
 			tracing_map_set_var(elt, var_idx, hist_val);
 			continue;
 		}
Signature · 1 line
2.17.1
#4 lore raw

PATCH v4 4/7 tracing: Add support for dynamic strings to synthetic events

In reply to Tom Zanussi

Currently, sythetic events only support static string fields such as:

  # echo 'test_latency u64 lat; char somename[32]' > /sys/kernel/debug/tracing/synthetic_events

Which is fine, but wastes a lot of space in the event.

It also prevents the most commonly-defined strings in the existing trace events e.g. those defined using __string(), from being passed to synthetic events via the trace() action.

With this change, synthetic events with dynamic fields can be defined:

  # echo 'test_latency u64 lat; char somename[]' > /sys/kernel/debug/tracing/synthetic_events

And the trace() action can be used to generate events using either dynamic or static strings:

  # echo 'hist:keys=name:lat=common_timestamp.usecs-$ts0:onmatch(sys.event).test_latency($lat,name)' > /sys/kernel/debug/tracing/events

The synthetic event dynamic strings are implemented in the same way as the existing __data_loc strings and appear as such in the format file.

[ <[email protected]>: added __set_synth_event_print_fmt() changes:

I added the following to make it work with trace-cmd. Dynamic strings must have __get_str() for events in the print_fmt otherwise it can't be parsed correctly. ]

---
 Documentation/trace/events.rst      |  15 +-
 Documentation/trace/histogram.rst   |  18 ++
 kernel/trace/synth_event_gen_test.c |  18 +-
 kernel/trace/trace_events_hist.c    |   9 +
 kernel/trace/trace_events_synth.c   | 248 ++++++++++++++++++++++++----
 kernel/trace/trace_synth.h          |   4 +
 6 files changed, 272 insertions(+), 40 deletions(-)
Documentation/trace/events.rst +13 -2
index f792b1959a33..2a5aa48eff6c 100644
@@ -589,8 +589,19 @@ name::
         { .type = "int",                .name = "my_int_field" },
   };
 
-See synth_field_size() for available types. If field_name contains [n]
-the field is considered to be an array.
+See synth_field_size() for available types.
+
+If field_name contains [n], the field is considered to be a static array.
+
+If field_names contains[] (no subscript), the field is considered to
+be a dynamic array, which will only take as much space in the event as
+is required to hold the array.
+
+Because space for an event is reserved before assigning field values
+to the event, using dynamic arrays implies that the piecewise
+in-kernel API described below can't be used with dynamic arrays.  The
+other non-piecewise in-kernel APIs can, however, be used with dynamic
+arrays.
 
 If the event is created from within a module, a pointer to the module
 must be passed to synth_event_create().  This will ensure that the
Documentation/trace/histogram.rst +18 -0
index 8408670d0328..b573604deabd 100644
@@ -1776,6 +1776,24 @@ consisting of the name of the new event along with one or more
 variables and their types, which can be any valid field type,
 separated by semicolons, to the tracing/synthetic_events file.
 
+See synth_field_size() for available types.
+
+If field_name contains [n], the field is considered to be a static array.
+
+If field_names contains[] (no subscript), the field is considered to
+be a dynamic array, which will only take as much space in the event as
+is required to hold the array.
+
+A string field can be specified using either the static notation:
+
+  char name[32];
+
+Or the dynamic:
+
+  char name[];
+
+The size limit for either is 256.
+
 For instance, the following creates a new event named 'wakeup_latency'
 with 3 fields: lat, pid, and prio.  Each of those fields is simply a
 variable reference to a variable on another event::
kernel/trace/synth_event_gen_test.c +12 -6
index 7d56d621ffea..edd912cd14aa 100644
@@ -242,9 +242,11 @@ static struct synth_field_desc create_synth_test_fields[] = {
 	{ .type = "pid_t",		.name = "next_pid_field" },
 	{ .type = "char[16]",		.name = "next_comm_field" },
 	{ .type = "u64",		.name = "ts_ns" },
+	{ .type = "char[]",		.name = "dynstring_field_1" },
 	{ .type = "u64",		.name = "ts_ms" },
 	{ .type = "unsigned int",	.name = "cpu" },
 	{ .type = "char[64]",		.name = "my_string_field" },
+	{ .type = "char[]",		.name = "dynstring_field_2" },
 	{ .type = "int",		.name = "my_int_field" },
 };
 
@@ -254,7 +256,7 @@ static struct synth_field_desc create_synth_test_fields[] = {
  */
 static int __init test_create_synth_event(void)
 {
-	u64 vals[7];
+	u64 vals[9];
 	int ret;
 
 	/* Create the create_synth_test event with the fields above */
@@ -292,10 +294,12 @@ static int __init test_create_synth_event(void)
 	vals[0] = 777;			/* next_pid_field */
 	vals[1] = (u64)(long)"tiddlywinks";	/* next_comm_field */
 	vals[2] = 1000000;		/* ts_ns */
-	vals[3] = 1000;			/* ts_ms */
-	vals[4] = raw_smp_processor_id(); /* cpu */
-	vals[5] = (u64)(long)"thneed";	/* my_string_field */
-	vals[6] = 398;			/* my_int_field */
+	vals[3] = (u64)(long)"xrayspecs";	/* dynstring_field_1 */
+	vals[4] = 1000;			/* ts_ms */
+	vals[5] = raw_smp_processor_id(); /* cpu */
+	vals[6] = (u64)(long)"thneed";	/* my_string_field */
+	vals[7] = (u64)(long)"kerplunk";	/* dynstring_field_2 */
+	vals[8] = 398;			/* my_int_field */
 
 	/* Now generate a create_synth_test event */
 	ret = synth_event_trace_array(create_synth_test, vals, ARRAY_SIZE(vals));
@@ -422,13 +426,15 @@ static int __init test_trace_synth_event(void)
 	int ret;
 
 	/* Trace some bogus values just for testing */
-	ret = synth_event_trace(create_synth_test, 7,	/* number of values */
+	ret = synth_event_trace(create_synth_test, 9,	/* number of values */
 				(u64)444,		/* next_pid_field */
 				(u64)(long)"clackers",	/* next_comm_field */
 				(u64)1000000,		/* ts_ns */
+				(u64)(long)"viewmaster",/* dynstring_field_1 */
 				(u64)1000,		/* ts_ms */
 				(u64)raw_smp_processor_id(), /* cpu */
 				(u64)(long)"Thneed",	/* my_string_field */
+				(u64)(long)"yoyos",	/* dynstring_field_2 */
 				(u64)999);		/* my_int_field */
 	return ret;
 }
kernel/trace/trace_events_hist.c +9 -0
index 812bc5f94b5c..c74a7d157306 100644
@@ -3289,6 +3289,15 @@ static int check_synth_field(struct synth_event *event,
 
 	field = event->fields[field_pos];
 
+	/*
+	 * A dynamic string synth field can accept static or
+	 * dynamic. A static string synth field can only accept a
+	 * same-sized static string, which is checked for later.
+	 */
+	if (strstr(hist_field->type, "char[") && field->is_string
+	    && field->is_dynamic)
+		return 0;
+
 	if (strcmp(field->type, hist_field->type) != 0) {
 		if (field->size != hist_field->size ||
 		    field->is_signed != hist_field->is_signed)
kernel/trace/trace_events_synth.c +216 -32

428 lines not on this page · raw message

kernel/trace/trace_synth.h +4 -0
index 5166705d1556..6e146b959dcd 100644
@@ -16,6 +16,8 @@ struct synth_field {
 	unsigned int offset;
 	bool is_signed;
 	bool is_string;
+	bool is_dynamic;
+	bool field_pos;
 };
 
 struct synth_event {
@@ -24,6 +26,8 @@ struct synth_event {
 	char					*name;
 	struct synth_field			**fields;
 	unsigned int				n_fields;
+	struct synth_field			**dynamic_fields;
+	unsigned int				n_dynamic_fields;
 	unsigned int				n_u64;
 	struct trace_event_class		class;
 	struct trace_event_call			call;
Signature · 1 line
2.17.1
#11 lore raw

PATCH v4 6/7 selftests/ftrace: Add test case for synthetic event dynamic strings

In reply to Tom Zanussi

Add a selftest that defines and traces a synthetic event that uses a dynamic string event field.

---
 .../trigger-synthetic-event-dynstring.tc      | 31 +++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 tools/testing/selftests/ftrace/test.d/trigger/inter-event/trigger-synthetic-event-dynstring.tc
tools/testing/selftests/ftrace/test.d/trigger/inter-event/trigger-synthetic-event-dynstring.tc new file +31 -0
new file mode 100644
index 000000000000..3d65c856eca3
@@ -0,0 +1,31 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+# description: event trigger - test inter-event histogram trigger trace action with dynamic string param
+# requires: set_event synthetic_events events/sched/sched_process_exec/hist "char name[]' >> synthetic_events":README
+
+fail() { #msg
+    echo $1
+    exit_fail
+}
+
+echo "Test create synthetic event"
+
+echo 'ping_test_latency u64 lat; char filename[]' > synthetic_events
+if [ ! -d events/synthetic/ping_test_latency ]; then
+    fail "Failed to create ping_test_latency synthetic event"
+fi
+
+echo "Test create histogram for synthetic event using trace action and dynamic strings"
+echo "Test histogram dynamic string variables,simple expression support and trace action"
+
+echo 'hist:key=pid:filenamevar=filename:ts0=common_timestamp.usecs' > events/sched/sched_process_exec/trigger
+echo 'hist:key=pid:lat=common_timestamp.usecs-$ts0:onmatch(sched.sched_process_exec).ping_test_latency($lat,$filenamevar) if comm == "ping"' > events/sched/sched_process_exit/trigger
+echo 'hist:keys=filename,lat:sort=filename,lat' > events/synthetic/ping_test_latency/trigger
+
+ping $LOCALHOST -c 5
+
+if ! grep -q "ping" events/synthetic/ping_test_latency/hist; then
+    fail "Failed to create dynamic string trace action inter-event histogram"
+fi
+
+exit 0
Signature · 1 line
2.17.1
#13 lore raw

PATCH v4 7/7 tracing: Change synthetic event string format to limit printed length

In reply to Tom Zanussi

From: Steven Rostedt <[email protected]>

Change the format for printing synthetic field strings to limit the length of the string printed even if it's not correctly terminated.

Description from Steve:

I also added this for a bit of paranoid, and probably should be a separate patch, just to make sure if the string isn't nul terminated, this will keep it from bleeding pass the end of the string.

---
 kernel/trace/trace_events_synth.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/trace_events_synth.c +3 -1
index 24bc6d61aa40..742ce5f62d6d 100644
@@ -234,7 +234,7 @@ static const char *synth_field_fmt(char *type)
 	else if (strcmp(type, "gfp_t") == 0)
 		fmt = "%x";
 	else if (synth_field_is_string(type))
-		fmt = "%s";
+		fmt = "%.*s";
 
 	return fmt;
 }
@@ -303,11 +303,13 @@ static enum print_line_t print_synth_event(struct trace_iterator *iter,
 				str_field = (char *)entry + data_offset;
 
 				trace_seq_printf(s, print_fmt, se->fields[i]->name,
+						 STR_VAR_LEN_MAX,
 						 str_field,
 						 i == se->n_fields - 1 ? "" : " ");
 				n_u64++;
 			} else {
 				trace_seq_printf(s, print_fmt, se->fields[i]->name,
+						 STR_VAR_LEN_MAX,
 						 (char *)&entry->fields[n_u64],
 						 i == se->n_fields - 1 ? "" : " ");
 				n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
Signature · 1 line
2.17.1
#0 lore raw

v4 cover tracing: Add dynamic strings for synthetic events

Hi,

This is v4 of the dynamic string support for synthetic events.

This version adds several patches addressing previous comments - a new testcase for the dynamic synth event strings along with a patch adding a blurb about the synthetic_events file, which should have been there in any case, Steve's patch for preventing string printing overruns (tracing: Change synthetic event string format to limit printed length).

There's also a new patch (tracing: Save normal string variables) that fixes a bug I found when adding the test case. The problem it fixes is only apparent when using the dynamic strings, although it could also happen with static strings. I wasn't able to produce the problem with static strings, in any case.

Other changes include adding the BUILD_BUG_ON() to the (tracing:
Change STR_VAR_MAX_LEN) patch, and folding in Steve's __set_synth_event_print_fmt() adding __get_str() calls for trace-cmd.

Thanks,

Tom

Previous versions text:

Hi,

This is v3 of the dynamic string support for synthetic events.

No code changes since v2, just added Fixes: tag for 'tracing: Fix parse_synth_field() error handling' as suggested by Masami along with his Reviewed-by, and Axel's Tested-bys.

Thanks,

Tom

Hi,

This is v2 of the dynamic string support for synthetic events.

v1 was missing some very important code in the in-kernel API functions that should have reserved room for dynamic strings. This adds it along also some additional test code in the synth_event_gen_test module that tests the dynamic strings for those APIs.

I also added Axel's changelog text to the 'tracing: Fix parse_synth_field() error handling' patch and used ssize_t as he also suggested. Thanks, Axel! I also noticed that the original patch didn't use <= as it should have, so changed that as well.

Neither my command-line tests or the synth_event_gen_test module are showing any problems at this point for me...

Tom

Original v1 text:

This patchset adds support for dynamic strings for synthetic events, as requested by Axel Rasmussen.

Actually, the first two patches should be applied in any case - the first just changes the current max string length and the second fixes a bug I found while testing.

It works fine for my simple test cases, but I still need to do a lot more testing, especially of the in-kernel API parts, which don't affect Axel's use case.

Anyway, Axel, please try it out and send me your Tested-by: if it works for you, and your broken testcase(s) if it doesn't. ;-)

Thanks,

Tom

The following changes since commit fdb46faeab2f3fa2b43a55059b33b8f98b2e1442:

x86: Use tracepoint_enabled() for msr tracepoints instead of open coding it (2020-09-28 10:36:02 -0400)

are available in the Git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/zanussi/linux-trace.git ftrace/synth-dynstring-v4

Steven Rostedt (1):
tracing: Change synthetic event string format to limit printed length

Tom Zanussi (6):
tracing: Change STR_VAR_MAX_LEN
tracing: Fix parse_synth_field() error handling
tracing: Save normal string variables
tracing: Add support for dynamic strings to synthetic events
tracing: Add README information for synthetic_events file selftests/ftrace: Add test case for synthetic event dynamic strings

 Documentation/trace/events.rst                |  15 +-
 Documentation/trace/histogram.rst             |  18 ++
 kernel/trace/synth_event_gen_test.c           |  18 +-
 kernel/trace/trace.c                          |   7 +-
 kernel/trace/trace_events_hist.c              |  45 ++-
 kernel/trace/trace_events_synth.c             | 256 +++++++++++++++---
 kernel/trace/trace_synth.h                    |   6 +-
 .../trigger-synthetic-event-dynstring.tc      |  31 +++
 8 files changed, 350 insertions(+), 46 deletions(-)
 create mode 100644 tools/testing/selftests/ftrace/test.d/trigger/inter-event/trigger-synthetic-event-dynstring.tc
Signature · 1 line
2.17.1
maintainer
#14 lore raw

Re: [PATCH v4 7/7] tracing: Change synthetic event string format to limit printed length

In reply to Tom Zanussi
Quoted 10 lines · On Sun, 4 Oct 2020 17:14:09 -0500 Tom Zanussi <[email protected]> wrote:

From: Steven Rostedt <[email protected]>

Change the format for printing synthetic field strings to limit the length of the string printed even if it's not correctly terminated.

Description from Steve:

I also added this for a bit of paranoid, and probably should be a separate patch, just to make sure if the string isn't nul terminated, this will keep it from bleeding pass the end of the string.

Just FYI. In the future, for something like this, you should still have your own Signed-off-by, as you are sending it (and part of the commit path). You could also add:

[ Need signed-off-by from Steven ]

Which I would have also added as well.

Thanks!

-- Steve

Quoted 31 lines
---
 kernel/trace/trace_events_synth.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/trace_events_synth.c +3 -1
index 24bc6d61aa40..742ce5f62d6d 100644
@@ -234,7 +234,7 @@ static const char *synth_field_fmt(char *type)
 	else if (strcmp(type, "gfp_t") == 0)
 		fmt = "%x";
 	else if (synth_field_is_string(type))
-		fmt = "%s";
+		fmt = "%.*s";
 
 	return fmt;
 }
@@ -303,11 +303,13 @@ static enum print_line_t print_synth_event(struct trace_iterator *iter,
 				str_field = (char *)entry + data_offset;
 
 				trace_seq_printf(s, print_fmt, se->fields[i]->name,
+						 STR_VAR_LEN_MAX,
 						 str_field,
 						 i == se->n_fields - 1 ? "" : " ");
 				n_u64++;
 			} else {
 				trace_seq_printf(s, print_fmt, se->fields[i]->name,
+						 STR_VAR_LEN_MAX,
 						 (char *)&entry->fields[n_u64],
 						 i == se->n_fields - 1 ? "" : " ");
 				n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
#15 lore raw

Re: [PATCH v4 7/7] tracing: Change synthetic event string format to limit printed length

Hi Steve,

Quoted 15 lines · Steven Rostedt
Quoted 11 lines · On Sun, 4 Oct 2020 17:14:09 -0500 Tom Zanussi <[email protected]> wrote:

From: Steven Rostedt <[email protected]>

Change the format for printing synthetic field strings to limit the length of the string printed even if it's not correctly terminated.

Description from Steve:

I also added this for a bit of paranoid, and probably should be a separate patch, just to make sure if the string isn't nul terminated,
this will keep it from bleeding pass the end of the string.

Quoted 9 lines

Just FYI. In the future, for something like this, you should still have
your own Signed-off-by, as you are sending it (and part of the commit path). You could also add:

[ Need signed-off-by from Steven ]

Which I would have also added as well.

Yeah, will do that next time. Thanks for the tip.

Tom

Quoted 46 lines

Thanks!

-- Steve

Quoted 39 lines
---
 kernel/trace/trace_events_synth.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/trace_events_synth.c +2 -1
index 24bc6d61aa40..742ce5f62d6d 100644
@@ -234,7 +234,7 @@ static const char *synth_field_fmt(char *type)
 	else if (strcmp(type, "gfp_t") == 0)
 		fmt = "%x";
 	else if (synth_field_is_string(type))
-		fmt = "%s";
+		fmt = "%.*s";
 
 	return fmt;
 }
@@ -303,11 +303,13 @@ static enum print_line_t
 print_synth_event(struct trace_iterator *iter,
 				str_field = (char *)entry +
 data_offset;
 
 				trace_seq_printf(s, print_fmt, se-
 >fields[i]->name,
+						 STR_VAR_LEN_MAX,
 						 str_field,
 						 i == se->n_fields - 1
 ? "" : " ");
 				n_u64++;
 			} else {
 				trace_seq_printf(s, print_fmt, se-
Quoted 1 line

fields[i]->name,

kernel/trace/trace_events_synth.c +1 -0
+						 STR_VAR_LEN_MAX,
 						 (char *)&entry-
Quoted 1 line

fields[n_u64],

i == se->n_fields - 1
? "" : " ");
n_u64 += STR_VAR_LEN_MAX / sizeof(u64);

maintainer Acked-by on v4 6/7
#12 lore raw

Re: [PATCH v4 6/7] selftests/ftrace: Add test case for synthetic event dynamic strings

In reply to Tom Zanussi
Quoted 4 lines · On Sun, 4 Oct 2020 17:14:08 -0500 Tom Zanussi <[email protected]> wrote:

Add a selftest that defines and traces a synthetic event that uses a dynamic string event field.

This looks good to me.

Thank you!

Quoted 45 lines
---
 .../trigger-synthetic-event-dynstring.tc      | 31 +++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 tools/testing/selftests/ftrace/test.d/trigger/inter-event/trigger-synthetic-event-dynstring.tc
tools/testing/selftests/ftrace/test.d/trigger/inter-event/trigger-synthetic-event-dynstring.tc new file +31 -0
new file mode 100644
index 000000000000..3d65c856eca3
@@ -0,0 +1,31 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+# description: event trigger - test inter-event histogram trigger trace action with dynamic string param
+# requires: set_event synthetic_events events/sched/sched_process_exec/hist "char name[]' >> synthetic_events":README
+
+fail() { #msg
+    echo $1
+    exit_fail
+}
+
+echo "Test create synthetic event"
+
+echo 'ping_test_latency u64 lat; char filename[]' > synthetic_events
+if [ ! -d events/synthetic/ping_test_latency ]; then
+    fail "Failed to create ping_test_latency synthetic event"
+fi
+
+echo "Test create histogram for synthetic event using trace action and dynamic strings"
+echo "Test histogram dynamic string variables,simple expression support and trace action"
+
+echo 'hist:key=pid:filenamevar=filename:ts0=common_timestamp.usecs' > events/sched/sched_process_exec/trigger
+echo 'hist:key=pid:lat=common_timestamp.usecs-$ts0:onmatch(sched.sched_process_exec).ping_test_latency($lat,$filenamevar) if comm == "ping"' > events/sched/sched_process_exit/trigger
+echo 'hist:keys=filename,lat:sort=filename,lat' > events/synthetic/ping_test_latency/trigger
+
+ping $LOCALHOST -c 5
+
+if ! grep -q "ping" events/synthetic/ping_test_latency/hist; then
+    fail "Failed to create dynamic string trace action inter-event histogram"
+fi
+
+exit 0
Signature · 1 line
2.17.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
maintainer Reviewed-by on v4 5/7
#10 lore raw

Re: [PATCH v4 5/7] tracing: Add README information for synthetic_events file

In reply to Tom Zanussi

Hi Tom,

Quoted 4 lines · On Sun, 4 Oct 2020 17:14:07 -0500 Tom Zanussi <[email protected]> wrote:

Add an entry with a basic description of events/synthetic_events along with a simple example.

This looks good to me.

Thank you,

Quoted 25 lines
---
 kernel/trace/trace.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/trace/trace.c +6 -1
index 3f2533adae72..73fd0e0c0f39 100644
@@ -5249,7 +5249,12 @@ static const char readme_msg[] =
 	"\t        trace(<synthetic_event>,param list)  - generate synthetic event\n"
 	"\t        save(field,...)                      - save current event fields\n"
 #ifdef CONFIG_TRACER_SNAPSHOT
-	"\t        snapshot()                           - snapshot the trace buffer\n"
+	"\t        snapshot()                           - snapshot the trace buffer\n\n"
+#endif
+#ifdef CONFIG_SYNTH_EVENTS
+	"  events/synthetic_events\t- Create/append/remove/show synthetic events\n"
+	"\t  Write into this file to define/undefine new synthetic events.\n"
+	"\t     example: echo 'myevent u64 lat; char name[]' >> synthetic_events\n"
 #endif
 #endif
 ;
Signature · 1 line
2.17.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
maintainer
#5 lore raw

Re: [PATCH v4 4/7] tracing: Add support for dynamic strings to synthetic events

In reply to Tom Zanussi

Hi Tom,

Quoted 19 lines · On Sun, 4 Oct 2020 17:14:06 -0500 Tom Zanussi <[email protected]> wrote:

Currently, sythetic events only support static string fields such as:

  # echo 'test_latency u64 lat; char somename[32]' > /sys/kernel/debug/tracing/synthetic_events

Which is fine, but wastes a lot of space in the event.

It also prevents the most commonly-defined strings in the existing trace events e.g. those defined using __string(), from being passed to synthetic events via the trace() action.

With this change, synthetic events with dynamic fields can be defined:

  # echo 'test_latency u64 lat; char somename[]' > /sys/kernel/debug/tracing/synthetic_events

And the trace() action can be used to generate events using either dynamic or static strings:

  # echo 'hist:keys=name:lat=common_timestamp.usecs-$ts0:onmatch(sys.event).test_latency($lat,name)' > /sys/kernel/debug/tracing/events
Quoted 8 lines

The synthetic event dynamic strings are implemented in the same way as the existing __data_loc strings and appear as such in the format file.

[ <[email protected]>: added __set_synth_event_print_fmt() changes:

I added the following to make it work with trace-cmd. Dynamic strings must have __get_str() for events in the print_fmt otherwise it can't be parsed correctly. ]

I confirmed this works, but have some questions;
- It seems no error message when we failed to define with wrong syntax

/sys/kernel/debug/tracing # echo 'myevent char name []' >> synthetic_events
sh: write error: Invalid argument
/sys/kernel/debug/tracing # cat error_log /sys/kernel/debug/tracing #

- what we write and what we see in synthetic_events are different

/sys/kernel/debug/tracing # echo 'myevent char name[]' >> synthetic_events /sys/kernel/debug/tracing # cat synthetic_events
myevent __data_loc char[] name

- And it is not able to re-define with that syntax

/sys/kernel/debug/tracing # echo > synthetic_events /sys/kernel/debug/tracing # echo 'myevent __data_loc char[] name' >> synthetic_events
sh: write error: Invalid argument
/sys/kernel/debug/tracing #

- It seems to accept wrong name for variables

/sys/kernel/debug/tracing # echo 'myevent char name]' >> synthetic_events /sys/kernel/debug/tracing # echo 'myevent2 char name;[]' >> synthetic_events /sys/kernel/debug/tracing # cat synthetic_events
myevent char name]
myevent2 __data_loc char[] name;

Some of those issues are not introduced from this series. I think we'd better fix those before introducing this series so that we can backport it to stable kernels.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
maintainer
#6 lore raw

Re: [PATCH v4 4/7] tracing: Add support for dynamic strings to synthetic events

Quoted 3 lines · On Thu, 8 Oct 2020 18:22:07 +0900 Masami Hiramatsu <[email protected]> wrote:

Some of those issues are not introduced from this series. I think we'd better fix those before introducing this series so that we can backport it to stable kernels.

I agree that these should be fixed, but I'm not sure I want to hold off this series before they are.

-- Steve

#8 lore raw

Re: [PATCH v4 4/7] tracing: Add support for dynamic strings to synthetic events

In reply to Masami Hiramatsu

Hi Masami,

Quoted 45 lines · Masami Hiramatsu

Hi Tom,

Quoted 31 lines · On Sun, 4 Oct 2020 17:14:06 -0500 Tom Zanussi <[email protected]> wrote:

Currently, sythetic events only support static string fields such as:

  # echo 'test_latency u64 lat; char somename[32]' >
/sys/kernel/debug/tracing/synthetic_events

Which is fine, but wastes a lot of space in the event.

It also prevents the most commonly-defined strings in the existing trace events e.g. those defined using __string(), from being passed to
synthetic events via the trace() action.

With this change, synthetic events with dynamic fields can be defined:

  # echo 'test_latency u64 lat; char somename[]' >
/sys/kernel/debug/tracing/synthetic_events

And the trace() action can be used to generate events using either dynamic or static strings:

  # echo 'hist:keys=name:lat=common_timestamp.usecs-
$ts0:onmatch(sys.event).test_latency($lat,name)' >
/sys/kernel/debug/tracing/events

The synthetic event dynamic strings are implemented in the same way as
the existing __data_loc strings and appear as such in the format file.

Quoted 8 lines

[ <[email protected]>: added __set_synth_event_print_fmt() changes:

I added the following to make it work with trace-cmd. Dynamic
strings
must have __get_str() for events in the print_fmt otherwise it
can't be
parsed correctly. ]

Quoted 3 lines

I confirmed this works, but have some questions;
- It seems no error message when we failed to define with wrong syntax

Yes, I need to add an error message for this (as well as others in this file).

Quoted 14 lines

/sys/kernel/debug/tracing # echo 'myevent char name []' >> synthetic_events
sh: write error: Invalid argument /sys/kernel/debug/tracing # cat error_log /sys/kernel/debug/tracing #

- what we write and what we see in synthetic_events are different

/sys/kernel/debug/tracing # echo 'myevent char name[]' >> synthetic_events
/sys/kernel/debug/tracing # cat synthetic_events
myevent __data_loc char[] name

Quoted 8 lines

- And it is not able to re-define with that syntax

/sys/kernel/debug/tracing # echo > synthetic_events /sys/kernel/debug/tracing # echo 'myevent __data_loc char[] name' >> synthetic_events
sh: write error: Invalid argument /sys/kernel/debug/tracing #

Yes, only the format needs to have the __data_loc, but the event description shouldn't.

Quoted 10 lines

- It seems to accept wrong name for variables

/sys/kernel/debug/tracing # echo 'myevent char name]' >> synthetic_events
/sys/kernel/debug/tracing # echo 'myevent2 char name;[]' >> synthetic_events
/sys/kernel/debug/tracing # cat synthetic_events
myevent char name]
myevent2 __data_loc char[] name;

Yes, I think all these other errors boil down to allowing illegal names. Applying your is_good_name() function for probe events to these fields should get rid of them.

Quoted 4 lines

Some of those issues are not introduced from this series. I think we'd better fix those before introducing this series so that we can backport it to stable kernels.

I should have patches addressing all of these shortly, tomorrow at the latest.

Thanks,

Tom

Quoted 3 lines

Thank you,

maintainer
#7 lore raw

Re: [PATCH v4 4/7] tracing: Add support for dynamic strings to synthetic events

In reply to Steven Rostedt
Quoted 9 lines · On Thu, 8 Oct 2020 15:21:50 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 3 lines · On Thu, 8 Oct 2020 18:22:07 +0900 Masami Hiramatsu <[email protected]> wrote:

Some of those issues are not introduced from this series. I think we'd better fix those before introducing this series so that we can backport it to stable kernels.

I agree that these should be fixed, but I'm not sure I want to hold off this series before they are.

OK, I think the last event-name issue will be separated from this patch, but others are directly related this patch. So those need to be fixed in this series.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>