#!/usr/bin/env python3 """可靠进程存活监控: 以 adb pidof + /proc/ 为唯一事实来源. 不用 frida enumerate_processes 判断存活 (有延迟/漏报, 且 spawn 后枚举不稳定)。 用法: python3 watch_proc.py [label] -- 对当前 com.duowan.kiwi 进程做轮询 输出: 每 2s 一行 pid/状态; 结束时报告存活与否与死亡精确时长。 """ import subprocess, sys, time ADB = ["adb", "-s", "127.0.0.1:5555"] PACKAGE = "com.duowan.kiwi" def adb(*a): return subprocess.run(ADB + list(a), capture_output=True, text=True) def pidof(): r = adb("shell", "pidof", PACKAGE) out = r.stdout.strip() return [int(x) for x in out.split()] if out else [] def proc_ok(pid): r = adb("shell", "ls", f"/proc/{pid}/status") return r.returncode == 0 def main(): seconds = int(sys.argv[1]) if len(sys.argv) > 1 else 60 label = sys.argv[2] if len(sys.argv) > 2 else "" print(f"[watch:{label}] {seconds}s, 起点 pids={pidof()}", flush=True) t0 = time.time() seen = {} last_alive = t0 last_dead = None while time.time() - t0 < seconds: pids = pidof() if pids: for p in pids: seen.setdefault(p, time.time()) last_alive = time.time() last_dead = None print(f" +{time.time()-t0:5.1f}s ALIVE pids={pids}", flush=True) else: if last_dead is None: last_dead = time.time() print(f" +{time.time()-t0:5.1f}s DEAD (was alive={last_alive-t0:.1f}s)", flush=True) time.sleep(2) dur = time.time() - t0 final = pidof() print(f"[watch:{label}] 结束: alive={bool(final)} final_pids={final} 存活时长={last_alive-t0:.1f}s 死亡时刻={last_dead-t0 if last_dead else '未死亡'} 总时长={dur:.1f}s", flush=True) if __name__ == "__main__": main()