linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Omar Sandoval <osandov@osandov.com>
To: Steven Rostedt <rostedt@goodmis.org>,
	Ingo Molnar <mingo@redhat.com>,
	Masami Hiramatsu <mhiramat@kernel.org>
Cc: linux-kernel@vger.kernel.org, kernel-team@fb.com,
	Omar Sandoval <osandov@fb.com>
Subject: [PATCH] tracing: expose current->comm to kprobe events
Date: Tue, 31 May 2016 23:17:54 -0700	[thread overview]
Message-ID: <cbf3e4d63a6a7870dba4a1b5445c5964847273e6.1464761545.git.osandov@fb.com> (raw)

From: Omar Sandoval <osandov@fb.com>

ftrace is very quick to give up on saving the task command line (see
`trace_save_cmdline()`). The workaround for events which really care
about the command line is to explicitly assign it as part of the entry.
However, this doesn't work for kprobe events, as there's no
straightforward way to get access to current->comm. Add a kprobe event
variable $comm which provides exactly that.

Signed-off-by: Omar Sandoval <osandov@fb.com>
---
This is a problem I ran into while trying to debug an unrelated issue. I
was considering trying to make ftrace try harder to save the command
line, but that would most likely be at the expense at most events which
don't care about the task comm. This is much less work.

 Documentation/trace/kprobetrace.txt |  2 ++
 kernel/trace/trace_kprobe.c         | 13 +++++++++++++
 kernel/trace/trace_probe.c          |  5 +++++
 kernel/trace/trace_probe.h          |  2 ++
 kernel/trace/trace_uprobe.c         |  7 +++++++
 5 files changed, 29 insertions(+)

diff --git a/Documentation/trace/kprobetrace.txt b/Documentation/trace/kprobetrace.txt
index d68ea5fc812b..90914cbc1e51 100644
--- a/Documentation/trace/kprobetrace.txt
+++ b/Documentation/trace/kprobetrace.txt
@@ -40,6 +40,7 @@ Synopsis of kprobe_events
   $stackN	: Fetch Nth entry of stack (N >= 0)
   $stack	: Fetch stack address.
   $retval	: Fetch return value.(*)
+  $comm		: Fetch current task comm.(***)
   +|-offs(FETCHARG) : Fetch memory at FETCHARG +|- offs address.(**)
   NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
   FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
@@ -48,6 +49,7 @@ Synopsis of kprobe_events
 
   (*) only for return probe.
   (**) this is useful for fetching a field of data structures.
+  (***) you probably want this as a string, i.e., $comm:string
 
 Types
 -----
diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
index 5546eec0505f..e3a087552d49 100644
--- a/kernel/trace/trace_kprobe.c
+++ b/kernel/trace/trace_kprobe.c
@@ -214,6 +214,18 @@ static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
 }
 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string_size));
 
+#define DEFINE_FETCH_comm(type)					\
+void FETCH_FUNC_NAME(comm, type)(struct pt_regs *regs,		\
+				 void *data, void *dest)	\
+{								\
+	fetch_memory_##type(regs, current->comm, dest);		\
+}								\
+NOKPROBE_SYMBOL(FETCH_FUNC_NAME(comm, type));
+
+DEFINE_BASIC_FETCH_FUNCS(comm)
+DEFINE_FETCH_comm(string)
+DEFINE_FETCH_comm(string_size)
+
 #define DEFINE_FETCH_symbol(type)					\
 void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, void *data, void *dest)\
 {									\
@@ -587,6 +599,7 @@ static int create_trace_kprobe(int argc, char **argv)
 	 *  $retval	: fetch return value
 	 *  $stack	: fetch stack address
 	 *  $stackN	: fetch Nth of stack (N:0-)
+	 *  $comm       : fetch current task comm
 	 *  @ADDR	: fetch memory at ADDR (ADDR should be in kernel)
 	 *  @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
 	 *  %REG	: fetch register REG
diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c
index 1d372fa6fefb..918775fcacd3 100644
--- a/kernel/trace/trace_probe.c
+++ b/kernel/trace/trace_probe.c
@@ -348,6 +348,11 @@ static int parse_probe_vars(char *arg, const struct fetch_type *t,
 			}
 		} else
 			ret = -EINVAL;
+	} else if (strcmp(arg, "comm") == 0) {
+		if (is_kprobe)
+			f->fn = t->fetch[FETCH_MTD_comm];
+		else
+			ret = -EINVAL;
 	} else
 		ret = -EINVAL;
 
diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h
index f6398db09114..3f1a987e6dc1 100644
--- a/kernel/trace/trace_probe.h
+++ b/kernel/trace/trace_probe.h
@@ -102,6 +102,7 @@ enum {
 	FETCH_MTD_reg = 0,
 	FETCH_MTD_stack,
 	FETCH_MTD_retval,
+	FETCH_MTD_comm,
 	FETCH_MTD_memory,
 	FETCH_MTD_symbol,
 	FETCH_MTD_deref,
@@ -213,6 +214,7 @@ DEFINE_FETCH_##method(u64)
 ASSIGN_FETCH_FUNC(reg, ftype),				\
 ASSIGN_FETCH_FUNC(stack, ftype),			\
 ASSIGN_FETCH_FUNC(retval, ftype),			\
+ASSIGN_FETCH_FUNC(comm, ftype),				\
 ASSIGN_FETCH_FUNC(memory, ftype),			\
 ASSIGN_FETCH_FUNC(symbol, ftype),			\
 ASSIGN_FETCH_FUNC(deref, ftype),			\
diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c
index c53485441c88..f7eedb191d1f 100644
--- a/kernel/trace/trace_uprobe.c
+++ b/kernel/trace/trace_uprobe.c
@@ -172,6 +172,13 @@ static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
 		*(u32 *)dest = len;
 }
 
+#define fetch_comm_u8		NULL
+#define fetch_comm_u16		NULL
+#define fetch_comm_u32		NULL
+#define fetch_comm_u64		NULL
+#define fetch_comm_string	NULL
+#define fetch_comm_string_size	NULL
+
 static unsigned long translate_user_vaddr(void *file_offset)
 {
 	unsigned long base_addr;
-- 
2.8.3

             reply	other threads:[~2016-06-01  6:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-01  6:17 Omar Sandoval [this message]
2016-06-02 15:40 ` [PATCH] tracing: expose current->comm to kprobe events Namhyung Kim
2016-06-02 18:21   ` Omar Sandoval
2016-06-03  8:59   ` Masami Hiramatsu
2016-06-03 16:41     ` Omar Sandoval

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cbf3e4d63a6a7870dba4a1b5445c5964847273e6.1464761545.git.osandov@fb.com \
    --to=osandov@osandov.com \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=osandov@fb.com \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).