dynamic-debug-howto.txt 12 KB

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