1781e9545SEiichi Tsukata /*
2781e9545SEiichi Tsukata * Ftrace trace backend
3781e9545SEiichi Tsukata *
4781e9545SEiichi Tsukata * Copyright (C) 2013 Hitachi, Ltd.
5781e9545SEiichi Tsukata * Created by Eiichi Tsukata <eiichi.tsukata.xh@hitachi.com>
6781e9545SEiichi Tsukata *
7781e9545SEiichi Tsukata * This work is licensed under the terms of the GNU GPL, version 2. See
8781e9545SEiichi Tsukata * the COPYING file in the top-level directory.
9781e9545SEiichi Tsukata *
10781e9545SEiichi Tsukata */
11781e9545SEiichi Tsukata
12d38ea87aSPeter Maydell #include "qemu/osdep.h"
13781e9545SEiichi Tsukata #include "trace/control.h"
140ab8ed18SDaniel P. Berrange #include "trace/ftrace.h"
15781e9545SEiichi Tsukata
16781e9545SEiichi Tsukata int trace_marker_fd;
17781e9545SEiichi Tsukata
find_mount(char * mount_point,const char * fstype)18babfff8eSNamhyung Kim static int find_mount(char *mount_point, const char *fstype)
19781e9545SEiichi Tsukata {
20781e9545SEiichi Tsukata char type[100];
21781e9545SEiichi Tsukata FILE *fp;
225070570cSNamhyung Kim int ret = 0;
23781e9545SEiichi Tsukata
24781e9545SEiichi Tsukata fp = fopen("/proc/mounts", "r");
25781e9545SEiichi Tsukata if (fp == NULL) {
26781e9545SEiichi Tsukata return 0;
27781e9545SEiichi Tsukata }
28781e9545SEiichi Tsukata
29781e9545SEiichi Tsukata while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
30babfff8eSNamhyung Kim mount_point, type) == 2) {
31babfff8eSNamhyung Kim if (strcmp(type, fstype) == 0) {
325070570cSNamhyung Kim ret = 1;
33781e9545SEiichi Tsukata break;
34781e9545SEiichi Tsukata }
35781e9545SEiichi Tsukata }
36781e9545SEiichi Tsukata fclose(fp);
37781e9545SEiichi Tsukata
385070570cSNamhyung Kim return ret;
39781e9545SEiichi Tsukata }
40781e9545SEiichi Tsukata
ftrace_init(void)415b808275SLluís Vilanova bool ftrace_init(void)
42781e9545SEiichi Tsukata {
43babfff8eSNamhyung Kim char mount_point[PATH_MAX];
44781e9545SEiichi Tsukata char path[PATH_MAX];
45c9add621SNamhyung Kim int tracefs_found;
46781e9545SEiichi Tsukata int trace_fd = -1;
47c9add621SNamhyung Kim const char *subdir = "";
48781e9545SEiichi Tsukata
49c9add621SNamhyung Kim tracefs_found = find_mount(mount_point, "tracefs");
50c9add621SNamhyung Kim if (!tracefs_found) {
51c9add621SNamhyung Kim tracefs_found = find_mount(mount_point, "debugfs");
52c9add621SNamhyung Kim subdir = "/tracing";
53c9add621SNamhyung Kim }
54c9add621SNamhyung Kim
55c9add621SNamhyung Kim if (tracefs_found) {
56*fd985831SStefan Hajnoczi if (snprintf(path, PATH_MAX, "%s%s/tracing_on", mount_point, subdir)
57*fd985831SStefan Hajnoczi >= sizeof(path)) {
58*fd985831SStefan Hajnoczi fprintf(stderr, "Using tracefs mountpoint would exceed PATH_MAX\n");
59*fd985831SStefan Hajnoczi return false;
60*fd985831SStefan Hajnoczi }
61781e9545SEiichi Tsukata trace_fd = open(path, O_WRONLY);
62781e9545SEiichi Tsukata if (trace_fd < 0) {
638ed53728SDaniel P. Berrange if (errno == EACCES) {
648ed53728SDaniel P. Berrange trace_marker_fd = open("/dev/null", O_WRONLY);
658ed53728SDaniel P. Berrange if (trace_marker_fd != -1) {
668ed53728SDaniel P. Berrange return true;
678ed53728SDaniel P. Berrange }
688ed53728SDaniel P. Berrange }
69781e9545SEiichi Tsukata perror("Could not open ftrace 'tracing_on' file");
70781e9545SEiichi Tsukata return false;
71781e9545SEiichi Tsukata } else {
72781e9545SEiichi Tsukata if (write(trace_fd, "1", 1) < 0) {
73781e9545SEiichi Tsukata perror("Could not write to 'tracing_on' file");
74781e9545SEiichi Tsukata close(trace_fd);
75781e9545SEiichi Tsukata return false;
76781e9545SEiichi Tsukata }
77781e9545SEiichi Tsukata close(trace_fd);
78781e9545SEiichi Tsukata }
79*fd985831SStefan Hajnoczi if (snprintf(path, PATH_MAX, "%s%s/trace_marker", mount_point, subdir)
80*fd985831SStefan Hajnoczi >= sizeof(path)) {
81*fd985831SStefan Hajnoczi fprintf(stderr, "Using tracefs mountpoint would exceed PATH_MAX\n");
82*fd985831SStefan Hajnoczi return false;
83*fd985831SStefan Hajnoczi }
84781e9545SEiichi Tsukata trace_marker_fd = open(path, O_WRONLY);
85781e9545SEiichi Tsukata if (trace_marker_fd < 0) {
86781e9545SEiichi Tsukata perror("Could not open ftrace 'trace_marker' file");
87781e9545SEiichi Tsukata return false;
88781e9545SEiichi Tsukata }
89781e9545SEiichi Tsukata } else {
90c9add621SNamhyung Kim fprintf(stderr, "tracefs is not mounted\n");
91781e9545SEiichi Tsukata return false;
92781e9545SEiichi Tsukata }
93781e9545SEiichi Tsukata
94781e9545SEiichi Tsukata return true;
95781e9545SEiichi Tsukata }
96