tools/dtrace: initial implementation of DTrace
Subsystems (3)
cover tools/dtrace: initial implementation of DTrace
This patch is also available, applied to bpf-next, at the following URL:
https://github.com/oracle/dtrace-linux-kernel/tree/dtrace-bpf
As suggested in feedback to my earlier patch submissions, this code takes an approach to avoid kernel code changes as much as possible. The current patch does not involve any kernel code changes. Further development of this code will continue with this approach, incrementally adding features to this first minimal implementation. The goal is a fully featured and functional DTrace implementation involving kernel changes only when strictly necessary.
The code presented here supports two very basic functions:
1. Listing probes that are used in BPF programs
# dtrace -l -s bpf_sample.o
ID PROVIDER MODULE FUNCTION NAME
18876 fbt vmlinux ksys_write entry
70423 syscall vmlinux write entry2. Loading BPF tracing programs and collecting data that they generate
# dtrace -s bpf_sample.o
CPU ID
15 70423 0xffff8c0968bf8ec0 0x00000000000001 0x0055e019eb3f60 0x0000000000002c
15 18876 0xffff8c0968bf8ec0 0x00000000000001 0x0055e019eb3f60 0x0000000000002c
...Only kprobes and syscall tracepoints are supported since this is an initial patch. It does show the use of a generic BPF function to implement the actual probe action, called from two distinct probe types. Follow-up patches will add more probe types, add more tracing features from the D language, add support for D script compilation to BPF, etc.
The implementation makes use of libbpf for handling BPF ELF objects, and uses the perf event output ring buffer (supported through BPF) to retrieve the tracing data. The next step in development will be adding support to libbpf for programs using shared functions from a collection of functions included in the BPF ELF object (as suggested by Alexei).
The code is structured as follows:
tools/dtrace/dtrace.c = command line utility
tools/dtrace/dt_bpf.c = interface to libbpf
tools/dtrace/dt_buffer.c = perf event output buffer handling
tools/dtrace/dt_fbt.c = kprobes probe provider
tools/dtrace/dt_syscall.c = syscall tracepoint probe provider
tools/dtrace/dt_probe.c = generic probe and probe provider handling code
This implements a generic interface to the actual
probe providers (dt_fbt and dt_syscall).
tools/dtrace/dt_hash.c = general probe hashing implementation
tools/dtrace/dt_utils.c = support code (manage list of online CPUs)
tools/dtrace/dtrace.h = API header file (used by BPF program source code) tools/dtrace/dtrace_impl.h = implementation header file tools/dtrace/bpf_sample.c = sample BPF program using two probe types
I included an entry for the MAINTAINERS file. I offer to actively maintain this code, and to keep advancing its development.
Cheers,
Kris Van Hees
PATCH 1/1 tools/dtrace: initial implementation of DTrace
This initial implementation of a tiny subset of DTrace functionality provides the following options:
dtrace [-lvV] [-b bufsz] -s script
-b set trace buffer size
-l list probes (only works with '-s script' for now) -s enable or list probes for the specified BPF program -V report DTrace API version
The patch comprises quite a bit of code due to DTrace requiring a few crucial components, even in its most basic form.
The code is structured around the command line interface implemented in dtrace.c. It provides option parsing and drives the three modes of operation that are currently implemented:
1. Report DTrace API version information. Report the version information and terminate.
2. List probes in BPF programs.
Initialize the list of probes that DTrace recognizes, load BPF programs, parse all BPF ELF section names, resolve them into known probes, and emit the probe names. Then terminate.
3. Load BPF programs and collect tracing data.
Initialize the list of probes that DTrace recognizes, load BPF programs and attach them to their corresponding probes, set up perf event output buffers, and start processing tracing data.
This implementation makes extensive use of BPF (handled by dt_bpf.c) and the perf event output ring buffer (handled by dt_buffer.c). DTrace-style probe handling (dt_probe.c) offers an interface to probes that hides the implementation details of the individual probe types by provider (dt_fbt.c and dt_syscall.c). Probe lookup by name uses a hashtable implementation (dt_hash.c). The dt_utils.c code populates a list of online CPU ids, so we know what CPUs we can obtain tracing data from.
Building the tool is trivial because its only dependency (libbpf) is in the kernel tree under tools/lib/bpf. A simple 'make' in the tools/dtrace directory suffices.
The 'dtrace' executable needs to run as root because BPF programs cannot be loaded by non-root users.
--- MAINTAINERS | 6 + tools/dtrace/Makefile | 88 ++++++++++ tools/dtrace/bpf_sample.c | 145 ++++++++++++++++ tools/dtrace/dt_bpf.c | 188 +++++++++++++++++++++ tools/dtrace/dt_buffer.c | 331 +++++++++++++++++++++++++++++++++++++ tools/dtrace/dt_fbt.c | 201 ++++++++++++++++++++++ tools/dtrace/dt_hash.c | 211 +++++++++++++++++++++++ tools/dtrace/dt_probe.c | 230 ++++++++++++++++++++++++++ tools/dtrace/dt_syscall.c | 179 ++++++++++++++++++++ tools/dtrace/dt_utils.c | 132 +++++++++++++++ tools/dtrace/dtrace.c | 249 ++++++++++++++++++++++++++++ tools/dtrace/dtrace.h | 13 ++ tools/dtrace/dtrace_impl.h | 101 +++++++++++ 13 files changed, 2074 insertions(+) create mode 100644 tools/dtrace/Makefile create mode 100644 tools/dtrace/bpf_sample.c create mode 100644 tools/dtrace/dt_bpf.c create mode 100644 tools/dtrace/dt_buffer.c create mode 100644 tools/dtrace/dt_fbt.c create mode 100644 tools/dtrace/dt_hash.c create mode 100644 tools/dtrace/dt_probe.c create mode 100644 tools/dtrace/dt_syscall.c create mode 100644 tools/dtrace/dt_utils.c create mode 100644 tools/dtrace/dtrace.c create mode 100644 tools/dtrace/dtrace.h create mode 100644 tools/dtrace/dtrace_impl.h
MAINTAINERS +6 -0
@@ -5474,6 +5474,12 @@ W: https://linuxtv.org S: Odd Fixes F: drivers/media/pci/dt3155/ +DTRACE +M: Kris Van Hees <[email protected]> +L: [email protected] +S: Maintained +F: tools/dtrace/ + DVB_USB_AF9015 MEDIA DRIVER M: Antti Palosaari <[email protected]> L: [email protected]
tools/dtrace/Makefile new file +88 -0
88 lines not on this page · raw message
tools/dtrace/bpf_sample.c new file +145 -0
145 lines not on this page · raw message
tools/dtrace/dt_bpf.c new file +188 -0
188 lines not on this page · raw message
tools/dtrace/dt_buffer.c new file +331 -0
331 lines not on this page · raw message
tools/dtrace/dt_fbt.c new file +201 -0
201 lines not on this page · raw message
tools/dtrace/dt_hash.c new file +211 -0
211 lines not on this page · raw message
tools/dtrace/dt_probe.c new file +230 -0
230 lines not on this page · raw message
tools/dtrace/dt_syscall.c new file +179 -0
179 lines not on this page · raw message
tools/dtrace/dt_utils.c new file +132 -0
132 lines not on this page · raw message
tools/dtrace/dtrace.c new file +249 -0
249 lines not on this page · raw message
tools/dtrace/dtrace.h new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* +* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. +*/ +#ifndef _UAPI_LINUX_DTRACE_H +#define _UAPI_LINUX_DTRACE_H + +struct dt_bpf_context { +u32 probe_id; +u64 argv[10 ]; +}; + +#endif /* _UAPI_LINUX_DTRACE_H */
tools/dtrace/dtrace_impl.h new file +101 -0
101 lines not on this page · raw message
Signature · 1 line
2.20.1
Quoted 8 lines · Kris Van Hees
tools/dtrace/dt_buffer.c new file +8
-0+/* +* Read the data_head offset from the header page of the ring buffer. The +* argument is declared 'volatile' because it references a memory mapped page +* that the kernel may be writing to while we access it here. +*/ +static u64 read_rb_head (volatile struct perf_event_mmap_page *rb_page) +{ +u64 head = rb_page->data_head;
tools/dtrace/dt_buffer.c line 212
Quoted 10 lines
tools/dtrace/dt_buffer.c new file +10
-0+ + asmvolatile ("" :::"memory" ); + +return head; +} + +/* +* Write the data_tail offset in the header page of the ring buffer. The +* argument is declared 'volatile' because it references a memory mapped page +* that the kernel may be writing to while we access it here.
s/writing/reading/
tools/dtrace/dt_buffer.c line 220
Quoted 8 lines
+ */
tools/dtrace/dt_buffer.c new file +7
-0+static void write_rb_tail (volatile struct perf_event_mmap_page *rb_page, +u64 tail) +{ + asmvolatile ("" :::"memory" ); + + rb_page->data_tail = tail; +}
That volatile usage is atrocious (kernel style would have you use {READ,WRITE}_ONCE()). Also your comments fail to mark these as load_acquire and store_release. And by only using a compiler barrier you're hard assuming TSO, which is somewhat fragile at best.
Alternatively, you can use the C11 bits and write:
return __atomic_load_n(&rb_page->data_head, __ATOMIC_ACQUIRE); __atomic_store_n(&rb_page->data_tail, tail, __ATOMIC_RELEASE);
Quoted 59 lines
tools/dtrace/dt_buffer.c new file +59
-0+/* +* Process and output the probe data at the supplied address. +*/ +static int output_event (int cpu,u64 *buf) +{ +u8 *data = (u8 *)buf; +struct perf_event_header *hdr; + + hdr = (struct perf_event_header *)data; + data +=sizeof (struct perf_event_header ); + +if (hdr->type ==PERF_RECORD_SAMPLE ) { +u8 *ptr = data; +u32 i, size, probe_id; + +/* +* struct { +* struct perf_event_header header; +* u32 size; +* u32 probe_id; +* u32 gap; +* u64 data[n]; +* } +* and data points to the 'size' member at this point. +*/ +if (ptr > (u8 *)buf + hdr->size) { +fprintf (stderr,"BAD: corrupted sample header\n" ); + goto out; + } + + size = *(u32 *)data; + data +=sizeof (size); + ptr +=sizeof (size) + size; +if (ptr != (u8 *)buf + hdr->size) { +fprintf (stderr,"BAD: invalid sample size\n" ); + goto out; + } + + probe_id = *(u32 *)data; + data +=sizeof (probe_id); + size -=sizeof (probe_id); + data +=sizeof (u32);/* skip 32-bit gap */ + size -=sizeof (u32); + buf = (u64 *)data; + +printf ("%3d %6d " , cpu, probe_id); +for (i =0 , size /=sizeof (u64); i < size; i++) +printf ("%#016lx " , buf[i]); +printf ("\n" ); + }else if (hdr->type ==PERF_RECORD_LOST ) { +u64 lost; + +/* +* struct { +* struct perf_event_header header; +* u64 id; +* u64 lost; +* } +* and data points to the 'id' member at this point.
tools/dtrace/dt_buffer.c line 290
Quoted 10 lines
tools/dtrace/dt_buffer.c new file +10
-0+*/ + lost = *(u64 *)(data +sizeof (u64)); + +printf ("[%ld probes dropped]\n" , lost); + }else +fprintf (stderr,"UNKNOWN: record type %d\n" , hdr->type); + +out: +return hdr->size; +}
I see a distinct lack of wrapping support. AFAICT when buf+hdr->size wraps you're doing out-of-bounds accesses.
Quoted 12 lines
tools/dtrace/dt_buffer.c new file +12
-0+/* +* Process the available probe data in the given buffer. +*/ +static void process_data (struct dtrace_buffer *buf) +{ +/* This is volatile because the kernel may be updating the content. */ +volatile struct perf_event_mmap_page *rb_page = buf->base; +u8 *base = (u8 *)buf->base + + buf->page_size; +u64 head =read_rb_head (rb_page); + +while (rb_page->data_tail != head) {
tools/dtrace/dt_buffer.c line 313
Quoted 10 lines
tools/dtrace/dt_buffer.c new file +10
-0+u64 tail = rb_page->data_tail; +u64 *ptr = (u64 *)(base + tail % buf->data_size); +int len; + + len =output_event (buf->cpu, ptr); + +write_rb_tail (rb_page, tail + len); + head =read_rb_head (rb_page); + } +}
more volatile yuck.
Also:
for (;;) {
head = __atomic_load_n(&rb_page->data_head, __ATOMIC_ACQUIRE);
tail = __atomic_load_n(&rb_page->data_tail, __ATOMIC_RELAXED);
if (head == tail)
break;
do {
hdr = buf->base + (tail & ((1UL << buf->data_shift) - 1));
if ((tail >> buf->data_shift) !=
((tail + hdr->size) >> buf->data_shift))
/* handle wrap case */
else
/* normal case */
tail += hdr->size;
} while (tail != head);
__atomic_store_n(&rb_page->data_tail, tail, __ATOMIC_RELEASE);
}Or something.
Quoted 7 lines
tools/dtrace/dt_buffer.c new file +7
-0+/* +* Wait for data to become available in any of the buffers. +*/ +int dt_buffer_poll (int epoll_fd,int timeout) +{ +struct epoll_event events[dt_numcpus]; +int i, cnt;
tools/dtrace/dt_buffer.c line 331
Quoted 10 lines
tools/dtrace/dt_buffer.c new file +10
-0+ + cnt =epoll_wait (epoll_fd, events, dt_numcpus, timeout); +if (cnt <0 ) +return -errno; + +for (i =0 ; i < cnt; i++) +process_data ((struct dtrace_buffer *)events[i].data.ptr); + +return cnt; +}
Or make sure to read on the CPU by having a poll thread per CPU, then you can do away with the memory barriers.
Quoted 11 lines · Kris Van Hees
tools/dtrace/dt_bpf.c new file +11
-0+int dt_bpf_attach (int event_id,int bpf_fd) +{ +int event_fd; +int rc; +struct perf_event_attr attr = {}; + + attr.type =PERF_TYPE_TRACEPOINT ; + attr.sample_type =PERF_SAMPLE_RAW ; + attr.sample_period =1 ; + attr.wakeup_events =1 ; + attr.config = event_id;
tools/dtrace/dt_bpf.c line 174
Quoted 10 lines
tools/dtrace/dt_bpf.c new file +10
-0+ +/* Register the event (based on its id), and obtain a fd. */ + event_fd =perf_event_open (&attr,-1 ,0 ,-1 ,0 ); +if (event_fd <0 ) { +perror ("sys_perf_event_open" ); +return -1 ; + } + +/* Enable the probe. */ + rc =ioctl (event_fd,PERF_EVENT_IOC_ENABLE ,0 );
AFAICT you didn't use attr.disabled = 1, so this IOC_ENABLE is completely superfluous.
Quoted 14 lines
tools/dtrace/dt_bpf.c new file +14
-0+if (rc <0 ) { +perror ("PERF_EVENT_IOC_ENABLE" ); +return -1 ; + } + +/* Associate the BPF program with the event. */ + rc =ioctl (event_fd,PERF_EVENT_IOC_SET_BPF , bpf_fd); +if (rc <0 ) { +perror ("PERF_EVENT_IOC_SET_BPF" ); +return -1 ; + } + +return 0 ; +}
Quoted 4 lines · Kris Van Hees
This initial implementation of a tiny subset of DTrace functionality provides the following options:
Quoted 8 lines
dtrace [-lvV] [-b bufsz] -s script
-b set trace buffer size
-l list probes (only works with '-s script' for now) -s enable or list probes for the specified BPF program -V report DTrace API versionThe patch comprises quite a bit of code due to DTrace requiring a few crucial components, even in its most basic form.
This patchset has moved from adding tests (which actually belong in selftests, not tools/dtrace), to the start of adding a giant tracer to the kernel code base.
First, in some ways you're doing this for me -- I've been the number one user of DTrace for 15 years -- and thanks, but no thanks. I don't need this anymore. I needed this 6 years ago, and I would have helped you build it, but in the meantime Linux has built something better, built from the ground up for BPF: bpftrace.
Second, you argued that DTrace was needed because of speculative tracing (a feature I never used), as you had customers who wanted it. Those customers aren't going to be happy with this initial tiny implementation of DTrace -- this is really the start of adding a large and complex tracer to the kernel.
We've all been working under the assumption that these user-space tracers did not belong in the kernel, and so far that's been working fine for us. Is it now open season for tracers in the kernel? Let's have:
tools/bpftracetools/plytools/systemtaptools/LTTngtools/sysdigtools/ktap
etc
Yes, that's ridiculous. If there's only going to be one, let's have the best one, bpftrace. We'll offer a version that is GPL, C, and has no dependencies.
But it would be news to us all that we're allowed to have even one.
There are more things that don't make sense about this, but I'll stop here for now.
Brendan
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Kris Van Hees
tools/dtrace/dt_bpf.c new file +16
-0+int dt_bpf_attach (int event_id,int bpf_fd) +{ +int event_fd; +int rc; +struct perf_event_attr attr = {}; + + attr.type =PERF_TYPE_TRACEPOINT ; + attr.sample_type =PERF_SAMPLE_RAW ; + attr.sample_period =1 ; + attr.wakeup_events =1 ; + attr.config = event_id; + +/* Register the event (based on its id), and obtain a fd. */ + event_fd =perf_event_open (&attr,-1 ,0 ,-1 ,0 ); +if (event_fd <0 ) { +perror ("sys_perf_event_open" );
Quoted 8 lines
tools/dtrace/dt_bpf.c line 174
Quoted 5 lines
tools/dtrace/dt_bpf.c new file +5
-0+return -1 ; + } + +/* Enable the probe. */ + rc =ioctl (event_fd,PERF_EVENT_IOC_ENABLE ,0 );AFAICT you didn't use attr.disabled = 1, so this
IOC_ENABLEis completely superfluous.
Oh yes, good point (and the same applies to the dt_buffer.c code where I set up the events that own each buffer - no point in doing an explicit enable there eiteher).
Thanks for catching this!
Quoted 14 lines
Quoted 14 lines
tools/dtrace/dt_bpf.c new file +14
-0+if (rc <0 ) { +perror ("PERF_EVENT_IOC_ENABLE" ); +return -1 ; + } + +/* Associate the BPF program with the event. */ + rc =ioctl (event_fd,PERF_EVENT_IOC_SET_BPF , bpf_fd); +if (rc <0 ) { +perror ("PERF_EVENT_IOC_SET_BPF" ); +return -1 ; + } + +return 0 ; +}
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Kris Van Hees
tools/dtrace/dt_buffer.c new file +12
-0+/* +* Read the data_head offset from the header page of the ring buffer. The +* argument is declared 'volatile' because it references a memory mapped page +* that the kernel may be writing to while we access it here. +*/ +static u64 read_rb_head (volatile struct perf_event_mmap_page *rb_page) +{ +u64 head = rb_page->data_head; + + asmvolatile ("" :::"memory" ); + +return head;
Quoted 8 lines
tools/dtrace/dt_buffer.c line 212
Quoted 6 lines
tools/dtrace/dt_buffer.c new file +6
-0+} + +/* +* Write the data_tail offset in the header page of the ring buffer. The +* argument is declared 'volatile' because it references a memory mapped page +* that the kernel may be writing to while we access it here. s/writing/reading/
Thanks!
Quoted 14 lines
tools/dtrace/dt_buffer.c line 220
Quoted 8 lines
+ */
tools/dtrace/dt_buffer.c new file +7
-0+static void write_rb_tail (volatile struct perf_event_mmap_page *rb_page, +u64 tail) +{ + asmvolatile ("" :::"memory" ); + + rb_page->data_tail = tail; +}That volatile usage is atrocious (kernel style would have you use {READ,WRITE}_ONCE()). Also your comments fail to mark these as
load_acquireandstore_release. And by only using a compiler barrier you're hard assuming TSO, which is somewhat fragile at best.
Quoted 5 lines
Alternatively, you can use the C11 bits and write:
return __atomic_load_n(&rb_page->data_head, __ATOMIC_ACQUIRE); __atomic_store_n(&rb_page->data_tail, tail, __ATOMIC_RELEASE);
Perhaps I should just use ring_buffer_read_head() and ring_buffer_write_tail() since they are provided in tools/include/linux/ring_buffer.h? I expect that would be even more preferable over __atomic_load_n() and __atomic_store_n()?
Quoted 64 lines
Quoted 64 lines
tools/dtrace/dt_buffer.c new file +64
-0+/* +* Process and output the probe data at the supplied address. +*/ +static int output_event (int cpu,u64 *buf) +{ +u8 *data = (u8 *)buf; +struct perf_event_header *hdr; + + hdr = (struct perf_event_header *)data; + data +=sizeof (struct perf_event_header ); + +if (hdr->type ==PERF_RECORD_SAMPLE ) { +u8 *ptr = data; +u32 i, size, probe_id; + +/* +* struct { +* struct perf_event_header header; +* u32 size; +* u32 probe_id; +* u32 gap; +* u64 data[n]; +* } +* and data points to the 'size' member at this point. +*/ +if (ptr > (u8 *)buf + hdr->size) { +fprintf (stderr,"BAD: corrupted sample header\n" ); + goto out; + } + + size = *(u32 *)data; + data +=sizeof (size); + ptr +=sizeof (size) + size; +if (ptr != (u8 *)buf + hdr->size) { +fprintf (stderr,"BAD: invalid sample size\n" ); + goto out; + } + + probe_id = *(u32 *)data; + data +=sizeof (probe_id); + size -=sizeof (probe_id); + data +=sizeof (u32);/* skip 32-bit gap */ + size -=sizeof (u32); + buf = (u64 *)data; + +printf ("%3d %6d " , cpu, probe_id); +for (i =0 , size /=sizeof (u64); i < size; i++) +printf ("%#016lx " , buf[i]); +printf ("\n" ); + }else if (hdr->type ==PERF_RECORD_LOST ) { +u64 lost; + +/* +* struct { +* struct perf_event_header header; +* u64 id; +* u64 lost; +* } +* and data points to the 'id' member at this point. +*/ + lost = *(u64 *)(data +sizeof (u64)); + +printf ("[%ld probes dropped]\n" , lost); + }else
Quoted 8 lines
tools/dtrace/dt_buffer.c line 290
Quoted 5 lines
tools/dtrace/dt_buffer.c new file +5
-0+fprintf (stderr,"UNKNOWN: record type %d\n" , hdr->type); + +out: +return hdr->size; +}I see a distinct lack of wrapping support. AFAICT when buf+hdr->size wraps you're doing out-of-bounds accesses.
Yes, that is correct. I'm actually trying to figure out why it didn't actually cause a SEGV when I tested this because I'm clearly reading past the end of the mmap'd memory. Thank you for noticing this - I was trying to be too minimal in the code I was putting out and really didn't pay attention to this.
Fixed in the V2 I am preparing.
Quoted 48 lines
Quoted 12 lines
tools/dtrace/dt_buffer.c new file +12
-0+/* +* Process the available probe data in the given buffer. +*/ +static void process_data (struct dtrace_buffer *buf) +{ +/* This is volatile because the kernel may be updating the content. */ +volatile struct perf_event_mmap_page *rb_page = buf->base; +u8 *base = (u8 *)buf->base + + buf->page_size; +u64 head =read_rb_head (rb_page); + +while (rb_page->data_tail != head) {tools/dtrace/dt_buffer.c line 313
Quoted 10 lines
tools/dtrace/dt_buffer.c new file +10
-0+u64 tail = rb_page->data_tail; +u64 *ptr = (u64 *)(base + tail % buf->data_size); +int len; + + len =output_event (buf->cpu, ptr); + +write_rb_tail (rb_page, tail + len); + head =read_rb_head (rb_page); + } +}more volatile yuck.
Also:
for (;;) { head = __atomic_load_n(&rb_page->data_head, __ATOMIC_ACQUIRE); tail = __atomic_load_n(&rb_page->data_tail, __ATOMIC_RELAXED); if (head == tail) break; do { hdr = buf->base + (tail & ((1UL << buf->data_shift) - 1)); if ((tail >> buf->data_shift) != ((tail + hdr->size) >> buf->data_shift)) /* handle wrap case */ else /* normal case */ tail += hdr->size; } while (tail != head); __atomic_store_n(&rb_page->data_tail, tail, __ATOMIC_RELEASE); }
Quoted 1 line
Or something.
Thank you for this suggestion. As mentioned above, I lean towards using the provided ring_buffer_(read_head,write_tail) implementations since that is the 'other end' of the ring buffer head/tail mechanism that is going to be kept in sync with any changes that might happen on the kernel side, right?
Quoted 12 lines
Quoted 12 lines
tools/dtrace/dt_buffer.c new file +12
-0+/* +* Wait for data to become available in any of the buffers. +*/ +int dt_buffer_poll (int epoll_fd,int timeout) +{ +struct epoll_event events[dt_numcpus]; +int i, cnt; + + cnt =epoll_wait (epoll_fd, events, dt_numcpus, timeout); +if (cnt <0 ) +return -errno; +
Quoted 8 lines
tools/dtrace/dt_buffer.c line 331
Quoted 5 lines
tools/dtrace/dt_buffer.c new file +5
-0+for (i =0 ; i < cnt; i++) +process_data ((struct dtrace_buffer *)events[i].data.ptr); + +return cnt; +}Or make sure to read on the CPU by having a poll thread per CPU, then you can do away with the memory barriers.
That is definitely something for the todo list for future optimizations.
Thanks for your review and code suggestions.
Kris
Em Wed, Jul 03, 2019 at 08:14:30PM -0700, Kris Van Hees escreveu:
Quoted 118 lines
This initial implementation of a tiny subset of DTrace functionality provides the following options:
dtrace [-lvV] [-b bufsz] -s script
-b set trace buffer size
-l list probes (only works with '-s script' for now) -s enable or list probes for the specified BPF program -V report DTrace API versionThe patch comprises quite a bit of code due to DTrace requiring a few crucial components, even in its most basic form.
The code is structured around the command line interface implemented in
dtrace.c. It provides option parsing and drives the three modes of operation that are currently implemented:1. Report DTrace API version information. Report the version information and terminate.
2. List probes in BPF programs.
Initialize the list of probes that DTrace recognizes, load BPF programs, parse all BPF ELF section names, resolve them into known probes, and emit the probe names. Then terminate.3. Load BPF programs and collect tracing data.
Initialize the list of probes that DTrace recognizes, load BPF programs and attach them to their corresponding probes, set up perf event output buffers, and start processing tracing data.This implementation makes extensive use of BPF (handled by
dt_bpf.c) and the perf event output ring buffer (handled bydt_buffer.c). DTrace-style probe handling (dt_probe.c) offers an interface to probes that hides the implementation details of the individual probe types by provider (dt_fbt.canddt_syscall.c). Probe lookup by name uses a hashtable implementation (dt_hash.c). Thedt_utils.ccode populates a list of online CPU ids, so we know what CPUs we can obtain tracing data from.Building the tool is trivial because its only dependency (libbpf) is in the kernel tree under
tools/lib/bpf. A simple 'make' in thetools/dtracedirectory suffices.The 'dtrace' executable needs to run as root because BPF programs cannot be loaded by non-root users.
Signed-off-by Kris Van Hees <[email protected]>Reviewed-by David Mc Lean <[email protected]>Reviewed-by Eugene Loh <[email protected]>--- MAINTAINERS | 6 + tools/dtrace/Makefile | 88 ++++++++++ tools/dtrace/bpf_sample.c | 145 ++++++++++++++++ tools/dtrace/dt_bpf.c | 188 +++++++++++++++++++++ tools/dtrace/dt_buffer.c | 331 +++++++++++++++++++++++++++++++++++++ tools/dtrace/dt_fbt.c | 201 ++++++++++++++++++++++ tools/dtrace/dt_hash.c | 211 +++++++++++++++++++++++ tools/dtrace/dt_probe.c | 230 ++++++++++++++++++++++++++ tools/dtrace/dt_syscall.c | 179 ++++++++++++++++++++ tools/dtrace/dt_utils.c | 132 +++++++++++++++ tools/dtrace/dtrace.c | 249 ++++++++++++++++++++++++++++ tools/dtrace/dtrace.h | 13 ++ tools/dtrace/dtrace_impl.h | 101 +++++++++++ 13 files changed, 2074 insertions(+) create mode 100644 tools/dtrace/Makefile create mode 100644 tools/dtrace/bpf_sample.c create mode 100644 tools/dtrace/dt_bpf.c create mode 100644 tools/dtrace/dt_buffer.c create mode 100644 tools/dtrace/dt_fbt.c create mode 100644 tools/dtrace/dt_hash.c create mode 100644 tools/dtrace/dt_probe.c create mode 100644 tools/dtrace/dt_syscall.c create mode 100644 tools/dtrace/dt_utils.c create mode 100644 tools/dtrace/dtrace.c create mode 100644 tools/dtrace/dtrace.h create mode 100644 tools/dtrace/dtrace_impl.hMAINTAINERS +6
-0@@ -5474,6 +5474,12 @@ W: https://linuxtv.org S: Odd Fixes F: drivers/media/pci/dt3155/ +DTRACE +M: Kris Van Hees <[email protected]> +L: [email protected] +S: Maintained +F: tools/dtrace/ + DVB_USB_AF9015 MEDIA DRIVER M: Antti Palosaari <[email protected]> L: [email protected]tools/dtrace/Makefile new file +20
-0@@ -0,0 +1,88 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# This Makefile is based on samples/bpf. +# +# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + +DT_VERSION := 2.0.0 +DT_GIT_VERSION := $(shell git rev-parse HEAD 2>/dev/null || \ + echo Unknown) + +DTRACE_PATH ?= $(abspath $(srctree)/$(src)) +TOOLS_PATH := $(DTRACE_PATH)/.. +SAMPLES_PATH := $(DTRACE_PATH)/../../samples + +hostprogs-y := dtrace + +LIBBPF := $(TOOLS_PATH)/lib/bpf/libbpf.a +OBJS := dt_bpf.o dt_buffer.o dt_utils.o dt_probe.o \ + dt_hash.o \ + dt_fbt.o dt_syscall.o
tools/dtrace/Makefile line 30
Quoted 10 lines
tools/dtrace/Makefile new file +10
-0+ +dtrace-objs := $(OBJS) dtrace.o + +always := $(hostprogs-y) +always += bpf_sample.o + +KBUILD_HOSTCFLAGS += -DDT_VERSION=\"$(DT_VERSION)\" +KBUILD_HOSTCFLAGS += -DDT_GIT_VERSION=\"$(DT_GIT_VERSION)\" +KBUILD_HOSTCFLAGS += -I$(srctree)/tools/lib +KBUILD_HOSTCFLAGS += -I$(srctree)/tools/perf
Interesting, what are you using from tools/perf/? So that we can move to tools/{include,lib,arch}.
Quoted 48 lines
tools/dtrace/Makefile new file +48
-0+KBUILD_HOSTCFLAGS += -I$(srctree)/tools/include/uapi +KBUILD_HOSTCFLAGS += -I$(srctree)/tools/include/ +KBUILD_HOSTCFLAGS += -I$(srctree)/usr/include + +KBUILD_HOSTLDLIBS := $(LIBBPF) -lelf + +LLC ?= llc +CLANG ?= clang +LLVM_OBJCOPY ?= llvm-objcopy + +ifdef CROSS_COMPILE +HOSTCC = $(CROSS_COMPILE)gcc +CLANG_ARCH_ARGS = -target $(ARCH) +endif + +all: + $(MAKE) -C ../../ $(CURDIR)/ DTRACE_PATH=$(CURDIR) + +clean: + $(MAKE) -C ../../ M=$(CURDIR) clean + @rm -f *~ + +$(LIBBPF): FORCE + $(MAKE) -C $(dir $@) RM='rm -rf' LDFLAGS= srctree=$(DTRACE_PATH)/../../ O= + +FORCE: + +.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC) + +verify_cmds: $(CLANG) $(LLC) + @for TOOL in $^ ; do \ + if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \ + echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\ + exit 1; \ + else true; fi; \ + done + +verify_target_bpf: verify_cmds + @if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \ + echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\ + echo " NOTICE: LLVM version >= 3.7.1 required" ;\ + exit 2; \ + else true; fi + +$(DTRACE_PATH)/*.c: verify_target_bpf $(LIBBPF) +$(src)/*.c: verify_target_bpf $(LIBBPF) + +$(obj)/%.o: $(src)/%.c
tools/dtrace/Makefile line 88
Quoted 10 lines
tools/dtrace/Makefile new file +10
-0+ @echo " CLANG-bpf " $@ + $(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) -I$(obj) \ + -I$(srctree)/tools/testing/selftests/bpf/ \ + -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \ + -D__TARGET_ARCH_$(ARCH) -Wno-compare-distinct-pointer-types \ + -Wno-gnu-variable-sized-type-not-at-end \ + -Wno-address-of-packed-member -Wno-tautological-compare \ + -Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \ + -I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \ + -O2 -emit-llvm -c $< -o -| $(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@
We have the above in tools/perf/util/llvm-utils.c, perhaps we need to move it to some place in lib/ to share?
Quoted 57 lines
tools/dtrace/bpf_sample.c new file +51
-0@@ -0,0 +1,145 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* This sample DTrace BPF tracing program demonstrates how actions can be +* associated with different probe types. +* +* The kprobe/ksys_write probe is a Function Boundary Tracing (FBT) entry probe +* on the ksys_write(fd, buf, count) function in the kernel. Arguments to the +* function can be retrieved from the CPU registers (struct pt_regs). +* +* The tracepoint/syscalls/sys_enter_write probe is a System Call entry probe +* for the write(d, buf, count) system call. Arguments to the system call can +* be retrieved from the tracepoint data passed to the BPF program as context +* struct syscall_data) when the probe fires. +* +* The BPF program associated with each probe prepares a DTrace BPF context +* (struct dt_bpf_context) that stores the probe ID and up to 10 arguments. +* Only 3 arguments are used in this sample. Then the prorgams call a shared +* BPF function (bpf_action) that implements the actual action to be taken when +* a probe fires. It prepares a data record to be stored in the tracing buffer +* and submits it to the buffer. The data in the data record is obtained from +* the DTrace BPF context. +* +* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. +*/ +#include <uapi/linux/bpf.h> +#include <linux/ptrace.h> +#include <linux/version.h> +#include <uapi/linux/unistd.h> +#include "bpf_helpers.h" + +#include "dtrace.h" + +struct syscall_data { +struct pt_regs *regs; +long syscall_nr; +long arg[6 ]; +}; + +struct bpf_map_def SEC("maps") buffers = { + .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY, + .key_size = sizeof(u32), + .value_size = sizeof(u32), + .max_entries = NR_CPUS, +}; + +#if defined(__amd64) +# define GET_REGS_ARG0 (regs) ((regs)->di) +# define GET_REGS_ARG1 (regs) ((regs)->si) +# define GET_REGS_ARG2 (regs) ((regs)->dx) +# define GET_REGS_ARG3 (regs) ((regs)->cx) +# define GET_REGS_ARG4 (regs) ((regs)->r8)
tools/dtrace/bpf_sample.c line 61
Quoted 10 lines
tools/dtrace/bpf_sample.c new file +10
-0+# define GET_REGS_ARG5 (regs) ((regs)->r9) +#else +# warning Argument retrieval from pt_regs is not supported yet on this arch. +# define GET_REGS_ARG0 (regs) 0 +# define GET_REGS_ARG1 (regs) 0 +# define GET_REGS_ARG2 (regs) 0 +# define GET_REGS_ARG3 (regs) 0 +# define GET_REGS_ARG4 (regs) 0 +# define GET_REGS_ARG5 (regs) 0 +#endif
We have this in tools/testing/selftests/bpf/bpf_helpers.h, probably need to move to some other place in tools/include/ where this can be shared.
- Arnaldo
Quoted 124 lines · Arnaldo Carvalho de Melo
Em Wed, Jul 03, 2019 at 08:14:30PM -0700, Kris Van Hees escreveu:
Quoted 123 lines
This initial implementation of a tiny subset of DTrace functionality provides the following options:
dtrace [-lvV] [-b bufsz] -s script
-b set trace buffer size
-l list probes (only works with '-s script' for now) -s enable or list probes for the specified BPF program -V report DTrace API versionThe patch comprises quite a bit of code due to DTrace requiring a few crucial components, even in its most basic form.
The code is structured around the command line interface implemented in
dtrace.c. It provides option parsing and drives the three modes of operation that are currently implemented:1. Report DTrace API version information. Report the version information and terminate.
2. List probes in BPF programs.
Initialize the list of probes that DTrace recognizes, load BPF programs, parse all BPF ELF section names, resolve them into known probes, and emit the probe names. Then terminate.3. Load BPF programs and collect tracing data.
Initialize the list of probes that DTrace recognizes, load BPF programs and attach them to their corresponding probes, set up perf event output buffers, and start processing tracing data.This implementation makes extensive use of BPF (handled by
dt_bpf.c) and the perf event output ring buffer (handled bydt_buffer.c). DTrace-style probe handling (dt_probe.c) offers an interface to probes that hides the implementation details of the individual probe types by provider (dt_fbt.canddt_syscall.c). Probe lookup by name uses a hashtable implementation (dt_hash.c). Thedt_utils.ccode populates a list of online CPU ids, so we know what CPUs we can obtain tracing data from.Building the tool is trivial because its only dependency (libbpf) is in the kernel tree under
tools/lib/bpf. A simple 'make' in thetools/dtracedirectory suffices.The 'dtrace' executable needs to run as root because BPF programs cannot be loaded by non-root users.
Signed-off-by Kris Van Hees <[email protected]>Reviewed-by David Mc Lean <[email protected]>Reviewed-by Eugene Loh <[email protected]>--- MAINTAINERS | 6 + tools/dtrace/Makefile | 88 ++++++++++ tools/dtrace/bpf_sample.c | 145 ++++++++++++++++ tools/dtrace/dt_bpf.c | 188 +++++++++++++++++++++ tools/dtrace/dt_buffer.c | 331 +++++++++++++++++++++++++++++++++++++ tools/dtrace/dt_fbt.c | 201 ++++++++++++++++++++++ tools/dtrace/dt_hash.c | 211 +++++++++++++++++++++++ tools/dtrace/dt_probe.c | 230 ++++++++++++++++++++++++++ tools/dtrace/dt_syscall.c | 179 ++++++++++++++++++++ tools/dtrace/dt_utils.c | 132 +++++++++++++++ tools/dtrace/dtrace.c | 249 ++++++++++++++++++++++++++++ tools/dtrace/dtrace.h | 13 ++ tools/dtrace/dtrace_impl.h | 101 +++++++++++ 13 files changed, 2074 insertions(+) create mode 100644 tools/dtrace/Makefile create mode 100644 tools/dtrace/bpf_sample.c create mode 100644 tools/dtrace/dt_bpf.c create mode 100644 tools/dtrace/dt_buffer.c create mode 100644 tools/dtrace/dt_fbt.c create mode 100644 tools/dtrace/dt_hash.c create mode 100644 tools/dtrace/dt_probe.c create mode 100644 tools/dtrace/dt_syscall.c create mode 100644 tools/dtrace/dt_utils.c create mode 100644 tools/dtrace/dtrace.c create mode 100644 tools/dtrace/dtrace.h create mode 100644 tools/dtrace/dtrace_impl.hMAINTAINERS +6
-0@@ -5474,6 +5474,12 @@ W: https://linuxtv.org S: Odd Fixes F: drivers/media/pci/dt3155/ +DTRACE +M: Kris Van Hees <[email protected]> +L: [email protected] +S: Maintained +F: tools/dtrace/ + DVB_USB_AF9015 MEDIA DRIVER M: Antti Palosaari <[email protected]> L: [email protected]tools/dtrace/Makefile new file +25
-0@@ -0,0 +1,88 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# This Makefile is based on samples/bpf. +# +# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + +DT_VERSION := 2.0.0 +DT_GIT_VERSION := $(shell git rev-parse HEAD 2>/dev/null || \ + echo Unknown) + +DTRACE_PATH ?= $(abspath $(srctree)/$(src)) +TOOLS_PATH := $(DTRACE_PATH)/.. +SAMPLES_PATH := $(DTRACE_PATH)/../../samples + +hostprogs-y := dtrace + +LIBBPF := $(TOOLS_PATH)/lib/bpf/libbpf.a +OBJS := dt_bpf.o dt_buffer.o dt_utils.o dt_probe.o \ + dt_hash.o \ + dt_fbt.o dt_syscall.o + +dtrace-objs := $(OBJS) dtrace.o + +always := $(hostprogs-y) +always += bpf_sample.o
Quoted 8 lines
tools/dtrace/Makefile line 30
Quoted 5 lines
tools/dtrace/Makefile new file +5
-0+ +KBUILD_HOSTCFLAGS += -DDT_VERSION=\"$(DT_VERSION)\" +KBUILD_HOSTCFLAGS += -DDT_GIT_VERSION=\"$(DT_GIT_VERSION)\" +KBUILD_HOSTCFLAGS += -I$(srctree)/tools/lib +KBUILD_HOSTCFLAGS += -I$(srctree)/tools/perfInteresting, what are you using from
tools/perf/? So that we can move totools/{include,lib,arch}.
This is my mistake... an earlier version of the code (as I was developing it) was using stuff from tools/perf, but that is no longer the case. Removing it.
Quoted 54 lines
Quoted 54 lines
tools/dtrace/Makefile new file +54
-0+KBUILD_HOSTCFLAGS += -I$(srctree)/tools/include/uapi +KBUILD_HOSTCFLAGS += -I$(srctree)/tools/include/ +KBUILD_HOSTCFLAGS += -I$(srctree)/usr/include + +KBUILD_HOSTLDLIBS := $(LIBBPF) -lelf + +LLC ?= llc +CLANG ?= clang +LLVM_OBJCOPY ?= llvm-objcopy + +ifdef CROSS_COMPILE +HOSTCC = $(CROSS_COMPILE)gcc +CLANG_ARCH_ARGS = -target $(ARCH) +endif + +all: + $(MAKE) -C ../../ $(CURDIR)/ DTRACE_PATH=$(CURDIR) + +clean: + $(MAKE) -C ../../ M=$(CURDIR) clean + @rm -f *~ + +$(LIBBPF): FORCE + $(MAKE) -C $(dir $@) RM='rm -rf' LDFLAGS= srctree=$(DTRACE_PATH)/../../ O= + +FORCE: + +.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC) + +verify_cmds: $(CLANG) $(LLC) + @for TOOL in $^ ; do \ + if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \ + echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\ + exit 1; \ + else true; fi; \ + done + +verify_target_bpf: verify_cmds + @if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \ + echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\ + echo " NOTICE: LLVM version >= 3.7.1 required" ;\ + exit 2; \ + else true; fi + +$(DTRACE_PATH)/*.c: verify_target_bpf $(LIBBPF) +$(src)/*.c: verify_target_bpf $(LIBBPF) + +$(obj)/%.o: $(src)/%.c + @echo " CLANG-bpf " $@ + $(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) -I$(obj) \ + -I$(srctree)/tools/testing/selftests/bpf/ \ + -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \ + -D__TARGET_ARCH_$(ARCH) -Wno-compare-distinct-pointer-types \ + -Wno-gnu-variable-sized-type-not-at-end \
Quoted 8 lines
tools/dtrace/Makefile line 88
Quoted 4 lines
tools/dtrace/Makefile new file +4
-0+ -Wno-address-of-packed-member -Wno-tautological-compare \ + -Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \ + -I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \ + -O2 -emit-llvm -c $< -o -| $(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@We have the above in
tools/perf/util/llvm-utils.c, perhaps we need to move it to some place inlib/to share?
Yes, if there is a way to put things like this in a central location so we can maintain a single copy that would be a good idea indeed.
Quoted 62 lines
Quoted 62 lines
tools/dtrace/bpf_sample.c new file +56
-0@@ -0,0 +1,145 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* This sample DTrace BPF tracing program demonstrates how actions can be +* associated with different probe types. +* +* The kprobe/ksys_write probe is a Function Boundary Tracing (FBT) entry probe +* on the ksys_write(fd, buf, count) function in the kernel. Arguments to the +* function can be retrieved from the CPU registers (struct pt_regs). +* +* The tracepoint/syscalls/sys_enter_write probe is a System Call entry probe +* for the write(d, buf, count) system call. Arguments to the system call can +* be retrieved from the tracepoint data passed to the BPF program as context +* struct syscall_data) when the probe fires. +* +* The BPF program associated with each probe prepares a DTrace BPF context +* (struct dt_bpf_context) that stores the probe ID and up to 10 arguments. +* Only 3 arguments are used in this sample. Then the prorgams call a shared +* BPF function (bpf_action) that implements the actual action to be taken when +* a probe fires. It prepares a data record to be stored in the tracing buffer +* and submits it to the buffer. The data in the data record is obtained from +* the DTrace BPF context. +* +* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. +*/ +#include <uapi/linux/bpf.h> +#include <linux/ptrace.h> +#include <linux/version.h> +#include <uapi/linux/unistd.h> +#include "bpf_helpers.h" + +#include "dtrace.h" + +struct syscall_data { +struct pt_regs *regs; +long syscall_nr; +long arg[6 ]; +}; + +struct bpf_map_def SEC("maps") buffers = { + .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY, + .key_size = sizeof(u32), + .value_size = sizeof(u32), + .max_entries = NR_CPUS, +}; + +#if defined(__amd64) +# define GET_REGS_ARG0 (regs) ((regs)->di) +# define GET_REGS_ARG1 (regs) ((regs)->si) +# define GET_REGS_ARG2 (regs) ((regs)->dx) +# define GET_REGS_ARG3 (regs) ((regs)->cx) +# define GET_REGS_ARG4 (regs) ((regs)->r8) +# define GET_REGS_ARG5 (regs) ((regs)->r9) +#else +# warning Argument retrieval from pt_regs is not supported yet on this arch. +# define GET_REGS_ARG0 (regs) 0 +# define GET_REGS_ARG1 (regs) 0
Quoted 8 lines
tools/dtrace/bpf_sample.c line 61
Quoted 5 lines
tools/dtrace/bpf_sample.c new file +5
-0+# define GET_REGS_ARG2 (regs) 0 +# define GET_REGS_ARG3 (regs) 0 +# define GET_REGS_ARG4 (regs) 0 +# define GET_REGS_ARG5 (regs) 0 +#endif We have this in
tools/testing/selftests/bpf/bpf_helpers.h, probably need to move to some other place intools/include/where this can be shared.
I should be using the ones in bpf_helpers (since I already include that anyway), and yes, if we can move that to a general use location under tools/include that would be a good idea.
Also, I jsut updated my code to use this and I added a PT_REGS_PARM6(x) for all the listed archs because I need to be able to get to up to 6 parameters rather than the supported 5. As far as I can see, all listed archs support argument passing of at least 6 arguments so this should be no problem.
Any objections?