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 | |
Ingo Molnar | f2cc020 | 2021-03-23 18:49:35 +0100 | [diff] [blame] | 171 | /* Recalculate the length and allocate buffer */ |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 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 | |
Masami Hiramatsu | d2aea95 | 2019-09-28 05:53:29 -0400 | [diff] [blame] | 181 | if (trace_probe_log.index >= trace_probe_log.argc) { |
| 182 | /** |
| 183 | * Set the error position is next to the last arg + space. |
| 184 | * Note that len includes the terminal null and the cursor |
Ingo Molnar | f2cc020 | 2021-03-23 18:49:35 +0100 | [diff] [blame] | 185 | * appears at pos + 1. |
Masami Hiramatsu | d2aea95 | 2019-09-28 05:53:29 -0400 | [diff] [blame] | 186 | */ |
| 187 | pos = len; |
| 188 | offset = 0; |
| 189 | } |
| 190 | |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 191 | /* And make a command string from argv array */ |
| 192 | p = command; |
| 193 | for (i = 0; i < trace_probe_log.argc; i++) { |
| 194 | len = strlen(trace_probe_log.argv[i]); |
| 195 | strcpy(p, trace_probe_log.argv[i]); |
| 196 | p[len] = ' '; |
| 197 | p += len + 1; |
| 198 | } |
| 199 | *(p - 1) = '\0'; |
| 200 | |
Steven Rostedt (VMware) | 2f754e7 | 2019-04-01 22:52:21 -0400 | [diff] [blame] | 201 | tracing_log_err(NULL, trace_probe_log.subsystem, command, |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 202 | trace_probe_err_text, err_type, pos + offset); |
| 203 | |
| 204 | kfree(command); |
| 205 | } |
| 206 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 207 | /* Split symbol and offset. */ |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 208 | int traceprobe_split_symbol_offset(char *symbol, long *offset) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 209 | { |
| 210 | char *tmp; |
| 211 | int ret; |
| 212 | |
| 213 | if (!offset) |
| 214 | return -EINVAL; |
| 215 | |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 216 | tmp = strpbrk(symbol, "+-"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 217 | if (tmp) { |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 218 | ret = kstrtol(tmp, 0, offset); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 219 | if (ret) |
| 220 | return ret; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 221 | *tmp = '\0'; |
| 222 | } else |
| 223 | *offset = 0; |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 228 | /* @buf must has MAX_EVENT_NAME_LEN size */ |
| 229 | int traceprobe_parse_event_name(const char **pevent, const char **pgroup, |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 230 | char *buf, int offset) |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 231 | { |
| 232 | const char *slash, *event = *pevent; |
Masami Hiramatsu | dec65d79 | 2019-03-14 13:30:20 +0900 | [diff] [blame] | 233 | int len; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 234 | |
| 235 | slash = strchr(event, '/'); |
Steven Rostedt (VMware) | bc1b973 | 2021-08-16 23:42:59 -0400 | [diff] [blame] | 236 | if (!slash) |
| 237 | slash = strchr(event, '.'); |
| 238 | |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 239 | if (slash) { |
| 240 | if (slash == event) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 241 | trace_probe_log_err(offset, NO_GROUP_NAME); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 242 | return -EINVAL; |
| 243 | } |
| 244 | if (slash - event + 1 > MAX_EVENT_NAME_LEN) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 245 | trace_probe_log_err(offset, GROUP_TOO_LONG); |
| 246 | return -EINVAL; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 247 | } |
| 248 | strlcpy(buf, event, slash - event + 1); |
Masami Hiramatsu | 5b7a962 | 2019-03-14 13:30:40 +0900 | [diff] [blame] | 249 | if (!is_good_name(buf)) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 250 | trace_probe_log_err(offset, BAD_GROUP_NAME); |
Masami Hiramatsu | 5b7a962 | 2019-03-14 13:30:40 +0900 | [diff] [blame] | 251 | return -EINVAL; |
| 252 | } |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 253 | *pgroup = buf; |
| 254 | *pevent = slash + 1; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 255 | offset += slash - event + 1; |
Masami Hiramatsu | dec65d79 | 2019-03-14 13:30:20 +0900 | [diff] [blame] | 256 | event = *pevent; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 257 | } |
Masami Hiramatsu | dec65d79 | 2019-03-14 13:30:20 +0900 | [diff] [blame] | 258 | len = strlen(event); |
| 259 | if (len == 0) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 260 | trace_probe_log_err(offset, NO_EVENT_NAME); |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 261 | return -EINVAL; |
Masami Hiramatsu | dec65d79 | 2019-03-14 13:30:20 +0900 | [diff] [blame] | 262 | } else if (len > MAX_EVENT_NAME_LEN) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 263 | trace_probe_log_err(offset, EVENT_TOO_LONG); |
| 264 | return -EINVAL; |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 265 | } |
Masami Hiramatsu | 5b7a962 | 2019-03-14 13:30:40 +0900 | [diff] [blame] | 266 | if (!is_good_name(event)) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 267 | trace_probe_log_err(offset, BAD_EVENT_NAME); |
Masami Hiramatsu | 5b7a962 | 2019-03-14 13:30:40 +0900 | [diff] [blame] | 268 | return -EINVAL; |
| 269 | } |
Masami Hiramatsu | 6212dd2 | 2018-11-05 18:02:36 +0900 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 273 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) |
| 274 | |
| 275 | static int parse_probe_vars(char *arg, const struct fetch_type *t, |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 276 | struct fetch_insn *code, unsigned int flags, int offs) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 277 | { |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 278 | unsigned long param; |
Steven Rostedt (VMware) | 3d739c1 | 2018-12-21 23:10:26 -0500 | [diff] [blame] | 279 | int ret = 0; |
| 280 | int len; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 281 | |
| 282 | if (strcmp(arg, "retval") == 0) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 283 | if (flags & TPARG_FL_RETURN) { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 284 | code->op = FETCH_OP_RETVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 285 | } else { |
| 286 | trace_probe_log_err(offs, RETVAL_ON_PROBE); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 287 | ret = -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 288 | } |
Steven Rostedt (VMware) | 3d739c1 | 2018-12-21 23:10:26 -0500 | [diff] [blame] | 289 | } else if ((len = str_has_prefix(arg, "stack"))) { |
| 290 | if (arg[len] == '\0') { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 291 | code->op = FETCH_OP_STACKP; |
Steven Rostedt (VMware) | 3d739c1 | 2018-12-21 23:10:26 -0500 | [diff] [blame] | 292 | } else if (isdigit(arg[len])) { |
| 293 | ret = kstrtoul(arg + len, 10, ¶m); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 294 | if (ret) { |
| 295 | goto inval_var; |
| 296 | } else if ((flags & TPARG_FL_KERNEL) && |
| 297 | param > PARAM_MAX_STACK) { |
| 298 | trace_probe_log_err(offs, BAD_STACK_NUM); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 299 | ret = -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 300 | } else { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 301 | code->op = FETCH_OP_STACK; |
| 302 | code->param = (unsigned int)param; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 303 | } |
| 304 | } else |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 305 | goto inval_var; |
Omar Sandoval | 35abb67 | 2016-06-08 18:38:02 -0700 | [diff] [blame] | 306 | } else if (strcmp(arg, "comm") == 0) { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 307 | code->op = FETCH_OP_COMM; |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 308 | #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API |
| 309 | } else if (((flags & TPARG_FL_MASK) == |
| 310 | (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) && |
Steven Rostedt (VMware) | 3d739c1 | 2018-12-21 23:10:26 -0500 | [diff] [blame] | 311 | (len = str_has_prefix(arg, "arg"))) { |
Steven Rostedt (VMware) | 3d739c1 | 2018-12-21 23:10:26 -0500 | [diff] [blame] | 312 | ret = kstrtoul(arg + len, 10, ¶m); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 313 | if (ret) { |
| 314 | goto inval_var; |
| 315 | } else if (!param || param > PARAM_MAX_STACK) { |
| 316 | trace_probe_log_err(offs, BAD_ARG_NUM); |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 317 | return -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 318 | } |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 319 | code->op = FETCH_OP_ARG; |
| 320 | code->param = (unsigned int)param - 1; |
| 321 | #endif |
Tzvetomir Stoyanov (VMware) | 7491e2c | 2021-08-19 11:26:06 -0400 | [diff] [blame] | 322 | } else if (flags & TPARG_FL_TPOINT) { |
| 323 | if (code->data) |
| 324 | return -EFAULT; |
| 325 | code->data = kstrdup(arg, GFP_KERNEL); |
| 326 | if (!code->data) |
| 327 | return -ENOMEM; |
| 328 | code->op = FETCH_OP_TP_ARG; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 329 | } else |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 330 | goto inval_var; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 331 | |
| 332 | return ret; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 333 | |
| 334 | inval_var: |
| 335 | trace_probe_log_err(offs, BAD_VAR); |
| 336 | return -EINVAL; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 337 | } |
| 338 | |
Masami Hiramatsu | 6218bf9 | 2019-06-20 00:08:27 +0900 | [diff] [blame] | 339 | static int str_to_immediate(char *str, unsigned long *imm) |
| 340 | { |
| 341 | if (isdigit(str[0])) |
| 342 | return kstrtoul(str, 0, imm); |
| 343 | else if (str[0] == '-') |
| 344 | return kstrtol(str, 0, (long *)imm); |
| 345 | else if (str[0] == '+') |
| 346 | return kstrtol(str + 1, 0, (long *)imm); |
| 347 | return -EINVAL; |
| 348 | } |
| 349 | |
Masami Hiramatsu | a42e3c4 | 2019-06-20 00:08:37 +0900 | [diff] [blame] | 350 | static int __parse_imm_string(char *str, char **pbuf, int offs) |
| 351 | { |
| 352 | size_t len = strlen(str); |
| 353 | |
| 354 | if (str[len - 1] != '"') { |
| 355 | trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE); |
| 356 | return -EINVAL; |
| 357 | } |
| 358 | *pbuf = kstrndup(str, len - 1, GFP_KERNEL); |
| 359 | return 0; |
| 360 | } |
| 361 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 362 | /* Recursive argument parser */ |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 363 | static int |
| 364 | parse_probe_arg(char *arg, const struct fetch_type *type, |
| 365 | struct fetch_insn **pcode, struct fetch_insn *end, |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 366 | unsigned int flags, int offs) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 367 | { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 368 | struct fetch_insn *code = *pcode; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 369 | unsigned long param; |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 370 | int deref = FETCH_OP_DEREF; |
Steven Rostedt (VMware) | bf173ca | 2018-10-12 12:50:22 -0400 | [diff] [blame] | 371 | long offset = 0; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 372 | char *tmp; |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 373 | int ret = 0; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 374 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 375 | switch (arg[0]) { |
| 376 | case '$': |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 377 | ret = parse_probe_vars(arg + 1, type, code, flags, offs); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 378 | break; |
| 379 | |
| 380 | case '%': /* named register */ |
| 381 | ret = regs_query_register_offset(arg + 1); |
| 382 | if (ret >= 0) { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 383 | code->op = FETCH_OP_REG; |
| 384 | code->param = (unsigned int)ret; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 385 | ret = 0; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 386 | } else |
| 387 | trace_probe_log_err(offs, BAD_REG_NAME); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 388 | break; |
| 389 | |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 390 | case '@': /* memory, file-offset or symbol */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 391 | if (isdigit(arg[1])) { |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 392 | ret = kstrtoul(arg + 1, 0, ¶m); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 393 | if (ret) { |
| 394 | trace_probe_log_err(offs, BAD_MEM_ADDR); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 395 | break; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 396 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 397 | /* load address */ |
| 398 | code->op = FETCH_OP_IMM; |
| 399 | code->immediate = param; |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 400 | } else if (arg[1] == '+') { |
| 401 | /* kprobes don't support file offsets */ |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 402 | if (flags & TPARG_FL_KERNEL) { |
| 403 | trace_probe_log_err(offs, FILE_ON_KPROBE); |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 404 | return -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 405 | } |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 406 | ret = kstrtol(arg + 2, 0, &offset); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 407 | if (ret) { |
| 408 | trace_probe_log_err(offs, BAD_FILE_OFFS); |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 409 | break; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 410 | } |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 411 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 412 | code->op = FETCH_OP_FOFFS; |
| 413 | code->immediate = (unsigned long)offset; // imm64? |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 414 | } else { |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 415 | /* uprobes don't support symbols */ |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 416 | if (!(flags & TPARG_FL_KERNEL)) { |
| 417 | trace_probe_log_err(offs, SYM_ON_UPROBE); |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 418 | return -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 419 | } |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 420 | /* Preserve symbol for updating */ |
| 421 | code->op = FETCH_NOP_SYMBOL; |
| 422 | code->data = kstrdup(arg + 1, GFP_KERNEL); |
| 423 | if (!code->data) |
| 424 | return -ENOMEM; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 425 | if (++code == end) { |
| 426 | trace_probe_log_err(offs, TOO_MANY_OPS); |
| 427 | return -EINVAL; |
| 428 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 429 | code->op = FETCH_OP_IMM; |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 430 | code->immediate = 0; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 431 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 432 | /* These are fetching from memory */ |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 433 | if (++code == end) { |
| 434 | trace_probe_log_err(offs, TOO_MANY_OPS); |
| 435 | return -EINVAL; |
| 436 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 437 | *pcode = code; |
| 438 | code->op = FETCH_OP_DEREF; |
| 439 | code->offset = offset; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 440 | break; |
| 441 | |
| 442 | case '+': /* deref memory */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 443 | case '-': |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 444 | if (arg[1] == 'u') { |
| 445 | deref = FETCH_OP_UDEREF; |
| 446 | arg[1] = arg[0]; |
| 447 | arg++; |
| 448 | } |
| 449 | if (arg[0] == '+') |
| 450 | arg++; /* Skip '+', because kstrtol() rejects it. */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 451 | tmp = strchr(arg, '('); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 452 | if (!tmp) { |
| 453 | trace_probe_log_err(offs, DEREF_NEED_BRACE); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 454 | return -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 455 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 456 | *tmp = '\0'; |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 457 | ret = kstrtol(arg, 0, &offset); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 458 | if (ret) { |
| 459 | trace_probe_log_err(offs, BAD_DEREF_OFFS); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 460 | break; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 461 | } |
| 462 | offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 463 | arg = tmp + 1; |
| 464 | tmp = strrchr(arg, ')'); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 465 | if (!tmp) { |
| 466 | trace_probe_log_err(offs + strlen(arg), |
| 467 | DEREF_OPEN_BRACE); |
| 468 | return -EINVAL; |
| 469 | } else { |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 470 | const struct fetch_type *t2 = find_fetch_type(NULL); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 471 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 472 | *tmp = '\0'; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 473 | ret = parse_probe_arg(arg, t2, &code, end, flags, offs); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 474 | if (ret) |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 475 | break; |
Masami Hiramatsu | a42e3c4 | 2019-06-20 00:08:37 +0900 | [diff] [blame] | 476 | if (code->op == FETCH_OP_COMM || |
| 477 | code->op == FETCH_OP_DATA) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 478 | trace_probe_log_err(offs, COMM_CANT_DEREF); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 479 | return -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 480 | } |
| 481 | if (++code == end) { |
| 482 | trace_probe_log_err(offs, TOO_MANY_OPS); |
| 483 | return -EINVAL; |
| 484 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 485 | *pcode = code; |
| 486 | |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 487 | code->op = deref; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 488 | code->offset = offset; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 489 | } |
| 490 | break; |
Masami Hiramatsu | 6218bf9 | 2019-06-20 00:08:27 +0900 | [diff] [blame] | 491 | case '\\': /* Immediate value */ |
Masami Hiramatsu | a42e3c4 | 2019-06-20 00:08:37 +0900 | [diff] [blame] | 492 | if (arg[1] == '"') { /* Immediate string */ |
| 493 | ret = __parse_imm_string(arg + 2, &tmp, offs + 2); |
| 494 | if (ret) |
| 495 | break; |
| 496 | code->op = FETCH_OP_DATA; |
| 497 | code->data = tmp; |
| 498 | } else { |
| 499 | ret = str_to_immediate(arg + 1, &code->immediate); |
| 500 | if (ret) |
| 501 | trace_probe_log_err(offs + 1, BAD_IMM); |
| 502 | else |
| 503 | code->op = FETCH_OP_IMM; |
| 504 | } |
Masami Hiramatsu | 6218bf9 | 2019-06-20 00:08:27 +0900 | [diff] [blame] | 505 | break; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 506 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 507 | if (!ret && code->op == FETCH_OP_NOP) { |
| 508 | /* Parsed, but do not find fetch method */ |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 509 | trace_probe_log_err(offs, BAD_FETCH_ARG); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 510 | ret = -EINVAL; |
| 511 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long)) |
| 516 | |
| 517 | /* Bitfield type needs to be parsed into a fetch function */ |
| 518 | static int __parse_bitfield_probe_arg(const char *bf, |
| 519 | const struct fetch_type *t, |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 520 | struct fetch_insn **pcode) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 521 | { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 522 | struct fetch_insn *code = *pcode; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 523 | unsigned long bw, bo; |
| 524 | char *tail; |
| 525 | |
| 526 | if (*bf != 'b') |
| 527 | return 0; |
| 528 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 529 | bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */ |
| 530 | |
| 531 | if (bw == 0 || *tail != '@') |
| 532 | return -EINVAL; |
| 533 | |
| 534 | bf = tail + 1; |
| 535 | bo = simple_strtoul(bf, &tail, 0); |
| 536 | |
| 537 | if (tail == bf || *tail != '/') |
| 538 | return -EINVAL; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 539 | code++; |
| 540 | if (code->op != FETCH_OP_NOP) |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 541 | return -EINVAL; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 542 | *pcode = code; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 543 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 544 | code->op = FETCH_OP_MOD_BF; |
| 545 | code->lshift = BYTES_TO_BITS(t->size) - (bw + bo); |
| 546 | code->rshift = BYTES_TO_BITS(t->size) - bw; |
| 547 | code->basesize = t->size; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 548 | |
| 549 | return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0; |
| 550 | } |
| 551 | |
| 552 | /* String length checking wrapper */ |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 553 | static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 554 | struct probe_arg *parg, unsigned int flags, int offset) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 555 | { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 556 | struct fetch_insn *code, *scode, *tmp = NULL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 557 | char *t, *t2, *t3; |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 558 | char *arg; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 559 | int ret, len; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 560 | |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 561 | arg = kstrdup(argv, GFP_KERNEL); |
| 562 | if (!arg) |
| 563 | return -ENOMEM; |
| 564 | |
| 565 | ret = -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 566 | len = strlen(arg); |
| 567 | if (len > MAX_ARGSTR_LEN) { |
| 568 | trace_probe_log_err(offset, ARG_TOO_LONG); |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 569 | goto out; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 570 | } else if (len == 0) { |
| 571 | trace_probe_log_err(offset, NO_ARG_BODY); |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 572 | goto out; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 573 | } |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 574 | |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 575 | ret = -ENOMEM; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 576 | parg->comm = kstrdup(arg, GFP_KERNEL); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 577 | if (!parg->comm) |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 578 | goto out; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 579 | |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 580 | ret = -EINVAL; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 581 | t = strchr(arg, ':'); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 582 | if (t) { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 583 | *t = '\0'; |
| 584 | t2 = strchr(++t, '['); |
| 585 | if (t2) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 586 | *t2++ = '\0'; |
| 587 | t3 = strchr(t2, ']'); |
| 588 | if (!t3) { |
| 589 | offset += t2 + strlen(t2) - arg; |
| 590 | trace_probe_log_err(offset, |
| 591 | ARRAY_NO_CLOSE); |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 592 | goto out; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 593 | } else if (t3[1] != '\0') { |
| 594 | trace_probe_log_err(offset + t3 + 1 - arg, |
| 595 | BAD_ARRAY_SUFFIX); |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 596 | goto out; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 597 | } |
| 598 | *t3 = '\0'; |
| 599 | if (kstrtouint(t2, 0, &parg->count) || !parg->count) { |
| 600 | trace_probe_log_err(offset + t2 - arg, |
| 601 | BAD_ARRAY_NUM); |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 602 | goto out; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 603 | } |
| 604 | if (parg->count > MAX_ARRAY_LEN) { |
| 605 | trace_probe_log_err(offset + t2 - arg, |
| 606 | ARRAY_TOO_BIG); |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 607 | goto out; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 608 | } |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 609 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 610 | } |
Masami Hiramatsu | 3dd1f7f | 2019-05-07 22:56:02 +0900 | [diff] [blame] | 611 | |
Masami Hiramatsu | a42e3c4 | 2019-06-20 00:08:37 +0900 | [diff] [blame] | 612 | /* |
Ingo Molnar | f2cc020 | 2021-03-23 18:49:35 +0100 | [diff] [blame] | 613 | * Since $comm and immediate string can not be dereferenced, |
Masami Hiramatsu | a42e3c4 | 2019-06-20 00:08:37 +0900 | [diff] [blame] | 614 | * we can find those by strcmp. |
| 615 | */ |
| 616 | if (strcmp(arg, "$comm") == 0 || strncmp(arg, "\\\"", 2) == 0) { |
Masami Hiramatsu | 3dd1f7f | 2019-05-07 22:56:02 +0900 | [diff] [blame] | 617 | /* The type of $comm must be "string", and not an array. */ |
| 618 | if (parg->count || (t && strcmp(t, "string"))) |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 619 | goto out; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 620 | parg->type = find_fetch_type("string"); |
Masami Hiramatsu | 3dd1f7f | 2019-05-07 22:56:02 +0900 | [diff] [blame] | 621 | } else |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 622 | parg->type = find_fetch_type(t); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 623 | if (!parg->type) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 624 | trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE); |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 625 | goto out; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 626 | } |
| 627 | parg->offset = *size; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 628 | *size += parg->type->size * (parg->count ?: 1); |
| 629 | |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 630 | ret = -ENOMEM; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 631 | if (parg->count) { |
| 632 | len = strlen(parg->type->fmttype) + 6; |
| 633 | parg->fmt = kmalloc(len, GFP_KERNEL); |
| 634 | if (!parg->fmt) |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 635 | goto out; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 636 | snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype, |
| 637 | parg->count); |
| 638 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 639 | |
Gustavo A. R. Silva | 8623b00 | 2019-01-14 22:34:08 -0600 | [diff] [blame] | 640 | code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 641 | if (!code) |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 642 | goto out; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 643 | code[FETCH_INSN_MAX - 1].op = FETCH_OP_END; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 644 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 645 | 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] | 646 | flags, offset); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 647 | if (ret) |
| 648 | goto fail; |
| 649 | |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 650 | ret = -EINVAL; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 651 | /* Store operation */ |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 652 | if (!strcmp(parg->type->name, "string") || |
| 653 | !strcmp(parg->type->name, "ustring")) { |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 654 | if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF && |
Masami Hiramatsu | a42e3c4 | 2019-06-20 00:08:37 +0900 | [diff] [blame] | 655 | code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM && |
Tzvetomir Stoyanov (VMware) | 7491e2c | 2021-08-19 11:26:06 -0400 | [diff] [blame] | 656 | code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 657 | trace_probe_log_err(offset + (t ? (t - arg) : 0), |
| 658 | BAD_STRING); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 659 | goto fail; |
| 660 | } |
Vamshi K Sthambamkadi | 3aa8fdc | 2020-06-15 20:00:38 +0530 | [diff] [blame] | 661 | if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM || |
Tzvetomir Stoyanov (VMware) | 7491e2c | 2021-08-19 11:26:06 -0400 | [diff] [blame] | 662 | code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG || |
| 663 | parg->count) { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 664 | /* |
Masami Hiramatsu | a42e3c4 | 2019-06-20 00:08:37 +0900 | [diff] [blame] | 665 | * IMM, DATA and COMM is pointing actual address, those |
| 666 | * must be kept, and if parg->count != 0, this is an |
| 667 | * array of string pointers instead of string address |
| 668 | * itself. |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 669 | */ |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 670 | code++; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 671 | if (code->op != FETCH_OP_NOP) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 672 | trace_probe_log_err(offset, TOO_MANY_OPS); |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 673 | goto fail; |
| 674 | } |
| 675 | } |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 676 | /* If op == DEREF, replace it with STRING */ |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 677 | if (!strcmp(parg->type->name, "ustring") || |
| 678 | code->op == FETCH_OP_UDEREF) |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 679 | code->op = FETCH_OP_ST_USTRING; |
| 680 | else |
| 681 | code->op = FETCH_OP_ST_STRING; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 682 | code->size = parg->type->size; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 683 | parg->dynamic = true; |
| 684 | } else if (code->op == FETCH_OP_DEREF) { |
| 685 | code->op = FETCH_OP_ST_MEM; |
| 686 | code->size = parg->type->size; |
Masami Hiramatsu | e65f7ae | 2019-05-15 14:38:42 +0900 | [diff] [blame] | 687 | } else if (code->op == FETCH_OP_UDEREF) { |
| 688 | code->op = FETCH_OP_ST_UMEM; |
| 689 | code->size = parg->type->size; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 690 | } else { |
| 691 | code++; |
| 692 | if (code->op != FETCH_OP_NOP) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 693 | trace_probe_log_err(offset, TOO_MANY_OPS); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 694 | goto fail; |
| 695 | } |
| 696 | code->op = FETCH_OP_ST_RAW; |
| 697 | code->size = parg->type->size; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 698 | } |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 699 | scode = code; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 700 | /* Modify operation */ |
| 701 | if (t != NULL) { |
| 702 | ret = __parse_bitfield_probe_arg(t, parg->type, &code); |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 703 | if (ret) { |
| 704 | trace_probe_log_err(offset + t - arg, BAD_BITFIELD); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 705 | goto fail; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 706 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 707 | } |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 708 | ret = -EINVAL; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 709 | /* Loop(Array) operation */ |
| 710 | if (parg->count) { |
| 711 | if (scode->op != FETCH_OP_ST_MEM && |
Masami Hiramatsu | 88903c4 | 2019-05-15 14:38:30 +0900 | [diff] [blame] | 712 | scode->op != FETCH_OP_ST_STRING && |
| 713 | scode->op != FETCH_OP_ST_USTRING) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 714 | trace_probe_log_err(offset + (t ? (t - arg) : 0), |
| 715 | BAD_STRING); |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 716 | goto fail; |
| 717 | } |
| 718 | code++; |
| 719 | if (code->op != FETCH_OP_NOP) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 720 | trace_probe_log_err(offset, TOO_MANY_OPS); |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 721 | goto fail; |
| 722 | } |
| 723 | code->op = FETCH_OP_LP_ARRAY; |
| 724 | code->param = parg->count; |
| 725 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 726 | code++; |
| 727 | code->op = FETCH_OP_END; |
| 728 | |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 729 | ret = 0; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 730 | /* Shrink down the code buffer */ |
Gustavo A. R. Silva | 8623b00 | 2019-01-14 22:34:08 -0600 | [diff] [blame] | 731 | parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 732 | if (!parg->code) |
| 733 | ret = -ENOMEM; |
| 734 | else |
| 735 | memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1)); |
| 736 | |
| 737 | fail: |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 738 | if (ret) { |
| 739 | for (code = tmp; code < tmp + FETCH_INSN_MAX; code++) |
Masami Hiramatsu | a42e3c4 | 2019-06-20 00:08:37 +0900 | [diff] [blame] | 740 | if (code->op == FETCH_NOP_SYMBOL || |
| 741 | code->op == FETCH_OP_DATA) |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 742 | kfree(code->data); |
| 743 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 744 | kfree(tmp); |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 745 | out: |
| 746 | kfree(arg); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 747 | |
| 748 | return ret; |
| 749 | } |
| 750 | |
| 751 | /* Return 1 if name is reserved or already used by another argument */ |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 752 | static int traceprobe_conflict_field_name(const char *name, |
| 753 | struct probe_arg *args, int narg) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 754 | { |
| 755 | int i; |
| 756 | |
| 757 | for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) |
| 758 | if (strcmp(reserved_field_names[i], name) == 0) |
| 759 | return 1; |
| 760 | |
| 761 | for (i = 0; i < narg; i++) |
| 762 | if (strcmp(args[i].name, name) == 0) |
| 763 | return 1; |
| 764 | |
| 765 | return 0; |
| 766 | } |
| 767 | |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 768 | int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg, |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 769 | unsigned int flags) |
| 770 | { |
| 771 | struct probe_arg *parg = &tp->args[i]; |
Steven Rostedt (VMware) | fcd9db5 | 2021-08-16 23:42:58 -0400 | [diff] [blame] | 772 | const char *body; |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 773 | |
| 774 | /* Increment count for freeing args in error case */ |
| 775 | tp->nr_args++; |
| 776 | |
| 777 | body = strchr(arg, '='); |
| 778 | if (body) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 779 | if (body - arg > MAX_ARG_NAME_LEN) { |
| 780 | trace_probe_log_err(0, ARG_NAME_TOO_LONG); |
Masami Hiramatsu | b4443c1 | 2019-03-14 13:30:30 +0900 | [diff] [blame] | 781 | return -EINVAL; |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 782 | } else if (body == arg) { |
| 783 | trace_probe_log_err(0, NO_ARG_NAME); |
| 784 | return -EINVAL; |
| 785 | } |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 786 | parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL); |
| 787 | body++; |
| 788 | } else { |
| 789 | /* If argument name is omitted, set "argN" */ |
| 790 | parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1); |
| 791 | body = arg; |
| 792 | } |
| 793 | if (!parg->name) |
| 794 | return -ENOMEM; |
| 795 | |
| 796 | if (!is_good_name(parg->name)) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 797 | trace_probe_log_err(0, BAD_ARG_NAME); |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 798 | return -EINVAL; |
| 799 | } |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 800 | if (traceprobe_conflict_field_name(parg->name, tp->args, i)) { |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 801 | trace_probe_log_err(0, USED_ARG_NAME); |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 802 | return -EINVAL; |
| 803 | } |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 804 | /* Parse fetch argument */ |
Masami Hiramatsu | ab105a4 | 2019-03-31 18:48:19 -0500 | [diff] [blame] | 805 | return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags, |
| 806 | body - arg); |
Masami Hiramatsu | d00bbea9 | 2018-11-05 18:01:40 +0900 | [diff] [blame] | 807 | } |
| 808 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 809 | void traceprobe_free_probe_arg(struct probe_arg *arg) |
| 810 | { |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 811 | struct fetch_insn *code = arg->code; |
| 812 | |
| 813 | while (code && code->op != FETCH_OP_END) { |
Masami Hiramatsu | a42e3c4 | 2019-06-20 00:08:37 +0900 | [diff] [blame] | 814 | if (code->op == FETCH_NOP_SYMBOL || |
| 815 | code->op == FETCH_OP_DATA) |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 816 | kfree(code->data); |
| 817 | code++; |
| 818 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 819 | kfree(arg->code); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 820 | kfree(arg->name); |
| 821 | kfree(arg->comm); |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 822 | kfree(arg->fmt); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 823 | } |
| 824 | |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 825 | int traceprobe_update_arg(struct probe_arg *arg) |
| 826 | { |
| 827 | struct fetch_insn *code = arg->code; |
| 828 | long offset; |
| 829 | char *tmp; |
| 830 | char c; |
| 831 | int ret = 0; |
| 832 | |
| 833 | while (code && code->op != FETCH_OP_END) { |
| 834 | if (code->op == FETCH_NOP_SYMBOL) { |
| 835 | if (code[1].op != FETCH_OP_IMM) |
| 836 | return -EINVAL; |
| 837 | |
Masami Hiramatsu | ee474b8 | 2018-11-01 23:29:28 +0900 | [diff] [blame] | 838 | tmp = strpbrk(code->data, "+-"); |
Masami Hiramatsu | a668281 | 2018-08-29 01:18:43 +0900 | [diff] [blame] | 839 | if (tmp) |
| 840 | c = *tmp; |
| 841 | ret = traceprobe_split_symbol_offset(code->data, |
| 842 | &offset); |
| 843 | if (ret) |
| 844 | return ret; |
| 845 | |
| 846 | code[1].immediate = |
| 847 | (unsigned long)kallsyms_lookup_name(code->data); |
| 848 | if (tmp) |
| 849 | *tmp = c; |
| 850 | if (!code[1].immediate) |
| 851 | return -ENOENT; |
| 852 | code[1].immediate += offset; |
| 853 | } |
| 854 | code++; |
| 855 | } |
| 856 | return 0; |
| 857 | } |
| 858 | |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 859 | /* When len=0, we just calculate the needed length */ |
| 860 | #define LEN_OR_ZERO (len ? len - pos : 0) |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 861 | static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, |
Steven Rostedt (VMware) | 007517a | 2021-08-19 00:13:27 -0400 | [diff] [blame] | 862 | enum probe_print_type ptype) |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 863 | { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 864 | struct probe_arg *parg; |
| 865 | int i, j; |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 866 | int pos = 0; |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 867 | const char *fmt, *arg; |
| 868 | |
Steven Rostedt (VMware) | 007517a | 2021-08-19 00:13:27 -0400 | [diff] [blame] | 869 | switch (ptype) { |
| 870 | case PROBE_PRINT_NORMAL: |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 871 | fmt = "(%lx)"; |
| 872 | arg = "REC->" FIELD_STRING_IP; |
Steven Rostedt (VMware) | 007517a | 2021-08-19 00:13:27 -0400 | [diff] [blame] | 873 | break; |
| 874 | case PROBE_PRINT_RETURN: |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 875 | fmt = "(%lx <- %lx)"; |
| 876 | arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; |
Steven Rostedt (VMware) | 007517a | 2021-08-19 00:13:27 -0400 | [diff] [blame] | 877 | break; |
Tzvetomir Stoyanov (VMware) | 7491e2c | 2021-08-19 11:26:06 -0400 | [diff] [blame] | 878 | case PROBE_PRINT_EVENT: |
| 879 | fmt = "(%u)"; |
| 880 | arg = "REC->" FIELD_STRING_TYPE; |
| 881 | break; |
Steven Rostedt (VMware) | 007517a | 2021-08-19 00:13:27 -0400 | [diff] [blame] | 882 | default: |
| 883 | WARN_ON_ONCE(1); |
| 884 | return 0; |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 885 | } |
| 886 | |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 887 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); |
| 888 | |
| 889 | for (i = 0; i < tp->nr_args; i++) { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 890 | parg = tp->args + i; |
| 891 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name); |
| 892 | if (parg->count) { |
| 893 | pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s", |
| 894 | parg->type->fmt); |
| 895 | for (j = 1; j < parg->count; j++) |
| 896 | pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s", |
| 897 | parg->type->fmt); |
| 898 | pos += snprintf(buf + pos, LEN_OR_ZERO, "}"); |
| 899 | } else |
| 900 | pos += snprintf(buf + pos, LEN_OR_ZERO, "%s", |
| 901 | parg->type->fmt); |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); |
| 905 | |
| 906 | for (i = 0; i < tp->nr_args; i++) { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 907 | parg = tp->args + i; |
| 908 | if (parg->count) { |
Steven Rostedt (VMware) | 2027942 | 2020-01-24 10:07:42 -0500 | [diff] [blame] | 909 | if ((strcmp(parg->type->name, "string") == 0) || |
| 910 | (strcmp(parg->type->name, "ustring") == 0)) |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 911 | fmt = ", __get_str(%s[%d])"; |
| 912 | else |
| 913 | fmt = ", REC->%s[%d]"; |
| 914 | for (j = 0; j < parg->count; j++) |
| 915 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
| 916 | fmt, parg->name, j); |
| 917 | } else { |
Steven Rostedt (VMware) | 2027942 | 2020-01-24 10:07:42 -0500 | [diff] [blame] | 918 | if ((strcmp(parg->type->name, "string") == 0) || |
| 919 | (strcmp(parg->type->name, "ustring") == 0)) |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 920 | fmt = ", __get_str(%s)"; |
| 921 | else |
| 922 | fmt = ", REC->%s"; |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 923 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 924 | fmt, parg->name); |
| 925 | } |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 926 | } |
| 927 | |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 928 | /* return the length of print_fmt */ |
| 929 | return pos; |
| 930 | } |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 931 | #undef LEN_OR_ZERO |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 932 | |
Steven Rostedt (VMware) | 007517a | 2021-08-19 00:13:27 -0400 | [diff] [blame] | 933 | int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype) |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 934 | { |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 935 | struct trace_event_call *call = trace_probe_event_call(tp); |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 936 | int len; |
| 937 | char *print_fmt; |
| 938 | |
| 939 | /* First: called with 0 length to calculate the needed length */ |
Steven Rostedt (VMware) | 007517a | 2021-08-19 00:13:27 -0400 | [diff] [blame] | 940 | len = __set_print_fmt(tp, NULL, 0, ptype); |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 941 | print_fmt = kmalloc(len + 1, GFP_KERNEL); |
| 942 | if (!print_fmt) |
| 943 | return -ENOMEM; |
| 944 | |
| 945 | /* Second: actually write the @print_fmt */ |
Steven Rostedt (VMware) | 007517a | 2021-08-19 00:13:27 -0400 | [diff] [blame] | 946 | __set_print_fmt(tp, print_fmt, len + 1, ptype); |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 947 | call->print_fmt = print_fmt; |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 948 | |
| 949 | return 0; |
| 950 | } |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 951 | |
| 952 | int traceprobe_define_arg_fields(struct trace_event_call *event_call, |
| 953 | size_t offset, struct trace_probe *tp) |
| 954 | { |
| 955 | int ret, i; |
| 956 | |
| 957 | /* Set argument names as fields */ |
| 958 | for (i = 0; i < tp->nr_args; i++) { |
| 959 | struct probe_arg *parg = &tp->args[i]; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 960 | const char *fmt = parg->type->fmttype; |
| 961 | int size = parg->type->size; |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 962 | |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 963 | if (parg->fmt) |
| 964 | fmt = parg->fmt; |
| 965 | if (parg->count) |
| 966 | size *= parg->count; |
| 967 | ret = trace_define_field(event_call, fmt, parg->name, |
| 968 | offset + parg->offset, size, |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 969 | parg->type->is_signed, |
| 970 | FILTER_OTHER); |
| 971 | if (ret) |
| 972 | return ret; |
| 973 | } |
| 974 | return 0; |
| 975 | } |
Masami Hiramatsu | 455b289 | 2019-06-01 00:17:06 +0900 | [diff] [blame] | 976 | |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 977 | static void trace_probe_event_free(struct trace_probe_event *tpe) |
| 978 | { |
| 979 | kfree(tpe->class.system); |
| 980 | kfree(tpe->call.name); |
| 981 | kfree(tpe->call.print_fmt); |
| 982 | kfree(tpe); |
| 983 | } |
| 984 | |
| 985 | int trace_probe_append(struct trace_probe *tp, struct trace_probe *to) |
| 986 | { |
| 987 | if (trace_probe_has_sibling(tp)) |
| 988 | return -EBUSY; |
| 989 | |
| 990 | list_del_init(&tp->list); |
| 991 | trace_probe_event_free(tp->event); |
| 992 | |
| 993 | tp->event = to->event; |
| 994 | list_add_tail(&tp->list, trace_probe_probe_list(to)); |
| 995 | |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | void trace_probe_unlink(struct trace_probe *tp) |
| 1000 | { |
| 1001 | list_del_init(&tp->list); |
| 1002 | if (list_empty(trace_probe_probe_list(tp))) |
| 1003 | trace_probe_event_free(tp->event); |
| 1004 | tp->event = NULL; |
| 1005 | } |
Masami Hiramatsu | 455b289 | 2019-06-01 00:17:06 +0900 | [diff] [blame] | 1006 | |
| 1007 | void trace_probe_cleanup(struct trace_probe *tp) |
| 1008 | { |
| 1009 | int i; |
| 1010 | |
| 1011 | for (i = 0; i < tp->nr_args; i++) |
| 1012 | traceprobe_free_probe_arg(&tp->args[i]); |
| 1013 | |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 1014 | if (tp->event) |
| 1015 | trace_probe_unlink(tp); |
Masami Hiramatsu | 455b289 | 2019-06-01 00:17:06 +0900 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | int trace_probe_init(struct trace_probe *tp, const char *event, |
Masami Hiramatsu | b61387c | 2020-01-22 12:23:25 +0900 | [diff] [blame] | 1019 | const char *group, bool alloc_filter) |
Masami Hiramatsu | 455b289 | 2019-06-01 00:17:06 +0900 | [diff] [blame] | 1020 | { |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1021 | struct trace_event_call *call; |
Masami Hiramatsu | b61387c | 2020-01-22 12:23:25 +0900 | [diff] [blame] | 1022 | size_t size = sizeof(struct trace_probe_event); |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1023 | int ret = 0; |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 1024 | |
Masami Hiramatsu | 455b289 | 2019-06-01 00:17:06 +0900 | [diff] [blame] | 1025 | if (!event || !group) |
| 1026 | return -EINVAL; |
| 1027 | |
Masami Hiramatsu | b61387c | 2020-01-22 12:23:25 +0900 | [diff] [blame] | 1028 | if (alloc_filter) |
| 1029 | size += sizeof(struct trace_uprobe_filter); |
| 1030 | |
| 1031 | tp->event = kzalloc(size, GFP_KERNEL); |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1032 | if (!tp->event) |
Masami Hiramatsu | 455b289 | 2019-06-01 00:17:06 +0900 | [diff] [blame] | 1033 | return -ENOMEM; |
| 1034 | |
Masami Hiramatsu | d59fae6 | 2019-09-17 14:11:37 +0900 | [diff] [blame] | 1035 | INIT_LIST_HEAD(&tp->event->files); |
| 1036 | INIT_LIST_HEAD(&tp->event->class.fields); |
| 1037 | INIT_LIST_HEAD(&tp->event->probes); |
| 1038 | INIT_LIST_HEAD(&tp->list); |
Julia Lawall | fc9d276 | 2020-05-07 21:30:08 +0200 | [diff] [blame] | 1039 | list_add(&tp->list, &tp->event->probes); |
Masami Hiramatsu | d59fae6 | 2019-09-17 14:11:37 +0900 | [diff] [blame] | 1040 | |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1041 | call = trace_probe_event_call(tp); |
| 1042 | call->class = &tp->event->class; |
| 1043 | call->name = kstrdup(event, GFP_KERNEL); |
| 1044 | if (!call->name) { |
| 1045 | ret = -ENOMEM; |
| 1046 | goto error; |
Masami Hiramatsu | 455b289 | 2019-06-01 00:17:06 +0900 | [diff] [blame] | 1047 | } |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1048 | |
| 1049 | tp->event->class.system = kstrdup(group, GFP_KERNEL); |
| 1050 | if (!tp->event->class.system) { |
| 1051 | ret = -ENOMEM; |
| 1052 | goto error; |
| 1053 | } |
Masami Hiramatsu | 455b289 | 2019-06-01 00:17:06 +0900 | [diff] [blame] | 1054 | |
| 1055 | return 0; |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1056 | |
| 1057 | error: |
| 1058 | trace_probe_cleanup(tp); |
| 1059 | return ret; |
Masami Hiramatsu | 455b289 | 2019-06-01 00:17:06 +0900 | [diff] [blame] | 1060 | } |
Masami Hiramatsu | 46e5376 | 2019-06-01 00:17:16 +0900 | [diff] [blame] | 1061 | |
Masami Hiramatsu | 8e24206 | 2021-08-19 19:26:02 +0900 | [diff] [blame] | 1062 | static struct trace_event_call * |
| 1063 | find_trace_event_call(const char *system, const char *event_name) |
| 1064 | { |
| 1065 | struct trace_event_call *tp_event; |
| 1066 | const char *name; |
| 1067 | |
| 1068 | list_for_each_entry(tp_event, &ftrace_events, list) { |
| 1069 | if (!tp_event->class->system || |
| 1070 | strcmp(system, tp_event->class->system)) |
| 1071 | continue; |
| 1072 | name = trace_event_name(tp_event); |
| 1073 | if (!name || strcmp(event_name, name)) |
| 1074 | continue; |
| 1075 | return tp_event; |
| 1076 | } |
| 1077 | |
| 1078 | return NULL; |
| 1079 | } |
| 1080 | |
Masami Hiramatsu | 46e5376 | 2019-06-01 00:17:16 +0900 | [diff] [blame] | 1081 | int trace_probe_register_event_call(struct trace_probe *tp) |
| 1082 | { |
Masami Hiramatsu | e3dc9f8 | 2019-06-01 00:17:57 +0900 | [diff] [blame] | 1083 | struct trace_event_call *call = trace_probe_event_call(tp); |
Masami Hiramatsu | 46e5376 | 2019-06-01 00:17:16 +0900 | [diff] [blame] | 1084 | int ret; |
| 1085 | |
Masami Hiramatsu | 8e24206 | 2021-08-19 19:26:02 +0900 | [diff] [blame] | 1086 | lockdep_assert_held(&event_mutex); |
| 1087 | |
| 1088 | if (find_trace_event_call(trace_probe_group_name(tp), |
| 1089 | trace_probe_name(tp))) |
| 1090 | return -EEXIST; |
| 1091 | |
Masami Hiramatsu | 46e5376 | 2019-06-01 00:17:16 +0900 | [diff] [blame] | 1092 | ret = register_trace_event(&call->event); |
| 1093 | if (!ret) |
| 1094 | return -ENODEV; |
| 1095 | |
| 1096 | ret = trace_add_event_call(call); |
| 1097 | if (ret) |
| 1098 | unregister_trace_event(&call->event); |
| 1099 | |
| 1100 | return ret; |
| 1101 | } |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 1102 | |
| 1103 | int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file) |
| 1104 | { |
| 1105 | struct event_file_link *link; |
| 1106 | |
| 1107 | link = kmalloc(sizeof(*link), GFP_KERNEL); |
| 1108 | if (!link) |
| 1109 | return -ENOMEM; |
| 1110 | |
| 1111 | link->file = file; |
| 1112 | INIT_LIST_HEAD(&link->list); |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1113 | list_add_tail_rcu(&link->list, &tp->event->files); |
Masami Hiramatsu | 747774d | 2019-06-01 00:17:37 +0900 | [diff] [blame] | 1114 | trace_probe_set_flag(tp, TP_FLAG_TRACE); |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp, |
| 1119 | struct trace_event_file *file) |
| 1120 | { |
| 1121 | struct event_file_link *link; |
| 1122 | |
| 1123 | trace_probe_for_each_link(link, tp) { |
| 1124 | if (link->file == file) |
| 1125 | return link; |
| 1126 | } |
| 1127 | |
| 1128 | return NULL; |
| 1129 | } |
| 1130 | |
| 1131 | int trace_probe_remove_file(struct trace_probe *tp, |
| 1132 | struct trace_event_file *file) |
| 1133 | { |
| 1134 | struct event_file_link *link; |
| 1135 | |
| 1136 | link = trace_probe_get_file_link(tp, file); |
| 1137 | if (!link) |
| 1138 | return -ENOENT; |
| 1139 | |
| 1140 | list_del_rcu(&link->list); |
| 1141 | synchronize_rcu(); |
| 1142 | kfree(link); |
| 1143 | |
Masami Hiramatsu | 60d53e2 | 2019-06-20 00:07:20 +0900 | [diff] [blame] | 1144 | if (list_empty(&tp->event->files)) |
Masami Hiramatsu | 747774d | 2019-06-01 00:17:37 +0900 | [diff] [blame] | 1145 | trace_probe_clear_flag(tp, TP_FLAG_TRACE); |
Masami Hiramatsu | b5f935e | 2019-06-01 00:17:26 +0900 | [diff] [blame] | 1146 | |
| 1147 | return 0; |
| 1148 | } |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 1149 | |
| 1150 | /* |
| 1151 | * Return the smallest index of different type argument (start from 1). |
| 1152 | * If all argument types and name are same, return 0. |
| 1153 | */ |
| 1154 | int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b) |
| 1155 | { |
| 1156 | int i; |
| 1157 | |
Masami Hiramatsu | d2aea95 | 2019-09-28 05:53:29 -0400 | [diff] [blame] | 1158 | /* In case of more arguments */ |
| 1159 | if (a->nr_args < b->nr_args) |
| 1160 | return a->nr_args + 1; |
| 1161 | if (a->nr_args > b->nr_args) |
| 1162 | return b->nr_args + 1; |
| 1163 | |
Masami Hiramatsu | ca89bc0 | 2019-06-20 00:07:49 +0900 | [diff] [blame] | 1164 | for (i = 0; i < a->nr_args; i++) { |
| 1165 | if ((b->nr_args <= i) || |
| 1166 | ((a->args[i].type != b->args[i].type) || |
| 1167 | (a->args[i].count != b->args[i].count) || |
| 1168 | strcmp(a->args[i].name, b->args[i].name))) |
| 1169 | return i + 1; |
| 1170 | } |
| 1171 | |
| 1172 | return 0; |
| 1173 | } |
Masami Hiramatsu | eb5bf81 | 2019-06-20 00:08:08 +0900 | [diff] [blame] | 1174 | |
| 1175 | bool trace_probe_match_command_args(struct trace_probe *tp, |
| 1176 | int argc, const char **argv) |
| 1177 | { |
| 1178 | char buf[MAX_ARGSTR_LEN + 1]; |
| 1179 | int i; |
| 1180 | |
| 1181 | if (tp->nr_args < argc) |
| 1182 | return false; |
| 1183 | |
| 1184 | for (i = 0; i < argc; i++) { |
| 1185 | snprintf(buf, sizeof(buf), "%s=%s", |
| 1186 | tp->args[i].name, tp->args[i].comm); |
| 1187 | if (strcmp(buf, argv[i])) |
| 1188 | return false; |
| 1189 | } |
| 1190 | return true; |
| 1191 | } |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 1192 | |
| 1193 | int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **)) |
| 1194 | { |
| 1195 | int argc = 0, ret = 0; |
| 1196 | char **argv; |
| 1197 | |
| 1198 | argv = argv_split(GFP_KERNEL, raw_command, &argc); |
| 1199 | if (!argv) |
| 1200 | return -ENOMEM; |
| 1201 | |
| 1202 | if (argc) |
| 1203 | ret = createfn(argc, (const char **)argv); |
| 1204 | |
| 1205 | argv_free(argv); |
| 1206 | |
| 1207 | return ret; |
| 1208 | } |