2023-02-19 04:33:21 +01:00
|
|
|
#!/usr/bin/env python3
|
2020-09-21 20:15:21 +02:00
|
|
|
# tools/parsecallstack.py
|
2020-06-01 14:13:57 +02:00
|
|
|
#
|
2024-09-10 14:15:58 +02:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
2020-06-01 14:13:57 +02:00
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership. The
|
|
|
|
# ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
# "License"); you may not use this file except in compliance with the
|
|
|
|
# License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
#
|
|
|
|
|
|
|
|
import argparse
|
2021-04-09 18:35:47 +02:00
|
|
|
import os
|
2020-06-01 14:13:57 +02:00
|
|
|
|
2021-04-05 02:06:30 +02:00
|
|
|
|
2020-06-01 14:13:57 +02:00
|
|
|
def parse_args():
|
|
|
|
|
2021-04-05 02:06:30 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
"""
|
2020-09-21 20:15:21 +02:00
|
|
|
parsecallstack.py -c CPUTYPE -f FILENAME\n\
|
|
|
|
This file can get the call stack when you get the log with the
|
|
|
|
register values from R0 to R15, together with the stack dump.\n
|
|
|
|
Then you will get a file with name callstack.cmm, run this file
|
2021-02-25 13:48:46 +01:00
|
|
|
in Trace32, load the symbol according to the indication, the call
|
2020-09-21 20:15:21 +02:00
|
|
|
stack will pop up.\n
|
2021-02-25 13:48:46 +01:00
|
|
|
Trace32 software is available at: https://www.lauterbach.com
|
2021-04-05 02:06:30 +02:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"-f",
|
|
|
|
"--filename",
|
|
|
|
action="store",
|
|
|
|
help="log file with registers and stack information",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-c",
|
|
|
|
"--cputype",
|
|
|
|
action="store",
|
|
|
|
help='''It supports ARM family CPU such as:
|
2020-09-21 20:15:21 +02:00
|
|
|
"CortexM0" "CortexM1" "CortexM3" "CortexM4"
|
|
|
|
"CortexM7" "CortexM23" "CortexM33" "CortexM35P"
|
2021-04-05 02:06:30 +02:00
|
|
|
"CortexR5" "CortexR7" "CortexA5" "CortexA7"''',
|
|
|
|
)
|
2020-09-21 20:15:21 +02:00
|
|
|
|
|
|
|
return parser.parse_args()
|
2020-06-01 14:13:57 +02:00
|
|
|
|
2021-04-05 02:06:30 +02:00
|
|
|
|
2020-06-01 14:13:57 +02:00
|
|
|
def get_regs(filename):
|
|
|
|
|
|
|
|
reglist = []
|
2021-04-05 02:06:30 +02:00
|
|
|
with open(filename, mode="r") as fl:
|
2020-06-01 14:13:57 +02:00
|
|
|
for line in fl:
|
2021-04-05 02:06:30 +02:00
|
|
|
lst = line.strip("\n").split(" ")
|
2020-06-01 14:13:57 +02:00
|
|
|
if "R0:" in lst:
|
|
|
|
reglist = lst[-8:]
|
|
|
|
if "R8:" in lst:
|
|
|
|
reglist += lst[-8:]
|
|
|
|
|
|
|
|
return reglist
|
|
|
|
|
2021-04-05 02:06:30 +02:00
|
|
|
|
2020-06-01 14:13:57 +02:00
|
|
|
def get_stackvalue(filename):
|
|
|
|
|
|
|
|
stackvalue = []
|
|
|
|
first = 1
|
2021-04-05 02:06:30 +02:00
|
|
|
with open(filename, mode="r") as fl:
|
2020-06-01 14:13:57 +02:00
|
|
|
for line in fl:
|
2021-04-05 02:06:30 +02:00
|
|
|
lst = line.strip("\n").split(" ")
|
2020-06-01 14:13:57 +02:00
|
|
|
if "up_stackdump:" in lst:
|
|
|
|
if first == 1:
|
|
|
|
first += 1
|
|
|
|
# strip ":" of sp
|
2021-04-05 02:06:30 +02:00
|
|
|
sp = lst[-9].strip(":")
|
2020-06-01 14:13:57 +02:00
|
|
|
# The first item is the sp to restore the stack.
|
|
|
|
stackvalue.append(sp)
|
|
|
|
stackvalue += lst[-8:]
|
|
|
|
|
|
|
|
return stackvalue
|
|
|
|
|
2021-04-05 02:06:30 +02:00
|
|
|
|
2020-06-01 14:13:57 +02:00
|
|
|
def generate_cmm(cpu, regs, stackvalue):
|
|
|
|
|
2021-04-05 02:06:30 +02:00
|
|
|
filename = os.path.join(os.getcwd(), "callstack.cmm")
|
|
|
|
with open(filename, mode="w") as fl:
|
2020-06-01 14:13:57 +02:00
|
|
|
# Select the CPU and symbol.
|
2020-09-21 20:15:21 +02:00
|
|
|
fl.write("SYStem.CPU %d\n" % cpu)
|
2020-06-01 14:13:57 +02:00
|
|
|
fl.write("SYS.M UP\n")
|
|
|
|
fl.write("Data.LOAD *\n")
|
|
|
|
fl.write("\n")
|
|
|
|
|
|
|
|
# Set R0-R15.
|
|
|
|
for num in range(len(regs)):
|
2020-09-21 20:15:21 +02:00
|
|
|
fl.write("Register.Set R%d 0x%s\n" % num, regs[num])
|
|
|
|
fl.write("\n")
|
2020-06-01 14:13:57 +02:00
|
|
|
|
|
|
|
# Recover the value in stack.
|
2020-09-21 20:15:21 +02:00
|
|
|
sp = int(stackvalue[0], 16)
|
2020-06-01 14:13:57 +02:00
|
|
|
for num in range(len(stackvalue) - 1):
|
|
|
|
address = hex(sp + num * 4)
|
|
|
|
value = stackvalue[num + 1]
|
2020-09-21 20:15:21 +02:00
|
|
|
fl.write("Data.Set ZSD:%d %%LE %%Long 0x%d\n" % address, value)
|
|
|
|
fl.write("\n")
|
2020-06-01 14:13:57 +02:00
|
|
|
|
|
|
|
# Show the call stack.
|
2020-09-21 20:15:21 +02:00
|
|
|
fl.write("data.view %%sYmbol.long %x\n" % sp)
|
|
|
|
fl.write("frame.view /Locals /Caller\n")
|
2020-06-01 14:13:57 +02:00
|
|
|
|
2021-04-05 02:06:30 +02:00
|
|
|
|
2020-06-01 14:13:57 +02:00
|
|
|
if __name__ == "__main__":
|
2020-09-21 20:15:21 +02:00
|
|
|
args = parse_args()
|
|
|
|
regs = get_regs(args.filename)
|
|
|
|
stackvalue = get_stackvalue(args.filename)
|
|
|
|
generate_cmm(args.cpu, regs, stackvalue)
|