kgdb.rst 32.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
=================================================
Using kgdb, kdb and the kernel debugger internals
=================================================

:Author: Jason Wessel

Introduction
============

The kernel has two different debugger front ends (kdb and kgdb) which
interface to the debug core. It is possible to use either of the
debugger front ends and dynamically transition between them if you
configure the kernel properly at compile and runtime.

Kdb is simplistic shell-style interface which you can use on a system
console with a keyboard or serial console. You can use it to inspect
memory, registers, process lists, dmesg, and even set breakpoints to
stop in a certain location. Kdb is not a source level debugger, although
you can set breakpoints and execute some basic kernel run control. Kdb
is mainly aimed at doing some analysis to aid in development or
diagnosing kernel problems. You can access some symbols by name in
kernel built-ins or in kernel modules if the code was built with
``CONFIG_KALLSYMS``.

Kgdb is intended to be used as a source level debugger for the Linux
kernel. It is used along with gdb to debug a Linux kernel. The
expectation is that gdb can be used to "break in" to the kernel to
inspect memory, variables and look through call stack information
similar to the way an application developer would use gdb to debug an
application. It is possible to place breakpoints in kernel code and
perform some limited execution stepping.

Two machines are required for using kgdb. One of these machines is a
development machine and the other is the target machine. The kernel to
be debugged runs on the target machine. The development machine runs an
instance of gdb against the vmlinux file which contains the symbols (not
a boot image such as bzImage, zImage, uImage...). In gdb the developer
specifies the connection parameters and connects to kgdb. The type of
connection a developer makes with gdb depends on the availability of
kgdb I/O modules compiled as built-ins or loadable kernel modules in the
test machine's kernel.

Compiling a kernel
==================

-  In order to enable compilation of kdb, you must first enable kgdb.

-  The kgdb test compile options are described in the kgdb test suite
   chapter.

Kernel config options for kgdb
------------------------------

54 55 56
To enable ``CONFIG_KGDB`` you should look under
:menuselection:`Kernel hacking --> Kernel debugging` and select
:menuselection:`KGDB: kernel debugger`.
57 58 59

While it is not a hard requirement that you have symbols in your vmlinux
file, gdb tends not to be very useful without the symbolic data, so you
60 61
will want to turn on ``CONFIG_DEBUG_INFO`` which is called
:menuselection:`Compile the kernel with debug info` in the config menu.
62 63

It is advised, but not required, that you turn on the
64 65
``CONFIG_FRAME_POINTER`` kernel option which is called :menuselection:`Compile
the kernel with frame pointers` in the config menu. This option inserts code
66 67 68 69 70 71
to into the compiled executable which saves the frame information in
registers or on the stack at different points which allows a debugger
such as gdb to more accurately construct stack back traces while
debugging the kernel.

If the architecture that you are using supports the kernel option
72
``CONFIG_STRICT_KERNEL_RWX``, you should consider turning it off. This
73 74 75
option will prevent the use of software breakpoints because it marks
certain regions of the kernel's memory space as read-only. If kgdb
supports it for the architecture you are using, you can use hardware
76
breakpoints if you desire to run with the ``CONFIG_STRICT_KERNEL_RWX``
77 78 79 80 81 82 83
option turned on, else you need to turn off this option.

Next you should choose one of more I/O drivers to interconnect debugging
host and debugged target. Early boot debugging requires a KGDB I/O
driver that supports early debugging and the driver must be built into
the kernel directly. Kgdb I/O driver configuration takes place via
kernel or module parameters which you can learn more about in the in the
84
section that describes the parameter kgdboc.
85

86
Here is an example set of ``.config`` symbols to enable or disable for kgdb::
87

88 89 90 91
  # CONFIG_STRICT_KERNEL_RWX is not set
  CONFIG_FRAME_POINTER=y
  CONFIG_KGDB=y
  CONFIG_KGDB_SERIAL_CONSOLE=y
92 93 94 95 96 97 98 99

Kernel config options for kdb
-----------------------------

Kdb is quite a bit more complex than the simple gdbstub sitting on top
of the kernel's debug core. Kdb must implement a shell, and also adds
some helper functions in other parts of the kernel, responsible for
printing out interesting data such as what you would see if you ran
100
``lsmod``, or ``ps``. In order to build kdb into the kernel you follow the
101 102 103
same steps as you would for kgdb.

