Some bugfixes, another function for packaging, and some text
[wine.git] / misc / ext_debugger.c
blobb18f06a71f48c17f2f0aa788176b706435e1a766
1 /*
2 * File : external.c
3 * Author : Kevin Holbrook
4 * Created : July 18, 1999
6 * Convenience functions to handle use of external debugger.
8 */
11 #include <unistd.h>
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
17 #include "options.h"
19 #define DBG_BUFF_SIZE 12
21 #define DBG_EXTERNAL_DEFAULT "gdb"
22 #define DBG_LOCATION_DEFAULT "/usr/local/bin/wine"
23 #define DBG_SLEEPTIME_DEFAULT 120
27 /* DEBUG_ExternalDebugger
29 * This function invokes an external debugger on the current
30 * wine process. The form of the command executed is:
31 * <debugger image> <wine image> <attach process id>
33 * The debugger command is normally invoked by a newly created xterm.
35 * The current calling process is temporarily put to sleep
36 * so that the invoked debugger has time to come up and attach.
38 * The following environment variables may be used:
40 * Name Use Default
41 * -------------------------------------------------------------------------------------
42 * WINE_DBG_EXTERNAL debugger command to invoke ("gdb")
43 * WINE_DBG_LOCATION fully qualified location of wine image ("/usr/local/bin/wine")
44 * WINE_DBG_NO_XTERM if set do not invoke xterm with command (not set)
45 * WINE_DBG_SLEEPTIME number of seconds to make process sleep (120)
48 * Usage:
50 * #include "debugtools.h"
52 * DEBUG_ExternalDebugger();
55 * Environment Example:
57 * export WINE_DBG_EXTERNAL="ddd"
58 * export WINE_DBG_NO_XTERM=1
59 * export WINE_DBG_SLEEPTIME=60
63 void DEBUG_ExternalDebugger(void)
65 pid_t attach_pid;
66 pid_t child_pid;
67 int dbg_sleep_secs = DBG_SLEEPTIME_DEFAULT;
68 char *dbg_sleeptime;
71 dbg_sleeptime = getenv("WINE_DBG_SLEEPTIME");
73 /* convert sleep time string to integer seconds */
74 if (dbg_sleeptime)
76 dbg_sleep_secs = atoi(dbg_sleeptime);
78 /* check for conversion error */
79 if (dbg_sleep_secs == 0)
80 dbg_sleep_secs = DBG_SLEEPTIME_DEFAULT;
83 /* get the curent process id */
84 attach_pid = getpid();
86 /* create new process */
87 child_pid = fork();
89 /* check if we are the child process */
90 if (child_pid == 0)
92 int status;
93 const char *dbg_external;
94 const char *dbg_wine_location;
95 const char *dbg_no_xterm;
96 char pid_string[DBG_BUFF_SIZE];
99 /* check settings in environment for debugger to use */
100 dbg_external = getenv("WINE_DBG_EXTERNAL");
101 dbg_wine_location = getenv("WINE_DBG_LOCATION");
102 dbg_no_xterm = getenv("WINE_DBG_NO_XTERM");
104 /* if not set in environment, use default */
105 if (!dbg_external)
106 dbg_external = "gdb";
108 /* if not set in environment, use default */
109 if (!dbg_wine_location)
110 dbg_wine_location = argv0;
112 /* check for empty string in WINE_DBG_NO_XTERM */
113 if (dbg_no_xterm && (strlen(dbg_no_xterm) < 1))
114 dbg_no_xterm = NULL;
116 /* clear the buffer */
117 memset(pid_string, 0, DBG_BUFF_SIZE);
119 /* make pid into string */
120 sprintf(pid_string, "%ld", (long) attach_pid);
122 /* now exec the debugger to get it's own clean memory space */
123 if (dbg_no_xterm)
124 status = execlp(dbg_external, dbg_external, dbg_wine_location, pid_string, NULL);
125 else
126 status = execlp("xterm", "xterm", "-e", dbg_external, dbg_wine_location, pid_string, NULL);
128 if (status == -1)
130 if (dbg_no_xterm)
131 fprintf(stderr, "DEBUG_ExternalDebugger failed to execute \"%s %s %s\" (%s)\n",
132 dbg_external, dbg_wine_location, pid_string, strerror(errno));
133 else
134 fprintf(stderr, "DEBUG_ExternalDebugger failed to execute \"xterm -e %s %s %s\" (%s)\n",
135 dbg_external, dbg_wine_location, pid_string, strerror(errno));
139 else if (child_pid != -1)
141 /* make the parent/caller sleep so the child/debugger can catch it */
142 sleep(dbg_sleep_secs);
144 else
145 fprintf(stderr, "DEBUG_ExternalDebugger failed.\n");