1
1
# 调试
2
2
3
+ # 常用调试选项
4
+ ## lib/Kconfig.debug
5
+ ``` c
6
+ config DEBUG_INFO
7
+ bool " Compile the kernel with debug info"
8
+ depends on DEBUG_KERNEL && !COMPILE_TEST
9
+ help
10
+ If you say Y here the resulting kernel image will include
11
+ debugging info resulting in a larger kernel image.
12
+ This adds debug symbols to the kernel and modules (gcc -g), and
13
+ is needed if you intend to use kernel crashdump or binary object
14
+ tools like crash, kgdb, LKCD, gdb, etc on the kernel.
15
+ Say Y here only if you plan to debug the kernel.
16
+
17
+ If unsure, say N.
18
+ config DEBUG_KERNEL
19
+ bool "Kernel debugging"
20
+ help
21
+ Say Y here if you are developing drivers or trying to debug and
22
+ identify kernel problems.
23
+
24
+ ```
25
+ * 不开启`COINFIG_DEBUG_INFO`无法在反汇编`vmlinux`时内联 C 源代码
26
+ ## init/Kconfig
27
+ ```c
28
+ config KALLSYMS
29
+ bool "Load all symbols for debugging/ksymoops" if EXPERT
30
+ default y
31
+ help
32
+ Say Y here to let the kernel print out symbolic crash information and
33
+ symbolic stack backtraces. This increases the size of the kernel
34
+ somewhat, as all symbols have to be loaded into the kernel image.
35
+
36
+ config KALLSYMS_ALL
37
+ bool "Include all symbols in kallsyms"
38
+ depends on DEBUG_KERNEL && KALLSYMS
39
+ help
40
+ Normally kallsyms only contains the symbols of functions for nicer
41
+ OOPS messages and backtraces (i.e., symbols from the text and inittext
42
+ sections). This is sufficient for most cases. And only in very rare
43
+ cases (e.g., when a debugger is used) all symbols are required (e.g.,
44
+ names of variables from the data sections, etc).
45
+
46
+ This option makes sure that all symbols are loaded into the kernel
47
+ image (i.e., symbols from all sections) in cost of increased kernel
48
+ size (depending on the kernel configuration, it may be 300KiB or
49
+ something like this).
50
+
51
+ Say N unless you really need all symbols.
52
+ ```
53
+
3
54
# 打印(print)
4
55
5
56
## printk
13
64
Append ``",keep"`` to not disable it when the real console takes over.
14
65
```
15
66
16
- ```
67
+ ``` c
17
68
keep_bootcon [KNL]
18
69
Do not unregister boot console at start. This is only
19
70
useful for debugging when something happens in the window
28
79
printk (KERN_DEBUG “error=%d\n”, error);
29
80
```
30
81
* 调整
31
- ```
82
+ ```sh
32
83
echo 5 > /proc/sys/kernel/printk_ratelimit # Wait sec before re-open printk
33
84
echo 10 > /proc/sys/kernel/printk_ratelimit_burst # Message number before limiting the rate
34
85
```
37
88
38
89
### loglevel
39
90
40
- ```
91
+ ``` c
41
92
loglevel= All Kernel Messages with a loglevel smaller than the
42
93
console loglevel will be printed to the console. It can
43
94
also be changed with klogd or other programs. The
@@ -141,7 +192,7 @@ static int __init loglevel(char *str)
141
192
early_param("loglevel", loglevel);
142
193
...* ```
143
194
```
144
- ### 调整
195
+ ### 调整 log level
145
196
146
197
* 如果不指定 printk 打印的 log level,那么它缺省的级别是 `DEFAULT_MESSAGE_LOGLEVEL`(通常 "4"=KERN_WARNING)
147
198
* 该缺省值可以通过 `CONFIG_DEFAULT_MESSAGE_LOGLEVEL` kernel config 选项 (make menuconfig-> Kernel Hacking -> Default message log level) 调整。
@@ -158,7 +209,7 @@ early_param("loglevel", loglevel);
158
209
# echo 8 > /proc/sys/kernel/printk
159
210
```
160
211
### logbuffer
161
- ```
212
+ ```c
162
213
log_buf_len=n[KMG] Sets the size of the printk ring buffer,
163
214
in bytes. n must be a power of two and greater
164
215
than the minimal size. The minimal size is defined
@@ -194,7 +245,7 @@ log_buf_len=n[KMG] Sets the size of the printk ring buffer,
194
245
* sysrq 的键映射表见 drivers/tty/sysrq.c 的` struct sysrq_key_op *sysrq_key_table[] ` 数组
195
246
196
247
## git二分法查找
197
- ```
248
+ ``` sh
198
249
git bisect start
199
250
git bisect bad [revision bug on]
200
251
git bisect good [revision no bug]
@@ -209,7 +260,80 @@ git bisect start - arch/x86
209
260
```
210
261
211
262
# Dynamic Debug
212
- * CONFIG_DYNAMIC_DEBUG
263
+ * ` CONFIG_DYNAMIC_DEBUG ` (lib/Kconfig.debug)
264
+ ``` c
265
+ config DYNAMIC_DEBUG
266
+ bool " Enable dynamic printk() support"
267
+ default n
268
+ depends on PRINTK
269
+ depends on (DEBUG_FS || PROC_FS)
270
+ select DYNAMIC_DEBUG_CORE
271
+ help
272
+
273
+ Compiles debug level messages into the kernel, which would not
274
+ otherwise be available at runtime. These messages can then be
275
+ enabled/disabled based on various levels of scope - per source file,
276
+ function, module, format string, and line number. This mechanism
277
+ implicitly compiles in all pr_debug() and dev_dbg() calls, which
278
+ enlarges the kernel text size by about 2%.
279
+
280
+ If a source file is compiled with DEBUG flag set, any
281
+ pr_debug() calls in it are enabled by default, but can be
282
+ disabled at runtime as below. Note that DEBUG flag is
283
+ turned on by many CONFIG_*DEBUG* options.
284
+
285
+ Usage:
286
+
287
+ Dynamic debugging is controlled via the 'dynamic_debug/control' file,
288
+ which is contained in the 'debugfs' filesystem or procfs.
289
+ Thus, the debugfs or procfs filesystem must first be mounted before
290
+ making use of this feature.
291
+ We refer the control file as: <debugfs>/dynamic_debug/control. This
292
+ file contains a list of the debug statements that can be enabled. The
293
+ format for each line of the file is:
294
+
295
+ filename:lineno [module]function flags format
296
+
297
+ filename : source file of the debug statement
298
+ lineno : line number of the debug statement
299
+ module : module that contains the debug statement
300
+ function : function that contains the debug statement
301
+ flags : '=p' means the line is turned 'on' for printing
302
+ format : the format used for the debug statement
303
+
304
+ From a live system:
305
+
306
+ nullarbor:~ # cat <debugfs>/dynamic_debug/control
307
+ # filename:lineno [module]function flags format
308
+ fs/aio.c:222 [aio]__put_ioctx =_ "__put_ioctx:\040freeing\040%p\012"
309
+ fs/aio.c:248 [aio]ioctx_alloc =_ "ENOMEM:\040nr_events\040too\040high\012"
310
+ fs/aio.c:1770 [aio]sys_io_cancel =_ "calling\040cancel\012"
311
+
312
+ Example usage:
313
+
314
+ // enable the message at line 1603 of file svcsock.c
315
+ nullarbor:~ # echo -n 'file svcsock.c line 1603 +p' >
316
+ <debugfs>/dynamic_debug/control
317
+
318
+ // enable all the messages in file svcsock.c
319
+ nullarbor:~ # echo -n 'file svcsock.c +p' >
320
+ <debugfs>/dynamic_debug/control
321
+
322
+ // enable all the messages in the NFS server module
323
+ nullarbor:~ # echo -n 'module nfsd +p' >
324
+ <debugfs>/dynamic_debug/control
325
+
326
+ // enable all 12 messages in the function svc_process()
327
+ nullarbor:~ # echo -n 'func svc_process +p' >
328
+ <debugfs>/dynamic_debug/control
329
+
330
+ // disable all 12 messages in the function svc_process()
331
+ nullarbor:~ # echo -n 'func svc_process -p' >
332
+ <debugfs>/dynamic_debug/control
333
+
334
+ See Documentation/admin-guide/dynamic-debug-howto.rst for additional
335
+ information.
336
+ ```
213
337
214
338
# References
215
339
* [Debugging by printing](https://elinux.org/Debugging_by_printing)
0 commit comments