-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwaitfor.py
63 lines (52 loc) · 1.85 KB
/
waitfor.py
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
from __future__ import with_statement
import psutil
import subprocess
import time
import gdb
def pidof(pgname):
pids = []
for proc in psutil.process_iter():
# search for matches in the process name and cmdline
try:
name = proc.name()
except psutil.Error:
pass
else:
if pgname in name:
pids.append(proc.pid)
continue
try:
cmdline = proc.cmdline()
except psutil.Error:
pass
else:
if cmdline and pgname in cmdline[0]:
pids.append(proc.pid)
return pids
class WaitforCommand (gdb.Command):
def __init__ (self):
self.lastFn = ''
super (WaitforCommand, self).__init__ ("waitfor", gdb.COMMAND_SUPPORT, gdb.COMPLETE_FILENAME)
def invoke (self, arg, from_tty):
args = arg.split(' ')
fn = [arg for arg in args if not arg.startswith('-')] [-1]
if len(fn) > 0:
self.lastFn = fn
if len(self.lastFn) == 0:
print('You have to specify the name of the process (for pidof) for the first time (it will be cached for later)')
return
while True:
try:
pids = pidof(self.lastFn)
if len(pids) > 0:
break
#pid = subprocess.check_output(["pidof", self.lastFn], bufsize=1024, stdout=subprocess.PIPE, stderr=subprocess.PIPE).strip()
except subprocess.CalledProcessError as e:
if e.returncode != 1:
raise e
time.sleep(0.1)
print('PID: %r' % pids)
gdb.execute('attach ' + str(pids[0]))
if '-c' in args:
gdb.execute('c')
WaitforCommand()