Skip to content

Commit 620def5

Browse files
committed
Add GDB integration with MicroPython debugging support
1 parent 75b5cb6 commit 620def5

File tree

5 files changed

+525
-3
lines changed

5 files changed

+525
-3
lines changed

ROADMAP_STATUS.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ This document tracks the implementation status of each roadmap item across relea
66

77
| Feature | Status | Implemented In | Progress | Priority | Notes |
88
|---------|--------|----------------|----------|----------|-------|
9-
| GDB integration for step-by-step debugging | In Progress | - | 25% | High | Initial integration started in v2025.03.01.8 with script improvements |
9+
| GDB integration for step-by-step debugging | In Progress | v2025.03.04.13 | 60% | High | Added comprehensive GDB integration with MicroPython debugging support, custom commands, and Python helpers |
1010
| Custom UART driver optimized for QEMU | Completed | v2025.03.03.11 | 100% | High | Fully implemented with enhanced features for testing and simulation |
11-
| Better semihosting integration | In Progress | v2025.03.01.8 | 50% | Medium | Basic integration complete, needs better MicroPython support |
12-
| Alternative QEMU machine types for STM32F4 | In Progress | v2025.03.01.8 | 40% | Medium | Initial configuration with olimex-stm32-h405 complete |
11+
| Better semihosting integration | In Progress | v2025.03.01.8 | 50% | High | Basic integration complete, needs better MicroPython support |
12+
| Alternative QEMU machine types for STM32F4 | In Progress | v2025.03.01.8 | 40% | High | Initial configuration with olimex-stm32-h405 complete |
1313
| Comprehensive unit testing framework | In Progress | v2025.03.03.11 | 65% | Medium | UART testing framework completed with network and device-to-device simulation capabilities |
1414

1515
## v1.2.0 Milestone: IoT and Simulation Capabilities
@@ -47,6 +47,13 @@ Based on current progress and priorities, the adjusted timeline is:
4747
## Recent Progress Updates
4848

4949
### March 2025 Update
50+
- **v2025.03.04.13 Release**: Added comprehensive GDB integration
51+
- Implemented GDB initialization with MicroPython support
52+
- Created debug script for QEMU-GDB integration
53+
- Added Python helper for MicroPython debugging
54+
- Created test script for debugging verification
55+
- Added custom GDB commands for Python state inspection
56+
- Improved debugging infrastructure with Python-level support
5057
- **v2025.03.03.11 Release**: Completed full implementation of custom UART driver
5158
- Implemented comprehensive custom UART driver with advanced simulation features
5259
- Created Python bindings for UART testing features

config/gdb/gdbinit

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# GDB initialization file for QEMU-MicroPython debugging
2+
3+
# Connect to QEMU's GDB server
4+
target remote localhost:1234
5+
6+
# Load debug symbols
7+
file firmware/build/firmware.elf
8+
9+
# Set up Python pretty-printers
10+
python
11+
import os
12+
import sys
13+
14+
# Add custom pretty printers for MicroPython objects
15+
class MpyObjectPrinter:
16+
def __init__(self, val):
17+
self.val = val
18+
19+
def to_string(self):
20+
# Extract MicroPython object type and value
21+
type_name = self.val['type']['name'].string()
22+
if type_name == "str":
23+
return f'mp_obj_str("{self.val["str"]}")'
24+
elif type_name == "int":
25+
return f'mp_obj_int({self.val["value"]})'
26+
return f'mp_obj({type_name})'
27+
28+
def register_printers(obj):
29+
if obj is None:
30+
obj = gdb
31+
obj.pretty_printers.append(lookup_function)
32+
33+
def lookup_function(val):
34+
if str(val.type).startswith("mp_obj_"):
35+
return MpyObjectPrinter(val)
36+
return None
37+
38+
register_printers(None)
39+
end
40+
41+
# Custom GDB commands for MicroPython debugging
42+
define mpy_bt
43+
# Print MicroPython backtrace
44+
print "MicroPython backtrace:"
45+
print "===================="
46+
call mp_print_backtrace()
47+
end
48+
document mpy_bt
49+
Print MicroPython backtrace showing Python-level call stack
50+
end
51+
52+
define mpy_locals
53+
# Print local variables in current Python frame
54+
print "Local variables:"
55+
print "==============="
56+
call mp_print_locals()
57+
end
58+
document mpy_locals
59+
Print local variables in the current Python frame
60+
end
61+
62+
define mpy_globals
63+
# Print global variables
64+
print "Global variables:"
65+
print "================"
66+
call mp_print_globals()
67+
end
68+
document mpy_globals
69+
Print global variables in the current context
70+
end
71+
72+
define mpy_stack
73+
# Print Python stack contents
74+
print "Python stack:"
75+
print "============"
76+
call mp_print_stack()
77+
end
78+
document mpy_stack
79+
Print contents of the Python stack
80+
end
81+
82+
# Set up common breakpoints
83+
break mp_execute_bytecode
84+
break mp_raise
85+
86+
# Configure output
87+
set print pretty on
88+
set print array on
89+
set print array-indexes on
90+
set python print-stack full
91+
92+
# Show version and configuration info
93+
show version
94+
show configuration
95+
96+
# Ready to debug
97+
echo \nGDB configured for MicroPython debugging. Available commands:\n
98+
echo mpy_bt - Show MicroPython backtrace\n
99+
echo mpy_locals - Show local variables\n
100+
echo mpy_globals - Show global variables\n
101+
echo mpy_stack - Show Python stack\n

