blob: d22b5c4b5b3a9918afd4b93295fc5fc84c48d7c2 [file] [log] [blame]
Sasha Goldshtein38847f02016-02-22 02:19:24 -08001#!/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 Goldshtein49d50ba2016-12-19 10:17:38 +00006# usage: trace [-h] [-p PID] [-L TID] [-v] [-Z STRING_SIZE] [-S]
7# [-M MAX_EVENTS] [-T] [-t] [-K] [-U] [-I header]
Mark Draytonaa6c9162016-11-03 15:36:29 +00008# probe [probe ...]
Sasha Goldshteinfd60d552016-03-01 12:15:34 -08009#
Sasha Goldshtein38847f02016-02-22 02:19:24 -080010# Licensed under the Apache License, Version 2.0 (the "License")
11# Copyright (C) 2016 Sasha Goldshtein.
12
Teng Qinc200b6c2017-12-16 00:15:55 -080013from __future__ import print_function
Sasha Goldshtein376ae5c2016-10-04 19:49:57 +030014from bcc import BPF, USDT
Teng Qin6b0ed372016-09-29 21:30:13 -070015from functools import partial
Sasha Goldshtein38847f02016-02-22 02:19:24 -080016from time import sleep, strftime
17import argparse
18import re
19import ctypes as ct
20import os
21import traceback
22import sys
23
Sasha Goldshtein38847f02016-02-22 02:19:24 -080024class Probe(object):
25 probe_count = 0
Sasha Goldshteinf4797b02016-10-17 01:44:56 -070026 streq_index = 0
Sasha Goldshtein38847f02016-02-22 02:19:24 -080027 max_events = None
28 event_count = 0
29 first_ts = 0
Teng Qinc200b6c2017-12-16 00:15:55 -080030 print_time = False
Sasha Goldshtein38847f02016-02-22 02:19:24 -080031 use_localtime = True
Teng Qinc200b6c2017-12-16 00:15:55 -080032 time_field = False
33 print_cpu = False
Mark Draytonaa6c9162016-11-03 15:36:29 +000034 tgid = -1
Sasha Goldshtein3e39a082016-03-24 08:39:47 -070035 pid = -1
Mark Drayton5f5687e2017-02-20 18:13:03 +000036 page_cnt = None
Sasha Goldshtein38847f02016-02-22 02:19:24 -080037
38 @classmethod
39 def configure(cls, args):
40 cls.max_events = args.max_events
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +000041 cls.print_time = args.timestamp or args.time
42 cls.use_localtime = not args.timestamp
Teng Qinc200b6c2017-12-16 00:15:55 -080043 cls.time_field = cls.print_time and (not cls.use_localtime)
44 cls.print_cpu = args.print_cpu
Sasha Goldshtein60c41922017-02-09 04:19:53 -050045 cls.first_ts = BPF.monotonic_time()
Mark Draytonaa6c9162016-11-03 15:36:29 +000046 cls.tgid = args.tgid or -1
Sasha Goldshtein3e39a082016-03-24 08:39:47 -070047 cls.pid = args.pid or -1
Mark Drayton5f5687e2017-02-20 18:13:03 +000048 cls.page_cnt = args.buffer_pages
Sasha Goldshtein38847f02016-02-22 02:19:24 -080049
Teng Qin6b0ed372016-09-29 21:30:13 -070050 def __init__(self, probe, string_size, kernel_stack, user_stack):
Sasha Goldshtein69e361a2016-09-27 19:40:00 +030051 self.usdt = None
Sasha Goldshteinf4797b02016-10-17 01:44:56 -070052 self.streq_functions = ""
Sasha Goldshtein38847f02016-02-22 02:19:24 -080053 self.raw_probe = probe
54 self.string_size = string_size
Teng Qin6b0ed372016-09-29 21:30:13 -070055 self.kernel_stack = kernel_stack
56 self.user_stack = user_stack
Sasha Goldshtein38847f02016-02-22 02:19:24 -080057 Probe.probe_count += 1
58 self._parse_probe()
59 self.probe_num = Probe.probe_count
60 self.probe_name = "probe_%s_%d" % \
Sasha Goldshtein3e39a082016-03-24 08:39:47 -070061 (self._display_function(), self.probe_num)
Paul Chaignon956ca1c2017-03-04 20:07:56 +010062 self.probe_name = re.sub(r'[^A-Za-z0-9_]', '_',
63 self.probe_name)
Sasha Goldshtein38847f02016-02-22 02:19:24 -080064
65 def __str__(self):
Sasha Goldshtein3e39a082016-03-24 08:39:47 -070066 return "%s:%s:%s FLT=%s ACT=%s/%s" % (self.probe_type,
67 self.library, self._display_function(), self.filter,
Sasha Goldshtein38847f02016-02-22 02:19:24 -080068 self.types, self.values)
69
70 def is_default_action(self):
71 return self.python_format == ""
72
73 def _bail(self, error):
Sasha Goldshtein3e39a082016-03-24 08:39:47 -070074 raise ValueError("error in probe '%s': %s" %
Sasha Goldshtein38847f02016-02-22 02:19:24 -080075 (self.raw_probe, error))
76
77 def _parse_probe(self):
78 text = self.raw_probe
79
Sasha Goldshtein23e72b82017-01-17 08:49:36 +000080 # There might be a function signature preceding the actual
81 # filter/print part, or not. Find the probe specifier first --
82 # it ends with either a space or an open paren ( for the
83 # function signature part.
84 # opt. signature
85 # probespec | rest
86 # --------- ---------- --
87 (spec, sig, rest) = re.match(r'([^ \t\(]+)(\([^\(]*\))?(.*)',
88 text).groups()
Sasha Goldshtein38847f02016-02-22 02:19:24 -080089
Sasha Goldshtein23e72b82017-01-17 08:49:36 +000090 self._parse_spec(spec)
Paul Chaignon956ca1c2017-03-04 20:07:56 +010091 # Remove the parens
92 self.signature = sig[1:-1] if sig else None
Sasha Goldshtein23e72b82017-01-17 08:49:36 +000093 if self.signature and self.probe_type in ['u', 't']:
94 self._bail("USDT and tracepoint probes can't have " +
95 "a function signature; use arg1, arg2, " +
96 "... instead")
97
98 text = rest.lstrip()
Sasha Goldshtein38847f02016-02-22 02:19:24 -080099 # If we now have a (, wait for the balanced closing ) and that
100 # will be the predicate
101 self.filter = None
102 if len(text) > 0 and text[0] == "(":
103 balance = 1
104 for i in range(1, len(text)):
105 if text[i] == "(":
106 balance += 1
107 if text[i] == ")":
108 balance -= 1
109 if balance == 0:
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300110 self._parse_filter(text[:i + 1])
111 text = text[i + 1:]
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800112 break
113 if self.filter is None:
114 self._bail("unmatched end of predicate")
115
116 if self.filter is None:
117 self.filter = "1"
118
119 # The remainder of the text is the printf action
120 self._parse_action(text.lstrip())
121
122 def _parse_spec(self, spec):
123 parts = spec.split(":")
124 # Two special cases: 'func' means 'p::func', 'lib:func' means
125 # 'p:lib:func'. Other combinations need to provide an empty
126 # value between delimiters, e.g. 'r::func' for a kretprobe on
127 # the function func.
128 if len(parts) == 1:
129 parts = ["p", "", parts[0]]
130 elif len(parts) == 2:
131 parts = ["p", parts[0], parts[1]]
132 if len(parts[0]) == 0:
133 self.probe_type = "p"
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700134 elif parts[0] in ["p", "r", "t", "u"]:
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800135 self.probe_type = parts[0]
136 else:
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700137 self._bail("probe type must be '', 'p', 't', 'r', " +
138 "or 'u', but got '%s'" % parts[0])
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800139 if self.probe_type == "t":
140 self.tp_category = parts[1]
141 self.tp_event = parts[2]
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800142 self.library = "" # kernel
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300143 self.function = "" # from TRACEPOINT_PROBE
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700144 elif self.probe_type == "u":
vkhromov5a2b39e2017-07-14 20:42:29 +0100145 self.library = ':'.join(parts[1:-1])
146 self.usdt_name = parts[-1]
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700147 self.function = "" # no function, just address
148 # We will discover the USDT provider by matching on
149 # the USDT name in the specified library
150 self._find_usdt_probe()
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800151 else:
vkhromov5a2b39e2017-07-14 20:42:29 +0100152 self.library = ':'.join(parts[1:-1])
153 self.function = parts[-1]
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800154
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700155 def _find_usdt_probe(self):
Sasha Goldshteindd045362016-11-13 05:07:38 -0800156 target = Probe.pid if Probe.pid and Probe.pid != -1 \
157 else Probe.tgid
Mark Draytonaa6c9162016-11-03 15:36:29 +0000158 self.usdt = USDT(path=self.library, pid=target)
Sasha Goldshtein69e361a2016-09-27 19:40:00 +0300159 for probe in self.usdt.enumerate_probes():
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700160 if probe.name == self.usdt_name:
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300161 return # Found it, will enable later
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700162 self._bail("unrecognized USDT probe %s" % self.usdt_name)
163
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800164 def _parse_filter(self, filt):
Sasha Goldshteinf4797b02016-10-17 01:44:56 -0700165 self.filter = self._rewrite_expr(filt)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800166
167 def _parse_types(self, fmt):
168 for match in re.finditer(
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300169 r'[^%]%(s|u|d|llu|lld|hu|hd|x|llx|c|K|U)', fmt):
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800170 self.types.append(match.group(1))
171 fmt = re.sub(r'([^%]%)(u|d|llu|lld|hu|hd)', r'\1d', fmt)
172 fmt = re.sub(r'([^%]%)(x|llx)', r'\1x', fmt)
Sasha Goldshteinaccd4cf2016-10-11 07:56:13 -0700173 fmt = re.sub('%K|%U', '%s', fmt)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800174 self.python_format = fmt.strip('"')
175
176 def _parse_action(self, action):
177 self.values = []
178 self.types = []
179 self.python_format = ""
180 if len(action) == 0:
181 return
182
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800183 action = action.strip()
Sasha Goldshteinf4797b02016-10-17 01:44:56 -0700184 match = re.search(r'(\".*?\"),?(.*)', action)
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800185 if match is None:
186 self._bail("expected format string in \"s")
187
188 self.raw_format = match.group(1)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800189 self._parse_types(self.raw_format)
Sasha Goldshteinf4797b02016-10-17 01:44:56 -0700190 for part in re.split('(?<!"),', match.group(2)):
191 part = self._rewrite_expr(part)
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800192 if len(part) > 0:
193 self.values.append(part)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800194
195 aliases = {
Naveen N. Rao4afa96a2016-05-03 14:54:21 +0530196 "retval": "PT_REGS_RC(ctx)",
197 "arg1": "PT_REGS_PARM1(ctx)",
198 "arg2": "PT_REGS_PARM2(ctx)",
199 "arg3": "PT_REGS_PARM3(ctx)",
200 "arg4": "PT_REGS_PARM4(ctx)",
201 "arg5": "PT_REGS_PARM5(ctx)",
202 "arg6": "PT_REGS_PARM6(ctx)",
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800203 "$uid": "(unsigned)(bpf_get_current_uid_gid() & 0xffffffff)",
204 "$gid": "(unsigned)(bpf_get_current_uid_gid() >> 32)",
205 "$pid": "(unsigned)(bpf_get_current_pid_tgid() & 0xffffffff)",
206 "$tgid": "(unsigned)(bpf_get_current_pid_tgid() >> 32)",
Yonghong Songf92fef22018-01-24 20:51:46 -0800207 "$cpu": "bpf_get_smp_processor_id()",
208 "$task" : "((struct task_struct *)bpf_get_current_task())"
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800209 }
210
Sasha Goldshteinf4797b02016-10-17 01:44:56 -0700211 def _generate_streq_function(self, string):
212 fname = "streq_%d" % Probe.streq_index
213 Probe.streq_index += 1
214 self.streq_functions += """
Sasha Goldshteinb9aec342017-01-16 18:41:22 +0000215static inline bool %s(char const *ignored, uintptr_t str) {
Sasha Goldshteinf4797b02016-10-17 01:44:56 -0700216 char needle[] = %s;
217 char haystack[sizeof(needle)];
218 bpf_probe_read(&haystack, sizeof(haystack), (void *)str);
Sasha Goldshteindcf16752017-01-17 07:40:57 +0000219 for (int i = 0; i < sizeof(needle) - 1; ++i) {
Sasha Goldshteinf4797b02016-10-17 01:44:56 -0700220 if (needle[i] != haystack[i]) {
221 return false;
222 }
223 }
224 return true;
225}
226 """ % (fname, string)
227 return fname
228
229 def _rewrite_expr(self, expr):
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800230 for alias, replacement in Probe.aliases.items():
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700231 # For USDT probes, we replace argN values with the
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300232 # actual arguments for that probe obtained using
Sasha Goldshtein69e361a2016-09-27 19:40:00 +0300233 # bpf_readarg_N macros emitted at BPF construction.
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700234 if alias.startswith("arg") and self.probe_type == "u":
235 continue
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800236 expr = expr.replace(alias, replacement)
Sasha Goldshteinf4797b02016-10-17 01:44:56 -0700237 matches = re.finditer('STRCMP\\(("[^"]+\\")', expr)
238 for match in matches:
239 string = match.group(1)
240 fname = self._generate_streq_function(string)
241 expr = expr.replace("STRCMP", fname, 1)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800242 return expr
243
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300244 p_type = {"u": ct.c_uint, "d": ct.c_int,
245 "llu": ct.c_ulonglong, "lld": ct.c_longlong,
246 "hu": ct.c_ushort, "hd": ct.c_short,
247 "x": ct.c_uint, "llx": ct.c_ulonglong, "c": ct.c_ubyte,
248 "K": ct.c_ulonglong, "U": ct.c_ulonglong}
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800249
250 def _generate_python_field_decl(self, idx, fields):
251 field_type = self.types[idx]
252 if field_type == "s":
253 ptype = ct.c_char * self.string_size
254 else:
255 ptype = Probe.p_type[field_type]
256 fields.append(("v%d" % idx, ptype))
257
258 def _generate_python_data_decl(self):
259 self.python_struct_name = "%s_%d_Data" % \
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700260 (self._display_function(), self.probe_num)
Teng Qinc200b6c2017-12-16 00:15:55 -0800261 fields = []
262 if self.time_field:
263 fields.append(("timestamp_ns", ct.c_ulonglong))
264 if self.print_cpu:
265 fields.append(("cpu", ct.c_int))
266 fields.extend([
Mark Draytonaa6c9162016-11-03 15:36:29 +0000267 ("tgid", ct.c_uint),
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800268 ("pid", ct.c_uint),
269 ("comm", ct.c_char * 16) # TASK_COMM_LEN
Teng Qinc200b6c2017-12-16 00:15:55 -0800270 ])
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800271 for i in range(0, len(self.types)):
272 self._generate_python_field_decl(i, fields)
Teng Qin6b0ed372016-09-29 21:30:13 -0700273 if self.kernel_stack:
274 fields.append(("kernel_stack_id", ct.c_int))
275 if self.user_stack:
276 fields.append(("user_stack_id", ct.c_int))
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800277 return type(self.python_struct_name, (ct.Structure,),
278 dict(_fields_=fields))
279
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300280 c_type = {"u": "unsigned int", "d": "int",
281 "llu": "unsigned long long", "lld": "long long",
282 "hu": "unsigned short", "hd": "short",
283 "x": "unsigned int", "llx": "unsigned long long",
284 "c": "char", "K": "unsigned long long",
285 "U": "unsigned long long"}
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800286 fmt_types = c_type.keys()
287
288 def _generate_field_decl(self, idx):
289 field_type = self.types[idx]
290 if field_type == "s":
291 return "char v%d[%d];\n" % (idx, self.string_size)
292 if field_type in Probe.fmt_types:
293 return "%s v%d;\n" % (Probe.c_type[field_type], idx)
294 self._bail("unrecognized format specifier %s" % field_type)
295
296 def _generate_data_decl(self):
297 # The BPF program will populate values into the struct
298 # according to the format string, and the Python program will
299 # construct the final display string.
300 self.events_name = "%s_events" % self.probe_name
301 self.struct_name = "%s_data_t" % self.probe_name
Teng Qin6b0ed372016-09-29 21:30:13 -0700302 self.stacks_name = "%s_stacks" % self.probe_name
303 stack_table = "BPF_STACK_TRACE(%s, 1024);" % self.stacks_name \
304 if (self.kernel_stack or self.user_stack) else ""
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800305 data_fields = ""
306 for i, field_type in enumerate(self.types):
307 data_fields += " " + \
308 self._generate_field_decl(i)
Teng Qinc200b6c2017-12-16 00:15:55 -0800309 time_str = "u64 timestamp_ns;" if self.time_field else ""
310 cpu_str = "int cpu;" if self.print_cpu else ""
Teng Qin6b0ed372016-09-29 21:30:13 -0700311 kernel_stack_str = " int kernel_stack_id;" \
312 if self.kernel_stack else ""
313 user_stack_str = " int user_stack_id;" \
314 if self.user_stack else ""
315
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800316 text = """
317struct %s
318{
Teng Qinc200b6c2017-12-16 00:15:55 -0800319%s
320%s
Mark Draytonaa6c9162016-11-03 15:36:29 +0000321 u32 tgid;
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800322 u32 pid;
323 char comm[TASK_COMM_LEN];
324%s
Teng Qin6b0ed372016-09-29 21:30:13 -0700325%s
326%s
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800327};
328
329BPF_PERF_OUTPUT(%s);
Teng Qin6b0ed372016-09-29 21:30:13 -0700330%s
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800331"""
Teng Qinc200b6c2017-12-16 00:15:55 -0800332 return text % (self.struct_name, time_str, cpu_str, data_fields,
Teng Qin6b0ed372016-09-29 21:30:13 -0700333 kernel_stack_str, user_stack_str,
334 self.events_name, stack_table)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800335
336 def _generate_field_assign(self, idx):
337 field_type = self.types[idx]
Sasha Goldshtein69e361a2016-09-27 19:40:00 +0300338 expr = self.values[idx].strip()
339 text = ""
340 if self.probe_type == "u" and expr[0:3] == "arg":
Sasha Goldshtein3a5256f2017-02-20 15:42:57 +0000341 arg_index = int(expr[3])
342 arg_ctype = self.usdt.get_probe_arg_ctype(
343 self.usdt_name, arg_index - 1)
344 text = (" %s %s = 0;\n" +
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300345 " bpf_usdt_readarg(%s, ctx, &%s);\n") \
Sasha Goldshtein3a5256f2017-02-20 15:42:57 +0000346 % (arg_ctype, expr, expr[3], expr)
Sasha Goldshtein69e361a2016-09-27 19:40:00 +0300347
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800348 if field_type == "s":
Sasha Goldshtein69e361a2016-09-27 19:40:00 +0300349 return text + """
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800350 if (%s != 0) {
351 bpf_probe_read(&__data.v%d, sizeof(__data.v%d), (void *)%s);
352 }
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300353 """ % (expr, idx, idx, expr)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800354 if field_type in Probe.fmt_types:
Sasha Goldshtein69e361a2016-09-27 19:40:00 +0300355 return text + " __data.v%d = (%s)%s;\n" % \
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800356 (idx, Probe.c_type[field_type], expr)
357 self._bail("unrecognized field type %s" % field_type)
358
Teng Qin0615bff2016-09-28 08:19:40 -0700359 def _generate_usdt_filter_read(self):
360 text = ""
Sasha Goldshteinb6300922017-01-16 18:43:11 +0000361 if self.probe_type != "u":
362 return text
363 for arg, _ in Probe.aliases.items():
364 if not (arg.startswith("arg") and
365 (arg in self.filter)):
366 continue
367 arg_index = int(arg.replace("arg", ""))
368 arg_ctype = self.usdt.get_probe_arg_ctype(
Sasha Goldshteindcf16752017-01-17 07:40:57 +0000369 self.usdt_name, arg_index - 1)
Sasha Goldshteinb6300922017-01-16 18:43:11 +0000370 if not arg_ctype:
371 self._bail("Unable to determine type of {} "
372 "in the filter".format(arg))
373 text += """
Teng Qin0615bff2016-09-28 08:19:40 -0700374 {} {}_filter;
375 bpf_usdt_readarg({}, ctx, &{}_filter);
Sasha Goldshteinb6300922017-01-16 18:43:11 +0000376 """.format(arg_ctype, arg, arg_index, arg)
377 self.filter = self.filter.replace(
378 arg, "{}_filter".format(arg))
Teng Qin0615bff2016-09-28 08:19:40 -0700379 return text
380
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700381 def generate_program(self, include_self):
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800382 data_decl = self._generate_data_decl()
Sasha Goldshteinb6300922017-01-16 18:43:11 +0000383 if Probe.pid != -1:
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800384 pid_filter = """
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800385 if (__pid != %d) { return 0; }
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300386 """ % Probe.pid
Sasha Goldshteinb6300922017-01-16 18:43:11 +0000387 # uprobes can have a built-in tgid filter passed to
388 # attach_uprobe, hence the check here -- for kprobes, we
389 # need to do the tgid test by hand:
Mark Draytonaa6c9162016-11-03 15:36:29 +0000390 elif len(self.library) == 0 and Probe.tgid != -1:
391 pid_filter = """
392 if (__tgid != %d) { return 0; }
393 """ % Probe.tgid
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800394 elif not include_self:
395 pid_filter = """
Mark Draytonaa6c9162016-11-03 15:36:29 +0000396 if (__tgid == %d) { return 0; }
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300397 """ % os.getpid()
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800398 else:
399 pid_filter = ""
400
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700401 prefix = ""
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700402 signature = "struct pt_regs *ctx"
Sasha Goldshtein23e72b82017-01-17 08:49:36 +0000403 if self.signature:
404 signature += ", " + self.signature
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700405
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800406 data_fields = ""
407 for i, expr in enumerate(self.values):
408 data_fields += self._generate_field_assign(i)
409
Sasha Goldshtein376ae5c2016-10-04 19:49:57 +0300410 if self.probe_type == "t":
411 heading = "TRACEPOINT_PROBE(%s, %s)" % \
412 (self.tp_category, self.tp_event)
413 ctx_name = "args"
414 else:
415 heading = "int %s(%s)" % (self.probe_name, signature)
416 ctx_name = "ctx"
Sasha Goldshtein4725a722016-10-18 20:54:47 +0300417
Teng Qinc200b6c2017-12-16 00:15:55 -0800418 time_str = """
419 __data.timestamp_ns = bpf_ktime_get_ns();""" if self.time_field else ""
420 cpu_str = """
421 __data.cpu = bpf_get_smp_processor_id();""" if self.print_cpu else ""
Sasha Goldshtein4725a722016-10-18 20:54:47 +0300422 stack_trace = ""
423 if self.user_stack:
424 stack_trace += """
425 __data.user_stack_id = %s.get_stackid(
426 %s, BPF_F_REUSE_STACKID | BPF_F_USER_STACK
427 );""" % (self.stacks_name, ctx_name)
428 if self.kernel_stack:
429 stack_trace += """
430 __data.kernel_stack_id = %s.get_stackid(
431 %s, BPF_F_REUSE_STACKID
432 );""" % (self.stacks_name, ctx_name)
433
Sasha Goldshtein376ae5c2016-10-04 19:49:57 +0300434 text = heading + """
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800435{
Mark Draytonaa6c9162016-11-03 15:36:29 +0000436 u64 __pid_tgid = bpf_get_current_pid_tgid();
437 u32 __tgid = __pid_tgid >> 32;
438 u32 __pid = __pid_tgid; // implicit cast to u32 for bottom half
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800439 %s
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800440 %s
Teng Qin0615bff2016-09-28 08:19:40 -0700441 %s
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800442 if (!(%s)) return 0;
443
444 struct %s __data = {0};
Teng Qinc200b6c2017-12-16 00:15:55 -0800445 %s
446 %s
Mark Draytonaa6c9162016-11-03 15:36:29 +0000447 __data.tgid = __tgid;
448 __data.pid = __pid;
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800449 bpf_get_current_comm(&__data.comm, sizeof(__data.comm));
450%s
Teng Qin6b0ed372016-09-29 21:30:13 -0700451%s
Sasha Goldshtein376ae5c2016-10-04 19:49:57 +0300452 %s.perf_submit(%s, &__data, sizeof(__data));
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800453 return 0;
454}
455"""
Sasha Goldshtein376ae5c2016-10-04 19:49:57 +0300456 text = text % (pid_filter, prefix,
Teng Qin0615bff2016-09-28 08:19:40 -0700457 self._generate_usdt_filter_read(), self.filter,
Teng Qinc200b6c2017-12-16 00:15:55 -0800458 self.struct_name, time_str, cpu_str, data_fields,
Sasha Goldshtein376ae5c2016-10-04 19:49:57 +0300459 stack_trace, self.events_name, ctx_name)
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700460
Sasha Goldshteinf4797b02016-10-17 01:44:56 -0700461 return self.streq_functions + data_decl + "\n" + text
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800462
463 @classmethod
464 def _time_off_str(cls, timestamp_ns):
465 return "%.6f" % (1e-9 * (timestamp_ns - cls.first_ts))
466
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800467 def _display_function(self):
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700468 if self.probe_type == 'p' or self.probe_type == 'r':
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800469 return self.function
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700470 elif self.probe_type == 'u':
471 return self.usdt_name
472 else: # self.probe_type == 't'
473 return self.tp_event
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800474
Mark Draytonaa6c9162016-11-03 15:36:29 +0000475 def print_stack(self, bpf, stack_id, tgid):
Teng Qin6b0ed372016-09-29 21:30:13 -0700476 if stack_id < 0:
Sasha Goldshteinaccd4cf2016-10-11 07:56:13 -0700477 print(" %d" % stack_id)
478 return
Teng Qin6b0ed372016-09-29 21:30:13 -0700479
480 stack = list(bpf.get_table(self.stacks_name).walk(stack_id))
481 for addr in stack:
Sasha Goldshtein1e34f4e2017-02-09 00:21:49 -0500482 print(" %s" % (bpf.sym(addr, tgid,
Sasha Goldshtein01553852017-02-09 03:58:09 -0500483 show_module=True, show_offset=True)))
Sasha Goldshteinaccd4cf2016-10-11 07:56:13 -0700484
Mark Draytonaa6c9162016-11-03 15:36:29 +0000485 def _format_message(self, bpf, tgid, values):
486 # Replace each %K with kernel sym and %U with user sym in tgid
Rafael Fonsecaaee5ecf2017-02-08 16:14:31 +0100487 kernel_placeholders = [i for i, t in enumerate(self.types)
488 if t == 'K']
489 user_placeholders = [i for i, t in enumerate(self.types)
490 if t == 'U']
Sasha Goldshteinaccd4cf2016-10-11 07:56:13 -0700491 for kp in kernel_placeholders:
Sasha Goldshtein01553852017-02-09 03:58:09 -0500492 values[kp] = bpf.ksym(values[kp], show_offset=True)
Sasha Goldshteinaccd4cf2016-10-11 07:56:13 -0700493 for up in user_placeholders:
Sasha Goldshtein1e34f4e2017-02-09 00:21:49 -0500494 values[up] = bpf.sym(values[up], tgid,
Sasha Goldshtein01553852017-02-09 03:58:09 -0500495 show_module=True, show_offset=True)
Sasha Goldshteinaccd4cf2016-10-11 07:56:13 -0700496 return self.python_format % tuple(values)
Teng Qin6b0ed372016-09-29 21:30:13 -0700497
498 def print_event(self, bpf, cpu, data, size):
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800499 # Cast as the generated structure type and display
500 # according to the format string in the probe.
501 event = ct.cast(data, ct.POINTER(self.python_struct)).contents
502 values = map(lambda i: getattr(event, "v%d" % i),
503 range(0, len(self.values)))
Mark Draytonaa6c9162016-11-03 15:36:29 +0000504 msg = self._format_message(bpf, event.tgid, values)
Teng Qinc200b6c2017-12-16 00:15:55 -0800505 if Probe.print_time:
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +0000506 time = strftime("%H:%M:%S") if Probe.use_localtime else \
507 Probe._time_off_str(event.timestamp_ns)
Teng Qinc200b6c2017-12-16 00:15:55 -0800508 print("%-8s " % time[:8], end="")
509 if Probe.print_cpu:
510 print("%-3s " % event.cpu, end="")
511 print("%-7d %-7d %-15s %-16s %s" %
512 (event.tgid, event.pid, event.comm.decode(),
513 self._display_function(), msg))
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800514
Teng Qin6b0ed372016-09-29 21:30:13 -0700515 if self.kernel_stack:
Sasha Goldshteinaccd4cf2016-10-11 07:56:13 -0700516 self.print_stack(bpf, event.kernel_stack_id, -1)
Mark Draytonaa6c9162016-11-03 15:36:29 +0000517 if self.user_stack:
518 self.print_stack(bpf, event.user_stack_id, event.tgid)
Teng Qin6b0ed372016-09-29 21:30:13 -0700519 if self.user_stack or self.kernel_stack:
Sasha Goldshteinaccd4cf2016-10-11 07:56:13 -0700520 print("")
Teng Qin6b0ed372016-09-29 21:30:13 -0700521
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800522 Probe.event_count += 1
523 if Probe.max_events is not None and \
524 Probe.event_count >= Probe.max_events:
525 exit()
526
527 def attach(self, bpf, verbose):
528 if len(self.library) == 0:
529 self._attach_k(bpf)
530 else:
531 self._attach_u(bpf)
532 self.python_struct = self._generate_python_data_decl()
Teng Qin6b0ed372016-09-29 21:30:13 -0700533 callback = partial(self.print_event, bpf)
Mark Drayton5f5687e2017-02-20 18:13:03 +0000534 bpf[self.events_name].open_perf_buffer(callback,
535 page_cnt=self.page_cnt)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800536
537 def _attach_k(self, bpf):
538 if self.probe_type == "r":
539 bpf.attach_kretprobe(event=self.function,
540 fn_name=self.probe_name)
Sasha Goldshtein376ae5c2016-10-04 19:49:57 +0300541 elif self.probe_type == "p":
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800542 bpf.attach_kprobe(event=self.function,
543 fn_name=self.probe_name)
Sasha Goldshtein376ae5c2016-10-04 19:49:57 +0300544 # Note that tracepoints don't need an explicit attach
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800545
546 def _attach_u(self, bpf):
547 libpath = BPF.find_library(self.library)
548 if libpath is None:
549 # This might be an executable (e.g. 'bash')
Sasha Goldshteinec679712016-10-04 18:33:36 +0300550 libpath = BPF.find_exe(self.library)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800551 if libpath is None or len(libpath) == 0:
552 self._bail("unable to find library %s" % self.library)
553
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700554 if self.probe_type == "u":
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300555 pass # Was already enabled by the BPF constructor
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700556 elif self.probe_type == "r":
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800557 bpf.attach_uretprobe(name=libpath,
558 sym=self.function,
559 fn_name=self.probe_name,
Sasha Goldshteinb6300922017-01-16 18:43:11 +0000560 pid=Probe.tgid)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800561 else:
562 bpf.attach_uprobe(name=libpath,
563 sym=self.function,
564 fn_name=self.probe_name,
Sasha Goldshteinb6300922017-01-16 18:43:11 +0000565 pid=Probe.tgid)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800566
567class Tool(object):
Mark Drayton5f5687e2017-02-20 18:13:03 +0000568 DEFAULT_PERF_BUFFER_PAGES = 64
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800569 examples = """
570EXAMPLES:
571
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800572trace do_sys_open
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800573 Trace the open syscall and print a default trace message when entered
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800574trace 'do_sys_open "%s", arg2'
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800575 Trace the open syscall and print the filename being opened
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800576trace 'sys_read (arg3 > 20000) "read %d bytes", arg3'
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800577 Trace the read syscall and print a message for reads >20000 bytes
Sasha Goldshtein23e72b82017-01-17 08:49:36 +0000578trace 'r::do_sys_open "%llx", retval'
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800579 Trace the return from the open syscall and print the return value
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800580trace 'c:open (arg2 == 42) "%s %d", arg1, arg2'
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800581 Trace the open() call from libc only if the flags (arg2) argument is 42
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800582trace 'c:malloc "size = %d", arg1'
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800583 Trace malloc calls and print the size being allocated
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800584trace 'p:c:write (arg1 == 1) "writing %d bytes to STDOUT", arg3'
585 Trace the write() call from libc to monitor writes to STDOUT
Mark Draytonaa6c9162016-11-03 15:36:29 +0000586trace 'r::__kmalloc (retval == 0) "kmalloc failed!"'
Sasha Goldshtein8acd0152016-02-22 02:25:03 -0800587 Trace returns from __kmalloc which returned a null pointer
Mark Draytonaa6c9162016-11-03 15:36:29 +0000588trace 'r:c:malloc (retval) "allocated = %x", retval'
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800589 Trace returns from malloc and print non-NULL allocated buffers
Sasha Goldshtein376ae5c2016-10-04 19:49:57 +0300590trace 't:block:block_rq_complete "sectors=%d", args->nr_sector'
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800591 Trace the block_rq_complete kernel tracepoint and print # of tx sectors
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700592trace 'u:pthread:pthread_create (arg4 != 0)'
593 Trace the USDT probe pthread_create when its 4th argument is non-zero
Sasha Goldshtein23e72b82017-01-17 08:49:36 +0000594trace 'p::SyS_nanosleep(struct timespec *ts) "sleep for %lld ns", ts->tv_nsec'
595 Trace the nanosleep syscall and print the sleep duration in ns
Yonghong Songf4470dc2017-12-13 14:12:13 -0800596trace -I 'linux/fs.h' \\
597 'p::uprobe_register(struct inode *inode) "a_ops = %llx", inode->i_mapping->a_ops'
598 Trace the uprobe_register inode mapping ops, and the symbol can be found
599 in /proc/kallsyms
600trace -I 'kernel/sched/sched.h' \\
601 'p::__account_cfs_rq_runtime(struct cfs_rq *cfs_rq) "%d", cfs_rq->runtime_remaining'
602 Trace the cfs scheduling runqueue remaining runtime. The struct cfs_rq is defined
603 in kernel/sched/sched.h which is in kernel source tree and not in kernel-devel
604 package. So this command needs to run at the kernel source tree root directory
605 so that the added header file can be found by the compiler.
tehnerd86293f02018-01-23 21:21:58 -0800606trace -I 'net/sock.h' \\
607 'udpv6_sendmsg(struct sock *sk) (sk->sk_dport == 13568)'
608 Trace udpv6 sendmsg calls only if socket's destination port is equal
609 to 53 (DNS; 13568 in big endian order)
Yonghong Songf92fef22018-01-24 20:51:46 -0800610trace -I 'linux/fs_struct.h' 'mntns_install "users = %d", $task->fs->users'
611 Trace the number of users accessing the file system of the current task
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800612"""
613
614 def __init__(self):
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300615 parser = argparse.ArgumentParser(description="Attach to " +
616 "functions and print trace messages.",
617 formatter_class=argparse.RawDescriptionHelpFormatter,
618 epilog=Tool.examples)
Mark Drayton5f5687e2017-02-20 18:13:03 +0000619 parser.add_argument("-b", "--buffer-pages", type=int,
620 default=Tool.DEFAULT_PERF_BUFFER_PAGES,
621 help="number of pages to use for perf_events ring buffer "
622 "(default: %(default)d)")
Mark Draytonaa6c9162016-11-03 15:36:29 +0000623 # we'll refer to the userspace concepts of "pid" and "tid" by
624 # their kernel names -- tgid and pid -- inside the script
625 parser.add_argument("-p", "--pid", type=int, metavar="PID",
626 dest="tgid", help="id of the process to trace (optional)")
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +0000627 parser.add_argument("-L", "--tid", type=int, metavar="TID",
Mark Draytonaa6c9162016-11-03 15:36:29 +0000628 dest="pid", help="id of the thread to trace (optional)")
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800629 parser.add_argument("-v", "--verbose", action="store_true",
630 help="print resulting BPF program code before executing")
631 parser.add_argument("-Z", "--string-size", type=int,
632 default=80, help="maximum size to read from strings")
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300633 parser.add_argument("-S", "--include-self",
634 action="store_true",
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800635 help="do not filter trace's own pid from the trace")
636 parser.add_argument("-M", "--max-events", type=int,
637 help="number of events to print before quitting")
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +0000638 parser.add_argument("-t", "--timestamp", action="store_true",
639 help="print timestamp column (offset from trace start)")
640 parser.add_argument("-T", "--time", action="store_true",
641 help="print time column")
Teng Qinc200b6c2017-12-16 00:15:55 -0800642 parser.add_argument("-C", "--print_cpu", action="store_true",
643 help="print CPU id")
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300644 parser.add_argument("-K", "--kernel-stack",
645 action="store_true", help="output kernel stack trace")
646 parser.add_argument("-U", "--user-stack",
647 action="store_true", help="output user stack trace")
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800648 parser.add_argument(metavar="probe", dest="probes", nargs="+",
649 help="probe specifier (see examples)")
Sasha Goldshtein4725a722016-10-18 20:54:47 +0300650 parser.add_argument("-I", "--include", action="append",
651 metavar="header",
ShelbyFrancesf5dbbdb2017-02-08 05:56:52 +0300652 help="additional header files to include in the BPF program "
Yonghong Songf4470dc2017-12-13 14:12:13 -0800653 "as either full path, "
654 "or relative to current working directory, "
655 "or relative to default kernel header search path")
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800656 self.args = parser.parse_args()
Mark Draytonaa6c9162016-11-03 15:36:29 +0000657 if self.args.tgid and self.args.pid:
Yonghong Songf4470dc2017-12-13 14:12:13 -0800658 parser.error("only one of -p and -L may be specified")
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800659
660 def _create_probes(self):
661 Probe.configure(self.args)
662 self.probes = []
663 for probe_spec in self.args.probes:
664 self.probes.append(Probe(
Teng Qin6b0ed372016-09-29 21:30:13 -0700665 probe_spec, self.args.string_size,
666 self.args.kernel_stack, self.args.user_stack))
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800667
668 def _generate_program(self):
669 self.program = """
670#include <linux/ptrace.h>
671#include <linux/sched.h> /* For TASK_COMM_LEN */
672
673"""
Sasha Goldshtein4725a722016-10-18 20:54:47 +0300674 for include in (self.args.include or []):
ShelbyFrancesf5dbbdb2017-02-08 05:56:52 +0300675 if include.startswith((".", "/")):
676 include = os.path.abspath(include)
677 self.program += "#include \"%s\"\n" % include
678 else:
679 self.program += "#include <%s>\n" % include
Sasha Goldshteinb950d6f2016-03-21 04:06:15 -0700680 self.program += BPF.generate_auto_includes(
Sasha Goldshteinfd60d552016-03-01 12:15:34 -0800681 map(lambda p: p.raw_probe, self.probes))
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800682 for probe in self.probes:
683 self.program += probe.generate_program(
Sasha Goldshtein3e39a082016-03-24 08:39:47 -0700684 self.args.include_self)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800685
686 if self.args.verbose:
687 print(self.program)
688
689 def _attach_probes(self):
Sasha Goldshtein69e361a2016-09-27 19:40:00 +0300690 usdt_contexts = []
691 for probe in self.probes:
692 if probe.usdt:
693 # USDT probes must be enabled before the BPF object
694 # is initialized, because that's where the actual
695 # uprobe is being attached.
696 probe.usdt.enable_probe(
697 probe.usdt_name, probe.probe_name)
Sasha Goldshteinf733cac2016-10-04 18:39:01 +0300698 if self.args.verbose:
699 print(probe.usdt.get_text())
Sasha Goldshtein69e361a2016-09-27 19:40:00 +0300700 usdt_contexts.append(probe.usdt)
701 self.bpf = BPF(text=self.program, usdt_contexts=usdt_contexts)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800702 for probe in self.probes:
703 if self.args.verbose:
704 print(probe)
705 probe.attach(self.bpf, self.args.verbose)
706
707 def _main_loop(self):
708 all_probes_trivial = all(map(Probe.is_default_action,
709 self.probes))
710
711 # Print header
Sasha Goldshtein49d50ba2016-12-19 10:17:38 +0000712 if self.args.timestamp or self.args.time:
Teng Qinc200b6c2017-12-16 00:15:55 -0800713 print("%-8s " % "TIME", end="");
714 if self.args.print_cpu:
715 print("%-3s " % "CPU", end="");
716 print("%-7s %-7s %-15s %-16s %s" %
717 ("PID", "TID", "COMM", "FUNC",
718 "-" if not all_probes_trivial else ""))
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800719
720 while True:
721 self.bpf.kprobe_poll()
722
723 def run(self):
724 try:
725 self._create_probes()
726 self._generate_program()
727 self._attach_probes()
728 self._main_loop()
729 except:
Sasha Goldshtein2febc292017-02-13 20:25:32 -0500730 exc_info = sys.exc_info()
731 sys_exit = exc_info[0] is SystemExit
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800732 if self.args.verbose:
733 traceback.print_exc()
Sasha Goldshtein2febc292017-02-13 20:25:32 -0500734 elif not sys_exit:
735 print(exc_info[1])
736 exit(0 if sys_exit else 1)
Sasha Goldshtein38847f02016-02-22 02:19:24 -0800737
738if __name__ == "__main__":
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300739 Tool().run()