The main config option for kdb is ``CONFIG_KGDB_KDB`` which is called
104 105 106
:menuselection:`KGDB_KDB: include kdb frontend for kgdb` in the config menu.
In theory you would have already also selected an I/O driver such as the
``CONFIG_KGDB_SERIAL_CONSOLE`` interface if you plan on using kdb on a
107 108 109
serial port, when you were configuring kgdb.

If you want to use a PS/2-style keyboard with kdb, you would select
110 111 112
``CONFIG_KDB_KEYBOARD`` which is called :menuselection:`KGDB_KDB: keyboard as
input device` in the config menu. The ``CONFIG_KDB_KEYBOARD`` option is not
used for anything in the gdb interface to kgdb. The ``CONFIG_KDB_KEYBOARD``
113 114
option only works with kdb.

115
Here is an example set of ``.config`` symbols to enable/disable kdb::
116

117 118 119 120 121 122
  # CONFIG_STRICT_KERNEL_RWX is not set
  CONFIG_FRAME_POINTER=y
  CONFIG_KGDB=y
  CONFIG_KGDB_SERIAL_CONSOLE=y
  CONFIG_KGDB_KDB=y
  CONFIG_KDB_KEYBOARD=y
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

Kernel Debugger Boot Arguments
==============================

This section describes the various runtime kernel parameters that affect
the configuration of the kernel debugger. The following chapter covers
using kdb and kgdb as well as providing some examples of the
configuration parameters.

Kernel parameter: kgdboc
------------------------

The kgdboc driver was originally an abbreviation meant to stand for
"kgdb over console". Today it is the primary mechanism to configure how
to communicate from gdb to kgdb as well as the devices you want to use
to interact with the kdb shell.

For kgdb/gdb, kgdboc is designed to work with a single serial port. It
is intended to cover the circumstance where you want to use a serial
console as your primary console as well as using it to perform kernel
debugging. It is also possible to use kgdb on a serial port which is not
designated as a system console. Kgdboc may be configured as a kernel
built-in or a kernel loadable module. You can only make use of
``kgdbwait`` and early debugging if you build kgdboc into the kernel as
a built-in.

Optionally you can elect to activate kms (Kernel Mode Setting)
integration. When you use kms with kgdboc and you have a video driver
that has atomic mode setting hooks, it is possible to enter the debugger
on the graphics console. When the kernel execution is resumed, the
previous graphics mode will be restored. This integration can serve as a
useful tool to aid in diagnosing crashes or doing analysis of memory
with kdb while allowing the full graphics console applications to run.

kgdboc arguments
~~~~~~~~~~~~~~~~

160 161 162
Usage::

	kgdboc=[kms][[,]kbd][[,]serial_device][,baud]
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

The order listed above must be observed if you use any of the optional
configurations together.

Abbreviations:

-  kms = Kernel Mode Setting

-  kbd = Keyboard

You can configure kgdboc to use the keyboard, and/or a serial device
depending on if you are using kdb and/or kgdb, in one of the following
scenarios. The order listed above must be observed if you use any of the
optional configurations together. Using kms + only gdb is generally not
a useful combination.

Using loadable module or built-in
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1. As a kernel built-in:

184 185 186
   Use the kernel boot argument::

	kgdboc=<tty-device>,[baud]
187 188 189

2. As a kernel loadable module:

190 191 192
   Use the command::

	modprobe kgdboc kgdboc=<tty-device>,[baud]
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

   Here are two examples of how you might format the kgdboc string. The
   first is for an x86 target using the first serial port. The second
   example is for the ARM Versatile AB using the second serial port.

   1. ``kgdboc=ttyS0,115200``

   2. ``kgdboc=ttyAMA1,115200``

Configure kgdboc at runtime with sysfs
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

At run time you can enable or disable kgdboc by echoing a parameters
into the sysfs. Here are two examples:

208
1. Enable kgdboc on ttyS0::
209

210
	echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc
211

212
2. Disable kgdboc::
213

214
	echo "" > /sys/module/kgdboc/parameters/kgdboc
215

216 217 218 219
.. note::

   You do not need to specify the baud if you are configuring the
   console on tty which is already configured or open.
220 221 222 223 224 225 226 227