scripts/debug_micropython.sh

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/bash
2+
# Debug MicroPython in QEMU with GDB support
3+
4+
set -e
5+
6+
# Define variables
7+
PROJECT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
8+
TOOLS_DIR="$PROJECT_DIR/tools"
9+
QEMU_DIR="$TOOLS_DIR/qemu"
10+
CONFIG_DIR="$PROJECT_DIR/config"
11+
BUILD_DIR="$PROJECT_DIR/firmware/build"
12+
FIRMWARE="$BUILD_DIR/firmware.bin"
13+
GDB_PORT=1234
14+
LOG_FILE="$PROJECT_DIR/debug_log.txt"
15+
16+
# Check dependencies
17+
if ! command -v arm-none-eabi-gdb &> /dev/null; then
18+
echo "Error: arm-none-eabi-gdb not found. Please install ARM toolchain."
19+
exit 1
20+
fi
21+
22+
# Find QEMU executable
23+
if [ -f "$QEMU_DIR/build/arm-softmmu/qemu-system-arm" ]; then
24+
QEMU_PATH="$QEMU_DIR/build/arm-softmmu/qemu-system-arm"
25+
elif [ -f "$QEMU_DIR/build/qemu-system-arm" ]; then
26+
QEMU_PATH="$QEMU_DIR/build/qemu-system-arm"
27+
else
28+
echo "Error: QEMU executable not found."
29+
exit 1
30+
fi
31+
32+
# Check if firmware exists
33+
if [ ! -f "$FIRMWARE" ]; then
34+
if [ -f "$BUILD_DIR/firmware0.bin" ] && [ -f "$BUILD_DIR/firmware1.bin" ]; then
35+
echo "Found split firmware files. Creating combined firmware for QEMU..."
36+
cat "$BUILD_DIR/firmware0.bin" > "$FIRMWARE"
37+
dd if=/dev/zero bs=1 count=$((0x20000 - $(stat -f%z "$BUILD_DIR/firmware0.bin"))) >> "$FIRMWARE" 2>/dev/null
38+
cat "$BUILD_DIR/firmware1.bin" >> "$FIRMWARE"
39+
else
40+
echo "Error: No firmware found. Please build first."
41+
exit 1
42+
fi
43+
fi
44+
45+
# Create debug directory if it doesn't exist
46+
mkdir -p "$PROJECT_DIR/debug"
47+
48+
# Copy GDB init file to debug directory
49+
cp "$CONFIG_DIR/gdb/gdbinit" "$PROJECT_DIR/debug/.gdbinit"
50+
51+
# Start QEMU in the background
52+
echo "Starting QEMU with GDB server on port $GDB_PORT..."
53+
$QEMU_PATH \
54+
-machine olimex-stm32-h405 \
55+
-cpu cortex-m4 \
56+
-m 128K \
57+
-kernel "$FIRMWARE" \
58+
-serial stdio \
59+
-monitor none \
60+
-nographic \
61+
-S \
62+
-s \
63+
-d guest_errors,unimp,exec \
64+
-semihosting-config enable=on,target=native \
65+
-semihosting \
66+
2>"$LOG_FILE" &
67+
68+
QEMU_PID=$!
69+
70+
# Give QEMU a moment to start
71+
sleep 1
72+
73+
# Check if QEMU is running
74+
if ! kill -0 $QEMU_PID 2>/dev/null; then
75+
echo "Error: QEMU failed to start. Check $LOG_FILE for details."
76+
exit 1
77+
fi
78+
79+
echo "QEMU started successfully (PID: $QEMU_PID)"
80+
echo "Starting GDB..."
81+
82+
# Start GDB with our init file
83+
cd "$PROJECT_DIR"
84+
arm-none-eabi-gdb -x debug/.gdbinit
85+
86+
# Clean up QEMU when GDB exits
87+
kill $QEMU_PID 2>/dev/null || true
88+
89+
echo "Debug session ended."

0 commit comments

Comments
 (0)