Skip to content

Commit

Permalink
add 36-tracepoint-args-sched_switch-use-custom-struct
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Jan 14, 2024
1 parent 5d1b4a8 commit 79b0604
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 1 deletion.
1 change: 1 addition & 0 deletions 36-tracepoint-args-sched_switch-use-custom-struct/Makefile
18 changes: 18 additions & 0 deletions 36-tracepoint-args-sched_switch-use-custom-struct/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

https://mozillazg.com/2022/05/ebpf-libbpf-tracepoint-common-questions.html

## Usage

build:

```
$ make
```

run:

```
$ make run
$ make cat
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@


## Usage

build:

```
$ make
```

run:

```
$ make run
$ make cat
```

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"log"
"time"

"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/rlimit"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf ../main.bpf.c -- -I../ -I../output

func main() {
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatal(err)
}

objs := bpfObjects{}
if err := loadBpfObjects(&objs, nil); err != nil {
log.Fatal(err)
}
defer objs.Close()

tp, err := link.Tracepoint("sched", "sched_switch", objs.TracepointSchedSchedSwitch, nil)
if err != nil {
log.Println(err)
return
}
defer tp.Close()

log.Println("Waiting for events...")
time.Sleep(time.Minute * 1024)

}
29 changes: 29 additions & 0 deletions 36-tracepoint-args-sched_switch-use-custom-struct/main.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "vmlinux.h"

#include <bpf/bpf_core_read.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

// sudo cat /sys/kernel/debug/tracing/events/sched/sched_switch/format
struct sched_switch_args {
char _[8];
char prev_comm[16];
pid_t prev_pid;
int prev_prio;
long prev_state;
char next_comm[16];
pid_t next_pid;
int next_prio;
};

SEC("tracepoint/sched/sched_switch")
int tracepoint__sched__sched_switch(struct sched_switch_args *ctx) {
u32 prev_pid = (u32)ctx->prev_pid;
u32 next_pid = (u32)ctx->next_pid;

char fmt[] = "sched_switch %d -> %d\n";
bpf_trace_printk(fmt, sizeof(fmt), prev_pid, next_pid);
return 0;
}

char _license[] SEC("license") = "GPL";
32 changes: 32 additions & 0 deletions 36-tracepoint-args-sched_switch-use-custom-struct/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"time"

bpf "github.com/aquasecurity/libbpfgo"
)

func main() {
bpfModule, err := bpf.NewModuleFromFile("main.bpf.o")
if err != nil {
panic(err)
}
defer bpfModule.Close()

if err := bpfModule.BPFLoadObject(); err != nil {
panic(err)
}
prog, err := bpfModule.GetProgram("tracepoint__sched__sched_switch")
if err != nil {
panic(err)
}
if _, err := prog.AttachTracepoint("sched", "sched_switch"); err != nil {
panic(err)
}

for {
fmt.Println("Waiting...")
time.Sleep(10 * time.Second)
}
}
4 changes: 3 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Examples by program type:
| ``BPF_PROG_TYPE_SYSCALL`` | | ``syscall`` | |
+-------------------------------------------+----------------------------------------+----------------------------------+-----------------------+
| ``BPF_PROG_TYPE_TRACEPOINT`` | | ``tp+`` |`04`_ `07`_ `14`_ |
+ + +----------------------------------+ +
+ + +----------------------------------+`35`_ `36`_ +
| | | ``tracepoint+`` | |
+-------------------------------------------+----------------------------------------+----------------------------------+-----------------------+
| ``BPF_PROG_TYPE_TRACING`` | ``BPF_MODIFY_RETURN`` | ``fmod_ret+`` | |
Expand Down Expand Up @@ -212,6 +212,8 @@ Examples by program type:
.. _32: 32-fentry-hello
.. _33: 33-xdp-hello
.. _34: 34-iter-task-hello
.. _35: 35-tracepoint-args-use-custom-struct
.. _36: 36-tracepoint-args-sched_switch-use-custom-struct


https://mozillazg.com/tag/libbpf.html

0 comments on commit 79b0604

Please sign in to comment.