KGDB remote debugging connection issue via USB and Serial connection
Asked Answered
M

1

5

I am having issues with serial and usb connection between host and target. Below is my setup. Both host and target do NOT have any serial (DB9) ports.

Host : Running windows + VMshare + Ubuntu

Target: Running linux kernel 3.19 . Has a MINI usb port which acts as a serial port, i think its ( CP210x uart to usb )

Connection 1 : Host ( USB to DB9 male-PL2303) + DB9 female to female + (DB9 male to USB) target.

Connection 2 : Host ( USB ) --cable-- (USB mini) Target

Host ( ubuntu VM ), can recognize the USB device (both connections types ) as /dev/ttyUSB0. The device does not show up on the windows device manager since VM takes over the device control.

Target boots into UEFI shell. I modify the syslinux.cfg file to append "kgdbwait kgdboc =ttyS0, 115200" to APPEND flag. Save the change ( press F2) then exit ( press F3 ). Boot into the image. Target now enters the kdb prompt with the following message

kgdb: Waiting for connection from remote gdb...
Entering kdb ( current= <64bit address>, pid 1) on processor 0 due to Keyboard Entry
Kgdb > _

on the host side, i do the following commands and below is error

root@ubuntu: cd /images
root@ubuntu: sudo gdb ./vmlinux
Reading symbols from ./vmlinux done.
(gdb)
(gdb) target remote /dev/ttyUSB0
Remote debugging using /dev/ttyUSB0
Ignoring packet error, continuing...
warning: unrecognized item "timeout" in "qSupported" response
Ignoring packet error, continuing...
Ignoring packet error, continuing...
Bogus trace status reply from target: timeout

Experiments i tried

  1. on Host i used " target remote /dev/ttyS0 " , still same issue
  2. Tried different cables in each connection ( 1 and 2 ) mentioned above
  3. on Target removed the edit in the syslinux.cfg file in UEFI shell, booted the image and entered kgdb using "echo g > /proc/sysrq-trigger"
  4. All kernel configuration related to KGDB* , KGDB_SERIAL*, KGDB_USB* are enabled
  5. all available baud rates

Questions

  1. If i use "kgdbwait kgdboc=ttyUSB0, 115200" instead of " kgdbwait kgdboc=ttyS0, 115200" the target does NOT halt. When the target boots up fully to login prompt, i can see the device is recognized as ttyUSB0 when using connection 1. However as it does not stop does that mean, KGDB using USB does not work ? or for USB debug , i need to use direct USB--USB wire ( connection 3 ) ?
  2. does syslinux.cfg support USB debug? becuase there is a SERIAL flag which has value" 0, 115200 " where 0 refers to ttyS0. syslinux documentation does not have any values for USB type device.
  3. using connection 2, why am i seeing the timeout and packet error issues
  4. occasionally with connection 2, when i execute " target remote /dev/ttyUSB0 " on the host, i notice junk characters on the target. So there is some communication happening, so tried different baud rates still same issue. Does this indicate anything inherently wrong with my setup?
  5. In many online forums/ documents i do not see the "entering kdb due to keyboard entry" when the kernel enters kdb prompt. is this unusual?
Modillion answered 29/3, 2016 at 0:58 Comment(1)
I was able to solve the issue by directly attaching the standalone linux box instead of windows+vm+ubuntu.. I am guessing VM could not tunnel the serial data properly. Vm is VM ware workstation pro 12. Is there any setting i need to change in the VM ? or is this a bug ? Responses to other questions would be helpful for my understanding . ThanksModillion
S
9

The setup of remote kgdb debug is a bit tedious. There are several prerequisites/limitations for the kgdb to work. I will try to break it down.

You have to prepare two machines in this setup.
HOST: Where the agent-proxy & GDB installed.
TARGET: The Linux system being debugged.

Connection Setup

[Host /dev/ttyUSB0] USB to Serial --------- COM port [Target /dev/ttyS0]

On the TARGET side, it's not possible to use the USB interface with kgdb. This is because all the USB-Serial driver (CP210x, PL2303, ...etc) did not implement the polling hook. You have to connect the COM port with a serial cable directly. It is ok to use the USB interface on the HOST side. Since it is a serial connection, you have to use a USB-to-Serial converter and install the proper driver on HOST.