More examples
^^^^^^^^^^^^^

You can configure kgdboc to use the keyboard, and/or a serial device
depending on if you are using kdb and/or kgdb, in one of the following
scenarios.

228 229 230 231 232
1. kdb and kgdb over only a serial port::

	kgdboc=<serial_device>[,baud]

   Example::
233

234
	kgdboc=ttyS0,115200
235

236
2. kdb and kgdb with keyboard and a serial port::
237

238
	kgdboc=kbd,<serial_device>[,baud]
239

240
   Example::
241

242
	kgdboc=kbd,ttyS0,115200
243

244
3. kdb with a keyboard::
245

246
	kgdboc=kbd
247

248
4. kdb with kernel mode setting::
249

250
	kgdboc=kms,kbd
251

252
5. kdb with kernel mode setting and kgdb over a serial port::
253

254
	kgdboc=kms,kbd,ttyS0,115200
255

256 257 258 259 260 261 262 263
.. note::

   Kgdboc does not support interrupting the target via the gdb remote
   protocol. You must manually send a :kbd:`SysRq-G` unless you have a proxy
   that splits console output to a terminal program. A console proxy has a
   separate TCP port for the debugger and a separate TCP port for the
   "human" console. The proxy can take care of sending the :kbd:`SysRq-G`
   for you.
264 265 266 267 268 269 270

When using kgdboc with no debugger proxy, you can end up connecting the
debugger at one of two entry points. If an exception occurs after you
have loaded kgdboc, a message should print on the console stating it is
waiting for the debugger. In this case you disconnect your terminal
program and then connect the debugger in its place. If you want to
interrupt the target system and forcibly enter a debug session you have
271
to issue a :kbd:`Sysrq` sequence and then type the letter :kbd:`g`. Then you
272
disconnect the terminal session and connect gdb. Your options if you
273
don't like this are to hack gdb to send the :kbd:`SysRq-G` for you as well as
274 275 276
on the initial connect, or to use a debugger proxy that allows an
unmodified gdb to do the debugging.

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
Kernel parameter: ``kgdboc_earlycon``
-------------------------------------

If you specify the kernel parameter ``kgdboc_earlycon`` and your serial
driver registers a boot console that supports polling (doesn't need
interrupts and implements a nonblocking read() function) kgdb will attempt
to work using the boot console until it can transition to the regular
tty driver specified by the ``kgdboc`` parameter.

Normally there is only one boot console (especially that implements the
read() function) so just adding ``kgdboc_earlycon`` on its own is
sufficient to make this work. If you have more than one boot console you
can add the boot console's name to differentiate. Note that names that
are registered through the boot console layer and the tty layer are not
the same for the same port.

For instance, on one board to be explicit you might do::

   kgdboc_earlycon=qcom_geni kgdboc=ttyMSM0

If the only boot console on the device was "qcom_geni", you could simplify::

   kgdboc_earlycon kgdboc=ttyMSM0

301 302
Kernel parameter: ``kgdbwait``
------------------------------
303 304 305 306 307 308 309 310 311 312 313 314 315

The Kernel command line option ``kgdbwait`` makes kgdb wait for a
debugger connection during booting of a kernel. You can only use this
option if you compiled a kgdb I/O driver into the kernel and you
specified the I/O driver configuration as a kernel command line option.
The kgdbwait parameter should always follow the configuration parameter
for the kgdb I/O driver in the kernel command line else the I/O driver
will not be configured prior to asking the kernel to use it to wait.

The kernel will stop and wait as early as the I/O driver and
architecture allows when you use this option. If you build the kgdb I/O
driver as a loadable kernel module kgdbwait will not do anything.

316 317
Kernel parameter: ``kgdbcon``
-----------------------------
318

319
The ``kgdbcon`` feature allows you to see printk() messages inside gdb
320
while gdb is connected to the kernel. Kdb does not make use of the kgdbcon
321 322 323 324 325 326
feature.

Kgdb supports using the gdb serial protocol to send console messages to
the debugger when the debugger is connected and running. There are two
ways to activate this feature.

327 328 329
1. Activate with the kernel command line option::

	kgdbcon
330

331
2. Use sysfs before configuring an I/O driver::
332

333
	echo 1 > /sys/module/kgdb/parameters/kgdb_use_con
334

335
.. note::
336

