]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/scripts/seastar-addr2line
import quincy beta 17.1.0
[ceph.git] / ceph / src / seastar / scripts / seastar-addr2line
1 #!/usr/bin/env python3
2 #
3 # This file is open source software, licensed to you under the terms
4 # of the Apache License, Version 2.0 (the "License"). See the NOTICE file
5 # distributed with this work for additional information regarding copyright
6 # ownership. You may not use this file except in compliance with the License.
7 #
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
17 # under the License.
18 #
19 # Copyright (C) 2017 ScyllaDB
20
21 import argparse
22 import collections
23 import re
24 import sys
25 import subprocess
26 from enum import Enum
27
28 from addr2line import BacktraceResolver
29
30 class StdinBacktraceIterator(object):
31 """
32 Read stdin char-by-char and stop when when user pressed Ctrl+D or the
33 Enter twice. Altough reading char-by-char is slow this won't be a
34 problem here as backtraces shouldn't be huge.
35 """
36 def __iter__(self):
37 linefeeds = 0
38 lines = []
39 line = []
40
41 while True:
42 char = sys.stdin.read(1)
43
44 if char == '\n':
45 linefeeds += 1
46
47 if len(line) > 0:
48 lines.append(''.join(line))
49 line = []
50 else:
51 line.append(char)
52 linefeeds = 0
53
54 if char == '' or linefeeds > 1:
55 break
56
57 return iter(lines)
58
59
60 description='Massage and pass addresses to the real addr2line for symbol lookup.'
61 epilog='''
62 There are three operational modes:
63 1) If -f is specified input will be read from FILE
64 2) If -f is omitted and there are ADDRESS args they will be read as input
65 3) If -f is omitted and there are no ADDRESS args input will be read from stdin
66 '''
67
68 cmdline_parser = argparse.ArgumentParser(
69 description=description,
70 epilog=epilog,
71 formatter_class=argparse.RawDescriptionHelpFormatter,
72 )
73
74 cmdline_parser.add_argument(
75 '-e',
76 '--executable',
77 type=str,
78 required=True,
79 metavar='EXECUTABLE',
80 dest='executable',
81 help='The executable where the addresses originate from')
82
83 cmdline_parser.add_argument(
84 '-f',
85 '--file',
86 type=str,
87 required=False,
88 metavar='FILE',
89 dest='file',
90 help='The file containing the addresses')
91
92 cmdline_parser.add_argument(
93 '-b',
94 '--before',
95 type=int,
96 metavar='BEFORE',
97 default=1,
98 help='Non-backtrace lines to print before resolved backtraces for context.'
99 ' Set to 0 to print only resolved backtraces.'
100 ' Set to -1 to print all non-backtrace lines. Default is 1.')
101
102 cmdline_parser.add_argument(
103 '-m',
104 '--match',
105 type=str,
106 metavar='MATCH',
107 help='Only resolve backtraces whose non-backtrace lines match the regular-expression.'
108 ' The amount of non-backtrace lines considered can be set with --before.'
109 ' By default no matching is performed.')
110
111 cmdline_parser.add_argument(
112 '-v',
113 '--verbose',
114 action='store_true',
115 default=False,
116 help='Make resolved backtraces verbose, prepend to each line the module'
117 ' it originates from, as well as the address being resolved')
118
119 cmdline_parser.add_argument(
120 '-t',
121 '--test',
122 action='store_true',
123 default=False,
124 help='Self-test')
125
126 cmdline_parser.add_argument(
127 'addresses',
128 type=str,
129 metavar='ADDRESS',
130 nargs='*',
131 help='Addresses to parse')
132
133 args = cmdline_parser.parse_args()
134
135 if args.test:
136 data = [
137 ('---', {'type': BacktraceResolver.BacktraceParser.Type.SEPARATOR}),
138
139 ('0x12f34', {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS, 'prefix': None, 'addresses': [{'path': None, 'addr': '0x12f34'}]}),
140 ('0xa1234 0xb4567', {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS, 'prefix': None, 'addresses': [{'path': None, 'addr': '0xa1234'},{'path': None, 'addr': '0xb4567'}]}),
141 (' 0xa1234 /my/path+0xb4567', {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS, 'prefix': None, 'addresses': [{'path': None, 'addr': '0xa1234'},{'path': '/my/path', 'addr': '0xb4567'}]}),
142 ('/my/path+0x12f34', {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS, 'prefix': None, 'addresses': [{'path': '/my/path', 'addr': '0x12f34'}]}),
143 (' /my/path+0x12f34', {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS, 'prefix': None, 'addresses': [{'path': '/my/path', 'addr': '0x12f34'}]}),
144
145 ('Some prefix 0x12f34', None),
146 ('Some prefix /my/path+0x12f34', None),
147
148 ('Reactor stalled on shard 1. Backtrace: 0x12f34',
149 {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS, 'prefix': 'Reactor stalled on shard 1. Backtrace:',
150 'addresses': [{'path': None, 'addr': '0x12f34'}]}),
151 ('Reactor stalled on shard 1. Backtrace: 0xa1234 0xb5678',
152 {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS, 'prefix': 'Reactor stalled on shard 1. Backtrace:',
153 'addresses': [{'path': None, 'addr': '0xa1234'}, {'path': None, 'addr': '0xb5678'}]}),
154 ('Reactor stalled on shard 1. Backtrace: /my/path+0xabcd',
155 {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS, 'prefix': 'Reactor stalled on shard 1. Backtrace:',
156 'addresses': [{'path': '/my/path', 'addr': '0xabcd'}]}),
157
158 ('Expected partition_end(), but got end of stream, at 0xa1234 0xb5678 /my/path+0xabcd',
159 {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS, 'prefix': 'Expected partition_end(), but got end of stream, at',
160 'addresses': [{'path': None, 'addr': '0xa1234'}, {'path': None, 'addr': '0xb5678'}, {'path': '/my/path', 'addr': '0xabcd'}]}),
161
162 ('==16118==ERROR: AddressSanitizer: heap-use-after-free on address 0x60700019c710 at pc 0x000014d24643 bp 0x7ffc51f72220 sp 0x7ffc51f72218', None),
163 ('READ of size 8 at 0x60700019c710 thread T0', None),
164 ('#0 0x14d24642 (/jenkins/workspace/scylla-enterprise/dtest-debug/scylla/.ccm/scylla-repository/1a5173bd45d01697d98ba2a7645f5d86afb2d0be/scylla/libexec/scylla+0x14d24642)',
165 {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS, 'prefix': None,
166 'addresses': [{'path': '/jenkins/workspace/scylla-enterprise/dtest-debug/scylla/.ccm/scylla-repository/1a5173bd45d01697d98ba2a7645f5d86afb2d0be/scylla/libexec/scylla', 'addr': '0x14d24642'}]}),
167
168 ('Apr 28 11:42:58 ip-172-31-2-154.ec2.internal scylla[10612]: Reactor stalled for 260 ms on shard 20.', None),
169 ('Apr 28 11:42:58 ip-172-31-2-154.ec2.internal scylla[10612]: Backtrace:', None),
170 ('Apr 28 11:42:58 ip-172-31-2-154.ec2.internal scylla[10612]: 0x0000000003163dc2',
171 {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS, 'prefix': 'Apr 28 11:42:58 ip-172-31-2-154.ec2.internal scylla[10612]:',
172 'addresses': [{'path': None, 'addr': '0x0000000003163dc2'}]}),
173
174 ('seastar::internal::backtraced<std::runtime_error> (throw_with_backtrace_exception_logging Backtrace: 0x42bc95 /lib64/libc.so.6+0x281e1 0x412cfd)',
175 {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS, 'prefix': 'seastar::internal::backtraced<std::runtime_error> (throw_with_backtrace_exception_logging Backtrace:',
176 'addresses': [{'path': None, 'addr': '0x42bc95'}, {'path': '/lib64/libc.so.6', 'addr': '0x281e1'}, {'path': None, 'addr': '0x412cfd'}]}),
177 ('seastar::nested_exception: seastar::internal::backtraced<std::runtime_error> (inner Backtrace: 0x42bc95 /lib64/libc.so.6+0x281e1 0x412cfd) (while cleaning up after unknown_obj)',
178 {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS, 'prefix': 'seastar::nested_exception: seastar::internal::backtraced<std::runtime_error> (inner Backtrace:',
179 'addresses': [{'path': None, 'addr': '0x42bc95'}, {'path': '/lib64/libc.so.6', 'addr': '0x281e1'}, {'path': None, 'addr': '0x412cfd'}]}),
180 ('seastar::nested_exception: seastar::internal::backtraced<std::runtime_error> (inner Backtrace: 0x42bc95 /lib64/libc.so.6+0x281e1 0x412cfd) '
181 '(while cleaning up after seastar::internal::backtraced<std::runtime_error> (outer Backtrace: 0x1234 /lib64/libc.so.6+0x5678 0xabcd))',
182 {'type': BacktraceResolver.BacktraceParser.Type.ADDRESS,
183 'prefix': 'seastar::nested_exception: seastar::internal::backtraced<std::runtime_error> (inner Backtrace: 0x42bc95 /lib64/libc.so.6+0x281e1 0x412cfd)'
184 ' (while cleaning up after seastar::internal::backtraced<std::runtime_error> (outer Backtrace:',
185 'addresses': [{'path': None, 'addr': '0x1234'}, {'path': '/lib64/libc.so.6', 'addr': '0x5678'}, {'path': None, 'addr': '0xabcd'}]}),
186 ]
187 parser = BacktraceResolver.BacktraceParser()
188 for line, expected in data:
189 res = parser(line)
190 assert res == expected, f"{line}:\nExpected {expected}\nBut got {res}"
191 exit(0)
192
193 if args.addresses and args.file:
194 print("Cannot use both -f and ADDRESS")
195 cmdline_parser.print_help()
196
197
198 if args.file:
199 lines = open(args.file, 'r')
200 elif args.addresses:
201 lines = args.addresses
202 else:
203 if sys.stdin.isatty():
204 lines = StdinBacktraceIterator()
205 else:
206 lines = sys.stdin
207
208 with BacktraceResolver(executable=args.executable, before_lines=args.before, context_re=args.match, verbose=args.verbose) as resolve:
209 p = re.compile(r'\W+')
210 for line in lines:
211 resolve(line)