kgdb.tmpl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 and the kgdb 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. <authorgroup>
  19. <author>
  20. <firstname>Tom</firstname>
  21. <surname>Rini</surname>
  22. <affiliation>
  23. <address>
  24. <email>trini@kernel.crashing.org</email>
  25. </address>
  26. </affiliation>
  27. </author>
  28. </authorgroup>
  29. <authorgroup>
  30. <author>
  31. <firstname>Amit S.</firstname>
  32. <surname>Kale</surname>
  33. <affiliation>
  34. <address>
  35. <email>amitkale@linsyssoft.com</email>
  36. </address>
  37. </affiliation>
  38. </author>
  39. </authorgroup>
  40. <copyright>
  41. <year>2008</year>
  42. <holder>Wind River Systems, Inc.</holder>
  43. </copyright>
  44. <copyright>
  45. <year>2004-2005</year>
  46. <holder>MontaVista Software, Inc.</holder>
  47. </copyright>
  48. <copyright>
  49. <year>2004</year>
  50. <holder>Amit S. Kale</holder>
  51. </copyright>
  52. <legalnotice>
  53. <para>
  54. This file is licensed under the terms of the GNU General Public License
  55. version 2. This program is licensed "as is" without any warranty of any
  56. kind, whether express or implied.
  57. </para>
  58. </legalnotice>
  59. </bookinfo>
  60. <toc></toc>
  61. <chapter id="Introduction">
  62. <title>Introduction</title>
  63. <para>
  64. kgdb is a source level debugger for linux kernel. It is used along
  65. with gdb to debug a linux kernel. The expectation is that gdb can
  66. be used to "break in" to the kernel to inspect memory, variables
  67. and look through a cal stack information similar to what an
  68. application developer would use gdb for. It is possible to place
  69. breakpoints in kernel code and perform some limited execution
  70. stepping.
  71. </para>
  72. <para>
  73. Two machines are required for using kgdb. One of these machines is a
  74. development machine and the other is a test machine. The kernel
  75. to be debugged runs on the test machine. The development machine
  76. runs an instance of gdb against the vmlinux file which contains
  77. the symbols (not boot image such as bzImage, zImage, uImage...).
  78. In gdb the developer specifies the connection parameters and
  79. connects to kgdb. Depending on which kgdb I/O modules exist in
  80. the kernel for a given architecture, it may be possible to debug
  81. the test machine's kernel with the development machine using a
  82. rs232 or ethernet connection.
  83. </para>
  84. </chapter>
  85. <chapter id="CompilingAKernel">
  86. <title>Compiling a kernel</title>
  87. <para>
  88. To enable <symbol>CONFIG_KGDB</symbol>, look under the "Kernel debugging"
  89. and then select "KGDB: kernel debugging with remote gdb".
  90. </para>
  91. <para>
  92. Next you should choose one of more I/O drivers to interconnect debugging
  93. host and debugged target. Early boot debugging requires a KGDB
  94. I/O driver that supports early debugging and the driver must be
  95. built into the kernel directly. Kgdb I/O driver configuration
  96. takes place via kernel or module parameters, see following
  97. chapter.
  98. </para>
  99. <para>
  100. The kgdb test compile options are described in the kgdb test suite chapter.
  101. </para>
  102. </chapter>
  103. <chapter id="EnableKGDB">
  104. <title>Enable kgdb for debugging</title>
  105. <para>
  106. In order to use kgdb you must activate it by passing configuration
  107. information to one of the kgdb I/O drivers. If you do not pass any
  108. configuration information kgdb will not do anything at all. Kgdb
  109. will only actively hook up to the kernel trap hooks if a kgdb I/O
  110. driver is loaded and configured. If you unconfigure a kgdb I/O
  111. driver, kgdb will unregister all the kernel hook points.
  112. </para>
  113. <para>
  114. All drivers can be reconfigured at run time, if
  115. <symbol>CONFIG_SYSFS</symbol> and <symbol>CONFIG_MODULES</symbol>
  116. are enabled, by echo'ing a new config string to
  117. <constant>/sys/module/&lt;driver&gt;/parameter/&lt;option&gt;</constant>.
  118. The driver can be unconfigured by passing an empty string. You cannot
  119. change the configuration while the debugger is attached. Make sure
  120. to detach the debugger with the <constant>detach</constant> command
  121. prior to trying unconfigure a kgdb I/O driver.
  122. </para>
  123. <sect1 id="kgdbwait">
  124. <title>Kernel parameter: kgdbwait</title>
  125. <para>
  126. The Kernel command line option <constant>kgdbwait</constant> makes
  127. kgdb wait for a debugger connection during booting of a kernel. You
  128. can only use this option you compiled a kgdb I/O driver into the
  129. kernel and you specified the I/O driver configuration as a kernel
  130. command line option. The kgdbwait parameter should always follow the
  131. configuration parameter for the kgdb I/O driver in the kernel
  132. command line else the I/O driver will not be configured prior to
  133. asking the kernel to use it to wait.
  134. </para>
  135. <para>
  136. The kernel will stop and wait as early as the I/O driver and
  137. architecture will allow when you use this option. If you build the
  138. kgdb I/O driver as a kernel module kgdbwait will not do anything.
  139. </para>
  140. </sect1>
  141. <sect1 id="kgdboc">
  142. <title>Kernel parameter: kgdboc</title>
  143. <para>
  144. The kgdboc driver was originally an abbreviation meant to stand for
  145. "kgdb over console". Kgdboc is designed to work with a single
  146. serial port. It was meant to cover the circumstance
  147. where you wanted to use a serial console as your primary console as
  148. well as using it to perform kernel debugging. Of course you can
  149. also use kgdboc without assigning a console to the same port.
  150. </para>
  151. <sect2 id="UsingKgdboc">
  152. <title>Using kgdboc</title>
  153. <para>
  154. You can configure kgdboc via sysfs or a module or kernel boot line
  155. parameter depending on if you build with CONFIG_KGDBOC as a module
  156. or built-in.
  157. <orderedlist>
  158. <listitem><para>From the module load or build-in</para>
  159. <para><constant>kgdboc=&lt;tty-device&gt;,[baud]</constant></para>
  160. <para>
  161. The example here would be if your console port was typically ttyS0, you would use something like <constant>kgdboc=ttyS0,115200</constant> or on the ARM Versatile AB you would likely use <constant>kgdboc=ttyAMA0,115200</constant>
  162. </para>
  163. </listitem>
  164. <listitem><para>From sysfs</para>
  165. <para><constant>echo ttyS0 &gt; /sys/module/kgdboc/parameters/kgdboc</constant></para>
  166. </listitem>
  167. </orderedlist>
  168. </para>
  169. <para>
  170. NOTE: Kgdboc does not support interrupting the target via the
  171. gdb remote protocol. You must manually send a sysrq-g unless you
  172. have a proxy that splits console output to a terminal problem and
  173. has a separate port for the debugger to connect to that sends the
  174. sysrq-g for you.
  175. </para>
  176. <para>When using kgdboc with no debugger proxy, you can end up
  177. connecting the debugger for one of two entry points. If an
  178. exception occurs after you have loaded kgdboc a message should print
  179. on the console stating it is waiting for the debugger. In case you
  180. disconnect your terminal program and then connect the debugger in
  181. its place. If you want to interrupt the target system and forcibly
  182. enter a debug session you have to issue a Sysrq sequence and then
  183. type the letter <constant>g</constant>. Then you disconnect the
  184. terminal session and connect gdb. Your options if you don't like
  185. this are to hack gdb to send the sysrq-g for you as well as on the
  186. initial connect, or to use a debugger proxy that allows an
  187. unmodified gdb to do the debugging.
  188. </para>
  189. </sect2>
  190. </sect1>
  191. <sect1 id="kgdbcon">
  192. <title>Kernel parameter: kgdbcon</title>
  193. <para>
  194. Kgdb supports using the gdb serial protocol to send console messages
  195. to the debugger when the debugger is connected and running. There
  196. are two ways to activate this feature.
  197. <orderedlist>
  198. <listitem><para>Activate with the kernel command line option:</para>
  199. <para><constant>kgdbcon</constant></para>
  200. </listitem>
  201. <listitem><para>Use sysfs before configuring an io driver</para>
  202. <para>
  203. <constant>echo 1 &gt; /sys/module/kgdb/parameters/kgdb_use_con</constant>
  204. </para>
  205. <para>
  206. NOTE: If you do this after you configure the kgdb I/O driver, the
  207. setting will not take effect until the next point the I/O is
  208. reconfigured.
  209. </para>
  210. </listitem>
  211. </orderedlist>
  212. </para>
  213. <para>
  214. IMPORTANT NOTE: Using this option with kgdb over the console
  215. (kgdboc) or kgdb over ethernet (kgdboe) is not supported.
  216. </para>
  217. </sect1>
  218. </chapter>
  219. <chapter id="ConnectingGDB">
  220. <title>Connecting gdb</title>
  221. <para>
  222. If you are using kgdboc, you need to have used kgdbwait as a boot
  223. argument, issued a sysrq-g, or the system you are going to debug
  224. has already taken an exception and is waiting for the debugger to
  225. attach before you can connect gdb.
  226. </para>
  227. <para>
  228. If you are not using different kgdb I/O driver other than kgdboc,
  229. you should be able to connect and the target will automatically
  230. respond.
  231. </para>
  232. <para>
  233. Example (using a serial port):
  234. </para>
  235. <programlisting>
  236. % gdb ./vmlinux
  237. (gdb) set remotebaud 115200
  238. (gdb) target remote /dev/ttyS0
  239. </programlisting>
  240. <para>
  241. Example (kgdb to a terminal server):
  242. </para>
  243. <programlisting>
  244. % gdb ./vmlinux
  245. (gdb) target remote udp:192.168.2.2:6443
  246. </programlisting>
  247. <para>
  248. Example (kgdb over ethernet):
  249. </para>
  250. <programlisting>
  251. % gdb ./vmlinux
  252. (gdb) target remote udp:192.168.2.2:6443
  253. </programlisting>
  254. <para>
  255. Once connected, you can debug a kernel the way you would debug an
  256. application program.
  257. </para>
  258. <para>
  259. If you are having problems connecting or something is going
  260. seriously wrong while debugging, it will most often be the case
  261. that you want to enable gdb to be verbose about its target
  262. communications. You do this prior to issuing the <constant>target
  263. remote</constant> command by typing in: <constant>set remote debug 1</constant>
  264. </para>
  265. </chapter>
  266. <chapter id="KGDBTestSuite">
  267. <title>kgdb Test Suite</title>
  268. <para>
  269. When kgdb is enabled in the kernel config you can also elect to
  270. enable the config parameter KGDB_TESTS. Turning this on will
  271. enable a special kgdb I/O module which is designed to test the
  272. kgdb internal functions.
  273. </para>
  274. <para>
  275. The kgdb tests are mainly intended for developers to test the kgdb
  276. internals as well as a tool for developing a new kgdb architecture
  277. specific implementation. These tests are not really for end users
  278. of the Linux kernel. The primary source of documentation would be
  279. to look in the drivers/misc/kgdbts.c file.
  280. </para>
  281. <para>
  282. The kgdb test suite can also be configured at compile time to run
  283. the core set of tests by setting the kernel config parameter
  284. KGDB_TESTS_ON_BOOT. This particular option is aimed at automated
  285. regression testing and does not require modifying the kernel boot
  286. config arguments. If this is turned on, the kgdb test suite can
  287. be disabled by specifying "kgdbts=" as a kernel boot argument.
  288. </para>
  289. </chapter>
  290. <chapter id="CommonBackEndReq">
  291. <title>KGDB Internals</title>
  292. <sect1 id="kgdbArchitecture">
  293. <title>Architecture Specifics</title>
  294. <para>
  295. Kgdb is organized into three basic components:
  296. <orderedlist>
  297. <listitem><para>kgdb core</para>
  298. <para>
  299. The kgdb core is found in kernel/kgdb.c. It contains:
  300. <itemizedlist>
  301. <listitem><para>All the logic to implement the gdb serial protocol</para></listitem>
  302. <listitem><para>A generic OS exception handler which includes sync'ing the processors into a stopped state on an multi cpu system.</para></listitem>
  303. <listitem><para>The API to talk to the kgdb I/O drivers</para></listitem>
  304. <listitem><para>The API to make calls to the arch specific kgdb implementation</para></listitem>
  305. <listitem><para>The logic to perform safe memory reads and writes to memory while using the debugger</para></listitem>
  306. <listitem><para>A full implementation for software breakpoints unless overridden by the arch</para></listitem>
  307. </itemizedlist>
  308. </para>
  309. </listitem>
  310. <listitem><para>kgdb arch specific implementation</para>
  311. <para>
  312. This implementation is generally found in arch/*/kernel/kgdb.c.
  313. As an example, arch/x86/kernel/kgdb.c contains the specifics to
  314. implement HW breakpoint as well as the initialization to
  315. dynamically register and unregister for the trap handlers on
  316. this architecture. The arch specific portion implements:
  317. <itemizedlist>
  318. <listitem><para>contains an arch specific trap catcher which
  319. invokes kgdb_handle_exception() to start kgdb about doing its
  320. work</para></listitem>
  321. <listitem><para>translation to and from gdb specific packet format to pt_regs</para></listitem>
  322. <listitem><para>Registration and unregistration of architecture specific trap hooks</para></listitem>
  323. <listitem><para>Any special exception handling and cleanup</para></listitem>
  324. <listitem><para>NMI exception handling and cleanup</para></listitem>
  325. <listitem><para>(optional)HW breakpoints</para></listitem>
  326. </itemizedlist>
  327. </para>
  328. </listitem>
  329. <listitem><para>kgdb I/O driver</para>
  330. <para>
  331. Each kgdb I/O driver has to provide an implemenation for the following:
  332. <itemizedlist>
  333. <listitem><para>configuration via builtin or module</para></listitem>
  334. <listitem><para>dynamic configuration and kgdb hook registration calls</para></listitem>
  335. <listitem><para>read and write character interface</para></listitem>
  336. <listitem><para>A cleanup handler for unconfiguring from the kgdb core</para></listitem>
  337. <listitem><para>(optional) Early debug methodology</para></listitem>
  338. </itemizedlist>
  339. Any given kgdb I/O driver has to operate very closely with the
  340. hardware and must do it in such a way that does not enable
  341. interrupts or change other parts of the system context without
  342. completely restoring them. The kgdb core will repeatedly "poll"
  343. a kgdb I/O driver for characters when it needs input. The I/O
  344. driver is expected to return immediately if there is no data
  345. available. Doing so allows for the future possibility to touch
  346. watch dog hardware in such a way as to have a target system not
  347. reset when these are enabled.
  348. </para>
  349. </listitem>
  350. </orderedlist>
  351. </para>
  352. <para>
  353. If you are intent on adding kgdb architecture specific support
  354. for a new architecture, the architecture should define
  355. <constant>HAVE_ARCH_KGDB</constant> in the architecture specific
  356. Kconfig file. This will enable kgdb for the architecture, and
  357. at that point you must create an architecture specific kgdb
  358. implementation.
  359. </para>
  360. <para>
  361. There are a few flags which must be set on every architecture in
  362. their &lt;asm/kgdb.h&gt; file. These are:
  363. <itemizedlist>
  364. <listitem>
  365. <para>
  366. NUMREGBYTES: The size in bytes of all of the registers, so
  367. that we can ensure they will all fit into a packet.
  368. </para>
  369. <para>
  370. BUFMAX: The size in bytes of the buffer GDB will read into.
  371. This must be larger than NUMREGBYTES.
  372. </para>
  373. <para>
  374. CACHE_FLUSH_IS_SAFE: Set to 1 if it is always safe to call
  375. flush_cache_range or flush_icache_range. On some architectures,
  376. these functions may not be safe to call on SMP since we keep other
  377. CPUs in a holding pattern.
  378. </para>
  379. </listitem>
  380. </itemizedlist>
  381. </para>
  382. <para>
  383. There are also the following functions for the common backend,
  384. found in kernel/kgdb.c, that must be supplied by the
  385. architecture-specific backend unless marked as (optional), in
  386. which case a default function maybe used if the architecture
  387. does not need to provide a specific implementation.
  388. </para>
  389. !Iinclude/linux/kgdb.h
  390. </sect1>
  391. <sect1 id="kgdbocDesign">
  392. <title>kgdboc internals</title>
  393. <para>
  394. The kgdboc driver is actually a very thin driver that relies on the
  395. underlying low level to the hardware driver having "polling hooks"
  396. which the to which the tty driver is attached. In the initial
  397. implementation of kgdboc it the serial_core was changed to expose a
  398. low level uart hook for doing polled mode reading and writing of a
  399. single character while in an atomic context. When kgdb makes an I/O
  400. request to the debugger, kgdboc invokes a call back in the serial
  401. core which in turn uses the call back in the uart driver. It is
  402. certainly possible to extend kgdboc to work with non-uart based
  403. consoles in the future.
  404. </para>
  405. <para>
  406. 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>
  407. #ifdef CONFIG_CONSOLE_POLL
  408. .poll_get_char = serial8250_get_poll_char,
  409. .poll_put_char = serial8250_put_poll_char,
  410. #endif
  411. </programlisting>
  412. Any implementation specifics around creating a polling driver use the
  413. <constant>#ifdef CONFIG_CONSOLE_POLL</constant>, as shown above.
  414. Keep in mind that polling hooks have to be implemented in such a way
  415. that they can be called from an atomic context and have to restore
  416. the state of the uart chip on return such that the system can return
  417. to normal when the debugger detaches. You need to be very careful
  418. with any kind of lock you consider, because failing here is most
  419. going to mean pressing the reset button.
  420. </para>
  421. </sect1>
  422. </chapter>
  423. <chapter id="credits">
  424. <title>Credits</title>
  425. <para>
  426. The following people have contributed to this document:
  427. <orderedlist>
  428. <listitem><para>Amit Kale<email>amitkale@linsyssoft.com</email></para></listitem>
  429. <listitem><para>Tom Rini<email>trini@kernel.crashing.org</email></para></listitem>
  430. </orderedlist>
  431. In March 2008 this document was completely rewritten by:
  432. <itemizedlist>
  433. <listitem><para>Jason Wessel<email>jason.wessel@windriver.com</email></para></listitem>
  434. </itemizedlist>
  435. </para>
  436. </chapter>
  437. </book>