Skip to content

Commit be3245e

Browse files
qmonnetborkmann
authored andcommitted
tools: bpftool: attempt to mount tracefs if required for tracelog cmd
As a follow-up to commit 30da46b ("tools: bpftool: add a command to dump the trace pipe"), attempt to mount the tracefs virtual file system if it is not detected on the system before trying to dump content of the tracing pipe on an invocation of "bpftool prog tracelog". Usually, tracefs in automatically mounted by debugfs when the user tries to access it (e.g. "ls /sys/kernel/debug/tracing" mounts the tracefs). So if we failed to find it, it is probably that debugfs is not here either. Therefore, we just attempt a single mount, at a location that does not involve debugfs: /sys/kernel/tracing. Suggested-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com> Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 0d7410e commit be3245e

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

tools/bpf/bpftool/common.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ void set_max_rlimit(void)
7676
setrlimit(RLIMIT_MEMLOCK, &rinf);
7777
}
7878

79-
static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
79+
static int
80+
mnt_fs(const char *target, const char *type, char *buff, size_t bufflen)
8081
{
8182
bool bind_done = false;
8283

@@ -98,15 +99,29 @@ static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
9899
bind_done = true;
99100
}
100101

101-
if (mount("bpf", target, "bpf", 0, "mode=0700")) {
102-
snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s",
103-
target, strerror(errno));
102+
if (mount(type, target, type, 0, "mode=0700")) {
103+
snprintf(buff, bufflen, "mount -t %s %s %s failed: %s",
104+
type, type, target, strerror(errno));
104105
return -1;
105106
}
106107

107108
return 0;
108109
}
109110

111+
int mount_tracefs(const char *target)
112+
{
113+
char err_str[ERR_MAX_LEN];
114+
int err;
115+
116+
err = mnt_fs(target, "tracefs", err_str, ERR_MAX_LEN);
117+
if (err) {
118+
err_str[ERR_MAX_LEN - 1] = '\0';
119+
p_err("can't mount tracefs: %s", err_str);
120+
}
121+
122+
return err;
123+
}
124+
110125
int open_obj_pinned(char *path, bool quiet)
111126
{
112127
int fd;
@@ -162,7 +177,7 @@ int mount_bpffs_for_pin(const char *name)
162177
/* nothing to do if already mounted */
163178
goto out_free;
164179

165-
err = mnt_bpffs(dir, err_str, ERR_MAX_LEN);
180+
err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
166181
if (err) {
167182
err_str[ERR_MAX_LEN - 1] = '\0';
168183
p_err("can't mount BPF file system to pin the object (%s): %s",

tools/bpf/bpftool/main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ void usage(void) __noreturn;
9898

9999
void set_max_rlimit(void);
100100

101+
int mount_tracefs(const char *target);
102+
101103
struct pinned_obj_table {
102104
DECLARE_HASHTABLE(table, 16);
103105
};

tools/bpf/bpftool/tracelog.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ find_tracefs_mnt_single(unsigned long magic, char *mnt, const char *mntpt)
5454
return true;
5555
}
5656

57-
static bool find_tracefs_pipe(char *mnt)
57+
static bool get_tracefs_pipe(char *mnt)
5858
{
5959
static const char * const known_mnts[] = {
6060
"/sys/kernel/debug/tracing",
@@ -88,7 +88,17 @@ static bool find_tracefs_pipe(char *mnt)
8888
fclose(fp);
8989

9090
/* The string from fscanf() might be truncated, check mnt is valid */
91-
if (!found || validate_tracefs_mnt(mnt, TRACEFS_MAGIC))
91+
if (found && validate_tracefs_mnt(mnt, TRACEFS_MAGIC))
92+
goto exit_found;
93+
94+
p_info("could not find tracefs, attempting to mount it now");
95+
/* Most of the time, tracefs is automatically mounted by debugfs at
96+
* /sys/kernel/debug/tracing when we try to access it. If we could not
97+
* find it, it is likely that debugfs is not mounted. Let's give one
98+
* attempt at mounting just tracefs at /sys/kernel/tracing.
99+
*/
100+
strcpy(mnt, known_mnts[1]);
101+
if (mount_tracefs(mnt))
92102
return false;
93103

94104
exit_found:
@@ -115,17 +125,13 @@ int do_tracelog(int argc, char **argv)
115125
.sa_handler = exit_tracelog
116126
};
117127
char trace_pipe[PATH_MAX];
118-
bool found_trace_pipe;
119128
size_t buff_len = 0;
120129

121130
if (json_output)
122131
jsonw_start_array(json_wtr);
123132

124-
found_trace_pipe = find_tracefs_pipe(trace_pipe);
125-
if (!found_trace_pipe) {
126-
p_err("could not find trace pipe, tracefs not mounted?");
133+
if (!get_tracefs_pipe(trace_pipe))
127134
return -1;
128-
}
129135

130136
trace_pipe_fd = fopen(trace_pipe, "r");
131137
if (!trace_pipe_fd) {

0 commit comments

Comments
 (0)