Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Common code for probe-based Dynamic events. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | * |
| 17 | * This code was copied from kernel/trace/trace_kprobe.c written by |
| 18 | * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> |
| 19 | * |
| 20 | * Updates to make this generic: |
| 21 | * Copyright (C) IBM Corporation, 2010-2011 |
| 22 | * Author: Srikar Dronamraju |
| 23 | */ |
| 24 | |
| 25 | #include "trace_probe.h" |
| 26 | |
| 27 | const char *reserved_field_names[] = { |
| 28 | "common_type", |
| 29 | "common_flags", |
| 30 | "common_preempt_count", |
| 31 | "common_pid", |
| 32 | "common_tgid", |
| 33 | FIELD_STRING_IP, |
| 34 | FIELD_STRING_RETIP, |
| 35 | FIELD_STRING_FUNC, |
| 36 | }; |
| 37 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 38 | /* Printing in basic type function template */ |
Namhyung Kim | 50eb267 | 2013-07-31 17:21:01 +0900 | [diff] [blame] | 39 | #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt) \ |
Namhyung Kim | b26c74e | 2013-11-26 14:19:59 +0900 | [diff] [blame] | 40 | __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 41 | const char *name, \ |
Namhyung Kim | 50eb267 | 2013-07-31 17:21:01 +0900 | [diff] [blame] | 42 | void *data, void *ent) \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 43 | { \ |
Namhyung Kim | 50eb267 | 2013-07-31 17:21:01 +0900 | [diff] [blame] | 44 | return trace_seq_printf(s, " %s=" fmt, name, *(type *)data); \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 45 | } \ |
Namhyung Kim | b26c74e | 2013-11-26 14:19:59 +0900 | [diff] [blame] | 46 | const char PRINT_TYPE_FMT_NAME(type)[] = fmt; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 47 | |
Namhyung Kim | 50eb267 | 2013-07-31 17:21:01 +0900 | [diff] [blame] | 48 | DEFINE_BASIC_PRINT_TYPE_FUNC(u8 , "0x%x") |
| 49 | DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "0x%x") |
| 50 | DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "0x%x") |
| 51 | DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "0x%Lx") |
| 52 | DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d") |
| 53 | DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d") |
| 54 | DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%d") |
| 55 | DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%Ld") |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 56 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 57 | /* Print type function for string type */ |
Namhyung Kim | b26c74e | 2013-11-26 14:19:59 +0900 | [diff] [blame] | 58 | __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 59 | const char *name, |
| 60 | void *data, void *ent) |
| 61 | { |
| 62 | int len = *(u32 *)data >> 16; |
| 63 | |
| 64 | if (!len) |
| 65 | return trace_seq_printf(s, " %s=(fault)", name); |
| 66 | else |
| 67 | return trace_seq_printf(s, " %s=\"%s\"", name, |
| 68 | (const char *)get_loc_data(data, ent)); |
| 69 | } |
| 70 | |
Namhyung Kim | b26c74e | 2013-11-26 14:19:59 +0900 | [diff] [blame] | 71 | const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\""; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 72 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 73 | #define CHECK_FETCH_FUNCS(method, fn) \ |
| 74 | (((FETCH_FUNC_NAME(method, u8) == fn) || \ |
| 75 | (FETCH_FUNC_NAME(method, u16) == fn) || \ |
| 76 | (FETCH_FUNC_NAME(method, u32) == fn) || \ |
| 77 | (FETCH_FUNC_NAME(method, u64) == fn) || \ |
| 78 | (FETCH_FUNC_NAME(method, string) == fn) || \ |
| 79 | (FETCH_FUNC_NAME(method, string_size) == fn)) \ |
| 80 | && (fn != NULL)) |
| 81 | |
| 82 | /* Data fetch function templates */ |
| 83 | #define DEFINE_FETCH_reg(type) \ |
Namhyung Kim | b26c74e | 2013-11-26 14:19:59 +0900 | [diff] [blame] | 84 | __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 85 | void *offset, void *dest) \ |
| 86 | { \ |
| 87 | *(type *)dest = (type)regs_get_register(regs, \ |
| 88 | (unsigned int)((unsigned long)offset)); \ |
| 89 | } |
| 90 | DEFINE_BASIC_FETCH_FUNCS(reg) |
| 91 | /* No string on the register */ |
| 92 | #define fetch_reg_string NULL |
| 93 | #define fetch_reg_string_size NULL |
| 94 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 95 | #define DEFINE_FETCH_retval(type) \ |
Namhyung Kim | b26c74e | 2013-11-26 14:19:59 +0900 | [diff] [blame] | 96 | __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs, \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 97 | void *dummy, void *dest) \ |
| 98 | { \ |
| 99 | *(type *)dest = (type)regs_return_value(regs); \ |
| 100 | } |
| 101 | DEFINE_BASIC_FETCH_FUNCS(retval) |
| 102 | /* No string on the retval */ |
| 103 | #define fetch_retval_string NULL |
| 104 | #define fetch_retval_string_size NULL |
| 105 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 106 | /* Dereference memory access function */ |
| 107 | struct deref_fetch_param { |
| 108 | struct fetch_param orig; |
| 109 | long offset; |
Hyeoncheol Lee | 3925f4a | 2013-07-01 13:44:32 +0900 | [diff] [blame] | 110 | fetch_func_t fetch; |
| 111 | fetch_func_t fetch_size; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | #define DEFINE_FETCH_deref(type) \ |
Namhyung Kim | b26c74e | 2013-11-26 14:19:59 +0900 | [diff] [blame] | 115 | __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs, \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 116 | void *data, void *dest) \ |
| 117 | { \ |
| 118 | struct deref_fetch_param *dprm = data; \ |
| 119 | unsigned long addr; \ |
| 120 | call_fetch(&dprm->orig, regs, &addr); \ |
| 121 | if (addr) { \ |
| 122 | addr += dprm->offset; \ |
Hyeoncheol Lee | 3925f4a | 2013-07-01 13:44:32 +0900 | [diff] [blame] | 123 | dprm->fetch(regs, (void *)addr, dest); \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 124 | } else \ |
| 125 | *(type *)dest = 0; \ |
| 126 | } |
| 127 | DEFINE_BASIC_FETCH_FUNCS(deref) |
| 128 | DEFINE_FETCH_deref(string) |
Hyeoncheol Lee | 3925f4a | 2013-07-01 13:44:32 +0900 | [diff] [blame] | 129 | |
| 130 | __kprobes void FETCH_FUNC_NAME(deref, string_size)(struct pt_regs *regs, |
| 131 | void *data, void *dest) |
| 132 | { |
| 133 | struct deref_fetch_param *dprm = data; |
| 134 | unsigned long addr; |
| 135 | |
| 136 | call_fetch(&dprm->orig, regs, &addr); |
| 137 | if (addr && dprm->fetch_size) { |
| 138 | addr += dprm->offset; |
| 139 | dprm->fetch_size(regs, (void *)addr, dest); |
| 140 | } else |
| 141 | *(string_size *)dest = 0; |
| 142 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 143 | |
| 144 | static __kprobes void update_deref_fetch_param(struct deref_fetch_param *data) |
| 145 | { |
| 146 | if (CHECK_FETCH_FUNCS(deref, data->orig.fn)) |
| 147 | update_deref_fetch_param(data->orig.data); |
| 148 | else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn)) |
| 149 | update_symbol_cache(data->orig.data); |
| 150 | } |
| 151 | |
| 152 | static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data) |
| 153 | { |
| 154 | if (CHECK_FETCH_FUNCS(deref, data->orig.fn)) |
| 155 | free_deref_fetch_param(data->orig.data); |
| 156 | else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn)) |
| 157 | free_symbol_cache(data->orig.data); |
| 158 | kfree(data); |
| 159 | } |
| 160 | |
| 161 | /* Bitfield fetch function */ |
| 162 | struct bitfield_fetch_param { |
| 163 | struct fetch_param orig; |
| 164 | unsigned char hi_shift; |
| 165 | unsigned char low_shift; |
| 166 | }; |
| 167 | |
| 168 | #define DEFINE_FETCH_bitfield(type) \ |
Namhyung Kim | b26c74e | 2013-11-26 14:19:59 +0900 | [diff] [blame] | 169 | __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs, \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 170 | void *data, void *dest) \ |
| 171 | { \ |
| 172 | struct bitfield_fetch_param *bprm = data; \ |
| 173 | type buf = 0; \ |
| 174 | call_fetch(&bprm->orig, regs, &buf); \ |
| 175 | if (buf) { \ |
| 176 | buf <<= bprm->hi_shift; \ |
| 177 | buf >>= bprm->low_shift; \ |
| 178 | } \ |
| 179 | *(type *)dest = buf; \ |
| 180 | } |
| 181 | |
| 182 | DEFINE_BASIC_FETCH_FUNCS(bitfield) |
| 183 | #define fetch_bitfield_string NULL |
| 184 | #define fetch_bitfield_string_size NULL |
| 185 | |
| 186 | static __kprobes void |
| 187 | update_bitfield_fetch_param(struct bitfield_fetch_param *data) |
| 188 | { |
| 189 | /* |
| 190 | * Don't check the bitfield itself, because this must be the |
| 191 | * last fetch function. |
| 192 | */ |
| 193 | if (CHECK_FETCH_FUNCS(deref, data->orig.fn)) |
| 194 | update_deref_fetch_param(data->orig.data); |
| 195 | else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn)) |
| 196 | update_symbol_cache(data->orig.data); |
| 197 | } |
| 198 | |
| 199 | static __kprobes void |
| 200 | free_bitfield_fetch_param(struct bitfield_fetch_param *data) |
| 201 | { |
| 202 | /* |
| 203 | * Don't check the bitfield itself, because this must be the |
| 204 | * last fetch function. |
| 205 | */ |
| 206 | if (CHECK_FETCH_FUNCS(deref, data->orig.fn)) |
| 207 | free_deref_fetch_param(data->orig.data); |
| 208 | else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn)) |
| 209 | free_symbol_cache(data->orig.data); |
| 210 | |
| 211 | kfree(data); |
| 212 | } |
| 213 | |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 214 | static const struct fetch_type *find_fetch_type(const char *type, |
| 215 | const struct fetch_type *ftbl) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 216 | { |
| 217 | int i; |
| 218 | |
| 219 | if (!type) |
| 220 | type = DEFAULT_FETCH_TYPE_STR; |
| 221 | |
| 222 | /* Special case: bitfield */ |
| 223 | if (*type == 'b') { |
| 224 | unsigned long bs; |
| 225 | |
| 226 | type = strchr(type, '/'); |
| 227 | if (!type) |
| 228 | goto fail; |
| 229 | |
| 230 | type++; |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 231 | if (kstrtoul(type, 0, &bs)) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 232 | goto fail; |
| 233 | |
| 234 | switch (bs) { |
| 235 | case 8: |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 236 | return find_fetch_type("u8", ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 237 | case 16: |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 238 | return find_fetch_type("u16", ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 239 | case 32: |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 240 | return find_fetch_type("u32", ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 241 | case 64: |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 242 | return find_fetch_type("u64", ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 243 | default: |
| 244 | goto fail; |
| 245 | } |
| 246 | } |
| 247 | |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 248 | for (i = 0; ftbl[i].name; i++) { |
| 249 | if (strcmp(type, ftbl[i].name) == 0) |
| 250 | return &ftbl[i]; |
| 251 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 252 | |
| 253 | fail: |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | /* Special function : only accept unsigned long */ |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame^] | 258 | static __kprobes void fetch_kernel_stack_address(struct pt_regs *regs, |
| 259 | void *dummy, void *dest) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 260 | { |
| 261 | *(unsigned long *)dest = kernel_stack_pointer(regs); |
| 262 | } |
| 263 | |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame^] | 264 | static __kprobes void fetch_user_stack_address(struct pt_regs *regs, |
| 265 | void *dummy, void *dest) |
| 266 | { |
| 267 | *(unsigned long *)dest = user_stack_pointer(regs); |
| 268 | } |
| 269 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 270 | static fetch_func_t get_fetch_size_function(const struct fetch_type *type, |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 271 | fetch_func_t orig_fn, |
| 272 | const struct fetch_type *ftbl) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 273 | { |
| 274 | int i; |
| 275 | |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 276 | if (type != &ftbl[FETCH_TYPE_STRING]) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 277 | return NULL; /* Only string type needs size function */ |
| 278 | |
| 279 | for (i = 0; i < FETCH_MTD_END; i++) |
| 280 | if (type->fetch[i] == orig_fn) |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 281 | return ftbl[FETCH_TYPE_STRSIZE].fetch[i]; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 282 | |
| 283 | WARN_ON(1); /* This should not happen */ |
| 284 | |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | /* Split symbol and offset. */ |
| 289 | int traceprobe_split_symbol_offset(char *symbol, unsigned long *offset) |
| 290 | { |
| 291 | char *tmp; |
| 292 | int ret; |
| 293 | |
| 294 | if (!offset) |
| 295 | return -EINVAL; |
| 296 | |
| 297 | tmp = strchr(symbol, '+'); |
| 298 | if (tmp) { |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 299 | /* skip sign because kstrtoul doesn't accept '+' */ |
| 300 | ret = kstrtoul(tmp + 1, 0, offset); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 301 | if (ret) |
| 302 | return ret; |
| 303 | |
| 304 | *tmp = '\0'; |
| 305 | } else |
| 306 | *offset = 0; |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) |
| 312 | |
| 313 | static int parse_probe_vars(char *arg, const struct fetch_type *t, |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame^] | 314 | struct fetch_param *f, bool is_return, |
| 315 | bool is_kprobe) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 316 | { |
| 317 | int ret = 0; |
| 318 | unsigned long param; |
| 319 | |
| 320 | if (strcmp(arg, "retval") == 0) { |
| 321 | if (is_return) |
| 322 | f->fn = t->fetch[FETCH_MTD_retval]; |
| 323 | else |
| 324 | ret = -EINVAL; |
| 325 | } else if (strncmp(arg, "stack", 5) == 0) { |
| 326 | if (arg[5] == '\0') { |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame^] | 327 | if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR)) |
| 328 | return -EINVAL; |
| 329 | |
| 330 | if (is_kprobe) |
| 331 | f->fn = fetch_kernel_stack_address; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 332 | else |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame^] | 333 | f->fn = fetch_user_stack_address; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 334 | } else if (isdigit(arg[5])) { |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 335 | ret = kstrtoul(arg + 5, 10, ¶m); |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame^] | 336 | if (ret || (is_kprobe && param > PARAM_MAX_STACK)) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 337 | ret = -EINVAL; |
| 338 | else { |
| 339 | f->fn = t->fetch[FETCH_MTD_stack]; |
| 340 | f->data = (void *)param; |
| 341 | } |
| 342 | } else |
| 343 | ret = -EINVAL; |
| 344 | } else |
| 345 | ret = -EINVAL; |
| 346 | |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | /* Recursive argument parser */ |
| 351 | static int parse_probe_arg(char *arg, const struct fetch_type *t, |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 352 | struct fetch_param *f, bool is_return, bool is_kprobe) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 353 | { |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 354 | const struct fetch_type *ftbl; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 355 | unsigned long param; |
| 356 | long offset; |
| 357 | char *tmp; |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 358 | int ret = 0; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 359 | |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 360 | ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table; |
| 361 | BUG_ON(ftbl == NULL); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 362 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 363 | switch (arg[0]) { |
| 364 | case '$': |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame^] | 365 | ret = parse_probe_vars(arg + 1, t, f, is_return, is_kprobe); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 366 | break; |
| 367 | |
| 368 | case '%': /* named register */ |
| 369 | ret = regs_query_register_offset(arg + 1); |
| 370 | if (ret >= 0) { |
| 371 | f->fn = t->fetch[FETCH_MTD_reg]; |
| 372 | f->data = (void *)(unsigned long)ret; |
| 373 | ret = 0; |
| 374 | } |
| 375 | break; |
| 376 | |
| 377 | case '@': /* memory or symbol */ |
| 378 | if (isdigit(arg[1])) { |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 379 | ret = kstrtoul(arg + 1, 0, ¶m); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 380 | if (ret) |
| 381 | break; |
| 382 | |
| 383 | f->fn = t->fetch[FETCH_MTD_memory]; |
| 384 | f->data = (void *)param; |
| 385 | } else { |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame^] | 386 | /* uprobes don't support symbols */ |
| 387 | if (!is_kprobe) |
| 388 | return -EINVAL; |
| 389 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 390 | ret = traceprobe_split_symbol_offset(arg + 1, &offset); |
| 391 | if (ret) |
| 392 | break; |
| 393 | |
| 394 | f->data = alloc_symbol_cache(arg + 1, offset); |
| 395 | if (f->data) |
| 396 | f->fn = t->fetch[FETCH_MTD_symbol]; |
| 397 | } |
| 398 | break; |
| 399 | |
| 400 | case '+': /* deref memory */ |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 401 | arg++; /* Skip '+', because kstrtol() rejects it. */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 402 | case '-': |
| 403 | tmp = strchr(arg, '('); |
| 404 | if (!tmp) |
| 405 | break; |
| 406 | |
| 407 | *tmp = '\0'; |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 408 | ret = kstrtol(arg, 0, &offset); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 409 | |
| 410 | if (ret) |
| 411 | break; |
| 412 | |
| 413 | arg = tmp + 1; |
| 414 | tmp = strrchr(arg, ')'); |
| 415 | |
| 416 | if (tmp) { |
| 417 | struct deref_fetch_param *dprm; |
| 418 | const struct fetch_type *t2; |
| 419 | |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 420 | t2 = find_fetch_type(NULL, ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 421 | *tmp = '\0'; |
| 422 | dprm = kzalloc(sizeof(struct deref_fetch_param), GFP_KERNEL); |
| 423 | |
| 424 | if (!dprm) |
| 425 | return -ENOMEM; |
| 426 | |
| 427 | dprm->offset = offset; |
Hyeoncheol Lee | 3925f4a | 2013-07-01 13:44:32 +0900 | [diff] [blame] | 428 | dprm->fetch = t->fetch[FETCH_MTD_memory]; |
| 429 | dprm->fetch_size = get_fetch_size_function(t, |
| 430 | dprm->fetch, ftbl); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 431 | ret = parse_probe_arg(arg, t2, &dprm->orig, is_return, |
| 432 | is_kprobe); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 433 | if (ret) |
| 434 | kfree(dprm); |
| 435 | else { |
| 436 | f->fn = t->fetch[FETCH_MTD_deref]; |
| 437 | f->data = (void *)dprm; |
| 438 | } |
| 439 | } |
| 440 | break; |
| 441 | } |
| 442 | if (!ret && !f->fn) { /* Parsed, but do not find fetch method */ |
| 443 | pr_info("%s type has no corresponding fetch method.\n", t->name); |
| 444 | ret = -EINVAL; |
| 445 | } |
| 446 | |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long)) |
| 451 | |
| 452 | /* Bitfield type needs to be parsed into a fetch function */ |
| 453 | static int __parse_bitfield_probe_arg(const char *bf, |
| 454 | const struct fetch_type *t, |
| 455 | struct fetch_param *f) |
| 456 | { |
| 457 | struct bitfield_fetch_param *bprm; |
| 458 | unsigned long bw, bo; |
| 459 | char *tail; |
| 460 | |
| 461 | if (*bf != 'b') |
| 462 | return 0; |
| 463 | |
| 464 | bprm = kzalloc(sizeof(*bprm), GFP_KERNEL); |
| 465 | if (!bprm) |
| 466 | return -ENOMEM; |
| 467 | |
| 468 | bprm->orig = *f; |
| 469 | f->fn = t->fetch[FETCH_MTD_bitfield]; |
| 470 | f->data = (void *)bprm; |
| 471 | bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */ |
| 472 | |
| 473 | if (bw == 0 || *tail != '@') |
| 474 | return -EINVAL; |
| 475 | |
| 476 | bf = tail + 1; |
| 477 | bo = simple_strtoul(bf, &tail, 0); |
| 478 | |
| 479 | if (tail == bf || *tail != '/') |
| 480 | return -EINVAL; |
| 481 | |
| 482 | bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo); |
| 483 | bprm->low_shift = bprm->hi_shift + bo; |
| 484 | |
| 485 | return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0; |
| 486 | } |
| 487 | |
| 488 | /* String length checking wrapper */ |
| 489 | int traceprobe_parse_probe_arg(char *arg, ssize_t *size, |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 490 | struct probe_arg *parg, bool is_return, bool is_kprobe) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 491 | { |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 492 | const struct fetch_type *ftbl; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 493 | const char *t; |
| 494 | int ret; |
| 495 | |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 496 | ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table; |
| 497 | BUG_ON(ftbl == NULL); |
| 498 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 499 | if (strlen(arg) > MAX_ARGSTR_LEN) { |
| 500 | pr_info("Argument is too long.: %s\n", arg); |
| 501 | return -ENOSPC; |
| 502 | } |
| 503 | parg->comm = kstrdup(arg, GFP_KERNEL); |
| 504 | if (!parg->comm) { |
| 505 | pr_info("Failed to allocate memory for command '%s'.\n", arg); |
| 506 | return -ENOMEM; |
| 507 | } |
| 508 | t = strchr(parg->comm, ':'); |
| 509 | if (t) { |
| 510 | arg[t - parg->comm] = '\0'; |
| 511 | t++; |
| 512 | } |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 513 | parg->type = find_fetch_type(t, ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 514 | if (!parg->type) { |
| 515 | pr_info("Unsupported type: %s\n", t); |
| 516 | return -EINVAL; |
| 517 | } |
| 518 | parg->offset = *size; |
| 519 | *size += parg->type->size; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 520 | ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return, is_kprobe); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 521 | |
| 522 | if (ret >= 0 && t != NULL) |
| 523 | ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch); |
| 524 | |
| 525 | if (ret >= 0) { |
| 526 | parg->fetch_size.fn = get_fetch_size_function(parg->type, |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 527 | parg->fetch.fn, |
| 528 | ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 529 | parg->fetch_size.data = parg->fetch.data; |
| 530 | } |
| 531 | |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | /* Return 1 if name is reserved or already used by another argument */ |
| 536 | int traceprobe_conflict_field_name(const char *name, |
| 537 | struct probe_arg *args, int narg) |
| 538 | { |
| 539 | int i; |
| 540 | |
| 541 | for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) |
| 542 | if (strcmp(reserved_field_names[i], name) == 0) |
| 543 | return 1; |
| 544 | |
| 545 | for (i = 0; i < narg; i++) |
| 546 | if (strcmp(args[i].name, name) == 0) |
| 547 | return 1; |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | void traceprobe_update_arg(struct probe_arg *arg) |
| 553 | { |
| 554 | if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn)) |
| 555 | update_bitfield_fetch_param(arg->fetch.data); |
| 556 | else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn)) |
| 557 | update_deref_fetch_param(arg->fetch.data); |
| 558 | else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn)) |
| 559 | update_symbol_cache(arg->fetch.data); |
| 560 | } |
| 561 | |
| 562 | void traceprobe_free_probe_arg(struct probe_arg *arg) |
| 563 | { |
| 564 | if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn)) |
| 565 | free_bitfield_fetch_param(arg->fetch.data); |
| 566 | else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn)) |
| 567 | free_deref_fetch_param(arg->fetch.data); |
| 568 | else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn)) |
| 569 | free_symbol_cache(arg->fetch.data); |
| 570 | |
| 571 | kfree(arg->name); |
| 572 | kfree(arg->comm); |
| 573 | } |
| 574 | |
| 575 | int traceprobe_command(const char *buf, int (*createfn)(int, char **)) |
| 576 | { |
| 577 | char **argv; |
| 578 | int argc, ret; |
| 579 | |
| 580 | argc = 0; |
| 581 | ret = 0; |
| 582 | argv = argv_split(GFP_KERNEL, buf, &argc); |
| 583 | if (!argv) |
| 584 | return -ENOMEM; |
| 585 | |
| 586 | if (argc) |
| 587 | ret = createfn(argc, argv); |
| 588 | |
| 589 | argv_free(argv); |
| 590 | |
| 591 | return ret; |
| 592 | } |
| 593 | |
| 594 | #define WRITE_BUFSIZE 4096 |
| 595 | |
| 596 | ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer, |
| 597 | size_t count, loff_t *ppos, |
| 598 | int (*createfn)(int, char **)) |
| 599 | { |
| 600 | char *kbuf, *tmp; |
| 601 | int ret = 0; |
| 602 | size_t done = 0; |
| 603 | size_t size; |
| 604 | |
| 605 | kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL); |
| 606 | if (!kbuf) |
| 607 | return -ENOMEM; |
| 608 | |
| 609 | while (done < count) { |
| 610 | size = count - done; |
| 611 | |
| 612 | if (size >= WRITE_BUFSIZE) |
| 613 | size = WRITE_BUFSIZE - 1; |
| 614 | |
| 615 | if (copy_from_user(kbuf, buffer + done, size)) { |
| 616 | ret = -EFAULT; |
| 617 | goto out; |
| 618 | } |
| 619 | kbuf[size] = '\0'; |
| 620 | tmp = strchr(kbuf, '\n'); |
| 621 | |
| 622 | if (tmp) { |
| 623 | *tmp = '\0'; |
| 624 | size = tmp - kbuf + 1; |
| 625 | } else if (done + size < count) { |
| 626 | pr_warning("Line length is too long: " |
| 627 | "Should be less than %d.", WRITE_BUFSIZE); |
| 628 | ret = -EINVAL; |
| 629 | goto out; |
| 630 | } |
| 631 | done += size; |
| 632 | /* Remove comments */ |
| 633 | tmp = strchr(kbuf, '#'); |
| 634 | |
| 635 | if (tmp) |
| 636 | *tmp = '\0'; |
| 637 | |
| 638 | ret = traceprobe_command(kbuf, createfn); |
| 639 | if (ret) |
| 640 | goto out; |
| 641 | } |
| 642 | ret = done; |
| 643 | |
| 644 | out: |
| 645 | kfree(kbuf); |
| 646 | |
| 647 | return ret; |
| 648 | } |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 649 | |
| 650 | static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, |
| 651 | bool is_return) |
| 652 | { |
| 653 | int i; |
| 654 | int pos = 0; |
| 655 | |
| 656 | const char *fmt, *arg; |
| 657 | |
| 658 | if (!is_return) { |
| 659 | fmt = "(%lx)"; |
| 660 | arg = "REC->" FIELD_STRING_IP; |
| 661 | } else { |
| 662 | fmt = "(%lx <- %lx)"; |
| 663 | arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; |
| 664 | } |
| 665 | |
| 666 | /* When len=0, we just calculate the needed length */ |
| 667 | #define LEN_OR_ZERO (len ? len - pos : 0) |
| 668 | |
| 669 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); |
| 670 | |
| 671 | for (i = 0; i < tp->nr_args; i++) { |
| 672 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s", |
| 673 | tp->args[i].name, tp->args[i].type->fmt); |
| 674 | } |
| 675 | |
| 676 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); |
| 677 | |
| 678 | for (i = 0; i < tp->nr_args; i++) { |
| 679 | if (strcmp(tp->args[i].type->name, "string") == 0) |
| 680 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
| 681 | ", __get_str(%s)", |
| 682 | tp->args[i].name); |
| 683 | else |
| 684 | pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s", |
| 685 | tp->args[i].name); |
| 686 | } |
| 687 | |
| 688 | #undef LEN_OR_ZERO |
| 689 | |
| 690 | /* return the length of print_fmt */ |
| 691 | return pos; |
| 692 | } |
| 693 | |
| 694 | int set_print_fmt(struct trace_probe *tp, bool is_return) |
| 695 | { |
| 696 | int len; |
| 697 | char *print_fmt; |
| 698 | |
| 699 | /* First: called with 0 length to calculate the needed length */ |
| 700 | len = __set_print_fmt(tp, NULL, 0, is_return); |
| 701 | print_fmt = kmalloc(len + 1, GFP_KERNEL); |
| 702 | if (!print_fmt) |
| 703 | return -ENOMEM; |
| 704 | |
| 705 | /* Second: actually write the @print_fmt */ |
| 706 | __set_print_fmt(tp, print_fmt, len + 1, is_return); |
| 707 | tp->call.print_fmt = print_fmt; |
| 708 | |
| 709 | return 0; |
| 710 | } |