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 | |
| 16 | const char *reserved_field_names[] = { |
| 17 | "common_type", |
| 18 | "common_flags", |
| 19 | "common_preempt_count", |
| 20 | "common_pid", |
| 21 | "common_tgid", |
| 22 | FIELD_STRING_IP, |
| 23 | FIELD_STRING_RETIP, |
| 24 | FIELD_STRING_FUNC, |
| 25 | }; |
| 26 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 27 | /* Printing in basic type function template */ |
Masami Hiramatsu | 17ce3dc | 2016-08-18 17:57:50 +0900 | [diff] [blame] | 28 | #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \ |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 29 | 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] | 30 | { \ |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 31 | trace_seq_printf(s, fmt, *(type *)data); \ |
Steven Rostedt (Red Hat) | d2b0191 | 2014-11-12 17:19:51 -0500 | [diff] [blame] | 32 | return !trace_seq_has_overflowed(s); \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 33 | } \ |
Masami Hiramatsu | 7bfbc63 | 2018-04-25 21:17:34 +0900 | [diff] [blame] | 34 | const char PRINT_TYPE_FMT_NAME(tname)[] = fmt; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 35 | |
Masami Hiramatsu | bdca79c2 | 2016-08-18 17:59:21 +0900 | [diff] [blame] | 36 | DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u") |
| 37 | DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u") |
| 38 | DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u") |
| 39 | DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu") |
Masami Hiramatsu | 17ce3dc | 2016-08-18 17:57:50 +0900 | [diff] [blame] | 40 | DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d") |
| 41 | DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d") |
| 42 | DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d") |
| 43 | DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld") |
| 44 | DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x") |
| 45 | DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x") |
| 46 | DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x") |
| 47 | DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx") |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 48 | |
Masami Hiramatsu | 60c2e0c | 2018-04-25 21:20:28 +0900 | [diff] [blame] | 49 | int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent) |
| 50 | { |
| 51 | trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data); |
| 52 | return !trace_seq_has_overflowed(s); |
| 53 | } |
| 54 | const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS"; |
| 55 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 56 | /* Print type function for string type */ |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 57 | 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] | 58 | { |
| 59 | int len = *(u32 *)data >> 16; |
| 60 | |
| 61 | if (!len) |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 62 | trace_seq_puts(s, "(fault)"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 63 | else |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 64 | trace_seq_printf(s, "\"%s\"", |
Steven Rostedt (Red Hat) | d2b0191 | 2014-11-12 17:19:51 -0500 | [diff] [blame] | 65 | (const char *)get_loc_data(data, ent)); |
| 66 | return !trace_seq_has_overflowed(s); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 67 | } |
| 68 | |
Namhyung Kim | b26c74e | 2013-11-26 14:19:59 +0900 | [diff] [blame] | 69 | const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\""; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 70 | |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 71 | /* Fetch type information table */ |
| 72 | static const struct fetch_type probe_fetch_types[] = { |
| 73 | /* Special types */ |
| 74 | __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, |
| 75 | "__data_loc char[]"), |
| 76 | /* Basic types */ |
| 77 | ASSIGN_FETCH_TYPE(u8, u8, 0), |
| 78 | ASSIGN_FETCH_TYPE(u16, u16, 0), |
| 79 | ASSIGN_FETCH_TYPE(u32, u32, 0), |
| 80 | ASSIGN_FETCH_TYPE(u64, u64, 0), |
| 81 | ASSIGN_FETCH_TYPE(s8, u8, 1), |
| 82 | ASSIGN_FETCH_TYPE(s16, u16, 1), |
| 83 | ASSIGN_FETCH_TYPE(s32, u32, 1), |
| 84 | ASSIGN_FETCH_TYPE(s64, u64, 1), |
| 85 | ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0), |
| 86 | ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0), |
| 87 | ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0), |
| 88 | ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0), |
Masami Hiramatsu | 60c2e0c | 2018-04-25 21:20:28 +0900 | [diff] [blame] | 89 | ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0), |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 90 | |
| 91 | ASSIGN_FETCH_TYPE_END |
| 92 | }; |
| 93 | |
| 94 | static const struct fetch_type *find_fetch_type(const char *type) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 95 | { |
| 96 | int i; |
| 97 | |
| 98 | if (!type) |
| 99 | type = DEFAULT_FETCH_TYPE_STR; |
| 100 | |
| 101 | /* Special case: bitfield */ |
| 102 | if (*type == 'b') { |
| 103 | unsigned long bs; |
| 104 | |
| 105 | type = strchr(type, '/'); |
| 106 | if (!type) |
| 107 | goto fail; |
| 108 | |
| 109 | type++; |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 110 | if (kstrtoul(type, 0, &bs)) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 111 | goto fail; |
| 112 | |
| 113 | switch (bs) { |
| 114 | case 8: |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 115 | return find_fetch_type("u8"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 116 | case 16: |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 117 | return find_fetch_type("u16"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 118 | case 32: |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 119 | return find_fetch_type("u32"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 120 | case 64: |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 121 | return find_fetch_type("u64"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 122 | default: |
| 123 | goto fail; |
| 124 | } |
| 125 | } |
| 126 | |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 127 | for (i = 0; probe_fetch_types[i].name; i++) { |
| 128 | if (strcmp(type, probe_fetch_types[i].name) == 0) |
| 129 | return &probe_fetch_types[i]; |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 130 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 131 | |
| 132 | fail: |
| 133 | return NULL; |
| 134 | } |
| 135 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 136 | /* Split symbol and offset. */ |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 137 | int traceprobe_split_symbol_offset(char *symbol, long *offset) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 138 | { |
| 139 | char *tmp; |
| 140 | int ret; |
| 141 | |
| 142 | if (!offset) |
| 143 | return -EINVAL; |
| 144 | |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 145 | tmp = strpbrk(symbol, "+-"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 146 | if (tmp) { |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 147 | ret = kstrtol(tmp, 0, offset); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 148 | if (ret) |
| 149 | return ret; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 150 | *tmp = '\0'; |
| 151 | } else |
| 152 | *offset = 0; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) |
| 158 | |
| 159 | static int parse_probe_vars(char *arg, const struct fetch_type *t, |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame^] | 160 | struct fetch_insn *code, unsigned int flags) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 161 | { |
| 162 | int ret = 0; |
| 163 | unsigned long param; |
| 164 | |
| 165 | if (strcmp(arg, "retval") == 0) { |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame^] | 166 | if (flags & TPARG_FL_RETURN) |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 167 | code->op = FETCH_OP_RETVAL; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 168 | else |
| 169 | ret = -EINVAL; |
| 170 | } else if (strncmp(arg, "stack", 5) == 0) { |
| 171 | if (arg[5] == '\0') { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 172 | code->op = FETCH_OP_STACKP; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 173 | } else if (isdigit(arg[5])) { |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 174 | ret = kstrtoul(arg + 5, 10, ¶m); |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame^] | 175 | if (ret || ((flags & TPARG_FL_KERNEL) && |
| 176 | param > PARAM_MAX_STACK)) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 177 | ret = -EINVAL; |
| 178 | else { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 179 | code->op = FETCH_OP_STACK; |
| 180 | code->param = (unsigned int)param; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 181 | } |
| 182 | } else |
| 183 | ret = -EINVAL; |
Omar Sandoval | 35abb67 | 2016-06-08 18:38:02 -0700 | [diff] [blame] | 184 | } else if (strcmp(arg, "comm") == 0) { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 185 | code->op = FETCH_OP_COMM; |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame^] | 186 | #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API |
| 187 | } else if (((flags & TPARG_FL_MASK) == |
| 188 | (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) && |
| 189 | strncmp(arg, "arg", 3) == 0) { |
| 190 | if (!isdigit(arg[3])) |
| 191 | return -EINVAL; |
| 192 | ret = kstrtoul(arg + 3, 10, ¶m); |
| 193 | if (ret || !param || param > PARAM_MAX_STACK) |
| 194 | return -EINVAL; |
| 195 | code->op = FETCH_OP_ARG; |
| 196 | code->param = (unsigned int)param - 1; |
| 197 | #endif |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 198 | } else |
| 199 | ret = -EINVAL; |
| 200 | |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | /* Recursive argument parser */ |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 205 | static int |
| 206 | parse_probe_arg(char *arg, const struct fetch_type *type, |
| 207 | struct fetch_insn **pcode, struct fetch_insn *end, |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame^] | 208 | unsigned int flags) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 209 | { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 210 | struct fetch_insn *code = *pcode; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 211 | unsigned long param; |
| 212 | long offset; |
| 213 | char *tmp; |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 214 | int ret = 0; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 215 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 216 | switch (arg[0]) { |
| 217 | case '$': |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame^] | 218 | ret = parse_probe_vars(arg + 1, type, code, flags); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 219 | break; |
| 220 | |
| 221 | case '%': /* named register */ |
| 222 | ret = regs_query_register_offset(arg + 1); |
| 223 | if (ret >= 0) { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 224 | code->op = FETCH_OP_REG; |
| 225 | code->param = (unsigned int)ret; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 226 | ret = 0; |
| 227 | } |
| 228 | break; |
| 229 | |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 230 | case '@': /* memory, file-offset or symbol */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 231 | if (isdigit(arg[1])) { |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 232 | ret = kstrtoul(arg + 1, 0, ¶m); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 233 | if (ret) |
| 234 | break; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 235 | /* load address */ |
| 236 | code->op = FETCH_OP_IMM; |
| 237 | code->immediate = param; |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 238 | } else if (arg[1] == '+') { |
| 239 | /* kprobes don't support file offsets */ |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame^] | 240 | if (flags & TPARG_FL_KERNEL) |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 241 | return -EINVAL; |
| 242 | |
| 243 | ret = kstrtol(arg + 2, 0, &offset); |
| 244 | if (ret) |
| 245 | break; |
| 246 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 247 | code->op = FETCH_OP_FOFFS; |
| 248 | code->immediate = (unsigned long)offset; // imm64? |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 249 | } else { |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 250 | /* uprobes don't support symbols */ |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame^] | 251 | if (!(flags & TPARG_FL_KERNEL)) |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 252 | return -EINVAL; |
| 253 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 254 | ret = traceprobe_split_symbol_offset(arg + 1, &offset); |
| 255 | if (ret) |
| 256 | break; |
| 257 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 258 | code->op = FETCH_OP_IMM; |
| 259 | code->immediate = |
| 260 | (unsigned long)kallsyms_lookup_name(arg + 1); |
| 261 | if (!code->immediate) |
| 262 | return -ENOENT; |
| 263 | code->immediate += offset; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 264 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 265 | /* These are fetching from memory */ |
| 266 | if (++code == end) |
| 267 | return -E2BIG; |
| 268 | *pcode = code; |
| 269 | code->op = FETCH_OP_DEREF; |
| 270 | code->offset = offset; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 271 | break; |
| 272 | |
| 273 | case '+': /* deref memory */ |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 274 | arg++; /* Skip '+', because kstrtol() rejects it. */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 275 | case '-': |
| 276 | tmp = strchr(arg, '('); |
| 277 | if (!tmp) |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 278 | return -EINVAL; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 279 | |
| 280 | *tmp = '\0'; |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 281 | ret = kstrtol(arg, 0, &offset); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 282 | if (ret) |
| 283 | break; |
| 284 | |
| 285 | arg = tmp + 1; |
| 286 | tmp = strrchr(arg, ')'); |
| 287 | |
| 288 | if (tmp) { |
Masami Hiramatsu | f451bc8 | 2018-04-25 21:18:32 +0900 | [diff] [blame] | 289 | const struct fetch_type *t2 = find_fetch_type(NULL); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 290 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 291 | *tmp = '\0'; |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame^] | 292 | ret = parse_probe_arg(arg, t2, &code, end, flags); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 293 | if (ret) |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 294 | break; |
| 295 | if (code->op == FETCH_OP_COMM) |
| 296 | return -EINVAL; |
| 297 | if (++code == end) |
| 298 | return -E2BIG; |
| 299 | *pcode = code; |
| 300 | |
| 301 | code->op = FETCH_OP_DEREF; |
| 302 | code->offset = offset; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 303 | } |
| 304 | break; |
| 305 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 306 | if (!ret && code->op == FETCH_OP_NOP) { |
| 307 | /* Parsed, but do not find fetch method */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 308 | ret = -EINVAL; |
| 309 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long)) |
| 314 | |
| 315 | /* Bitfield type needs to be parsed into a fetch function */ |
| 316 | static int __parse_bitfield_probe_arg(const char *bf, |
| 317 | const struct fetch_type *t, |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 318 | struct fetch_insn **pcode) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 319 | { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 320 | struct fetch_insn *code = *pcode; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 321 | unsigned long bw, bo; |
| 322 | char *tail; |
| 323 | |
| 324 | if (*bf != 'b') |
| 325 | return 0; |
| 326 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 327 | bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */ |
| 328 | |
| 329 | if (bw == 0 || *tail != '@') |
| 330 | return -EINVAL; |
| 331 | |
| 332 | bf = tail + 1; |
| 333 | bo = simple_strtoul(bf, &tail, 0); |
| 334 | |
| 335 | if (tail == bf || *tail != '/') |
| 336 | return -EINVAL; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 337 | code++; |
| 338 | if (code->op != FETCH_OP_NOP) |
| 339 | return -E2BIG; |
| 340 | *pcode = code; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 341 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 342 | code->op = FETCH_OP_MOD_BF; |
| 343 | code->lshift = BYTES_TO_BITS(t->size) - (bw + bo); |
| 344 | code->rshift = BYTES_TO_BITS(t->size) - bw; |
| 345 | code->basesize = t->size; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 346 | |
| 347 | return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0; |
| 348 | } |
| 349 | |
| 350 | /* String length checking wrapper */ |
| 351 | int traceprobe_parse_probe_arg(char *arg, ssize_t *size, |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame^] | 352 | struct probe_arg *parg, unsigned int flags) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 353 | { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 354 | struct fetch_insn *code, *scode, *tmp = NULL; |
| 355 | char *t, *t2; |
| 356 | int ret, len; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 357 | |
| 358 | if (strlen(arg) > MAX_ARGSTR_LEN) { |
| 359 | pr_info("Argument is too long.: %s\n", arg); |
| 360 | return -ENOSPC; |
| 361 | } |
| 362 | parg->comm = kstrdup(arg, GFP_KERNEL); |
| 363 | if (!parg->comm) { |
| 364 | pr_info("Failed to allocate memory for command '%s'.\n", arg); |
| 365 | return -ENOMEM; |
| 366 | } |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 367 | t = strchr(arg, ':'); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 368 | if (t) { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 369 | *t = '\0'; |
| 370 | t2 = strchr(++t, '['); |
| 371 | if (t2) { |
| 372 | *t2 = '\0'; |
| 373 | parg->count = simple_strtoul(t2 + 1, &t2, 0); |
| 374 | if (strcmp(t2, "]") || parg->count == 0) |
| 375 | return -EINVAL; |
| 376 | if (parg->count > MAX_ARRAY_LEN) |
| 377 | return -E2BIG; |
| 378 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 379 | } |
Omar Sandoval | 35abb67 | 2016-06-08 18:38:02 -0700 | [diff] [blame] | 380 | /* |
| 381 | * The default type of $comm should be "string", and it can't be |
| 382 | * dereferenced. |
| 383 | */ |
| 384 | if (!t && strcmp(arg, "$comm") == 0) |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 385 | parg->type = find_fetch_type("string"); |
| 386 | else |
| 387 | parg->type = find_fetch_type(t); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 388 | if (!parg->type) { |
| 389 | pr_info("Unsupported type: %s\n", t); |
| 390 | return -EINVAL; |
| 391 | } |
| 392 | parg->offset = *size; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 393 | *size += parg->type->size * (parg->count ?: 1); |
| 394 | |
| 395 | if (parg->count) { |
| 396 | len = strlen(parg->type->fmttype) + 6; |
| 397 | parg->fmt = kmalloc(len, GFP_KERNEL); |
| 398 | if (!parg->fmt) |
| 399 | return -ENOMEM; |
| 400 | snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype, |
| 401 | parg->count); |
| 402 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 403 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 404 | code = tmp = kzalloc(sizeof(*code) * FETCH_INSN_MAX, GFP_KERNEL); |
| 405 | if (!code) |
| 406 | return -ENOMEM; |
| 407 | code[FETCH_INSN_MAX - 1].op = FETCH_OP_END; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 408 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 409 | ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1], |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame^] | 410 | flags); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 411 | if (ret) |
| 412 | goto fail; |
| 413 | |
| 414 | /* Store operation */ |
| 415 | if (!strcmp(parg->type->name, "string")) { |
| 416 | if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_IMM && |
| 417 | code->op != FETCH_OP_COMM) { |
| 418 | pr_info("string only accepts memory or address.\n"); |
| 419 | ret = -EINVAL; |
| 420 | goto fail; |
| 421 | } |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 422 | if (code->op != FETCH_OP_DEREF || parg->count) { |
| 423 | /* |
| 424 | * IMM and COMM is pointing actual address, those must |
| 425 | * be kept, and if parg->count != 0, this is an array |
| 426 | * of string pointers instead of string address itself. |
| 427 | */ |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 428 | code++; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 429 | if (code->op != FETCH_OP_NOP) { |
| 430 | ret = -E2BIG; |
| 431 | goto fail; |
| 432 | } |
| 433 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 434 | code->op = FETCH_OP_ST_STRING; /* In DEREF case, replace it */ |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 435 | code->size = parg->type->size; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 436 | parg->dynamic = true; |
| 437 | } else if (code->op == FETCH_OP_DEREF) { |
| 438 | code->op = FETCH_OP_ST_MEM; |
| 439 | code->size = parg->type->size; |
| 440 | } else { |
| 441 | code++; |
| 442 | if (code->op != FETCH_OP_NOP) { |
| 443 | ret = -E2BIG; |
| 444 | goto fail; |
| 445 | } |
| 446 | code->op = FETCH_OP_ST_RAW; |
| 447 | code->size = parg->type->size; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 448 | } |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 449 | scode = code; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 450 | /* Modify operation */ |
| 451 | if (t != NULL) { |
| 452 | ret = __parse_bitfield_probe_arg(t, parg->type, &code); |
| 453 | if (ret) |
| 454 | goto fail; |
| 455 | } |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 456 | /* Loop(Array) operation */ |
| 457 | if (parg->count) { |
| 458 | if (scode->op != FETCH_OP_ST_MEM && |
| 459 | scode->op != FETCH_OP_ST_STRING) { |
| 460 | pr_info("array only accepts memory or address\n"); |
| 461 | ret = -EINVAL; |
| 462 | goto fail; |
| 463 | } |
| 464 | code++; |
| 465 | if (code->op != FETCH_OP_NOP) { |
| 466 | ret = -E2BIG; |
| 467 | goto fail; |
| 468 | } |
| 469 | code->op = FETCH_OP_LP_ARRAY; |
| 470 | code->param = parg->count; |
| 471 | } |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 472 | code++; |
| 473 | code->op = FETCH_OP_END; |
| 474 | |
| 475 | /* Shrink down the code buffer */ |
| 476 | parg->code = kzalloc(sizeof(*code) * (code - tmp + 1), GFP_KERNEL); |
| 477 | if (!parg->code) |
| 478 | ret = -ENOMEM; |
| 479 | else |
| 480 | memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1)); |
| 481 | |
| 482 | fail: |
| 483 | kfree(tmp); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 484 | |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | /* Return 1 if name is reserved or already used by another argument */ |
| 489 | int traceprobe_conflict_field_name(const char *name, |
| 490 | struct probe_arg *args, int narg) |
| 491 | { |
| 492 | int i; |
| 493 | |
| 494 | for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) |
| 495 | if (strcmp(reserved_field_names[i], name) == 0) |
| 496 | return 1; |
| 497 | |
| 498 | for (i = 0; i < narg; i++) |
| 499 | if (strcmp(args[i].name, name) == 0) |
| 500 | return 1; |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 505 | void traceprobe_free_probe_arg(struct probe_arg *arg) |
| 506 | { |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 507 | kfree(arg->code); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 508 | kfree(arg->name); |
| 509 | kfree(arg->comm); |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 510 | kfree(arg->fmt); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 511 | } |
| 512 | |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 513 | /* When len=0, we just calculate the needed length */ |
| 514 | #define LEN_OR_ZERO (len ? len - pos : 0) |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 515 | static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, |
| 516 | bool is_return) |
| 517 | { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 518 | struct probe_arg *parg; |
| 519 | int i, j; |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 520 | int pos = 0; |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 521 | const char *fmt, *arg; |
| 522 | |
| 523 | if (!is_return) { |
| 524 | fmt = "(%lx)"; |
| 525 | arg = "REC->" FIELD_STRING_IP; |
| 526 | } else { |
| 527 | fmt = "(%lx <- %lx)"; |
| 528 | arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; |
| 529 | } |
| 530 | |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 531 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); |
| 532 | |
| 533 | for (i = 0; i < tp->nr_args; i++) { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 534 | parg = tp->args + i; |
| 535 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name); |
| 536 | if (parg->count) { |
| 537 | pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s", |
| 538 | parg->type->fmt); |
| 539 | for (j = 1; j < parg->count; j++) |
| 540 | pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s", |
| 541 | parg->type->fmt); |
| 542 | pos += snprintf(buf + pos, LEN_OR_ZERO, "}"); |
| 543 | } else |
| 544 | pos += snprintf(buf + pos, LEN_OR_ZERO, "%s", |
| 545 | parg->type->fmt); |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); |
| 549 | |
| 550 | for (i = 0; i < tp->nr_args; i++) { |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 551 | parg = tp->args + i; |
| 552 | if (parg->count) { |
| 553 | if (strcmp(parg->type->name, "string") == 0) |
| 554 | fmt = ", __get_str(%s[%d])"; |
| 555 | else |
| 556 | fmt = ", REC->%s[%d]"; |
| 557 | for (j = 0; j < parg->count; j++) |
| 558 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
| 559 | fmt, parg->name, j); |
| 560 | } else { |
| 561 | if (strcmp(parg->type->name, "string") == 0) |
| 562 | fmt = ", __get_str(%s)"; |
| 563 | else |
| 564 | fmt = ", REC->%s"; |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 565 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 566 | fmt, parg->name); |
| 567 | } |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 568 | } |
| 569 | |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 570 | /* return the length of print_fmt */ |
| 571 | return pos; |
| 572 | } |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 573 | #undef LEN_OR_ZERO |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 574 | |
Masami Hiramatsu | 0a46c85 | 2018-04-25 21:19:30 +0900 | [diff] [blame] | 575 | int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return) |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 576 | { |
| 577 | int len; |
| 578 | char *print_fmt; |
| 579 | |
| 580 | /* First: called with 0 length to calculate the needed length */ |
| 581 | len = __set_print_fmt(tp, NULL, 0, is_return); |
| 582 | print_fmt = kmalloc(len + 1, GFP_KERNEL); |
| 583 | if (!print_fmt) |
| 584 | return -ENOMEM; |
| 585 | |
| 586 | /* Second: actually write the @print_fmt */ |
| 587 | __set_print_fmt(tp, print_fmt, len + 1, is_return); |
| 588 | tp->call.print_fmt = print_fmt; |
| 589 | |
| 590 | return 0; |
| 591 | } |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 592 | |
| 593 | int traceprobe_define_arg_fields(struct trace_event_call *event_call, |
| 594 | size_t offset, struct trace_probe *tp) |
| 595 | { |
| 596 | int ret, i; |
| 597 | |
| 598 | /* Set argument names as fields */ |
| 599 | for (i = 0; i < tp->nr_args; i++) { |
| 600 | struct probe_arg *parg = &tp->args[i]; |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 601 | const char *fmt = parg->type->fmttype; |
| 602 | int size = parg->type->size; |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 603 | |
Masami Hiramatsu | 40b53b7 | 2018-04-25 21:21:55 +0900 | [diff] [blame] | 604 | if (parg->fmt) |
| 605 | fmt = parg->fmt; |
| 606 | if (parg->count) |
| 607 | size *= parg->count; |
| 608 | ret = trace_define_field(event_call, fmt, parg->name, |
| 609 | offset + parg->offset, size, |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 610 | parg->type->is_signed, |
| 611 | FILTER_OTHER); |
| 612 | if (ret) |
| 613 | return ret; |
| 614 | } |
| 615 | return 0; |
| 616 | } |