/
gotrace.py
executable file
·117 lines (94 loc) · 3.44 KB
/
gotrace.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
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
#!/usr/bin/env python
"""gotrace: Graphical otrace launcher
"""
import logging
import os
import signal
import sys
import threading
import time
import traceback
import gtermhost
def main(args=None):
import imp
global Gterm_host, Host_secret, Trace_shell
if args is None:
args = sys.argv[1:]
funcname = ""
server = "localhost"
hostname = ""
j = 0
while j < len(args)-1:
if args[j] == "-f":
funcname = args[j+1]
elif args[j] == "-n":
hostname = args[j+1]
elif args[j] == "-s":
server = args[j+1]
else:
break
j += 2
if j >= len(args):
print >> sys.stderr, "Usage: gotrace [-f function_name] [-n hostname] [-s server_addr[:port] (default: localhost:%d)] program_file [arg1 arg2 ...]" % gtermhost.DEFAULT_HOST_PORT
sys.exit(1)
filepath = args[j]
args = args[j+1:]
if not os.path.isfile(filepath) or not os.access(filepath, os.R_OK):
print >> sys.stderr, "gotrace: Unable to read file %s" % filepath
sys.exit(1)
abspath = os.path.abspath(filepath)
filedir, basename = os.path.split(abspath)
modname, extension = os.path.splitext(basename)
if not hostname:
hostname = modname
if ":" in server:
server, sep, port = server.partition(":")
port = int(port)
else:
port = gtermhost.DEFAULT_HOST_PORT
# Load program as module
modfile, modpath, moddesc = imp.find_module(modname, [filedir])
modobj = imp.load_module(modname, modfile, modpath, moddesc)
orig_funcobj = getattr(modobj, funcname, None) if funcname else None
if funcname and not callable(orig_funcobj):
print >> sys.stderr, "gotrace: Program %s does not have function named '%s'" % (filepath, funcname)
sys.exit(1)
# Connect to gterm as host, invoking OShell
oshell_globals = modobj.__dict__
Gterm_host, Host_secret, Trace_shell = gtermhost.gterm_connect(hostname, server,
server_port=port,
connect_kw={},
oshell_globals=oshell_globals,
oshell_thread=True,
oshell_unsafe=True,
oshell_init=modname+".trc")
def host_shutdown():
print >> sys.stderr, "Shutting down"
gtermhost.gterm_shutdown(Trace_shell)
def sigterm(signal, frame):
logging.warning("SIGTERM signal received")
host_shutdown()
signal.signal(signal.SIGTERM, sigterm)
try:
if funcname:
# Delay to ensure tracing has started
time.sleep(1)
# Call function in module (may be wrapped, if being traced)
funcobj = getattr(modobj, funcname)
if args:
funcobj(args)
else:
funcobj()
else:
# Blocks until run command is issued
Trace_shell.loop(wait_to_run=True)
except Exception, excp:
traceback.print_exc()
print >> sys.stderr, "\nType ^C to abort"
Trace_shell.execute("cd ~~")
while not Trace_shell.shutting_down:
time.sleep(1)
finally:
host_shutdown()
if __name__ == "__main__":
main(args=sys.argv[1:])