Skip to content

Instantly share code, notes, and snippets.

@markusrt
Created January 11, 2012 11:58
Show Gist options
  • Select an option

  • Save markusrt/1594355 to your computer and use it in GitHub Desktop.

Select an option

Save markusrt/1594355 to your computer and use it in GitHub Desktop.

Revisions

  1. markusrt revised this gist Jan 11, 2012. No changes.
  2. markusrt created this gist Jan 11, 2012.
    77 changes: 77 additions & 0 deletions debug.bat
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    @echo off
    setlocal

    REM Config section, change if required
    set REG=C:\Windows\System32\reg.exe
    set DEBUGGER=vsjitdebugger.exe
    REM Config section end

    set method=enable
    set debugExe=
    set args=
    set verbose=0
    :optStart
    set ARG=%~1
    if "%~1" == "" goto :optEnd
    if /i "%1" == "/enable" (
    set method=enable
    goto :nextOpt
    )
    if /i "%1" == "/disable" (
    set method=disable
    goto :nextOpt
    )
    if /i "%ARG:~0,5%" == "/exe:" (
    set debugExe=%ARG:~5%
    goto :nextOpt
    )
    if /i "%1" == "/?" (
    goto :help
    )
    if /i "%1" == "/v" (
    set verbose=1
    goto :nextOpt
    )
    if /i "%ARG:~,1%" == "/" (
    echo %~n0: Unknown option %ARG% 1>&2
    exit /b 1
    )
    set args=%args% "%~1"
    :nextOpt
    shift /1
    goto :optStart
    :optEnd

    if "%debugExe%" == "" goto :help

    if "" == "%args%" set args=.

    call :%method% %args% && ( exit /b 0 ) || ( exit /b 1 )
    exit /b 0


    :enable
    if "1" == "%verbose%" echo on
    %REG% ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\%debugExe%" /v debugger /t REG_SZ /d %DEBUGGER% /f && exit /b 0
    @echo off
    exit /b 1

    :disable
    if "1" == "%verbose%" echo on
    %REG% DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\%debugExe%" /f && exit /b 0
    @echo off
    exit /b 1

    :help
    echo Enables debugging for a specified executable. This causes a debugger [%DEBUGGER%] to be launched each time the program starts.
    echo.
    echo USAGE: %~n0 /enable /exe:executable [/v]
    echo %~n0 /diable /exe:executable [/v]
    echo.
    echo /enable Enables debugging for the specified executable
    echo /disable Disables debugging for the specified executable
    echo /v "Verbose" output
    echo.
    echo EXAMPLE: %~n0 /enable /exe:cmd.exe
    echo.
    exit /b 1