dynamic-debug-howto.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. A another way is to use wildcard. The match rule support '*' (matches
  80. zero or more characters) and '?' (matches exactly one character).For
  81. example, you can match all usb drivers:
  82. ~# echo "file drivers/usb/* +p" > <debugfs>/dynamic_debug/control
  83. At the syntactical level, a command comprises a sequence of match
  84. specifications, followed by a flags change specification.
  85. command ::= match-spec* flags-spec
  86. The match-spec's are used to choose a subset of the known pr_debug()
  87. callsites to which to apply the flags-spec. Think of them as a query
  88. with implicit ANDs between each pair. Note that an empty list of
  89. match-specs will select all debug statement callsites.
  90. A match specification comprises a keyword, which controls the
  91. attribute of the callsite to be compared, and a value to compare
  92. against. Possible keywords are:
  93. match-spec ::= 'func' string |
  94. 'file' string |
  95. 'module' string |
  96. 'format' string |
  97. 'line' line-range
  98. line-range ::= lineno |
  99. '-'lineno |
  100. lineno'-' |
  101. lineno'-'lineno
  102. // Note: line-range cannot contain space, e.g.
  103. // "1-30" is valid range but "1 - 30" is not.
  104. lineno ::= unsigned-int
  105. The meanings of each keyword are:
  106. func
  107. The given string is compared against the function name
  108. of each callsite. Example:
  109. func svc_tcp_accept
  110. file
  111. The given string is compared against either the full pathname, the
  112. src-root relative pathname, or the basename of the source file of
  113. each callsite. Examples:
  114. file svcsock.c
  115. file kernel/freezer.c
  116. file /usr/src/packages/BUILD/sgi-enhancednfs-1.4/default/net/sunrpc/svcsock.c
  117. module
  118. The given string is compared against the module name
  119. of each callsite. The module name is the string as
  120. seen in "lsmod", i.e. without the directory or the .ko
  121. suffix and with '-' changed to '_'. Examples:
  122. module sunrpc
  123. module nfsd
  124. format
  125. The given string is searched for in the dynamic debug format
  126. string. Note that the string does not need to match the
  127. entire format, only some part. Whitespace and other
  128. special characters can be escaped using C octal character
  129. escape \ooo notation, e.g. the space character is \040.
  130. Alternatively, the string can be enclosed in double quote
  131. characters (") or single quote characters (').
  132. Examples:
  133. format svcrdma: // many of the NFS/RDMA server pr_debugs
  134. format readahead // some pr_debugs in the readahead cache
  135. format nfsd:\040SETATTR // one way to match a format with whitespace
  136. format "nfsd: SETATTR" // a neater way to match a format with whitespace
  137. format 'nfsd: SETATTR' // yet another way to match a format with whitespace
  138. line
  139. The given line number or range of line numbers is compared
  140. against the line number of each pr_debug() callsite. A single
  141. line number matches the callsite line number exactly. A
  142. range of line numbers matches any callsite between the first
  143. and last line number inclusive. An empty first number means
  144. the first line in the file, an empty line number means the
  145. last number in the file. Examples:
  146. line 1603 // exactly line 1603
  147. line 1600-1605 // the six lines from line 1600 to line 1605
  148. line -1605 // the 1605 lines from line 1 to line 1605
  149. line 1600- // all lines from line 1600 to the end of the file
  150. The flags specification comprises a change operation followed
  151. by one or more flag characters. The change operation is one
  152. of the characters:
  153. - remove the given flags
  154. + add the given flags
  155. = set the flags to the given flags
  156. The flags are:
  157. p enables the pr_debug() callsite.
  158. f Include the function name in the printed message
  159. l Include line number in the printed message
  160. m Include module name in the printed message
  161. t Include thread ID in messages not generated from interrupt context
  162. _ No flags are set. (Or'd with others on input)
  163. For print_hex_dump_debug() and print_hex_dump_bytes(), only 'p' flag
  164. have meaning, other flags ignored.
  165. For display, the flags are preceded by '='
  166. (mnemonic: what the flags are currently equal to).
  167. Note the regexp ^[-+=][flmpt_]+$ matches a flags specification.
  168. To clear all flags at once, use "=_" or "-flmpt".
  169. Debug messages during Boot Process
  170. ==================================
  171. To activate debug messages for core code and built-in modules during
  172. the boot process, even before userspace and debugfs exists, use
  173. dyndbg="QUERY", module.dyndbg="QUERY", or ddebug_query="QUERY"
  174. (ddebug_query is obsoleted by dyndbg, and deprecated). QUERY follows
  175. the syntax described above, but must not exceed 1023 characters. Your
  176. bootloader may impose lower limits.
  177. These dyndbg params are processed just after the ddebug tables are
  178. processed, as part of the arch_initcall. Thus you can enable debug
  179. messages in all code run after this arch_initcall via this boot
  180. parameter.
  181. On an x86 system for example ACPI enablement is a subsys_initcall and
  182. dyndbg="file ec.c +p"
  183. will show early Embedded Controller transactions during ACPI setup if
  184. your machine (typically a laptop) has an Embedded Controller.
  185. PCI (or other devices) initialization also is a hot candidate for using
  186. this boot parameter for debugging purposes.
  187. If foo module is not built-in, foo.dyndbg will still be processed at
  188. boot time, without effect, but will be reprocessed when module is
  189. loaded later. dyndbg_query= and bare dyndbg= are only processed at
  190. boot.
  191. Debug Messages at Module Initialization Time
  192. ============================================
  193. When "modprobe foo" is called, modprobe scans /proc/cmdline for
  194. foo.params, strips "foo.", and passes them to the kernel along with
  195. params given in modprobe args or /etc/modprob.d/*.conf files,
  196. in the following order:
  197. 1. # parameters given via /etc/modprobe.d/*.conf
  198. options foo dyndbg=+pt
  199. options foo dyndbg # defaults to +p
  200. 2. # foo.dyndbg as given in boot args, "foo." is stripped and passed
  201. foo.dyndbg=" func bar +p; func buz +mp"
  202. 3. # args to modprobe
  203. modprobe foo dyndbg==pmf # override previous settings
  204. These dyndbg queries are applied in order, with last having final say.
  205. This allows boot args to override or modify those from /etc/modprobe.d
  206. (sensible, since 1 is system wide, 2 is kernel or boot specific), and
  207. modprobe args to override both.
  208. In the foo.dyndbg="QUERY" form, the query must exclude "module foo".
  209. "foo" is extracted from the param-name, and applied to each query in
  210. "QUERY", and only 1 match-spec of each type is allowed.
  211. The dyndbg option is a "fake" module parameter, which means:
  212. - modules do not need to define it explicitly
  213. - every module gets it tacitly, whether they use pr_debug or not
  214. - it doesn't appear in /sys/module/$module/parameters/
  215. To see it, grep the control file, or inspect /proc/cmdline.
  216. For CONFIG_DYNAMIC_DEBUG kernels, any settings given at boot-time (or
  217. enabled by -DDEBUG flag during compilation) can be disabled later via
  218. the sysfs interface if the debug messages are no longer needed:
  219. echo "module module_name -p" > <debugfs>/dynamic_debug/control
  220. Examples
  221. ========
  222. // enable the message at line 1603 of file svcsock.c
  223. nullarbor:~ # echo -n 'file svcsock.c line 1603 +p' >
  224. <debugfs>/dynamic_debug/control
  225. // enable all the messages in file svcsock.c
  226. nullarbor:~ # echo -n 'file svcsock.c +p' >
  227. <debugfs>/dynamic_debug/control
  228. // enable all the messages in the NFS server module
  229. nullarbor:~ # echo -n 'module nfsd +p' >
  230. <debugfs>/dynamic_debug/control
  231. // enable all 12 messages in the function svc_process()
  232. nullarbor:~ # echo -n 'func svc_process +p' >
  233. <debugfs>/dynamic_debug/control
  234. // disable all 12 messages in the function svc_process()
  235. nullarbor:~ # echo -n 'func svc_process -p' >
  236. <debugfs>/dynamic_debug/control
  237. // enable messages for NFS calls READ, READLINK, READDIR and READDIR+.
  238. nullarbor:~ # echo -n 'format "nfsd: READ" +p' >
  239. <debugfs>/dynamic_debug/control
  240. // enable messages in files of which the paths include string "usb"
  241. nullarbor:~ # echo -n '*usb* +p' > <debugfs>/dynamic_debug/control
  242. // enable all messages
  243. nullarbor:~ # echo -n '+p' > <debugfs>/dynamic_debug/control
  244. // add module, function to all enabled messages
  245. nullarbor:~ # echo -n '+mf' > <debugfs>/dynamic_debug/control
  246. // boot-args example, with newlines and comments for readability
  247. Kernel command line: ...
  248. // see whats going on in dyndbg=value processing
  249. dynamic_debug.verbose=1
  250. // enable pr_debugs in 2 builtins, #cmt is stripped
  251. dyndbg="module params +p #cmt ; module sys +p"
  252. // enable pr_debugs in 2 functions in a module loaded later
  253. pc87360.dyndbg="func pc87360_init_device +p; func pc87360_find +p"