diff mbox series

[1/2] trace-cmd list: Use tracefs to help find events for -e

Message ID 20210416213018.3890036-2-rostedt@goodmis.org (mailing list archive)
State Superseded
Headers show
Series trace-cmd list: Include ftrace in event listings | expand

Commit Message

Steven Rostedt April 16, 2021, 9:30 p.m. UTC
From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

When the user specifies -e and wants to search for events, use the tracefs
helper functions tracefs_event_systems() and tracefs_system_events() to
find the matching events.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 lib/trace-cmd/trace-input.c |   6 +-
 tracecmd/trace-list.c       | 136 ++++++++++++++++++++++++++++++++++--
 2 files changed, 135 insertions(+), 7 deletions(-)

Comments

Tzvetomir Stoyanov (VMware) April 19, 2021, 5:10 a.m. UTC | #1
On Sat, Apr 17, 2021 at 1:08 AM Steven Rostedt <rostedt@goodmis.org> wrote:
>
> From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>
>
> When the user specifies -e and wants to search for events, use the tracefs
> helper functions tracefs_event_systems() and tracefs_system_events() to
> find the matching events.
>
> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
> ---
>  lib/trace-cmd/trace-input.c |   6 +-
>  tracecmd/trace-list.c       | 136 ++++++++++++++++++++++++++++++++++--
>  2 files changed, 135 insertions(+), 7 deletions(-)
>
> diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c
> index 991abd5f..a540ab24 100644
> --- a/lib/trace-cmd/trace-input.c
> +++ b/lib/trace-cmd/trace-input.c
> @@ -1234,9 +1234,11 @@ static unsigned long long timestamp_calc(unsigned long long ts, int cpu,
>                         ts -= handle->tsc_calc.offset;
>                         ts = mul_u64_u32_shr(ts, handle->tsc_calc.mult, handle->tsc_calc.shift);
>                 } else {
> -                       tracecmd_warning("Timestamp $llu is before the initial offset %llu, set it to 0",
> +                       tracecmd_warning("Timestamp %llu is before the initial offset %llu\n"
> +                                        "\tSetting offset to 0",
>                                          ts, handle->tsc_calc.offset);
> -                       ts = 0;
> +                       handle->tsc_calc.offset = 0;
> +                       ts = mul_u64_u32_shr(ts, handle->tsc_calc.mult, handle->tsc_calc.shift);

That looks like from a different patch? I'll send v3 from my patch set
with these fixes.

>                 }
>         }
>
> diff --git a/tracecmd/trace-list.c b/tracecmd/trace-list.c
> index 63216b43..84ee573b 100644
> --- a/tracecmd/trace-list.c
> +++ b/tracecmd/trace-list.c
> @@ -105,6 +105,122 @@ static void process_file_re(process_file_func func,
>         regfree(&reg);
>  }
>
> +static void show_event(process_file_func func, const char *system, const char *event)
> +{
> +       char *buf;
> +       int ret;
> +
> +       ret = asprintf(&buf, "%s:%s", system, event);
> +       if (ret < 0)
> +               die("Can not allocate event");
> +       func(buf, strlen(buf));
> +       free(buf);
> +}
> +
> +static void show_system(process_file_func func, const char *system)
> +{
> +       char **events;
> +       int e;
> +
> +       events = tracefs_system_events(NULL, system);
> +       if (!events) /* die? */
> +               return;
> +
> +       for (e = 0; events[e]; e++)
> +               show_event(func, system, events[e]);
> +}
> +
> +static void show_event_systems(process_file_func func, char **systems)
> +{
> +       int s;
> +
> +       for (s = 0; systems[s]; s++)
> +               show_system(func, systems[s]);
> +}
> +
> +
> +static void process_events(process_file_func func, const char *re)
> +{
> +       regex_t system_reg;
> +       regex_t event_reg;
> +       char *str;
> +       size_t l = strlen(re);
> +       bool just_systems = true;
> +       char **systems;
> +       char **events;
> +       char *system;
> +       char *event;
> +       int s, e;
> +
> +       systems = tracefs_event_systems(NULL);
> +       if (!systems)
> +               return process_file_re(func, "available_events", re);
> +
> +       if (!re || l == 0) {
> +               show_event_systems(func, systems);
> +               return;
> +       }
> +
> +       str = strdup(re);
> +       if (!str)
> +               die("Can not allocate momory for regex");
> +
> +       system = strtok(str, ":");
> +       event = strtok(NULL, "");
> +
> +       if (regcomp(&system_reg, system, REG_ICASE|REG_NOSUB))
> +               die("invalid regex '%s'", system);
> +
> +       if (event) {
> +               if (regcomp(&event_reg, event, REG_ICASE|REG_NOSUB))
> +                       die("invalid regex '%s'", event);
> +       } else {
> +               /*
> +                * If the regex ends with ":", then event would be null,
> +                * but we do not want to match events.
> +                */
> +               if (re[l-1] != ':')
> +                       just_systems = false;
> +       }
> +       free(str);
> +
> +       for (s = 0; systems[s]; s++) {
> +
> +               if (regexec(&system_reg, systems[s], 0, NULL, 0) == 0) {
> +                       if (!event) {
> +                               show_system(func, systems[s]);
> +                               continue;
> +                       }
> +                       events = tracefs_system_events(NULL, systems[s]);
> +                       if (!events) /* die? */
> +                               continue;
> +                       for (e = 0; events[e]; e++) {
> +                               if (regexec(&event_reg, events[e], 0, NULL, 0) == 0)
> +                                       show_event(func, systems[s], events[e]);
> +                       }
> +                       tracefs_list_free(events);
> +                       continue;
> +               }
> +               if (just_systems)
> +                       continue;
> +
> +               events = tracefs_system_events(NULL, systems[s]);
> +               if (!events) /* die? */
> +                       continue;
> +
> +               for (e = 0; events[e]; e++) {
> +                       if (regexec(&system_reg, events[e], 0, NULL, 0) == 0)
> +                               show_event(func, systems[s], events[e]);
> +               }
> +               tracefs_list_free(events);
> +       }
> +       tracefs_list_free(systems);
> +
> +       regfree(&system_reg);
> +       if (event)
> +               regfree(&event_reg);
> +}
> +
>  static int show_file_write(char *buf, int len)
>  {
>         return fwrite(buf, 1, len, stdout);
> @@ -210,24 +326,34 @@ static int event_format_write(char *fbuf, int len)
>         return 0;
>  }
>
> +static int event_name(char *buf, int len)
> +{
> +       printf("%s\n", buf);
> +
> +       return 0;
> +}
> +
>
>  static void show_event_filter_re(const char *re)
>  {
> -       process_file_re(event_filter_write, "available_events", re);
> +       process_events(event_filter_write, re);
>  }
>
>
>  static void show_event_trigger_re(const char *re)
>  {
> -       process_file_re(event_trigger_write, "available_events", re);
> +       process_events(event_trigger_write, re);
>  }
>
> -
>  static void show_event_format_re(const char *re)
>  {
> -       process_file_re(event_format_write, "available_events", re);
> +       process_events(event_format_write, re);
>  }
>
> +static void show_event_names_re(const char *re)
> +{
> +       process_events(event_name, re);
> +}
>
>  static void show_events(const char *eventre, int flags)
>  {
> @@ -244,7 +370,7 @@ static void show_events(const char *eventre, int flags)
>                 else if (flags & SHOW_EVENT_TRIGGER)
>                         show_event_trigger_re(eventre);
>                 else
> -                       show_file_re("available_events", eventre);
> +                       show_event_names_re(eventre);
>         } else
>                 show_file("available_events");
>  }
> --
> 2.29.2
>


