Skip to content

Commit 7e465ba

Browse files
tzanussirostedt
authored andcommitted
tracing: Make traceprobe parsing code reusable
traceprobe_probes_write() and traceprobe_command() actually contain nothing that ties them to kprobes - the code is generically useful for similar types of parsing elsewhere, so separate it out and move it to trace.c/trace.h. Other than moving it, the only change is in naming: traceprobe_probes_write() becomes trace_parse_run_command() and traceprobe_command() becomes trace_run_command(). Link: http://lkml.kernel.org/r/ae5c26ea40c196a8986854d921eb6e713ede7e3f.1506105045.git.tom.zanussi@linux.intel.com Signed-off-by: Tom Zanussi <tom.zanussi@linux.intel.com> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent 4f36c2d commit 7e465ba

6 files changed

Lines changed: 103 additions & 103 deletions

File tree

‎kernel/trace/trace.c‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8281,6 +8281,92 @@ void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
82818281
}
82828282
EXPORT_SYMBOL_GPL(ftrace_dump);
82838283

8284+
int trace_run_command(const char *buf, int (*createfn)(int, char **))
8285+
{
8286+
char **argv;
8287+
int argc, ret;
8288+
8289+
argc = 0;
8290+
ret = 0;
8291+
argv = argv_split(GFP_KERNEL, buf, &argc);
8292+
if (!argv)
8293+
return -ENOMEM;
8294+
8295+
if (argc)
8296+
ret = createfn(argc, argv);
8297+
8298+
argv_free(argv);
8299+
8300+
return ret;
8301+
}
8302+
8303+
#define WRITE_BUFSIZE 4096
8304+
8305+
ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
8306+
size_t count, loff_t *ppos,
8307+
int (*createfn)(int, char **))
8308+
{
8309+
char *kbuf, *buf, *tmp;
8310+
int ret = 0;
8311+
size_t done = 0;
8312+
size_t size;
8313+
8314+
kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
8315+
if (!kbuf)
8316+
return -ENOMEM;
8317+
8318+
while (done < count) {
8319+
size = count - done;
8320+
8321+
if (size >= WRITE_BUFSIZE)
8322+
size = WRITE_BUFSIZE - 1;
8323+
8324+
if (copy_from_user(kbuf, buffer + done, size)) {
8325+
ret = -EFAULT;
8326+
goto out;
8327+
}
8328+
kbuf[size] = '\0';
8329+
buf = kbuf;
8330+
do {
8331+
tmp = strchr(buf, '\n');
8332+
if (tmp) {
8333+
*tmp = '\0';
8334+
size = tmp - buf + 1;
8335+
} else {
8336+
size = strlen(buf);
8337+
if (done + size < count) {
8338+
if (buf != kbuf)
8339+
break;
8340+
/* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
8341+
pr_warn("Line length is too long: Should be less than %d\n",
8342+
WRITE_BUFSIZE - 2);
8343+
ret = -EINVAL;
8344+
goto out;
8345+
}
8346+
}
8347+
done += size;
8348+
8349+
/* Remove comments */
8350+
tmp = strchr(buf, '#');
8351+
8352+
if (tmp)
8353+
*tmp = '\0';
8354+
8355+
ret = trace_run_command(buf, createfn);
8356+
if (ret)
8357+
goto out;
8358+
buf += size;
8359+
8360+
} while (done < count);
8361+
}
8362+
ret = done;
8363+
8364+
out:
8365+
kfree(kbuf);
8366+
8367+
return ret;
8368+
}
8369+
82848370
__init static int tracer_alloc_buffers(void)
82858371
{
82868372
int ring_buf_size;

‎kernel/trace/trace.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,13 @@ void trace_printk_start_comm(void);
17521752
int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set);
17531753
int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled);
17541754

1755+
#define MAX_EVENT_NAME_LEN 64
1756+
1757+
extern int trace_run_command(const char *buf, int (*createfn)(int, char**));
1758+
extern ssize_t trace_parse_run_command(struct file *file,
1759+
const char __user *buffer, size_t count, loff_t *ppos,
1760+
int (*createfn)(int, char**));
1761+
17551762
/*
17561763
* Normal trace_printk() and friends allocates special buffers
17571764
* to do the manipulation, as well as saves the print formats

‎kernel/trace/trace_kprobe.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -907,8 +907,8 @@ static int probes_open(struct inode *inode, struct file *file)
907907
static ssize_t probes_write(struct file *file, const char __user *buffer,
908908
size_t count, loff_t *ppos)
909909
{
910-
return traceprobe_probes_write(file, buffer, count, ppos,
911-
create_trace_kprobe);
910+
return trace_parse_run_command(file, buffer, count, ppos,
911+
create_trace_kprobe);
912912
}
913913

914914
static const struct file_operations kprobe_events_ops = {
@@ -1433,9 +1433,9 @@ static __init int kprobe_trace_self_tests_init(void)
14331433

14341434
pr_info("Testing kprobe tracing: ");
14351435

1436-
ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
1437-
"$stack $stack0 +0($stack)",
1438-
create_trace_kprobe);
1436+
ret = trace_run_command("p:testprobe kprobe_trace_selftest_target "
1437+
"$stack $stack0 +0($stack)",
1438+
create_trace_kprobe);
14391439
if (WARN_ON_ONCE(ret)) {
14401440
pr_warn("error on probing function entry.\n");
14411441
warn++;
@@ -1455,8 +1455,8 @@ static __init int kprobe_trace_self_tests_init(void)
14551455
}
14561456
}
14571457

1458-
ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
1459-
"$retval", create_trace_kprobe);
1458+
ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target "
1459+
"$retval", create_trace_kprobe);
14601460
if (WARN_ON_ONCE(ret)) {
14611461
pr_warn("error on probing function return.\n");
14621462
warn++;
@@ -1526,13 +1526,13 @@ static __init int kprobe_trace_self_tests_init(void)
15261526
disable_trace_kprobe(tk, file);
15271527
}
15281528

1529-
ret = traceprobe_command("-:testprobe", create_trace_kprobe);
1529+
ret = trace_run_command("-:testprobe", create_trace_kprobe);
15301530
if (WARN_ON_ONCE(ret)) {
15311531
pr_warn("error on deleting a probe.\n");
15321532
warn++;
15331533
}
15341534

1535-
ret = traceprobe_command("-:testprobe2", create_trace_kprobe);
1535+
ret = trace_run_command("-:testprobe2", create_trace_kprobe);
15361536
if (WARN_ON_ONCE(ret)) {
15371537
pr_warn("error on deleting a probe.\n");
15381538
warn++;

‎kernel/trace/trace_probe.c‎

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -623,92 +623,6 @@ void traceprobe_free_probe_arg(struct probe_arg *arg)
623623
kfree(arg->comm);
624624
}
625625

626-
int traceprobe_command(const char *buf, int (*createfn)(int, char **))
627-
{
628-
char **argv;
629-
int argc, ret;
630-
631-
argc = 0;
632-
ret = 0;
633-
argv = argv_split(GFP_KERNEL, buf, &argc);
634-
if (!argv)
635-
return -ENOMEM;
636-
637-
if (argc)
638-
ret = createfn(argc, argv);
639-
640-
argv_free(argv);
641-
642-
return ret;
643-
}
644-
645-
#define WRITE_BUFSIZE 4096
646-
647-
ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,
648-
size_t count, loff_t *ppos,
649-
int (*createfn)(int, char **))
650-
{
651-
char *kbuf, *buf, *tmp;
652-
int ret = 0;
653-
size_t done = 0;
654-
size_t size;
655-
656-
kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
657-
if (!kbuf)
658-
return -ENOMEM;
659-
660-
while (done < count) {
661-
size = count - done;
662-
663-
if (size >= WRITE_BUFSIZE)
664-
size = WRITE_BUFSIZE - 1;
665-
666-
if (copy_from_user(kbuf, buffer + done, size)) {
667-
ret = -EFAULT;
668-
goto out;
669-
}
670-
kbuf[size] = '\0';
671-
buf = kbuf;
672-
do {
673-
tmp = strchr(buf, '\n');
674-
if (tmp) {
675-
*tmp = '\0';
676-
size = tmp - buf + 1;
677-
} else {
678-
size = strlen(buf);
679-
if (done + size < count) {
680-
if (buf != kbuf)
681-
break;
682-
/* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
683-
pr_warn("Line length is too long: Should be less than %d\n",
684-
WRITE_BUFSIZE - 2);
685-
ret = -EINVAL;
686-
goto out;
687-
}
688-
}
689-
done += size;
690-
691-
/* Remove comments */
692-
tmp = strchr(buf, '#');
693-
694-
if (tmp)
695-
*tmp = '\0';
696-
697-
ret = traceprobe_command(buf, createfn);
698-
if (ret)
699-
goto out;
700-
buf += size;
701-
702-
} while (done < count);
703-
}
704-
ret = done;
705-
706-
out:
707-
kfree(kbuf);
708-
709-
return ret;
710-
}
711-
712626
static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
713627
bool is_return)
714628
{

‎kernel/trace/trace_probe.h‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
#define MAX_TRACE_ARGS 128
4444
#define MAX_ARGSTR_LEN 63
45-
#define MAX_EVENT_NAME_LEN 64
4645
#define MAX_STRING_SIZE PATH_MAX
4746

4847
/* Reserved field names */
@@ -356,12 +355,6 @@ extern void traceprobe_free_probe_arg(struct probe_arg *arg);
356355

357356
extern int traceprobe_split_symbol_offset(char *symbol, unsigned long *offset);
358357

359-
extern ssize_t traceprobe_probes_write(struct file *file,
360-
const char __user *buffer, size_t count, loff_t *ppos,
361-
int (*createfn)(int, char**));
362-
363-
extern int traceprobe_command(const char *buf, int (*createfn)(int, char**));
364-
365358
/* Sum up total data length for dynamic arraies (strings) */
366359
static nokprobe_inline int
367360
__get_data_size(struct trace_probe *tp, struct pt_regs *regs)

‎kernel/trace/trace_uprobe.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ static int probes_open(struct inode *inode, struct file *file)
651651
static ssize_t probes_write(struct file *file, const char __user *buffer,
652652
size_t count, loff_t *ppos)
653653
{
654-
return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
654+
return trace_parse_run_command(file, buffer, count, ppos, create_trace_uprobe);
655655
}
656656

657657
static const struct file_operations uprobe_events_ops = {

0 commit comments

Comments
 (0)