Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 2 | # |
| 3 | # trace Trace a function and print a trace message based on its |
| 4 | # parameters, with an optional filter. |
| 5 | # |
yonghong-song | c2a530b | 2019-10-20 09:35:55 -0700 | [diff] [blame] | 6 | # usage: trace [-h] [-p PID] [-L TID] [-v] [-Z STRING_SIZE] [-S] [-c cgroup_path] |
vijunag | 9924e64 | 2019-01-23 12:35:33 +0530 | [diff] [blame] | 7 | # [-M MAX_EVENTS] [-s SYMBOLFILES] [-T] [-t] [-K] [-U] [-a] [-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 |
Sumanth Korikkar | 7cbd074 | 2020-04-27 09:09:28 -0500 | [diff] [blame] | 14 | from bcc import BPF, USDT, StrcmpRewrite |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 15 | from functools import partial |
Jonathan Giddy | ec0691e | 2021-02-21 09:44:26 +0000 | [diff] [blame^] | 16 | from time import strftime |
Maik Riechert | 3a0d3c4 | 2019-05-23 17:57:10 +0100 | [diff] [blame] | 17 | import time |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 18 | import argparse |
| 19 | import re |
| 20 | import ctypes as ct |
| 21 | import os |
| 22 | import traceback |
| 23 | import sys |
| 24 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 25 | class Probe(object): |
| 26 | probe_count = 0 |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 27 | streq_index = 0 |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 28 | max_events = None |
| 29 | event_count = 0 |
| 30 | first_ts = 0 |
Maik Riechert | 3a0d3c4 | 2019-05-23 17:57:10 +0100 | [diff] [blame] | 31 | first_ts_real = None |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 32 | print_time = False |
Maik Riechert | 3a0d3c4 | 2019-05-23 17:57:10 +0100 | [diff] [blame] | 33 | print_unix_timestamp = False |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 34 | use_localtime = True |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 35 | time_field = False |
| 36 | print_cpu = False |
Mirek Klimos | e538228 | 2018-01-26 14:52:50 -0800 | [diff] [blame] | 37 | print_address = False |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 38 | tgid = -1 |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 39 | pid = -1 |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 40 | page_cnt = None |
vijunag | 9924e64 | 2019-01-23 12:35:33 +0530 | [diff] [blame] | 41 | build_id_enabled = False |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 42 | |
| 43 | @classmethod |
| 44 | def configure(cls, args): |
| 45 | cls.max_events = args.max_events |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 46 | cls.print_time = args.timestamp or args.time |
Maik Riechert | 3a0d3c4 | 2019-05-23 17:57:10 +0100 | [diff] [blame] | 47 | cls.print_unix_timestamp = args.unix_timestamp |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 48 | cls.use_localtime = not args.timestamp |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 49 | cls.time_field = cls.print_time and (not cls.use_localtime) |
| 50 | cls.print_cpu = args.print_cpu |
Mirek Klimos | e538228 | 2018-01-26 14:52:50 -0800 | [diff] [blame] | 51 | cls.print_address = args.address |
Sasha Goldshtein | 60c4192 | 2017-02-09 04:19:53 -0500 | [diff] [blame] | 52 | cls.first_ts = BPF.monotonic_time() |
Maik Riechert | 3a0d3c4 | 2019-05-23 17:57:10 +0100 | [diff] [blame] | 53 | cls.first_ts_real = time.time() |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 54 | cls.tgid = args.tgid or -1 |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 55 | cls.pid = args.pid or -1 |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 56 | cls.page_cnt = args.buffer_pages |
Nikita V. Shirokov | 3953c70 | 2018-07-27 16:13:47 -0700 | [diff] [blame] | 57 | cls.bin_cmp = args.bin_cmp |
vijunag | 9924e64 | 2019-01-23 12:35:33 +0530 | [diff] [blame] | 58 | cls.build_id_enabled = args.sym_file_list is not None |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 59 | |
yonghong-song | c2a530b | 2019-10-20 09:35:55 -0700 | [diff] [blame] | 60 | def __init__(self, probe, string_size, kernel_stack, user_stack, |
tty5 | 5cf529e | 2019-12-06 17:52:56 +0800 | [diff] [blame] | 61 | cgroup_map_name, name, msg_filter): |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 62 | self.usdt = None |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 63 | self.streq_functions = "" |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 64 | self.raw_probe = probe |
| 65 | self.string_size = string_size |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 66 | self.kernel_stack = kernel_stack |
| 67 | self.user_stack = user_stack |
Sumanth Korikkar | 7cbd074 | 2020-04-27 09:09:28 -0500 | [diff] [blame] | 68 | self.probe_user_list = set() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 69 | Probe.probe_count += 1 |
| 70 | self._parse_probe() |
| 71 | self.probe_num = Probe.probe_count |
| 72 | self.probe_name = "probe_%s_%d" % \ |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 73 | (self._display_function(), self.probe_num) |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 74 | self.probe_name = re.sub(r'[^A-Za-z0-9_]', '_', |
| 75 | self.probe_name) |
yonghong-song | c2a530b | 2019-10-20 09:35:55 -0700 | [diff] [blame] | 76 | self.cgroup_map_name = cgroup_map_name |
Jonathan Giddy | ec0691e | 2021-02-21 09:44:26 +0000 | [diff] [blame^] | 77 | if name is None: |
| 78 | # An empty bytestring is always contained in the command |
| 79 | # name so this will always succeed. |
| 80 | self.name = b'' |
| 81 | else: |
| 82 | self.name = name.encode('ascii') |
tty5 | 5cf529e | 2019-12-06 17:52:56 +0800 | [diff] [blame] | 83 | self.msg_filter = msg_filter |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 84 | # compiler can generate proper codes for function |
| 85 | # signatures with "syscall__" prefix |
| 86 | if self.is_syscall_kprobe: |
| 87 | self.probe_name = "syscall__" + self.probe_name[6:] |
| 88 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 89 | def __str__(self): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 90 | return "%s:%s:%s FLT=%s ACT=%s/%s" % (self.probe_type, |
| 91 | self.library, self._display_function(), self.filter, |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 92 | self.types, self.values) |
| 93 | |
| 94 | def is_default_action(self): |
| 95 | return self.python_format == "" |
| 96 | |
| 97 | def _bail(self, error): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 98 | raise ValueError("error in probe '%s': %s" % |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 99 | (self.raw_probe, error)) |
| 100 | |
| 101 | def _parse_probe(self): |
| 102 | text = self.raw_probe |
| 103 | |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 104 | # There might be a function signature preceding the actual |
| 105 | # filter/print part, or not. Find the probe specifier first -- |
| 106 | # it ends with either a space or an open paren ( for the |
| 107 | # function signature part. |
| 108 | # opt. signature |
| 109 | # probespec | rest |
| 110 | # --------- ---------- -- |
| 111 | (spec, sig, rest) = re.match(r'([^ \t\(]+)(\([^\(]*\))?(.*)', |
| 112 | text).groups() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 113 | |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 114 | self._parse_spec(spec) |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 115 | # Remove the parens |
| 116 | self.signature = sig[1:-1] if sig else None |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 117 | if self.signature and self.probe_type in ['u', 't']: |
| 118 | self._bail("USDT and tracepoint probes can't have " + |
| 119 | "a function signature; use arg1, arg2, " + |
| 120 | "... instead") |
| 121 | |
| 122 | text = rest.lstrip() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 123 | # If we now have a (, wait for the balanced closing ) and that |
| 124 | # will be the predicate |
| 125 | self.filter = None |
| 126 | if len(text) > 0 and text[0] == "(": |
| 127 | balance = 1 |
| 128 | for i in range(1, len(text)): |
| 129 | if text[i] == "(": |
| 130 | balance += 1 |
| 131 | if text[i] == ")": |
| 132 | balance -= 1 |
| 133 | if balance == 0: |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 134 | self._parse_filter(text[:i + 1]) |
| 135 | text = text[i + 1:] |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 136 | break |
| 137 | if self.filter is None: |
| 138 | self._bail("unmatched end of predicate") |
| 139 | |
| 140 | if self.filter is None: |
| 141 | self.filter = "1" |
| 142 | |
| 143 | # The remainder of the text is the printf action |
| 144 | self._parse_action(text.lstrip()) |
| 145 | |
Ferenc Fejes | d7b427e | 2020-08-01 21:18:57 +0200 | [diff] [blame] | 146 | def _parse_offset(self, func_and_offset): |
| 147 | func, offset_str = func_and_offset.split("+") |
| 148 | try: |
| 149 | if "x" in offset_str or "X" in offset_str: |
| 150 | offset = int(offset_str, 16) |
| 151 | else: |
| 152 | offset = int(offset_str) |
| 153 | except ValueError: |
| 154 | self._bail("invalid offset format " + |
| 155 | " '%s', must be decimal or hexadecimal" % offset_str) |
| 156 | |
| 157 | return func, offset |
| 158 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 159 | def _parse_spec(self, spec): |
| 160 | parts = spec.split(":") |
| 161 | # Two special cases: 'func' means 'p::func', 'lib:func' means |
| 162 | # 'p:lib:func'. Other combinations need to provide an empty |
| 163 | # value between delimiters, e.g. 'r::func' for a kretprobe on |
| 164 | # the function func. |
| 165 | if len(parts) == 1: |
| 166 | parts = ["p", "", parts[0]] |
| 167 | elif len(parts) == 2: |
| 168 | parts = ["p", parts[0], parts[1]] |
| 169 | if len(parts[0]) == 0: |
| 170 | self.probe_type = "p" |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 171 | elif parts[0] in ["p", "r", "t", "u"]: |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 172 | self.probe_type = parts[0] |
| 173 | else: |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 174 | self._bail("probe type must be '', 'p', 't', 'r', " + |
| 175 | "or 'u', but got '%s'" % parts[0]) |
Ferenc Fejes | d7b427e | 2020-08-01 21:18:57 +0200 | [diff] [blame] | 176 | self.offset = 0 |
| 177 | if "+" in parts[-1]: |
| 178 | parts[-1], self.offset = self._parse_offset(parts[-1]) |
| 179 | |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 180 | if self.probe_type == "t": |
| 181 | self.tp_category = parts[1] |
| 182 | self.tp_event = parts[2] |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 183 | self.library = "" # kernel |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 184 | self.function = "" # from TRACEPOINT_PROBE |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 185 | elif self.probe_type == "u": |
Fuji Goro | 2162516 | 2020-03-08 08:16:54 +0000 | [diff] [blame] | 186 | # u:<library>[:<provider>]:<probe> where :<provider> is optional |
| 187 | self.library = parts[1] |
| 188 | self.usdt_name = ":".join(parts[2:]) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 189 | self.function = "" # no function, just address |
| 190 | # We will discover the USDT provider by matching on |
| 191 | # the USDT name in the specified library |
| 192 | self._find_usdt_probe() |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 193 | else: |
vkhromov | 5a2b39e | 2017-07-14 20:42:29 +0100 | [diff] [blame] | 194 | self.library = ':'.join(parts[1:-1]) |
| 195 | self.function = parts[-1] |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 196 | |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 197 | # only x64 syscalls needs checking, no other syscall wrapper yet. |
| 198 | self.is_syscall_kprobe = False |
| 199 | if self.probe_type == "p" and len(self.library) == 0 and \ |
| 200 | self.function[:10] == "__x64_sys_": |
| 201 | self.is_syscall_kprobe = True |
| 202 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 203 | def _find_usdt_probe(self): |
Sasha Goldshtein | dd04536 | 2016-11-13 05:07:38 -0800 | [diff] [blame] | 204 | target = Probe.pid if Probe.pid and Probe.pid != -1 \ |
| 205 | else Probe.tgid |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 206 | self.usdt = USDT(path=self.library, pid=target) |
Fuji Goro | 2162516 | 2020-03-08 08:16:54 +0000 | [diff] [blame] | 207 | |
| 208 | parts = self.usdt_name.split(":") |
| 209 | if len(parts) == 1: |
| 210 | provider_name = None |
| 211 | usdt_name = parts[0].encode("ascii") |
| 212 | else: |
| 213 | provider_name = parts[0].encode("ascii") |
| 214 | usdt_name = parts[1].encode("ascii") |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 215 | for probe in self.usdt.enumerate_probes(): |
Fuji Goro | 2162516 | 2020-03-08 08:16:54 +0000 | [diff] [blame] | 216 | if ((not provider_name or probe.provider == provider_name) |
| 217 | and probe.name == usdt_name): |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 218 | return # Found it, will enable later |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 219 | self._bail("unrecognized USDT probe %s" % self.usdt_name) |
| 220 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 221 | def _parse_filter(self, filt): |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 222 | self.filter = self._rewrite_expr(filt) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 223 | |
| 224 | def _parse_types(self, fmt): |
| 225 | for match in re.finditer( |
yonghong-song | f720257 | 2018-09-19 08:50:59 -0700 | [diff] [blame] | 226 | r'[^%]%(s|u|d|lu|llu|ld|lld|hu|hd|x|lx|llx|c|K|U)', fmt): |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 227 | self.types.append(match.group(1)) |
yonghong-song | f720257 | 2018-09-19 08:50:59 -0700 | [diff] [blame] | 228 | fmt = re.sub(r'([^%]%)(u|d|lu|llu|ld|lld|hu|hd)', r'\1d', fmt) |
| 229 | fmt = re.sub(r'([^%]%)(x|lx|llx)', r'\1x', fmt) |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 230 | fmt = re.sub('%K|%U', '%s', fmt) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 231 | self.python_format = fmt.strip('"') |
| 232 | |
| 233 | def _parse_action(self, action): |
| 234 | self.values = [] |
| 235 | self.types = [] |
| 236 | self.python_format = "" |
| 237 | if len(action) == 0: |
| 238 | return |
| 239 | |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 240 | action = action.strip() |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 241 | match = re.search(r'(\".*?\"),?(.*)', action) |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 242 | if match is None: |
| 243 | self._bail("expected format string in \"s") |
| 244 | |
| 245 | self.raw_format = match.group(1) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 246 | self._parse_types(self.raw_format) |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 247 | for part in re.split('(?<!"),', match.group(2)): |
| 248 | part = self._rewrite_expr(part) |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 249 | if len(part) > 0: |
| 250 | self.values.append(part) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 251 | |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 252 | aliases_arg = { |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 253 | "arg1": "PT_REGS_PARM1(ctx)", |
| 254 | "arg2": "PT_REGS_PARM2(ctx)", |
| 255 | "arg3": "PT_REGS_PARM3(ctx)", |
| 256 | "arg4": "PT_REGS_PARM4(ctx)", |
| 257 | "arg5": "PT_REGS_PARM5(ctx)", |
| 258 | "arg6": "PT_REGS_PARM6(ctx)", |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | aliases_indarg = { |
Prashant Bhole | 05765ee | 2018-12-28 01:47:56 +0900 | [diff] [blame] | 262 | "arg1": "({u64 _val; struct pt_regs *_ctx = (struct pt_regs *)PT_REGS_PARM1(ctx);" |
Sumanth Korikkar | 7f6066d | 2020-05-20 10:49:56 -0500 | [diff] [blame] | 263 | " bpf_probe_read_kernel(&_val, sizeof(_val), &(PT_REGS_PARM1(_ctx))); _val;})", |
Xiaozhou Liu | 25a0ef3 | 2019-01-14 14:14:43 +0800 | [diff] [blame] | 264 | "arg2": "({u64 _val; struct pt_regs *_ctx = (struct pt_regs *)PT_REGS_PARM1(ctx);" |
Sumanth Korikkar | 7f6066d | 2020-05-20 10:49:56 -0500 | [diff] [blame] | 265 | " bpf_probe_read_kernel(&_val, sizeof(_val), &(PT_REGS_PARM2(_ctx))); _val;})", |
Xiaozhou Liu | 25a0ef3 | 2019-01-14 14:14:43 +0800 | [diff] [blame] | 266 | "arg3": "({u64 _val; struct pt_regs *_ctx = (struct pt_regs *)PT_REGS_PARM1(ctx);" |
Sumanth Korikkar | 7f6066d | 2020-05-20 10:49:56 -0500 | [diff] [blame] | 267 | " bpf_probe_read_kernel(&_val, sizeof(_val), &(PT_REGS_PARM3(_ctx))); _val;})", |
Xiaozhou Liu | 25a0ef3 | 2019-01-14 14:14:43 +0800 | [diff] [blame] | 268 | "arg4": "({u64 _val; struct pt_regs *_ctx = (struct pt_regs *)PT_REGS_PARM1(ctx);" |
Sumanth Korikkar | 7f6066d | 2020-05-20 10:49:56 -0500 | [diff] [blame] | 269 | " bpf_probe_read_kernel(&_val, sizeof(_val), &(PT_REGS_PARM4(_ctx))); _val;})", |
Xiaozhou Liu | 25a0ef3 | 2019-01-14 14:14:43 +0800 | [diff] [blame] | 270 | "arg5": "({u64 _val; struct pt_regs *_ctx = (struct pt_regs *)PT_REGS_PARM1(ctx);" |
Sumanth Korikkar | 7f6066d | 2020-05-20 10:49:56 -0500 | [diff] [blame] | 271 | " bpf_probe_read_kernel(&_val, sizeof(_val), &(PT_REGS_PARM5(_ctx))); _val;})", |
Xiaozhou Liu | 25a0ef3 | 2019-01-14 14:14:43 +0800 | [diff] [blame] | 272 | "arg6": "({u64 _val; struct pt_regs *_ctx = (struct pt_regs *)PT_REGS_PARM1(ctx);" |
Sumanth Korikkar | 7f6066d | 2020-05-20 10:49:56 -0500 | [diff] [blame] | 273 | " bpf_probe_read_kernel(&_val, sizeof(_val), &(PT_REGS_PARM6(_ctx))); _val;})", |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | aliases_common = { |
| 277 | "retval": "PT_REGS_RC(ctx)", |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 278 | "$uid": "(unsigned)(bpf_get_current_uid_gid() & 0xffffffff)", |
| 279 | "$gid": "(unsigned)(bpf_get_current_uid_gid() >> 32)", |
| 280 | "$pid": "(unsigned)(bpf_get_current_pid_tgid() & 0xffffffff)", |
| 281 | "$tgid": "(unsigned)(bpf_get_current_pid_tgid() >> 32)", |
Yonghong Song | f92fef2 | 2018-01-24 20:51:46 -0800 | [diff] [blame] | 282 | "$cpu": "bpf_get_smp_processor_id()", |
Jonathan Giddy | ec0691e | 2021-02-21 09:44:26 +0000 | [diff] [blame^] | 283 | "$task": "((struct task_struct *)bpf_get_current_task())" |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 286 | def _rewrite_expr(self, expr): |
Sumanth Korikkar | 7cbd074 | 2020-04-27 09:09:28 -0500 | [diff] [blame] | 287 | # Find the occurances of any arg[1-6]@user. Use it later to |
| 288 | # identify bpf_probe_read_user |
| 289 | for matches in re.finditer(r'(arg[1-6])(@user)', expr): |
| 290 | if matches.group(1).strip() not in self.probe_user_list: |
| 291 | self.probe_user_list.add(matches.group(1).strip()) |
| 292 | # Remove @user occurrences from arg before resolving to its |
| 293 | # corresponding aliases. |
| 294 | expr = re.sub(r'(arg[1-6])@user', r'\1', expr) |
| 295 | rdict = StrcmpRewrite.rewrite_expr(expr, |
| 296 | self.bin_cmp, self.library, |
| 297 | self.probe_user_list, self.streq_functions, |
| 298 | Probe.streq_index) |
| 299 | expr = rdict["expr"] |
| 300 | self.streq_functions = rdict["streq_functions"] |
| 301 | Probe.streq_index = rdict["probeid"] |
| 302 | alias_to_check = Probe.aliases_indarg \ |
| 303 | if self.is_syscall_kprobe \ |
| 304 | else Probe.aliases_arg |
| 305 | # For USDT probes, we replace argN values with the |
| 306 | # actual arguments for that probe obtained using |
| 307 | # bpf_readarg_N macros emitted at BPF construction. |
| 308 | if not self.probe_type == "u": |
| 309 | for alias, replacement in alias_to_check.items(): |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 310 | expr = expr.replace(alias, replacement) |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 311 | for alias, replacement in Probe.aliases_common.items(): |
| 312 | expr = expr.replace(alias, replacement) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 313 | return expr |
| 314 | |
yonghong-song | f720257 | 2018-09-19 08:50:59 -0700 | [diff] [blame] | 315 | p_type = {"u": ct.c_uint, "d": ct.c_int, "lu": ct.c_ulong, |
| 316 | "ld": ct.c_long, |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 317 | "llu": ct.c_ulonglong, "lld": ct.c_longlong, |
| 318 | "hu": ct.c_ushort, "hd": ct.c_short, |
yonghong-song | f720257 | 2018-09-19 08:50:59 -0700 | [diff] [blame] | 319 | "x": ct.c_uint, "lx": ct.c_ulong, "llx": ct.c_ulonglong, |
| 320 | "c": ct.c_ubyte, |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 321 | "K": ct.c_ulonglong, "U": ct.c_ulonglong} |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 322 | |
| 323 | def _generate_python_field_decl(self, idx, fields): |
| 324 | field_type = self.types[idx] |
| 325 | if field_type == "s": |
| 326 | ptype = ct.c_char * self.string_size |
| 327 | else: |
| 328 | ptype = Probe.p_type[field_type] |
| 329 | fields.append(("v%d" % idx, ptype)) |
| 330 | |
| 331 | def _generate_python_data_decl(self): |
| 332 | self.python_struct_name = "%s_%d_Data" % \ |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 333 | (self._display_function(), self.probe_num) |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 334 | fields = [] |
| 335 | if self.time_field: |
| 336 | fields.append(("timestamp_ns", ct.c_ulonglong)) |
| 337 | if self.print_cpu: |
| 338 | fields.append(("cpu", ct.c_int)) |
| 339 | fields.extend([ |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 340 | ("tgid", ct.c_uint), |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 341 | ("pid", ct.c_uint), |
| 342 | ("comm", ct.c_char * 16) # TASK_COMM_LEN |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 343 | ]) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 344 | for i in range(0, len(self.types)): |
| 345 | self._generate_python_field_decl(i, fields) |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 346 | if self.kernel_stack: |
| 347 | fields.append(("kernel_stack_id", ct.c_int)) |
| 348 | if self.user_stack: |
| 349 | fields.append(("user_stack_id", ct.c_int)) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 350 | return type(self.python_struct_name, (ct.Structure,), |
| 351 | dict(_fields_=fields)) |
| 352 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 353 | c_type = {"u": "unsigned int", "d": "int", |
yonghong-song | f720257 | 2018-09-19 08:50:59 -0700 | [diff] [blame] | 354 | "lu": "unsigned long", "ld": "long", |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 355 | "llu": "unsigned long long", "lld": "long long", |
| 356 | "hu": "unsigned short", "hd": "short", |
yonghong-song | f720257 | 2018-09-19 08:50:59 -0700 | [diff] [blame] | 357 | "x": "unsigned int", "lx": "unsigned long", |
| 358 | "llx": "unsigned long long", |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 359 | "c": "char", "K": "unsigned long long", |
| 360 | "U": "unsigned long long"} |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 361 | fmt_types = c_type.keys() |
| 362 | |
| 363 | def _generate_field_decl(self, idx): |
| 364 | field_type = self.types[idx] |
| 365 | if field_type == "s": |
| 366 | return "char v%d[%d];\n" % (idx, self.string_size) |
| 367 | if field_type in Probe.fmt_types: |
| 368 | return "%s v%d;\n" % (Probe.c_type[field_type], idx) |
| 369 | self._bail("unrecognized format specifier %s" % field_type) |
| 370 | |
| 371 | def _generate_data_decl(self): |
| 372 | # The BPF program will populate values into the struct |
| 373 | # according to the format string, and the Python program will |
| 374 | # construct the final display string. |
| 375 | self.events_name = "%s_events" % self.probe_name |
| 376 | self.struct_name = "%s_data_t" % self.probe_name |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 377 | self.stacks_name = "%s_stacks" % self.probe_name |
vijunag | 9924e64 | 2019-01-23 12:35:33 +0530 | [diff] [blame] | 378 | stack_type = "BPF_STACK_TRACE" if self.build_id_enabled is False \ |
| 379 | else "BPF_STACK_TRACE_BUILDID" |
Jonathan Giddy | ec0691e | 2021-02-21 09:44:26 +0000 | [diff] [blame^] | 380 | stack_table = "%s(%s, 1024);" % (stack_type, self.stacks_name) \ |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 381 | if (self.kernel_stack or self.user_stack) else "" |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 382 | data_fields = "" |
| 383 | for i, field_type in enumerate(self.types): |
| 384 | data_fields += " " + \ |
| 385 | self._generate_field_decl(i) |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 386 | time_str = "u64 timestamp_ns;" if self.time_field else "" |
| 387 | cpu_str = "int cpu;" if self.print_cpu else "" |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 388 | kernel_stack_str = " int kernel_stack_id;" \ |
| 389 | if self.kernel_stack else "" |
| 390 | user_stack_str = " int user_stack_id;" \ |
| 391 | if self.user_stack else "" |
| 392 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 393 | text = """ |
| 394 | struct %s |
| 395 | { |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 396 | %s |
| 397 | %s |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 398 | u32 tgid; |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 399 | u32 pid; |
| 400 | char comm[TASK_COMM_LEN]; |
| 401 | %s |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 402 | %s |
| 403 | %s |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 404 | }; |
| 405 | |
| 406 | BPF_PERF_OUTPUT(%s); |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 407 | %s |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 408 | """ |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 409 | return text % (self.struct_name, time_str, cpu_str, data_fields, |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 410 | kernel_stack_str, user_stack_str, |
| 411 | self.events_name, stack_table) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 412 | |
| 413 | def _generate_field_assign(self, idx): |
| 414 | field_type = self.types[idx] |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 415 | expr = self.values[idx].strip() |
| 416 | text = "" |
| 417 | if self.probe_type == "u" and expr[0:3] == "arg": |
Sasha Goldshtein | 3a5256f | 2017-02-20 15:42:57 +0000 | [diff] [blame] | 418 | arg_index = int(expr[3]) |
| 419 | arg_ctype = self.usdt.get_probe_arg_ctype( |
| 420 | self.usdt_name, arg_index - 1) |
| 421 | text = (" %s %s = 0;\n" + |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 422 | " bpf_usdt_readarg(%s, ctx, &%s);\n") \ |
Sasha Goldshtein | 3a5256f | 2017-02-20 15:42:57 +0000 | [diff] [blame] | 423 | % (arg_ctype, expr, expr[3], expr) |
Sumanth Korikkar | 7f6066d | 2020-05-20 10:49:56 -0500 | [diff] [blame] | 424 | probe_read_func = "bpf_probe_read_kernel" |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 425 | if field_type == "s": |
Sumanth Korikkar | 7cbd074 | 2020-04-27 09:09:28 -0500 | [diff] [blame] | 426 | if self.library: |
| 427 | probe_read_func = "bpf_probe_read_user" |
| 428 | else: |
| 429 | alias_to_check = Probe.aliases_indarg \ |
| 430 | if self.is_syscall_kprobe \ |
| 431 | else Probe.aliases_arg |
| 432 | for arg, alias in alias_to_check.items(): |
| 433 | if alias == expr and arg in self.probe_user_list: |
| 434 | probe_read_func = "bpf_probe_read_user" |
| 435 | break |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 436 | return text + """ |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 437 | if (%s != 0) { |
yonghong-song | 61484e1 | 2018-09-17 22:24:31 -0700 | [diff] [blame] | 438 | void *__tmp = (void *)%s; |
Sumanth Korikkar | 7cbd074 | 2020-04-27 09:09:28 -0500 | [diff] [blame] | 439 | %s(&__data.v%d, sizeof(__data.v%d), __tmp); |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 440 | } |
Sumanth Korikkar | 7cbd074 | 2020-04-27 09:09:28 -0500 | [diff] [blame] | 441 | """ % (expr, expr, probe_read_func, idx, idx) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 442 | if field_type in Probe.fmt_types: |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 443 | return text + " __data.v%d = (%s)%s;\n" % \ |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 444 | (idx, Probe.c_type[field_type], expr) |
| 445 | self._bail("unrecognized field type %s" % field_type) |
| 446 | |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 447 | def _generate_usdt_filter_read(self): |
| 448 | text = "" |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 449 | if self.probe_type != "u": |
| 450 | return text |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 451 | for arg, _ in Probe.aliases_arg.items(): |
| 452 | if not (arg in self.filter): |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 453 | continue |
| 454 | arg_index = int(arg.replace("arg", "")) |
| 455 | arg_ctype = self.usdt.get_probe_arg_ctype( |
Sasha Goldshtein | dcf1675 | 2017-01-17 07:40:57 +0000 | [diff] [blame] | 456 | self.usdt_name, arg_index - 1) |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 457 | if not arg_ctype: |
| 458 | self._bail("Unable to determine type of {} " |
| 459 | "in the filter".format(arg)) |
| 460 | text += """ |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 461 | {} {}_filter; |
| 462 | bpf_usdt_readarg({}, ctx, &{}_filter); |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 463 | """.format(arg_ctype, arg, arg_index, arg) |
| 464 | self.filter = self.filter.replace( |
| 465 | arg, "{}_filter".format(arg)) |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 466 | return text |
| 467 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 468 | def generate_program(self, include_self): |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 469 | data_decl = self._generate_data_decl() |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 470 | if Probe.pid != -1: |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 471 | pid_filter = """ |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 472 | if (__pid != %d) { return 0; } |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 473 | """ % Probe.pid |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 474 | # uprobes can have a built-in tgid filter passed to |
| 475 | # attach_uprobe, hence the check here -- for kprobes, we |
| 476 | # need to do the tgid test by hand: |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 477 | elif len(self.library) == 0 and Probe.tgid != -1: |
| 478 | pid_filter = """ |
| 479 | if (__tgid != %d) { return 0; } |
| 480 | """ % Probe.tgid |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 481 | elif not include_self: |
| 482 | pid_filter = """ |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 483 | if (__tgid == %d) { return 0; } |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 484 | """ % os.getpid() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 485 | else: |
| 486 | pid_filter = "" |
| 487 | |
yonghong-song | c2a530b | 2019-10-20 09:35:55 -0700 | [diff] [blame] | 488 | if self.cgroup_map_name is not None: |
| 489 | cgroup_filter = """ |
| 490 | if (%s.check_current_task(0) <= 0) { return 0; } |
| 491 | """ % self.cgroup_map_name |
| 492 | else: |
| 493 | cgroup_filter = "" |
| 494 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 495 | prefix = "" |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 496 | signature = "struct pt_regs *ctx" |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 497 | if self.signature: |
| 498 | signature += ", " + self.signature |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 499 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 500 | data_fields = "" |
| 501 | for i, expr in enumerate(self.values): |
| 502 | data_fields += self._generate_field_assign(i) |
| 503 | |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 504 | if self.probe_type == "t": |
| 505 | heading = "TRACEPOINT_PROBE(%s, %s)" % \ |
| 506 | (self.tp_category, self.tp_event) |
| 507 | ctx_name = "args" |
| 508 | else: |
| 509 | heading = "int %s(%s)" % (self.probe_name, signature) |
| 510 | ctx_name = "ctx" |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 511 | |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 512 | time_str = """ |
| 513 | __data.timestamp_ns = bpf_ktime_get_ns();""" if self.time_field else "" |
| 514 | cpu_str = """ |
| 515 | __data.cpu = bpf_get_smp_processor_id();""" if self.print_cpu else "" |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 516 | stack_trace = "" |
| 517 | if self.user_stack: |
| 518 | stack_trace += """ |
| 519 | __data.user_stack_id = %s.get_stackid( |
Yonghong Song | 90f2086 | 2019-11-27 09:16:23 -0800 | [diff] [blame] | 520 | %s, BPF_F_USER_STACK |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 521 | );""" % (self.stacks_name, ctx_name) |
| 522 | if self.kernel_stack: |
| 523 | stack_trace += """ |
| 524 | __data.kernel_stack_id = %s.get_stackid( |
Yonghong Song | 90f2086 | 2019-11-27 09:16:23 -0800 | [diff] [blame] | 525 | %s, 0 |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 526 | );""" % (self.stacks_name, ctx_name) |
| 527 | |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 528 | text = heading + """ |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 529 | { |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 530 | u64 __pid_tgid = bpf_get_current_pid_tgid(); |
| 531 | u32 __tgid = __pid_tgid >> 32; |
| 532 | u32 __pid = __pid_tgid; // implicit cast to u32 for bottom half |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 533 | %s |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 534 | %s |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 535 | %s |
yonghong-song | c2a530b | 2019-10-20 09:35:55 -0700 | [diff] [blame] | 536 | %s |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 537 | if (!(%s)) return 0; |
| 538 | |
| 539 | struct %s __data = {0}; |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 540 | %s |
| 541 | %s |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 542 | __data.tgid = __tgid; |
| 543 | __data.pid = __pid; |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 544 | bpf_get_current_comm(&__data.comm, sizeof(__data.comm)); |
| 545 | %s |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 546 | %s |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 547 | %s.perf_submit(%s, &__data, sizeof(__data)); |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 548 | return 0; |
| 549 | } |
| 550 | """ |
yonghong-song | c2a530b | 2019-10-20 09:35:55 -0700 | [diff] [blame] | 551 | text = text % (pid_filter, cgroup_filter, prefix, |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 552 | self._generate_usdt_filter_read(), self.filter, |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 553 | self.struct_name, time_str, cpu_str, data_fields, |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 554 | stack_trace, self.events_name, ctx_name) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 555 | |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 556 | return self.streq_functions + data_decl + "\n" + text |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 557 | |
| 558 | @classmethod |
| 559 | def _time_off_str(cls, timestamp_ns): |
Maik Riechert | 3a0d3c4 | 2019-05-23 17:57:10 +0100 | [diff] [blame] | 560 | offset = 1e-9 * (timestamp_ns - cls.first_ts) |
| 561 | if cls.print_unix_timestamp: |
| 562 | return "%.6f" % (offset + cls.first_ts_real) |
| 563 | else: |
| 564 | return "%.6f" % offset |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 565 | |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 566 | def _display_function(self): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 567 | if self.probe_type == 'p' or self.probe_type == 'r': |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 568 | return self.function |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 569 | elif self.probe_type == 'u': |
| 570 | return self.usdt_name |
| 571 | else: # self.probe_type == 't' |
| 572 | return self.tp_event |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 573 | |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 574 | def print_stack(self, bpf, stack_id, tgid): |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 575 | if stack_id < 0: |
Mirek Klimos | e538228 | 2018-01-26 14:52:50 -0800 | [diff] [blame] | 576 | print(" %d" % stack_id) |
| 577 | return |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 578 | |
| 579 | stack = list(bpf.get_table(self.stacks_name).walk(stack_id)) |
| 580 | for addr in stack: |
Mirek Klimos | e538228 | 2018-01-26 14:52:50 -0800 | [diff] [blame] | 581 | print(" ", end="") |
| 582 | if Probe.print_address: |
| 583 | print("%16x " % addr, end="") |
| 584 | print("%s" % (bpf.sym(addr, tgid, |
| 585 | show_module=True, show_offset=True))) |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 586 | |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 587 | def _format_message(self, bpf, tgid, values): |
| 588 | # 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] | 589 | kernel_placeholders = [i for i, t in enumerate(self.types) |
| 590 | if t == 'K'] |
| 591 | user_placeholders = [i for i, t in enumerate(self.types) |
| 592 | if t == 'U'] |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 593 | for kp in kernel_placeholders: |
Sasha Goldshtein | 0155385 | 2017-02-09 03:58:09 -0500 | [diff] [blame] | 594 | values[kp] = bpf.ksym(values[kp], show_offset=True) |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 595 | for up in user_placeholders: |
Sasha Goldshtein | 1e34f4e | 2017-02-09 00:21:49 -0500 | [diff] [blame] | 596 | values[up] = bpf.sym(values[up], tgid, |
Sasha Goldshtein | 0155385 | 2017-02-09 03:58:09 -0500 | [diff] [blame] | 597 | show_module=True, show_offset=True) |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 598 | return self.python_format % tuple(values) |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 599 | |
| 600 | def print_event(self, bpf, cpu, data, size): |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 601 | # Cast as the generated structure type and display |
| 602 | # according to the format string in the probe. |
| 603 | event = ct.cast(data, ct.POINTER(self.python_struct)).contents |
Jonathan Giddy | ec0691e | 2021-02-21 09:44:26 +0000 | [diff] [blame^] | 604 | if self.name not in event.comm: |
tty5 | 9ce7b7e | 2019-12-04 22:49:38 +0800 | [diff] [blame] | 605 | return |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 606 | values = map(lambda i: getattr(event, "v%d" % i), |
| 607 | range(0, len(self.values))) |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 608 | msg = self._format_message(bpf, event.tgid, values) |
Jonathan Giddy | ec0691e | 2021-02-21 09:44:26 +0000 | [diff] [blame^] | 609 | if self.msg_filter and self.msg_filter not in msg: |
tty5 | 5cf529e | 2019-12-06 17:52:56 +0800 | [diff] [blame] | 610 | return |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 611 | if Probe.print_time: |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 612 | time = strftime("%H:%M:%S") if Probe.use_localtime else \ |
| 613 | Probe._time_off_str(event.timestamp_ns) |
Maik Riechert | 3a0d3c4 | 2019-05-23 17:57:10 +0100 | [diff] [blame] | 614 | if Probe.print_unix_timestamp: |
| 615 | print("%-17s " % time[:17], end="") |
| 616 | else: |
| 617 | print("%-8s " % time[:8], end="") |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 618 | if Probe.print_cpu: |
| 619 | print("%-3s " % event.cpu, end="") |
| 620 | print("%-7d %-7d %-15s %-16s %s" % |
jeromemarchand | b96ebcd | 2018-10-10 01:58:15 +0200 | [diff] [blame] | 621 | (event.tgid, event.pid, |
| 622 | event.comm.decode('utf-8', 'replace'), |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 623 | self._display_function(), msg)) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 624 | |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 625 | if self.kernel_stack: |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 626 | self.print_stack(bpf, event.kernel_stack_id, -1) |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 627 | if self.user_stack: |
| 628 | self.print_stack(bpf, event.user_stack_id, event.tgid) |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 629 | if self.user_stack or self.kernel_stack: |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 630 | print("") |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 631 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 632 | Probe.event_count += 1 |
| 633 | if Probe.max_events is not None and \ |
| 634 | Probe.event_count >= Probe.max_events: |
| 635 | exit() |
Alban Crequy | 8bb4e47 | 2019-12-21 16:09:53 +0100 | [diff] [blame] | 636 | sys.stdout.flush() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 637 | |
| 638 | def attach(self, bpf, verbose): |
| 639 | if len(self.library) == 0: |
| 640 | self._attach_k(bpf) |
| 641 | else: |
| 642 | self._attach_u(bpf) |
| 643 | self.python_struct = self._generate_python_data_decl() |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 644 | callback = partial(self.print_event, bpf) |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 645 | bpf[self.events_name].open_perf_buffer(callback, |
| 646 | page_cnt=self.page_cnt) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 647 | |
| 648 | def _attach_k(self, bpf): |
| 649 | if self.probe_type == "r": |
| 650 | bpf.attach_kretprobe(event=self.function, |
| 651 | fn_name=self.probe_name) |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 652 | elif self.probe_type == "p": |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 653 | bpf.attach_kprobe(event=self.function, |
Ferenc Fejes | d7b427e | 2020-08-01 21:18:57 +0200 | [diff] [blame] | 654 | fn_name=self.probe_name, |
| 655 | event_off=self.offset) |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 656 | # Note that tracepoints don't need an explicit attach |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 657 | |
| 658 | def _attach_u(self, bpf): |
| 659 | libpath = BPF.find_library(self.library) |
| 660 | if libpath is None: |
| 661 | # This might be an executable (e.g. 'bash') |
Sasha Goldshtein | ec67971 | 2016-10-04 18:33:36 +0300 | [diff] [blame] | 662 | libpath = BPF.find_exe(self.library) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 663 | if libpath is None or len(libpath) == 0: |
| 664 | self._bail("unable to find library %s" % self.library) |
| 665 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 666 | if self.probe_type == "u": |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 667 | pass # Was already enabled by the BPF constructor |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 668 | elif self.probe_type == "r": |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 669 | bpf.attach_uretprobe(name=libpath, |
| 670 | sym=self.function, |
| 671 | fn_name=self.probe_name, |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 672 | pid=Probe.tgid) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 673 | else: |
| 674 | bpf.attach_uprobe(name=libpath, |
| 675 | sym=self.function, |
| 676 | fn_name=self.probe_name, |
Ferenc Fejes | d7b427e | 2020-08-01 21:18:57 +0200 | [diff] [blame] | 677 | pid=Probe.tgid, |
| 678 | sym_off=self.offset) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 679 | |
| 680 | class Tool(object): |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 681 | DEFAULT_PERF_BUFFER_PAGES = 64 |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 682 | examples = """ |
| 683 | EXAMPLES: |
| 684 | |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 685 | trace do_sys_open |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 686 | Trace the open syscall and print a default trace message when entered |
Ferenc Fejes | d7b427e | 2020-08-01 21:18:57 +0200 | [diff] [blame] | 687 | trace kfree_skb+0x12 |
| 688 | Trace the kfree_skb kernel function after the instruction on the 0x12 offset |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 689 | trace 'do_sys_open "%s", arg2' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 690 | Trace the open syscall and print the filename being opened |
tty5 | 9ce7b7e | 2019-12-04 22:49:38 +0800 | [diff] [blame] | 691 | trace 'do_sys_open "%s", arg2' -n main |
| 692 | Trace the open syscall and only print event that process names containing "main" |
tty5 | 5cf529e | 2019-12-06 17:52:56 +0800 | [diff] [blame] | 693 | trace 'do_sys_open "%s", arg2' -f config |
| 694 | Trace the open syscall and print the filename being opened filtered by "config" |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 695 | trace 'sys_read (arg3 > 20000) "read %d bytes", arg3' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 696 | Trace the read syscall and print a message for reads >20000 bytes |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 697 | trace 'r::do_sys_open "%llx", retval' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 698 | Trace the return from the open syscall and print the return value |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 699 | trace 'c:open (arg2 == 42) "%s %d", arg1, arg2' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 700 | 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] | 701 | trace 'c:malloc "size = %d", arg1' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 702 | Trace malloc calls and print the size being allocated |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 703 | trace 'p:c:write (arg1 == 1) "writing %d bytes to STDOUT", arg3' |
| 704 | Trace the write() call from libc to monitor writes to STDOUT |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 705 | trace 'r::__kmalloc (retval == 0) "kmalloc failed!"' |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 706 | Trace returns from __kmalloc which returned a null pointer |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 707 | trace 'r:c:malloc (retval) "allocated = %x", retval' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 708 | Trace returns from malloc and print non-NULL allocated buffers |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 709 | trace 't:block:block_rq_complete "sectors=%d", args->nr_sector' |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 710 | Trace the block_rq_complete kernel tracepoint and print # of tx sectors |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 711 | trace 'u:pthread:pthread_create (arg4 != 0)' |
| 712 | Trace the USDT probe pthread_create when its 4th argument is non-zero |
Fuji Goro | 2162516 | 2020-03-08 08:16:54 +0000 | [diff] [blame] | 713 | trace 'u:pthread:libpthread:pthread_create (arg4 != 0)' |
| 714 | Ditto, but the provider name "libpthread" is specified. |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 715 | trace 'p::SyS_nanosleep(struct timespec *ts) "sleep for %lld ns", ts->tv_nsec' |
| 716 | Trace the nanosleep syscall and print the sleep duration in ns |
yonghong-song | c2a530b | 2019-10-20 09:35:55 -0700 | [diff] [blame] | 717 | trace -c /sys/fs/cgroup/system.slice/workload.service '__x64_sys_nanosleep' '__x64_sys_clone' |
| 718 | Trace nanosleep/clone syscall calls only under workload.service |
| 719 | cgroup hierarchy. |
Yonghong Song | f4470dc | 2017-12-13 14:12:13 -0800 | [diff] [blame] | 720 | trace -I 'linux/fs.h' \\ |
| 721 | 'p::uprobe_register(struct inode *inode) "a_ops = %llx", inode->i_mapping->a_ops' |
| 722 | Trace the uprobe_register inode mapping ops, and the symbol can be found |
| 723 | in /proc/kallsyms |
| 724 | trace -I 'kernel/sched/sched.h' \\ |
| 725 | 'p::__account_cfs_rq_runtime(struct cfs_rq *cfs_rq) "%d", cfs_rq->runtime_remaining' |
| 726 | Trace the cfs scheduling runqueue remaining runtime. The struct cfs_rq is defined |
| 727 | in kernel/sched/sched.h which is in kernel source tree and not in kernel-devel |
| 728 | package. So this command needs to run at the kernel source tree root directory |
| 729 | so that the added header file can be found by the compiler. |
tehnerd | 86293f0 | 2018-01-23 21:21:58 -0800 | [diff] [blame] | 730 | trace -I 'net/sock.h' \\ |
| 731 | 'udpv6_sendmsg(struct sock *sk) (sk->sk_dport == 13568)' |
| 732 | Trace udpv6 sendmsg calls only if socket's destination port is equal |
| 733 | to 53 (DNS; 13568 in big endian order) |
Yonghong Song | f92fef2 | 2018-01-24 20:51:46 -0800 | [diff] [blame] | 734 | trace -I 'linux/fs_struct.h' 'mntns_install "users = %d", $task->fs->users' |
| 735 | 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] | 736 | """ |
| 737 | |
| 738 | def __init__(self): |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 739 | parser = argparse.ArgumentParser(description="Attach to " + |
| 740 | "functions and print trace messages.", |
| 741 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 742 | epilog=Tool.examples) |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 743 | parser.add_argument("-b", "--buffer-pages", type=int, |
| 744 | default=Tool.DEFAULT_PERF_BUFFER_PAGES, |
| 745 | help="number of pages to use for perf_events ring buffer " |
| 746 | "(default: %(default)d)") |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 747 | # we'll refer to the userspace concepts of "pid" and "tid" by |
| 748 | # their kernel names -- tgid and pid -- inside the script |
| 749 | parser.add_argument("-p", "--pid", type=int, metavar="PID", |
| 750 | dest="tgid", help="id of the process to trace (optional)") |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 751 | parser.add_argument("-L", "--tid", type=int, metavar="TID", |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 752 | dest="pid", help="id of the thread to trace (optional)") |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 753 | parser.add_argument("-v", "--verbose", action="store_true", |
| 754 | help="print resulting BPF program code before executing") |
| 755 | parser.add_argument("-Z", "--string-size", type=int, |
| 756 | default=80, help="maximum size to read from strings") |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 757 | parser.add_argument("-S", "--include-self", |
| 758 | action="store_true", |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 759 | help="do not filter trace's own pid from the trace") |
| 760 | parser.add_argument("-M", "--max-events", type=int, |
| 761 | help="number of events to print before quitting") |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 762 | parser.add_argument("-t", "--timestamp", action="store_true", |
| 763 | help="print timestamp column (offset from trace start)") |
Maik Riechert | 3a0d3c4 | 2019-05-23 17:57:10 +0100 | [diff] [blame] | 764 | parser.add_argument("-u", "--unix-timestamp", action="store_true", |
| 765 | help="print UNIX timestamp instead of offset from trace start, requires -t") |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 766 | parser.add_argument("-T", "--time", action="store_true", |
| 767 | help="print time column") |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 768 | parser.add_argument("-C", "--print_cpu", action="store_true", |
| 769 | help="print CPU id") |
Jonathan Giddy | ec0691e | 2021-02-21 09:44:26 +0000 | [diff] [blame^] | 770 | parser.add_argument("-c", "--cgroup-path", type=str, |
| 771 | metavar="CGROUP_PATH", dest="cgroup_path", |
yonghong-song | c2a530b | 2019-10-20 09:35:55 -0700 | [diff] [blame] | 772 | help="cgroup path") |
tty5 | 9ce7b7e | 2019-12-04 22:49:38 +0800 | [diff] [blame] | 773 | parser.add_argument("-n", "--name", type=str, |
| 774 | help="only print process names containing this name") |
tty5 | 5cf529e | 2019-12-06 17:52:56 +0800 | [diff] [blame] | 775 | parser.add_argument("-f", "--msg-filter", type=str, dest="msg_filter", |
| 776 | help="only print the msg of event containing this string") |
Nikita V. Shirokov | 3953c70 | 2018-07-27 16:13:47 -0700 | [diff] [blame] | 777 | parser.add_argument("-B", "--bin_cmp", action="store_true", |
| 778 | help="allow to use STRCMP with binary values") |
Jonathan Giddy | ec0691e | 2021-02-21 09:44:26 +0000 | [diff] [blame^] | 779 | parser.add_argument('-s', "--sym_file_list", type=str, |
| 780 | metavar="SYM_FILE_LIST", dest="sym_file_list", |
vijunag | 9924e64 | 2019-01-23 12:35:33 +0530 | [diff] [blame] | 781 | help="coma separated list of symbol files to use \ |
| 782 | for symbol resolution") |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 783 | parser.add_argument("-K", "--kernel-stack", |
| 784 | action="store_true", help="output kernel stack trace") |
| 785 | parser.add_argument("-U", "--user-stack", |
| 786 | action="store_true", help="output user stack trace") |
Mirek Klimos | e538228 | 2018-01-26 14:52:50 -0800 | [diff] [blame] | 787 | parser.add_argument("-a", "--address", action="store_true", |
| 788 | help="print virtual address in stacks") |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 789 | parser.add_argument(metavar="probe", dest="probes", nargs="+", |
| 790 | help="probe specifier (see examples)") |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 791 | parser.add_argument("-I", "--include", action="append", |
| 792 | metavar="header", |
ShelbyFrances | f5dbbdb | 2017-02-08 05:56:52 +0300 | [diff] [blame] | 793 | help="additional header files to include in the BPF program " |
Yonghong Song | f4470dc | 2017-12-13 14:12:13 -0800 | [diff] [blame] | 794 | "as either full path, " |
| 795 | "or relative to current working directory, " |
| 796 | "or relative to default kernel header search path") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 797 | parser.add_argument("--ebpf", action="store_true", |
| 798 | help=argparse.SUPPRESS) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 799 | self.args = parser.parse_args() |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 800 | if self.args.tgid and self.args.pid: |
Yonghong Song | f4470dc | 2017-12-13 14:12:13 -0800 | [diff] [blame] | 801 | parser.error("only one of -p and -L may be specified") |
yonghong-song | c2a530b | 2019-10-20 09:35:55 -0700 | [diff] [blame] | 802 | if self.args.cgroup_path is not None: |
| 803 | self.cgroup_map_name = "__cgroup" |
| 804 | else: |
| 805 | self.cgroup_map_name = None |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 806 | |
| 807 | def _create_probes(self): |
| 808 | Probe.configure(self.args) |
| 809 | self.probes = [] |
| 810 | for probe_spec in self.args.probes: |
| 811 | self.probes.append(Probe( |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 812 | probe_spec, self.args.string_size, |
yonghong-song | c2a530b | 2019-10-20 09:35:55 -0700 | [diff] [blame] | 813 | self.args.kernel_stack, self.args.user_stack, |
tty5 | 5cf529e | 2019-12-06 17:52:56 +0800 | [diff] [blame] | 814 | self.cgroup_map_name, self.args.name, self.args.msg_filter)) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 815 | |
| 816 | def _generate_program(self): |
| 817 | self.program = """ |
| 818 | #include <linux/ptrace.h> |
| 819 | #include <linux/sched.h> /* For TASK_COMM_LEN */ |
| 820 | |
| 821 | """ |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 822 | for include in (self.args.include or []): |
ShelbyFrances | f5dbbdb | 2017-02-08 05:56:52 +0300 | [diff] [blame] | 823 | if include.startswith((".", "/")): |
| 824 | include = os.path.abspath(include) |
| 825 | self.program += "#include \"%s\"\n" % include |
| 826 | else: |
| 827 | self.program += "#include <%s>\n" % include |
Sasha Goldshtein | b950d6f | 2016-03-21 04:06:15 -0700 | [diff] [blame] | 828 | self.program += BPF.generate_auto_includes( |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 829 | map(lambda p: p.raw_probe, self.probes)) |
yonghong-song | c2a530b | 2019-10-20 09:35:55 -0700 | [diff] [blame] | 830 | if self.cgroup_map_name is not None: |
| 831 | self.program += "BPF_CGROUP_ARRAY(%s, 1);\n" % \ |
| 832 | self.cgroup_map_name |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 833 | for probe in self.probes: |
| 834 | self.program += probe.generate_program( |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 835 | self.args.include_self) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 836 | |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 837 | if self.args.verbose or self.args.ebpf: |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 838 | print(self.program) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 839 | if self.args.ebpf: |
| 840 | exit() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 841 | |
| 842 | def _attach_probes(self): |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 843 | usdt_contexts = [] |
| 844 | for probe in self.probes: |
| 845 | if probe.usdt: |
| 846 | # USDT probes must be enabled before the BPF object |
| 847 | # is initialized, because that's where the actual |
| 848 | # uprobe is being attached. |
| 849 | probe.usdt.enable_probe( |
| 850 | probe.usdt_name, probe.probe_name) |
Sasha Goldshtein | f733cac | 2016-10-04 18:39:01 +0300 | [diff] [blame] | 851 | if self.args.verbose: |
| 852 | print(probe.usdt.get_text()) |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 853 | usdt_contexts.append(probe.usdt) |
| 854 | self.bpf = BPF(text=self.program, usdt_contexts=usdt_contexts) |
vijunag | 9924e64 | 2019-01-23 12:35:33 +0530 | [diff] [blame] | 855 | if self.args.sym_file_list is not None: |
| 856 | print("Note: Kernel bpf will report stack map with ip/build_id") |
| 857 | map(lambda x: self.bpf.add_module(x), self.args.sym_file_list.split(',')) |
yonghong-song | c2a530b | 2019-10-20 09:35:55 -0700 | [diff] [blame] | 858 | |
| 859 | # if cgroup filter is requested, update the cgroup array map |
| 860 | if self.cgroup_map_name is not None: |
| 861 | cgroup_array = self.bpf.get_table(self.cgroup_map_name) |
| 862 | cgroup_array[0] = self.args.cgroup_path |
| 863 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 864 | for probe in self.probes: |
| 865 | if self.args.verbose: |
| 866 | print(probe) |
| 867 | probe.attach(self.bpf, self.args.verbose) |
| 868 | |
| 869 | def _main_loop(self): |
| 870 | all_probes_trivial = all(map(Probe.is_default_action, |
| 871 | self.probes)) |
| 872 | |
| 873 | # Print header |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 874 | if self.args.timestamp or self.args.time: |
Maik Riechert | 3a0d3c4 | 2019-05-23 17:57:10 +0100 | [diff] [blame] | 875 | col_fmt = "%-17s " if self.args.unix_timestamp else "%-8s " |
Jonathan Giddy | ec0691e | 2021-02-21 09:44:26 +0000 | [diff] [blame^] | 876 | print(col_fmt % "TIME", end="") |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 877 | if self.args.print_cpu: |
Jonathan Giddy | ec0691e | 2021-02-21 09:44:26 +0000 | [diff] [blame^] | 878 | print("%-3s " % "CPU", end="") |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 879 | print("%-7s %-7s %-15s %-16s %s" % |
| 880 | ("PID", "TID", "COMM", "FUNC", |
| 881 | "-" if not all_probes_trivial else "")) |
Alban Crequy | 8bb4e47 | 2019-12-21 16:09:53 +0100 | [diff] [blame] | 882 | sys.stdout.flush() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 883 | |
| 884 | while True: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame] | 885 | self.bpf.perf_buffer_poll() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 886 | |
| 887 | def run(self): |
| 888 | try: |
| 889 | self._create_probes() |
| 890 | self._generate_program() |
| 891 | self._attach_probes() |
| 892 | self._main_loop() |
| 893 | except: |
Sasha Goldshtein | 2febc29 | 2017-02-13 20:25:32 -0500 | [diff] [blame] | 894 | exc_info = sys.exc_info() |
| 895 | sys_exit = exc_info[0] is SystemExit |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 896 | if self.args.verbose: |
| 897 | traceback.print_exc() |
Sasha Goldshtein | 2febc29 | 2017-02-13 20:25:32 -0500 | [diff] [blame] | 898 | elif not sys_exit: |
| 899 | print(exc_info[1]) |
| 900 | exit(0 if sys_exit else 1) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 901 | |
| 902 | if __name__ == "__main__": |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 903 | Tool().run() |