blob: 5490bcc762cfd053f2cc3611f6b23cb19a4c971b [file] [log] [blame]
Brendan Gregg177e07e2015-08-18 16:11:35 -07001#!/usr/bin/env python
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07002#
3# syncsnoop Trace sync() syscall.
4# For Linux, uses BCC, eBPF. Embedded C.
5#
6# Written as a basic example of BCC trace & reformat. See
7# examples/hello_world.py for a BCC trace with default output example.
8#
9# Copyright (c) 2015 Brendan Gregg.
10# Licensed under the Apache License, Version 2.0 (the "License")
11#
12# 13-Aug-2015 Brendan Gregg Created this.
13
Brendan Gregg762b1b42015-08-18 16:49:48 -070014from __future__ import print_function
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070015from bpf import BPF
16import sys
17
18# load BPF program
19b = BPF(text = """
20int do_sync(void *ctx) {
21 bpf_trace_printk("sync()\\n");
22 return 0;
23};
24""")
25BPF.attach_kprobe(b.load_func("do_sync", BPF.KPROBE), "sys_sync")
26
27# header
Brendan Gregg762b1b42015-08-18 16:49:48 -070028print("%-18s %s" % ("TIME(s)", "CALL"))
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070029
30# open trace pipe
31try:
32 trace = open("/sys/kernel/debug/tracing/trace_pipe", "r")
33except:
Brendan Gregg762b1b42015-08-18 16:49:48 -070034 print("ERROR: opening trace_pipe", file=sys.stderr)
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070035 exit(1)
36
37# format output
38while 1:
39 try:
40 line = trace.readline().rstrip()
41 except KeyboardInterrupt:
42 pass; exit()
43
44 prolog, time_s, colon, word = line.rsplit(" ", 3)
45 time_s = time_s[:-1] # strip trailing ":"
46
Brendan Gregg762b1b42015-08-18 16:49:48 -070047 print("%-18s %s" % (time_s, word))