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