Skip to content

Commit

Permalink
[gdb] Add ConsoleBuffer pretty-printer for px4_dmesg command
Browse files Browse the repository at this point in the history
  • Loading branch information
niklaut committed Feb 23, 2024
1 parent f4d83fd commit fce452e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.3.10

- Add `px4_dmesg` command and pretty printer to display the dmesg buffer in GDB.
- Add missing SDMMC2 peripheral to STM32F765 SVD file.
- Add CrashDebug to `killme` command.
- Limit yoctopuce dependency to v1.0.
Expand Down
30 changes: 30 additions & 0 deletions src/emdbg/debug/gdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,36 @@ Pretty prints a table of open files and their private data handle.
```


### px4_dmesg

```
px4_dmesg
```

Prints the dmesg buffer of PX4.

```
(gdb) px4_dmesg
$1 = ConsoleBuffer(2394B/4095B: [0 -> 2394]) =
HW arch: PX4_FMU_V6X
HW type: V6X010010
HW version: 0x010
HW revision: 0x010
PX4 git-hash: 1cac91d5a9d19dc081bc54d0ea2b7d26ed64c8d8
PX4 version: 1.14.0 40 (17694784)
PX4 git-branch: develop
Vendor version: 3.0.0 64 (50331712)
OS: NuttX
OS version: Release 11.0.0 (184549631)
OS git-hash: b25bc43cd81e257c5e63ac17c7c4331510584af6
Build datetime: Feb 23 2024 17:18:44
Build uri: localhost
Build variant: default
Toolchain: GNU GCC, 9.3.1 20200408 (release)
PX4GUID: 000600000000373833333430510d002c0045
```


### px4_switch_task

```
Expand Down
2 changes: 1 addition & 1 deletion src/emdbg/debug/px4/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from .task import all_tasks, all_tasks_as_table, all_files_as_table, task_switch
from .semaphore import Semaphore
from .buffer import UartBuffer
from .buffer import UartBuffer, ConsoleBuffer
from .device import all_registers, all_registers_as_table, all_gpios_as_table
from .device import vector_table, vector_table_as_table, Device, coredump
from .device import discover as discover_device
Expand Down
29 changes: 29 additions & 0 deletions src/emdbg/debug/px4/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import annotations
from .base import Base
from .utils import gdb_len


class UartBuffer(Base):
Expand Down Expand Up @@ -34,3 +35,31 @@ def children(self) -> Iterator[tuple[str, Any]]:
content = "".join(chr(v) if chr(v).isprintable() else f"\\{v:02x}"
for v in content.tobytes())
yield ("content", content)


class ConsoleBuffer(Base):
"""
Pretty Printing Console buffers
"""
def __init__(self, gdb, buf_ptr: "gdb.Value"):
super().__init__(gdb)
self._buf = buf_ptr

def to_string(self) -> str:
ptr = int(self._buf['_buffer'].address)
size = gdb_len(self._buf['_buffer'])
head, tail = self._buf['_tail'], self._buf['_head']
used = head - tail if (tail <= head) else size - tail + head
header = f"ConsoleBuffer({used}B/{size}B: "
if tail <= head: header += f"[{tail} -> {head}]) =\n"
else: header += f"{head}] <- [{tail}) =\n"
if used:
if (tail <= head):
# [tail, head]
content = self.read_memory(ptr + tail, used).tobytes()
else:
# head], [tail
content = self.read_memory(ptr + tail, size - tail).tobytes()
content += self.read_memory(ptr, head).tobytes()
header += "".join(chr(v) for v in content)
return header
18 changes: 16 additions & 2 deletions src/emdbg/debug/remote/px4.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ def invoke(self, argument, from_tty):
else:
print("No tasks found!")

class PX4_Dmesg(gdb.Command):
"""
Print the dmesg buffer.
"""
def __init__(self):
super().__init__("px4_dmesg", gdb.COMMAND_USER)

@report_exception
def invoke(self, argument, from_tty):
gdb.execute("print g_console_buffer")


class PX4_Registers(gdb.Command):
"""
Expand Down Expand Up @@ -325,6 +336,7 @@ def invoke(self, argument, from_tty):
PX4_Discover()
PX4_Tasks()
PX4_Files()
PX4_Dmesg()
PX4_Registers()
PX4_Interrupts()
PX4_Gpios()
Expand Down Expand Up @@ -398,11 +410,13 @@ def invoke(self, argument, from_tty):
PX4_Reload()

# Pretty Printers
def _px4_print_semaphore(val):
def _px4_pretty_printer(val):
stype = str(val.type)
if stype == "sem_t" or stype == "struct sem_s":
return px4.Semaphore(gdb, val)
if stype == "struct uart_buffer_s":
return px4.UartBuffer(gdb, val)
if stype == "ConsoleBuffer" or stype == "class ConsoleBuffer":
return px4.ConsoleBuffer(gdb, val)
return None
gdb.pretty_printers.append(_px4_print_semaphore)
gdb.pretty_printers.append(_px4_pretty_printer)

0 comments on commit fce452e

Please sign in to comment.