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