Skip to content

Instantly share code, notes, and snippets.

@argp
Created December 28, 2011 09:56
Show Gist options
  • Save argp/1527396 to your computer and use it in GitHub Desktop.
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
#!/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
@vrdhn
Copy link

vrdhn commented Sep 8, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment