This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 8031c9c1de6ff90f58162cb862cb5475cbee19df Author: xuxingliang <[email protected]> AuthorDate: Mon Nov 11 17:37:31 2024 +0800 gdb/mm: cache global variables to save time of memleak check Signed-off-by: xuxingliang <[email protected]> --- tools/gdb/nuttxgdb/memleak.py | 21 +++++++++++++++++++++ tools/gdb/nuttxgdb/utils.py | 9 +++++++++ 2 files changed, 30 insertions(+) diff --git a/tools/gdb/nuttxgdb/memleak.py b/tools/gdb/nuttxgdb/memleak.py index 7303052033..2a4b2455b6 100644 --- a/tools/gdb/nuttxgdb/memleak.py +++ b/tools/gdb/nuttxgdb/memleak.py @@ -19,7 +19,9 @@ ############################################################################ import bisect +import json import time +from os import path from typing import Dict, Generator, List import gdb @@ -60,8 +62,20 @@ class MMLeak(gdb.Command): utils.alias("memleak", "mm leak") def global_nodes(self) -> List[GlobalNode]: + cache = path.join( + path.dirname(path.abspath(gdb.objfiles()[0].filename)), + f"{utils.get_elf_md5()}-globals.json", + ) + nodes: List[GlobalNode] = [] + if path.isfile(cache): + with open(cache, "r") as f: + variables = json.load(f) + for var in variables: + nodes.append(GlobalNode(var["address"], var["size"])) + return nodes + longsize = utils.get_long_type().sizeof for objfile in gdb.objfiles(): elf = self.elf.load_from_path(objfile.filename) @@ -77,6 +91,13 @@ class MMLeak(gdb.Command): address = symbol["st_value"] nodes.append(GlobalNode(address, size)) + with open(cache, "w") as f: + variables = [ + {"address": node.address, "size": node.nodesize} for node in nodes + ] + str = utils.jsonify(variables) + f.write(str) + return nodes def invoke(self, arg: str, from_tty: bool) -> None: diff --git a/tools/gdb/nuttxgdb/utils.py b/tools/gdb/nuttxgdb/utils.py index 66237c82ff..f2aaec8358 100644 --- a/tools/gdb/nuttxgdb/utils.py +++ b/tools/gdb/nuttxgdb/utils.py @@ -23,6 +23,7 @@ from __future__ import annotations import argparse +import hashlib import importlib import json import os @@ -856,6 +857,14 @@ def gather_gdbcommands(modules=None, path=None) -> List[gdb.Command]: return commands +def get_elf_md5(): + """Return the md5 checksum of the current ELF file""" + file = gdb.objfiles()[0].filename + with open(file, "rb") as f: + hash = hashlib.md5(f.read()).hexdigest() + return hash + + def jsonify(obj, indent=None): if not obj: return "{}"
