Jiri Olsa | 592d5a6 | 2015-09-02 09:56:34 +0200 | [diff] [blame] | 1 | #ifndef _GNU_SOURCE |
| 2 | # define _GNU_SOURCE |
| 3 | #endif |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
Jiri Olsa | 988bdb3 | 2015-09-02 09:56:35 +0200 | [diff] [blame] | 8 | #include <errno.h> |
| 9 | #include <unistd.h> |
Jiri Olsa | 4605eab | 2015-09-02 09:56:43 +0200 | [diff] [blame] | 10 | #include "fs.h" |
Jiri Olsa | 592d5a6 | 2015-09-02 09:56:34 +0200 | [diff] [blame] | 11 | |
| 12 | #include "tracing_path.h" |
| 13 | |
| 14 | |
Jiri Olsa | ccb5597 | 2015-10-05 20:06:01 +0200 | [diff] [blame] | 15 | char tracing_mnt[PATH_MAX] = "/sys/kernel/debug"; |
| 16 | char tracing_path[PATH_MAX] = "/sys/kernel/debug/tracing"; |
| 17 | char tracing_events_path[PATH_MAX] = "/sys/kernel/debug/tracing/events"; |
Jiri Olsa | 592d5a6 | 2015-09-02 09:56:34 +0200 | [diff] [blame] | 18 | |
| 19 | |
| 20 | static void __tracing_path_set(const char *tracing, const char *mountpoint) |
| 21 | { |
Jiri Olsa | dc240c5 | 2015-09-19 16:47:07 +0200 | [diff] [blame] | 22 | snprintf(tracing_mnt, sizeof(tracing_mnt), "%s", mountpoint); |
Jiri Olsa | 592d5a6 | 2015-09-02 09:56:34 +0200 | [diff] [blame] | 23 | snprintf(tracing_path, sizeof(tracing_path), "%s/%s", |
| 24 | mountpoint, tracing); |
| 25 | snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s", |
| 26 | mountpoint, tracing, "events"); |
| 27 | } |
| 28 | |
| 29 | static const char *tracing_path_tracefs_mount(void) |
| 30 | { |
| 31 | const char *mnt; |
| 32 | |
Jiri Olsa | 4605eab | 2015-09-02 09:56:43 +0200 | [diff] [blame] | 33 | mnt = tracefs__mount(); |
Jiri Olsa | 592d5a6 | 2015-09-02 09:56:34 +0200 | [diff] [blame] | 34 | if (!mnt) |
| 35 | return NULL; |
| 36 | |
| 37 | __tracing_path_set("", mnt); |
| 38 | |
| 39 | return mnt; |
| 40 | } |
| 41 | |
| 42 | static const char *tracing_path_debugfs_mount(void) |
| 43 | { |
| 44 | const char *mnt; |
| 45 | |
Jiri Olsa | 4605eab | 2015-09-02 09:56:43 +0200 | [diff] [blame] | 46 | mnt = debugfs__mount(); |
Jiri Olsa | 592d5a6 | 2015-09-02 09:56:34 +0200 | [diff] [blame] | 47 | if (!mnt) |
| 48 | return NULL; |
| 49 | |
| 50 | __tracing_path_set("tracing/", mnt); |
| 51 | |
| 52 | return mnt; |
| 53 | } |
| 54 | |
| 55 | const char *tracing_path_mount(void) |
| 56 | { |
| 57 | const char *mnt; |
| 58 | |
| 59 | mnt = tracing_path_tracefs_mount(); |
| 60 | if (mnt) |
| 61 | return mnt; |
| 62 | |
| 63 | mnt = tracing_path_debugfs_mount(); |
| 64 | |
| 65 | return mnt; |
| 66 | } |
| 67 | |
| 68 | void tracing_path_set(const char *mntpt) |
| 69 | { |
| 70 | __tracing_path_set("tracing/", mntpt); |
| 71 | } |
| 72 | |
| 73 | char *get_tracing_file(const char *name) |
| 74 | { |
| 75 | char *file; |
| 76 | |
| 77 | if (asprintf(&file, "%s/%s", tracing_path, name) < 0) |
| 78 | return NULL; |
| 79 | |
| 80 | return file; |
| 81 | } |
| 82 | |
| 83 | void put_tracing_file(char *file) |
| 84 | { |
| 85 | free(file); |
| 86 | } |
Jiri Olsa | 988bdb3 | 2015-09-02 09:56:35 +0200 | [diff] [blame] | 87 | |
| 88 | static int strerror_open(int err, char *buf, size_t size, const char *filename) |
| 89 | { |
| 90 | char sbuf[128]; |
| 91 | |
| 92 | switch (err) { |
| 93 | case ENOENT: |
Jiri Olsa | 4f234f0 | 2015-09-02 09:56:36 +0200 | [diff] [blame] | 94 | /* |
| 95 | * We will get here if we can't find the tracepoint, but one of |
| 96 | * debugfs or tracefs is configured, which means you probably |
| 97 | * want some tracepoint which wasn't compiled in your kernel. |
| 98 | * - jirka |
| 99 | */ |
Jiri Olsa | 4605eab | 2015-09-02 09:56:43 +0200 | [diff] [blame] | 100 | if (debugfs__configured() || tracefs__configured()) { |
Jiri Olsa | 988bdb3 | 2015-09-02 09:56:35 +0200 | [diff] [blame] | 101 | snprintf(buf, size, |
| 102 | "Error:\tFile %s/%s not found.\n" |
| 103 | "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n", |
Jiri Olsa | 4f234f0 | 2015-09-02 09:56:36 +0200 | [diff] [blame] | 104 | tracing_events_path, filename); |
Jiri Olsa | 988bdb3 | 2015-09-02 09:56:35 +0200 | [diff] [blame] | 105 | break; |
| 106 | } |
| 107 | snprintf(buf, size, "%s", |
Jiri Olsa | 4f234f0 | 2015-09-02 09:56:36 +0200 | [diff] [blame] | 108 | "Error:\tUnable to find debugfs/tracefs\n" |
| 109 | "Hint:\tWas your kernel compiled with debugfs/tracefs support?\n" |
| 110 | "Hint:\tIs the debugfs/tracefs filesystem mounted?\n" |
Jiri Olsa | 988bdb3 | 2015-09-02 09:56:35 +0200 | [diff] [blame] | 111 | "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'"); |
| 112 | break; |
| 113 | case EACCES: { |
Jiri Olsa | 988bdb3 | 2015-09-02 09:56:35 +0200 | [diff] [blame] | 114 | snprintf(buf, size, |
| 115 | "Error:\tNo permissions to read %s/%s\n" |
| 116 | "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n", |
Jiri Olsa | dc240c5 | 2015-09-19 16:47:07 +0200 | [diff] [blame] | 117 | tracing_events_path, filename, tracing_mnt); |
Jiri Olsa | 988bdb3 | 2015-09-02 09:56:35 +0200 | [diff] [blame] | 118 | } |
| 119 | break; |
| 120 | default: |
| 121 | snprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf))); |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int tracing_path__strerror_open_tp(int err, char *buf, size_t size, const char *sys, const char *name) |
| 129 | { |
| 130 | char path[PATH_MAX]; |
| 131 | |
Jiri Olsa | 4f234f0 | 2015-09-02 09:56:36 +0200 | [diff] [blame] | 132 | snprintf(path, PATH_MAX, "%s/%s", sys, name ?: "*"); |
Jiri Olsa | 988bdb3 | 2015-09-02 09:56:35 +0200 | [diff] [blame] | 133 | |
| 134 | return strerror_open(err, buf, size, path); |
| 135 | } |