Commit | Line | Data |
---|---|---|
ca4e7e14 | 1 | /* Startup code for gdbtk. |
a5f4fbff | 2 | Copyright 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc. |
4604b34c SG |
3 | |
4 | Written by Stu Grossman <grossman@cygnus.com> of Cygnus Support. | |
754e5da2 SG |
5 | |
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
6c9638b4 | 20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
754e5da2 SG |
21 | |
22 | #include "defs.h" | |
23 | #include "symtab.h" | |
24 | #include "inferior.h" | |
25 | #include "command.h" | |
26 | #include "bfd.h" | |
27 | #include "symfile.h" | |
28 | #include "objfiles.h" | |
29 | #include "target.h" | |
018d76dd KS |
30 | #include "gdbcore.h" |
31 | #include "tracepoint.h" | |
929db6e5 | 32 | #include "demangle.h" |
018d76dd KS |
33 | |
34 | #ifdef _WIN32 | |
35 | #include <winuser.h> | |
36 | #endif | |
37 | ||
e6e9507d EZ |
38 | #include <sys/stat.h> |
39 | ||
754e5da2 SG |
40 | #include <tcl.h> |
41 | #include <tk.h> | |
2476848a MH |
42 | #include <itcl.h> |
43 | #include <tix.h> | |
dd3dd918 | 44 | #include "guitcl.h" |
ca4e7e14 | 45 | #include "gdbtk.h" |
2476848a MH |
46 | |
47 | #ifdef IDE | |
018d76dd | 48 | /* start-sanitize-ide */ |
2476848a MH |
49 | #include "event.h" |
50 | #include "idetcl.h" | |
018d76dd KS |
51 | #include "ilutk.h" |
52 | /* end-sanitize-ide */ | |
2476848a MH |
53 | #endif |
54 | ||
73d3dbd4 | 55 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
56 | #include <stdarg.h> |
57 | #else | |
cd2df226 | 58 | #include <varargs.h> |
85c613aa | 59 | #endif |
cd2df226 SG |
60 | #include <signal.h> |
61 | #include <fcntl.h> | |
8532893d | 62 | #include <unistd.h> |
86db943c SG |
63 | #include <setjmp.h> |
64 | #include "top.h" | |
736a82e7 | 65 | #include <sys/ioctl.h> |
2b576293 | 66 | #include "gdb_string.h" |
09722039 | 67 | #include "dis-asm.h" |
6131622e SG |
68 | #include <stdio.h> |
69 | #include "gdbcmd.h" | |
736a82e7 | 70 | |
929db6e5 | 71 | #include "annotate.h" |
018d76dd | 72 | #include <sys/time.h> |
018d76dd | 73 | |
ca4e7e14 JI |
74 | /* For Cygwin32, we use a timer to periodically check for Windows |
75 | messages. FIXME: It would be better to not poll, but to instead | |
76 | rewrite the target_wait routines to serve as input sources. | |
77 | Unfortunately, that will be a lot of work. */ | |
78 | static sigset_t nullsigmask; | |
79 | static struct sigaction act1, act2; | |
80 | static struct itimerval it_on, it_off; | |
8b3f9ed6 | 81 | |
94a6f14f MH |
82 | extern int Tktable_Init PARAMS ((Tcl_Interp *interp)); |
83 | ||
2476848a | 84 | static void gdbtk_init PARAMS ((char *)); |
ca4e7e14 | 85 | void gdbtk_interactive PARAMS ((void)); |
b607efe7 FF |
86 | static void cleanup_init PARAMS ((int)); |
87 | static void tk_command PARAMS ((char *, int)); | |
ca4e7e14 | 88 | |
6f5af15b | 89 | void gdbtk_add_hooks PARAMS ((void)); |
ca4e7e14 JI |
90 | int gdbtk_test PARAMS ((char *)); |
91 | ||
92 | /* | |
93 | * gdbtk_fputs is defined in the gdbtk_hooks.c, but we need it here | |
94 | * because we delay adding this hook till all the setup is done. That | |
95 | * way errors will go to stdout. | |
96 | */ | |
97 | ||
98 | extern void gdbtk_fputs PARAMS ((const char *, FILE *)); | |
b607efe7 | 99 | |
754e5da2 | 100 | /* Handle for TCL interpreter */ |
7f6cb62e | 101 | Tcl_Interp *gdbtk_interp = NULL; |
754e5da2 | 102 | |
0776b0b0 | 103 | static int gdbtk_timer_going = 0; |
0776b0b0 | 104 | |
4ff5d55a MH |
105 | /* linked variable used to tell tcl what the current thread is */ |
106 | int gdb_context = 0; | |
107 | ||
ca4e7e14 JI |
108 | /* This variable is true when the inferior is running. See note in |
109 | * gdbtk.h for details. | |
110 | */ | |
ca4e7e14 | 111 | int running_now; |
fda6fadc | 112 | |
7f6cb62e KS |
113 | /* This variable holds the name of a Tcl file which should be sourced by the |
114 | interpreter when it goes idle at startup. Used with the testsuite. */ | |
ca4e7e14 JI |
115 | static char *gdbtk_source_filename = NULL; |
116 | \f | |
9b119644 ILT |
117 | #ifndef _WIN32 |
118 | ||
119 | /* Supply malloc calls for tcl/tk. We do not want to do this on | |
120 | Windows, because Tcl_Alloc is probably in a DLL which will not call | |
121 | the mmalloc routines. */ | |
8c19daa1 SG |
122 | |
123 | char * | |
a5a6e3bd | 124 | Tcl_Alloc (size) |
8c19daa1 SG |
125 | unsigned int size; |
126 | { | |
127 | return xmalloc (size); | |
128 | } | |
129 | ||
130 | char * | |
131 | Tcl_Realloc (ptr, size) | |
132 | char *ptr; | |
133 | unsigned int size; | |
134 | { | |
135 | return xrealloc (ptr, size); | |
136 | } | |
137 | ||
138 | void | |
139 | Tcl_Free(ptr) | |
140 | char *ptr; | |
141 | { | |
142 | free (ptr); | |
143 | } | |
144 | ||
018d76dd | 145 | #endif /* ! _WIN32 */ |
9b119644 | 146 | |
018d76dd KS |
147 | #ifdef _WIN32 |
148 | ||
149 | /* On Windows, if we hold a file open, other programs can't write to | |
ca4e7e14 JI |
150 | * it. In particular, we don't want to hold the executable open, |
151 | * because it will mean that people have to get out of the debugging | |
152 | * session in order to remake their program. So we close it, although | |
153 | * this will cost us if and when we need to reopen it. | |
154 | */ | |
018d76dd | 155 | |
ca4e7e14 | 156 | void |
018d76dd KS |
157 | close_bfds () |
158 | { | |
159 | struct objfile *o; | |
160 | ||
161 | ALL_OBJFILES (o) | |
162 | { | |
163 | if (o->obfd != NULL) | |
164 | bfd_cache_close (o->obfd); | |
165 | } | |
166 | ||
167 | if (exec_bfd != NULL) | |
168 | bfd_cache_close (exec_bfd); | |
169 | } | |
170 | ||
171 | #endif /* _WIN32 */ | |
172 | ||
754e5da2 | 173 | \f |
ca4e7e14 JI |
174 | /* TclDebug (const char *fmt, ...) works just like printf() but |
175 | * sends the output to the GDB TK debug window. | |
176 | * Not for normal use; just a convenient tool for debugging | |
177 | */ | |
41756e56 | 178 | |
ca4e7e14 | 179 | void |
41756e56 | 180 | #ifdef ANSI_PROTOTYPES |
ca4e7e14 | 181 | TclDebug (const char *fmt, ...) |
41756e56 | 182 | #else |
ca4e7e14 | 183 | TclDebug (va_alist) |
41756e56 FF |
184 | va_dcl |
185 | #endif | |
186 | { | |
187 | va_list args; | |
ca4e7e14 | 188 | char buf[512], *v[2], *merge; |
41756e56 FF |
189 | |
190 | #ifdef ANSI_PROTOTYPES | |
ca4e7e14 | 191 | va_start (args, fmt); |
41756e56 | 192 | #else |
ca4e7e14 | 193 | char *fmt; |
41756e56 | 194 | va_start (args); |
ca4e7e14 | 195 | fmt = va_arg (args, char *); |
41756e56 FF |
196 | #endif |
197 | ||
ca4e7e14 JI |
198 | v[0] = "debug"; |
199 | v[1] = buf; | |
41756e56 | 200 | |
ca4e7e14 JI |
201 | vsprintf (buf, fmt, args); |
202 | va_end (args); | |
018d76dd | 203 | |
ca4e7e14 JI |
204 | merge = Tcl_Merge (2, v); |
205 | Tcl_Eval (gdbtk_interp, merge); | |
206 | Tcl_Free (merge); | |
41756e56 FF |
207 | } |
208 | ||
ca4e7e14 JI |
209 | \f |
210 | /* | |
211 | * The rest of this file contains the start-up, and event handling code for gdbtk. | |
212 | */ | |
213 | ||
214 | /* | |
215 | * This cleanup function is added to the cleanup list that surrounds the Tk | |
216 | * main in gdbtk_init. It deletes the Tcl interpreter. | |
217 | */ | |
218 | ||
41756e56 | 219 | static void |
ca4e7e14 JI |
220 | cleanup_init (ignored) |
221 | int ignored; | |
41756e56 | 222 | { |
ca4e7e14 JI |
223 | if (gdbtk_interp != NULL) |
224 | Tcl_DeleteInterp (gdbtk_interp); | |
225 | gdbtk_interp = NULL; | |
41756e56 FF |
226 | } |
227 | ||
ca4e7e14 JI |
228 | /* Come here during long calculations to check for GUI events. Usually invoked |
229 | via the QUIT macro. */ | |
230 | ||
231 | void | |
232 | gdbtk_interactive () | |
929db6e5 | 233 | { |
ca4e7e14 | 234 | /* Tk_DoOneEvent (TK_DONT_WAIT|TK_IDLE_EVENTS); */ |
929db6e5 EZ |
235 | } |
236 | ||
ca4e7e14 JI |
237 | |
238 | void | |
239 | gdbtk_start_timer () | |
6131622e | 240 | { |
ca4e7e14 JI |
241 | static int first = 1; |
242 | /*TclDebug ("Starting timer....");*/ | |
243 | if (first) | |
244 | { | |
245 | /* first time called, set up all the structs */ | |
246 | first = 0; | |
247 | sigemptyset (&nullsigmask); | |
85c613aa | 248 | |
ca4e7e14 JI |
249 | act1.sa_handler = x_event; |
250 | act1.sa_mask = nullsigmask; | |
251 | act1.sa_flags = 0; | |
6131622e | 252 | |
ca4e7e14 JI |
253 | act2.sa_handler = SIG_IGN; |
254 | act2.sa_mask = nullsigmask; | |
255 | act2.sa_flags = 0; | |
6131622e | 256 | |
ca4e7e14 JI |
257 | it_on.it_interval.tv_sec = 0; |
258 | it_on.it_interval.tv_usec = 250000; /* .25 sec */ | |
259 | it_on.it_value.tv_sec = 0; | |
260 | it_on.it_value.tv_usec = 250000; | |
6131622e | 261 | |
ca4e7e14 JI |
262 | it_off.it_interval.tv_sec = 0; |
263 | it_off.it_interval.tv_usec = 0; | |
264 | it_off.it_value.tv_sec = 0; | |
265 | it_off.it_value.tv_usec = 0; | |
266 | } | |
267 | ||
268 | if (!gdbtk_timer_going) | |
269 | { | |
270 | sigaction (SIGALRM, &act1, NULL); | |
271 | setitimer (ITIMER_REAL, &it_on, NULL); | |
272 | gdbtk_timer_going = 1; | |
273 | } | |
6131622e SG |
274 | } |
275 | ||
ca4e7e14 JI |
276 | void |
277 | gdbtk_stop_timer () | |
8a19b35a | 278 | { |
ca4e7e14 | 279 | if (gdbtk_timer_going) |
8a19b35a | 280 | { |
ca4e7e14 JI |
281 | gdbtk_timer_going = 0; |
282 | /*TclDebug ("Stopping timer.");*/ | |
283 | setitimer (ITIMER_REAL, &it_off, NULL); | |
284 | sigaction (SIGALRM, &act2, NULL); | |
8a19b35a | 285 | } |
8a19b35a MH |
286 | } |
287 | ||
ca4e7e14 | 288 | /* gdbtk_init installs this function as a final cleanup. */ |
6131622e | 289 | |
ca4e7e14 JI |
290 | static void |
291 | gdbtk_cleanup (dummy) | |
292 | PTR dummy; | |
293 | { | |
4d14b252 | 294 | Tcl_Eval (gdbtk_interp, "gdbtk_cleanup"); |
ca4e7e14 | 295 | #ifdef IDE |
1dd251f9 DM |
296 | { |
297 | struct ide_event_handle *h = (struct ide_event_handle *) dummy; | |
298 | ide_interface_deregister_all (h); | |
299 | } | |
ca4e7e14 JI |
300 | #endif |
301 | Tcl_Finalize (); | |
6131622e SG |
302 | } |
303 | ||
ca4e7e14 JI |
304 | /* Initialize gdbtk. This involves creating a Tcl interpreter, |
305 | * defining all the Tcl commands that the GUI will use, pointing | |
306 | * all the gdb "hooks" to the correct functions, | |
307 | * and setting the Tcl auto loading environment so that we can find all | |
308 | * the Tcl based library files. | |
309 | */ | |
310 | ||
754e5da2 | 311 | static void |
ca4e7e14 JI |
312 | gdbtk_init ( argv0 ) |
313 | char *argv0; | |
754e5da2 | 314 | { |
ca4e7e14 JI |
315 | struct cleanup *old_chain; |
316 | char *lib, *gdbtk_lib, *gdbtk_lib_tmp, *gdbtk_file; | |
6f5af15b | 317 | int found_main; |
ca4e7e14 | 318 | Tcl_Obj *auto_path_elem, *auto_path_name; |
6f5af15b | 319 | |
ca4e7e14 JI |
320 | #ifdef IDE |
321 | /* start-sanitize-ide */ | |
322 | struct ide_event_handle *h; | |
323 | const char *errmsg; | |
324 | char *libexecdir; | |
325 | /* end-sanitize-ide */ | |
326 | #endif | |
327 | ||
328 | /* If there is no DISPLAY environment variable, Tk_Init below will fail, | |
329 | causing gdb to abort. If instead we simply return here, gdb will | |
330 | gracefully degrade to using the command line interface. */ | |
754e5da2 | 331 | |
ca4e7e14 JI |
332 | #ifndef WINNT |
333 | if (getenv ("DISPLAY") == NULL) | |
754e5da2 | 334 | return; |
ca4e7e14 | 335 | #endif |
754e5da2 | 336 | |
ad3b8c4a | 337 | old_chain = make_cleanup ((make_cleanup_func) cleanup_init, 0); |
929db6e5 | 338 | |
ca4e7e14 JI |
339 | /* First init tcl and tk. */ |
340 | Tcl_FindExecutable (argv0); | |
341 | gdbtk_interp = Tcl_CreateInterp (); | |
754e5da2 | 342 | |
ca4e7e14 JI |
343 | #ifdef TCL_MEM_DEBUG |
344 | Tcl_InitMemory (gdbtk_interp); | |
345 | #endif | |
754e5da2 | 346 | |
ca4e7e14 JI |
347 | if (!gdbtk_interp) |
348 | error ("Tcl_CreateInterp failed"); | |
754e5da2 | 349 | |
ca4e7e14 JI |
350 | if (Tcl_Init(gdbtk_interp) != TCL_OK) |
351 | error ("Tcl_Init failed: %s", gdbtk_interp->result); | |
754e5da2 | 352 | |
ca4e7e14 JI |
353 | #ifndef IDE |
354 | /* For the IDE we register the cleanup later, after we've | |
355 | initialized events. */ | |
356 | make_final_cleanup (gdbtk_cleanup, NULL); | |
746d1df4 SG |
357 | #endif |
358 | ||
ca4e7e14 JI |
359 | /* Initialize the Paths variable. */ |
360 | if (ide_initialize_paths (gdbtk_interp, "gdbtcl") != TCL_OK) | |
361 | error ("ide_initialize_paths failed: %s", gdbtk_interp->result); | |
746d1df4 | 362 | |
ca4e7e14 JI |
363 | #ifdef IDE |
364 | /* start-sanitize-ide */ | |
365 | /* Find the directory where we expect to find idemanager. We ignore | |
366 | errors since it doesn't really matter if this fails. */ | |
367 | libexecdir = Tcl_GetVar2 (gdbtk_interp, "Paths", "libexecdir", TCL_GLOBAL_ONLY); | |
746d1df4 | 368 | |
ca4e7e14 | 369 | IluTk_Init (); |
929db6e5 | 370 | |
ca4e7e14 JI |
371 | h = ide_event_init_from_environment (&errmsg, libexecdir); |
372 | make_final_cleanup (gdbtk_cleanup, h); | |
373 | if (h == NULL) | |
746d1df4 | 374 | { |
ca4e7e14 JI |
375 | Tcl_AppendResult (gdbtk_interp, "can't initialize event system: ", errmsg, |
376 | (char *) NULL); | |
377 | fprintf(stderr, "WARNING: ide_event_init_client failed: %s\n", gdbtk_interp->result); | |
746d1df4 | 378 | |
ca4e7e14 | 379 | Tcl_SetVar (gdbtk_interp, "IDE_ENABLED", "0", 0); |
746d1df4 | 380 | } |
ca4e7e14 | 381 | else |
3d9f68c0 | 382 | { |
ca4e7e14 JI |
383 | if (ide_create_tclevent_command (gdbtk_interp, h) != TCL_OK) |
384 | error ("ide_create_tclevent_command failed: %s", gdbtk_interp->result); | |
0422b59e | 385 | |
ca4e7e14 JI |
386 | if (ide_create_edit_command (gdbtk_interp, h) != TCL_OK) |
387 | error ("ide_create_edit_command failed: %s", gdbtk_interp->result); | |
388 | ||
389 | if (ide_create_property_command (gdbtk_interp, h) != TCL_OK) | |
390 | error ("ide_create_property_command failed: %s", gdbtk_interp->result); | |
018d76dd | 391 | |
ca4e7e14 JI |
392 | if (ide_create_build_command (gdbtk_interp, h) != TCL_OK) |
393 | error ("ide_create_build_command failed: %s", gdbtk_interp->result); | |
754e5da2 | 394 | |
ca4e7e14 JI |
395 | if (ide_create_window_register_command (gdbtk_interp, h, "gdb-restore") |
396 | != TCL_OK) | |
397 | error ("ide_create_window_register_command failed: %s", | |
398 | gdbtk_interp->result); | |
fda6fadc | 399 | |
ca4e7e14 JI |
400 | if (ide_create_window_command (gdbtk_interp, h) != TCL_OK) |
401 | error ("ide_create_window_command failed: %s", gdbtk_interp->result); | |
0b7148e4 | 402 | |
ca4e7e14 JI |
403 | if (ide_create_exit_command (gdbtk_interp, h) != TCL_OK) |
404 | error ("ide_create_exit_command failed: %s", gdbtk_interp->result); | |
018d76dd | 405 | |
ca4e7e14 JI |
406 | if (ide_create_help_command (gdbtk_interp) != TCL_OK) |
407 | error ("ide_create_help_command failed: %s", gdbtk_interp->result); | |
018d76dd | 408 | |
ca4e7e14 JI |
409 | /* |
410 | if (ide_initialize (gdbtk_interp, "gdb") != TCL_OK) | |
411 | error ("ide_initialize failed: %s", gdbtk_interp->result); | |
412 | */ | |
479f0f18 | 413 | |
ca4e7e14 | 414 | Tcl_SetVar (gdbtk_interp, "IDE_ENABLED", "1", 0); |
7234efcb | 415 | } |
ca4e7e14 JI |
416 | /* end-sanitize-ide */ |
417 | #else | |
418 | Tcl_SetVar (gdbtk_interp, "IDE_ENABLED", "0", 0); | |
419 | #endif /* IDE */ | |
0776b0b0 | 420 | |
ca4e7e14 JI |
421 | /* We don't want to open the X connection until we've done all the |
422 | IDE initialization. Otherwise, goofy looking unfinished windows | |
423 | pop up when ILU drops into the TCL event loop. */ | |
86db943c | 424 | |
ca4e7e14 JI |
425 | if (Tk_Init(gdbtk_interp) != TCL_OK) |
426 | error ("Tk_Init failed: %s", gdbtk_interp->result); | |
86db943c | 427 | |
ca4e7e14 JI |
428 | if (Itcl_Init(gdbtk_interp) == TCL_ERROR) |
429 | error ("Itcl_Init failed: %s", gdbtk_interp->result); | |
430 | Tcl_StaticPackage(gdbtk_interp, "Tktable", Tktable_Init, | |
431 | (Tcl_PackageInitProc *) NULL); | |
09722039 | 432 | |
ca4e7e14 JI |
433 | if (Tix_Init(gdbtk_interp) != TCL_OK) |
434 | error ("Tix_Init failed: %s", gdbtk_interp->result); | |
435 | Tcl_StaticPackage(gdbtk_interp, "Tktable", Tktable_Init, | |
436 | (Tcl_PackageInitProc *) NULL); | |
86db943c | 437 | |
ca4e7e14 JI |
438 | if (Tktable_Init(gdbtk_interp) != TCL_OK) |
439 | error ("Tktable_Init failed: %s", gdbtk_interp->result); | |
929db6e5 | 440 | |
ca4e7e14 JI |
441 | Tcl_StaticPackage(gdbtk_interp, "Tktable", Tktable_Init, |
442 | (Tcl_PackageInitProc *) NULL); | |
443 | /* | |
444 | * These are the commands to do some Windows Specific stuff... | |
445 | */ | |
018d76dd | 446 | |
ca4e7e14 JI |
447 | #ifdef __CYGWIN32__ |
448 | if (ide_create_messagebox_command (gdbtk_interp) != TCL_OK) | |
449 | error ("messagebox command initialization failed"); | |
450 | /* On Windows, create a sizebox widget command */ | |
451 | if (ide_create_sizebox_command (gdbtk_interp) != TCL_OK) | |
452 | error ("sizebox creation failed"); | |
453 | if (ide_create_winprint_command (gdbtk_interp) != TCL_OK) | |
454 | error ("windows print code initialization failed"); | |
018d76dd | 455 | /* start-sanitize-ide */ |
ca4e7e14 JI |
456 | /* An interface to ShellExecute. */ |
457 | if (ide_create_shell_execute_command (gdbtk_interp) != TCL_OK) | |
458 | error ("shell execute command initialization failed"); | |
018d76dd | 459 | /* end-sanitize-ide */ |
ca4e7e14 JI |
460 | if (ide_create_win_grab_command (gdbtk_interp) != TCL_OK) |
461 | error ("grab support command initialization failed"); | |
462 | /* Path conversion functions. */ | |
463 | if (ide_create_cygwin_path_command (gdbtk_interp) != TCL_OK) | |
464 | error ("cygwin path command initialization failed"); | |
465 | #endif | |
018d76dd | 466 | |
ca4e7e14 JI |
467 | /* |
468 | * This adds all the Gdbtk commands. | |
469 | */ | |
470 | ||
471 | if (Gdbtk_Init(gdbtk_interp) != TCL_OK) | |
018d76dd | 472 | { |
ca4e7e14 | 473 | error("Gdbtk_Init failed: %s", gdbtk_interp->result); |
018d76dd | 474 | } |
4f17e6eb | 475 | |
ca4e7e14 JI |
476 | Tcl_StaticPackage(gdbtk_interp, "Gdbtk", Gdbtk_Init, NULL); |
477 | ||
478 | /* This adds all the hooks that call up from the bowels of gdb | |
479 | * back into Tcl-land... | |
480 | */ | |
e0f7db02 | 481 | |
ca4e7e14 JI |
482 | gdbtk_add_hooks(); |
483 | ||
484 | /* Add a back door to Tk from the gdb console... */ | |
e0f7db02 | 485 | |
ca4e7e14 JI |
486 | add_com ("tk", class_obscure, tk_command, |
487 | "Send a command directly into tk."); | |
e0f7db02 | 488 | |
ca4e7e14 | 489 | /* find the gdb tcl library and source main.tcl */ |
929db6e5 | 490 | |
ca4e7e14 JI |
491 | gdbtk_lib = getenv ("GDBTK_LIBRARY"); |
492 | if (!gdbtk_lib) | |
6f5af15b MH |
493 | { |
494 | if (access ("gdbtcl/main.tcl", R_OK) == 0) | |
495 | gdbtk_lib = "gdbtcl"; | |
496 | else | |
497 | gdbtk_lib = GDBTK_LIBRARY; | |
498 | } | |
499 | ||
ca4e7e14 | 500 | gdbtk_lib_tmp = xstrdup (gdbtk_lib); |
929db6e5 | 501 | |
ca4e7e14 JI |
502 | found_main = 0; |
503 | /* see if GDBTK_LIBRARY is a path list */ | |
504 | lib = strtok (gdbtk_lib_tmp, GDBTK_PATH_SEP); | |
929db6e5 | 505 | |
ca4e7e14 | 506 | auto_path_name = Tcl_NewStringObj ("auto_path", -1); |
929db6e5 | 507 | |
ca4e7e14 | 508 | do |
929db6e5 | 509 | { |
ca4e7e14 JI |
510 | auto_path_elem = Tcl_NewStringObj (lib, -1); |
511 | if (Tcl_ObjSetVar2 (gdbtk_interp, auto_path_name, NULL, auto_path_elem, | |
512 | TCL_GLOBAL_ONLY | TCL_APPEND_VALUE | TCL_LIST_ELEMENT ) == NULL) | |
929db6e5 | 513 | { |
ca4e7e14 JI |
514 | fputs_unfiltered (Tcl_GetVar (gdbtk_interp, "errorInfo", 0), gdb_stderr); |
515 | error (""); | |
929db6e5 | 516 | } |
ca4e7e14 | 517 | if (!found_main) |
929db6e5 | 518 | { |
ca4e7e14 JI |
519 | gdbtk_file = concat (lib, "/main.tcl", (char *) NULL); |
520 | if (access (gdbtk_file, R_OK) == 0) | |
929db6e5 | 521 | { |
ca4e7e14 JI |
522 | found_main++; |
523 | Tcl_SetVar (gdbtk_interp, "GDBTK_LIBRARY", lib, 0); | |
929db6e5 EZ |
524 | } |
525 | } | |
ca4e7e14 JI |
526 | } |
527 | while ((lib = strtok (NULL, ":")) != NULL); | |
929db6e5 | 528 | |
ca4e7e14 JI |
529 | free (gdbtk_lib_tmp); |
530 | Tcl_DecrRefCount(auto_path_name); | |
e0f7db02 | 531 | |
ca4e7e14 | 532 | if (!found_main) |
e6e9507d | 533 | { |
ca4e7e14 | 534 | /* Try finding it with the auto path. */ |
929db6e5 | 535 | |
ca4e7e14 JI |
536 | static const char script[] ="\ |
537 | proc gdbtk_find_main {} {\n\ | |
538 | global auto_path GDBTK_LIBRARY\n\ | |
539 | foreach dir $auto_path {\n\ | |
540 | set f [file join $dir main.tcl]\n\ | |
541 | if {[file exists $f]} then {\n\ | |
542 | set GDBTK_LIBRARY $dir\n\ | |
543 | return $f\n\ | |
544 | }\n\ | |
545 | }\n\ | |
546 | return ""\n\ | |
547 | }\n\ | |
548 | gdbtk_find_main"; | |
929db6e5 | 549 | |
ca4e7e14 JI |
550 | if (Tcl_GlobalEval (gdbtk_interp, (char *) script) != TCL_OK) |
551 | { | |
552 | fputs_unfiltered (Tcl_GetVar (gdbtk_interp, "errorInfo", 0), gdb_stderr); | |
553 | error (""); | |
554 | } | |
929db6e5 | 555 | |
ca4e7e14 | 556 | if (gdbtk_interp->result[0] != '\0') |
929db6e5 | 557 | { |
ca4e7e14 JI |
558 | gdbtk_file = xstrdup (gdbtk_interp->result); |
559 | found_main++; | |
929db6e5 EZ |
560 | } |
561 | } | |
11f91b2b | 562 | |
ca4e7e14 | 563 | if (!found_main) |
929db6e5 | 564 | { |
ca4e7e14 JI |
565 | fputs_unfiltered_hook = NULL; /* Force errors to stdout/stderr */ |
566 | if (getenv("GDBTK_LIBRARY")) | |
929db6e5 | 567 | { |
ca4e7e14 JI |
568 | fprintf_unfiltered (stderr, "Unable to find main.tcl in %s\n",getenv("GDBTK_LIBRARY")); |
569 | fprintf_unfiltered (stderr, | |
570 | "Please set GDBTK_LIBRARY to a path that includes the GDB tcl files.\n"); | |
929db6e5 EZ |
571 | } |
572 | else | |
573 | { | |
ca4e7e14 JI |
574 | fprintf_unfiltered (stderr, "Unable to find main.tcl in %s\n", GDBTK_LIBRARY); |
575 | fprintf_unfiltered (stderr, "You might want to set GDBTK_LIBRARY\n"); | |
929db6e5 | 576 | } |
ca4e7e14 | 577 | error(""); |
929db6e5 | 578 | } |
11f91b2b | 579 | |
ca4e7e14 JI |
580 | /* Defer setup of fputs_unfiltered_hook to near the end so that error messages |
581 | prior to this point go to stdout/stderr. */ | |
929db6e5 | 582 | |
ca4e7e14 | 583 | fputs_unfiltered_hook = gdbtk_fputs; |
929db6e5 | 584 | |
ca4e7e14 JI |
585 | /* start-sanitize-tclpro */ |
586 | #ifdef TCLPRO_DEBUGGER | |
587 | { | |
588 | Tcl_DString source_cmd; | |
929db6e5 | 589 | |
ca4e7e14 JI |
590 | Tcl_DStringInit (&source_cmd); |
591 | Tcl_DStringAppend (&source_cmd, | |
592 | "if {[info exists env(DEBUG_STUB)]} {source $env(DEBUG_STUB); " -1); | |
593 | Tcl_DStringAppend (&source_cmd, "debugger_init; debugger_eval {source {", -1); | |
594 | Tcl_DStringAppend (&source_cmd, gdbtk_file, -1); | |
595 | Tcl_DStringAppend (&source_cmd, "}}} else {source {", -1); | |
596 | Tcl_DStringAppend (&source_cmd, gdbtk_file, -1); | |
597 | Tcl_DStringAppend (&source_cmd, "}}", -1); | |
598 | if (Tcl_GlobalEval (gdbtk_interp, Tcl_DStringValue (&source_cmd)) != TCL_OK) | |
599 | #else | |
600 | /* end-sanitize-tclpro */ | |
601 | if (Tcl_EvalFile (gdbtk_interp, gdbtk_file) != TCL_OK) | |
602 | /* start-sanitize-tclpro */ | |
603 | #endif | |
604 | /* end-sanitize-tclpro */ | |
605 | { | |
606 | char *msg; | |
929db6e5 | 607 | |
ca4e7e14 JI |
608 | /* Force errorInfo to be set up propertly. */ |
609 | Tcl_AddErrorInfo (gdbtk_interp, ""); | |
e0f7db02 | 610 | |
ca4e7e14 | 611 | msg = Tcl_GetVar (gdbtk_interp, "errorInfo", TCL_GLOBAL_ONLY); |
4f17e6eb | 612 | |
ca4e7e14 | 613 | fputs_unfiltered_hook = NULL; /* Force errors to stdout/stderr */ |
7f6cb62e | 614 | |
ca4e7e14 JI |
615 | #ifdef _WIN32 |
616 | MessageBox (NULL, msg, NULL, MB_OK | MB_ICONERROR | MB_TASKMODAL); | |
617 | #else | |
618 | fputs_unfiltered (msg, gdb_stderr); | |
619 | #endif | |
7f6cb62e | 620 | |
ca4e7e14 | 621 | error (""); |
7f6cb62e | 622 | } |
ca4e7e14 JI |
623 | /* start-sanitize-tclpro */ |
624 | #ifdef TCLPRO_DEBUGGER | |
625 | Tcl_DStringFree(&source_cmd); | |
7f6cb62e | 626 | } |
ca4e7e14 JI |
627 | #endif |
628 | /* end-sanitize-tclpro */ | |
7f6cb62e | 629 | |
ca4e7e14 JI |
630 | #ifdef IDE |
631 | /* start-sanitize-ide */ | |
632 | /* Don't do this until we have initialized. Otherwise, we may get a | |
633 | run command before we are ready for one. */ | |
634 | if (ide_run_server_init (gdbtk_interp, h) != TCL_OK) | |
635 | error ("ide_run_server_init failed: %s", gdbtk_interp->result); | |
636 | /* end-sanitize-ide */ | |
637 | #endif | |
7f6cb62e | 638 | |
ca4e7e14 | 639 | free (gdbtk_file); |
7f6cb62e | 640 | |
ca4e7e14 JI |
641 | /* Now source in the filename provided by the --tclcommand option. |
642 | This is mostly used for the gdbtk testsuite... */ | |
7f6cb62e | 643 | |
ca4e7e14 JI |
644 | if (gdbtk_source_filename != NULL) |
645 | { | |
646 | char *s = "after idle source "; | |
647 | char *script = concat (s, gdbtk_source_filename, (char *) NULL); | |
648 | Tcl_Eval (gdbtk_interp, script); | |
649 | free (gdbtk_source_filename); | |
650 | free (script); | |
651 | } | |
652 | ||
653 | ||
654 | discard_cleanups (old_chain); | |
7f6cb62e KS |
655 | } |
656 | ||
ca4e7e14 JI |
657 | /* gdbtk_test is used in main.c to validate the -tclcommand option to |
658 | gdb, which sources in a file of tcl code after idle during the | |
659 | startup procedure. */ | |
660 | ||
7f6cb62e KS |
661 | int |
662 | gdbtk_test (filename) | |
663 | char *filename; | |
664 | { | |
665 | if (access (filename, R_OK) != 0) | |
666 | return 0; | |
667 | else | |
668 | gdbtk_source_filename = xstrdup (filename); | |
669 | return 1; | |
670 | } | |
ca4e7e14 | 671 | |
3f37b696 | 672 | /* Come here during initialize_all_files () */ |
754e5da2 SG |
673 | |
674 | void | |
675 | _initialize_gdbtk () | |
676 | { | |
c5197511 SG |
677 | if (use_windows) |
678 | { | |
679 | /* Tell the rest of the world that Gdbtk is now set up. */ | |
754e5da2 | 680 | |
c5197511 | 681 | init_ui_hook = gdbtk_init; |
47792960 KS |
682 | #ifdef __CYGWIN32__ |
683 | (void) FreeConsole (); | |
684 | #endif | |
c5197511 | 685 | } |
cb432079 EZ |
686 | #ifdef __CYGWIN32__ |
687 | else | |
688 | { | |
689 | DWORD ft = GetFileType (GetStdHandle (STD_INPUT_HANDLE)); | |
690 | void cygwin32_attach_handle_to_fd (char *, int, HANDLE, int, int); | |
691 | ||
692 | switch (ft) | |
693 | { | |
694 | case FILE_TYPE_DISK: | |
695 | case FILE_TYPE_CHAR: | |
696 | case FILE_TYPE_PIPE: | |
697 | break; | |
698 | default: | |
699 | AllocConsole(); | |
700 | cygwin32_attach_handle_to_fd ("/dev/conin", 0, | |
701 | GetStdHandle (STD_INPUT_HANDLE), | |
702 | 1, GENERIC_READ); | |
703 | cygwin32_attach_handle_to_fd ("/dev/conout", 1, | |
704 | GetStdHandle (STD_OUTPUT_HANDLE), | |
705 | 0, GENERIC_WRITE); | |
706 | cygwin32_attach_handle_to_fd ("/dev/conout", 2, | |
707 | GetStdHandle (STD_ERROR_HANDLE), | |
708 | 0, GENERIC_WRITE); | |
709 | break; | |
710 | } | |
711 | } | |
712 | #endif | |
754e5da2 | 713 | } |
ca4e7e14 JI |
714 | |
715 | static void | |
716 | tk_command (cmd, from_tty) | |
717 | char *cmd; | |
718 | int from_tty; | |
719 | { | |
720 | int retval; | |
721 | char *result; | |
722 | struct cleanup *old_chain; | |
723 | ||
724 | /* Catch case of no argument, since this will make the tcl interpreter dump core. */ | |
725 | if (cmd == NULL) | |
726 | error_no_arg ("tcl command to interpret"); | |
727 | ||
728 | retval = Tcl_Eval (gdbtk_interp, cmd); | |
729 | ||
730 | result = strdup (gdbtk_interp->result); | |
731 | ||
732 | old_chain = make_cleanup (free, result); | |
733 | ||
734 | if (retval != TCL_OK) | |
735 | error (result); | |
736 | ||
737 | printf_unfiltered ("%s\n", result); | |
738 | ||
739 | do_cleanups (old_chain); | |
740 | } | |
741 |