Skip to content

Commit 5bf652a

Browse files
Namhyung Kimrostedt
authored andcommitted
tracing/probes: Integrate duplicate set_print_fmt()
The set_print_fmt() functions are implemented almost same for [ku]probes. Move it to a common place and get rid of the duplication. Acked-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Acked-by: Oleg Nesterov <oleg@redhat.com> Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com> Cc: zhangwei(Jovi) <jovi.zhangwei@huawei.com> Cc: Arnaldo Carvalho de Melo <acme@ghostprotocols.net> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 2dc1018 commit 5bf652a

4 files changed

Lines changed: 66 additions & 116 deletions

File tree

kernel/trace/trace_kprobe.c

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -964,67 +964,6 @@ static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
964964
return 0;
965965
}
966966

967-
static int __set_print_fmt(struct trace_kprobe *tk, char *buf, int len)
968-
{
969-
int i;
970-
int pos = 0;
971-
972-
const char *fmt, *arg;
973-
974-
if (!trace_kprobe_is_return(tk)) {
975-
fmt = "(%lx)";
976-
arg = "REC->" FIELD_STRING_IP;
977-
} else {
978-
fmt = "(%lx <- %lx)";
979-
arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
980-
}
981-
982-
/* When len=0, we just calculate the needed length */
983-
#define LEN_OR_ZERO (len ? len - pos : 0)
984-
985-
pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
986-
987-
for (i = 0; i < tk->tp.nr_args; i++) {
988-
pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
989-
tk->tp.args[i].name, tk->tp.args[i].type->fmt);
990-
}
991-
992-
pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
993-
994-
for (i = 0; i < tk->tp.nr_args; i++) {
995-
if (strcmp(tk->tp.args[i].type->name, "string") == 0)
996-
pos += snprintf(buf + pos, LEN_OR_ZERO,
997-
", __get_str(%s)",
998-
tk->tp.args[i].name);
999-
else
1000-
pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
1001-
tk->tp.args[i].name);
1002-
}
1003-
1004-
#undef LEN_OR_ZERO
1005-
1006-
/* return the length of print_fmt */
1007-
return pos;
1008-
}
1009-
1010-
static int set_print_fmt(struct trace_kprobe *tk)
1011-
{
1012-
int len;
1013-
char *print_fmt;
1014-
1015-
/* First: called with 0 length to calculate the needed length */
1016-
len = __set_print_fmt(tk, NULL, 0);
1017-
print_fmt = kmalloc(len + 1, GFP_KERNEL);
1018-
if (!print_fmt)
1019-
return -ENOMEM;
1020-
1021-
/* Second: actually write the @print_fmt */
1022-
__set_print_fmt(tk, print_fmt, len + 1);
1023-
tk->tp.call.print_fmt = print_fmt;
1024-
1025-
return 0;
1026-
}
1027-
1028967
#ifdef CONFIG_PERF_EVENTS
1029968

