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