Skip to content

Commit 25a7d91

Browse files
committed
perf parse-events: Use get/put_events_file()
Instead of accessing the trace_events_path variable directly, that may not have been properly initialized wrt detecting where tracefs is mounted. Cc: Adrian Hunter <[email protected]> Cc: David Ahern <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Wang Nan <[email protected]> Link: https://lkml.kernel.org/n/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent c02cab2 commit 25a7d91

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

tools/perf/tests/parse-events.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ static int count_tracepoints(void)
13281328
TEST_ASSERT_VAL("Can't open events dir", events_dir);
13291329

13301330
while ((events_ent = readdir(events_dir))) {
1331-
char sys_path[PATH_MAX];
1331+
char *sys_path;
13321332
struct dirent *sys_ent;
13331333
DIR *sys_dir;
13341334

@@ -1339,8 +1339,8 @@ static int count_tracepoints(void)
13391339
|| !strcmp(events_ent->d_name, "header_page"))
13401340
continue;
13411341

1342-
scnprintf(sys_path, PATH_MAX, "%s/%s",
1343-
tracing_events_path, events_ent->d_name);
1342+
sys_path = get_events_file(events_ent->d_name);
1343+
TEST_ASSERT_VAL("Can't get sys path", sys_path);
13441344

13451345
sys_dir = opendir(sys_path);
13461346
TEST_ASSERT_VAL("Can't open sys dir", sys_dir);
@@ -1356,6 +1356,7 @@ static int count_tracepoints(void)
13561356
}
13571357

13581358
closedir(sys_dir);
1359+
put_events_file(sys_path);
13591360
}
13601361

13611362
closedir(events_dir);

tools/perf/util/parse-events.c

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,19 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config)
189189
int fd;
190190
u64 id;
191191
char evt_path[MAXPATHLEN];
192-
char dir_path[MAXPATHLEN];
192+
char *dir_path;
193193

194194
sys_dir = opendir(tracing_events_path);
195195
if (!sys_dir)
196196
return NULL;
197197

198198
for_each_subsystem(sys_dir, sys_dirent) {
199-
200-
snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
201-
sys_dirent->d_name);
199+
dir_path = get_events_file(sys_dirent->d_name);
200+
if (!dir_path)
201+
continue;
202202
evt_dir = opendir(dir_path);
203203
if (!evt_dir)
204-
continue;
204+
goto next;
205205

206206
for_each_event(dir_path, evt_dir, evt_dirent) {
207207

@@ -217,6 +217,7 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config)
217217
close(fd);
218218
id = atoll(id_buf);
219219
if (id == config) {
220+
put_events_file(dir_path);
220221
closedir(evt_dir);
221222
closedir(sys_dir);
222223
path = zalloc(sizeof(*path));
@@ -241,6 +242,8 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config)
241242
}
242243
}
243244
closedir(evt_dir);
245+
next:
246+
put_events_file(dir_path);
244247
}
245248

246249
closedir(sys_dir);
@@ -511,14 +514,19 @@ static int add_tracepoint_multi_event(struct list_head *list, int *idx,
511514
struct parse_events_error *err,
512515
struct list_head *head_config)
513516
{
514-
char evt_path[MAXPATHLEN];
517+
char *evt_path;
515518
struct dirent *evt_ent;
516519
DIR *evt_dir;
517520
int ret = 0, found = 0;
518521

519-
snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
522+
evt_path = get_events_file(sys_name);
523+
if (!evt_path) {
524+
tracepoint_error(err, errno, sys_name, evt_name);
525+
return -1;
526+
}
520527
evt_dir = opendir(evt_path);
521528
if (!evt_dir) {
529+
put_events_file(evt_path);
522530
tracepoint_error(err, errno, sys_name, evt_name);
523531
return -1;
524532
}
@@ -544,6 +552,7 @@ static int add_tracepoint_multi_event(struct list_head *list, int *idx,
544552
ret = -1;
545553
}
546554

555+
put_events_file(evt_path);
547556
closedir(evt_dir);
548557
return ret;
549558
}
@@ -2091,7 +2100,7 @@ void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
20912100
DIR *sys_dir, *evt_dir;
20922101
struct dirent *sys_dirent, *evt_dirent;
20932102
char evt_path[MAXPATHLEN];
2094-
char dir_path[MAXPATHLEN];
2103+
char *dir_path;
20952104
char **evt_list = NULL;
20962105
unsigned int evt_i = 0, evt_num = 0;
20972106
bool evt_num_known = false;
@@ -2112,11 +2121,12 @@ void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
21122121
!strglobmatch(sys_dirent->d_name, subsys_glob))
21132122
continue;
21142123

