File: ftrace.c

package info (click to toggle)
qemu 1%3A2.1%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 56,688 kB
  • sloc: ansic: 806,370; sh: 12,093; asm: 10,812; python: 8,293; cpp: 6,289; perl: 4,521; makefile: 2,326; objc: 914; xml: 526
file content (79 lines) | stat: -rw-r--r-- 1,883 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * 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.
 *
 */

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include "trace.h"
#include "trace/control.h"

int trace_marker_fd;

static int find_debugfs(char *debugfs)
{
    char type[100];
    FILE *fp;

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

    while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
                  debugfs, type) == 2) {
        if (strcmp(type, "debugfs") == 0) {
            break;
        }
    }
    fclose(fp);

    if (strcmp(type, "debugfs") != 0) {
        return 0;
    }
    return 1;
}

bool ftrace_init(void)
{
    char debugfs[PATH_MAX];
    char path[PATH_MAX];
    int debugfs_found;
    int trace_fd = -1;

    debugfs_found = find_debugfs(debugfs);
    if (debugfs_found) {
        snprintf(path, PATH_MAX, "%s/tracing/tracing_on", debugfs);
        trace_fd = open(path, O_WRONLY);
        if (trace_fd < 0) {
            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);
        }
        snprintf(path, PATH_MAX, "%s/tracing/trace_marker", debugfs);
        trace_marker_fd = open(path, O_WRONLY);
        if (trace_marker_fd < 0) {
            perror("Could not open ftrace 'trace_marker' file");
            return false;
        }
    } else {
        fprintf(stderr, "debugfs is not mounted\n");
        return false;
    }

    return true;
}