Now let's dump the core file from within the mod_perl server. Sometimes the program aborts abnormally via the SIGSEGVsignal (a segfault), but no core file is dumped. And without the core file it's hard to find the cause of the problem, unless you run the program inside gdb or another debugger in the first place. In order to get the core file, the application must:
Have the same effective UID as the real UID (the same goes for GID). This is the case with mod_perl unless you modify these settings in the server configuration file.
Be running from a directory that is writable by the process at the moment of the segmentation fault. Note that the program might change its current directory during its run, so it's possible that the core file will need to be dumped in a different directory from the one from which the program was started. For example when mod_perl runs an Apache::Registryscript, it changes its directory to the one in which the script's source is located.
Be started from a shell process with sufficient resource allocations for the core file to be dumped. You can override the default setting from within a shell script if the process is not started manually. In addition, you can use BSD::Resource to manipulate the setting from within the code as well.
You can use ulimit for a Bourne-style shell and limit for a C-style shell to check and adjust the resource allocation. For example, inside bash, you may set the core file size to unlimited with:
panic% ulimit -c unlimited
or for csh:
panic% limit coredumpsize unlimited
For example, you can set an upper limit of 8 MB on the core file with:
panic% ulimit -c 8388608
This ensures that if the core file would be bigger than 8 MB, it will be not created.
Note that when you are running the program under a debugger like gdb, which traps the SIGSEGV signal, the core file will not be dumped. Instead, gdb allows you to examine the program stack and other things without having the core file.
First let's test that we get the core file from the command line (under tcsh):
panic% limit coredumpsize unlimited panic% perl -MDebug::DumpCore -e 'Debug::DumpCore::segv( )' Segmentation fault (core dumped) panic% ls -l core -rw------- 1 stas stas 954368 Jul 31 23:52 core
Indeed, we can see that the core file was dumped. Let's write a simple script that uses Debug::DumpCore, as shown in Example 21-9.
use strict; use Debug::DumpCore ( ); use Cwd( ) my $r = shift; $r->send_http_header("text/plain"); my $dir = getcwd; $r->print("The core should be found at $dir/core\n"); Debug::DumpCore::segv( );
In this script we load the Debug::DumpCore and Cwd modules. Then we acquire the request object and send the HTTP headers. Now we come to the real part—we get the current working directory, print out the location of the core file that we are about to dump, and finally call Debug::DumpCore::segv( ), which dumps the core file.
Before we run the script we make sure that the shell sets the core file size to be unlimited, start the server in single-server mode as a non-root user, and generate a request to the script:
panic% cd /home/httpd/httpd_perl/bin panic% limit coredumpsize unlimited panic% ./httpd_perl -X # issue a request here Segmentation fault (core dumped)
Our browser prints out:
The core should be found at /home/httpd/perl/core
And indeed the core file appears where we were told it would (remember that Apache::Registry scripts change their directory to the location of the script source):
panic% ls -l /home/httpd/perl/core -rw------- 1 stas httpd 4669440 Jul 31 23:58 /home/httpd/perl/core
As you can see it's a 4.7 MB core file. Notice that mod_perl was started as user stas, which has write permission for the directory /home/httpd/perl.