# watch_login.gdb -- HARDWARE watchpoint on OriginMgr.m_isLoggedIn.
# Software int3 breakpoints looked unreliable under Wine wow64 (i386:x64-32 arch
# warning, zero hits even though FIFA drains the socket). Debug-register
# watchpoints work at the CPU level and catch ANY write to the byte, revealing
# the real setter + backtrace regardless of which code path does it.
#
#   Q: does m_isLoggedIn ([OriginMgr+0x13]) EVER get written? by what instruction?
#
# Also arms HARDWARE exec breakpoints (hbreak) on the known setter/matcher so we
# can tell "bp mechanism broken" apart from "code never runs".
set pagination off
set confirm off
set width 0
attach PIDHERE
set architecture i386:x86-64

# CRITICAL for Wine: it drives thread scheduling with SIGUSR1/USR2 and realtime
# signals. gdb halts on them by default, which (in -batch) ends the script and
# DETACHES within seconds -- the reason earlier traces saw zero hits. Pass them
# through silently so the game keeps running and our bps/watchpoints survive.
handle SIGUSR1 nostop noprint pass
handle SIGUSR2 nostop noprint pass
handle SIGPIPE nostop noprint pass
handle SIG32 nostop noprint pass
handle SIG33 nostop noprint pass
handle SIG34 nostop noprint pass
handle SIG35 nostop noprint pass

# resolve the live OriginMgr and the flag byte address
set $om = *(unsigned long*)0x1448acf50
printf "OriginMgr = %#lx   m_isLoggedIn byte @ %#lx = %d\n", $om, $om+0x13, *(unsigned char*)($om+0x13)

# --- the decisive probe: catch ANY write to the flag byte ---
watch *(unsigned char*)($om+0x13)
commands
  printf "\n*** m_isLoggedIn WRITE: %d -> %d  at rip=%#lx ***\n", $arg0, *(unsigned char*)($om+0x13), $rip
  printf "backtrace:\n"
  bt 6
  continue
end

# --- validation / cross-check: hardware exec bps on the theorised machinery ---
hbreak *0x146f1e0ab
commands
  silent
  printf ">> [hbreak] setter 0x146f1e0ab reached (mov [rcx+0x13],1)  rcx=%#lx\n", $rcx
  continue
end
hbreak *0x147102880
commands
  silent
  printf ">> [hbreak] sender matcher 0x147102880 reached\n"
  continue
end

printf "\n=== watch_login armed (HW watchpoint + hbreak). Drive the game: dismiss popup, ONLINE tab, reconnect. ===\n"
continue
