Upgrade to Pro — share decks privately, control downloads, hide ads and more …

管你要 trace 什麼、bpftrace 用下去就對了 — COSCUP 2025

管你要 trace 什麼、bpftrace 用下去就對了 — COSCUP 2025

Avatar for shunghsiyu

shunghsiyu

August 08, 2025
Tweet

More Decks by shunghsiyu

Other Decks in Programming

Transcript

  1. 3 $ systemctl restart super-important.service Job for super-important.service failed because

    the control process exited with error code. See "systemctl status super-important.service" and "journalctl -xe" for details.
  2. $ systemctl status super-important.service • super-important.service Loaded: loaded (/etc/systemd/system/super-important.service; disabled…

    Active: failed (Result: exit-code) since Thu 2025-07-12 00:00:01 UTC Process: 80054 ExecStart=/usr/sbin/super-important (code=exited, status… Jul 12 00:00:01 system systemd[1]: Starting Your Own Service... Jul 12 00:00:01 system super-important[80054]: Configuration file not found! 4
  3. 5

  4. $ man super-important No manual entry for super-important Possibly, man

    page is not installed, try online at: https://manpages.opensuse.org/something 6
  5. 7

  6. 8

  7. $ systemctl status super-important.service • super-important.service Loaded: loaded (/etc/systemd/system/super-important.service; disabled…

    Active: failed (Result: exit-code) since Thu 2025-07-12 00:00:01 UTC Process: 80054 ExecStart=/usr/sbin/super-important (code=exited, status… Jul 12 00:00:01 system systemd[1]: Starting Your Own Service... Jul 12 00:00:01 system super-important[80054]: Configuration file not found! 9
  8. 10

  9. 11 $ bpftrace -e ' tracepoint:syscalls:sys_enter_open, /comm == "super-important"/ {

    printf("%s", str(args->filename)) }' Attaching 1 probes… /etc/super-important-special.conf
  10. 16 Tracing - Is this function called? What function is

    called? - What are the arguments? - What is the return value? - How long does something take?
  11. 17 bpftrace Language - Event-driven - Awk-like language, inspired by

    DTrace - C-like data structure definition & usage
  12. 20 Probe - an event (usually with wildecard/* support) -

    specific function called/returns - “tracepoint” (declared by developer) - bpftrace started, timer firing, etc…
  13. 21 $ bpftrace -e ' tracepoint:syscalls:sys_enter_open, /comm == "super-important"/ {

    printf("%s", str(args->filename)) }' Attaching 1 probes… /etc/super-important-special.conf
  14. 27

  15. 37 Built-ins - Special variables - args: arguments associated with

    probe - comm: program name of current process - pid: ID of current process
  16. 43 Functions - Helpers provided by bpftrace - printf(): printing

    with formatting - str(): convert string pointer to actual string
  17. 46 Built-ins - Special variables - args: arguments associated with

    probe - comm: program name of current process - pid: ID of current process
  18. 57 Built-ins - Special variables - $1, $2, …: nth

    positional parameter passed to the bpftrace program
  19. 68 tracepoint:syscalls:sys_enter_open … /comm == str($1)/ { printf("%s", str(args->filename)); }

    tracepoint:syscalls:sys_exit_open … /comm == str($1)/ { printf("%d", args->ret); }
  20. - A BPF memory object, e.g. @filename - storage area

    - (usually) key-value map - i.e. similar to a global variable 72 Map Variable
  21. 73 tracepoint:syscalls:sys_enter_open … /comm == str($1)/ { @filename = str(args->filename);

    } tracepoint:syscalls:sys_exit_open … /comm == str($1)/ { printf("%s: %d", @filename, args->ret); }
  22. 75 tracepoint:syscalls:sys_enter_open … /comm == str($1)/ { @filename = str(args->filename);

    } tracepoint:syscalls:sys_exit_open … /… && args->ret < 0 / { printf("%s: %d", @filename, args->ret); }
  23. 77 tracepoint:syscalls:sys_exit_open, tracepoint:syscalls:sys_exit_openat* /comm == str($1) && !!@files[tid]/ { printf("%s:

    %d\n", args->filename, args->ret); delete(@files[tid]); /* Remove used value */
  24. 80 Operators and Expressions - Supports arithmetic operators - +

    - * / - Logical (&&), Bitwise (^ |), and Relational (<= !=) works, too
  25. 81 Latency - How long does it take? - Get

    a timestamp when started - Get a timestamp when ended - Calculate difference between the timestamps
  26. 82 Functions - Helpers provided by bpftrace - nsecs(): returns

    a timestamp in nanoseconds - can drop parenthesis when no argument
  27. - Lexical-scoped variable, e.g. $duration - stores simple values -

    numbers (int, long, …), strings - i.e. similar to a local variable 89 Scratch Variable
  28. 93 Dealing with Lots of Data - What if open()

    was calls 1k times per seconds? - Statistics to the rescue: - minimum, maximum, average, sum - quantile, histogram, time-series
  29. $ ./sys_latency_hist.bt 'super-important' @latency: [256, 512) 64 | | [512,

    1K) 216818 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [1K, 2K) 160007 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [2K, 4K) 30255 |@@@@@@@ | [4K, 8K) 7114 |@ | [8K, 16K) 318 | | [16K, 32K) 150 | | 99
  30. 101 Probe - Any (non-inlined) function in kernel or application

    - TCP connection change -> kernel’s tcp_set_state() - read/write to disk -> tracepoint:block:block_io - database query -> MySQL/Postgres query__start() - interactive shell usage -> bash’s readline()
  31. 105 bpftrace Strengths 1. Safe 2. Dynamic Tracing 3. Low

    Overhead 4. Easy eBPF Make kernel programming possible for everyone
  32. 106 bpftrace Strengths 1. Safe 2. Dynamic Tracing 3. Low

    Overhead 4. Easy eBPF Image from https://ebpf.io/what-is-ebpf/
  33. 109 Adding printf() - Needs to recompile and restart the

    process - more than once (?) - No Heisenbug
  34. 111 Similar Tools - strace/ltrace: high overhead, as high as

    100x - ftrace: less dynamic, restricted process - LTTng: requires out-of-tree kernel module - bcc: less up-to-date in terms of features
  35. 116 Resources Book - BPF Performance Tools Videos - An

    introduction to bpftrace tracing language - bpftrace: a path to the ultimate Linux tracing… Texts - A thorough introduction to bpftrace - bpftrace(8) Manual Page