Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 3 | # tplist Display kernel tracepoints or USDT probes and their formats. |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 4 | # |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 5 | # USAGE: tplist [-p PID] [-l LIB] [-v] [filter] |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 8 | # Copyright (C) 2016 Sasha Goldshtein. |
| 9 | |
| 10 | import argparse |
| 11 | import fnmatch |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 12 | import os |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 13 | import re |
| 14 | import sys |
| 15 | |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 16 | from bcc import USDT |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 17 | |
| 18 | trace_root = "/sys/kernel/debug/tracing" |
| 19 | event_root = os.path.join(trace_root, "events") |
| 20 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 21 | parser = argparse.ArgumentParser( |
| 22 | description="Display kernel tracepoints or USDT probes " + |
| 23 | "and their formats.", |
| 24 | formatter_class=argparse.RawDescriptionHelpFormatter) |
| 25 | parser.add_argument("-p", "--pid", type=int, default=None, |
| 26 | help="List USDT probes in the specified process") |
| 27 | parser.add_argument("-l", "--lib", default="", |
| 28 | help="List USDT probes in the specified library or executable") |
Rafael Fonseca | 838ad4b | 2017-02-09 16:20:24 +0100 | [diff] [blame] | 29 | parser.add_argument("-v", dest="verbosity", action="count", default=0, |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 30 | help="Increase verbosity level (print variables, arguments, etc.)") |
| 31 | parser.add_argument(dest="filter", nargs="?", |
| 32 | help="A filter that specifies which probes/tracepoints to print") |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 33 | args = parser.parse_args() |
| 34 | |
| 35 | def print_tpoint_format(category, event): |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 36 | fmt = open(os.path.join(event_root, category, event, "format")) \ |
| 37 | .readlines() |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 38 | 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 Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 45 | if field_name.startswith("common_"): |
| 46 | continue |
| 47 | print(" %s %s;" % (field_type, field_name)) |
| 48 | |
| 49 | def print_tpoint(category, event): |
| 50 | tpoint = "%s:%s" % (category, event) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 51 | if not args.filter or fnmatch.fnmatch(tpoint, args.filter): |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 52 | print(tpoint) |
Sasha Goldshtein | 6e91a74 | 2016-10-06 18:18:18 +0300 | [diff] [blame] | 53 | if args.verbosity > 0: |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 54 | print_tpoint_format(category, event) |
| 55 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 56 | def print_tracepoints(): |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 57 | 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 Goldshtein | 6e91a74 | 2016-10-06 18:18:18 +0300 | [diff] [blame] | 66 | def print_usdt_argument_details(location): |
Rafael F | d7a5ff0 | 2017-03-03 19:57:28 +0100 | [diff] [blame] | 67 | for idx in range(0, location.num_arguments): |
Sasha Goldshtein | 6e91a74 | 2016-10-06 18:18:18 +0300 | [diff] [blame] | 68 | arg = location.get_argument(idx) |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 69 | print(" argument #%d %s" % (idx + 1, arg)) |
Sasha Goldshtein | 6e91a74 | 2016-10-06 18:18:18 +0300 | [diff] [blame] | 70 | |
| 71 | def print_usdt_details(probe): |
| 72 | if args.verbosity > 0: |
| 73 | print(probe) |
| 74 | if args.verbosity > 1: |
Rafael F | d7a5ff0 | 2017-03-03 19:57:28 +0100 | [diff] [blame] | 75 | for idx in range(0, probe.num_locations): |
Sasha Goldshtein | 6e91a74 | 2016-10-06 18:18:18 +0300 | [diff] [blame] | 76 | loc = probe.get_location(idx) |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 77 | print(" location #%d %s" % (idx + 1, loc)) |
Sasha Goldshtein | 6e91a74 | 2016-10-06 18:18:18 +0300 | [diff] [blame] | 78 | 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 Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 83 | print("%s %s:%s" % |
| 84 | (probe.bin_path, probe.provider, probe.name)) |
Sasha Goldshtein | 6e91a74 | 2016-10-06 18:18:18 +0300 | [diff] [blame] | 85 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 86 | def print_usdt(pid, lib): |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 87 | reader = USDT(path=lib, pid=pid) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 88 | probes_seen = [] |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 89 | for probe in reader.enumerate_probes(): |
| 90 | probe_name = probe.short_name() |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 91 | 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 Goldshtein | 6e91a74 | 2016-10-06 18:18:18 +0300 | [diff] [blame] | 95 | print_usdt_details(probe) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 96 | |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 97 | if __name__ == "__main__": |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 98 | try: |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 99 | if args.pid or args.lib != "": |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 100 | print_usdt(args.pid, args.lib) |
| 101 | else: |
| 102 | print_tracepoints() |
| 103 | except: |
Brenden Blanco | bc94d4c | 2016-05-05 12:05:07 -0700 | [diff] [blame] | 104 | if sys.exc_info()[0] is not SystemExit: |
| 105 | print(sys.exc_info()[1]) |