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