From a8e812dea5567a9ec582e3c14cb90c0090d3f052 Mon Sep 17 00:00:00 2001 From: oliviermartin Date: Mon, 22 Oct 2012 14:01:38 +0000 Subject: ArmPlatformPkg/Scripts: Added ARM DS-5 scripts to debug UEFI These scripts allow to automatically load the symbols using either the report file created by the BaseTools or using the memory regions defined in the arguments. This is the scripts for DS-5 prior to DS5 v5.12. Signed-off-by: Olivier Martin git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13874 6f19259b-4bc3-4df7-8a09-765794883524 --- ArmPlatformPkg/Scripts/Ds5/build_report.py | 54 +++++ ArmPlatformPkg/Scripts/Ds5/cmd_load_symbols.py | 100 +++++++++ ArmPlatformPkg/Scripts/Ds5/edk2_debugger.py | 212 ++++++++++++++++++ ArmPlatformPkg/Scripts/Ds5/firmware_volume.py | 299 +++++++++++++++++++++++++ ArmPlatformPkg/Scripts/Ds5/system_table.py | 127 +++++++++++ 5 files changed, 792 insertions(+) create mode 100644 ArmPlatformPkg/Scripts/Ds5/build_report.py create mode 100644 ArmPlatformPkg/Scripts/Ds5/cmd_load_symbols.py create mode 100644 ArmPlatformPkg/Scripts/Ds5/edk2_debugger.py create mode 100644 ArmPlatformPkg/Scripts/Ds5/firmware_volume.py create mode 100644 ArmPlatformPkg/Scripts/Ds5/system_table.py (limited to 'ArmPlatformPkg/Scripts/Ds5') diff --git a/ArmPlatformPkg/Scripts/Ds5/build_report.py b/ArmPlatformPkg/Scripts/Ds5/build_report.py new file mode 100644 index 0000000000..3805c0b43b --- /dev/null +++ b/ArmPlatformPkg/Scripts/Ds5/build_report.py @@ -0,0 +1,54 @@ +# +# Copyright (c) 2011-2012, ARM Limited. All rights reserved. +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the BSD License +# which accompanies this distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# + +import re + +class BuildReport: + PCDs = {} + + def parse_platform_summary(self, file): + pass + + def parse_pcd_report(self, report_file): + pcd_reg = re.compile(" (\*P|\*F|\*M| ) (\w+)(\ +)\: (.*) \((\w+)\) = (.*)\n") + + for line in report_file.xreadlines(): + stripped_line = line.strip() + if re.match("\<=+\>", stripped_line): + return + elif re.match("g.*Guid", stripped_line): + guid = stripped_line + self.PCDs[guid] = {} + else: + m = pcd_reg.match(line) + if m: + self.PCDs[guid][m.group(2)] = (m.group(6).strip(),m.group(5)) + + def parse_firmware_device(self, file): + pass + + def parse_module_summary(self, file): + #print "Module Summary" + pass + + CONST_SECTION_HEADERS = [('Platform Summary', parse_platform_summary), + ('Platform Configuration Database Report',parse_pcd_report), + ('Firmware Device (FD)',parse_firmware_device), + ('Module Summary',parse_module_summary)] + + def __init__(self, filename = 'report.log'): + report_file = open(filename, 'r') + for line in report_file.xreadlines(): + for section_header in BuildReport.CONST_SECTION_HEADERS: + if line.strip() == section_header[0]: + section_header[1](self, report_file) + #print self.PCDs diff --git a/ArmPlatformPkg/Scripts/Ds5/cmd_load_symbols.py b/ArmPlatformPkg/Scripts/Ds5/cmd_load_symbols.py new file mode 100644 index 0000000000..424eb63cbb --- /dev/null +++ b/ArmPlatformPkg/Scripts/Ds5/cmd_load_symbols.py @@ -0,0 +1,100 @@ +# +# Copyright (c) 2011-2012, ARM Limited. All rights reserved. +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the BSD License +# which accompanies this distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# + +from arm_ds.debugger_v1 import Debugger +from arm_ds.debugger_v1 import DebugException + +import re, sys, getopt + +import edk2_debugger + +# Reload external classes +reload(edk2_debugger) + +def usage(): + print "-a,--all: Load all symbols" + print "-l,--report=: Filename for the EDK2 report log" + print "-m,--sysmem=(base,size): System Memory region" + print "-f,--fv=(base,size): Firmware region" + print "-r,--rom=(base,size): ROM region" + +load_all = False +report_file = None +regions = [] +opts,args = getopt.getopt(sys.argv[1:], "har:vm:vr:vf:v", ["help","all","report=","sysmem=","rom=","fv="]) +if (opts is None) or (not opts): + report_file = '../../../report.log' +else: + region_reg = re.compile("\((.*),(.*)\)") + base_reg = re.compile("(.*)") + + for o,a in opts: + region_type = None + regex = None + m = None + if o in ("-h","--help"): + usage() + sys.exit() + elif o in ("-a","--all"): + load_all = True + elif o in ("-l","--report"): + report_file = a + elif o in ("-m","--sysmem"): + region_type = edk2_debugger.ArmPlatformDebugger.REGION_TYPE_SYSMEM + regex = region_reg + elif o in ("-f","--fv"): + region_type = edk2_debugger.ArmPlatformDebugger.REGION_TYPE_FV + regex = region_reg + elif o in ("-r","--rom"): + region_type = edk2_debugger.ArmPlatformDebugger.REGION_TYPE_ROM + regex = region_reg + else: + assert False, "Unhandled option" + + if region_type: + m = regex.match(a) + if m: + if regex.groups == 1: + regions.append((region_type,int(m.group(1),0),0)) + else: + regions.append((region_type,int(m.group(1),0),int(m.group(2),0))) + else: + if regex.groups == 1: + raise Exception('cmd_load_symbols', "Expect a base address") + else: + raise Exception('cmd_load_symbols', "Expect a region format as (base,size)") + +# Debugger object for accessing the debugger +debugger = Debugger() + +# Initialisation commands +ec = debugger.getExecutionContext(0) +ec.getExecutionService().stop() +ec.getExecutionService().waitForStop() +# in case the execution context reference is out of date +ec = debugger.getExecutionContext(0) + +armplatform_debugger = edk2_debugger.ArmPlatformDebugger(ec, report_file, regions) + +try: + armplatform_debugger = edk2_debugger.ArmPlatformDebugger(ec, report_file, regions) + + if load_all: + armplatform_debugger.load_all_symbols() + else: + armplatform_debugger.load_current_symbols() +except IOError, (ErrorNumber, ErrorMessage): + print "Error: %s" % ErrorMessage +except Exception, (ErrorClass, ErrorMessage): + print "Error(%s): %s" % (ErrorClass, ErrorMessage) +except DebugException, de: + print "DebugError: %s" % (de.getMessage()) diff --git a/ArmPlatformPkg/Scripts/Ds5/edk2_debugger.py b/ArmPlatformPkg/Scripts/Ds5/edk2_debugger.py new file mode 100644 index 0000000000..f922a7283e --- /dev/null +++ b/ArmPlatformPkg/Scripts/Ds5/edk2_debugger.py @@ -0,0 +1,212 @@ +# +# Copyright (c) 2011-2012, ARM Limited. All rights reserved. +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the BSD License +# which accompanies this distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# + +import os + +import firmware_volume +import build_report +import system_table + +# Reload external classes +reload(firmware_volume) +reload(build_report) +reload(system_table) + +def readMem32(executionContext, address): + bytes = executionContext.getMemoryService().read(address, 4, 32) + return struct.unpack('= debug_info[0]) and (pc < debug_info[0] + debug_info[1]): + found = True + if found == False: + info = self.firmware_volumes[fv_base].load_symbols_at(pc) + debug_infos.append(info) + + #self.firmware_volumes[fv_base].load_symbols_at(pc) + elif self.in_sysmem(pc): + debug_infos = [] + + if self.system_table is None: + # Find the System Table + self.system_table = system_table.SystemTable(self.ec, self.platform.sysmembase, self.platform.sysmemsize) + + # Find the Debug Info Table + debug_info_table_base = self.system_table.get_configuration_table(system_table.DebugInfoTable.CONST_DEBUG_INFO_TABLE_GUID) + self.debug_info_table = system_table.DebugInfoTable(self.ec, debug_info_table_base) + + stack_frame = self.ec.getTopLevelStackFrame() + info = self.debug_info_table.load_symbols_at(int(stack_frame.getRegisterService().getValue('PC')) & 0xFFFFFFFF) + debug_infos.append(info) + while stack_frame.next() is not None: + stack_frame = stack_frame.next() + + # Stack frame attached to 'PC' + pc = int(stack_frame.getRegisterService().getValue('PC')) & 0xFFFFFFFF + + # Check if the symbols for this stack frame have already been loaded + found = False + for debug_info in debug_infos: + if (pc >= debug_info[0]) and (pc < debug_info[0] + debug_info[1]): + found = True + if found == False: + info = self.debug_info_table.load_symbols_at(pc) + debug_infos.append(info) + + #self.debug_info_table.load_symbols_at(pc) + else: + raise Exception('ArmPlatformDebugger', "Not supported region") + + def load_all_symbols(self): + # Load all the XIP symbols attached to the Firmware Volume + for (fv_base, fv_size) in self.platform.fvs: + if self.firmware_volumes.has_key(fv_base) == False: + self.firmware_volumes[fv_base] = firmware_volume.FirmwareVolume(self.ec, fv_base, fv_size) + self.firmware_volumes[fv_base].load_all_symbols() + + try: + # Load all symbols of module loaded into System Memory + if self.system_table is None: + # Find the System Table + self.system_table = system_table.SystemTable(self.ec, self.platform.sysmembase, self.platform.sysmemsize) + + + # Find the Debug Info Table + debug_info_table_base = self.system_table.get_configuration_table(system_table.DebugInfoTable.CONST_DEBUG_INFO_TABLE_GUID) + self.debug_info_table = system_table.DebugInfoTable(self.ec, debug_info_table_base) + + self.debug_info_table.load_all_symbols() + except: + # Debugger exception could be excepted if DRAM has not been initialized or if we have not started to run from DRAM yet + print "Note: no symbols have been found in System Memory (possible cause: the UEFI permanent memory has been installed yet)" + pass diff --git a/ArmPlatformPkg/Scripts/Ds5/firmware_volume.py b/ArmPlatformPkg/Scripts/Ds5/firmware_volume.py new file mode 100644 index 0000000000..01f8597e5f --- /dev/null +++ b/ArmPlatformPkg/Scripts/Ds5/firmware_volume.py @@ -0,0 +1,299 @@ +# +# Copyright (c) 2011-2012, ARM Limited. All rights reserved. +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the BSD License +# which accompanies this distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# + +from arm_ds.debugger_v1 import DebugException + +import struct +import string + +class EfiFileSection(object): + EFI_SECTION_PE32 = 0x10 + EFI_SECTION_PIC = 0x11 + EFI_SECTION_TE = 0x12 + + EFI_IMAGE_DEBUG_TYPE_CODEVIEW = 0x2 + + SIZEOF_EFI_FFS_FILE_HEADER = 0x28 + + def __init__(self, ec, base): + self.base = base + self.ec = ec + + def __str__(self): + return "FileSection(type:0x%X, size:0x%x)" % (self.get_type(), self.get_size()) + + def get_base(self): + return self.base + + def get_type(self): + return struct.unpack("B", self.ec.getMemoryService().read(self.base + 0x3, 1, 8))[0] + + def get_size(self): + return (struct.unpack(">= 1 + + return highest_bit + + def get_next_section(self, section=None): + if section == None: + if self.get_type() != FirmwareFile.EFI_FV_FILETYPE_FFS_MIN: + section_base = self.get_base() + 0x18; + else: + return None + else: + section_base = int(section.get_base() + section.get_size()) + + # Align to next 4 byte boundary + if (section_base & 0x3) != 0: + section_base = section_base + 0x4 - (section_base & 0x3) + + if section_base < self.get_base() + self.get_size(): + return EfiFileSection(self.ec, section_base) + else: + return None + +class FirmwareVolume: + CONST_FV_SIGNATURE = ('_','F','V','H') + EFI_FVB2_ERASE_POLARITY = 0x800 + + DebugInfos = [] + + def __init__(self, ec, fv_base, fv_size): + self.ec = ec + self.fv_base = fv_base + self.fv_size = fv_size + + try: + signature = struct.unpack("cccc", self.ec.getMemoryService().read(fv_base + 0x28, 4, 32)) + except DebugException: + raise Exception("FirmwareVolume", "Not possible to access the defined firmware volume at [0x%X,0x%X]. Could be the used build report does not correspond to your current debugging context." % (int(fv_base),int(fv_base+fv_size))) + if signature != FirmwareVolume.CONST_FV_SIGNATURE: + raise Exception("FirmwareVolume", "This is not a valid firmware volume") + + def get_size(self): + return self.ec.getMemoryService().readMemory32(self.fv_base + 0x20) + + def get_attributes(self): + return self.ec.getMemoryService().readMemory32(self.fv_base + 0x2C) + + def get_polarity(self): + attributes = self.get_attributes() + if attributes & FirmwareVolume.EFI_FVB2_ERASE_POLARITY: + return 1 + else: + return 0 + + def get_next_ffs(self, ffs=None): + if ffs == None: + # Get the offset of the first FFS file from the FV header + ffs_base = self.fv_base + self.ec.getMemoryService().readMemory16(self.fv_base + 0x30) + else: + # Goto the next FFS file + ffs_base = int(ffs.get_base() + ffs.get_size()) + + # Align to next 8 byte boundary + if (ffs_base & 0x7) != 0: + ffs_base = ffs_base + 0x8 - (ffs_base & 0x7) + + if ffs_base < self.fv_base + self.get_size(): + return FirmwareFile(self, ffs_base, self.ec) + else: + return None + + def get_debug_info(self): + self.DebugInfos = [] + + ffs = self.get_next_ffs() + while ffs != None: + section = ffs.get_next_section() + while section != None: + type = section.get_type() + if (type == EfiFileSection.EFI_SECTION_TE) or (type == EfiFileSection.EFI_SECTION_PE32): + self.DebugInfos.append((section.get_base(), section.get_size(), section.get_type())) + section = ffs.get_next_section(section) + ffs = self.get_next_ffs(ffs) + + def load_symbols_at(self, addr): + if self.DebugInfos == []: + self.get_debug_info() + + for debug_info in self.DebugInfos: + if (addr >= debug_info[0]) and (addr < debug_info[0] + debug_info[1]): + if debug_info[2] == EfiFileSection.EFI_SECTION_TE: + section = EfiSectionTE(self.ec, debug_info[0] + 0x4) + elif debug_info[2] == EfiFileSection.EFI_SECTION_PE32: + section = EfiSectionPE32(self.ec, debug_info[0] + 0x4) + else: + raise Exception('FirmwareVolume','Section Type not supported') + + edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase()) + + return debug_info + + def load_all_symbols(self): + if self.DebugInfos == []: + self.get_debug_info() + + for debug_info in self.DebugInfos: + if debug_info[2] == EfiFileSection.EFI_SECTION_TE: + section = EfiSectionTE(self.ec, debug_info[0] + 0x4) + elif debug_info[2] == EfiFileSection.EFI_SECTION_PE32: + section = EfiSectionPE32(self.ec, debug_info[0] + 0x4) + else: + continue + + edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase()) diff --git a/ArmPlatformPkg/Scripts/Ds5/system_table.py b/ArmPlatformPkg/Scripts/Ds5/system_table.py new file mode 100644 index 0000000000..f74554fa65 --- /dev/null +++ b/ArmPlatformPkg/Scripts/Ds5/system_table.py @@ -0,0 +1,127 @@ +# +# Copyright (c) 2011-2012, ARM Limited. All rights reserved. +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the BSD License +# which accompanies this distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# + +from arm_ds.debugger_v1 import DebugException + +import firmware_volume +import struct + +class DebugInfoTable: + CONST_DEBUG_INFO_TABLE_GUID = ( 0x49152E77L, 0x47641ADAL, 0xFE7AA2B7L, 0x8B5ED9FEL) + + DebugInfos = [] + + def __init__(self, ec, debug_info_table_header_offset): + self.ec = ec + self.base = debug_info_table_header_offset + + def get_debug_info(self): + count = self.ec.getMemoryService().readMemory32(self.base + 0x4) + debug_info_table_base = self.ec.getMemoryService().readMemory32(self.base + 0x8) + + self.DebugInfos = [] + + for i in range(0, count): + # Get the address of the structure EFI_DEBUG_IMAGE_INFO + debug_info = self.ec.getMemoryService().readMemory32(debug_info_table_base + (i * 4)) + if debug_info: + debug_info_type = self.ec.getMemoryService().readMemory32(debug_info) + # Normal Debug Info Type + if debug_info_type == 1: + # Get the base address of the structure EFI_LOADED_IMAGE_PROTOCOL + loaded_image_protocol = self.ec.getMemoryService().readMemory32(debug_info + 0x4) + + image_base = self.ec.getMemoryService().readMemory32(loaded_image_protocol + 0x20) + image_size = self.ec.getMemoryService().readMemory32(loaded_image_protocol + 0x28) + + self.DebugInfos.append((image_base,image_size)) + + # Return (base, size) + def load_symbols_at(self, addr): + if self.DebugInfos == []: + self.get_debug_info() + + found = False + for debug_info in self.DebugInfos: + if (addr >= debug_info[0]) and (addr < debug_info[0] + debug_info[1]): + section = firmware_volume.EfiSectionPE32(self.ec, debug_info[0]) + + edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase()) + + found = True + return debug_info + + if found == False: + raise Exception('DebugInfoTable','No symbol found at 0x%x' % addr) + + def load_all_symbols(self): + if self.DebugInfos == []: + self.get_debug_info() + + for debug_info in self.DebugInfos: + section = firmware_volume.EfiSectionPE32(self.ec, debug_info[0]) + + edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase()) + + def dump(self): + self.get_debug_info() + for debug_info in self.DebugInfos: + base_pe32 = debug_info[0] + section = firmware_volume.EfiSectionPE32(self.ec, base_pe32) + print section.get_debug_filepath() + +class SystemTable: + CONST_ST_SIGNATURE = ('I','B','I',' ','S','Y','S','T') + + def __init__(self, ec, membase, memsize): + self.membase = membase + self.memsize = memsize + self.ec = ec + + found = False + + # Start from the top of the memory + offset = self.membase + self.memsize + # Align to highest 4MB boundary + offset = offset & ~0x3FFFFF + # We should not have a System Table at the top of the System Memory + offset = offset - 0x400000 + + # Start at top and look on 4MB boundaries for system table ptr structure + while offset > self.membase: + try: + signature = struct.unpack("cccccccc", self.ec.getMemoryService().read(str(offset), 8, 32)) + except DebugException: + raise Exception('SystemTable','Fail to access System Memory. Ensure all the memory in the region [0x%x;0x%X] is accessible.' % (membase,membase+memsize)) + if signature == SystemTable.CONST_ST_SIGNATURE: + found = True + self.system_table_base = self.ec.getMemoryService().readMemory32(offset + 0x8) + break + offset = offset - 0x400000 + + if not found: + raise Exception('SystemTable','System Table not found in System Memory [0x%x;0x%X]' % (membase,membase+memsize)) + + def get_configuration_table(self, conf_table_guid): + # Number of configuration Table entry + conf_table_entry_count = self.ec.getMemoryService().readMemory32(self.system_table_base + 0x40) + + # Get location of the Configuration Table entries + conf_table_offset = self.ec.getMemoryService().readMemory32(self.system_table_base + 0x44) + + for i in range(0, conf_table_entry_count): + offset = conf_table_offset + (i * 0x14) + guid = struct.unpack("