--
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
Steven Rostedt April 22, 2021, 9:14 p.m. UTC | #2
On Mon, 19 Apr 2021 08:10:57 +0300
Tzvetomir Stoyanov <tz.stoyanov@gmail.com> wrote:

> > --- a/lib/trace-cmd/trace-input.c
> > +++ b/lib/trace-cmd/trace-input.c
> > @@ -1234,9 +1234,11 @@ static unsigned long long timestamp_calc(unsigned long long ts, int cpu,
> >                         ts -= handle->tsc_calc.offset;
> >                         ts = mul_u64_u32_shr(ts, handle->tsc_calc.mult, handle->tsc_calc.shift);
> >                 } else {
> > -                       tracecmd_warning("Timestamp $llu is before the initial offset %llu, set it to 0",
> > +                       tracecmd_warning("Timestamp %llu is before the initial offset %llu\n"
> > +                                        "\tSetting offset to 0",
> >                                          ts, handle->tsc_calc.offset);
> > -                       ts = 0;
> > +                       handle->tsc_calc.offset = 0;
> > +                       ts = mul_u64_u32_shr(ts, handle->tsc_calc.mult, handle->tsc_calc.shift);  
> 
> That looks like from a different patch? I'll send v3 from my patch set
> with these fixes.
> 
> >                 }
> >         }

Yes, that was leftover from another change that accidentally got committed
into this one. Thanks for noticing. I'll be sending out a v2.

-- Steve
diff mbox series

Patch

diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c
index 991abd5f..a540ab24 100644
--- a/lib/trace-cmd/trace-input.c
+++ b/lib/trace-cmd/trace-input.c
@@ -1234,9 +1234,11 @@  static unsigned long long timestamp_calc(unsigned long long ts, int cpu,
 			ts -= handle->tsc_calc.offset;
 			ts = mul_u64_u32_shr(ts, handle->tsc_calc.mult, handle->tsc_calc.shift);
 		} else {
-			tracecmd_warning("Timestamp $llu is before the initial offset %llu, set it to 0",
+			tracecmd_warning("Timestamp %llu is before the initial offset %llu\n"
+					 "\tSetting offset to 0",
 					 ts, handle->tsc_calc.offset);
-			ts = 0;
+			handle->tsc_calc.offset = 0;
+			ts = mul_u64_u32_shr(ts, handle->tsc_calc.mult, handle->tsc_calc.shift);
 		}
 	}
 
