gdb/mm: cache global variables to save time of memleak check

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
This commit is contained in:
xuxingliang
2024-11-11 17:37:31 +08:00
committed by Xiang Xiao
parent a0e9b82a79
commit 8031c9c1de
2 changed files with 30 additions and 0 deletions
+21
View File
@@ -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:
+9
View File
@@ -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 "{}"