https://kernelnewbies.org/FAQ/BUG

BUG() and BUG_ON(condition) are used as a debugging help when something in the kernel goes terribly wrong. When a BUG_ON() assertion fails, or the code takes a branch with BUG() in it, the kernel will print out the contents of the registers and a stack trace. After that the current process will die.

The following are examples of how BUG() and BUG_ON() are used, from a piece of code that is not supposed to run in interrupt context. The explicit if with BUG() is the coding style used in older kernels. In the 2.6 kernel, generally BUG_ON() is preferred.

  1. if (in_interrupt())
  2. BUG();
  3.  
  4. BUG_ON(in_interrupt());

How it works

  1. #define BUG() \
  2. do { \
  3. asm volatile("ud2"); \
  4. unreachable(); \
  5. } while (0)

unreachable():

https://lkml.org/lkml/2016/2/10/821

  1. Hi,
  2.  
  3. I noticed that the use of the function -- unreachable() -- inside of
  4. the BUG() macro in arch/x86/include/asm/bug.h causes compiler output
  5. to be suspect based on review of assembly output for quite a few
  6. areas.
  7.  
  8. if as a test, you remove the call to unreachable() in the BUG() macro,
  9. it seems to generate a large number of build warnings about the use of
  10. uninitialized variables that are apparently masked by the compiler
  11. since it believes this code is going to halt, even in the cases where
  12. the BUG() macro is used conditionally, as in an if (condition) then
  13. BUG() (which the compiler does not seem to understand).
  14.  
  15. This seems to indicate that the use of these built in macros telling
  16. the compiler to create a bunch of infinite jump labels is masking
  17. quite a few bugs lurking around in the regular code since gcc
  18. apparently just throws out the checks for uninitialized variables in
  19. any function if it sees this macro anywhere in the function.

  

BUG() is defined as an invalid instruction, which means the CPU will throw an invalid opcode exception. This is caught in arch/i386/kernel/entry.S, in the invalid_op entry point, which calls the generated function do_invalid_op from arch/i386/kernel/traps.c. The following macros generate the do_invalid_op() function:

  1. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  2. fastcall void do_##name(struct pt_regs * regs, long error_code) \
  3. { \
  4. siginfo_t info; \
  5. info.si_signo = signr; \
  6. info.si_errno = 0; \
  7. info.si_code = sicode; \
  8. info.si_addr = (void __user *)siaddr; \
  9. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  10. == NOTIFY_STOP) \
  11. return; \
  12. do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
  13. }
  14.  
  15. DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->eip)

The do_trap() function will discover that the trap happened while running in kernel mode, and that there is no fixup for exceptions that happen while running at this address. See FAQ/TestWpBit to learn about exception fixups.

  1. kernel_trap: {
  2. if (!fixup_exception(regs))
  3. die(str, regs, error_code);
  4. return;
  5. }

That in turn means that the current thread dies, printing a register dump and stack trace before it goes. The die() function has some magic of its own, which I won't go into here.

[yaowei@BCLinux linux]$ less arch/x86/kernel/crash
crash.c crash_dump_32.c crash_dump_64.c

[yaowei@BCLinux linux]$ less arch/x86/kernel/dumpstack
dumpstack_32.c dumpstack_64.c dumpstack.c

[yaowei@BCLinux linux]$ less arch/x86/kernel/traps.c

[yaowei@BCLinux linux]$ less kernel/panic.c

