crash handling

Estaba yo preguntándome esta tarde si sería posible lanzar de forma automática gdb cuando un programa cascara, sin necesidad de ponerse a depurar después, reproducir el bug, etc, al estilo del manejador de errores que instala Visual Studio en Windows que pregunta si se desea depurar la aplicación.

Me ha salido algo como esto (HTML horrible para el coloreado de sintaxis patrocinado por Vim):

#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

void attach_gdb(int signum)
{
    pid_t gdbpid;
    pid_t debuggee_pid = getpid();
    int gdb_exit_status;
    char pidbuf[100];
    char exebuf[100]; // should be safe unless PIDs reach ~90 digits ;P
    sprintf(pidbuf, "%d", debuggee_pid);
    sprintf(exebuf, "/proc/%d/exe", debuggee_pid);
    printf("Signal %d received. Attaching GDB to process. PID=%d\n", signum, debuggee_pid);

    if ((gdbpid = fork()) == 0)
    {
        execlp("gdb", "gdb", exebuf, pidbuf, NULL);
        perror("execlp");
        exit(1);
    }

    waitpid(gdbpid, &gdb_exit_status, 0); // hold on, we want to be debugged
    exit(1);
}

void __attribute__((constructor)) setup_handler()
{
    struct sigaction sa, old_sa;
    sa.sa_handler = attach_gdb;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags=0;
    // Register the handler for all actions which dump core by default
    sigaction(SIGSEGV, &sa, &old_sa);
    sigaction(SIGFPE, &sa, &old_sa);
    sigaction(SIGABRT, &sa, &old_sa);
    sigaction(SIGILL, &sa, &old_sa);
    sigaction(SIGQUIT, &sa, &old_sa);
}

Si se compila como librería dinámica y se enlaza a cualquiera de nuestros programas (o se precarga con LD_PRELOAD) lanza un gdb asociado al proceso al recibir cualquiera de las señales que causarían un core dump y terminación del programa :D.

¿Alguna idea para mejorarlo y que sea práctico? ¿Existe ya algo asi en un paquete Debian que hace mil cosas molonas más? (seguro que sí XD).

1 Comment »

  1. Brdvrets said,

    July 27, 2017 @ 23:06 pm

    need payday loan today pay day loan what is payday loan [url=https://vl8cashadvance.org/]cash advance american express[/url] ’

  2. Jmdqscach said,

    July 27, 2017 @ 23:33 pm

    bad credit installment loans get loan online credit repair [url=https://an6paydayloans.org/]usa payday loan store[/url] ’

  3. Hjkdsig said,

    July 28, 2017 @ 01:23 am

    direct loan lenders online speedy cash payday loan direct lender [url=https://lp3personalloan.org/]same day loans no credit[/url] ’

  4. MoisesWap said,

    July 28, 2017 @ 01:37 am

    generic cialis does work

    http://buycialisuonline.com/ – buy cheap cialis online

    buy cheap cialis online

    canada buy cialis online

  5. Hjkdsig said,

    July 28, 2017 @ 01:55 am

    cash advance usa payday loan store account now payday loans [url=https://lp3personalloan.org/]quick bucks payday loan[/url] ’

  6. Jmdqscach said,

    July 28, 2017 @ 01:58 am

    small payday loans same day loans no credit loans no credit check [url=https://an6paydayloans.org/]no credit check non payday loan[/url] ’

  7. Hottest Girl In Manchester said,

    July 28, 2017 @ 02:07 am

    I simply must tell you that you have written a great and also unique article
    that I actually enjoyed reading. I’m fascinated with how well
    you laid out your material and presented your views. Thanks.

  8. MydmwHope said,

    July 28, 2017 @ 02:10 am

    bad credit installment loans instant payday loan no credit cash loans [url=https://e1paydayloans.org/]loan[/url] ’

  9. Vsflshesy said,

    July 28, 2017 @ 02:52 am

    personal loans bank cash advance america payday loan no credit check [url=https://fo7cashadvance.org/]instant payday loans[/url] ’

  10. SHOKSVAR said,

    July 28, 2017 @ 03:17 am

    online buy tadalafil
    buy cialis online
    walmart cialis online pharmacy
    cialis online

RSS feed for comments on this post · TrackBack URI

Leave a Comment

This blog is protected by Dave\'s Spam Karma 2: 17770 Spams eaten and counting...