I want to use GDB to debug a KGDB device on a remote device. However, typically KGDB operates through a serial connection (target remote /dev/ttyUSB0
). To debug the KGDB on the remote device, I wrote a simple Python script that converts the serial connection to TCP. This means that using a program like Telnet, I confirmed that it is possible to connect to the remote device exactly the same as with the serial connection (see the program below).
However, when I try to connect using target remote IP:Port
instead of target remote /dev/ttyUSB0
on the remote device, GDB does not function properly. Do you have any ideas about what might be causing this?
import asyncio
import serial
# Serial port configuration (adjust these values accordingly)
SERIAL_PORT = 'COM6'
SERIAL_BAUDRATE = 115200
# TCP server configuration
TCP_HOST = '0.0.0.0'
TCP_PORT = 12345 # Telnet default port
async def handle_client(reader, writer):
print(f"Connect using telnet {TCP_HOST} {TCP_PORT}")
print("Enter 'Ctrl + ]' to exit")
# Open serial port
ser = serial.Serial(SERIAL_PORT, SERIAL_BAUDRATE, timeout=0) # Set timeout for non-blocking read
# Function to read from serial and write to TCP client
async def serial_to_tcp():
while True:
data = ser.read(ser.in_waiting or 1) # Read at least one byte or all available
if data:
writer.write(data)
await writer.drain()
await asyncio.sleep(0.01) # Small sleep to yield to other tasks
# Function to read from TCP client and write to serial
async def tcp_to_serial():
while True:
data = await reader.read(100) # Adjust the read size as necessary
if not data:
break
ser.write(data)
# Start tasks for bidirectional communication
serial_to_tcp_task = asyncio.create_task(serial_to_tcp())
tcp_to_serial_task = asyncio.create_task(tcp_to_serial())
try:
# Wait for tasks to complete
await asyncio.gather(serial_to_tcp_task, tcp_to_serial_task)
except asyncio.CancelledError:
pass
finally:
# Close serial port
ser.close()
print("Client disconnected.")
writer.close()
await writer.wait_closed()
async def main():
server = await asyncio.start_server(
handle_client, TCP_HOST, TCP_PORT)
async with server:
await server.serve_forever()
asyncio.run(main())