Created
December 28, 2011 09:56
-
-
Save argp/1527396 to your computer and use it in GitHub Desktop.
Python script to wait for a process to launch and attach gdb to it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import string | |
import commands | |
import subprocess | |
true = True | |
false = False | |
def main(argv): | |
argc = len(argv) | |
if argc != 2: | |
print '[*] gdbwaitforproc.py by argp at domain census-labs.com' | |
print '[*] usage: %s <process name>' % (argv[0]) | |
sys.exit(1) | |
pname = argv[1] | |
pstr = '' | |
while true: | |
pstr = commands.getoutput('ps aux | grep %s | grep -v grep | grep -v %s' % (pname, argv[0])) | |
if pstr == '': | |
print '[+] waiting for process: %s' % (pname) | |
else: | |
break | |
pid = string.split(pstr)[1] | |
if pid == '': | |
print '[!] error finding the PID of process %s' % (pname) | |
sys.exit(1) | |
gdb_proc = subprocess.Popen(['gdb', '--command=~/.gdbinit', '--pid=%d' % (int(pid))]) | |
gdb_proc.wait() | |
if __name__ == '__main__': | |
main(sys.argv) | |
sys.exit(0) | |
# EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A better ps line:
pstr = commands.getoutput('ps -u %s -o pid,fname | grep %s ' % (os.environ['USER'],pname))
Also change to [0] of split to get the pid.