Release 20020605.
[wine/multimedia.git] / debugger / ext_debugger.c
bloba42c07d46c96e63b8c015fd647605632f8738999
1 /*
2 * Convenience functions to handle use of external debugger.
4 * Copyright 1999 Kevin Holbrook
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <unistd.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
30 #define DBG_BUFF_SIZE 12
32 #define DBG_EXTERNAL_DEFAULT "gdb"
33 #define DBG_LOCATION_DEFAULT "/usr/local/bin/wine"
34 #define DBG_SLEEPTIME_DEFAULT 120
38 /* DEBUG_ExternalDebugger
40 * This function invokes an external debugger on the current
41 * wine process. The form of the command executed is:
42 * <debugger image> <wine image> <attach process id>
44 * The debugger command is normally invoked by a newly created xterm.
46 * The current calling process is temporarily put to sleep
47 * so that the invoked debugger has time to come up and attach.
49 * The following environment variables may be used:
51 * Name Use Default
52 * -------------------------------------------------------------------------------------
53 * WINE_DBG_EXTERNAL debugger command to invoke ("gdb")
54 * WINE_DBG_LOCATION fully qualified location of wine image ("/usr/local/bin/wine")
55 * WINE_DBG_NO_XTERM if set do not invoke xterm with command (not set)
56 * WINE_DBG_SLEEPTIME number of seconds to make process sleep (120)
59 * Usage:
61 * #include "wine/debug.h"
63 * DEBUG_ExternalDebugger();
66 * Environment Example:
68 * export WINE_DBG_EXTERNAL="ddd"
69 * export WINE_DBG_NO_XTERM=1
70 * export WINE_DBG_SLEEPTIME=60
74 void DEBUG_ExternalDebugger(void)
76 pid_t attach_pid;
77 pid_t child_pid;
78 int dbg_sleep_secs = DBG_SLEEPTIME_DEFAULT;
79 char *dbg_sleeptime;
82 dbg_sleeptime = getenv("WINE_DBG_SLEEPTIME");
84 /* convert sleep time string to integer seconds */
85 if (dbg_sleeptime)
87 dbg_sleep_secs = atoi(dbg_sleeptime);
89 /* check for conversion error */
90 if (dbg_sleep_secs == 0)
91 dbg_sleep_secs = DBG_SLEEPTIME_DEFAULT;
94 /* get the current process id */
95 attach_pid = getpid();
97 /* create new process */
98 child_pid = fork();
100 /* check if we are the child process */
101 if (child_pid == 0)
103 int status;
104 const char *dbg_external;
105 const char *dbg_wine_location;
106 const char *dbg_no_xterm;
107 char pid_string[DBG_BUFF_SIZE];
110 /* check settings in environment for debugger to use */
111 dbg_external = getenv("WINE_DBG_EXTERNAL");
112 dbg_wine_location = getenv("WINE_DBG_LOCATION");
113 dbg_no_xterm = getenv("WINE_DBG_NO_XTERM");
115 /* if not set in environment, use default */
116 if (!dbg_external)
117 dbg_external = "gdb";
119 /* if not set in environment, use default */
120 if (!dbg_wine_location)
121 dbg_wine_location = "wine";
123 /* check for empty string in WINE_DBG_NO_XTERM */
124 if (dbg_no_xterm && (strlen(dbg_no_xterm) < 1))
125 dbg_no_xterm = NULL;
127 /* clear the buffer */
128 memset(pid_string, 0, DBG_BUFF_SIZE);
130 /* make pid into string */
131 sprintf(pid_string, "%ld", (long) attach_pid);
133 /* now exec the debugger to get it's own clean memory space */
134 if (dbg_no_xterm)
135 status = execlp(dbg_external, dbg_external, dbg_wine_location, pid_string, NULL);
136 else
137 status = execlp("xterm", "xterm", "-e", dbg_external, dbg_wine_location, pid_string, NULL);
139 if (status == -1)
141 if (dbg_no_xterm)
142 fprintf(stderr, "DEBUG_ExternalDebugger failed to execute \"%s %s %s\" (%s)\n",
143 dbg_external, dbg_wine_location, pid_string, strerror(errno));
144 else
145 fprintf(stderr, "DEBUG_ExternalDebugger failed to execute \"xterm -e %s %s %s\" (%s)\n",
146 dbg_external, dbg_wine_location, pid_string, strerror(errno));
150 else if (child_pid != -1)
152 /* make the parent/caller sleep so the child/debugger can catch it */
153 sleep(dbg_sleep_secs);
155 else
156 fprintf(stderr, "DEBUG_ExternalDebugger failed.\n");