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
22 #include "wine/port.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:
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)
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)
78 int dbg_sleep_secs
= DBG_SLEEPTIME_DEFAULT
;
82 dbg_sleeptime
= getenv("WINE_DBG_SLEEPTIME");
84 /* convert sleep time string to integer seconds */
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 */
100 /* check if we are the child process */
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 */
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))
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 */
135 status
= execlp(dbg_external
, dbg_external
, dbg_wine_location
, pid_string
, NULL
);
137 status
= execlp("xterm", "xterm", "-e", dbg_external
, dbg_wine_location
, pid_string
, NULL
);
142 fprintf(stderr
, "DEBUG_ExternalDebugger failed to execute \"%s %s %s\" (%s)\n",
143 dbg_external
, dbg_wine_location
, pid_string
, strerror(errno
));
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
);
156 fprintf(stderr
, "DEBUG_ExternalDebugger failed.\n");