Steven Rostedt (VMware) | bcea3f9 | 2018-08-16 11:23:53 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Common code for probe-based Dynamic events. |
| 4 | * |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 5 | * This code was copied from kernel/trace/trace_kprobe.c written by |
| 6 | * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> |
| 7 | * |
| 8 | * Updates to make this generic: |
| 9 | * Copyright (C) IBM Corporation, 2010-2011 |
| 10 | * Author: Srikar Dronamraju |
| 11 | */ |
Masami Hiramatsu | 7257634 | 2017-02-07 20:21:28 +0900 | [diff] [blame] | 12 | #define pr_fmt(fmt) "trace_probe: " fmt |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 13 | |
| 14 | #include "trace_probe.h" |
| 15 | |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 16 | #undef C |
| 17 | #define C(a, b) b |
| 18 | |
| 19 | static const char *trace_probe_err_text[] = { ERRORS }; |
| 20 | |
Valdis Klētnieks | 0841625 | 2019-03-12 04:52:58 -0400 | [diff] [blame] | 21 | static const char *reserved_field_names[] = { |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 22 | "common_type", |
| 23 | "common_flags", |
| 24 | "common_preempt_count", |
| 25 | "common_pid", |
| 26 | "common_tgid", |
| 27 | FIELD_STRING_IP, |
| 28 | FIELD_STRING_RETIP, |
| 29 | FIELD_STRING_FUNC, |
| 30 | }; |
| 31 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 32 | /* Printing in basic type function template */ |
Masami Hiramatsu | 17ce3dc | 2016-08-18 17:57:50 +0900 | [diff] [blame] | 33 | #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \ |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 34 | int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 35 | { \ |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 36 | trace_seq_printf(s, fmt, *(type *)data); \ |
Steven Rostedt (Red Hat) | d2b0191 | 2014-11-12 17:19:51 -0500 | [diff] [blame] | 37 | return !trace_seq_has_overflowed(s); \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 38 | } \ |
Masami Hiramatsu | 7bfbc63 | 2018-04-25 21:17:34 +0900 | [diff] [blame] | 39 | const char PRINT_TYPE_FMT_NAME(tname)[] = fmt; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 40 | |
Masami Hiramatsu | bdca79c2 | 2016-08-18 17:59:21 +0900 | [diff] [blame] | 41 | DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u") |
| 42 | DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u") |
| 43 | DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u") |
| 44 | DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu") |
Masami Hiramatsu | 17ce3dc | 2016-08-18 17:57:50 +0900 | [diff] [blame] | 45 | DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d") |
| 46 | DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d") |
| 47 | DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d") |
| 48 | DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld") |
| 49 | DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x") |
| 50 | DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x") |
| 51 | DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x") |
| 52 | DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx") |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 53 | |
Masami Hiramatsu | 60c2e0c | 2018-04-25 21:20:28 +0900 | [diff] [blame] | 54 | int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent) |
| 55 | { |
| 56 | trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data); |
| 57 | return !trace_seq_has_overflowed(s); |
| 58 | } |
| 59 | const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS"; |
| 60 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 61 | /* Print type function for string type */ |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 62 | int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 63 | { |
| 64 | int len = *(u32 *)data >> 16; |
| 65 | |
| 66 | if (!len) |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 67 | trace_seq_puts(s, "(fault)"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 68 | else |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 69 | trace_seq_printf(s, "\"%s\"", |
Steven Rostedt (Red Hat) | d2b0191 | 2014-11-12 17:19:51 -0500 | [diff] [blame] | 70 | (const char *)get_loc_data(data, ent)); |
| 71 | return !trace_seq_has_overflowed(s); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 72 | } |
| 73 | |
Namhyung Kim | b26c74e | 2013-11-26 14:19:59 +0900 | [diff] [blame] | 74 | const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\""; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 75 | |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 76 | /* Fetch type information table */ |
| 77 | static const struct fetch_type probe_fetch_types[] = { |
| 78 | /* Special types */ |
| 79 | __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, |
| 80 | "__data_loc char[]"), |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 81 | __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, |
| 82 | "__data_loc char[]"), |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 83 | /* Basic types */ |
| 84 | ASSIGN_FETCH_TYPE(u8, u8, 0), |
| 85 | ASSIGN_FETCH_TYPE(u16, u16, 0), |
| 86 | ASSIGN_FETCH_TYPE(u32, u32, 0), |
| 87 | ASSIGN_FETCH_TYPE(u64, u64, 0), |
| 88 | ASSIGN_FETCH_TYPE(s8, u8, 1), |
| 89 | ASSIGN_FETCH_TYPE(s16, u16, 1), |
| 90 | ASSIGN_FETCH_TYPE(s32, u32, 1), |
| 91 | ASSIGN_FETCH_TYPE(s64, u64, 1), |
| 92 | ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0), |
| 93 | ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0), |
| 94 | ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0), |
| 95 | ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0), |
Masami Hiramatsu | 60c2e0c | 2018-04-25 21:20:28 +0900 | [diff] [blame] | 96 | ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0), |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 97 | |
| 98 | ASSIGN_FETCH_TYPE_END |
| 99 | }; |
| 100 | |
| 101 | static const struct fetch_type *find_fetch_type(const char *type) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 102 | { |
| 103 | int i; |
| 104 | |
| 105 | if (!type) |
| 106 | type = DEFAULT_FETCH_TYPE_STR; |
| 107 | |
| 108 | /* Special case: bitfield */ |
| 109 | if (*type == 'b') { |
| 110 | unsigned long bs; |
| 111 | |
| 112 | type = strchr(type, '/'); |
| 113 | if (!type) |
| 114 | goto fail; |
| 115 | |
| 116 | type++; |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 117 | if (kstrtoul(type, 0, &bs)) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 118 | goto fail; |
| 119 | |
| 120 | switch (bs) { |
| 121 | case 8: |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 122 | return find_fetch_type("u8"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 123 | case 16: |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 124 | return find_fetch_type("u16"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 125 | case 32: |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 126 | return find_fetch_type("u32"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 127 | case 64: |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 128 | return find_fetch_type("u64"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 129 | default: |
| 130 | goto fail; |
| 131 | } |
| 132 | } |
| 133 | |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 134 | for (i = 0; probe_fetch_types[i].name; i++) { |
| 135 | if (strcmp(type, probe_fetch_types[i].name) == 0) |
| 136 | return &probe_fetch_types[i]; |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 137 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 138 | |
| 139 | fail: |
| 140 | return NULL; |
| 141 | } |
| 142 | |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 143 | static struct trace_probe_log trace_probe_log; |
| 144 | |
| 145 | void trace_probe_log_init(const char *subsystem, int argc, const char **argv) |
| 146 | { |
| 147 | trace_probe_log.subsystem = subsystem; |
| 148 | trace_probe_log.argc = argc; |
| 149 | trace_probe_log.argv = argv; |
| 150 | trace_probe_log.index = 0; |
| 151 | } |
| 152 | |
| 153 | void trace_probe_log_clear(void) |
| 154 | { |
| 155 | memset(&trace_probe_log, 0, sizeof(trace_probe_log)); |
| 156 | } |
| 157 | |
| 158 | void trace_probe_log_set_index(int index) |
| 159 | { |
| 160 | trace_probe_log.index = index; |
| 161 | } |
| 162 | |
| 163 | void __trace_probe_log_err(int offset, int err_type) |
| 164 | { |
| 165 | char *command, *p; |
| 166 | int i, len = 0, pos = 0; |
| 167 | |
| 168 | if (!trace_probe_log.argv) |
| 169 | return; |
| 170 | |
| 171 | /* Recalcurate the length and allocate buffer */ |
| 172 | for (i = 0; i < trace_probe_log.argc; i++) { |
| 173 | if (i == trace_probe_log.index) |
| 174 | pos = len; |
| 175 | len += strlen(trace_probe_log.argv[i]) + 1; |
| 176 | } |
| 177 | command = kzalloc(len, GFP_KERNEL); |
| 178 | if (!command) |
| 179 | return; |
| 180 | |
| 181 | /* And make a command string from argv array */ |
| 182 | p = command; |
| 183 | for (i = 0; i < trace_probe_log.argc; i++) { |
| 184 | len = strlen(trace_probe_log.argv[i]); |
| 185 | strcpy(p, trace_probe_log.argv[i]); |
| 186 | p[len] = ' '; |
| 187 | p += len + 1; |
| 188 | } |
| 189 | *(p - 1) = '\0'; |
| 190 | |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 191 | tracing_log_err(NULL, trace_probe_log.subsystem, command, |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 192 | trace_probe_err_text, err_type, pos + offset); |
| 193 | |
| 194 | kfree(command); |
| 195 | } |
| 196 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 197 | /* Split symbol and offset. */ |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 198 | int traceprobe_split_symbol_offset(char *symbol, long *offset) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 199 | { |
| 200 | char *tmp; |
| 201 | int ret; |
| 202 | |
| 203 | if (!offset) |
| 204 | return -EINVAL; |
| 205 | |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 206 | tmp = strpbrk(symbol, "+-"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 207 | if (tmp) { |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 208 | ret = kstrtol(tmp, 0, offset); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 209 | if (ret) |
| 210 | return ret; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 211 | *tmp = '\0'; |
| 212 | } else |
| 213 | *offset = 0; |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 218 | /* @buf must has MAX_EVENT_NAME_LEN size */ |
| 219 | int traceprobe_parse_event_name(const char **pevent, const char **pgroup, |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 220 | char *buf, int offset) |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 221 | { |
| 222 | const char *slash, *event = *pevent; |
Masami Hiramatsu | dec65d79 | 2019-03-14 13:30:20 +0900 | [diff] [blame] | 223 | int len; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 224 | |
| 225 | slash = strchr(event, '/'); |
| 226 | if (slash) { |
| 227 | if (slash == event) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 228 | trace_probe_log_err(offset, NO_GROUP_NAME); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 229 | return -EINVAL; |
| 230 | } |
| 231 | if (slash - event + 1 > MAX_EVENT_NAME_LEN) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 232 | trace_probe_log_err(offset, GROUP_TOO_LONG); |
| 233 | return -EINVAL; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 234 | } |
| 235 | strlcpy(buf, event, slash - event + 1); |
Masami Hiramatsu | 5b7a962 | 2019-03-14 13:30:40 +0900 | [diff] [blame] | 236 | if (!is_good_name(buf)) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 237 | trace_probe_log_err(offset, BAD_GROUP_NAME); |
Masami Hiramatsu | 5b7a962 | 2019-03-14 13:30:40 +0900 | [diff] [blame] | 238 | return -EINVAL; |
| 239 | } |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 240 | *pgroup = buf; |
| 241 | *pevent = slash + 1; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 242 | offset += slash - event + 1; |
Masami Hiramatsu | dec65d79 | 2019-03-14 13:30:20 +0900 | [diff] [blame] | 243 | event = *pevent; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 244 | } |
Masami Hiramatsu | dec65d79 | 2019-03-14 13:30:20 +0900 | [diff] [blame] | 245 | len = strlen(event); |
| 246 | if (len == 0) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 247 | trace_probe_log_err(offset, NO_EVENT_NAME); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 248 | return -EINVAL; |
Masami Hiramatsu | dec65d79 | 2019-03-14 13:30:20 +0900 | [diff] [blame] | 249 | } else if (len > MAX_EVENT_NAME_LEN) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 250 | trace_probe_log_err(offset, EVENT_TOO_LONG); |
| 251 | return -EINVAL; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 252 | } |
Masami Hiramatsu | 5b7a962 | 2019-03-14 13:30:40 +0900 | [diff] [blame] | 253 | if (!is_good_name(event)) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 254 | trace_probe_log_err(offset, BAD_EVENT_NAME); |
Masami Hiramatsu | 5b7a962 | 2019-03-14 13:30:40 +0900 | [diff] [blame] | 255 | return -EINVAL; |
| 256 | } |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 260 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) |
| 261 | |
| 262 | static int parse_probe_vars(char *arg, const struct fetch_type *t, |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 263 | struct fetch_insn *code, unsigned int flags, int offs) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 264 | { |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 265 | unsigned long param; |
Steven Rostedt (VMware) | 3d739c1 | 2018-12-21 23:10:26 -0500 | [diff] [blame] | 266 | int ret = 0; |
| 267 | int len; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 268 | |
| 269 | if (strcmp(arg, "retval") == 0) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 270 | if (flags & TPARG_FL_RETURN) { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 271 | code->op = FETCH_OP_RETVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 272 | } else { |
| 273 | trace_probe_log_err(offs, RETVAL_ON_PROBE); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 274 | ret = -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 275 | } |
Steven Rostedt (VMware) | 3d739c1 | 2018-12-21 23:10:26 -0500 | [diff] [blame] | 276 | } else if ((len = str_has_prefix(arg, "stack"))) { |
| 277 | if (arg[len] == '\0') { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 278 | code->op = FETCH_OP_STACKP; |
Steven Rostedt (VMware) | 3d739c1 | 2018-12-21 23:10:26 -0500 | [diff] [blame] | 279 | } else if (isdigit(arg[len])) { |
| 280 | ret = kstrtoul(arg + len, 10, ¶m); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 281 | if (ret) { |
| 282 | goto inval_var; |
| 283 | } else if ((flags & TPARG_FL_KERNEL) && |
| 284 | param > PARAM_MAX_STACK) { |
| 285 | trace_probe_log_err(offs, BAD_STACK_NUM); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 286 | ret = -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 287 | } else { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 288 | code->op = FETCH_OP_STACK; |
| 289 | code->param = (unsigned int)param; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 290 | } |
| 291 | } else |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 292 | goto inval_var; |
Omar Sandoval | 35abb67 | 2016-06-08 18:38:02 -0700 | [diff] [blame] | 293 | } else if (strcmp(arg, "comm") == 0) { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 294 | code->op = FETCH_OP_COMM; |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 295 | #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API |
| 296 | } else if (((flags & TPARG_FL_MASK) == |
| 297 | (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) && |
Steven Rostedt (VMware) | 3d739c1 | 2018-12-21 23:10:26 -0500 | [diff] [blame] | 298 | (len = str_has_prefix(arg, "arg"))) { |
Steven Rostedt (VMware) | 3d739c1 | 2018-12-21 23:10:26 -0500 | [diff] [blame] | 299 | ret = kstrtoul(arg + len, 10, ¶m); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 300 | if (ret) { |
| 301 | goto inval_var; |
| 302 | } else if (!param || param > PARAM_MAX_STACK) { |
| 303 | trace_probe_log_err(offs, BAD_ARG_NUM); |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 304 | return -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 305 | } |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 306 | code->op = FETCH_OP_ARG; |
| 307 | code->param = (unsigned int)param - 1; |
| 308 | #endif |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 309 | } else |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 310 | goto inval_var; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 311 | |
| 312 | return ret; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 313 | |
| 314 | inval_var: |
| 315 | trace_probe_log_err(offs, BAD_VAR); |
| 316 | return -EINVAL; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /* Recursive argument parser */ |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 320 | static int |
| 321 | parse_probe_arg(char *arg, const struct fetch_type *type, |
| 322 | struct fetch_insn **pcode, struct fetch_insn *end, |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 323 | unsigned int flags, int offs) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 324 | { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 325 | struct fetch_insn *code = *pcode; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 326 | unsigned long param; |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 327 | int deref = FETCH_OP_DEREF; |
Steven Rostedt (VMware) | bf173ca | 2018-10-12 12:50:22 -0400 | [diff] [blame] | 328 | long offset = 0; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 329 | char *tmp; |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 330 | int ret = 0; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 331 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 332 | switch (arg[0]) { |
| 333 | case '$': |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 334 | ret = parse_probe_vars(arg + 1, type, code, flags, offs); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 335 | break; |
| 336 | |
| 337 | case '%': /* named register */ |
| 338 | ret = regs_query_register_offset(arg + 1); |
| 339 | if (ret >= 0) { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 340 | code->op = FETCH_OP_REG; |
| 341 | code->param = (unsigned int)ret; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 342 | ret = 0; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 343 | } else |
| 344 | trace_probe_log_err(offs, BAD_REG_NAME); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 345 | break; |
| 346 | |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 347 | case '@': /* memory, file-offset or symbol */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 348 | if (isdigit(arg[1])) { |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 349 | ret = kstrtoul(arg + 1, 0, ¶m); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 350 | if (ret) { |
| 351 | trace_probe_log_err(offs, BAD_MEM_ADDR); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 352 | break; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 353 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 354 | /* load address */ |
| 355 | code->op = FETCH_OP_IMM; |
| 356 | code->immediate = param; |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 357 | } else if (arg[1] == '+') { |
| 358 | /* kprobes don't support file offsets */ |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 359 | if (flags & TPARG_FL_KERNEL) { |
| 360 | trace_probe_log_err(offs, FILE_ON_KPROBE); |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 361 | return -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 362 | } |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 363 | ret = kstrtol(arg + 2, 0, &offset); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 364 | if (ret) { |
| 365 | trace_probe_log_err(offs, BAD_FILE_OFFS); |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 366 | break; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 367 | } |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 368 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 369 | code->op = FETCH_OP_FOFFS; |
| 370 | code->immediate = (unsigned long)offset; // imm64? |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 371 | } else { |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 372 | /* uprobes don't support symbols */ |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 373 | if (!(flags & TPARG_FL_KERNEL)) { |
| 374 | trace_probe_log_err(offs, SYM_ON_UPROBE); |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 375 | return -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 376 | } |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 377 | /* Preserve symbol for updating */ |
| 378 | code->op = FETCH_NOP_SYMBOL; |
| 379 | code->data = kstrdup(arg + 1, GFP_KERNEL); |
| 380 | if (!code->data) |
| 381 | return -ENOMEM; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 382 | if (++code == end) { |
| 383 | trace_probe_log_err(offs, TOO_MANY_OPS); |
| 384 | return -EINVAL; |
| 385 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 386 | code->op = FETCH_OP_IMM; |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 387 | code->immediate = 0; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 388 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 389 | /* These are fetching from memory */ |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 390 | if (++code == end) { |
| 391 | trace_probe_log_err(offs, TOO_MANY_OPS); |
| 392 | return -EINVAL; |
| 393 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 394 | *pcode = code; |
| 395 | code->op = FETCH_OP_DEREF; |
| 396 | code->offset = offset; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 397 | break; |
| 398 | |
| 399 | case '+': /* deref memory */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 400 | case '-': |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 401 | if (arg[1] == 'u') { |
| 402 | deref = FETCH_OP_UDEREF; |
| 403 | arg[1] = arg[0]; |
| 404 | arg++; |
| 405 | } |
| 406 | if (arg[0] == '+') |
| 407 | arg++; /* Skip '+', because kstrtol() rejects it. */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 408 | tmp = strchr(arg, '('); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 409 | if (!tmp) { |
| 410 | trace_probe_log_err(offs, DEREF_NEED_BRACE); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 411 | return -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 412 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 413 | *tmp = '\0'; |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 414 | ret = kstrtol(arg, 0, &offset); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 415 | if (ret) { |
| 416 | trace_probe_log_err(offs, BAD_DEREF_OFFS); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 417 | break; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 418 | } |
| 419 | offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 420 | arg = tmp + 1; |
| 421 | tmp = strrchr(arg, ')'); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 422 | if (!tmp) { |
| 423 | trace_probe_log_err(offs + strlen(arg), |
| 424 | DEREF_OPEN_BRACE); |
| 425 | return -EINVAL; |
| 426 | } else { |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 427 | const struct fetch_type *t2 = find_fetch_type(NULL); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 428 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 429 | *tmp = '\0'; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 430 | ret = parse_probe_arg(arg, t2, &code, end, flags, offs); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 431 | if (ret) |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 432 | break; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 433 | if (code->op == FETCH_OP_COMM) { |
| 434 | trace_probe_log_err(offs, COMM_CANT_DEREF); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 435 | return -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 436 | } |
| 437 | if (++code == end) { |
| 438 | trace_probe_log_err(offs, TOO_MANY_OPS); |
| 439 | return -EINVAL; |
| 440 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 441 | *pcode = code; |
| 442 | |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 443 | code->op = deref; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 444 | code->offset = offset; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 445 | } |
| 446 | break; |
| 447 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 448 | if (!ret && code->op == FETCH_OP_NOP) { |
| 449 | /* Parsed, but do not find fetch method */ |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 450 | trace_probe_log_err(offs, BAD_FETCH_ARG); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 451 | ret = -EINVAL; |
| 452 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long)) |
| 457 | |
| 458 | /* Bitfield type needs to be parsed into a fetch function */ |
| 459 | static int __parse_bitfield_probe_arg(const char *bf, |
| 460 | const struct fetch_type *t, |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 461 | struct fetch_insn **pcode) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 462 | { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 463 | struct fetch_insn *code = *pcode; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 464 | unsigned long bw, bo; |
| 465 | char *tail; |
| 466 | |
| 467 | if (*bf != 'b') |
| 468 | return 0; |
| 469 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 470 | bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */ |
| 471 | |
| 472 | if (bw == 0 || *tail != '@') |
| 473 | return -EINVAL; |
| 474 | |
| 475 | bf = tail + 1; |
| 476 | bo = simple_strtoul(bf, &tail, 0); |
| 477 | |
| 478 | if (tail == bf || *tail != '/') |
| 479 | return -EINVAL; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 480 | code++; |
| 481 | if (code->op != FETCH_OP_NOP) |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 482 | return -EINVAL; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 483 | *pcode = code; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 484 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 485 | code->op = FETCH_OP_MOD_BF; |
| 486 | code->lshift = BYTES_TO_BITS(t->size) - (bw + bo); |
| 487 | code->rshift = BYTES_TO_BITS(t->size) - bw; |
| 488 | code->basesize = t->size; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 489 | |
| 490 | return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0; |
| 491 | } |
| 492 | |
| 493 | /* String length checking wrapper */ |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 494 | static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size, |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 495 | struct probe_arg *parg, unsigned int flags, int offset) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 496 | { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 497 | struct fetch_insn *code, *scode, *tmp = NULL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 498 | char *t, *t2, *t3; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 499 | int ret, len; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 500 | |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 501 | len = strlen(arg); |
| 502 | if (len > MAX_ARGSTR_LEN) { |
| 503 | trace_probe_log_err(offset, ARG_TOO_LONG); |
| 504 | return -EINVAL; |
| 505 | } else if (len == 0) { |
| 506 | trace_probe_log_err(offset, NO_ARG_BODY); |
| 507 | return -EINVAL; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 508 | } |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 509 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 510 | parg->comm = kstrdup(arg, GFP_KERNEL); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 511 | if (!parg->comm) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 512 | return -ENOMEM; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 513 | |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 514 | t = strchr(arg, ':'); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 515 | if (t) { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 516 | *t = '\0'; |
| 517 | t2 = strchr(++t, '['); |
| 518 | if (t2) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 519 | *t2++ = '\0'; |
| 520 | t3 = strchr(t2, ']'); |
| 521 | if (!t3) { |
| 522 | offset += t2 + strlen(t2) - arg; |
| 523 | trace_probe_log_err(offset, |
| 524 | ARRAY_NO_CLOSE); |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 525 | return -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 526 | } else if (t3[1] != '\0') { |
| 527 | trace_probe_log_err(offset + t3 + 1 - arg, |
| 528 | BAD_ARRAY_SUFFIX); |
| 529 | return -EINVAL; |
| 530 | } |
| 531 | *t3 = '\0'; |
| 532 | if (kstrtouint(t2, 0, &parg->count) || !parg->count) { |
| 533 | trace_probe_log_err(offset + t2 - arg, |
| 534 | BAD_ARRAY_NUM); |
| 535 | return -EINVAL; |
| 536 | } |
| 537 | if (parg->count > MAX_ARRAY_LEN) { |
| 538 | trace_probe_log_err(offset + t2 - arg, |
| 539 | ARRAY_TOO_BIG); |
| 540 | return -EINVAL; |
| 541 | } |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 542 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 543 | } |
Masami Hiramatsu | 3dd1f7f | 2019-05-07 22:56:02 +0900 | [diff] [blame] | 544 | |
| 545 | /* Since $comm can not be dereferred, we can find $comm by strcmp */ |
| 546 | if (strcmp(arg, "$comm") == 0) { |
| 547 | /* The type of $comm must be "string", and not an array. */ |
| 548 | if (parg->count || (t && strcmp(t, "string"))) |
| 549 | return -EINVAL; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 550 | parg->type = find_fetch_type("string"); |
Masami Hiramatsu | 3dd1f7f | 2019-05-07 22:56:02 +0900 | [diff] [blame] | 551 | } else |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 552 | parg->type = find_fetch_type(t); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 553 | if (!parg->type) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 554 | trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 555 | return -EINVAL; |
| 556 | } |
| 557 | parg->offset = *size; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 558 | *size += parg->type->size * (parg->count ?: 1); |
| 559 | |
| 560 | if (parg->count) { |
| 561 | len = strlen(parg->type->fmttype) + 6; |
| 562 | parg->fmt = kmalloc(len, GFP_KERNEL); |
| 563 | if (!parg->fmt) |
| 564 | return -ENOMEM; |
| 565 | snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype, |
| 566 | parg->count); |
| 567 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 568 | |
Gustavo A. R. Silva | 8623b00 | 2019-01-14 22:34:08 -0600 | [diff] [blame] | 569 | code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 570 | if (!code) |
| 571 | return -ENOMEM; |
| 572 | code[FETCH_INSN_MAX - 1].op = FETCH_OP_END; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 573 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 574 | ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1], |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 575 | flags, offset); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 576 | if (ret) |
| 577 | goto fail; |
| 578 | |
| 579 | /* Store operation */ |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 580 | if (!strcmp(parg->type->name, "string") || |
| 581 | !strcmp(parg->type->name, "ustring")) { |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 582 | if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF && |
| 583 | code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 584 | trace_probe_log_err(offset + (t ? (t - arg) : 0), |
| 585 | BAD_STRING); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 586 | ret = -EINVAL; |
| 587 | goto fail; |
| 588 | } |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 589 | if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM) || |
| 590 | parg->count) { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 591 | /* |
| 592 | * IMM and COMM is pointing actual address, those must |
| 593 | * be kept, and if parg->count != 0, this is an array |
| 594 | * of string pointers instead of string address itself. |
| 595 | */ |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 596 | code++; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 597 | if (code->op != FETCH_OP_NOP) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 598 | trace_probe_log_err(offset, TOO_MANY_OPS); |
| 599 | ret = -EINVAL; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 600 | goto fail; |
| 601 | } |
| 602 | } |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 603 | /* If op == DEREF, replace it with STRING */ |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 604 | if (!strcmp(parg->type->name, "ustring") || |
| 605 | code->op == FETCH_OP_UDEREF) |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 606 | code->op = FETCH_OP_ST_USTRING; |
| 607 | else |
| 608 | code->op = FETCH_OP_ST_STRING; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 609 | code->size = parg->type->size; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 610 | parg->dynamic = true; |
| 611 | } else if (code->op == FETCH_OP_DEREF) { |
| 612 | code->op = FETCH_OP_ST_MEM; |
| 613 | code->size = parg->type->size; |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 614 | } else if (code->op == FETCH_OP_UDEREF) { |
| 615 | code->op = FETCH_OP_ST_UMEM; |
| 616 | code->size = parg->type->size; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 617 | } else { |
| 618 | code++; |
| 619 | if (code->op != FETCH_OP_NOP) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 620 | trace_probe_log_err(offset, TOO_MANY_OPS); |
| 621 | ret = -EINVAL; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 622 | goto fail; |
| 623 | } |
| 624 | code->op = FETCH_OP_ST_RAW; |
| 625 | code->size = parg->type->size; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 626 | } |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 627 | scode = code; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 628 | /* Modify operation */ |
| 629 | if (t != NULL) { |
| 630 | ret = __parse_bitfield_probe_arg(t, parg->type, &code); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 631 | if (ret) { |
| 632 | trace_probe_log_err(offset + t - arg, BAD_BITFIELD); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 633 | goto fail; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 634 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 635 | } |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 636 | /* Loop(Array) operation */ |
| 637 | if (parg->count) { |
| 638 | if (scode->op != FETCH_OP_ST_MEM && |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 639 | scode->op != FETCH_OP_ST_STRING && |
| 640 | scode->op != FETCH_OP_ST_USTRING) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 641 | trace_probe_log_err(offset + (t ? (t - arg) : 0), |
| 642 | BAD_STRING); |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 643 | ret = -EINVAL; |
| 644 | goto fail; |
| 645 | } |
| 646 | code++; |
| 647 | if (code->op != FETCH_OP_NOP) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 648 | trace_probe_log_err(offset, TOO_MANY_OPS); |
| 649 | ret = -EINVAL; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 650 | goto fail; |
| 651 | } |
| 652 | code->op = FETCH_OP_LP_ARRAY; |
| 653 | code->param = parg->count; |
| 654 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 655 | code++; |
| 656 | code->op = FETCH_OP_END; |
| 657 | |
| 658 | /* Shrink down the code buffer */ |
Gustavo A. R. Silva | 8623b00 | 2019-01-14 22:34:08 -0600 | [diff] [blame] | 659 | parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 660 | if (!parg->code) |
| 661 | ret = -ENOMEM; |
| 662 | else |
| 663 | memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1)); |
| 664 | |
| 665 | fail: |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 666 | if (ret) { |
| 667 | for (code = tmp; code < tmp + FETCH_INSN_MAX; code++) |
| 668 | if (code->op == FETCH_NOP_SYMBOL) |
| 669 | kfree(code->data); |
| 670 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 671 | kfree(tmp); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 672 | |
| 673 | return ret; |
| 674 | } |
| 675 | |
| 676 | /* Return 1 if name is reserved or already used by another argument */ |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 677 | static int traceprobe_conflict_field_name(const char *name, |
| 678 | struct probe_arg *args, int narg) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 679 | { |
| 680 | int i; |
| 681 | |
| 682 | for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) |
| 683 | if (strcmp(reserved_field_names[i], name) == 0) |
| 684 | return 1; |
| 685 | |
| 686 | for (i = 0; i < narg; i++) |
| 687 | if (strcmp(args[i].name, name) == 0) |
| 688 | return 1; |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 693 | int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, char *arg, |
| 694 | unsigned int flags) |
| 695 | { |
| 696 | struct probe_arg *parg = &tp->args[i]; |
| 697 | char *body; |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 698 | |
| 699 | /* Increment count for freeing args in error case */ |
| 700 | tp->nr_args++; |
| 701 | |
| 702 | body = strchr(arg, '='); |
| 703 | if (body) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 704 | if (body - arg > MAX_ARG_NAME_LEN) { |
| 705 | trace_probe_log_err(0, ARG_NAME_TOO_LONG); |
Masami Hiramatsu | b4443c1 | 2019-03-14 13:30:30 +0900 | [diff] [blame] | 706 | return -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 707 | } else if (body == arg) { |
| 708 | trace_probe_log_err(0, NO_ARG_NAME); |
| 709 | return -EINVAL; |
| 710 | } |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 711 | parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL); |
| 712 | body++; |
| 713 | } else { |
| 714 | /* If argument name is omitted, set "argN" */ |
| 715 | parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1); |
| 716 | body = arg; |
| 717 | } |
| 718 | if (!parg->name) |
| 719 | return -ENOMEM; |
| 720 | |
| 721 | if (!is_good_name(parg->name)) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 722 | trace_probe_log_err(0, BAD_ARG_NAME); |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 723 | return -EINVAL; |
| 724 | } |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 725 | if (traceprobe_conflict_field_name(parg->name, tp->args, i)) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 726 | trace_probe_log_err(0, USED_ARG_NAME); |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 727 | return -EINVAL; |
| 728 | } |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 729 | /* Parse fetch argument */ |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 730 | return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags, |
| 731 | body - arg); |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 732 | } |
| 733 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 734 | void traceprobe_free_probe_arg(struct probe_arg *arg) |
| 735 | { |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 736 | struct fetch_insn *code = arg->code; |
| 737 | |
| 738 | while (code && code->op != FETCH_OP_END) { |
| 739 | if (code->op == FETCH_NOP_SYMBOL) |
| 740 | kfree(code->data); |
| 741 | code++; |
| 742 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 743 | kfree(arg->code); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 744 | kfree(arg->name); |
| 745 | kfree(arg->comm); |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 746 | kfree(arg->fmt); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 747 | } |
| 748 | |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 749 | int traceprobe_update_arg(struct probe_arg *arg) |
| 750 | { |
| 751 | struct fetch_insn *code = arg->code; |
| 752 | long offset; |
| 753 | char *tmp; |
| 754 | char c; |
| 755 | int ret = 0; |
| 756 | |
| 757 | while (code && code->op != FETCH_OP_END) { |
| 758 | if (code->op == FETCH_NOP_SYMBOL) { |
| 759 | if (code[1].op != FETCH_OP_IMM) |
| 760 | return -EINVAL; |
| 761 | |
Masami Hiramatsu | ee474b8 | 2018-11-01 23:29:28 +0900 | [diff] [blame] | 762 | tmp = strpbrk(code->data, "+-"); |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 763 | if (tmp) |
| 764 | c = *tmp; |
| 765 | ret = traceprobe_split_symbol_offset(code->data, |
| 766 | &offset); |
| 767 | if (ret) |
| 768 | return ret; |
| 769 | |
| 770 | code[1].immediate = |
| 771 | (unsigned long)kallsyms_lookup_name(code->data); |
| 772 | if (tmp) |
| 773 | *tmp = c; |
| 774 | if (!code[1].immediate) |
| 775 | return -ENOENT; |
| 776 | code[1].immediate += offset; |
| 777 | } |
| 778 | code++; |
| 779 | } |
| 780 | return 0; |
| 781 | } |
| 782 | |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 783 | /* When len=0, we just calculate the needed length */ |
| 784 | #define LEN_OR_ZERO (len ? len - pos : 0) |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 785 | static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, |
| 786 | bool is_return) |
| 787 | { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 788 | struct probe_arg *parg; |
| 789 | int i, j; |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 790 | int pos = 0; |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 791 | const char *fmt, *arg; |
| 792 | |
| 793 | if (!is_return) { |
| 794 | fmt = "(%lx)"; |
| 795 | arg = "REC->" FIELD_STRING_IP; |
| 796 | } else { |
| 797 | fmt = "(%lx <- %lx)"; |
| 798 | arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; |
| 799 | } |
| 800 | |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 801 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); |
| 802 | |
| 803 | for (i = 0; i < tp->nr_args; i++) { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 804 | parg = tp->args + i; |
| 805 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name); |
| 806 | if (parg->count) { |
| 807 | pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s", |
| 808 | parg->type->fmt); |
| 809 | for (j = 1; j < parg->count; j++) |
| 810 | pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s", |
| 811 | parg->type->fmt); |
| 812 | pos += snprintf(buf + pos, LEN_OR_ZERO, "}"); |
| 813 | } else |
| 814 | pos += snprintf(buf + pos, LEN_OR_ZERO, "%s", |
| 815 | parg->type->fmt); |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); |
| 819 | |
| 820 | for (i = 0; i < tp->nr_args; i++) { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 821 | parg = tp->args + i; |
| 822 | if (parg->count) { |
| 823 | if (strcmp(parg->type->name, "string") == 0) |
| 824 | fmt = ", __get_str(%s[%d])"; |
| 825 | else |
| 826 | fmt = ", REC->%s[%d]"; |
| 827 | for (j = 0; j < parg->count; j++) |
| 828 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
| 829 | fmt, parg->name, j); |
| 830 | } else { |
| 831 | if (strcmp(parg->type->name, "string") == 0) |
| 832 | fmt = ", __get_str(%s)"; |
| 833 | else |
| 834 | fmt = ", REC->%s"; |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 835 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 836 | fmt, parg->name); |
| 837 | } |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 838 | } |
| 839 | |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 840 | /* return the length of print_fmt */ |
| 841 | return pos; |
| 842 | } |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 843 | #undef LEN_OR_ZERO |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 844 | |
Masami Hiramatsu | 0a46c85 | 2018-04-25 21:19:30 +0900 | [diff] [blame] | 845 | int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return) |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 846 | { |
| 847 | int len; |
| 848 | char *print_fmt; |
| 849 | |
| 850 | /* First: called with 0 length to calculate the needed length */ |
| 851 | len = __set_print_fmt(tp, NULL, 0, is_return); |
| 852 | print_fmt = kmalloc(len + 1, GFP_KERNEL); |
| 853 | if (!print_fmt) |
| 854 | return -ENOMEM; |
| 855 | |
| 856 | /* Second: actually write the @print_fmt */ |
| 857 | __set_print_fmt(tp, print_fmt, len + 1, is_return); |
| 858 | tp->call.print_fmt = print_fmt; |
| 859 | |
| 860 | return 0; |
| 861 | } |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 862 | |
| 863 | int traceprobe_define_arg_fields(struct trace_event_call *event_call, |
| 864 | size_t offset, struct trace_probe *tp) |
| 865 | { |
| 866 | int ret, i; |
| 867 | |
| 868 | /* Set argument names as fields */ |
| 869 | for (i = 0; i < tp->nr_args; i++) { |
| 870 | struct probe_arg *parg = &tp->args[i]; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 871 | const char *fmt = parg->type->fmttype; |
| 872 | int size = parg->type->size; |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 873 | |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 874 | if (parg->fmt) |
| 875 | fmt = parg->fmt; |
| 876 | if (parg->count) |
| 877 | size *= parg->count; |
| 878 | ret = trace_define_field(event_call, fmt, parg->name, |
| 879 | offset + parg->offset, size, |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 880 | parg->type->is_signed, |
| 881 | FILTER_OTHER); |
| 882 | if (ret) |
| 883 | return ret; |
| 884 | } |
| 885 | return 0; |
| 886 | } |
Masami Hiramatsu | 455b289 | 2019-06-01 00:17:06 +0900 | [diff] [blame] | 887 | |
| 888 | |
| 889 | void trace_probe_cleanup(struct trace_probe *tp) |
| 890 | { |
| 891 | int i; |
| 892 | |
| 893 | for (i = 0; i < tp->nr_args; i++) |
| 894 | traceprobe_free_probe_arg(&tp->args[i]); |
| 895 | |
| 896 | kfree(tp->call.class->system); |
| 897 | kfree(tp->call.name); |
| 898 | kfree(tp->call.print_fmt); |
| 899 | } |
| 900 | |
| 901 | int trace_probe_init(struct trace_probe *tp, const char *event, |
| 902 | const char *group) |
| 903 | { |
| 904 | if (!event || !group) |
| 905 | return -EINVAL; |
| 906 | |
| 907 | tp->call.class = &tp->class; |
| 908 | tp->call.name = kstrdup(event, GFP_KERNEL); |
| 909 | if (!tp->call.name) |
| 910 | return -ENOMEM; |
| 911 | |
| 912 | tp->class.system = kstrdup(group, GFP_KERNEL); |
| 913 | if (!tp->class.system) { |
| 914 | kfree(tp->call.name); |
| 915 | tp->call.name = NULL; |
| 916 | return -ENOMEM; |
| 917 | } |
| 918 | INIT_LIST_HEAD(&tp->files); |
| 919 | INIT_LIST_HEAD(&tp->class.fields); |
| 920 | |
| 921 | return 0; |
| 922 | } |
Masami Hiramatsu | 46e5376 | 2019-06-01 00:17:16 +0900 | [diff] [blame] | 923 | |
| 924 | int trace_probe_register_event_call(struct trace_probe *tp) |
| 925 | { |
| 926 | struct trace_event_call *call = &tp->call; |
| 927 | int ret; |
| 928 | |
| 929 | ret = register_trace_event(&call->event); |
| 930 | if (!ret) |
| 931 | return -ENODEV; |
| 932 | |
| 933 | ret = trace_add_event_call(call); |
| 934 | if (ret) |
| 935 | unregister_trace_event(&call->event); |
| 936 | |
| 937 | return ret; |
| 938 | } |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame^] | 939 | |
| 940 | int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file) |
| 941 | { |
| 942 | struct event_file_link *link; |
| 943 | |
| 944 | link = kmalloc(sizeof(*link), GFP_KERNEL); |
| 945 | if (!link) |
| 946 | return -ENOMEM; |
| 947 | |
| 948 | link->file = file; |
| 949 | INIT_LIST_HEAD(&link->list); |
| 950 | list_add_tail_rcu(&link->list, &tp->files); |
| 951 | tp->flags |= TP_FLAG_TRACE; |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp, |
| 956 | struct trace_event_file *file) |
| 957 | { |
| 958 | struct event_file_link *link; |
| 959 | |
| 960 | trace_probe_for_each_link(link, tp) { |
| 961 | if (link->file == file) |
| 962 | return link; |
| 963 | } |
| 964 | |
| 965 | return NULL; |
| 966 | } |
| 967 | |
| 968 | int trace_probe_remove_file(struct trace_probe *tp, |
| 969 | struct trace_event_file *file) |
| 970 | { |
| 971 | struct event_file_link *link; |
| 972 | |
| 973 | link = trace_probe_get_file_link(tp, file); |
| 974 | if (!link) |
| 975 | return -ENOENT; |
| 976 | |
| 977 | list_del_rcu(&link->list); |
| 978 | synchronize_rcu(); |
| 979 | kfree(link); |
| 980 | |
| 981 | if (list_empty(&tp->files)) |
| 982 | tp->flags &= ~TP_FLAG_TRACE; |
| 983 | |
| 984 | return 0; |
| 985 | } |