From 593339d8db2e2d9dd2eabca2b65638b975a813b5 Mon Sep 17 00:00:00 2001 From: Gary Lin Date: Wed, 27 Feb 2019 16:54:44 +0800 Subject: [PATCH] examples/tracing: Handle KeyboardInterrupt Signed-off-by: Gary Lin --- examples/tracing/hello_fields.py | 2 ++ examples/tracing/mysqld_query.py | 2 ++ examples/tracing/nodejs_http_server.py | 2 ++ examples/tracing/sync_timing.py | 13 ++++++++----- examples/tracing/tcpv4connect.py | 2 ++ examples/tracing/trace_fields.py | 5 ++++- examples/tracing/urandomread-explicit.py | 2 ++ examples/tracing/urandomread.py | 2 ++ 8 files changed, 24 insertions(+), 6 deletions(-) diff --git a/examples/tracing/hello_fields.py b/examples/tracing/hello_fields.py index b8ee6db47b68..9ed6da5dfe44 100755 --- a/examples/tracing/hello_fields.py +++ b/examples/tracing/hello_fields.py @@ -26,4 +26,6 @@ (task, pid, cpu, flags, ts, msg) = b.trace_fields() except ValueError: continue + except KeyboardInterrupt: + exit() printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg)) diff --git a/examples/tracing/mysqld_query.py b/examples/tracing/mysqld_query.py index aa453ce683c6..73c7f26f04b5 100755 --- a/examples/tracing/mysqld_query.py +++ b/examples/tracing/mysqld_query.py @@ -59,4 +59,6 @@ except ValueError: print("value error") continue + except KeyboardInterrupt: + exit() printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg)) diff --git a/examples/tracing/nodejs_http_server.py b/examples/tracing/nodejs_http_server.py index 1f6a7b90661d..a86ca956cfa9 100755 --- a/examples/tracing/nodejs_http_server.py +++ b/examples/tracing/nodejs_http_server.py @@ -52,4 +52,6 @@ except ValueError: print("value error") continue + except KeyboardInterrupt: + exit() printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg)) diff --git a/examples/tracing/sync_timing.py b/examples/tracing/sync_timing.py index 4fad777c46f0..1d89ce554d6a 100755 --- a/examples/tracing/sync_timing.py +++ b/examples/tracing/sync_timing.py @@ -45,8 +45,11 @@ # format output start = 0 while 1: - (task, pid, cpu, flags, ts, ms) = b.trace_fields() - if start == 0: - start = ts - ts = ts - start - printb(b"At time %.2f s: multiple syncs detected, last %s ms ago" % (ts, ms)) + try: + (task, pid, cpu, flags, ts, ms) = b.trace_fields() + if start == 0: + start = ts + ts = ts - start + printb(b"At time %.2f s: multiple syncs detected, last %s ms ago" % (ts, ms)) + except KeyboardInterrupt: + exit() diff --git a/examples/tracing/tcpv4connect.py b/examples/tracing/tcpv4connect.py index 81385e8f851e..26d937636ee3 100755 --- a/examples/tracing/tcpv4connect.py +++ b/examples/tracing/tcpv4connect.py @@ -94,6 +94,8 @@ def inet_ntoa(addr): except ValueError: # Ignore messages from other tracers continue + except KeyboardInterrupt: + exit() # Ignore messages from other tracers if _tag != "trace_tcp4connect": diff --git a/examples/tracing/trace_fields.py b/examples/tracing/trace_fields.py index 8b57f9a216e7..1c5beef2143f 100755 --- a/examples/tracing/trace_fields.py +++ b/examples/tracing/trace_fields.py @@ -18,4 +18,7 @@ b = BPF(text=prog) b.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="hello") print("PID MESSAGE") -b.trace_print(fmt="{1} {5}") +try: + b.trace_print(fmt="{1} {5}") +except KeyboardInterrupt: + exit() diff --git a/examples/tracing/urandomread-explicit.py b/examples/tracing/urandomread-explicit.py index 0706092a7aa3..9291402ba948 100755 --- a/examples/tracing/urandomread-explicit.py +++ b/examples/tracing/urandomread-explicit.py @@ -50,4 +50,6 @@ (task, pid, cpu, flags, ts, msg) = b.trace_fields() except ValueError: continue + except KeyboardInterrupt: + exit() printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg)) diff --git a/examples/tracing/urandomread.py b/examples/tracing/urandomread.py index c1468c8cdd77..69bcf8d11150 100755 --- a/examples/tracing/urandomread.py +++ b/examples/tracing/urandomread.py @@ -33,4 +33,6 @@ (task, pid, cpu, flags, ts, msg) = b.trace_fields() except ValueError: continue + except KeyboardInterrupt: + exit() printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))