1030969
/* Kprobe profile handler */
@@ -1175,7 +1114,7 @@ static int register_kprobe_event(struct trace_kprobe *tk)
11751114
call->event.funcs = &kprobe_funcs;
11761115
call->class->define_fields = kprobe_event_define_fields;
11771116
}
1178-
if (set_print_fmt(tk) < 0)
1117+
if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
11791118
return -ENOMEM;
11801119
ret = register_ftrace_event(&call->event);
11811120
if (!ret) {

kernel/trace/trace_probe.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,3 +837,65 @@ ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,
837837

838838
return ret;
839839
}
840+
841+
static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
842+
bool is_return)
843+
{
844+
int i;
845+
int pos = 0;
846+
847+
const char *fmt, *arg;
848+
849+
if (!is_return) {
850+
fmt = "(%lx)";
851+
arg = "REC->" FIELD_STRING_IP;
852+
} else {
853+
fmt = "(%lx <- %lx)";
854+
arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
855+
}
856+
857+
/* When len=0, we just calculate the needed length */
858+
#define LEN_OR_ZERO (len ? len - pos : 0)
859+
860+
pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
861+
862+
for (i = 0; i < tp->nr_args; i++) {
863+
pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
864+
tp->args[i].name, tp->args[i].type->fmt);
865+
}
866+
867+
pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
868+
869+
for (i = 0; i < tp->nr_args; i++) {
870+
if (strcmp(tp->args[i].type->name, "string") == 0)
871+
pos += snprintf(buf + pos, LEN_OR_ZERO,
872+
", __get_str(%s)",
873+
tp->args[i].name);
874+
else
875+
pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
876+
tp->args[i].name);
877+
}
878+
879+
#undef LEN_OR_ZERO
880+
881+
/* return the length of print_fmt */
882+
return pos;
883+
}
884+
885+
int set_print_fmt(struct trace_probe *tp, bool is_return)
886+
{
887+
int len;
888+
char *print_fmt;
889+
890+
/* First: called with 0 length to calculate the needed length */
891+
len = __set_print_fmt(tp, NULL, 0, is_return);
892+
print_fmt = kmalloc(len + 1, GFP_KERNEL);
893+
if (!print_fmt)
894+
return -ENOMEM;
895+
896+
/* Second: actually write the @print_fmt */
897+
__set_print_fmt(tp, print_fmt, len + 1, is_return);
898+
tp->call.print_fmt = print_fmt;
899+
900+
return 0;
901+
}

kernel/trace/trace_probe.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,5 @@ store_trace_args(int ent_size, struct trace_probe *tp, struct pt_regs *regs,
226226
data + tp->args[i].offset);
227227
}
228228
}
229+
230+
extern int set_print_fmt(struct trace_probe *tp, bool is_return);

kernel/trace/trace_uprobe.c

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -682,59 +682,6 @@ static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
682682
return 0;
683683
}
684684

685-
#define LEN_OR_ZERO (len ? len - pos : 0)
686-
static int __set_print_fmt(struct trace_uprobe *tu, char *buf, int len)
687-
{
688-
const char *fmt, *arg;
689-
int i;
690-
int pos = 0;
691-
692-
if (is_ret_probe(tu)) {
693-
fmt = "(%lx <- %lx)";
694-
arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
695-
} else {
696-
fmt = "(%lx)";
697-
arg = "REC->" FIELD_STRING_IP;
698-
}
699-
700-
/* When len=0, we just calculate the needed length */
701-
702-
pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
703-
704-
for (i = 0; i < tu->tp.nr_args; i++) {
705-
pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
706-
tu->tp.args[i].name, tu->tp.args[i].type->fmt);
707-
}
708-
709-
pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
710-
711-
for (i = 0; i < tu->tp.nr_args; i++) {
712-
pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
713-
tu->tp.args[i].name);
714-
}
715-
716-
return pos; /* return the length of print_fmt */
717-
}
718-
#undef LEN_OR_ZERO
719-
720-
static int set_print_fmt(struct trace_uprobe *tu)
721-
{
722-
char *print_fmt;
723-
int len;
724-
725-
/* First: called with 0 length to calculate the needed length */
726-
len = __set_print_fmt(tu, NULL, 0);
727-
print_fmt = kmalloc(len + 1, GFP_KERNEL);
728-
if (!print_fmt)
729-
return -ENOMEM;
730-
731-
/* Second: actually write the @print_fmt */
732-
__set_print_fmt(tu, print_fmt, len + 1);
733-
tu->tp.call.print_fmt = print_fmt;
734-
735-
return 0;
736-
}
737-
738685
#ifdef CONFIG_PERF_EVENTS
739686
static bool
740687
__uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
@@ -966,7 +913,7 @@ static int register_uprobe_event(struct trace_uprobe *tu)
966913
call->event.funcs = &uprobe_funcs;
967914
call->class->define_fields = uprobe_event_define_fields;
968915

969-
if (set_print_fmt(tu) < 0)
916+
if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
970917
return -ENOMEM;
971918

972919
ret = register_ftrace_event(&call->event);

0 commit comments

Comments
 (0)