examples/tracing: Handle KeyboardInterrupt
Signed-off-by: Gary Lin <glin@suse.com>
diff --git a/examples/tracing/hello_fields.py b/examples/tracing/hello_fields.py
index b8ee6db..9ed6da5 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 aa453ce..73c7f26 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 1f6a7b9..a86ca95 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 4fad777..1d89ce5 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 81385e8..26d9376 100755
--- a/examples/tracing/tcpv4connect.py
+++ b/examples/tracing/tcpv4connect.py
@@ -94,6 +94,8 @@
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 8b57f9a..1c5beef 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 0706092..9291402 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 c1468c8..69bcf8d 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))