blob: 25f1e8cbd5d804ff2233abcfe09ed28d9eadcfa4 [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
14from bpf import BPF
15import sys
16
17# load BPF program
18b = BPF(text = """
19int do_sync(void *ctx) {
20 bpf_trace_printk("sync()\\n");
21 return 0;
22};
23""")
24BPF.attach_kprobe(b.load_func("do_sync", BPF.KPROBE), "sys_sync")
25
26# header
27print "%-18s %s" % ("TIME(s)", "CALL")
28
29# open trace pipe
30try:
31 trace = open("/sys/kernel/debug/tracing/trace_pipe", "r")
32except:
33 print >> sys.stderr, "ERROR: opening trace_pipe"
34 exit(1)
35
36# format output
37while 1:
38 try:
39 line = trace.readline().rstrip()
40 except KeyboardInterrupt:
41 pass; exit()
42
43 prolog, time_s, colon, word = line.rsplit(" ", 3)
44 time_s = time_s[:-1] # strip trailing ":"
45
46 print "%-18s %s" % (time_s, word)