1*d5af79c0SRandy Dunlap================================================= 2*d5af79c0SRandy DunlapUsing kgdb, kdb and the kernel debugger internals 3*d5af79c0SRandy Dunlap================================================= 4*d5af79c0SRandy Dunlap 5*d5af79c0SRandy Dunlap:Author: Jason Wessel 6*d5af79c0SRandy Dunlap 7*d5af79c0SRandy DunlapIntroduction 8*d5af79c0SRandy Dunlap============ 9*d5af79c0SRandy Dunlap 10*d5af79c0SRandy DunlapThe kernel has two different debugger front ends (kdb and kgdb) which 11*d5af79c0SRandy Dunlapinterface to the debug core. It is possible to use either of the 12*d5af79c0SRandy Dunlapdebugger front ends and dynamically transition between them if you 13*d5af79c0SRandy Dunlapconfigure the kernel properly at compile and runtime. 14*d5af79c0SRandy Dunlap 15*d5af79c0SRandy DunlapKdb is simplistic shell-style interface which you can use on a system 16*d5af79c0SRandy Dunlapconsole with a keyboard or serial console. You can use it to inspect 17*d5af79c0SRandy Dunlapmemory, registers, process lists, dmesg, and even set breakpoints to 18*d5af79c0SRandy Dunlapstop in a certain location. Kdb is not a source level debugger, although 19*d5af79c0SRandy Dunlapyou can set breakpoints and execute some basic kernel run control. Kdb 20*d5af79c0SRandy Dunlapis mainly aimed at doing some analysis to aid in development or 21*d5af79c0SRandy Dunlapdiagnosing kernel problems. You can access some symbols by name in 22*d5af79c0SRandy Dunlapkernel built-ins or in kernel modules if the code was built with 23*d5af79c0SRandy Dunlap``CONFIG_KALLSYMS``. 24*d5af79c0SRandy Dunlap 25*d5af79c0SRandy DunlapKgdb is intended to be used as a source level debugger for the Linux 26*d5af79c0SRandy Dunlapkernel. It is used along with gdb to debug a Linux kernel. The 27*d5af79c0SRandy Dunlapexpectation is that gdb can be used to "break in" to the kernel to 28*d5af79c0SRandy Dunlapinspect memory, variables and look through call stack information 29*d5af79c0SRandy Dunlapsimilar to the way an application developer would use gdb to debug an 30*d5af79c0SRandy Dunlapapplication. It is possible to place breakpoints in kernel code and 31*d5af79c0SRandy Dunlapperform some limited execution stepping. 32*d5af79c0SRandy Dunlap 33*d5af79c0SRandy DunlapTwo machines are required for using kgdb. One of these machines is a 34*d5af79c0SRandy Dunlapdevelopment machine and the other is the target machine. The kernel to 35*d5af79c0SRandy Dunlapbe debugged runs on the target machine. The development machine runs an 36*d5af79c0SRandy Dunlapinstance of gdb against the vmlinux file which contains the symbols (not 37*d5af79c0SRandy Dunlapa boot image such as bzImage, zImage, uImage...). In gdb the developer 38*d5af79c0SRandy Dunlapspecifies the connection parameters and connects to kgdb. The type of 39*d5af79c0SRandy Dunlapconnection a developer makes with gdb depends on the availability of 40*d5af79c0SRandy Dunlapkgdb I/O modules compiled as built-ins or loadable kernel modules in the 41*d5af79c0SRandy Dunlaptest machine's kernel. 42*d5af79c0SRandy Dunlap 43*d5af79c0SRandy DunlapCompiling a kernel 44*d5af79c0SRandy Dunlap================== 45*d5af79c0SRandy Dunlap 46*d5af79c0SRandy Dunlap- In order to enable compilation of kdb, you must first enable kgdb. 47*d5af79c0SRandy Dunlap 48*d5af79c0SRandy Dunlap- The kgdb test compile options are described in the kgdb test suite 49*d5af79c0SRandy Dunlap chapter. 50*d5af79c0SRandy Dunlap 51*d5af79c0SRandy DunlapKernel config options for kgdb 52*d5af79c0SRandy Dunlap------------------------------ 53*d5af79c0SRandy Dunlap 54*d5af79c0SRandy DunlapTo enable ``CONFIG_KGDB`` you should look under 55*d5af79c0SRandy Dunlap:menuselection:`Kernel hacking --> Kernel debugging` and select 56*d5af79c0SRandy Dunlap:menuselection:`KGDB: kernel debugger`. 57*d5af79c0SRandy Dunlap 58*d5af79c0SRandy DunlapWhile it is not a hard requirement that you have symbols in your vmlinux 59*d5af79c0SRandy Dunlapfile, gdb tends not to be very useful without the symbolic data, so you 60*d5af79c0SRandy Dunlapwill want to turn on ``CONFIG_DEBUG_INFO`` which is called 61*d5af79c0SRandy Dunlap:menuselection:`Compile the kernel with debug info` in the config menu. 62*d5af79c0SRandy Dunlap 63*d5af79c0SRandy DunlapIt is advised, but not required, that you turn on the 64*d5af79c0SRandy Dunlap``CONFIG_FRAME_POINTER`` kernel option which is called :menuselection:`Compile 65*d5af79c0SRandy Dunlapthe kernel with frame pointers` in the config menu. This option inserts code 66*d5af79c0SRandy Dunlapinto the compiled executable which saves the frame information in registers 67*d5af79c0SRandy Dunlapor on the stack at different points which allows a debugger such as gdb to 68*d5af79c0SRandy Dunlapmore accurately construct stack back traces while debugging the kernel. 69*d5af79c0SRandy Dunlap 70*d5af79c0SRandy DunlapIf the architecture that you are using supports the kernel option 71*d5af79c0SRandy Dunlap``CONFIG_STRICT_KERNEL_RWX``, you should consider turning it off. This 72*d5af79c0SRandy Dunlapoption will prevent the use of software breakpoints because it marks 73*d5af79c0SRandy Dunlapcertain regions of the kernel's memory space as read-only. If kgdb 74*d5af79c0SRandy Dunlapsupports it for the architecture you are using, you can use hardware 75*d5af79c0SRandy Dunlapbreakpoints if you desire to run with the ``CONFIG_STRICT_KERNEL_RWX`` 76*d5af79c0SRandy Dunlapoption turned on, else you need to turn off this option. 77*d5af79c0SRandy Dunlap 78*d5af79c0SRandy DunlapNext you should choose one or more I/O drivers to interconnect the debugging 79*d5af79c0SRandy Dunlaphost and debugged target. Early boot debugging requires a KGDB I/O 80*d5af79c0SRandy Dunlapdriver that supports early debugging and the driver must be built into 81*d5af79c0SRandy Dunlapthe kernel directly. Kgdb I/O driver configuration takes place via 82*d5af79c0SRandy Dunlapkernel or module parameters which you can learn more about in the 83*d5af79c0SRandy Dunlapsection that describes the parameter kgdboc. 84*d5af79c0SRandy Dunlap 85*d5af79c0SRandy DunlapHere is an example set of ``.config`` symbols to enable or disable for kgdb:: 86*d5af79c0SRandy Dunlap 87*d5af79c0SRandy Dunlap # CONFIG_STRICT_KERNEL_RWX is not set 88*d5af79c0SRandy Dunlap CONFIG_FRAME_POINTER=y 89*d5af79c0SRandy Dunlap CONFIG_KGDB=y 90*d5af79c0SRandy Dunlap CONFIG_KGDB_SERIAL_CONSOLE=y 91*d5af79c0SRandy Dunlap 92*d5af79c0SRandy DunlapKernel config options for kdb 93*d5af79c0SRandy Dunlap----------------------------- 94*d5af79c0SRandy Dunlap 95*d5af79c0SRandy DunlapKdb is quite a bit more complex than the simple gdbstub sitting on top 96*d5af79c0SRandy Dunlapof the kernel's debug core. Kdb must implement a shell, and also adds 97*d5af79c0SRandy Dunlapsome helper functions in other parts of the kernel, responsible for 98*d5af79c0SRandy Dunlapprinting out interesting data such as what you would see if you ran 99*d5af79c0SRandy Dunlap``lsmod``, or ``ps``. In order to build kdb into the kernel you follow the 100*d5af79c0SRandy Dunlapsame steps as you would for kgdb. 101*d5af79c0SRandy Dunlap 102*d5af79c0SRandy DunlapThe main config option for kdb is ``CONFIG_KGDB_KDB`` which is called 103*d5af79c0SRandy Dunlap:menuselection:`KGDB_KDB: include kdb frontend for kgdb` in the config menu. 104*d5af79c0SRandy DunlapIn theory you would have already also selected an I/O driver such as the 105*d5af79c0SRandy Dunlap``CONFIG_KGDB_SERIAL_CONSOLE`` interface if you plan on using kdb on a 106*d5af79c0SRandy Dunlapserial port, when you were configuring kgdb. 107*d5af79c0SRandy Dunlap 108*d5af79c0SRandy DunlapIf you want to use a PS/2-style keyboard with kdb, you would select 109*d5af79c0SRandy Dunlap``CONFIG_KDB_KEYBOARD`` which is called :menuselection:`KGDB_KDB: keyboard as 110*d5af79c0SRandy Dunlapinput device` in the config menu. The ``CONFIG_KDB_KEYBOARD`` option is not 111*d5af79c0SRandy Dunlapused for anything in the gdb interface to kgdb. The ``CONFIG_KDB_KEYBOARD`` 112*d5af79c0SRandy Dunlapoption only works with kdb. 113*d5af79c0SRandy Dunlap 114*d5af79c0SRandy DunlapHere is an example set of ``.config`` symbols to enable/disable kdb:: 115*d5af79c0SRandy Dunlap 116*d5af79c0SRandy Dunlap # CONFIG_STRICT_KERNEL_RWX is not set 117*d5af79c0SRandy Dunlap CONFIG_FRAME_POINTER=y 118*d5af79c0SRandy Dunlap CONFIG_KGDB=y 119*d5af79c0SRandy Dunlap CONFIG_KGDB_SERIAL_CONSOLE=y 120*d5af79c0SRandy Dunlap CONFIG_KGDB_KDB=y 121*d5af79c0SRandy Dunlap CONFIG_KDB_KEYBOARD=y 122*d5af79c0SRandy Dunlap 123*d5af79c0SRandy DunlapKernel Debugger Boot Arguments 124*d5af79c0SRandy Dunlap============================== 125*d5af79c0SRandy Dunlap 126*d5af79c0SRandy DunlapThis section describes the various runtime kernel parameters that affect 127*d5af79c0SRandy Dunlapthe configuration of the kernel debugger. The following chapter covers 128*d5af79c0SRandy Dunlapusing kdb and kgdb as well as providing some examples of the 129*d5af79c0SRandy Dunlapconfiguration parameters. 130*d5af79c0SRandy Dunlap 131*d5af79c0SRandy DunlapKernel parameter: kgdboc 132*d5af79c0SRandy Dunlap------------------------ 133*d5af79c0SRandy Dunlap 134*d5af79c0SRandy DunlapThe kgdboc driver was originally an abbreviation meant to stand for 135*d5af79c0SRandy Dunlap"kgdb over console". Today it is the primary mechanism to configure how 136*d5af79c0SRandy Dunlapto communicate from gdb to kgdb as well as the devices you want to use 137*d5af79c0SRandy Dunlapto interact with the kdb shell. 138*d5af79c0SRandy Dunlap 139*d5af79c0SRandy DunlapFor kgdb/gdb, kgdboc is designed to work with a single serial port. It 140*d5af79c0SRandy Dunlapis intended to cover the circumstance where you want to use a serial 141*d5af79c0SRandy Dunlapconsole as your primary console as well as using it to perform kernel 142*d5af79c0SRandy Dunlapdebugging. It is also possible to use kgdb on a serial port which is not 143*d5af79c0SRandy Dunlapdesignated as a system console. Kgdboc may be configured as a kernel 144*d5af79c0SRandy Dunlapbuilt-in or a kernel loadable module. You can only make use of 145*d5af79c0SRandy Dunlap``kgdbwait`` and early debugging if you build kgdboc into the kernel as 146*d5af79c0SRandy Dunlapa built-in. 147*d5af79c0SRandy Dunlap 148*d5af79c0SRandy DunlapOptionally you can elect to activate kms (Kernel Mode Setting) 149*d5af79c0SRandy Dunlapintegration. When you use kms with kgdboc and you have a video driver 150*d5af79c0SRandy Dunlapthat has atomic mode setting hooks, it is possible to enter the debugger 151*d5af79c0SRandy Dunlapon the graphics console. When the kernel execution is resumed, the 152*d5af79c0SRandy Dunlapprevious graphics mode will be restored. This integration can serve as a 153*d5af79c0SRandy Dunlapuseful tool to aid in diagnosing crashes or doing analysis of memory 154*d5af79c0SRandy Dunlapwith kdb while allowing the full graphics console applications to run. 155*d5af79c0SRandy Dunlap 156*d5af79c0SRandy Dunlapkgdboc arguments 157*d5af79c0SRandy Dunlap~~~~~~~~~~~~~~~~ 158*d5af79c0SRandy Dunlap 159*d5af79c0SRandy DunlapUsage:: 160*d5af79c0SRandy Dunlap 161*d5af79c0SRandy Dunlap kgdboc=[kms][[,]kbd][[,]serial_device][,baud] 162*d5af79c0SRandy Dunlap 163*d5af79c0SRandy DunlapThe order listed above must be observed if you use any of the optional 164*d5af79c0SRandy Dunlapconfigurations together. 165*d5af79c0SRandy Dunlap 166*d5af79c0SRandy DunlapAbbreviations: 167*d5af79c0SRandy Dunlap 168*d5af79c0SRandy Dunlap- kms = Kernel Mode Setting 169*d5af79c0SRandy Dunlap 170*d5af79c0SRandy Dunlap- kbd = Keyboard 171*d5af79c0SRandy Dunlap 172*d5af79c0SRandy DunlapYou can configure kgdboc to use the keyboard, and/or a serial device 173*d5af79c0SRandy Dunlapdepending on if you are using kdb and/or kgdb, in one of the following 174*d5af79c0SRandy Dunlapscenarios. The order listed above must be observed if you use any of the 175*d5af79c0SRandy Dunlapoptional configurations together. Using kms + only gdb is generally not 176*d5af79c0SRandy Dunlapa useful combination. 177*d5af79c0SRandy Dunlap 178*d5af79c0SRandy DunlapUsing loadable module or built-in 179*d5af79c0SRandy Dunlap^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 180*d5af79c0SRandy Dunlap 181*d5af79c0SRandy Dunlap1. As a kernel built-in: 182*d5af79c0SRandy Dunlap 183*d5af79c0SRandy Dunlap Use the kernel boot argument:: 184*d5af79c0SRandy Dunlap 185*d5af79c0SRandy Dunlap kgdboc=<tty-device>,[baud] 186*d5af79c0SRandy Dunlap 187*d5af79c0SRandy Dunlap2. As a kernel loadable module: 188*d5af79c0SRandy Dunlap 189*d5af79c0SRandy Dunlap Use the command:: 190*d5af79c0SRandy Dunlap 191*d5af79c0SRandy Dunlap modprobe kgdboc kgdboc=<tty-device>,[baud] 192*d5af79c0SRandy Dunlap 193*d5af79c0SRandy Dunlap Here are two examples of how you might format the kgdboc string. The 194*d5af79c0SRandy Dunlap first is for an x86 target using the first serial port. The second 195*d5af79c0SRandy Dunlap example is for the ARM Versatile AB using the second serial port. 196*d5af79c0SRandy Dunlap 197*d5af79c0SRandy Dunlap 1. ``kgdboc=ttyS0,115200`` 198*d5af79c0SRandy Dunlap 199*d5af79c0SRandy Dunlap 2. ``kgdboc=ttyAMA1,115200`` 200*d5af79c0SRandy Dunlap 201*d5af79c0SRandy DunlapConfigure kgdboc at runtime with sysfs 202*d5af79c0SRandy Dunlap^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 203*d5af79c0SRandy Dunlap 204*d5af79c0SRandy DunlapAt run time you can enable or disable kgdboc by writing parameters 205*d5af79c0SRandy Dunlapinto sysfs. Here are two examples: 206*d5af79c0SRandy Dunlap 207*d5af79c0SRandy Dunlap1. Enable kgdboc on ttyS0:: 208*d5af79c0SRandy Dunlap 209*d5af79c0SRandy Dunlap echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc 210*d5af79c0SRandy Dunlap 211*d5af79c0SRandy Dunlap2. Disable kgdboc:: 212*d5af79c0SRandy Dunlap 213*d5af79c0SRandy Dunlap echo "" > /sys/module/kgdboc/parameters/kgdboc 214*d5af79c0SRandy Dunlap 215*d5af79c0SRandy Dunlap.. note:: 216*d5af79c0SRandy Dunlap 217*d5af79c0SRandy Dunlap You do not need to specify the baud if you are configuring the 218*d5af79c0SRandy Dunlap console on tty which is already configured or open. 219*d5af79c0SRandy Dunlap 220*d5af79c0SRandy DunlapMore examples 221*d5af79c0SRandy Dunlap^^^^^^^^^^^^^ 222*d5af79c0SRandy Dunlap 223*d5af79c0SRandy DunlapYou can configure kgdboc to use the keyboard, and/or a serial device 224*d5af79c0SRandy Dunlapdepending on if you are using kdb and/or kgdb, in one of the following 225*d5af79c0SRandy Dunlapscenarios. 226*d5af79c0SRandy Dunlap 227*d5af79c0SRandy Dunlap1. kdb and kgdb over only a serial port:: 228*d5af79c0SRandy Dunlap 229*d5af79c0SRandy Dunlap kgdboc=<serial_device>[,baud] 230*d5af79c0SRandy Dunlap 231*d5af79c0SRandy Dunlap Example:: 232*d5af79c0SRandy Dunlap 233*d5af79c0SRandy Dunlap kgdboc=ttyS0,115200 234*d5af79c0SRandy Dunlap 235*d5af79c0SRandy Dunlap2. kdb and kgdb with keyboard and a serial port:: 236*d5af79c0SRandy Dunlap 237*d5af79c0SRandy Dunlap kgdboc=kbd,<serial_device>[,baud] 238*d5af79c0SRandy Dunlap 239*d5af79c0SRandy Dunlap Example:: 240*d5af79c0SRandy Dunlap 241*d5af79c0SRandy Dunlap kgdboc=kbd,ttyS0,115200 242*d5af79c0SRandy Dunlap 243*d5af79c0SRandy Dunlap3. kdb with a keyboard:: 244*d5af79c0SRandy Dunlap 245*d5af79c0SRandy Dunlap kgdboc=kbd 246*d5af79c0SRandy Dunlap 247*d5af79c0SRandy Dunlap4. kdb with kernel mode setting:: 248*d5af79c0SRandy Dunlap 249*d5af79c0SRandy Dunlap kgdboc=kms,kbd 250*d5af79c0SRandy Dunlap 251*d5af79c0SRandy Dunlap5. kdb with kernel mode setting and kgdb over a serial port:: 252*d5af79c0SRandy Dunlap 253*d5af79c0SRandy Dunlap kgdboc=kms,kbd,ttyS0,115200 254*d5af79c0SRandy Dunlap 255*d5af79c0SRandy Dunlap.. note:: 256*d5af79c0SRandy Dunlap 257*d5af79c0SRandy Dunlap Kgdboc does not support interrupting the target via the gdb remote 258*d5af79c0SRandy Dunlap protocol. You must manually send a `SysRq-G` unless you have a proxy 259*d5af79c0SRandy Dunlap that splits console output to a terminal program. A console proxy has a 260*d5af79c0SRandy Dunlap separate TCP port for the debugger and a separate TCP port for the 261*d5af79c0SRandy Dunlap "human" console. The proxy can take care of sending the `SysRq-G` 262*d5af79c0SRandy Dunlap for you. 263*d5af79c0SRandy Dunlap 264*d5af79c0SRandy DunlapWhen using kgdboc with no debugger proxy, you can end up connecting the 265*d5af79c0SRandy Dunlapdebugger at one of two entry points. If an exception occurs after you 266*d5af79c0SRandy Dunlaphave loaded kgdboc, a message should print on the console stating it is 267*d5af79c0SRandy Dunlapwaiting for the debugger. In this case you disconnect your terminal 268*d5af79c0SRandy Dunlapprogram and then connect the debugger in its place. If you want to 269*d5af79c0SRandy Dunlapinterrupt the target system and forcibly enter a debug session you have 270*d5af79c0SRandy Dunlapto issue a `Sysrq` sequence and then type the letter `g`. Then you 271*d5af79c0SRandy Dunlapdisconnect the terminal session and connect gdb. Your options if you 272*d5af79c0SRandy Dunlapdon't like this are to hack gdb to send the `SysRq-G` for you as well as 273*d5af79c0SRandy Dunlapon the initial connect, or to use a debugger proxy that allows an 274*d5af79c0SRandy Dunlapunmodified gdb to do the debugging. 275*d5af79c0SRandy Dunlap 276*d5af79c0SRandy DunlapKernel parameter: ``kgdboc_earlycon`` 277*d5af79c0SRandy Dunlap------------------------------------- 278*d5af79c0SRandy Dunlap 279*d5af79c0SRandy DunlapIf you specify the kernel parameter ``kgdboc_earlycon`` and your serial 280*d5af79c0SRandy Dunlapdriver registers a boot console that supports polling (doesn't need 281*d5af79c0SRandy Dunlapinterrupts and implements a nonblocking read() function) kgdb will attempt 282*d5af79c0SRandy Dunlapto work using the boot console until it can transition to the regular 283*d5af79c0SRandy Dunlaptty driver specified by the ``kgdboc`` parameter. 284*d5af79c0SRandy Dunlap 285*d5af79c0SRandy DunlapNormally there is only one boot console (especially that implements the 286*d5af79c0SRandy Dunlapread() function) so just adding ``kgdboc_earlycon`` on its own is 287*d5af79c0SRandy Dunlapsufficient to make this work. If you have more than one boot console you 288*d5af79c0SRandy Dunlapcan add the boot console's name to differentiate. Note that names that 289*d5af79c0SRandy Dunlapare registered through the boot console layer and the tty layer are not 290*d5af79c0SRandy Dunlapthe same for the same port. 291*d5af79c0SRandy Dunlap 292*d5af79c0SRandy DunlapFor instance, on one board to be explicit you might do:: 293*d5af79c0SRandy Dunlap 294*d5af79c0SRandy Dunlap kgdboc_earlycon=qcom_geni kgdboc=ttyMSM0 295*d5af79c0SRandy Dunlap 296*d5af79c0SRandy DunlapIf the only boot console on the device was "qcom_geni", you could simplify:: 297*d5af79c0SRandy Dunlap 298*d5af79c0SRandy Dunlap kgdboc_earlycon kgdboc=ttyMSM0 299*d5af79c0SRandy Dunlap 300*d5af79c0SRandy DunlapKernel parameter: ``kgdbwait`` 301*d5af79c0SRandy Dunlap------------------------------ 302*d5af79c0SRandy Dunlap 303*d5af79c0SRandy DunlapThe Kernel command line option ``kgdbwait`` makes kgdb wait for a 304*d5af79c0SRandy Dunlapdebugger connection during booting of a kernel. You can only use this 305*d5af79c0SRandy Dunlapoption if you compiled a kgdb I/O driver into the kernel and you 306*d5af79c0SRandy Dunlapspecified the I/O driver configuration as a kernel command line option. 307*d5af79c0SRandy DunlapThe kgdbwait parameter should always follow the configuration parameter 308*d5af79c0SRandy Dunlapfor the kgdb I/O driver in the kernel command line else the I/O driver 309*d5af79c0SRandy Dunlapwill not be configured prior to asking the kernel to use it to wait. 310*d5af79c0SRandy Dunlap 311*d5af79c0SRandy DunlapThe kernel will stop and wait as early as the I/O driver and 312*d5af79c0SRandy Dunlaparchitecture allows when you use this option. If you build the kgdb I/O 313*d5af79c0SRandy Dunlapdriver as a loadable kernel module kgdbwait will not do anything. 314*d5af79c0SRandy Dunlap 315*d5af79c0SRandy DunlapKernel parameter: ``kgdbcon`` 316*d5af79c0SRandy Dunlap----------------------------- 317*d5af79c0SRandy Dunlap 318*d5af79c0SRandy DunlapThe ``kgdbcon`` feature allows you to see printk() messages inside gdb 319*d5af79c0SRandy Dunlapwhile gdb is connected to the kernel. Kdb does not make use of the kgdbcon 320*d5af79c0SRandy Dunlapfeature. 321*d5af79c0SRandy Dunlap 322*d5af79c0SRandy DunlapKgdb supports using the gdb serial protocol to send console messages to 323*d5af79c0SRandy Dunlapthe debugger when the debugger is connected and running. There are two 324*d5af79c0SRandy Dunlapways to activate this feature. 325*d5af79c0SRandy Dunlap 326*d5af79c0SRandy Dunlap1. Activate with the kernel command line option:: 327*d5af79c0SRandy Dunlap 328*d5af79c0SRandy Dunlap kgdbcon 329*d5af79c0SRandy Dunlap 330*d5af79c0SRandy Dunlap2. Use sysfs before configuring an I/O driver:: 331*d5af79c0SRandy Dunlap 332*d5af79c0SRandy Dunlap echo 1 > /sys/module/debug_core/parameters/kgdb_use_con 333*d5af79c0SRandy Dunlap 334*d5af79c0SRandy Dunlap.. note:: 335*d5af79c0SRandy Dunlap 336*d5af79c0SRandy Dunlap If you do this after you configure the kgdb I/O driver, the 337*d5af79c0SRandy Dunlap setting will not take effect until the next point the I/O is 338*d5af79c0SRandy Dunlap reconfigured. 339*d5af79c0SRandy Dunlap 340*d5af79c0SRandy Dunlap.. important:: 341*d5af79c0SRandy Dunlap 342*d5af79c0SRandy Dunlap You cannot use kgdboc + kgdbcon on a tty that is an 343*d5af79c0SRandy Dunlap active system console. An example of incorrect usage is:: 344*d5af79c0SRandy Dunlap 345*d5af79c0SRandy Dunlap console=ttyS0,115200 kgdboc=ttyS0 kgdbcon 346*d5af79c0SRandy Dunlap 347*d5af79c0SRandy DunlapIt is possible to use this option with kgdboc on a tty that is not a 348*d5af79c0SRandy Dunlapsystem console. 349*d5af79c0SRandy Dunlap 350*d5af79c0SRandy DunlapRun time parameter: ``kgdbreboot`` 351*d5af79c0SRandy Dunlap---------------------------------- 352*d5af79c0SRandy Dunlap 353*d5af79c0SRandy DunlapThe kgdbreboot feature allows you to change how the debugger deals with 354*d5af79c0SRandy Dunlapthe reboot notification. You have 3 choices for the behavior. The 355*d5af79c0SRandy Dunlapdefault behavior is always set to 0. 356*d5af79c0SRandy Dunlap 357*d5af79c0SRandy Dunlap.. tabularcolumns:: |p{0.4cm}|p{11.5cm}|p{5.6cm}| 358*d5af79c0SRandy Dunlap 359*d5af79c0SRandy Dunlap.. flat-table:: 360*d5af79c0SRandy Dunlap :widths: 1 10 8 361*d5af79c0SRandy Dunlap 362*d5af79c0SRandy Dunlap * - 1 363*d5af79c0SRandy Dunlap - ``echo -1 > /sys/module/debug_core/parameters/kgdbreboot`` 364*d5af79c0SRandy Dunlap - Ignore the reboot notification entirely. 365*d5af79c0SRandy Dunlap 366*d5af79c0SRandy Dunlap * - 2 367*d5af79c0SRandy Dunlap - ``echo 0 > /sys/module/debug_core/parameters/kgdbreboot`` 368*d5af79c0SRandy Dunlap - Send the detach message to any attached debugger client. 369*d5af79c0SRandy Dunlap 370*d5af79c0SRandy Dunlap * - 3 371*d5af79c0SRandy Dunlap - ``echo 1 > /sys/module/debug_core/parameters/kgdbreboot`` 372*d5af79c0SRandy Dunlap - Enter the debugger on reboot notify. 373*d5af79c0SRandy Dunlap 374*d5af79c0SRandy DunlapKernel parameter: ``nokaslr`` 375*d5af79c0SRandy Dunlap----------------------------- 376*d5af79c0SRandy Dunlap 377*d5af79c0SRandy DunlapIf the architecture that you are using enables KASLR by default, 378*d5af79c0SRandy Dunlapyou should consider turning it off. KASLR randomizes the 379*d5af79c0SRandy Dunlapvirtual address where the kernel image is mapped and confuses 380*d5af79c0SRandy Dunlapgdb which resolves addresses of kernel symbols from the symbol table 381*d5af79c0SRandy Dunlapof vmlinux. 382*d5af79c0SRandy Dunlap 383*d5af79c0SRandy DunlapUsing kdb 384*d5af79c0SRandy Dunlap========= 385*d5af79c0SRandy Dunlap 386*d5af79c0SRandy DunlapQuick start for kdb on a serial port 387*d5af79c0SRandy Dunlap------------------------------------ 388*d5af79c0SRandy Dunlap 389*d5af79c0SRandy DunlapThis is a quick example of how to use kdb. 390*d5af79c0SRandy Dunlap 391*d5af79c0SRandy Dunlap1. Configure kgdboc at boot using kernel parameters:: 392*d5af79c0SRandy Dunlap 393*d5af79c0SRandy Dunlap console=ttyS0,115200 kgdboc=ttyS0,115200 nokaslr 394*d5af79c0SRandy Dunlap 395*d5af79c0SRandy Dunlap OR 396*d5af79c0SRandy Dunlap 397*d5af79c0SRandy Dunlap Configure kgdboc after the kernel has booted; assuming you are using 398*d5af79c0SRandy Dunlap a serial port console:: 399*d5af79c0SRandy Dunlap 400*d5af79c0SRandy Dunlap echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc 401*d5af79c0SRandy Dunlap 402*d5af79c0SRandy Dunlap2. Enter the kernel debugger manually or by waiting for an oops or 403*d5af79c0SRandy Dunlap fault. There are several ways you can enter the kernel debugger 404*d5af79c0SRandy Dunlap manually; all involve using the `SysRq-G`, which means you must have 405*d5af79c0SRandy Dunlap enabled ``CONFIG_MAGIC_SYSRQ=y`` in your kernel config. 406*d5af79c0SRandy Dunlap 407*d5af79c0SRandy Dunlap - When logged in as root or with a super user session you can run:: 408*d5af79c0SRandy Dunlap 409*d5af79c0SRandy Dunlap echo g > /proc/sysrq-trigger 410*d5af79c0SRandy Dunlap 411*d5af79c0SRandy Dunlap - Example using minicom 2.2 412*d5af79c0SRandy Dunlap 413*d5af79c0SRandy Dunlap Press: `CTRL-A` `f` `g` 414*d5af79c0SRandy Dunlap 415*d5af79c0SRandy Dunlap - When you have telneted to a terminal server that supports sending 416*d5af79c0SRandy Dunlap a remote break 417*d5af79c0SRandy Dunlap 418*d5af79c0SRandy Dunlap Press: `CTRL-]` 419*d5af79c0SRandy Dunlap 420*d5af79c0SRandy Dunlap Type in: ``send break`` 421*d5af79c0SRandy Dunlap 422*d5af79c0SRandy Dunlap Press: `Enter` `g` 423*d5af79c0SRandy Dunlap 424*d5af79c0SRandy Dunlap3. From the kdb prompt you can run the ``help`` command to see a complete 425*d5af79c0SRandy Dunlap list of the commands that are available. 426*d5af79c0SRandy Dunlap 427*d5af79c0SRandy Dunlap Some useful commands in kdb include: 428*d5af79c0SRandy Dunlap 429*d5af79c0SRandy Dunlap =========== ================================================================= 430*d5af79c0SRandy Dunlap ``lsmod`` Shows where kernel modules are loaded 431*d5af79c0SRandy Dunlap ``ps`` Displays only the active processes 432*d5af79c0SRandy Dunlap ``ps A`` Shows all the processes 433*d5af79c0SRandy Dunlap ``summary`` Shows kernel version info and memory usage 434*d5af79c0SRandy Dunlap ``bt`` Get a backtrace of the current process using dump_stack() 435*d5af79c0SRandy Dunlap ``dmesg`` View the kernel syslog buffer 436*d5af79c0SRandy Dunlap ``go`` Continue the system 437*d5af79c0SRandy Dunlap =========== ================================================================= 438*d5af79c0SRandy Dunlap 439*d5af79c0SRandy Dunlap4. When you are done using kdb you need to consider rebooting the system 440*d5af79c0SRandy Dunlap or using the ``go`` command to resuming normal kernel execution. If you 441*d5af79c0SRandy Dunlap have paused the kernel for a lengthy period of time, applications 442*d5af79c0SRandy Dunlap that rely on timely networking or anything to do with real wall clock 443*d5af79c0SRandy Dunlap time could be adversely affected, so you should take this into 444*d5af79c0SRandy Dunlap consideration when using the kernel debugger. 445*d5af79c0SRandy Dunlap 446*d5af79c0SRandy DunlapQuick start for kdb using a keyboard connected console 447*d5af79c0SRandy Dunlap------------------------------------------------------ 448*d5af79c0SRandy Dunlap 449*d5af79c0SRandy DunlapThis is a quick example of how to use kdb with a keyboard. 450*d5af79c0SRandy Dunlap 451*d5af79c0SRandy Dunlap1. Configure kgdboc at boot using kernel parameters:: 452*d5af79c0SRandy Dunlap 453*d5af79c0SRandy Dunlap kgdboc=kbd 454*d5af79c0SRandy Dunlap 455*d5af79c0SRandy Dunlap OR 456*d5af79c0SRandy Dunlap 457*d5af79c0SRandy Dunlap Configure kgdboc after the kernel has booted:: 458*d5af79c0SRandy Dunlap 459*d5af79c0SRandy Dunlap echo kbd > /sys/module/kgdboc/parameters/kgdboc 460*d5af79c0SRandy Dunlap 461*d5af79c0SRandy Dunlap2. Enter the kernel debugger manually or by waiting for an oops or 462*d5af79c0SRandy Dunlap fault. There are several ways you can enter the kernel debugger 463*d5af79c0SRandy Dunlap manually; all involve using the `SysRq-G`, which means you must have 464*d5af79c0SRandy Dunlap enabled ``CONFIG_MAGIC_SYSRQ=y`` in your kernel config. 465*d5af79c0SRandy Dunlap 466*d5af79c0SRandy Dunlap - When logged in as root or with a super user session you can run:: 467*d5af79c0SRandy Dunlap 468*d5af79c0SRandy Dunlap echo g > /proc/sysrq-trigger 469*d5af79c0SRandy Dunlap 470*d5af79c0SRandy Dunlap - Example using a laptop keyboard: 471*d5af79c0SRandy Dunlap 472*d5af79c0SRandy Dunlap Press and hold down: `Alt` 473*d5af79c0SRandy Dunlap 474*d5af79c0SRandy Dunlap Press and hold down: `Fn` 475*d5af79c0SRandy Dunlap 476*d5af79c0SRandy Dunlap Press and release the key with the label: `SysRq` 477*d5af79c0SRandy Dunlap 478*d5af79c0SRandy Dunlap Release: `Fn` 479*d5af79c0SRandy Dunlap 480*d5af79c0SRandy Dunlap Press and release: `g` 481*d5af79c0SRandy Dunlap 482*d5af79c0SRandy Dunlap Release: `Alt` 483*d5af79c0SRandy Dunlap 484*d5af79c0SRandy Dunlap - Example using a PS/2 101-key keyboard 485*d5af79c0SRandy Dunlap 486*d5af79c0SRandy Dunlap Press and hold down: `Alt` 487*d5af79c0SRandy Dunlap 488*d5af79c0SRandy Dunlap Press and release the key with the label: `SysRq` 489*d5af79c0SRandy Dunlap 490*d5af79c0SRandy Dunlap Press and release: `g` 491*d5af79c0SRandy Dunlap 492*d5af79c0SRandy Dunlap Release: `Alt` 493*d5af79c0SRandy Dunlap 494*d5af79c0SRandy Dunlap3. Now type in a kdb command such as ``help``, ``dmesg``, ``bt`` or ``go`` to 495*d5af79c0SRandy Dunlap continue kernel execution. 496*d5af79c0SRandy Dunlap 497*d5af79c0SRandy DunlapUsing kgdb / gdb 498*d5af79c0SRandy Dunlap================ 499*d5af79c0SRandy Dunlap 500*d5af79c0SRandy DunlapIn order to use kgdb you must activate it by passing configuration 501*d5af79c0SRandy Dunlapinformation to one of the kgdb I/O drivers. If you do not pass any 502*d5af79c0SRandy Dunlapconfiguration information kgdb will not do anything at all. Kgdb will 503*d5af79c0SRandy Dunlaponly actively hook up to the kernel trap hooks if a kgdb I/O driver is 504*d5af79c0SRandy Dunlaploaded and configured. If you unconfigure a kgdb I/O driver, kgdb will 505*d5af79c0SRandy Dunlapunregister all the kernel hook points. 506*d5af79c0SRandy Dunlap 507*d5af79c0SRandy DunlapAll kgdb I/O drivers can be reconfigured at run time, if 508*d5af79c0SRandy Dunlap``CONFIG_SYSFS`` and ``CONFIG_MODULES`` are enabled, by echo'ing a new 509*d5af79c0SRandy Dunlapconfig string to ``/sys/module/<driver>/parameter/<option>``. The driver 510*d5af79c0SRandy Dunlapcan be unconfigured by passing an empty string. You cannot change the 511*d5af79c0SRandy Dunlapconfiguration while the debugger is attached. Make sure to detach the 512*d5af79c0SRandy Dunlapdebugger with the ``detach`` command prior to trying to unconfigure a 513*d5af79c0SRandy Dunlapkgdb I/O driver. 514*d5af79c0SRandy Dunlap 515*d5af79c0SRandy DunlapConnecting with gdb to a serial port 516*d5af79c0SRandy Dunlap------------------------------------ 517*d5af79c0SRandy Dunlap 518*d5af79c0SRandy Dunlap1. Configure kgdboc 519*d5af79c0SRandy Dunlap 520*d5af79c0SRandy Dunlap Configure kgdboc at boot using kernel parameters:: 521*d5af79c0SRandy Dunlap 522*d5af79c0SRandy Dunlap kgdboc=ttyS0,115200 523*d5af79c0SRandy Dunlap 524*d5af79c0SRandy Dunlap OR 525*d5af79c0SRandy Dunlap 526*d5af79c0SRandy Dunlap Configure kgdboc after the kernel has booted:: 527*d5af79c0SRandy Dunlap 528*d5af79c0SRandy Dunlap echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc 529*d5af79c0SRandy Dunlap 530*d5af79c0SRandy Dunlap2. Stop kernel execution (break into the debugger) 531*d5af79c0SRandy Dunlap 532*d5af79c0SRandy Dunlap In order to connect to gdb via kgdboc, the kernel must first be 533*d5af79c0SRandy Dunlap stopped. There are several ways to stop the kernel which include 534*d5af79c0SRandy Dunlap using kgdbwait as a boot argument, via a `SysRq-G`, or running the 535*d5af79c0SRandy Dunlap kernel until it takes an exception where it waits for the debugger to 536*d5af79c0SRandy Dunlap attach. 537*d5af79c0SRandy Dunlap 538*d5af79c0SRandy Dunlap - When logged in as root or with a super user session you can run:: 539*d5af79c0SRandy Dunlap 540*d5af79c0SRandy Dunlap echo g > /proc/sysrq-trigger 541*d5af79c0SRandy Dunlap 542*d5af79c0SRandy Dunlap - Example using minicom 2.2 543*d5af79c0SRandy Dunlap 544*d5af79c0SRandy Dunlap Press: `CTRL-A` `f` `g` 545*d5af79c0SRandy Dunlap 546*d5af79c0SRandy Dunlap - When you have telneted to a terminal server that supports sending 547*d5af79c0SRandy Dunlap a remote break 548*d5af79c0SRandy Dunlap 549*d5af79c0SRandy Dunlap Press: `CTRL-]` 550*d5af79c0SRandy Dunlap 551*d5af79c0SRandy Dunlap Type in: ``send break`` 552*d5af79c0SRandy Dunlap 553*d5af79c0SRandy Dunlap Press: `Enter` `g` 554*d5af79c0SRandy Dunlap 555*d5af79c0SRandy Dunlap3. Connect from gdb 556*d5af79c0SRandy Dunlap 557*d5af79c0SRandy Dunlap Example (using a directly connected port):: 558*d5af79c0SRandy Dunlap 559*d5af79c0SRandy Dunlap % gdb ./vmlinux 560*d5af79c0SRandy Dunlap (gdb) set serial baud 115200 561*d5af79c0SRandy Dunlap (gdb) target remote /dev/ttyS0 562*d5af79c0SRandy Dunlap 563*d5af79c0SRandy Dunlap 564*d5af79c0SRandy Dunlap Example (kgdb to a terminal server on TCP port 2012):: 565*d5af79c0SRandy Dunlap 566*d5af79c0SRandy Dunlap % gdb ./vmlinux 567*d5af79c0SRandy Dunlap (gdb) target remote 192.168.2.2:2012 568*d5af79c0SRandy Dunlap 569*d5af79c0SRandy Dunlap 570*d5af79c0SRandy Dunlap Once connected, you can debug a kernel the way you would debug an 571*d5af79c0SRandy Dunlap application program. 572*d5af79c0SRandy Dunlap 573*d5af79c0SRandy Dunlap If you are having problems connecting or something is going seriously 574*d5af79c0SRandy Dunlap wrong while debugging, it will most often be the case that you want 575*d5af79c0SRandy Dunlap to enable gdb to be verbose about its target communications. You do 576*d5af79c0SRandy Dunlap this prior to issuing the ``target remote`` command by typing in:: 577*d5af79c0SRandy Dunlap 578*d5af79c0SRandy Dunlap set debug remote 1 579*d5af79c0SRandy Dunlap 580*d5af79c0SRandy DunlapRemember if you continue in gdb, and need to "break in" again, you need 581*d5af79c0SRandy Dunlapto issue an other `SysRq-G`. It is easy to create a simple entry point by 582*d5af79c0SRandy Dunlapputting a breakpoint at ``sys_sync`` and then you can run ``sync`` from a 583*d5af79c0SRandy Dunlapshell or script to break into the debugger. 584*d5af79c0SRandy Dunlap 585*d5af79c0SRandy Dunlapkgdb and kdb interoperability 586*d5af79c0SRandy Dunlap============================= 587*d5af79c0SRandy Dunlap 588*d5af79c0SRandy DunlapIt is possible to transition between kdb and kgdb dynamically. The debug 589*d5af79c0SRandy Dunlapcore will remember which you used the last time and automatically start 590*d5af79c0SRandy Dunlapin the same mode. 591*d5af79c0SRandy Dunlap 592*d5af79c0SRandy DunlapSwitching between kdb and kgdb 593*d5af79c0SRandy Dunlap------------------------------ 594*d5af79c0SRandy Dunlap 595*d5af79c0SRandy DunlapSwitching from kgdb to kdb 596*d5af79c0SRandy Dunlap~~~~~~~~~~~~~~~~~~~~~~~~~~ 597*d5af79c0SRandy Dunlap 598*d5af79c0SRandy DunlapThere are two ways to switch from kgdb to kdb: you can use gdb to issue 599*d5af79c0SRandy Dunlapa maintenance packet, or you can blindly type the command ``$3#33``. 600*d5af79c0SRandy DunlapWhenever the kernel debugger stops in kgdb mode it will print the 601*d5af79c0SRandy Dunlapmessage ``KGDB or $3#33 for KDB``. It is important to note that you have 602*d5af79c0SRandy Dunlapto type the sequence correctly in one pass. You cannot type a backspace 603*d5af79c0SRandy Dunlapor delete because kgdb will interpret that as part of the debug stream. 604*d5af79c0SRandy Dunlap 605*d5af79c0SRandy Dunlap1. Change from kgdb to kdb by blindly typing:: 606*d5af79c0SRandy Dunlap 607*d5af79c0SRandy Dunlap $3#33 608*d5af79c0SRandy Dunlap 609*d5af79c0SRandy Dunlap2. Change from kgdb to kdb with gdb:: 610*d5af79c0SRandy Dunlap 611*d5af79c0SRandy Dunlap maintenance packet 3 612*d5af79c0SRandy Dunlap 613*d5af79c0SRandy Dunlap .. note:: 614*d5af79c0SRandy Dunlap 615*d5af79c0SRandy Dunlap Now you must kill gdb. Typically you press `CTRL-Z` and issue 616*d5af79c0SRandy Dunlap the command:: 617*d5af79c0SRandy Dunlap 618*d5af79c0SRandy Dunlap kill -9 % 619*d5af79c0SRandy Dunlap 620*d5af79c0SRandy DunlapChange from kdb to kgdb 621*d5af79c0SRandy Dunlap~~~~~~~~~~~~~~~~~~~~~~~ 622*d5af79c0SRandy Dunlap 623*d5af79c0SRandy DunlapThere are two ways you can change from kdb to kgdb. You can manually 624*d5af79c0SRandy Dunlapenter kgdb mode by issuing the kgdb command from the kdb shell prompt, 625*d5af79c0SRandy Dunlapor you can connect gdb while the kdb shell prompt is active. The kdb 626*d5af79c0SRandy Dunlapshell looks for the typical first commands that gdb would issue with the 627*d5af79c0SRandy Dunlapgdb remote protocol and if it sees one of those commands it 628*d5af79c0SRandy Dunlapautomatically changes into kgdb mode. 629*d5af79c0SRandy Dunlap 630*d5af79c0SRandy Dunlap1. From kdb issue the command:: 631*d5af79c0SRandy Dunlap 632*d5af79c0SRandy Dunlap kgdb 633*d5af79c0SRandy Dunlap 634*d5af79c0SRandy Dunlap2. At the kdb prompt, disconnect the terminal program and connect gdb in 635*d5af79c0SRandy Dunlap its place. 636*d5af79c0SRandy Dunlap 637*d5af79c0SRandy DunlapRunning kdb commands from gdb 638*d5af79c0SRandy Dunlap----------------------------- 639*d5af79c0SRandy Dunlap 640*d5af79c0SRandy DunlapIt is possible to run a limited set of kdb commands from gdb, using the 641*d5af79c0SRandy Dunlapgdb monitor command. You don't want to execute any of the run control or 642*d5af79c0SRandy Dunlapbreakpoint operations, because it can disrupt the state of the kernel 643*d5af79c0SRandy Dunlapdebugger. You should be using gdb for breakpoints and run control 644*d5af79c0SRandy Dunlapoperations if you have gdb connected. The more useful commands to run 645*d5af79c0SRandy Dunlapare things like lsmod, dmesg, ps or possibly some of the memory 646*d5af79c0SRandy Dunlapinformation commands. To see all the kdb commands you can run 647*d5af79c0SRandy Dunlap``monitor help``. 648*d5af79c0SRandy Dunlap 649*d5af79c0SRandy DunlapExample:: 650*d5af79c0SRandy Dunlap 651*d5af79c0SRandy Dunlap (gdb) monitor ps 652*d5af79c0SRandy Dunlap 1 idle process (state I) and 653*d5af79c0SRandy Dunlap 27 sleeping system daemon (state M) processes suppressed, 654*d5af79c0SRandy Dunlap use 'ps A' to see all. 655*d5af79c0SRandy Dunlap Task Addr Pid Parent [*] cpu State Thread Command 656*d5af79c0SRandy Dunlap 657*d5af79c0SRandy Dunlap 0xc78291d0 1 0 0 0 S 0xc7829404 init 658*d5af79c0SRandy Dunlap 0xc7954150 942 1 0 0 S 0xc7954384 dropbear 659*d5af79c0SRandy Dunlap 0xc78789c0 944 1 0 0 S 0xc7878bf4 sh 660*d5af79c0SRandy Dunlap (gdb) 661*d5af79c0SRandy Dunlap 662*d5af79c0SRandy Dunlapkgdb Test Suite 663*d5af79c0SRandy Dunlap=============== 664*d5af79c0SRandy Dunlap 665*d5af79c0SRandy DunlapWhen kgdb is enabled in the kernel config you can also elect to enable 666*d5af79c0SRandy Dunlapthe config parameter ``KGDB_TESTS``. Turning this on will enable a special 667*d5af79c0SRandy Dunlapkgdb I/O module which is designed to test the kgdb internal functions. 668*d5af79c0SRandy Dunlap 669*d5af79c0SRandy DunlapThe kgdb tests are mainly intended for developers to test the kgdb 670*d5af79c0SRandy Dunlapinternals as well as a tool for developing a new kgdb architecture 671*d5af79c0SRandy Dunlapspecific implementation. These tests are not really for end users of the 672*d5af79c0SRandy DunlapLinux kernel. The primary source of documentation would be to look in 673*d5af79c0SRandy Dunlapthe ``drivers/misc/kgdbts.c`` file. 674*d5af79c0SRandy Dunlap 675*d5af79c0SRandy DunlapThe kgdb test suite can also be configured at compile time to run the 676*d5af79c0SRandy Dunlapcore set of tests by setting the kernel config parameter 677*d5af79c0SRandy Dunlap``KGDB_TESTS_ON_BOOT``. This particular option is aimed at automated 678*d5af79c0SRandy Dunlapregression testing and does not require modifying the kernel boot config 679*d5af79c0SRandy Dunlaparguments. If this is turned on, the kgdb test suite can be disabled by 680*d5af79c0SRandy Dunlapspecifying ``kgdbts=`` as a kernel boot argument. 681*d5af79c0SRandy Dunlap 682*d5af79c0SRandy DunlapKernel Debugger Internals 683*d5af79c0SRandy Dunlap========================= 684*d5af79c0SRandy Dunlap 685*d5af79c0SRandy DunlapArchitecture Specifics 686*d5af79c0SRandy Dunlap---------------------- 687*d5af79c0SRandy Dunlap 688*d5af79c0SRandy DunlapThe kernel debugger is organized into a number of components: 689*d5af79c0SRandy Dunlap 690*d5af79c0SRandy Dunlap1. The debug core 691*d5af79c0SRandy Dunlap 692*d5af79c0SRandy Dunlap The debug core is found in ``kernel/debugger/debug_core.c``. It 693*d5af79c0SRandy Dunlap contains: 694*d5af79c0SRandy Dunlap 695*d5af79c0SRandy Dunlap - A generic OS exception handler which includes sync'ing the 696*d5af79c0SRandy Dunlap processors into a stopped state on an multi-CPU system. 697*d5af79c0SRandy Dunlap 698*d5af79c0SRandy Dunlap - The API to talk to the kgdb I/O drivers 699*d5af79c0SRandy Dunlap 700*d5af79c0SRandy Dunlap - The API to make calls to the arch-specific kgdb implementation 701*d5af79c0SRandy Dunlap 702*d5af79c0SRandy Dunlap - The logic to perform safe memory reads and writes to memory while 703*d5af79c0SRandy Dunlap using the debugger 704*d5af79c0SRandy Dunlap 705*d5af79c0SRandy Dunlap - A full implementation for software breakpoints unless overridden 706*d5af79c0SRandy Dunlap by the arch 707*d5af79c0SRandy Dunlap 708*d5af79c0SRandy Dunlap - The API to invoke either the kdb or kgdb frontend to the debug 709*d5af79c0SRandy Dunlap core. 710*d5af79c0SRandy Dunlap 711*d5af79c0SRandy Dunlap - The structures and callback API for atomic kernel mode setting. 712*d5af79c0SRandy Dunlap 713*d5af79c0SRandy Dunlap .. note:: kgdboc is where the kms callbacks are invoked. 714*d5af79c0SRandy Dunlap 715*d5af79c0SRandy Dunlap2. kgdb arch-specific implementation 716*d5af79c0SRandy Dunlap 717*d5af79c0SRandy Dunlap This implementation is generally found in ``arch/*/kernel/kgdb.c``. As 718*d5af79c0SRandy Dunlap an example, ``arch/x86/kernel/kgdb.c`` contains the specifics to 719*d5af79c0SRandy Dunlap implement HW breakpoint as well as the initialization to dynamically 720*d5af79c0SRandy Dunlap register and unregister for the trap handlers on this architecture. 721*d5af79c0SRandy Dunlap The arch-specific portion implements: 722*d5af79c0SRandy Dunlap 723*d5af79c0SRandy Dunlap - contains an arch-specific trap catcher which invokes 724*d5af79c0SRandy Dunlap kgdb_handle_exception() to start kgdb about doing its work 725*d5af79c0SRandy Dunlap 726*d5af79c0SRandy Dunlap - translation to and from gdb specific packet format to struct pt_regs 727*d5af79c0SRandy Dunlap 728*d5af79c0SRandy Dunlap - Registration and unregistration of architecture specific trap 729*d5af79c0SRandy Dunlap hooks 730*d5af79c0SRandy Dunlap 731*d5af79c0SRandy Dunlap - Any special exception handling and cleanup 732*d5af79c0SRandy Dunlap 733*d5af79c0SRandy Dunlap - NMI exception handling and cleanup 734*d5af79c0SRandy Dunlap 735*d5af79c0SRandy Dunlap - (optional) HW breakpoints 736*d5af79c0SRandy Dunlap 737*d5af79c0SRandy Dunlap3. gdbstub frontend (aka kgdb) 738*d5af79c0SRandy Dunlap 739*d5af79c0SRandy Dunlap The gdbstub is located in ``kernel/debug/gdbstub.c``. It contains: 740*d5af79c0SRandy Dunlap 741*d5af79c0SRandy Dunlap - All the logic to implement the gdb serial protocol 742*d5af79c0SRandy Dunlap 743*d5af79c0SRandy Dunlap4. kdb frontend 744*d5af79c0SRandy Dunlap 745*d5af79c0SRandy Dunlap The kdb debugger shell is broken down into a number of components. 746*d5af79c0SRandy Dunlap The kdb core is located in kernel/debug/kdb. There are a number of 747*d5af79c0SRandy Dunlap helper functions in some of the other kernel components to make it 748*d5af79c0SRandy Dunlap possible for kdb to examine and report information about the kernel 749*d5af79c0SRandy Dunlap without taking locks that could cause a kernel deadlock. The kdb core 750*d5af79c0SRandy Dunlap implements the following functionality. 751*d5af79c0SRandy Dunlap 752*d5af79c0SRandy Dunlap - A simple shell 753*d5af79c0SRandy Dunlap 754*d5af79c0SRandy Dunlap - The kdb core command set 755*d5af79c0SRandy Dunlap 756*d5af79c0SRandy Dunlap - A registration API to register additional kdb shell commands. 757*d5af79c0SRandy Dunlap 758*d5af79c0SRandy Dunlap - A good example of a self-contained kdb module is the ``ftdump`` 759*d5af79c0SRandy Dunlap command for dumping the ftrace buffer. See: 760*d5af79c0SRandy Dunlap ``kernel/trace/trace_kdb.c`` 761*d5af79c0SRandy Dunlap 762*d5af79c0SRandy Dunlap - For an example of how to dynamically register a new kdb command 763*d5af79c0SRandy Dunlap you can build the kdb_hello.ko kernel module from 764*d5af79c0SRandy Dunlap ``samples/kdb/kdb_hello.c``. To build this example you can set 765*d5af79c0SRandy Dunlap ``CONFIG_SAMPLES=y`` and ``CONFIG_SAMPLE_KDB=m`` in your kernel 766*d5af79c0SRandy Dunlap config. Later run ``modprobe kdb_hello`` and the next time you 767*d5af79c0SRandy Dunlap enter the kdb shell, you can run the ``hello`` command. 768*d5af79c0SRandy Dunlap 769*d5af79c0SRandy Dunlap - The implementation for kdb_printf() which emits messages directly 770*d5af79c0SRandy Dunlap to I/O drivers, bypassing the kernel log. 771*d5af79c0SRandy Dunlap 772*d5af79c0SRandy Dunlap - SW / HW breakpoint management for the kdb shell 773*d5af79c0SRandy Dunlap 774*d5af79c0SRandy Dunlap5. kgdb I/O driver 775*d5af79c0SRandy Dunlap 776*d5af79c0SRandy Dunlap Each kgdb I/O driver has to provide an implementation for the 777*d5af79c0SRandy Dunlap following: 778*d5af79c0SRandy Dunlap 779*d5af79c0SRandy Dunlap - configuration via built-in or module 780*d5af79c0SRandy Dunlap 781*d5af79c0SRandy Dunlap - dynamic configuration and kgdb hook registration calls 782*d5af79c0SRandy Dunlap 783*d5af79c0SRandy Dunlap - read and write character interface 784*d5af79c0SRandy Dunlap 785*d5af79c0SRandy Dunlap - A cleanup handler for unconfiguring from the kgdb core 786*d5af79c0SRandy Dunlap 787*d5af79c0SRandy Dunlap - (optional) Early debug methodology 788*d5af79c0SRandy Dunlap 789*d5af79c0SRandy Dunlap Any given kgdb I/O driver has to operate very closely with the 790*d5af79c0SRandy Dunlap hardware and must do it in such a way that does not enable interrupts 791*d5af79c0SRandy Dunlap or change other parts of the system context without completely 792*d5af79c0SRandy Dunlap restoring them. The kgdb core will repeatedly "poll" a kgdb I/O 793*d5af79c0SRandy Dunlap driver for characters when it needs input. The I/O driver is expected 794*d5af79c0SRandy Dunlap to return immediately if there is no data available. Doing so allows 795*d5af79c0SRandy Dunlap for the future possibility to touch watchdog hardware in such a way 796*d5af79c0SRandy Dunlap as to have a target system not reset when these are enabled. 797*d5af79c0SRandy Dunlap 798*d5af79c0SRandy DunlapIf you are intent on adding kgdb architecture specific support for a new 799*d5af79c0SRandy Dunlaparchitecture, the architecture should define ``HAVE_ARCH_KGDB`` in the 800*d5af79c0SRandy Dunlaparchitecture specific Kconfig file. This will enable kgdb for the 801*d5af79c0SRandy Dunlaparchitecture, and at that point you must create an architecture specific 802*d5af79c0SRandy Dunlapkgdb implementation. 803*d5af79c0SRandy Dunlap 804*d5af79c0SRandy DunlapThere are a few flags which must be set on every architecture in their 805*d5af79c0SRandy Dunlap``asm/kgdb.h`` file. These are: 806*d5af79c0SRandy Dunlap 807*d5af79c0SRandy Dunlap- ``NUMREGBYTES``: 808*d5af79c0SRandy Dunlap The size in bytes of all of the registers, so that we 809*d5af79c0SRandy Dunlap can ensure they will all fit into a packet. 810*d5af79c0SRandy Dunlap 811*d5af79c0SRandy Dunlap- ``BUFMAX``: 812*d5af79c0SRandy Dunlap The size in bytes of the buffer GDB will read into. This must 813*d5af79c0SRandy Dunlap be larger than NUMREGBYTES. 814*d5af79c0SRandy Dunlap 815*d5af79c0SRandy Dunlap- ``CACHE_FLUSH_IS_SAFE``: 816*d5af79c0SRandy Dunlap Set to 1 if it is always safe to call 817*d5af79c0SRandy Dunlap flush_cache_range or flush_icache_range. On some architectures, 818*d5af79c0SRandy Dunlap these functions may not be safe to call on SMP since we keep other 819*d5af79c0SRandy Dunlap CPUs in a holding pattern. 820*d5af79c0SRandy Dunlap 821*d5af79c0SRandy DunlapThere are also the following functions for the common backend, found in 822*d5af79c0SRandy Dunlap``kernel/kgdb.c``, that must be supplied by the architecture-specific 823*d5af79c0SRandy Dunlapbackend unless marked as (optional), in which case a default function 824*d5af79c0SRandy Dunlapmaybe used if the architecture does not need to provide a specific 825*d5af79c0SRandy Dunlapimplementation. 826*d5af79c0SRandy Dunlap 827*d5af79c0SRandy Dunlap.. kernel-doc:: include/linux/kgdb.h 828*d5af79c0SRandy Dunlap :internal: 829*d5af79c0SRandy Dunlap 830*d5af79c0SRandy Dunlapkgdboc internals 831*d5af79c0SRandy Dunlap---------------- 832*d5af79c0SRandy Dunlap 833*d5af79c0SRandy Dunlapkgdboc and uarts 834*d5af79c0SRandy Dunlap~~~~~~~~~~~~~~~~ 835*d5af79c0SRandy Dunlap 836*d5af79c0SRandy DunlapThe kgdboc driver is actually a very thin driver that relies on the 837*d5af79c0SRandy Dunlapunderlying low level to the hardware driver having "polling hooks" to 838*d5af79c0SRandy Dunlapwhich the tty driver is attached. In the initial implementation of 839*d5af79c0SRandy Dunlapkgdboc the serial_core was changed to expose a low level UART hook for 840*d5af79c0SRandy Dunlapdoing polled mode reading and writing of a single character while in an 841*d5af79c0SRandy Dunlapatomic context. When kgdb makes an I/O request to the debugger, kgdboc 842*d5af79c0SRandy Dunlapinvokes a callback in the serial core which in turn uses the callback in 843*d5af79c0SRandy Dunlapthe UART driver. 844*d5af79c0SRandy Dunlap 845*d5af79c0SRandy DunlapWhen using kgdboc with a UART, the UART driver must implement two 846*d5af79c0SRandy Dunlapcallbacks in the struct uart_ops. 847*d5af79c0SRandy DunlapExample from ``drivers/8250.c``:: 848*d5af79c0SRandy Dunlap 849*d5af79c0SRandy Dunlap 850*d5af79c0SRandy Dunlap #ifdef CONFIG_CONSOLE_POLL 851*d5af79c0SRandy Dunlap .poll_get_char = serial8250_get_poll_char, 852*d5af79c0SRandy Dunlap .poll_put_char = serial8250_put_poll_char, 853*d5af79c0SRandy Dunlap #endif 854*d5af79c0SRandy Dunlap 855*d5af79c0SRandy Dunlap 856*d5af79c0SRandy DunlapAny implementation specifics around creating a polling driver use the 857*d5af79c0SRandy Dunlap``#ifdef CONFIG_CONSOLE_POLL``, as shown above. Keep in mind that 858*d5af79c0SRandy Dunlappolling hooks have to be implemented in such a way that they can be 859*d5af79c0SRandy Dunlapcalled from an atomic context and have to restore the state of the UART 860*d5af79c0SRandy Dunlapchip on return such that the system can return to normal when the 861*d5af79c0SRandy Dunlapdebugger detaches. You need to be very careful with any kind of lock you 862*d5af79c0SRandy Dunlapconsider, because failing here is most likely going to mean pressing the 863*d5af79c0SRandy Dunlapreset button. 864*d5af79c0SRandy Dunlap 865*d5af79c0SRandy Dunlapkgdboc and keyboards 866*d5af79c0SRandy Dunlap~~~~~~~~~~~~~~~~~~~~~~~~ 867*d5af79c0SRandy Dunlap 868*d5af79c0SRandy DunlapThe kgdboc driver contains logic to configure communications with an 869*d5af79c0SRandy Dunlapattached keyboard. The keyboard infrastructure is only compiled into the 870*d5af79c0SRandy Dunlapkernel when ``CONFIG_KDB_KEYBOARD=y`` is set in the kernel configuration. 871*d5af79c0SRandy Dunlap 872*d5af79c0SRandy DunlapThe core polled keyboard driver for PS/2 type keyboards is in 873*d5af79c0SRandy Dunlap``drivers/char/kdb_keyboard.c``. This driver is hooked into the debug core 874*d5af79c0SRandy Dunlapwhen kgdboc populates the callback in the array called 875*d5af79c0SRandy Dunlap:c:expr:`kdb_poll_funcs[]`. The kdb_get_kbd_char() is the top-level 876*d5af79c0SRandy Dunlapfunction which polls hardware for single character input. 877*d5af79c0SRandy Dunlap 878*d5af79c0SRandy Dunlapkgdboc and kms 879*d5af79c0SRandy Dunlap~~~~~~~~~~~~~~~~~~ 880*d5af79c0SRandy Dunlap 881*d5af79c0SRandy DunlapThe kgdboc driver contains logic to request the graphics display to 882*d5af79c0SRandy Dunlapswitch to a text context when you are using ``kgdboc=kms,kbd``, provided 883*d5af79c0SRandy Dunlapthat you have a video driver which has a frame buffer console and atomic 884*d5af79c0SRandy Dunlapkernel mode setting support. 885*d5af79c0SRandy Dunlap 886*d5af79c0SRandy DunlapEvery time the kernel debugger is entered it calls 887*d5af79c0SRandy Dunlapkgdboc_pre_exp_handler() which in turn calls con_debug_enter() 888*d5af79c0SRandy Dunlapin the virtual console layer. On resuming kernel execution, the kernel 889*d5af79c0SRandy Dunlapdebugger calls kgdboc_post_exp_handler() which in turn calls 890*d5af79c0SRandy Dunlapcon_debug_leave(). 891*d5af79c0SRandy Dunlap 892*d5af79c0SRandy DunlapAny video driver that wants to be compatible with the kernel debugger 893*d5af79c0SRandy Dunlapand the atomic kms callbacks must implement the ``mode_set_base_atomic``, 894*d5af79c0SRandy Dunlap``fb_debug_enter`` and ``fb_debug_leave operations``. For the 895*d5af79c0SRandy Dunlap``fb_debug_enter`` and ``fb_debug_leave`` the option exists to use the 896*d5af79c0SRandy Dunlapgeneric drm fb helper functions or implement something custom for the 897*d5af79c0SRandy Dunlaphardware. The following example shows the initialization of the 898*d5af79c0SRandy Dunlap.mode_set_base_atomic operation in 899*d5af79c0SRandy Dunlapdrivers/gpu/drm/i915/intel_display.c:: 900*d5af79c0SRandy Dunlap 901*d5af79c0SRandy Dunlap 902*d5af79c0SRandy Dunlap static const struct drm_crtc_helper_funcs intel_helper_funcs = { 903*d5af79c0SRandy Dunlap [...] 904*d5af79c0SRandy Dunlap .mode_set_base_atomic = intel_pipe_set_base_atomic, 905*d5af79c0SRandy Dunlap [...] 906*d5af79c0SRandy Dunlap }; 907*d5af79c0SRandy Dunlap 908*d5af79c0SRandy Dunlap 909*d5af79c0SRandy DunlapHere is an example of how the i915 driver initializes the 910*d5af79c0SRandy Dunlapfb_debug_enter and fb_debug_leave functions to use the generic drm 911*d5af79c0SRandy Dunlaphelpers in ``drivers/gpu/drm/i915/intel_fb.c``:: 912*d5af79c0SRandy Dunlap 913*d5af79c0SRandy Dunlap 914*d5af79c0SRandy Dunlap static struct fb_ops intelfb_ops = { 915*d5af79c0SRandy Dunlap [...] 916*d5af79c0SRandy Dunlap .fb_debug_enter = drm_fb_helper_debug_enter, 917*d5af79c0SRandy Dunlap .fb_debug_leave = drm_fb_helper_debug_leave, 918*d5af79c0SRandy Dunlap [...] 919*d5af79c0SRandy Dunlap }; 920*d5af79c0SRandy Dunlap 921*d5af79c0SRandy Dunlap 922*d5af79c0SRandy DunlapCredits 923*d5af79c0SRandy Dunlap======= 924*d5af79c0SRandy Dunlap 925*d5af79c0SRandy DunlapThe following people have contributed to this document: 926*d5af79c0SRandy Dunlap 927*d5af79c0SRandy Dunlap1. Amit Kale <[email protected]> 928*d5af79c0SRandy Dunlap 929*d5af79c0SRandy Dunlap2. Tom Rini <[email protected]> 930*d5af79c0SRandy Dunlap 931*d5af79c0SRandy DunlapIn March 2008 this document was completely rewritten by: 932*d5af79c0SRandy Dunlap 933*d5af79c0SRandy Dunlap- Jason Wessel <[email protected]> 934*d5af79c0SRandy Dunlap 935*d5af79c0SRandy DunlapIn Jan 2010 this document was updated to include kdb. 936*d5af79c0SRandy Dunlap 937*d5af79c0SRandy Dunlap- Jason Wessel <[email protected]> 938