test: Add more pretty print for service monitoring
[framework/connectivity/connman.git] / test / backtrace
1 #!/usr/bin/python
2
3 import os
4 import re
5 import sys
6 import subprocess
7
8 if (len(sys.argv) < 3):
9         print "Usage: %s [binary] [log]" % (sys.argv[0])
10         sys.exit(1)
11
12 binary = sys.argv[1]
13 count = 0
14 frames = []
15 addrs = []
16
17 log_file = open(sys.argv[2], 'r')
18
19 # Extract addresses
20 for line in log_file:
21         matchobj = re.compile(r'\[(0x[0-9a-f]+)\]$').search(line)
22         if matchobj:
23                 addrs.append(matchobj.group(1))
24
25 log_file.close()
26
27 # Feed into addr2line
28 command = ['addr2line', '--demangle', '--functions', '--basename',
29                                                         '-e', binary]
30 command.extend(addrs)
31
32 p = subprocess.Popen(command, shell=False, bufsize=0,
33                 stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
34 (child_stdin, child_stdout) = (p.stdin, p.stdout)
35
36 child_stdin.close()
37
38 # Backtrace display
39 for line in child_stdout:
40
41         if line.startswith("??"):
42                 continue
43
44         line = line.strip()
45
46         frames.append(line)
47
48 child_stdout.close()
49
50 frame_count = len(frames);
51
52 count = 0
53 print "-------- backtrace --------"
54 while count < frame_count:
55         print "[%d]: %s() [%s]" % (count/2, frames[count], frames[count + 1])
56         count = count + 2
57 print "---------------------------"