debugobjects.tmpl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. </itemizedlist>
  90. Each of these functions takes the address of the real object and
  91. a pointer to the object type specific debug description
  92. structure.
  93. </para>
  94. <para>
  95. Each detected error is reported in the statistics and a limited
  96. number of errors are printk'ed including a full stack trace.
  97. </para>
  98. <para>
  99. The statistics are available via /sys/kernel/debug/debug_objects/stats.
  100. They provide information about the number of warnings and the
  101. number of successful fixups along with information about the
  102. usage of the internal tracking objects and the state of the
  103. internal tracking objects pool.
  104. </para>
  105. </chapter>
  106. <chapter id="debugfunctions">
  107. <title>Debug functions</title>
  108. <sect1 id="prototypes">
  109. <title>Debug object function reference</title>
  110. !Elib/debugobjects.c
  111. </sect1>
  112. <sect1 id="debug_object_init">
  113. <title>debug_object_init</title>
  114. <para>
  115. This function is called whenever the initialization function
  116. of a real object is called.
  117. </para>
  118. <para>
  119. When the real object is already tracked by debugobjects it is
  120. checked, whether the object can be initialized. Initializing
  121. is not allowed for active and destroyed objects. When
  122. debugobjects detects an error, then it calls the fixup_init
  123. function of the object type description structure if provided
  124. by the caller. The fixup function can correct the problem
  125. before the real initialization of the object happens. E.g. it
  126. can deactivate an active object in order to prevent damage to
  127. the subsystem.
  128. </para>
  129. <para>
  130. When the real object is not yet tracked by debugobjects,
  131. debugobjects allocates a tracker object for the real object
  132. and sets the tracker object state to ODEBUG_STATE_INIT. It
  133. verifies that the object is not on the callers stack. If it is
  134. on the callers stack then a limited number of warnings
  135. including a full stack trace is printk'ed. The calling code
  136. must use debug_object_init_on_stack() and remove the object
  137. before leaving the function which allocated it. See next
  138. section.
  139. </para>
  140. </sect1>
  141. <sect1 id="debug_object_init_on_stack">
  142. <title>debug_object_init_on_stack</title>
  143. <para>
  144. This function is called whenever the initialization function
  145. of a real object which resides on the stack is called.
  146. </para>
  147. <para>
  148. When the real object is already tracked by debugobjects it is
  149. checked, whether the object can be initialized. Initializing
  150. is not allowed for active and destroyed objects. When
  151. debugobjects detects an error, then it calls the fixup_init
  152. function of the object type description structure if provided
  153. by the caller. The fixup function can correct the problem
  154. before the real initialization of the object happens. E.g. it
  155. can deactivate an active object in order to prevent damage to
  156. the subsystem.
  157. </para>
  158. <para>
  159. When the real object is not yet tracked by debugobjects
  160. debugobjects allocates a tracker object for the real object
  161. and sets the tracker object state to ODEBUG_STATE_INIT. It
  162. verifies that the object is on the callers stack.
  163. </para>
  164. <para>
  165. An object which is on the stack must be removed from the
  166. tracker by calling debug_object_free() before the function
  167. which allocates the object returns. Otherwise we keep track of
  168. stale objects.
  169. </para>
  170. </sect1>
  171. <sect1 id="debug_object_activate">
  172. <title>debug_object_activate</title>
  173. <para>
  174. This function is called whenever the activation function of a
  175. real object is called.
  176. </para>
  177. <para>
  178. When the real object is already tracked by debugobjects it is
  179. checked, whether the object can be activated. Activating is
  180. not allowed for active and destroyed objects. When
  181. debugobjects detects an error, then it calls the
  182. fixup_activate function of the object type description
  183. structure if provided by the caller. The fixup function can
  184. correct the problem before the real activation of the object
  185. happens. E.g. it can deactivate an active object in order to
  186. prevent damage to the subsystem.
  187. </para>
  188. <para>
  189. When the real object is not yet tracked by debugobjects then
  190. the fixup_activate function is called if available. This is
  191. necessary to allow the legitimate activation of statically
  192. allocated and initialized objects. The fixup function checks
  193. whether the object is valid and calls the debug_objects_init()
  194. function to initialize the tracking of this object.
  195. </para>
  196. <para>
  197. When the activation is legitimate, then the state of the
  198. associated tracker object is set to ODEBUG_STATE_ACTIVE.
  199. </para>
  200. </sect1>
  201. <sect1 id="debug_object_deactivate">
  202. <title>debug_object_deactivate</title>
  203. <para>
  204. This function is called whenever the deactivation function of
  205. a real object is called.
  206. </para>
  207. <para>
  208. When the real object is tracked by debugobjects it is checked,
  209. whether the object can be deactivated. Deactivating is not
  210. allowed for untracked or destroyed objects.
  211. </para>
  212. <para>
  213. When the deactivation is legitimate, then the state of the
  214. associated tracker object is set to ODEBUG_STATE_INACTIVE.
  215. </para>
  216. </sect1>
  217. <sect1 id="debug_object_destroy">
  218. <title>debug_object_destroy</title>
  219. <para>
  220. This function is called to mark an object destroyed. This is
  221. useful to prevent the usage of invalid objects, which are
  222. still available in memory: either statically allocated objects
  223. or objects which are freed later.
  224. </para>
  225. <para>
  226. When the real object is tracked by debugobjects it is checked,
  227. whether the object can be destroyed. Destruction is not
  228. allowed for active and destroyed objects. When debugobjects
  229. detects an error, then it calls the fixup_destroy function of
  230. the object type description structure if provided by the
  231. caller. The fixup function can correct the problem before the
  232. real destruction of the object happens. E.g. it can deactivate
  233. an active object in order to prevent damage to the subsystem.
  234. </para>
  235. <para>
  236. When the destruction is legitimate, then the state of the
  237. associated tracker object is set to ODEBUG_STATE_DESTROYED.
  238. </para>
  239. </sect1>
  240. <sect1 id="debug_object_free">
  241. <title>debug_object_free</title>
  242. <para>
  243. This function is called before an object is freed.
  244. </para>
  245. <para>
  246. When the real object is tracked by debugobjects it is checked,
  247. whether the object can be freed. Free is not allowed for
  248. active objects. When debugobjects detects an error, then it
  249. calls the fixup_free function of the object type description
  250. structure if provided by the caller. The fixup function can
  251. correct the problem before the real free of the object
  252. happens. E.g. it can deactivate an active object in order to
  253. prevent damage to the subsystem.
  254. </para>
  255. <para>
  256. Note that debug_object_free removes the object from the
  257. tracker. Later usage of the object is detected by the other
  258. debug checks.
  259. </para>
  260. </sect1>
  261. </chapter>
  262. <chapter id="fixupfunctions">
  263. <title>Fixup functions</title>
  264. <sect1 id="debug_obj_descr">
  265. <title>Debug object type description structure</title>
  266. !Iinclude/linux/debugobjects.h
  267. </sect1>
  268. <sect1 id="fixup_init">
  269. <title>fixup_init</title>
  270. <para>
  271. This function is called from the debug code whenever a problem
  272. in debug_object_init is detected. The function takes the
  273. address of the object and the state which is currently
  274. recorded in the tracker.
  275. </para>
  276. <para>
  277. Called from debug_object_init when the object state is:
  278. <itemizedlist>
  279. <listitem><para>ODEBUG_STATE_ACTIVE</para></listitem>
  280. </itemizedlist>
  281. </para>
  282. <para>
  283. The function returns 1 when the fixup was successful,
  284. otherwise 0. The return value is used to update the
  285. statistics.
  286. </para>
  287. <para>
  288. Note, that the function needs to call the debug_object_init()
  289. function again, after the damage has been repaired in order to
  290. keep the state consistent.
  291. </para>
  292. </sect1>
  293. <sect1 id="fixup_activate">
  294. <title>fixup_activate</title>
  295. <para>
  296. This function is called from the debug code whenever a problem
  297. in debug_object_activate is detected.
  298. </para>
  299. <para>
  300. Called from debug_object_activate when the object state is:
  301. <itemizedlist>
  302. <listitem><para>ODEBUG_STATE_NOTAVAILABLE</para></listitem>
  303. <listitem><para>ODEBUG_STATE_ACTIVE</para></listitem>
  304. </itemizedlist>
  305. </para>
  306. <para>
  307. The function returns 1 when the fixup was successful,
  308. otherwise 0. The return value is used to update the
  309. statistics.
  310. </para>
  311. <para>
  312. Note that the function needs to call the debug_object_activate()
  313. function again after the damage has been repaired in order to
  314. keep the state consistent.
  315. </para>
  316. <para>
  317. The activation of statically initialized objects is a special
  318. case. When debug_object_activate() has no tracked object for
  319. this object address then fixup_activate() is called with
  320. object state ODEBUG_STATE_NOTAVAILABLE. The fixup function
  321. needs to check whether this is a legitimate case of a
  322. statically initialized object or not. In case it is it calls
  323. debug_object_init() and debug_object_activate() to make the
  324. object known to the tracker and marked active. In this case
  325. the function should return 0 because this is not a real fixup.
  326. </para>
  327. </sect1>
  328. <sect1 id="fixup_destroy">
  329. <title>fixup_destroy</title>
  330. <para>
  331. This function is called from the debug code whenever a problem
  332. in debug_object_destroy is detected.
  333. </para>
  334. <para>
  335. Called from debug_object_destroy when the object state is:
  336. <itemizedlist>
  337. <listitem><para>ODEBUG_STATE_ACTIVE</para></listitem>
  338. </itemizedlist>
  339. </para>
  340. <para>
  341. The function returns 1 when the fixup was successful,
  342. otherwise 0. The return value is used to update the
  343. statistics.
  344. </para>
  345. </sect1>
  346. <sect1 id="fixup_free">
  347. <title>fixup_free</title>
  348. <para>
  349. This function is called from the debug code whenever a problem
  350. in debug_object_free is detected. Further it can be called
  351. from the debug checks in kfree/vfree, when an active object is
  352. detected from the debug_check_no_obj_freed() sanity checks.
  353. </para>
  354. <para>
  355. Called from debug_object_free() or debug_check_no_obj_freed()
  356. when the object state is:
  357. <itemizedlist>
  358. <listitem><para>ODEBUG_STATE_ACTIVE</para></listitem>
  359. </itemizedlist>
  360. </para>
  361. <para>
  362. The function returns 1 when the fixup was successful,
  363. otherwise 0. The return value is used to update the
  364. statistics.
  365. </para>
  366. </sect1>
  367. </chapter>
  368. <chapter id="bugs">
  369. <title>Known Bugs And Assumptions</title>
  370. <para>
  371. None (knock on wood).
  372. </para>
  373. </chapter>
  374. </book>