dynamic-debug-howto.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. Dynamic debug
  2. +++++++++++++
  3. Introduction
  4. ============
  5. This document describes how to use the dynamic debug (dyndbg) feature.
  6. Dynamic debug is designed to allow you to dynamically enable/disable
  7. kernel code to obtain additional kernel information. Currently, if
  8. ``CONFIG_DYNAMIC_DEBUG`` is set, then all ``pr_debug()``/``dev_dbg()`` and
  9. ``print_hex_dump_debug()``/``print_hex_dump_bytes()`` calls can be dynamically
  10. enabled per-callsite.
  11. If ``CONFIG_DYNAMIC_DEBUG`` is not set, ``print_hex_dump_debug()`` is just
  12. shortcut for ``print_hex_dump(KERN_DEBUG)``.
  13. For ``print_hex_dump_debug()``/``print_hex_dump_bytes()``, format string is
  14. its ``prefix_str`` argument, if it is constant string; or ``hexdump``
  15. in case ``prefix_str`` is built dynamically.
  16. Dynamic debug has even more useful features:
  17. * Simple query language allows turning on and off debugging
  18. statements by matching any combination of 0 or 1 of:
  19. - source filename
  20. - function name
  21. - line number (including ranges of line numbers)
  22. - module name
  23. - format string
  24. * Provides a debugfs control file: ``<debugfs>/dynamic_debug/control``
  25. which can be read to display the complete list of known debug
  26. statements, to help guide you
  27. Controlling dynamic debug Behaviour
  28. ===================================
  29. The behaviour of ``pr_debug()``/``dev_dbg()`` are controlled via writing to a
  30. control file in the 'debugfs' filesystem. Thus, you must first mount
  31. the debugfs filesystem, in order to make use of this feature.
  32. Subsequently, we refer to the control file as:
  33. ``<debugfs>/dynamic_debug/control``. For example, if you want to enable
  34. printing from source file ``svcsock.c``, line 1603 you simply do::
  35. nullarbor:~ # echo 'file svcsock.c line 1603 +p' >
  36. <debugfs>/dynamic_debug/control
  37. If you make a mistake with the syntax, the write will fail thus::
  38. nullarbor:~ # echo 'file svcsock.c wtf 1 +p' >
  39. <debugfs>/dynamic_debug/control
  40. -bash: echo: write error: Invalid argument
  41. Viewing Dynamic Debug Behaviour
  42. ===============================
  43. You can view the currently configured behaviour of all the debug
  44. statements via::
  45. nullarbor:~ # cat <debugfs>/dynamic_debug/control
  46. # filename:lineno [module]function flags format
  47. /usr/src/packages/BUILD/sgi-enhancednfs-1.4/default/net/sunrpc/svc_rdma.c:323 [svcxprt_rdma]svc_rdma_cleanup =_ "SVCRDMA Module Removed, deregister RPC RDMA transport\012"
  48. /usr/src/packages/BUILD/sgi-enhancednfs-1.4/default/net/sunrpc/svc_rdma.c:341 [svcxprt_rdma]svc_rdma_init =_ "\011max_inline : %d\012"
  49. /usr/src/packages/BUILD/sgi-enhancednfs-1.4/default/net/sunrpc/svc_rdma.c:340 [svcxprt_rdma]svc_rdma_init =_ "\011sq_depth : %d\012"
  50. /usr/src/packages/BUILD/sgi-enhancednfs-1.4/default/net/sunrpc/svc_rdma.c:338 [svcxprt_rdma]svc_rdma_init =_ "\011max_requests : %d\012"
  51. ...
  52. You can also apply standard Unix text manipulation filters to this
  53. data, e.g.::
  54. nullarbor:~ # grep -i rdma <debugfs>/dynamic_debug/control | wc -l
  55. 62
  56. nullarbor:~ # grep -i tcp <debugfs>/dynamic_debug/control | wc -l
  57. 42
  58. The third column shows the currently enabled flags for each debug
  59. statement callsite (see below for definitions of the flags). The
  60. default value, with no flags enabled, is ``=_``. So you can view all
  61. the debug statement callsites with any non-default flags::
  62. nullarbor:~ # awk '$3 != "=_"' <debugfs>/dynamic_debug/control
  63. # filename:lineno [module]function flags format
  64. /usr/src/packages/BUILD/sgi-enhancednfs-1.4/default/net/sunrpc/svcsock.c:1603 [sunrpc]svc_send p "svc_process: st_sendto returned %d\012"
  65. Command Language Reference
  66. ==========================
  67. At the lexical level, a command comprises a sequence of words separated
  68. by spaces or tabs. So these are all equivalent::
  69. nullarbor:~ # echo -n 'file svcsock.c line 1603 +p' >
  70. <debugfs>/dynamic_debug/control
  71. nullarbor:~ # echo -n ' file svcsock.c line 1603 +p ' >
  72. <debugfs>/dynamic_debug/control
  73. nullarbor:~ # echo -n 'file svcsock.c line 1603 +p' >
  74. <debugfs>/dynamic_debug/control
  75. Command submissions are bounded by a write() system call.
  76. Multiple commands can be written together, separated by ``;`` or ``\n``::
  77. ~# echo "func pnpacpi_get_resources +p; func pnp_assign_mem +p" \
  78. > <debugfs>/dynamic_debug/control
  79. If your query set is big, you can batch them too::
  80. ~# cat query-batch-file > <debugfs>/dynamic_debug/control
  81. A another way is to use wildcard. The match rule support ``*`` (matches
  82. zero or more characters) and ``?`` (matches exactly one character).For
  83. example, you can match all usb drivers::
  84. ~# echo "file drivers/usb/* +p" > <debugfs>/dynamic_debug/control
  85. At the syntactical level, a command comprises a sequence of match
  86. specifications, followed by a flags change specification::
  87. command ::= match-spec* flags-spec
  88. The match-spec's are used to choose a subset of the known pr_debug()
  89. callsites to which to apply the flags-spec. Think of them as a query
  90. with implicit ANDs between each pair. Note that an empty list of
  91. match-specs will select all debug statement callsites.
  92. A match specification comprises a keyword, which controls the
  93. attribute of the callsite to be compared, and a value to compare
  94. against. Possible keywords are:::
  95. match-spec ::= 'func' string |
  96. 'file' string |
  97. 'module' string |
  98. 'format' string |
  99. 'line' line-range
  100. line-range ::= lineno |
  101. '-'lineno |
  102. lineno'-' |
  103. lineno'-'lineno
  104. lineno ::= unsigned-int
  105. .. note::
  106. ``line-range`` cannot contain space, e.g.
  107. "1-30" is valid range but "1 - 30" is not.
  108. The meanings of each keyword are:
  109. func
  110. The given string is compared against the function name
  111. of each callsite. Example::
  112. func svc_tcp_accept
  113. file
  114. The given string is compared against either the full pathname, the
  115. src-root relative pathname, or the basename of the source file of
  116. each callsite. Examples::
  117. file svcsock.c
  118. file kernel/freezer.c
  119. file /usr/src/packages/BUILD/sgi-enhancednfs-1.4/default/net/sunrpc/svcsock.c
  120. module
  121. The given string is compared against the module name
  122. of each callsite. The module name is the string as
  123. seen in ``lsmod``, i.e. without the directory or the ``.ko``
  124. suffix and with ``-`` changed to ``_``. Examples::
  125. module sunrpc
  126. module nfsd
  127. format
  128. The given string is searched for in the dynamic debug format
  129. string. Note that the string does not need to match the
  130. entire format, only some part. Whitespace and other
  131. special characters can be escaped using C octal character
  132. escape ``\ooo`` notation, e.g. the space character is ``\040``.
  133. Alternatively, the string can be enclosed in double quote
  134. characters (``"``) or single quote characters (``'``).
  135. Examples::
  136. format svcrdma: // many of the NFS/RDMA server pr_debugs
  137. format readahead // some pr_debugs in the readahead cache
  138. format nfsd:\040SETATTR // one way to match a format with whitespace
  139. format "nfsd: SETATTR" // a neater way to match a format with whitespace
  140. format 'nfsd: SETATTR' // yet another way to match a format with whitespace
  141. line
  142. The given line number or range of line numbers is compared
  143. against the line number of each ``pr_debug()`` callsite. A single
  144. line number matches the callsite line number exactly. A
  145. range of line numbers matches any callsite between the first
  146. and last line number inclusive. An empty first number means
  147. the first line in the file, an empty last line number means the
  148. last line number in the file. Examples::
  149. line 1603 // exactly line 1603
  150. line 1600-1605 // the six lines from line 1600 to line 1605
  151. line -1605 // the 1605 lines from line 1 to line 1605
  152. line 1600- // all lines from line 1600 to the end of the file
  153. The flags specification comprises a change operation followed
  154. by one or more flag characters. The change operation is one
  155. of the characters::
  156. - remove the given flags
  157. + add the given flags
  158. = set the flags to the given flags
  159. The flags are::
  160. p enables the pr_debug() callsite.
  161. f Include the function name in the printed message
  162. l Include line number in the printed message
  163. m Include module name in the printed message
  164. t Include thread ID in messages not generated from interrupt context
  165. _ No flags are set. (Or'd with others on input)
  166. For ``print_hex_dump_debug()`` and ``print_hex_dump_bytes()``, only ``p`` flag
  167. have meaning, other flags ignored.
  168. For display, the flags are preceded by ``=``
  169. (mnemonic: what the flags are currently equal to).
  170. Note the regexp ``^[-+=][flmpt_]+$`` matches a flags specification.
  171. To clear all flags at once, use ``=_`` or ``-flmpt``.
  172. Debug messages during Boot Process
  173. ==================================
  174. To activate debug messages for core code and built-in modules during
  175. the boot process, even before userspace and debugfs exists, use
  176. ``dyndbg="QUERY"``, ``module.dyndbg="QUERY"``, or ``ddebug_query="QUERY"``
  177. (``ddebug_query`` is obsoleted by ``dyndbg``, and deprecated). QUERY follows
  178. the syntax described above, but must not exceed 1023 characters. Your
  179. bootloader may impose lower limits.
  180. These ``dyndbg`` params are processed just after the ddebug tables are
  181. processed, as part of the arch_initcall. Thus you can enable debug
  182. messages in all code run after this arch_initcall via this boot
  183. parameter.
  184. On an x86 system for example ACPI enablement is a subsys_initcall and::
  185. dyndbg="file ec.c +p"
  186. will show early Embedded Controller transactions during ACPI setup if
  187. your machine (typically a laptop) has an Embedded Controller.
  188. PCI (or other devices) initialization also is a hot candidate for using
  189. this boot parameter for debugging purposes.
  190. If ``foo`` module is not built-in, ``foo.dyndbg`` will still be processed at
  191. boot time, without effect, but will be reprocessed when module is
  192. loaded later. ``dyndbg_query=`` and bare ``dyndbg=`` are only processed at
  193. boot.
  194. Debug Messages at Module Initialization Time
  195. ============================================
  196. When ``modprobe foo`` is called, modprobe scans ``/proc/cmdline`` for
  197. ``foo.params``, strips ``foo.``, and passes them to the kernel along with
  198. params given in modprobe args or ``/etc/modprob.d/*.conf`` files,
  199. in the following order:
  200. 1. parameters given via ``/etc/modprobe.d/*.conf``::
  201. options foo dyndbg=+pt
  202. options foo dyndbg # defaults to +p
  203. 2. ``foo.dyndbg`` as given in boot args, ``foo.`` is stripped and passed::
  204. foo.dyndbg=" func bar +p; func buz +mp"
  205. 3. args to modprobe::
  206. modprobe foo dyndbg==pmf # override previous settings
  207. These ``dyndbg`` queries are applied in order, with last having final say.
  208. This allows boot args to override or modify those from ``/etc/modprobe.d``
  209. (sensible, since 1 is system wide, 2 is kernel or boot specific), and
  210. modprobe args to override both.
  211. In the ``foo.dyndbg="QUERY"`` form, the query must exclude ``module foo``.
  212. ``foo`` is extracted from the param-name, and applied to each query in
  213. ``QUERY``, and only 1 match-spec of each type is allowed.
  214. The ``dyndbg`` option is a "fake" module parameter, which means:
  215. - modules do not need to define it explicitly
  216. - every module gets it tacitly, whether they use pr_debug or not
  217. - it doesn't appear in ``/sys/module/$module/parameters/``
  218. To see it, grep the control file, or inspect ``/proc/cmdline.``
  219. For ``CONFIG_DYNAMIC_DEBUG`` kernels, any settings given at boot-time (or
  220. enabled by ``-DDEBUG`` flag during compilation) can be disabled later via
  221. the sysfs interface if the debug messages are no longer needed::
  222. echo "module module_name -p" > <debugfs>/dynamic_debug/control
  223. Examples
  224. ========
  225. ::
  226. // enable the message at line 1603 of file svcsock.c
  227. nullarbor:~ # echo -n 'file svcsock.c line 1603 +p' >
  228. <debugfs>/dynamic_debug/control
  229. // enable all the messages in file svcsock.c
  230. nullarbor:~ # echo -n 'file svcsock.c +p' >
  231. <debugfs>/dynamic_debug/control
  232. // enable all the messages in the NFS server module
  233. nullarbor:~ # echo -n 'module nfsd +p' >
  234. <debugfs>/dynamic_debug/control
  235. // enable all 12 messages in the function svc_process()
  236. nullarbor:~ # echo -n 'func svc_process +p' >
  237. <debugfs>/dynamic_debug/control
  238. // disable all 12 messages in the function svc_process()
  239. nullarbor:~ # echo -n 'func svc_process -p' >
  240. <debugfs>/dynamic_debug/control
  241. // enable messages for NFS calls READ, READLINK, READDIR and READDIR+.
  242. nullarbor:~ # echo -n 'format "nfsd: READ" +p' >
  243. <debugfs>/dynamic_debug/control
  244. // enable messages in files of which the paths include string "usb"
  245. nullarbor:~ # echo -n '*usb* +p' > <debugfs>/dynamic_debug/control
  246. // enable all messages
  247. nullarbor:~ # echo -n '+p' > <debugfs>/dynamic_debug/control
  248. // add module, function to all enabled messages
  249. nullarbor:~ # echo -n '+mf' > <debugfs>/dynamic_debug/control
  250. // boot-args example, with newlines and comments for readability
  251. Kernel command line: ...
  252. // see whats going on in dyndbg=value processing
  253. dynamic_debug.verbose=1
  254. // enable pr_debugs in 2 builtins, #cmt is stripped
  255. dyndbg="module params +p #cmt ; module sys +p"
  256. // enable pr_debugs in 2 functions in a module loaded later
  257. pc87360.dyndbg="func pc87360_init_device +p; func pc87360_find +p"