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