aboutsummaryrefslogtreecommitdiff
path: root/libraries/libprof/profile-runner.py
blob: b96d6b6fba8b307c63dd84def361320080f83741 (plain) (blame)
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
#!/usr/bin/env python3

"""
Tool to run some test code under the profiler on the Cryptech Alpha.

This assumes that the HSM code was built with DO_PROFILING=1, and
requires an ST-LINK programmer and the Python pexpect package.
"""

import subprocess
import argparse
import pexpect
import atexit
import time
import sys
import os

parser = argparse.ArgumentParser(description = __doc__,
                                 formatter_class = argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--hsm-elf",
                    default = os.path.expanduser("~/git.cryptech.is/sw/stm32/projects/hsm/hsm.elf"),
                    help    = "where you keep the profiled hsm.elf binary")
parser.add_argument("--openocd-config",
                    default = "/usr/share/openocd/scripts/board/st_nucleo_f401re.cfg",
                    help    = "OpenOCD ST-LINK configuration file ")
parser.add_argument("--gmon-output",
                    default = "profile-runner.gmon",
                    help    = "where to leave raw profiler output")
parser.add_argument("--gprof-output", type = argparse.FileType("w"),
                    default = "profile-runner.gprof",
                    help    = "where to leave profiler output after processing with gprof")
parser.add_argument("--user",
                    default = "wheel",
                    help    = "user name for logging in on the HSM console")
parser.add_argument("--pin",
                    default = "fnord",
                    help    = "PIN for logging in on the HSM console")
parser.add_argument("command", nargs = 1,
                    help    = "test program to run with profiling")
parser.add_argument("arguments", nargs = argparse.REMAINDER,
                    help    = argparse.SUPPRESS)
args = parser.parse_args()

openocd = subprocess.Popen(("openocd", "-f", args.openocd_config))
atexit.register(openocd.terminate)

time.sleep(5)

telnet = pexpect.spawn("telnet localhost 4444")
telnet.expect(">")
telnet.sendline("arm semihosting enable")
telnet.expect(">")
telnet.sendline("exit")

console = pexpect.spawn("cryptech_console")
console.sendline("")
if console.expect(["cryptech>", "Username:"]):
    console.sendline(args.user)
    console.expect("Password:")
    console.sendline(args.pin)
    console.expect("cryptech>")
console.sendline("profile start")
console.expect("cryptech>")

cmd = args.command + args.arguments
sys.stderr.write("Running command: {}\n".format(" ".join(cmd)))
subprocess.check_call(cmd)

console.sendline("profile stop")
console.expect("cryptech>", timeout = 900)
os.rename("gmon.out", args.gmon_output)

subprocess.check_call(("gprof", args.hsm_elf, args.gmon_output), stdout = args.gprof_output)