From 2e2c8cc931d2db9d02e2226438577a85b8f75ead Mon Sep 17 00:00:00 2001 From: Daniel Leung Date: Thu, 7 Apr 2022 14:41:59 -0700 Subject: [PATCH] scripts: logging/dictionary/sys-t: log module info in collateral This updates the MIPI Sys-T collateral generation to include log modules in the XML file. This allows the parser to show where each log message is coming from. Note that the addition of Name propety in sys:Client is to clarify that this collateral is from Zephyr. Also note he addition of Mask property in syst:Guid is for parser to match this collateral to any incoming messages, as the module IDs are encoded in the set bits in the GUID. Without the mask, the parser will discard any messages with non-zero module IDs. This is based on the observation of the reference parser, systprint, where pseudo GUIDs are generated from the module/unit IDs (origin unit) in the incoming messages for matching with collaterals. Signed-off-by: Daniel Leung --- scripts/logging/dictionary/database_gen.py | 32 ++++++++++++------- .../dictionary_parser/log_database.py | 13 ++------ .../dictionary/dictionary_parser/mipi_syst.py | 5 +-- .../dictionary/dictionary_parser/utils.py | 24 ++++++++++++++ 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/scripts/logging/dictionary/database_gen.py b/scripts/logging/dictionary/database_gen.py index 994e3688f70..d70843e0ffb 100755 --- a/scripts/logging/dictionary/database_gen.py +++ b/scripts/logging/dictionary/database_gen.py @@ -23,6 +23,7 @@ import sys import dictionary_parser.log_database from dictionary_parser.log_database import LogDatabase from dictionary_parser.utils import extract_one_string_in_section +from dictionary_parser.utils import find_string_in_mappings import elftools from elftools.elf.constants import SH_FLAGS @@ -166,7 +167,7 @@ def find_log_const_symbols(elf): return ret_list -def parse_log_const_symbols(database, log_const_section, log_const_symbols): +def parse_log_const_symbols(database, log_const_section, log_const_symbols, string_mappings): """Find the log instances and map source IDs to names""" if database.is_tgt_little_endian(): formatter = "<" @@ -210,7 +211,9 @@ def parse_log_const_symbols(database, log_const_section, log_const_symbols): str_ptr, level = struct.unpack(formatter, datum) # Offset to rodata section for string - instance_name = database.find_string(str_ptr) + instance_name = find_string_in_mappings(string_mappings, str_ptr) + if instance_name is None: + instance_name = "unknown" logger.info("Found Log Instance: %s, level: %d", instance_name, level) @@ -255,7 +258,7 @@ def process_kconfigs(elf, database): kconfigs['CONFIG_LOG_TIMESTAMP_64BIT']) -def extract_logging_subsys_information(elf, database): +def extract_logging_subsys_information(elf, database, string_mappings): """ Extract logging subsys related information and store in database. @@ -274,7 +277,7 @@ def extract_logging_subsys_information(elf, database): # Find all "log_const_*" symbols and parse them log_const_symbols = find_log_const_symbols(elf) - parse_log_const_symbols(database, section_log_const, log_const_symbols) + parse_log_const_symbols(database, section_log_const, log_const_symbols, string_mappings) def is_die_attr_ref(attr): @@ -510,9 +513,7 @@ def extract_static_strings(elf, database, section_extraction=False): if one_str not in string_mappings: string_mappings[one_str] = rawstr_map[one_str] - if len(string_mappings) > 0: - database.set_string_mappings(string_mappings) - logger.info("Found %d strings", len(string_mappings)) + return string_mappings def main(): @@ -536,12 +537,10 @@ def main(): if args.json: logger.info("JSON Database file %s", args.json) section_extraction = True - get_subsys_info = True if args.syst: logger.info("MIPI Sys-T Collateral file %s", args.syst) section_extraction = False - get_subsys_info = False elf = ELFFile(elffile) @@ -574,11 +573,20 @@ def main(): PTR_FMT = '0x%016x' # Extract strings from ELF files - extract_static_strings(elf, database, section_extraction) + string_mappings = extract_static_strings(elf, database, section_extraction) + if len(string_mappings) > 0: + database.set_string_mappings(string_mappings) + logger.info("Found %d strings", len(string_mappings)) # Extract information related to logging subsystem - if get_subsys_info: - extract_logging_subsys_information(elf, database) + if not section_extraction: + # The logging subsys information (e.g. log module names) + # may require additional strings outside of those extracted + # via ELF DWARF variables. So generate a new string mappings + # with strings in various ELF sections. + string_mappings = extract_static_strings(elf, database, section_extraction=True) + + extract_logging_subsys_information(elf, database, string_mappings) # Write database file if args.json: diff --git a/scripts/logging/dictionary/dictionary_parser/log_database.py b/scripts/logging/dictionary/dictionary_parser/log_database.py index 1607b14205a..1baccfe3b69 100644 --- a/scripts/logging/dictionary/dictionary_parser/log_database.py +++ b/scripts/logging/dictionary/dictionary_parser/log_database.py @@ -14,6 +14,7 @@ import json from .mipi_syst import gen_syst_xml_file from .utils import extract_one_string_in_section +from .utils import find_string_in_mappings ARCHS = { @@ -190,17 +191,7 @@ class LogDatabase(): Find string pointed by string_ptr in the string mapping list. Return None if not found. """ - if string_ptr in self.database['string_mappings']: - return self.database['string_mappings'][string_ptr] - - # No direct match on pointer value. - # This may be a combined string. So check for that. - for ptr, string in self.database['string_mappings'].items(): - if ptr <= string_ptr < (ptr + len(string)): - whole_str = self.database['string_mappings'][ptr] - return whole_str[string_ptr - ptr:] - - return None + return find_string_in_mappings(self.database['string_mappings'], string_ptr) def __find_string_in_sections(self, string_ptr): diff --git a/scripts/logging/dictionary/dictionary_parser/mipi_syst.py b/scripts/logging/dictionary/dictionary_parser/mipi_syst.py index 4afef3064dd..467e74400c1 100644 --- a/scripts/logging/dictionary/dictionary_parser/mipi_syst.py +++ b/scripts/logging/dictionary/dictionary_parser/mipi_syst.py @@ -18,7 +18,7 @@ XML_HEADER = """ xsi:schemaLocation="http://www.mipi.org/1.0/sys-t https://www.mipi.org/schema/sys-t/sys-t_1-0.xsd"> - + """ XML_FOOTER = """ @@ -59,7 +59,8 @@ XML_CATALOG64_FOOTER = """ XML_GUIDS = """ - + """ diff --git a/scripts/logging/dictionary/dictionary_parser/utils.py b/scripts/logging/dictionary/dictionary_parser/utils.py index 98f9e1a1862..33a126261ba 100644 --- a/scripts/logging/dictionary/dictionary_parser/utils.py +++ b/scripts/logging/dictionary/dictionary_parser/utils.py @@ -41,3 +41,27 @@ def extract_one_string_in_section(section, str_ptr): offset += 1 return ret_str + + +def find_string_in_mappings(string_mappings, str_ptr): + """ + Find string pointed by string_ptr in the string mapping + list. Return None if not found. + """ + if string_mappings is None: + return None + + if len(string_mappings) == 0: + return None + + if str_ptr in string_mappings: + return string_mappings[str_ptr] + + # No direct match on pointer value. + # This may be a combined string. So check for that. + for ptr, string in string_mappings.items(): + if ptr <= str_ptr < (ptr + len(string)): + whole_str = string_mappings[ptr] + return whole_str[str_ptr - ptr:] + + return None