kgdb.tmpl 18 KB

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