Skip to contentSkip to conversation

trace_syscalls: init print_fmt

PATCH In review Lai Jiangshan 2 participants 4 messages Dec 9, 2009 – Dec 11, 2009 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 7/10 trace_syscalls: init print_fmt

Init print_fmt for trace_syscalls.
It will be used for replacing ->show_format().

---

kernel/trace/trace_syscalls.c +61 -1
index 75289f3..dcd8699 100644
@@ -191,6 +191,61 @@ int syscall_enter_format(struct ftrace_event_call *call, struct trace_seq *s)
 	return trace_seq_putc(s, '\n');
 }
 
+static
+int  __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
+{
+	int i;
+	int pos = 0;
+
+	pos += snprintf(buf + pos, len > pos ? len - pos : 0, "\"");
+	for (i = 0; i < entry->nb_args; i++) {
+		pos += snprintf(buf + pos, len > pos ? len - pos : 0,
+				"%s: 0x%%0%zulx%s", entry->args[i],
+				sizeof(unsigned long),
+				i == entry->nb_args - 1 ? "" : ", ");
+	}
+	pos += snprintf(buf + pos, len > pos ? len - pos : 0, "\"");
+
+	for (i = 0; i < entry->nb_args; i++) {
+		pos += snprintf(buf + pos, len > pos ? len - pos : 0,
+				", ((unsigned long)(REC->%s))", entry->args[i]);
+	}
+
+	/* return the length of print_fmt */
+	return pos;
+}
+
+static int set_syscall_print_fmt(struct ftrace_event_call *call)
+{
+	char *print_fmt;
+	int len;
+	struct syscall_metadata *entry = call->data;
+
+	if (entry->enter_event != call) {
+		call->print_fmt = "\"0x%lx\", REC->ret";
+		return 0;
+	}
+
+	len = __set_enter_print_fmt(entry, NULL, 0);
+
+	print_fmt = kmalloc(len + 1, GFP_KERNEL);
+	if (!print_fmt)
+		return -ENOMEM;
+
+	__set_enter_print_fmt(entry, print_fmt, len + 1);
+	call->print_fmt = print_fmt;
+
+	return 0;
+}
+
+static void free_syscall_print_fmt(struct ftrace_event_call *call)
+{
+	struct syscall_metadata *entry = call->data;
+
+	if (entry->enter_event == call)
+		kfree(call->print_fmt);
+}
+
 int syscall_exit_format(struct ftrace_event_call *call, struct trace_seq *s)
 {
 	int ret;
@@ -386,9 +441,14 @@ int init_syscall_trace(struct ftrace_event_call *call)
 {
 	int id;
 
+	if (set_syscall_print_fmt(call) < 0)
+		return -ENOMEM;
+
 	id = register_ftrace_event(call->event);
-	if (!id)
+	if (!id) {
+		free_syscall_print_fmt(call);
 		return -ENODEV;
+	}
 	call->id = id;
 	INIT_LIST_HEAD(&call->fields);
 	return 0;
maintainer
#1 lore raw
Quoted 2 lines · Lai Jiangshan

Init print_fmt for trace_syscalls.
It will be used for replacing ->show_format().

This needs more explanation too.

Quoted 19 lines

---

kernel/trace/trace_syscalls.c +8 -0
index 75289f3..dcd8699 100644
@@ -191,6 +191,61 @@ int syscall_enter_format(struct ftrace_event_call *call, struct trace_seq *s)
 	return trace_seq_putc(s, '\n');
 }
 
+static
+int  __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
+{
+	int i;
+	int pos = 0;
+
+	pos += snprintf(buf + pos, len > pos ? len - pos : 0, "\"");
+	for (i = 0; i < entry->nb_args; i++) {

kernel/trace/trace_syscalls.c int syscall_enter_format(struct ftrace_event_call *call, struct trace_seq *s) · line 211

Quoted 10 lines
kernel/trace/trace_syscalls.c +10 -0
+		pos += snprintf(buf + pos, len > pos ? len - pos : 0,
+				"%s: 0x%%0%zulx%s", entry->args[i],
+				sizeof(unsigned long),
+				i == entry->nb_args - 1 ? "" : ", ");
+	}
+	pos += snprintf(buf + pos, len > pos ? len - pos : 0, "\"");
+
+	for (i = 0; i < entry->nb_args; i++) {
+		pos += snprintf(buf + pos, len > pos ? len - pos : 0,
+				", ((unsigned long)(REC->%s))", entry->args[i]);

Yuck! 4 snprintf(buf + pos, len > pos ? len - pos: 0 ...

Please make a wrapper for that.

Actually, this could use the new trace_seq code if I separate the buffer. You would just need to do:

	struct trace_seq s;

	trace_seq_init(&s, buf, len);

	trace_seq_putc(&s, '"');
	for (i = 0; i < entry->nb_args; i++)
		trace_seq_printf(&s, "%s: 0x%%0%zulx%s", entry->args[i],
				sizeof(unsigned long),
				i == entry->nb_args - 1 ? "" : ", ");

	trace_seq_putc(&s, '"');

	for (i = 0; i < entry->nb_args; i++)
		trace_seq_printf(&s, ", ((unsigned long)(REC->%s)",
				entry->args[i]);

	return s.len;

Looks much better, and less error prone.

-- Steve

Quoted 58 lines
kernel/trace/trace_syscalls.c +43 -1
+	}
+
+	/* return the length of print_fmt */
+	return pos;
+}
+
+static int set_syscall_print_fmt(struct ftrace_event_call *call)
+{
+	char *print_fmt;
+	int len;
+	struct syscall_metadata *entry = call->data;
+
+	if (entry->enter_event != call) {
+		call->print_fmt = "\"0x%lx\", REC->ret";
+		return 0;
+	}
+
+	len = __set_enter_print_fmt(entry, NULL, 0);
+
+	print_fmt = kmalloc(len + 1, GFP_KERNEL);
+	if (!print_fmt)
+		return -ENOMEM;
+
+	__set_enter_print_fmt(entry, print_fmt, len + 1);
+	call->print_fmt = print_fmt;
+
+	return 0;
+}
+
+static void free_syscall_print_fmt(struct ftrace_event_call *call)
+{
+	struct syscall_metadata *entry = call->data;
+
+	if (entry->enter_event == call)
+		kfree(call->print_fmt);
+}
+
 int syscall_exit_format(struct ftrace_event_call *call, struct trace_seq *s)
 {
 	int ret;
@@ -386,9 +441,14 @@ int init_syscall_trace(struct ftrace_event_call *call)
 {
 	int id;
 
+	if (set_syscall_print_fmt(call) < 0)
+		return -ENOMEM;
+
 	id = register_ftrace_event(call->event);
-	if (!id)
+	if (!id) {
+		free_syscall_print_fmt(call);
 		return -ENODEV;
+	}
 	call->id = id;
 	INIT_LIST_HEAD(&call->fields);
 	return 0;
#2 lore raw
Quoted 37 lines · Steven Rostedt
Quoted 2 lines · Lai Jiangshan

Init print_fmt for trace_syscalls.
It will be used for replacing ->show_format().

This needs more explanation too.

Quoted 18 lines

---

kernel/trace/trace_syscalls.c +8 -0
index 75289f3..dcd8699 100644
@@ -191,6 +191,61 @@ int syscall_enter_format(struct ftrace_event_call *call, struct trace_seq *s)
 	return trace_seq_putc(s, '\n');
 }
 
+static
+int  __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
+{
+	int i;
+	int pos = 0;
+
+	pos += snprintf(buf + pos, len > pos ? len - pos : 0, "\"");
+	for (i = 0; i < entry->nb_args; i++) {

kernel/trace/trace_syscalls.c int syscall_enter_format(struct ftrace_event_call *call, struct trace_seq *s) · line 211

Quoted 10 lines
kernel/trace/trace_syscalls.c +10 -0
+		pos += snprintf(buf + pos, len > pos ? len - pos : 0,
+				"%s: 0x%%0%zulx%s", entry->args[i],
+				sizeof(unsigned long),
+				i == entry->nb_args - 1 ? "" : ", ");
+	}
+	pos += snprintf(buf + pos, len > pos ? len - pos : 0, "\"");
+
+	for (i = 0; i < entry->nb_args; i++) {
+		pos += snprintf(buf + pos, len > pos ? len - pos : 0,
+				", ((unsigned long)(REC->%s))", entry->args[i]);

Yuck! 4 snprintf(buf + pos, len > pos ? len - pos: 0 ...

Quoted 1 line

Please make a wrapper for that.

Do you mind if I use a macro?
Readability are still the same...

static
int __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)

{
	int i;
	int pos = 0;

#define BUF_PRINTF(fmt...) \

do {									\
	pos += snprintf(buf + pos, len > pos ? len - pos : 0, fmt);	\
} while (0)
	BUF_PRINTF("\"");
	for (i = 0; i < entry->nb_args; i++) {
		BUF_PRINTF("%s: 0x%%0%zulx%s", entry->args[i],
				sizeof(unsigned long),
				i == entry->nb_args - 1 ? "\"" : ", ");
	}

	for (i = 0; i < entry->nb_args; i++)
		BUF_PRINTF(", ((unsigned long)(REC->%s))", entry->args[i]);

#under BUF_PRINTF

	/* return the length of print_fmt */
	return pos;
}
Quoted 5 lines

Actually, this could use the new trace_seq code if I separate the buffer. You would just need to do:

	struct trace_seq s;

I think trace_seq can not help.
__set_enter_print_fmt() is called twice.
We calculate the length at the first time(don't write any thing), then allocate memory and then really "snprintf" string into the buffer.

-- Lai

Quoted 22 lines
	trace_seq_init(&s, buf, len);

	trace_seq_putc(&s, '"');
	for (i = 0; i < entry->nb_args; i++)
		trace_seq_printf(&s, "%s: 0x%%0%zulx%s", entry->args[i],
				sizeof(unsigned long),
				i == entry->nb_args - 1 ? "" : ", ");

	trace_seq_putc(&s, '"');

	for (i = 0; i < entry->nb_args; i++)
		trace_seq_printf(&s, ", ((unsigned long)(REC->%s)",
				entry->args[i]);

	return s.len;

Looks much better, and less error prone.

-- Steve

maintainer
#3 lore raw
Quoted 74 lines · Lai Jiangshan
Quoted 37 lines · Steven Rostedt
Quoted 2 lines · Lai Jiangshan

Init print_fmt for trace_syscalls.
It will be used for replacing ->show_format().

This needs more explanation too.

Quoted 18 lines

---

kernel/trace/trace_syscalls.c +8 -0
index 75289f3..dcd8699 100644
@@ -191,6 +191,61 @@ int syscall_enter_format(struct ftrace_event_call *call, struct trace_seq *s)
 	return trace_seq_putc(s, '\n');
 }
 
+static
+int  __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
+{
+	int i;
+	int pos = 0;
+
+	pos += snprintf(buf + pos, len > pos ? len - pos : 0, "\"");
+	for (i = 0; i < entry->nb_args; i++) {

kernel/trace/trace_syscalls.c int syscall_enter_format(struct ftrace_event_call *call, struct trace_seq *s) · line 211

Quoted 10 lines
kernel/trace/trace_syscalls.c +10 -0
+		pos += snprintf(buf + pos, len > pos ? len - pos : 0,
+				"%s: 0x%%0%zulx%s", entry->args[i],
+				sizeof(unsigned long),
+				i == entry->nb_args - 1 ? "" : ", ");
+	}
+	pos += snprintf(buf + pos, len > pos ? len - pos : 0, "\"");
+
+	for (i = 0; i < entry->nb_args; i++) {
+		pos += snprintf(buf + pos, len > pos ? len - pos : 0,
+				", ((unsigned long)(REC->%s))", entry->args[i]);

Yuck! 4 snprintf(buf + pos, len > pos ? len - pos: 0 ...

Quoted 1 line

Please make a wrapper for that.

Do you mind if I use a macro?
Readability are still the same...

static
int __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)

{
	int i;
	int pos = 0;

#define BUF_PRINTF(fmt...) \

do {									\
	pos += snprintf(buf + pos, len > pos ? len - pos : 0, fmt);	\
} while (0)
	BUF_PRINTF("\"");
	for (i = 0; i < entry->nb_args; i++) {
		BUF_PRINTF("%s: 0x%%0%zulx%s", entry->args[i],
				sizeof(unsigned long),
				i == entry->nb_args - 1 ? "\"" : ", ");
	}

	for (i = 0; i < entry->nb_args; i++)
		BUF_PRINTF(", ((unsigned long)(REC->%s))", entry->args[i]);

#under BUF_PRINTF

	/* return the length of print_fmt */
	return pos;
}
Quoted 4 lines

Actually, this could use the new trace_seq code if I separate the buffer. You would just need to do:

Quoted 7 lines
Quoted 1 line
	struct trace_seq s;

I think trace_seq can not help.
__set_enter_print_fmt() is called twice.
We calculate the length at the first time(don't write any thing), then allocate memory and then really "snprintf" string into the buffer.

Oh God, I see what you are doing. Too many side effects here.

This code needs major comments. Anyway, you already calculated the length that is needed, you can just do "len ? len - pos : 0".

First, please comment what it is doing (that it gets called twice, once with 0 length to calculate the needed length and once with the calculated length).

Then just make the test a macro:

#define LEN_OR_ZERO (len ? len - pos : 0)

	pos += snprintf(buf + pos, LEN_OR_ZERO, fmt);

...

#undef LEN_OR_ZERO

With comments explaining what is happening and this macro, it can make reading the code a bit easier to understand what is going on.

-- Steve

Quoted 24 lines

-- Lai

Quoted 22 lines
	trace_seq_init(&s, buf, len);

	trace_seq_putc(&s, '"');
	for (i = 0; i < entry->nb_args; i++)
		trace_seq_printf(&s, "%s: 0x%%0%zulx%s", entry->args[i],
				sizeof(unsigned long),
				i == entry->nb_args - 1 ? "" : ", ");

	trace_seq_putc(&s, '"');

	for (i = 0; i < entry->nb_args; i++)
		trace_seq_printf(&s, ", ((unsigned long)(REC->%s)",
				entry->args[i]);

	return s.len;

Looks much better, and less error prone.

-- Steve