def GetPerformanceAttributes(object, counter, instance = None, inum=-1, format = None, machine=None): # NOTE: Many counters require 2 samples to give accurate results, # including "% Processor Time" (as by definition, at any instant, a # thread's CPU usage is either 0 or 100). To read counters like this, # you should copy this function, but keep the counter open, and call # CollectQueryData() each time you need to know. # See http://msdn.microsoft.com/library/en-us/dnperfmo/html/perfmonpt2.asp # My older explanation for this was that the "AddCounter" process forced # the CPU to 100%, but the above makes more sense :) import win32pdh if format is None: format = win32pdh.PDH_FMT_LONG path = win32pdh.MakeCounterPath( (machine,object,instance, None, inum,counter) ) hq = win32pdh.OpenQuery() try: hc = win32pdh.AddCounter(hq, path) try: win32pdh.CollectQueryData(hq) type, val = win32pdh.GetFormattedCounterValue(hc, format) return val finally: win32pdh.RemoveCounter(hc) finally: win32pdh.CloseQuery(hq)
def ShowAllProcesses(): object = find_pdh_counter_localized_name("Process") items, instances = win32pdh.EnumObjectItems(None, None, object, win32pdh.PERF_DETAIL_WIZARD) # Need to track multiple instances of the same name. instance_dict = {} for instance in instances: try: instance_dict[instance] = instance_dict[instance] + 1 except KeyError: instance_dict[instance] = 0 # Bit of a hack to get useful info. items = [find_pdh_counter_localized_name("ID Process")] + items[:5] print("Process Name", ",".join(items)) for instance, max_instances in instance_dict.items(): for inum in range(max_instances + 1): hq = win32pdh.OpenQuery() hcs = [] for item in items: path = win32pdh.MakeCounterPath( (None, object, instance, None, inum, item)) hcs.append(win32pdh.AddCounter(hq, path)) win32pdh.CollectQueryData(hq) # as per http://support.microsoft.com/default.aspx?scid=kb;EN-US;q262938, some "%" based # counters need two collections time.sleep(0.01) win32pdh.CollectQueryData(hq) print("%-15s\t" % (instance[:15]), end=" ") for hc in hcs: type, val = win32pdh.GetFormattedCounterValue( hc, win32pdh.PDH_FMT_LONG) print("%5d" % (val), end=" ") win32pdh.RemoveCounter(hc) print() win32pdh.CloseQuery(hq)
def CloseCounter(hq, hc): win32pdh.RemoveCounter(hc) win32pdh.CloseQuery(hq)
def _StartDebugger(self): try: if self.cpu_hq != None: win32pdh.RemoveCounter(self.cpu_counter_handle) win32pdh.CloseQuery(self.cpu_hq) self.cpu_hq = None self.cpu_counter_handle = None except: pass # Clear all our event handlers self.started = Event() self.quit = Event() self.handlingFault = Event() self.handledFault = Event() self.crashInfo = None self.fault = False self.pid = None self.cpu_process = None self.cpu_path = None self.cpu_hq = None self.cpu_counter_handle = None #(fd, self.tempfile) = tempfile.mkstemp() #print self.tempfile self.tempfile = "debugger.TMP" try: os.remove(self.tempfile) except: pass #os.close(fd) #with open(self.tempfile, "rb+") as f: # print "succeed"\ self.tempfilepid = "debugger.TMPPID" #(fd, self.tempfilepid) = tempfile.mkstemp() #os.close(fd) self.thread = Process(group=None, target=WindowsDebugEngineProcess_run, kwargs={ 'Started': self.started, 'HandlingFault': self.handlingFault, 'HandledFault': self.handledFault, 'CommandLine': self.CommandLine, 'Service': self.Service, 'ProcessName': self.ProcessName, 'ProcessID': self.ProcessID, 'KernelConnectionString': self.KernelConnectionString, 'SymbolsPath': self.SymbolsPath, 'IgnoreFirstChanceGardPage': self.IgnoreFirstChanceGardPage, 'IgnoreSecondChanceGardPage': self.IgnoreSecondChanceGardPage, 'Quit': self.quit, 'Tempfile': self.tempfile, 'WinDbg': self.WinDbg, 'TempfilePid': self.tempfilepid, 'FaultOnEarlyExit': self.FaultOnEarlyExit }) # Kick off our thread: self.thread.start() # Wait it...! self.started.wait() if (self.TimeOut == -1): while True: if self.DetectedFault() or self.quit.is_set(): return time.sleep(0.1) if not self.NoCpuKill: # Make sure we wait at least 1 second # for program to startup. Needed with new # CPU killing k0de. time.sleep(self.TimeOut)
def stopMonitor(self): for counter in self.registeredCounters: win32pdh.RemoveCounter(self.registeredCounters[counter][1]) win32pdh.CloseQuery(self.registeredCounters[counter][0]) self.registeredCounters.clear()
def _StartDebugger(self): try: if self.cpu_hq is not None: win32pdh.RemoveCounter(self.cpu_counter_handle) win32pdh.CloseQuery(self.cpu_hq) self.cpu_hq = None self.cpu_counter_handle = None except: pass # Clear all our event handlers self.started = Event() self.quit = Event() self.handlingFault = Event() self.handledFault = Event() self.crashInfo = None self.fault = False self.pid = None self.cpu_process = None self.cpu_path = None self.cpu_hq = None self.cpu_counter_handle = None (fd, self.tempfile) = tempfile.mkstemp() os.close(fd) (fd, self.tempfilepid) = tempfile.mkstemp() os.close(fd) try: os.unlink(self.tempfile) except: pass self.thread = Process(group=None, target=WindowsDebugEngineProcess_run, kwargs={ 'Started': self.started, 'HandlingFault': self.handlingFault, 'HandledFault': self.handledFault, 'CommandLine': self.CommandLine, 'Service': self.Service, 'ProcessName': self.ProcessName, 'ProcessID': self.ProcessID, 'KernelConnectionString': self.KernelConnectionString, 'SymbolsPath': self.SymbolsPath, 'IgnoreFirstChanceGardPage': self.IgnoreFirstChanceGardPage, 'IgnoreSecondChanceGardPage': self.IgnoreSecondChanceGardPage, 'Quit': self.quit, 'Tempfile': self.tempfile, 'WinDbg': self.WinDbg, 'TempfilePid': self.tempfilepid, 'FaultOnEarlyExit': self.FaultOnEarlyExit }) # Kick off our thread: self.thread.start() # Wait it...! self.started.wait() if not self.NoCpuKill: # Make sure we wait at least 1 second # for program to startup. Needed with new # CPU killing k0de. time.sleep(1)
def _StartDebugger(self): try: if self.cpu_hq != None: win32pdh.RemoveCounter(self.cpu_counter_handle) win32pdh.CloseQuery(self.cpu_hq) self.cpu_hq = None self.cpu_counter_handle = None except: pass # Clear all our event handlers self.started = Event() self.quit = Event() self.handlingFault = Event() self.handledFault = Event() self.crashInfo = None self.fault = False self.pid = None self.cpu_process = None self.cpu_path = None self.cpu_hq = None self.cpu_counter_handle = None (fd, self.tempfile) = tempfile.mkstemp() os.close(fd) (fd, self.tempfilepid) = tempfile.mkstemp() os.close(fd) try: os.unlink(self.tempfile) except: pass self.thread = Process(group=None, target=WindowsDebugEngineProcess_run, kwargs={ 'Started': self.started, 'HandlingFault': self.handlingFault, 'HandledFault': self.handledFault, 'CommandLine': self.CommandLine, 'Service': self.Service, 'ProcessName': self.ProcessName, 'ProcessID': self.ProcessID, 'KernelConnectionString': self.KernelConnectionString, 'SymbolsPath': self.SymbolsPath, 'IgnoreFirstChanceGardPage': self.IgnoreFirstChanceGardPage, 'IgnoreSecondChanceGardPage': self.IgnoreSecondChanceGardPage, 'Quit': self.quit, 'Tempfile': self.tempfile, 'TempfilePid': self.tempfilepid }) # Kick off our thread: self.thread.start() # Wait it...! self.started.wait()
win32pdh.RemoveCounter(counter_handle) counter_handle = None except win32pdh.error, e: #print e pass win32pdh.CloseQuery(hq) hq = None except: print sys.exc_info() finally: try: if counter_handle != None: win32pdh.RemoveCounter(counter_handle) counter_handle = None if hq != None: win32pdh.CloseQuery(hq) hq = None except: pass # SHouldn't get here...we hope! return None def OnTestFinished(self): if not self.StartOnCall or not self._IsDebuggerAlive(): return self._StopDebugger()
def close(self): """Remove the counter request from the win32pdh query. """ win32pdh.RemoveCounter(self.hc)
def _StartDebugger(self): try: if self.cpu_hq != None: win32pdh.RemoveCounter(self.cpu_counter_handle) win32pdh.CloseQuery(self.cpu_hq) self.cpu_hq = None self.cpu_counter_handle = None except: pass # Clear all our event handlers self.started = Event() self.quit = Event() self.handlingFault = Event() self.handledFault = Event() self.crashInfo = None self.fault = False self.pid = None self.cpu_process = None self.cpu_path = None self.cpu_hq = None self.cpu_counter_handle = None #(fd, self.tempfile) = tempfile.mkstemp() #print self.tempfile self.tempfile = "debugger.TMP" try: os.remove(self.tempfile) except: pass #os.close(fd) #with open(self.tempfile, "rb+") as f: # print "succeed"\ self.tempfilepid = "debugger.TMPPID" #(fd, self.tempfilepid) = tempfile.mkstemp() #os.close(fd) self.thread = Process(group=None, target=WindowsDebugEngineProcess_run, kwargs={ 'Started': self.started, 'HandlingFault': self.handlingFault, 'HandledFault': self.handledFault, 'CommandLine': self.CommandLine, 'Service': self.Service, 'ProcessName': self.ProcessName, 'ProcessID': self.ProcessID, 'KernelConnectionString': self.KernelConnectionString, 'SymbolsPath': self.SymbolsPath, 'IgnoreFirstChanceGardPage': self.IgnoreFirstChanceGardPage, 'IgnoreSecondChanceGardPage': self.IgnoreSecondChanceGardPage, 'Quit': self.quit, 'Tempfile': self.tempfile, 'WinDbg': self.WinDbg, 'TempfilePid': self.tempfilepid, 'FaultOnEarlyExit': self.FaultOnEarlyExit }) # Kick off our thread: self.thread.start() # Wait it...! self.started.wait() if (self.TimeOut == -1): while True: if self.DetectedFault() or self.quit.is_set(): return time.sleep(0.1) if self.NoCpuKill: # Make sure we wait at least 1 second # for program to startup. Needed with new # CPU killing k0de. #print 11111111111 time.sleep(self.TimeOut) else: #print 111111 count = 10000 while True: #print 2222222 count -= 1 if count <= 1: break try: with open(self.tempfilepid, "rb+") as fd: pid = fd.read() self.pid = int(pid) #print "[*] DEBUG PROCESS ID : ", self.pid try: #print "delete tmpfileid" os.remove(self.tempfilepid) os.unlink(self.tempfilepid) break except: pass break except: pass if self.pid != None: try: #Wprint 111111 # Check and see if the CPU utalization is low cpu = psutil.Process( self.pid).get_cpu_percent(interval=1.0) if cpu != None and cpu < 1.0: cpu = psutil.Process( self.pid).get_cpu_percent(interval=1.0) if cpu != None and cpu < 1.0 and not self.quit.is_set( ): #print "PublisherCall: Stopping debugger, CPU:", cpu self._StopDebugger() return except NoSuchProcess, e: print e pass