Linux程序调试之core dump文件配置

  1. Linux core dump配置
    https://blog.csdn.net/star_xiong/article/details/43529637?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally (crashed). In practice, other key pieces of program state are usually dumped at the same time, including the processor registers, which may include the program counter and stack pointer, memory management information, and other processor and operating system flags and information. The name comes from the once-standard memory technology core memory. Core dumps are often used to diagnose or debug errors in computer programs.

On many operating systems, a fatal error in a program automatically triggers a core dump, and by extension the phrase “to dump core” has come to mean, in many cases, any fatal error, regardless of whether a record of the program memory is created.

  • 终端输入 ulimit -a

    查看系统core文件的大小限制(第一行),core文件大小显示为0 表示没有打开core dump设置:
    命令行输入:ulimit -a

  • 利用ulimit -c [kbytes]设置系统允许生成的core文件的大小

    ulimit -c 0 不产生core文件

    ulimit -c 1024 设置core文件大小为1024K

    ulimit -c unlimited 不限制core文件的大小

    此处执行ulimit -c unlimited,然后执行ulimit -a查看结果,可以发现core file size的值发生变化。

    此时,core dump设置已经打开,再次执行程序出现错误时候,将会在当前工作目录生成core文件,然后就可以使用gdb调试core文件。

  • 修改文件/etc/profile,添加语句 ulimit -c unlimited。重启或者执行source /etc/profile即刻生效

    如果需要修改特定用户配置core,则需要在用户的 ~/.bash_profile 里加上 ulimit -c unlimited 来让特定的用户可以产生 core 文件

  • 在core dump文件名自动添加进程ID

    #echo 1 > /proc/sys/kernel/core_uses_pid

  • 指定内核转存的文件名和目录**

    修改文件/etc/sysctl.conf,添加下述命令:

    kernel.core_pattern = /tmp/corefiles/core %e %p %t – 切记保证目录 /tmp/corefiles存在
    kernel.core_users_pid = 1

    执行sysctl -p /etc/sysctl.conf 使得修改即刻生效

  • 测试是否配置成功

    kill -s SIGSEGV $$

  • PL

你可能感兴趣的:(后端开发,linux,服务器,运维)