-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdbremote.py
executable file
·79 lines (68 loc) · 1.98 KB
/
gdbremote.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
import socket
from subprocess import *
import subprocess
import select
from os import read
import signal
import sys
import errno
TCPBUFSIZ = 1024
TCPADDR = '127.0.0.1'
TCPPORT = 4711
SUBPBUFSIZ = 1024
BIN = '/usr/local/bin/gdb'
print "Starting process %s..." % BIN
process = Popen([BIN], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print process
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.setblocking(0)
server.bind((TCPADDR, TCPPORT))
server.listen(1)
clients = []
rlist = [server, process.stdout, process.stderr]
wlist = []
xlist = []
def appendClient(client):
rlist.append(client)
clients.append(client)
def broadcastData(data):
for sock in clients:
sock.send(data)
def signalHandler(sig, frame):
if sig == signal.SIGINT:
process.kill()
for sock in clients:
sock.close()
server.close()
sys.exit(0)
def exitProc(code):
for sock in clients:
sock.close()
server.close()
sys.exit(code)
# set up a signal handler
signal.signal(signal.SIGINT, signalHandler)
while True:
try: r,w,e = select.select(rlist, wlist, xlist)
except select.error, v:
if v[0] != errno.EINTR: raise
else: signalHandler(signal.SIGINT, 0)
for fd in r:
if fd == server:
# new client connected, add to readList
conn, addr = server.accept()
appendClient(conn)
print "Client (%s, %s) connected" % addr
elif fd == process.stdout or fd == process.stderr:
# Got data from running process, redirect
if process.poll() is not None:
exitProc(0)
data = read(fd.fileno(), SUBPBUFSIZ)
broadcastData(data)
else:
# Got data from one of the connected clients
data = fd.recv(TCPBUFSIZ)
process.stdin.write(data)
process.stdin.flush()