RE: [ILUG] [OT] Forcing core dumps, one solution

From: Caolan McNamara (Caolan.McNamara at domain ul.ie)
Date: Tue 28 Sep 1999 - 15:27:17 IST


On 28-Sep-99 Jakma, Paul wrote:
>>
>> I'm messing with someone elses code which has a lot of traps
>> for segsegv
>> signals are set up which I want to avoid. How can I force a program to
>> core dump in the signal handler? I can't just remove the
>> signal(SIGSEGV
>> calls because half of them are in libraries. I want a bloody
>> core dump so
>> I can figure out why the damn program is crashing!
>>
>
>the default action, apart from the well known signals, is usually to dump
>core and exit. Try sending it some signals for which the programme has no
>handler installed. eg USR1, USR2, BUS...

man 7 signal, all the ones with C in the action column will cause a core
dump by default.

but thats not really your question is it, all these will force it to segv
right there and then when the signal is received, but you want it to segv where
it would have naturally, and you dont want to go and remove all the segv signal
installers. What you probably mean is that you want the installation of the
signal handler for segv to be ignored, without changing a line of code.
Sooo... why dont we try a LD_PRELOAD library, heres one that would do what
you want for the SEGV handler (at least i reckon it would), it doesnt handle
the posix sigaction and friends, but you might not being using them, and if
you are just follow the same mechanism to handle that as shown here.

So heres the .so source
compile this like so....
gcc -fPIC -shared idea.c -o idea.so -ldl
and heres idea.c

#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

typedef void (*sighandler_t)(int);

void (*signal(int signum, void (*handler)(int)))(int)
    {
    void* handle = 0;
    sighandler_t (*realsignal)(int signum, sighandler_t handler);
   
    if (signum == 11)
        return;
    else
        {
        handle = dlopen("/lib/libc.so", RTLD_LAZY);
        if (handle == NULL)
            {
            fputs (dlerror(), stderr);
            return;
            }
        realsignal = dlsym(handle, "signal");
        (*realsignal)(signum,handler);
        dlclose(handle);
        }
    }

Now before you run your program do a setenv (or export=) LD_PRELOAD /tmp/idea.so
and when the program runs (if its dynamically linked to libc, and noone in
their right mind does anything else), any attempt to install a signal handler
for SEGV will be happily ignored.

C.

source also at http://www.csn.ul.ie/~caolan/publink/segv
(or it would be except that dave airlie is busy murdering the webserver, so
you'll have to use the included source, and miss my example program)

Real Life: Caolan McNamara * Doing: MSc in HCI
Work: Caolan.McNamara at domain ul.ie * Phone: +353-86-8790257
URL: http://www.csn.ul.ie/~caolan * Sig: an oblique strategy
Its centre



This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:04:37 GMT