Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 2 | # |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 3 | # argdist Trace a function and display a distribution of its |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 4 | # parameter values as a histogram or frequency count. |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 5 | # |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 6 | # USAGE: argdist [-h] [-p PID] [-z STRING_SIZE] [-i INTERVAL] [-n COUNT] [-v] |
| 7 | # [-c] [-T TOP] [-C specifier] [-H specifier] [-I header] |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 8 | # |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 10 | # Copyright (C) 2016 Sasha Goldshtein. |
| 11 | |
Sumanth Korikkar | 306080b | 2020-04-27 04:37:23 -0500 | [diff] [blame] | 12 | from bcc import BPF, USDT, StrcmpRewrite |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 13 | from time import sleep, strftime |
| 14 | import argparse |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 15 | import re |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 16 | import traceback |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 17 | import os |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 18 | import sys |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 19 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 20 | class Probe(object): |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 21 | next_probe_index = 0 |
Sasha Goldshtein | c8f752f | 2016-10-17 02:18:43 -0700 | [diff] [blame] | 22 | streq_index = 0 |
Rafael Fonseca | af236e7 | 2017-02-15 17:28:26 +0100 | [diff] [blame] | 23 | aliases = {"$PID": "(bpf_get_current_pid_tgid() >> 32)"} |
Sasha Goldshtein | 5e4e1f4 | 2016-02-12 06:52:19 -0800 | [diff] [blame] | 24 | |
| 25 | def _substitute_aliases(self, expr): |
| 26 | if expr is None: |
| 27 | return expr |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 28 | for alias, subst in Probe.aliases.items(): |
Sasha Goldshtein | 5e4e1f4 | 2016-02-12 06:52:19 -0800 | [diff] [blame] | 29 | expr = expr.replace(alias, subst) |
| 30 | return expr |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 31 | |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 32 | def _parse_signature(self): |
| 33 | params = map(str.strip, self.signature.split(',')) |
| 34 | self.param_types = {} |
| 35 | for param in params: |
| 36 | # If the type is a pointer, the * can be next to the |
| 37 | # param name. Other complex types like arrays are not |
| 38 | # supported right now. |
| 39 | index = param.rfind('*') |
| 40 | index = index if index != -1 else param.rfind(' ') |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 41 | param_type = param[0:index + 1].strip() |
| 42 | param_name = param[index + 1:].strip() |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 43 | self.param_types[param_name] = param_type |
Sumanth Korikkar | 306080b | 2020-04-27 04:37:23 -0500 | [diff] [blame] | 44 | # Maintain list of user params. Then later decide to |
| 45 | # switch to bpf_probe_read or bpf_probe_read_user. |
| 46 | if "__user" in param_type.split(): |
| 47 | self.probe_user_list.add(param_name) |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 48 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 49 | def _generate_entry(self): |
| 50 | self.entry_probe_func = self.probe_func_name + "_entry" |
| 51 | text = """ |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 52 | int PROBENAME(struct pt_regs *ctx SIGNATURE) |
| 53 | { |
Rafael Fonseca | af236e7 | 2017-02-15 17:28:26 +0100 | [diff] [blame] | 54 | u64 __pid_tgid = bpf_get_current_pid_tgid(); |
| 55 | u32 __pid = __pid_tgid; // lower 32 bits |
| 56 | u32 __tgid = __pid_tgid >> 32; // upper 32 bits |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 57 | PID_FILTER |
| 58 | COLLECT |
| 59 | return 0; |
| 60 | } |
| 61 | """ |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 62 | text = text.replace("PROBENAME", self.entry_probe_func) |
| 63 | text = text.replace("SIGNATURE", |
| 64 | "" if len(self.signature) == 0 else ", " + self.signature) |
Rafael Fonseca | af236e7 | 2017-02-15 17:28:26 +0100 | [diff] [blame] | 65 | text = text.replace("PID_FILTER", self._generate_pid_filter()) |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 66 | collect = "" |
| 67 | for pname in self.args_to_probe: |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 68 | param_hash = self.hashname_prefix + pname |
| 69 | if pname == "__latency": |
| 70 | collect += """ |
| 71 | u64 __time = bpf_ktime_get_ns(); |
Rafael Fonseca | af236e7 | 2017-02-15 17:28:26 +0100 | [diff] [blame] | 72 | %s.update(&__pid, &__time); |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 73 | """ % param_hash |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 74 | else: |
Rafael Fonseca | af236e7 | 2017-02-15 17:28:26 +0100 | [diff] [blame] | 75 | collect += "%s.update(&__pid, &%s);\n" % \ |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 76 | (param_hash, pname) |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 77 | text = text.replace("COLLECT", collect) |
| 78 | return text |
| 79 | |
| 80 | def _generate_entry_probe(self): |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 81 | # Any $entry(name) expressions result in saving that argument |
| 82 | # when entering the function. |
| 83 | self.args_to_probe = set() |
| 84 | regex = r"\$entry\((\w+)\)" |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 85 | for expr in self.exprs: |
| 86 | for arg in re.finditer(regex, expr): |
| 87 | self.args_to_probe.add(arg.group(1)) |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 88 | for arg in re.finditer(regex, self.filter): |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 89 | self.args_to_probe.add(arg.group(1)) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 90 | if any(map(lambda expr: "$latency" in expr, self.exprs)) or \ |
| 91 | "$latency" in self.filter: |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 92 | self.args_to_probe.add("__latency") |
| 93 | self.param_types["__latency"] = "u64" # nanoseconds |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 94 | for pname in self.args_to_probe: |
| 95 | if pname not in self.param_types: |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 96 | raise ValueError("$entry(%s): no such param" % |
| 97 | arg) |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 98 | |
| 99 | self.hashname_prefix = "%s_param_" % self.probe_hash_name |
| 100 | text = "" |
| 101 | for pname in self.args_to_probe: |
| 102 | # Each argument is stored in a separate hash that is |
| 103 | # keyed by pid. |
| 104 | text += "BPF_HASH(%s, u32, %s);\n" % \ |
| 105 | (self.hashname_prefix + pname, |
| 106 | self.param_types[pname]) |
| 107 | text += self._generate_entry() |
| 108 | return text |
| 109 | |
| 110 | def _generate_retprobe_prefix(self): |
| 111 | # After we're done here, there are __%s_val variables for each |
| 112 | # argument we needed to probe using $entry(name), and they all |
| 113 | # have values (which isn't necessarily the case if we missed |
| 114 | # the method entry probe). |
Rafael Fonseca | af236e7 | 2017-02-15 17:28:26 +0100 | [diff] [blame] | 115 | text = "" |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 116 | self.param_val_names = {} |
| 117 | for pname in self.args_to_probe: |
| 118 | val_name = "__%s_val" % pname |
| 119 | text += "%s *%s = %s.lookup(&__pid);\n" % \ |
| 120 | (self.param_types[pname], val_name, |
| 121 | self.hashname_prefix + pname) |
| 122 | text += "if (%s == 0) { return 0 ; }\n" % val_name |
| 123 | self.param_val_names[pname] = val_name |
| 124 | return text |
| 125 | |
| 126 | def _replace_entry_exprs(self): |
| 127 | for pname, vname in self.param_val_names.items(): |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 128 | if pname == "__latency": |
| 129 | entry_expr = "$latency" |
| 130 | val_expr = "(bpf_ktime_get_ns() - *%s)" % vname |
| 131 | else: |
| 132 | entry_expr = "$entry(%s)" % pname |
| 133 | val_expr = "(*%s)" % vname |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 134 | for i in range(0, len(self.exprs)): |
| 135 | self.exprs[i] = self.exprs[i].replace( |
| 136 | entry_expr, val_expr) |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 137 | self.filter = self.filter.replace(entry_expr, |
| 138 | val_expr) |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 139 | |
| 140 | def _attach_entry_probe(self): |
| 141 | if self.is_user: |
| 142 | self.bpf.attach_uprobe(name=self.library, |
| 143 | sym=self.function, |
| 144 | fn_name=self.entry_probe_func, |
| 145 | pid=self.pid or -1) |
| 146 | else: |
| 147 | self.bpf.attach_kprobe(event=self.function, |
| 148 | fn_name=self.entry_probe_func) |
| 149 | |
Sasha Goldshtein | 7983d6b | 2016-02-13 23:14:18 -0800 | [diff] [blame] | 150 | def _bail(self, error): |
| 151 | raise ValueError("error parsing probe '%s': %s" % |
| 152 | (self.raw_spec, error)) |
| 153 | |
| 154 | def _validate_specifier(self): |
| 155 | # Everything after '#' is the probe label, ignore it |
| 156 | spec = self.raw_spec.split('#')[0] |
| 157 | parts = spec.strip().split(':') |
| 158 | if len(parts) < 3: |
| 159 | self._bail("at least the probe type, library, and " + |
| 160 | "function signature must be specified") |
| 161 | if len(parts) > 6: |
| 162 | self._bail("extraneous ':'-separated parts detected") |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 163 | if parts[0] not in ["r", "p", "t", "u"]: |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 164 | self._bail("probe type must be 'p', 'r', 't', or 'u'" + |
| 165 | " but got '%s'" % parts[0]) |
Sasha Goldshtein | 3fa7ba1 | 2017-01-14 11:17:40 +0000 | [diff] [blame] | 166 | if re.match(r"\S+\(.*\)", parts[2]) is None: |
Sasha Goldshtein | 7983d6b | 2016-02-13 23:14:18 -0800 | [diff] [blame] | 167 | self._bail(("function signature '%s' has an invalid " + |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 168 | "format") % parts[2]) |
| 169 | |
| 170 | def _parse_expr_types(self, expr_types): |
| 171 | if len(expr_types) == 0: |
| 172 | self._bail("no expr types specified") |
| 173 | self.expr_types = expr_types.split(',') |
| 174 | |
| 175 | def _parse_exprs(self, exprs): |
| 176 | if len(exprs) == 0: |
| 177 | self._bail("no exprs specified") |
| 178 | self.exprs = exprs.split(',') |
Sasha Goldshtein | 7983d6b | 2016-02-13 23:14:18 -0800 | [diff] [blame] | 179 | |
Sasha Goldshtein | 3fa7ba1 | 2017-01-14 11:17:40 +0000 | [diff] [blame] | 180 | def _make_valid_identifier(self, ident): |
| 181 | return re.sub(r'[^A-Za-z0-9_]', '_', ident) |
| 182 | |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 183 | def __init__(self, tool, type, specifier): |
| 184 | self.usdt_ctx = None |
Sasha Goldshtein | c8f752f | 2016-10-17 02:18:43 -0700 | [diff] [blame] | 185 | self.streq_functions = "" |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 186 | self.pid = tool.args.pid |
Sasha Goldshtein | d2f4762 | 2016-10-04 18:40:15 +0300 | [diff] [blame] | 187 | self.cumulative = tool.args.cumulative or False |
Sasha Goldshtein | 7983d6b | 2016-02-13 23:14:18 -0800 | [diff] [blame] | 188 | self.raw_spec = specifier |
Sumanth Korikkar | 306080b | 2020-04-27 04:37:23 -0500 | [diff] [blame] | 189 | self.probe_user_list = set() |
| 190 | self.bin_cmp = False |
Sasha Goldshtein | 7983d6b | 2016-02-13 23:14:18 -0800 | [diff] [blame] | 191 | self._validate_specifier() |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 192 | |
Sasha Goldshtein | 7983d6b | 2016-02-13 23:14:18 -0800 | [diff] [blame] | 193 | spec_and_label = specifier.split('#') |
Sasha Goldshtein | ed21adf | 2016-02-12 03:04:53 -0800 | [diff] [blame] | 194 | self.label = spec_and_label[1] \ |
| 195 | if len(spec_and_label) == 2 else None |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 196 | |
Sasha Goldshtein | ed21adf | 2016-02-12 03:04:53 -0800 | [diff] [blame] | 197 | parts = spec_and_label[0].strip().split(':') |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 198 | self.type = type # hist or freq |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 199 | self.probe_type = parts[0] |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 200 | fparts = parts[2].split('(') |
Sasha Goldshtein | 7983d6b | 2016-02-13 23:14:18 -0800 | [diff] [blame] | 201 | self.function = fparts[0].strip() |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 202 | if self.probe_type == "t": |
| 203 | self.library = "" # kernel |
| 204 | self.tp_category = parts[1] |
| 205 | self.tp_event = self.function |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 206 | elif self.probe_type == "u": |
| 207 | self.library = parts[1] |
Sasha Goldshtein | 3fa7ba1 | 2017-01-14 11:17:40 +0000 | [diff] [blame] | 208 | self.probe_func_name = self._make_valid_identifier( |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 209 | "%s_probe%d" % |
Sasha Goldshtein | 3fa7ba1 | 2017-01-14 11:17:40 +0000 | [diff] [blame] | 210 | (self.function, Probe.next_probe_index)) |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 211 | self._enable_usdt_probe() |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 212 | else: |
| 213 | self.library = parts[1] |
| 214 | self.is_user = len(self.library) > 0 |
Sasha Goldshtein | 7983d6b | 2016-02-13 23:14:18 -0800 | [diff] [blame] | 215 | self.signature = fparts[1].strip()[:-1] |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 216 | self._parse_signature() |
| 217 | |
| 218 | # If the user didn't specify an expression to probe, we probe |
| 219 | # the retval in a ret probe, or simply the value "1" otherwise. |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 220 | self.is_default_expr = len(parts) < 5 |
| 221 | if not self.is_default_expr: |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 222 | self._parse_expr_types(parts[3]) |
| 223 | self._parse_exprs(parts[4]) |
| 224 | if len(self.exprs) != len(self.expr_types): |
| 225 | self._bail("mismatched # of exprs and types") |
| 226 | if self.type == "hist" and len(self.expr_types) > 1: |
| 227 | self._bail("histograms can only have 1 expr") |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 228 | else: |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 229 | if not self.probe_type == "r" and self.type == "hist": |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 230 | self._bail("histograms must have expr") |
| 231 | self.expr_types = \ |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 232 | ["u64" if not self.probe_type == "r" else "int"] |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 233 | self.exprs = \ |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 234 | ["1" if not self.probe_type == "r" else "$retval"] |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 235 | self.filter = "" if len(parts) != 6 else parts[5] |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 236 | self._substitute_exprs() |
| 237 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 238 | # Do we need to attach an entry probe so that we can collect an |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 239 | # argument that is required for an exit (return) probe? |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 240 | def check(expr): |
| 241 | keywords = ["$entry", "$latency"] |
| 242 | return any(map(lambda kw: kw in expr, keywords)) |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 243 | self.entry_probe_required = self.probe_type == "r" and \ |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 244 | (any(map(check, self.exprs)) or check(self.filter)) |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 245 | |
Sasha Goldshtein | 3fa7ba1 | 2017-01-14 11:17:40 +0000 | [diff] [blame] | 246 | self.probe_func_name = self._make_valid_identifier( |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 247 | "%s_probe%d" % |
Sasha Goldshtein | 3fa7ba1 | 2017-01-14 11:17:40 +0000 | [diff] [blame] | 248 | (self.function, Probe.next_probe_index)) |
| 249 | self.probe_hash_name = self._make_valid_identifier( |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 250 | "%s_hash%d" % |
Sasha Goldshtein | 3fa7ba1 | 2017-01-14 11:17:40 +0000 | [diff] [blame] | 251 | (self.function, Probe.next_probe_index)) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 252 | Probe.next_probe_index += 1 |
| 253 | |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 254 | def _enable_usdt_probe(self): |
| 255 | self.usdt_ctx = USDT(path=self.library, pid=self.pid) |
| 256 | self.usdt_ctx.enable_probe( |
| 257 | self.function, self.probe_func_name) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 258 | |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 259 | def _substitute_exprs(self): |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 260 | def repl(expr): |
| 261 | expr = self._substitute_aliases(expr) |
Sumanth Korikkar | 306080b | 2020-04-27 04:37:23 -0500 | [diff] [blame] | 262 | rdict = StrcmpRewrite.rewrite_expr(expr, |
| 263 | self.bin_cmp, self.library, |
| 264 | self.probe_user_list, self.streq_functions, |
| 265 | Probe.streq_index) |
| 266 | expr = rdict["expr"] |
| 267 | self.streq_functions = rdict["streq_functions"] |
| 268 | Probe.streq_index = rdict["probeid"] |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 269 | return expr.replace("$retval", "PT_REGS_RC(ctx)") |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 270 | for i in range(0, len(self.exprs)): |
| 271 | self.exprs[i] = repl(self.exprs[i]) |
| 272 | self.filter = repl(self.filter) |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 273 | |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 274 | def _is_string(self, expr_type): |
| 275 | return expr_type == "char*" or expr_type == "char *" |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 276 | |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 277 | def _generate_hash_field(self, i): |
| 278 | if self._is_string(self.expr_types[i]): |
| 279 | return "struct __string_t v%d;\n" % i |
| 280 | else: |
| 281 | return "%s v%d;\n" % (self.expr_types[i], i) |
| 282 | |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 283 | def _generate_usdt_arg_assignment(self, i): |
| 284 | expr = self.exprs[i] |
| 285 | if self.probe_type == "u" and expr[0:3] == "arg": |
Sasha Goldshtein | 3a5256f | 2017-02-20 15:42:57 +0000 | [diff] [blame] | 286 | arg_index = int(expr[3]) |
| 287 | arg_ctype = self.usdt_ctx.get_probe_arg_ctype( |
| 288 | self.function, arg_index - 1) |
| 289 | return (" %s %s = 0;\n" + |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 290 | " bpf_usdt_readarg(%s, ctx, &%s);\n") \ |
Sasha Goldshtein | 3a5256f | 2017-02-20 15:42:57 +0000 | [diff] [blame] | 291 | % (arg_ctype, expr, expr[3], expr) |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 292 | else: |
| 293 | return "" |
| 294 | |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 295 | def _generate_field_assignment(self, i): |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 296 | text = self._generate_usdt_arg_assignment(i) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 297 | if self._is_string(self.expr_types[i]): |
Sumanth Korikkar | 306080b | 2020-04-27 04:37:23 -0500 | [diff] [blame] | 298 | if self.is_user or \ |
| 299 | self.exprs[i] in self.probe_user_list: |
| 300 | probe_readfunc = "bpf_probe_read_user" |
| 301 | else: |
| 302 | probe_readfunc = "bpf_probe_read" |
| 303 | return (text + " %s(&__key.v%d.s," + |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 304 | " sizeof(__key.v%d.s), (void *)%s);\n") % \ |
Sumanth Korikkar | 306080b | 2020-04-27 04:37:23 -0500 | [diff] [blame] | 305 | (probe_readfunc, i, i, self.exprs[i]) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 306 | else: |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 307 | return text + " __key.v%d = %s;\n" % \ |
| 308 | (i, self.exprs[i]) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 309 | |
| 310 | def _generate_hash_decl(self): |
| 311 | if self.type == "hist": |
| 312 | return "BPF_HISTOGRAM(%s, %s);" % \ |
| 313 | (self.probe_hash_name, self.expr_types[0]) |
| 314 | else: |
| 315 | text = "struct %s_key_t {\n" % self.probe_hash_name |
| 316 | for i in range(0, len(self.expr_types)): |
| 317 | text += self._generate_hash_field(i) |
| 318 | text += "};\n" |
| 319 | text += "BPF_HASH(%s, struct %s_key_t, u64);\n" % \ |
| 320 | (self.probe_hash_name, self.probe_hash_name) |
| 321 | return text |
| 322 | |
| 323 | def _generate_key_assignment(self): |
| 324 | if self.type == "hist": |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 325 | return self._generate_usdt_arg_assignment(0) + \ |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 326 | ("%s __key = %s;\n" % |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 327 | (self.expr_types[0], self.exprs[0])) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 328 | else: |
| 329 | text = "struct %s_key_t __key = {};\n" % \ |
| 330 | self.probe_hash_name |
| 331 | for i in range(0, len(self.exprs)): |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 332 | text += self._generate_field_assignment(i) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 333 | return text |
| 334 | |
| 335 | def _generate_hash_update(self): |
| 336 | if self.type == "hist": |
| 337 | return "%s.increment(bpf_log2l(__key));" % \ |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 338 | self.probe_hash_name |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 339 | else: |
| 340 | return "%s.increment(__key);" % self.probe_hash_name |
| 341 | |
| 342 | def _generate_pid_filter(self): |
| 343 | # Kernel probes need to explicitly filter pid, because the |
| 344 | # attach interface doesn't support pid filtering |
| 345 | if self.pid is not None and not self.is_user: |
Rafael Fonseca | af236e7 | 2017-02-15 17:28:26 +0100 | [diff] [blame] | 346 | return "if (__tgid != %d) { return 0; }" % self.pid |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 347 | else: |
| 348 | return "" |
| 349 | |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 350 | def generate_text(self): |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 351 | program = "" |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 352 | probe_text = """ |
| 353 | DATA_DECL |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 354 | """ + ( |
| 355 | "TRACEPOINT_PROBE(%s, %s)" % |
| 356 | (self.tp_category, self.tp_event) |
| 357 | if self.probe_type == "t" |
| 358 | else "int PROBENAME(struct pt_regs *ctx SIGNATURE)") + """ |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 359 | { |
Rafael Fonseca | af236e7 | 2017-02-15 17:28:26 +0100 | [diff] [blame] | 360 | u64 __pid_tgid = bpf_get_current_pid_tgid(); |
| 361 | u32 __pid = __pid_tgid; // lower 32 bits |
| 362 | u32 __tgid = __pid_tgid >> 32; // upper 32 bits |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 363 | PID_FILTER |
| 364 | PREFIX |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 365 | KEY_EXPR |
Akilesh Kailash | b8269aa | 2020-05-11 11:54:26 -0700 | [diff] [blame] | 366 | if (!(FILTER)) return 0; |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 367 | COLLECT |
| 368 | return 0; |
| 369 | } |
| 370 | """ |
| 371 | prefix = "" |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 372 | signature = "" |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 373 | |
| 374 | # If any entry arguments are probed in a ret probe, we need |
| 375 | # to generate an entry probe to collect them |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 376 | if self.entry_probe_required: |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 377 | program += self._generate_entry_probe() |
| 378 | prefix += self._generate_retprobe_prefix() |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 379 | # Replace $entry(paramname) with a reference to the |
| 380 | # value we collected when entering the function: |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 381 | self._replace_entry_exprs() |
| 382 | |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 383 | if self.probe_type == "p" and len(self.signature) > 0: |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 384 | # Only entry uprobes/kprobes can have user-specified |
| 385 | # signatures. Other probes force it to (). |
| 386 | signature = ", " + self.signature |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 387 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 388 | program += probe_text.replace("PROBENAME", |
| 389 | self.probe_func_name) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 390 | program = program.replace("SIGNATURE", signature) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 391 | program = program.replace("PID_FILTER", |
| 392 | self._generate_pid_filter()) |
| 393 | |
| 394 | decl = self._generate_hash_decl() |
| 395 | key_expr = self._generate_key_assignment() |
| 396 | collect = self._generate_hash_update() |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 397 | program = program.replace("DATA_DECL", decl) |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 398 | program = program.replace("KEY_EXPR", key_expr) |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 399 | program = program.replace("FILTER", |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 400 | "1" if len(self.filter) == 0 else self.filter) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 401 | program = program.replace("COLLECT", collect) |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 402 | program = program.replace("PREFIX", prefix) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 403 | |
Sasha Goldshtein | c8f752f | 2016-10-17 02:18:43 -0700 | [diff] [blame] | 404 | return self.streq_functions + program |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 405 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 406 | def _attach_u(self): |
| 407 | libpath = BPF.find_library(self.library) |
| 408 | if libpath is None: |
Sasha Goldshtein | ec67971 | 2016-10-04 18:33:36 +0300 | [diff] [blame] | 409 | libpath = BPF.find_exe(self.library) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 410 | if libpath is None or len(libpath) == 0: |
Sasha Goldshtein | 5a1d2e3 | 2016-03-30 08:14:44 -0700 | [diff] [blame] | 411 | self._bail("unable to find library %s" % self.library) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 412 | |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 413 | if self.probe_type == "r": |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 414 | self.bpf.attach_uretprobe(name=libpath, |
| 415 | sym=self.function, |
| 416 | fn_name=self.probe_func_name, |
| 417 | pid=self.pid or -1) |
| 418 | else: |
| 419 | self.bpf.attach_uprobe(name=libpath, |
| 420 | sym=self.function, |
| 421 | fn_name=self.probe_func_name, |
| 422 | pid=self.pid or -1) |
| 423 | |
| 424 | def _attach_k(self): |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 425 | if self.probe_type == "t": |
| 426 | pass # Nothing to do for tracepoints |
| 427 | elif self.probe_type == "r": |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 428 | self.bpf.attach_kretprobe(event=self.function, |
| 429 | fn_name=self.probe_func_name) |
| 430 | else: |
| 431 | self.bpf.attach_kprobe(event=self.function, |
| 432 | fn_name=self.probe_func_name) |
| 433 | |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 434 | def attach(self, bpf): |
| 435 | self.bpf = bpf |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 436 | if self.probe_type == "u": |
| 437 | return |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 438 | if self.is_user: |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 439 | self._attach_u() |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 440 | else: |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 441 | self._attach_k() |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 442 | if self.entry_probe_required: |
| 443 | self._attach_entry_probe() |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 444 | |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 445 | def _v2s(self, v): |
| 446 | # Most fields can be converted with plain str(), but strings |
| 447 | # are wrapped in a __string_t which has an .s field |
| 448 | if "__string_t" in type(v).__name__: |
| 449 | return str(v.s) |
| 450 | return str(v) |
| 451 | |
| 452 | def _display_expr(self, i): |
| 453 | # Replace ugly latency calculation with $latency |
| 454 | expr = self.exprs[i].replace( |
| 455 | "(bpf_ktime_get_ns() - *____latency_val)", "$latency") |
| 456 | # Replace alias values back with the alias name |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 457 | for alias, subst in Probe.aliases.items(): |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 458 | expr = expr.replace(subst, alias) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 459 | # Replace retval expression with $retval |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 460 | expr = expr.replace("PT_REGS_RC(ctx)", "$retval") |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 461 | # Replace ugly (*__param_val) expressions with param name |
| 462 | return re.sub(r"\(\*__(\w+)_val\)", r"\1", expr) |
| 463 | |
| 464 | def _display_key(self, key): |
| 465 | if self.is_default_expr: |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 466 | if not self.probe_type == "r": |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 467 | return "total calls" |
| 468 | else: |
| 469 | return "retval = %s" % str(key.v0) |
| 470 | else: |
| 471 | # The key object has v0, ..., vk fields containing |
| 472 | # the values of the expressions from self.exprs |
| 473 | def str_i(i): |
| 474 | key_i = self._v2s(getattr(key, "v%d" % i)) |
| 475 | return "%s = %s" % \ |
| 476 | (self._display_expr(i), key_i) |
| 477 | return ", ".join(map(str_i, range(0, len(self.exprs)))) |
| 478 | |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 479 | def display(self, top): |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 480 | data = self.bpf.get_table(self.probe_hash_name) |
| 481 | if self.type == "freq": |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 482 | print(self.label or self.raw_spec) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 483 | print("\t%-10s %s" % ("COUNT", "EVENT")) |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 484 | sdata = sorted(data.items(), key=lambda p: p[1].value) |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 485 | if top is not None: |
Sasha Goldshtein | d2f4762 | 2016-10-04 18:40:15 +0300 | [diff] [blame] | 486 | sdata = sdata[-top:] |
| 487 | for key, value in sdata: |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 488 | # Print some nice values if the user didn't |
| 489 | # specify an expression to probe |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 490 | if self.is_default_expr: |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 491 | if not self.probe_type == "r": |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 492 | key_str = "total calls" |
| 493 | else: |
| 494 | key_str = "retval = %s" % \ |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 495 | self._v2s(key.v0) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 496 | else: |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 497 | key_str = self._display_key(key) |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 498 | print("\t%-10s %s" % |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 499 | (str(value.value), key_str)) |
| 500 | elif self.type == "hist": |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 501 | label = self.label or (self._display_expr(0) |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 502 | if not self.is_default_expr else "retval") |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 503 | data.print_log2_hist(val_type=label) |
Sasha Goldshtein | d2f4762 | 2016-10-04 18:40:15 +0300 | [diff] [blame] | 504 | if not self.cumulative: |
| 505 | data.clear() |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 506 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 507 | def __str__(self): |
| 508 | return self.label or self.raw_spec |
| 509 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 510 | class Tool(object): |
| 511 | examples = """ |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 512 | Probe specifier syntax: |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 513 | {p,r,t,u}:{[library],category}:function(signature)[:type[,type...]:expr[,expr...][:filter]][#label] |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 514 | Where: |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 515 | p,r,t,u -- probe at function entry, function exit, kernel |
| 516 | tracepoint, or USDT probe |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 517 | in exit probes: can use $retval, $entry(param), $latency |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 518 | library -- the library that contains the function |
| 519 | (leave empty for kernel functions) |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 520 | category -- the category of the kernel tracepoint (e.g. net, sched) |
| 521 | function -- the function name to trace (or tracepoint name) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 522 | signature -- the function's parameters, as in the C header |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 523 | type -- the type of the expression to collect (supports multiple) |
| 524 | expr -- the expression to collect (supports multiple) |
Sasha Goldshtein | ed21adf | 2016-02-12 03:04:53 -0800 | [diff] [blame] | 525 | filter -- the filter that is applied to collected values |
| 526 | label -- the label for this probe in the resulting output |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 527 | |
| 528 | EXAMPLES: |
| 529 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 530 | argdist -H 'p::__kmalloc(u64 size):u64:size' |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 531 | Print a histogram of allocation sizes passed to kmalloc |
| 532 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 533 | argdist -p 1005 -C 'p:c:malloc(size_t size):size_t:size:size==16' |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 534 | Print a frequency count of how many times process 1005 called malloc |
| 535 | with an allocation size of 16 bytes |
| 536 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 537 | argdist -C 'r:c:gets():char*:(char*)$retval#snooped strings' |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 538 | Snoop on all strings returned by gets() |
| 539 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 540 | argdist -H 'r::__kmalloc(size_t size):u64:$latency/$entry(size)#ns per byte' |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 541 | Print a histogram of nanoseconds per byte from kmalloc allocations |
| 542 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 543 | argdist -C 'p::__kmalloc(size_t sz, gfp_t flags):size_t:sz:flags&GFP_ATOMIC' |
Sasha Goldshtein | 7983d6b | 2016-02-13 23:14:18 -0800 | [diff] [blame] | 544 | Print frequency count of kmalloc allocation sizes that have GFP_ATOMIC |
| 545 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 546 | argdist -p 1005 -C 'p:c:write(int fd):int:fd' -T 5 |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 547 | Print frequency counts of how many times writes were issued to a |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 548 | particular file descriptor number, in process 1005, but only show |
| 549 | the top 5 busiest fds |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 550 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 551 | argdist -p 1005 -H 'r:c:read()' |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 552 | Print a histogram of results (sizes) returned by read() in process 1005 |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 553 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 554 | argdist -C 'r::__vfs_read():u32:$PID:$latency > 100000' |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 555 | Print frequency of reads by process where the latency was >0.1ms |
| 556 | |
muahao | 852e19b | 2018-08-22 01:17:36 +0800 | [diff] [blame] | 557 | argdist -H 'r::__vfs_read(void *file, void *buf, size_t count):size_t: |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 558 | $entry(count):$latency > 1000000' |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 559 | Print a histogram of read sizes that were longer than 1ms |
| 560 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 561 | argdist -H \\ |
Sasha Goldshtein | ed21adf | 2016-02-12 03:04:53 -0800 | [diff] [blame] | 562 | 'p:c:write(int fd, const void *buf, size_t count):size_t:count:fd==1' |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 563 | Print a histogram of buffer sizes passed to write() across all |
| 564 | processes, where the file descriptor was 1 (STDOUT) |
| 565 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 566 | argdist -C 'p:c:fork()#fork calls' |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 567 | Count fork() calls in libc across all processes |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 568 | Can also use funccount.py, which is easier and more flexible |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 569 | |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 570 | argdist -H 't:block:block_rq_complete():u32:args->nr_sector' |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 571 | Print histogram of number of sectors in completing block I/O requests |
| 572 | |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 573 | argdist -C 't:irq:irq_handler_entry():int:args->irq' |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 574 | Aggregate interrupts by interrupt request (IRQ) |
| 575 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 576 | argdist -C 'u:pthread:pthread_start():u64:arg2' -p 1337 |
| 577 | Print frequency of function addresses used as a pthread start function, |
| 578 | relying on the USDT pthread_start probe in process 1337 |
| 579 | |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 580 | argdist -H 'p:c:sleep(u32 seconds):u32:seconds' \\ |
| 581 | -H 'p:c:nanosleep(struct timespec *req):long:req->tv_nsec' |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 582 | Print histograms of sleep() and nanosleep() parameter values |
| 583 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 584 | argdist -p 2780 -z 120 \\ |
Sasha Goldshtein | ed21adf | 2016-02-12 03:04:53 -0800 | [diff] [blame] | 585 | -C 'p:c:write(int fd, char* buf, size_t len):char*:buf:fd==1' |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 586 | Spy on writes to STDOUT performed by process 2780, up to a string size |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 587 | of 120 characters |
Yonghong Song | f4470dc | 2017-12-13 14:12:13 -0800 | [diff] [blame] | 588 | |
| 589 | argdist -I 'kernel/sched/sched.h' \\ |
| 590 | -C 'p::__account_cfs_rq_runtime(struct cfs_rq *cfs_rq):s64:cfs_rq->runtime_remaining' |
| 591 | Trace on the cfs scheduling runqueue remaining runtime. The struct cfs_rq is defined |
| 592 | in kernel/sched/sched.h which is in kernel source tree and not in kernel-devel |
| 593 | package. So this command needs to run at the kernel source tree root directory |
| 594 | so that the added header file can be found by the compiler. |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 595 | """ |
| 596 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 597 | def __init__(self): |
| 598 | parser = argparse.ArgumentParser(description="Trace a " + |
| 599 | "function and display a summary of its parameter values.", |
| 600 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 601 | epilog=Tool.examples) |
| 602 | parser.add_argument("-p", "--pid", type=int, |
| 603 | help="id of the process to trace (optional)") |
| 604 | parser.add_argument("-z", "--string-size", default=80, |
| 605 | type=int, |
| 606 | help="maximum string size to read from char* arguments") |
| 607 | parser.add_argument("-i", "--interval", default=1, type=int, |
Akilesh Kailash | 8996719 | 2018-05-18 13:36:54 -0700 | [diff] [blame] | 608 | help="output interval, in seconds (default 1 second)") |
| 609 | parser.add_argument("-d", "--duration", type=int, |
| 610 | help="total duration of trace, in seconds") |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 611 | parser.add_argument("-n", "--number", type=int, dest="count", |
| 612 | help="number of outputs") |
| 613 | parser.add_argument("-v", "--verbose", action="store_true", |
| 614 | help="print resulting BPF program code before executing") |
Sasha Goldshtein | d2f4762 | 2016-10-04 18:40:15 +0300 | [diff] [blame] | 615 | parser.add_argument("-c", "--cumulative", action="store_true", |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 616 | help="do not clear histograms and freq counts at " + |
| 617 | "each interval") |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 618 | parser.add_argument("-T", "--top", type=int, |
| 619 | help="number of top results to show (not applicable to " + |
| 620 | "histograms)") |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 621 | parser.add_argument("-H", "--histogram", action="append", |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 622 | dest="histspecifier", metavar="specifier", |
| 623 | help="probe specifier to capture histogram of " + |
| 624 | "(see examples below)") |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 625 | parser.add_argument("-C", "--count", action="append", |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 626 | dest="countspecifier", metavar="specifier", |
| 627 | help="probe specifier to capture count of " + |
| 628 | "(see examples below)") |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 629 | parser.add_argument("-I", "--include", action="append", |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 630 | metavar="header", |
ShelbyFrances | f5dbbdb | 2017-02-08 05:56:52 +0300 | [diff] [blame] | 631 | help="additional header files to include in the BPF program " |
Yonghong Song | f4470dc | 2017-12-13 14:12:13 -0800 | [diff] [blame] | 632 | "as either full path, " |
| 633 | "or relative to relative to current working directory, " |
| 634 | "or relative to default kernel header search path") |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 635 | self.args = parser.parse_args() |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 636 | self.usdt_ctx = None |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 637 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 638 | def _create_probes(self): |
| 639 | self.probes = [] |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 640 | for specifier in (self.args.countspecifier or []): |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 641 | self.probes.append(Probe(self, "freq", specifier)) |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 642 | for histspecifier in (self.args.histspecifier or []): |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 643 | self.probes.append(Probe(self, "hist", histspecifier)) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 644 | if len(self.probes) == 0: |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 645 | print("at least one specifier is required") |
Sasha Goldshtein | f7ab443 | 2017-02-13 18:46:49 -0500 | [diff] [blame] | 646 | exit(1) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 647 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 648 | def _generate_program(self): |
| 649 | bpf_source = """ |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 650 | struct __string_t { char s[%d]; }; |
| 651 | |
| 652 | #include <uapi/linux/ptrace.h> |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 653 | """ % self.args.string_size |
| 654 | for include in (self.args.include or []): |
ShelbyFrances | f5dbbdb | 2017-02-08 05:56:52 +0300 | [diff] [blame] | 655 | if include.startswith((".", "/")): |
| 656 | include = os.path.abspath(include) |
| 657 | bpf_source += "#include \"%s\"\n" % include |
| 658 | else: |
| 659 | bpf_source += "#include <%s>\n" % include |
| 660 | |
Sasha Goldshtein | b950d6f | 2016-03-21 04:06:15 -0700 | [diff] [blame] | 661 | bpf_source += BPF.generate_auto_includes( |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 662 | map(lambda p: p.raw_spec, self.probes)) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 663 | for probe in self.probes: |
| 664 | bpf_source += probe.generate_text() |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 665 | if self.args.verbose: |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 666 | for text in [probe.usdt_ctx.get_text() |
| 667 | for probe in self.probes |
| 668 | if probe.usdt_ctx]: |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 669 | print(text) |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 670 | print(bpf_source) |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 671 | usdt_contexts = [probe.usdt_ctx |
| 672 | for probe in self.probes if probe.usdt_ctx] |
| 673 | self.bpf = BPF(text=bpf_source, usdt_contexts=usdt_contexts) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 674 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 675 | def _attach(self): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 676 | for probe in self.probes: |
| 677 | probe.attach(self.bpf) |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 678 | if self.args.verbose: |
yonghong-song | 6070dcb | 2018-06-22 14:23:29 -0700 | [diff] [blame] | 679 | print("open uprobes: %s" % list(self.bpf.uprobe_fds.keys())) |
| 680 | print("open kprobes: %s" % list(self.bpf.kprobe_fds.keys())) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 681 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 682 | def _main_loop(self): |
| 683 | count_so_far = 0 |
Akilesh Kailash | 8996719 | 2018-05-18 13:36:54 -0700 | [diff] [blame] | 684 | seconds = 0 |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 685 | while True: |
| 686 | try: |
| 687 | sleep(self.args.interval) |
Akilesh Kailash | 8996719 | 2018-05-18 13:36:54 -0700 | [diff] [blame] | 688 | seconds += self.args.interval |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 689 | except KeyboardInterrupt: |
| 690 | exit() |
| 691 | print("[%s]" % strftime("%H:%M:%S")) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 692 | for probe in self.probes: |
| 693 | probe.display(self.args.top) |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 694 | count_so_far += 1 |
| 695 | if self.args.count is not None and \ |
| 696 | count_so_far >= self.args.count: |
| 697 | exit() |
Akilesh Kailash | 8996719 | 2018-05-18 13:36:54 -0700 | [diff] [blame] | 698 | if self.args.duration and \ |
| 699 | seconds >= self.args.duration: |
| 700 | exit() |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 701 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 702 | def run(self): |
| 703 | try: |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 704 | self._create_probes() |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 705 | self._generate_program() |
| 706 | self._attach() |
| 707 | self._main_loop() |
| 708 | except: |
Sasha Goldshtein | f7ab443 | 2017-02-13 18:46:49 -0500 | [diff] [blame] | 709 | exc_info = sys.exc_info() |
| 710 | sys_exit = exc_info[0] is SystemExit |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 711 | if self.args.verbose: |
| 712 | traceback.print_exc() |
Sasha Goldshtein | f7ab443 | 2017-02-13 18:46:49 -0500 | [diff] [blame] | 713 | elif not sys_exit: |
| 714 | print(exc_info[1]) |
| 715 | exit(0 if sys_exit else 1) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 716 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 717 | if __name__ == "__main__": |
| 718 | Tool().run() |