PG与gdb core

来源:这里教程网 时间:2026-03-14 21:21:58 作者:

1.coredump位置

cat /proc/sys/kernel/core_pattern
执行coredump生成的位置及格式
echo “/tmp/core.%e.%p” >/proc/sys/kernel/core_pattern

2.coredump文件限制

1)内存会话级
ulimit -c unlimited
2)持久型
/etc/profile追加
echo “ulimit -c unlimited” >>/etc/profile

3.coredump触发机制

(1)内存访问越界
  a) 由于使用错误的下标,导致数组访问越界。  b) 搜索字符串时,依靠字符串结束符来判断字符串是否结束,但是字符串没有正常的使用结束符。  c) 使用strcpy, strcat, sprintf, strcmp,strcasecmp等字符串操作函数,将目标字符串读/写爆。应该使用strncpy, strlcpy, strncat, strlcat, snprintf, strncmp, strncasecmp等函数防止读写越界。
(2)多线程程序使用了线程不安全的函数。
(3)多线程读写的数据未加锁保护
(4)非法指针
(5)堆栈溢出

4.coredump读取

readelf -h 读取coredump文件头
[postgres@postgre12 ~]$ readelf -h coredump.27665
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2’s complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: CORE (Core file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x0
Start of program headers: 64 (bytes into file)
Start of section headers: 0 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 155
Size of section headers: 0 (bytes)
Number of section headers: 0
Section header string table index: 0

5.gdb coredump

—gdb debug
—gdb+program+core
—program 程序
—core 程序生成的coredump
—示例
gdb 可执行程序 可执行程序core
[postgres@postgre12 proc_staus]$ gdb ./sample2 /tmp/core.sample2.75595
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 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-redhat-linux-gnu”.
For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/…
Reading symbols from /home/postgres/proc_staus/sample2…done.
[New LWP 75595]
Core was generated by `./sample2’.
**Program terminated with signal 11, Segmentation fault. #0 0x0000000000400680 in main () at sample2.c:8
8 printf(“assign %d values to a[%d]…\n”, n, m);**

相关推荐