1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
#!/usr/bin/env python
"""Hang Analyzer
A prototype hang analyzer for Evergreen 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,
and run the tools from step 1. The list of processes can be provided as an option.
3. Java processes will be dumped using jstack, if available.
Supports Linux, MacOS X, Solaris, and Windows.
"""
import StringIO
import csv
import glob
import itertools
import os
import platform
import re
import signal
import subprocess
import sys
import tempfile
import threading
import time
from distutils import spawn
from optparse import OptionParser
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, take_dump = False):
"""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
".symopt +0x10", # Enable line loading (off by default in CDB, on by default in WinDBG)
".reload", # Reload symbols
"!peb", # Dump current exe, & environment variables
"lm", # Dump loaded modules
"~* kp 100", # Dump All Threads with function arguments
".dump /ma /u dump_" + process_name + "." + str(pid) + "." + self.get_dump_ext() if take_dump else "",
# Dump to file, dump_<process name>_<time stamp>_<pid in hex>.<pid>.mdmp
".detach", # Detach
"q" # Quit
]
call([dbg, '-c', ";".join(cmds), '-p', str(pid)])
stream.write("INFO: Done analyzing process\n")
def get_dump_ext(self):
return "mdmp"
def dump_core(self, pid, output_file):
"""Take a dump of pid to specified file"""
dbg = self.__find_debugger()
if dbg is None:
sys.stdout.write("WARNING: Debugger cdb.exe not found, skipping dumping of %d to %s\n" % (pid, output_file))
return
sys.stdout.write("INFO: Debugger %s, analyzing %d to %s\n" % (dbg, pid, output_file))
call([dbg, '-c', ".dump /ma %s;.detach;q" % output_file, '-p', str(pid)] )
sys.stdout.write("INFO: Done analyzing process\n")
class WindowsProcessList(object):
def __find_ps(self):
"""Finds tasklist """
return os.path.join(os.environ["WINDIR"], "system32", "tasklist.exe")
def dump_processes(self):
"""Get list of [Pid, Process Name]"""
ps = self.__find_ps()
sys.stdout.write("INFO: Getting list of processes using %s\n" % ps)
ret = callo([ps, "/FO", "CSV"])
b = StringIO.StringIO(ret)
csvReader = csv.reader(b)
p = [[int(row[1]), row[0]] for row in csvReader if row[1] != "PID"]
sys.stdout.write("INFO: Done analyzing process\n")
return p
# LLDB dumper is for MacOS X
class LLDBDumper(object):
def __find_debugger(self):
"""Finds the installed debugger"""
return find_program('lldb', ['/usr/bin'])
def dump_info(self, pid, process_name, stream, take_dump = False):
dbg = self.__find_debugger()
if dbg is None:
stream.write("WARNING: Debugger lldb not found, skipping dumping of %d\n" % (pid))
return
stream.write("INFO: Debugger %s, analyzing %d\n" % (dbg, pid))
lldb_version = callo([dbg, "--version"])
stream.write(lldb_version)
# Do we have the XCode or LLVM version of lldb?
# Old versions of lldb do not work well when taking commands via a file
# XCode (7.2): lldb-340.4.119
# LLVM - lldb version 3.7.0 ( revision )
if 'version' not in lldb_version:
# We have XCode's lldb
lldb_version = lldb_version[lldb_version.index("lldb-"):]
lldb_version = lldb_version.replace('lldb-', '')
lldb_major_version = int(lldb_version[:lldb_version.index('.')])
if lldb_major_version < 340:
stream.write("WARNING: Debugger lldb is too old, please upgrade to XCode 7.2\n")
return
cmds = [
"attach -p %d" % pid,
"target modules list",
"thread backtrace all",
"process save-core dump_" + process_name + "." + str(pid) + "." + self.get_dump_ext() if take_dump else "",
"settings set interpreter.prompt-on-quit false",
"quit",
]
tf = tempfile.NamedTemporaryFile()
for c in cmds:
tf.write(c + "\n")
tf.flush()
# Works on in MacOS 10.9 & later
#call([dbg] + list( itertools.chain.from_iterable([['-o', b] for b in cmds])))
call(['cat', tf.name])
call([dbg, '--source', tf.name])
stream.write("INFO: Done analyzing process\n")
def get_dump_ext(self):
return "core"
def dump_core(self, pid, output_file):
"""Take a dump of pid to specified file"""
sys.stderr.write("ERROR: lldb does not support dumps, stupid debugger\n")
class DarwinProcessList(object):
def __find_ps(self):
"""Finds ps"""
return find_program('ps', ['/bin'])
def dump_processes(self):
"""Get list of [Pid, Process Name]"""
ps = self.__find_ps()
sys.stdout.write("INFO: Getting list of processes using %s\n" % ps)
ret = callo([ps, "-axco", "pid,comm"])
b = StringIO.StringIO(ret)
csvReader = csv.reader(b, delimiter=' ', quoting=csv.QUOTE_NONE, skipinitialspace=True)
p = [[int(row[0]), row[1]] for row in csvReader if row[0] != "PID"]
sys.stdout.write("INFO: Done analyzing process\n")
return p
# GDB dumper is for Linux & Solaris
class GDBDumper(object):
def __find_debugger(self):
"""Finds the installed debugger"""
return find_program('gdb', ['/opt/mongodbtoolchain/gdb/bin', '/usr/bin'])
def dump_info(self, pid, process_name, stream, take_dump = False):
dbg = self.__find_debugger()
if dbg is None:
stream.write("WARNING: Debugger gdb not found, skipping dumping of %d\n" % (pid))
return
stream.write("INFO: Debugger %s, analyzing %d\n" % (dbg, pid))
call([dbg, "--version"])
script_dir = os.path.dirname(os.path.abspath(__file__))
print("dir %s" % script_dir);
gdb_dir = os.path.join(script_dir, "gdb")
printers_script = os.path.join(gdb_dir, "mongo.py")
cmds = [
"set pagination off",
"attach %d" % pid,
"info sharedlibrary",
"info threads", # Dump a simple list of commands to get the thread name
"set python print-stack full",
"source " + printers_script,
"thread apply all bt",
"gcore dump_" + process_name + "." + str(pid) + "." + self.get_dump_ext() if take_dump else "",
"mongodb-analyze",
"set confirm off",
"quit",
]
call([dbg, "--quiet", "--nx"] + list( itertools.chain.from_iterable([['-ex', b] for b in cmds])))
stream.write("INFO: Done analyzing process\n")
def get_dump_ext(self):
return "core"
def _find_gcore(self):
"""Finds the installed gcore"""
dbg = "/usr/bin/gcore"
if os.path.exists(dbg):
return dbg
return None
def dump_core(self, pid, output_file):
"""Take a dump of pid to specified file"""
dbg = self._find_gcore()
if dbg is None:
sys.stdout.write("WARNING: Debugger gcore not found, skipping dumping of %d to %s\n" % (pid, output_file))
return
sys.stdout.write("INFO: Debugger %s, analyzing %d to %s\n" % (dbg, pid, output_file))
call([dbg, "-o", output_file, str(pid)])
sys.stdout.write("INFO: Done analyzing process\n")
# GCore appends the pid to the output file name
return output_file + "." + str(pid)
class LinuxProcessList(object):
def __find_ps(self):
"""Finds ps"""
return find_program('ps', ['/bin', '/usr/bin'])
def dump_processes(self):
"""Get list of [Pid, Process Name]"""
ps = self.__find_ps()
sys.stdout.write("INFO: Getting list of processes using %s\n" % ps)
call([ps, "--version"])
ret = callo([ps, "-eo", "pid,args"])
b = StringIO.StringIO(ret)
csvReader = csv.reader(b, delimiter=' ', quoting=csv.QUOTE_NONE, skipinitialspace=True)
p = [[int(row[0]), os.path.split(row[1])[1]] for row in csvReader if row[0] != "PID"]
sys.stdout.write("INFO: Done analyzing process\n")
return p
class SolarisProcessList(object):
def __find_ps(self):
"""Finds ps"""
return find_program('ps', ['/bin', '/usr/bin'])
def dump_processes(self):
"""Get list of [Pid, Process Name]"""
ps = self.__find_ps()
sys.stdout.write("INFO: Getting list of processes using %s\n" % ps)
ret = callo([ps, "-eo", "pid,args"])
b = StringIO.StringIO(ret)
csvReader = csv.reader(b, delimiter=' ', quoting=csv.QUOTE_NONE, skipinitialspace=True)
p = [[int(row[0]), os.path.split(row[1])[1]] for row in csvReader if row[0] != "PID"]
sys.stdout.write("INFO: Done analyzing process\n")
return p
# jstack is a JDK utility
class JstackDumper(object):
def __find_debugger(self):
"""Finds the installed jstack debugger"""
return find_program('jstack', ['/usr/bin'])
def dump_info(self, pid, process_name, stream, take_dump = False):
"""Dump java thread stack traces to the console"""
jstack = self.__find_debugger()
if jstack is None:
stream.write("WARNING: Debugger jstack not found, skipping dumping of %d\n" % (pid))
return
stream.write("INFO: Debugger %s, analyzing %d\n" % (jstack, pid))
call([jstack, "-l", str(pid)])
stream.write("INFO: Done analyzing process\n")
def dump_core(self, pid, output_file):
"""Take a dump of pid to specified file"""
sys.stderr.write("ERROR: jstack does not support dumps\n")
# jstack is a JDK utility
class JstackWindowsDumper(object):
def dump_info(self, pid, process_name, stream):
"""Dump java thread stack traces to the console"""
stream.write("WARNING: Debugger jstack not supported, skipping dumping of %d\n" % (pid))
def get_hang_analyzers():
dbg = None
jstack = None
ps = None
if sys.platform.startswith("linux"):
dbg = GDBDumper()
jstack = JstackDumper()
ps = LinuxProcessList()
elif sys.platform.startswith("sunos"):
dbg = GDBDumper()
jstack = JstackDumper()
ps = SolarisProcessList()
elif os.name == 'nt' or (os.name == "posix" and sys.platform == "cygwin"):
dbg = WindowsDumper()
jstack = JstackWindowsDumper()
ps = WindowsProcessList()
elif sys.platform == "darwin":
dbg = LLDBDumper()
jstack = JstackDumper()
ps = DarwinProcessList()
return [ps, dbg, jstack]
def check_dump_quota(quota, ext):
"""Check if sum of the files with ext is within the specified quota in megabytes"""
files = glob.glob("*." + ext)
size_sum = 0
for file_name in files:
size_sum += os.path.getsize(file_name)
return (size_sum <= quota)
def signal_process(pid, signalnum):
"""Signal process with signal, N/A on Windows"""
try:
os.kill(pid, signalnum)
print "Waiting for process to report"
time.sleep(5)
except OSError,e:
print "Hit OS error trying to signal process: " + str(e)
except AttributeError:
print "Cannot send signal to a process on Windows"
def timeout_protector():
print "Script timeout has been hit, terminating"
if sys.platform == "win32":
# Have the process exit with code 9 when it terminates itself to closely match the exit code
# of the process when it sends itself a SIGKILL.
handle = win32process.GetCurrentProcess()
win32process.TerminateProcess(handle, 9)
else:
os.kill(os.getpid(), signal.SIGKILL)
# Basic procedure
#
# 1. Get a list of interesting processes
# 2. Dump useful information or take dumps
def main():
print "Python Version: " + sys.version
print "OS: " + platform.platform()
try:
distro = platform.linux_distribution()
print "Linux Distribution: " + str(distro)
except AttributeError:
print "Cannot determine Linux distro since Python is too old"
try:
uid = os.getuid()
print "Current User: " + str(uid)
current_login = os.getlogin()
print "Current Login: " + current_login
except OSError:
print "Cannot determine Unix Current Login"
except AttributeError:
print "Cannot determine Unix Current Login, not supported on Windows"
interesting_processes = ["mongo", "mongod", "mongos", "_test", "dbtest", "python", "java"]
go_processes = []
parser = OptionParser(description=__doc__)
parser.add_option('-p', '--process_names', dest='process_names', help='List of process names to analyze')
parser.add_option('-g', '--go_process_names', dest='go_process_names', help='List of go process names to analyze')
parser.add_option('-s', '--max_core_dumps_size', dest='max_core_dumps_size', default=10000, help='Maximum total size of core dumps to keep in megabytes')
(options, args) = parser.parse_args()
if options.process_names is not None:
interesting_processes = options.process_names.split(',')
if options.go_process_names is not None:
go_processes = options.go_process_names.split(',')
interesting_processes += go_processes
[ps, dbg, jstack] = get_hang_analyzers()
if( ps == None or (dbg == None and jstack == None)):
sys.stderr.write("hang_analyzer.py: Unsupported platform: %s\n" % (sys.platform))
exit(1)
# Make sure the script does not hang
timer = threading.Timer(120, timeout_protector)
timer.start()
processes_orig = ps.dump_processes()
# Find all running interesting processes by doing a substring match.
processes = [a for a in processes_orig
if any([a[1].find(ip) >= 0 for ip in interesting_processes]) and a[0] != os.getpid()]
sys.stdout.write("Found %d interesting processes\n" % len(processes))
max_dump_size_bytes = int(options.max_core_dumps_size) * 1024 * 1024
if( len(processes) == 0):
for process in processes_orig:
sys.stdout.write("Ignoring process %d of %s\n" % (process[0], process[1]))
else:
# Dump all other processes including go programs, except python & java.
for process in [a for a in processes if not re.match("^(java|python)", a[1])]:
sys.stdout.write("Dumping process %d of %s\n" % (process[0], process[1]))
dbg.dump_info(process[0], process[1], sys.stdout, check_dump_quota(max_dump_size_bytes, dbg.get_dump_ext()))
# Dump java processes using jstack.
for process in [a for a in processes if a[1].startswith("java")]:
sys.stdout.write("Dumping process %d of %s\n" % (process[0], process[1]))
jstack.dump_info(process[0], process[1], sys.stdout)
# Signal go processes to ensure they print out stack traces, and die on POSIX OSes.
# On Windows, this will simply kill the process since python emulates SIGABRT as
# TerminateProcess.
# Note: The stacktrace output may be captured elsewhere (i.e. resmoke).
for process in [a for a in processes if a[1] in go_processes]:
sys.stdout.write("Sending signal SIGABRT to go process %d of %s\n" % (process[0], process[1]))
signal_process(process[0], signal.SIGABRT)
# Dump python processes after signalling them.
for process in [a for a in processes if a[1].startswith("python")]:
sys.stdout.write("Sending signal SIGUSR1 to python process %d of %s\n" % (process[0], process[1]))
signal_process(process[0], signal.SIGUSR1)
dbg.dump_info(process[0], process[1], sys.stdout, check_dump_quota(max_dump_size_bytes, dbg.get_dump_ext()))
# Suspend the timer so we can exit cleanly
timer.cancel()
sys.stdout.write("Done analyzing processes for hangs\n")
if __name__ == "__main__":
main()
|