Bootstrap

stm32出现hardfault-自动化分析map文件

可通过如下python脚本进行分析:

import os

import sys

#在此输入HardFault_Handler时打印的PC指针

address = 0x20006980

#以下代码不要要更改

def is_hex(data):

    try:

        int(data, 16)

        return True

    except ValueError:

        return False

def find_line_number(map_file, address):

    line_number = None

    linestring = ""

    with open(map_file, 'r') as f:

        line_num = 0

        lines = f.readlines()

        for line in lines:

            line_num = line_num + 1

            line = line.replace('\n', '').strip()

            parts = line.split()

            if len(parts) >= 3:

                if is_hex(parts[1]) :

                    func_address = int(parts[1], 16)

                    if func_address >= address:

                        line_number = str(line_num - 1) if func_address > address else str(line_num)

                        break

                elif is_hex(parts[2]) :

                    func_address = int(parts[2], 16)

                    if func_address >= address:

                        line_number = str(line_num - 1) if func_address > address else str(line_num)

                        break

            linestring = line

    return line_number, (linestring if func_address > address else line)

if __name__ == "__main__":

    script_path = os.path.dirname(os.path.realpath(__file__))

    os.chdir(script_path)

    map_file = '../Listings/SC_Func.map'

    line_number, linestring = find_line_number(map_file, address)

    if line_number:

        print(f"Function at address: {hex(address)}, LineNumber: {line_number}")

        print(f"Content:  {linestring}")

    else:

        print(f"No function found at address {hex(address)}")

;