dynamic-debug-howto.txt 12 KB

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