from:http://blog.csdn.net/adaptiver/article/details/37656507linux
1、 段错误缘由分析
1 使用非法的指针,包括使用未经初始化及已经释放的指针(指针使用以前和释放以后置为NULL)
2 内存读/写越界。包括数组访问越界,或在使用一些写内存的函数时,长度指定不正确或者这些函数自己不能指定长度,典型的函数有strcpy(strncpy),sprintf
(snprint)等等。
3 对于C++对象,请经过相应类的接口来去内存进行操做,禁止经过其返回的指针对内存进行写操做,典型的如string类的data()和c_str()两个接口。
4 函数不要返回其中局部对象的引用或地址,当函数返回时,函数栈弹出,局部对象的地址将失效,改写或读这些地址都会形成未知的后果。
5 避免在栈中定义过大的数组,不然可能致使进程的栈空间不足,此时也会出现段错误。
6 操做系统的相关限制,如:进程能够分配的最大内存,进程能够打开的最大文件描述符个数等,这些须要经过ulimit或setrlimit或sysctl来解除相关的限制。
7 多线程的程序,涉及到多个线程同时操做一块内存时必须进行互斥,不然内存中的内存将不可预料
8 使用非线程安全的函数调用,例如 strerror 函数等
9 在有信号的环境中,使用不可重入函数调用,而这些函数内部会读或写某片内存区,当信号中断时,内存写操做将被打断,而下次进入时将不避免的出错。
10 跨进程传递某个地址
11 某些有特殊要求的系统调用,例如epool_wait,正常状况下使用close关闭一个套接字后,epool会再也不返回这个socket上的事件,可是若是你使用dup或dup2操做,将
致使epool没法进行移除操做。
2、 段错误缘由查找
1) 查看函数调用栈
在头文件"execinfo.h"中声明了三个函数用于获取当前线程的函数调用堆栈
Function: int backtrace(void **buffer,int size)
该函数用与获取当前线程的调用堆栈,获取的信息将会被存放在buffer中,它是一个指针列表。参数 size 用来指定buffer中能够保存多少个void* 元素。函数返回值是实际获取的指针个数,最大不超过size大小。
在buffer中的指针实际是从堆栈中获取的返回地址,每个堆栈框架有一个返回地址。
注意某些编译器的优化选项对获取正确的调用堆栈有干扰,另外内联函数没有堆栈框架;删除框架指针也会使没法正确解析堆栈内容。
Function: char ** backtrace_symbols (void *const *buffer, int size)
backtrace_symbols将从backtrace函数获取的信息转化为一个字符串数组. 参数buffer应该是从backtrace函数获取的数组指针,size是该数组中的元素个数(backtrace的返回值) 。
函数返回值是一个指向字符串数组的指针,它的大小同buffer相同.每一个字符串包含了一个相对于buffer中对应元素的可打印信息.它包括函数名,函数的偏移地址,和实际的返回地址。
如今,只有使用ELF二进制格式的程序和苦衷才能获取函数名称和偏移地址.在其余系统,只有16进制的返回地址能被获取.另外,你可能须要传递相应的标志给连接器,以能支持函数名功能(好比,在使用GNU ld的系统中,你须要传递(-rdynamic))。
该函数的返回值是经过malloc函数申请的空间,所以调用这必须使用free函数来释放指针。
注意:若是不能为字符串获取足够的空间函数的返回值将会为NULL
Function:void backtrace_symbols_fd (void *const *buffer, int size, int fd)
backtrace_symbols_fd与backtrace_symbols 函数具备相同的功能,不一样的是它不会给调用者返回字符串数组,而是将结果写入文件描述符为fd的文件中,每一个函数对应一行.它不须要调用malloc函数,所以适用于有可能调用该函数会失败的状况。
下面的例子显示了这三个函数的用法
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
/* Obtain a backtrace and print it to stdout. */
void print_trace (void)
{
void *array[10];
size_t size;
char **strings;
size_t i;
size = backtrace (array, 10);
strings = backtrace_symbols (array, size);
printf ("Obtained %zd stack frames.\n", size);
for (i = 0; i < size; i++)
{
printf ("%s\n", strings);
}
free (strings);
}
/* A dummy function to make the backtrace more interesting. */
void dummy_function (void)
{
print_trace ();
}
int main (void)
{
dummy_function ();
return 0;
}
备注:void *const *buffer -- buffer指向char类型的常量指针的指针(非常拗口)
2) 查看寄存器内容
要查看寄存器内容有两个解决办法:数组
A) 在内核里面把这些寄存器打印出来;安全
图一:段错误时内核执行路径多线程
根据上图,咱们只须要在__do_user_fault的时候把打印信息打开就能够了,以下:架构
把app
#ifdef CONFIG_DEBUG_USER框架
if (user_debug & UDBG_SEGV) {socket
printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",函数
tsk->comm, sig, addr, fsr);工具
show_pte(tsk->mm, addr);
show_regs(regs);
}
#endif
改为
printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
tsk->comm, sig, addr, fsr);
show_pte(tsk->mm, addr);
show_regs(regs);
就能够了;
里面会打印出pc寄存器的值。
B) 在上层程序里面把寄存器打印出来;
这个作法的主要思路就是先拦截SIGSEGV信号,而后在信号处理函数里面打印信息:
信号拦截代码以下:
static void catch_sigsegv()
{
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_sigaction = sigsegv_handler;
action.sa_flags = SA_SIGINFO; // 注意这里,flag 是 SA_SIGINFO,这样信号处理函数就会多一些信息。
if(sigaction(SIGSEGV, &action, NULL) < 0){
perror("sigaction");
}
}
只须要在main函数里面加入这个函数就能够了,
main(…)
{
….
catch_sigsegv();
…
}
下面来看看这个处理函数sigsegv_handler是怎么写的,代码以下:
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <ucontext.h>
#include <dlfcn.h>
static void sigsegv_handler(int signum, siginfo_t* info, void*ptr)
{
static const char *si_codes[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
int i;
ucontext_t *ucontext = (ucontext_t*)ptr;
void *bt[100];
char **strings;
printf("Segmentation Fault Trace:\n");
printf("info.si_signo = %d\n", signum);
printf("info.si_errno = %d\n", info->si_errno);
printf("info.si_code = %d (%s)\n", info->si_code, si_codes[info->si_code]);
printf("info.si_addr = %p\n", info->si_addr);
/*for arm*/
printf("the arm_fp 0x%3x\n",ucontext->uc_mcontext.arm_fp);
printf("the arm_ip 0x%3x\n",ucontext->uc_mcontext.arm_ip);
printf("the arm_sp 0x%3x\n",ucontext->uc_mcontext.arm_sp);
printf("the arm_lr 0x%3x\n",ucontext->uc_mcontext.arm_lr);
printf("the arm_pc 0x%3x\n",ucontext->uc_mcontext.arm_pc);
printf("the arm_cpsr 0x%3x\n",ucontext->uc_mcontext.arm_cpsr);
printf("the falut_address 0x%3x\n",ucontext->uc_mcontext.fault_address);
printf("Stack trace (non-dedicated):");
int sz = backtrace(bt, 20);
printf("the stack trace is %d\n",sz);
strings = backtrace_symbols(bt, sz);
for(i = 0; i < sz; ++i){
printf("%s\n", strings[i]);
}
_exit (-1);
}
测试代码以下:
void test_segv()
{
char *i=0;
*i=10;
}
void cause_segv()
{
printf("this is the cause_segv\n");
test_segv();
}
int main(int argc,char **argv)
{
catch_sigsegv();
cause_segv();
return 0;
}
编译方法:
gcc segment_trace.c -g –rdynamic –o segment_trace
执行:
./segment_trace
输出以下:
this is the catch_sigsegv
Segmentation Fault Trace:
info.si_signo = 11
info.si_errno = 0
info.si_code = 1 (SEGV_MAPERR)
info.si_addr = (nil)
the arm_fp 0xb7f8a3d4
the arm_ip 0xb7f8a3d8
the arm_sp 0xb7f8a3c0
the arm_lr 0x8998
the arm_pc 0x8974
the arm_cpsr 0x60000010
the falut_address 0x 0
Stack trace (non-dedicated):the stack trace is 5
./segment_trace(backtrace_symbols+0x1c8) [0x8844]
/lib/libc.so.6(__default_rt_sa_restorer+0) [0xb5e22230]
./segment_trace(cause_segv+0x18) [0x8998]
./segment_trace(main+0x20) [0x89c0]
/lib/libc.so.6(__libc_start_main+0x108) [0xb5e0c10c]
C) 输出信息分析
根据上面的输出能够看出一些端倪:
根据栈信息,能够看出是在cause_segv里面出了问题,可是最后一层栈信息是看不到的,另外须要根据pc寄存器的值来定位:
addr2line -f -e segment_trace 0x8974
test_segv
/home/wf/test/segment_trace.c:55
能够看到说是在55行,一看:
恰好是
*i=10;
这一行,
并且能够看出,函数名是test_segv,
因此基本上不须要打印栈信息,也能够定位了。
也可使用 objdump 工具:
objdump -S -l -z -j .text segment_trace >1.txt
查看 0x8974 地址的代码。
百度百科上关于段错误的资料。
A
segmentation
fault
(often
shortened
to
SIGSEGV
)
is
a
particular
error
condition
that
can
occur
during
the
operation
of
computer
software
.
A
segmentation
fault
occurs
when
a
program
attempts
to
access
a
memory
location
that
it
is
not
allowed
to
access,
or
attempts
to
access
a
memory
location
in
a
way
that
is
not
allowed
(for
example,
attempting
to
write
to
a
read-only
location,
or
to
overwrite
part
of
the
operating
system).
Segmentation
is
one
approach
to
memory
management
and
protection
in
the
operating
system
.
It
has
been
superseded
by
paging
for
most
purposes,
but
much
of
the
terminology
of
segmentation
is
still
used,
"segmentation
fault"
being
an
example.
Some
operating
systems
still
have
segmentation
at
some
logical
level
although
paging
is
used
as
the
main
memory
management
policy.
On
Unix-like
operating
systems,
a
process
that
accesses
an
invalid
memory
address
receives
the
SIGSEGV
signal
.
On
Microsoft
Windows
,
a
process
that
accesses
invalid
memory
receives
the
STATUS_ACCESS_VIOLATION
exception
.
上述文字没有给出
SIGSEGV
的定义,仅仅说它是“计算机软件操做过程当中的一种错误
状况”。文字描述了
SIGSEGV
在什么时候发生,即“当程序试图访问不被容许访问的内存区域
(好比,尝试写一块属于操做系统的内存)
,或以错误的类型访问内存区域(好比,尝试写
一块只读内存)
。
这个描述是准确的。
为了加深理解,
咱们再更加详细的归纳一下
SIGSEGV
。
�
SIGSEGV
是在访问内存时发生的错误,它属于内存管理的范畴
�
SIGSEGV
是一个用户态的概念,是操做系统在用户态程序错误访问内存时所作出的处
理。
�
当用户态程序访问(访问表示读、写或执行)不容许访问的内存时,产生
SIGSEGV
。
�
当用户态程序以错误的方式访问容许访问的内存时,产生
SIGSEGV
。
从用户态程序开发的角度,
咱们并不须要理解操做系统复杂的内存管理机制,
这是和硬
件平台相关的。可是,了解内核发送
SIGSEGV
信号的流程,对咱们理解
SIGSEGV
是颇有
帮
助
的
。
在
《
Understanding
Linux
Kernel
Edition
3
》
和《
Understanding
the
Linux
Virtual
Memory
Manager
》相关章节都有一幅总图对此描述,对比之下,笔者认为
ULK
的图更为直观。
Segmentation
Fault
in
Linux
6
Y
e
s
地
址
是
否
属
于
进
程
地
址
空
间
N
o
访
问
类
型
(读、写、执
行
)
是否符合该内存区域类型
该
异
常
(指
page fault
)
是
否
发
生
在
用
户
态
Y
e
s
N
o
访
问
合
法
,分
配
新
内
存
非
法
访
问
,发
送
segfault
信
号
给
用
户
态
程
序
Y
e
s
内
核
错
误
N
o
Page fault
异
常
图
1.
SIGSEGV
Overview
图
1
红色部分展现了内核发送
SIGSEGV
信号给用户态程序的整体流程。当用户态
程序访问一个会引起
SIGSEGV
的地址时,硬件首先产生一个
page
fault
,即“缺页异常”。
在内核的
page
fault
处理函数中,
首先判断该地址是否属于用户态程序的地址空间
[*]
。
以
Intel
的
32bit
IA32
架构的
CPU
为例,
用户态程序的地址空间为
[0
,
3G]
,
内核地址空间为
[3G
,
4G]
。
若是该地址属于用户态地址空间,
检查访问的类型是否和该内存区域的类型是否匹配,
不匹
配,则发送
SIGSEGV
信号;若是该地址不属于用户态地址空间,检查访问该地址的操做是
否发生在用户态,若是是,发送
SIGSEGV
信号。
[*]
这里的用户态程序地址空间,特指程序能够访问的地址空间范围。若是广义的说,
一个进程的地址空间应该包括内核空间部分,只是它不能访问而已。
图
2
更为详细的描绘了内核发送
SIGSEGV
信号的流程。在这里咱们再也不累述图中
流程,在后面章节的例子中,笔者会结合实际,描述具体的流程