# Path: Peach/agent.py
# class Monitor(object):
#     """
#     Extend from this to implement a Monitor.
#     Monitors are run by an Agent and must operate in an async manner.
#     Any blocking tasks must be performed in another thread.
# 
#     Agent: onTestStarting()
#     Agent: onTestFinished()
#     Agent: redoTest()
#     Agent: Sending redoTest result [False]
#     Agent: detectFault()
#     Agent: Detected fault!
#     Agent: Sending detectFault result [True]
#     Agent: getMonitorData()
#     Agent: onFault()
#     Agent: stopRun()
#     Agent: onShutdown()
#     """
# 
#     def __init__(self, args):
#         """
#         Arguments are supplied via the Peach XML file.
# 
#         @type	args: Dictionary
#         @param	args: Dictionary of parameters
#         """
#         self._name = None
# 
#     def OnTestStarting(self):
#         """Called right before start of test case or variation."""
#         pass
# 
#     def OnTestFinished(self):
#         """Called right after a test case or variation."""
#         pass
# 
#     def GetMonitorData(self):
#         """Get any monitored data from a test case."""
#         return None
# 
#     def RedoTest(self):
#         """Should the current test be re-performed."""
#         return False
# 
#     def DetectedFault(self):
#         """Check if a fault was detected."""
#         return False
# 
#     def OnFault(self):
#         """Called when a fault was detected."""
#         pass
# 
#     def OnShutdown(self):
#         """Called when Agent is shutting down, typically at end of a test run or when a Stop-Run occurs."""
#         pass
# 
#     def StopRun(self):
#         """Return True to force test run to fail. This should return True if an unrecoverable error occurs."""
#         return False
# 
#     def PublisherCall(self, method):
#         """Called when a call action is being performed.
#         Call actions are used to launch programs, this gives the monitor a chance to determine if it should be
#         running the program under a debugger instead. Note: This is a bit of a hack to get this working.
#         """
#         pass


import os
import gc
import re
import sys
import time
import struct
import signal
import pickle
import tempfile
import psutil
    import comtypes
    import PyDbgEng
    import win32serviceutil
    import win32service
    import win32api, win32con, win32process, win32pdh
    import vtrace, envi
    import threading
from Peach.agent import Monitor
    from ctypes import *
    from comtypes import HRESULT, COMError
    from comtypes.client import CreateObject, GetEvents, PumpEvents
    from comtypes.hresult import S_OK, E_FAIL, E_UNEXPECTED, E_INVALIDARG
    from comtypes.automation import IID
    from comtypes.gen import DbgEng
    from multiprocessing import *
        procname = procname.lower()

        try:
            ourPid = win32api.GetCurrentProcessId()

        except:
            pass

        pids = win32process.EnumProcesses()
        for pid in pids:
            if ourPid == pid:
                continue

            try:
                hPid = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, 0, pid)

                try:
                    mids = win32process.EnumProcessModules(hPid)
                    for mid in mids:
                        name = str(win32process.GetModuleFileNameEx(hPid, mid))
                        if name.lower().find(procname) != -1:
                            return pid

                finally:
                    win32api.CloseHandle(hPid)
            except:
                pass

        return None


# Path: Peach/agent.py
# class Monitor(object):
#     """
#     Extend from this to implement a Monitor.
#     Monitors are run by an Agent and must operate in an async manner.
#     Any blocking tasks must be performed in another thread.
# 
#     Agent: onTestStarting()
#     Agent: onTestFinished()
#     Agent: redoTest()
#     Agent: Sending redoTest result [False]
#     Agent: detectFault()
#     Agent: Detected fault!
#     Agent: Sending detectFault result [True]
#     Agent: getMonitorData()
#     Agent: onFault()
#     Agent: stopRun()
#     Agent: onShutdown()
#     """
# 
#     def __init__(self, args):
#         """
#         Arguments are supplied via the Peach XML file.
# 
#         @type	args: Dictionary
#         @param	args: Dictionary of parameters
#         """
#         self._name = None
# 
#     def OnTestStarting(self):
#         """Called right before start of test case or variation."""
#         pass
# 
#     def OnTestFinished(self):
#         """Called right after a test case or variation."""
#         pass
# 
#     def GetMonitorData(self):
#         """Get any monitored data from a test case."""
#         return None
# 
#     def RedoTest(self):
#         """Should the current test be re-performed."""
#         return False
# 
#     def DetectedFault(self):
#         """Check if a fault was detected."""
#         return False
# 
#     def OnFault(self):
#         """Called when a fault was detected."""
#         pass
# 
#     def OnShutdown(self):
#         """Called when Agent is shutting down, typically at end of a test run or when a Stop-Run occurs."""
#         pass
# 
#     def StopRun(self):
#         """Return True to force test run to fail. This should return True if an unrecoverable error occurs."""
#         return False
# 
#     def PublisherCall(self, method):
#         """Called when a call action is being performed.
#         Call actions are used to launch programs, this gives the monitor a chance to determine if it should be
#         running the program under a debugger instead. Note: This is a bit of a hack to get this working.
#         """
#         pass


# Path: Peach/Agent/debugger.py
import os
import gc
import re
import sys
import time
import struct
import signal
import pickle
import tempfile
import psutil
    import comtypes
    import PyDbgEng
    import win32serviceutil
    import win32service
    import win32api, win32con, win32process, win32pdh
    import vtrace, envi
    import threading
from Peach.agent import Monitor
    from ctypes import *
    from comtypes import HRESULT, COMError
    from comtypes.client import CreateObject, GetEvents, PumpEvents
    from comtypes.hresult import S_OK, E_FAIL, E_UNEXPECTED, E_INVALIDARG
    from comtypes.automation import IID
    from comtypes.gen import DbgEng
    from multiprocessing import *

        procname = procname.lower()

        try:
            ourPid = win32api.GetCurrentProcessId()

        except:
            pass

        pids = win32process.EnumProcesses()
        for pid in pids:
            if ourPid == pid:
                continue

            try:
                hPid = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, 0, pid)

                try:
                    mids = win32process.EnumProcessModules(hPid)
                    for mid in mids:
                        name = str(win32process.GetModuleFileNameEx(hPid, mid))
                        if name.lower().find(procname) != -1:
                            return pid

                finally:
                    win32api.CloseHandle(hPid)
            except:
                pass

        return None


    class WindowsDebugEngine(Monitor):