Skip to content

Commit

Permalink
Ignore structs that are likely in the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinosu committed Dec 18, 2024
1 parent be57d1e commit a7b8eeb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 35 deletions.
10 changes: 5 additions & 5 deletions debugger2/src/debugger/base_debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
from . import mion


def panic(*args, **kwargs):
raise AssertionError(*args, **kwargs)


class BaseDebugger:
def __init__(self) -> None:
do_nothing = lambda *args, **kwargs: None
Expand Down Expand Up @@ -76,6 +72,8 @@ async def run_command(self, command: str):
return result

async def console(self, command: str):
"""Experimental"""

self.stream_queue = deque[str](maxlen=None)
self.process.stdin.write(
f'-interpreter-exec console "{command}"\n'.encode()
Expand Down Expand Up @@ -125,7 +123,9 @@ async def _stdout_dispatch(self) -> None:
case _ if kind in mion.STREAM:
self.stream_queue.append(message)
case _:
panic(f"Received unknown message from GDB: {kind}")
raise ValueError(
f"Received unknown message kind from GDB: {kind}"
)

async def _inferior_dispatch(self) -> None:
self._inferior_dispatch_done.clear()
Expand Down
3 changes: 0 additions & 3 deletions debugger2/src/debugger/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ async def compile(source_path: str | Path, output_path: str | Path) -> None:
str(output_path),
"-ggdb",
"-O0",
# "-Wall",
# "-Wextra",
# "-Werror",
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
Expand Down
16 changes: 9 additions & 7 deletions debugger2/src/debugger/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ async def var_details(
type = res["type"]

res = await self.run_command("-var-list-children VARIABLE")
childs = []
if res["numchild"] != "0":
childs.extend((c["exp"], c["type"]) for c in res["children"])
childs = (
[(c["exp"], c["type"]) for c in res["children"]]
if res["numchild"] != "0"
else []
)
await self.run_command("-var-delete VARIABLE")

res = await self.run_command(f"-data-evaluate-expression {var}")
Expand Down Expand Up @@ -122,9 +124,9 @@ def follow(var: str, type: str, children: list[tuple[str, str]]):
structs: dict[str, list[tuple[str, str]]] = {} # legacy

for i, frame in enumerate(await self.frames()):
vars = dict[str, Obj]()
queue = deque()

vars = dict[str, Obj]()
for var in await self.variables(i):
type, value, addr, childs = await self.var_details(var, i)
vars[var] = Obj(type, value, addr)
Expand All @@ -141,10 +143,8 @@ def follow(var: str, type: str, children: list[tuple[str, str]]):
type, value, addr, childs = await self.var_details(var, i)
except ValueError:
continue

if (addr, type) in addresses:
continue

addresses[addr, type] = Obj(type, value, addr)
if (
childs
Expand Down Expand Up @@ -198,7 +198,9 @@ async def legacy_trace(self):
"heap_data": {
addr: {"addr": addr, "typeName": o.type, "value": o.value}
for (addr, _), o in reversed(memory.items())
if "*" not in o.type and "struct" in o.type
if "*" not in o.type
and "struct" in o.type
and not o.addr.startswith("0xffff")
},
}

Expand Down
9 changes: 4 additions & 5 deletions debugger2/src/debugger/mion.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ def loads(result: str) -> any:

result = _remove_octals(result)
result = _remove_array_keys(result)
result = sub(r"([a-zA-Z\-_]+)=", r'"\1":', result)
result = sub(r"([a-zA-Z\-_]+)=", r'"\1":', result) # replace kv pairs
try:
return json.loads(f"{{{result}}}")
except json.JSONDecodeError:
# result = sub(r"([a-zA-Z\-_]+) =", r'"\1":', result)
return json.loads(result)


Expand All @@ -59,11 +58,11 @@ def valueloads(result: str) -> any:
result = _remove_octals(result)
result = _remove_array_keys(result)
result = _remove_hexnums(result)
result = sub(r"= (\d+) '[^']+'", r"= \1", result) # char aliases
result = sub(r"= (\d+) '[^']+'", r"= \1", result) # remove char aliases
result = sub(
r"= (\"0x[0-9a-zA-Z]+\") <[^>]+>", r"= \1", result
) # function aliases
result = sub(r"([a-zA-Z\-_]+) =", r'"\1":', result)
) # remove function aliases
result = sub(r"([a-zA-Z\-_]+) =", r'"\1":', result) # replace kv pairs
return json.loads(result)


Expand Down
26 changes: 14 additions & 12 deletions debugger2/src/demonstration.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ def print_blue(message: any) -> None:
for function in functions:
await debug.breakpoint(function)

# res = await debug.run_command("-symbol-info-types")
# custom_types = [
# sym["name"]
# for file in res["symbols"]["debug"]
# for sym in file["symbols"]
# if "line" in sym
# ]
# for type in custom_types:
# try:
# pp(await debug.console(f"ptype struct {type}"))
# except ValueError:
# continue
# --- experimental
res = await debug.run_command("-symbol-info-types")
custom_types = [
sym["name"]
for file in res["symbols"]["debug"]
for sym in file["symbols"]
if "line" in sym
]
for type in custom_types:
try:
pp(await debug.console(f"ptype struct {type}"))
except ValueError:
continue
# ---

await debug.run()
print(await debug.frames())
Expand Down
3 changes: 0 additions & 3 deletions debugger2/src/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ async def executeNext(sid: str) -> None:
json.loads(json.dumps(type, default=asdict)),
to=sid,
)

pp(json.loads(json.dumps(legacy_mem, default=asdict)))

await server.emit(
"sendBackendStateToUser",
json.loads(json.dumps(legacy_mem, default=asdict)),
Expand Down

0 comments on commit a7b8eeb

Please sign in to comment.