Ubuntu18.04でcore dump デフォルトで生成

Ubuntu18.04でcore dump ファイルを常に生成するように設定変更します。

# core dump file 出力の設定変更

- core dump fileの生成場所

/etc/sysctl.confにcore dump fileの生成場所を追加します。

```
$ sudo vi /etc/sysctl.conf

kernel.core_pattern = /tmp/core.%e.%p.%h.%t
```

- core dump 常に生成するように設定変更

すべてプロセス、全てユーザでcore dump生成可能にします。

```
$ sudo vi /etc/security/limits.conf

root             -       core            unlimited
*                -       core            unlimited
```

- apportを無効に変更

```
$ sudo vi /etc/default/apport
#enabled=1
enabled=0
```

# core dump file の使用方法


- core dump file の読み込み

```
$ file core.ndt_mapping.2687
core.ndt_mapping.2687: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from '/home/k-koh/autoware.ai/install/lidar_localizer/lib/lidar_localizer/ndt_mapping', real uid: 1000, effective uid: 1000, real gid: 1000, effective gid: 1000, execfn: '/home/k-koh/autoware.ai/install/lidar_localizer/lib/lidar_localizer/ndt_mapping', platform: 'x86_64'

```

- core dump file の解析

gdb {実行ファイル} {コアファイル}

```
$ gdb /home/k-koh/autoware.ai/install/lidar_localizer/lib/lidar_localizer/ndt_mapping core.ndt_mapping.2687

Reading symbols from /home/k-koh/autoware.ai/install/lidar_localizer/lib/lidar_localizer/ndt_mapping...done.

warning: Unexpected size of section `.reg-xstate/2687' in core file.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `/home/k-koh/autoware.ai/install/lidar_localizer/lib/lidar_localizer/ndt_mapping'.
Program terminated with signal SIGSEGV, Segmentation fault.

warning: Unexpected size of section `.reg-xstate/2687' in core file.
#0  0x00007f343857b45d in void pcl::PointRepresentation<pcl::PointXYZI>::vectorize<std::vector<float, std::allocator<float> > >(pcl::PointXYZI const&, std::vector<float, std::allocator<float> >&) const () from /usr/lib/x86_64-linux-gnu/libpcl_kdtree.so.1.8
[Current thread is 1 (Thread 0x7f3438ee0c80 (LWP 2687))]
(gdb) 

```

- bt で問題対象プログラムframeリストを表示

