diff --git a/tools/gdb/gcore.py b/tools/gdb/gcore.py index c9c98a4c9a0..499d065bab3 100644 --- a/tools/gdb/gcore.py +++ b/tools/gdb/gcore.py @@ -23,7 +23,6 @@ import argparse import os import shutil -import tempfile import gdb import utils @@ -53,6 +52,14 @@ def parse_args(args): class NXGcore(gdb.Command): def __init__(self): + self.tempfile = utils.import_check( + "tempfile", + errmsg="No tempfile module found, please try gdb-multiarch instead.\n", + ) + + if not self.tempfile: + return + super(NXGcore, self).__init__("nxgcore", gdb.COMMAND_USER) def invoke(self, args, from_tty): @@ -63,7 +70,7 @@ class NXGcore(gdb.Command): objfile = gdb.current_progspace().objfiles()[0] elffile = objfile.filename - tmpfile = tempfile.NamedTemporaryFile(suffix=".elf") + tmpfile = self.tempfile.NamedTemporaryFile(suffix=".elf") # Create temporary ELF file with sections for each memory region diff --git a/tools/gdb/memdump.py b/tools/gdb/memdump.py index b925aa80dbc..133f0fd7cd9 100644 --- a/tools/gdb/memdump.py +++ b/tools/gdb/memdump.py @@ -646,6 +646,12 @@ class Memleak(gdb.Command): """Memleak check""" def __init__(self): + self.elf = utils.import_check( + "elftools.elf.elffile", "ELFFile", "Plase pip install pyelftools\n" + ) + if not self.elf: + return + super(Memleak, self).__init__("memleak", gdb.COMMAND_USER) def check_alive(self, pid): @@ -756,12 +762,6 @@ class Memleak(gdb.Command): return {"simple": args.simple, "detail": args.detail} def invoke(self, args, from_tty): - self.elf = utils.import_check( - "elftools.elf.elffile", "ELFFile", "Plase pip install pyelftools\n" - ) - if not self.elf: - return - if sizeof_size_t == 4: align = 11 else: @@ -898,6 +898,14 @@ have {i} some backtrace leak, total leak memory is {int(leaksize)} bytes\n" class Memmap(gdb.Command): def __init__(self): + self.np = utils.import_check("numpy", errmsg="Please pip install numpy\n") + self.plt = utils.import_check( + "matplotlib", "pyplot", errmsg="Please pip install matplotlib\n" + ) + self.math = utils.import_check("math") + if not self.np or not self.plt or not self.math: + return + super(Memmap, self).__init__("memmap", gdb.COMMAND_USER) def save_memory_map(self, mallinfo, output_file): @@ -946,14 +954,6 @@ class Memmap(gdb.Command): return args.output def invoke(self, args, from_tty): - self.np = utils.import_check("numpy", errmsg="Please pip install numpy\n") - self.plt = utils.import_check( - "matplotlib", "pyplot", errmsg="Please pip install matplotlib\n" - ) - self.math = utils.import_check("math") - if not self.np or not self.plt or not self.math: - return - output_file = self.parse_arguments(args.split(" ")) meminfo = self.allocinfo() self.save_memory_map(meminfo, output_file + ".png") diff --git a/tools/gdb/net.py b/tools/gdb/net.py index 7de874680e3..379bbc9b74f 100644 --- a/tools/gdb/net.py +++ b/tools/gdb/net.py @@ -23,7 +23,7 @@ import utils from lists import dq_for_every, sq_for_every socket = utils.import_check( - "socket", errmsg="No socket module found, please try gdb-multiarch instead." + "socket", errmsg="No socket module found, please try gdb-multiarch instead.\n" ) NET_IPv4 = utils.get_symbol_value("CONFIG_NET_IPv4") diff --git a/tools/gdb/utils.py b/tools/gdb/utils.py index 34b365bee46..2c45426578a 100644 --- a/tools/gdb/utils.py +++ b/tools/gdb/utils.py @@ -20,7 +20,6 @@ # ############################################################################ -import cProfile import re import shlex from typing import List, Tuple, Union @@ -359,10 +358,17 @@ class Profile(gdb.Command): """ def __init__(self): + self.cProfile = import_check( + "cProfile", errmsg="cProfile module not found, try gdb-multiarch.\n" + ) + if not self.cProfile: + return + super(Profile, self).__init__("profile", gdb.COMMAND_USER) def invoke(self, args, from_tty): - cProfile.run(f"gdb.execute('{args}')", sort="cumulative") + + self.cProfile.run(f"gdb.execute('{args}')", sort="cumulative") Profile()