当前位置:   article > 正文

Linux笔记 程序非正常退出报错 Segmenttation fault_函数退出的时候报segmentation fault

函数退出的时候报segmentation fault

以这个test2.c为例

  1 #include <stdio.h>                                                                                                                   
  2 #include <stdlib.h>
  3
  4 void main(){
  5         char* str = "dongnaoedu";
  6         *str = 'h';
  7         printf("%s\n",str);
  8 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

可以看到这里的第六行把*str=’h’,由于str是常量,不能被改变。所以这里明显有错误。

1 编译test2.c生成test2

root@iZ2zea2ti45wm5djtomfm5Z:/usr/yeliang/linux# gcc test2.c -g -o test2
root@iZ2zea2ti45wm5djtomfm5Z:/usr/yeliang/linux# ls
core  test1.c  test2  test2.c
  • 1
  • 2
  • 3

2 执行生成的test2

root@iZ2zea2ti45wm5djtomfm5Z:/usr/yeliang/linux# ./test2
Segmentation fault
  • 1
  • 2

看到报错了。但是怎么看错误日志呢?

3 使用ulimit -a命令

 tabstop=8root@iZ2zea2ti45wm5djtomfm5Z:/usr/yeliang/linux# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3869
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65535
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 3869
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

这段代码中的第一行

core file size          (blocks, -c) 0
  • 1

会将错误保存到core文件。此时的core文件大小是0。说明没有开启这个错误日志。
如果是0 需要给core创建一个大小

4 指定core文件的大小
使用ulimit -c 1024 创建的core文件的大小是1024
再次使用ulimit -a命令。会看到文件的大小是1024。

root@iZ2zea2ti45wm5djtomfm5Z:/usr/yeliang/linux# ulimit -c 1024
root@iZ2zea2ti45wm5djtomfm5Z:/usr/yeliang/linux# ulimit -a
core file size          (blocks, -c) 1024
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3869
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65535
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 3869
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

5 再次执行test2

root@iZ2zea2ti45wm5djtomfm5Z:/usr/yeliang/linux# ./test2
Segmentation fault (core dumped)
  • 1
  • 2

可以看到还是之前的Segmentation fault错误。但是错误日志输出到了core.

可以看到生成的core文件

root@iZ2zea2ti45wm5djtomfm5Z:/usr/yeliang/linux# ls
core  test1  test1.c  test2  test2.c
  • 1
  • 2

6 查看core文件使用gdb core

root@iZ2zea2ti45wm5djtomfm5Z:/usr/yeliang/linux# gdb test2 core
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test2...done.
[New LWP 16042]
Core was generated by `./test2'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000400541 in main () at test2.c:6
6        *str = 'h';
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

可以看到直接定位到了代码中的第6行

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/108134
推荐阅读
相关标签
  

闽ICP备14008679号