Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anybody know if someone had integrated libsegfault.so and gdbserver in order to get gdb attached on the fly to a crashed program?

It's mentioned in http://sourceware.org/ml/gdb/2007-06/msg00360.html before.
But no one seemed to have actually implemented this kind of idea.
Is there any obstacles for realizing this?

My requirements are the following:

  1. Being able to plugin to any elf binary executable (ex. by using LD_PRELOAD)
  2. The binary may be a multithreaded executable
  3. The binary may link to a library that contain main function
  4. This should work in various cpu architecture other than x86 (MIPS, ARM, PPC at least)

So if there's already a solution like this, I wanted a link, but if there isn't yet, I wanted to know why it's not already implemented as a wheel.
It might be just that no one didn't needed it... but I think this is quite useful to prepare as a standard.

Any technical or political issue other than just putting it together code is wanted.

like image 730
holmes Avatar asked Dec 29 '09 15:12

holmes


People also ask

What is GDB gdbserver?

gdbserver is a control program for Unix-like systems, which allows you to connect your program with a remote GDB via target remote ---but without linking in the usual debugging stub.

How do I quit gdbserver?

gdbserver runs on the target, not the host. Terminating it is target dependent. For example, if your target is UNIX-ish, you could remote login and use ps and kill from a target shell. For any type of target, rebooting should kill gdbserver .

How do I start gdbserver on target?

To start gdbserver without supplying an initial command to run or process ID to attach, use the --multi command line option. Then you can connect using target extended-remote and start the program you want to debug. In multi-process mode gdbserver does not automatically exit unless you use the option --once .


1 Answers

Doesn't seem too hard.

$ ./a.out
Caught signal at 0x400966: Segmentation fault
Segmentation fault
$ GDB_COMM=:1024 ./a.out
Caught signal at 0x400966: Segmentation fault
Attached; pid = 2369
Listening on port 1024
$ gdb ./a.out
Reading symbols from /home/me/a.out...done.
(gdb) target remote :1024
Remote debugging using :1024
#define _XOPEN_SOURCE 500
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
static char *gdb_comm;
static void segv_handler(int sig, siginfo_t *si, void *uc) {
    pid_t child;
    char msg[84], pid[20];
    char *const argv[] = {"gdbserver", gdb_comm, "--attach", pid, NULL};
    sprintf(msg, "Caught signal at %p", si->si_addr);
    psignal(si->si_signo, msg);
    if (gdb_comm && *gdb_comm) {
        switch ((child = fork())) {
        case 0:
            sprintf(pid, "%ld", (long)getppid());
            execvp(argv[0], argv);
            perror("Failed to start gdbserver");
            _exit(-1);
        case -1:
            perror("failed to fork");
        default:
            waitpid(child, NULL, 0);
            break;
        }
    }
}
int main(int argc, char **argv) {
    static struct sigaction segv_action = {
        .sa_sigaction = segv_handler,
        .sa_flags = SA_RESETHAND | SA_SIGINFO,
    };
    gdb_comm = getenv("GDB_COMM");
    sigaction(SIGILL, &segv_action, NULL);
    sigaction(SIGFPE, &segv_action, NULL);
    sigaction(SIGSEGV, &segv_action, NULL);
    sigaction(SIGBUS, &segv_action, NULL);
    *(int *)main = 0;
    return 0;
}
like image 176
ephemient Avatar answered Nov 12 '22 15:11

ephemient