337
   If you do this after you configure the kgdb I/O driver, the
338 339 340
   setting will not take effect until the next point the I/O is
   reconfigured.

341 342 343 344 345 346
.. important::

   You cannot use kgdboc + kgdbcon on a tty that is an
   active system console. An example of incorrect usage is::

	console=ttyS0,115200 kgdboc=ttyS0 kgdbcon
347 348 349 350

It is possible to use this option with kgdboc on a tty that is not a
system console.

351 352
Run time parameter: ``kgdbreboot``
----------------------------------
353 354 355 356 357

The kgdbreboot feature allows you to change how the debugger deals with
the reboot notification. You have 3 choices for the behavior. The
default behavior is always set to 0.

358
.. tabularcolumns:: |p{0.4cm}|p{11.5cm}|p{5.6cm}|
359

360 361
.. flat-table::
  :widths: 1 10 8
362

363 364 365
  * - 1
    - ``echo -1 > /sys/module/debug_core/parameters/kgdbreboot``
    - Ignore the reboot notification entirely.
366

367 368 369
  * - 2
    - ``echo 0 > /sys/module/debug_core/parameters/kgdbreboot``
    - Send the detach message to any attached debugger client.
370

371 372 373
  * - 3
    - ``echo 1 > /sys/module/debug_core/parameters/kgdbreboot``
    - Enter the debugger on reboot notify.
374

375 376 377 378 379 380 381 382 383
Kernel parameter: ``nokaslr``
-----------------------------

If the architecture that you are using enable KASLR by default,
you should consider turning it off.  KASLR randomizes the
virtual address where the kernel image is mapped and confuse
gdb which resolve kernel symbol address from symbol table
of vmlinux.

384 385 386 387 388 389 390 391
Using kdb
=========

Quick start for kdb on a serial port
------------------------------------

This is a quick example of how to use kdb.

392
1. Configure kgdboc at boot using kernel parameters::
393

394
	console=ttyS0,115200 kgdboc=ttyS0,115200 nokaslr
395 396 397 398

   OR

   Configure kgdboc after the kernel has booted; assuming you are using
399
   a serial port console::
400

401
	echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc
402 403 404

2. Enter the kernel debugger manually or by waiting for an oops or
   fault. There are several ways you can enter the kernel debugger
405 406
   manually; all involve using the :kbd:`SysRq-G`, which means you must have
   enabled ``CONFIG_MAGIC_SysRq=y`` in your kernel config.
407

408
   -  When logged in as root or with a super user session you can run::
409

410
	echo g > /proc/sysrq-trigger
411 412 413

   -  Example using minicom 2.2

414
      Press: :kbd:`CTRL-A` :kbd:`f` :kbd:`g`
415 416 417 418

   -  When you have telneted to a terminal server that supports sending
      a remote break

419
      Press: :kbd:`CTRL-]`
420

421
      Type in: ``send break``
422

423
      Press: :kbd:`Enter` :kbd:`g`
424

425
3. From the kdb prompt you can run the ``help`` command to see a complete
426 427 428 429
   list of the commands that are available.

   Some useful commands in kdb include:

430 431 432 433 434
   =========== =================================================================
   ``lsmod``   Shows where kernel modules are loaded
   ``ps``      Displays only the active processes
   ``ps A``    Shows all the processes
   ``summary`` Shows kernel version info and memory usage
435
   ``bt``      Get a backtrace of the current process using dump_stack()
436 437 438
   ``dmesg``   View the kernel syslog buffer
   ``go``      Continue the system
   =========== =================================================================
439 440

4. When you are done using kdb you need to consider rebooting the system
441
   or using the ``go`` command to resuming normal kernel execution. If you
442 443 444 445 446 447 448 449 450 451
   have paused the kernel for a lengthy period of time, applications
   that rely on timely networking or anything to do with real wall clock
   time could be adversely affected, so you should take this into
   consideration when using the kernel debugger.

Quick start for kdb using a keyboard connected console
------------------------------------------------------

This is a quick example of how to use kdb with a keyboard.

452
1. Configure kgdboc at boot using kernel parameters::
453

454
	kgdboc=kbd
455 456 457

   OR

458
   Configure kgdboc after the kernel has booted::
459

460
	echo kbd > /sys/module/kgdboc/parameters/kgdboc
