aab7797380fed51f52eaaea85edeee87e0179290
[pspp-builds.git] / src / ui / debugger.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program 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
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "debugger.h"
20
21 #if HAVE_SYS_TYPES_H && HAVE_SYS_WAIT_H
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/wait.h>
28
29 /* Fork, start gdb and connect to the parent process.
30    If that happens successfully, then this function does not return,
31    but exits with EXIT_FAILURE. Otherwise it returns.
32  */
33 void
34 connect_debugger (void)
35 {
36   char pidstr[20];
37   pid_t pid;
38
39   snprintf (pidstr, 20, "%d", getpid ());
40   pid = fork ();
41   if ( pid  == -1 )
42     {
43       perror ("Cannot fork");
44       return ;
45     }
46   if ( pid == 0 )
47     {
48       /* child */
49       execlp ("gdb", "gdb", "-p", pidstr, NULL);
50       perror ("Cannot exec debugger");
51       exit (EXIT_FAILURE);
52     }
53   else
54     {
55       int status;
56       wait (&status);
57       if ( EXIT_SUCCESS != WEXITSTATUS (status) )
58         return ;
59     }
60
61   exit (EXIT_FAILURE);
62 }
63
64 #else /* !(HAVE_SYS_TYPES_H && HAVE_SYS_WAIT_H) */
65 /* Don't know how to connect to gdb.
66    Just return.
67  */
68 void
69 connect_debugger (void)
70 {
71 }
72 #endif /* !(HAVE_SYS_TYPES_H && HAVE_SYS_WAIT_H) */