Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[qemu/ar7.git] / trace / ftrace.c
blob61692a8682984a24cafd438e907f4d3bf06417ed
1 /*
2 * Ftrace trace backend
4 * Copyright (C) 2013 Hitachi, Ltd.
5 * Created by Eiichi Tsukata <eiichi.tsukata.xh@hitachi.com>
7 * This work is licensed under the terms of the GNU GPL, version 2. See
8 * the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "trace/control.h"
14 #include "trace/ftrace.h"
16 int trace_marker_fd;
18 static int find_mount(char *mount_point, const char *fstype)
20 char type[100];
21 FILE *fp;
22 int ret = 0;
24 fp = fopen("/proc/mounts", "r");
25 if (fp == NULL) {
26 return 0;
29 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
30 mount_point, type) == 2) {
31 if (strcmp(type, fstype) == 0) {
32 ret = 1;
33 break;
36 fclose(fp);
38 return ret;
41 bool ftrace_init(void)
43 char mount_point[PATH_MAX];
44 char path[PATH_MAX];
45 int tracefs_found;
46 int trace_fd = -1;
47 const char *subdir = "";
49 tracefs_found = find_mount(mount_point, "tracefs");
50 if (!tracefs_found) {
51 tracefs_found = find_mount(mount_point, "debugfs");
52 subdir = "/tracing";
55 if (tracefs_found) {
56 snprintf(path, PATH_MAX, "%s%s/tracing_on", mount_point, subdir);
57 trace_fd = open(path, O_WRONLY);
58 if (trace_fd < 0) {
59 if (errno == EACCES) {
60 trace_marker_fd = open("/dev/null", O_WRONLY);
61 if (trace_marker_fd != -1) {
62 return true;
65 perror("Could not open ftrace 'tracing_on' file");
66 return false;
67 } else {
68 if (write(trace_fd, "1", 1) < 0) {
69 perror("Could not write to 'tracing_on' file");
70 close(trace_fd);
71 return false;
73 close(trace_fd);
75 snprintf(path, PATH_MAX, "%s%s/trace_marker", mount_point, subdir);
76 trace_marker_fd = open(path, O_WRONLY);
77 if (trace_marker_fd < 0) {
78 perror("Could not open ftrace 'trace_marker' file");
79 return false;
81 } else {
82 fprintf(stderr, "tracefs is not mounted\n");
83 return false;
86 return true;