blob: f83ba2601e55d51073ce383b3c2e2c0b7ee3cb2d [file] [log] [blame]
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -03001=================================================
2Using kgdb, kdb and the kernel debugger internals
3=================================================
4
5:Author: Jason Wessel
6
7Introduction
8============
9
10The kernel has two different debugger front ends (kdb and kgdb) which
11interface to the debug core. It is possible to use either of the
12debugger front ends and dynamically transition between them if you
13configure the kernel properly at compile and runtime.
14
15Kdb is simplistic shell-style interface which you can use on a system
16console with a keyboard or serial console. You can use it to inspect
17memory, registers, process lists, dmesg, and even set breakpoints to
18stop in a certain location. Kdb is not a source level debugger, although
19you can set breakpoints and execute some basic kernel run control. Kdb
20is mainly aimed at doing some analysis to aid in development or
21diagnosing kernel problems. You can access some symbols by name in
22kernel built-ins or in kernel modules if the code was built with
23``CONFIG_KALLSYMS``.
24
25Kgdb is intended to be used as a source level debugger for the Linux
26kernel. It is used along with gdb to debug a Linux kernel. The
27expectation is that gdb can be used to "break in" to the kernel to
28inspect memory, variables and look through call stack information
29similar to the way an application developer would use gdb to debug an
30application. It is possible to place breakpoints in kernel code and
31perform some limited execution stepping.
32
33Two machines are required for using kgdb. One of these machines is a
34development machine and the other is the target machine. The kernel to
35be debugged runs on the target machine. The development machine runs an
36instance of gdb against the vmlinux file which contains the symbols (not
37a boot image such as bzImage, zImage, uImage...). In gdb the developer
38specifies the connection parameters and connects to kgdb. The type of
39connection a developer makes with gdb depends on the availability of
40kgdb I/O modules compiled as built-ins or loadable kernel modules in the
41test machine's kernel.
42
43Compiling a kernel
44==================
45
46- In order to enable compilation of kdb, you must first enable kgdb.
47
48- The kgdb test compile options are described in the kgdb test suite
49 chapter.
50
51Kernel config options for kgdb
52------------------------------
53
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -030054To enable ``CONFIG_KGDB`` you should look under
55:menuselection:`Kernel hacking --> Kernel debugging` and select
56:menuselection:`KGDB: kernel debugger`.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -030057
58While it is not a hard requirement that you have symbols in your vmlinux
59file, gdb tends not to be very useful without the symbolic data, so you
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -030060will want to turn on ``CONFIG_DEBUG_INFO`` which is called
61:menuselection:`Compile the kernel with debug info` in the config menu.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -030062
63It is advised, but not required, that you turn on the
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -030064``CONFIG_FRAME_POINTER`` kernel option which is called :menuselection:`Compile
65the kernel with frame pointers` in the config menu. This option inserts code
Tiezhu Yangc12af302020-11-16 17:42:47 +080066into the compiled executable which saves the frame information in registers
67or on the stack at different points which allows a debugger such as gdb to
68more accurately construct stack back traces while debugging the kernel.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -030069
70If the architecture that you are using supports the kernel option
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -030071``CONFIG_STRICT_KERNEL_RWX``, you should consider turning it off. This
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -030072option will prevent the use of software breakpoints because it marks
73certain regions of the kernel's memory space as read-only. If kgdb
74supports it for the architecture you are using, you can use hardware
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -030075breakpoints if you desire to run with the ``CONFIG_STRICT_KERNEL_RWX``
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -030076option turned on, else you need to turn off this option.
77
78Next you should choose one of more I/O drivers to interconnect debugging
79host and debugged target. Early boot debugging requires a KGDB I/O
80driver that supports early debugging and the driver must be built into
81the kernel directly. Kgdb I/O driver configuration takes place via
82kernel or module parameters which you can learn more about in the in the
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -030083section that describes the parameter kgdboc.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -030084
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -030085Here is an example set of ``.config`` symbols to enable or disable for kgdb::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -030086
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -030087 # CONFIG_STRICT_KERNEL_RWX is not set
88 CONFIG_FRAME_POINTER=y
89 CONFIG_KGDB=y
90 CONFIG_KGDB_SERIAL_CONSOLE=y
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -030091
92Kernel config options for kdb
93-----------------------------
94
95Kdb is quite a bit more complex than the simple gdbstub sitting on top
96of the kernel's debug core. Kdb must implement a shell, and also adds
97some helper functions in other parts of the kernel, responsible for
98printing out interesting data such as what you would see if you ran
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -030099``lsmod``, or ``ps``. In order to build kdb into the kernel you follow the
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300100same steps as you would for kgdb.
101
102The main config option for kdb is ``CONFIG_KGDB_KDB`` which is called
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300103:menuselection:`KGDB_KDB: include kdb frontend for kgdb` in the config menu.
104In theory you would have already also selected an I/O driver such as the
105``CONFIG_KGDB_SERIAL_CONSOLE`` interface if you plan on using kdb on a
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300106serial port, when you were configuring kgdb.
107
108If you want to use a PS/2-style keyboard with kdb, you would select
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300109``CONFIG_KDB_KEYBOARD`` which is called :menuselection:`KGDB_KDB: keyboard as
110input device` in the config menu. The ``CONFIG_KDB_KEYBOARD`` option is not
111used for anything in the gdb interface to kgdb. The ``CONFIG_KDB_KEYBOARD``
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300112option only works with kdb.
113
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300114Here is an example set of ``.config`` symbols to enable/disable kdb::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300115
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300116 # CONFIG_STRICT_KERNEL_RWX is not set
117 CONFIG_FRAME_POINTER=y
118 CONFIG_KGDB=y
119 CONFIG_KGDB_SERIAL_CONSOLE=y
120 CONFIG_KGDB_KDB=y
121 CONFIG_KDB_KEYBOARD=y
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300122
123Kernel Debugger Boot Arguments
124==============================
125
126This section describes the various runtime kernel parameters that affect
127the configuration of the kernel debugger. The following chapter covers
128using kdb and kgdb as well as providing some examples of the
129configuration parameters.
130
131Kernel parameter: kgdboc
132------------------------
133
134The kgdboc driver was originally an abbreviation meant to stand for
135"kgdb over console". Today it is the primary mechanism to configure how
136to communicate from gdb to kgdb as well as the devices you want to use
137to interact with the kdb shell.
138
139For kgdb/gdb, kgdboc is designed to work with a single serial port. It
140is intended to cover the circumstance where you want to use a serial
141console as your primary console as well as using it to perform kernel
142debugging. It is also possible to use kgdb on a serial port which is not
143designated as a system console. Kgdboc may be configured as a kernel
144built-in or a kernel loadable module. You can only make use of
145``kgdbwait`` and early debugging if you build kgdboc into the kernel as
146a built-in.
147
148Optionally you can elect to activate kms (Kernel Mode Setting)
149integration. When you use kms with kgdboc and you have a video driver
150that has atomic mode setting hooks, it is possible to enter the debugger
151on the graphics console. When the kernel execution is resumed, the
152previous graphics mode will be restored. This integration can serve as a
153useful tool to aid in diagnosing crashes or doing analysis of memory
154with kdb while allowing the full graphics console applications to run.
155
156kgdboc arguments
157~~~~~~~~~~~~~~~~
158
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300159Usage::
160
161 kgdboc=[kms][[,]kbd][[,]serial_device][,baud]
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300162
163The order listed above must be observed if you use any of the optional
164configurations together.
165
166Abbreviations:
167
168- kms = Kernel Mode Setting
169
170- kbd = Keyboard
171
172You can configure kgdboc to use the keyboard, and/or a serial device
173depending on if you are using kdb and/or kgdb, in one of the following
174scenarios. The order listed above must be observed if you use any of the
175optional configurations together. Using kms + only gdb is generally not
176a useful combination.
177
178Using loadable module or built-in
179^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
180
1811. As a kernel built-in:
182
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300183 Use the kernel boot argument::
184
185 kgdboc=<tty-device>,[baud]
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300186
1872. As a kernel loadable module:
188
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300189 Use the command::
190
191 modprobe kgdboc kgdboc=<tty-device>,[baud]
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300192
193 Here are two examples of how you might format the kgdboc string. The
194 first is for an x86 target using the first serial port. The second
195 example is for the ARM Versatile AB using the second serial port.
196
197 1. ``kgdboc=ttyS0,115200``
198
199 2. ``kgdboc=ttyAMA1,115200``
200
201Configure kgdboc at runtime with sysfs
202^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
203
204At run time you can enable or disable kgdboc by echoing a parameters
205into the sysfs. Here are two examples:
206
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03002071. Enable kgdboc on ttyS0::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300208
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300209 echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300210
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03002112. Disable kgdboc::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300212
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300213 echo "" > /sys/module/kgdboc/parameters/kgdboc
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300214
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300215.. note::
216
217 You do not need to specify the baud if you are configuring the
218 console on tty which is already configured or open.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300219
220More examples
221^^^^^^^^^^^^^
222
223You can configure kgdboc to use the keyboard, and/or a serial device
224depending on if you are using kdb and/or kgdb, in one of the following
225scenarios.
226
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03002271. kdb and kgdb over only a serial port::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300228
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300229 kgdboc=<serial_device>[,baud]
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300230
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300231 Example::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300232
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300233 kgdboc=ttyS0,115200
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300234
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03002352. kdb and kgdb with keyboard and a serial port::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300236
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300237 kgdboc=kbd,<serial_device>[,baud]
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300238
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300239 Example::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300240
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300241 kgdboc=kbd,ttyS0,115200
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300242
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03002433. kdb with a keyboard::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300244
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300245 kgdboc=kbd
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300246
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03002474. kdb with kernel mode setting::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300248
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300249 kgdboc=kms,kbd
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300250
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03002515. kdb with kernel mode setting and kgdb over a serial port::
252
253 kgdboc=kms,kbd,ttyS0,115200
254
255.. note::
256
257 Kgdboc does not support interrupting the target via the gdb remote
258 protocol. You must manually send a :kbd:`SysRq-G` unless you have a proxy
259 that splits console output to a terminal program. A console proxy has a
260 separate TCP port for the debugger and a separate TCP port for the
261 "human" console. The proxy can take care of sending the :kbd:`SysRq-G`
262 for you.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300263
264When using kgdboc with no debugger proxy, you can end up connecting the
265debugger at one of two entry points. If an exception occurs after you
266have loaded kgdboc, a message should print on the console stating it is
267waiting for the debugger. In this case you disconnect your terminal
268program and then connect the debugger in its place. If you want to
269interrupt the target system and forcibly enter a debug session you have
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300270to issue a :kbd:`Sysrq` sequence and then type the letter :kbd:`g`. Then you
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300271disconnect the terminal session and connect gdb. Your options if you
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300272don't like this are to hack gdb to send the :kbd:`SysRq-G` for you as well as
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300273on the initial connect, or to use a debugger proxy that allows an
274unmodified gdb to do the debugging.
275
Douglas Andersonf71fc3b2020-05-07 13:08:47 -0700276Kernel parameter: ``kgdboc_earlycon``
277-------------------------------------
278
279If you specify the kernel parameter ``kgdboc_earlycon`` and your serial
280driver registers a boot console that supports polling (doesn't need
281interrupts and implements a nonblocking read() function) kgdb will attempt
282to work using the boot console until it can transition to the regular
283tty driver specified by the ``kgdboc`` parameter.
284
285Normally there is only one boot console (especially that implements the
286read() function) so just adding ``kgdboc_earlycon`` on its own is
287sufficient to make this work. If you have more than one boot console you
288can add the boot console's name to differentiate. Note that names that
289are registered through the boot console layer and the tty layer are not
290the same for the same port.
291
292For instance, on one board to be explicit you might do::
293
294 kgdboc_earlycon=qcom_geni kgdboc=ttyMSM0
295
296If the only boot console on the device was "qcom_geni", you could simplify::
297
298 kgdboc_earlycon kgdboc=ttyMSM0
299
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300300Kernel parameter: ``kgdbwait``
301------------------------------
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300302
303The Kernel command line option ``kgdbwait`` makes kgdb wait for a
304debugger connection during booting of a kernel. You can only use this
305option if you compiled a kgdb I/O driver into the kernel and you
306specified the I/O driver configuration as a kernel command line option.
307The kgdbwait parameter should always follow the configuration parameter
308for the kgdb I/O driver in the kernel command line else the I/O driver
309will not be configured prior to asking the kernel to use it to wait.
310
311The kernel will stop and wait as early as the I/O driver and
312architecture allows when you use this option. If you build the kgdb I/O
313driver as a loadable kernel module kgdbwait will not do anything.
314
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300315Kernel parameter: ``kgdbcon``
316-----------------------------
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300317
Puranjay Mohan2d88fc62020-08-11 00:06:13 +0530318The ``kgdbcon`` feature allows you to see printk() messages inside gdb
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300319while gdb is connected to the kernel. Kdb does not make use of the kgdbcon
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300320feature.
321
322Kgdb supports using the gdb serial protocol to send console messages to
323the debugger when the debugger is connected and running. There are two
324ways to activate this feature.
325
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03003261. Activate with the kernel command line option::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300327
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300328 kgdbcon
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300329
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03003302. Use sysfs before configuring an I/O driver::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300331
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300332 echo 1 > /sys/module/kgdb/parameters/kgdb_use_con
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300333
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300334.. note::
335
336 If you do this after you configure the kgdb I/O driver, the
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300337 setting will not take effect until the next point the I/O is
338 reconfigured.
339
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300340.. important::
341
342 You cannot use kgdboc + kgdbcon on a tty that is an
343 active system console. An example of incorrect usage is::
344
345 console=ttyS0,115200 kgdboc=ttyS0 kgdbcon
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300346
347It is possible to use this option with kgdboc on a tty that is not a
348system console.
349
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300350Run time parameter: ``kgdbreboot``
351----------------------------------
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300352
353The kgdbreboot feature allows you to change how the debugger deals with
354the reboot notification. You have 3 choices for the behavior. The
355default behavior is always set to 0.
356
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300357.. tabularcolumns:: |p{0.4cm}|p{11.5cm}|p{5.6cm}|
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300358
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300359.. flat-table::
360 :widths: 1 10 8
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300361
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300362 * - 1
363 - ``echo -1 > /sys/module/debug_core/parameters/kgdbreboot``
364 - Ignore the reboot notification entirely.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300365
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300366 * - 2
367 - ``echo 0 > /sys/module/debug_core/parameters/kgdbreboot``
368 - Send the detach message to any attached debugger client.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300369
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300370 * - 3
371 - ``echo 1 > /sys/module/debug_core/parameters/kgdbreboot``
372 - Enter the debugger on reboot notify.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300373
Zhouyi Zhou14994a92017-07-07 15:11:46 +0800374Kernel parameter: ``nokaslr``
375-----------------------------
376
377If the architecture that you are using enable KASLR by default,
378you should consider turning it off. KASLR randomizes the
379virtual address where the kernel image is mapped and confuse
380gdb which resolve kernel symbol address from symbol table
381of vmlinux.
382
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300383Using kdb
384=========
385
386Quick start for kdb on a serial port
387------------------------------------
388
389This is a quick example of how to use kdb.
390
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03003911. Configure kgdboc at boot using kernel parameters::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300392
Zhouyi Zhou14994a92017-07-07 15:11:46 +0800393 console=ttyS0,115200 kgdboc=ttyS0,115200 nokaslr
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300394
395 OR
396
397 Configure kgdboc after the kernel has booted; assuming you are using
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300398 a serial port console::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300399
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300400 echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300401
4022. Enter the kernel debugger manually or by waiting for an oops or
403 fault. There are several ways you can enter the kernel debugger
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300404 manually; all involve using the :kbd:`SysRq-G`, which means you must have
Lukas Bulwahne765c742021-12-30 18:24:23 +0100405 enabled ``CONFIG_MAGIC_SYSRQ=y`` in your kernel config.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300406
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300407 - When logged in as root or with a super user session you can run::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300408
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300409 echo g > /proc/sysrq-trigger
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300410
411 - Example using minicom 2.2
412
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300413 Press: :kbd:`CTRL-A` :kbd:`f` :kbd:`g`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300414
415 - When you have telneted to a terminal server that supports sending
416 a remote break
417
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300418 Press: :kbd:`CTRL-]`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300419
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300420 Type in: ``send break``
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300421
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300422 Press: :kbd:`Enter` :kbd:`g`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300423
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03004243. From the kdb prompt you can run the ``help`` command to see a complete
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300425 list of the commands that are available.
426
427 Some useful commands in kdb include:
428
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300429 =========== =================================================================
430 ``lsmod`` Shows where kernel modules are loaded
431 ``ps`` Displays only the active processes
432 ``ps A`` Shows all the processes
433 ``summary`` Shows kernel version info and memory usage
Puranjay Mohan2d88fc62020-08-11 00:06:13 +0530434 ``bt`` Get a backtrace of the current process using dump_stack()
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300435 ``dmesg`` View the kernel syslog buffer
436 ``go`` Continue the system
437 =========== =================================================================
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300438
4394. When you are done using kdb you need to consider rebooting the system
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300440 or using the ``go`` command to resuming normal kernel execution. If you
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300441 have paused the kernel for a lengthy period of time, applications
442 that rely on timely networking or anything to do with real wall clock
443 time could be adversely affected, so you should take this into
444 consideration when using the kernel debugger.
445
446Quick start for kdb using a keyboard connected console
447------------------------------------------------------
448
449This is a quick example of how to use kdb with a keyboard.
450
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03004511. Configure kgdboc at boot using kernel parameters::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300452
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300453 kgdboc=kbd
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300454
455 OR
456
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300457 Configure kgdboc after the kernel has booted::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300458
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300459 echo kbd > /sys/module/kgdboc/parameters/kgdboc
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300460
4612. Enter the kernel debugger manually or by waiting for an oops or
462 fault. There are several ways you can enter the kernel debugger
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300463 manually; all involve using the :kbd:`SysRq-G`, which means you must have
Lukas Bulwahne765c742021-12-30 18:24:23 +0100464 enabled ``CONFIG_MAGIC_SYSRQ=y`` in your kernel config.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300465
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300466 - When logged in as root or with a super user session you can run::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300467
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300468 echo g > /proc/sysrq-trigger
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300469
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300470 - Example using a laptop keyboard:
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300471
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300472 Press and hold down: :kbd:`Alt`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300473
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300474 Press and hold down: :kbd:`Fn`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300475
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300476 Press and release the key with the label: :kbd:`SysRq`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300477
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300478 Release: :kbd:`Fn`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300479
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300480 Press and release: :kbd:`g`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300481
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300482 Release: :kbd:`Alt`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300483
484 - Example using a PS/2 101-key keyboard
485
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300486 Press and hold down: :kbd:`Alt`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300487
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300488 Press and release the key with the label: :kbd:`SysRq`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300489
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300490 Press and release: :kbd:`g`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300491
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300492 Release: :kbd:`Alt`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300493
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03004943. Now type in a kdb command such as ``help``, ``dmesg``, ``bt`` or ``go`` to
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300495 continue kernel execution.
496
497Using kgdb / gdb
498================
499
500In order to use kgdb you must activate it by passing configuration
501information to one of the kgdb I/O drivers. If you do not pass any
502configuration information kgdb will not do anything at all. Kgdb will
503only actively hook up to the kernel trap hooks if a kgdb I/O driver is
504loaded and configured. If you unconfigure a kgdb I/O driver, kgdb will
505unregister all the kernel hook points.
506
507All kgdb I/O drivers can be reconfigured at run time, if
508``CONFIG_SYSFS`` and ``CONFIG_MODULES`` are enabled, by echo'ing a new
509config string to ``/sys/module/<driver>/parameter/<option>``. The driver
510can be unconfigured by passing an empty string. You cannot change the
511configuration while the debugger is attached. Make sure to detach the
512debugger with the ``detach`` command prior to trying to unconfigure a
513kgdb I/O driver.
514
515Connecting with gdb to a serial port
516------------------------------------
517
5181. Configure kgdboc
519
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300520 Configure kgdboc at boot using kernel parameters::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300521
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300522 kgdboc=ttyS0,115200
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300523
524 OR
525
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300526 Configure kgdboc after the kernel has booted::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300527
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300528 echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300529
5302. Stop kernel execution (break into the debugger)
531
532 In order to connect to gdb via kgdboc, the kernel must first be
533 stopped. There are several ways to stop the kernel which include
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300534 using kgdbwait as a boot argument, via a :kbd:`SysRq-G`, or running the
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300535 kernel until it takes an exception where it waits for the debugger to
536 attach.
537
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300538 - When logged in as root or with a super user session you can run::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300539
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300540 echo g > /proc/sysrq-trigger
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300541
542 - Example using minicom 2.2
543
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300544 Press: :kbd:`CTRL-A` :kbd:`f` :kbd:`g`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300545
546 - When you have telneted to a terminal server that supports sending
547 a remote break
548
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300549 Press: :kbd:`CTRL-]`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300550
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300551 Type in: ``send break``
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300552
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300553 Press: :kbd:`Enter` :kbd:`g`
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300554
5553. Connect from gdb
556
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300557 Example (using a directly connected port)::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300558
559 % gdb ./vmlinux
Christian Löhle689d8012022-01-05 09:58:38 +0000560 (gdb) set serial baud 115200
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300561 (gdb) target remote /dev/ttyS0
562
563
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300564 Example (kgdb to a terminal server on TCP port 2012)::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300565
566 % gdb ./vmlinux
567 (gdb) target remote 192.168.2.2:2012
568
569
570 Once connected, you can debug a kernel the way you would debug an
571 application program.
572
573 If you are having problems connecting or something is going seriously
574 wrong while debugging, it will most often be the case that you want
575 to enable gdb to be verbose about its target communications. You do
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300576 this prior to issuing the ``target remote`` command by typing in::
577
578 set debug remote 1
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300579
580Remember if you continue in gdb, and need to "break in" again, you need
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300581to issue an other :kbd:`SysRq-G`. It is easy to create a simple entry point by
582putting a breakpoint at ``sys_sync`` and then you can run ``sync`` from a
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300583shell or script to break into the debugger.
584
585kgdb and kdb interoperability
586=============================
587
588It is possible to transition between kdb and kgdb dynamically. The debug
589core will remember which you used the last time and automatically start
590in the same mode.
591
592Switching between kdb and kgdb
593------------------------------
594
595Switching from kgdb to kdb
596~~~~~~~~~~~~~~~~~~~~~~~~~~
597
598There are two ways to switch from kgdb to kdb: you can use gdb to issue
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300599a maintenance packet, or you can blindly type the command ``$3#33``.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300600Whenever the kernel debugger stops in kgdb mode it will print the
601message ``KGDB or $3#33 for KDB``. It is important to note that you have
602to type the sequence correctly in one pass. You cannot type a backspace
603or delete because kgdb will interpret that as part of the debug stream.
604
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03006051. Change from kgdb to kdb by blindly typing::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300606
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300607 $3#33
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300608
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03006092. Change from kgdb to kdb with gdb::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300610
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300611 maintenance packet 3
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300612
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300613 .. note::
614
615 Now you must kill gdb. Typically you press :kbd:`CTRL-Z` and issue
616 the command::
617
618 kill -9 %
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300619
620Change from kdb to kgdb
621~~~~~~~~~~~~~~~~~~~~~~~
622
623There are two ways you can change from kdb to kgdb. You can manually
624enter kgdb mode by issuing the kgdb command from the kdb shell prompt,
625or you can connect gdb while the kdb shell prompt is active. The kdb
626shell looks for the typical first commands that gdb would issue with the
627gdb remote protocol and if it sees one of those commands it
628automatically changes into kgdb mode.
629
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03006301. From kdb issue the command::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300631
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300632 kgdb
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300633
634 Now disconnect your terminal program and connect gdb in its place
635
6362. At the kdb prompt, disconnect the terminal program and connect gdb in
637 its place.
638
639Running kdb commands from gdb
640-----------------------------
641
642It is possible to run a limited set of kdb commands from gdb, using the
643gdb monitor command. You don't want to execute any of the run control or
644breakpoint operations, because it can disrupt the state of the kernel
645debugger. You should be using gdb for breakpoints and run control
646operations if you have gdb connected. The more useful commands to run
647are things like lsmod, dmesg, ps or possibly some of the memory
648information commands. To see all the kdb commands you can run
649``monitor help``.
650
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300651Example::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300652
653 (gdb) monitor ps
654 1 idle process (state I) and
655 27 sleeping system daemon (state M) processes suppressed,
656 use 'ps A' to see all.
657 Task Addr Pid Parent [*] cpu State Thread Command
658
659 0xc78291d0 1 0 0 0 S 0xc7829404 init
660 0xc7954150 942 1 0 0 S 0xc7954384 dropbear
661 0xc78789c0 944 1 0 0 S 0xc7878bf4 sh
662 (gdb)
663
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300664kgdb Test Suite
665===============
666
667When kgdb is enabled in the kernel config you can also elect to enable
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300668the config parameter ``KGDB_TESTS``. Turning this on will enable a special
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300669kgdb I/O module which is designed to test the kgdb internal functions.
670
671The kgdb tests are mainly intended for developers to test the kgdb
672internals as well as a tool for developing a new kgdb architecture
673specific implementation. These tests are not really for end users of the
674Linux kernel. The primary source of documentation would be to look in
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300675the ``drivers/misc/kgdbts.c`` file.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300676
677The kgdb test suite can also be configured at compile time to run the
678core set of tests by setting the kernel config parameter
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300679``KGDB_TESTS_ON_BOOT``. This particular option is aimed at automated
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300680regression testing and does not require modifying the kernel boot config
681arguments. If this is turned on, the kgdb test suite can be disabled by
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300682specifying ``kgdbts=`` as a kernel boot argument.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300683
684Kernel Debugger Internals
685=========================
686
687Architecture Specifics
688----------------------
689
690The kernel debugger is organized into a number of components:
691
6921. The debug core
693
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300694 The debug core is found in ``kernel/debugger/debug_core.c``. It
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300695 contains:
696
697 - A generic OS exception handler which includes sync'ing the
698 processors into a stopped state on an multi-CPU system.
699
700 - The API to talk to the kgdb I/O drivers
701
702 - The API to make calls to the arch-specific kgdb implementation
703
704 - The logic to perform safe memory reads and writes to memory while
705 using the debugger
706
707 - A full implementation for software breakpoints unless overridden
708 by the arch
709
710 - The API to invoke either the kdb or kgdb frontend to the debug
711 core.
712
713 - The structures and callback API for atomic kernel mode setting.
714
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300715 .. note:: kgdboc is where the kms callbacks are invoked.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300716
7172. kgdb arch-specific implementation
718
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300719 This implementation is generally found in ``arch/*/kernel/kgdb.c``. As
720 an example, ``arch/x86/kernel/kgdb.c`` contains the specifics to
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300721 implement HW breakpoint as well as the initialization to dynamically
722 register and unregister for the trap handlers on this architecture.
723 The arch-specific portion implements:
724
725 - contains an arch-specific trap catcher which invokes
Puranjay Mohan2d88fc62020-08-11 00:06:13 +0530726 kgdb_handle_exception() to start kgdb about doing its work
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300727
Mauro Carvalho Chehab365ff562020-09-29 11:18:20 +0200728 - translation to and from gdb specific packet format to struct pt_regs
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300729
730 - Registration and unregistration of architecture specific trap
731 hooks
732
733 - Any special exception handling and cleanup
734
735 - NMI exception handling and cleanup
736
737 - (optional) HW breakpoints
738
7393. gdbstub frontend (aka kgdb)
740
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300741 The gdbstub is located in ``kernel/debug/gdbstub.c``. It contains:
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300742
743 - All the logic to implement the gdb serial protocol
744
7454. kdb frontend
746
747 The kdb debugger shell is broken down into a number of components.
748 The kdb core is located in kernel/debug/kdb. There are a number of
749 helper functions in some of the other kernel components to make it
750 possible for kdb to examine and report information about the kernel
751 without taking locks that could cause a kernel deadlock. The kdb core
752 contains implements the following functionality.
753
754 - A simple shell
755
756 - The kdb core command set
757
758 - A registration API to register additional kdb shell commands.
759
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300760 - A good example of a self-contained kdb module is the ``ftdump``
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300761 command for dumping the ftrace buffer. See:
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300762 ``kernel/trace/trace_kdb.c``
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300763
764 - For an example of how to dynamically register a new kdb command
765 you can build the kdb_hello.ko kernel module from
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300766 ``samples/kdb/kdb_hello.c``. To build this example you can set
767 ``CONFIG_SAMPLES=y`` and ``CONFIG_SAMPLE_KDB=m`` in your kernel
768 config. Later run ``modprobe kdb_hello`` and the next time you
769 enter the kdb shell, you can run the ``hello`` command.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300770
Puranjay Mohan2d88fc62020-08-11 00:06:13 +0530771 - The implementation for kdb_printf() which emits messages directly
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300772 to I/O drivers, bypassing the kernel log.
773
774 - SW / HW breakpoint management for the kdb shell
775
7765. kgdb I/O driver
777
778 Each kgdb I/O driver has to provide an implementation for the
779 following:
780
781 - configuration via built-in or module
782
783 - dynamic configuration and kgdb hook registration calls
784
785 - read and write character interface
786
787 - A cleanup handler for unconfiguring from the kgdb core
788
789 - (optional) Early debug methodology
790
791 Any given kgdb I/O driver has to operate very closely with the
792 hardware and must do it in such a way that does not enable interrupts
793 or change other parts of the system context without completely
794 restoring them. The kgdb core will repeatedly "poll" a kgdb I/O
795 driver for characters when it needs input. The I/O driver is expected
796 to return immediately if there is no data available. Doing so allows
797 for the future possibility to touch watchdog hardware in such a way
798 as to have a target system not reset when these are enabled.
799
800If you are intent on adding kgdb architecture specific support for a new
801architecture, the architecture should define ``HAVE_ARCH_KGDB`` in the
802architecture specific Kconfig file. This will enable kgdb for the
803architecture, and at that point you must create an architecture specific
804kgdb implementation.
805
806There are a few flags which must be set on every architecture in their
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300807``asm/kgdb.h`` file. These are:
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300808
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300809- ``NUMREGBYTES``:
810 The size in bytes of all of the registers, so that we
811 can ensure they will all fit into a packet.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300812
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300813- ``BUFMAX``:
814 The size in bytes of the buffer GDB will read into. This must
815 be larger than NUMREGBYTES.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300816
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300817- ``CACHE_FLUSH_IS_SAFE``:
818 Set to 1 if it is always safe to call
819 flush_cache_range or flush_icache_range. On some architectures,
820 these functions may not be safe to call on SMP since we keep other
821 CPUs in a holding pattern.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300822
823There are also the following functions for the common backend, found in
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300824``kernel/kgdb.c``, that must be supplied by the architecture-specific
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300825backend unless marked as (optional), in which case a default function
826maybe used if the architecture does not need to provide a specific
827implementation.
828
829.. kernel-doc:: include/linux/kgdb.h
830 :internal:
831
832kgdboc internals
833----------------
834
835kgdboc and uarts
836~~~~~~~~~~~~~~~~
837
838The kgdboc driver is actually a very thin driver that relies on the
839underlying low level to the hardware driver having "polling hooks" to
840which the tty driver is attached. In the initial implementation of
841kgdboc the serial_core was changed to expose a low level UART hook for
842doing polled mode reading and writing of a single character while in an
843atomic context. When kgdb makes an I/O request to the debugger, kgdboc
844invokes a callback in the serial core which in turn uses the callback in
845the UART driver.
846
847When using kgdboc with a UART, the UART driver must implement two
Mauro Carvalho Chehab365ff562020-09-29 11:18:20 +0200848callbacks in the struct uart_ops.
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300849Example from ``drivers/8250.c``::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300850
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300851
852 #ifdef CONFIG_CONSOLE_POLL
853 .poll_get_char = serial8250_get_poll_char,
854 .poll_put_char = serial8250_put_poll_char,
855 #endif
856
857
858Any implementation specifics around creating a polling driver use the
859``#ifdef CONFIG_CONSOLE_POLL``, as shown above. Keep in mind that
860polling hooks have to be implemented in such a way that they can be
861called from an atomic context and have to restore the state of the UART
862chip on return such that the system can return to normal when the
863debugger detaches. You need to be very careful with any kind of lock you
864consider, because failing here is most likely going to mean pressing the
865reset button.
866
867kgdboc and keyboards
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300868~~~~~~~~~~~~~~~~~~~~~~~~
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300869
870The kgdboc driver contains logic to configure communications with an
871attached keyboard. The keyboard infrastructure is only compiled into the
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300872kernel when ``CONFIG_KDB_KEYBOARD=y`` is set in the kernel configuration.
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300873
Randy Dunlap06467a72020-07-07 11:03:58 -0700874The core polled keyboard driver for PS/2 type keyboards is in
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300875``drivers/char/kdb_keyboard.c``. This driver is hooked into the debug core
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300876when kgdboc populates the callback in the array called
Mauro Carvalho Chehab365ff562020-09-29 11:18:20 +0200877:c:expr:`kdb_poll_funcs[]`. The kdb_get_kbd_char() is the top-level
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300878function which polls hardware for single character input.
879
880kgdboc and kms
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300881~~~~~~~~~~~~~~~~~~
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300882
883The kgdboc driver contains logic to request the graphics display to
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300884switch to a text context when you are using ``kgdboc=kms,kbd``, provided
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300885that you have a video driver which has a frame buffer console and atomic
886kernel mode setting support.
887
888Every time the kernel debugger is entered it calls
Puranjay Mohan2d88fc62020-08-11 00:06:13 +0530889kgdboc_pre_exp_handler() which in turn calls con_debug_enter()
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300890in the virtual console layer. On resuming kernel execution, the kernel
Puranjay Mohan2d88fc62020-08-11 00:06:13 +0530891debugger calls kgdboc_post_exp_handler() which in turn calls
892con_debug_leave().
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300893
894Any video driver that wants to be compatible with the kernel debugger
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300895and the atomic kms callbacks must implement the ``mode_set_base_atomic``,
896``fb_debug_enter`` and ``fb_debug_leave operations``. For the
897``fb_debug_enter`` and ``fb_debug_leave`` the option exists to use the
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300898generic drm fb helper functions or implement something custom for the
899hardware. The following example shows the initialization of the
900.mode_set_base_atomic operation in
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300901drivers/gpu/drm/i915/intel_display.c::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300902
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300903
904 static const struct drm_crtc_helper_funcs intel_helper_funcs = {
905 [...]
906 .mode_set_base_atomic = intel_pipe_set_base_atomic,
907 [...]
908 };
909
910
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300911Here is an example of how the i915 driver initializes the
912fb_debug_enter and fb_debug_leave functions to use the generic drm
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300913helpers in ``drivers/gpu/drm/i915/intel_fb.c``::
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300914
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300915
916 static struct fb_ops intelfb_ops = {
917 [...]
918 .fb_debug_enter = drm_fb_helper_debug_enter,
919 .fb_debug_leave = drm_fb_helper_debug_leave,
920 [...]
921 };
922
923
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300924Credits
925=======
926
927The following people have contributed to this document:
928
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03009291. Amit Kale <amitkale@linsyssoft.com>
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300930
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -03009312. Tom Rini <trini@kernel.crashing.org>
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300932
933In March 2008 this document was completely rewritten by:
934
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300935- Jason Wessel <jason.wessel@windriver.com>
Mauro Carvalho Chehab7fb2e8a2017-05-11 16:23:50 -0300936
937In Jan 2010 this document was updated to include kdb.
938
Mauro Carvalho Chehab821c6df2017-05-11 17:43:20 -0300939- Jason Wessel <jason.wessel@windriver.com>