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