好像不太难。
$ ./a.out
在 0x400966 处捕获信号:分段错误
分段故障
$ GDB_COMM=:1024 ./a.out
在 0x400966 处捕获信号:分段错误
随附的; PID = 2369
监听 1024 端口
$ gdb ./a.out
从 /home/me/a.out 读取符号...完成。
(gdb) 远程目标:1024
使用 :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;
}