461 462 463

2. Enter the kernel debugger manually or by waiting for an oops or
   fault. There are several ways you can enter the kernel debugger
464 465
   manually; all involve using the :kbd:`SysRq-G`, which means you must have
   enabled ``CONFIG_MAGIC_SysRq=y`` in your kernel config.
466

467
   -  When logged in as root or with a super user session you can run::
468

469
	echo g > /proc/sysrq-trigger
470

471
   -  Example using a laptop keyboard:
472

473
      Press and hold down: :kbd:`Alt`
474

475
      Press and hold down: :kbd:`Fn`
476

477
      Press and release the key with the label: :kbd:`SysRq`
478

479
      Release: :kbd:`Fn`
480

481
      Press and release: :kbd:`g`
482

483
      Release: :kbd:`Alt`
484 485 486

   -  Example using a PS/2 101-key keyboard

487
      Press and hold down: :kbd:`Alt`
488

489
      Press and release the key with the label: :kbd:`SysRq`
490

491
      Press and release: :kbd:`g`
492

493
      Release: :kbd:`Alt`
494

495
3. Now type in a kdb command such as ``help``, ``dmesg``, ``bt`` or ``go`` to
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
   continue kernel execution.

Using kgdb / gdb
================

In order to use kgdb you must activate it by passing configuration
information to one of the kgdb I/O drivers. If you do not pass any
configuration information kgdb will not do anything at all. Kgdb will
only actively hook up to the kernel trap hooks if a kgdb I/O driver is
loaded and configured. If you unconfigure a kgdb I/O driver, kgdb will
unregister all the kernel hook points.

All kgdb I/O drivers can be reconfigured at run time, if
``CONFIG_SYSFS`` and ``CONFIG_MODULES`` are enabled, by echo'ing a new
config string to ``/sys/module/<driver>/parameter/<option>``. The driver
can be unconfigured by passing an empty string. You cannot change the
configuration while the debugger is attached. Make sure to detach the
debugger with the ``detach`` command prior to trying to unconfigure a
kgdb I/O driver.

Connecting with gdb to a serial port
------------------------------------

1. Configure kgdboc

521
   Configure kgdboc at boot using kernel parameters::
522

523
	kgdboc=ttyS0,115200
524 525 526

   OR

527
   Configure kgdboc after the kernel has booted::
528

529
	echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc
530 531 532 533 534

2. Stop kernel execution (break into the debugger)

   In order to connect to gdb via kgdboc, the kernel must first be
   stopped. There are several ways to stop the kernel which include
535
   using kgdbwait as a boot argument, via a :kbd:`SysRq-G`, or running the
536 537 538
   kernel until it takes an exception where it waits for the debugger to
   attach.

539
   -  When logged in as root or with a super user session you can run::
540

541
	echo g > /proc/sysrq-trigger
542 543 544

   -  Example using minicom 2.2

545
      Press: :kbd:`CTRL-A` :kbd:`f` :kbd:`g`
546 547 548 549

   -  When you have telneted to a terminal server that supports sending
      a remote break

550
      Press: :kbd:`CTRL-]`
551

552
      Type in: ``send break``
553

554
      Press: :kbd:`Enter` :kbd:`g`
555 556 557

3. Connect from gdb

558
   Example (using a directly connected port)::
559 560 561 562 563 564

           % gdb ./vmlinux
           (gdb) set remotebaud 115200
           (gdb) target remote /dev/ttyS0


565
   Example (kgdb to a terminal server on TCP port 2012)::
566 567 568 569 570 571 572 573 574 575 576

           % gdb ./vmlinux
           (gdb) target remote 192.168.2.2:2012


   Once connected, you can debug a kernel the way you would debug an
   application program.

   If you are having problems connecting or something is going seriously
   wrong while debugging, it will most often be the case that you want
   to enable gdb to be verbose about its target communications. You do
577 578 579
   this prior to issuing the ``target remote`` command by typing in::

	set debug remote 1
580 581

Remember if you continue in gdb, and need to "break in" again, you need
582 583
to issue an other :kbd:`SysRq-G`. It is easy to create a simple entry point by
putting a breakpoint at ``sys_sync`` and then you can run ``sync`` from a
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
shell or script to break into the debugger.

kgdb and kdb interoperability
=============================

