debug-段错误
大多數的linux發型版本關閉了內核轉存儲功能,使用:
ulimit -c查看自己的linux內核是否關閉了核心轉存儲功能;
andrew@andrew-Thurley:/proc/net$ ulimit -c 0輸出為0 說明內核關閉了核心轉存儲功能,-c選項表示內核轉存儲文件的大小限制,使用如下命令可以放開內核轉存儲功能:
ulimit -c unlimited這個命令是不限制內核轉存儲文件的大小,但是內存有限的情況下可能會想限制一下內核轉存儲文件的大小,可以使用以下命令設置內核轉存儲文件的大小,如下命令限制內核轉存儲文件的大小為1G;
ulimit -c 1073741824查看系統的核心轉存儲是否被限制為0 ,若是被限制為0使用
將核心轉存儲設置為無限制
編譯程序加上-g 選項
調試
要想使用GDB調試內核轉存儲文件,應當以以下方式啟動GDB
gdb -c core文件 ./a.out
在使用大型文件系統時,會希望將內核轉存儲放在固定的位置。默認情況下會在當前目錄下生成,但是可能很難弄清文件在哪在哪生成。這種情況下可以配置/etc/systl.conf文件,來決定文件的生成目錄和文件的命名;
例如在/etc/systl.conf 文件中添加如下內容:
kernel.core_pattern = /var/core/%t-%e-%p-%c.core
kernel.core_uses_pid = 0
然后執行
sysctl -p
上述生成核心轉存儲文件的命名:
時刻-進程名-PID-內核轉存儲最大大小.core
kernel.core_pattern中可以設置的格式符:
kernel.core_uses_pid = 0 設置為0是因為我們改變了文件名中的PID的位置,如果設置該值為1,文件名末尾就會添加.PID
segment.c文件示例
andrew@andrew-Thurley:/work/linux-sys/DEBUG/segmentation$ gdb -c core ./segment 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 <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 ./segment...done. [New LWP 7103] Core was generated by `./segment'. Program terminated with signal SIGSEGV, Segmentation fault. #0 0x00000000004004ed in main (argc=1, argv=0x7ffcab228948) at segment.c:16 16 (*a) = 1; (gdb) bt full #0 0x00000000004004ed in main (argc=1, argv=0x7ffcab228948) at segment.c:16a = 0x0 (gdb) bt #0 0x00000000004004ed in main (argc=1, argv=0x7ffcab228948) at segment.c:16 (gdb) frame 0 #0 0x00000000004004ed in main (argc=1, argv=0x7ffcab228948) at segment.c:16 16 (*a) = 1;(gdb) disassemble 0x00000000004004ed Dump of assembler code for function main:0x00000000004004d6 <+0>: push %rbp0x00000000004004d7 <+1>: mov %rsp,%rbp0x00000000004004da <+4>: mov %edi,-0x14(%rbp)0x00000000004004dd <+7>: mov %rsi,-0x20(%rbp)0x00000000004004e1 <+11>: movq $0x0,-0x8(%rbp)0x00000000004004e9 <+19>: mov -0x8(%rbp),%rax => 0x00000000004004ed <+23>: movl $0x1,(%rax)0x00000000004004f3 <+29>: mov $0x0,%eax0x00000000004004f8 <+34>: pop %rbp0x00000000004004f9 <+35>: retq End of assembler dump. (gdb) list 11 12 int *a = NULL; 13 14 15 16 (*a) = 1; 17 18 19 20 return 0;GDB list命令可以查看函數周圍的源代碼
可以看出代碼在中為指向NULL的指針賦值,產生段錯誤;
4.使用內核轉儲掩碼來排除要轉儲的內存塊
你可能會因為不希望設置ulimit 的時候太僵硬導致空間不夠沒有得到完整的轉儲,所以設置ulimit 為"unlimited" (不限制)。但是如果執行一個占用內存很恐怖的程序,當這個程序內核轉儲的時候也就會生成體積很恐怖的轉儲文件。
為了避免這種情況,可以指定內核轉儲掩碼來排除要轉儲的內存段。
掩碼請查看/usr/src/linux/Documentation/sysctl/kernel.txt 中的3.4 小節,沒有內核源碼可以到這里的網絡版,這里摘錄出來如下:
The following 7 memory types are supported:
- (bit 0) anonymous private memory(匿名私有內存段)
- (bit 1) anonymous shared memory(匿名共享內存段)
- (bit 2) file-backed private memory(file-backed 私有內存段)
- (bit 3) file-backed shared memory(file-bakced 共享內存段)
- (bit 4) ELF header pages in file-backed private memory areas (it is
effective only if the bit 2 is cleared)(ELF 文件映射,只有在bit 2 復位的時候才起作用) - (bit 5) hugetlb private memory(大頁面私有內存)
- (bit 6) hugetlb shared memory(大頁面共享內存)
設置方法很簡單:找到程序的PID,然后修改/proc/PID/coredump_filter 的值。
如果你要設置某些還沒有運行的進程的內核轉儲掩碼,請修改/proc/self/coredump_filter 的值。
PS:
a. 默認的coredump_filter 的值一般是0x23,至于代表什么,請換成二進制00100011,從右向左看,bit 0、bit 1、bit 5 被置位,也就是說會轉儲所有的匿名內存段和大頁面私有內存段。
b. 共享內存段都是一樣的,可以不必轉儲。
全局設置沒有什么好說的,把你的配置寫入/etc/profile.d/ 目錄下任意一個新建的文件當中,別忘了設置屬組和所有者為root:root,權限為751。
根據上面說的,寫入的要有一條ulimit 指令,可能還有一條sysctl 指令,最后可能還有一條cat 指令。
更多的設置請查看上面提到的kernel.txt 和proc.txt,例如你想把SUID 程序也轉儲,你需要
sysctl -w 'fs.suid_dumpable=1'總結
- 上一篇: 作者:李大中(1976-),男,中国联合
- 下一篇: 进程间通信-system-v