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
23import sys
24from simpleperf_report_lib import *
25
26
27def report_sample(record_file, symfs_dir, kallsyms_file=None):
28    """ read record_file, and print each sample"""
29    lib = ReportLib()
30
31    lib.ShowIpForUnknownSymbol()
32    if symfs_dir is not None:
33        lib.SetSymfs(symfs_dir)
34    if record_file is not None:
35        lib.SetRecordFile(record_file)
36    if kallsyms_file is not None:
37        lib.SetKallsymsFile(kallsyms_file)
38
39    while True:
40        sample = lib.GetNextSample()
41        if sample is None:
42            lib.Close()
43            break
44        event = lib.GetEventOfCurrentSample()
45        symbol = lib.GetSymbolOfCurrentSample()
46        callchain = lib.GetCallChainOfCurrentSample()
47
48        sec = sample.time / 1000000000
49        usec = (sample.time - sec * 1000000000) / 1000
50        print('%s\t%d [%03d] %d.%d:\t\t%d %s:' % (sample.thread_comm,
51                                                  sample.tid, sample.cpu, sec,
52                                                  usec, sample.period, event.name))
53        print('%16x\t%s (%s)' % (sample.ip, symbol.symbol_name, symbol.dso_name))
54        for i in range(callchain.nr):
55            entry = callchain.entries[i]
56            print('%16x\t%s (%s)' % (entry.ip, entry.symbol.symbol_name, entry.symbol.dso_name))
57        print('')
58
59
60if __name__ == '__main__':
61    parser = argparse.ArgumentParser(description='Report samples in perf.data.')
62    parser.add_argument('--symfs',
63                        help='Set the path to find binaries with symbols and debug info.')
64    parser.add_argument('--kallsyms', help='Set the path to find kernel symbols.')
65    parser.add_argument('record_file', nargs='?', default='perf.data',
66                        help='Default is perf.data.')
67    args = parser.parse_args()
68    report_sample(args.record_file, args.symfs, args.kallsyms)
69