ftrace.c 2.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Ftrace trace backend
 *
 * Copyright (C) 2013 Hitachi, Ltd.
 * Created by Eiichi Tsukata <eiichi.tsukata.xh@hitachi.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */

Peter Maydell's avatar
Peter Maydell committed
12
#include "qemu/osdep.h"
13
#include "trace/control.h"
14
#include "trace/ftrace.h"
15 16 17

int trace_marker_fd;

18
static int find_mount(char *mount_point, const char *fstype)
19 20 21
{
    char type[100];
    FILE *fp;
22
    int ret = 0;
23 24 25 26 27 28 29

    fp = fopen("/proc/mounts", "r");
    if (fp == NULL) {
        return 0;
    }

    while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
30 31
                  mount_point, type) == 2) {
        if (strcmp(type, fstype) == 0) {
32
            ret = 1;
33 34 35 36 37
            break;
        }
    }
    fclose(fp);

38
    return ret;
39 40
}

41
bool ftrace_init(void)
42
{
43
    char mount_point[PATH_MAX];
44
    char path[PATH_MAX];
45
    int tracefs_found;
46
    int trace_fd = -1;
47
    const char *subdir = "";
48

49 50 51 52 53 54 55 56
    tracefs_found = find_mount(mount_point, "tracefs");
    if (!tracefs_found) {
        tracefs_found = find_mount(mount_point, "debugfs");
        subdir = "/tracing";
    }

    if (tracefs_found) {
        snprintf(path, PATH_MAX, "%s%s/tracing_on", mount_point, subdir);
57 58
        trace_fd = open(path, O_WRONLY);
        if (trace_fd < 0) {
59 60 61 62 63 64
            if (errno == EACCES) {
                trace_marker_fd = open("/dev/null", O_WRONLY);
                if (trace_marker_fd != -1) {
                    return true;
                }
            }
65 66 67 68 69 70 71 72 73 74
            perror("Could not open ftrace 'tracing_on' file");
            return false;
        } else {
            if (write(trace_fd, "1", 1) < 0) {
                perror("Could not write to 'tracing_on' file");
                close(trace_fd);
                return false;
            }
            close(trace_fd);
        }
75
        snprintf(path, PATH_MAX, "%s%s/trace_marker", mount_point, subdir);
76 77 78 79 80 81
        trace_marker_fd = open(path, O_WRONLY);
        if (trace_marker_fd < 0) {
            perror("Could not open ftrace 'trace_marker' file");
            return false;
        }
    } else {
82
        fprintf(stderr, "tracefs is not mounted\n");
83 84 85 86 87
        return false;
    }

    return true;
}