• 如何生成和调试 Linux 程序崩溃产生的 core 文件


    程序运行过程中,由于一些设计不合理的原因,会导致 core dump 问题,此时通常情况下不会有一些我们通常设计好的调试信息输出。那么我们如何来查看 core dump 的具体原因呢?

    本文简单写一下 core dump 出现时如何产生 core 文件及如何进行简单的调试。

    1.如何生成 core 文件

    首先我们需要使用 ulimit 命令打开 core 文件生成开关:

    • 可以使用 ulimit -c 查看 core 文件生成是否打开,如果命令执行结果为 0,表示该功能关闭,不会生成 core 文件
    • 通常我们可以使用 ulimit -c unlimited 命令打开生成 core 文件的开关,这个 core 文件的大小是没有限制的
    • 当然我们也可以通过 ulimit -c filesize 来指定 filesize 大小的 kbyte,如果做了大小指定,那么 core 文件生成会被裁剪,不完整的 core 文件在调试过程中也可能会出现问题。

    所以生成 core 文件最常用的打开开关操作为:

    ulimit -c unlimited
    
    • 1

    默认生成的 core 文件通常在程序当前运行目录下,格式通常为 core.xxxx,例如:

    core.14223 core.22325 
    
    • 1

    2.如何调试 core 文件

    调试 core 文件,我们通常使用的工具为 gdb。在发生 core dump 之后,可以实用 gdb 打开并查看core文件的内容,定位文件中引发 core dump 的具体内容。

    执行命令的通常格式如下:

    gdb [execfile] [core file]
    
    • 1

    例如:

    gdb sysbench core.22325
    
    • 1

    进入 gdb 后,使用 backtrace 或 bt 命令查看 backtrace(调用栈) 以检查产生错误的程序运行到哪里,来定位core dump 的位置。例如下面:

    [oracle@Oracle sysbench-0.5]$ gdb sysbench core.22325
    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 /usr/local/sysbench/bin/sysbench...done.
    
    warning: exec file is newer than core file.
    [New LWP 22327]
    [New LWP 22325]
    [New LWP 22326]
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib64/libthread_db.so.1".
    Core was generated by `sysbench --test /home/oracle/sysbench-0.5/sysbench/tests/db/oltp.lua --oracle-h'.
    Program terminated with signal 11, Segmentation fault.
    #0  0x00007f2098376858 in slpmloclfv () from /data/oracle/product/11.2.0/db_1/lib/libclntshcore.so.12.1
    (gdb) bt
    #0  0x00007f2098376858 in slpmloclfv () from /data/oracle/product/11.2.0/db_1/lib/libclntshcore.so.12.1
    #1  0x00007f2098376632 in slpmloc () from /data/oracle/product/11.2.0/db_1/lib/libclntshcore.so.12.1
    #2  0x00007f2098373fd8 in lpmloadpkg () from /data/oracle/product/11.2.0/db_1/lib/libclntshcore.so.12.1
    #3  0x00007f209835602b in lfvLoadPkg () from /data/oracle/product/11.2.0/db_1/lib/libclntshcore.so.12.1
    #4  0x00007f2098355cd6 in lfvSetShlMode () from /data/oracle/product/11.2.0/db_1/lib/libclntshcore.so.12.1
    #5  0x00007f20983556d0 in lfvini1 () from /data/oracle/product/11.2.0/db_1/lib/libclntshcore.so.12.1
    #6  0x00007f2098355315 in lfvinit () from /data/oracle/product/11.2.0/db_1/lib/libclntshcore.so.12.1
    #7  0x00007f209b5736d9 in kpummpin () from /data/oracle/product/11.2.0/db_1/lib/libclntsh.so.12.1
    #8  0x00007f209affe614 in kpuenvcr () from /data/oracle/product/11.2.0/db_1/lib/libclntsh.so.12.1
    #9  0x00007f209afb567f in OCIEnvCreate () from /data/oracle/product/11.2.0/db_1/lib/libclntsh.so.12.1
    #10 0x0000000000413f11 in ora_drv_init () at drv_oracle.c:246
    #11 0x00000000004098f5 in db_init (name=0xe14220 "oracle", name@entry=0x0) at db_driver.c:235
    #12 0x0000000000410125 in sb_lua_db_init_once () at script_lua.c:599
    #13 0x00007f209a2c520b in __pthread_once_slow (once_control=0x895838 , init_routine=0x410110 )
        at pthread_once.c:117
    #14 0x00007f209a2cbeee in __GI___pthread_once (once_control=once_control@entry=0x895838 ,
        init_routine=init_routine@entry=0x410110 <sb_lua_db_init_once>) at pthread_once.c:146
    #15 0x000000000040ffb0 in sb_lua_db_connect (L=0xe27e70) at script_lua.c:610
    #16 0x0000000000410100 in sb_lua_op_thread_init (thread_id=) at script_lua.c:324
    #17 0x0000000000405402 in worker_thread (arg=) at sysbench.c:493
    #18 0x00007f209a2c6ea5 in start_thread (arg=0x7f209e6f9700) at pthread_create.c:307
    #19 0x00007f2099fefb0d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    至此,就是实用 gdb 调试 core dump 信息的整个过程,如果对您有用,欢迎点赞关注作者,谢谢。

  • 相关阅读:
    Koltin协程:异步热数据流的设计与使用
    vue同时校验多个表单
    S/4 HANA 大白话 - 财务会计-1
    笔记本屏幕忽亮忽暗解决方法大全,总有一款适合你
    cd /op-bash: 无法为立即文档创建临时文件: 设备上没有空间
    VMWare不使用简易安装,手动安装ISO操作手册
    Python中安装Beautiful Soup库及其相关解析器的方法2-1
    【深度学习理论】(7) 长短时记忆网络 LSTM
    Go 基础15-在init()函数中检查包级变量的初始状态
    猿创征文|〖Python 数据库开发实战 - Python与MySQL交互篇⑰〗- 项目实战 - 实现用户管理 - 修改用户
  • 原文地址:https://blog.csdn.net/chrisy521/article/details/126654432