It is possible to transition between kdb and kgdb dynamically. The debug
core will remember which you used the last time and automatically start
in the same mode.

Switching between kdb and kgdb
------------------------------

Switching from kgdb to kdb
~~~~~~~~~~~~~~~~~~~~~~~~~~

There are two ways to switch from kgdb to kdb: you can use gdb to issue
600
a maintenance packet, or you can blindly type the command ``$3#33``.
601 602 603 604 605
Whenever the kernel debugger stops in kgdb mode it will print the
message ``KGDB or $3#33 for KDB``. It is important to note that you have
to type the sequence correctly in one pass. You cannot type a backspace
or delete because kgdb will interpret that as part of the debug stream.

606
1. Change from kgdb to kdb by blindly typing::
607

608
	$3#33
609

610
2. Change from kgdb to kdb with gdb::
611

612
	maintenance packet 3
613

614 615 616 617 618 619
   .. note::

     Now you must kill gdb. Typically you press :kbd:`CTRL-Z` and issue
     the command::

	kill -9 %
620 621 622 623 624 625 626 627 628 629 630

Change from kdb to kgdb
~~~~~~~~~~~~~~~~~~~~~~~

There are two ways you can change from kdb to kgdb. You can manually
enter kgdb mode by issuing the kgdb command from the kdb shell prompt,
or you can connect gdb while the kdb shell prompt is active. The kdb
shell looks for the typical first commands that gdb would issue with the
gdb remote protocol and if it sees one of those commands it
automatically changes into kgdb mode.

631
1. From kdb issue the command::
632

633
	kgdb
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651

   Now disconnect your terminal program and connect gdb in its place

2. At the kdb prompt, disconnect the terminal program and connect gdb in
   its place.

Running kdb commands from gdb
-----------------------------

It is possible to run a limited set of kdb commands from gdb, using the
gdb monitor command. You don't want to execute any of the run control or
breakpoint operations, because it can disrupt the state of the kernel
debugger. You should be using gdb for breakpoints and run control
operations if you have gdb connected. The more useful commands to run
are things like lsmod, dmesg, ps or possibly some of the memory
information commands. To see all the kdb commands you can run
``monitor help``.

652
Example::
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668

    (gdb) monitor ps
    1 idle process (state I) and
    27 sleeping system daemon (state M) processes suppressed,
    use 'ps A' to see all.
    Task Addr       Pid   Parent [*] cpu State Thread     Command

    0xc78291d0        1        0  0    0   S  0xc7829404  init
    0xc7954150      942        1  0    0   S  0xc7954384  dropbear
    0xc78789c0      944        1  0    0   S  0xc7878bf4  sh
    (gdb)

kgdb Test Suite
===============

When kgdb is enabled in the kernel config you can also elect to enable
669
the config parameter ``KGDB_TESTS``. Turning this on will enable a special
670 671 672 673 674 675
kgdb I/O module which is designed to test the kgdb internal functions.

The kgdb tests are mainly intended for developers to test the kgdb
internals as well as a tool for developing a new kgdb architecture
specific implementation. These tests are not really for end users of the
Linux kernel. The primary source of documentation would be to look in
676
the ``drivers/misc/kgdbts.c`` file.
677 678 679

The kgdb test suite can also be configured at compile time to run the
core set of tests by setting the kernel config parameter
680
``KGDB_TESTS_ON_BOOT``. This particular option is aimed at automated
681 682
regression testing and does not require modifying the kernel boot config
arguments. If this is turned on, the kgdb test suite can be disabled by
683
specifying ``kgdbts=`` as a kernel boot argument.
684 685 686 687 688 689 690 691 692 693 694

Kernel Debugger Internals
=========================

Architecture Specifics
----------------------

The kernel debugger is organized into a number of components:

1. The debug core

695
   The debug core is found in ``kernel/debugger/debug_core.c``. It
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
   contains:

   -  A generic OS exception handler which includes sync'ing the
      processors into a stopped state on an multi-CPU system.

   -  The API to talk to the kgdb I/O drivers

   -  The API to make calls to the arch-specific kgdb implementation

   -  The logic to perform safe memory reads and writes to memory while
      using the debugger

   -  A full implementation for software breakpoints unless overridden
      by the arch

   -  The API to invoke either the kdb or kgdb frontend to the debug
      core.

   -  The structures and callback API for atomic kernel mode setting.

