# scripts/launch_debug_session.ps1 # # Wrapper around pcsx-redux.exe + gdb-multiarch that auto-launches the # gdb_tape_atoms.gdb helper and (optionally) hot-reloads the .ps-exe. # # Generated by track gdb_tape_atom_debugging_20260711 — see # C:\projects\Pikuma\ps1-ai\docs\gdb_tape_atom_debugging.md for the manual. # # usage: # .\launch_debug_session.ps1 # bare gdb attach (assumes pcsx-redux is up) # .\launch_debug_session.ps1 -AutoLoadAtoms # also source gdb_tape_atoms.gdb on startup # .\launch_debug_session.ps1 -AutoResetEmu # hot-reload the .ps-exe before attaching # .\launch_debug_session.ps1 -AutoStartEmu # launch pcsx-redux.exe if not running # # All three flags can be combined: -AutoStartEmu -AutoResetEmu -AutoLoadAtoms [CmdletBinding()] param( [switch]$AutoLoadAtoms, [switch]$AutoResetEmu, [switch]$AutoStartEmu, [string]$ExePath = (Join-Path $PSScriptRoot '..\build\hello_gte.ps-exe'), [string]$ElfPath = (Join-Path $PSScriptRoot '..\build\hello_gte.elf') ) $ErrorActionPreference = 'Stop' # ── Pre-checks (E10: pcsx-redux on :3333, E11: gdb-multiarch in PATH) ── function Test-Prerequisites { # E11: gdb-multiarch must be in PATH. if (-not (Get-Command gdb-multiarch -ErrorAction SilentlyContinue)) { Write-Error "gdb-multiarch not found in PATH. Install via scoop or .\update_deps.ps1." -ErrorAction Stop exit 1 } # E10: pcsx-redux must be reachable on :3333 (or we auto-start). $tcp = New-Object System.Net.Sockets.TcpClient try { $async = $tcp.BeginConnect('localhost', 3333, $null, $null) $connected = $async.AsyncWaitHandle.WaitOne(2000, $false) -and -not $async.IsFaulted } catch { $connected = $false } $tcp.Close() if (-not $connected) { if ($AutoStartEmu) { Write-Host "pcsx-redux not running; auto-starting..." -ForegroundColor Cyan $pcsxExe = (Join-Path $PSScriptRoot '..\toolchain\pcsx-redux\vsprojects\x64\Release\pcsx-redux.exe') if (-not (Test-Path $pcsxExe)) { Write-Error "pcsx-redux.exe not found at $pcsxExe. Build it first, or pass -ExePath + start pcsx-redux manually." -ErrorAction Stop exit 1 } Start-Process -FilePath $pcsxExe -PassThru | Out-Null Start-Sleep -Seconds 3 } else { Write-Error "Cannot connect to pcsx-redux at localhost:3333. Start pcsx-redux first (or pass -AutoStartEmu)." -ErrorAction Stop exit 1 } } } # ── Optional: hot-reload .ps-exe via pcsx-redux's web server (port 8080) ── function Invoke-HotReload { $absolutePath = [System.IO.Path]::GetFullPath($ExePath) $uri = 'http://localhost:8080/api/v1/load-exec' $body = @{ filename = $absolutePath } | ConvertTo-Json try { Invoke-RestMethod -Uri $uri -Method Post -Body $body -ContentType 'application/json' | Out-Null Write-Host "Hot-reloaded $absolutePath into pcsx-redux." -ForegroundColor Green } catch { Write-Warning "Could not hot-reload via pcsx-redux web server (port 8080): $_" } } # ── Main ── Test-Prerequisites if ($AutoResetEmu) { Invoke-HotReload } $gdbScript = Join-Path $PSScriptRoot 'gdb\gdb_tape_atoms.gdb' $gdbArgs = @( '-q' '-ex', "file $ElfPath" '-ex', 'target remote localhost:3333' ) if ($AutoLoadAtoms) { $gdbArgs += @('-ex', "source $gdbScript") } Write-Host "Launching gdb-multiarch (script: $gdbScript, elf: $ElfPath)..." -ForegroundColor Cyan & gdb-multiarch @gdbArgs