Skip to content

Commit

Permalink
ftrace: Autodetect location of tracing folder
Browse files Browse the repository at this point in the history
If you don't provide a tracing location in the global config key section,
rt-app usually looks in /sys/kernel/debug for the tracing folder.

This patch allows rt-app to examine the content of /etc/mtab, and apply
the following ordering:

* If tracefs is mounted, use that location
* If tracefs is not found anywhere, then look for debugfs
* If we find neither, just use /sys/kernel/debug

All of this is overridden if you provide the config key.

This should allow rt-app to work on any platform, at least from tracing
POV.

Signed-off-by: Chris Redpath <[email protected]>
  • Loading branch information
credp committed Jul 13, 2022
1 parent c2c95d2 commit c5ff4f0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/rt-app_parse_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#define JSON_FILE_BUF_SIZE 4096
#define DEFAULT_MEM_BUF_SIZE (4 * 1024 * 1024)

#define DEFAULT_FTRACE_DIR "/sys/kernel/debug"

#ifndef TRUE
#define TRUE true
#define FALSE false
Expand Down Expand Up @@ -1209,7 +1207,7 @@ parse_global(struct json_object *global, rtapp_options_t *opts)
opts->io_device = strdup("/dev/null");
opts->mem_buffer_size = DEFAULT_MEM_BUF_SIZE;
opts->cumulative_slack = 0;
opts->ftracedir = strdup(DEFAULT_FTRACE_DIR);
opts->ftracedir = ftrace_dir();
return;
}

Expand Down Expand Up @@ -1307,7 +1305,7 @@ parse_global(struct json_object *global, rtapp_options_t *opts)
log_critical(PFX "Invalid ftrace categories");
exit(EXIT_INV_CONFIG);
}
opts->ftracedir = get_string_value_from(global, "ftracedir", TRUE, DEFAULT_FTRACE_DIR);
opts->ftracedir = get_string_value_from(global, "ftracedir", TRUE, ftrace_dir());

opts->lock_pages = get_bool_value_from(global, "lock_pages", TRUE, 1);
opts->pi_enabled = get_bool_value_from(global, "pi_enabled", TRUE, 0);
Expand Down
43 changes: 43 additions & 0 deletions src/rt-app_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <mntent.h>

#include "rt-app_utils.h"

Expand Down Expand Up @@ -267,6 +268,48 @@ resource_to_string(resource_t resource, char *resource_name)
return 0;
}

#define DEFAULT_FTRACE_DIR "/sys/kernel/debug"

char *mtab_by_type(const char *type, int typelen)
{
FILE *fd_mntent;

fd_mntent = setmntent("/etc/mtab", "r");
if(fd_mntent)
{
struct mntent *mntent;

while(mntent = getmntent(fd_mntent), mntent)
{
log_debug("mntent->dir=%s type=%s", mntent->mnt_dir, mntent->mnt_type);
if(!strncmp(type, mntent->mnt_type, typelen))
{
char *path = strdup(mntent->mnt_dir);
endmntent(fd_mntent);
log_info("Found target");
return path;
}
}
endmntent(fd_mntent);
}
return NULL;
}

const char *ftrace_dir(void)
{
char *path;

path = mtab_by_type("tracefs", sizeof("tracefs")-1);
if(path)
return path;

path = mtab_by_type("debugfs", sizeof("debugfs")-1);
if(path)
return path;

return strdup(DEFAULT_FTRACE_DIR);
}

int ftrace_setup(char *categories)
{
char *cat = strtok(categories, ",");
Expand Down
4 changes: 4 additions & 0 deletions src/rt-app_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ string_to_resource(const char *name, resource_t *resource);
int
resource_to_string(resource_t resource, char *name);

/* ftrace_dir always returns a string on the heap */
const char *
ftrace_dir(void);

int
ftrace_setup(char *categories);

Expand Down

0 comments on commit c5ff4f0

Please sign in to comment.