slirp: fix segv when init failed
[qemu.git] / trace / ftrace.c
blobe953922f5bd71360a3fe418a60dc2a4072fcffda
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.h"
14 #include "trace/control.h"
16 int trace_marker_fd;
18 static int find_debugfs(char *debugfs)
20 char type[100];
21 FILE *fp;
23 fp = fopen("/proc/mounts", "r");
24 if (fp == NULL) {
25 return 0;
28 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
29 debugfs, type) == 2) {
30 if (strcmp(type, "debugfs") == 0) {
31 break;
34 fclose(fp);
36 if (strcmp(type, "debugfs") != 0) {
37 return 0;
39 return 1;
42 bool ftrace_init(void)
44 char debugfs[PATH_MAX];
45 char path[PATH_MAX];
46 int debugfs_found;
47 int trace_fd = -1;
49 debugfs_found = find_debugfs(debugfs);
50 if (debugfs_found) {
51 snprintf(path, PATH_MAX, "%s/tracing/tracing_on", debugfs);
52 trace_fd = open(path, O_WRONLY);
53 if (trace_fd < 0) {
54 perror("Could not open ftrace 'tracing_on' file");
55 return false;
56 } else {
57 if (write(trace_fd, "1", 1) < 0) {
58 perror("Could not write to 'tracing_on' file");
59 close(trace_fd);
60 return false;
62 close(trace_fd);
64 snprintf(path, PATH_MAX, "%s/tracing/trace_marker", debugfs);
65 trace_marker_fd = open(path, O_WRONLY);
66 if (trace_marker_fd < 0) {
67 perror("Could not open ftrace 'trace_marker' file");
68 return false;
70 } else {
71 fprintf(stderr, "debugfs is not mounted\n");
72 return false;
75 return true;