linux段错误后重启进程,Linux下分析进程segment fault的方法

长时间不用,命令容易忘,记录一下,以备忘.

1. 程序在编译时,记得加 -g 选项,以生成带符号表的目标程序.

2. 执行:

sudo echo "core" > /proc/sys/kernel/core_pattern //需要root权限.进程碰到段错误后,生成名为core的文件,并存放在进程同目录.

ulimit -c unlimited

3. 段错误发生后,执行:

gdb ./程序名 ./core

示例:

Queue is empty: TRUE

Queue is full: FALSE

Element number in Queue: 0

We are going to add 5 elements into queue:

enqueue ok: q->data[0]=The first element

enqueue ok: q->data[1]=2:aaaaaaaaaaaaaaaaaaaaaaaaaaaaa

enqueue ok: q->data[2]=3:The third element

enqueue ok: q->data[3]=4:Bejing Beijing

enqueue ok: q->data[4]=5:The fivth element

Queue is empty: FALSE

Queue is full: FALSE

Element number in Queue: 5

We are going to remove 2 elements from queue:

段错误 (核心已转储)

执行gdb ./Queue ./core

$ gdb ./Queue ./core

GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1

Copyright (C) 2016 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later 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 "x86_64-linux-gnu".

Type "show configuration" for configuration details.

For bug reporting instructions, please see:

.

Find the GDB manual and other documentation resources online at:

.

For help, type "help".

Type "apropos word" to search for commands related to "word"...

Reading symbols from ./Queue...done.

warning: exec file is newer than core file.

[New LWP 5377]

Core was generated by `./Queue'.

Program terminated with signal SIGSEGV, Segmentation fault.

#0 strlen () at ../sysdeps/x86_64/strlen.S:106

106../sysdeps/x86_64/strlen.S: 没有那个文件或目录.strlen () at ../sysdeps/x86_64/strlen.S:106

106../sysdeps/x86_64/strlen.S: 没有那个文件或目录.

strlen()出错,这种一般因为入参为空指针,顺着代码排查即可.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值