Skip to content

Commit

Permalink
#87 add client symbol cache for gdb (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown321 authored Jan 16, 2024
1 parent 08f3b11 commit 922eedd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
18 changes: 16 additions & 2 deletions decomp2dbg/clients/gdb/gdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, gdb_client, name="decompiler", host="127.0.0.1", port=3662):
self.symbol_mapper = SymbolMapper()
self._is_pie = None
self._lvar_bptr = None
self._symbol_cache = set()

@property
@lru_cache()
Expand Down Expand Up @@ -52,6 +53,13 @@ def decompiler_connected(self):
def decompiler_disconnected(self):
self.gdb_client.on_decompiler_disconnected(self.name)

def _cache(self, item):
if item in self._symbol_cache:
return False

self._symbol_cache.add(item)
return True

def update_symbols(self):
self.symbol_mapper.text_base_addr = self.text_base_addr

Expand All @@ -66,7 +74,10 @@ def update_symbols(self):

# add symbols with native support if possible
for addr, func in func_headers.items():
syms_to_add.append((func["name"], int(addr, 0), "function", func["size"]))
symbol = (func["name"], int(addr, 0), "function", func["size"])
new_entry = self._cache(symbol)
if new_entry:
syms_to_add.append(symbol)
sym_name_set.add(func["name"])

for addr, global_var in global_vars.items():
Expand All @@ -75,7 +86,10 @@ def update_symbols(self):
if clean_name in sym_name_set:
continue

syms_to_add.append((clean_name, int(addr, 0), "object", global_var_size))
symbol = (clean_name, int(addr,0), "object", global_var_size)
new_entry = self._cache(symbol)
if new_entry:
syms_to_add.append(symbol)

try:
self.symbol_mapper.add_native_symbols(syms_to_add)
Expand Down
32 changes: 32 additions & 0 deletions decomp2dbg/clients/gdb/gdb_client_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import unittest
import sys
from unittest.mock import Mock

sys.modules["gdb"] = Mock()
from decomp2dbg import GDBDecompilerClient


# run with python -m unittest gdb_client_test.TestGDBDecompilerClient

class TestGDBDecompilerClient(unittest.TestCase):
def setUp(self) -> None:
self._symbols = [
("f1", 0x100, "function", 0x10),
("f2", 0x200, "function", 0x10),
("g1", 0x300, "object", 8),
("g2", 0x400, "object", 8)
]

self._c = GDBDecompilerClient(None)
for s in self._symbols:
self._c._cache(s)

def test_cache_new_entry(self):
entry = ("new symbol f3", 0x500, "function", 0x10)
is_new = self._c._cache(entry)
self.assertEqual(is_new, True, "entry is not not new")

def test_cache_old_entry(self):
existing = self._symbols[0]
is_new = self._c._cache(existing)
self.assertEqual(is_new, False, "entry is old, but cache func says it's new")
3 changes: 3 additions & 0 deletions decomp2dbg/clients/gdb/symbol_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def add_native_symbols(self, sym_info_list):
return False

# info("{:d} symbols will be added".format(len(sym_info_list)))
if len(sym_info_list) == 0:
return True

self._delete_old_sym_files()

# add each symbol into a mass symbol commit
Expand Down

0 comments on commit 922eedd

Please sign in to comment.