[1/4] perf trace-event-info: Add tracepoint_id_to_name() helper

Message ID 20230419122912.73746-2-yangjihong1@huawei.com
State New
Headers
Series perf tools: Add printing perf_event_attr `config` and `id` symbol in perf_event_attr__fprintf() |

Commit Message

Yang Jihong April 19, 2023, 12:29 p.m. UTC
  Add tracepoint_id_to_name() helper to search for the trace events directory
by given event id and return the corresponding tracepoint.

Signed-off-by: Yang Jihong <yangjihong1@huawei.com>
---
 tools/perf/util/trace-event-info.c | 13 +++++++++++++
 tools/perf/util/trace-event.h      |  6 ++++++
 2 files changed, 19 insertions(+)
  

Comments

Adrian Hunter May 10, 2023, 7:48 a.m. UTC | #1
On 19/04/23 15:29, Yang Jihong wrote:
> Add tracepoint_id_to_name() helper to search for the trace events directory
> by given event id and return the corresponding tracepoint.
> 
> Signed-off-by: Yang Jihong <yangjihong1@huawei.com>
> ---
>  tools/perf/util/trace-event-info.c | 13 +++++++++++++
>  tools/perf/util/trace-event.h      |  6 ++++++
>  2 files changed, 19 insertions(+)
> 
> diff --git a/tools/perf/util/trace-event-info.c b/tools/perf/util/trace-event-info.c
> index c24b3a15e319..79907caaf0ca 100644
> --- a/tools/perf/util/trace-event-info.c
> +++ b/tools/perf/util/trace-event-info.c
> @@ -466,6 +466,19 @@ static struct tracepoint_path *tracepoint_id_to_path(u64 config)
>  	return NULL;
>  }
>  
> +char *tracepoint_id_to_name(u64 config)
> +{
> +	char buf[MAX_EVENT_LENGTH];
> +	struct tracepoint_path *path = NULL;

Initializer should be tracepoint_id_to_path(config) i.e.

	struct tracepoint_path *path = tracepoint_id_to_path(config);

> +
> +	path = tracepoint_id_to_path(config);
> +	if (path == NULL)
> +		return NULL;
> +
> +	snprintf(buf, sizeof(buf), "%s:%s", path->system, path->name);

asprintf() can be used then buf and strdup() are not needed.

> +	return strdup(buf);
> +}
> +
>  static struct tracepoint_path *tracepoint_name_to_path(const char *name)
>  {
>  	struct tracepoint_path *path = zalloc(sizeof(*path));
> diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
> index a0cff184b1cd..a69ee29419f3 100644
> --- a/tools/perf/util/trace-event.h
> +++ b/tools/perf/util/trace-event.h
> @@ -62,6 +62,12 @@ unsigned long long eval_flag(const char *flag);
>  
>  int read_tracing_data(int fd, struct list_head *pattrs);
>  
> +/*
> + * Return the tracepoint name in the format "subsystem:event_name",
> + * callers should free the returned string.
> + */
> +char *tracepoint_id_to_name(u64 config);
> +
>  struct tracing_data {
>  	/* size is only valid if temp is 'true' */
>  	ssize_t size;
  
Yang Jihong May 10, 2023, 8:09 a.m. UTC | #2
Hello,

On 2023/5/10 15:48, Adrian Hunter wrote:
> On 19/04/23 15:29, Yang Jihong wrote:
>> Add tracepoint_id_to_name() helper to search for the trace events directory
>> by given event id and return the corresponding tracepoint.
>>
>> Signed-off-by: Yang Jihong <yangjihong1@huawei.com>
>> ---
>>   tools/perf/util/trace-event-info.c | 13 +++++++++++++
>>   tools/perf/util/trace-event.h      |  6 ++++++
>>   2 files changed, 19 insertions(+)
>>
>> diff --git a/tools/perf/util/trace-event-info.c b/tools/perf/util/trace-event-info.c
>> index c24b3a15e319..79907caaf0ca 100644
>> --- a/tools/perf/util/trace-event-info.c
>> +++ b/tools/perf/util/trace-event-info.c
>> @@ -466,6 +466,19 @@ static struct tracepoint_path *tracepoint_id_to_path(u64 config)
>>   	return NULL;
>>   }
>>   
>> +char *tracepoint_id_to_name(u64 config)
>> +{
>> +	char buf[MAX_EVENT_LENGTH];
>> +	struct tracepoint_path *path = NULL;
> 
> Initializer should be tracepoint_id_to_path(config) i.e.
> 
OK, will fix in next version.

> 	struct tracepoint_path *path = tracepoint_id_to_path(config);
> 
>> +
>> +	path = tracepoint_id_to_path(config);
>> +	if (path == NULL)
>> +		return NULL;
>> +
>> +	snprintf(buf, sizeof(buf), "%s:%s", path->system, path->name);
> 
> asprintf() can be used then buf and strdup() are not needed.
> 
OK, will fix in next version.

Thanks for correcting.

Thanks,
Yang
  

Patch

diff --git a/tools/perf/util/trace-event-info.c b/tools/perf/util/trace-event-info.c
index c24b3a15e319..79907caaf0ca 100644
--- a/tools/perf/util/trace-event-info.c
+++ b/tools/perf/util/trace-event-info.c
@@ -466,6 +466,19 @@  static struct tracepoint_path *tracepoint_id_to_path(u64 config)
 	return NULL;
 }
 
+char *tracepoint_id_to_name(u64 config)
+{
+	char buf[MAX_EVENT_LENGTH];
+	struct tracepoint_path *path = NULL;
+
+	path = tracepoint_id_to_path(config);
+	if (path == NULL)
+		return NULL;
+
+	snprintf(buf, sizeof(buf), "%s:%s", path->system, path->name);
+	return strdup(buf);
+}
+
 static struct tracepoint_path *tracepoint_name_to_path(const char *name)
 {
 	struct tracepoint_path *path = zalloc(sizeof(*path));
diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
index a0cff184b1cd..a69ee29419f3 100644
--- a/tools/perf/util/trace-event.h
+++ b/tools/perf/util/trace-event.h
@@ -62,6 +62,12 @@  unsigned long long eval_flag(const char *flag);
 
 int read_tracing_data(int fd, struct list_head *pattrs);
 
+/*
+ * Return the tracepoint name in the format "subsystem:event_name",
+ * callers should free the returned string.
+ */
+char *tracepoint_id_to_name(u64 config);
+
 struct tracing_data {
 	/* size is only valid if temp is 'true' */
 	ssize_t size;