2013-04-11 19:25:15 +08:00
|
|
|
/*
|
|
|
|
* Ftrace trace backend
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Hitachi, Ltd.
|
|
|
|
* Created by Eiichi Tsukata <eiichi.tsukata.xh@hitachi.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-30 01:50:05 +08:00
|
|
|
#include "qemu/osdep.h"
|
2013-04-11 19:25:15 +08:00
|
|
|
#include "trace/control.h"
|
2017-01-26 00:14:15 +08:00
|
|
|
#include "trace/ftrace.h"
|
2013-04-11 19:25:15 +08:00
|
|
|
|
|
|
|
int trace_marker_fd;
|
|
|
|
|
2017-11-07 23:31:35 +08:00
|
|
|
static int find_mount(char *mount_point, const char *fstype)
|
2013-04-11 19:25:15 +08:00
|
|
|
{
|
|
|
|
char type[100];
|
|
|
|
FILE *fp;
|
2017-11-07 23:31:34 +08:00
|
|
|
int ret = 0;
|
2013-04-11 19:25:15 +08:00
|
|
|
|
|
|
|
fp = fopen("/proc/mounts", "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
|
2017-11-07 23:31:35 +08:00
|
|
|
mount_point, type) == 2) {
|
|
|
|
if (strcmp(type, fstype) == 0) {
|
2017-11-07 23:31:34 +08:00
|
|
|
ret = 1;
|
2013-04-11 19:25:15 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
2017-11-07 23:31:34 +08:00
|
|
|
return ret;
|
2013-04-11 19:25:15 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 21:02:14 +08:00
|
|
|
bool ftrace_init(void)
|
2013-04-11 19:25:15 +08:00
|
|
|
{
|
2017-11-07 23:31:35 +08:00
|
|
|
char mount_point[PATH_MAX];
|
2013-04-11 19:25:15 +08:00
|
|
|
char path[PATH_MAX];
|
2017-11-07 23:31:36 +08:00
|
|
|
int tracefs_found;
|
2013-04-11 19:25:15 +08:00
|
|
|
int trace_fd = -1;
|
2017-11-07 23:31:36 +08:00
|
|
|
const char *subdir = "";
|
2013-04-11 19:25:15 +08:00
|
|
|
|
2017-11-07 23:31:36 +08:00
|
|
|
tracefs_found = find_mount(mount_point, "tracefs");
|
|
|
|
if (!tracefs_found) {
|
|
|
|
tracefs_found = find_mount(mount_point, "debugfs");
|
|
|
|
subdir = "/tracing";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tracefs_found) {
|
2019-03-22 01:08:30 +08:00
|
|
|
if (snprintf(path, PATH_MAX, "%s%s/tracing_on", mount_point, subdir)
|
|
|
|
>= sizeof(path)) {
|
|
|
|
fprintf(stderr, "Using tracefs mountpoint would exceed PATH_MAX\n");
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-11 19:25:15 +08:00
|
|
|
trace_fd = open(path, O_WRONLY);
|
|
|
|
if (trace_fd < 0) {
|
2016-10-04 21:35:51 +08:00
|
|
|
if (errno == EACCES) {
|
|
|
|
trace_marker_fd = open("/dev/null", O_WRONLY);
|
|
|
|
if (trace_marker_fd != -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-04-11 19:25:15 +08:00
|
|
|
perror("Could not open ftrace 'tracing_on' file");
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (write(trace_fd, "1", 1) < 0) {
|
|
|
|
perror("Could not write to 'tracing_on' file");
|
|
|
|
close(trace_fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
close(trace_fd);
|
|
|
|
}
|
2019-03-22 01:08:30 +08:00
|
|
|
if (snprintf(path, PATH_MAX, "%s%s/trace_marker", mount_point, subdir)
|
|
|
|
>= sizeof(path)) {
|
|
|
|
fprintf(stderr, "Using tracefs mountpoint would exceed PATH_MAX\n");
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-11 19:25:15 +08:00
|
|
|
trace_marker_fd = open(path, O_WRONLY);
|
|
|
|
if (trace_marker_fd < 0) {
|
|
|
|
perror("Could not open ftrace 'trace_marker' file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-07 23:31:36 +08:00
|
|
|
fprintf(stderr, "tracefs is not mounted\n");
|
2013-04-11 19:25:15 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|