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