1#!/usr/bin/env python
2#
3# Copyright (C) 2016 The Android Open Source Project
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
18"""report_sample.py: report samples in the same format as `perf script`.
19"""
20
21from __future__ import print_function
22import argparse
23from simpleperf_report_lib import ReportLib
24
25
26def report_sample(record_file, symfs_dir, kallsyms_file, show_tracing_data):
27    """ read record_file, and print each sample"""
28    lib = ReportLib()
29
30    lib.ShowIpForUnknownSymbol()
31    if symfs_dir is not None:
32        lib.SetSymfs(symfs_dir)
33    if record_file is not None:
34        lib.SetRecordFile(record_file)
35    if kallsyms_file is not None:
36        lib.SetKallsymsFile(kallsyms_file)
37
38    while True:
39        sample = lib.GetNextSample()
40        if sample is None:
41            lib.Close()
42            break
43        event = lib.GetEventOfCurrentSample()
44        symbol = lib.GetSymbolOfCurrentSample()
45        callchain = lib.GetCallChainOfCurrentSample()
46
47        sec = sample.time / 1000000000
48        usec = (sample.time - sec * 1000000000) / 1000
49        print('%s\t%d [%03d] %d.%06d:\t\t%d %s:' % (sample.thread_comm,
50                                                    sample.tid, sample.cpu, sec,
51                                                    usec, sample.period, event.name))
52        print('%16x\t%s (%s)' % (sample.ip, symbol.symbol_name, symbol.dso_name))
53        for i in range(callchain.nr):
54            entry = callchain.entries[i]
55            print('%16x\t%s (%s)' % (entry.ip, entry.symbol.symbol_name, entry.symbol.dso_name))
56        if show_tracing_data:
57            data = lib.GetTracingDataOfCurrentSample()
58            if data:
59                print('\ttracing data:')
60                for key, value in data.items():
61                    print('\t\t%s : %s' % (key, value))
62        print('')
63
64
65def main():
66    parser = argparse.ArgumentParser(description='Report samples in perf.data.')
67    parser.add_argument('--symfs',
68                        help='Set the path to find binaries with symbols and debug info.')
69    parser.add_argument('--kallsyms', help='Set the path to find kernel symbols.')
70    parser.add_argument('record_file', nargs='?', default='perf.data',
71                        help='Default is perf.data.')
72    parser.add_argument('--show_tracing_data', action='store_true', help='print tracing data.')
73    args = parser.parse_args()
74    report_sample(args.record_file, args.symfs, args.kallsyms, args.show_tracing_data)
75
76
77if __name__ == '__main__':
78    main()
79