红联Linux门户
Linux帮助

嵌入式Linux开打core dumps调试功能

发布时间:2017-05-03 10:29:31来源:By Toradex秦海作者:toradexsh
1.简介
在Linux下为了调试应用程序可以选择GDB方式运行应用程序进行调试,这种方式也可以调试程序由于接收某些信号导致的异常退出。不过当不具备直接GDB调试环境的情况下,我们也可以使用Core Dumps功能来调试程序异常退出的问题。
Core Dumps(核心转储),是操作系统当应用程序进程接收到某些如“segmentation fault(段错误)”或者“illegal instruction(非法指令)”等信号而退出停止运行时,会将包含当时内存和寄存器分配以及进程状态等信息写成的一个文件,这个文件可以稍后使用gdb进行分析来判定程序异常退出的原因,更多信息请见’man core’。
本文所演示的平台来自于ToradexApalis TK1 ARM嵌入式平台,这是一个基于nVidiaTgera K1的ARM处理器,支持四核心Cortex-A15和192 CUDA核心的GPU。
 
2.准备
a).Apalis TK1 ARM(https://www.toradex.cn/computer-on-modules/apalis-arm-family/nvidia-tegra-k1)核心版配合Apalis Ixora载板(https://www.toradex.cn/products/carrier-board/ixora-carrier-board),连接调试串口UART1到开发主机方便调试。
b).Apalis TK1系统使用Toradex Linux Release V2.7b1,并安装了nVidiaJetPack R21.5,具体的下载和更新方法请参考:http://developer.toradex.com/knowledge-base/installing-nvidia-jetpack-with-l4t-on-apalis-tk1
 
3.查看Linux Kernel配置
由于Toradex ARM模块产品Linux BSP默认kernel是没有打开的,因此可以在TK1模块系统上面通过执行下面命令测试是否kernel已经支持Core Dumps
a).查看Linux kernel配置参数
zcat /proc/config.gz | grep CONFIG_ELF_CORE
zcat /proc/config.gz | grep CONFIG_COREDUMP 
b).Apalis TK1 当前V2.7b1版本kernel版本为3.10.40,查看结果如下
ubuntu@tegra-ubuntu:~$ zcat /proc/config.gz | grep CONFIG_ELF_CORE
# CONFIG_ELF_CORE is not set
ubuntu@tegra-ubuntu:~$ zcat /proc/config.gz | grep CONFIG_COREDUMP
CONFIG_COREDUMP=y
c).下载kernel源代码修改配置并重新编译
由上面结果可见,当前TK1 kernel配置并未完整支持core dumps功能,需要下载对应kernel源代码修改配置并重新编译
./Apalis TK1 V2.7b1 kernel源代码下载以及重新编译请见:http://developer.toradex.com/knowledge-base/build-u-boot-and-linux-kernel-from-source-code
./如下修改”.config”
//标记为”y”
->General Setup ->Configure standard kernel features (expert users) ->Enable ELF core dumps
->userspace binary formats ->Enable core dump support
./重新编译uImage按照如下方法将其替换到Apalis TK1上面
//mount Apalis TK1 boot分区
ubuntu@tegra-ubuntu:~$sudomkdir /media/ubuntu/mmcblk0p1
ubuntu@tegra-ubuntu:~$sudomount -t vfat /dev/mmcblk0p1 /media/ubuntu/mmcblk0p1
//备份uimage
ubuntu@tegra-ubuntu:~$cd /media/ubuntu/mmcblk0p1
ubuntu@tegra-ubuntu:~$sudomv uimage uimage.bak
//复制新的uimage到boot分区
ubuntu@tegra-ubuntu:~$sudocp ../uimage .
//重启
ubuntu@tegra-ubuntu:~$sudo reboot
./重启后再次查看两个配置选项都为”y”配置完成状态了
 
4.User Space打开core dumps功能并测试
a).User Space打开core dumps 功能
//开启core dumps并设置最大文件大小,或者为无限大;默认为0,意味着未开启。
ubuntu@tegra-ubuntu:~$ ulimit-c 1024 (or unlimited)
//查看状态
ubuntu@tegra-ubuntu:~$ ulimit -a
core file size          (blocks, -c) unlimited
……
b).默认情况下在当前目录下生成名为“core“的文件,每次运行会覆盖原来文件,可以通过下面配置更改生成文件的命名,路径以及格式
./将生成文件命名为”core.pid”,pid为应用程序的进程号,目录还是当前目录
ubuntu@tegra-ubuntu:~$ sudosh -c "echo 1 > /proc/sys/kernel/core_uses_pid"
./更进一步还可以通过设置下面来配置生成文件的目录和格式,如下设置生成文件目录为“/temp”,格式说明如下
# %p –进程号
# %u –进程用户id
# %g –进程用户组id
# %s –生成core文件时收到的信号
# %t –生成core文件的时间戳(seconds since 0:00h, 1 Jan 1970)
# %h –主机名
# %e –程序文件名
ubuntu@tegra-ubuntu:~$ sudosh -c "echo "/tmp/core-%e-%s-%u-%g-%p-%t" > /proc/sys/kernel/core_pattern"
./另外,如果需要使用”suid”或者”gdui”权限运行程序,需要设置如下
ubuntu@tegra-ubuntu:~$ sudosh -c "echo 2 > /proc/sys/fs/suid_dumpable" 
c).测试core dumps功能
./编写下面简单程序用于测试core dumps功能
#include <iostream>
int main()
{
int *p = NULL;
std::cout<<*p<<std::endl;
return 0;
}
./编译并执行,可以看到core dumps功能启动了
ubuntu@tegra-ubuntu:~$gcc -g -Wall -o coredump coredump.cpp -lstdc++
ubuntu@tegra-ubuntu:~$ ./coredump
Segmentation fault (core dumped)
 
5.查看core dumps 文件
利用GDB查看core dumps文件
ubuntu@tegra-ubuntu:~$ gdb --core=/tmp/core-coredump-11-1000-1000-2072-1493286737
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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 "arm-linux-gnueabihf".
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".
[New LWP 2072]
Core was generated by `./coredump'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x000086c4 in ?? ()
(gdb)
 
6.为正常运行程序创建core dump
除了为异常退出程序创建core dump方法,页可以使用gdb工具包里面的gcore功能为正常运行的程序创建core dump,基本语法如下,详细情况这里就不多做描述了。
gcore-o /tmp/… xxx(process ID)
 
参考:
嵌入式Linux开打core dumps调试功能
 
本文永久更新地址:http://www.linuxdiyf.com/linux/30480.html