2020-09-22 02:15:21 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# tools/parsecallstack.py
|
2020-06-01 20:13:57 +08: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 os
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
|
2020-09-22 02:15:21 +08:00
|
|
|
parser = argparse.ArgumentParser("""
|
|
|
|
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 20:48:46 +08:00
|
|
|
in Trace32, load the symbol according to the indication, the call
|
2020-09-22 02:15:21 +08:00
|
|
|
stack will pop up.\n
|
2021-02-25 20:48:46 +08:00
|
|
|
Trace32 software is available at: https://www.lauterbach.com
|
2020-09-22 02:15:21 +08:00
|
|
|
""")
|
2020-06-01 20:13:57 +08:00
|
|
|
|
|
|
|
parser.add_argument("-f", "--filename", action = "store",
|
|
|
|
help = "log file with registers and stack information")
|
|
|
|
parser.add_argument("-c", "--cputype", action = "store",
|
2020-09-22 02:15:21 +08:00
|
|
|
help = '''It supports ARM family CPU such as:
|
|
|
|
"CortexM0" "CortexM1" "CortexM3" "CortexM4"
|
|
|
|
"CortexM7" "CortexM23" "CortexM33" "CortexM35P"
|
|
|
|
"CortexR5" "CortexR7" "CortexA5" "CortexA7"''')
|
|
|
|
|
|
|
|
return parser.parse_args()
|
2020-06-01 20:13:57 +08:00
|
|
|
|
|
|
|
def get_regs(filename):
|
|
|
|
|
|
|
|
reglist = []
|
|
|
|
with open(filename, mode='r') as fl:
|
|
|
|
for line in fl:
|
|
|
|
lst = line.strip('\n').split(' ')
|
|
|
|
if "R0:" in lst:
|
|
|
|
reglist = lst[-8:]
|
|
|
|
if "R8:" in lst:
|
|
|
|
reglist += lst[-8:]
|
|
|
|
|
|
|
|
return reglist
|
|
|
|
|
|
|
|
def get_stackvalue(filename):
|
|
|
|
|
|
|
|
stackvalue = []
|
|
|
|
first = 1
|
|
|
|
with open(filename, mode='r') as fl:
|
|
|
|
for line in fl:
|
|
|
|
lst = line.strip('\n').split(' ')
|
|
|
|
if "up_stackdump:" in lst:
|
|
|
|
if first == 1:
|
|
|
|
first += 1
|
|
|
|
# strip ":" of sp
|
|
|
|
sp = lst[-9].strip(':')
|
|
|
|
# The first item is the sp to restore the stack.
|
|
|
|
stackvalue.append(sp)
|
|
|
|
stackvalue += lst[-8:]
|
|
|
|
|
|
|
|
return stackvalue
|
|
|
|
|
|
|
|
def generate_cmm(cpu, regs, stackvalue):
|
|
|
|
|
2020-09-22 02:15:21 +08:00
|
|
|
filename = os.path.join(os.getcwd(), 'callstack.cmm')
|
2020-06-01 20:13:57 +08:00
|
|
|
with open(filename, mode='w') as fl:
|
|
|
|
# Select the CPU and symbol.
|
2020-09-22 02:15:21 +08:00
|
|
|
fl.write("SYStem.CPU %d\n" % cpu)
|
2020-06-01 20:13:57 +08: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-22 02:15:21 +08:00
|
|
|
fl.write("Register.Set R%d 0x%s\n" % num, regs[num])
|
|
|
|
fl.write("\n")
|
2020-06-01 20:13:57 +08:00
|
|
|
|
|
|
|
# Recover the value in stack.
|
2020-09-22 02:15:21 +08:00
|
|
|
sp = int(stackvalue[0], 16)
|
2020-06-01 20:13:57 +08:00
|
|
|
for num in range(len(stackvalue) - 1):
|
|
|
|
address = hex(sp + num * 4)
|
|
|
|
value = stackvalue[num + 1]
|
2020-09-22 02:15:21 +08:00
|
|
|
fl.write("Data.Set ZSD:%d %%LE %%Long 0x%d\n" % address, value)
|
|
|
|
fl.write("\n")
|
2020-06-01 20:13:57 +08:00
|
|
|
|
|
|
|
# Show the call stack.
|
2020-09-22 02:15:21 +08:00
|
|
|
fl.write("data.view %%sYmbol.long %x\n" % sp)
|
|
|
|
fl.write("frame.view /Locals /Caller\n")
|
2020-06-01 20:13:57 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-09-22 02:15:21 +08:00
|
|
|
args = parse_args()
|
|
|
|
regs = get_regs(args.filename)
|
|
|
|
stackvalue = get_stackvalue(args.filename)
|
|
|
|
generate_cmm(args.cpu, regs, stackvalue)
|