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( |
| 203 | "%s_probe%d" % \ |
| 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( |
| 241 | "%s_probe%d" % \ |
| 242 | (self.function, Probe.next_probe_index)) |
| 243 | self.probe_hash_name = self._make_valid_identifier( |
| 244 | "%s_hash%d" % \ |
| 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": |
| 296 | return (" u64 %s = 0;\n" + |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 297 | " bpf_usdt_readarg(%s, ctx, &%s);\n") \ |
| 298 | % (expr, expr[3], expr) |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 299 | else: |
| 300 | return "" |
| 301 | |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 302 | def _generate_field_assignment(self, i): |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 303 | text = self._generate_usdt_arg_assignment(i) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 304 | if self._is_string(self.expr_types[i]): |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 305 | return (text + " bpf_probe_read(&__key.v%d.s," + |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 306 | " sizeof(__key.v%d.s), (void *)%s);\n") % \ |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 307 | (i, i, self.exprs[i]) |
| 308 | else: |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 309 | return text + " __key.v%d = %s;\n" % \ |
| 310 | (i, self.exprs[i]) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 311 | |
| 312 | def _generate_hash_decl(self): |
| 313 | if self.type == "hist": |
| 314 | return "BPF_HISTOGRAM(%s, %s);" % \ |
| 315 | (self.probe_hash_name, self.expr_types[0]) |
| 316 | else: |
| 317 | text = "struct %s_key_t {\n" % self.probe_hash_name |
| 318 | for i in range(0, len(self.expr_types)): |
| 319 | text += self._generate_hash_field(i) |
| 320 | text += "};\n" |
| 321 | text += "BPF_HASH(%s, struct %s_key_t, u64);\n" % \ |
| 322 | (self.probe_hash_name, self.probe_hash_name) |
| 323 | return text |
| 324 | |
| 325 | def _generate_key_assignment(self): |
| 326 | if self.type == "hist": |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 327 | return self._generate_usdt_arg_assignment(0) + \ |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 328 | ("%s __key = %s;\n" % |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 329 | (self.expr_types[0], self.exprs[0])) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 330 | else: |
| 331 | text = "struct %s_key_t __key = {};\n" % \ |
| 332 | self.probe_hash_name |
| 333 | for i in range(0, len(self.exprs)): |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 334 | text += self._generate_field_assignment(i) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 335 | return text |
| 336 | |
| 337 | def _generate_hash_update(self): |
| 338 | if self.type == "hist": |
| 339 | return "%s.increment(bpf_log2l(__key));" % \ |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 340 | self.probe_hash_name |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 341 | else: |
| 342 | return "%s.increment(__key);" % self.probe_hash_name |
| 343 | |
| 344 | def _generate_pid_filter(self): |
| 345 | # Kernel probes need to explicitly filter pid, because the |
| 346 | # attach interface doesn't support pid filtering |
| 347 | if self.pid is not None and not self.is_user: |
Rafael Fonseca | af236e7 | 2017-02-15 17:28:26 +0100 | [diff] [blame^] | 348 | return "if (__tgid != %d) { return 0; }" % self.pid |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 349 | else: |
| 350 | return "" |
| 351 | |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 352 | def generate_text(self): |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 353 | program = "" |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 354 | probe_text = """ |
| 355 | DATA_DECL |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 356 | """ + ( |
| 357 | "TRACEPOINT_PROBE(%s, %s)" % |
| 358 | (self.tp_category, self.tp_event) |
| 359 | if self.probe_type == "t" |
| 360 | else "int PROBENAME(struct pt_regs *ctx SIGNATURE)") + """ |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 361 | { |
Rafael Fonseca | af236e7 | 2017-02-15 17:28:26 +0100 | [diff] [blame^] | 362 | u64 __pid_tgid = bpf_get_current_pid_tgid(); |
| 363 | u32 __pid = __pid_tgid; // lower 32 bits |
| 364 | u32 __tgid = __pid_tgid >> 32; // upper 32 bits |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 365 | PID_FILTER |
| 366 | PREFIX |
| 367 | if (!(FILTER)) return 0; |
| 368 | KEY_EXPR |
| 369 | COLLECT |
| 370 | return 0; |
| 371 | } |
| 372 | """ |
| 373 | prefix = "" |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 374 | signature = "" |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 375 | |
| 376 | # If any entry arguments are probed in a ret probe, we need |
| 377 | # to generate an entry probe to collect them |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 378 | if self.entry_probe_required: |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 379 | program += self._generate_entry_probe() |
| 380 | prefix += self._generate_retprobe_prefix() |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 381 | # Replace $entry(paramname) with a reference to the |
| 382 | # value we collected when entering the function: |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 383 | self._replace_entry_exprs() |
| 384 | |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 385 | if self.probe_type == "p" and len(self.signature) > 0: |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 386 | # Only entry uprobes/kprobes can have user-specified |
| 387 | # signatures. Other probes force it to (). |
| 388 | signature = ", " + self.signature |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 389 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 390 | program += probe_text.replace("PROBENAME", |
| 391 | self.probe_func_name) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 392 | program = program.replace("SIGNATURE", signature) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 393 | program = program.replace("PID_FILTER", |
| 394 | self._generate_pid_filter()) |
| 395 | |
| 396 | decl = self._generate_hash_decl() |
| 397 | key_expr = self._generate_key_assignment() |
| 398 | collect = self._generate_hash_update() |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 399 | program = program.replace("DATA_DECL", decl) |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 400 | program = program.replace("KEY_EXPR", key_expr) |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 401 | program = program.replace("FILTER", |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 402 | "1" if len(self.filter) == 0 else self.filter) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 403 | program = program.replace("COLLECT", collect) |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 404 | program = program.replace("PREFIX", prefix) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 405 | |
Sasha Goldshtein | c8f752f | 2016-10-17 02:18:43 -0700 | [diff] [blame] | 406 | return self.streq_functions + program |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 407 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 408 | def _attach_u(self): |
| 409 | libpath = BPF.find_library(self.library) |
| 410 | if libpath is None: |
Sasha Goldshtein | ec67971 | 2016-10-04 18:33:36 +0300 | [diff] [blame] | 411 | libpath = BPF.find_exe(self.library) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 412 | if libpath is None or len(libpath) == 0: |
Sasha Goldshtein | 5a1d2e3 | 2016-03-30 08:14:44 -0700 | [diff] [blame] | 413 | self._bail("unable to find library %s" % self.library) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 414 | |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 415 | if self.probe_type == "r": |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 416 | self.bpf.attach_uretprobe(name=libpath, |
| 417 | sym=self.function, |
| 418 | fn_name=self.probe_func_name, |
| 419 | pid=self.pid or -1) |
| 420 | else: |
| 421 | self.bpf.attach_uprobe(name=libpath, |
| 422 | sym=self.function, |
| 423 | fn_name=self.probe_func_name, |
| 424 | pid=self.pid or -1) |
| 425 | |
| 426 | def _attach_k(self): |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 427 | if self.probe_type == "t": |
| 428 | pass # Nothing to do for tracepoints |
| 429 | elif self.probe_type == "r": |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 430 | self.bpf.attach_kretprobe(event=self.function, |
| 431 | fn_name=self.probe_func_name) |
| 432 | else: |
| 433 | self.bpf.attach_kprobe(event=self.function, |
| 434 | fn_name=self.probe_func_name) |
| 435 | |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 436 | def attach(self, bpf): |
| 437 | self.bpf = bpf |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 438 | if self.probe_type == "u": |
| 439 | return |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 440 | if self.is_user: |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 441 | self._attach_u() |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 442 | else: |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 443 | self._attach_k() |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 444 | if self.entry_probe_required: |
| 445 | self._attach_entry_probe() |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 446 | |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 447 | def _v2s(self, v): |
| 448 | # Most fields can be converted with plain str(), but strings |
| 449 | # are wrapped in a __string_t which has an .s field |
| 450 | if "__string_t" in type(v).__name__: |
| 451 | return str(v.s) |
| 452 | return str(v) |
| 453 | |
| 454 | def _display_expr(self, i): |
| 455 | # Replace ugly latency calculation with $latency |
| 456 | expr = self.exprs[i].replace( |
| 457 | "(bpf_ktime_get_ns() - *____latency_val)", "$latency") |
| 458 | # Replace alias values back with the alias name |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 459 | for alias, subst in Probe.aliases.items(): |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 460 | expr = expr.replace(subst, alias) |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 461 | # Replace retval expression with $retval |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 462 | expr = expr.replace("PT_REGS_RC(ctx)", "$retval") |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 463 | # Replace ugly (*__param_val) expressions with param name |
| 464 | return re.sub(r"\(\*__(\w+)_val\)", r"\1", expr) |
| 465 | |
| 466 | def _display_key(self, key): |
| 467 | if self.is_default_expr: |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 468 | if not self.probe_type == "r": |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 469 | return "total calls" |
| 470 | else: |
| 471 | return "retval = %s" % str(key.v0) |
| 472 | else: |
| 473 | # The key object has v0, ..., vk fields containing |
| 474 | # the values of the expressions from self.exprs |
| 475 | def str_i(i): |
| 476 | key_i = self._v2s(getattr(key, "v%d" % i)) |
| 477 | return "%s = %s" % \ |
| 478 | (self._display_expr(i), key_i) |
| 479 | return ", ".join(map(str_i, range(0, len(self.exprs)))) |
| 480 | |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 481 | def display(self, top): |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 482 | data = self.bpf.get_table(self.probe_hash_name) |
| 483 | if self.type == "freq": |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 484 | print(self.label or self.raw_spec) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 485 | print("\t%-10s %s" % ("COUNT", "EVENT")) |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 486 | sdata = sorted(data.items(), key=lambda p: p[1].value) |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 487 | if top is not None: |
Sasha Goldshtein | d2f4762 | 2016-10-04 18:40:15 +0300 | [diff] [blame] | 488 | sdata = sdata[-top:] |
| 489 | for key, value in sdata: |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 490 | # Print some nice values if the user didn't |
| 491 | # specify an expression to probe |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 492 | if self.is_default_expr: |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 493 | if not self.probe_type == "r": |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 494 | key_str = "total calls" |
| 495 | else: |
| 496 | key_str = "retval = %s" % \ |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 497 | self._v2s(key.v0) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 498 | else: |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 499 | key_str = self._display_key(key) |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 500 | print("\t%-10s %s" % |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 501 | (str(value.value), key_str)) |
| 502 | elif self.type == "hist": |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 503 | label = self.label or (self._display_expr(0) |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 504 | if not self.is_default_expr else "retval") |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 505 | data.print_log2_hist(val_type=label) |
Sasha Goldshtein | d2f4762 | 2016-10-04 18:40:15 +0300 | [diff] [blame] | 506 | if not self.cumulative: |
| 507 | data.clear() |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 508 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 509 | def __str__(self): |
| 510 | return self.label or self.raw_spec |
| 511 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 512 | class Tool(object): |
| 513 | examples = """ |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 514 | Probe specifier syntax: |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 515 | {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] | 516 | Where: |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 517 | p,r,t,u -- probe at function entry, function exit, kernel |
| 518 | tracepoint, or USDT probe |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 519 | in exit probes: can use $retval, $entry(param), $latency |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 520 | library -- the library that contains the function |
| 521 | (leave empty for kernel functions) |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 522 | category -- the category of the kernel tracepoint (e.g. net, sched) |
| 523 | function -- the function name to trace (or tracepoint name) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 524 | signature -- the function's parameters, as in the C header |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 525 | type -- the type of the expression to collect (supports multiple) |
| 526 | expr -- the expression to collect (supports multiple) |
Sasha Goldshtein | ed21adf | 2016-02-12 03:04:53 -0800 | [diff] [blame] | 527 | filter -- the filter that is applied to collected values |
| 528 | label -- the label for this probe in the resulting output |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 529 | |
| 530 | EXAMPLES: |
| 531 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 532 | argdist -H 'p::__kmalloc(u64 size):u64:size' |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 533 | Print a histogram of allocation sizes passed to kmalloc |
| 534 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 535 | 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] | 536 | Print a frequency count of how many times process 1005 called malloc |
| 537 | with an allocation size of 16 bytes |
| 538 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 539 | argdist -C 'r:c:gets():char*:(char*)$retval#snooped strings' |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 540 | Snoop on all strings returned by gets() |
| 541 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 542 | 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] | 543 | Print a histogram of nanoseconds per byte from kmalloc allocations |
| 544 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 545 | 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] | 546 | Print frequency count of kmalloc allocation sizes that have GFP_ATOMIC |
| 547 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 548 | 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] | 549 | Print frequency counts of how many times writes were issued to a |
Sasha Goldshtein | 392d5c8 | 2016-02-12 11:14:20 -0800 | [diff] [blame] | 550 | particular file descriptor number, in process 1005, but only show |
| 551 | the top 5 busiest fds |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 552 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 553 | argdist -p 1005 -H 'r:c:read()' |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 554 | Print a histogram of results (sizes) returned by read() in process 1005 |
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 -C 'r::__vfs_read():u32:$PID:$latency > 100000' |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 557 | Print frequency of reads by process where the latency was >0.1ms |
| 558 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 559 | argdist -H 'r::__vfs_read(void *file, void *buf, size_t count):size_t |
| 560 | $entry(count):$latency > 1000000' |
Sasha Goldshtein | e350115 | 2016-02-13 03:56:29 -0800 | [diff] [blame] | 561 | Print a histogram of read sizes that were longer than 1ms |
| 562 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 563 | argdist -H \\ |
Sasha Goldshtein | ed21adf | 2016-02-12 03:04:53 -0800 | [diff] [blame] | 564 | '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] | 565 | Print a histogram of buffer sizes passed to write() across all |
| 566 | processes, where the file descriptor was 1 (STDOUT) |
| 567 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 568 | argdist -C 'p:c:fork()#fork calls' |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 569 | Count fork() calls in libc across all processes |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 570 | Can also use funccount.py, which is easier and more flexible |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 571 | |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 572 | argdist -H 't:block:block_rq_complete():u32:args->nr_sector' |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 573 | Print histogram of number of sectors in completing block I/O requests |
| 574 | |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 575 | argdist -C 't:irq:irq_handler_entry():int:args->irq' |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 576 | Aggregate interrupts by interrupt request (IRQ) |
| 577 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 578 | argdist -C 'u:pthread:pthread_start():u64:arg2' -p 1337 |
| 579 | Print frequency of function addresses used as a pthread start function, |
| 580 | relying on the USDT pthread_start probe in process 1337 |
| 581 | |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 582 | argdist -H 'p:c:sleep(u32 seconds):u32:seconds' \\ |
| 583 | -H 'p:c:nanosleep(struct timespec *req):long:req->tv_nsec' |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 584 | Print histograms of sleep() and nanosleep() parameter values |
| 585 | |
Sasha Goldshtein | 7df65da | 2016-02-14 05:12:27 -0800 | [diff] [blame] | 586 | argdist -p 2780 -z 120 \\ |
Sasha Goldshtein | ed21adf | 2016-02-12 03:04:53 -0800 | [diff] [blame] | 587 | -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] | 588 | 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] | 589 | of 120 characters |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 590 | """ |
| 591 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 592 | def __init__(self): |
| 593 | parser = argparse.ArgumentParser(description="Trace a " + |
| 594 | "function and display a summary of its parameter values.", |
| 595 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 596 | epilog=Tool.examples) |
| 597 | parser.add_argument("-p", "--pid", type=int, |
| 598 | help="id of the process to trace (optional)") |
| 599 | parser.add_argument("-z", "--string-size", default=80, |
| 600 | type=int, |
| 601 | help="maximum string size to read from char* arguments") |
| 602 | parser.add_argument("-i", "--interval", default=1, type=int, |
| 603 | help="output interval, in seconds") |
| 604 | parser.add_argument("-n", "--number", type=int, dest="count", |
| 605 | help="number of outputs") |
| 606 | parser.add_argument("-v", "--verbose", action="store_true", |
| 607 | help="print resulting BPF program code before executing") |
Sasha Goldshtein | d2f4762 | 2016-10-04 18:40:15 +0300 | [diff] [blame] | 608 | parser.add_argument("-c", "--cumulative", action="store_true", |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 609 | help="do not clear histograms and freq counts at " + |
| 610 | "each interval") |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 611 | parser.add_argument("-T", "--top", type=int, |
| 612 | help="number of top results to show (not applicable to " + |
| 613 | "histograms)") |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 614 | parser.add_argument("-H", "--histogram", action="append", |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 615 | dest="histspecifier", metavar="specifier", |
| 616 | help="probe specifier to capture histogram of " + |
| 617 | "(see examples below)") |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 618 | parser.add_argument("-C", "--count", action="append", |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 619 | dest="countspecifier", metavar="specifier", |
| 620 | help="probe specifier to capture count of " + |
| 621 | "(see examples below)") |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 622 | parser.add_argument("-I", "--include", action="append", |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 623 | metavar="header", |
ShelbyFrances | f5dbbdb | 2017-02-08 05:56:52 +0300 | [diff] [blame] | 624 | help="additional header files to include in the BPF program " |
| 625 | "as either full path, or relative to '/usr/include'") |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 626 | self.args = parser.parse_args() |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 627 | self.usdt_ctx = None |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 628 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 629 | def _create_probes(self): |
| 630 | self.probes = [] |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 631 | for specifier in (self.args.countspecifier or []): |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 632 | self.probes.append(Probe(self, "freq", specifier)) |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 633 | for histspecifier in (self.args.histspecifier or []): |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 634 | self.probes.append(Probe(self, "hist", histspecifier)) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 635 | if len(self.probes) == 0: |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 636 | print("at least one specifier is required") |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 637 | exit() |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 638 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 639 | def _generate_program(self): |
| 640 | bpf_source = """ |
Sasha Goldshtein | cc27edf | 2016-02-14 03:49:01 -0800 | [diff] [blame] | 641 | struct __string_t { char s[%d]; }; |
| 642 | |
| 643 | #include <uapi/linux/ptrace.h> |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 644 | """ % self.args.string_size |
| 645 | for include in (self.args.include or []): |
ShelbyFrances | f5dbbdb | 2017-02-08 05:56:52 +0300 | [diff] [blame] | 646 | if include.startswith((".", "/")): |
| 647 | include = os.path.abspath(include) |
| 648 | bpf_source += "#include \"%s\"\n" % include |
| 649 | else: |
| 650 | bpf_source += "#include <%s>\n" % include |
| 651 | |
Sasha Goldshtein | b950d6f | 2016-03-21 04:06:15 -0700 | [diff] [blame] | 652 | bpf_source += BPF.generate_auto_includes( |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 653 | map(lambda p: p.raw_spec, self.probes)) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 654 | for probe in self.probes: |
| 655 | bpf_source += probe.generate_text() |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 656 | if self.args.verbose: |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 657 | for text in [probe.usdt_ctx.get_text() |
| 658 | for probe in self.probes |
| 659 | if probe.usdt_ctx]: |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 660 | print(text) |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 661 | print(bpf_source) |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 662 | usdt_contexts = [probe.usdt_ctx |
| 663 | for probe in self.probes if probe.usdt_ctx] |
| 664 | self.bpf = BPF(text=bpf_source, usdt_contexts=usdt_contexts) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 665 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 666 | def _attach(self): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 667 | for probe in self.probes: |
| 668 | probe.attach(self.bpf) |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 669 | if self.args.verbose: |
Mark Drayton | cb679d7 | 2016-07-15 23:55:22 +0100 | [diff] [blame] | 670 | print("open uprobes: %s" % self.bpf.open_uprobes) |
| 671 | print("open kprobes: %s" % self.bpf.open_kprobes) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 672 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 673 | def _main_loop(self): |
| 674 | count_so_far = 0 |
| 675 | while True: |
| 676 | try: |
| 677 | sleep(self.args.interval) |
| 678 | except KeyboardInterrupt: |
| 679 | exit() |
| 680 | print("[%s]" % strftime("%H:%M:%S")) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 681 | for probe in self.probes: |
| 682 | probe.display(self.args.top) |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 683 | count_so_far += 1 |
| 684 | if self.args.count is not None and \ |
| 685 | count_so_far >= self.args.count: |
| 686 | exit() |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 687 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 688 | def run(self): |
| 689 | try: |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 690 | self._create_probes() |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 691 | self._generate_program() |
| 692 | self._attach() |
| 693 | self._main_loop() |
| 694 | except: |
| 695 | if self.args.verbose: |
| 696 | traceback.print_exc() |
Brenden Blanco | bc94d4c | 2016-05-05 12:05:07 -0700 | [diff] [blame] | 697 | elif sys.exc_info()[0] is not SystemExit: |
| 698 | print(sys.exc_info()[1]) |
Sasha Goldshtein | 8538485 | 2016-02-12 01:29:39 -0800 | [diff] [blame] | 699 | |
Sasha Goldshtein | c955130 | 2016-02-21 02:21:46 -0800 | [diff] [blame] | 700 | if __name__ == "__main__": |
| 701 | Tool().run() |