kgdb.tmpl 42 KB

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