File: ftrace_format.py

package info (click to toggle)
optee-os 4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,560 kB
  • sloc: ansic: 441,914; asm: 12,903; python: 3,719; makefile: 1,676; sh: 238
file content (82 lines) | stat: -rwxr-xr-x 2,202 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2023, Linaro Limited
#
# Converts a ftrace binary file to text. The input file has the following
# format:
#
#  <ASCII text> <zero or more nul bytes> FTRACE\x00\x01 <binary data>...
#
# <binary data> is an array of 64-bit integers.
# - When the topmost byte is 0, the entry indicates a function return and the
# remaining bytes are a duration in nanoseconds.
# - A non-zero value is a stack depth, indicating a function entry, and the
# remaining bytes are the function's address.

import sys


line = ""
curr_depth = 0


def usage():
    print(f"Usage: {sys.argv[0]} ftrace.out")
    print("Converts a ftrace file to text. Output is written to stdout.")
    sys.exit(0)


def format_time(ns):
    if ns < 1000000:
        us = ns / 1000
        return f"{us:7.3f} us"
    elif ns < 1000000000:
        ms = ns / 1000000
        return f"{ms:7.3f} ms"
    else:
        s = ns / 1000000000
        return f"{s:7.3f} s "


def display(depth, val):
    global line, curr_depth
    if depth != 0:
        curr_depth = depth
        if line != "":
            line = line.replace("TIME", " " * 10) + " {"
            print(line)
            line = ""
        line = f" TIME | {depth:3} | " + " " * depth + f"0x{val:016x}()"
    else:
        if line != "":
            line = line.replace("TIME", format_time(val))
            print(line)
            line = ""
        else:
            if curr_depth != 0:
                curr_depth = curr_depth - 1
                print(" " + format_time(val) + f" | {curr_depth:3} | " +
                      " " * curr_depth + "}")


def main():
    if len(sys.argv) < 2:
        usage()
    with open(sys.argv[1], 'rb') as f:
        s = f.read()
    magic = s.find(b'FTRACE\x00\x01')
    if magic == -1:
        print("Magic not found", file=sys.stderr)
        sys.exit(1)
    print(s[:magic].rstrip(b'\x00').decode())
    s = s[magic + 8:]
    for i in range(0, len(s), 8):
        elem = int.from_bytes(s[i:i + 8], byteorder="little", signed=False)
        depth = elem >> 56
        val = elem & 0xFFFFFFFFFFFFFF
        display(depth, val)


if __name__ == "__main__":
    main()