Skip to contentSkip to conversation

tracing: Updates for 4.20 (or 5.0)

GIT PULL Steven Rostedt 2 participants 3 messages Oct 30, 2018 – Oct 31, 2018

Subsystems (6)
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

Linus,

The biggest change here is the updates to kprobes

Back in January I posted patches to create function based events. These were the events that you suggested I make to allow developers to easily create events in code where no trace event exists. After posting those changes for review, it was suggested that we implement this instead with kprobes.

The problem with kprobes is that the interface is too complex and needs to be simplified. Masami Hiramatsu posted patches in March and I've been playing with them a bit. There's been a bit of clean up in the kprobe code that was inspired by the function based event patches, and a couple of enhancements to the kprobe event interface.

- If the arch supports it (we added support for x86), you can place a kprobe event at the start of a function and use $arg1, $arg2, etc to reference the arguments of a function. (Before you needed to know what register or where on the stack the argument was).

- The second is a way to see array of events. For example, if you reference a mac address, you can add:

echo 'p:mac ip_rcv perm_addr=+574($arg2):x8[6]' > kprobe_events

And this will produce:

mac: (ip_rcv+0x0/0x140) perm_addr={0x52,0x54,0x0,0xc0,0x76,0xec}

Other changes include

- Exporting trace_dump_stack to modules

- Have the stack tracer trace the entire stack (stop trying to remove tracing itself, as we keep removing too much).

- Added support for SDT in uprobes

Please pull the latest trace-v4.20 tree, which can be found at:

git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace.git
trace-v4.20

Tag SHA1: ef335507c87fe732bf6f1018fda7a77484559b51 Head SHA1: a2acce536921bd793bae13fa344fcea157638e72

Masami Hiramatsu (15):
tracing: probeevent: Cleanup print argument functions
tracing: probeevent: Cleanup argument field definition
tracing: probeevent: Remove NOKPROBE_SYMBOL from print functions
tracing: probeevent: Introduce new argument fetching code
tracing: probeevent: Unify fetch type tables
tracing: probeevent: Return consumed bytes of dynamic area
tracing: probeevent: Append traceprobe_ for exported function
tracing: probeevent: Unify fetch_insn processing common part
tracing: probeevent: Add symbol type
tracing: probeevent: Add array type support
x86: ptrace: Add function argument access API
tracing: probeevent: Add $argN for accessing function args tracing/uprobes: Fix to return -EFAULT if copy_from_user failed tracing/kprobes: Check the probe on unloaded module correctly tracing/kprobes: Allow kprobe-events to record module symbol

Nikolay Borisov (1):
tracing: Export trace_dump_stack to modules

Ravi Bangoria (4):
uprobes: Support SDT markers having reference count (semaphore) uprobes/sdt: Prevent multiple reference counter for same uprobe trace_uprobe/sdt: Prevent multiple reference counter for same uprobe perf probe: Support SDT markers having reference counter (semaphore)

Song Liu (1):
trace_uprobe: support reference counter in fd-based uprobe

Steven Rostedt (VMware) (3):
tracing/kprobe: Remove unneeded extra strchr() from create_trace_kprobe()
tracing: probeevent: Fix uninitialized used of offset in parse args
tracing: Have stack tracer trace full stack

----

 Documentation/trace/kprobetrace.rst |  23 +-
 arch/Kconfig                        |   7 +
 arch/x86/Kconfig                    |   1 +
 arch/x86/include/asm/ptrace.h       |  38 ++
 include/linux/trace_events.h        |   3 +-
 include/linux/uprobes.h             |   5 +
 kernel/events/core.c                |  49 ++-
 kernel/events/uprobes.c             | 278 ++++++++++++++-
 kernel/trace/trace.c                |  12 +-
 kernel/trace/trace_event_perf.c     |   7 +-
 kernel/trace/trace_kprobe.c         | 412 +++++++++-------------
 kernel/trace/trace_probe.c          | 672 ++++++++++++++++++------------------
 kernel/trace/trace_probe.h          | 289 +++++-----------
 kernel/trace/trace_probe_tmpl.h     | 216 ++++++++++++
 kernel/trace/trace_stack.c          |   2 +-
 kernel/trace/trace_uprobe.c         | 255 ++++++++------
 tools/perf/util/probe-event.c       |  39 ++-
 tools/perf/util/probe-event.h       |   1 +
 tools/perf/util/probe-file.c        |  34 +-
 tools/perf/util/probe-file.h        |   1 +
 tools/perf/util/symbol-elf.c        |  46 ++-
 tools/perf/util/symbol.h            |   7 +
 22 files changed, 1439 insertions(+), 958 deletions(-)
 create mode 100644 kernel/trace/trace_probe_tmpl.h

