Commit | Line | Data |
---|---|---|
8ab83f56 SD |
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 | ||
8ab83f56 | 38 | /* Printing in basic type function template */ |
50eb2672 | 39 | #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt) \ |
b26c74e1 | 40 | __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \ |
8ab83f56 | 41 | const char *name, \ |
50eb2672 | 42 | void *data, void *ent) \ |
8ab83f56 | 43 | { \ |
50eb2672 | 44 | return trace_seq_printf(s, " %s=" fmt, name, *(type *)data); \ |
8ab83f56 | 45 | } \ |
b26c74e1 | 46 | const char PRINT_TYPE_FMT_NAME(type)[] = fmt; |
8ab83f56 | 47 | |
50eb2672 NK |
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") | |
8ab83f56 | 56 | |
8ab83f56 | 57 | /* Print type function for string type */ |
b26c74e1 | 58 | __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, |
8ab83f56 SD |
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 | ||
b26c74e1 | 71 | const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\""; |
8ab83f56 | 72 | |
8ab83f56 SD |
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) \ | |
b26c74e1 | 84 | __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \ |
8ab83f56 SD |
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 | ||
8ab83f56 | 95 | #define DEFINE_FETCH_retval(type) \ |
b26c74e1 | 96 | __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs, \ |
8ab83f56 SD |
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 | ||
8ab83f56 SD |
106 | /* Dereference memory access function */ |
107 | struct deref_fetch_param { | |
108 | struct fetch_param orig; | |
109 | long offset; | |
3925f4a5 HL |
110 | fetch_func_t fetch; |
111 | fetch_func_t fetch_size; | |
8ab83f56 SD |
112 | }; |
113 | ||
114 | #define DEFINE_FETCH_deref(type) \ | |
b26c74e1 | 115 | __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs, \ |
8ab83f56 SD |
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; \ | |
3925f4a5 | 123 | dprm->fetch(regs, (void *)addr, dest); \ |
8ab83f56 SD |
124 | } else \ |
125 | *(type *)dest = 0; \ | |
126 | } | |
127 | DEFINE_BASIC_FETCH_FUNCS(deref) | |
128 | DEFINE_FETCH_deref(string) | |
3925f4a5 HL |
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 | } | |
8ab83f56 SD |
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) \ | |
b26c74e1 | 169 | __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs, \ |
8ab83f56 SD |
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 | ||
34fee3a1 NK |
214 | static const struct fetch_type *find_fetch_type(const char *type, |
215 | const struct fetch_type *ftbl) | |
8ab83f56 SD |
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++; | |
bcd83ea6 | 231 | if (kstrtoul(type, 0, &bs)) |
8ab83f56 SD |
232 | goto fail; |
233 | ||
234 | switch (bs) { | |
235 | case 8: | |
34fee3a1 | 236 | return find_fetch_type("u8", ftbl); |
8ab83f56 | 237 | case 16: |
34fee3a1 | 238 | return find_fetch_type("u16", ftbl); |
8ab83f56 | 239 | case 32: |
34fee3a1 | 240 | return find_fetch_type("u32", ftbl); |
8ab83f56 | 241 | case 64: |
34fee3a1 | 242 | return find_fetch_type("u64", ftbl); |
8ab83f56 SD |
243 | default: |
244 | goto fail; | |
245 | } | |
246 | } | |
247 | ||
34fee3a1 NK |
248 | for (i = 0; ftbl[i].name; i++) { |
249 | if (strcmp(type, ftbl[i].name) == 0) | |
250 | return &ftbl[i]; | |
251 | } | |
8ab83f56 SD |
252 | |
253 | fail: | |
254 | return NULL; | |
255 | } | |
256 | ||
257 | /* Special function : only accept unsigned long */ | |
258 | static __kprobes void fetch_stack_address(struct pt_regs *regs, | |
259 | void *dummy, void *dest) | |
260 | { | |
261 | *(unsigned long *)dest = kernel_stack_pointer(regs); | |
262 | } | |
263 | ||
264 | static fetch_func_t get_fetch_size_function(const struct fetch_type *type, | |
34fee3a1 NK |
265 | fetch_func_t orig_fn, |
266 | const struct fetch_type *ftbl) | |
8ab83f56 SD |
267 | { |
268 | int i; | |
269 | ||
34fee3a1 | 270 | if (type != &ftbl[FETCH_TYPE_STRING]) |
8ab83f56 SD |
271 | return NULL; /* Only string type needs size function */ |
272 | ||
273 | for (i = 0; i < FETCH_MTD_END; i++) | |
274 | if (type->fetch[i] == orig_fn) | |
34fee3a1 | 275 | return ftbl[FETCH_TYPE_STRSIZE].fetch[i]; |
8ab83f56 SD |
276 | |
277 | WARN_ON(1); /* This should not happen */ | |
278 | ||
279 | return NULL; | |
280 | } | |
281 | ||
282 | /* Split symbol and offset. */ | |
283 | int traceprobe_split_symbol_offset(char *symbol, unsigned long *offset) | |
284 | { | |
285 | char *tmp; | |
286 | int ret; | |
287 | ||
288 | if (!offset) | |
289 | return -EINVAL; | |
290 | ||
291 | tmp = strchr(symbol, '+'); | |
292 | if (tmp) { | |
bcd83ea6 DW |
293 | /* skip sign because kstrtoul doesn't accept '+' */ |
294 | ret = kstrtoul(tmp + 1, 0, offset); | |
8ab83f56 SD |
295 | if (ret) |
296 | return ret; | |
297 | ||
298 | *tmp = '\0'; | |
299 | } else | |
300 | *offset = 0; | |
301 | ||
302 | return 0; | |
303 | } | |
304 | ||
305 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) | |
306 | ||
307 | static int parse_probe_vars(char *arg, const struct fetch_type *t, | |
308 | struct fetch_param *f, bool is_return) | |
309 | { | |
310 | int ret = 0; | |
311 | unsigned long param; | |
312 | ||
313 | if (strcmp(arg, "retval") == 0) { | |
314 | if (is_return) | |
315 | f->fn = t->fetch[FETCH_MTD_retval]; | |
316 | else | |
317 | ret = -EINVAL; | |
318 | } else if (strncmp(arg, "stack", 5) == 0) { | |
319 | if (arg[5] == '\0') { | |
320 | if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0) | |
321 | f->fn = fetch_stack_address; | |
322 | else | |
323 | ret = -EINVAL; | |
324 | } else if (isdigit(arg[5])) { | |
bcd83ea6 | 325 | ret = kstrtoul(arg + 5, 10, ¶m); |
8ab83f56 SD |
326 | if (ret || param > PARAM_MAX_STACK) |
327 | ret = -EINVAL; | |
328 | else { | |
329 | f->fn = t->fetch[FETCH_MTD_stack]; | |
330 | f->data = (void *)param; | |
331 | } | |
332 | } else | |
333 | ret = -EINVAL; | |
334 | } else | |
335 | ret = -EINVAL; | |
336 | ||
337 | return ret; | |
338 | } | |
339 | ||
340 | /* Recursive argument parser */ | |
341 | static int parse_probe_arg(char *arg, const struct fetch_type *t, | |
f3f096cf | 342 | struct fetch_param *f, bool is_return, bool is_kprobe) |
8ab83f56 | 343 | { |
34fee3a1 | 344 | const struct fetch_type *ftbl; |
8ab83f56 SD |
345 | unsigned long param; |
346 | long offset; | |
347 | char *tmp; | |
34fee3a1 | 348 | int ret = 0; |
8ab83f56 | 349 | |
34fee3a1 NK |
350 | ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table; |
351 | BUG_ON(ftbl == NULL); | |
f3f096cf SD |
352 | |
353 | /* Until uprobe_events supports only reg arguments */ | |
354 | if (!is_kprobe && arg[0] != '%') | |
355 | return -EINVAL; | |
356 | ||
8ab83f56 SD |
357 | switch (arg[0]) { |
358 | case '$': | |
359 | ret = parse_probe_vars(arg + 1, t, f, is_return); | |
360 | break; | |
361 | ||
362 | case '%': /* named register */ | |
363 | ret = regs_query_register_offset(arg + 1); | |
364 | if (ret >= 0) { | |
365 | f->fn = t->fetch[FETCH_MTD_reg]; | |
366 | f->data = (void *)(unsigned long)ret; | |
367 | ret = 0; | |
368 | } | |
369 | break; | |
370 | ||
371 | case '@': /* memory or symbol */ | |
372 | if (isdigit(arg[1])) { | |
bcd83ea6 | 373 | ret = kstrtoul(arg + 1, 0, ¶m); |
8ab83f56 SD |
374 | if (ret) |
375 | break; | |
376 | ||
377 | f->fn = t->fetch[FETCH_MTD_memory]; | |
378 | f->data = (void *)param; | |
379 | } else { | |
380 | ret = traceprobe_split_symbol_offset(arg + 1, &offset); | |
381 | if (ret) | |
382 | break; | |
383 | ||
384 | f->data = alloc_symbol_cache(arg + 1, offset); | |
385 | if (f->data) | |
386 | f->fn = t->fetch[FETCH_MTD_symbol]; | |
387 | } | |
388 | break; | |
389 | ||
390 | case '+': /* deref memory */ | |
bcd83ea6 | 391 | arg++; /* Skip '+', because kstrtol() rejects it. */ |
8ab83f56 SD |
392 | case '-': |
393 | tmp = strchr(arg, '('); | |
394 | if (!tmp) | |
395 | break; | |
396 | ||
397 | *tmp = '\0'; | |
bcd83ea6 | 398 | ret = kstrtol(arg, 0, &offset); |
8ab83f56 SD |
399 | |
400 | if (ret) | |
401 | break; | |
402 | ||
403 | arg = tmp + 1; | |
404 | tmp = strrchr(arg, ')'); | |
405 | ||
406 | if (tmp) { | |
407 | struct deref_fetch_param *dprm; | |
408 | const struct fetch_type *t2; | |
409 | ||
34fee3a1 | 410 | t2 = find_fetch_type(NULL, ftbl); |
8ab83f56 SD |
411 | *tmp = '\0'; |
412 | dprm = kzalloc(sizeof(struct deref_fetch_param), GFP_KERNEL); | |
413 | ||
414 | if (!dprm) | |
415 | return -ENOMEM; | |
416 | ||
417 | dprm->offset = offset; | |
3925f4a5 HL |
418 | dprm->fetch = t->fetch[FETCH_MTD_memory]; |
419 | dprm->fetch_size = get_fetch_size_function(t, | |
420 | dprm->fetch, ftbl); | |
f3f096cf SD |
421 | ret = parse_probe_arg(arg, t2, &dprm->orig, is_return, |
422 | is_kprobe); | |
8ab83f56 SD |
423 | if (ret) |
424 | kfree(dprm); | |
425 | else { | |
426 | f->fn = t->fetch[FETCH_MTD_deref]; | |
427 | f->data = (void *)dprm; | |
428 | } | |
429 | } | |
430 | break; | |
431 | } | |
432 | if (!ret && !f->fn) { /* Parsed, but do not find fetch method */ | |
433 | pr_info("%s type has no corresponding fetch method.\n", t->name); | |
434 | ret = -EINVAL; | |
435 | } | |
436 | ||
437 | return ret; | |
438 | } | |
439 | ||
440 | #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long)) | |
441 | ||
442 | /* Bitfield type needs to be parsed into a fetch function */ | |
443 | static int __parse_bitfield_probe_arg(const char *bf, | |
444 | const struct fetch_type *t, | |
445 | struct fetch_param *f) | |
446 | { | |
447 | struct bitfield_fetch_param *bprm; | |
448 | unsigned long bw, bo; | |
449 | char *tail; | |
450 | ||
451 | if (*bf != 'b') | |
452 | return 0; | |
453 | ||
454 | bprm = kzalloc(sizeof(*bprm), GFP_KERNEL); | |
455 | if (!bprm) | |
456 | return -ENOMEM; | |
457 | ||
458 | bprm->orig = *f; | |
459 | f->fn = t->fetch[FETCH_MTD_bitfield]; | |
460 | f->data = (void *)bprm; | |
461 | bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */ | |
462 | ||
463 | if (bw == 0 || *tail != '@') | |
464 | return -EINVAL; | |
465 | ||
466 | bf = tail + 1; | |
467 | bo = simple_strtoul(bf, &tail, 0); | |
468 | ||
469 | if (tail == bf || *tail != '/') | |
470 | return -EINVAL; | |
471 | ||
472 | bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo); | |
473 | bprm->low_shift = bprm->hi_shift + bo; | |
474 | ||
475 | return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0; | |
476 | } | |
477 | ||
478 | /* String length checking wrapper */ | |
479 | int traceprobe_parse_probe_arg(char *arg, ssize_t *size, | |
f3f096cf | 480 | struct probe_arg *parg, bool is_return, bool is_kprobe) |
8ab83f56 | 481 | { |
34fee3a1 | 482 | const struct fetch_type *ftbl; |
8ab83f56 SD |
483 | const char *t; |
484 | int ret; | |
485 | ||
34fee3a1 NK |
486 | ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table; |
487 | BUG_ON(ftbl == NULL); | |
488 | ||
8ab83f56 SD |
489 | if (strlen(arg) > MAX_ARGSTR_LEN) { |
490 | pr_info("Argument is too long.: %s\n", arg); | |
491 | return -ENOSPC; | |
492 | } | |
493 | parg->comm = kstrdup(arg, GFP_KERNEL); | |
494 | if (!parg->comm) { | |
495 | pr_info("Failed to allocate memory for command '%s'.\n", arg); | |
496 | return -ENOMEM; | |
497 | } | |
498 | t = strchr(parg->comm, ':'); | |
499 | if (t) { | |
500 | arg[t - parg->comm] = '\0'; | |
501 | t++; | |
502 | } | |
34fee3a1 | 503 | parg->type = find_fetch_type(t, ftbl); |
8ab83f56 SD |
504 | if (!parg->type) { |
505 | pr_info("Unsupported type: %s\n", t); | |
506 | return -EINVAL; | |
507 | } | |
508 | parg->offset = *size; | |
509 | *size += parg->type->size; | |
f3f096cf | 510 | ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return, is_kprobe); |
8ab83f56 SD |
511 | |
512 | if (ret >= 0 && t != NULL) | |
513 | ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch); | |
514 | ||
515 | if (ret >= 0) { | |
516 | parg->fetch_size.fn = get_fetch_size_function(parg->type, | |
34fee3a1 NK |
517 | parg->fetch.fn, |
518 | ftbl); | |
8ab83f56 SD |
519 | parg->fetch_size.data = parg->fetch.data; |
520 | } | |
521 | ||
522 | return ret; | |
523 | } | |
524 | ||
525 | /* Return 1 if name is reserved or already used by another argument */ | |
526 | int traceprobe_conflict_field_name(const char *name, | |
527 | struct probe_arg *args, int narg) | |
528 | { | |
529 | int i; | |
530 | ||
531 | for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) | |
532 | if (strcmp(reserved_field_names[i], name) == 0) | |
533 | return 1; | |
534 | ||
535 | for (i = 0; i < narg; i++) | |
536 | if (strcmp(args[i].name, name) == 0) | |
537 | return 1; | |
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
542 | void traceprobe_update_arg(struct probe_arg *arg) | |
543 | { | |
544 | if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn)) | |
545 | update_bitfield_fetch_param(arg->fetch.data); | |
546 | else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn)) | |
547 | update_deref_fetch_param(arg->fetch.data); | |
548 | else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn)) | |
549 | update_symbol_cache(arg->fetch.data); | |
550 | } | |
551 | ||
552 | void traceprobe_free_probe_arg(struct probe_arg *arg) | |
553 | { | |
554 | if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn)) | |
555 | free_bitfield_fetch_param(arg->fetch.data); | |
556 | else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn)) | |
557 | free_deref_fetch_param(arg->fetch.data); | |
558 | else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn)) | |
559 | free_symbol_cache(arg->fetch.data); | |
560 | ||
561 | kfree(arg->name); | |
562 | kfree(arg->comm); | |
563 | } | |
564 | ||
565 | int traceprobe_command(const char *buf, int (*createfn)(int, char **)) | |
566 | { | |
567 | char **argv; | |
568 | int argc, ret; | |
569 | ||
570 | argc = 0; | |
571 | ret = 0; | |
572 | argv = argv_split(GFP_KERNEL, buf, &argc); | |
573 | if (!argv) | |
574 | return -ENOMEM; | |
575 | ||
576 | if (argc) | |
577 | ret = createfn(argc, argv); | |
578 | ||
579 | argv_free(argv); | |
580 | ||
581 | return ret; | |
582 | } | |
583 | ||
584 | #define WRITE_BUFSIZE 4096 | |
585 | ||
586 | ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer, | |
587 | size_t count, loff_t *ppos, | |
588 | int (*createfn)(int, char **)) | |
589 | { | |
590 | char *kbuf, *tmp; | |
591 | int ret = 0; | |
592 | size_t done = 0; | |
593 | size_t size; | |
594 | ||
595 | kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL); | |
596 | if (!kbuf) | |
597 | return -ENOMEM; | |
598 | ||
599 | while (done < count) { | |
600 | size = count - done; | |
601 | ||
602 | if (size >= WRITE_BUFSIZE) | |
603 | size = WRITE_BUFSIZE - 1; | |
604 | ||
605 | if (copy_from_user(kbuf, buffer + done, size)) { | |
606 | ret = -EFAULT; | |
607 | goto out; | |
608 | } | |
609 | kbuf[size] = '\0'; | |
610 | tmp = strchr(kbuf, '\n'); | |
611 | ||
612 | if (tmp) { | |
613 | *tmp = '\0'; | |
614 | size = tmp - kbuf + 1; | |
615 | } else if (done + size < count) { | |
616 | pr_warning("Line length is too long: " | |
617 | "Should be less than %d.", WRITE_BUFSIZE); | |
618 | ret = -EINVAL; | |
619 | goto out; | |
620 | } | |
621 | done += size; | |
622 | /* Remove comments */ | |
623 | tmp = strchr(kbuf, '#'); | |
624 | ||
625 | if (tmp) | |
626 | *tmp = '\0'; | |
627 | ||
628 | ret = traceprobe_command(kbuf, createfn); | |
629 | if (ret) | |
630 | goto out; | |
631 | } | |
632 | ret = done; | |
633 | ||
634 | out: | |
635 | kfree(kbuf); | |
636 | ||
637 | return ret; | |
638 | } | |
5bf652aa NK |
639 | |
640 | static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, | |
641 | bool is_return) | |
642 | { | |
643 | int i; | |
644 | int pos = 0; | |
645 | ||
646 | const char *fmt, *arg; | |
647 | ||
648 | if (!is_return) { | |
649 | fmt = "(%lx)"; | |
650 | arg = "REC->" FIELD_STRING_IP; | |
651 | } else { | |
652 | fmt = "(%lx <- %lx)"; | |
653 | arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; | |
654 | } | |
655 | ||
656 | /* When len=0, we just calculate the needed length */ | |
657 | #define LEN_OR_ZERO (len ? len - pos : 0) | |
658 | ||
659 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); | |
660 | ||
661 | for (i = 0; i < tp->nr_args; i++) { | |
662 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s", | |
663 | tp->args[i].name, tp->args[i].type->fmt); | |
664 | } | |
665 | ||
666 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); | |
667 | ||
668 | for (i = 0; i < tp->nr_args; i++) { | |
669 | if (strcmp(tp->args[i].type->name, "string") == 0) | |
670 | pos += snprintf(buf + pos, LEN_OR_ZERO, | |
671 | ", __get_str(%s)", | |
672 | tp->args[i].name); | |
673 | else | |
674 | pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s", | |
675 | tp->args[i].name); | |
676 | } | |
677 | ||
678 | #undef LEN_OR_ZERO | |
679 | ||
680 | /* return the length of print_fmt */ | |
681 | return pos; | |
682 | } | |
683 | ||
684 | int set_print_fmt(struct trace_probe *tp, bool is_return) | |
685 | { | |
686 | int len; | |
687 | char *print_fmt; | |
688 | ||
689 | /* First: called with 0 length to calculate the needed length */ | |
690 | len = __set_print_fmt(tp, NULL, 0, is_return); | |
691 | print_fmt = kmalloc(len + 1, GFP_KERNEL); | |
692 | if (!print_fmt) | |
693 | return -ENOMEM; | |
694 | ||
695 | /* Second: actually write the @print_fmt */ | |
696 | __set_print_fmt(tp, print_fmt, len + 1, is_return); | |
697 | tp->call.print_fmt = print_fmt; | |
698 | ||
699 | return 0; | |
700 | } |