Merge remote-tracking branch 'remotes/juanquintela/tags/migration/20150612' into...
[qemu.git] / trace / ftrace.c
bloba7ae371e6fac2ec20bb5dff4115112a3c3bc69ec
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 <stdio.h>
13 #include <string.h>
14 #include <fcntl.h>
15 #include <limits.h>
16 #include "trace.h"
17 #include "trace/control.h"
19 int trace_marker_fd;
21 static int find_debugfs(char *debugfs)
23 char type[100];
24 FILE *fp;
26 fp = fopen("/proc/mounts", "r");
27 if (fp == NULL) {
28 return 0;
31 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
32 debugfs, type) == 2) {
33 if (strcmp(type, "debugfs") == 0) {
34 break;
37 fclose(fp);
39 if (strcmp(type, "debugfs") != 0) {
40 return 0;
42 return 1;
45 bool ftrace_init(void)
47 char debugfs[PATH_MAX];
48 char path[PATH_MAX];
49 int debugfs_found;
50 int trace_fd = -1;
52 debugfs_found = find_debugfs(debugfs);
53 if (debugfs_found) {
54 snprintf(path, PATH_MAX, "%s/tracing/tracing_on", debugfs);
55 trace_fd = open(path, O_WRONLY);
56 if (trace_fd < 0) {
57 perror("Could not open ftrace 'tracing_on' file");
58 return false;
59 } else {
60 if (write(trace_fd, "1", 1) < 0) {
61 perror("Could not write to 'tracing_on' file");
62 close(trace_fd);
63 return false;
65 close(trace_fd);
67 snprintf(path, PATH_MAX, "%s/tracing/trace_marker", debugfs);
68 trace_marker_fd = open(path, O_WRONLY);
69 if (trace_marker_fd < 0) {
70 perror("Could not open ftrace 'trace_marker' file");
71 return false;
73 } else {
74 fprintf(stderr, "debugfs is not mounted\n");
75 return false;
78 return true;