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