kernel BUG的更多相关文章

  1. 线上centos6出现软死锁 kernel:BUG: soft lockup

    线上centos6出现软死锁 kernel:BUG: soft lockup 今天线上一台centos6机器用xshell一直连接不上,然后在xshell上显示 Message from syslog ...

  2. I.MX6 Kernel BUG at include/linux/netdevice.h:520!

    /*************************************************************************** * I.MX6 Kernel BUG at i ...

  3. RHEL6 kernel bug在hadoop上的测试

    最近给hadoop集群升级了RHEL6,发现性能比之前的差了不少.发现淘宝内核组发现并解决了这个问题 原文链接:http://blog.donghao.org/2013/03/20/hadoop%E9 ...

  4. kernel:NMI watchdog: BUG: soft lockup - CPU#6 stuck for 28s! CentOS7linux中内核被锁死

    环境说明:虚拟机 CentOS7中解压一个8G的包时,内核报错 Message from syslogd@cosmo-01 at Apr 25 11:05:59 ... kernel:NMI watc ...

  5. karottc A Simple linux-virus Analysis、Linux Kernel <= 2.6.37 - Local Privilege Escalation、CVE-2010-4258、CVE-2010-3849、CVE-2010-3850

    catalog . 程序功能概述 . 感染文件 . 前置知识 . 获取ROOT权限: Linux Kernel <= - Local Privilege Escalation 1. 程序功能概述 ...

  6. Linux bug 14258279: scheduling clock overflows in 208 days

    早上同事反映数据库不能用.无法正常登录主机.多次尝试后终于登上主机,检查系统日志发现下述错误: BUG: soft lockup - CPU#5 stuck for 17163091988s! 貌似是 ...

  7. 深入 kernel panic 流程【转】

    一.前言 我们在项目开发过程中,很多时候会出现由于某种原因经常会导致手机系统死机重启的情况(重启分Android重启跟kernel重启,而我们这里只讨论kernel重启也就是 kernel panic ...

  8. CentOS 7.1系统自动重启的Bug定位过程

    [问题] 有同事反应最近有多台MongoDB的服务器CentOS 7.1系统会自动重启,分析了下问题原因. [排查过程] 1. 检查系统日志/var/log/message,并没有记录异常信息,jou ...

  9. 总结一下内核DEBUG中的dump_stack, BUG, BUG_ON以及panic

    有点空闲时间,让我们来总结一下内核DEBUG中的各个语句吧.随便找个内核驱动,在init函数里面加入如下代码测试: u8 a = 1, b = 0; printk("----------du ...

随机推荐

  1. python的tips:字符和字符串的问题

    今天,自己建立了一个redis,python去访问的时候, 设置可以key以后,再读取key,返回的是字符, 和字符串比较,需要做一个转换, 信息如下: import redisr=redis.Red ...

  2. poj 1151(离散化+矩形面积并)

    题目链接:http://poj.org/problem?id=1151 关于离散化,这篇博客讲的很好:http://www.cppblog.com/MiYu/archive/2010/10/15/12 ...

  3. vagrant virtualbox VM inaccessible解决办法

    vagrant无法访问的提示:Please open VirtualBox and clear out your inaccessible virtual machines or find a way ...

  4. 学习SPRING BOOT, SPRING CLOUD之Eureka和security

    有意思,明天去杨浦报名了一个SPRING CLOUD沙龙, 今天再抓紧看看哈哈哈. Eureka服务端: EurekaApplication.java package com.packtpub.Eur ...

  5. 通过UUID方式在fstab中挂载分区

    https://blog.csdn.net/lanmolei814/article/details/45692153

  6. 16aspx源码要求安装.csproj类型怎么安装

    更改打开方式(不要双击打开),右键项目-打开方式选VS打开(应该会出现一个转换界面,转换下就好了).如果没有出那个界面我也没办法了

  7. J.U.C并发框架源码阅读(八)ArrayBlockingQueue

    基于版本jdk1.7.0_80 java.util.concurrent.ArrayBlockingQueue 代码如下 /* * ORACLE PROPRIETARY/CONFIDENTIAL. U ...

  8. 牛客网 暑期ACM多校训练营(第二场)D.money-贪心 or 动态规划

    D.money 贪心,直接贴官方的题解吧. 题目大意 你要按照顺序依次经过n个商店,每到达一个商店你可以购买一件商品,也可以出售你手中的商品. 同一时刻你手上最多拿一件商品.在第i个商店购买和出售的代 ...

  9. HDU 2546.饭卡-动态规划0-1背包

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  10. 洛谷 P1616 疯狂的采药【裸完全背包】

    题目背景 此题为NOIP2005普及组第三题的疯狂版. 此题为纪念LiYuxiang而生. 题目描述 LiYuxiang是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的 ...