~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/scripts/python/arm-cs-trace-disasm.py

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /tools/perf/scripts/python/arm-cs-trace-disasm.py (Architecture alpha) and /tools/perf/scripts/python/arm-cs-trace-disasm.py (Architecture i386)


  1 # SPDX-License-Identifier: GPL-2.0                  1 # SPDX-License-Identifier: GPL-2.0
  2 # arm-cs-trace-disasm.py: ARM CoreSight Trace       2 # arm-cs-trace-disasm.py: ARM CoreSight Trace Dump With Disassember
  3 #                                                   3 #
  4 # Author: Tor Jeremiassen <tor@ti.com>               4 # Author: Tor Jeremiassen <tor@ti.com>
  5 #         Mathieu Poirier <mathieu.poirier@lina      5 #         Mathieu Poirier <mathieu.poirier@linaro.org>
  6 #         Leo Yan <leo.yan@linaro.org>               6 #         Leo Yan <leo.yan@linaro.org>
  7 #         Al Grant <Al.Grant@arm.com>                7 #         Al Grant <Al.Grant@arm.com>
  8                                                     8 
  9 from __future__ import print_function               9 from __future__ import print_function
 10 import os                                          10 import os
 11 from os import path                                11 from os import path
 12 import re                                          12 import re
 13 from subprocess import *                           13 from subprocess import *
 14 from optparse import OptionParser, make_option     14 from optparse import OptionParser, make_option
 15                                                    15 
 16 from perf_trace_context import perf_set_itrace     16 from perf_trace_context import perf_set_itrace_options, \
 17         perf_sample_insn, perf_sample_srccode      17         perf_sample_insn, perf_sample_srccode
 18                                                    18 
 19 # Below are some example commands for using th     19 # Below are some example commands for using this script.
 20 #                                                  20 #
 21 # Output disassembly with objdump:                 21 # Output disassembly with objdump:
 22 #  perf script -s scripts/python/arm-cs-trace-     22 #  perf script -s scripts/python/arm-cs-trace-disasm.py \
 23 #               -- -d objdump -k path/to/vmlin     23 #               -- -d objdump -k path/to/vmlinux
 24 # Output disassembly with llvm-objdump:            24 # Output disassembly with llvm-objdump:
 25 #  perf script -s scripts/python/arm-cs-trace-     25 #  perf script -s scripts/python/arm-cs-trace-disasm.py \
 26 #               -- -d llvm-objdump-11 -k path/     26 #               -- -d llvm-objdump-11 -k path/to/vmlinux
 27 # Output only source line and symbols:             27 # Output only source line and symbols:
 28 #  perf script -s scripts/python/arm-cs-trace-     28 #  perf script -s scripts/python/arm-cs-trace-disasm.py
 29                                                    29 
 30 # Command line parsing.                            30 # Command line parsing.
 31 option_list = [                                    31 option_list = [
 32         # formatting options for the bottom en     32         # formatting options for the bottom entry of the stack
 33         make_option("-k", "--vmlinux", dest="v     33         make_option("-k", "--vmlinux", dest="vmlinux_name",
 34                     help="Set path to vmlinux      34                     help="Set path to vmlinux file"),
 35         make_option("-d", "--objdump", dest="o     35         make_option("-d", "--objdump", dest="objdump_name",
 36                     help="Set path to objdump      36                     help="Set path to objdump executable file"),
 37         make_option("-v", "--verbose", dest="v     37         make_option("-v", "--verbose", dest="verbose",
 38                     action="store_true", defau     38                     action="store_true", default=False,
 39                     help="Enable debugging log     39                     help="Enable debugging log")
 40 ]                                                  40 ]
 41                                                    41 
 42 parser = OptionParser(option_list=option_list)     42 parser = OptionParser(option_list=option_list)
 43 (options, args) = parser.parse_args()              43 (options, args) = parser.parse_args()
 44                                                    44 
 45 # Initialize global dicts and regular expressi     45 # Initialize global dicts and regular expression
 46 disasm_cache = dict()                              46 disasm_cache = dict()
 47 cpu_data = dict()                                  47 cpu_data = dict()
 48 disasm_re = re.compile(r"^\s*([0-9a-fA-F]+):")     48 disasm_re = re.compile(r"^\s*([0-9a-fA-F]+):")
 49 disasm_func_re = re.compile(r"^\s*([0-9a-fA-F]     49 disasm_func_re = re.compile(r"^\s*([0-9a-fA-F]+)\s.*:")
 50 cache_size = 64*1024                               50 cache_size = 64*1024
 51                                                    51 
 52 glb_source_file_name    = None                     52 glb_source_file_name    = None
 53 glb_line_number         = None                     53 glb_line_number         = None
 54 glb_dso                 = None                     54 glb_dso                 = None
 55                                                    55 
 56 def get_optional(perf_dict, field):                56 def get_optional(perf_dict, field):
 57        if field in perf_dict:                      57        if field in perf_dict:
 58                return perf_dict[field]             58                return perf_dict[field]
 59        return "[unknown]"                          59        return "[unknown]"
 60                                                    60 
 61 def get_offset(perf_dict, field):                  61 def get_offset(perf_dict, field):
 62         if field in perf_dict:                     62         if field in perf_dict:
 63                 return "+%#x" % perf_dict[fiel     63                 return "+%#x" % perf_dict[field]
 64         return ""                                  64         return ""
 65                                                    65 
 66 def get_dso_file_path(dso_name, dso_build_id):     66 def get_dso_file_path(dso_name, dso_build_id):
 67         if (dso_name == "[kernel.kallsyms]" or     67         if (dso_name == "[kernel.kallsyms]" or dso_name == "vmlinux"):
 68                 if (options.vmlinux_name):         68                 if (options.vmlinux_name):
 69                         return options.vmlinux     69                         return options.vmlinux_name;
 70                 else:                              70                 else:
 71                         return dso_name            71                         return dso_name
 72                                                    72 
 73         if (dso_name == "[vdso]") :                73         if (dso_name == "[vdso]") :
 74                 append = "/vdso"                   74                 append = "/vdso"
 75         else:                                      75         else:
 76                 append = "/elf"                    76                 append = "/elf"
 77                                                    77 
 78         dso_path = os.environ['PERF_BUILDID_DI     78         dso_path = os.environ['PERF_BUILDID_DIR'] + "/" + dso_name + "/" + dso_build_id + append;
 79         # Replace duplicate slash chars to sin     79         # Replace duplicate slash chars to single slash char
 80         dso_path = dso_path.replace('//', '/',     80         dso_path = dso_path.replace('//', '/', 1)
 81         return dso_path                            81         return dso_path
 82                                                    82 
 83 def read_disam(dso_fname, dso_start, start_add     83 def read_disam(dso_fname, dso_start, start_addr, stop_addr):
 84         addr_range = str(start_addr) + ":" + s     84         addr_range = str(start_addr) + ":" + str(stop_addr) + ":" + dso_fname
 85                                                    85 
 86         # Don't let the cache get too big, cle     86         # Don't let the cache get too big, clear it when it hits max size
 87         if (len(disasm_cache) > cache_size):       87         if (len(disasm_cache) > cache_size):
 88                 disasm_cache.clear();              88                 disasm_cache.clear();
 89                                                    89 
 90         if addr_range in disasm_cache:             90         if addr_range in disasm_cache:
 91                 disasm_output = disasm_cache[a     91                 disasm_output = disasm_cache[addr_range];
 92         else:                                      92         else:
 93                 start_addr = start_addr - dso_     93                 start_addr = start_addr - dso_start;
 94                 stop_addr = stop_addr - dso_st     94                 stop_addr = stop_addr - dso_start;
 95                 disasm = [ options.objdump_nam     95                 disasm = [ options.objdump_name, "-d", "-z",
 96                            "--start-address="+     96                            "--start-address="+format(start_addr,"#x"),
 97                            "--stop-address="+f     97                            "--stop-address="+format(stop_addr,"#x") ]
 98                 disasm += [ dso_fname ]            98                 disasm += [ dso_fname ]
 99                 disasm_output = check_output(d     99                 disasm_output = check_output(disasm).decode('utf-8').split('\n')
100                 disasm_cache[addr_range] = dis    100                 disasm_cache[addr_range] = disasm_output
101                                                   101 
102         return disasm_output                      102         return disasm_output
103                                                   103 
104 def print_disam(dso_fname, dso_start, start_ad    104 def print_disam(dso_fname, dso_start, start_addr, stop_addr):
105         for line in read_disam(dso_fname, dso_    105         for line in read_disam(dso_fname, dso_start, start_addr, stop_addr):
106                 m = disasm_func_re.search(line    106                 m = disasm_func_re.search(line)
107                 if m is None:                     107                 if m is None:
108                         m = disasm_re.search(l    108                         m = disasm_re.search(line)
109                         if m is None:             109                         if m is None:
110                                 continue          110                                 continue
111                 print("\t" + line)                111                 print("\t" + line)
112                                                   112 
113 def print_sample(sample):                         113 def print_sample(sample):
114         print("Sample = { cpu: %04d addr: 0x%0    114         print("Sample = { cpu: %04d addr: 0x%016x phys_addr: 0x%016x ip: 0x%016x " \
115               "pid: %d tid: %d period: %d time    115               "pid: %d tid: %d period: %d time: %d }" % \
116               (sample['cpu'], sample['addr'],     116               (sample['cpu'], sample['addr'], sample['phys_addr'], \
117                sample['ip'], sample['pid'], sa    117                sample['ip'], sample['pid'], sample['tid'], \
118                sample['period'], sample['time'    118                sample['period'], sample['time']))
119                                                   119 
120 def trace_begin():                                120 def trace_begin():
121         print('ARM CoreSight Trace Data Assemb    121         print('ARM CoreSight Trace Data Assembler Dump')
122                                                   122 
123 def trace_end():                                  123 def trace_end():
124         print('End')                              124         print('End')
125                                                   125 
126 def trace_unhandled(event_name, context, event    126 def trace_unhandled(event_name, context, event_fields_dict):
127         print(' '.join(['%s=%s'%(k,str(v))for     127         print(' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())]))
128                                                   128 
129 def common_start_str(comm, sample):               129 def common_start_str(comm, sample):
130         sec = int(sample["time"] / 1000000000)    130         sec = int(sample["time"] / 1000000000)
131         ns = sample["time"] % 1000000000          131         ns = sample["time"] % 1000000000
132         cpu = sample["cpu"]                       132         cpu = sample["cpu"]
133         pid = sample["pid"]                       133         pid = sample["pid"]
134         tid = sample["tid"]                       134         tid = sample["tid"]
135         return "%16s %5u/%-5u [%04u] %9u.%09u     135         return "%16s %5u/%-5u [%04u] %9u.%09u  " % (comm, pid, tid, cpu, sec, ns)
136                                                   136 
137 # This code is copied from intel-pt-events.py     137 # This code is copied from intel-pt-events.py for printing source code
138 # line and symbols.                               138 # line and symbols.
139 def print_srccode(comm, param_dict, sample, sy    139 def print_srccode(comm, param_dict, sample, symbol, dso):
140         ip = sample["ip"]                         140         ip = sample["ip"]
141         if symbol == "[unknown]":                 141         if symbol == "[unknown]":
142                 start_str = common_start_str(c    142                 start_str = common_start_str(comm, sample) + ("%x" % ip).rjust(16).ljust(40)
143         else:                                     143         else:
144                 offs = get_offset(param_dict,     144                 offs = get_offset(param_dict, "symoff")
145                 start_str = common_start_str(c    145                 start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40)
146                                                   146 
147         global glb_source_file_name               147         global glb_source_file_name
148         global glb_line_number                    148         global glb_line_number
149         global glb_dso                            149         global glb_dso
150                                                   150 
151         source_file_name, line_number, source_    151         source_file_name, line_number, source_line = perf_sample_srccode(perf_script_context)
152         if source_file_name:                      152         if source_file_name:
153                 if glb_line_number == line_num    153                 if glb_line_number == line_number and glb_source_file_name == source_file_name:
154                         src_str = ""              154                         src_str = ""
155                 else:                             155                 else:
156                         if len(source_file_nam    156                         if len(source_file_name) > 40:
157                                 src_file = (".    157                                 src_file = ("..." + source_file_name[-37:]) + " "
158                         else:                     158                         else:
159                                 src_file = sou    159                                 src_file = source_file_name.ljust(41)
160                                                   160 
161                         if source_line is None    161                         if source_line is None:
162                                 src_str = src_    162                                 src_str = src_file + str(line_number).rjust(4) + " <source not found>"
163                         else:                     163                         else:
164                                 src_str = src_    164                                 src_str = src_file + str(line_number).rjust(4) + " " + source_line
165                 glb_dso = None                    165                 glb_dso = None
166         elif dso == glb_dso:                      166         elif dso == glb_dso:
167                 src_str = ""                      167                 src_str = ""
168         else:                                     168         else:
169                 src_str = dso                     169                 src_str = dso
170                 glb_dso = dso                     170                 glb_dso = dso
171                                                   171 
172         glb_line_number = line_number             172         glb_line_number = line_number
173         glb_source_file_name = source_file_nam    173         glb_source_file_name = source_file_name
174                                                   174 
175         print(start_str, src_str)                 175         print(start_str, src_str)
176                                                   176 
177 def process_event(param_dict):                    177 def process_event(param_dict):
178         global cache_size                         178         global cache_size
179         global options                            179         global options
180                                                   180 
181         sample = param_dict["sample"]             181         sample = param_dict["sample"]
182         comm = param_dict["comm"]                 182         comm = param_dict["comm"]
183                                                   183 
184         name = param_dict["ev_name"]              184         name = param_dict["ev_name"]
185         dso = get_optional(param_dict, "dso")     185         dso = get_optional(param_dict, "dso")
186         dso_bid = get_optional(param_dict, "ds    186         dso_bid = get_optional(param_dict, "dso_bid")
187         dso_start = get_optional(param_dict, "    187         dso_start = get_optional(param_dict, "dso_map_start")
188         dso_end = get_optional(param_dict, "ds    188         dso_end = get_optional(param_dict, "dso_map_end")
189         symbol = get_optional(param_dict, "sym    189         symbol = get_optional(param_dict, "symbol")
190                                                   190 
191         cpu = sample["cpu"]                       191         cpu = sample["cpu"]
192         ip = sample["ip"]                         192         ip = sample["ip"]
193         addr = sample["addr"]                     193         addr = sample["addr"]
194                                                   194 
195         if (options.verbose == True):             195         if (options.verbose == True):
196                 print("Event type: %s" % name)    196                 print("Event type: %s" % name)
197                 print_sample(sample)              197                 print_sample(sample)
198                                                   198 
199         # Initialize CPU data if it's empty, a    199         # Initialize CPU data if it's empty, and directly return back
200         # if this is the first tracing event f    200         # if this is the first tracing event for this CPU.
201         if (cpu_data.get(str(cpu) + 'addr') ==    201         if (cpu_data.get(str(cpu) + 'addr') == None):
202                 cpu_data[str(cpu) + 'addr'] =     202                 cpu_data[str(cpu) + 'addr'] = addr
203                 return                            203                 return
204                                                   204 
205         # If cannot find dso so cannot dump as    205         # If cannot find dso so cannot dump assembler, bail out
206         if (dso == '[unknown]'):                  206         if (dso == '[unknown]'):
207                 return                            207                 return
208                                                   208 
209         # Validate dso start and end addresses    209         # Validate dso start and end addresses
210         if ((dso_start == '[unknown]') or (dso    210         if ((dso_start == '[unknown]') or (dso_end == '[unknown]')):
211                 print("Failed to find valid ds    211                 print("Failed to find valid dso map for dso %s" % dso)
212                 return                            212                 return
213                                                   213 
214         if (name[0:12] == "instructions"):        214         if (name[0:12] == "instructions"):
215                 print_srccode(comm, param_dict    215                 print_srccode(comm, param_dict, sample, symbol, dso)
216                 return                            216                 return
217                                                   217 
218         # Don't proceed if this event is not a    218         # Don't proceed if this event is not a branch sample, .
219         if (name[0:8] != "branches"):             219         if (name[0:8] != "branches"):
220                 return                            220                 return
221                                                   221 
222         # The format for packet is:               222         # The format for packet is:
223         #                                         223         #
224         #                 +------------+------    224         #                 +------------+------------+------------+
225         #  sample_prev:   |    addr    |    ip    225         #  sample_prev:   |    addr    |    ip      |    cpu     |
226         #                 +------------+------    226         #                 +------------+------------+------------+
227         #  sample_next:   |    addr    |    ip    227         #  sample_next:   |    addr    |    ip      |    cpu     |
228         #                 +------------+------    228         #                 +------------+------------+------------+
229         #                                         229         #
230         # We need to combine the two continuou    230         # We need to combine the two continuous packets to get the instruction
231         # range for sample_prev::cpu:             231         # range for sample_prev::cpu:
232         #                                         232         #
233         #     [ sample_prev::addr .. sample_ne    233         #     [ sample_prev::addr .. sample_next::ip ]
234         #                                         234         #
235         # For this purose, sample_prev::addr i    235         # For this purose, sample_prev::addr is stored into cpu_data structure
236         # and read back for 'start_addr' when     236         # and read back for 'start_addr' when the new packet comes, and we need
237         # to use sample_next::ip to calculate     237         # to use sample_next::ip to calculate 'stop_addr', plusing extra 4 for
238         # 'stop_addr' is for the sake of objdu    238         # 'stop_addr' is for the sake of objdump so the final assembler dump can
239         # include last instruction for sample_    239         # include last instruction for sample_next::ip.
240         start_addr = cpu_data[str(cpu) + 'addr    240         start_addr = cpu_data[str(cpu) + 'addr']
241         stop_addr  = ip + 4                       241         stop_addr  = ip + 4
242                                                   242 
243         # Record for previous sample packet       243         # Record for previous sample packet
244         cpu_data[str(cpu) + 'addr'] = addr        244         cpu_data[str(cpu) + 'addr'] = addr
245                                                   245 
246         # Handle CS_ETM_TRACE_ON packet if sta    246         # Handle CS_ETM_TRACE_ON packet if start_addr=0 and stop_addr=4
247         if (start_addr == 0 and stop_addr == 4    247         if (start_addr == 0 and stop_addr == 4):
248                 print("CPU%d: CS_ETM_TRACE_ON     248                 print("CPU%d: CS_ETM_TRACE_ON packet is inserted" % cpu)
249                 return                            249                 return
250                                                   250 
251         if (start_addr < int(dso_start) or sta    251         if (start_addr < int(dso_start) or start_addr > int(dso_end)):
252                 print("Start address 0x%x is o    252                 print("Start address 0x%x is out of range [ 0x%x .. 0x%x ] for dso %s" % (start_addr, int(dso_start), int(dso_end), dso))
253                 return                            253                 return
254                                                   254 
255         if (stop_addr < int(dso_start) or stop    255         if (stop_addr < int(dso_start) or stop_addr > int(dso_end)):
256                 print("Stop address 0x%x is ou    256                 print("Stop address 0x%x is out of range [ 0x%x .. 0x%x ] for dso %s" % (stop_addr, int(dso_start), int(dso_end), dso))
257                 return                            257                 return
258                                                   258 
259         if (options.objdump_name != None):        259         if (options.objdump_name != None):
260                 # It doesn't need to decrease     260                 # It doesn't need to decrease virtual memory offset for disassembly
261                 # for kernel dso and executabl    261                 # for kernel dso and executable file dso, so in this case we set
262                 # vm_start to zero.               262                 # vm_start to zero.
263                 if (dso == "[kernel.kallsyms]"    263                 if (dso == "[kernel.kallsyms]" or dso_start == 0x400000):
264                         dso_vm_start = 0          264                         dso_vm_start = 0
265                 else:                             265                 else:
266                         dso_vm_start = int(dso    266                         dso_vm_start = int(dso_start)
267                                                   267 
268                 dso_fname = get_dso_file_path(    268                 dso_fname = get_dso_file_path(dso, dso_bid)
269                 if path.exists(dso_fname):        269                 if path.exists(dso_fname):
270                         print_disam(dso_fname,    270                         print_disam(dso_fname, dso_vm_start, start_addr, stop_addr)
271                 else:                             271                 else:
272                         print("Failed to find     272                         print("Failed to find dso %s for address range [ 0x%x .. 0x%x ]" % (dso, start_addr, stop_addr))
273                                                   273 
274         print_srccode(comm, param_dict, sample    274         print_srccode(comm, param_dict, sample, symbol, dso)
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php