3 # Copyright (C) 2017 Francis Deslauriers <francis.deslauriers@efficios.com>
5 # SPDX-License-Identifier: LGPL-2.1-only
13 def addr2line(executable
, addr
):
15 Uses binutils' addr2line to get function containing a given address
19 cmd
+= ['-e', executable
]
21 # Print function names
22 cmd
+= ['--functions']
24 # Expand inlined functions
25 cmd
+= ['--addresses', addr
]
27 addr2line_output
= subprocess
.getoutput(' '.join(cmd
))
29 # Omit the last 2 lines as the caller of main can not be determine
30 fcts
= [addr2line_output
.split()[-2]]
32 fcts
= [ f
for f
in fcts
if '??' not in f
]
36 def extract_user_func_names(executable
, raw_callstack
):
38 Given a callstack from the Babeltrace CLI output, returns a set
39 containing the name of the functions. This assumes that the binary have
40 not changed since the execution.
42 recorded_callstack
= set()
44 # Remove commas and split on spaces
45 for index
, addr
in enumerate(raw_callstack
.replace(',', '').split(' ')):
46 # Consider only the elements starting with '0x' which are the
47 # addresses recorded in the callstack
49 funcs
= addr2line(executable
, addr
)
50 recorded_callstack
.update(funcs
)
52 return recorded_callstack
54 def extract_kernel_func_names(raw_callstack
):
56 Given a callstack from the Babeltrace CLI output, returns a set
57 containing the name of the functions.
58 Uses the /proc/kallsyms procfile to find the symbol associated with an
59 address. This function should only be used if the user is root or has
60 access to /proc/kallsyms.
62 recorded_callstack
= set()
65 # We read kallsyms file and save the output
66 with
open('/proc/kallsyms') as kallsyms_f
:
67 for line
in kallsyms_f
:
68 line_tokens
= line
.split()
70 symbol
= line_tokens
[2]
71 addresses
.append(int(addr
, 16))
72 syms
.append({'addr':int(addr
, 16), 'symbol':symbol
})
74 # Save the address and symbol in a sorted list of tupple
75 syms
= sorted(syms
, key
=lambda k
:k
['addr'])
76 # We save the list of addresses in a seperate sorted list to easily bisect
77 # the closer address of a symbol.
78 addresses
= sorted(addresses
)
80 # Remove commas and split on spaces
81 for addr
in raw_callstack
.replace(',', '').split(' '):
83 # Search the location of the address in the addresses list and
84 # deference this location in the syms list to add the associated
86 loc
= bisect
.bisect(addresses
, int(addr
, 16))
87 recorded_callstack
.add(syms
[loc
-1]['symbol'])
89 return recorded_callstack
91 # Regex capturing the callstack_user and callstack_kernel context
92 user_cs_rexp
='.*callstack_user\ \=\ \[(.*)\]\ .*\}, \{.*\}'
93 kernel_cs_rexp
='.*callstack_kernel\ \=\ \[(.*)\]\ .*\}, \{.*\}'
97 Reads a line from stdin and expect it to be a wellformed Babeltrace CLI
98 output containing containing a callstack context of the domain passed
101 expected_callstack
= set()
102 recorded_callstack
= set()
105 if len(sys
.argv
) <= 2:
107 raise ValueError('USAGE: ./{} (--kernel|--user EXE) FUNC-NAMES'.format(sys
.argv
[0]))
109 # If the `--user` option is passed, save the next argument as the path
113 if sys
.argv
[argc
] in '--kernel':
114 rexp
= kernel_cs_rexp
116 elif sys
.argv
[argc
] in '--user':
120 executable
= sys
.argv
[argc
]
122 raise Exception('Unknown domain')
126 # Extract the function names that are expected to be found call stack of
128 for func
in sys
.argv
[argc
:]:
129 expected_callstack
.add(func
)
131 # Read the tested line for STDIN
133 for line
in sys
.stdin
:
137 # Extract the userspace callstack context of the event
138 m
= re
.match(rexp
, event_line
)
140 # If there is no match, exit with error
142 raise re
.error('Callstack not found in event line')
144 raw_callstack
= str(m
.group(1))
145 if cs_type
in 'user':
146 recorded_callstack
=extract_user_func_names(executable
, raw_callstack
)
147 elif cs_type
in 'kernel':
148 recorded_callstack
=extract_kernel_func_names(raw_callstack
)
150 raise Exception('Unknown domain')
152 # Verify that all expected function are present in the callstack
153 for e
in expected_callstack
:
154 if e
not in recorded_callstack
:
155 raise Exception('Expected function name not found in recorded callstack')
159 if __name__
== '__main__':
This page took 0.032819 seconds and 4 git commands to generate.