---------------------------

Documentation/trace/kprobetrace.rst +19 -4
index 8bfc75c90806..47e765c2f2c3 100644
@@ -45,16 +45,18 @@ Synopsis of kprobe_events
   @SYM[+|-offs]	: Fetch memory at SYM +|- offs (SYM should be a data symbol)
   $stackN	: Fetch Nth entry of stack (N >= 0)
   $stack	: Fetch stack address.
-  $retval	: Fetch return value.(*)
+  $argN		: Fetch the Nth function argument. (N >= 1) (\*1)
+  $retval	: Fetch return value.(\*2)
   $comm		: Fetch current task comm.
-  +|-offs(FETCHARG) : Fetch memory at FETCHARG +|- offs address.(**)
+  +|-offs(FETCHARG) : Fetch memory at FETCHARG +|- offs address.(\*3)
   NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
   FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
 		  (u8/u16/u32/u64/s8/s16/s32/s64), hexadecimal types
 		  (x8/x16/x32/x64), "string" and bitfield are supported.
 
-  (*) only for return probe.
-  (**) this is useful for fetching a field of data structures.
+  (\*1) only for the probe on function entry (offs == 0).
+  (\*2) only for return probe.
+  (\*3) this is useful for fetching a field of data structures.
 
 Types
 -----
@@ -64,14 +66,27 @@ respectively. 'x' prefix implies it is unsigned. Traced arguments are shown
 in decimal ('s' and 'u') or hexadecimal ('x'). Without type casting, 'x32'
 or 'x64' is used depends on the architecture (e.g. x86-32 uses x32, and
 x86-64 uses x64).
+These value types can be an array. To record array data, you can add '[N]'
+(where N is a fixed number, less than 64) to the base type.
+E.g. 'x16[4]' means an array of x16 (2bytes hex) with 4 elements.
+Note that the array can be applied to memory type fetchargs, you can not
+apply it to registers/stack-entries etc. (for example, '$stack1:x8[8]' is
+wrong, but '+8($stack):x8[8]' is OK.)
 String type is a special type, which fetches a "null-terminated" string from
 kernel space. This means it will fail and store NULL if the string container
 has been paged out.
+The string array type is a bit different from other types. For other base
+types, <base-type>[1] is equal to <base-type> (e.g. +0(%di):x32[1] is same
+as +0(%di):x32.) But string[1] is not equal to string. The string type itself
+represents "char array", but string array type represents "char * array".
+So, for example, +0(%di):string[1] is equal to +0(+0(%di)):string.
 Bitfield is another special type, which takes 3 parameters, bit-width, bit-
 offset, and container-size (usually 32). The syntax is::
 
  b<bit-width>@<bit-offset>/<container-size>
 
+Symbol type('symbol') is an alias of u32 or u64 type (depends on BITS_PER_LONG)
+which shows given pointer in "symbol+offset" style.
 For $comm, the default type is "string"; any other type is invalid.
 
 
arch/Kconfig +7 -0
index 6801123932a5..facace0c90fc 100644
@@ -290,6 +290,13 @@ config HAVE_RSEQ
 	  This symbol should be selected by an architecture if it
 	  supports an implementation of restartable sequences.
 
+config HAVE_FUNCTION_ARG_ACCESS_API
+	bool
+	help
+	  This symbol should be selected by an architecure if it supports
+	  the API needed to access function arguments from pt_regs,
+	  declared in asm/ptrace.h
+
 config HAVE_CLK
 	bool
 	help
arch/x86/Kconfig +1 -0
index 1a0be022f91d..972973851779 100644
@@ -184,6 +184,7 @@ config X86
 	select HAVE_RCU_TABLE_INVALIDATE	if HAVE_RCU_TABLE_FREE
 	select HAVE_REGS_AND_STACK_ACCESS_API
 	select HAVE_RELIABLE_STACKTRACE		if X86_64 && (UNWINDER_FRAME_POINTER || UNWINDER_ORC) && STACK_VALIDATION
+	select HAVE_FUNCTION_ARG_ACCESS_API
 	select HAVE_STACKPROTECTOR		if CC_HAS_SANE_STACKPROTECTOR
 	select HAVE_STACK_VALIDATION		if X86_64
 	select HAVE_RSEQ
arch/x86/include/asm/ptrace.h +38 -0
index 6de1fd3d0097..c2304b25e2fd 100644
@@ -256,6 +256,44 @@ static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
 		return 0;
 }
 
