def _updateCounterPathsForChildProcesses(self, counter): # Create a counter path for each instance of the child process that # is running. If any of these paths are not in our counter list, # add them to our counter query and append them to the counter list, # so that we'll begin tracking their statistics. We don't need to # worry about removing invalid paths from the list, as # getCounterValue() will generate a value of 0 for those. hq = self.registeredCounters[counter][0] oldCounterListLength = len(self.registeredCounters[counter][1]) pdh.PdhEnumObjectsA(None, None, 0, 1, 0, True) expandedPaths = _getExpandedCounterPaths(self.childProcess, counter) if not expandedPaths: return for expandedPath in expandedPaths: alreadyInCounterList = False for singleCounter in self.registeredCounters[counter][1]: if expandedPath == singleCounter[1]: alreadyInCounterList = True if not alreadyInCounterList: try: newhc = HANDLE() if pdh.PdhAddCounterA(hq, expandedPath, 0, byref(newhc)) != 0: raise TalosError( "Could not add expanded win32 counter %s" % expandedPath) self.registeredCounters[counter][1].append( (newhc, expandedPath)) except: continue if oldCounterListLength != len(self.registeredCounters[counter][1]): pdh.PdhCollectQueryData(hq)
def _addCounter(self, processName, counterType, counterName): pCounterPathElements = _PDH_COUNTER_PATH_ELEMENTS_A( LPSTR(None), LPSTR(counterType), LPSTR(processName), LPSTR(None), DWORD(-1), LPSTR(counterName) ) pcchbufferSize = DWORD(0) # First run we just try to get the buffer size so we can allocate a # string big enough to fill it if pdh.PdhMakeCounterPathA(pointer(pCounterPathElements), LPCSTR(0), pointer(pcchbufferSize), DWORD(0)) != _PDH_MORE_DATA: raise TalosError( "Could not create counter path for counter %s for %s" % (counterName, processName) ) szFullPathBuffer = LPCSTR('\0'*pcchbufferSize.value) # Then we run to get the actual value if pdh.PdhMakeCounterPathA(pointer(pCounterPathElements), szFullPathBuffer, pointer(pcchbufferSize), DWORD(0)) != 0: raise TalosError( "Could not create counter path for counter %s for %s" % (counterName, processName) ) path = szFullPathBuffer.value hq = HANDLE() if pdh.PdhOpenQuery(None, None, byref(hq)) != 0: raise TalosError("Could not open win32 counter query") hc = HANDLE() if pdh.PdhAddCounterA(hq, path, 0, byref(hc)) != 0: raise TalosError("Could not add win32 counter %s" % path) self.registeredCounters[counterName] = [hq, [(hc, path)]]
def find_debugger_info(debug, debugger, debugger_args): debuggerInfo = None if debug or debugger or debugger_args: import mozdebug if not debugger: # No debugger name was provided. Look for the default ones on # current OS. debugger = mozdebug.get_default_debugger_name( mozdebug.DebuggerSearch.KeepLooking) debuggerInfo = None if debugger: debuggerInfo = mozdebug.get_debugger_info(debugger, debugger_args) if debuggerInfo is None: raise TalosError( 'Could not find a suitable debugger in your PATH.') return debuggerInfo
def run_browser(command, minidump_dir, timeout=None, on_started=None, debug=None, debugger=None, debugger_args=None, **kwargs): """ Run the browser using the given `command`. After the browser prints __endTimestamp, we give it 5 seconds to quit and kill it if it's still alive at that point. Note that this method ensure that the process is killed at the end. If this is not possible, an exception will be raised. :param command: the commad (as a string list) to run the browser :param minidump_dir: a path where to extract minidumps in case the browser hang. This have to be the same value used in `mozcrash.check_for_crashes`. :param timeout: if specified, timeout to wait for the browser before we raise a :class:`TalosError` :param on_started: a callback that can be used to do things just after the browser has been started. The callback must takes an argument, which is the psutil.Process instance :param kwargs: additional keyword arguments for the :class:`ProcessHandler` instance Returns a ProcessContext instance, with available output and pid used. """ debugger_info = find_debugger_info(debug, debugger, debugger_args) if debugger_info is not None: return run_in_debug_mode(command, debugger_info, on_started=on_started, env=kwargs.get('env')) context = ProcessContext() first_time = int(time.time()) * 1000 wait_for_quit_timeout = 5 event = Event() reader = Reader(event) LOG.info("Using env: %s" % pprint.pformat(kwargs['env'])) kwargs['storeOutput'] = False kwargs['processOutputLine'] = reader kwargs['onFinish'] = event.set proc = ProcessHandler(command, **kwargs) reader.proc = proc proc.run() LOG.process_start(proc.pid, ' '.join(command)) try: context.process = psutil.Process(proc.pid) if on_started: on_started(context.process) # wait until we saw __endTimestamp in the proc output, # or the browser just terminated - or we have a timeout if not event.wait(timeout): LOG.info("Timeout waiting for test completion; killing browser...") # try to extract the minidump stack if the browser hangs mozcrash.kill_and_get_minidump(proc.pid, minidump_dir) raise TalosError("timeout") if reader.got_end_timestamp: for i in range(1, wait_for_quit_timeout): if proc.wait(1) is not None: break if proc.poll() is None: LOG.info( "Browser shutdown timed out after {0} seconds, terminating" " process.".format(wait_for_quit_timeout)) elif reader.got_timeout: raise TalosError('TIMEOUT: %s' % reader.timeout_message) finally: # this also handle KeyboardInterrupt # ensure early the process is really terminated return_code = None try: return_code = context.kill_process() if return_code is None: return_code = proc.wait(1) except Exception: # Maybe killed by kill_and_get_minidump(), maybe ended? LOG.info("Unable to kill process") LOG.info(traceback.format_exc()) reader.output.append( "__startBeforeLaunchTimestamp%d__endBeforeLaunchTimestamp" % first_time) reader.output.append( "__startAfterTerminationTimestamp%d__endAfterTerminationTimestamp" % (int(time.time()) * 1000)) if return_code is not None: LOG.process_exit(proc.pid, return_code) else: LOG.debug("Unable to detect exit code of the process %s." % proc.pid) context.output = reader.output return context
def run_browser(command, minidump_dir, timeout=None, on_started=None, **kwargs): """ Run the browser using the given `command`. After the browser prints __endTimestamp, we give it 5 seconds to quit and kill it if it's still alive at that point. Note that this method ensure that the process is killed at the end. If this is not possible, an exception will be raised. :param command: the commad (as a string list) to run the browser :param minidump_dir: a path where to extract minidumps in case the browser hang. This have to be the same value used in `mozcrash.check_for_crashes`. :param timeout: if specified, timeout to wait for the browser before we raise a :class:`TalosError` :param on_started: a callback that can be used to do things just after the browser has been started. The callback must takes an argument, which is the psutil.Process instance :param kwargs: additional keyword arguments for the :class:`ProcessHandler` instance Returns a ProcessContext instance, with available output and pid used. """ context = ProcessContext() first_time = int(time.time()) * 1000 wait_for_quit_timeout = 5 event = Event() reader = Reader(event) kwargs['storeOutput'] = False kwargs['processOutputLine'] = reader kwargs['onFinish'] = event.set proc = ProcessHandler(command, **kwargs) proc.run() try: context.process = psutil.Process(proc.pid) if on_started: on_started(context.process) # wait until we saw __endTimestamp in the proc output, # or the browser just terminated - or we have a timeout if not event.wait(timeout): # try to extract the minidump stack if the browser hangs mozcrash.kill_and_get_minidump(proc.pid, minidump_dir) raise TalosError("timeout") if reader.got_end_timestamp: for i in range(1, wait_for_quit_timeout): if proc.wait(1) is not None: break if proc.poll() is None: logging.info( "Browser shutdown timed out after {0} seconds, terminating" " process.".format(wait_for_quit_timeout)) elif reader.got_timeout: raise TalosError('TIMEOUT: %s' % reader.timeout_message) finally: # this also handle KeyboardInterrupt # ensure early the process is really terminated context.kill_process() return_code = proc.wait(1) reader.output.append( "__startBeforeLaunchTimestamp%d__endBeforeLaunchTimestamp" % first_time) reader.output.append( "__startAfterTerminationTimestamp%d__endAfterTerminationTimestamp" % (int(time.time()) * 1000)) logging.info("Browser exited with error code: {0}".format(return_code)) context.output = reader.output return context