|
| 1 | +#! @PYTHON@ |
| 2 | +# |
| 3 | +# Copyright (c) 2012 Nicira, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at: |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import optparse |
| 18 | +import os |
| 19 | +import re |
| 20 | +import subprocess |
| 21 | +import sys |
| 22 | + |
| 23 | + |
| 24 | +addr2line_cache = {} # None if addr2line is missing or broken. |
| 25 | + |
| 26 | + |
| 27 | +def addr2line(binary, addr): |
| 28 | + global addr2line_cache |
| 29 | + |
| 30 | + if addr2line_cache is None: |
| 31 | + return "" |
| 32 | + |
| 33 | + if addr in addr2line_cache: |
| 34 | + return addr2line_cache[addr] |
| 35 | + |
| 36 | + cmd = ["addr2line", "-f", "-s", "-e", binary, addr] |
| 37 | + try: |
| 38 | + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 39 | + stderr=subprocess.PIPE) |
| 40 | + lines = proc.stdout.readlines() |
| 41 | + failed = proc.returncode |
| 42 | + except OSError: |
| 43 | + failed = True |
| 44 | + |
| 45 | + if failed: |
| 46 | + addr2line_cache = None |
| 47 | + return "" |
| 48 | + |
| 49 | + lines = [l.strip() for l in lines] |
| 50 | + return " ".join(lines) |
| 51 | + |
| 52 | + |
| 53 | +def main(): |
| 54 | + parser = optparse.OptionParser(version='@VERSION@', |
| 55 | + usage="usage: %prog [binary]", |
| 56 | + description="""\ |
| 57 | +Parses the output of ovs-appctl backtrace producing a more human readable |
| 58 | +result. Expected usage is for ovs-appctl backtrace to be piped in.""") |
| 59 | + options, args = parser.parse_args() |
| 60 | + |
| 61 | + if len(args) > 1: |
| 62 | + parser.print_help() |
| 63 | + sys.exit(1) |
| 64 | + |
| 65 | + if len(args) == 1: |
| 66 | + binary = args[0] |
| 67 | + else: |
| 68 | + binary = "@sbindir@/ovs-vswitchd" |
| 69 | + debug = "/usr/lib/debug%s.debug" % binary |
| 70 | + if os.path.exists(debug): |
| 71 | + binary = debug |
| 72 | + |
| 73 | + print "Binary: %s\n" % binary |
| 74 | + |
| 75 | + stdin = sys.stdin.read() |
| 76 | + trace_list = stdin.strip().split("\n\n") |
| 77 | + |
| 78 | + try: |
| 79 | + #Remove the first line from each trace. |
| 80 | + trace_list = [trace[(trace.index("\n") + 1):] for trace in trace_list] |
| 81 | + except ValueError: |
| 82 | + sys.stdout.write(stdin) |
| 83 | + sys.exit(1) |
| 84 | + |
| 85 | + trace_map = {} |
| 86 | + for trace in trace_list: |
| 87 | + trace_map[trace] = trace_map.get(trace, 0) + 1 |
| 88 | + |
| 89 | + sorted_traces = sorted(trace_map.items(), key=(lambda x: x[1]), |
| 90 | + reverse=True) |
| 91 | + for trace, count in sorted_traces: |
| 92 | + lines = trace.splitlines() |
| 93 | + longest = max(len(l) for l in lines) |
| 94 | + |
| 95 | + print "Backtrace Count: %d" % count |
| 96 | + for line in lines: |
| 97 | + match = re.search(r'\[(0x.*)]', line) |
| 98 | + if match: |
| 99 | + print "%s %s" % (line.ljust(longest), |
| 100 | + addr2line(binary, match.group(1))) |
| 101 | + else: |
| 102 | + print line |
| 103 | + print |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + main() |
0 commit comments