716
      .. note:: kgdboc is where the kms callbacks are invoked.
717 718 719

2. kgdb arch-specific implementation

720 721
   This implementation is generally found in ``arch/*/kernel/kgdb.c``. As
   an example, ``arch/x86/kernel/kgdb.c`` contains the specifics to
722 723 724 725 726
   implement HW breakpoint as well as the initialization to dynamically
   register and unregister for the trap handlers on this architecture.
   The arch-specific portion implements:

   -  contains an arch-specific trap catcher which invokes
727
      kgdb_handle_exception() to start kgdb about doing its work
728

729
   -  translation to and from gdb specific packet format to :c:type:`pt_regs`
730 731 732 733 734 735 736 737 738 739 740 741

   -  Registration and unregistration of architecture specific trap
      hooks

   -  Any special exception handling and cleanup

   -  NMI exception handling and cleanup

   -  (optional) HW breakpoints

3. gdbstub frontend (aka kgdb)

742
   The gdbstub is located in ``kernel/debug/gdbstub.c``. It contains:
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760

   -  All the logic to implement the gdb serial protocol

4. kdb frontend

   The kdb debugger shell is broken down into a number of components.
   The kdb core is located in kernel/debug/kdb. There are a number of
   helper functions in some of the other kernel components to make it
   possible for kdb to examine and report information about the kernel
   without taking locks that could cause a kernel deadlock. The kdb core
   contains implements the following functionality.

   -  A simple shell

   -  The kdb core command set

   -  A registration API to register additional kdb shell commands.

761
      -  A good example of a self-contained kdb module is the ``ftdump``
762
         command for dumping the ftrace buffer. See:
763
         ``kernel/trace/trace_kdb.c``
764 765 766

      -  For an example of how to dynamically register a new kdb command
         you can build the kdb_hello.ko kernel module from
767 768 769 770
         ``samples/kdb/kdb_hello.c``. To build this example you can set
         ``CONFIG_SAMPLES=y`` and ``CONFIG_SAMPLE_KDB=m`` in your kernel
         config. Later run ``modprobe kdb_hello`` and the next time you
         enter the kdb shell, you can run the ``hello`` command.
771

772
   -  The implementation for kdb_printf() which emits messages directly
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
      to I/O drivers, bypassing the kernel log.

   -  SW / HW breakpoint management for the kdb shell

5. kgdb I/O driver

   Each kgdb I/O driver has to provide an implementation for the
   following:

   -  configuration via built-in or module

   -  dynamic configuration and kgdb hook registration calls

   -  read and write character interface

   -  A cleanup handler for unconfiguring from the kgdb core

   -  (optional) Early debug methodology

   Any given kgdb I/O driver has to operate very closely with the
   hardware and must do it in such a way that does not enable interrupts
   or change other parts of the system context without completely
   restoring them. The kgdb core will repeatedly "poll" a kgdb I/O
   driver for characters when it needs input. The I/O driver is expected
   to return immediately if there is no data available. Doing so allows
   for the future possibility to touch watchdog hardware in such a way
   as to have a target system not reset when these are enabled.

If you are intent on adding kgdb architecture specific support for a new
architecture, the architecture should define ``HAVE_ARCH_KGDB`` in the
architecture specific Kconfig file. This will enable kgdb for the
architecture, and at that point you must create an architecture specific
kgdb implementation.

There are a few flags which must be set on every architecture in their
808
``asm/kgdb.h`` file. These are:
809

810 811 812
-  ``NUMREGBYTES``:
     The size in bytes of all of the registers, so that we
     can ensure they will all fit into a packet.
813

814 815 816
-  ``BUFMAX``:
     The size in bytes of the buffer GDB will read into. This must
     be larger than NUMREGBYTES.
817

818 819 820 821 822
-  ``CACHE_FLUSH_IS_SAFE``:
     Set to 1 if it is always safe to call
     flush_cache_range or flush_icache_range. On some architectures,
     these functions may not be safe to call on SMP since we keep other
     CPUs in a holding pattern.
823 824

There are also the following functions for the common backend, found in
825
``kernel/kgdb.c``, that must be supplied by the architecture-specific
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
backend unless marked as (optional), in which case a default function
maybe used if the architecture does not need to provide a specific
implementation.

.. kernel-doc:: include/linux/kgdb.h
   :internal:

kgdboc internals
----------------

kgdboc and uarts
~~~~~~~~~~~~~~~~

The kgdboc driver is actually a very thin driver that relies on the
underlying low level to the hardware driver having "polling hooks" to
which the tty driver is attached. In the initial implementation of
kgdboc the serial_core was changed to expose a low level UART hook for
doing polled mode reading and writing of a single character while in an
atomic context. When kgdb makes an I/O request to the debugger, kgdboc
invokes a callback in the serial core which in turn uses the callback in
the UART driver.

When using kgdboc with a UART, the UART driver must implement two
849 850
callbacks in the :c:type:`struct uart_ops <uart_ops>`.
Example from ``drivers/8250.c``::
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868


    #ifdef CONFIG_CONSOLE_POLL
        .poll_get_char = serial8250_get_poll_char,
        .poll_put_char = serial8250_put_poll_char,
    #endif


Any implementation specifics around creating a polling driver use the
``#ifdef CONFIG_CONSOLE_POLL``, as shown above. Keep in mind that
polling hooks have to be implemented in such a way that they can be
called from an atomic context and have to restore the state of the UART
chip on return such that the system can return to normal when the
debugger detaches. You need to be very careful with any kind of lock you
consider, because failing here is most likely going to mean pressing the
reset button.

kgdboc and keyboards
869
~~~~~~~~~~~~~~~~~~~~~~~~
870 871 872

The kgdboc driver contains logic to configure communications with an
attached keyboard. The keyboard infrastructure is only compiled into the
873
kernel when ``CONFIG_KDB_KEYBOARD=y`` is set in the kernel configuration.
874

875
The core polled keyboard driver for PS/2 type keyboards is in
876
``drivers/char/kdb_keyboard.c``. This driver is hooked into the debug core
877
when kgdboc populates the callback in the array called
878
:c:type:`kdb_poll_funcs[]`. The kdb_get_kbd_char() is the top-level
879 880 881
function which polls hardware for single character input.

kgdboc and kms
882
~~~~~~~~~~~~~~~~~~
883 884

The kgdboc driver contains logic to request the graphics display to
885
switch to a text context when you are using ``kgdboc=kms,kbd``, provided
886 887 888 889
that you have a video driver which has a frame buffer console and atomic
kernel mode setting support.

Every time the kernel debugger is entered it calls
890
kgdboc_pre_exp_handler() which in turn calls con_debug_enter()
891
in the virtual console layer. On resuming kernel execution, the kernel
892 893
debugger calls kgdboc_post_exp_handler() which in turn calls
con_debug_leave().
894 895

Any video driver that wants to be compatible with the kernel debugger
896 897 898
and the atomic kms callbacks must implement the ``mode_set_base_atomic``,
``fb_debug_enter`` and ``fb_debug_leave operations``. For the
``fb_debug_enter`` and ``fb_debug_leave`` the option exists to use the
899 900 901
generic drm fb helper functions or implement something custom for the
hardware. The following example shows the initialization of the
.mode_set_base_atomic operation in
902
drivers/gpu/drm/i915/intel_display.c::
903 904 905 906 907 908 909 910 911 912 913


    static const struct drm_crtc_helper_funcs intel_helper_funcs = {
    [...]
            .mode_set_base_atomic = intel_pipe_set_base_atomic,
    [...]
    };


Here is an example of how the i915 driver initializes the
fb_debug_enter and fb_debug_leave functions to use the generic drm
914
helpers in ``drivers/gpu/drm/i915/intel_fb.c``::
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929


    static struct fb_ops intelfb_ops = {
    [...]
           .fb_debug_enter = drm_fb_helper_debug_enter,
           .fb_debug_leave = drm_fb_helper_debug_leave,
    [...]
    };


Credits
=======

The following people have contributed to this document:

930
1. Amit Kale <amitkale@linsyssoft.com>
931

932
2. Tom Rini <trini@kernel.crashing.org>
933 934 935

In March 2008 this document was completely rewritten by:

936
-  Jason Wessel <jason.wessel@windriver.com>
937 938 939

In Jan 2010 this document was updated to include kdb.

940
-  Jason Wessel <jason.wessel@windriver.com>