Skip to content

Commit

Permalink
gdb/mm: cache global variables to save time of memleak check
Browse files Browse the repository at this point in the history
Signed-off-by: xuxingliang <[email protected]>
  • Loading branch information
XuNeo authored and xiaoxiang781216 committed Nov 25, 2024
1 parent a0e9b82 commit 8031c9c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tools/gdb/nuttxgdb/memleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
############################################################################

import bisect
import json
import time
from os import path
from typing import Dict, Generator, List

import gdb
Expand Down Expand Up @@ -60,8 +62,20 @@ def __init__(self):
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)
Expand All @@ -77,6 +91,13 @@ def global_nodes(self) -> List[GlobalNode]:
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:
Expand Down
9 changes: 9 additions & 0 deletions tools/gdb/nuttxgdb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from __future__ import annotations

import argparse
import hashlib
import importlib
import json
import os
Expand Down Expand Up @@ -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 "{}"
Expand Down

0 comments on commit 8031c9c

Please sign in to comment.