debugobjects.tmpl 15 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="debug-objects-guide">
  5. <bookinfo>
  6. <title>Debug objects life time</title>
  7. <authorgroup>
  8. <author>
  9. <firstname>Thomas</firstname>
  10. <surname>Gleixner</surname>
  11. <affiliation>
  12. <address>
  13. <email>tglx@linutronix.de</email>
  14. </address>
  15. </affiliation>
  16. </author>
  17. </authorgroup>
  18. <copyright>
  19. <year>2008</year>
  20. <holder>Thomas Gleixner</holder>
  21. </copyright>
  22. <legalnotice>
  23. <para>
  24. This documentation is free software; you can redistribute
  25. it and/or modify it under the terms of the GNU General Public
  26. License version 2 as published by the Free Software Foundation.
  27. </para>
  28. <para>
  29. This program is distributed in the hope that it will be
  30. useful, but WITHOUT ANY WARRANTY; without even the implied
  31. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  32. See the GNU General Public License for more details.
  33. </para>
  34. <para>
  35. You should have received a copy of the GNU General Public
  36. License along with this program; if not, write to the Free
  37. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  38. MA 02111-1307 USA
  39. </para>
  40. <para>
  41. For more details see the file COPYING in the source
  42. distribution of Linux.
  43. </para>
  44. </legalnotice>
  45. </bookinfo>
  46. <toc></toc>
  47. <chapter id="intro">
  48. <title>Introduction</title>
  49. <para>
  50. debugobjects is a generic infrastructure to track the life time
  51. of kernel objects and validate the operations on those.
  52. </para>
  53. <para>
  54. debugobjects is useful to check for the following error patterns:
  55. <itemizedlist>
  56. <listitem><para>Activation of uninitialized objects</para></listitem>
  57. <listitem><para>Initialization of active objects</para></listitem>
  58. <listitem><para>Usage of freed/destroyed objects</para></listitem>
  59. </itemizedlist>
  60. </para>
  61. <para>
  62. debugobjects is not changing the data structure of the real
  63. object so it can be compiled in with a minimal runtime impact
  64. and enabled on demand with a kernel command line option.
  65. </para>
  66. </chapter>
  67. <chapter id="howto">
  68. <title>Howto use debugobjects</title>
  69. <para>
  70. A kernel subsystem needs to provide a data structure which
  71. describes the object type and add calls into the debug code at
  72. appropriate places. The data structure to describe the object
  73. type needs at minimum the name of the object type. Optional
  74. functions can and should be provided to fixup detected problems
  75. so the kernel can continue to work and the debug information can
  76. be retrieved from a live system instead of hard core debugging
  77. with serial consoles and stack trace transcripts from the
  78. monitor.
  79. </para>
  80. <para>
  81. The debug calls provided by debugobjects are:
  82. <itemizedlist>
  83. <listitem><para>debug_object_init</para></listitem>
  84. <listitem><para>debug_object_init_on_stack</para></listitem>
  85. <listitem><para>debug_object_activate</para></listitem>
  86. <listitem><para>debug_object_deactivate</para></listitem>
  87. <listitem><para>debug_object_destroy</para></listitem>
  88. <listitem><para>debug_object_free</para></listitem>
  89. <listitem><para>debug_object_assert_init</para></listitem>
  90. </itemizedlist>
  91. Each of these functions takes the address of the real object and
  92. a pointer to the object type specific debug description
  93. structure.
  94. </para>
  95. <para>
  96. Each detected error is reported in the statistics and a limited
  97. number of errors are printk'ed including a full stack trace.
  98. </para>
  99. <para>
  100. The statistics are available via /sys/kernel/debug/debug_objects/stats.
  101. They provide information about the number of warnings and the
  102. number of successful fixups along with information about the
  103. usage of the internal tracking objects and the state of the
  104. internal tracking objects pool.
  105. </para>
  106. </chapter>
  107. <chapter id="debugfunctions">
  108. <title>Debug functions</title>
  109. <sect1 id="prototypes">
  110. <title>Debug object function reference</title>
  111. !Elib/debugobjects.c
  112. </sect1>
  113. <sect1 id="debug_object_init">
  114. <title>debug_object_init</title>
  115. <para>
  116. This function is called whenever the initialization function
  117. of a real object is called.
  118. </para>
  119. <para>
  120. When the real object is already tracked by debugobjects it is
  121. checked, whether the object can be initialized. Initializing
  122. is not allowed for active and destroyed objects. When
  123. debugobjects detects an error, then it calls the fixup_init
  124. function of the object type description structure if provided
  125. by the caller. The fixup function can correct the problem
  126. before the real initialization of the object happens. E.g. it
  127. can deactivate an active object in order to prevent damage to
  128. the subsystem.
  129. </para>
  130. <para>
  131. When the real object is not yet tracked by debugobjects,
  132. debugobjects allocates a tracker object for the real object
  133. and sets the tracker object state to ODEBUG_STATE_INIT. It
  134. verifies that the object is not on the callers stack. If it is
  135. on the callers stack then a limited number of warnings
  136. including a full stack trace is printk'ed. The calling code
  137. must use debug_object_init_on_stack() and remove the object
  138. before leaving the function which allocated it. See next
  139. section.
  140. </para>
  141. </sect1>
  142. <sect1 id="debug_object_init_on_stack">
  143. <title>debug_object_init_on_stack</title>
  144. <para>
  145. This function is called whenever the initialization function
  146. of a real object which resides on the stack is called.
  147. </para>
  148. <para>
  149. When the real object is already tracked by debugobjects it is
  150. checked, whether the object can be initialized. Initializing
  151. is not allowed for active and destroyed objects. When
  152. debugobjects detects an error, then it calls the fixup_init
  153. function of the object type description structure if provided
  154. by the caller. The fixup function can correct the problem
  155. before the real initialization of the object happens. E.g. it
  156. can deactivate an active object in order to prevent damage to
  157. the subsystem.
  158. </para>
  159. <para>
  160. When the real object is not yet tracked by debugobjects
  161. debugobjects allocates a tracker object for the real object
  162. and sets the tracker object state to ODEBUG_STATE_INIT. It
  163. verifies that the object is on the callers stack.
  164. </para>
  165. <para>
  166. An object which is on the stack must be removed from the
  167. tracker by calling debug_object_free() before the function
  168. which allocates the object returns. Otherwise we keep track of
  169. stale objects.
  170. </para>
  171. </sect1>
  172. <sect1 id="debug_object_activate">
  173. <title>debug_object_activate</title>
  174. <para>
  175. This function is called whenever the activation function of a
  176. real object is called.
  177. </para>
  178. <para>
  179. When the real object is already tracked by debugobjects it is
  180. checked, whether the object can be activated. Activating is
  181. not allowed for active and destroyed objects. When
  182. debugobjects detects an error, then it calls the
  183. fixup_activate function of the object type description
  184. structure if provided by the caller. The fixup function can
  185. correct the problem before the real activation of the object
  186. happens. E.g. it can deactivate an active object in order to
  187. prevent damage to the subsystem.
  188. </para>
  189. <para>
  190. When the real object is not yet tracked by debugobjects then
  191. the fixup_activate function is called if available. This is
  192. necessary to allow the legitimate activation of statically
  193. allocated and initialized objects. The fixup function checks
  194. whether the object is valid and calls the debug_objects_init()
  195. function to initialize the tracking of this object.
  196. </para>
  197. <para>
  198. When the activation is legitimate, then the state of the
  199. associated tracker object is set to ODEBUG_STATE_ACTIVE.
  200. </para>
  201. </sect1>
  202. <sect1 id="debug_object_deactivate">
  203. <title>debug_object_deactivate</title>
  204. <para>
  205. This function is called whenever the deactivation function of
  206. a real object is called.
  207. </para>
  208. <para>
  209. When the real object is tracked by debugobjects it is checked,
  210. whether the object can be deactivated. Deactivating is not
  211. allowed for untracked or destroyed objects.
  212. </para>
  213. <para>
  214. When the deactivation is legitimate, then the state of the
  215. associated tracker object is set to ODEBUG_STATE_INACTIVE.
  216. </para>
  217. </sect1>
  218. <sect1 id="debug_object_destroy">
  219. <title>debug_object_destroy</title>
  220. <para>
  221. This function is called to mark an object destroyed. This is
  222. useful to prevent the usage of invalid objects, which are
  223. still available in memory: either statically allocated objects
  224. or objects which are freed later.
  225. </para>
  226. <para>
  227. When the real object is tracked by debugobjects it is checked,
  228. whether the object can be destroyed. Destruction is not
  229. allowed for active and destroyed objects. When debugobjects
  230. detects an error, then it calls the fixup_destroy function of
  231. the object type description structure if provided by the
  232. caller. The fixup function can correct the problem before the
  233. real destruction of the object happens. E.g. it can deactivate
  234. an active object in order to prevent damage to the subsystem.
  235. </para>
  236. <para>
  237. When the destruction is legitimate, then the state of the
  238. associated tracker object is set to ODEBUG_STATE_DESTROYED.
  239. </para>
  240. </sect1>
  241. <sect1 id="debug_object_free">
  242. <title>debug_object_free</title>
  243. <para>
  244. This function is called before an object is freed.
  245. </para>
  246. <para>
  247. When the real object is tracked by debugobjects it is checked,
  248. whether the object can be freed. Free is not allowed for
  249. active objects. When debugobjects detects an error, then it
  250. calls the fixup_free function of the object type description
  251. structure if provided by the caller. The fixup function can
  252. correct the problem before the real free of the object
  253. happens. E.g. it can deactivate an active object in order to
  254. prevent damage to the subsystem.
  255. </para>
  256. <para>
  257. Note that debug_object_free removes the object from the
  258. tracker. Later usage of the object is detected by the other
  259. debug checks.
  260. </para>
  261. </sect1>
  262. <sect1 id="debug_object_assert_init">
  263. <title>debug_object_assert_init</title>
  264. <para>
  265. This function is called to assert that an object has been
  266. initialized.
  267. </para>
  268. <para>
  269. When the real object is not tracked by debugobjects, it calls
  270. fixup_assert_init of the object type description structure
  271. provided by the caller, with the hardcoded object state
  272. ODEBUG_NOT_AVAILABLE. The fixup function can correct the problem
  273. by calling debug_object_init and other specific initializing
  274. functions.
  275. </para>
  276. <para>
  277. When the real object is already tracked by debugobjects it is
  278. ignored.
  279. </para>
  280. </sect1>
  281. </chapter>
  282. <chapter id="fixupfunctions">
  283. <title>Fixup functions</title>
  284. <sect1 id="debug_obj_descr">
  285. <title>Debug object type description structure</title>
  286. !Iinclude/linux/debugobjects.h
  287. </sect1>
  288. <sect1 id="fixup_init">
  289. <title>fixup_init</title>
  290. <para>
  291. This function is called from the debug code whenever a problem
  292. in debug_object_init is detected. The function takes the
  293. address of the object and the state which is currently
  294. recorded in the tracker.
  295. </para>
  296. <para>
  297. Called from debug_object_init when the object state is:
  298. <itemizedlist>
  299. <listitem><para>ODEBUG_STATE_ACTIVE</para></listitem>
  300. </itemizedlist>
  301. </para>
  302. <para>
  303. The function returns 1 when the fixup was successful,
  304. otherwise 0. The return value is used to update the
  305. statistics.
  306. </para>
  307. <para>
  308. Note, that the function needs to call the debug_object_init()
  309. function again, after the damage has been repaired in order to
  310. keep the state consistent.
  311. </para>
  312. </sect1>
  313. <sect1 id="fixup_activate">
  314. <title>fixup_activate</title>
  315. <para>
  316. This function is called from the debug code whenever a problem
  317. in debug_object_activate is detected.
  318. </para>
  319. <para>
  320. Called from debug_object_activate when the object state is:
  321. <itemizedlist>
  322. <listitem><para>ODEBUG_STATE_NOTAVAILABLE</para></listitem>
  323. <listitem><para>ODEBUG_STATE_ACTIVE</para></listitem>
  324. </itemizedlist>
  325. </para>
  326. <para>
  327. The function returns 1 when the fixup was successful,
  328. otherwise 0. The return value is used to update the
  329. statistics.
  330. </para>
  331. <para>
  332. Note that the function needs to call the debug_object_activate()
  333. function again after the damage has been repaired in order to
  334. keep the state consistent.
  335. </para>
  336. <para>
  337. The activation of statically initialized objects is a special
  338. case. When debug_object_activate() has no tracked object for
  339. this object address then fixup_activate() is called with
  340. object state ODEBUG_STATE_NOTAVAILABLE. The fixup function
  341. needs to check whether this is a legitimate case of a
  342. statically initialized object or not. In case it is it calls
  343. debug_object_init() and debug_object_activate() to make the
  344. object known to the tracker and marked active. In this case
  345. the function should return 0 because this is not a real fixup.
  346. </para>
  347. </sect1>
  348. <sect1 id="fixup_destroy">
  349. <title>fixup_destroy</title>
  350. <para>
  351. This function is called from the debug code whenever a problem
  352. in debug_object_destroy is detected.
  353. </para>
  354. <para>
  355. Called from debug_object_destroy when the object state is:
  356. <itemizedlist>
  357. <listitem><para>ODEBUG_STATE_ACTIVE</para></listitem>
  358. </itemizedlist>
  359. </para>
  360. <para>
  361. The function returns 1 when the fixup was successful,
  362. otherwise 0. The return value is used to update the
  363. statistics.
  364. </para>
  365. </sect1>
  366. <sect1 id="fixup_free">
  367. <title>fixup_free</title>
  368. <para>
  369. This function is called from the debug code whenever a problem
  370. in debug_object_free is detected. Further it can be called
  371. from the debug checks in kfree/vfree, when an active object is
  372. detected from the debug_check_no_obj_freed() sanity checks.
  373. </para>
  374. <para>
  375. Called from debug_object_free() or debug_check_no_obj_freed()
  376. when the object state is:
  377. <itemizedlist>
  378. <listitem><para>ODEBUG_STATE_ACTIVE</para></listitem>
  379. </itemizedlist>
  380. </para>
  381. <para>
  382. The function returns 1 when the fixup was successful,
  383. otherwise 0. The return value is used to update the
  384. statistics.
  385. </para>
  386. </sect1>
  387. <sect1 id="fixup_assert_init">
  388. <title>fixup_assert_init</title>
  389. <para>
  390. This function is called from the debug code whenever a problem
  391. in debug_object_assert_init is detected.
  392. </para>
  393. <para>
  394. Called from debug_object_assert_init() with a hardcoded state
  395. ODEBUG_STATE_NOTAVAILABLE when the object is not found in the
  396. debug bucket.
  397. </para>
  398. <para>
  399. The function returns 1 when the fixup was successful,
  400. otherwise 0. The return value is used to update the
  401. statistics.
  402. </para>
  403. <para>
  404. Note, this function should make sure debug_object_init() is
  405. called before returning.
  406. </para>
  407. <para>
  408. The handling of statically initialized objects is a special
  409. case. The fixup function should check if this is a legitimate
  410. case of a statically initialized object or not. In this case only
  411. debug_object_init() should be called to make the object known to
  412. the tracker. Then the function should return 0 because this is not
  413. a real fixup.
  414. </para>
  415. </sect1>
  416. </chapter>
  417. <chapter id="bugs">
  418. <title>Known Bugs And Assumptions</title>
  419. <para>
  420. None (knock on wood).
  421. </para>
  422. </chapter>
  423. </book>