Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # trace Trace a function and print a trace message based on its |
| 4 | # parameters, with an optional filter. |
| 5 | # |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 6 | # usage: trace [-h] [-p PID] [-L TID] [-v] [-Z STRING_SIZE] [-S] |
| 7 | # [-M MAX_EVENTS] [-T] [-t] [-K] [-U] [-I header] |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 8 | # probe [probe ...] |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 9 | # |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 10 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 11 | # Copyright (C) 2016 Sasha Goldshtein. |
| 12 | |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 13 | from __future__ import print_function |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 14 | from bcc import BPF, USDT |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 15 | from functools import partial |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 16 | from time import sleep, strftime |
| 17 | import argparse |
| 18 | import re |
| 19 | import ctypes as ct |
| 20 | import os |
| 21 | import traceback |
| 22 | import sys |
| 23 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 24 | class Probe(object): |
| 25 | probe_count = 0 |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 26 | streq_index = 0 |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 27 | max_events = None |
| 28 | event_count = 0 |
| 29 | first_ts = 0 |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 30 | print_time = False |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 31 | use_localtime = True |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 32 | time_field = False |
| 33 | print_cpu = False |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 34 | tgid = -1 |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 35 | pid = -1 |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 36 | page_cnt = None |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 37 | |
| 38 | @classmethod |
| 39 | def configure(cls, args): |
| 40 | cls.max_events = args.max_events |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 41 | cls.print_time = args.timestamp or args.time |
| 42 | cls.use_localtime = not args.timestamp |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 43 | cls.time_field = cls.print_time and (not cls.use_localtime) |
| 44 | cls.print_cpu = args.print_cpu |
Sasha Goldshtein | 60c4192 | 2017-02-09 04:19:53 -0500 | [diff] [blame] | 45 | cls.first_ts = BPF.monotonic_time() |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 46 | cls.tgid = args.tgid or -1 |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 47 | cls.pid = args.pid or -1 |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 48 | cls.page_cnt = args.buffer_pages |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 49 | |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 50 | def __init__(self, probe, string_size, kernel_stack, user_stack): |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 51 | self.usdt = None |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 52 | self.streq_functions = "" |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 53 | self.raw_probe = probe |
| 54 | self.string_size = string_size |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 55 | self.kernel_stack = kernel_stack |
| 56 | self.user_stack = user_stack |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 57 | Probe.probe_count += 1 |
| 58 | self._parse_probe() |
| 59 | self.probe_num = Probe.probe_count |
| 60 | self.probe_name = "probe_%s_%d" % \ |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 61 | (self._display_function(), self.probe_num) |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 62 | self.probe_name = re.sub(r'[^A-Za-z0-9_]', '_', |
| 63 | self.probe_name) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 64 | |
| 65 | def __str__(self): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 66 | return "%s:%s:%s FLT=%s ACT=%s/%s" % (self.probe_type, |
| 67 | self.library, self._display_function(), self.filter, |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 68 | self.types, self.values) |
| 69 | |
| 70 | def is_default_action(self): |
| 71 | return self.python_format == "" |
| 72 | |
| 73 | def _bail(self, error): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 74 | raise ValueError("error in probe '%s': %s" % |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 75 | (self.raw_probe, error)) |
| 76 | |
| 77 | def _parse_probe(self): |
| 78 | text = self.raw_probe |
| 79 | |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 80 | # There might be a function signature preceding the actual |
| 81 | # filter/print part, or not. Find the probe specifier first -- |
| 82 | # it ends with either a space or an open paren ( for the |
| 83 | # function signature part. |
| 84 | # opt. signature |
| 85 | # probespec | rest |
| 86 | # --------- ---------- -- |
| 87 | (spec, sig, rest) = re.match(r'([^ \t\(]+)(\([^\(]*\))?(.*)', |
| 88 | text).groups() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 89 | |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 90 | self._parse_spec(spec) |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 91 | # Remove the parens |
| 92 | self.signature = sig[1:-1] if sig else None |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 93 | if self.signature and self.probe_type in ['u', 't']: |
| 94 | self._bail("USDT and tracepoint probes can't have " + |
| 95 | "a function signature; use arg1, arg2, " + |
| 96 | "... instead") |
| 97 | |
| 98 | text = rest.lstrip() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 99 | # If we now have a (, wait for the balanced closing ) and that |
| 100 | # will be the predicate |
| 101 | self.filter = None |
| 102 | if len(text) > 0 and text[0] == "(": |
| 103 | balance = 1 |
| 104 | for i in range(1, len(text)): |
| 105 | if text[i] == "(": |
| 106 | balance += 1 |
| 107 | if text[i] == ")": |
| 108 | balance -= 1 |
| 109 | if balance == 0: |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 110 | self._parse_filter(text[:i + 1]) |
| 111 | text = text[i + 1:] |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 112 | break |
| 113 | if self.filter is None: |
| 114 | self._bail("unmatched end of predicate") |
| 115 | |
| 116 | if self.filter is None: |
| 117 | self.filter = "1" |
| 118 | |
| 119 | # The remainder of the text is the printf action |
| 120 | self._parse_action(text.lstrip()) |
| 121 | |
| 122 | def _parse_spec(self, spec): |
| 123 | parts = spec.split(":") |
| 124 | # Two special cases: 'func' means 'p::func', 'lib:func' means |
| 125 | # 'p:lib:func'. Other combinations need to provide an empty |
| 126 | # value between delimiters, e.g. 'r::func' for a kretprobe on |
| 127 | # the function func. |
| 128 | if len(parts) == 1: |
| 129 | parts = ["p", "", parts[0]] |
| 130 | elif len(parts) == 2: |
| 131 | parts = ["p", parts[0], parts[1]] |
| 132 | if len(parts[0]) == 0: |
| 133 | self.probe_type = "p" |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 134 | elif parts[0] in ["p", "r", "t", "u"]: |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 135 | self.probe_type = parts[0] |
| 136 | else: |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 137 | self._bail("probe type must be '', 'p', 't', 'r', " + |
| 138 | "or 'u', but got '%s'" % parts[0]) |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 139 | if self.probe_type == "t": |
| 140 | self.tp_category = parts[1] |
| 141 | self.tp_event = parts[2] |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 142 | self.library = "" # kernel |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 143 | self.function = "" # from TRACEPOINT_PROBE |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 144 | elif self.probe_type == "u": |
vkhromov | 5a2b39e | 2017-07-14 20:42:29 +0100 | [diff] [blame] | 145 | self.library = ':'.join(parts[1:-1]) |
| 146 | self.usdt_name = parts[-1] |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 147 | self.function = "" # no function, just address |
| 148 | # We will discover the USDT provider by matching on |
| 149 | # the USDT name in the specified library |
| 150 | self._find_usdt_probe() |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 151 | else: |
vkhromov | 5a2b39e | 2017-07-14 20:42:29 +0100 | [diff] [blame] | 152 | self.library = ':'.join(parts[1:-1]) |
| 153 | self.function = parts[-1] |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 154 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 155 | def _find_usdt_probe(self): |
Sasha Goldshtein | dd04536 | 2016-11-13 05:07:38 -0800 | [diff] [blame] | 156 | target = Probe.pid if Probe.pid and Probe.pid != -1 \ |
| 157 | else Probe.tgid |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 158 | self.usdt = USDT(path=self.library, pid=target) |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 159 | for probe in self.usdt.enumerate_probes(): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 160 | if probe.name == self.usdt_name: |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 161 | return # Found it, will enable later |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 162 | self._bail("unrecognized USDT probe %s" % self.usdt_name) |
| 163 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 164 | def _parse_filter(self, filt): |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 165 | self.filter = self._rewrite_expr(filt) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 166 | |
| 167 | def _parse_types(self, fmt): |
| 168 | for match in re.finditer( |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 169 | r'[^%]%(s|u|d|llu|lld|hu|hd|x|llx|c|K|U)', fmt): |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 170 | self.types.append(match.group(1)) |
| 171 | fmt = re.sub(r'([^%]%)(u|d|llu|lld|hu|hd)', r'\1d', fmt) |
| 172 | fmt = re.sub(r'([^%]%)(x|llx)', r'\1x', fmt) |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 173 | fmt = re.sub('%K|%U', '%s', fmt) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 174 | self.python_format = fmt.strip('"') |
| 175 | |
| 176 | def _parse_action(self, action): |
| 177 | self.values = [] |
| 178 | self.types = [] |
| 179 | self.python_format = "" |
| 180 | if len(action) == 0: |
| 181 | return |
| 182 | |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 183 | action = action.strip() |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 184 | match = re.search(r'(\".*?\"),?(.*)', action) |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 185 | if match is None: |
| 186 | self._bail("expected format string in \"s") |
| 187 | |
| 188 | self.raw_format = match.group(1) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 189 | self._parse_types(self.raw_format) |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 190 | for part in re.split('(?<!"),', match.group(2)): |
| 191 | part = self._rewrite_expr(part) |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 192 | if len(part) > 0: |
| 193 | self.values.append(part) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 194 | |
| 195 | aliases = { |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 196 | "retval": "PT_REGS_RC(ctx)", |
| 197 | "arg1": "PT_REGS_PARM1(ctx)", |
| 198 | "arg2": "PT_REGS_PARM2(ctx)", |
| 199 | "arg3": "PT_REGS_PARM3(ctx)", |
| 200 | "arg4": "PT_REGS_PARM4(ctx)", |
| 201 | "arg5": "PT_REGS_PARM5(ctx)", |
| 202 | "arg6": "PT_REGS_PARM6(ctx)", |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 203 | "$uid": "(unsigned)(bpf_get_current_uid_gid() & 0xffffffff)", |
| 204 | "$gid": "(unsigned)(bpf_get_current_uid_gid() >> 32)", |
| 205 | "$pid": "(unsigned)(bpf_get_current_pid_tgid() & 0xffffffff)", |
| 206 | "$tgid": "(unsigned)(bpf_get_current_pid_tgid() >> 32)", |
Yonghong Song | f92fef2 | 2018-01-24 20:51:46 -0800 | [diff] [blame] | 207 | "$cpu": "bpf_get_smp_processor_id()", |
| 208 | "$task" : "((struct task_struct *)bpf_get_current_task())" |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 211 | def _generate_streq_function(self, string): |
| 212 | fname = "streq_%d" % Probe.streq_index |
| 213 | Probe.streq_index += 1 |
| 214 | self.streq_functions += """ |
Sasha Goldshtein | b9aec34 | 2017-01-16 18:41:22 +0000 | [diff] [blame] | 215 | static inline bool %s(char const *ignored, uintptr_t str) { |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 216 | char needle[] = %s; |
| 217 | char haystack[sizeof(needle)]; |
| 218 | bpf_probe_read(&haystack, sizeof(haystack), (void *)str); |
Sasha Goldshtein | dcf1675 | 2017-01-17 07:40:57 +0000 | [diff] [blame] | 219 | for (int i = 0; i < sizeof(needle) - 1; ++i) { |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 220 | if (needle[i] != haystack[i]) { |
| 221 | return false; |
| 222 | } |
| 223 | } |
| 224 | return true; |
| 225 | } |
| 226 | """ % (fname, string) |
| 227 | return fname |
| 228 | |
| 229 | def _rewrite_expr(self, expr): |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 230 | for alias, replacement in Probe.aliases.items(): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 231 | # For USDT probes, we replace argN values with the |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 232 | # actual arguments for that probe obtained using |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 233 | # bpf_readarg_N macros emitted at BPF construction. |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 234 | if alias.startswith("arg") and self.probe_type == "u": |
| 235 | continue |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 236 | expr = expr.replace(alias, replacement) |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 237 | matches = re.finditer('STRCMP\\(("[^"]+\\")', expr) |
| 238 | for match in matches: |
| 239 | string = match.group(1) |
| 240 | fname = self._generate_streq_function(string) |
| 241 | expr = expr.replace("STRCMP", fname, 1) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 242 | return expr |
| 243 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 244 | p_type = {"u": ct.c_uint, "d": ct.c_int, |
| 245 | "llu": ct.c_ulonglong, "lld": ct.c_longlong, |
| 246 | "hu": ct.c_ushort, "hd": ct.c_short, |
| 247 | "x": ct.c_uint, "llx": ct.c_ulonglong, "c": ct.c_ubyte, |
| 248 | "K": ct.c_ulonglong, "U": ct.c_ulonglong} |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 249 | |
| 250 | def _generate_python_field_decl(self, idx, fields): |
| 251 | field_type = self.types[idx] |
| 252 | if field_type == "s": |
| 253 | ptype = ct.c_char * self.string_size |
| 254 | else: |
| 255 | ptype = Probe.p_type[field_type] |
| 256 | fields.append(("v%d" % idx, ptype)) |
| 257 | |
| 258 | def _generate_python_data_decl(self): |
| 259 | self.python_struct_name = "%s_%d_Data" % \ |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 260 | (self._display_function(), self.probe_num) |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 261 | fields = [] |
| 262 | if self.time_field: |
| 263 | fields.append(("timestamp_ns", ct.c_ulonglong)) |
| 264 | if self.print_cpu: |
| 265 | fields.append(("cpu", ct.c_int)) |
| 266 | fields.extend([ |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 267 | ("tgid", ct.c_uint), |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 268 | ("pid", ct.c_uint), |
| 269 | ("comm", ct.c_char * 16) # TASK_COMM_LEN |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 270 | ]) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 271 | for i in range(0, len(self.types)): |
| 272 | self._generate_python_field_decl(i, fields) |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 273 | if self.kernel_stack: |
| 274 | fields.append(("kernel_stack_id", ct.c_int)) |
| 275 | if self.user_stack: |
| 276 | fields.append(("user_stack_id", ct.c_int)) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 277 | return type(self.python_struct_name, (ct.Structure,), |
| 278 | dict(_fields_=fields)) |
| 279 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 280 | c_type = {"u": "unsigned int", "d": "int", |
| 281 | "llu": "unsigned long long", "lld": "long long", |
| 282 | "hu": "unsigned short", "hd": "short", |
| 283 | "x": "unsigned int", "llx": "unsigned long long", |
| 284 | "c": "char", "K": "unsigned long long", |
| 285 | "U": "unsigned long long"} |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 286 | fmt_types = c_type.keys() |
| 287 | |
| 288 | def _generate_field_decl(self, idx): |
| 289 | field_type = self.types[idx] |
| 290 | if field_type == "s": |
| 291 | return "char v%d[%d];\n" % (idx, self.string_size) |
| 292 | if field_type in Probe.fmt_types: |
| 293 | return "%s v%d;\n" % (Probe.c_type[field_type], idx) |
| 294 | self._bail("unrecognized format specifier %s" % field_type) |
| 295 | |
| 296 | def _generate_data_decl(self): |
| 297 | # The BPF program will populate values into the struct |
| 298 | # according to the format string, and the Python program will |
| 299 | # construct the final display string. |
| 300 | self.events_name = "%s_events" % self.probe_name |
| 301 | self.struct_name = "%s_data_t" % self.probe_name |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 302 | self.stacks_name = "%s_stacks" % self.probe_name |
| 303 | stack_table = "BPF_STACK_TRACE(%s, 1024);" % self.stacks_name \ |
| 304 | if (self.kernel_stack or self.user_stack) else "" |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 305 | data_fields = "" |
| 306 | for i, field_type in enumerate(self.types): |
| 307 | data_fields += " " + \ |
| 308 | self._generate_field_decl(i) |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 309 | time_str = "u64 timestamp_ns;" if self.time_field else "" |
| 310 | cpu_str = "int cpu;" if self.print_cpu else "" |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 311 | kernel_stack_str = " int kernel_stack_id;" \ |
| 312 | if self.kernel_stack else "" |
| 313 | user_stack_str = " int user_stack_id;" \ |
| 314 | if self.user_stack else "" |
| 315 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 316 | text = """ |
| 317 | struct %s |
| 318 | { |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 319 | %s |
| 320 | %s |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 321 | u32 tgid; |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 322 | u32 pid; |
| 323 | char comm[TASK_COMM_LEN]; |
| 324 | %s |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 325 | %s |
| 326 | %s |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | BPF_PERF_OUTPUT(%s); |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 330 | %s |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 331 | """ |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 332 | return text % (self.struct_name, time_str, cpu_str, data_fields, |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 333 | kernel_stack_str, user_stack_str, |
| 334 | self.events_name, stack_table) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 335 | |
| 336 | def _generate_field_assign(self, idx): |
| 337 | field_type = self.types[idx] |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 338 | expr = self.values[idx].strip() |
| 339 | text = "" |
| 340 | if self.probe_type == "u" and expr[0:3] == "arg": |
Sasha Goldshtein | 3a5256f | 2017-02-20 15:42:57 +0000 | [diff] [blame] | 341 | arg_index = int(expr[3]) |
| 342 | arg_ctype = self.usdt.get_probe_arg_ctype( |
| 343 | self.usdt_name, arg_index - 1) |
| 344 | text = (" %s %s = 0;\n" + |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 345 | " bpf_usdt_readarg(%s, ctx, &%s);\n") \ |
Sasha Goldshtein | 3a5256f | 2017-02-20 15:42:57 +0000 | [diff] [blame] | 346 | % (arg_ctype, expr, expr[3], expr) |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 347 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 348 | if field_type == "s": |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 349 | return text + """ |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 350 | if (%s != 0) { |
| 351 | bpf_probe_read(&__data.v%d, sizeof(__data.v%d), (void *)%s); |
| 352 | } |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 353 | """ % (expr, idx, idx, expr) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 354 | if field_type in Probe.fmt_types: |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 355 | return text + " __data.v%d = (%s)%s;\n" % \ |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 356 | (idx, Probe.c_type[field_type], expr) |
| 357 | self._bail("unrecognized field type %s" % field_type) |
| 358 | |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 359 | def _generate_usdt_filter_read(self): |
| 360 | text = "" |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 361 | if self.probe_type != "u": |
| 362 | return text |
| 363 | for arg, _ in Probe.aliases.items(): |
| 364 | if not (arg.startswith("arg") and |
| 365 | (arg in self.filter)): |
| 366 | continue |
| 367 | arg_index = int(arg.replace("arg", "")) |
| 368 | arg_ctype = self.usdt.get_probe_arg_ctype( |
Sasha Goldshtein | dcf1675 | 2017-01-17 07:40:57 +0000 | [diff] [blame] | 369 | self.usdt_name, arg_index - 1) |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 370 | if not arg_ctype: |
| 371 | self._bail("Unable to determine type of {} " |
| 372 | "in the filter".format(arg)) |
| 373 | text += """ |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 374 | {} {}_filter; |
| 375 | bpf_usdt_readarg({}, ctx, &{}_filter); |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 376 | """.format(arg_ctype, arg, arg_index, arg) |
| 377 | self.filter = self.filter.replace( |
| 378 | arg, "{}_filter".format(arg)) |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 379 | return text |
| 380 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 381 | def generate_program(self, include_self): |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 382 | data_decl = self._generate_data_decl() |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 383 | if Probe.pid != -1: |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 384 | pid_filter = """ |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 385 | if (__pid != %d) { return 0; } |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 386 | """ % Probe.pid |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 387 | # uprobes can have a built-in tgid filter passed to |
| 388 | # attach_uprobe, hence the check here -- for kprobes, we |
| 389 | # need to do the tgid test by hand: |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 390 | elif len(self.library) == 0 and Probe.tgid != -1: |
| 391 | pid_filter = """ |
| 392 | if (__tgid != %d) { return 0; } |
| 393 | """ % Probe.tgid |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 394 | elif not include_self: |
| 395 | pid_filter = """ |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 396 | if (__tgid == %d) { return 0; } |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 397 | """ % os.getpid() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 398 | else: |
| 399 | pid_filter = "" |
| 400 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 401 | prefix = "" |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 402 | signature = "struct pt_regs *ctx" |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 403 | if self.signature: |
| 404 | signature += ", " + self.signature |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 405 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 406 | data_fields = "" |
| 407 | for i, expr in enumerate(self.values): |
| 408 | data_fields += self._generate_field_assign(i) |
| 409 | |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 410 | if self.probe_type == "t": |
| 411 | heading = "TRACEPOINT_PROBE(%s, %s)" % \ |
| 412 | (self.tp_category, self.tp_event) |
| 413 | ctx_name = "args" |
| 414 | else: |
| 415 | heading = "int %s(%s)" % (self.probe_name, signature) |
| 416 | ctx_name = "ctx" |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 417 | |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 418 | time_str = """ |
| 419 | __data.timestamp_ns = bpf_ktime_get_ns();""" if self.time_field else "" |
| 420 | cpu_str = """ |
| 421 | __data.cpu = bpf_get_smp_processor_id();""" if self.print_cpu else "" |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 422 | stack_trace = "" |
| 423 | if self.user_stack: |
| 424 | stack_trace += """ |
| 425 | __data.user_stack_id = %s.get_stackid( |
| 426 | %s, BPF_F_REUSE_STACKID | BPF_F_USER_STACK |
| 427 | );""" % (self.stacks_name, ctx_name) |
| 428 | if self.kernel_stack: |
| 429 | stack_trace += """ |
| 430 | __data.kernel_stack_id = %s.get_stackid( |
| 431 | %s, BPF_F_REUSE_STACKID |
| 432 | );""" % (self.stacks_name, ctx_name) |
| 433 | |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 434 | text = heading + """ |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 435 | { |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 436 | u64 __pid_tgid = bpf_get_current_pid_tgid(); |
| 437 | u32 __tgid = __pid_tgid >> 32; |
| 438 | u32 __pid = __pid_tgid; // implicit cast to u32 for bottom half |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 439 | %s |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 440 | %s |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 441 | %s |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 442 | if (!(%s)) return 0; |
| 443 | |
| 444 | struct %s __data = {0}; |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 445 | %s |
| 446 | %s |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 447 | __data.tgid = __tgid; |
| 448 | __data.pid = __pid; |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 449 | bpf_get_current_comm(&__data.comm, sizeof(__data.comm)); |
| 450 | %s |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 451 | %s |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 452 | %s.perf_submit(%s, &__data, sizeof(__data)); |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 453 | return 0; |
| 454 | } |
| 455 | """ |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 456 | text = text % (pid_filter, prefix, |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 457 | self._generate_usdt_filter_read(), self.filter, |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 458 | self.struct_name, time_str, cpu_str, data_fields, |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 459 | stack_trace, self.events_name, ctx_name) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 460 | |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 461 | return self.streq_functions + data_decl + "\n" + text |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 462 | |
| 463 | @classmethod |
| 464 | def _time_off_str(cls, timestamp_ns): |
| 465 | return "%.6f" % (1e-9 * (timestamp_ns - cls.first_ts)) |
| 466 | |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 467 | def _display_function(self): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 468 | if self.probe_type == 'p' or self.probe_type == 'r': |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 469 | return self.function |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 470 | elif self.probe_type == 'u': |
| 471 | return self.usdt_name |
| 472 | else: # self.probe_type == 't' |
| 473 | return self.tp_event |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 474 | |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 475 | def print_stack(self, bpf, stack_id, tgid): |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 476 | if stack_id < 0: |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 477 | print(" %d" % stack_id) |
| 478 | return |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 479 | |
| 480 | stack = list(bpf.get_table(self.stacks_name).walk(stack_id)) |
| 481 | for addr in stack: |
Sasha Goldshtein | 1e34f4e | 2017-02-09 00:21:49 -0500 | [diff] [blame] | 482 | print(" %s" % (bpf.sym(addr, tgid, |
Sasha Goldshtein | 0155385 | 2017-02-09 03:58:09 -0500 | [diff] [blame] | 483 | show_module=True, show_offset=True))) |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 484 | |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 485 | def _format_message(self, bpf, tgid, values): |
| 486 | # Replace each %K with kernel sym and %U with user sym in tgid |
Rafael Fonseca | aee5ecf | 2017-02-08 16:14:31 +0100 | [diff] [blame] | 487 | kernel_placeholders = [i for i, t in enumerate(self.types) |
| 488 | if t == 'K'] |
| 489 | user_placeholders = [i for i, t in enumerate(self.types) |
| 490 | if t == 'U'] |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 491 | for kp in kernel_placeholders: |
Sasha Goldshtein | 0155385 | 2017-02-09 03:58:09 -0500 | [diff] [blame] | 492 | values[kp] = bpf.ksym(values[kp], show_offset=True) |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 493 | for up in user_placeholders: |
Sasha Goldshtein | 1e34f4e | 2017-02-09 00:21:49 -0500 | [diff] [blame] | 494 | values[up] = bpf.sym(values[up], tgid, |
Sasha Goldshtein | 0155385 | 2017-02-09 03:58:09 -0500 | [diff] [blame] | 495 | show_module=True, show_offset=True) |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 496 | return self.python_format % tuple(values) |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 497 | |
| 498 | def print_event(self, bpf, cpu, data, size): |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 499 | # Cast as the generated structure type and display |
| 500 | # according to the format string in the probe. |
| 501 | event = ct.cast(data, ct.POINTER(self.python_struct)).contents |
| 502 | values = map(lambda i: getattr(event, "v%d" % i), |
| 503 | range(0, len(self.values))) |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 504 | msg = self._format_message(bpf, event.tgid, values) |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 505 | if Probe.print_time: |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 506 | time = strftime("%H:%M:%S") if Probe.use_localtime else \ |
| 507 | Probe._time_off_str(event.timestamp_ns) |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 508 | print("%-8s " % time[:8], end="") |
| 509 | if Probe.print_cpu: |
| 510 | print("%-3s " % event.cpu, end="") |
| 511 | print("%-7d %-7d %-15s %-16s %s" % |
| 512 | (event.tgid, event.pid, event.comm.decode(), |
| 513 | self._display_function(), msg)) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 514 | |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 515 | if self.kernel_stack: |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 516 | self.print_stack(bpf, event.kernel_stack_id, -1) |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 517 | if self.user_stack: |
| 518 | self.print_stack(bpf, event.user_stack_id, event.tgid) |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 519 | if self.user_stack or self.kernel_stack: |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 520 | print("") |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 521 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 522 | Probe.event_count += 1 |
| 523 | if Probe.max_events is not None and \ |
| 524 | Probe.event_count >= Probe.max_events: |
| 525 | exit() |
| 526 | |
| 527 | def attach(self, bpf, verbose): |
| 528 | if len(self.library) == 0: |
| 529 | self._attach_k(bpf) |
| 530 | else: |
| 531 | self._attach_u(bpf) |
| 532 | self.python_struct = self._generate_python_data_decl() |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 533 | callback = partial(self.print_event, bpf) |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 534 | bpf[self.events_name].open_perf_buffer(callback, |
| 535 | page_cnt=self.page_cnt) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 536 | |
| 537 | def _attach_k(self, bpf): |
| 538 | if self.probe_type == "r": |
| 539 | bpf.attach_kretprobe(event=self.function, |
| 540 | fn_name=self.probe_name) |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 541 | elif self.probe_type == "p": |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 542 | bpf.attach_kprobe(event=self.function, |
| 543 | fn_name=self.probe_name) |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 544 | # Note that tracepoints don't need an explicit attach |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 545 | |
| 546 | def _attach_u(self, bpf): |
| 547 | libpath = BPF.find_library(self.library) |
| 548 | if libpath is None: |
| 549 | # This might be an executable (e.g. 'bash') |
Sasha Goldshtein | ec67971 | 2016-10-04 18:33:36 +0300 | [diff] [blame] | 550 | libpath = BPF.find_exe(self.library) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 551 | if libpath is None or len(libpath) == 0: |
| 552 | self._bail("unable to find library %s" % self.library) |
| 553 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 554 | if self.probe_type == "u": |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 555 | pass # Was already enabled by the BPF constructor |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 556 | elif self.probe_type == "r": |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 557 | bpf.attach_uretprobe(name=libpath, |
| 558 | sym=self.function, |
| 559 | fn_name=self.probe_name, |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 560 | pid=Probe.tgid) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 561 | else: |
| 562 | bpf.attach_uprobe(name=libpath, |
| 563 | sym=self.function, |
| 564 | fn_name=self.probe_name, |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 565 | pid=Probe.tgid) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 566 | |
| 567 | class Tool(object): |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 568 | DEFAULT_PERF_BUFFER_PAGES = 64 |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 569 | examples = """ |
| 570 | EXAMPLES: |
| 571 | |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 572 | trace do_sys_open |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 573 | Trace the open syscall and print a default trace message when entered |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 574 | trace 'do_sys_open "%s", arg2' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 575 | Trace the open syscall and print the filename being opened |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 576 | trace 'sys_read (arg3 > 20000) "read %d bytes", arg3' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 577 | Trace the read syscall and print a message for reads >20000 bytes |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 578 | trace 'r::do_sys_open "%llx", retval' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 579 | Trace the return from the open syscall and print the return value |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 580 | trace 'c:open (arg2 == 42) "%s %d", arg1, arg2' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 581 | Trace the open() call from libc only if the flags (arg2) argument is 42 |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 582 | trace 'c:malloc "size = %d", arg1' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 583 | Trace malloc calls and print the size being allocated |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 584 | trace 'p:c:write (arg1 == 1) "writing %d bytes to STDOUT", arg3' |
| 585 | Trace the write() call from libc to monitor writes to STDOUT |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 586 | trace 'r::__kmalloc (retval == 0) "kmalloc failed!"' |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 587 | Trace returns from __kmalloc which returned a null pointer |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 588 | trace 'r:c:malloc (retval) "allocated = %x", retval' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 589 | Trace returns from malloc and print non-NULL allocated buffers |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 590 | trace 't:block:block_rq_complete "sectors=%d", args->nr_sector' |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 591 | Trace the block_rq_complete kernel tracepoint and print # of tx sectors |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 592 | trace 'u:pthread:pthread_create (arg4 != 0)' |
| 593 | Trace the USDT probe pthread_create when its 4th argument is non-zero |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 594 | trace 'p::SyS_nanosleep(struct timespec *ts) "sleep for %lld ns", ts->tv_nsec' |
| 595 | Trace the nanosleep syscall and print the sleep duration in ns |
Yonghong Song | f4470dc | 2017-12-13 14:12:13 -0800 | [diff] [blame] | 596 | trace -I 'linux/fs.h' \\ |
| 597 | 'p::uprobe_register(struct inode *inode) "a_ops = %llx", inode->i_mapping->a_ops' |
| 598 | Trace the uprobe_register inode mapping ops, and the symbol can be found |
| 599 | in /proc/kallsyms |
| 600 | trace -I 'kernel/sched/sched.h' \\ |
| 601 | 'p::__account_cfs_rq_runtime(struct cfs_rq *cfs_rq) "%d", cfs_rq->runtime_remaining' |
| 602 | Trace the cfs scheduling runqueue remaining runtime. The struct cfs_rq is defined |
| 603 | in kernel/sched/sched.h which is in kernel source tree and not in kernel-devel |
| 604 | package. So this command needs to run at the kernel source tree root directory |
| 605 | so that the added header file can be found by the compiler. |
tehnerd | 86293f0 | 2018-01-23 21:21:58 -0800 | [diff] [blame] | 606 | trace -I 'net/sock.h' \\ |
| 607 | 'udpv6_sendmsg(struct sock *sk) (sk->sk_dport == 13568)' |
| 608 | Trace udpv6 sendmsg calls only if socket's destination port is equal |
| 609 | to 53 (DNS; 13568 in big endian order) |
Yonghong Song | f92fef2 | 2018-01-24 20:51:46 -0800 | [diff] [blame] | 610 | trace -I 'linux/fs_struct.h' 'mntns_install "users = %d", $task->fs->users' |
| 611 | Trace the number of users accessing the file system of the current task |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 612 | """ |
| 613 | |
| 614 | def __init__(self): |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 615 | parser = argparse.ArgumentParser(description="Attach to " + |
| 616 | "functions and print trace messages.", |
| 617 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 618 | epilog=Tool.examples) |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 619 | parser.add_argument("-b", "--buffer-pages", type=int, |
| 620 | default=Tool.DEFAULT_PERF_BUFFER_PAGES, |
| 621 | help="number of pages to use for perf_events ring buffer " |
| 622 | "(default: %(default)d)") |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 623 | # we'll refer to the userspace concepts of "pid" and "tid" by |
| 624 | # their kernel names -- tgid and pid -- inside the script |
| 625 | parser.add_argument("-p", "--pid", type=int, metavar="PID", |
| 626 | dest="tgid", help="id of the process to trace (optional)") |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 627 | parser.add_argument("-L", "--tid", type=int, metavar="TID", |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 628 | dest="pid", help="id of the thread to trace (optional)") |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 629 | parser.add_argument("-v", "--verbose", action="store_true", |
| 630 | help="print resulting BPF program code before executing") |
| 631 | parser.add_argument("-Z", "--string-size", type=int, |
| 632 | default=80, help="maximum size to read from strings") |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 633 | parser.add_argument("-S", "--include-self", |
| 634 | action="store_true", |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 635 | help="do not filter trace's own pid from the trace") |
| 636 | parser.add_argument("-M", "--max-events", type=int, |
| 637 | help="number of events to print before quitting") |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 638 | parser.add_argument("-t", "--timestamp", action="store_true", |
| 639 | help="print timestamp column (offset from trace start)") |
| 640 | parser.add_argument("-T", "--time", action="store_true", |
| 641 | help="print time column") |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 642 | parser.add_argument("-C", "--print_cpu", action="store_true", |
| 643 | help="print CPU id") |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 644 | parser.add_argument("-K", "--kernel-stack", |
| 645 | action="store_true", help="output kernel stack trace") |
| 646 | parser.add_argument("-U", "--user-stack", |
| 647 | action="store_true", help="output user stack trace") |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 648 | parser.add_argument(metavar="probe", dest="probes", nargs="+", |
| 649 | help="probe specifier (see examples)") |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 650 | parser.add_argument("-I", "--include", action="append", |
| 651 | metavar="header", |
ShelbyFrances | f5dbbdb | 2017-02-08 05:56:52 +0300 | [diff] [blame] | 652 | help="additional header files to include in the BPF program " |
Yonghong Song | f4470dc | 2017-12-13 14:12:13 -0800 | [diff] [blame] | 653 | "as either full path, " |
| 654 | "or relative to current working directory, " |
| 655 | "or relative to default kernel header search path") |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 656 | self.args = parser.parse_args() |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 657 | if self.args.tgid and self.args.pid: |
Yonghong Song | f4470dc | 2017-12-13 14:12:13 -0800 | [diff] [blame] | 658 | parser.error("only one of -p and -L may be specified") |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 659 | |
| 660 | def _create_probes(self): |
| 661 | Probe.configure(self.args) |
| 662 | self.probes = [] |
| 663 | for probe_spec in self.args.probes: |
| 664 | self.probes.append(Probe( |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 665 | probe_spec, self.args.string_size, |
| 666 | self.args.kernel_stack, self.args.user_stack)) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 667 | |
| 668 | def _generate_program(self): |
| 669 | self.program = """ |
| 670 | #include <linux/ptrace.h> |
| 671 | #include <linux/sched.h> /* For TASK_COMM_LEN */ |
| 672 | |
| 673 | """ |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 674 | for include in (self.args.include or []): |
ShelbyFrances | f5dbbdb | 2017-02-08 05:56:52 +0300 | [diff] [blame] | 675 | if include.startswith((".", "/")): |
| 676 | include = os.path.abspath(include) |
| 677 | self.program += "#include \"%s\"\n" % include |
| 678 | else: |
| 679 | self.program += "#include <%s>\n" % include |
Sasha Goldshtein | b950d6f | 2016-03-21 04:06:15 -0700 | [diff] [blame] | 680 | self.program += BPF.generate_auto_includes( |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 681 | map(lambda p: p.raw_probe, self.probes)) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 682 | for probe in self.probes: |
| 683 | self.program += probe.generate_program( |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 684 | self.args.include_self) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 685 | |
| 686 | if self.args.verbose: |
| 687 | print(self.program) |
| 688 | |
| 689 | def _attach_probes(self): |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 690 | usdt_contexts = [] |
| 691 | for probe in self.probes: |
| 692 | if probe.usdt: |
| 693 | # USDT probes must be enabled before the BPF object |
| 694 | # is initialized, because that's where the actual |
| 695 | # uprobe is being attached. |
| 696 | probe.usdt.enable_probe( |
| 697 | probe.usdt_name, probe.probe_name) |
Sasha Goldshtein | f733cac | 2016-10-04 18:39:01 +0300 | [diff] [blame] | 698 | if self.args.verbose: |
| 699 | print(probe.usdt.get_text()) |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 700 | usdt_contexts.append(probe.usdt) |
| 701 | self.bpf = BPF(text=self.program, usdt_contexts=usdt_contexts) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 702 | for probe in self.probes: |
| 703 | if self.args.verbose: |
| 704 | print(probe) |
| 705 | probe.attach(self.bpf, self.args.verbose) |
| 706 | |
| 707 | def _main_loop(self): |
| 708 | all_probes_trivial = all(map(Probe.is_default_action, |
| 709 | self.probes)) |
| 710 | |
| 711 | # Print header |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 712 | if self.args.timestamp or self.args.time: |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 713 | print("%-8s " % "TIME", end=""); |
| 714 | if self.args.print_cpu: |
| 715 | print("%-3s " % "CPU", end=""); |
| 716 | print("%-7s %-7s %-15s %-16s %s" % |
| 717 | ("PID", "TID", "COMM", "FUNC", |
| 718 | "-" if not all_probes_trivial else "")) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 719 | |
| 720 | while True: |
| 721 | self.bpf.kprobe_poll() |
| 722 | |
| 723 | def run(self): |
| 724 | try: |
| 725 | self._create_probes() |
| 726 | self._generate_program() |
| 727 | self._attach_probes() |
| 728 | self._main_loop() |
| 729 | except: |
Sasha Goldshtein | 2febc29 | 2017-02-13 20:25:32 -0500 | [diff] [blame] | 730 | exc_info = sys.exc_info() |
| 731 | sys_exit = exc_info[0] is SystemExit |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 732 | if self.args.verbose: |
| 733 | traceback.print_exc() |
Sasha Goldshtein | 2febc29 | 2017-02-13 20:25:32 -0500 | [diff] [blame] | 734 | elif not sys_exit: |
| 735 | print(exc_info[1]) |
| 736 | exit(0 if sys_exit else 1) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 737 | |
| 738 | if __name__ == "__main__": |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 739 | Tool().run() |