diff --git a/tracecmd/trace-list.c b/tracecmd/trace-list.c
index 63216b43..84ee573b 100644
--- a/tracecmd/trace-list.c
+++ b/tracecmd/trace-list.c
@@ -105,6 +105,122 @@  static void process_file_re(process_file_func func,
 	regfree(&reg);
 }
 
+static void show_event(process_file_func func, const char *system, const char *event)
+{
+	char *buf;
+	int ret;
+
+	ret = asprintf(&buf, "%s:%s", system, event);
+	if (ret < 0)
+		die("Can not allocate event");
+	func(buf, strlen(buf));
+	free(buf);
+}
+
+static void show_system(process_file_func func, const char *system)
+{
+	char **events;
+	int e;
+
+	events = tracefs_system_events(NULL, system);
+	if (!events) /* die? */
+		return;
+
+	for (e = 0; events[e]; e++)
+		show_event(func, system, events[e]);
+}
+
+static void show_event_systems(process_file_func func, char **systems)
+{
+	int s;
+
+	for (s = 0; systems[s]; s++)
+		show_system(func, systems[s]);
+}
+
+
+static void process_events(process_file_func func, const char *re)
+{
+	regex_t system_reg;
+	regex_t event_reg;
+	char *str;
+	size_t l = strlen(re);
+	bool just_systems = true;
+	char **systems;
+	char **events;
+	char *system;
+	char *event;
+	int s, e;
+
+	systems = tracefs_event_systems(NULL);
+	if (!systems)
+		return process_file_re(func, "available_events", re);
+
+	if (!re || l == 0) {
+		show_event_systems(func, systems);
+		return;
+	}
+
+	str = strdup(re);
+	if (!str)
+		die("Can not allocate momory for regex");
+
+	system = strtok(str, ":");
+	event = strtok(NULL, "");
+
+	if (regcomp(&system_reg, system, REG_ICASE|REG_NOSUB))
+		die("invalid regex '%s'", system);
+
+	if (event) {
+		if (regcomp(&event_reg, event, REG_ICASE|REG_NOSUB))
+			die("invalid regex '%s'", event);
+	} else {
+		/*
+		 * If the regex ends with ":", then event would be null,
+		 * but we do not want to match events.
+		 */
+		if (re[l-1] != ':')
+			just_systems = false;
+	}
+	free(str);
+
+	for (s = 0; systems[s]; s++) {
+
+		if (regexec(&system_reg, systems[s], 0, NULL, 0) == 0) {
+			if (!event) {
+				show_system(func, systems[s]);
+				continue;
+			}
+			events = tracefs_system_events(NULL, systems[s]);
+			if (!events) /* die? */
+				continue;
+			for (e = 0; events[e]; e++) {
+				if (regexec(&event_reg, events[e], 0, NULL, 0) == 0)
+					show_event(func, systems[s], events[e]);
+			}
+			tracefs_list_free(events);
+			continue;
+		}
+		if (just_systems)
+			continue;
+
+		events = tracefs_system_events(NULL, systems[s]);
+		if (!events) /* die? */
+			continue;
+
+		for (e = 0; events[e]; e++) {
+			if (regexec(&system_reg, events[e], 0, NULL, 0) == 0)
+				show_event(func, systems[s], events[e]);
+		}
+		tracefs_list_free(events);
+	}
+	tracefs_list_free(systems);
+
+	regfree(&system_reg);
+	if (event)
+		regfree(&event_reg);
+}
+
 static int show_file_write(char *buf, int len)
 {
 	return fwrite(buf, 1, len, stdout);
@@ -210,24 +326,34 @@  static int event_format_write(char *fbuf, int len)
 	return 0;
 }
 
+static int event_name(char *buf, int len)
+{
+	printf("%s\n", buf);
+
+	return 0;
+}
+
 
 static void show_event_filter_re(const char *re)
 {
-	process_file_re(event_filter_write, "available_events", re);
+	process_events(event_filter_write, re);
 }
 
 
 static void show_event_trigger_re(const char *re)
 {
-	process_file_re(event_trigger_write, "available_events", re);
+	process_events(event_trigger_write, re);
 }
 
-
 static void show_event_format_re(const char *re)
 {
-	process_file_re(event_format_write, "available_events", re);
+	process_events(event_format_write, re);
 }
 
+static void show_event_names_re(const char *re)
+{
+	process_events(event_name, re);
+}
 
 static void show_events(const char *eventre, int flags)
 {
@@ -244,7 +370,7 @@  static void show_events(const char *eventre, int flags)
 		else if (flags & SHOW_EVENT_TRIGGER)
 			show_event_trigger_re(eventre);
 		else
-			show_file_re("available_events", eventre);
+			show_event_names_re(eventre);
 	} else
 		show_file("available_events");
 }