#include <windows.h>

#define USAGE "debugger.exe <process path>"
int main(int args, char *argv[])
{
    if(args < 2)
    {
            printf("Usage : %s\n\n",USAGE);
            system("pause");
            exit(0x0);
    }
            
    STARTUPINFO si = {0x0};
    PROCESS_INFORMATION pi = {0x0};
    DEBUG_EVENT DebugEvent;
    
    printf("Starting Process...");
    
    if(!CreateProcess(argv[1], NULL, NULL, NULL, FALSE, DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &si, &pi))
    {
        printf("FAILED : 0x%x\n\n", GetLastError());
        system("pause");
        exit(0x0);
    }

    printf("OK");

    printf("\nDebugging Process (ID : 0x%x)...\n\n", pi.dwProcessId);
    
    while(1)
    {

       WaitForDebugEvent(&DebugEvent, INFINITE);
       
       if(DebugEvent.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
       {
           printf("\n\nProcess exited\n\n");
           CloseHandle(pi.hProcess);
           CloseHandle(pi.hThread);
           break;
       }
       
       ContinueDebugEvent(DebugEvent.dwProcessId, DebugEvent.dwThreadId, DBG_CONTINUE);
   }
   
   system("pause");
   return 0x0;
}
