赞
踩
以这个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 }
可以看到这里的第六行把*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
2 执行生成的test2
root@iZ2zea2ti45wm5djtomfm5Z:/usr/yeliang/linux# ./test2
Segmentation fault
看到报错了。但是怎么看错误日志呢?
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
这段代码中的第一行
core file size (blocks, -c) 0
会将错误保存到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
5 再次执行test2
root@iZ2zea2ti45wm5djtomfm5Z:/usr/yeliang/linux# ./test2
Segmentation fault (core dumped)
可以看到还是之前的Segmentation fault错误。但是错误日志输出到了core.
可以看到生成的core文件
root@iZ2zea2ti45wm5djtomfm5Z:/usr/yeliang/linux# ls
core test1 test1.c test2 test2.c
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';
可以看到直接定位到了代码中的第6行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。