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 a 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 look under
98    "Kernel hacking" / "Kernel debugging" and select "KGDB: kernel debugger".
99    </para>
100    <para>
101    While it is not a hard requirement that you have symbols in your
102    vmlinux file, gdb tends not to be very useful without the symbolic
103    data, so you will want to turn
104    on <symbol>CONFIG_DEBUG_INFO</symbol> which is called "Compile the
105    kernel with debug info" in the config menu.
106    </para>
107    <para>
108    It is advised, but not required, that you turn on the
109    <symbol>CONFIG_FRAME_POINTER</symbol> kernel option which is called "Compile the
110    kernel with frame pointers" in the config menu.  This option
111    inserts code to into the compiled executable which saves the frame
112    information in registers or on the stack at different points which
113    allows a debugger such as gdb to more accurately construct
114    stack back traces while debugging the kernel.
115    </para>
116    <para>
117    If the architecture that you are using supports the kernel option
118    CONFIG_DEBUG_RODATA, you should consider turning it off.  This
119    option will prevent the use of software breakpoints because it
120    marks certain regions of the kernel's memory space as read-only.
121    If kgdb supports it for the architecture you are using, you can
122    use hardware breakpoints if you desire to run with the
123    CONFIG_DEBUG_RODATA option turned on, else you need to turn off
124    this option.
125    </para>
126    <para>
127    Next you should choose one of more I/O drivers to interconnect
128    debugging host and debugged target.  Early boot debugging requires
129    a KGDB I/O driver that supports early debugging and the driver
130    must be built into the kernel directly. Kgdb I/O driver
131    configuration takes place via kernel or module parameters which
132    you can learn more about in the in the section that describes the
133    parameter "kgdboc".
134    </para>
135    <para>Here is an example set of .config symbols to enable or
136    disable for kgdb:
137    <itemizedlist>
138    <listitem><para># CONFIG_DEBUG_RODATA is not set</para></listitem>
139    <listitem><para>CONFIG_FRAME_POINTER=y</para></listitem>
140    <listitem><para>CONFIG_KGDB=y</para></listitem>
141    <listitem><para>CONFIG_KGDB_SERIAL_CONSOLE=y</para></listitem>
142    </itemizedlist>
143    </para>
144  </sect1>
145  <sect1 id="CompileKDB">
146    <title>Kernel config options for kdb</title>
147    <para>Kdb is quite a bit more complex than the simple gdbstub
148    sitting on top of the kernel's debug core.  Kdb must implement a
149    shell, and also adds some helper functions in other parts of the
150    kernel, responsible for printing out interesting data such as what
151    you would see if you ran "lsmod", or "ps".  In order to build kdb
152    into the kernel you follow the same steps as you would for kgdb.
153    </para>
154    <para>The main config option for kdb
155    is <symbol>CONFIG_KGDB_KDB</symbol> which is called "KGDB_KDB:
156    include kdb frontend for kgdb" in the config menu.  In theory you
157    would have already also selected an I/O driver such as the
158    CONFIG_KGDB_SERIAL_CONSOLE interface if you plan on using kdb on a
159    serial port, when you were configuring kgdb.
160    </para>
161    <para>If you want to use a PS/2-style keyboard with kdb, you would
162    select CONFIG_KDB_KEYBOARD which is called "KGDB_KDB: keyboard as
163    input device" in the config menu.  The CONFIG_KDB_KEYBOARD option
164    is not used for anything in the gdb interface to kgdb.  The
165    CONFIG_KDB_KEYBOARD option only works with kdb.
166    </para>
167    <para>Here is an example set of .config symbols to enable/disable kdb:
168    <itemizedlist>
169    <listitem><para># CONFIG_DEBUG_RODATA is not set</para></listitem>
170    <listitem><para>CONFIG_FRAME_POINTER=y</para></listitem>
171    <listitem><para>CONFIG_KGDB=y</para></listitem>
172    <listitem><para>CONFIG_KGDB_SERIAL_CONSOLE=y</para></listitem>
173    <listitem><para>CONFIG_KGDB_KDB=y</para></listitem>
174    <listitem><para>CONFIG_KDB_KEYBOARD=y</para></listitem>
175    </itemizedlist>
176    </para>
177  </sect1>
178  </chapter>
179  <chapter id="kgdbKernelArgs">
180  <title>Kernel Debugger Boot Arguments</title>
181  <para>This section describes the various runtime kernel
182  parameters that affect the configuration of the kernel debugger.
183  The following chapter covers using kdb and kgdb as well as
184  providing some examples of the configuration parameters.</para>
185   <sect1 id="kgdboc">
186   <title>Kernel parameter: kgdboc</title>
187   <para>The kgdboc driver was originally an abbreviation meant to
188   stand for "kgdb over console".  Today it is the primary mechanism
189   to configure how to communicate from gdb to kgdb as well as the
190   devices you want to use to interact with the kdb shell.
191   </para>
192   <para>For kgdb/gdb, kgdboc is designed to work with a single serial
193   port. It is intended to cover the circumstance where you want to
194   use a serial console as your primary console as well as using it to
195   perform kernel debugging.  It is also possible to use kgdb on a
196   serial port which is not designated as a system console.  Kgdboc
197   may be configured as a kernel built-in or a kernel loadable module.
198   You can only make use of <constant>kgdbwait</constant> and early
199   debugging if you build kgdboc into the kernel as a built-in.
200   </para>
201   <para>Optionally you can elect to activate kms (Kernel Mode
202   Setting) integration.  When you use kms with kgdboc and you have a
203   video driver that has atomic mode setting hooks, it is possible to
204   enter the debugger on the graphics console.  When the kernel
205   execution is resumed, the previous graphics mode will be restored.
206   This integration can serve as a useful tool to aid in diagnosing
207   crashes or doing analysis of memory with kdb while allowing the
208   full graphics console applications to run.
209   </para>
210   <sect2 id="kgdbocArgs">
211   <title>kgdboc arguments</title>
212   <para>Usage: <constant>kgdboc=[kms][[,]kbd][[,]serial_device][,baud]</constant></para>
213   <para>The order listed above must be observed if you use any of the
214   optional configurations together.
215   </para>
216   <para>Abbreviations:
217   <itemizedlist>
218   <listitem><para>kms = Kernel Mode Setting</para></listitem>
219   <listitem><para>kbd = Keyboard</para></listitem>
220   </itemizedlist>
221   </para>
222   <para>You can configure kgdboc to use the keyboard, and/or a serial
223   device depending on if you are using kdb and/or kgdb, in one of the
224   following scenarios.  The order listed above must be observed if
225   you use any of the optional configurations together.  Using kms +
226   only gdb is generally not a useful combination.</para>
227   <sect3 id="kgdbocArgs1">
228   <title>Using loadable module or built-in</title>
229   <para>
230   <orderedlist>
231   <listitem><para>As a kernel built-in:</para>
232   <para>Use the kernel boot argument: <constant>kgdboc=&lt;tty-device&gt;,[baud]</constant></para></listitem>
233   <listitem>
234   <para>As a kernel loadable module:</para>
235   <para>Use the command: <constant>modprobe kgdboc kgdboc=&lt;tty-device&gt;,[baud]</constant></para>
236   <para>Here are two examples of how you might format the kgdboc
237   string. The first is for an x86 target using the first serial port.
238   The second example is for the ARM Versatile AB using the second
239   serial port.
240   <orderedlist>
241   <listitem><para><constant>kgdboc=ttyS0,115200</constant></para></listitem>
242   <listitem><para><constant>kgdboc=ttyAMA1,115200</constant></para></listitem>
243   </orderedlist>
244   </para>
245   </listitem>
246   </orderedlist></para>
247   </sect3>
248   <sect3 id="kgdbocArgs2">
249   <title>Configure kgdboc at runtime with sysfs</title>
250   <para>At run time you can enable or disable kgdboc by echoing a
251   parameters into the sysfs.  Here are two examples:</para>
252   <orderedlist>
253   <listitem><para>Enable kgdboc on ttyS0</para>
254   <para><constant>echo ttyS0 &gt; /sys/module/kgdboc/parameters/kgdboc</constant></para></listitem>
255   <listitem><para>Disable kgdboc</para>
256   <para><constant>echo "" &gt; /sys/module/kgdboc/parameters/kgdboc</constant></para></listitem>
257   </orderedlist>
258   <para>NOTE: You do not need to specify the baud if you are
259   configuring the console on tty which is already configured or
260   open.</para>
261   </sect3>
262   <sect3 id="kgdbocArgs3">
263   <title>More examples</title>
264   <para>You can configure kgdboc to use the keyboard, and/or a serial device
265   depending on if you are using kdb and/or kgdb, in one of the
266   following scenarios.
267   <orderedlist>
268   <listitem><para>kdb and kgdb over only a serial port</para>
269   <para><constant>kgdboc=&lt;serial_device&gt;[,baud]</constant></para>
270   <para>Example: <constant>kgdboc=ttyS0,115200</constant></para>
271   </listitem>
272   <listitem><para>kdb and kgdb with keyboard and a serial port</para>
273   <para><constant>kgdboc=kbd,&lt;serial_device&gt;[,baud]</constant></para>
274   <para>Example: <constant>kgdboc=kbd,ttyS0,115200</constant></para>
275   </listitem>
276   <listitem><para>kdb with a keyboard</para>
277   <para><constant>kgdboc=kbd</constant></para>
278   </listitem>
279   <listitem><para>kdb with kernel mode setting</para>
280   <para><constant>kgdboc=kms,kbd</constant></para>
281   </listitem>
282   <listitem><para>kdb with kernel mode setting and kgdb over a serial port</para>
283   <para><constant>kgdboc=kms,kbd,ttyS0,115200</constant></para>
284   </listitem>
285   </orderedlist>
286   </para>
287   <para>NOTE: Kgdboc does not support interrupting the target via the
288   gdb remote protocol.  You must manually send a sysrq-g unless you
289   have a proxy that splits console output to a terminal program.
290   A console proxy has a separate TCP port for the debugger and a separate
291   TCP port for the "human" console.  The proxy can take care of sending
292   the sysrq-g for you.
293   </para>
294   <para>When using kgdboc with no debugger proxy, you can end up
295    connecting the debugger at one of two entry points.  If an
296    exception occurs after you have loaded kgdboc, a message should
297    print on the console stating it is waiting for the debugger.  In
298    this case you disconnect your terminal program and then connect the
299    debugger in its place.  If you want to interrupt the target system
300    and forcibly enter a debug session you have to issue a Sysrq
301    sequence and then type the letter <constant>g</constant>.  Then
302    you disconnect the terminal session and connect gdb.  Your options
303    if you don't like this are to hack gdb to send the sysrq-g for you
304    as well as on the initial connect, or to use a debugger proxy that
305    allows an unmodified gdb to do the debugging.
306   </para>
307   </sect3>
308   </sect2>
309   </sect1>
310   <sect1 id="kgdbwait">
311   <title>Kernel parameter: kgdbwait</title>
312   <para>
313   The Kernel command line option <constant>kgdbwait</constant> makes
314   kgdb wait for a debugger connection during booting of a kernel.  You
315   can only use this option if you compiled a kgdb I/O driver into the
316   kernel and you specified the I/O driver configuration as a kernel
317   command line option.  The kgdbwait parameter should always follow the
318   configuration parameter for the kgdb I/O driver in the kernel
319   command line else the I/O driver will not be configured prior to
320   asking the kernel to use it to wait.
321   </para>
322   <para>
323   The kernel will stop and wait as early as the I/O driver and
324   architecture allows when you use this option.  If you build the
325   kgdb I/O driver as a loadable kernel module kgdbwait will not do
326   anything.
327   </para>
328   </sect1>
329   <sect1 id="kgdbcon">
330   <title>Kernel parameter: kgdbcon</title>
331   <para> The kgdbcon feature allows you to see printk() messages
332   inside gdb while gdb is connected to the kernel.  Kdb does not make
333    use of the kgdbcon feature.
334   </para>
335   <para>Kgdb supports using the gdb serial protocol to send console
336   messages to the debugger when the debugger is connected and running.
337   There are two ways to activate this feature.
338   <orderedlist>
339   <listitem><para>Activate with the kernel command line option:</para>
340   <para><constant>kgdbcon</constant></para>
341   </listitem>
342   <listitem><para>Use sysfs before configuring an I/O driver</para>
343   <para>
344   <constant>echo 1 &gt; /sys/module/kgdb/parameters/kgdb_use_con</constant>
345   </para>
346   <para>
347   NOTE: If you do this after you configure the kgdb I/O driver, the
348   setting will not take effect until the next point the I/O is
349   reconfigured.
350   </para>
351   </listitem>
352   </orderedlist>
353  </para>
354   <para>IMPORTANT NOTE: You cannot use kgdboc + kgdbcon on a tty that is an
355   active system console.  An example of incorrect usage is <constant>console=ttyS0,115200 kgdboc=ttyS0 kgdbcon</constant>
356   </para>
357   <para>It is possible to use this option with kgdboc on a tty that is not a system console.
358   </para>
359  </sect1>
360   <sect1 id="kgdbreboot">
361   <title>Run time parameter: kgdbreboot</title>
362   <para> The kgdbreboot feature allows you to change how the debugger
363   deals with the reboot notification.  You have 3 choices for the
364   behavior.  The default behavior is always set to 0.</para>
365   <orderedlist>
366   <listitem><para>echo -1 > /sys/module/debug_core/parameters/kgdbreboot</para>
367   <para>Ignore the reboot notification entirely.</para>
368   </listitem>
369   <listitem><para>echo 0 > /sys/module/debug_core/parameters/kgdbreboot</para>
370   <para>Send the detach message to any attached debugger client.</para>
371   </listitem>
372   <listitem><para>echo 1 > /sys/module/debug_core/parameters/kgdbreboot</para>
373   <para>Enter the debugger on reboot notify.</para>
374   </listitem>
375   </orderedlist>
376  </sect1>
377  </chapter>
378  <chapter id="usingKDB">
379  <title>Using kdb</title>
380  <para>
381  </para>
382  <sect1 id="quickKDBserial">
383  <title>Quick start for kdb on a serial port</title>
384  <para>This is a quick example of how to use kdb.</para>
385  <para><orderedlist>
386  <listitem><para>Configure kgdboc at boot using kernel parameters:
387  <itemizedlist>
388  <listitem><para><constant>console=ttyS0,115200 kgdboc=ttyS0,115200</constant></para></listitem>
389  </itemizedlist></para>
390  <para>OR</para>
391  <para>Configure kgdboc after the kernel has booted; assuming you are using a serial port console:
392  <itemizedlist>
393  <listitem><para><constant>echo ttyS0 &gt; /sys/module/kgdboc/parameters/kgdboc</constant></para></listitem>
394  </itemizedlist>
395  </para>
396  </listitem>
397  <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>
398  <itemizedlist>
399  <listitem><para>When logged in as root or with a super user session you can run:</para>
400   <para><constant>echo g &gt; /proc/sysrq-trigger</constant></para></listitem>
401  <listitem><para>Example using minicom 2.2</para>
402  <para>Press: <constant>Control-a</constant></para>
403  <para>Press: <constant>f</constant></para>
404  <para>Press: <constant>g</constant></para>
405  </listitem>
406  <listitem><para>When you have telneted to a terminal server that supports sending a remote break</para>
407  <para>Press: <constant>Control-]</constant></para>
408  <para>Type in:<constant>send break</constant></para>
409  <para>Press: <constant>Enter</constant></para>
410  <para>Press: <constant>g</constant></para>
411  </listitem>
412  </itemizedlist>
413  </listitem>
414  <listitem><para>From the kdb prompt you can run the "help" command to see a complete list of the commands that are available.</para>
415  <para>Some useful commands in kdb include:
416  <itemizedlist>
417  <listitem><para>lsmod  -- Shows where kernel modules are loaded</para></listitem>
418  <listitem><para>ps -- Displays only the active processes</para></listitem>
419  <listitem><para>ps A -- Shows all the processes</para></listitem>
420  <listitem><para>summary -- Shows kernel version info and memory usage</para></listitem>
421  <listitem><para>bt -- Get a backtrace of the current process using dump_stack()</para></listitem>
422  <listitem><para>dmesg -- View the kernel syslog buffer</para></listitem>
423  <listitem><para>go -- Continue the system</para></listitem>
424  </itemizedlist>
425  </para>
426  </listitem>
427  <listitem>
428  <para>When you are done using kdb you need to consider rebooting the
429  system or using the "go" command to resuming normal kernel
430  execution.  If you have paused the kernel for a lengthy period of
431  time, applications that rely on timely networking or anything to do
432  with real wall clock time could be adversely affected, so you
433  should take this into consideration when using the kernel
434  debugger.</para>
435  </listitem>
436  </orderedlist></para>
437  </sect1>
438  <sect1 id="quickKDBkeyboard">
439  <title>Quick start for kdb using a keyboard connected console</title>
440  <para>This is a quick example of how to use kdb with a keyboard.</para>
441  <para><orderedlist>
442  <listitem><para>Configure kgdboc at boot using kernel parameters:
443  <itemizedlist>
444  <listitem><para><constant>kgdboc=kbd</constant></para></listitem>
445  </itemizedlist></para>
446  <para>OR</para>
447  <para>Configure kgdboc after the kernel has booted:
448  <itemizedlist>
449  <listitem><para><constant>echo kbd &gt; /sys/module/kgdboc/parameters/kgdboc</constant></para></listitem>
450  </itemizedlist>
451  </para>
452  </listitem>
453  <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>
454  <itemizedlist>
455  <listitem><para>When logged in as root or with a super user session you can run:</para>
456   <para><constant>echo g &gt; /proc/sysrq-trigger</constant></para></listitem>
457  <listitem><para>Example using a laptop keyboard</para>
458  <para>Press and hold down: <constant>Alt</constant></para>
459  <para>Press and hold down: <constant>Fn</constant></para>
460  <para>Press and release the key with the label: <constant>SysRq</constant></para>
461  <para>Release: <constant>Fn</constant></para>
462  <para>Press and release: <constant>g</constant></para>
463  <para>Release: <constant>Alt</constant></para>
464  </listitem>
465  <listitem><para>Example using a PS/2 101-key keyboard</para>
466  <para>Press and hold down: <constant>Alt</constant></para>
467  <para>Press and release the key with the label: <constant>SysRq</constant></para>
468  <para>Press and release: <constant>g</constant></para>
469  <para>Release: <constant>Alt</constant></para>
470  </listitem>
471  </itemizedlist>
472  </listitem>
473  <listitem>
474  <para>Now type in a kdb command such as "help", "dmesg", "bt" or "go" to continue kernel execution.</para>
475  </listitem>
476  </orderedlist></para>
477  </sect1>
478  </chapter>
479  <chapter id="EnableKGDB">
480   <title>Using kgdb / gdb</title>
481   <para>In order to use kgdb you must activate it by passing
482   configuration information to one of the kgdb I/O drivers.  If you
483   do not pass any configuration information kgdb will not do anything
484   at all.  Kgdb will only actively hook up to the kernel trap hooks
485   if a kgdb I/O driver is loaded and configured.  If you unconfigure
486   a kgdb I/O driver, kgdb will unregister all the kernel hook points.
487   </para>
488   <para> All kgdb I/O drivers can be reconfigured at run time, if
489   <symbol>CONFIG_SYSFS</symbol> and <symbol>CONFIG_MODULES</symbol>
490   are enabled, by echo'ing a new config string to
491   <constant>/sys/module/&lt;driver&gt;/parameter/&lt;option&gt;</constant>.
492   The driver can be unconfigured by passing an empty string.  You cannot
493   change the configuration while the debugger is attached.  Make sure
494   to detach the debugger with the <constant>detach</constant> command
495   prior to trying to unconfigure a kgdb I/O driver.
496   </para>
497  <sect1 id="ConnectingGDB">
498  <title>Connecting with gdb to a serial port</title>
499  <orderedlist>
500  <listitem><para>Configure kgdboc</para>
501   <para>Configure kgdboc at boot using kernel parameters:
502   <itemizedlist>
503    <listitem><para><constant>kgdboc=ttyS0,115200</constant></para></listitem>
504   </itemizedlist></para>
505   <para>OR</para>
506   <para>Configure kgdboc after the kernel has booted:
507   <itemizedlist>
508    <listitem><para><constant>echo ttyS0 &gt; /sys/module/kgdboc/parameters/kgdboc</constant></para></listitem>
509   </itemizedlist></para>
510  </listitem>
511  <listitem>
512  <para>Stop kernel execution (break into the debugger)</para>
513  <para>In order to connect to gdb via kgdboc, the kernel must
514  first be stopped.  There are several ways to stop the kernel which
515  include using kgdbwait as a boot argument, via a sysrq-g, or running
516  the kernel until it takes an exception where it waits for the
517  debugger to attach.
518  <itemizedlist>
519  <listitem><para>When logged in as root or with a super user session you can run:</para>
520   <para><constant>echo g &gt; /proc/sysrq-trigger</constant></para></listitem>
521  <listitem><para>Example using minicom 2.2</para>
522  <para>Press: <constant>Control-a</constant></para>
523  <para>Press: <constant>f</constant></para>
524  <para>Press: <constant>g</constant></para>
525  </listitem>
526  <listitem><para>When you have telneted to a terminal server that supports sending a remote break</para>
527  <para>Press: <constant>Control-]</constant></para>
528  <para>Type in:<constant>send break</constant></para>
529  <para>Press: <constant>Enter</constant></para>
530  <para>Press: <constant>g</constant></para>
531  </listitem>
532  </itemizedlist>
533  </para>
534  </listitem>
535  <listitem>
536    <para>Connect from gdb</para>
537    <para>
538    Example (using a directly connected port):
539    </para>
540    <programlisting>
541    % gdb ./vmlinux
542    (gdb) set remotebaud 115200
543    (gdb) target remote /dev/ttyS0
544    </programlisting>
545    <para>
546    Example (kgdb to a terminal server on TCP port 2012):
547    </para>
548    <programlisting>
549    % gdb ./vmlinux
550    (gdb) target remote 192.168.2.2:2012
551    </programlisting>
552    <para>
553    Once connected, you can debug a kernel the way you would debug an
554    application program.
555    </para>
556    <para>
557    If you are having problems connecting or something is going
558    seriously wrong while debugging, it will most often be the case
559    that you want to enable gdb to be verbose about its target
560    communications.  You do this prior to issuing the <constant>target
561    remote</constant> command by typing in: <constant>set debug remote 1</constant>
562    </para>
563  </listitem>
564  </orderedlist>
565  <para>Remember if you continue in gdb, and need to "break in" again,
566  you need to issue an other sysrq-g.  It is easy to create a simple
567  entry point by putting a breakpoint at <constant>sys_sync</constant>
568  and then you can run "sync" from a shell or script to break into the
569  debugger.</para>
570  </sect1>
571  </chapter>
572  <chapter id="switchKdbKgdb">
573  <title>kgdb and kdb interoperability</title>
574  <para>It is possible to transition between kdb and kgdb dynamically.
575  The debug core will remember which you used the last time and
576  automatically start in the same mode.</para>
577  <sect1>
578  <title>Switching between kdb and kgdb</title>
579  <sect2>
580  <title>Switching from kgdb to kdb</title>
581  <para>
582  There are two ways to switch from kgdb to kdb: you can use gdb to
583  issue a maintenance packet, or you can blindly type the command $3#33.
584  Whenever the kernel debugger stops in kgdb mode it will print the
585  message <constant>KGDB or $3#33 for KDB</constant>.  It is important
586  to note that you have to type the sequence correctly in one pass.
587  You cannot type a backspace or delete because kgdb will interpret
588  that as part of the debug stream.
589  <orderedlist>
590  <listitem><para>Change from kgdb to kdb by blindly typing:</para>
591  <para><constant>$3#33</constant></para></listitem>
592  <listitem><para>Change from kgdb to kdb with gdb</para>
593  <para><constant>maintenance packet 3</constant></para>
594  <para>NOTE: Now you must kill gdb. Typically you press control-z and
595  issue the command: kill -9 %</para></listitem>
596  </orderedlist>
597  </para>
598  </sect2>
599  <sect2>
600  <title>Change from kdb to kgdb</title>
601  <para>There are two ways you can change from kdb to kgdb.  You can
602  manually enter kgdb mode by issuing the kgdb command from the kdb
603  shell prompt, or you can connect gdb while the kdb shell prompt is
604  active.  The kdb shell looks for the typical first commands that gdb
605  would issue with the gdb remote protocol and if it sees one of those
606  commands it automatically changes into kgdb mode.</para>
607  <orderedlist>
608  <listitem><para>From kdb issue the command:</para>
609  <para><constant>kgdb</constant></para>
610  <para>Now disconnect your terminal program and connect gdb in its place</para></listitem>
611  <listitem><para>At the kdb prompt, disconnect the terminal program and connect gdb in its place.</para></listitem>
612  </orderedlist>
613  </sect2>
614  </sect1>
615  <sect1>
616  <title>Running kdb commands from gdb</title>
617  <para>It is possible to run a limited set of kdb commands from gdb,
618  using the gdb monitor command.  You don't want to execute any of the
619  run control or breakpoint operations, because it can disrupt the
620  state of the kernel debugger.  You should be using gdb for
621  breakpoints and run control operations if you have gdb connected.
622  The more useful commands to run are things like lsmod, dmesg, ps or
623  possibly some of the memory information commands.  To see all the kdb
624  commands you can run <constant>monitor help</constant>.</para>
625  <para>Example:
626  <informalexample><programlisting>
627(gdb) monitor ps
6281 idle process (state I) and
62927 sleeping system daemon (state M) processes suppressed,
630use 'ps A' to see all.
631Task Addr       Pid   Parent [*] cpu State Thread     Command
632
6330xc78291d0        1        0  0    0   S  0xc7829404  init
6340xc7954150      942        1  0    0   S  0xc7954384  dropbear
6350xc78789c0      944        1  0    0   S  0xc7878bf4  sh
636(gdb)
637  </programlisting></informalexample>
638  </para>
639  </sect1>
640  </chapter>
641  <chapter id="KGDBTestSuite">
642    <title>kgdb Test Suite</title>
643    <para>
644    When kgdb is enabled in the kernel config you can also elect to
645    enable the config parameter KGDB_TESTS.  Turning this on will
646    enable a special kgdb I/O module which is designed to test the
647    kgdb internal functions.
648    </para>
649    <para>
650    The kgdb tests are mainly intended for developers to test the kgdb
651    internals as well as a tool for developing a new kgdb architecture
652    specific implementation.  These tests are not really for end users
653    of the Linux kernel.  The primary source of documentation would be
654    to look in the drivers/misc/kgdbts.c file.
655    </para>
656    <para>
657    The kgdb test suite can also be configured at compile time to run
658    the core set of tests by setting the kernel config parameter
659    KGDB_TESTS_ON_BOOT.  This particular option is aimed at automated
660    regression testing and does not require modifying the kernel boot
661    config arguments.  If this is turned on, the kgdb test suite can
662    be disabled by specifying "kgdbts=" as a kernel boot argument.
663    </para>
664  </chapter>
665  <chapter id="CommonBackEndReq">
666  <title>Kernel Debugger Internals</title>
667  <sect1 id="kgdbArchitecture">
668    <title>Architecture Specifics</title>
669      <para>
670      The kernel debugger is organized into a number of components:
671      <orderedlist>
672      <listitem><para>The debug core</para>
673      <para>
674      The debug core is found in kernel/debugger/debug_core.c.  It contains:
675      <itemizedlist>
676      <listitem><para>A generic OS exception handler which includes
677      sync'ing the processors into a stopped state on an multi-CPU
678      system.</para></listitem>
679      <listitem><para>The API to talk to the kgdb I/O drivers</para></listitem>
680      <listitem><para>The API to make calls to the arch-specific kgdb implementation</para></listitem>
681      <listitem><para>The logic to perform safe memory reads and writes to memory while using the debugger</para></listitem>
682      <listitem><para>A full implementation for software breakpoints unless overridden by the arch</para></listitem>
683      <listitem><para>The API to invoke either the kdb or kgdb frontend to the debug core.</para></listitem>
684      <listitem><para>The structures and callback API for atomic kernel mode setting.</para>
685      <para>NOTE: kgdboc is where the kms callbacks are invoked.</para></listitem>
686      </itemizedlist>
687      </para>
688      </listitem>
689      <listitem><para>kgdb arch-specific implementation</para>
690      <para>
691      This implementation is generally found in arch/*/kernel/kgdb.c.
692      As an example, arch/x86/kernel/kgdb.c contains the specifics to
693      implement HW breakpoint as well as the initialization to
694      dynamically register and unregister for the trap handlers on
695      this architecture.  The arch-specific portion implements:
696      <itemizedlist>
697      <listitem><para>contains an arch-specific trap catcher which
698      invokes kgdb_handle_exception() to start kgdb about doing its
699      work</para></listitem>
700      <listitem><para>translation to and from gdb specific packet format to pt_regs</para></listitem>
701      <listitem><para>Registration and unregistration of architecture specific trap hooks</para></listitem>
702      <listitem><para>Any special exception handling and cleanup</para></listitem>
703      <listitem><para>NMI exception handling and cleanup</para></listitem>
704      <listitem><para>(optional) HW breakpoints</para></listitem>
705      </itemizedlist>
706      </para>
707      </listitem>
708      <listitem><para>gdbstub frontend (aka kgdb)</para>
709      <para>The gdbstub is located in kernel/debug/gdbstub.c. It contains:</para>
710      <itemizedlist>
711        <listitem><para>All the logic to implement the gdb serial protocol</para></listitem>
712      </itemizedlist>
713      </listitem>
714      <listitem><para>kdb frontend</para>
715      <para>The kdb debugger shell is broken down into a number of
716      components.  The kdb core is located in kernel/debug/kdb.  There
717      are a number of helper functions in some of the other kernel
718      components to make it possible for kdb to examine and report
719      information about the kernel without taking locks that could
720      cause a kernel deadlock.  The kdb core contains implements the following functionality.</para>
721      <itemizedlist>
722        <listitem><para>A simple shell</para></listitem>
723        <listitem><para>The kdb core command set</para></listitem>
724        <listitem><para>A registration API to register additional kdb shell commands.</para>
725	<itemizedlist>
726        <listitem><para>A good example of a self-contained kdb module
727        is the "ftdump" command for dumping the ftrace buffer.  See:
728        kernel/trace/trace_kdb.c</para></listitem>
729        <listitem><para>For an example of how to dynamically register
730        a new kdb command you can build the kdb_hello.ko kernel module
731        from samples/kdb/kdb_hello.c.  To build this example you can
732        set CONFIG_SAMPLES=y and CONFIG_SAMPLE_KDB=m in your kernel
733        config.  Later run "modprobe kdb_hello" and the next time you
734        enter the kdb shell, you can run the "hello"
735        command.</para></listitem>
736	</itemizedlist></listitem>
737        <listitem><para>The implementation for kdb_printf() which
738        emits messages directly to I/O drivers, bypassing the kernel
739        log.</para></listitem>
740        <listitem><para>SW / HW breakpoint management for the kdb shell</para></listitem>
741      </itemizedlist>
742      </listitem>
743      <listitem><para>kgdb I/O driver</para>
744      <para>
745      Each kgdb I/O driver has to provide an implementation for the following:
746      <itemizedlist>
747      <listitem><para>configuration via built-in or module</para></listitem>
748      <listitem><para>dynamic configuration and kgdb hook registration calls</para></listitem>
749      <listitem><para>read and write character interface</para></listitem>
750      <listitem><para>A cleanup handler for unconfiguring from the kgdb core</para></listitem>
751      <listitem><para>(optional) Early debug methodology</para></listitem>
752      </itemizedlist>
753      Any given kgdb I/O driver has to operate very closely with the
754      hardware and must do it in such a way that does not enable
755      interrupts or change other parts of the system context without
756      completely restoring them. The kgdb core will repeatedly "poll"
757      a kgdb I/O driver for characters when it needs input.  The I/O
758      driver is expected to return immediately if there is no data
759      available.  Doing so allows for the future possibility to touch
760      watchdog hardware in such a way as to have a target system not
761      reset when these are enabled.
762      </para>
763      </listitem>
764      </orderedlist>
765      </para>
766      <para>
767      If you are intent on adding kgdb architecture specific support
768      for a new architecture, the architecture should define
769      <constant>HAVE_ARCH_KGDB</constant> in the architecture specific
770      Kconfig file.  This will enable kgdb for the architecture, and
771      at that point you must create an architecture specific kgdb
772      implementation.
773      </para>
774      <para>
775      There are a few flags which must be set on every architecture in
776      their &lt;asm/kgdb.h&gt; file.  These are:
777      <itemizedlist>
778        <listitem>
779          <para>
780          NUMREGBYTES: The size in bytes of all of the registers, so
781          that we can ensure they will all fit into a packet.
782          </para>
783        </listitem>
784        <listitem>
785          <para>
786          BUFMAX: The size in bytes of the buffer GDB will read into.
787          This must be larger than NUMREGBYTES.
788          </para>
789        </listitem>
790        <listitem>
791          <para>
792          CACHE_FLUSH_IS_SAFE: Set to 1 if it is always safe to call
793          flush_cache_range or flush_icache_range.  On some architectures,
794          these functions may not be safe to call on SMP since we keep other
795          CPUs in a holding pattern.
796          </para>
797        </listitem>
798      </itemizedlist>
799      </para>
800      <para>
801      There are also the following functions for the common backend,
802      found in kernel/kgdb.c, that must be supplied by the
803      architecture-specific backend unless marked as (optional), in
804      which case a default function maybe used if the architecture
805      does not need to provide a specific implementation.
806      </para>
807<!-- include/linux/kgdb.h -->
808<refentry id="API-kgdb-skipexception">
809<refentryinfo>
810 <title>LINUX</title>
811 <productname>Kernel Hackers Manual</productname>
812 <date>July 2017</date>
813</refentryinfo>
814<refmeta>
815 <refentrytitle><phrase>kgdb_skipexception</phrase></refentrytitle>
816 <manvolnum>9</manvolnum>
817 <refmiscinfo class="version">4.1.27</refmiscinfo>
818</refmeta>
819<refnamediv>
820 <refname>kgdb_skipexception</refname>
821 <refpurpose>
822  (optional) exit kgdb_handle_exception early
823 </refpurpose>
824</refnamediv>
825<refsynopsisdiv>
826 <title>Synopsis</title>
827  <funcsynopsis><funcprototype>
828   <funcdef>int <function>kgdb_skipexception </function></funcdef>
829   <paramdef>int <parameter>exception</parameter></paramdef>
830   <paramdef>struct pt_regs * <parameter>regs</parameter></paramdef>
831  </funcprototype></funcsynopsis>
832</refsynopsisdiv>
833<refsect1>
834 <title>Arguments</title>
835 <variablelist>
836  <varlistentry>
837   <term><parameter>exception</parameter></term>
838   <listitem>
839    <para>
840     Exception vector number
841    </para>
842   </listitem>
843  </varlistentry>
844  <varlistentry>
845   <term><parameter>regs</parameter></term>
846   <listitem>
847    <para>
848     Current <structname>struct pt_regs</structname>.
849    </para>
850   </listitem>
851  </varlistentry>
852 </variablelist>
853</refsect1>
854<refsect1>
855<title>Description</title>
856<para>
857   On some architectures it is required to skip a breakpoint
858   exception when it occurs after a breakpoint has been removed.
859   This can be implemented in the architecture specific portion of kgdb.
860</para>
861</refsect1>
862</refentry>
863
864<refentry id="API-kgdb-breakpoint">
865<refentryinfo>
866 <title>LINUX</title>
867 <productname>Kernel Hackers Manual</productname>
868 <date>July 2017</date>
869</refentryinfo>
870<refmeta>
871 <refentrytitle><phrase>kgdb_breakpoint</phrase></refentrytitle>
872 <manvolnum>9</manvolnum>
873 <refmiscinfo class="version">4.1.27</refmiscinfo>
874</refmeta>
875<refnamediv>
876 <refname>kgdb_breakpoint</refname>
877 <refpurpose>
878     compiled in breakpoint
879 </refpurpose>
880</refnamediv>
881<refsynopsisdiv>
882 <title>Synopsis</title>
883  <funcsynopsis><funcprototype>
884   <funcdef>void <function>kgdb_breakpoint </function></funcdef>
885   <paramdef> <parameter>void</parameter></paramdef>
886  </funcprototype></funcsynopsis>
887</refsynopsisdiv>
888<refsect1>
889 <title>Arguments</title>
890 <variablelist>
891  <varlistentry>
892   <term><parameter>void</parameter></term>
893   <listitem>
894    <para>
895     no arguments
896    </para>
897   </listitem>
898  </varlistentry>
899 </variablelist>
900</refsect1>
901<refsect1>
902<title>Description</title>
903<para>
904   </para><para>
905
906   This will be implemented as a static inline per architecture.  This
907   function is called by the kgdb core to execute an architecture
908   specific trap to cause kgdb to enter the exception processing.
909</para>
910</refsect1>
911</refentry>
912
913<refentry id="API-kgdb-arch-init">
914<refentryinfo>
915 <title>LINUX</title>
916 <productname>Kernel Hackers Manual</productname>
917 <date>July 2017</date>
918</refentryinfo>
919<refmeta>
920 <refentrytitle><phrase>kgdb_arch_init</phrase></refentrytitle>
921 <manvolnum>9</manvolnum>
922 <refmiscinfo class="version">4.1.27</refmiscinfo>
923</refmeta>
924<refnamediv>
925 <refname>kgdb_arch_init</refname>
926 <refpurpose>
927     Perform any architecture specific initalization.
928 </refpurpose>
929</refnamediv>
930<refsynopsisdiv>
931 <title>Synopsis</title>
932  <funcsynopsis><funcprototype>
933   <funcdef>int <function>kgdb_arch_init </function></funcdef>
934   <paramdef> <parameter>void</parameter></paramdef>
935  </funcprototype></funcsynopsis>
936</refsynopsisdiv>
937<refsect1>
938 <title>Arguments</title>
939 <variablelist>
940  <varlistentry>
941   <term><parameter>void</parameter></term>
942   <listitem>
943    <para>
944     no arguments
945    </para>
946   </listitem>
947  </varlistentry>
948 </variablelist>
949</refsect1>
950<refsect1>
951<title>Description</title>
952<para>
953   </para><para>
954
955   This function will handle the initalization of any architecture
956   specific callbacks.
957</para>
958</refsect1>
959</refentry>
960
961<refentry id="API-kgdb-arch-exit">
962<refentryinfo>
963 <title>LINUX</title>
964 <productname>Kernel Hackers Manual</productname>
965 <date>July 2017</date>
966</refentryinfo>
967<refmeta>
968 <refentrytitle><phrase>kgdb_arch_exit</phrase></refentrytitle>
969 <manvolnum>9</manvolnum>
970 <refmiscinfo class="version">4.1.27</refmiscinfo>
971</refmeta>
972<refnamediv>
973 <refname>kgdb_arch_exit</refname>
974 <refpurpose>
975     Perform any architecture specific uninitalization.
976 </refpurpose>
977</refnamediv>
978<refsynopsisdiv>
979 <title>Synopsis</title>
980  <funcsynopsis><funcprototype>
981   <funcdef>void <function>kgdb_arch_exit </function></funcdef>
982   <paramdef> <parameter>void</parameter></paramdef>
983  </funcprototype></funcsynopsis>
984</refsynopsisdiv>
985<refsect1>
986 <title>Arguments</title>
987 <variablelist>
988  <varlistentry>
989   <term><parameter>void</parameter></term>
990   <listitem>
991    <para>
992     no arguments
993    </para>
994   </listitem>
995  </varlistentry>
996 </variablelist>
997</refsect1>
998<refsect1>
999<title>Description</title>
1000<para>
1001   </para><para>
1002
1003   This function will handle the uninitalization of any architecture
1004   specific callbacks, for dynamic registration and unregistration.
1005</para>
1006</refsect1>
1007</refentry>
1008
1009<refentry id="API-pt-regs-to-gdb-regs">
1010<refentryinfo>
1011 <title>LINUX</title>
1012 <productname>Kernel Hackers Manual</productname>
1013 <date>July 2017</date>
1014</refentryinfo>
1015<refmeta>
1016 <refentrytitle><phrase>pt_regs_to_gdb_regs</phrase></refentrytitle>
1017 <manvolnum>9</manvolnum>
1018 <refmiscinfo class="version">4.1.27</refmiscinfo>
1019</refmeta>
1020<refnamediv>
1021 <refname>pt_regs_to_gdb_regs</refname>
1022 <refpurpose>
1023     Convert ptrace regs to GDB regs
1024 </refpurpose>
1025</refnamediv>
1026<refsynopsisdiv>
1027 <title>Synopsis</title>
1028  <funcsynopsis><funcprototype>
1029   <funcdef>void <function>pt_regs_to_gdb_regs </function></funcdef>
1030   <paramdef>unsigned long * <parameter>gdb_regs</parameter></paramdef>
1031   <paramdef>struct pt_regs * <parameter>regs</parameter></paramdef>
1032  </funcprototype></funcsynopsis>
1033</refsynopsisdiv>
1034<refsect1>
1035 <title>Arguments</title>
1036 <variablelist>
1037  <varlistentry>
1038   <term><parameter>gdb_regs</parameter></term>
1039   <listitem>
1040    <para>
1041     A pointer to hold the registers in the order GDB wants.
1042    </para>
1043   </listitem>
1044  </varlistentry>
1045  <varlistentry>
1046   <term><parameter>regs</parameter></term>
1047   <listitem>
1048    <para>
1049     The <structname>struct pt_regs</structname> of the current process.
1050    </para>
1051   </listitem>
1052  </varlistentry>
1053 </variablelist>
1054</refsect1>
1055<refsect1>
1056<title>Description</title>
1057<para>
1058   Convert the pt_regs in <parameter>regs</parameter> into the format for registers that
1059   GDB expects, stored in <parameter>gdb_regs</parameter>.
1060</para>
1061</refsect1>
1062</refentry>
1063
1064<refentry id="API-sleeping-thread-to-gdb-regs">
1065<refentryinfo>
1066 <title>LINUX</title>
1067 <productname>Kernel Hackers Manual</productname>
1068 <date>July 2017</date>
1069</refentryinfo>
1070<refmeta>
1071 <refentrytitle><phrase>sleeping_thread_to_gdb_regs</phrase></refentrytitle>
1072 <manvolnum>9</manvolnum>
1073 <refmiscinfo class="version">4.1.27</refmiscinfo>
1074</refmeta>
1075<refnamediv>
1076 <refname>sleeping_thread_to_gdb_regs</refname>
1077 <refpurpose>
1078     Convert ptrace regs to GDB regs
1079 </refpurpose>
1080</refnamediv>
1081<refsynopsisdiv>
1082 <title>Synopsis</title>
1083  <funcsynopsis><funcprototype>
1084   <funcdef>void <function>sleeping_thread_to_gdb_regs </function></funcdef>
1085   <paramdef>unsigned long * <parameter>gdb_regs</parameter></paramdef>
1086   <paramdef>struct task_struct * <parameter>p</parameter></paramdef>
1087  </funcprototype></funcsynopsis>
1088</refsynopsisdiv>
1089<refsect1>
1090 <title>Arguments</title>
1091 <variablelist>
1092  <varlistentry>
1093   <term><parameter>gdb_regs</parameter></term>
1094   <listitem>
1095    <para>
1096     A pointer to hold the registers in the order GDB wants.
1097    </para>
1098   </listitem>
1099  </varlistentry>
1100  <varlistentry>
1101   <term><parameter>p</parameter></term>
1102   <listitem>
1103    <para>
1104     The <structname>struct task_struct</structname> of the desired process.
1105    </para>
1106   </listitem>
1107  </varlistentry>
1108 </variablelist>
1109</refsect1>
1110<refsect1>
1111<title>Description</title>
1112<para>
1113   Convert the register values of the sleeping process in <parameter>p</parameter> to
1114   the format that GDB expects.
1115   This function is called when kgdb does not have access to the
1116   <structname>struct pt_regs</structname> and therefore it should fill the gdb registers
1117   <parameter>gdb_regs</parameter> with what has	been saved in <structname>struct thread_struct</structname>
1118   thread field during switch_to.
1119</para>
1120</refsect1>
1121</refentry>
1122
1123<refentry id="API-gdb-regs-to-pt-regs">
1124<refentryinfo>
1125 <title>LINUX</title>
1126 <productname>Kernel Hackers Manual</productname>
1127 <date>July 2017</date>
1128</refentryinfo>
1129<refmeta>
1130 <refentrytitle><phrase>gdb_regs_to_pt_regs</phrase></refentrytitle>
1131 <manvolnum>9</manvolnum>
1132 <refmiscinfo class="version">4.1.27</refmiscinfo>
1133</refmeta>
1134<refnamediv>
1135 <refname>gdb_regs_to_pt_regs</refname>
1136 <refpurpose>
1137     Convert GDB regs to ptrace regs.
1138 </refpurpose>
1139</refnamediv>
1140<refsynopsisdiv>
1141 <title>Synopsis</title>
1142  <funcsynopsis><funcprototype>
1143   <funcdef>void <function>gdb_regs_to_pt_regs </function></funcdef>
1144   <paramdef>unsigned long * <parameter>gdb_regs</parameter></paramdef>
1145   <paramdef>struct pt_regs * <parameter>regs</parameter></paramdef>
1146  </funcprototype></funcsynopsis>
1147</refsynopsisdiv>
1148<refsect1>
1149 <title>Arguments</title>
1150 <variablelist>
1151  <varlistentry>
1152   <term><parameter>gdb_regs</parameter></term>
1153   <listitem>
1154    <para>
1155     A pointer to hold the registers we've received from GDB.
1156    </para>
1157   </listitem>
1158  </varlistentry>
1159  <varlistentry>
1160   <term><parameter>regs</parameter></term>
1161   <listitem>
1162    <para>
1163     A pointer to a <structname>struct pt_regs</structname> to hold these values in.
1164    </para>
1165   </listitem>
1166  </varlistentry>
1167 </variablelist>
1168</refsect1>
1169<refsect1>
1170<title>Description</title>
1171<para>
1172   Convert the GDB regs in <parameter>gdb_regs</parameter> into the pt_regs, and store them
1173   in <parameter>regs</parameter>.
1174</para>
1175</refsect1>
1176</refentry>
1177
1178<refentry id="API-kgdb-arch-handle-exception">
1179<refentryinfo>
1180 <title>LINUX</title>
1181 <productname>Kernel Hackers Manual</productname>
1182 <date>July 2017</date>
1183</refentryinfo>
1184<refmeta>
1185 <refentrytitle><phrase>kgdb_arch_handle_exception</phrase></refentrytitle>
1186 <manvolnum>9</manvolnum>
1187 <refmiscinfo class="version">4.1.27</refmiscinfo>
1188</refmeta>
1189<refnamediv>
1190 <refname>kgdb_arch_handle_exception</refname>
1191 <refpurpose>
1192     Handle architecture specific GDB packets.
1193 </refpurpose>
1194</refnamediv>
1195<refsynopsisdiv>
1196 <title>Synopsis</title>
1197  <funcsynopsis><funcprototype>
1198   <funcdef>int <function>kgdb_arch_handle_exception </function></funcdef>
1199   <paramdef>int <parameter>vector</parameter></paramdef>
1200   <paramdef>int <parameter>signo</parameter></paramdef>
1201   <paramdef>int <parameter>err_code</parameter></paramdef>
1202   <paramdef>char * <parameter>remcom_in_buffer</parameter></paramdef>
1203   <paramdef>char * <parameter>remcom_out_buffer</parameter></paramdef>
1204   <paramdef>struct pt_regs * <parameter>regs</parameter></paramdef>
1205  </funcprototype></funcsynopsis>
1206</refsynopsisdiv>
1207<refsect1>
1208 <title>Arguments</title>
1209 <variablelist>
1210  <varlistentry>
1211   <term><parameter>vector</parameter></term>
1212   <listitem>
1213    <para>
1214     The error vector of the exception that happened.
1215    </para>
1216   </listitem>
1217  </varlistentry>
1218  <varlistentry>
1219   <term><parameter>signo</parameter></term>
1220   <listitem>
1221    <para>
1222     The signal number of the exception that happened.
1223    </para>
1224   </listitem>
1225  </varlistentry>
1226  <varlistentry>
1227   <term><parameter>err_code</parameter></term>
1228   <listitem>
1229    <para>
1230     The error code of the exception that happened.
1231    </para>
1232   </listitem>
1233  </varlistentry>
1234  <varlistentry>
1235   <term><parameter>remcom_in_buffer</parameter></term>
1236   <listitem>
1237    <para>
1238     The buffer of the packet we have read.
1239    </para>
1240   </listitem>
1241  </varlistentry>
1242  <varlistentry>
1243   <term><parameter>remcom_out_buffer</parameter></term>
1244   <listitem>
1245    <para>
1246     The buffer of <constant>BUFMAX</constant> bytes to write a packet into.
1247    </para>
1248   </listitem>
1249  </varlistentry>
1250  <varlistentry>
1251   <term><parameter>regs</parameter></term>
1252   <listitem>
1253    <para>
1254     The <structname>struct pt_regs</structname> of the current process.
1255    </para>
1256   </listitem>
1257  </varlistentry>
1258 </variablelist>
1259</refsect1>
1260<refsect1>
1261<title>Description</title>
1262<para>
1263   This function MUST handle the 'c' and 's' command packets,
1264   as well packets to set / remove a hardware breakpoint, if used.
1265   If there are additional packets which the hardware needs to handle,
1266   they are handled here.  The code should return -1 if it wants to
1267   process more packets, and a <constant>0</constant> or <constant>1</constant> if it wants to exit from the
1268   kgdb callback.
1269</para>
1270</refsect1>
1271</refentry>
1272
1273<refentry id="API-kgdb-roundup-cpus">
1274<refentryinfo>
1275 <title>LINUX</title>
1276 <productname>Kernel Hackers Manual</productname>
1277 <date>July 2017</date>
1278</refentryinfo>
1279<refmeta>
1280 <refentrytitle><phrase>kgdb_roundup_cpus</phrase></refentrytitle>
1281 <manvolnum>9</manvolnum>
1282 <refmiscinfo class="version">4.1.27</refmiscinfo>
1283</refmeta>
1284<refnamediv>
1285 <refname>kgdb_roundup_cpus</refname>
1286 <refpurpose>
1287     Get other CPUs into a holding pattern
1288 </refpurpose>
1289</refnamediv>
1290<refsynopsisdiv>
1291 <title>Synopsis</title>
1292  <funcsynopsis><funcprototype>
1293   <funcdef>void <function>kgdb_roundup_cpus </function></funcdef>
1294   <paramdef>unsigned long <parameter>flags</parameter></paramdef>
1295  </funcprototype></funcsynopsis>
1296</refsynopsisdiv>
1297<refsect1>
1298 <title>Arguments</title>
1299 <variablelist>
1300  <varlistentry>
1301   <term><parameter>flags</parameter></term>
1302   <listitem>
1303    <para>
1304     Current IRQ state
1305    </para>
1306   </listitem>
1307  </varlistentry>
1308 </variablelist>
1309</refsect1>
1310<refsect1>
1311<title>Description</title>
1312<para>
1313   On SMP systems, we need to get the attention of the other CPUs
1314   and get them into a known state.  This should do what is needed
1315   to get the other CPUs to call <function>kgdb_wait</function>. Note that on some arches,
1316   the NMI approach is not used for rounding up all the CPUs. For example,
1317   in case of MIPS, <function>smp_call_function</function> is used to roundup CPUs. In
1318   this case, we have to make sure that interrupts are enabled before
1319   calling <function>smp_call_function</function>. The argument to this function is
1320   the flags that will be used when restoring the interrupts. There is
1321   <function>local_irq_save</function> call before <function>kgdb_roundup_cpus</function>.
1322   </para><para>
1323
1324   On non-SMP systems, this is not called.
1325</para>
1326</refsect1>
1327</refentry>
1328
1329<refentry id="API-kgdb-arch-set-pc">
1330<refentryinfo>
1331 <title>LINUX</title>
1332 <productname>Kernel Hackers Manual</productname>
1333 <date>July 2017</date>
1334</refentryinfo>
1335<refmeta>
1336 <refentrytitle><phrase>kgdb_arch_set_pc</phrase></refentrytitle>
1337 <manvolnum>9</manvolnum>
1338 <refmiscinfo class="version">4.1.27</refmiscinfo>
1339</refmeta>
1340<refnamediv>
1341 <refname>kgdb_arch_set_pc</refname>
1342 <refpurpose>
1343     Generic call back to the program counter
1344 </refpurpose>
1345</refnamediv>
1346<refsynopsisdiv>
1347 <title>Synopsis</title>
1348  <funcsynopsis><funcprototype>
1349   <funcdef>void <function>kgdb_arch_set_pc </function></funcdef>
1350   <paramdef>struct pt_regs * <parameter>regs</parameter></paramdef>
1351   <paramdef>unsigned long <parameter>pc</parameter></paramdef>
1352  </funcprototype></funcsynopsis>
1353</refsynopsisdiv>
1354<refsect1>
1355 <title>Arguments</title>
1356 <variablelist>
1357  <varlistentry>
1358   <term><parameter>regs</parameter></term>
1359   <listitem>
1360    <para>
1361     Current <structname>struct pt_regs</structname>.
1362    </para>
1363   </listitem>
1364  </varlistentry>
1365  <varlistentry>
1366   <term><parameter>pc</parameter></term>
1367   <listitem>
1368    <para>
1369     The new value for the program counter
1370    </para>
1371   </listitem>
1372  </varlistentry>
1373 </variablelist>
1374</refsect1>
1375<refsect1>
1376<title>Description</title>
1377<para>
1378   This function handles updating the program counter and requires an
1379   architecture specific implementation.
1380</para>
1381</refsect1>
1382</refentry>
1383
1384<refentry id="API-kgdb-arch-late">
1385<refentryinfo>
1386 <title>LINUX</title>
1387 <productname>Kernel Hackers Manual</productname>
1388 <date>July 2017</date>
1389</refentryinfo>
1390<refmeta>
1391 <refentrytitle><phrase>kgdb_arch_late</phrase></refentrytitle>
1392 <manvolnum>9</manvolnum>
1393 <refmiscinfo class="version">4.1.27</refmiscinfo>
1394</refmeta>
1395<refnamediv>
1396 <refname>kgdb_arch_late</refname>
1397 <refpurpose>
1398     Perform any architecture specific initalization.
1399 </refpurpose>
1400</refnamediv>
1401<refsynopsisdiv>
1402 <title>Synopsis</title>
1403  <funcsynopsis><funcprototype>
1404   <funcdef>void <function>kgdb_arch_late </function></funcdef>
1405   <paramdef> <parameter>void</parameter></paramdef>
1406  </funcprototype></funcsynopsis>
1407</refsynopsisdiv>
1408<refsect1>
1409 <title>Arguments</title>
1410 <variablelist>
1411  <varlistentry>
1412   <term><parameter>void</parameter></term>
1413   <listitem>
1414    <para>
1415     no arguments
1416    </para>
1417   </listitem>
1418  </varlistentry>
1419 </variablelist>
1420</refsect1>
1421<refsect1>
1422<title>Description</title>
1423<para>
1424   </para><para>
1425
1426   This function will handle the late initalization of any
1427   architecture specific callbacks.  This is an optional function for
1428   handling things like late initialization of hw breakpoints.  The
1429   default implementation does nothing.
1430</para>
1431</refsect1>
1432</refentry>
1433
1434<refentry id="API-struct-kgdb-arch">
1435<refentryinfo>
1436 <title>LINUX</title>
1437 <productname>Kernel Hackers Manual</productname>
1438 <date>July 2017</date>
1439</refentryinfo>
1440<refmeta>
1441 <refentrytitle><phrase>struct kgdb_arch</phrase></refentrytitle>
1442 <manvolnum>9</manvolnum>
1443 <refmiscinfo class="version">4.1.27</refmiscinfo>
1444</refmeta>
1445<refnamediv>
1446 <refname>struct kgdb_arch</refname>
1447 <refpurpose>
1448     Describe architecture specific values.
1449 </refpurpose>
1450</refnamediv>
1451<refsynopsisdiv>
1452 <title>Synopsis</title>
1453  <programlisting>
1454struct kgdb_arch {
1455  unsigned char gdb_bpt_instr[BREAK_INSTR_SIZE];
1456  unsigned long flags;
1457  int (* set_breakpoint) (unsigned long, char *);
1458  int (* remove_breakpoint) (unsigned long, char *);
1459  int (* set_hw_breakpoint) (unsigned long, int, enum kgdb_bptype);
1460  int (* remove_hw_breakpoint) (unsigned long, int, enum kgdb_bptype);
1461  void (* disable_hw_break) (struct pt_regs *regs);
1462  void (* remove_all_hw_break) (void);
1463  void (* correct_hw_break) (void);
1464  void (* enable_nmi) (bool on);
1465};  </programlisting>
1466</refsynopsisdiv>
1467 <refsect1>
1468  <title>Members</title>
1469  <variablelist>
1470    <varlistentry>      <term>gdb_bpt_instr[BREAK_INSTR_SIZE]</term>
1471      <listitem><para>
1472   The instruction to trigger a breakpoint.
1473      </para></listitem>
1474    </varlistentry>
1475    <varlistentry>      <term>flags</term>
1476      <listitem><para>
1477   Flags for the breakpoint, currently just <constant>KGDB_HW_BREAKPOINT</constant>.
1478      </para></listitem>
1479    </varlistentry>
1480    <varlistentry>      <term>set_breakpoint</term>
1481      <listitem><para>
1482   Allow an architecture to specify how to set a software
1483   breakpoint.
1484      </para></listitem>
1485    </varlistentry>
1486    <varlistentry>      <term>remove_breakpoint</term>
1487      <listitem><para>
1488   Allow an architecture to specify how to remove a
1489   software breakpoint.
1490      </para></listitem>
1491    </varlistentry>
1492    <varlistentry>      <term>set_hw_breakpoint</term>
1493      <listitem><para>
1494   Allow an architecture to specify how to set a hardware
1495   breakpoint.
1496      </para></listitem>
1497    </varlistentry>
1498    <varlistentry>      <term>remove_hw_breakpoint</term>
1499      <listitem><para>
1500   Allow an architecture to specify how to remove a
1501   hardware breakpoint.
1502      </para></listitem>
1503    </varlistentry>
1504    <varlistentry>      <term>disable_hw_break</term>
1505      <listitem><para>
1506   Allow an architecture to specify how to disable
1507   hardware breakpoints for a single cpu.
1508      </para></listitem>
1509    </varlistentry>
1510    <varlistentry>      <term>remove_all_hw_break</term>
1511      <listitem><para>
1512   Allow an architecture to specify how to remove all
1513   hardware breakpoints.
1514      </para></listitem>
1515    </varlistentry>
1516    <varlistentry>      <term>correct_hw_break</term>
1517      <listitem><para>
1518   Allow an architecture to specify how to correct the
1519   hardware debug registers.
1520      </para></listitem>
1521    </varlistentry>
1522    <varlistentry>      <term>enable_nmi</term>
1523      <listitem><para>
1524   Manage NMI-triggered entry to KGDB
1525      </para></listitem>
1526    </varlistentry>
1527  </variablelist>
1528 </refsect1>
1529</refentry>
1530
1531<refentry id="API-struct-kgdb-io">
1532<refentryinfo>
1533 <title>LINUX</title>
1534 <productname>Kernel Hackers Manual</productname>
1535 <date>July 2017</date>
1536</refentryinfo>
1537<refmeta>
1538 <refentrytitle><phrase>struct kgdb_io</phrase></refentrytitle>
1539 <manvolnum>9</manvolnum>
1540 <refmiscinfo class="version">4.1.27</refmiscinfo>
1541</refmeta>
1542<refnamediv>
1543 <refname>struct kgdb_io</refname>
1544 <refpurpose>
1545     Describe the interface for an I/O driver to talk with KGDB.
1546 </refpurpose>
1547</refnamediv>
1548<refsynopsisdiv>
1549 <title>Synopsis</title>
1550  <programlisting>
1551struct kgdb_io {
1552  const char * name;
1553  int (* read_char) (void);
1554  void (* write_char) (u8);
1555  void (* flush) (void);
1556  int (* init) (void);
1557  void (* pre_exception) (void);
1558  void (* post_exception) (void);
1559  int is_console;
1560};  </programlisting>
1561</refsynopsisdiv>
1562 <refsect1>
1563  <title>Members</title>
1564  <variablelist>
1565    <varlistentry>      <term>name</term>
1566      <listitem><para>
1567   Name of the I/O driver.
1568      </para></listitem>
1569    </varlistentry>
1570    <varlistentry>      <term>read_char</term>
1571      <listitem><para>
1572   Pointer to a function that will return one char.
1573      </para></listitem>
1574    </varlistentry>
1575    <varlistentry>      <term>write_char</term>
1576      <listitem><para>
1577   Pointer to a function that will write one char.
1578      </para></listitem>
1579    </varlistentry>
1580    <varlistentry>      <term>flush</term>
1581      <listitem><para>
1582   Pointer to a function that will flush any pending writes.
1583      </para></listitem>
1584    </varlistentry>
1585    <varlistentry>      <term>init</term>
1586      <listitem><para>
1587   Pointer to a function that will initialize the device.
1588      </para></listitem>
1589    </varlistentry>
1590    <varlistentry>      <term>pre_exception</term>
1591      <listitem><para>
1592   Pointer to a function that will do any prep work for
1593   the I/O driver.
1594      </para></listitem>
1595    </varlistentry>
1596    <varlistentry>      <term>post_exception</term>
1597      <listitem><para>
1598   Pointer to a function that will do any cleanup work
1599   for the I/O driver.
1600      </para></listitem>
1601    </varlistentry>
1602    <varlistentry>      <term>is_console</term>
1603      <listitem><para>
1604   1 if the end device is a console 0 if the I/O device is
1605   not a console
1606      </para></listitem>
1607    </varlistentry>
1608  </variablelist>
1609 </refsect1>
1610</refentry>
1611
1612  </sect1>
1613  <sect1 id="kgdbocDesign">
1614  <title>kgdboc internals</title>
1615  <sect2>
1616  <title>kgdboc and uarts</title>
1617  <para>
1618  The kgdboc driver is actually a very thin driver that relies on the
1619  underlying low level to the hardware driver having "polling hooks"
1620  to which the tty driver is attached.  In the initial
1621  implementation of kgdboc the serial_core was changed to expose a
1622  low level UART hook for doing polled mode reading and writing of a
1623  single character while in an atomic context.  When kgdb makes an I/O
1624  request to the debugger, kgdboc invokes a callback in the serial
1625  core which in turn uses the callback in the UART driver.</para>
1626  <para>
1627  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>
1628#ifdef CONFIG_CONSOLE_POLL
1629	.poll_get_char = serial8250_get_poll_char,
1630	.poll_put_char = serial8250_put_poll_char,
1631#endif
1632  </programlisting>
1633  Any implementation specifics around creating a polling driver use the
1634  <constant>#ifdef CONFIG_CONSOLE_POLL</constant>, as shown above.
1635  Keep in mind that polling hooks have to be implemented in such a way
1636  that they can be called from an atomic context and have to restore
1637  the state of the UART chip on return such that the system can return
1638  to normal when the debugger detaches.  You need to be very careful
1639  with any kind of lock you consider, because failing here is most likely
1640  going to mean pressing the reset button.
1641  </para>
1642  </sect2>
1643  <sect2 id="kgdbocKbd">
1644  <title>kgdboc and keyboards</title>
1645  <para>The kgdboc driver contains logic to configure communications
1646  with an attached keyboard.  The keyboard infrastructure is only
1647  compiled into the kernel when CONFIG_KDB_KEYBOARD=y is set in the
1648  kernel configuration.</para>
1649  <para>The core polled keyboard driver driver for PS/2 type keyboards
1650  is in drivers/char/kdb_keyboard.c.  This driver is hooked into the
1651  debug core when kgdboc populates the callback in the array
1652  called <constant>kdb_poll_funcs[]</constant>.  The
1653  kdb_get_kbd_char() is the top-level function which polls hardware
1654  for single character input.
1655  </para>
1656  </sect2>
1657  <sect2 id="kgdbocKms">
1658  <title>kgdboc and kms</title>
1659  <para>The kgdboc driver contains logic to request the graphics
1660  display to switch to a text context when you are using
1661  "kgdboc=kms,kbd", provided that you have a video driver which has a
1662  frame buffer console and atomic kernel mode setting support.</para>
1663  <para>
1664  Every time the kernel
1665  debugger is entered it calls kgdboc_pre_exp_handler() which in turn
1666  calls con_debug_enter() in the virtual console layer.  On resuming kernel
1667  execution, the kernel debugger calls kgdboc_post_exp_handler() which
1668  in turn calls con_debug_leave().</para>
1669  <para>Any video driver that wants to be compatible with the kernel
1670  debugger and the atomic kms callbacks must implement the
1671  mode_set_base_atomic, fb_debug_enter and fb_debug_leave operations.
1672  For the fb_debug_enter and fb_debug_leave the option exists to use
1673  the generic drm fb helper functions or implement something custom for
1674  the hardware.  The following example shows the initialization of the
1675  .mode_set_base_atomic operation in
1676  drivers/gpu/drm/i915/intel_display.c:
1677  <informalexample>
1678  <programlisting>
1679static const struct drm_crtc_helper_funcs intel_helper_funcs = {
1680[...]
1681        .mode_set_base_atomic = intel_pipe_set_base_atomic,
1682[...]
1683};
1684  </programlisting>
1685  </informalexample>
1686  </para>
1687  <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
1688  drivers/gpu/drm/i915/intel_fb.c:
1689  <informalexample>
1690  <programlisting>
1691static struct fb_ops intelfb_ops = {
1692[...]
1693       .fb_debug_enter = drm_fb_helper_debug_enter,
1694       .fb_debug_leave = drm_fb_helper_debug_leave,
1695[...]
1696};
1697  </programlisting>
1698  </informalexample>
1699  </para>
1700  </sect2>
1701  </sect1>
1702  </chapter>
1703  <chapter id="credits">
1704     <title>Credits</title>
1705	<para>
1706		The following people have contributed to this document:
1707		<orderedlist>
1708			<listitem><para>Amit Kale<email>amitkale@linsyssoft.com</email></para></listitem>
1709			<listitem><para>Tom Rini<email>trini@kernel.crashing.org</email></para></listitem>
1710		</orderedlist>
1711                In March 2008 this document was completely rewritten by:
1712		<itemizedlist>
1713		<listitem><para>Jason Wessel<email>jason.wessel@windriver.com</email></para></listitem>
1714		</itemizedlist>
1715                In Jan 2010 this document was updated to include kdb.
1716		<itemizedlist>
1717		<listitem><para>Jason Wessel<email>jason.wessel@windriver.com</email></para></listitem>
1718		</itemizedlist>
1719	</para>
1720  </chapter>
1721</book>
1722
1723