Add 6 tools tracking dbus.
[platform/upstream/bcc.git] / tools / dbus-messages-sent.py
1 #!/usr/bin/python
2
3 from bcc import BPF
4 from time import sleep
5 from ctypes import c_ushort, c_int, c_ulonglong
6 from sys import argv
7
8
9 print("Hit Ctrl-C to end.")
10 count = 100
11 interval = 5
12 loop = 0
13 # load BPF program
14 bpf_text="""#include <uapi/linux/ptrace.h>
15 #include <linux/sched.h>
16
17 struct p_name{
18         char buf[80];
19         u32 pid;
20 };
21
22 BPF_HASH(counter, struct p_name);
23 BPF_HASH(licznik);
24
25 int sending_message(struct pt_regs *ctx, void *ptri, void *message){
26         struct p_name process = {' ', 0};
27         u64 zero = 0;
28         u32 ret = PT_REGS_RC(ctx);
29         process.pid = bpf_get_current_pid_tgid();
30         bpf_get_current_comm(&(process.buf), sizeof(process.buf));
31         counter.increment(process);
32         return 0;
33 }
34
35 """
36 bpf_text = bpf_text.replace('INTERVAL', '%d' % interval)
37 b = BPF(text=bpf_text)
38
39 b.attach_uprobe(name="dbus-1", sym="dbus_connection_send", fn_name="sending_message")
40 b.attach_uprobe(name="dbus-1", sym="dbus_connection_send_preallocated", fn_name="sending_message")
41 b.attach_uprobe(name="dbus-1", sym="dbus_connection_send_with_reply", fn_name="sending_message")
42 b.attach_uprobe(name="dbus-1", sym="dbus_connection_send_with_reply_and_block", fn_name="sending_message")
43
44
45 while (1):
46         if count > 0:
47                 loop += 1
48                 if loop > count:
49                         exit()
50         sleep(interval)
51         print ("%d:" % loop)
52         stats = b["counter"]
53         print("%10s %20s %20s" % ("PID", "NAME", "MESSAGES SENT"))
54         for k, v in stats.items():
55                 print("%10d %20s %20d\n" % (k.pid, k.buf.encode('string-escape'), v.value ))