1
0
Fork 0
forked from nuttx/nuttx-update

gdb/macro: cache macro info to json and load directly

Use json module to save macro info to json file and load directly. It can save 2seconds for x4b projects to load plugin

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
This commit is contained in:
xuxingliang 2024-10-31 14:15:06 +08:00 committed by Xiang Xiao
parent 96a518af35
commit b296b1debe
2 changed files with 27 additions and 20 deletions

View file

@ -38,6 +38,7 @@
# Currently, we are using the second method.
import hashlib
import json
import os
import re
import time
@ -132,30 +133,37 @@ def fetch_macro_info(file):
with open(file, "rb") as f:
hash = hashlib.md5(f.read()).hexdigest()
cache = path.join(path.dirname(path.abspath(file)), f"{hash}.macro")
macros = {}
p = re.compile(".*macro[ ]*:[ ]*([\S]+\(.*?\)|[\w]+)[ ]*(.*)")
cache = path.join(path.dirname(path.abspath(file)), f"{hash}.json")
print(f"Load macro: {cache}")
if not path.isfile(cache):
start = time.time()
t = time.time()
os.system(f'readelf -wm "{file}" > "{cache}"')
print(f"readelf took {time.time() - start:.1f} seconds")
print(f"readelf took {time.time() - t:.1f} seconds")
t = time.time()
with open(cache, "r") as f2:
for line in f2.readlines():
if not line.startswith(" DW_MACRO_define") and not line.startswith(
" DW_MACRO_undef"
):
continue
if not parse_macro(line, macros, p):
print(f"Failed to parse {line}")
print(f"Parse macro took {time.time() - t:.1f} seconds")
with open(cache, "w") as f2:
dump = json.dumps(macros, indent=4, sort_keys=True)
f2.write(dump)
print(f"Cache macro info to {cache}")
else:
print(f"Load macro info from {cache}")
with open(cache, "r") as f2:
macros = json.load(f2)
p = re.compile(".*macro[ ]*:[ ]*([\S]+\(.*?\)|[\w]+)[ ]*(.*)")
macros = {}
start = time.time()
with open(cache, "r") as f2:
for line in f2.readlines():
if not line.startswith(" DW_MACRO_define") and not line.startswith(
" DW_MACRO_undef"
):
continue
if not parse_macro(line, macros, p):
print(f"Failed to parse {line}")
print(f"Parse macro took {time.time() - start:.1f} seconds")
return macros

View file

@ -211,7 +211,6 @@ def get_symbol_value(name, locspec="nx_start", cacheable=True):
# Try to expand macro by reading elf
global g_macro_ctx
if not g_macro_ctx:
gdb.write("No macro context found, trying to load from ELF\n")
if len(gdb.objfiles()) > 0:
g_macro_ctx = MacroCtx(gdb.objfiles()[0].filename)
else: