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
/home/your-user-name/coredumps/core-%e-sig%s-user%u-group%g-pid%p-time%t
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:
/etc/security/limits.d/core.conf
like:

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



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

(4) modify your systemd service conf
e.g., /etc/systemd/system/your-service.service
to add:
LimitCORE=infinity
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:
/home/your-user-name/coredumps/core-%e-sig%s-user%u-group%g-pid%p-time%t


References:
http://www.kibinlabs.com/re-enabling-core-dumps-redhat-7/

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


References:
(1) print shared_ptr
http://stackoverflow.com/questions/24917556/how-to-access-target-of-stdtr1shared-ptr-in-gdb
(2) print variables:
http://ftp.gnu.org/old-gnu/Manuals/gdb/html_chapter/gdb_9.html
(3) change call stack frame:
http://www.unknownroad.com/rtfm/gdbtut/gdbstack.html