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;

    }

  • 相关阅读:
    标准部件工具箱概述
    从数据库和文件夹中读取图片并且resize
    控件的Lookup
    分隔字符串并以List返回(strSplit函数)
    窗体中的选中数据传递给报表
    对筛选之后的grid进行求和统计
    动态添加图片控件例子
    利用CSV文件导入数据的例子
    调用打开文件的对话框
    ax设置数据源的操作
  • 原文地址:https://www.cnblogs.com/justin-y-lin/p/5424495.html
Copyright © 2011-2022 走看看