]> Pileus Git - ~andy/linux/blob - kernel/trace/trace_probe.c
tracing/probes: Implement 'stack' fetch method for uprobes
[~andy/linux] / kernel / trace / trace_probe.c
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
38 /* Printing  in basic type function template */
39 #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt)                         \
40 __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s,   \
41                                                 const char *name,       \
42                                                 void *data, void *ent)  \
43 {                                                                       \
44         return trace_seq_printf(s, " %s=" fmt, name, *(type *)data);    \
45 }                                                                       \
46 const char PRINT_TYPE_FMT_NAME(type)[] = fmt;
47
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")
56
57 /* Print type function for string type */
58 __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s,
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
71 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
72
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)                                          \
84 __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs,         \
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
95 #define DEFINE_FETCH_retval(type)                                       \
96 __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,      \
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
106 #define DEFINE_FETCH_memory(type)                                       \
107 __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,      \
108                                           void *addr, void *dest)       \
109 {                                                                       \
110         type retval;                                                    \
111         if (probe_kernel_address(addr, retval))                         \
112                 *(type *)dest = 0;                                      \
113         else                                                            \
114                 *(type *)dest = retval;                                 \
115 }
116 DEFINE_BASIC_FETCH_FUNCS(memory)
117 /*
118  * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
119  * length and relative data location.
120  */
121 __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
122                                                       void *addr, void *dest)
123 {
124         long ret;
125         int maxlen = get_rloc_len(*(u32 *)dest);
126         u8 *dst = get_rloc_data(dest);
127         u8 *src = addr;
128         mm_segment_t old_fs = get_fs();
129
130         if (!maxlen)
131                 return;
132
133         /*
134          * Try to get string again, since the string can be changed while
135          * probing.
136          */
137         set_fs(KERNEL_DS);
138         pagefault_disable();
139
140         do
141                 ret = __copy_from_user_inatomic(dst++, src++, 1);
142         while (dst[-1] && ret == 0 && src - (u8 *)addr < maxlen);
143
144         dst[-1] = '\0';
145         pagefault_enable();
146         set_fs(old_fs);
147
148         if (ret < 0) {  /* Failed to fetch string */
149                 ((u8 *)get_rloc_data(dest))[0] = '\0';
150                 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
151         } else {
152                 *(u32 *)dest = make_data_rloc(src - (u8 *)addr,
153                                               get_rloc_offs(*(u32 *)dest));
154         }
155 }
156
157 /* Return the length of string -- including null terminal byte */
158 __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
159                                                         void *addr, void *dest)
160 {
161         mm_segment_t old_fs;
162         int ret, len = 0;
163         u8 c;
164
165         old_fs = get_fs();
166         set_fs(KERNEL_DS);
167         pagefault_disable();
168
169         do {
170                 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
171                 len++;
172         } while (c && ret == 0 && len < MAX_STRING_SIZE);
173
174         pagefault_enable();
175         set_fs(old_fs);
176
177         if (ret < 0)    /* Failed to check the length */
178                 *(u32 *)dest = 0;
179         else
180                 *(u32 *)dest = len;
181 }
182
183 /* Memory fetching by symbol */
184 struct symbol_cache {
185         char            *symbol;
186         long            offset;
187         unsigned long   addr;
188 };
189
190 static unsigned long update_symbol_cache(struct symbol_cache *sc)
191 {
192         sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
193
194         if (sc->addr)
195                 sc->addr += sc->offset;
196
197         return sc->addr;
198 }
199
200 static void free_symbol_cache(struct symbol_cache *sc)
201 {
202         kfree(sc->symbol);
203         kfree(sc);
204 }
205
206 static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
207 {
208         struct symbol_cache *sc;
209
210         if (!sym || strlen(sym) == 0)
211                 return NULL;
212
213         sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
214         if (!sc)
215                 return NULL;
216
217         sc->symbol = kstrdup(sym, GFP_KERNEL);
218         if (!sc->symbol) {
219                 kfree(sc);
220                 return NULL;
221         }
222         sc->offset = offset;
223         update_symbol_cache(sc);
224
225         return sc;
226 }
227
228 #define DEFINE_FETCH_symbol(type)                                       \
229 __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,      \
230                                           void *data, void *dest)       \
231 {                                                                       \
232         struct symbol_cache *sc = data;                                 \
233         if (sc->addr)                                                   \
234                 fetch_memory_##type(regs, (void *)sc->addr, dest);      \
235         else                                                            \
236                 *(type *)dest = 0;                                      \
237 }
238 DEFINE_BASIC_FETCH_FUNCS(symbol)
239 DEFINE_FETCH_symbol(string)
240 DEFINE_FETCH_symbol(string_size)
241
242 /* Dereference memory access function */
243 struct deref_fetch_param {
244         struct fetch_param      orig;
245         long                    offset;
246 };
247
248 #define DEFINE_FETCH_deref(type)                                        \
249 __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,       \
250                                             void *data, void *dest)     \
251 {                                                                       \
252         struct deref_fetch_param *dprm = data;                          \
253         unsigned long addr;                                             \
254         call_fetch(&dprm->orig, regs, &addr);                           \
255         if (addr) {                                                     \
256                 addr += dprm->offset;                                   \
257                 fetch_memory_##type(regs, (void *)addr, dest);          \
258         } else                                                          \
259                 *(type *)dest = 0;                                      \
260 }
261 DEFINE_BASIC_FETCH_FUNCS(deref)
262 DEFINE_FETCH_deref(string)
263 DEFINE_FETCH_deref(string_size)
264
265 static __kprobes void update_deref_fetch_param(struct deref_fetch_param *data)
266 {
267         if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
268                 update_deref_fetch_param(data->orig.data);
269         else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
270                 update_symbol_cache(data->orig.data);
271 }
272
273 static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
274 {
275         if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
276                 free_deref_fetch_param(data->orig.data);
277         else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
278                 free_symbol_cache(data->orig.data);
279         kfree(data);
280 }
281
282 /* Bitfield fetch function */
283 struct bitfield_fetch_param {
284         struct fetch_param      orig;
285         unsigned char           hi_shift;
286         unsigned char           low_shift;
287 };
288
289 #define DEFINE_FETCH_bitfield(type)                                     \
290 __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs,    \
291                                             void *data, void *dest)     \
292 {                                                                       \
293         struct bitfield_fetch_param *bprm = data;                       \
294         type buf = 0;                                                   \
295         call_fetch(&bprm->orig, regs, &buf);                            \
296         if (buf) {                                                      \
297                 buf <<= bprm->hi_shift;                                 \
298                 buf >>= bprm->low_shift;                                \
299         }                                                               \
300         *(type *)dest = buf;                                            \
301 }
302
303 DEFINE_BASIC_FETCH_FUNCS(bitfield)
304 #define fetch_bitfield_string           NULL
305 #define fetch_bitfield_string_size      NULL
306
307 static __kprobes void
308 update_bitfield_fetch_param(struct bitfield_fetch_param *data)
309 {
310         /*
311          * Don't check the bitfield itself, because this must be the
312          * last fetch function.
313          */
314         if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
315                 update_deref_fetch_param(data->orig.data);
316         else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
317                 update_symbol_cache(data->orig.data);
318 }
319
320 static __kprobes void
321 free_bitfield_fetch_param(struct bitfield_fetch_param *data)
322 {
323         /*
324          * Don't check the bitfield itself, because this must be the
325          * last fetch function.
326          */
327         if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
328                 free_deref_fetch_param(data->orig.data);
329         else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
330                 free_symbol_cache(data->orig.data);
331
332         kfree(data);
333 }
334
335 static const struct fetch_type *find_fetch_type(const char *type,
336                                                 const struct fetch_type *ftbl)
337 {
338         int i;
339
340         if (!type)
341                 type = DEFAULT_FETCH_TYPE_STR;
342
343         /* Special case: bitfield */
344         if (*type == 'b') {
345                 unsigned long bs;
346
347                 type = strchr(type, '/');
348                 if (!type)
349                         goto fail;
350
351                 type++;
352                 if (kstrtoul(type, 0, &bs))
353                         goto fail;
354
355                 switch (bs) {
356                 case 8:
357                         return find_fetch_type("u8", ftbl);
358                 case 16:
359                         return find_fetch_type("u16", ftbl);
360                 case 32:
361                         return find_fetch_type("u32", ftbl);
362                 case 64:
363                         return find_fetch_type("u64", ftbl);
364                 default:
365                         goto fail;
366                 }
367         }
368
369         for (i = 0; ftbl[i].name; i++) {
370                 if (strcmp(type, ftbl[i].name) == 0)
371                         return &ftbl[i];
372         }
373
374 fail:
375         return NULL;
376 }
377
378 /* Special function : only accept unsigned long */
379 static __kprobes void fetch_stack_address(struct pt_regs *regs,
380                                         void *dummy, void *dest)
381 {
382         *(unsigned long *)dest = kernel_stack_pointer(regs);
383 }
384
385 static fetch_func_t get_fetch_size_function(const struct fetch_type *type,
386                                             fetch_func_t orig_fn,
387                                             const struct fetch_type *ftbl)
388 {
389         int i;
390
391         if (type != &ftbl[FETCH_TYPE_STRING])
392                 return NULL;    /* Only string type needs size function */
393
394         for (i = 0; i < FETCH_MTD_END; i++)
395                 if (type->fetch[i] == orig_fn)
396                         return ftbl[FETCH_TYPE_STRSIZE].fetch[i];
397
398         WARN_ON(1);     /* This should not happen */
399
400         return NULL;
401 }
402
403 /* Split symbol and offset. */
404 int traceprobe_split_symbol_offset(char *symbol, unsigned long *offset)
405 {
406         char *tmp;
407         int ret;
408
409         if (!offset)
410                 return -EINVAL;
411
412         tmp = strchr(symbol, '+');
413         if (tmp) {
414                 /* skip sign because kstrtoul doesn't accept '+' */
415                 ret = kstrtoul(tmp + 1, 0, offset);
416                 if (ret)
417                         return ret;
418
419                 *tmp = '\0';
420         } else
421                 *offset = 0;
422
423         return 0;
424 }
425
426 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
427
428 static int parse_probe_vars(char *arg, const struct fetch_type *t,
429                             struct fetch_param *f, bool is_return)
430 {
431         int ret = 0;
432         unsigned long param;
433
434         if (strcmp(arg, "retval") == 0) {
435                 if (is_return)
436                         f->fn = t->fetch[FETCH_MTD_retval];
437                 else
438                         ret = -EINVAL;
439         } else if (strncmp(arg, "stack", 5) == 0) {
440                 if (arg[5] == '\0') {
441                         if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
442                                 f->fn = fetch_stack_address;
443                         else
444                                 ret = -EINVAL;
445                 } else if (isdigit(arg[5])) {
446                         ret = kstrtoul(arg + 5, 10, &param);
447                         if (ret || param > PARAM_MAX_STACK)
448                                 ret = -EINVAL;
449                         else {
450                                 f->fn = t->fetch[FETCH_MTD_stack];
451                                 f->data = (void *)param;
452                         }
453                 } else
454                         ret = -EINVAL;
455         } else
456                 ret = -EINVAL;
457
458         return ret;
459 }
460
461 /* Recursive argument parser */
462 static int parse_probe_arg(char *arg, const struct fetch_type *t,
463                      struct fetch_param *f, bool is_return, bool is_kprobe)
464 {
465         const struct fetch_type *ftbl;
466         unsigned long param;
467         long offset;
468         char *tmp;
469         int ret = 0;
470
471         ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table;
472         BUG_ON(ftbl == NULL);
473
474         /* Until uprobe_events supports only reg arguments */
475         if (!is_kprobe && arg[0] != '%')
476                 return -EINVAL;
477
478         switch (arg[0]) {
479         case '$':
480                 ret = parse_probe_vars(arg + 1, t, f, is_return);
481                 break;
482
483         case '%':       /* named register */
484                 ret = regs_query_register_offset(arg + 1);
485                 if (ret >= 0) {
486                         f->fn = t->fetch[FETCH_MTD_reg];
487                         f->data = (void *)(unsigned long)ret;
488                         ret = 0;
489                 }
490                 break;
491
492         case '@':       /* memory or symbol */
493                 if (isdigit(arg[1])) {
494                         ret = kstrtoul(arg + 1, 0, &param);
495                         if (ret)
496                                 break;
497
498                         f->fn = t->fetch[FETCH_MTD_memory];
499                         f->data = (void *)param;
500                 } else {
501                         ret = traceprobe_split_symbol_offset(arg + 1, &offset);
502                         if (ret)
503                                 break;
504
505                         f->data = alloc_symbol_cache(arg + 1, offset);
506                         if (f->data)
507                                 f->fn = t->fetch[FETCH_MTD_symbol];
508                 }
509                 break;
510
511         case '+':       /* deref memory */
512                 arg++;  /* Skip '+', because kstrtol() rejects it. */
513         case '-':
514                 tmp = strchr(arg, '(');
515                 if (!tmp)
516                         break;
517
518                 *tmp = '\0';
519                 ret = kstrtol(arg, 0, &offset);
520
521                 if (ret)
522                         break;
523
524                 arg = tmp + 1;
525                 tmp = strrchr(arg, ')');
526
527                 if (tmp) {
528                         struct deref_fetch_param        *dprm;
529                         const struct fetch_type         *t2;
530
531                         t2 = find_fetch_type(NULL, ftbl);
532                         *tmp = '\0';
533                         dprm = kzalloc(sizeof(struct deref_fetch_param), GFP_KERNEL);
534
535                         if (!dprm)
536                                 return -ENOMEM;
537
538                         dprm->offset = offset;
539                         ret = parse_probe_arg(arg, t2, &dprm->orig, is_return,
540                                                         is_kprobe);
541                         if (ret)
542                                 kfree(dprm);
543                         else {
544                                 f->fn = t->fetch[FETCH_MTD_deref];
545                                 f->data = (void *)dprm;
546                         }
547                 }
548                 break;
549         }
550         if (!ret && !f->fn) {   /* Parsed, but do not find fetch method */
551                 pr_info("%s type has no corresponding fetch method.\n", t->name);
552                 ret = -EINVAL;
553         }
554
555         return ret;
556 }
557
558 #define BYTES_TO_BITS(nb)       ((BITS_PER_LONG * (nb)) / sizeof(long))
559
560 /* Bitfield type needs to be parsed into a fetch function */
561 static int __parse_bitfield_probe_arg(const char *bf,
562                                       const struct fetch_type *t,
563                                       struct fetch_param *f)
564 {
565         struct bitfield_fetch_param *bprm;
566         unsigned long bw, bo;
567         char *tail;
568
569         if (*bf != 'b')
570                 return 0;
571
572         bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
573         if (!bprm)
574                 return -ENOMEM;
575
576         bprm->orig = *f;
577         f->fn = t->fetch[FETCH_MTD_bitfield];
578         f->data = (void *)bprm;
579         bw = simple_strtoul(bf + 1, &tail, 0);  /* Use simple one */
580
581         if (bw == 0 || *tail != '@')
582                 return -EINVAL;
583
584         bf = tail + 1;
585         bo = simple_strtoul(bf, &tail, 0);
586
587         if (tail == bf || *tail != '/')
588                 return -EINVAL;
589
590         bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
591         bprm->low_shift = bprm->hi_shift + bo;
592
593         return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
594 }
595
596 /* String length checking wrapper */
597 int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
598                 struct probe_arg *parg, bool is_return, bool is_kprobe)
599 {
600         const struct fetch_type *ftbl;
601         const char *t;
602         int ret;
603
604         ftbl = is_kprobe ? kprobes_fetch_type_table : uprobes_fetch_type_table;
605         BUG_ON(ftbl == NULL);
606
607         if (strlen(arg) > MAX_ARGSTR_LEN) {
608                 pr_info("Argument is too long.: %s\n",  arg);
609                 return -ENOSPC;
610         }
611         parg->comm = kstrdup(arg, GFP_KERNEL);
612         if (!parg->comm) {
613                 pr_info("Failed to allocate memory for command '%s'.\n", arg);
614                 return -ENOMEM;
615         }
616         t = strchr(parg->comm, ':');
617         if (t) {
618                 arg[t - parg->comm] = '\0';
619                 t++;
620         }
621         parg->type = find_fetch_type(t, ftbl);
622         if (!parg->type) {
623                 pr_info("Unsupported type: %s\n", t);
624                 return -EINVAL;
625         }
626         parg->offset = *size;
627         *size += parg->type->size;
628         ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return, is_kprobe);
629
630         if (ret >= 0 && t != NULL)
631                 ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
632
633         if (ret >= 0) {
634                 parg->fetch_size.fn = get_fetch_size_function(parg->type,
635                                                               parg->fetch.fn,
636                                                               ftbl);
637                 parg->fetch_size.data = parg->fetch.data;
638         }
639
640         return ret;
641 }
642
643 /* Return 1 if name is reserved or already used by another argument */
644 int traceprobe_conflict_field_name(const char *name,
645                                struct probe_arg *args, int narg)
646 {
647         int i;
648
649         for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
650                 if (strcmp(reserved_field_names[i], name) == 0)
651                         return 1;
652
653         for (i = 0; i < narg; i++)
654                 if (strcmp(args[i].name, name) == 0)
655                         return 1;
656
657         return 0;
658 }
659
660 void traceprobe_update_arg(struct probe_arg *arg)
661 {
662         if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
663                 update_bitfield_fetch_param(arg->fetch.data);
664         else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
665                 update_deref_fetch_param(arg->fetch.data);
666         else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
667                 update_symbol_cache(arg->fetch.data);
668 }
669
670 void traceprobe_free_probe_arg(struct probe_arg *arg)
671 {
672         if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
673                 free_bitfield_fetch_param(arg->fetch.data);
674         else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
675                 free_deref_fetch_param(arg->fetch.data);
676         else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
677                 free_symbol_cache(arg->fetch.data);
678
679         kfree(arg->name);
680         kfree(arg->comm);
681 }
682
683 int traceprobe_command(const char *buf, int (*createfn)(int, char **))
684 {
685         char **argv;
686         int argc, ret;
687
688         argc = 0;
689         ret = 0;
690         argv = argv_split(GFP_KERNEL, buf, &argc);
691         if (!argv)
692                 return -ENOMEM;
693
694         if (argc)
695                 ret = createfn(argc, argv);
696
697         argv_free(argv);
698
699         return ret;
700 }
701
702 #define WRITE_BUFSIZE  4096
703
704 ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,
705                                 size_t count, loff_t *ppos,
706                                 int (*createfn)(int, char **))
707 {
708         char *kbuf, *tmp;
709         int ret = 0;
710         size_t done = 0;
711         size_t size;
712
713         kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
714         if (!kbuf)
715                 return -ENOMEM;
716
717         while (done < count) {
718                 size = count - done;
719
720                 if (size >= WRITE_BUFSIZE)
721                         size = WRITE_BUFSIZE - 1;
722
723                 if (copy_from_user(kbuf, buffer + done, size)) {
724                         ret = -EFAULT;
725                         goto out;
726                 }
727                 kbuf[size] = '\0';
728                 tmp = strchr(kbuf, '\n');
729
730                 if (tmp) {
731                         *tmp = '\0';
732                         size = tmp - kbuf + 1;
733                 } else if (done + size < count) {
734                         pr_warning("Line length is too long: "
735                                    "Should be less than %d.", WRITE_BUFSIZE);
736                         ret = -EINVAL;
737                         goto out;
738                 }
739                 done += size;
740                 /* Remove comments */
741                 tmp = strchr(kbuf, '#');
742
743                 if (tmp)
744                         *tmp = '\0';
745
746                 ret = traceprobe_command(kbuf, createfn);
747                 if (ret)
748                         goto out;
749         }
750         ret = done;
751
752 out:
753         kfree(kbuf);
754
755         return ret;
756 }
757
758 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
759                            bool is_return)
760 {
761         int i;
762         int pos = 0;
763
764         const char *fmt, *arg;
765
766         if (!is_return) {
767                 fmt = "(%lx)";
768                 arg = "REC->" FIELD_STRING_IP;
769         } else {
770                 fmt = "(%lx <- %lx)";
771                 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
772         }
773
774         /* When len=0, we just calculate the needed length */
775 #define LEN_OR_ZERO (len ? len - pos : 0)
776
777         pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
778
779         for (i = 0; i < tp->nr_args; i++) {
780                 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
781                                 tp->args[i].name, tp->args[i].type->fmt);
782         }
783
784         pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
785
786         for (i = 0; i < tp->nr_args; i++) {
787                 if (strcmp(tp->args[i].type->name, "string") == 0)
788                         pos += snprintf(buf + pos, LEN_OR_ZERO,
789                                         ", __get_str(%s)",
790                                         tp->args[i].name);
791                 else
792                         pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
793                                         tp->args[i].name);
794         }
795
796 #undef LEN_OR_ZERO
797
798         /* return the length of print_fmt */
799         return pos;
800 }
801
802 int set_print_fmt(struct trace_probe *tp, bool is_return)
803 {
804         int len;
805         char *print_fmt;
806
807         /* First: called with 0 length to calculate the needed length */
808         len = __set_print_fmt(tp, NULL, 0, is_return);
809         print_fmt = kmalloc(len + 1, GFP_KERNEL);
810         if (!print_fmt)
811                 return -ENOMEM;
812
813         /* Second: actually write the @print_fmt */
814         __set_print_fmt(tp, print_fmt, len + 1, is_return);
815         tp->call.print_fmt = print_fmt;
816
817         return 0;
818 }