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