6 | | </p> |
7 | | <ul><li><a class="wiki" href="/wiki/linux/profiling"><b style="color:#000;background:#ffff66">linux</b>/profiling</a> |
8 | | </li></ul><h2 id="KernelDebugging">Kernel Debugging</h2> |
9 | | <p> |
10 | | <span class="wikianchor" id="dmesg"></span> |
11 | | </p> |
12 | | <h3 id="ringbuffer">ring buffer</h3> |
13 | | <p> |
14 | | The kernel ring buffer is where various messages are sent by the kernel and its modules (ie debug/info/warn) as long as CONFIG_PRINTK is enabled. You can also enable CONFIG_PRINTK_TIME to enable timestamps on these messages. |
15 | | </p> |
16 | | <p> |
17 | | Typically an application such as syslog will pull messages from here and write them to a log, however the <tt>dmesg</tt> command will display them directly from the ring buffer: |
18 | | </p> |
19 | | <pre class="wiki">dmesg # display all messages |
| 6 | * [wiki:linux/profiling linux/profiling] |
| 7 | |
| 8 | == Kernel Debugging == |
| 9 | |
| 10 | [=#dmesg] |
| 11 | === ring buffer === |
| 12 | The kernel ring buffer is where various messages are sent by the kernel and its modules (ie debug/info/warn) as long as {{{CONFIG_PRINTK}}} is enabled. You can also enable {{{CONFIG_PRINTK_TIME}}} to enable timestamps on these messages. |
| 13 | |
| 14 | Typically an application such as {{{syslog}}} will pull messages from here and write them to a log, however the {{{dmesg}}} command will display them directly from the ring buffer: |
| 15 | {{{#!bash |
| 16 | dmesg # display all messages |
21 | | </pre><p> |
22 | | <span class="wikianchor" id="printk"></span> |
23 | | </p> |
24 | | <h3 id="printkandloglevels">printk and log levels</h3> |
25 | | <p> |
26 | | According to <b style="color:#000;background:#ffff66">Linux</b> Device Drivers, the most common debugging technique is monitoring which is done by calling printf at suitable points. In the kernel you accomplish the same with printk. |
27 | | </p> |
28 | | <p> |
29 | | The kernel defines various log levels or severity levels which are strings that you can pre-pend to debug statements defined in <a class="ext-link" href="http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/linux/include/linux/kern_levels.h"><span class="icon"></span><b style="color:#000;background:#ffff66">linux</b>/include/<b style="color:#000;background:#ffff66">linux</b>/kern_levels.h</a>: |
30 | | </p> |
31 | | <ul><li>KERN_EMERG (0) - system is unusable |
32 | | </li><li>KERN_ALERT (1) - actino must be taken immediately |
33 | | </li><li>KERN_CRIT (2) - critical conditions |
34 | | </li><li>KERN_ERR (3) - error conditions |
35 | | </li><li>KERN_WARNING (4) - warning conditions |
36 | | </li><li>KERN_NOTICE (5) - normal but significant condition |
37 | | </li><li>KERN_INFO (6) - informational |
38 | | </li><li>KERN_DEBUG (7) - debug-level messages |
39 | | </li><li>KERN_DEFAULT (d) - the default kernel loglevel |
40 | | </li></ul><p> |
| 18 | }}} |
| 19 | |
| 20 | [=#printk] |
| 21 | === printk and log levels === |
| 22 | According to Linux Device Drivers, the most common debugging technique is monitoring which is done by calling {{{printf}}} at suitable points. In the kernel you accomplish the same with {{{printk}}}. |
| 23 | |
| 24 | The kernel defines various log levels or severity levels which are strings that you can pre-pend to debug statements defined in {{{linux/include/linux/kern_levels.h}}}: |
| 25 | * KERN_EMERG (0) - system is unusable |
| 26 | * KERN_ALERT (1) - actino must be taken immediately |
| 27 | * KERN_CRIT (2) - critical conditions |
| 28 | * KERN_ERR (3) - error conditions |
| 29 | * KERN_WARNING (4) - warning conditions |
| 30 | * KERN_NOTICE (5) - normal but significant condition |
| 31 | * KERN_INFO (6) - informational |
| 32 | * KERN_DEBUG (7) - debug-level messages |
| 33 | * KERN_DEFAULT (d) - the default kernel loglevel |
| 34 | |
52 | | </p> |
53 | | <ul><li><a class="ext-link" href="http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/printk-formats.txt"><span class="icon"></span>Documentation/printk-formats.txt</a> |
54 | | </li><li><a class="ext-link" href="http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/linux/include/linux/kern_levels.h"><span class="icon"></span><b style="color:#000;background:#ffff66">linux</b>/include/<b style="color:#000;background:#ffff66">linux</b>/kern_levels.h</a> |
55 | | </li><li><a class="ext-link" href="http://elinux.org/Debugging_by_printing#Log_Levels"><span class="icon"></span>Debugging by printing</a> |
56 | | </li></ul><p> |
57 | | <span class="wikianchor" id="pr-debug"></span> |
58 | | </p> |
59 | | <h3 id="pr-debuganddev_dbg">pr-debug() and dev_dbg()</h3> |
60 | | <p> |
61 | | Instead of using printk() directly it is recommended to use the pr_debug() and dev_dbg() macros for the following reasons: |
62 | | </p> |
63 | | <ul><li>they use <a class="wiki" href="/wiki/linux/troubleshooting#dynamic-debugging">dynamic debugging</a> (if enabled in the kernel, otherwise those are just printk's) which avoids performance hits caused by rendering the printk's regardless of loglevel tuning |
64 | | </li><li>they can display device-specific details and/or can have easily formatted prefixes |
65 | | </li></ul><p> |
66 | | <span class="wikianchor" id="dynamic-debug"></span> |
67 | | </p> |
68 | | <h3 id="dynamicdebug">dynamic debug</h3> |
69 | | <p> |
| 47 | * [http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/printk-formats.txt Documentation/printk-formats.txt] |
| 48 | * [http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/linux/include/linux/kern_levels.h linux/include/linux/kern_levels.h] |
| 49 | * [http://elinux.org/Debugging_by_printing#Log_Levels Debugging by printing] |
| 50 | |
| 51 | |
| 52 | [=#pr-debug] |
| 53 | === pr-debug() and dev_dbg() === |
| 54 | Instead of using {{{printk()}}} directly it is recommended to use the {{{pr_debug()}}} and {{{dev_dbg()}}} macros for the following reasons: |
| 55 | * they use dynamic debugging (if enabled in the kernel, otherwise those are just printk's) which avoids performance hits caused by rendering the printk's regardless of loglevel tuning |
| 56 | * they can display device-specific details and/or can have easily formatted prefixes |
| 57 | |
| 58 | [=#dynamic] |
| 59 | === dynamic debug === |
85 | | </p> |
86 | | <p> |
87 | | The format of each line of the file is: <tt>filename:lineno [module]function flags format</tt>: |
88 | | </p> |
89 | | <ul><li>filename : source file of the debug statement |
90 | | </li><li>lineno : line number of the debug statement |
91 | | </li><li>module : module that contains the debug statement |
92 | | </li><li>function : function that contains the debug statement |
93 | | </li><li>flags : '+p' means the line is turned 'on' for printing |
94 | | </li><li>format : the format used for the debug statement |
95 | | </li></ul><p> |
96 | | For details and examples please see the kernel <a class="ext-link" href="http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/dynamic-debug-howto.txt"><span class="icon"></span>Documentation/dynamic-debug-howto.txt</a> |
97 | | </p> |
98 | | <p> |
| 72 | |
| 73 | The format of each line of the file is: {{{filename:lineno [module]function flags format}}}: |
| 74 | * filename : source file of the debug statement |
| 75 | * lineno : line number of the debug statement |
| 76 | * module : module that contains the debug statement |
| 77 | * function : function that contains the debug statement |
| 78 | * flags : '+p' means the line is turned 'on' for printing |
| 79 | * format : the format used for the debug statement |
| 80 | |
| 81 | For details and examples please see the kernel Documentation/dynamic-debug-howto.txt |
| 82 | |
100 | | </p> |
101 | | <ul><li><a class="ext-link" href="http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/dynamic-debug-howto.txt"><span class="icon"></span>Documentation/dynamic-debug-howto.txt</a> |
102 | | </li><li><a class="ext-link" href="https://www.kernel.org/doc/ols/2009/ols2009-pages-39-46.pdf"><span class="icon"></span>Dynamic Debug by Jason Baron of Red Hat (2009)</a> |
103 | | </li></ul><p> |
104 | | <span class="wikianchor" id="early-serial-debug"></span> |
105 | | </p> |
106 | | <h2 id="EarlySerialDebug">Early Serial Debug</h2> |
107 | | <p> |
108 | | Typically the serial drivers don't get initialized until after some of the more critical components. This can result in any kernel crash/hang not reporting anything from the serial console after <strong>Starting kernel ...</strong>. |
109 | | </p> |
110 | | <p> |
| 84 | * [http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/dynamic-debug-howto.txt Documentation/dynamic-debug-howto.txt] |
| 85 | * [https://www.kernel.org/doc/ols/2009/ols2009-pages-39-46.pdf Dynamic Debug by Jason Baron of Red Hat (2009)] |
| 86 | |
| 87 | == Early Serial Debug == |
| 88 | Typically the serial drivers don't get initialized until after some of the more critical components. This can result in any kernel crash/hang not reporting anything from the serial console after {{{'Starting kernel ....'}}} |
| 89 | |
139 | | </p> |
140 | | <ul><li>'b' - reboot immediately (without sync) |
141 | | </li><li>'c' - perform a system crash via a NULL pointer dereference (which will show a crashdump if enabled) |
142 | | </li><li>'d' - show all locks that are held |
143 | | </li><li>'e' - send a SIGTERM to all processes except for PID1 (init) |
144 | | </li><li>'f' - call oom_kill to kill a memory hog process |
145 | | </li><li>'h' - display help |
146 | | </li><li>'i' - send a SIGKILL to all processes except for PID1 (init) |
147 | | </li><li>'l' - show a stack backtrace for all active CPU's |
148 | | </li></ul><p> |
| 118 | * 'b' - reboot immediately (without sync) |
| 119 | * 'c' - perform a system crash via a NULL pointer dereference (which will show a crashdump if enabled) |
| 120 | * 'd' - show all locks that are held |
| 121 | * 'e' - send a SIGTERM to all processes except for PID1 (init) |
| 122 | * 'f' - call oom_kill to kill a memory hog process |
| 123 | * 'h' - display help |
| 124 | * 'i' - send a SIGKILL to all processes except for PID1 (init) |
| 125 | * 'l' - show a stack backtrace for all active CPU's |
| 126 | |
150 | | </p> |
151 | | <ul><li><a class="ext-link" href="https://www.kernel.org/doc/Documentation/sysrq.txt"><span class="icon"></span>https://www.kernel.org/doc/Documentation/sysrq.txt</a> |
152 | | </li></ul></div> |
153 | | |
154 | | <div class="trac-modifiedby"> |
155 | | <span><a href="/wiki/linux/troubleshooting?action=diff&version=6" title="Version 6 by psidhu: fix flag">Last modified</a> <a class="timeline" href="/timeline?from=2016-01-11T16%3A04%3A46-08%3A00&precision=second" title="See timeline at 01/11/16 16:04:46">21 months ago</a></span> |
156 | | <span class="trac-print">Last modified on 01/11/16 16:04:46</span> |
157 | | </div> |
158 | | |
159 | | |
160 | | </div> |
161 | | |
162 | | |
163 | | </div> |
164 | | <div id="altlinks"> |
165 | | <h3>Download in other formats:</h3> |
166 | | <ul> |
167 | | <li class="last first"> |
168 | | <a rel="nofollow" href="/wiki/linux/troubleshooting?format=txt">Plain Text</a> |
169 | | </li> |
170 | | </ul> |
171 | | </div> |
172 | | </div> |
173 | | <div id="footer" lang="en" xml:lang="en"><hr /> |
174 | | <a id="tracpowered" href="http://trac.edgewall.org/"><img src="/chrome/common/trac_logo_mini.png" height="30" width="107" alt="Trac Powered" /></a> |
175 | | <p class="left">Powered by <a href="/about"><strong>Trac 1.0</strong></a><br /> |
176 | | By <a href="http://www.edgewall.org/">Edgewall Software</a>.</p> |
177 | | <p class="right">Visit the Trac open source project at<br /><a href="http://trac.edgewall.org/">http://trac.edgewall.org/</a></p> |
178 | | </div> |
179 | | }}} |
| 128 | * [https://www.kernel.org/doc/Documentation/sysrq.txt sysrq.txt] |