zoukankan      html  css  js  c++  java
  • Linux内核调试方法总结之调试宏

    本文介绍的内核调试宏属于静态调试方法,通过调试宏主动触发oops从而打印出函数调用栈信息。

     

    1) BUG_ON 查看bug处堆栈内容,主动制造oops

    Linux中BUG_ON,WARN_ON用于调试,比如

    #define BUG_ON(condition) do { /

             if (unlikely((condition)!=0)) /

                     BUG(); /

     } while(0)

    如果觉得该condition下是一个BUG,可以添加此调试信息,查看对应堆栈内容

    具体的BUG_ON最终调用__bug

    __bug

    {

    *(int*)0=0;

    }

    从而地址非法访问,例如在你的驱动中调用BUG_ON(1),dmesg会出现下面类似信息:

    [   19.360000] Unable to handle kernel NULL pointer dereference at virtual address 00000000

    [   19.360000] pgd = c0004000

    [   19.360000] [00000000] *pgd=00000000

     

    函数的调用流程为

    fault.c

    __do_kernel_fault--------[   19.360000] Unable to handle kernel NULL pointer dereference at virtual address 00000000

    traps.c

    die--->__die--->__show_regs   /   dump_mem

     

    2) WARN_ON 调用dump_stack打印堆栈信息,不会oops

    #define WARN_ON(condition) do{/

             if(unlikely((condition) != 0)) {/

                       printk(“Badness in %s at %s:%d ”, __FUNCTION__, __FILE__, __LINE__);/

                       dump_stack();/

    }

    }while(0)

     

    3)     BUILD_BUG_ON 编译调试宏

    #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))

    BUILD_BUG_ON宏中的condition如果为真就会报错。假设条件为真,则BUILD_BUG_ON变化为:(void) sizeof (char[-1]),这种语法是在难理解,因为char[-1]本来就是非法的。结果导致编译错误。

    例子:

    #define condition 0

    static int __init main_init(void)

    {

             printk("in %s function ", __func__);

     

             BUILD_BUG_ON(condition);/*if the macro "confition" is not zero, the program can not be compiled to success. so the BUILD_BUG_ON used for the build of program*/

     

             WARN_ON(!condition);/*if the macro "confition" is not zero, there will dump the satck information for this program. so the WARN_ON used for debugof program*/

     

             BUG_ON(condition);  /*if the macro "confition" is not zero, The kernel will occur an Oops errro "Unable to handle kernel NULL pointer dereference at virtual address 00000000". so the BUG_ON used for ending the bug program*/

            

             return 0;

    }

  • 相关阅读:
    看了陈安之的文字 无论怎样 都要记住的是 你仍然是你自己 改变是应用他人的方法提高自己 改变是做更优秀更独特的自己
    流行的Ajax应用演示和源码下载(转)
    WEB2.0概念诠释(根据网络资料归纳)之一
    ASP.NET中文件上传下载方法集合(较为详细的介绍 转)
    建立梦想清单
    Ajax的应用
    Ajax的问题
    WEB2.0概念诠释(根据网络资料归纳)之三
    IIS突然挂掉
    VS2008软件90天过期,解决升级问题。
  • 原文地址:https://www.cnblogs.com/justin-y-lin/p/5424495.html
Copyright © 2011-2022 走看看