总线错误(bus err)

bus error
在posix兼容的平台上,引起bus errors的进程通常会收到内核发送一个SIGBUS信号。当然,SIGBUS也可能是设备故障引起的。bus errors错误由硬件引起的问题较少,大部分是由程序bug引起的。
引起bus errors的问题主要两种:
1)不存在的地址,程序产生了一条指令,读写不存在的物理地址。
2)unaligned access(未对齐的访问)
对齐指的是当存取N字节的数据时,内存的起始地址addr % N =0
未对齐产生的起因在内存芯片,内存芯片一次可以读取固定字节的数据。如果你试图访问一个未对齐的内存地址,将导致内存可能读取两次。
针对此种问题,不同的CPU平台架构对未对齐的处理是不一致的:一些平台能够支持未对齐架构的存取,会有部分性能损失;一些平台会抛出异常;一些平台本身不支持,但会悄悄的不宝任何错误,读取一个错误的内存地址。
下面给出一个未对齐访问的例子:
debian:/home/bsh/process_as# cat bus_error.c 
#include <stdlib.h>
 
int main(int argc, char **argv) {
    int *iptr;
    char *cptr;


 
    #if defined(__GNUC__)
        # if defined(__i386__)
            /* Enable Alignment Checking on x86 */
                    __asm__("pushf\n"
                                 "orl $0x40000,(%esp)\n"
                                         "popf");
        # elif defined(__x86_64__) 
                             /* Enable Alignment Checking on x86_64 */
                                     __asm__("pushf\n"
                                                 "orl $0x40000,(%rsp)\n"
                                                          "popf");
        # endif
    #endif


        /* malloc() always provides aligned memory */
    cptr = (char *) malloc(sizeof(int) + 1);
 
    /* Increment the pointer by one, making it misaligned */
    iptr = (int *) ++cptr;
 
    /* Dereference it as an int pointer, causing an unaligned access */
    *iptr = 42;
 
    return 0;
}
GDB调试:
debian:/home/bsh/process_as# gdb
GNU gdb (GDB) 7.0.1-debian
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) file ./bus_error
Reading symbols from /home/bsh/process_as/bus_error...done.
(gdb) run
Starting program: /home/bsh/process_as/bus_error 


Program received signal SIGBUS, Bus error.
0x080483f7 in main (argc=1, argv=0xbffff7b4) at bus_error.c:29
29          *iptr = 42;
(gdb) x/i $pc
0x80483f7 <main+51>:    movl   $0x2a,(%eax)   #出问题的pc指令
(gdb) p %eax
A syntax error in expression, near `%eax'.
(gdb) p $eax
$1 = 134520841
(gdb) p/x $eax      # p/x  hex打印eax值,地址 0x804a009  % 4 =1 地址不符合未对齐
$2 = 0x804a009      

(gdb) 


引文:

http://en.wikipedia.org/wiki/Segmentation_fault




  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值