/kern_oII/Documentation/DocBook/kgdb.tmpl

http://omnia2droid.googlecode.com/ · Go Template · 459 lines · 449 code · 10 blank · 0 comment · 0 complexity · a544c0bb0a18c80daf391f839457ccc9 MD5 · raw file

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