2115-
snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
2116-
sys_dirent->d_name);
2124+
dir_path = get_events_file(sys_dirent->d_name);
2125+
if (!dir_path)
2126+
continue;
21172127
evt_dir = opendir(dir_path);
21182128
if (!evt_dir)
2119-
continue;
2129+
goto next;
21202130

21212131
for_each_event(dir_path, evt_dir, evt_dirent) {
21222132
if (event_glob != NULL &&
@@ -2132,11 +2142,15 @@ void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
21322142
sys_dirent->d_name, evt_dirent->d_name);
21332143

21342144
evt_list[evt_i] = strdup(evt_path);
2135-
if (evt_list[evt_i] == NULL)
2145+
if (evt_list[evt_i] == NULL) {
2146+
put_events_file(dir_path);
21362147
goto out_close_evt_dir;
2148+
}
21372149
evt_i++;
21382150
}
21392151
closedir(evt_dir);
2152+
next:
2153+
put_events_file(dir_path);
21402154
}
21412155
closedir(sys_dir);
21422156

@@ -2184,19 +2198,19 @@ int is_valid_tracepoint(const char *event_string)
21842198
DIR *sys_dir, *evt_dir;
21852199
struct dirent *sys_dirent, *evt_dirent;
21862200
char evt_path[MAXPATHLEN];
2187-
char dir_path[MAXPATHLEN];
2201+
char *dir_path;
21882202

21892203
sys_dir = opendir(tracing_events_path);
21902204
if (!sys_dir)
21912205
return 0;
21922206

21932207
for_each_subsystem(sys_dir, sys_dirent) {
2194-
2195-
snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
2196-
sys_dirent->d_name);
2208+
dir_path = get_events_file(sys_dirent->d_name);
2209+
if (!dir_path)
2210+
continue;
21972211
evt_dir = opendir(dir_path);
21982212
if (!evt_dir)
2199-
continue;
2213+
goto next;
22002214

22012215
for_each_event(dir_path, evt_dir, evt_dirent) {
22022216
snprintf(evt_path, MAXPATHLEN, "%s:%s",
@@ -2208,6 +2222,8 @@ int is_valid_tracepoint(const char *event_string)
22082222
}
22092223
}
22102224
closedir(evt_dir);
2225+
next:
2226+
put_events_file(dir_path);
22112227
}
22122228
closedir(sys_dir);
22132229
return 0;

tools/perf/util/trace-event.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,19 @@ void trace_event__cleanup(struct trace_event *t)
7575
static struct event_format*
7676
tp_format(const char *sys, const char *name)
7777
{
78+
char *tp_dir = get_events_file(sys);
7879
struct pevent *pevent = tevent.pevent;
7980
struct event_format *event = NULL;
8081
char path[PATH_MAX];
8182
size_t size;
8283
char *data;
8384
int err;
8485

85-
scnprintf(path, PATH_MAX, "%s/%s/%s/format",
86-
tracing_events_path, sys, name);
86+
if (!tp_dir)
87+
return ERR_PTR(-errno);
88+
89+
scnprintf(path, PATH_MAX, "%s/%s/format", tp_dir, name);
90+
put_events_file(tp_dir);
8791

8892
err = filename__read_str(path, &data, &size);
8993
if (err)

0 commit comments

Comments
 (0)