blob: db4b68b49f170dce83a418fe45ac6b7ee43cbd63 [file] [log] [blame]
Sasha Goldshteinfd60d552016-03-01 12:15:34 -08001#!/usr/bin/env python
2#
Sasha Goldshtein3e39a082016-03-24 08:39:47 -07003# tplist Display kernel tracepoints or USDT probes and their formats.
Sasha Goldshteinfd60d552016-03-01 12:15:34 -08004#
Sasha Goldshtein3e39a082016-03-24 08:39:47 -07005# USAGE: tplist [-p PID] [-l LIB] [-v] [filter]
Sasha Goldshteinfd60d552016-03-01 12:15:34 -08006#
7# Licensed under the Apache License, Version 2.0 (the "License")
8# Copyright (C) 2016 Sasha Goldshtein.
9
10import argparse
11import fnmatch
Sasha Goldshteinfd60d552016-03-01 12:15:34 -080012import os
Sasha Goldshtein3e39a082016-03-24 08:39:47 -070013import re
14import sys
15
Sasha Goldshtein69e361a2016-09-27 19:40:00 +030016from bcc import USDT
Sasha Goldshteinfd60d552016-03-01 12:15:34 -080017
18trace_root = "/sys/kernel/debug/tracing"
19event_root = os.path.join(trace_root, "events")
20
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030021parser = argparse.ArgumentParser(
22 description="Display kernel tracepoints or USDT probes " +
23 "and their formats.",
24 formatter_class=argparse.RawDescriptionHelpFormatter)
25parser.add_argument("-p", "--pid", type=int, default=None,
26 help="List USDT probes in the specified process")
27parser.add_argument("-l", "--lib", default="",
28 help="List USDT probes in the specified library or executable")
Rafael Fonseca838ad4b2017-02-09 16:20:24 +010029parser.add_argument("-v", dest="verbosity", action="count", default=0,
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030030 help="Increase verbosity level (print variables, arguments, etc.)")
31parser.add_argument(dest="filter", nargs="?",
32 help="A filter that specifies which probes/tracepoints to print")
Sasha Goldshteinfd60d552016-03-01 12:15:34 -080033args = parser.parse_args()
34
35def print_tpoint_format(category, event):
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030036 fmt = open(os.path.join(event_root, category, event, "format")) \
37 .readlines()
Sasha Goldshteinfd60d552016-03-01 12:15:34 -080038 for line in fmt:
39 match = re.search(r'field:([^;]*);', line)
40 if match is None:
41 continue
42 parts = match.group(1).split()
43 field_name = parts[-1:][0]
44 field_type = " ".join(parts[:-1])
Sasha Goldshteinfd60d552016-03-01 12:15:34 -080045 if field_name.startswith("common_"):
46 continue
47 print(" %s %s;" % (field_type, field_name))
48
49def print_tpoint(category, event):
50 tpoint = "%s:%s" % (category, event)
Sasha Goldshtein3e39a082016-03-24 08:39:47 -070051 if not args.filter or fnmatch.fnmatch(tpoint, args.filter):
Sasha Goldshteinfd60d552016-03-01 12:15:34 -080052 print(tpoint)
Sasha Goldshtein6e91a742016-10-06 18:18:18 +030053 if args.verbosity > 0:
Sasha Goldshteinfd60d552016-03-01 12:15:34 -080054 print_tpoint_format(category, event)
55
Sasha Goldshtein3e39a082016-03-24 08:39:47 -070056def print_tracepoints():
Sasha Goldshteinfd60d552016-03-01 12:15:34 -080057 for category in os.listdir(event_root):
58 cat_dir = os.path.join(event_root, category)
59 if not os.path.isdir(cat_dir):
60 continue
61 for event in os.listdir(cat_dir):
62 evt_dir = os.path.join(cat_dir, event)
63 if os.path.isdir(evt_dir):
64 print_tpoint(category, event)
65
Sasha Goldshtein6e91a742016-10-06 18:18:18 +030066def print_usdt_argument_details(location):
Rafael Fd7a5ff02017-03-03 19:57:28 +010067 for idx in range(0, location.num_arguments):
Sasha Goldshtein6e91a742016-10-06 18:18:18 +030068 arg = location.get_argument(idx)
Paul Chaignon956ca1c2017-03-04 20:07:56 +010069 print(" argument #%d %s" % (idx + 1, arg))
Sasha Goldshtein6e91a742016-10-06 18:18:18 +030070
71def print_usdt_details(probe):
72 if args.verbosity > 0:
73 print(probe)
74 if args.verbosity > 1:
Rafael Fd7a5ff02017-03-03 19:57:28 +010075 for idx in range(0, probe.num_locations):
Sasha Goldshtein6e91a742016-10-06 18:18:18 +030076 loc = probe.get_location(idx)
Paul Chaignon956ca1c2017-03-04 20:07:56 +010077 print(" location #%d %s" % (idx + 1, loc))
Sasha Goldshtein6e91a742016-10-06 18:18:18 +030078 print_usdt_argument_details(loc)
79 else:
80 print(" %d location(s)" % probe.num_locations)
81 print(" %d argument(s)" % probe.num_arguments)
82 else:
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030083 print("%s %s:%s" %
84 (probe.bin_path, probe.provider, probe.name))
Sasha Goldshtein6e91a742016-10-06 18:18:18 +030085
Sasha Goldshtein3e39a082016-03-24 08:39:47 -070086def print_usdt(pid, lib):
Sasha Goldshtein69e361a2016-09-27 19:40:00 +030087 reader = USDT(path=lib, pid=pid)
Sasha Goldshtein3e39a082016-03-24 08:39:47 -070088 probes_seen = []
Sasha Goldshtein69e361a2016-09-27 19:40:00 +030089 for probe in reader.enumerate_probes():
90 probe_name = probe.short_name()
Sasha Goldshtein3e39a082016-03-24 08:39:47 -070091 if not args.filter or fnmatch.fnmatch(probe_name, args.filter):
92 if probe_name in probes_seen:
93 continue
94 probes_seen.append(probe_name)
Sasha Goldshtein6e91a742016-10-06 18:18:18 +030095 print_usdt_details(probe)
Sasha Goldshtein3e39a082016-03-24 08:39:47 -070096
Sasha Goldshteinfd60d552016-03-01 12:15:34 -080097if __name__ == "__main__":
Sasha Goldshtein3e39a082016-03-24 08:39:47 -070098 try:
Sasha Goldshtein69e361a2016-09-27 19:40:00 +030099 if args.pid or args.lib != "":
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700100 print_usdt(args.pid, args.lib)
101 else:
102 print_tracepoints()
103 except:
Brenden Blancobc94d4c2016-05-05 12:05:07 -0700104 if sys.exc_info()[0] is not SystemExit:
105 print(sys.exc_info()[1])