core dump
当程序运行的过程中异常终止或崩溃,操作系统会将程序当时的内存状态记录下来,保存在一个文件中,这种行为就叫 core dump(核心转储)
打开 core dump 功能
终端输入:
ulimit -c
如果输出 0,说明没有开启 core dump 功能
不限制 core dump 存储信息的大小,可以执行:
ulimit -c unlimited
限制 core dump 大小,可以执行(单位是 KB):
ulimit -c xxx
以上指令只对当前终端有效,想要永久生效需要修改 "/etc/security/limits.conf" 文件,重启后生效
sudo vim /etc/security/limits.conf
修改文件内容如下:
修改 core 文件保存路径和命名
默认情况下,内核在 core dump 时所产生的 core 文件放在与该程序相同的目录中,并且文件名固定为 core
执行下面指令修改 core 文件路径:
echo /tmp/crash/core-%e-%t | sudo tee /proc/sys/kernel/core_pattern
其中 "/tmp/crash/core-%e-%t" 是 core dump 文件的保存路径和命名
注意:这里的 "/tmp/crash/" 目录如果不存在,程序崩溃时候不会自动创建 "/tmp/crash/" 目录,也就不会保存 core dump 文件了
参数表:
- %p - insert pid into filename 添加pid(进程id)
- %u - insert current uid into filename 添加当前uid(用户id)
- %g - insert current gid into filename 添加当前gid(用户组id)
- %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 添加导致产生core的命令名
重启系统后 /proc/sys/kernel/core_pattern 修改内容失效问题
原因:
系统的 apport 服务会上报系统内部错误,系统启动初始化时候将其自身注册为核心转储处理程序, 会重新覆盖一些系统配置。
解决办法:
apport 服务启动配置文件在 /etc/init.d/apport 中(ubuntu20.0.4),修改如下:
程序添加 gdb 调试信息
catkin_make 编译:
catkin_make -DCMAKE_BUILD_TYPE=Debug
或者在 CMakelist.txt 文件前面添加以下代码
SET(CMAKE_BUILD_TYPE "Debug")
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
当程序运行崩溃后,会在 "/tmp/crash/" 目录中产生 core dump 文件
e.g.
在 ros 功能包的 path_follow_local_planner 包中的 ControllerPathFollow::computeVelocityCommands 函数添加操作空指针代码触发产生 core dump 文件
在仿真中运行点到点任务,调用 path_follower,在 /tmp/crash/ 中产生 core-navit_controlle-1678801018 文件
文件中 navit_controlle 字段说明了崩溃是发生在 navit_controlle(r_node) 程序中
然后用 gdb 调用 navit_controller_node 所在的可执行文件(库) 来调试 core dump 文件,执行命令:
gdb ~/ws/navit_ws/devel/lib/libnavit_controller.so core-navit_controlle-1678801018
如图:
参考:
https://www.cnblogs.com/hazir/p/linxu_core_dump.html
【解决方案】解决Ubuntu重启后,core_pattern失效问题——手动关闭apport_faith的运维开发之路的博客-CSDN博客