+/**
+ * regs_get_kernel_argument() - get Nth function argument in kernel
+ * @regs:	pt_regs of that context
+ * @n:		function argument number (start from 0)
+ *
+ * regs_get_argument() returns @n th argument of the function call.
+ * Note that this chooses most probably assignment, in some case
+ * it can be incorrect.
+ * This is expected to be called from kprobes or ftrace with regs
+ * where the top of stack is the return address.
+ */
+static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs,
+						     unsigned int n)
+{
+	static const unsigned int argument_offs[] = {
+#ifdef __i386__
+		offsetof(struct pt_regs, ax),
+		offsetof(struct pt_regs, cx),
+		offsetof(struct pt_regs, dx),
+#define NR_REG_ARGUMENTS 3
+#else
+		offsetof(struct pt_regs, di),
+		offsetof(struct pt_regs, si),
+		offsetof(struct pt_regs, dx),
+		offsetof(struct pt_regs, cx),
+		offsetof(struct pt_regs, r8),
+		offsetof(struct pt_regs, r9),
+#define NR_REG_ARGUMENTS 6
+#endif
+	};
+
+	if (n >= NR_REG_ARGUMENTS) {
+		n -= NR_REG_ARGUMENTS - 1;
+		return regs_get_kernel_stack_nth(regs, n);
+	} else
+		return regs_get_register(regs, argument_offs[n]);
+}
+
 #define arch_has_single_step()	(1)
 #ifdef CONFIG_X86_DEBUGCTLMSR
 #define arch_has_block_step()	(1)
include/linux/trace_events.h +2 -1
index 78a010e19ed4..4130a5497d40 100644
@@ -575,7 +575,8 @@ extern int bpf_get_kprobe_info(const struct perf_event *event,
 			       bool perf_type_tracepoint);
 #endif
 #ifdef CONFIG_UPROBE_EVENTS
-extern int  perf_uprobe_init(struct perf_event *event, bool is_retprobe);
+extern int  perf_uprobe_init(struct perf_event *event,
+			     unsigned long ref_ctr_offset, bool is_retprobe);
 extern void perf_uprobe_destroy(struct perf_event *event);
 extern int bpf_get_uprobe_info(const struct perf_event *event,
 			       u32 *fd_type, const char **filename,
include/linux/uprobes.h +5 -0
index bb9d2084af03..103a48a48872 100644
@@ -123,6 +123,7 @@ extern unsigned long uprobe_get_swbp_addr(struct pt_regs *regs);
 extern unsigned long uprobe_get_trap_addr(struct pt_regs *regs);
 extern int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t);
 extern int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc);
+extern int uprobe_register_refctr(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc);
 extern int uprobe_apply(struct inode *inode, loff_t offset, struct uprobe_consumer *uc, bool);
 extern void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc);
 extern int uprobe_mmap(struct vm_area_struct *vma);
@@ -160,6 +161,10 @@ uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
 {
 	return -ENOSYS;
 }
+static inline int uprobe_register_refctr(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc)
+{
+	return -ENOSYS;
+}
 static inline int
 uprobe_apply(struct inode *inode, loff_t offset, struct uprobe_consumer *uc, bool add)
 {
kernel/events/core.c +39 -10

101 lines not on this page · raw message

kernel/events/uprobes.c +271 -7

382 lines not on this page · raw message

kernel/trace/trace.c +9 -3
index bf6f1d70484d..ff1c4b20cd0a 100644
@@ -2727,6 +2727,7 @@ void trace_dump_stack(int skip)
 	__ftrace_trace_stack(global_trace.trace_buffer.buffer,
 			     flags, skip, preempt_count(), NULL);
 }
+EXPORT_SYMBOL_GPL(trace_dump_stack);
 
 static DEFINE_PER_CPU(int, user_stack_count);
 
@@ -4621,13 +4622,18 @@ static const char readme_msg[] =
   "place (kretprobe): [<module>:]<symbol>[+<offset>]|<memaddr>\n"
 #endif
 #ifdef CONFIG_UPROBE_EVENTS
-	"\t    place: <path>:<offset>\n"
+  "   place (uprobe): <path>:<offset>[(ref_ctr_offset)]\n"
 #endif
 	"\t     args: <name>=fetcharg[:type]\n"
 	"\t fetcharg: %<register>, @<address>, @<symbol>[+|-<offset>],\n"
+#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
+	"\t           $stack<index>, $stack, $retval, $comm, $arg<N>\n"
+#else
 	"\t           $stack<index>, $stack, $retval, $comm\n"
