Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # trace Trace a function and print a trace message based on its |
| 4 | # parameters, with an optional filter. |
| 5 | # |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 6 | # usage: trace [-h] [-p PID] [-L TID] [-v] [-Z STRING_SIZE] [-S] |
Mirek Klimos | e538228 | 2018-01-26 14:52:50 -0800 | [diff] [blame] | 7 | # [-M MAX_EVENTS] [-T] [-t] [-K] [-U] [-a] [-I header] |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 8 | # probe [probe ...] |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 9 | # |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 10 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 11 | # Copyright (C) 2016 Sasha Goldshtein. |
| 12 | |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 13 | from __future__ import print_function |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 14 | from bcc import BPF, USDT |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 15 | from functools import partial |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 16 | from time import sleep, strftime |
| 17 | import argparse |
| 18 | import re |
| 19 | import ctypes as ct |
| 20 | import os |
| 21 | import traceback |
| 22 | import sys |
| 23 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 24 | class Probe(object): |
| 25 | probe_count = 0 |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 26 | streq_index = 0 |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 27 | max_events = None |
| 28 | event_count = 0 |
| 29 | first_ts = 0 |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 30 | print_time = False |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 31 | use_localtime = True |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 32 | time_field = False |
| 33 | print_cpu = False |
Mirek Klimos | e538228 | 2018-01-26 14:52:50 -0800 | [diff] [blame] | 34 | print_address = False |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 35 | tgid = -1 |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 36 | pid = -1 |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 37 | page_cnt = None |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 38 | |
| 39 | @classmethod |
| 40 | def configure(cls, args): |
| 41 | cls.max_events = args.max_events |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 42 | cls.print_time = args.timestamp or args.time |
| 43 | cls.use_localtime = not args.timestamp |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 44 | cls.time_field = cls.print_time and (not cls.use_localtime) |
| 45 | cls.print_cpu = args.print_cpu |
Mirek Klimos | e538228 | 2018-01-26 14:52:50 -0800 | [diff] [blame] | 46 | cls.print_address = args.address |
Sasha Goldshtein | 60c4192 | 2017-02-09 04:19:53 -0500 | [diff] [blame] | 47 | cls.first_ts = BPF.monotonic_time() |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 48 | cls.tgid = args.tgid or -1 |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 49 | cls.pid = args.pid or -1 |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 50 | cls.page_cnt = args.buffer_pages |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 51 | |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 52 | def __init__(self, probe, string_size, kernel_stack, user_stack): |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 53 | self.usdt = None |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 54 | self.streq_functions = "" |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 55 | self.raw_probe = probe |
| 56 | self.string_size = string_size |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 57 | self.kernel_stack = kernel_stack |
| 58 | self.user_stack = user_stack |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 59 | Probe.probe_count += 1 |
| 60 | self._parse_probe() |
| 61 | self.probe_num = Probe.probe_count |
| 62 | self.probe_name = "probe_%s_%d" % \ |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 63 | (self._display_function(), self.probe_num) |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 64 | self.probe_name = re.sub(r'[^A-Za-z0-9_]', '_', |
| 65 | self.probe_name) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 66 | |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 67 | # compiler can generate proper codes for function |
| 68 | # signatures with "syscall__" prefix |
| 69 | if self.is_syscall_kprobe: |
| 70 | self.probe_name = "syscall__" + self.probe_name[6:] |
| 71 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 72 | def __str__(self): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 73 | return "%s:%s:%s FLT=%s ACT=%s/%s" % (self.probe_type, |
| 74 | self.library, self._display_function(), self.filter, |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 75 | self.types, self.values) |
| 76 | |
| 77 | def is_default_action(self): |
| 78 | return self.python_format == "" |
| 79 | |
| 80 | def _bail(self, error): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 81 | raise ValueError("error in probe '%s': %s" % |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 82 | (self.raw_probe, error)) |
| 83 | |
| 84 | def _parse_probe(self): |
| 85 | text = self.raw_probe |
| 86 | |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 87 | # There might be a function signature preceding the actual |
| 88 | # filter/print part, or not. Find the probe specifier first -- |
| 89 | # it ends with either a space or an open paren ( for the |
| 90 | # function signature part. |
| 91 | # opt. signature |
| 92 | # probespec | rest |
| 93 | # --------- ---------- -- |
| 94 | (spec, sig, rest) = re.match(r'([^ \t\(]+)(\([^\(]*\))?(.*)', |
| 95 | text).groups() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 96 | |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 97 | self._parse_spec(spec) |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 98 | # Remove the parens |
| 99 | self.signature = sig[1:-1] if sig else None |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 100 | if self.signature and self.probe_type in ['u', 't']: |
| 101 | self._bail("USDT and tracepoint probes can't have " + |
| 102 | "a function signature; use arg1, arg2, " + |
| 103 | "... instead") |
| 104 | |
| 105 | text = rest.lstrip() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 106 | # If we now have a (, wait for the balanced closing ) and that |
| 107 | # will be the predicate |
| 108 | self.filter = None |
| 109 | if len(text) > 0 and text[0] == "(": |
| 110 | balance = 1 |
| 111 | for i in range(1, len(text)): |
| 112 | if text[i] == "(": |
| 113 | balance += 1 |
| 114 | if text[i] == ")": |
| 115 | balance -= 1 |
| 116 | if balance == 0: |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 117 | self._parse_filter(text[:i + 1]) |
| 118 | text = text[i + 1:] |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 119 | break |
| 120 | if self.filter is None: |
| 121 | self._bail("unmatched end of predicate") |
| 122 | |
| 123 | if self.filter is None: |
| 124 | self.filter = "1" |
| 125 | |
| 126 | # The remainder of the text is the printf action |
| 127 | self._parse_action(text.lstrip()) |
| 128 | |
| 129 | def _parse_spec(self, spec): |
| 130 | parts = spec.split(":") |
| 131 | # Two special cases: 'func' means 'p::func', 'lib:func' means |
| 132 | # 'p:lib:func'. Other combinations need to provide an empty |
| 133 | # value between delimiters, e.g. 'r::func' for a kretprobe on |
| 134 | # the function func. |
| 135 | if len(parts) == 1: |
| 136 | parts = ["p", "", parts[0]] |
| 137 | elif len(parts) == 2: |
| 138 | parts = ["p", parts[0], parts[1]] |
| 139 | if len(parts[0]) == 0: |
| 140 | self.probe_type = "p" |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 141 | elif parts[0] in ["p", "r", "t", "u"]: |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 142 | self.probe_type = parts[0] |
| 143 | else: |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 144 | self._bail("probe type must be '', 'p', 't', 'r', " + |
| 145 | "or 'u', but got '%s'" % parts[0]) |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 146 | if self.probe_type == "t": |
| 147 | self.tp_category = parts[1] |
| 148 | self.tp_event = parts[2] |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 149 | self.library = "" # kernel |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 150 | self.function = "" # from TRACEPOINT_PROBE |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 151 | elif self.probe_type == "u": |
vkhromov | 5a2b39e | 2017-07-14 20:42:29 +0100 | [diff] [blame] | 152 | self.library = ':'.join(parts[1:-1]) |
| 153 | self.usdt_name = parts[-1] |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 154 | self.function = "" # no function, just address |
| 155 | # We will discover the USDT provider by matching on |
| 156 | # the USDT name in the specified library |
| 157 | self._find_usdt_probe() |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 158 | else: |
vkhromov | 5a2b39e | 2017-07-14 20:42:29 +0100 | [diff] [blame] | 159 | self.library = ':'.join(parts[1:-1]) |
| 160 | self.function = parts[-1] |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 161 | |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 162 | # only x64 syscalls needs checking, no other syscall wrapper yet. |
| 163 | self.is_syscall_kprobe = False |
| 164 | if self.probe_type == "p" and len(self.library) == 0 and \ |
| 165 | self.function[:10] == "__x64_sys_": |
| 166 | self.is_syscall_kprobe = True |
| 167 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 168 | def _find_usdt_probe(self): |
Sasha Goldshtein | dd04536 | 2016-11-13 05:07:38 -0800 | [diff] [blame] | 169 | target = Probe.pid if Probe.pid and Probe.pid != -1 \ |
| 170 | else Probe.tgid |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 171 | self.usdt = USDT(path=self.library, pid=target) |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 172 | for probe in self.usdt.enumerate_probes(): |
Javier Honduvilla Coto | 1ef82e2 | 2018-04-19 14:14:24 +0200 | [diff] [blame] | 173 | if probe.name == self.usdt_name.encode('ascii'): |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 174 | return # Found it, will enable later |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 175 | self._bail("unrecognized USDT probe %s" % self.usdt_name) |
| 176 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 177 | def _parse_filter(self, filt): |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 178 | self.filter = self._rewrite_expr(filt) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 179 | |
| 180 | def _parse_types(self, fmt): |
| 181 | for match in re.finditer( |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 182 | r'[^%]%(s|u|d|llu|lld|hu|hd|x|llx|c|K|U)', fmt): |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 183 | self.types.append(match.group(1)) |
| 184 | fmt = re.sub(r'([^%]%)(u|d|llu|lld|hu|hd)', r'\1d', fmt) |
| 185 | fmt = re.sub(r'([^%]%)(x|llx)', r'\1x', fmt) |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 186 | fmt = re.sub('%K|%U', '%s', fmt) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 187 | self.python_format = fmt.strip('"') |
| 188 | |
| 189 | def _parse_action(self, action): |
| 190 | self.values = [] |
| 191 | self.types = [] |
| 192 | self.python_format = "" |
| 193 | if len(action) == 0: |
| 194 | return |
| 195 | |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 196 | action = action.strip() |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 197 | match = re.search(r'(\".*?\"),?(.*)', action) |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 198 | if match is None: |
| 199 | self._bail("expected format string in \"s") |
| 200 | |
| 201 | self.raw_format = match.group(1) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 202 | self._parse_types(self.raw_format) |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 203 | for part in re.split('(?<!"),', match.group(2)): |
| 204 | part = self._rewrite_expr(part) |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 205 | if len(part) > 0: |
| 206 | self.values.append(part) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 207 | |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 208 | aliases_arg = { |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 209 | "arg1": "PT_REGS_PARM1(ctx)", |
| 210 | "arg2": "PT_REGS_PARM2(ctx)", |
| 211 | "arg3": "PT_REGS_PARM3(ctx)", |
| 212 | "arg4": "PT_REGS_PARM4(ctx)", |
| 213 | "arg5": "PT_REGS_PARM5(ctx)", |
| 214 | "arg6": "PT_REGS_PARM6(ctx)", |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | aliases_indarg = { |
| 218 | "arg1": "({u64 _val; struct pt_regs *_ctx = PT_REGS_PARM1(ctx);" |
| 219 | " bpf_probe_read(&_val, sizeof(_val), &(PT_REGS_PARM1(_ctx))); _val;})", |
| 220 | "arg2": "({u64 _val; struct pt_regs *_ctx = PT_REGS_PARM2(ctx);" |
| 221 | " bpf_probe_read(&_val, sizeof(_val), &(PT_REGS_PARM2(_ctx))); _val;})", |
| 222 | "arg3": "({u64 _val; struct pt_regs *_ctx = PT_REGS_PARM3(ctx);" |
| 223 | " bpf_probe_read(&_val, sizeof(_val), &(PT_REGS_PARM3(_ctx))); _val;})", |
| 224 | "arg4": "({u64 _val; struct pt_regs *_ctx = PT_REGS_PARM4(ctx);" |
| 225 | " bpf_probe_read(&_val, sizeof(_val), &(PT_REGS_PARM4(_ctx))); _val;})", |
| 226 | "arg5": "({u64 _val; struct pt_regs *_ctx = PT_REGS_PARM5(ctx);" |
| 227 | " bpf_probe_read(&_val, sizeof(_val), &(PT_REGS_PARM5(_ctx))); _val;})", |
| 228 | "arg6": "({u64 _val; struct pt_regs *_ctx = PT_REGS_PARM6(ctx);" |
| 229 | " bpf_probe_read(&_val, sizeof(_val), &(PT_REGS_PARM6(_ctx))); _val;})", |
| 230 | } |
| 231 | |
| 232 | aliases_common = { |
| 233 | "retval": "PT_REGS_RC(ctx)", |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 234 | "$uid": "(unsigned)(bpf_get_current_uid_gid() & 0xffffffff)", |
| 235 | "$gid": "(unsigned)(bpf_get_current_uid_gid() >> 32)", |
| 236 | "$pid": "(unsigned)(bpf_get_current_pid_tgid() & 0xffffffff)", |
| 237 | "$tgid": "(unsigned)(bpf_get_current_pid_tgid() >> 32)", |
Yonghong Song | f92fef2 | 2018-01-24 20:51:46 -0800 | [diff] [blame] | 238 | "$cpu": "bpf_get_smp_processor_id()", |
| 239 | "$task" : "((struct task_struct *)bpf_get_current_task())" |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 242 | def _generate_streq_function(self, string): |
| 243 | fname = "streq_%d" % Probe.streq_index |
| 244 | Probe.streq_index += 1 |
| 245 | self.streq_functions += """ |
Sasha Goldshtein | b9aec34 | 2017-01-16 18:41:22 +0000 | [diff] [blame] | 246 | static inline bool %s(char const *ignored, uintptr_t str) { |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 247 | char needle[] = %s; |
| 248 | char haystack[sizeof(needle)]; |
| 249 | bpf_probe_read(&haystack, sizeof(haystack), (void *)str); |
Sasha Goldshtein | dcf1675 | 2017-01-17 07:40:57 +0000 | [diff] [blame] | 250 | for (int i = 0; i < sizeof(needle) - 1; ++i) { |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 251 | if (needle[i] != haystack[i]) { |
| 252 | return false; |
| 253 | } |
| 254 | } |
| 255 | return true; |
| 256 | } |
| 257 | """ % (fname, string) |
| 258 | return fname |
| 259 | |
| 260 | def _rewrite_expr(self, expr): |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 261 | if self.is_syscall_kprobe: |
| 262 | for alias, replacement in Probe.aliases_indarg.items(): |
| 263 | expr = expr.replace(alias, replacement) |
| 264 | else: |
| 265 | for alias, replacement in Probe.aliases_arg.items(): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 266 | # For USDT probes, we replace argN values with the |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 267 | # actual arguments for that probe obtained using |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 268 | # bpf_readarg_N macros emitted at BPF construction. |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 269 | if self.probe_type == "u": |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 270 | continue |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 271 | expr = expr.replace(alias, replacement) |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 272 | for alias, replacement in Probe.aliases_common.items(): |
| 273 | expr = expr.replace(alias, replacement) |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -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) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 279 | return expr |
| 280 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 281 | p_type = {"u": ct.c_uint, "d": ct.c_int, |
| 282 | "llu": ct.c_ulonglong, "lld": ct.c_longlong, |
| 283 | "hu": ct.c_ushort, "hd": ct.c_short, |
| 284 | "x": ct.c_uint, "llx": ct.c_ulonglong, "c": ct.c_ubyte, |
| 285 | "K": ct.c_ulonglong, "U": ct.c_ulonglong} |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 286 | |
| 287 | def _generate_python_field_decl(self, idx, fields): |
| 288 | field_type = self.types[idx] |
| 289 | if field_type == "s": |
| 290 | ptype = ct.c_char * self.string_size |
| 291 | else: |
| 292 | ptype = Probe.p_type[field_type] |
| 293 | fields.append(("v%d" % idx, ptype)) |
| 294 | |
| 295 | def _generate_python_data_decl(self): |
| 296 | self.python_struct_name = "%s_%d_Data" % \ |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 297 | (self._display_function(), self.probe_num) |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 298 | fields = [] |
| 299 | if self.time_field: |
| 300 | fields.append(("timestamp_ns", ct.c_ulonglong)) |
| 301 | if self.print_cpu: |
| 302 | fields.append(("cpu", ct.c_int)) |
| 303 | fields.extend([ |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 304 | ("tgid", ct.c_uint), |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 305 | ("pid", ct.c_uint), |
| 306 | ("comm", ct.c_char * 16) # TASK_COMM_LEN |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 307 | ]) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 308 | for i in range(0, len(self.types)): |
| 309 | self._generate_python_field_decl(i, fields) |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 310 | if self.kernel_stack: |
| 311 | fields.append(("kernel_stack_id", ct.c_int)) |
| 312 | if self.user_stack: |
| 313 | fields.append(("user_stack_id", ct.c_int)) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 314 | return type(self.python_struct_name, (ct.Structure,), |
| 315 | dict(_fields_=fields)) |
| 316 | |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 317 | c_type = {"u": "unsigned int", "d": "int", |
| 318 | "llu": "unsigned long long", "lld": "long long", |
| 319 | "hu": "unsigned short", "hd": "short", |
| 320 | "x": "unsigned int", "llx": "unsigned long long", |
| 321 | "c": "char", "K": "unsigned long long", |
| 322 | "U": "unsigned long long"} |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 323 | fmt_types = c_type.keys() |
| 324 | |
| 325 | def _generate_field_decl(self, idx): |
| 326 | field_type = self.types[idx] |
| 327 | if field_type == "s": |
| 328 | return "char v%d[%d];\n" % (idx, self.string_size) |
| 329 | if field_type in Probe.fmt_types: |
| 330 | return "%s v%d;\n" % (Probe.c_type[field_type], idx) |
| 331 | self._bail("unrecognized format specifier %s" % field_type) |
| 332 | |
| 333 | def _generate_data_decl(self): |
| 334 | # The BPF program will populate values into the struct |
| 335 | # according to the format string, and the Python program will |
| 336 | # construct the final display string. |
| 337 | self.events_name = "%s_events" % self.probe_name |
| 338 | self.struct_name = "%s_data_t" % self.probe_name |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 339 | self.stacks_name = "%s_stacks" % self.probe_name |
| 340 | stack_table = "BPF_STACK_TRACE(%s, 1024);" % self.stacks_name \ |
| 341 | if (self.kernel_stack or self.user_stack) else "" |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 342 | data_fields = "" |
| 343 | for i, field_type in enumerate(self.types): |
| 344 | data_fields += " " + \ |
| 345 | self._generate_field_decl(i) |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 346 | time_str = "u64 timestamp_ns;" if self.time_field else "" |
| 347 | cpu_str = "int cpu;" if self.print_cpu else "" |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 348 | kernel_stack_str = " int kernel_stack_id;" \ |
| 349 | if self.kernel_stack else "" |
| 350 | user_stack_str = " int user_stack_id;" \ |
| 351 | if self.user_stack else "" |
| 352 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 353 | text = """ |
| 354 | struct %s |
| 355 | { |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 356 | %s |
| 357 | %s |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 358 | u32 tgid; |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 359 | u32 pid; |
| 360 | char comm[TASK_COMM_LEN]; |
| 361 | %s |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 362 | %s |
| 363 | %s |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | BPF_PERF_OUTPUT(%s); |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 367 | %s |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 368 | """ |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 369 | return text % (self.struct_name, time_str, cpu_str, data_fields, |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 370 | kernel_stack_str, user_stack_str, |
| 371 | self.events_name, stack_table) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 372 | |
| 373 | def _generate_field_assign(self, idx): |
| 374 | field_type = self.types[idx] |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 375 | expr = self.values[idx].strip() |
| 376 | text = "" |
| 377 | if self.probe_type == "u" and expr[0:3] == "arg": |
Sasha Goldshtein | 3a5256f | 2017-02-20 15:42:57 +0000 | [diff] [blame] | 378 | arg_index = int(expr[3]) |
| 379 | arg_ctype = self.usdt.get_probe_arg_ctype( |
| 380 | self.usdt_name, arg_index - 1) |
| 381 | text = (" %s %s = 0;\n" + |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 382 | " bpf_usdt_readarg(%s, ctx, &%s);\n") \ |
Sasha Goldshtein | 3a5256f | 2017-02-20 15:42:57 +0000 | [diff] [blame] | 383 | % (arg_ctype, expr, expr[3], expr) |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 384 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 385 | if field_type == "s": |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 386 | return text + """ |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 387 | if (%s != 0) { |
| 388 | bpf_probe_read(&__data.v%d, sizeof(__data.v%d), (void *)%s); |
| 389 | } |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 390 | """ % (expr, idx, idx, expr) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 391 | if field_type in Probe.fmt_types: |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 392 | return text + " __data.v%d = (%s)%s;\n" % \ |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 393 | (idx, Probe.c_type[field_type], expr) |
| 394 | self._bail("unrecognized field type %s" % field_type) |
| 395 | |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 396 | def _generate_usdt_filter_read(self): |
| 397 | text = "" |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 398 | if self.probe_type != "u": |
| 399 | return text |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 400 | for arg, _ in Probe.aliases_arg.items(): |
| 401 | if not (arg in self.filter): |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 402 | continue |
| 403 | arg_index = int(arg.replace("arg", "")) |
| 404 | arg_ctype = self.usdt.get_probe_arg_ctype( |
Sasha Goldshtein | dcf1675 | 2017-01-17 07:40:57 +0000 | [diff] [blame] | 405 | self.usdt_name, arg_index - 1) |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 406 | if not arg_ctype: |
| 407 | self._bail("Unable to determine type of {} " |
| 408 | "in the filter".format(arg)) |
| 409 | text += """ |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 410 | {} {}_filter; |
| 411 | bpf_usdt_readarg({}, ctx, &{}_filter); |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 412 | """.format(arg_ctype, arg, arg_index, arg) |
| 413 | self.filter = self.filter.replace( |
| 414 | arg, "{}_filter".format(arg)) |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 415 | return text |
| 416 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 417 | def generate_program(self, include_self): |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 418 | data_decl = self._generate_data_decl() |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 419 | if Probe.pid != -1: |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 420 | pid_filter = """ |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 421 | if (__pid != %d) { return 0; } |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 422 | """ % Probe.pid |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 423 | # uprobes can have a built-in tgid filter passed to |
| 424 | # attach_uprobe, hence the check here -- for kprobes, we |
| 425 | # need to do the tgid test by hand: |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 426 | elif len(self.library) == 0 and Probe.tgid != -1: |
| 427 | pid_filter = """ |
| 428 | if (__tgid != %d) { return 0; } |
| 429 | """ % Probe.tgid |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 430 | elif not include_self: |
| 431 | pid_filter = """ |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 432 | if (__tgid == %d) { return 0; } |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 433 | """ % os.getpid() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 434 | else: |
| 435 | pid_filter = "" |
| 436 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 437 | prefix = "" |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 438 | signature = "struct pt_regs *ctx" |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 439 | if self.signature: |
| 440 | signature += ", " + self.signature |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 441 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 442 | data_fields = "" |
| 443 | for i, expr in enumerate(self.values): |
| 444 | data_fields += self._generate_field_assign(i) |
| 445 | |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 446 | if self.probe_type == "t": |
| 447 | heading = "TRACEPOINT_PROBE(%s, %s)" % \ |
| 448 | (self.tp_category, self.tp_event) |
| 449 | ctx_name = "args" |
| 450 | else: |
| 451 | heading = "int %s(%s)" % (self.probe_name, signature) |
| 452 | ctx_name = "ctx" |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 453 | |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 454 | time_str = """ |
| 455 | __data.timestamp_ns = bpf_ktime_get_ns();""" if self.time_field else "" |
| 456 | cpu_str = """ |
| 457 | __data.cpu = bpf_get_smp_processor_id();""" if self.print_cpu else "" |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 458 | stack_trace = "" |
| 459 | if self.user_stack: |
| 460 | stack_trace += """ |
| 461 | __data.user_stack_id = %s.get_stackid( |
| 462 | %s, BPF_F_REUSE_STACKID | BPF_F_USER_STACK |
| 463 | );""" % (self.stacks_name, ctx_name) |
| 464 | if self.kernel_stack: |
| 465 | stack_trace += """ |
| 466 | __data.kernel_stack_id = %s.get_stackid( |
| 467 | %s, BPF_F_REUSE_STACKID |
| 468 | );""" % (self.stacks_name, ctx_name) |
| 469 | |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 470 | text = heading + """ |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 471 | { |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 472 | u64 __pid_tgid = bpf_get_current_pid_tgid(); |
| 473 | u32 __tgid = __pid_tgid >> 32; |
| 474 | u32 __pid = __pid_tgid; // implicit cast to u32 for bottom half |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 475 | %s |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 476 | %s |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 477 | %s |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 478 | if (!(%s)) return 0; |
| 479 | |
| 480 | struct %s __data = {0}; |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 481 | %s |
| 482 | %s |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 483 | __data.tgid = __tgid; |
| 484 | __data.pid = __pid; |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 485 | bpf_get_current_comm(&__data.comm, sizeof(__data.comm)); |
| 486 | %s |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 487 | %s |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 488 | %s.perf_submit(%s, &__data, sizeof(__data)); |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | """ |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 492 | text = text % (pid_filter, prefix, |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 493 | self._generate_usdt_filter_read(), self.filter, |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 494 | self.struct_name, time_str, cpu_str, data_fields, |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 495 | stack_trace, self.events_name, ctx_name) |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 496 | |
Sasha Goldshtein | f4797b0 | 2016-10-17 01:44:56 -0700 | [diff] [blame] | 497 | return self.streq_functions + data_decl + "\n" + text |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 498 | |
| 499 | @classmethod |
| 500 | def _time_off_str(cls, timestamp_ns): |
| 501 | return "%.6f" % (1e-9 * (timestamp_ns - cls.first_ts)) |
| 502 | |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 503 | def _display_function(self): |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 504 | if self.probe_type == 'p' or self.probe_type == 'r': |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 505 | return self.function |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 506 | elif self.probe_type == 'u': |
| 507 | return self.usdt_name |
| 508 | else: # self.probe_type == 't' |
| 509 | return self.tp_event |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 510 | |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 511 | def print_stack(self, bpf, stack_id, tgid): |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 512 | if stack_id < 0: |
Mirek Klimos | e538228 | 2018-01-26 14:52:50 -0800 | [diff] [blame] | 513 | print(" %d" % stack_id) |
| 514 | return |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 515 | |
| 516 | stack = list(bpf.get_table(self.stacks_name).walk(stack_id)) |
| 517 | for addr in stack: |
Mirek Klimos | e538228 | 2018-01-26 14:52:50 -0800 | [diff] [blame] | 518 | print(" ", end="") |
| 519 | if Probe.print_address: |
| 520 | print("%16x " % addr, end="") |
| 521 | print("%s" % (bpf.sym(addr, tgid, |
| 522 | show_module=True, show_offset=True))) |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 523 | |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 524 | def _format_message(self, bpf, tgid, values): |
| 525 | # Replace each %K with kernel sym and %U with user sym in tgid |
Rafael Fonseca | aee5ecf | 2017-02-08 16:14:31 +0100 | [diff] [blame] | 526 | kernel_placeholders = [i for i, t in enumerate(self.types) |
| 527 | if t == 'K'] |
| 528 | user_placeholders = [i for i, t in enumerate(self.types) |
| 529 | if t == 'U'] |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 530 | for kp in kernel_placeholders: |
Sasha Goldshtein | 0155385 | 2017-02-09 03:58:09 -0500 | [diff] [blame] | 531 | values[kp] = bpf.ksym(values[kp], show_offset=True) |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 532 | for up in user_placeholders: |
Sasha Goldshtein | 1e34f4e | 2017-02-09 00:21:49 -0500 | [diff] [blame] | 533 | values[up] = bpf.sym(values[up], tgid, |
Sasha Goldshtein | 0155385 | 2017-02-09 03:58:09 -0500 | [diff] [blame] | 534 | show_module=True, show_offset=True) |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 535 | return self.python_format % tuple(values) |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 536 | |
| 537 | def print_event(self, bpf, cpu, data, size): |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 538 | # Cast as the generated structure type and display |
| 539 | # according to the format string in the probe. |
| 540 | event = ct.cast(data, ct.POINTER(self.python_struct)).contents |
| 541 | values = map(lambda i: getattr(event, "v%d" % i), |
| 542 | range(0, len(self.values))) |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 543 | msg = self._format_message(bpf, event.tgid, values) |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 544 | if Probe.print_time: |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 545 | time = strftime("%H:%M:%S") if Probe.use_localtime else \ |
| 546 | Probe._time_off_str(event.timestamp_ns) |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 547 | print("%-8s " % time[:8], end="") |
| 548 | if Probe.print_cpu: |
| 549 | print("%-3s " % event.cpu, end="") |
| 550 | print("%-7d %-7d %-15s %-16s %s" % |
| 551 | (event.tgid, event.pid, event.comm.decode(), |
| 552 | self._display_function(), msg)) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 553 | |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 554 | if self.kernel_stack: |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 555 | self.print_stack(bpf, event.kernel_stack_id, -1) |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 556 | if self.user_stack: |
| 557 | self.print_stack(bpf, event.user_stack_id, event.tgid) |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 558 | if self.user_stack or self.kernel_stack: |
Sasha Goldshtein | accd4cf | 2016-10-11 07:56:13 -0700 | [diff] [blame] | 559 | print("") |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 560 | |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 561 | Probe.event_count += 1 |
| 562 | if Probe.max_events is not None and \ |
| 563 | Probe.event_count >= Probe.max_events: |
| 564 | exit() |
| 565 | |
| 566 | def attach(self, bpf, verbose): |
| 567 | if len(self.library) == 0: |
| 568 | self._attach_k(bpf) |
| 569 | else: |
| 570 | self._attach_u(bpf) |
| 571 | self.python_struct = self._generate_python_data_decl() |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 572 | callback = partial(self.print_event, bpf) |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 573 | bpf[self.events_name].open_perf_buffer(callback, |
| 574 | page_cnt=self.page_cnt) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 575 | |
| 576 | def _attach_k(self, bpf): |
| 577 | if self.probe_type == "r": |
| 578 | bpf.attach_kretprobe(event=self.function, |
| 579 | fn_name=self.probe_name) |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 580 | elif self.probe_type == "p": |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 581 | bpf.attach_kprobe(event=self.function, |
| 582 | fn_name=self.probe_name) |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 583 | # Note that tracepoints don't need an explicit attach |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 584 | |
| 585 | def _attach_u(self, bpf): |
| 586 | libpath = BPF.find_library(self.library) |
| 587 | if libpath is None: |
| 588 | # This might be an executable (e.g. 'bash') |
Sasha Goldshtein | ec67971 | 2016-10-04 18:33:36 +0300 | [diff] [blame] | 589 | libpath = BPF.find_exe(self.library) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 590 | if libpath is None or len(libpath) == 0: |
| 591 | self._bail("unable to find library %s" % self.library) |
| 592 | |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 593 | if self.probe_type == "u": |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 594 | pass # Was already enabled by the BPF constructor |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 595 | elif self.probe_type == "r": |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 596 | bpf.attach_uretprobe(name=libpath, |
| 597 | sym=self.function, |
| 598 | fn_name=self.probe_name, |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 599 | pid=Probe.tgid) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 600 | else: |
| 601 | bpf.attach_uprobe(name=libpath, |
| 602 | sym=self.function, |
| 603 | fn_name=self.probe_name, |
Sasha Goldshtein | b630092 | 2017-01-16 18:43:11 +0000 | [diff] [blame] | 604 | pid=Probe.tgid) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 605 | |
| 606 | class Tool(object): |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 607 | DEFAULT_PERF_BUFFER_PAGES = 64 |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 608 | examples = """ |
| 609 | EXAMPLES: |
| 610 | |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 611 | trace do_sys_open |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 612 | Trace the open syscall and print a default trace message when entered |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 613 | trace 'do_sys_open "%s", arg2' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 614 | Trace the open syscall and print the filename being opened |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 615 | trace 'sys_read (arg3 > 20000) "read %d bytes", arg3' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 616 | Trace the read syscall and print a message for reads >20000 bytes |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 617 | trace 'r::do_sys_open "%llx", retval' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 618 | Trace the return from the open syscall and print the return value |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 619 | trace 'c:open (arg2 == 42) "%s %d", arg1, arg2' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 620 | Trace the open() call from libc only if the flags (arg2) argument is 42 |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 621 | trace 'c:malloc "size = %d", arg1' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 622 | Trace malloc calls and print the size being allocated |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 623 | trace 'p:c:write (arg1 == 1) "writing %d bytes to STDOUT", arg3' |
| 624 | Trace the write() call from libc to monitor writes to STDOUT |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 625 | trace 'r::__kmalloc (retval == 0) "kmalloc failed!"' |
Sasha Goldshtein | 8acd015 | 2016-02-22 02:25:03 -0800 | [diff] [blame] | 626 | Trace returns from __kmalloc which returned a null pointer |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 627 | trace 'r:c:malloc (retval) "allocated = %x", retval' |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 628 | Trace returns from malloc and print non-NULL allocated buffers |
Sasha Goldshtein | 376ae5c | 2016-10-04 19:49:57 +0300 | [diff] [blame] | 629 | trace 't:block:block_rq_complete "sectors=%d", args->nr_sector' |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 630 | Trace the block_rq_complete kernel tracepoint and print # of tx sectors |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 631 | trace 'u:pthread:pthread_create (arg4 != 0)' |
| 632 | Trace the USDT probe pthread_create when its 4th argument is non-zero |
Sasha Goldshtein | 23e72b8 | 2017-01-17 08:49:36 +0000 | [diff] [blame] | 633 | trace 'p::SyS_nanosleep(struct timespec *ts) "sleep for %lld ns", ts->tv_nsec' |
| 634 | Trace the nanosleep syscall and print the sleep duration in ns |
Yonghong Song | f4470dc | 2017-12-13 14:12:13 -0800 | [diff] [blame] | 635 | trace -I 'linux/fs.h' \\ |
| 636 | 'p::uprobe_register(struct inode *inode) "a_ops = %llx", inode->i_mapping->a_ops' |
| 637 | Trace the uprobe_register inode mapping ops, and the symbol can be found |
| 638 | in /proc/kallsyms |
| 639 | trace -I 'kernel/sched/sched.h' \\ |
| 640 | 'p::__account_cfs_rq_runtime(struct cfs_rq *cfs_rq) "%d", cfs_rq->runtime_remaining' |
| 641 | Trace the cfs scheduling runqueue remaining runtime. The struct cfs_rq is defined |
| 642 | in kernel/sched/sched.h which is in kernel source tree and not in kernel-devel |
| 643 | package. So this command needs to run at the kernel source tree root directory |
| 644 | so that the added header file can be found by the compiler. |
tehnerd | 86293f0 | 2018-01-23 21:21:58 -0800 | [diff] [blame] | 645 | trace -I 'net/sock.h' \\ |
| 646 | 'udpv6_sendmsg(struct sock *sk) (sk->sk_dport == 13568)' |
| 647 | Trace udpv6 sendmsg calls only if socket's destination port is equal |
| 648 | to 53 (DNS; 13568 in big endian order) |
Yonghong Song | f92fef2 | 2018-01-24 20:51:46 -0800 | [diff] [blame] | 649 | trace -I 'linux/fs_struct.h' 'mntns_install "users = %d", $task->fs->users' |
| 650 | Trace the number of users accessing the file system of the current task |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 651 | """ |
| 652 | |
| 653 | def __init__(self): |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 654 | parser = argparse.ArgumentParser(description="Attach to " + |
| 655 | "functions and print trace messages.", |
| 656 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 657 | epilog=Tool.examples) |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 658 | parser.add_argument("-b", "--buffer-pages", type=int, |
| 659 | default=Tool.DEFAULT_PERF_BUFFER_PAGES, |
| 660 | help="number of pages to use for perf_events ring buffer " |
| 661 | "(default: %(default)d)") |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 662 | # we'll refer to the userspace concepts of "pid" and "tid" by |
| 663 | # their kernel names -- tgid and pid -- inside the script |
| 664 | parser.add_argument("-p", "--pid", type=int, metavar="PID", |
| 665 | dest="tgid", help="id of the process to trace (optional)") |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 666 | parser.add_argument("-L", "--tid", type=int, metavar="TID", |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 667 | dest="pid", help="id of the thread to trace (optional)") |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 668 | parser.add_argument("-v", "--verbose", action="store_true", |
| 669 | help="print resulting BPF program code before executing") |
| 670 | parser.add_argument("-Z", "--string-size", type=int, |
| 671 | default=80, help="maximum size to read from strings") |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 672 | parser.add_argument("-S", "--include-self", |
| 673 | action="store_true", |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 674 | help="do not filter trace's own pid from the trace") |
| 675 | parser.add_argument("-M", "--max-events", type=int, |
| 676 | help="number of events to print before quitting") |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 677 | parser.add_argument("-t", "--timestamp", action="store_true", |
| 678 | help="print timestamp column (offset from trace start)") |
| 679 | parser.add_argument("-T", "--time", action="store_true", |
| 680 | help="print time column") |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 681 | parser.add_argument("-C", "--print_cpu", action="store_true", |
| 682 | help="print CPU id") |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 683 | parser.add_argument("-K", "--kernel-stack", |
| 684 | action="store_true", help="output kernel stack trace") |
| 685 | parser.add_argument("-U", "--user-stack", |
| 686 | action="store_true", help="output user stack trace") |
Mirek Klimos | e538228 | 2018-01-26 14:52:50 -0800 | [diff] [blame] | 687 | parser.add_argument("-a", "--address", action="store_true", |
| 688 | help="print virtual address in stacks") |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 689 | parser.add_argument(metavar="probe", dest="probes", nargs="+", |
| 690 | help="probe specifier (see examples)") |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 691 | parser.add_argument("-I", "--include", action="append", |
| 692 | metavar="header", |
ShelbyFrances | f5dbbdb | 2017-02-08 05:56:52 +0300 | [diff] [blame] | 693 | help="additional header files to include in the BPF program " |
Yonghong Song | f4470dc | 2017-12-13 14:12:13 -0800 | [diff] [blame] | 694 | "as either full path, " |
| 695 | "or relative to current working directory, " |
| 696 | "or relative to default kernel header search path") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 697 | parser.add_argument("--ebpf", action="store_true", |
| 698 | help=argparse.SUPPRESS) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 699 | self.args = parser.parse_args() |
Mark Drayton | aa6c916 | 2016-11-03 15:36:29 +0000 | [diff] [blame] | 700 | if self.args.tgid and self.args.pid: |
Yonghong Song | f4470dc | 2017-12-13 14:12:13 -0800 | [diff] [blame] | 701 | parser.error("only one of -p and -L may be specified") |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 702 | |
| 703 | def _create_probes(self): |
| 704 | Probe.configure(self.args) |
| 705 | self.probes = [] |
| 706 | for probe_spec in self.args.probes: |
| 707 | self.probes.append(Probe( |
Teng Qin | 6b0ed37 | 2016-09-29 21:30:13 -0700 | [diff] [blame] | 708 | probe_spec, self.args.string_size, |
| 709 | self.args.kernel_stack, self.args.user_stack)) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 710 | |
| 711 | def _generate_program(self): |
| 712 | self.program = """ |
| 713 | #include <linux/ptrace.h> |
| 714 | #include <linux/sched.h> /* For TASK_COMM_LEN */ |
| 715 | |
| 716 | """ |
Sasha Goldshtein | 4725a72 | 2016-10-18 20:54:47 +0300 | [diff] [blame] | 717 | for include in (self.args.include or []): |
ShelbyFrances | f5dbbdb | 2017-02-08 05:56:52 +0300 | [diff] [blame] | 718 | if include.startswith((".", "/")): |
| 719 | include = os.path.abspath(include) |
| 720 | self.program += "#include \"%s\"\n" % include |
| 721 | else: |
| 722 | self.program += "#include <%s>\n" % include |
Sasha Goldshtein | b950d6f | 2016-03-21 04:06:15 -0700 | [diff] [blame] | 723 | self.program += BPF.generate_auto_includes( |
Sasha Goldshtein | fd60d55 | 2016-03-01 12:15:34 -0800 | [diff] [blame] | 724 | map(lambda p: p.raw_probe, self.probes)) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 725 | for probe in self.probes: |
| 726 | self.program += probe.generate_program( |
Sasha Goldshtein | 3e39a08 | 2016-03-24 08:39:47 -0700 | [diff] [blame] | 727 | self.args.include_self) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 728 | |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 729 | if self.args.verbose or self.args.ebpf: |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 730 | print(self.program) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 731 | if self.args.ebpf: |
| 732 | exit() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 733 | |
| 734 | def _attach_probes(self): |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 735 | usdt_contexts = [] |
| 736 | for probe in self.probes: |
| 737 | if probe.usdt: |
| 738 | # USDT probes must be enabled before the BPF object |
| 739 | # is initialized, because that's where the actual |
| 740 | # uprobe is being attached. |
| 741 | probe.usdt.enable_probe( |
| 742 | probe.usdt_name, probe.probe_name) |
Sasha Goldshtein | f733cac | 2016-10-04 18:39:01 +0300 | [diff] [blame] | 743 | if self.args.verbose: |
| 744 | print(probe.usdt.get_text()) |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 745 | usdt_contexts.append(probe.usdt) |
| 746 | self.bpf = BPF(text=self.program, usdt_contexts=usdt_contexts) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 747 | for probe in self.probes: |
| 748 | if self.args.verbose: |
| 749 | print(probe) |
| 750 | probe.attach(self.bpf, self.args.verbose) |
| 751 | |
| 752 | def _main_loop(self): |
| 753 | all_probes_trivial = all(map(Probe.is_default_action, |
| 754 | self.probes)) |
| 755 | |
| 756 | # Print header |
Sasha Goldshtein | 49d50ba | 2016-12-19 10:17:38 +0000 | [diff] [blame] | 757 | if self.args.timestamp or self.args.time: |
Teng Qin | c200b6c | 2017-12-16 00:15:55 -0800 | [diff] [blame] | 758 | print("%-8s " % "TIME", end=""); |
| 759 | if self.args.print_cpu: |
| 760 | print("%-3s " % "CPU", end=""); |
| 761 | print("%-7s %-7s %-15s %-16s %s" % |
| 762 | ("PID", "TID", "COMM", "FUNC", |
| 763 | "-" if not all_probes_trivial else "")) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 764 | |
| 765 | while True: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame] | 766 | self.bpf.perf_buffer_poll() |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 767 | |
| 768 | def run(self): |
| 769 | try: |
| 770 | self._create_probes() |
| 771 | self._generate_program() |
| 772 | self._attach_probes() |
| 773 | self._main_loop() |
| 774 | except: |
Sasha Goldshtein | 2febc29 | 2017-02-13 20:25:32 -0500 | [diff] [blame] | 775 | exc_info = sys.exc_info() |
| 776 | sys_exit = exc_info[0] is SystemExit |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 777 | if self.args.verbose: |
| 778 | traceback.print_exc() |
Sasha Goldshtein | 2febc29 | 2017-02-13 20:25:32 -0500 | [diff] [blame] | 779 | elif not sys_exit: |
| 780 | print(exc_info[1]) |
| 781 | exit(0 if sys_exit else 1) |
Sasha Goldshtein | 38847f0 | 2016-02-22 02:19:24 -0800 | [diff] [blame] | 782 | |
| 783 | if __name__ == "__main__": |
Sasha Goldshtein | f41ae86 | 2016-10-19 01:14:30 +0300 | [diff] [blame] | 784 | Tool().run() |