-
Notifications
You must be signed in to change notification settings - Fork 7
/
loader
executable file
·293 lines (234 loc) · 8.29 KB
/
loader
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/bin/bash
_=[ 'exec' '/bin/sh' '-c' '''
command -v python >/dev/null && exec python "$0" "$@"
command -v python3 >/dev/null && exec python3 "$0" "$@"
exec python "$0" "$@"
''' "$0" "$@"
]
del _
import os
import sys
import signal
print(os.path.abspath(__file__))
dirname, filename = os.path.split(__file__)
dirname = os.path.expanduser(dirname)
andb_dir = os.path.abspath(dirname)
from andb.loader import FileWrap, GdbLoader, LldbLoader
import argparse
loader_desc = """
Alibaba Noslate Debugger Loader
A better experience for any node.js developers.
1) Debugging a corefile using lldb/gdb,
andb -l -c core
andb -g -c core
-l, --lldb : choose lldb for debugging (default)
-g, --gdb : choose gdb for debgging
-c, --core : path to corefile
2) Debug a live process,
andb -l -p <pid>
-p, --pid : pid of living process
3) Batch debugging,
andb -l -c core -b -xf export_snapshot.cmd
andb -l -c core -b -x iso g p
-b, --batch : batch mode
-xf, --command-file : command from file
-x, --command : command from line
-x and -xf can be used multiple times and in conjunction.
4) Export core.heapsnapshot by MapReduce,
andb -l -c core -m snapshot -j 4
-m : --mode, MapReduce mode.
-j : --jobs, allow N jobs at once.
"""
parser = argparse.ArgumentParser(description=loader_desc, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-g', '--gdb', action='store_true', help='using gdb as debugger.')
parser.add_argument('-l', '--lldb', action='store_true', help='using lldb as debugger. (default)')
parser.add_argument('-p', '--pid', nargs=1, type=int, help='the process id to attach to.')
parser.add_argument('-b', '--batch', action='store_true', help='the process id to attach to.')
parser.add_argument('-t', '--tag', nargs=1, type=str, help='specified version for debugging.')
parser.add_argument('-m', '--mode', nargs=1, choices=['snapshot', 'cache'], help='mapreduce mode (snapshot, cache).')
parser.add_argument('-j', '--jobs', action='store', type=int, help='jobs for mapreduce.')
parser.add_argument('-x', '--command', dest='cmds', action='append', nargs="+", type=str, help='eval command can be multiple times.')
parser.add_argument('-xf', '--command-file', dest='cmds', action='append', nargs=1, type=FileWrap, help='eval command can be multiple times.')
parser.add_argument('-c', '--core', nargs="?", type=str, help='path to corefile.')
parser.add_argument('--args', nargs=argparse.REMAINDER, help='debugger options')
parser.add_argument('binary', nargs="?", type=str, help='node or shinki binaray')
parser.add_argument('-z', '--tsr', action='store_true', help='Generate Technical Support Report.')
parser.add_argument('--sysroot', action='store_true', help='Makeup sysroot for corefile.')
args, dbg_opts = parser.parse_known_args()
#if not andb_dir in sys.path:
# sys.path.insert(0, andb_dir)
#print('gdb:', args.gdb, 'lldb:', args.lldb,
# 'core:', args.core, 'binary:', args.binary, 'opts:', args.opts)
# node and node.typ path
binary = None
typfile = None
def Abort():
print("Aborted, killall sub-processes.")
os.killpg(0, 9)
def term_handler(signo, frame):
print('')
print("Received Signal(%d)" % signo)
Abort()
def GetLoader(andb_dir):
if args.gdb:
return GdbLoader(andb_dir)
return LldbLoader(andb_dir)
if args.binary:
""" use specified binary
"""
binary = args.binary
if args.core:
""" only corefile
"""
from andb.loader import CorefileAuxiliaryDownloader, Corefile
corepath = args.core
corefileFmt = Corefile()
corefileFmt.Load(corepath)
if not args.binary:
corefileAuxiliaryDownloader = CorefileAuxiliaryDownloader(corefileFmt.ArchName())
if args.tag and len(args.tag) > 0:
info = corefileAuxiliaryDownloader.FetchByTag(args.tag[0])
else:
buildId = corefileFmt.GetBuildId()
print('build-id:', buildId)
info = corefileAuxiliaryDownloader.FetchByBuildId(buildId)
typfile = info['typ']
os.environ['ANDB_TYP'] = typfile
if 'bin' in info:
binary = info['bin']
def IndexProcess(size):
loader = GetLoader(andb_dir)
loader.SetExec(binary)
loader.SetTyp(typfile)
loader.SetCore(args.core)
loader.BatchOn()
loader.AddCommandFile('%s/init/pre_mapreduce.cmd'%andb_dir)
loader.AddCommandLine('mapreduce index %d'%size)
opts = loader.Opts()
return os.spawnvp(os.P_WAIT, opts[0], opts)
def SnapProcess(index):
print("my index is %d" % index)
loader = GetLoader(andb_dir)
loader.SetExec(binary)
loader.SetTyp(typfile)
loader.SetCore(args.core)
loader.BatchOn()
loader.AddCommandFile('%s/init/pre_mapreduce.cmd'%andb_dir)
loader.AddCommandLine('mapreduce snapshot %d'%index)
opts = loader.Opts()
#os.execp(opts[0], opts)
os.spawnvp(os.P_WAIT, opts[0], opts)
def ReduceProcess(concurrency):
loader = GetLoader(andb_dir)
loader.SetExec(binary)
loader.SetTyp(typfile)
loader.SetCore(args.core)
loader.BatchOn()
loader.AddCommandFile('%s/init/pre_mapreduce.cmd'%andb_dir)
loader.AddCommandLine('mapreduce reduce')
opts = loader.Opts()
os.spawnvp(os.P_WAIT, opts[0], opts)
def MapReduce():
from multiprocessing import Process, Pool
from time import time
import re
t0 = time()
concurrency = 4
if args.jobs:
concurrency = args.jobs
p = Process(target=IndexProcess, args=(100,))
p.start()
p.join()
t1 = time()
print("IndexProcesses done.")
reduce_process = Process(target=ReduceProcess, args=(concurrency,))
reduce_process.start()
maps = []
for f in os.listdir("snapshot.d"):
if f.endswith(".map"):
n = re.findall(r'\d+', f)[0]
maps.append(int(n))
maps.sort()
print(maps)
pool = Pool(processes=concurrency)
results = [ pool.apply_async(SnapProcess, (i,)) for i in maps ]
pool.close()
pool.join()
t2 = time()
print("SnapshotProcesses all done.")
failed = False
for i in maps:
if not os.path.exists("snapshot.d/snapshot_%d.rec" % i):
print("Job map_%d failed." % i)
failed = True
if failed:
print("ReduceProcess not satisfied.")
Abort()
assert 0
print("Wait ReduceProcess to complete ...")
reduce_process.join()
t3 = time()
print('real {:.3f}s'.format(t3-t0))
print('map {:.3f}s'.format(t1-t0))
print('parse {:.3f}s'.format(t2-t1))
print('reduce {:.3f}s'.format(t3-t2))
def TsrProcess():
loader = GetLoader(andb_dir)
loader.SetExec(binary)
loader.SetTyp(typfile)
loader.SetCore(args.core)
loader.BatchOn()
loader.AddCommandLine('andb tsr')
opts = loader.Opts()
os.spawnvp(os.P_WAIT, opts[0], opts)
if __name__ == '__main__':
# Generate Technical Support Report.
if args.tsr:
from andb.loader import TechReport, TechReportText
if args.core:
TsrProcess()
rpt = TechReport(corefileFmt)
rpt.Generate("%s.tsr" % args.core)
elif args.binary:
txt = TechReportText(args.binary)
txt.ShowAll()
exit(0)
# Makeup sysroot
if args.sysroot:
from andb.loader import SysrootMaker
print('makeup sysroot')
srm = SysrootMaker(binary, corefileFmt, sysRoot="sysroot")
srm.Makeup()
exit(0)
# map reduce mode
if args.mode:
# leader the new process group
os.setpgrp()
signal.signal(signal.SIGINT, term_handler)
signal.signal(signal.SIGTERM, term_handler)
MapReduce()
exit(0)
if binary is None and \
args.core is None and \
args.pid is None:
exit(0)
# single process mode
loader = GetLoader(andb_dir)
if binary:
loader.SetExec(binary)
if args.core:
loader.SetCore(args.core)
if args.pid:
loader.SetPid(args.pid[0])
if args.batch:
loader.BatchOn()
if args.args and len(args.args) > 0:
loader.AddArgs(args.args)
if args.cmds and len(args.cmds) > 0:
print(args.cmds)
loader.AddCommands(args.cmds)
# ignore CTRL+C for python
signal.signal(signal.SIGINT, signal.SIG_IGN)
# spawn dbg
opts = loader.Opts()
os.spawnvp(os.P_WAIT, opts[0], opts)