Skip to content

Commit

Permalink
Remove handling of KeyboardInterrupt exception (iovisor#1843)
Browse files Browse the repository at this point in the history
KeyboardInterrupt exception is not handled anymore. It will be
propagated and handled by the caller.
  • Loading branch information
banh-gao authored and yonghong-song committed Jun 21, 2018
1 parent 365eade commit ee9f0e0
Showing 1 changed file with 34 additions and 45 deletions.
79 changes: 34 additions & 45 deletions src/python/bcc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,30 +1041,27 @@ def trace_fields(self, nonblocking=False):
fields (task, pid, cpu, flags, timestamp, msg) or None if no
line was read (nonblocking=True)
"""
try:
while True:
line = self.trace_readline(nonblocking)
if not line and nonblocking: return (None,) * 6
# don't print messages related to lost events
if line.startswith(b"CPU:"): continue
task = line[:16].lstrip()
line = line[17:]
ts_end = line.find(b":")
pid, cpu, flags, ts = line[:ts_end].split()
cpu = cpu[1:-1]
# line[ts_end:] will have ": [sym_or_addr]: msgs"
# For trace_pipe debug output, the addr typically
# is invalid (e.g., 0x1). For kernel 4.12 or earlier,
# if address is not able to match a kernel symbol,
# nothing will be printed out. For kernel 4.13 and later,
# however, the illegal address will be printed out.
# Hence, both cases are handled here.
line = line[ts_end + 1:]
sym_end = line.find(b":")
msg = line[sym_end + 2:]
return (task, int(pid), int(cpu), flags, float(ts), msg)
except KeyboardInterrupt:
exit()
while True:
line = self.trace_readline(nonblocking)
if not line and nonblocking: return (None,) * 6
# don't print messages related to lost events
if line.startswith(b"CPU:"): continue
task = line[:16].lstrip()
line = line[17:]
ts_end = line.find(b":")
pid, cpu, flags, ts = line[:ts_end].split()
cpu = cpu[1:-1]
# line[ts_end:] will have ": [sym_or_addr]: msgs"
# For trace_pipe debug output, the addr typically
# is invalid (e.g., 0x1). For kernel 4.12 or earlier,
# if address is not able to match a kernel symbol,
# nothing will be printed out. For kernel 4.13 and later,
# however, the illegal address will be printed out.
# Hence, both cases are handled here.
line = line[ts_end + 1:]
sym_end = line.find(b":")
msg = line[sym_end + 2:]
return (task, int(pid), int(cpu), flags, float(ts), msg)

def trace_readline(self, nonblocking=False):
"""trace_readline(nonblocking=False)
Expand All @@ -1080,8 +1077,6 @@ def trace_readline(self, nonblocking=False):
line = trace.readline(1024).rstrip()
except IOError:
pass
except KeyboardInterrupt:
exit()
return line

def trace_print(self, fmt=None):
Expand All @@ -1093,18 +1088,15 @@ def trace_print(self, fmt=None):
example: trace_print(fmt="pid {1}, msg = {5}")
"""

try:
while True:
if fmt:
fields = self.trace_fields(nonblocking=False)
if not fields: continue
line = fmt.format(*fields)
else:
line = self.trace_readline(nonblocking=False)
print(line)
sys.stdout.flush()
except KeyboardInterrupt:
exit()
while True:
if fmt:
fields = self.trace_fields(nonblocking=False)
if not fields: continue
line = fmt.format(*fields)
else:
line = self.trace_readline(nonblocking=False)
print(line)
sys.stdout.flush()

@staticmethod
def _sym_cache(pid):
Expand Down Expand Up @@ -1194,13 +1186,10 @@ def perf_buffer_poll(self, timeout = -1):
Poll from all open perf ring buffers, calling the callback that was
provided when calling open_perf_buffer for each entry.
"""
try:
readers = (ct.c_void_p * len(self.perf_buffers))()
for i, v in enumerate(self.perf_buffers.values()):
readers[i] = v
lib.perf_reader_poll(len(readers), readers, timeout)
except KeyboardInterrupt:
exit()
readers = (ct.c_void_p * len(self.perf_buffers))()
for i, v in enumerate(self.perf_buffers.values()):
readers[i] = v
lib.perf_reader_poll(len(readers), readers, timeout)

def kprobe_poll(self, timeout = -1):
"""kprobe_poll(self)
Expand Down

0 comments on commit ee9f0e0

Please sign in to comment.