blob: 9749543d9b2424fe739afc4ea9ac47cdb88abfed [file] [log] [blame]
Eiichi Tsukata781e9542013-04-11 20:25:15 +09001/*
2 * Ftrace trace backend
3 *
4 * Copyright (C) 2013 Hitachi, Ltd.
5 * Created by Eiichi Tsukata <eiichi.tsukata.xh@hitachi.com>
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2. See
8 * the COPYING file in the top-level directory.
9 *
10 */
11
Peter Maydelld38ea872016-01-29 17:50:05 +000012#include "qemu/osdep.h"
Eiichi Tsukata781e9542013-04-11 20:25:15 +090013#include "trace/control.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000014#include "trace/ftrace.h"
Eiichi Tsukata781e9542013-04-11 20:25:15 +090015
16int trace_marker_fd;
17
Namhyung Kimbabfff82017-11-08 00:31:35 +090018static int find_mount(char *mount_point, const char *fstype)
Eiichi Tsukata781e9542013-04-11 20:25:15 +090019{
20 char type[100];
21 FILE *fp;
Namhyung Kim50705702017-11-08 00:31:34 +090022 int ret = 0;
Eiichi Tsukata781e9542013-04-11 20:25:15 +090023
24 fp = fopen("/proc/mounts", "r");
25 if (fp == NULL) {
26 return 0;
27 }
28
29 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
Namhyung Kimbabfff82017-11-08 00:31:35 +090030 mount_point, type) == 2) {
31 if (strcmp(type, fstype) == 0) {
Namhyung Kim50705702017-11-08 00:31:34 +090032 ret = 1;
Eiichi Tsukata781e9542013-04-11 20:25:15 +090033 break;
34 }
35 }
36 fclose(fp);
37
Namhyung Kim50705702017-11-08 00:31:34 +090038 return ret;
Eiichi Tsukata781e9542013-04-11 20:25:15 +090039}
40
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +020041bool ftrace_init(void)
Eiichi Tsukata781e9542013-04-11 20:25:15 +090042{
Namhyung Kimbabfff82017-11-08 00:31:35 +090043 char mount_point[PATH_MAX];
Eiichi Tsukata781e9542013-04-11 20:25:15 +090044 char path[PATH_MAX];
Namhyung Kimc9add622017-11-08 00:31:36 +090045 int tracefs_found;
Eiichi Tsukata781e9542013-04-11 20:25:15 +090046 int trace_fd = -1;
Namhyung Kimc9add622017-11-08 00:31:36 +090047 const char *subdir = "";
Eiichi Tsukata781e9542013-04-11 20:25:15 +090048
Namhyung Kimc9add622017-11-08 00:31:36 +090049 tracefs_found = find_mount(mount_point, "tracefs");
50 if (!tracefs_found) {
51 tracefs_found = find_mount(mount_point, "debugfs");
52 subdir = "/tracing";
53 }
54
55 if (tracefs_found) {
Stefan Hajnoczifd985832019-03-21 17:08:30 +000056 if (snprintf(path, PATH_MAX, "%s%s/tracing_on", mount_point, subdir)
57 >= sizeof(path)) {
58 fprintf(stderr, "Using tracefs mountpoint would exceed PATH_MAX\n");
59 return false;
60 }
Eiichi Tsukata781e9542013-04-11 20:25:15 +090061 trace_fd = open(path, O_WRONLY);
62 if (trace_fd < 0) {
Daniel P. Berrange8ed53722016-10-04 14:35:51 +010063 if (errno == EACCES) {
64 trace_marker_fd = open("/dev/null", O_WRONLY);
65 if (trace_marker_fd != -1) {
66 return true;
67 }
68 }
Eiichi Tsukata781e9542013-04-11 20:25:15 +090069 perror("Could not open ftrace 'tracing_on' file");
70 return false;
71 } else {
72 if (write(trace_fd, "1", 1) < 0) {
73 perror("Could not write to 'tracing_on' file");
74 close(trace_fd);
75 return false;
76 }
77 close(trace_fd);
78 }
Stefan Hajnoczifd985832019-03-21 17:08:30 +000079 if (snprintf(path, PATH_MAX, "%s%s/trace_marker", mount_point, subdir)
80 >= sizeof(path)) {
81 fprintf(stderr, "Using tracefs mountpoint would exceed PATH_MAX\n");
82 return false;
83 }
Eiichi Tsukata781e9542013-04-11 20:25:15 +090084 trace_marker_fd = open(path, O_WRONLY);
85 if (trace_marker_fd < 0) {
86 perror("Could not open ftrace 'trace_marker' file");
87 return false;
88 }
89 } else {
Namhyung Kimc9add622017-11-08 00:31:36 +090090 fprintf(stderr, "tracefs is not mounted\n");
Eiichi Tsukata781e9542013-04-11 20:25:15 +090091 return false;
92 }
93
Eiichi Tsukata781e9542013-04-11 20:25:15 +090094 return true;
95}