Thursday, June 9, 2016

Enable core dumps for systemd services on CentOS 7

Core dumps are very useful to C++ programs to debug critical crashes like segfault, etc.

How to enable core dumps for systemd services on CentOS 7?

(1) change the core_pattern to some place you could write to
$ cat /proc/sys/kernel/core_pattern
Note: your-user-name is the user name you are using to run your program which would crash to generate a core dump.

(2) create a new file:

*       hard        core        unlimited
*       soft        core        unlimited
to enable core dumps for all users

(3) modify /etc/systemd/system.conf
to add:

(4) modify your systemd service conf
e.g., /etc/systemd/system/your-service.service
to add:
in the "[Service]" section.

(5) reload the new systemd conf and restart your service
systemctl daemon-reexec
systemctl stop your-service
systemctl start your-service
(6) how to test it
You could kill your service process by sending signal 11 (SIGSEGV) to your process. By right, you should see a new core dump at:


yum update meets "No packages marked for update"

When you use "yum update", you could see "No packages marked for update", though you are sure that there are some package updates.

In this case, you could try:
yum clean all
yum update your-package-name

How to print std::shared_ptr in GDB

GDB is a very useful debugging tool for C++, especially when your program crashes due to unknown errors. At such case, you could enable core dump and then debug your program with the core dump generated when your program crashes.

One thing to discuss here is how to print std::shared_ptr variables:
by default, if we print a std::shared_ptr variable "msg", we could see the contents that the "msg" is pointing to:
(gdb) print msg
$1 = std::shared_ptr (count 1, weak 0) 0x7f2ba8002740

what we could do instead is to:
(gdb) print (*msg._M_ptr)
which would print the contents

(1) print shared_ptr
(2) print variables:
(3) change call stack frame: