diff options
author | Peter Wu <peter@lekensteyn.nl> | 2015-10-17 15:51:29 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2015-10-17 15:51:29 +0200 |
commit | f772fabe133cc926529f9585fd25d0294b83d9f8 (patch) | |
tree | 85f9861015e8005fb423edc7fa2fcfb56c4fbd5c | |
parent | ca75547d3c99b9ede62e000f4b788f04242adb8d (diff) | |
download | scripts-f772fabe133cc926529f9585fd25d0294b83d9f8.tar.gz |
sigs.py: print signals for a process
Print pending, blocked, ignored and caught signals for a process.
-rwxr-xr-x | sigs.py | 59 |
1 files changed, 59 insertions, 0 deletions
@@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +""" +Prints the registered signals for a Linux process. +""" +import signal, subprocess, sys + +keyword = sys.argv[1] +try: + pid = int(keyword) +except ValueError: + pids = subprocess.check_output(["pgrep", keyword], + universal_newlines=True).split() + if len(pids) == 1: + pid = int(pids[0]) + else: + if not pids: + print("No such process: %s", keyword) + else: + print("More than one process:", *pids) + sys.exit(1) + +def bits(n): + i = 0 + while n: + if n & 1: + yield i + n >>= 1 + i += 1 + +def print_sigs(sigset): + # Note 32 and 33 are unusable, those are used by nptl(7): + sigs = { + 32: "(NPTL thread cancellation)", + 33: "(NPTL POSIX timers)", + } + for name in dir(signal): + if not name.startswith("SIG") or name.startswith("SIG_"): + continue + signum = int(getattr(signal, name)) + sigs[signum] = name + for bit in bits(sigset): + signum = bit + 1 + if signal.SIGRTMIN < signum <= signal.SIGRTMAX: + signame = "SIGRTMIN+%d" % (signum - signal.SIGRTMIN) + elif signum in sigs: + signame = sigs[signum] + else: + signame = "(signal %d)" % signum + print(" %016x %2d %s" % (1 << bit, signum, signame)) + +with open("/proc/%d/status" % pid) as f: + for line in f: + if not any(line.startswith("Sig%s" % suffix) + for suffix in ("Pnd", "Blk", "Ign", "Cgt")): + continue + key, val = line.split() + sigset = int(val, 16) + print("%s %016x" % (key, sigset)) + print_sigs(sigset) |