kgdb.rst 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. =================================================
  2. Using kgdb, kdb and the kernel debugger internals
  3. =================================================
  4. :Author: Jason Wessel
  5. Introduction
  6. ============
  7. The kernel has two different debugger front ends (kdb and kgdb) which
  8. interface to the debug core. It is possible to use either of the
  9. debugger front ends and dynamically transition between them if you
  10. configure the kernel properly at compile and runtime.
  11. Kdb is simplistic shell-style interface which you can use on a system
  12. console with a keyboard or serial console. You can use it to inspect
  13. memory, registers, process lists, dmesg, and even set breakpoints to
  14. stop in a certain location. Kdb is not a source level debugger, although
  15. you can set breakpoints and execute some basic kernel run control. Kdb
  16. is mainly aimed at doing some analysis to aid in development or
  17. diagnosing kernel problems. You can access some symbols by name in
  18. kernel built-ins or in kernel modules if the code was built with
  19. ``CONFIG_KALLSYMS``.
  20. Kgdb is intended to be used as a source level debugger for the Linux
  21. kernel. It is used along with gdb to debug a Linux kernel. The
  22. expectation is that gdb can be used to "break in" to the kernel to
  23. inspect memory, variables and look through call stack information
  24. similar to the way an application developer would use gdb to debug an
  25. application. It is possible to place breakpoints in kernel code and
  26. perform some limited execution stepping.
  27. Two machines are required for using kgdb. One of these machines is a
  28. development machine and the other is the target machine. The kernel to
  29. be debugged runs on the target machine. The development machine runs an
  30. instance of gdb against the vmlinux file which contains the symbols (not
  31. a boot image such as bzImage, zImage, uImage...). In gdb the developer
  32. specifies the connection parameters and connects to kgdb. The type of
  33. connection a developer makes with gdb depends on the availability of
  34. kgdb I/O modules compiled as built-ins or loadable kernel modules in the
  35. test machine's kernel.
  36. Compiling a kernel
  37. ==================
  38. - In order to enable compilation of kdb, you must first enable kgdb.
  39. - The kgdb test compile options are described in the kgdb test suite
  40. chapter.
  41. Kernel config options for kgdb
  42. ------------------------------
  43. To enable ``CONFIG_KGDB`` you should look under
  44. :menuselection:`Kernel hacking --> Kernel debugging` and select
  45. :menuselection:`KGDB: kernel debugger`.
  46. While it is not a hard requirement that you have symbols in your vmlinux
  47. file, gdb tends not to be very useful without the symbolic data, so you
  48. will want to turn on ``CONFIG_DEBUG_INFO`` which is called
  49. :menuselection:`Compile the kernel with debug info` in the config menu.
  50. It is advised, but not required, that you turn on the
  51. ``CONFIG_FRAME_POINTER`` kernel option which is called :menuselection:`Compile
  52. the kernel with frame pointers` in the config menu. This option inserts code
  53. to into the compiled executable which saves the frame information in
  54. registers or on the stack at different points which allows a debugger
  55. such as gdb to more accurately construct stack back traces while
  56. debugging the kernel.
  57. If the architecture that you are using supports the kernel option
  58. ``CONFIG_STRICT_KERNEL_RWX``, you should consider turning it off. This
  59. option will prevent the use of software breakpoints because it marks
  60. certain regions of the kernel's memory space as read-only. If kgdb
  61. supports it for the architecture you are using, you can use hardware
  62. breakpoints if you desire to run with the ``CONFIG_STRICT_KERNEL_RWX``
  63. option turned on, else you need to turn off this option.
  64. Next you should choose one of more I/O drivers to interconnect debugging
  65. host and debugged target. Early boot debugging requires a KGDB I/O
  66. driver that supports early debugging and the driver must be built into
  67. the kernel directly. Kgdb I/O driver configuration takes place via
  68. kernel or module parameters which you can learn more about in the in the
  69. section that describes the parameter kgdboc.
  70. Here is an example set of ``.config`` symbols to enable or disable for kgdb::
  71. # CONFIG_STRICT_KERNEL_RWX is not set
  72. CONFIG_FRAME_POINTER=y
  73. CONFIG_KGDB=y
  74. CONFIG_KGDB_SERIAL_CONSOLE=y
  75. Kernel config options for kdb
  76. -----------------------------
  77. Kdb is quite a bit more complex than the simple gdbstub sitting on top
  78. of the kernel's debug core. Kdb must implement a shell, and also adds
  79. some helper functions in other parts of the kernel, responsible for
  80. printing out interesting data such as what you would see if you ran
  81. ``lsmod``, or ``ps``. In order to build kdb into the kernel you follow the
  82. same steps as you would for kgdb.
  83. The main config option for kdb is ``CONFIG_KGDB_KDB`` which is called
  84. :menuselection:`KGDB_KDB: include kdb frontend for kgdb` in the config menu.
  85. In theory you would have already also selected an I/O driver such as the
  86. ``CONFIG_KGDB_SERIAL_CONSOLE`` interface if you plan on using kdb on a
  87. serial port, when you were configuring kgdb.
  88. If you want to use a PS/2-style keyboard with kdb, you would select
  89. ``CONFIG_KDB_KEYBOARD`` which is called :menuselection:`KGDB_KDB: keyboard as
  90. input device` in the config menu. The ``CONFIG_KDB_KEYBOARD`` option is not
  91. used for anything in the gdb interface to kgdb. The ``CONFIG_KDB_KEYBOARD``
  92. option only works with kdb.
  93. Here is an example set of ``.config`` symbols to enable/disable kdb::
  94. # CONFIG_STRICT_KERNEL_RWX is not set
  95. CONFIG_FRAME_POINTER=y
  96. CONFIG_KGDB=y
  97. CONFIG_KGDB_SERIAL_CONSOLE=y
  98. CONFIG_KGDB_KDB=y
  99. CONFIG_KDB_KEYBOARD=y
  100. Kernel Debugger Boot Arguments
  101. ==============================
  102. This section describes the various runtime kernel parameters that affect
  103. the configuration of the kernel debugger. The following chapter covers
  104. using kdb and kgdb as well as providing some examples of the
  105. configuration parameters.
  106. Kernel parameter: kgdboc
  107. ------------------------
  108. The kgdboc driver was originally an abbreviation meant to stand for
  109. "kgdb over console". Today it is the primary mechanism to configure how
  110. to communicate from gdb to kgdb as well as the devices you want to use
  111. to interact with the kdb shell.
  112. For kgdb/gdb, kgdboc is designed to work with a single serial port. It
  113. is intended to cover the circumstance where you want to use a serial
  114. console as your primary console as well as using it to perform kernel
  115. debugging. It is also possible to use kgdb on a serial port which is not
  116. designated as a system console. Kgdboc may be configured as a kernel
  117. built-in or a kernel loadable module. You can only make use of
  118. ``kgdbwait`` and early debugging if you build kgdboc into the kernel as
  119. a built-in.
  120. Optionally you can elect to activate kms (Kernel Mode Setting)
  121. integration. When you use kms with kgdboc and you have a video driver
  122. that has atomic mode setting hooks, it is possible to enter the debugger
  123. on the graphics console. When the kernel execution is resumed, the
  124. previous graphics mode will be restored. This integration can serve as a
  125. useful tool to aid in diagnosing crashes or doing analysis of memory
  126. with kdb while allowing the full graphics console applications to run.
  127. kgdboc arguments
  128. ~~~~~~~~~~~~~~~~
  129. Usage::
  130. kgdboc=[kms][[,]kbd][[,]serial_device][,baud]
  131. The order listed above must be observed if you use any of the optional
  132. configurations together.
  133. Abbreviations:
  134. - kms = Kernel Mode Setting
  135. - kbd = Keyboard
  136. You can configure kgdboc to use the keyboard, and/or a serial device
  137. depending on if you are using kdb and/or kgdb, in one of the following
  138. scenarios. The order listed above must be observed if you use any of the
  139. optional configurations together. Using kms + only gdb is generally not
  140. a useful combination.
  141. Using loadable module or built-in
  142. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  143. 1. As a kernel built-in:
  144. Use the kernel boot argument::
  145. kgdboc=<tty-device>,[baud]
  146. 2. As a kernel loadable module:
  147. Use the command::
  148. modprobe kgdboc kgdboc=<tty-device>,[baud]
  149. Here are two examples of how you might format the kgdboc string. The
  150. first is for an x86 target using the first serial port. The second
  151. example is for the ARM Versatile AB using the second serial port.
  152. 1. ``kgdboc=ttyS0,115200``
  153. 2. ``kgdboc=ttyAMA1,115200``
  154. Configure kgdboc at runtime with sysfs
  155. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  156. At run time you can enable or disable kgdboc by echoing a parameters
  157. into the sysfs. Here are two examples:
  158. 1. Enable kgdboc on ttyS0::
  159. echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc
  160. 2. Disable kgdboc::
  161. echo "" > /sys/module/kgdboc/parameters/kgdboc
  162. .. note::
  163. You do not need to specify the baud if you are configuring the
  164. console on tty which is already configured or open.
  165. More examples
  166. ^^^^^^^^^^^^^
  167. You can configure kgdboc to use the keyboard, and/or a serial device
  168. depending on if you are using kdb and/or kgdb, in one of the following
  169. scenarios.
  170. 1. kdb and kgdb over only a serial port::
  171. kgdboc=<serial_device>[,baud]
  172. Example::
  173. kgdboc=ttyS0,115200
  174. 2. kdb and kgdb with keyboard and a serial port::
  175. kgdboc=kbd,<serial_device>[,baud]
  176. Example::
  177. kgdboc=kbd,ttyS0,115200
  178. 3. kdb with a keyboard::
  179. kgdboc=kbd
  180. 4. kdb with kernel mode setting::
  181. kgdboc=kms,kbd
  182. 5. kdb with kernel mode setting and kgdb over a serial port::
  183. kgdboc=kms,kbd,ttyS0,115200
  184. .. note::
  185. Kgdboc does not support interrupting the target via the gdb remote
  186. protocol. You must manually send a :kbd:`SysRq-G` unless you have a proxy
  187. that splits console output to a terminal program. A console proxy has a
  188. separate TCP port for the debugger and a separate TCP port for the
  189. "human" console. The proxy can take care of sending the :kbd:`SysRq-G`
  190. for you.
  191. When using kgdboc with no debugger proxy, you can end up connecting the
  192. debugger at one of two entry points. If an exception occurs after you
  193. have loaded kgdboc, a message should print on the console stating it is
  194. waiting for the debugger. In this case you disconnect your terminal
  195. program and then connect the debugger in its place. If you want to
  196. interrupt the target system and forcibly enter a debug session you have
  197. to issue a :kbd:`Sysrq` sequence and then type the letter :kbd:`g`. Then you
  198. disconnect the terminal session and connect gdb. Your options if you
  199. don't like this are to hack gdb to send the :kbd:`SysRq-G` for you as well as
  200. on the initial connect, or to use a debugger proxy that allows an
  201. unmodified gdb to do the debugging.
  202. Kernel parameter: ``kgdboc_earlycon``
  203. -------------------------------------
  204. If you specify the kernel parameter ``kgdboc_earlycon`` and your serial
  205. driver registers a boot console that supports polling (doesn't need
  206. interrupts and implements a nonblocking read() function) kgdb will attempt
  207. to work using the boot console until it can transition to the regular
  208. tty driver specified by the ``kgdboc`` parameter.
  209. Normally there is only one boot console (especially that implements the
  210. read() function) so just adding ``kgdboc_earlycon`` on its own is
  211. sufficient to make this work. If you have more than one boot console you
  212. can add the boot console's name to differentiate. Note that names that
  213. are registered through the boot console layer and the tty layer are not
  214. the same for the same port.
  215. For instance, on one board to be explicit you might do::
  216. kgdboc_earlycon=qcom_geni kgdboc=ttyMSM0
  217. If the only boot console on the device was "qcom_geni", you could simplify::
  218. kgdboc_earlycon kgdboc=ttyMSM0
  219. Kernel parameter: ``kgdbwait``
  220. ------------------------------
  221. The Kernel command line option ``kgdbwait`` makes kgdb wait for a
  222. debugger connection during booting of a kernel. You can only use this
  223. option if you compiled a kgdb I/O driver into the kernel and you
  224. specified the I/O driver configuration as a kernel command line option.
  225. The kgdbwait parameter should always follow the configuration parameter
  226. for the kgdb I/O driver in the kernel command line else the I/O driver
  227. will not be configured prior to asking the kernel to use it to wait.
  228. The kernel will stop and wait as early as the I/O driver and
  229. architecture allows when you use this option. If you build the kgdb I/O
  230. driver as a loadable kernel module kgdbwait will not do anything.
  231. Kernel parameter: ``kgdbcon``
  232. -----------------------------
  233. The ``kgdbcon`` feature allows you to see printk() messages inside gdb
  234. while gdb is connected to the kernel. Kdb does not make use of the kgdbcon
  235. feature.
  236. Kgdb supports using the gdb serial protocol to send console messages to
  237. the debugger when the debugger is connected and running. There are two
  238. ways to activate this feature.
  239. 1. Activate with the kernel command line option::
  240. kgdbcon
  241. 2. Use sysfs before configuring an I/O driver::
  242. echo 1 > /sys/module/kgdb/parameters/kgdb_use_con
  243. .. note::
  244. If you do this after you configure the kgdb I/O driver, the
  245. setting will not take effect until the next point the I/O is
  246. reconfigured.
  247. .. important::
  248. You cannot use kgdboc + kgdbcon on a tty that is an
  249. active system console. An example of incorrect usage is::
  250. console=ttyS0,115200 kgdboc=ttyS0 kgdbcon
  251. It is possible to use this option with kgdboc on a tty that is not a
  252. system console.
  253. Run time parameter: ``kgdbreboot``
  254. ----------------------------------
  255. The kgdbreboot feature allows you to change how the debugger deals with
  256. the reboot notification. You have 3 choices for the behavior. The
  257. default behavior is always set to 0.
  258. .. tabularcolumns:: |p{0.4cm}|p{11.5cm}|p{5.6cm}|
  259. .. flat-table::
  260. :widths: 1 10 8
  261. * - 1
  262. - ``echo -1 > /sys/module/debug_core/parameters/kgdbreboot``
  263. - Ignore the reboot notification entirely.
  264. * - 2
  265. - ``echo 0 > /sys/module/debug_core/parameters/kgdbreboot``
  266. - Send the detach message to any attached debugger client.
  267. * - 3
  268. - ``echo 1 > /sys/module/debug_core/parameters/kgdbreboot``
  269. - Enter the debugger on reboot notify.
  270. Kernel parameter: ``nokaslr``
  271. -----------------------------
  272. If the architecture that you are using enable KASLR by default,
  273. you should consider turning it off. KASLR randomizes the
  274. virtual address where the kernel image is mapped and confuse
  275. gdb which resolve kernel symbol address from symbol table
  276. of vmlinux.
  277. Using kdb
  278. =========
  279. Quick start for kdb on a serial port
  280. ------------------------------------
  281. This is a quick example of how to use kdb.
  282. 1. Configure kgdboc at boot using kernel parameters::
  283. console=ttyS0,115200 kgdboc=ttyS0,115200 nokaslr
  284. OR
  285. Configure kgdboc after the kernel has booted; assuming you are using
  286. a serial port console::
  287. echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc
  288. 2. Enter the kernel debugger manually or by waiting for an oops or
  289. fault. There are several ways you can enter the kernel debugger
  290. manually; all involve using the :kbd:`SysRq-G`, which means you must have
  291. enabled ``CONFIG_MAGIC_SysRq=y`` in your kernel config.
  292. - When logged in as root or with a super user session you can run::
  293. echo g > /proc/sysrq-trigger
  294. - Example using minicom 2.2
  295. Press: :kbd:`CTRL-A` :kbd:`f` :kbd:`g`
  296. - When you have telneted to a terminal server that supports sending
  297. a remote break
  298. Press: :kbd:`CTRL-]`
  299. Type in: ``send break``
  300. Press: :kbd:`Enter` :kbd:`g`
  301. 3. From the kdb prompt you can run the ``help`` command to see a complete
  302. list of the commands that are available.
  303. Some useful commands in kdb include:
  304. =========== =================================================================
  305. ``lsmod`` Shows where kernel modules are loaded
  306. ``ps`` Displays only the active processes
  307. ``ps A`` Shows all the processes
  308. ``summary`` Shows kernel version info and memory usage
  309. ``bt`` Get a backtrace of the current process using dump_stack()
  310. ``dmesg`` View the kernel syslog buffer
  311. ``go`` Continue the system
  312. =========== =================================================================
  313. 4. When you are done using kdb you need to consider rebooting the system
  314. or using the ``go`` command to resuming normal kernel execution. If you
  315. have paused the kernel for a lengthy period of time, applications
  316. that rely on timely networking or anything to do with real wall clock
  317. time could be adversely affected, so you should take this into
  318. consideration when using the kernel debugger.
  319. Quick start for kdb using a keyboard connected console
  320. ------------------------------------------------------
  321. This is a quick example of how to use kdb with a keyboard.
  322. 1. Configure kgdboc at boot using kernel parameters::
  323. kgdboc=kbd
  324. OR
  325. Configure kgdboc after the kernel has booted::
  326. echo kbd > /sys/module/kgdboc/parameters/kgdboc
  327. 2. Enter the kernel debugger manually or by waiting for an oops or
  328. fault. There are several ways you can enter the kernel debugger
  329. manually; all involve using the :kbd:`SysRq-G`, which means you must have
  330. enabled ``CONFIG_MAGIC_SysRq=y`` in your kernel config.
  331. - When logged in as root or with a super user session you can run::
  332. echo g > /proc/sysrq-trigger
  333. - Example using a laptop keyboard:
  334. Press and hold down: :kbd:`Alt`
  335. Press and hold down: :kbd:`Fn`
  336. Press and release the key with the label: :kbd:`SysRq`
  337. Release: :kbd:`Fn`
  338. Press and release: :kbd:`g`
  339. Release: :kbd:`Alt`
  340. - Example using a PS/2 101-key keyboard
  341. Press and hold down: :kbd:`Alt`
  342. Press and release the key with the label: :kbd:`SysRq`
  343. Press and release: :kbd:`g`
  344. Release: :kbd:`Alt`
  345. 3. Now type in a kdb command such as ``help``, ``dmesg``, ``bt`` or ``go`` to
  346. continue kernel execution.
  347. Using kgdb / gdb
  348. ================
  349. In order to use kgdb you must activate it by passing configuration
  350. information to one of the kgdb I/O drivers. If you do not pass any
  351. configuration information kgdb will not do anything at all. Kgdb will
  352. only actively hook up to the kernel trap hooks if a kgdb I/O driver is
  353. loaded and configured. If you unconfigure a kgdb I/O driver, kgdb will
  354. unregister all the kernel hook points.
  355. All kgdb I/O drivers can be reconfigured at run time, if
  356. ``CONFIG_SYSFS`` and ``CONFIG_MODULES`` are enabled, by echo'ing a new
  357. config string to ``/sys/module/<driver>/parameter/<option>``. The driver
  358. can be unconfigured by passing an empty string. You cannot change the
  359. configuration while the debugger is attached. Make sure to detach the
  360. debugger with the ``detach`` command prior to trying to unconfigure a
  361. kgdb I/O driver.
  362. Connecting with gdb to a serial port
  363. ------------------------------------
  364. 1. Configure kgdboc
  365. Configure kgdboc at boot using kernel parameters::
  366. kgdboc=ttyS0,115200
  367. OR
  368. Configure kgdboc after the kernel has booted::
  369. echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc
  370. 2. Stop kernel execution (break into the debugger)
  371. In order to connect to gdb via kgdboc, the kernel must first be
  372. stopped. There are several ways to stop the kernel which include
  373. using kgdbwait as a boot argument, via a :kbd:`SysRq-G`, or running the
  374. kernel until it takes an exception where it waits for the debugger to
  375. attach.
  376. - When logged in as root or with a super user session you can run::
  377. echo g > /proc/sysrq-trigger
  378. - Example using minicom 2.2
  379. Press: :kbd:`CTRL-A` :kbd:`f` :kbd:`g`
  380. - When you have telneted to a terminal server that supports sending
  381. a remote break
  382. Press: :kbd:`CTRL-]`
  383. Type in: ``send break``
  384. Press: :kbd:`Enter` :kbd:`g`
  385. 3. Connect from gdb
  386. Example (using a directly connected port)::
  387. % gdb ./vmlinux
  388. (gdb) set remotebaud 115200
  389. (gdb) target remote /dev/ttyS0
  390. Example (kgdb to a terminal server on TCP port 2012)::
  391. % gdb ./vmlinux
  392. (gdb) target remote 192.168.2.2:2012
  393. Once connected, you can debug a kernel the way you would debug an
  394. application program.
  395. If you are having problems connecting or something is going seriously
  396. wrong while debugging, it will most often be the case that you want
  397. to enable gdb to be verbose about its target communications. You do
  398. this prior to issuing the ``target remote`` command by typing in::
  399. set debug remote 1
  400. Remember if you continue in gdb, and need to "break in" again, you need
  401. to issue an other :kbd:`SysRq-G`. It is easy to create a simple entry point by
  402. putting a breakpoint at ``sys_sync`` and then you can run ``sync`` from a
  403. shell or script to break into the debugger.
  404. kgdb and kdb interoperability
  405. =============================
  406. It is possible to transition between kdb and kgdb dynamically. The debug
  407. core will remember which you used the last time and automatically start
  408. in the same mode.
  409. Switching between kdb and kgdb
  410. ------------------------------
  411. Switching from kgdb to kdb
  412. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  413. There are two ways to switch from kgdb to kdb: you can use gdb to issue
  414. a maintenance packet, or you can blindly type the command ``$3#33``.
  415. Whenever the kernel debugger stops in kgdb mode it will print the
  416. message ``KGDB or $3#33 for KDB``. It is important to note that you have
  417. to type the sequence correctly in one pass. You cannot type a backspace
  418. or delete because kgdb will interpret that as part of the debug stream.
  419. 1. Change from kgdb to kdb by blindly typing::
  420. $3#33
  421. 2. Change from kgdb to kdb with gdb::
  422. maintenance packet 3
  423. .. note::
  424. Now you must kill gdb. Typically you press :kbd:`CTRL-Z` and issue
  425. the command::
  426. kill -9 %
  427. Change from kdb to kgdb
  428. ~~~~~~~~~~~~~~~~~~~~~~~
  429. There are two ways you can change from kdb to kgdb. You can manually
  430. enter kgdb mode by issuing the kgdb command from the kdb shell prompt,
  431. or you can connect gdb while the kdb shell prompt is active. The kdb
  432. shell looks for the typical first commands that gdb would issue with the
  433. gdb remote protocol and if it sees one of those commands it
  434. automatically changes into kgdb mode.
  435. 1. From kdb issue the command::
  436. kgdb
  437. Now disconnect your terminal program and connect gdb in its place
  438. 2. At the kdb prompt, disconnect the terminal program and connect gdb in
  439. its place.
  440. Running kdb commands from gdb
  441. -----------------------------
  442. It is possible to run a limited set of kdb commands from gdb, using the
  443. gdb monitor command. You don't want to execute any of the run control or
  444. breakpoint operations, because it can disrupt the state of the kernel
  445. debugger. You should be using gdb for breakpoints and run control
  446. operations if you have gdb connected. The more useful commands to run
  447. are things like lsmod, dmesg, ps or possibly some of the memory
  448. information commands. To see all the kdb commands you can run
  449. ``monitor help``.
  450. Example::
  451. (gdb) monitor ps
  452. 1 idle process (state I) and
  453. 27 sleeping system daemon (state M) processes suppressed,
  454. use 'ps A' to see all.
  455. Task Addr Pid Parent [*] cpu State Thread Command
  456. 0xc78291d0 1 0 0 0 S 0xc7829404 init
  457. 0xc7954150 942 1 0 0 S 0xc7954384 dropbear
  458. 0xc78789c0 944 1 0 0 S 0xc7878bf4 sh
  459. (gdb)
  460. kgdb Test Suite
  461. ===============
  462. When kgdb is enabled in the kernel config you can also elect to enable
  463. the config parameter ``KGDB_TESTS``. Turning this on will enable a special
  464. kgdb I/O module which is designed to test the kgdb internal functions.
  465. The kgdb tests are mainly intended for developers to test the kgdb
  466. internals as well as a tool for developing a new kgdb architecture
  467. specific implementation. These tests are not really for end users of the
  468. Linux kernel. The primary source of documentation would be to look in
  469. the ``drivers/misc/kgdbts.c`` file.
  470. The kgdb test suite can also be configured at compile time to run the
  471. core set of tests by setting the kernel config parameter
  472. ``KGDB_TESTS_ON_BOOT``. This particular option is aimed at automated
  473. regression testing and does not require modifying the kernel boot config
  474. arguments. If this is turned on, the kgdb test suite can be disabled by
  475. specifying ``kgdbts=`` as a kernel boot argument.
  476. Kernel Debugger Internals
  477. =========================
  478. Architecture Specifics
  479. ----------------------
  480. The kernel debugger is organized into a number of components:
  481. 1. The debug core
  482. The debug core is found in ``kernel/debugger/debug_core.c``. It
  483. contains:
  484. - A generic OS exception handler which includes sync'ing the
  485. processors into a stopped state on an multi-CPU system.
  486. - The API to talk to the kgdb I/O drivers
  487. - The API to make calls to the arch-specific kgdb implementation
  488. - The logic to perform safe memory reads and writes to memory while
  489. using the debugger
  490. - A full implementation for software breakpoints unless overridden
  491. by the arch
  492. - The API to invoke either the kdb or kgdb frontend to the debug
  493. core.
  494. - The structures and callback API for atomic kernel mode setting.
  495. .. note:: kgdboc is where the kms callbacks are invoked.
  496. 2. kgdb arch-specific implementation
  497. This implementation is generally found in ``arch/*/kernel/kgdb.c``. As
  498. an example, ``arch/x86/kernel/kgdb.c`` contains the specifics to
  499. implement HW breakpoint as well as the initialization to dynamically
  500. register and unregister for the trap handlers on this architecture.
  501. The arch-specific portion implements:
  502. - contains an arch-specific trap catcher which invokes
  503. kgdb_handle_exception() to start kgdb about doing its work
  504. - translation to and from gdb specific packet format to struct pt_regs
  505. - Registration and unregistration of architecture specific trap
  506. hooks
  507. - Any special exception handling and cleanup
  508. - NMI exception handling and cleanup
  509. - (optional) HW breakpoints
  510. 3. gdbstub frontend (aka kgdb)
  511. The gdbstub is located in ``kernel/debug/gdbstub.c``. It contains:
  512. - All the logic to implement the gdb serial protocol
  513. 4. kdb frontend
  514. The kdb debugger shell is broken down into a number of components.
  515. The kdb core is located in kernel/debug/kdb. There are a number of
  516. helper functions in some of the other kernel components to make it
  517. possible for kdb to examine and report information about the kernel
  518. without taking locks that could cause a kernel deadlock. The kdb core
  519. contains implements the following functionality.
  520. - A simple shell
  521. - The kdb core command set
  522. - A registration API to register additional kdb shell commands.
  523. - A good example of a self-contained kdb module is the ``ftdump``
  524. command for dumping the ftrace buffer. See:
  525. ``kernel/trace/trace_kdb.c``
  526. - For an example of how to dynamically register a new kdb command
  527. you can build the kdb_hello.ko kernel module from
  528. ``samples/kdb/kdb_hello.c``. To build this example you can set
  529. ``CONFIG_SAMPLES=y`` and ``CONFIG_SAMPLE_KDB=m`` in your kernel
  530. config. Later run ``modprobe kdb_hello`` and the next time you
  531. enter the kdb shell, you can run the ``hello`` command.
  532. - The implementation for kdb_printf() which emits messages directly
  533. to I/O drivers, bypassing the kernel log.
  534. - SW / HW breakpoint management for the kdb shell
  535. 5. kgdb I/O driver
  536. Each kgdb I/O driver has to provide an implementation for the
  537. following:
  538. - configuration via built-in or module
  539. - dynamic configuration and kgdb hook registration calls
  540. - read and write character interface
  541. - A cleanup handler for unconfiguring from the kgdb core
  542. - (optional) Early debug methodology
  543. Any given kgdb I/O driver has to operate very closely with the
  544. hardware and must do it in such a way that does not enable interrupts
  545. or change other parts of the system context without completely
  546. restoring them. The kgdb core will repeatedly "poll" a kgdb I/O
  547. driver for characters when it needs input. The I/O driver is expected
  548. to return immediately if there is no data available. Doing so allows
  549. for the future possibility to touch watchdog hardware in such a way
  550. as to have a target system not reset when these are enabled.
  551. If you are intent on adding kgdb architecture specific support for a new
  552. architecture, the architecture should define ``HAVE_ARCH_KGDB`` in the
  553. architecture specific Kconfig file. This will enable kgdb for the
  554. architecture, and at that point you must create an architecture specific
  555. kgdb implementation.
  556. There are a few flags which must be set on every architecture in their
  557. ``asm/kgdb.h`` file. These are:
  558. - ``NUMREGBYTES``:
  559. The size in bytes of all of the registers, so that we
  560. can ensure they will all fit into a packet.
  561. - ``BUFMAX``:
  562. The size in bytes of the buffer GDB will read into. This must
  563. be larger than NUMREGBYTES.
  564. - ``CACHE_FLUSH_IS_SAFE``:
  565. Set to 1 if it is always safe to call
  566. flush_cache_range or flush_icache_range. On some architectures,
  567. these functions may not be safe to call on SMP since we keep other
  568. CPUs in a holding pattern.
  569. There are also the following functions for the common backend, found in
  570. ``kernel/kgdb.c``, that must be supplied by the architecture-specific
  571. backend unless marked as (optional), in which case a default function
  572. maybe used if the architecture does not need to provide a specific
  573. implementation.
  574. .. kernel-doc:: include/linux/kgdb.h
  575. :internal:
  576. kgdboc internals
  577. ----------------
  578. kgdboc and uarts
  579. ~~~~~~~~~~~~~~~~
  580. The kgdboc driver is actually a very thin driver that relies on the
  581. underlying low level to the hardware driver having "polling hooks" to
  582. which the tty driver is attached. In the initial implementation of
  583. kgdboc the serial_core was changed to expose a low level UART hook for
  584. doing polled mode reading and writing of a single character while in an
  585. atomic context. When kgdb makes an I/O request to the debugger, kgdboc
  586. invokes a callback in the serial core which in turn uses the callback in
  587. the UART driver.
  588. When using kgdboc with a UART, the UART driver must implement two
  589. callbacks in the struct uart_ops.
  590. Example from ``drivers/8250.c``::
  591. #ifdef CONFIG_CONSOLE_POLL
  592. .poll_get_char = serial8250_get_poll_char,
  593. .poll_put_char = serial8250_put_poll_char,
  594. #endif
  595. Any implementation specifics around creating a polling driver use the
  596. ``#ifdef CONFIG_CONSOLE_POLL``, as shown above. Keep in mind that
  597. polling hooks have to be implemented in such a way that they can be
  598. called from an atomic context and have to restore the state of the UART
  599. chip on return such that the system can return to normal when the
  600. debugger detaches. You need to be very careful with any kind of lock you
  601. consider, because failing here is most likely going to mean pressing the
  602. reset button.
  603. kgdboc and keyboards
  604. ~~~~~~~~~~~~~~~~~~~~~~~~
  605. The kgdboc driver contains logic to configure communications with an
  606. attached keyboard. The keyboard infrastructure is only compiled into the
  607. kernel when ``CONFIG_KDB_KEYBOARD=y`` is set in the kernel configuration.
  608. The core polled keyboard driver for PS/2 type keyboards is in
  609. ``drivers/char/kdb_keyboard.c``. This driver is hooked into the debug core
  610. when kgdboc populates the callback in the array called
  611. :c:expr:`kdb_poll_funcs[]`. The kdb_get_kbd_char() is the top-level
  612. function which polls hardware for single character input.
  613. kgdboc and kms
  614. ~~~~~~~~~~~~~~~~~~
  615. The kgdboc driver contains logic to request the graphics display to
  616. switch to a text context when you are using ``kgdboc=kms,kbd``, provided
  617. that you have a video driver which has a frame buffer console and atomic
  618. kernel mode setting support.
  619. Every time the kernel debugger is entered it calls
  620. kgdboc_pre_exp_handler() which in turn calls con_debug_enter()
  621. in the virtual console layer. On resuming kernel execution, the kernel
  622. debugger calls kgdboc_post_exp_handler() which in turn calls
  623. con_debug_leave().
  624. Any video driver that wants to be compatible with the kernel debugger
  625. and the atomic kms callbacks must implement the ``mode_set_base_atomic``,
  626. ``fb_debug_enter`` and ``fb_debug_leave operations``. For the
  627. ``fb_debug_enter`` and ``fb_debug_leave`` the option exists to use the
  628. generic drm fb helper functions or implement something custom for the
  629. hardware. The following example shows the initialization of the
  630. .mode_set_base_atomic operation in
  631. drivers/gpu/drm/i915/intel_display.c::
  632. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  633. [...]
  634. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  635. [...]
  636. };
  637. Here is an example of how the i915 driver initializes the
  638. fb_debug_enter and fb_debug_leave functions to use the generic drm
  639. helpers in ``drivers/gpu/drm/i915/intel_fb.c``::
  640. static struct fb_ops intelfb_ops = {
  641. [...]
  642. .fb_debug_enter = drm_fb_helper_debug_enter,
  643. .fb_debug_leave = drm_fb_helper_debug_leave,
  644. [...]
  645. };
  646. Credits
  647. =======
  648. The following people have contributed to this document:
  649. 1. Amit Kale <amitkale@linsyssoft.com>
  650. 2. Tom Rini <trini@kernel.crashing.org>
  651. In March 2008 this document was completely rewritten by:
  652. - Jason Wessel <jason.wessel@windriver.com>
  653. In Jan 2010 this document was updated to include kdb.
  654. - Jason Wessel <jason.wessel@windriver.com>