0001 =================================================
0002 Using kgdb, kdb and the kernel debugger internals
0003 =================================================
0004
0005 :Author: Jason Wessel
0006
0007 Introduction
0008 ============
0009
0010 The kernel has two different debugger front ends (kdb and kgdb) which
0011 interface to the debug core. It is possible to use either of the
0012 debugger front ends and dynamically transition between them if you
0013 configure the kernel properly at compile and runtime.
0014
0015 Kdb is simplistic shell-style interface which you can use on a system
0016 console with a keyboard or serial console. You can use it to inspect
0017 memory, registers, process lists, dmesg, and even set breakpoints to
0018 stop in a certain location. Kdb is not a source level debugger, although
0019 you can set breakpoints and execute some basic kernel run control. Kdb
0020 is mainly aimed at doing some analysis to aid in development or
0021 diagnosing kernel problems. You can access some symbols by name in
0022 kernel built-ins or in kernel modules if the code was built with
0023 ``CONFIG_KALLSYMS``.
0024
0025 Kgdb is intended to be used as a source level debugger for the Linux
0026 kernel. It is used along with gdb to debug a Linux kernel. The
0027 expectation is that gdb can be used to "break in" to the kernel to
0028 inspect memory, variables and look through call stack information
0029 similar to the way an application developer would use gdb to debug an
0030 application. It is possible to place breakpoints in kernel code and
0031 perform some limited execution stepping.
0032
0033 Two machines are required for using kgdb. One of these machines is a
0034 development machine and the other is the target machine. The kernel to
0035 be debugged runs on the target machine. The development machine runs an
0036 instance of gdb against the vmlinux file which contains the symbols (not
0037 a boot image such as bzImage, zImage, uImage...). In gdb the developer
0038 specifies the connection parameters and connects to kgdb. The type of
0039 connection a developer makes with gdb depends on the availability of
0040 kgdb I/O modules compiled as built-ins or loadable kernel modules in the
0041 test machine's kernel.
0042
0043 Compiling a kernel
0044 ==================
0045
0046 - In order to enable compilation of kdb, you must first enable kgdb.
0047
0048 - The kgdb test compile options are described in the kgdb test suite
0049 chapter.
0050
0051 Kernel config options for kgdb
0052 ------------------------------
0053
0054 To enable ``CONFIG_KGDB`` you should look under
0055 :menuselection:`Kernel hacking --> Kernel debugging` and select
0056 :menuselection:`KGDB: kernel debugger`.
0057
0058 While it is not a hard requirement that you have symbols in your vmlinux
0059 file, gdb tends not to be very useful without the symbolic data, so you
0060 will want to turn on ``CONFIG_DEBUG_INFO`` which is called
0061 :menuselection:`Compile the kernel with debug info` in the config menu.
0062
0063 It is advised, but not required, that you turn on the
0064 ``CONFIG_FRAME_POINTER`` kernel option which is called :menuselection:`Compile
0065 the kernel with frame pointers` in the config menu. This option inserts code
0066 into the compiled executable which saves the frame information in registers
0067 or on the stack at different points which allows a debugger such as gdb to
0068 more accurately construct stack back traces while debugging the kernel.
0069
0070 If the architecture that you are using supports the kernel option
0071 ``CONFIG_STRICT_KERNEL_RWX``, you should consider turning it off. This
0072 option will prevent the use of software breakpoints because it marks
0073 certain regions of the kernel's memory space as read-only. If kgdb
0074 supports it for the architecture you are using, you can use hardware
0075 breakpoints if you desire to run with the ``CONFIG_STRICT_KERNEL_RWX``
0076 option turned on, else you need to turn off this option.
0077
0078 Next you should choose one of more I/O drivers to interconnect debugging
0079 host and debugged target. Early boot debugging requires a KGDB I/O
0080 driver that supports early debugging and the driver must be built into
0081 the kernel directly. Kgdb I/O driver configuration takes place via
0082 kernel or module parameters which you can learn more about in the in the
0083 section that describes the parameter kgdboc.
0084
0085 Here is an example set of ``.config`` symbols to enable or disable for kgdb::
0086
0087 # CONFIG_STRICT_KERNEL_RWX is not set
0088 CONFIG_FRAME_POINTER=y
0089 CONFIG_KGDB=y
0090 CONFIG_KGDB_SERIAL_CONSOLE=y
0091
0092 Kernel config options for kdb
0093 -----------------------------
0094
0095 Kdb is quite a bit more complex than the simple gdbstub sitting on top
0096 of the kernel's debug core. Kdb must implement a shell, and also adds
0097 some helper functions in other parts of the kernel, responsible for
0098 printing out interesting data such as what you would see if you ran
0099 ``lsmod``, or ``ps``. In order to build kdb into the kernel you follow the
0100 same steps as you would for kgdb.
0101
0102 The main config option for kdb is ``CONFIG_KGDB_KDB`` which is called
0103 :menuselection:`KGDB_KDB: include kdb frontend for kgdb` in the config menu.
0104 In theory you would have already also selected an I/O driver such as the
0105 ``CONFIG_KGDB_SERIAL_CONSOLE`` interface if you plan on using kdb on a
0106 serial port, when you were configuring kgdb.
0107
0108 If you want to use a PS/2-style keyboard with kdb, you would select
0109 ``CONFIG_KDB_KEYBOARD`` which is called :menuselection:`KGDB_KDB: keyboard as
0110 input device` in the config menu. The ``CONFIG_KDB_KEYBOARD`` option is not
0111 used for anything in the gdb interface to kgdb. The ``CONFIG_KDB_KEYBOARD``
0112 option only works with kdb.
0113
0114 Here is an example set of ``.config`` symbols to enable/disable kdb::
0115
0116 # CONFIG_STRICT_KERNEL_RWX is not set
0117 CONFIG_FRAME_POINTER=y
0118 CONFIG_KGDB=y
0119 CONFIG_KGDB_SERIAL_CONSOLE=y
0120 CONFIG_KGDB_KDB=y
0121 CONFIG_KDB_KEYBOARD=y
0122
0123 Kernel Debugger Boot Arguments
0124 ==============================
0125
0126 This section describes the various runtime kernel parameters that affect
0127 the configuration of the kernel debugger. The following chapter covers
0128 using kdb and kgdb as well as providing some examples of the
0129 configuration parameters.
0130
0131 Kernel parameter: kgdboc
0132 ------------------------
0133
0134 The kgdboc driver was originally an abbreviation meant to stand for
0135 "kgdb over console". Today it is the primary mechanism to configure how
0136 to communicate from gdb to kgdb as well as the devices you want to use
0137 to interact with the kdb shell.
0138
0139 For kgdb/gdb, kgdboc is designed to work with a single serial port. It
0140 is intended to cover the circumstance where you want to use a serial
0141 console as your primary console as well as using it to perform kernel
0142 debugging. It is also possible to use kgdb on a serial port which is not
0143 designated as a system console. Kgdboc may be configured as a kernel
0144 built-in or a kernel loadable module. You can only make use of
0145 ``kgdbwait`` and early debugging if you build kgdboc into the kernel as
0146 a built-in.
0147
0148 Optionally you can elect to activate kms (Kernel Mode Setting)
0149 integration. When you use kms with kgdboc and you have a video driver
0150 that has atomic mode setting hooks, it is possible to enter the debugger
0151 on the graphics console. When the kernel execution is resumed, the
0152 previous graphics mode will be restored. This integration can serve as a
0153 useful tool to aid in diagnosing crashes or doing analysis of memory
0154 with kdb while allowing the full graphics console applications to run.
0155
0156 kgdboc arguments
0157 ~~~~~~~~~~~~~~~~
0158
0159 Usage::
0160
0161 kgdboc=[kms][[,]kbd][[,]serial_device][,baud]
0162
0163 The order listed above must be observed if you use any of the optional
0164 configurations together.
0165
0166 Abbreviations:
0167
0168 - kms = Kernel Mode Setting
0169
0170 - kbd = Keyboard
0171
0172 You can configure kgdboc to use the keyboard, and/or a serial device
0173 depending on if you are using kdb and/or kgdb, in one of the following
0174 scenarios. The order listed above must be observed if you use any of the
0175 optional configurations together. Using kms + only gdb is generally not
0176 a useful combination.
0177
0178 Using loadable module or built-in
0179 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0180
0181 1. As a kernel built-in:
0182
0183 Use the kernel boot argument::
0184
0185 kgdboc=<tty-device>,[baud]
0186
0187 2. As a kernel loadable module:
0188
0189 Use the command::
0190
0191 modprobe kgdboc kgdboc=<tty-device>,[baud]
0192
0193 Here are two examples of how you might format the kgdboc string. The
0194 first is for an x86 target using the first serial port. The second
0195 example is for the ARM Versatile AB using the second serial port.
0196
0197 1. ``kgdboc=ttyS0,115200``
0198
0199 2. ``kgdboc=ttyAMA1,115200``
0200
0201 Configure kgdboc at runtime with sysfs
0202 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0203
0204 At run time you can enable or disable kgdboc by echoing a parameters
0205 into the sysfs. Here are two examples:
0206
0207 1. Enable kgdboc on ttyS0::
0208
0209 echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc
0210
0211 2. Disable kgdboc::
0212
0213 echo "" > /sys/module/kgdboc/parameters/kgdboc
0214
0215 .. note::
0216
0217 You do not need to specify the baud if you are configuring the
0218 console on tty which is already configured or open.
0219
0220 More examples
0221 ^^^^^^^^^^^^^
0222
0223 You can configure kgdboc to use the keyboard, and/or a serial device
0224 depending on if you are using kdb and/or kgdb, in one of the following
0225 scenarios.
0226
0227 1. kdb and kgdb over only a serial port::
0228
0229 kgdboc=<serial_device>[,baud]
0230
0231 Example::
0232
0233 kgdboc=ttyS0,115200
0234
0235 2. kdb and kgdb with keyboard and a serial port::
0236
0237 kgdboc=kbd,<serial_device>[,baud]
0238
0239 Example::
0240
0241 kgdboc=kbd,ttyS0,115200
0242
0243 3. kdb with a keyboard::
0244
0245 kgdboc=kbd
0246
0247 4. kdb with kernel mode setting::
0248
0249 kgdboc=kms,kbd
0250
0251 5. kdb with kernel mode setting and kgdb over a serial port::
0252
0253 kgdboc=kms,kbd,ttyS0,115200
0254
0255 .. note::
0256
0257 Kgdboc does not support interrupting the target via the gdb remote
0258 protocol. You must manually send a :kbd:`SysRq-G` unless you have a proxy
0259 that splits console output to a terminal program. A console proxy has a
0260 separate TCP port for the debugger and a separate TCP port for the
0261 "human" console. The proxy can take care of sending the :kbd:`SysRq-G`
0262 for you.
0263
0264 When using kgdboc with no debugger proxy, you can end up connecting the
0265 debugger at one of two entry points. If an exception occurs after you
0266 have loaded kgdboc, a message should print on the console stating it is
0267 waiting for the debugger. In this case you disconnect your terminal
0268 program and then connect the debugger in its place. If you want to
0269 interrupt the target system and forcibly enter a debug session you have
0270 to issue a :kbd:`Sysrq` sequence and then type the letter :kbd:`g`. Then you
0271 disconnect the terminal session and connect gdb. Your options if you
0272 don't like this are to hack gdb to send the :kbd:`SysRq-G` for you as well as
0273 on the initial connect, or to use a debugger proxy that allows an
0274 unmodified gdb to do the debugging.
0275
0276 Kernel parameter: ``kgdboc_earlycon``
0277 -------------------------------------
0278
0279 If you specify the kernel parameter ``kgdboc_earlycon`` and your serial
0280 driver registers a boot console that supports polling (doesn't need
0281 interrupts and implements a nonblocking read() function) kgdb will attempt
0282 to work using the boot console until it can transition to the regular
0283 tty driver specified by the ``kgdboc`` parameter.
0284
0285 Normally there is only one boot console (especially that implements the
0286 read() function) so just adding ``kgdboc_earlycon`` on its own is
0287 sufficient to make this work. If you have more than one boot console you
0288 can add the boot console's name to differentiate. Note that names that
0289 are registered through the boot console layer and the tty layer are not
0290 the same for the same port.
0291
0292 For instance, on one board to be explicit you might do::
0293
0294 kgdboc_earlycon=qcom_geni kgdboc=ttyMSM0
0295
0296 If the only boot console on the device was "qcom_geni", you could simplify::
0297
0298 kgdboc_earlycon kgdboc=ttyMSM0
0299
0300 Kernel parameter: ``kgdbwait``
0301 ------------------------------
0302
0303 The Kernel command line option ``kgdbwait`` makes kgdb wait for a
0304 debugger connection during booting of a kernel. You can only use this
0305 option if you compiled a kgdb I/O driver into the kernel and you
0306 specified the I/O driver configuration as a kernel command line option.
0307 The kgdbwait parameter should always follow the configuration parameter
0308 for the kgdb I/O driver in the kernel command line else the I/O driver
0309 will not be configured prior to asking the kernel to use it to wait.
0310
0311 The kernel will stop and wait as early as the I/O driver and
0312 architecture allows when you use this option. If you build the kgdb I/O
0313 driver as a loadable kernel module kgdbwait will not do anything.
0314
0315 Kernel parameter: ``kgdbcon``
0316 -----------------------------
0317
0318 The ``kgdbcon`` feature allows you to see printk() messages inside gdb
0319 while gdb is connected to the kernel. Kdb does not make use of the kgdbcon
0320 feature.
0321
0322 Kgdb supports using the gdb serial protocol to send console messages to
0323 the debugger when the debugger is connected and running. There are two
0324 ways to activate this feature.
0325
0326 1. Activate with the kernel command line option::
0327
0328 kgdbcon
0329
0330 2. Use sysfs before configuring an I/O driver::
0331
0332 echo 1 > /sys/module/kgdb/parameters/kgdb_use_con
0333
0334 .. note::
0335
0336 If you do this after you configure the kgdb I/O driver, the
0337 setting will not take effect until the next point the I/O is
0338 reconfigured.
0339
0340 .. important::
0341
0342 You cannot use kgdboc + kgdbcon on a tty that is an
0343 active system console. An example of incorrect usage is::
0344
0345 console=ttyS0,115200 kgdboc=ttyS0 kgdbcon
0346
0347 It is possible to use this option with kgdboc on a tty that is not a
0348 system console.
0349
0350 Run time parameter: ``kgdbreboot``
0351 ----------------------------------
0352
0353 The kgdbreboot feature allows you to change how the debugger deals with
0354 the reboot notification. You have 3 choices for the behavior. The
0355 default behavior is always set to 0.
0356
0357 .. tabularcolumns:: |p{0.4cm}|p{11.5cm}|p{5.6cm}|
0358
0359 .. flat-table::
0360 :widths: 1 10 8
0361
0362 * - 1
0363 - ``echo -1 > /sys/module/debug_core/parameters/kgdbreboot``
0364 - Ignore the reboot notification entirely.
0365
0366 * - 2
0367 - ``echo 0 > /sys/module/debug_core/parameters/kgdbreboot``
0368 - Send the detach message to any attached debugger client.
0369
0370 * - 3
0371 - ``echo 1 > /sys/module/debug_core/parameters/kgdbreboot``
0372 - Enter the debugger on reboot notify.
0373
0374 Kernel parameter: ``nokaslr``
0375 -----------------------------
0376
0377 If the architecture that you are using enable KASLR by default,
0378 you should consider turning it off. KASLR randomizes the
0379 virtual address where the kernel image is mapped and confuse
0380 gdb which resolve kernel symbol address from symbol table
0381 of vmlinux.
0382
0383 Using kdb
0384 =========
0385
0386 Quick start for kdb on a serial port
0387 ------------------------------------
0388
0389 This is a quick example of how to use kdb.
0390
0391 1. Configure kgdboc at boot using kernel parameters::
0392
0393 console=ttyS0,115200 kgdboc=ttyS0,115200 nokaslr
0394
0395 OR
0396
0397 Configure kgdboc after the kernel has booted; assuming you are using
0398 a serial port console::
0399
0400 echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc
0401
0402 2. Enter the kernel debugger manually or by waiting for an oops or
0403 fault. There are several ways you can enter the kernel debugger
0404 manually; all involve using the :kbd:`SysRq-G`, which means you must have
0405 enabled ``CONFIG_MAGIC_SYSRQ=y`` in your kernel config.
0406
0407 - When logged in as root or with a super user session you can run::
0408
0409 echo g > /proc/sysrq-trigger
0410
0411 - Example using minicom 2.2
0412
0413 Press: :kbd:`CTRL-A` :kbd:`f` :kbd:`g`
0414
0415 - When you have telneted to a terminal server that supports sending
0416 a remote break
0417
0418 Press: :kbd:`CTRL-]`
0419
0420 Type in: ``send break``
0421
0422 Press: :kbd:`Enter` :kbd:`g`
0423
0424 3. From the kdb prompt you can run the ``help`` command to see a complete
0425 list of the commands that are available.
0426
0427 Some useful commands in kdb include:
0428
0429 =========== =================================================================
0430 ``lsmod`` Shows where kernel modules are loaded
0431 ``ps`` Displays only the active processes
0432 ``ps A`` Shows all the processes
0433 ``summary`` Shows kernel version info and memory usage
0434 ``bt`` Get a backtrace of the current process using dump_stack()
0435 ``dmesg`` View the kernel syslog buffer
0436 ``go`` Continue the system
0437 =========== =================================================================
0438
0439 4. When you are done using kdb you need to consider rebooting the system
0440 or using the ``go`` command to resuming normal kernel execution. If you
0441 have paused the kernel for a lengthy period of time, applications
0442 that rely on timely networking or anything to do with real wall clock
0443 time could be adversely affected, so you should take this into
0444 consideration when using the kernel debugger.
0445
0446 Quick start for kdb using a keyboard connected console
0447 ------------------------------------------------------
0448
0449 This is a quick example of how to use kdb with a keyboard.
0450
0451 1. Configure kgdboc at boot using kernel parameters::
0452
0453 kgdboc=kbd
0454
0455 OR
0456
0457 Configure kgdboc after the kernel has booted::
0458
0459 echo kbd > /sys/module/kgdboc/parameters/kgdboc
0460
0461 2. Enter the kernel debugger manually or by waiting for an oops or
0462 fault. There are several ways you can enter the kernel debugger
0463 manually; all involve using the :kbd:`SysRq-G`, which means you must have
0464 enabled ``CONFIG_MAGIC_SYSRQ=y`` in your kernel config.
0465
0466 - When logged in as root or with a super user session you can run::
0467
0468 echo g > /proc/sysrq-trigger
0469
0470 - Example using a laptop keyboard:
0471
0472 Press and hold down: :kbd:`Alt`
0473
0474 Press and hold down: :kbd:`Fn`
0475
0476 Press and release the key with the label: :kbd:`SysRq`
0477
0478 Release: :kbd:`Fn`
0479
0480 Press and release: :kbd:`g`
0481
0482 Release: :kbd:`Alt`
0483
0484 - Example using a PS/2 101-key keyboard
0485
0486 Press and hold down: :kbd:`Alt`
0487
0488 Press and release the key with the label: :kbd:`SysRq`
0489
0490 Press and release: :kbd:`g`
0491
0492 Release: :kbd:`Alt`
0493
0494 3. Now type in a kdb command such as ``help``, ``dmesg``, ``bt`` or ``go`` to
0495 continue kernel execution.
0496
0497 Using kgdb / gdb
0498 ================
0499
0500 In order to use kgdb you must activate it by passing configuration
0501 information to one of the kgdb I/O drivers. If you do not pass any
0502 configuration information kgdb will not do anything at all. Kgdb will
0503 only actively hook up to the kernel trap hooks if a kgdb I/O driver is
0504 loaded and configured. If you unconfigure a kgdb I/O driver, kgdb will
0505 unregister all the kernel hook points.
0506
0507 All kgdb I/O drivers can be reconfigured at run time, if
0508 ``CONFIG_SYSFS`` and ``CONFIG_MODULES`` are enabled, by echo'ing a new
0509 config string to ``/sys/module/<driver>/parameter/<option>``. The driver
0510 can be unconfigured by passing an empty string. You cannot change the
0511 configuration while the debugger is attached. Make sure to detach the
0512 debugger with the ``detach`` command prior to trying to unconfigure a
0513 kgdb I/O driver.
0514
0515 Connecting with gdb to a serial port
0516 ------------------------------------
0517
0518 1. Configure kgdboc
0519
0520 Configure kgdboc at boot using kernel parameters::
0521
0522 kgdboc=ttyS0,115200
0523
0524 OR
0525
0526 Configure kgdboc after the kernel has booted::
0527
0528 echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc
0529
0530 2. Stop kernel execution (break into the debugger)
0531
0532 In order to connect to gdb via kgdboc, the kernel must first be
0533 stopped. There are several ways to stop the kernel which include
0534 using kgdbwait as a boot argument, via a :kbd:`SysRq-G`, or running the
0535 kernel until it takes an exception where it waits for the debugger to
0536 attach.
0537
0538 - When logged in as root or with a super user session you can run::
0539
0540 echo g > /proc/sysrq-trigger
0541
0542 - Example using minicom 2.2
0543
0544 Press: :kbd:`CTRL-A` :kbd:`f` :kbd:`g`
0545
0546 - When you have telneted to a terminal server that supports sending
0547 a remote break
0548
0549 Press: :kbd:`CTRL-]`
0550
0551 Type in: ``send break``
0552
0553 Press: :kbd:`Enter` :kbd:`g`
0554
0555 3. Connect from gdb
0556
0557 Example (using a directly connected port)::
0558
0559 % gdb ./vmlinux
0560 (gdb) set serial baud 115200
0561 (gdb) target remote /dev/ttyS0
0562
0563
0564 Example (kgdb to a terminal server on TCP port 2012)::
0565
0566 % gdb ./vmlinux
0567 (gdb) target remote 192.168.2.2:2012
0568
0569
0570 Once connected, you can debug a kernel the way you would debug an
0571 application program.
0572
0573 If you are having problems connecting or something is going seriously
0574 wrong while debugging, it will most often be the case that you want
0575 to enable gdb to be verbose about its target communications. You do
0576 this prior to issuing the ``target remote`` command by typing in::
0577
0578 set debug remote 1
0579
0580 Remember if you continue in gdb, and need to "break in" again, you need
0581 to issue an other :kbd:`SysRq-G`. It is easy to create a simple entry point by
0582 putting a breakpoint at ``sys_sync`` and then you can run ``sync`` from a
0583 shell or script to break into the debugger.
0584
0585 kgdb and kdb interoperability
0586 =============================
0587
0588 It is possible to transition between kdb and kgdb dynamically. The debug
0589 core will remember which you used the last time and automatically start
0590 in the same mode.
0591
0592 Switching between kdb and kgdb
0593 ------------------------------
0594
0595 Switching from kgdb to kdb
0596 ~~~~~~~~~~~~~~~~~~~~~~~~~~
0597
0598 There are two ways to switch from kgdb to kdb: you can use gdb to issue
0599 a maintenance packet, or you can blindly type the command ``$3#33``.
0600 Whenever the kernel debugger stops in kgdb mode it will print the
0601 message ``KGDB or $3#33 for KDB``. It is important to note that you have
0602 to type the sequence correctly in one pass. You cannot type a backspace
0603 or delete because kgdb will interpret that as part of the debug stream.
0604
0605 1. Change from kgdb to kdb by blindly typing::
0606
0607 $3#33
0608
0609 2. Change from kgdb to kdb with gdb::
0610
0611 maintenance packet 3
0612
0613 .. note::
0614
0615 Now you must kill gdb. Typically you press :kbd:`CTRL-Z` and issue
0616 the command::
0617
0618 kill -9 %
0619
0620 Change from kdb to kgdb
0621 ~~~~~~~~~~~~~~~~~~~~~~~
0622
0623 There are two ways you can change from kdb to kgdb. You can manually
0624 enter kgdb mode by issuing the kgdb command from the kdb shell prompt,
0625 or you can connect gdb while the kdb shell prompt is active. The kdb
0626 shell looks for the typical first commands that gdb would issue with the
0627 gdb remote protocol and if it sees one of those commands it
0628 automatically changes into kgdb mode.
0629
0630 1. From kdb issue the command::
0631
0632 kgdb
0633
0634 Now disconnect your terminal program and connect gdb in its place
0635
0636 2. At the kdb prompt, disconnect the terminal program and connect gdb in
0637 its place.
0638
0639 Running kdb commands from gdb
0640 -----------------------------
0641
0642 It is possible to run a limited set of kdb commands from gdb, using the
0643 gdb monitor command. You don't want to execute any of the run control or
0644 breakpoint operations, because it can disrupt the state of the kernel
0645 debugger. You should be using gdb for breakpoints and run control
0646 operations if you have gdb connected. The more useful commands to run
0647 are things like lsmod, dmesg, ps or possibly some of the memory
0648 information commands. To see all the kdb commands you can run
0649 ``monitor help``.
0650
0651 Example::
0652
0653 (gdb) monitor ps
0654 1 idle process (state I) and
0655 27 sleeping system daemon (state M) processes suppressed,
0656 use 'ps A' to see all.
0657 Task Addr Pid Parent [*] cpu State Thread Command
0658
0659 0xc78291d0 1 0 0 0 S 0xc7829404 init
0660 0xc7954150 942 1 0 0 S 0xc7954384 dropbear
0661 0xc78789c0 944 1 0 0 S 0xc7878bf4 sh
0662 (gdb)
0663
0664 kgdb Test Suite
0665 ===============
0666
0667 When kgdb is enabled in the kernel config you can also elect to enable
0668 the config parameter ``KGDB_TESTS``. Turning this on will enable a special
0669 kgdb I/O module which is designed to test the kgdb internal functions.
0670
0671 The kgdb tests are mainly intended for developers to test the kgdb
0672 internals as well as a tool for developing a new kgdb architecture
0673 specific implementation. These tests are not really for end users of the
0674 Linux kernel. The primary source of documentation would be to look in
0675 the ``drivers/misc/kgdbts.c`` file.
0676
0677 The kgdb test suite can also be configured at compile time to run the
0678 core set of tests by setting the kernel config parameter
0679 ``KGDB_TESTS_ON_BOOT``. This particular option is aimed at automated
0680 regression testing and does not require modifying the kernel boot config
0681 arguments. If this is turned on, the kgdb test suite can be disabled by
0682 specifying ``kgdbts=`` as a kernel boot argument.
0683
0684 Kernel Debugger Internals
0685 =========================
0686
0687 Architecture Specifics
0688 ----------------------
0689
0690 The kernel debugger is organized into a number of components:
0691
0692 1. The debug core
0693
0694 The debug core is found in ``kernel/debugger/debug_core.c``. It
0695 contains:
0696
0697 - A generic OS exception handler which includes sync'ing the
0698 processors into a stopped state on an multi-CPU system.
0699
0700 - The API to talk to the kgdb I/O drivers
0701
0702 - The API to make calls to the arch-specific kgdb implementation
0703
0704 - The logic to perform safe memory reads and writes to memory while
0705 using the debugger
0706
0707 - A full implementation for software breakpoints unless overridden
0708 by the arch
0709
0710 - The API to invoke either the kdb or kgdb frontend to the debug
0711 core.
0712
0713 - The structures and callback API for atomic kernel mode setting.
0714
0715 .. note:: kgdboc is where the kms callbacks are invoked.
0716
0717 2. kgdb arch-specific implementation
0718
0719 This implementation is generally found in ``arch/*/kernel/kgdb.c``. As
0720 an example, ``arch/x86/kernel/kgdb.c`` contains the specifics to
0721 implement HW breakpoint as well as the initialization to dynamically
0722 register and unregister for the trap handlers on this architecture.
0723 The arch-specific portion implements:
0724
0725 - contains an arch-specific trap catcher which invokes
0726 kgdb_handle_exception() to start kgdb about doing its work
0727
0728 - translation to and from gdb specific packet format to struct pt_regs
0729
0730 - Registration and unregistration of architecture specific trap
0731 hooks
0732
0733 - Any special exception handling and cleanup
0734
0735 - NMI exception handling and cleanup
0736
0737 - (optional) HW breakpoints
0738
0739 3. gdbstub frontend (aka kgdb)
0740
0741 The gdbstub is located in ``kernel/debug/gdbstub.c``. It contains:
0742
0743 - All the logic to implement the gdb serial protocol
0744
0745 4. kdb frontend
0746
0747 The kdb debugger shell is broken down into a number of components.
0748 The kdb core is located in kernel/debug/kdb. There are a number of
0749 helper functions in some of the other kernel components to make it
0750 possible for kdb to examine and report information about the kernel
0751 without taking locks that could cause a kernel deadlock. The kdb core
0752 contains implements the following functionality.
0753
0754 - A simple shell
0755
0756 - The kdb core command set
0757
0758 - A registration API to register additional kdb shell commands.
0759
0760 - A good example of a self-contained kdb module is the ``ftdump``
0761 command for dumping the ftrace buffer. See:
0762 ``kernel/trace/trace_kdb.c``
0763
0764 - For an example of how to dynamically register a new kdb command
0765 you can build the kdb_hello.ko kernel module from
0766 ``samples/kdb/kdb_hello.c``. To build this example you can set
0767 ``CONFIG_SAMPLES=y`` and ``CONFIG_SAMPLE_KDB=m`` in your kernel
0768 config. Later run ``modprobe kdb_hello`` and the next time you
0769 enter the kdb shell, you can run the ``hello`` command.
0770
0771 - The implementation for kdb_printf() which emits messages directly
0772 to I/O drivers, bypassing the kernel log.
0773
0774 - SW / HW breakpoint management for the kdb shell
0775
0776 5. kgdb I/O driver
0777
0778 Each kgdb I/O driver has to provide an implementation for the
0779 following:
0780
0781 - configuration via built-in or module
0782
0783 - dynamic configuration and kgdb hook registration calls
0784
0785 - read and write character interface
0786
0787 - A cleanup handler for unconfiguring from the kgdb core
0788
0789 - (optional) Early debug methodology
0790
0791 Any given kgdb I/O driver has to operate very closely with the
0792 hardware and must do it in such a way that does not enable interrupts
0793 or change other parts of the system context without completely
0794 restoring them. The kgdb core will repeatedly "poll" a kgdb I/O
0795 driver for characters when it needs input. The I/O driver is expected
0796 to return immediately if there is no data available. Doing so allows
0797 for the future possibility to touch watchdog hardware in such a way
0798 as to have a target system not reset when these are enabled.
0799
0800 If you are intent on adding kgdb architecture specific support for a new
0801 architecture, the architecture should define ``HAVE_ARCH_KGDB`` in the
0802 architecture specific Kconfig file. This will enable kgdb for the
0803 architecture, and at that point you must create an architecture specific
0804 kgdb implementation.
0805
0806 There are a few flags which must be set on every architecture in their
0807 ``asm/kgdb.h`` file. These are:
0808
0809 - ``NUMREGBYTES``:
0810 The size in bytes of all of the registers, so that we
0811 can ensure they will all fit into a packet.
0812
0813 - ``BUFMAX``:
0814 The size in bytes of the buffer GDB will read into. This must
0815 be larger than NUMREGBYTES.
0816
0817 - ``CACHE_FLUSH_IS_SAFE``:
0818 Set to 1 if it is always safe to call
0819 flush_cache_range or flush_icache_range. On some architectures,
0820 these functions may not be safe to call on SMP since we keep other
0821 CPUs in a holding pattern.
0822
0823 There are also the following functions for the common backend, found in
0824 ``kernel/kgdb.c``, that must be supplied by the architecture-specific
0825 backend unless marked as (optional), in which case a default function
0826 maybe used if the architecture does not need to provide a specific
0827 implementation.
0828
0829 .. kernel-doc:: include/linux/kgdb.h
0830 :internal:
0831
0832 kgdboc internals
0833 ----------------
0834
0835 kgdboc and uarts
0836 ~~~~~~~~~~~~~~~~
0837
0838 The kgdboc driver is actually a very thin driver that relies on the
0839 underlying low level to the hardware driver having "polling hooks" to
0840 which the tty driver is attached. In the initial implementation of
0841 kgdboc the serial_core was changed to expose a low level UART hook for
0842 doing polled mode reading and writing of a single character while in an
0843 atomic context. When kgdb makes an I/O request to the debugger, kgdboc
0844 invokes a callback in the serial core which in turn uses the callback in
0845 the UART driver.
0846
0847 When using kgdboc with a UART, the UART driver must implement two
0848 callbacks in the struct uart_ops.
0849 Example from ``drivers/8250.c``::
0850
0851
0852 #ifdef CONFIG_CONSOLE_POLL
0853 .poll_get_char = serial8250_get_poll_char,
0854 .poll_put_char = serial8250_put_poll_char,
0855 #endif
0856
0857
0858 Any implementation specifics around creating a polling driver use the
0859 ``#ifdef CONFIG_CONSOLE_POLL``, as shown above. Keep in mind that
0860 polling hooks have to be implemented in such a way that they can be
0861 called from an atomic context and have to restore the state of the UART
0862 chip on return such that the system can return to normal when the
0863 debugger detaches. You need to be very careful with any kind of lock you
0864 consider, because failing here is most likely going to mean pressing the
0865 reset button.
0866
0867 kgdboc and keyboards
0868 ~~~~~~~~~~~~~~~~~~~~~~~~
0869
0870 The kgdboc driver contains logic to configure communications with an
0871 attached keyboard. The keyboard infrastructure is only compiled into the
0872 kernel when ``CONFIG_KDB_KEYBOARD=y`` is set in the kernel configuration.
0873
0874 The core polled keyboard driver for PS/2 type keyboards is in
0875 ``drivers/char/kdb_keyboard.c``. This driver is hooked into the debug core
0876 when kgdboc populates the callback in the array called
0877 :c:expr:`kdb_poll_funcs[]`. The kdb_get_kbd_char() is the top-level
0878 function which polls hardware for single character input.
0879
0880 kgdboc and kms
0881 ~~~~~~~~~~~~~~~~~~
0882
0883 The kgdboc driver contains logic to request the graphics display to
0884 switch to a text context when you are using ``kgdboc=kms,kbd``, provided
0885 that you have a video driver which has a frame buffer console and atomic
0886 kernel mode setting support.
0887
0888 Every time the kernel debugger is entered it calls
0889 kgdboc_pre_exp_handler() which in turn calls con_debug_enter()
0890 in the virtual console layer. On resuming kernel execution, the kernel
0891 debugger calls kgdboc_post_exp_handler() which in turn calls
0892 con_debug_leave().
0893
0894 Any video driver that wants to be compatible with the kernel debugger
0895 and the atomic kms callbacks must implement the ``mode_set_base_atomic``,
0896 ``fb_debug_enter`` and ``fb_debug_leave operations``. For the
0897 ``fb_debug_enter`` and ``fb_debug_leave`` the option exists to use the
0898 generic drm fb helper functions or implement something custom for the
0899 hardware. The following example shows the initialization of the
0900 .mode_set_base_atomic operation in
0901 drivers/gpu/drm/i915/intel_display.c::
0902
0903
0904 static const struct drm_crtc_helper_funcs intel_helper_funcs = {
0905 [...]
0906 .mode_set_base_atomic = intel_pipe_set_base_atomic,
0907 [...]
0908 };
0909
0910
0911 Here is an example of how the i915 driver initializes the
0912 fb_debug_enter and fb_debug_leave functions to use the generic drm
0913 helpers in ``drivers/gpu/drm/i915/intel_fb.c``::
0914
0915
0916 static struct fb_ops intelfb_ops = {
0917 [...]
0918 .fb_debug_enter = drm_fb_helper_debug_enter,
0919 .fb_debug_leave = drm_fb_helper_debug_leave,
0920 [...]
0921 };
0922
0923
0924 Credits
0925 =======
0926
0927 The following people have contributed to this document:
0928
0929 1. Amit Kale <amitkale@linsyssoft.com>
0930
0931 2. Tom Rini <trini@kernel.crashing.org>
0932
0933 In March 2008 this document was completely rewritten by:
0934
0935 - Jason Wessel <jason.wessel@windriver.com>
0936
0937 In Jan 2010 this document was updated to include kdb.
0938
0939 - Jason Wessel <jason.wessel@windriver.com>