1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <sys/times.h>
25 #include <sys/types.h>
31 #ifdef HAVE_SYS_SELECT_H
32 #include <sys/select.h>
33 #endif /* HAVE_SYS_SELECT_H */
36 #include <string.h> /* for bzero on BSD systems */
44 # define SELECT_MASK fd_set
50 # define SELECT_MASK void
52 # define SELECT_MASK int
57 static int do_query (char *prompt);
58 static void debug (const gchar *progname, int method);
59 static void stack_trace (char **);
60 static void stack_trace_sigchld (int);
63 static int stack_trace_done;
66 g_debug (const gchar *progname)
70 fprintf (stdout, "[n]othing, [e]xit, [s]tack trace, [a]ttach to process: ");
73 fgets (buf, 32, stdin);
74 if (strcmp (buf, "n\n") == 0)
76 else if (strcmp (buf, "s\n") == 0)
77 debug (progname, STACK_TRACE);
78 else if (strcmp (buf, "a\n") == 0)
79 debug (progname, INTERACTIVE);
85 g_attach_process (const gchar *progname,
88 if (!query || do_query ("attach to process"))
89 debug (progname, INTERACTIVE);
93 g_stack_trace (const gchar *progname,
96 if (!query || do_query ("print stack trace"))
97 debug (progname, STACK_TRACE);
101 do_query (char *prompt)
105 fprintf (stdout, "%s (y/n) ", prompt);
108 fgets (buf, 32, stdin);
109 if ((strcmp (buf, "yes\n") == 0) ||
110 (strcmp (buf, "y\n") == 0) ||
111 (strcmp (buf, "YES\n") == 0) ||
112 (strcmp (buf, "Y\n") == 0))
119 debug (const char *progname,
124 char *args[4] = { "gdb", NULL, NULL, NULL };
127 sprintf (buf, "%d", (int) getpid ());
129 args[1] = (gchar*) progname;
135 fprintf (stdout, "pid: %s\n", buf);
144 else if (pid == (pid_t) -1)
146 perror ("could not fork");
158 stack_trace (char **args)
166 int sel, index, state;
170 stack_trace_done = 0;
171 signal (SIGCHLD, stack_trace_sigchld);
173 if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
175 perror ("could open pipe");
182 close (0); dup (in_fd[0]); /* set the stdin to the in pipe */
183 close (1); dup (out_fd[1]); /* set the stdout to the out pipe */
184 close (2); dup (out_fd[1]); /* set the stderr to the out pipe */
186 execvp (args[0], args); /* exec gdb */
187 perror ("exec failed");
190 else if (pid == (pid_t) -1)
192 perror ("could not fork");
197 FD_SET (out_fd[0], &fdset);
199 write (in_fd[1], "backtrace\n", 10);
200 write (in_fd[1], "p x = 0\n", 8);
201 write (in_fd[1], "quit\n", 5);
212 sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
216 if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
218 if (read (out_fd[0], &c, 1))
232 if ((c == '\n') || (c == '\r'))
235 fprintf (stdout, "%s", buffer);
245 else if (stack_trace_done)
257 stack_trace_sigchld (int signum)
259 stack_trace_done = 1;