```
(gdb) bt
#0  0x00007f343857b45d in void pcl::PointRepresentation<pcl::PointXYZI>::vectorize<std::vector<float, std::allocator<float> > >(pcl::PointXYZI const&, std::vector<float, std::allocator<float> >&) const () from /usr/lib/x86_64-linux-gnu/libpcl_kdtree.so.1.8
#1  0x00007f34385ad9df in pcl::KdTreeFLANN<pcl::PointXYZI, flann::L2_Simple<float> >::radiusSearch(pcl::PointXYZI const&, double, std::vector<int, std::allocator<int> >&, std::vector<float, std::allocator<float> >&, unsigned int) const () from /usr/lib/x86_64-linux-gnu/libpcl_kdtree.so.1.8
#2  0x0000564f8386ab3b in pcl::VoxelGridCovariance<pcl::PointXYZI>::radiusSearch (this=0x564f83a8f2e0 <ndt+512>, point=..., radius=<optimized out>, 
    k_leaves=std::vector of length 0, capacity 0, k_sqr_distances=..., max_nn=<optimized out>) at /usr/include/pcl-1.8/pcl/filters/voxel_grid_covariance.h:482
#3  0x0000564f8386e447 in pcl::NormalDistributionsTransform<pcl::PointXYZI, pcl::PointXYZI>::computeDerivatives (this=0x564f83a8f0e0 <ndt>, score_gradient=..., 
    hessian=..., trans_cloud=..., p=..., compute_hessian=true) at /usr/include/pcl-1.8/pcl/registration/impl/ndt.hpp:206
#4  0x0000564f8386fd2d in pcl::NormalDistributionsTransform<pcl::PointXYZI, pcl::PointXYZI>::computeTransformation (this=0x564f83a8f0e0 <ndt>, output=..., 
    guess=...) at /usr/include/pcl-1.8/pcl/registration/impl/ndt.hpp:120
#5  0x0000564f838491fe in pcl::Registration<pcl::PointXYZI, pcl::PointXYZI, float>::align (guess=..., output=..., this=0x564f83a8f0e0 <ndt>)
    at /usr/include/pcl-1.8/pcl/registration/impl/registration.hpp:216
#6  points_callback (input=...) at /home/k-koh/autoware.ai/src/autoware/core_perception/lidar_localizer/nodes/ndt_mapping/ndt_mapping.cpp:600
#7  0x0000564f8385474e in boost::function1<void, boost::shared_ptr<sensor_msgs::PointCloud2_<std::allocator<void> > const> const&>::operator() (a0=..., 
    this=<optimized out>) at /usr/include/boost/function/function_template.hpp:759
#8  boost::detail::function::void_function_obj_invoker1<boost::function<void (boost::shared_ptr<sensor_msgs::PointCloud2_<std::allocator<void> > const> const&)>, void, boost::shared_ptr<sensor_msgs::PointCloud2_<std::allocator<void> > const> >::invoke(boost::detail::function::function_buffer&, boost::shared_ptr<sensor_msgs::PointCloud2_<std::allocator<void> > const>) (function_obj_ptr=..., a0=...) at /usr/include/boost/function/function_template.hpp:159
#9  0x0000564f8386622f in boost::function1<void, boost::shared_ptr<sensor_msgs::PointCloud2_<std::allocator<void> > const> >::operator() (a0=..., 
    this=0x564f8537ff48) at /usr/include/boost/function/function_template.hpp:759
#10 ros::SubscriptionCallbackHelperT<boost::shared_ptr<sensor_msgs::PointCloud2_<std::allocator<void> > const> const&, void>::call (this=0x564f8537ff40, 
    params=...) at /opt/ros/melodic/include/ros/subscription_callback_helper.h:144
#11 0x00007f3435b85fa2 in ros::SubscriptionQueue::call() () from /opt/ros/melodic/lib/libroscpp.so
#12 0x00007f3435b308a9 in ros::CallbackQueue::callOneCB(ros::CallbackQueue::TLS*) () from /opt/ros/melodic/lib/libroscpp.so
#13 0x00007f3435b3261b in ros::CallbackQueue::callAvailable(ros::WallDuration) () from /opt/ros/melodic/lib/libroscpp.so
#14 0x00007f3435b89b49 in ros::SingleThreadedSpinner::spin(ros::CallbackQueue*) () from /opt/ros/melodic/lib/libroscpp.so
#15 0x00007f3435b7242b in ros::spin() () from /opt/ros/melodic/lib/libroscpp.so
#16 0x0000564f83843671 in main (argc=<optimized out>, argv=<optimized out>)
    at /home/k-koh/autoware.ai/src/autoware/core_perception/lidar_localizer/nodes/ndt_mapping/ndt_mapping.cpp:1045

```

- frame で問題ユーザプログラムframeに移動

```
(gdb) frame 6
#6  points_callback (input=...) at /home/k-koh/autoware.ai/src/autoware/core_perception/lidar_localizer/nodes/ndt_mapping/ndt_mapping.cpp:600
600	    ndt.align(*output_cloud, init_guess);

```

- print で変数内容を確認

```
(gdb) print init_guess
$1 = {<Eigen::PlainObjectBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> >> = {<Eigen::MatrixBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> >> = {<Eigen::DenseBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> >> = {<Eigen::DenseCoeffsBase<Eigen::Matrix<float, 4, 4, 0, 4, 4>, 3>> = {<Eigen::DenseCoeffsBase<Eigen::Matrix<float, 4, 4, 0, 4, 4>, 1>> = {<Eigen::DenseCoeffsBase<Eigen::Matrix<float, 4, 4, 0, 4, 4>, 0>> = {<Eigen::EigenBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> >> = {<No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, m_storage = {m_data = {array = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
          1}}}}, <No data fields>}
```

# よく使用するGDBのコマンド

```
break [ file :] function
- Set a breakpoint at function (in file).

run [ arglist]
- Start your program (with arglist, if specified).

bt
- Backtrace: display the program stack.

print expr
- Display the value of an expression.

c
- Continue running your program (after stopping, e.g. at a breakpoint).

next
- Execute next program line (after stopping); step over any function calls in the line.

edit [ file :] function
- look at the program line where it is presently stopped.

list [ file :] function
- type the text of the program in the vicinity of where it is presently stopped.

step
- Execute next program line (after stopping); step into any function calls in the line.

help [ name]
- Show information about GDB command name, or general information about using GDB.

quit
- Exit from GDB.
```