#!/system/bin/sh
# TNG eWallet — 启动期 /proc 扫描：定位阻塞在 read 的 TigerTally 线程及其 fd 目标。
# 用法: adb shell su -c 'sh /data/local/tmp/_tng_proc_scan.sh [loop_seconds]'
PKG="my.com.tngdigital.ewallet"
LOOP="${1:-1}"   # 持续扫描秒数（默认 1 秒抓一次快照）

# 选主进程（cmdline 恰为包名，排除 :tools / :goacqowmmt 等子进程）
main_pid=""
for p in $(pidof "$PKG"); do
  if [ "$(tr '\0' ' ' < /proc/$p/cmdline 2>/dev/null | tr -d ' ')" = "$PKG" ]; then
    main_pid="$p"
    break
  fi
done

[ -z "$main_pid" ] && { echo "NO-MAIN-PROC pidof=$(pidof $PKG)"; exit 1; }
echo "=== main pid=$main_pid ==="

i=0
while [ $i -lt "$LOOP" ]; do
  i=$((i+1))
  echo "--- scan #$i ---"
  # 1) 所有可疑线程的状态
  for tid in $(ls /proc/$main_pid/task 2>/dev/null); do
    comm=$(cat /proc/$main_pid/task/$tid/comm 2>/dev/null)
    case "$comm" in
      *pool*|*location*|*tally*|*Tiger*|*tiger*)
        syscall=$(cat /proc/$main_pid/task/$tid/syscall 2>/dev/null)
        wchan=$(cat /proc/$main_pid/task/$tid/wchan 2>/dev/null)
        stat=$(cat /proc/$main_pid/task/$tid/stat 2>/dev/null | awk '{print $3}')
        echo "TID=$tid comm=$comm state=$stat syscall=[$syscall] wchan=$wchan"
        ;;
    esac
  done
  # 2) 所有管道/套接字 fd（TigerTally 握手候选）
  for fd in /proc/$main_pid/fd/*; do
    tgt=$(readlink "$fd" 2>/dev/null)
    case "$tgt" in
      *pipe:*|*socket:*|*anon_inode:*)
        echo "FD=$(basename $fd) -> $tgt"
        ;;
    esac
  done
  [ $i -lt "$LOOP" ] && sleep 1
done
echo "=== done ==="
