2002-02-02 04:17:29 +05:30
|
|
|
/*
|
2012-03-02 17:59:36 +05:30
|
|
|
* stacktrace.c - ps debugging additions
|
2002-02-02 04:17:29 +05:30
|
|
|
* Gnu debugger stack trace code provided by Peter Mattis
|
|
|
|
* <petm@CSUA.Berkeley.EDU> on Thu, 2 Nov 1995
|
|
|
|
*
|
|
|
|
* Modified for easy use by Albert Cahalan.
|
2012-03-02 17:59:36 +05:30
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-12-28 03:52:43 +05:30
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2011-10-09 17:22:57 +05:30
|
|
|
#include "common.h"
|
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
#define INTERACTIVE 0
|
|
|
|
#define STACK_TRACE 1
|
|
|
|
|
2012-01-14 03:08:47 +05:30
|
|
|
char *stored_prog_name = "you forgot to set \"program\"";
|
2002-02-02 04:17:29 +05:30
|
|
|
static int stack_trace_done;
|
|
|
|
|
|
|
|
/***********/
|
|
|
|
static void debug_stop(char **args){
|
|
|
|
execvp (args[0], args);
|
2011-11-07 21:51:41 +05:30
|
|
|
perror ("exec failed");
|
2002-02-02 04:17:29 +05:30
|
|
|
_exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********/
|
2012-01-02 12:56:45 +05:30
|
|
|
static void stack_trace_sigchld(int signum){
|
|
|
|
(void)signum;
|
2002-02-02 04:17:29 +05:30
|
|
|
stack_trace_done = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************/
|
|
|
|
static void stack_trace(char **args){
|
|
|
|
pid_t pid;
|
|
|
|
int in_fd[2];
|
|
|
|
int out_fd[2];
|
|
|
|
fd_set fdset;
|
|
|
|
fd_set readset;
|
|
|
|
struct timeval tv;
|
|
|
|
int sel, index, state;
|
|
|
|
char buffer[256];
|
|
|
|
char c;
|
|
|
|
|
|
|
|
stack_trace_done = 0;
|
|
|
|
signal(SIGCHLD, stack_trace_sigchld);
|
2011-11-07 21:51:41 +05:30
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
if((pipe (in_fd) == -1) || (pipe (out_fd) == -1)){
|
2011-11-07 21:51:41 +05:30
|
|
|
perror ("could open pipe");
|
2002-02-02 04:17:29 +05:30
|
|
|
_exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
pid = fork ();
|
|
|
|
if (pid == 0){
|
|
|
|
close (0); dup (in_fd[0]); /* set the stdin to the in pipe */
|
|
|
|
close (1); dup (out_fd[1]); /* set the stdout to the out pipe */
|
|
|
|
close (2); dup (out_fd[1]); /* set the stderr to the out pipe */
|
|
|
|
execvp (args[0], args); /* exec gdb */
|
2011-11-07 21:51:41 +05:30
|
|
|
perror ("exec failed");
|
2002-02-02 04:17:29 +05:30
|
|
|
_exit (0);
|
|
|
|
} else {
|
|
|
|
if(pid == (pid_t) -1){
|
2011-11-07 21:51:41 +05:30
|
|
|
perror ("could not fork");
|
2002-02-02 04:17:29 +05:30
|
|
|
_exit (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_ZERO (&fdset);
|
|
|
|
FD_SET (out_fd[0], &fdset);
|
|
|
|
|
|
|
|
write (in_fd[1], "backtrace\n", 10);
|
|
|
|
write (in_fd[1], "p x = 0\n", 8);
|
|
|
|
write (in_fd[1], "quit\n", 5);
|
2011-11-07 21:51:41 +05:30
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
index = 0;
|
|
|
|
state = 0;
|
2011-11-07 21:51:41 +05:30
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
for(;;){
|
|
|
|
readset = fdset;
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
|
|
|
|
if (sel == -1) break;
|
2011-11-07 21:51:41 +05:30
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
if((sel > 0) && (FD_ISSET (out_fd[0], &readset))){
|
|
|
|
if(read (out_fd[0], &c, 1)){
|
|
|
|
switch(state){
|
|
|
|
case 0:
|
|
|
|
if(c == '#'){
|
|
|
|
state = 1;
|
|
|
|
index = 0;
|
|
|
|
buffer[index++] = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
buffer[index++] = c;
|
|
|
|
if((c == '\n') || (c == '\r')){
|
|
|
|
buffer[index] = 0;
|
|
|
|
fprintf (stderr, "%s", buffer);
|
|
|
|
state = 0;
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(stack_trace_done) break;
|
|
|
|
}
|
2011-11-07 21:51:41 +05:30
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
close (in_fd[0]);
|
|
|
|
close (in_fd[1]);
|
|
|
|
close (out_fd[0]);
|
|
|
|
close (out_fd[1]);
|
|
|
|
_exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/************/
|
|
|
|
void debug(int method, char *prog_name){
|
|
|
|
pid_t pid;
|
|
|
|
char buf[16];
|
|
|
|
char *args[4] = { "gdb", NULL, NULL, NULL };
|
|
|
|
int x;
|
2011-11-07 21:51:41 +05:30
|
|
|
|
|
|
|
snprintf (buf, sizeof(buf), "%d", getpid ());
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
args[1] = prog_name;
|
|
|
|
args[2] = buf;
|
|
|
|
|
|
|
|
pid = fork ();
|
|
|
|
if(pid == 0){
|
|
|
|
switch (method){
|
|
|
|
case INTERACTIVE:
|
2011-11-07 21:51:41 +05:30
|
|
|
fprintf (stderr, "debug_stop\n");
|
2002-02-02 04:17:29 +05:30
|
|
|
debug_stop(args);
|
|
|
|
break;
|
|
|
|
case STACK_TRACE:
|
2011-11-07 21:51:41 +05:30
|
|
|
fprintf (stderr, "stack_trace\n");
|
2002-02-02 04:17:29 +05:30
|
|
|
stack_trace(args);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_exit(0);
|
|
|
|
} else if(pid == (pid_t) -1){
|
2011-11-07 21:51:41 +05:30
|
|
|
perror ("could not fork");
|
2002-02-02 04:17:29 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
x = 1;
|
|
|
|
while(x); /* wait for debugger? */
|
|
|
|
}
|
|
|
|
|
|
|
|
/************/
|
2012-01-02 12:56:45 +05:30
|
|
|
static void stack_trace_sigsegv(int signum){
|
|
|
|
(void)signum;
|
2002-02-02 04:17:29 +05:30
|
|
|
debug(STACK_TRACE, stored_prog_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/************/
|
|
|
|
void init_stack_trace(char *prog_name){
|
|
|
|
stored_prog_name = prog_name;
|
|
|
|
signal(SIGSEGV, stack_trace_sigsegv);
|
|
|
|
}
|