Removed my authorship lines.
[pspp] / src / ui / debugger.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/wait.h>
27
28
29 #include "debugger.h"
30
31 /* Fork, start gdb and connect to the parent process. 
32    If that happens successfully, then this function does not return,
33    but exits with EXIT_FAILURE. Otherwise it returns.
34  */
35    
36 void
37 connect_debugger (void)
38 {
39   char pidstr[20];
40   pid_t pid;
41
42   snprintf (pidstr, 20, "%d", getpid ());
43   pid = fork ();
44   if ( pid  == -1 ) 
45     {
46       perror ("Cannot fork");
47       return ;
48     }
49   if ( pid == 0 ) 
50     {
51       /* child */
52       execlp ("gdb", "gdb", "-p", pidstr, NULL);
53       perror ("Cannot exec debugger");
54       exit (EXIT_FAILURE);
55     }
56   else
57     {
58       int status;
59       wait (&status);
60       if ( EXIT_SUCCESS != WEXITSTATUS (status) ) 
61         return ;
62     }
63   
64   exit (EXIT_FAILURE);
65 }
66