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