#!/usr/bin/env python import re import os import sys MY_PID = str(os.getpid()) PATTERN_LITERAL = '|'.join('(' + arg + ')' for arg in sys.argv[1:]) PATTERN = re.compile(PATTERN_LITERAL) TID_TO_COMM = {} STACK_TO_TID_LIST = {} def get_comm(tid): comm_path = os.path.join('/proc', tid, 'comm') try: with open(comm_path) as comm_file: return comm_file.readline().strip() except: return None def get_stack(tid): stack_path = os.path.join('/proc', tid, 'stack') try: with open(stack_path) as stack_file: return stack_file.read() except: return None for pid in os.listdir('/proc'): if not pid.isdigit(): continue if pid == MY_PID: continue comm = get_comm(pid) if not comm: continue if not PATTERN.search(comm): continue task_path = os.path.join('/proc', pid, 'task') for tid in os.listdir(task_path): TID_TO_COMM[tid] = comm stack = get_stack(tid) if not stack: continue STACK_TO_TID_LIST.setdefault(stack, []).append(tid) for stack, tid_list in STACK_TO_TID_LIST.items(): tid_list.sort(key=lambda tid: int(tid)) for tid in tid_list: print tid, TID_TO_COMM[tid] print stack print