📄 linux设备驱动程序学习(2)-调试技术 - linux设备驱动程序 - tekkaman ninja.htm
字号:
<DIV style="TEXT-ALIGN: left"><FONT size=4><SPAN
style="COLOR: rgb(0,1,255)">一、内核中的调试支持</SPAN></FONT><BR></DIV>在前面已经建议过:学习编写驱动程序要构建安装自己的内核(标准主线内核)。最重要的原因之一是:内核开发者已经建立了多项用于调试的功能。但是由于这些功能会造成额外的输出,并导致能下降,因此发行版厂商通常会禁止发行版内核中的调试功能。<BR>为了实现内核调试,我在内核配置上增加了几项:<BR>
Kernel hacking
--->
<BR> [*]
Magic SysRq key<BR>
[*] Kernel
debugging<BR>
[*] Debug slab memory
allocations <BR>
[*] Spinlock and
rw-lock debugging: basic checks
<BR>
[*] Spinlock debugging:
sleep-inside-spinlock
checking<BR>
[*] Compile the kernel with debug
info <BR>
[*] Magic SysRq key <BR>Device
Drivers ---> <BR>
Generic Driver Options
---><BR>
[*] Driver Core verbose debug
messages <BR>General setup
---><BR> [*]
Configure standard kernel features (for small
systems) ---><BR>
[*] Load all
symbols for
debugging/ksymoops<BR>书上介绍的还有其他配置,有的我不需要,或是s3c2440不支持,菜单里看不见。<BR>
<HR style="WIDTH: 100%; HEIGHT: 2px">
</DIV></DIV><FONT size=4><SPAN
style="COLOR: rgb(0,1,255)">二、通过打印调试<BR>(1)printk<BR><FONT
size=3> </FONT><FONT size=3><SPAN
style="COLOR: rgb(0,1,2)">首先,printk有8个loglevel,定义在<FONT
color=#0000ff><linux/kernel.h></FONT>中:</SPAN></FONT><BR></SPAN></FONT>
<TABLE style="BORDER-COLLAPSE: collapse"
borderColor=#999999 cellSpacing=0 cellPadding=0
width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: rgb(0,0,0)"><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">define</SPAN> KERN_EMERG <SPAN
style="COLOR: rgb(255,0,255)">"<0>"</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/* system is
unusable
*/</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">define</SPAN> KERN_ALERT <SPAN
style="COLOR: rgb(255,0,255)">"<1>"</SPAN>
<SPAN style="COLOR: rgb(255,153,0)">/* action
must be taken immediately*/</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">define</SPAN> KERN_CRIT <SPAN
style="COLOR: rgb(255,0,255)">"<2>"</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/* critical
conditions */</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">define</SPAN> KERN_ERR <SPAN
style="COLOR: rgb(255,0,255)">"<3>"</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/* error
conditions */</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">define</SPAN> KERN_WARNING <SPAN
style="COLOR: rgb(255,0,255)">"<4>"</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/* warning
conditions */</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">define</SPAN> KERN_NOTICE <SPAN
style="COLOR: rgb(255,0,255)">"<5>"</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/* normal but
significant condition */</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">define</SPAN> KERN_INFO <SPAN
style="COLOR: rgb(255,0,255)">"<6>"</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/*
informational */</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">define</SPAN> KERN_DEBUG <SPAN
style="COLOR: rgb(255,0,255)">"<7>"</SPAN> <SPAN
style="COLOR: rgb(255,153,0)">/* debug-level
messages
*/</SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE><BR><FONT
size=3>未指定优先级的默认级别定义在/kernel/printk.c中:</FONT><BR>
<TABLE style="BORDER-COLLAPSE: collapse"
borderColor=#999999 cellSpacing=0 cellPadding=0
width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: rgb(0,0,0)"><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">define</SPAN>
DEFAULT_MESSAGE_LOGLEVEL 4 <SPAN
style="COLOR: rgb(255,153,0)">/* KERN_WARNING
*/</SPAN></SPAN></CODE></P></TD></TR></TBODY></TABLE><FONT
size=3><BR>当优先级的值小于console_loglevel这个整数变量的值,信息才能显示出来。而console_loglevel的初始值DEFAULT_CONSOLE_LOGLEVEL也定义在/kernel/printk.c中:</FONT>
<TABLE style="BORDER-COLLAPSE: collapse"
borderColor=#999999 cellSpacing=0 cellPadding=0
width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: rgb(0,0,0)"><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">define</SPAN>
DEFAULT_CONSOLE_LOGLEVEL 7 <SPAN
style="COLOR: rgb(255,153,0)">/* anything MORE
serious than KERN_DEBUG
*/</SPAN><BR></SPAN></CODE></P></TD></TR></TBODY></TABLE>
<HR id=null>
<FONT size=3><BR>而在运行是改变console_loglevel的程序(<SPAN
style="FONT-WEIGHT: normal; COLOR: rgb(0,1,2)"></SPAN><SPAN
style="COLOR: rgb(0,1,255)">《Linux设备驱动程序(第3版)》提供</SPAN><SPAN
style="FONT-WEIGHT: normal; COLOR: rgb(0,1,2)"></SPAN>)如下:</FONT>
<TABLE style="BORDER-COLLAPSE: collapse"
borderColor=#999999 cellSpacing=0 cellPadding=0
width="95%" bgColor=#f1f1f1 border=1>
<TBODY>
<TR>
<TD>
<P
style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN
style="COLOR: rgb(0,0,0)"><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">include</SPAN> <SPAN
style="COLOR: rgb(0,0,204)"><</SPAN>stdio<SPAN
style="COLOR: rgb(0,0,204)">.</SPAN>h<SPAN
style="COLOR: rgb(0,0,204)">></SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">include</SPAN> <SPAN
style="COLOR: rgb(0,0,204)"><</SPAN>stdlib<SPAN
style="COLOR: rgb(0,0,204)">.</SPAN>h<SPAN
style="COLOR: rgb(0,0,204)">></SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">include</SPAN> <SPAN
style="COLOR: rgb(0,0,204)"><</SPAN><SPAN
style="COLOR: rgb(255,0,0)">string</SPAN><SPAN
style="COLOR: rgb(0,0,204)">.</SPAN>h<SPAN
style="COLOR: rgb(0,0,204)">></SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">include</SPAN> <SPAN
style="COLOR: rgb(0,0,204)"><</SPAN><SPAN
style="COLOR: rgb(255,0,0)">errno</SPAN><SPAN
style="COLOR: rgb(0,0,204)">.</SPAN>h<SPAN
style="COLOR: rgb(0,0,204)">></SPAN><BR><SPAN
style="COLOR: rgb(255,153,0)"></SPAN><SPAN
style="COLOR: rgb(255,153,0)"></SPAN><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">define</SPAN>
__LIBRARY__ <SPAN
style="COLOR: rgb(255,153,0)">/* _syscall3 and
friends are only available through this
*/</SPAN><BR><SPAN
style="COLOR: rgb(0,0,204)">#</SPAN><SPAN
style="COLOR: rgb(255,0,0)">include</SPAN> <SPAN
style="COLOR: rgb(0,0,204)"><</SPAN>linux<SPAN
style="COLOR: rgb(0,0,204)">/</SPAN>unistd<SPAN
style="COLOR: rgb(0,0,204)">.</SPAN>h<SPAN
style="COLOR: rgb(0,0,204)">></SPAN><BR><SPAN
style="COLOR: rgb(255,153,0)">/* define the
system call, to override the library function
*/</SPAN><BR>_syscall3<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN><SPAN
style="COLOR: rgb(0,0,255)">int</SPAN><SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> syslog<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">int</SPAN><SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> type<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">char</SPAN> <SPAN
style="COLOR: rgb(0,0,204)">*</SPAN><SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> bufp<SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> <SPAN
style="COLOR: rgb(0,0,255)">int</SPAN><SPAN
style="COLOR: rgb(0,0,204)">,</SPAN> len<SPAN
style="COLOR: rgb(0,0,204)">)</SPAN><SPAN
style="COLOR: rgb(0,0,204)">;</SPAN><BR><BR><SPAN
style="COLOR: rgb(0,0,255)">int</SPAN> main<SPAN
style="COLOR: rgb(0,0,204)">(</SPAN><SPAN
style="COLOR: rgb(0,0,255)">int</SPAN> argc<SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -