tools: bpftool: attempt to mount tracefs if required for tracelog cmd
As a follow-up to commit 30da46b5dc
("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>
This commit is contained in:
parent
0d7410ea6e
commit
be3245e22d
|
@ -76,7 +76,8 @@ void set_max_rlimit(void)
|
||||||
setrlimit(RLIMIT_MEMLOCK, &rinf);
|
setrlimit(RLIMIT_MEMLOCK, &rinf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
|
static int
|
||||||
|
mnt_fs(const char *target, const char *type, char *buff, size_t bufflen)
|
||||||
{
|
{
|
||||||
bool bind_done = false;
|
bool bind_done = false;
|
||||||
|
|
||||||
|
@ -98,15 +99,29 @@ static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
|
||||||
bind_done = true;
|
bind_done = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mount("bpf", target, "bpf", 0, "mode=0700")) {
|
if (mount(type, target, type, 0, "mode=0700")) {
|
||||||
snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s",
|
snprintf(buff, bufflen, "mount -t %s %s %s failed: %s",
|
||||||
target, strerror(errno));
|
type, type, target, strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int mount_tracefs(const char *target)
|
||||||
|
{
|
||||||
|
char err_str[ERR_MAX_LEN];
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = mnt_fs(target, "tracefs", err_str, ERR_MAX_LEN);
|
||||||
|
if (err) {
|
||||||
|
err_str[ERR_MAX_LEN - 1] = '\0';
|
||||||
|
p_err("can't mount tracefs: %s", err_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
int open_obj_pinned(char *path, bool quiet)
|
int open_obj_pinned(char *path, bool quiet)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
@ -162,7 +177,7 @@ int mount_bpffs_for_pin(const char *name)
|
||||||
/* nothing to do if already mounted */
|
/* nothing to do if already mounted */
|
||||||
goto out_free;
|
goto out_free;
|
||||||
|
|
||||||
err = mnt_bpffs(dir, err_str, ERR_MAX_LEN);
|
err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
|
||||||
if (err) {
|
if (err) {
|
||||||
err_str[ERR_MAX_LEN - 1] = '\0';
|
err_str[ERR_MAX_LEN - 1] = '\0';
|
||||||
p_err("can't mount BPF file system to pin the object (%s): %s",
|
p_err("can't mount BPF file system to pin the object (%s): %s",
|
||||||
|
|
|
@ -98,6 +98,8 @@ void usage(void) __noreturn;
|
||||||
|
|
||||||
void set_max_rlimit(void);
|
void set_max_rlimit(void);
|
||||||
|
|
||||||
|
int mount_tracefs(const char *target);
|
||||||
|
|
||||||
struct pinned_obj_table {
|
struct pinned_obj_table {
|
||||||
DECLARE_HASHTABLE(table, 16);
|
DECLARE_HASHTABLE(table, 16);
|
||||||
};
|
};
|
||||||
|
|
|
@ -54,7 +54,7 @@ find_tracefs_mnt_single(unsigned long magic, char *mnt, const char *mntpt)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool find_tracefs_pipe(char *mnt)
|
static bool get_tracefs_pipe(char *mnt)
|
||||||
{
|
{
|
||||||
static const char * const known_mnts[] = {
|
static const char * const known_mnts[] = {
|
||||||
"/sys/kernel/debug/tracing",
|
"/sys/kernel/debug/tracing",
|
||||||
|
@ -88,7 +88,17 @@ static bool find_tracefs_pipe(char *mnt)
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
/* The string from fscanf() might be truncated, check mnt is valid */
|
/* The string from fscanf() might be truncated, check mnt is valid */
|
||||||
if (!found || validate_tracefs_mnt(mnt, TRACEFS_MAGIC))
|
if (found && validate_tracefs_mnt(mnt, TRACEFS_MAGIC))
|
||||||
|
goto exit_found;
|
||||||
|
|
||||||
|
p_info("could not find tracefs, attempting to mount it now");
|
||||||
|
/* Most of the time, tracefs is automatically mounted by debugfs at
|
||||||
|
* /sys/kernel/debug/tracing when we try to access it. If we could not
|
||||||
|
* find it, it is likely that debugfs is not mounted. Let's give one
|
||||||
|
* attempt at mounting just tracefs at /sys/kernel/tracing.
|
||||||
|
*/
|
||||||
|
strcpy(mnt, known_mnts[1]);
|
||||||
|
if (mount_tracefs(mnt))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
exit_found:
|
exit_found:
|
||||||
|
@ -115,17 +125,13 @@ int do_tracelog(int argc, char **argv)
|
||||||
.sa_handler = exit_tracelog
|
.sa_handler = exit_tracelog
|
||||||
};
|
};
|
||||||
char trace_pipe[PATH_MAX];
|
char trace_pipe[PATH_MAX];
|
||||||
bool found_trace_pipe;
|
|
||||||
size_t buff_len = 0;
|
size_t buff_len = 0;
|
||||||
|
|
||||||
if (json_output)
|
if (json_output)
|
||||||
jsonw_start_array(json_wtr);
|
jsonw_start_array(json_wtr);
|
||||||
|
|
||||||
found_trace_pipe = find_tracefs_pipe(trace_pipe);
|
if (!get_tracefs_pipe(trace_pipe))
|
||||||
if (!found_trace_pipe) {
|
|
||||||
p_err("could not find trace pipe, tracefs not mounted?");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
trace_pipe_fd = fopen(trace_pipe, "r");
|
trace_pipe_fd = fopen(trace_pipe, "r");
|
||||||
if (!trace_pipe_fd) {
|
if (!trace_pipe_fd) {
|
||||||
|
|
Loading…
Reference in New Issue