-	"\t     type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string,\n"
-	"\t           b<bit-width>@<bit-offset>/<container-size>\n"
+#endif
+	"\t     type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
+	"\t           b<bit-width>@<bit-offset>/<container-size>,\n"
+	"\t           <type>\\[<array-size>\\]\n"
 #endif
 	"  events/\t\t- Directory containing all trace event subsystems:\n"
 	"      enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
kernel/trace/trace_event_perf.c +4 -3
index 69a3fe926e8c..76217bbef815 100644
@@ -290,7 +290,8 @@ void perf_kprobe_destroy(struct perf_event *p_event)
 #endif /* CONFIG_KPROBE_EVENTS */
 
 #ifdef CONFIG_UPROBE_EVENTS
-int perf_uprobe_init(struct perf_event *p_event, bool is_retprobe)
+int perf_uprobe_init(struct perf_event *p_event,
+		     unsigned long ref_ctr_offset, bool is_retprobe)
 {
 	int ret;
 	char *path = NULL;
@@ -312,8 +313,8 @@ int perf_uprobe_init(struct perf_event *p_event, bool is_retprobe)
 		goto out;
 	}
 
-	tp_event = create_local_trace_uprobe(
-		path, p_event->attr.probe_offset, is_retprobe);
+	tp_event = create_local_trace_uprobe(path, p_event->attr.probe_offset,
+					     ref_ctr_offset, is_retprobe);
 	if (IS_ERR(tp_event)) {
 		ret = PTR_ERR(tp_event);
 		goto out;
kernel/trace/trace_kprobe.c +162 -250

568 lines not on this page · raw message

kernel/trace/trace_probe.c +331 -341

893 lines not on this page · raw message

kernel/trace/trace_probe.h +86 -203

394 lines not on this page · raw message

kernel/trace/trace_probe_tmpl.h new file +216 -0

216 lines not on this page · raw message

kernel/trace/trace_stack.c +1 -1
index 4237eba4ef20..2b0d1ee3241c 100644
@@ -111,7 +111,7 @@ check_stack(unsigned long ip, unsigned long *stack)
 	stack_trace_max_size = this_size;
 
 	stack_trace_max.nr_entries = 0;
-	stack_trace_max.skip = 3;
+	stack_trace_max.skip = 0;
 
 	save_stack_trace(&stack_trace_max);
 
kernel/trace/trace_uprobe.c +142 -113

252 lines not on this page · raw message

tools/perf/util/probe-event.c +35 -4
index f119eb628dbb..e86f8be89157 100644

Lines of this file are not shown. View the raw message.

tools/perf/util/probe-event.h +1 -0
index 45b14f020558..15a98c3a2a2f 100644

Lines of this file are not shown. View the raw message.

tools/perf/util/probe-file.c +28 -6
index b76088fadf3d..aac7817d9e14 100644

Lines of this file are not shown. View the raw message.

tools/perf/util/probe-file.h +1 -0
index 63f29b1d22c1..2a249182f2a6 100644

Lines of this file are not shown. View the raw message.

tools/perf/util/symbol-elf.c +34 -12

46 lines not on this page · raw message

tools/perf/util/symbol.h +7 -0
index f25fae4b5743..20f49779116b 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

#1 lore raw
Quoted 2 lines · Steven Rostedt

The biggest change here is the updates to kprobes

Pulled.

However, a little note:

Quoted 1 line

- Added support for SDT in uprobes

As an explanation, the above kind of sucks. "SDT"? I had to look it up. I added a note to the commit message, but please, don't use random acronyms in kernel explanations unless they are industry-standard and comprehensible to kernel people.

Good acronyms: "TLB", "WTF", "LOL". People know what those mean. "SDT"? Not so much.

Linus

author maintainer
#2 lore raw
Quoted 7 lines · On Tue, 30 Oct 2018 10:02:16 -0700 Linus Torvalds…

As an explanation, the above kind of sucks. "SDT"? I had to look it up. I added a note to the commit message, but please, don't use random acronyms in kernel explanations unless they are industry-standard and comprehensible to kernel people.

Good acronyms: "TLB", "WTF", "LOL". People know what those mean. "SDT"? Not so much.

Apologies, this is one of the reasons I wanted to wait till the end of the week to do the pull request. I'm currently traveling (on my way home), and using my laptop for all my work (at home I use my workstation). I almost sent the pull request without even mentioning the SDT work. That's because I had tested that and pushed it into linux-next some time ago and completely forgot about it. I caught the error looking at the patches before sending, redid the signed tag by mentioning it and sent it out.

I would have had a deeper explanation about the change otherwise. I really hate merge windows while I'm traveling :-(

-- Steve