How to edit the core dump pattern without disabling apport?

Currently I have enabled core dumps by editing /etc/limits.conf and addig * soft core unlimited as well as using ulimit -c unlimited.

But the core_pattern is this:

kernel.core_pattern = |/usr/share/apport/apport %p %s %c %P

So the core is transmitted to apport. I don't mind, and I don't want to mess with apport (I have a memory of something going wrong once when doing so), moreover apport nicely generates a core file in the directory of my binary when it crashes.

Is there any way to edit the location where apport saves the core, or its file name (instead of a simple core)? Similar to what I could do by disabling apport and editing the core_pattern, but without disabling apport.


Solution 1:

You can edit the apport python file to do this. Changing the location of the core is straight forward: instead of

core_path = os.path.join(cwd, 'core') 

you can put

core_path = os.path.join('my_custom_path, 'core)

the name can also be changed in a similar manner i.e. from:

core_path += '.' + str(pid)

to

core_path += '.' + str(my_custom_name) + '.' + str(pid)

note that if you want to add the binary name to the core file, you will have to pass it to apport using the core_pattern i.e something like

|/usr/share/apport/apport %p %s %c %d %P %e

Notice the %e above which passes the executable name to the apport script

Remember to restart the apport service after making a change in the script

Solution 2:

Replying late to OP but maybe this will help someone else.

I was trying to do something similar, enable core dumps on startup using a systemd service (in case settings were changed by another developer/deployment).

I also got stuck with apport overwriting the core_pattern:

kernel.core_pattern = |/usr/share/apport/apport %p %s %c %P

Since I didn't want to disable apport, I was able to bypass the apport path by just triggering my service after apport:

[Unit]
Description=Enable Core Dumps
After=apport

[Service]
ExecStart=/usr/bin/enable_core_dump.sh

[Install]
WantedBy=multi-user.target

Then from the bash script I could change core dump location and naming convention.

Solution 3:

I found a better way to do this as making changes to apport means that every time we upgrade apport we will have to add in our changes.

Linux allows you to provide a shell script as the program to execute every time a core is generated. Inside this core file, you can call apport as always and then do your custom stuff. This includes changing the name and location of the core file.

To find the path of the core generated by apport, you can use the following

core_dir=realpath '/proc/$1/cwd'