### core文件使用

在Linux下程序崩溃,特别是在循环中产生Segment Fault错误时,根本不知道程序在哪出错,这时,利用core文件可以快速找到出错的问题所在。

#@author:       gr
#@date:         2015-05-27
#@email:        forgerui@gmail.com

一、core文件的产生

有时候程序崩溃并没有产生core文件,这时需要设置core文件大小。

1.1. 查看core文件大小

如果core文件大小为0,就不会产生core文件。

ulimit -a 		#查看所有大小
ulimit -c 		#查看core文件大小

1.2. 设置core文件大小

ulimit -c 1024			#设为1024
ulimit -c unlimited		#设置成不受限制

如果想永久修改大小,可以修改/etc/security/limits.conf,设置如下:

#<domain>      <type>  <item>         <value>
	*         	hard	core		unlimited

1.3. core文件的命名

如果将/proc/kernel/core_uses_pid设置为1,表示添加pid作为扩展名,core文件形式如core.3871。为0,则生成的文件同一命名成core,这样可能会覆盖掉同一文件名。

二、core文件使用

使用gdb可以查看出错时的堆栈信息。

gdb -c core exe(运行文件名)
bt		#查看出错时的堆栈信息
where	#和bt相似

#0  0x00007fef3232a267 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:55
#1  0x00007fef3232beca in __GI_abort () at abort.c:89
#2  0x00007fef32c3e06d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007fef32c3bee6 in () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007fef32c3bf31 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007fef32c3c149 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007fef3411ba5a in cv::error(cv::Exception const&) () from /usr/local/lib/libopencv_core.so.2.4
#7  0x00007fef34218a84 in cv::Mat::Mat(cv::Mat const&, cv::Rect_<int> const&) () from /usr/local/lib/libopencv_core.so.2.4
#8  0x0000000000409adb in cv::Mat::operator() (this=0x7ffd19b49020, roi=...) at /usr/local/include/opencv2/core/mat.hpp:379
#9  0x000000000041372a in EnsembleTracker::drawResult (this=0x1e1af90, frame=..., frameClone=..., scale=1) at tracker.h:107
#10 0x00000000004122ba in TrakerManager::doWork (this=0x7ffd19b49880, frame=..., frame2=...) at multiTrackAssociation.cpp:762
#11 0x0000000000405310 in multiTrack (readerType=0, detectorType=1) at main.cpp:174
#12 0x00000000004059f5 in main (argc=3, argv=0x7ffd19b49bd8) at main.cpp:246

由于程序中使用了opencv,发现在程序出错时报错的信息比较多。我们可以看到在#9之后就是自己的代码,#8之前的代码是其它程序实现的,出错的位置在tracker.h的107行,再根据出错时候的输出信息:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /home/grlab/app/opencv-2.4.9/modules/core/src/matrix.cpp, line 323
terminate called after throwing an instance of 'cv::Exception'
what():  /home/grlab/app/opencv-2.4.9/modules/core/src/matrix.cpp:323: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat

可知程序的问题应该是没有进行MatRect检查,Rect的位置超越了Mat的大小,只要与Mat的大小相与一下就可以解决了。

rect &= Rect(0, 0, imgMat.cols, imgMat.rows);
Mat roiMat = imgMat(rect);

三、Reference

  1. http://www.cnblogs.com/li-hao/archive/2011/09/25/2190278.html
  2. http://blog.sina.com.cn/s/blog_537f4d9b0100wi88.html
  3. http://www.cnblogs.com/dongzhiquan/archive/2012/01/20/2328355.html
posted @ 2015-06-01 15:29  bairuiworld  阅读(814)  评论(0编辑  收藏  举报