Skip to content

Commit 8dd2a13

Browse files
olsajiriacmel
authored andcommitted
perf evsel: Propagate error info from tp_format
Propagate error info from tp_format via ERR_PTR to get it all the way down to the parse-event.c tracepoint adding routines. Following functions now return pointer with encoded error: - tp_format - trace_event__tp_format - perf_evsel__newtp_idx - perf_evsel__newtp This affects several other places in perf, that cannot use pointer check anymore, but must utilize the err.h interface, when getting error information from above functions list. Signed-off-by: Jiri Olsa <[email protected]> Cc: David Ahern <[email protected]> Cc: Matt Fleming <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Raphael Beamonte <[email protected]> Link: http://lkml.kernel.org/r/[email protected] [ Add two missing ERR_PTR() and one IS_ERR() ] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent e2f9f8e commit 8dd2a13

File tree

11 files changed

+60
-24
lines changed

11 files changed

+60
-24
lines changed

tools/perf/builtin-trace.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <stdlib.h>
3939
#include <sys/mman.h>
4040
#include <linux/futex.h>
41+
#include <linux/err.h>
4142

4243
/* For older distros: */
4344
#ifndef MAP_STACK
@@ -245,13 +246,14 @@ static struct perf_evsel *perf_evsel__syscall_newtp(const char *direction, void
245246
struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
246247

247248
/* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
248-
if (evsel == NULL)
249+
if (IS_ERR(evsel))
249250
evsel = perf_evsel__newtp("syscalls", direction);
250251

251-
if (evsel) {
252-
if (perf_evsel__init_syscall_tp(evsel, handler))
253-
goto out_delete;
254-
}
252+
if (IS_ERR(evsel))
253+
return NULL;
254+
255+
if (perf_evsel__init_syscall_tp(evsel, handler))
256+
goto out_delete;
255257

256258
return evsel;
257259

@@ -1705,12 +1707,12 @@ static int trace__read_syscall_info(struct trace *trace, int id)
17051707
snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
17061708
sc->tp_format = trace_event__tp_format("syscalls", tp_name);
17071709

1708-
if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
1710+
if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
17091711
snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
17101712
sc->tp_format = trace_event__tp_format("syscalls", tp_name);
17111713
}
17121714

1713-
if (sc->tp_format == NULL)
1715+
if (IS_ERR(sc->tp_format))
17141716
return -1;
17151717

17161718
sc->args = sc->tp_format->format.fields;
@@ -2390,7 +2392,8 @@ static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
23902392
static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
23912393
{
23922394
struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
2393-
if (evsel == NULL)
2395+
2396+
if (IS_ERR(evsel))
23942397
return false;
23952398

23962399
if (perf_evsel__field(evsel, "pathname") == NULL) {

tools/perf/tests/evsel-tp-sched.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <linux/err.h>
12
#include <traceevent/event-parse.h>
23
#include "evsel.h"
34
#include "tests.h"
@@ -36,8 +37,8 @@ int test__perf_evsel__tp_sched_test(void)
3637
struct perf_evsel *evsel = perf_evsel__newtp("sched", "sched_switch");
3738
int ret = 0;
3839

39-
if (evsel == NULL) {
40-
pr_debug("perf_evsel__new\n");
40+
if (IS_ERR(evsel)) {
41+
pr_debug("perf_evsel__newtp failed with %ld\n", PTR_ERR(evsel));
4142
return -1;
4243
}
4344

@@ -66,6 +67,11 @@ int test__perf_evsel__tp_sched_test(void)
6667

6768
evsel = perf_evsel__newtp("sched", "sched_wakeup");
6869

70+
if (IS_ERR(evsel)) {
71+
pr_debug("perf_evsel__newtp failed with %ld\n", PTR_ERR(evsel));
72+
return -1;
73+
}
74+
6975
if (perf_evsel__test_field(evsel, "comm", 16, true))
7076
ret = -1;
7177

tools/perf/tests/mmap-basic.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "thread_map.h"
44
#include "cpumap.h"
55
#include "tests.h"
6+
#include <linux/err.h>
67

78
/*
89
* This test will generate random numbers of calls to some getpid syscalls,
@@ -65,7 +66,7 @@ int test__basic_mmap(void)
6566

6667
snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
6768
evsels[i] = perf_evsel__newtp("syscalls", name);
68-
if (evsels[i] == NULL) {
69+
if (IS_ERR(evsels[i])) {
6970
pr_debug("perf_evsel__new\n");
7071
goto out_delete_evlist;
7172
}

tools/perf/tests/openat-syscall-all-cpus.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <api/fs/fs.h>
2+
#include <linux/err.h>
23
#include "evsel.h"
34
#include "tests.h"
45
#include "thread_map.h"
@@ -31,7 +32,7 @@ int test__openat_syscall_event_on_all_cpus(void)
3132
CPU_ZERO(&cpu_set);
3233

3334
evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
34-
if (evsel == NULL) {
35+
if (IS_ERR(evsel)) {
3536
tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
3637
pr_err("%s\n", errbuf);
3738
goto out_thread_map_delete;

tools/perf/tests/openat-syscall-tp-fields.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <linux/err.h>
12
#include "perf.h"
23
#include "evlist.h"
34
#include "evsel.h"
@@ -30,7 +31,7 @@ int test__syscall_openat_tp_fields(void)
3031
}
3132

3233
evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
33-
if (evsel == NULL) {
34+
if (IS_ERR(evsel)) {
3435
pr_debug("%s: perf_evsel__newtp\n", __func__);
3536
goto out_delete_evlist;
3637
}

tools/perf/tests/openat-syscall.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <api/fs/tracing_path.h>
2+
#include <linux/err.h>
23
#include "thread_map.h"
34
#include "evsel.h"
45
#include "debug.h"
@@ -19,7 +20,7 @@ int test__openat_syscall_event(void)
1920
}
2021

2122
evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
22-
if (evsel == NULL) {
23+
if (IS_ERR(evsel)) {
2324
tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
2425
pr_err("%s\n", errbuf);
2526
goto out_thread_map_delete;

tools/perf/util/evlist.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/bitops.h>
2626
#include <linux/hash.h>
2727
#include <linux/log2.h>
28+
#include <linux/err.h>
2829

2930
static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx);
3031
static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx);
@@ -265,7 +266,7 @@ int perf_evlist__add_newtp(struct perf_evlist *evlist,
265266
{
266267
struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
267268

268-
if (evsel == NULL)
269+
if (IS_ERR(evsel))
269270
return -1;
270271

271272
evsel->handler = handler;

tools/perf/util/evsel.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <traceevent/event-parse.h>
1414
#include <linux/hw_breakpoint.h>
1515
#include <linux/perf_event.h>
16+
#include <linux/err.h>
1617
#include <sys/resource.h>
1718
#include "asm/bug.h"
1819
#include "callchain.h"
@@ -225,11 +226,17 @@ struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
225226
return evsel;
226227
}
227228

229+
/*
230+
* Returns pointer with encoded error via <linux/err.h> interface.
231+
*/
228232
struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
229233
{
230234
struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
235+
int err = -ENOMEM;
231236

232-
if (evsel != NULL) {
237+
if (evsel == NULL) {
238+
goto out_err;
239+
} else {
233240
struct perf_event_attr attr = {
234241
.type = PERF_TYPE_TRACEPOINT,
235242
.sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
@@ -240,8 +247,10 @@ struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int
240247
goto out_free;
241248

242249
evsel->tp_format = trace_event__tp_format(sys, name);
243-
if (evsel->tp_format == NULL)
250+
if (IS_ERR(evsel->tp_format)) {
251+
err = PTR_ERR(evsel->tp_format);
244252
goto out_free;
253+
}
245254

246255
event_attr_init(&attr);
247256
attr.config = evsel->tp_format->id;
@@ -254,7 +263,8 @@ struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int
254263
out_free:
255264
zfree(&evsel->name);
256265
free(evsel);
257-
return NULL;
266+
out_err:
267+
return ERR_PTR(err);
258268
}
259269

260270
const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {

tools/perf/util/evsel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ static inline struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr)
160160

161161
struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx);
162162

163+
/*
164+
* Returns pointer with encoded error via <linux/err.h> interface.
165+
*/
163166
static inline struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name)
164167
{
165168
return perf_evsel__newtp_idx(sys, name, 0);

tools/perf/util/parse-events.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <linux/hw_breakpoint.h>
2+
#include <linux/err.h>
23
#include "util.h"
34
#include "../perf.h"
45
#include "evlist.h"
@@ -393,11 +394,10 @@ static int add_tracepoint(struct list_head *list, int *idx,
393394
struct perf_evsel *evsel;
394395

395396
evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
396-
if (!evsel)
397-
return -ENOMEM;
397+
if (IS_ERR(evsel))
398+
return PTR_ERR(evsel);
398399

399400
list_add_tail(&evsel->node, list);
400-
401401
return 0;
402402
}
403403

tools/perf/util/trace-event.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <sys/stat.h>
88
#include <fcntl.h>
99
#include <linux/kernel.h>
10+
#include <linux/err.h>
1011
#include <traceevent/event-parse.h>
1112
#include <api/fs/tracing_path.h>
1213
#include "trace-event.h"
@@ -66,6 +67,9 @@ void trace_event__cleanup(struct trace_event *t)
6667
pevent_free(t->pevent);
6768
}
6869

70+
/*
71+
* Returns pointer with encoded error via <linux/err.h> interface.
72+
*/
6973
static struct event_format*
7074
tp_format(const char *sys, const char *name)
7175
{
@@ -74,24 +78,29 @@ tp_format(const char *sys, const char *name)
7478
char path[PATH_MAX];
7579
size_t size;
7680
char *data;
81+
int err;
7782

7883
scnprintf(path, PATH_MAX, "%s/%s/%s/format",
7984
tracing_events_path, sys, name);
8085

81-
if (filename__read_str(path, &data, &size))
82-
return NULL;
86+
err = filename__read_str(path, &data, &size);
87+
if (err)
88+
return ERR_PTR(err);
8389

8490
pevent_parse_format(pevent, &event, data, size, sys);
8591

8692
free(data);
8793
return event;
8894
}
8995

96+
/*
97+
* Returns pointer with encoded error via <linux/err.h> interface.
98+
*/
9099
struct event_format*
91100
trace_event__tp_format(const char *sys, const char *name)
92101
{
93102
if (!tevent_initialized && trace_event__init2())
94-
return NULL;
103+
return ERR_PTR(-ENOMEM);
95104

96105
return tp_format(sys, name);
97106
}

0 commit comments

Comments
 (0)