zoukankan      html  css  js  c++  java
  • gdb 调试core

    这几天编译项目,总是遇到segment fault: core dumped。一开始还能马上知道是刚才的修改引起的,到后来就不行了。到网上搜了一下core dump,很方便的找到问题了。记录一下。

    当程序crash退出时,内核会在当前工作目录下生成一个core文件(是一个内存映像,同时加上调试信息)。使用gdb来查 看core文件,可以指示出导致程序出错的代码所在文件和行数。

    1. 使用ulimit -a,查看core文件的信息。
     

         core file size,这一行为0,则表示关闭了此功能,不会生成core文件。
    2. 打开core dump。
           在当前编译的终端,使用ulimit -c unlimited命令,允许当前生成没有大小限制的core file.
           (unlimited也可以为生成core文件的大小,单位为kbyte.)
    3. 利用gdb查看core
        $ gdb exe-file core.xxxx
       进去后使用bt或者where,查看出错地方。
      注意:待调试的可执行文件,在编译的时候需要加-g,core文件才能正常显示出错信息!



    关于打开core dump,还有几种方法。
    自己用的方法,只能对当前终端有效,退出就无效。
    这样做,是因为只想临时生成core file,不需要每次crash时都自动生成。
    1. 用户在自己的~/.bash_profile中加入ulimit -S -c unlimited > /dev/null 2>&1。
        这样设置后允许当前用户生成没有大小限制的core dump文件。
    2. 修改/etc/profile,加入或者修改 ulimit -S -c unlimited > /dev/null 2>&1。
        这样设置后允许所有用户生成没有大小限制的core dump文件。
        这样做的优点是不需要重起系统,缺点是无法控制只让某些用户生成core dump文件。

    关于core file位置和名字
    1. core file 默认位置与可执行程序在同一目录下。
    2. 文件名。/proc/sys/kernel/core_uses_pid,可以控制core文件的文件名中是否添加pid作为扩展。
       文件内容为1,表示添加pid作为扩展名,生成的core文件格式为core.xxxx;(xxxx为pid)
       为0则表示生成的core文件统一命名为core。
       可通过以下命令修改此文件:
       echo "1" > /proc/sys/kernel/core_uses_pid
    3. proc/sys/kernel/core_pattern可以控制core文件保存位置和文件名格式。
       可通过以下命令修改此文件:
       echo "/corefile/core-%e-%p-%t" > proc/sys/kernel/core_pattern,
        可以将core文件统一生成到/corefile目录下,产生的文件名为 core-命令名-pid-时间戳。
        以下是参数列表:
        %p - insert pid into filename 添加pid
        %u - insert current uid into filename 添加当前uid
        %g - insert current gid into filename 添加当前gid
        %s - insert signal that caused the coredump into the filename 添加导致产生core的信号
        %t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间
        %h - insert hostname where the coredump happened into filename 添加主机名
        %e - insert coredumping executable name into filename 添加命令名
  • 相关阅读:
    Spring boot mybatis : Error creating bean with name 'com.github.pagehelper.autoconfigure.MapperAutoConfiguration': Invocation of init method failed;
    方法调用
    初识MQ
    Shell 变量
    Eclipse 导入本地 Git 项目
    IDEA 常用快捷键
    Xshell 配色方案
    冒泡排序
    递归
    安卓服务Service详解
  • 原文地址:https://www.cnblogs.com/vigarbuaa/p/2841184.html
Copyright © 2011-2022 走看看