#!/usr/bin/env python -u """Hang Analyzer A prototype hang analyzer for MCI integration to help investigate test timeouts 1. Script supports taking dumps, and/or dumping a summary of useful information about a process 2. Script will iterate through a list of interesting processes (mongo, mongod, and mongos), and run the tools from step 1. Supports Linux, MacOS X, Solaris, and Windows. """ import StringIO import csv import itertools import os import platform import signal import subprocess import sys import tempfile import threading import time from distutils import spawn from optparse import OptionParser _is_windows = (sys.platform == "win32") if _is_windows: import win32event import win32api if sys.platform == "win32": import win32process def call(a = []): sys.stdout.write(str(a) + "\n") sys.stdout.flush() ret = subprocess.call(a) if( ret != 0): sys.stderr.write("Bad exit code %d\n" % (ret)) raise Exception() # Copied from python 2.7 version of subprocess.py def check_output(*popenargs, **kwargs): r"""Run command with arguments and return its output as a byte string. If the exit code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and output in the output attribute. The arguments are the same as for the Popen constructor. Example: >>> check_output(["ls", "-l", "/dev/null"]) 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' The stdout argument is not allowed as it is used internally. To capture standard error in the result, use stderr=STDOUT. >>> check_output(["/bin/sh", "-c", ... "ls -l non_existent_file ; exit 0"], ... stderr=STDOUT) 'ls: non_existent_file: No such file or directory\n' """ if 'stdout' in kwargs: raise ValueError('stdout argument not allowed, it will be overridden.') process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs) output, unused_err = process.communicate() retcode = process.poll() if retcode: cmd = kwargs.get("args") if cmd is None: cmd = popenargs[0] raise CalledProcessError(retcode, cmd, output=output) return output def callo(a = []): sys.stdout.write(str(a) + "\n") sys.stdout.flush() return check_output(a) def find_program(prog, paths): """Finds the specified program in env PATH, or tries a set of paths """ loc = spawn.find_executable(prog) if(loc != None): return loc for loc in paths: p = os.path.join(loc, prog) if os.path.exists(p): return p return None class WindowsDumper(object): def __find_debugger(self): """Finds the installed debugger""" # We are looking for c:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64 cdb = spawn.find_executable('cdb.exe') if(cdb != None): return cdb from win32com.shell import shell, shellcon # Cygwin via sshd does not expose the normal environment variables # Use the shell api to get the variable instead rootDir = shell.SHGetFolderPath(0, shellcon.CSIDL_PROGRAM_FILESX86, None, 0) for i in range(0,2): pathToTest = os.path.join(rootDir, "Windows Kits", "8." + str(i), "Debuggers", "x64" ) sys.stdout.write("Checking for debugger in %s\n" % pathToTest) if(os.path.exists(pathToTest)): return os.path.join(pathToTest, "cdb.exe") return None def dump_info(self, pid, process_name, stream): """Dump useful information to the console""" dbg = self.__find_debugger() if dbg is None: stream.write("WARNING: Debugger cdb.exe not found, skipping dumping of %d\n" % (pid)) return stream.write("INFO: Debugger %s, analyzing %d\n" % (dbg, pid)) cmds = [ ".symfix", # Fixup symbol path ".reload", # Reload symbols "!peb", # Dump current exe, & environment variables "lm", # Dump loaded modules "~* k 100", # Dump All Threads ".dump /ma /u dump_" + process_name + ".mdmp", # Dump to file, dump__