Make sure __ASM_GLOBAL_FUNC generates code in the text segment.
[wine/wine64.git] / programs / winedbg / ext_debugger.c
bloba65485dbfd927e9c3a89eed7117e7a43b9d53e5e
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 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
32 #define DBG_BUFF_SIZE 12
34 #define DBG_EXTERNAL_DEFAULT "gdb"
35 #define DBG_LOCATION_DEFAULT "/usr/local/bin/wine"
36 #define DBG_SLEEPTIME_DEFAULT 120
40 /* DEBUG_ExternalDebugger
42 * This function invokes an external debugger on the current
43 * wine process. The form of the command executed is:
44 * <debugger image> <wine image> <attach process id>
46 * The debugger command is normally invoked by a newly created xterm.
48 * The current calling process is temporarily put to sleep
49 * so that the invoked debugger has time to come up and attach.
51 * The following environment variables may be used:
53 * Name Use Default
54 * -------------------------------------------------------------------------------------
55 * WINE_DBG_EXTERNAL debugger command to invoke ("gdb")
56 * WINE_DBG_LOCATION fully qualified location of wine image ("/usr/local/bin/wine")
57 * WINE_DBG_NO_XTERM if set do not invoke xterm with command (not set)
58 * WINE_DBG_SLEEPTIME number of seconds to make process sleep (120)
61 * Usage:
63 * #include "wine/debug.h"
65 * DEBUG_ExternalDebugger();
68 * Environment Example:
70 * export WINE_DBG_EXTERNAL="ddd"
71 * export WINE_DBG_NO_XTERM=1
72 * export WINE_DBG_SLEEPTIME=60
76 void DEBUG_ExternalDebugger(void)
78 pid_t attach_pid;
79 pid_t child_pid;
80 int dbg_sleep_secs = DBG_SLEEPTIME_DEFAULT;
81 char *dbg_sleeptime;
84 dbg_sleeptime = getenv("WINE_DBG_SLEEPTIME");
86 /* convert sleep time string to integer seconds */
87 if (dbg_sleeptime)
89 dbg_sleep_secs = atoi(dbg_sleeptime);
91 /* check for conversion error */
92 if (dbg_sleep_secs == 0)
93 dbg_sleep_secs = DBG_SLEEPTIME_DEFAULT;
96 /* get the current process id */
97 attach_pid = getpid();
99 /* create new process */
100 child_pid = fork();
102 /* check if we are the child process */
103 if (child_pid == 0)
105 int status;
106 const char *dbg_external;
107 const char *dbg_wine_location;
108 const char *dbg_no_xterm;
109 char pid_string[DBG_BUFF_SIZE];
112 /* check settings in environment for debugger to use */
113 dbg_external = getenv("WINE_DBG_EXTERNAL");
114 dbg_wine_location = getenv("WINE_DBG_LOCATION");
115 dbg_no_xterm = getenv("WINE_DBG_NO_XTERM");
117 /* if not set in environment, use default */
118 if (!dbg_external)
119 dbg_external = "gdb";
121 /* if not set in environment, use default */
122 if (!dbg_wine_location)
123 if (!(dbg_wine_location = getenv("WINELOADER")))
124 dbg_wine_location = "miscemu/wine";
126 /* check for empty string in WINE_DBG_NO_XTERM */
127 if (dbg_no_xterm && (strlen(dbg_no_xterm) < 1))
128 dbg_no_xterm = NULL;
130 /* clear the buffer */
131 memset(pid_string, 0, DBG_BUFF_SIZE);
133 /* make pid into string */
134 snprintf(pid_string, sizeof(pid_string), "%ld", (long) attach_pid);
136 /* now exec the debugger to get it's own clean memory space */
137 if (dbg_no_xterm)
138 status = execlp(dbg_external, dbg_external, dbg_wine_location, pid_string, NULL);
139 else
140 status = execlp("xterm", "xterm", "-e", dbg_external, dbg_wine_location, pid_string, NULL);
142 if (status == -1)
144 if (dbg_no_xterm)
145 fprintf(stderr, "DEBUG_ExternalDebugger failed to execute \"%s %s %s\" (%s)\n",
146 dbg_external, dbg_wine_location, pid_string, strerror(errno));
147 else
148 fprintf(stderr, "DEBUG_ExternalDebugger failed to execute \"xterm -e %s %s %s\" (%s)\n",
149 dbg_external, dbg_wine_location, pid_string, strerror(errno));
153 else if (child_pid != -1)
155 /* make the parent/caller sleep so the child/debugger can catch it */
156 sleep(dbg_sleep_secs);
158 else
159 fprintf(stderr, "DEBUG_ExternalDebugger failed.\n");