15def main(basedir, cmd):
16 logs = collections.deque(maxlen=500)
17
18 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
19 stderr=subprocess.STDOUT,
20 encoding='ISO-8859-1')
21 for line in iter(proc.stdout.readline, ''):
22 sys.stdout.write(line)
23 logs.append(line)
24 proc.wait()
25 print(
'Command exited with code %s' % proc.returncode)
26
27 try:
28 subprocess.check_call(['addr2line', '--help'],
29 stdout=subprocess.DEVNULL,
30 stderr=subprocess.DEVNULL)
31 except OSError:
32 print(
'addr2line not found on PATH. Skipping symbolization.')
33 return
34
35
36
37
38
39
40
41
42
43
44
45 stack_line = r'^(?P<path>.+)\(\+?(?P<addr>.*)\) ?\[(?P<addr2>.+)\]'
46
47
48
49
50 extra_path = r'/.*\.\./'
51 is_first = True
52 last = ""
53 for line in logs:
54 line = line.strip()
55
56
57 if line == last:
58 continue
59 last = line
60
61 m = re.search(stack_line, line)
62 if m:
63 if is_first:
64 print(
'#######################################')
65 print(
'symbolized stacktrace follows')
66 print(
'#######################################')
67 is_first = False
68
69 path = m.group('path')
70 addr = m.group('addr')
71 addr2 = m.group('addr2')
72 if os.path.exists(path):
73 if not addr or not addr.startswith('0x'):
74 addr = addr2
75 try:
76 sym = subprocess.check_output([
77 'addr2line', '--demangle', '--pretty-print', '--functions',
78 '--exe='+path, addr
80 except subprocess.CalledProcessError:
81 sym = ''
82 sym = sym.strip()
83
84
85 if sym and not sym.startswith('?'):
86 if path.startswith(basedir):
87 path = path[
len(basedir) + 1:]
88 sym = re.sub(extra_path, '', sym)
89 line = path + ' ' + sym
91
92 sys.exit(proc.returncode)
93
94
def print(*args, **kwargs)
static DecodeResult decode(std::string path)