Set the proper baud rate on both sides:

[Target] stty -F /dev/ttyS0 115200
[Host] stty -F /dev/ttyUSB0 115200

Make sure the serial connection works on both direction. You can use:

[Host] cat /dev/ttyUSB0
[Target] echo 'from TARGET to HOST' > /dev/ttyS0

[Target] cat /dev/ttyS0
[Host] echo 'from HOST to TARGET' > /dev/ttyUSB0

You should see the messages on both side of machine. If not, there might be some problems on the cable or driver.

Compile Kernel

Enable KGDB* , KGDB_SERIAL*, KGDB_USB*, DEBUG_INFO, DEBUG_INFO_DWARF4, MAGIC_SYSRQ in the kernel config. Compile and install on the TARGET.

The main purpose here is to enable KGDB feature & preserve debug information in vmlinux.

agent-proxy Setup

agent-proxy acts as a proxy for the TARGET's serial port. It splits up the serial port for multiplexing. One for primary console I/O, the other for GDB session. Thus, we can work on both simultaneously. You should run the agent-proxy on HOST machine.

git clone http://git.kernel.org/pub/scm/utils/kernel/kgdb/agent-proxy.git
cd agent-proxy ; make
./agent-proxy 5550^5551 0 /dev/ttyUSB0,115200

This will redirect:

  • TARGET's console to HOST:5550
  • TARGET's kgdb listening port to HOST:5551

Start To Debug

First, open the primary console:

[Host] telnet localhost 5550

Entering the kdb mode, either by:

[Target] echo ttyS0,115200 > /sys/module/kgdboc/parameters/kgdboc
[Target] dmesg | tail
(you should see KGDB: Registered I/O driver kgdboc, otherwise it failed)

[Target] echo g >/proc/sysrq-trigger

Or, by adding the following kernel parameters in TARGET's bootloader (for early kernel debug):

console=tty0 console=ttyS0,115200 kgdbwait kgdboc=ttyS0,115200

The TARGET machine will halt immediately once it breaks into the kdb.
At the same time, you will see a kdb prompt on the primary console:

....
Entering kdb (current=0xcb846c80, pid 2301) on processor 3 due to Keyboard Entry
[3]kdb>

Type kgdb then enter. The TARGET is now pending for remote GDB's connection. We will connect it from the HOST.

Host> gdb vmlinux
(gdb) target remote localhost:5551
Remote debugging using localhost:5551
kgdb_breakpoint () at kernel/debug/debug_core.c:1072
1072             wmb(); /* Sync point after breakpoint */
(gdb)

Enjoy your kernel debugging!

Straus answered 26/4, 2016 at 10:14 Comment(5)
thanks for the detailed steps. Even though i got past my initial hurdle, i am sure your information will be useful for someone else who is startingModillion
Many posts on kgdb say that, for the built-in kgdb case, kernel parameter order matters, i.e., you should not use the order kgdbwait kgdboc=ttyS0,115200, but first configure kgdboc: kgdboc=ttyS0,115200 kgdbwaitButcherbird
One thing that took some time for me to find is that, on the host side, you have to have one of the following: (a) same architecture as the target, or (b) use a cross-gdb tool appropriate for the target, if on a different architecture. This is because gdb, of an architecture different than the target's, cannot really "understand" the vmlinux binary and does not have a clue this is the case, because, presumably, vmlinux does not contain any architecture info (like elf type or similar).Butcherbird
I also tried gdb on a chroot to a copy of the target's disk, using qemu-user-static (in Ubuntu 14.04), but it seems not to work (cannot understand the code, in vmlinux, to create breakpoints, which it seems gdb does first locally, before asking kgdb to remotely set it).Butcherbird
One more thing is that, on the host side, you have to start gdb (or cross-gdb) at the root of the kernel build-tree (of the target's kernel). This is for gbd to find the code of the kernel and it means either the target's kernel was (cross-)built on the host, or you have an image of the target's disk on the host. Of course, vmlinux has to be the target's uncompressed kernel (transfered on the host, if built on the target).Butcherbird

© 2022 - 2024 — McMap. All rights reserved.