1 /* TK interface routines.
2 Copyright 1994 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #include <sys/types.h>
30 #include <sys/param.h>
34 #include <sys/filio.h>
37 #include <sys/errno.h>
44 /* Non-zero means that we're doing the gdbtk interface. */
47 /* Non-zero means we are reloading breakpoints, etc from the
48 Gdbtk kernel, and we should suppress various messages */
49 static int gdbtk_reloading
= 0;
51 /* Handle for TCL interpreter */
52 static Tcl_Interp
*interp
= NULL
;
54 /* Handle for TK main window */
55 static Tk_Window mainWindow
= NULL
;
64 /* This routine redirects the output of fputs_unfiltered so that
65 the user can see what's going on in his debugger window. */
67 static char holdbuf
[200];
68 static char *holdbufp
= holdbuf
;
69 static int holdfree
= sizeof (holdbuf
);
74 if (holdbufp
== holdbuf
)
77 Tcl_VarEval (interp
, "gdbtk_tcl_fputs ", "{", holdbuf
, "}", NULL
);
79 holdfree
= sizeof (holdbuf
);
88 Tcl_VarEval (interp
, "gdbtk_tcl_flush", NULL
);
97 len
= strlen (ptr
) + 1;
103 if (len
> sizeof (holdbuf
))
105 Tcl_VarEval (interp
, "gdbtk_tcl_fputs ", "{", ptr
, "}", NULL
);
110 strncpy (holdbufp
, ptr
, len
);
123 query
= va_arg (args
, char *);
125 vsprintf(buf
, query
, args
);
126 Tcl_VarEval (interp
, "gdbtk_tcl_query ", "{", buf
, "}", NULL
);
128 val
= atol (interp
->result
);
133 full_filename(symtab
)
134 struct symtab
*symtab
;
142 if (symtab
->fullname
)
143 return savestring(symtab
->fullname
, strlen(symtab
->fullname
));
145 if (symtab
->filename
[0] == '/')
146 return savestring(symtab
->filename
, strlen(symtab
->filename
));
149 pathlen
= strlen(symtab
->dirname
);
152 if (symtab
->filename
)
153 pathlen
+= strlen(symtab
->filename
);
155 filename
= xmalloc(pathlen
+1);
158 strcpy(filename
, symtab
->dirname
);
161 if (symtab
->filename
)
162 strcat(filename
, symtab
->filename
);
168 breakpoint_notify(b
, action
)
169 struct breakpoint
*b
;
173 char bpnum
[50], line
[50], pc
[50];
174 struct symtab_and_line sal
;
178 if (b
->type
!= bp_breakpoint
)
181 sal
= find_pc_line (b
->address
, 0);
183 filename
= full_filename (sal
.symtab
);
185 sprintf (bpnum
, "%d", b
->number
);
186 sprintf (line
, "%d", sal
.line
);
187 sprintf (pc
, "0x%x", b
->address
);
189 v
= Tcl_VarEval (interp
,
190 "gdbtk_tcl_breakpoint ",
200 gdbtk_fputs (interp
->result
);
209 gdbtk_create_breakpoint(b
)
210 struct breakpoint
*b
;
212 breakpoint_notify(b
, "create");
216 gdbtk_delete_breakpoint(b
)
217 struct breakpoint
*b
;
219 breakpoint_notify(b
, "delete");
223 gdbtk_enable_breakpoint(b
)
224 struct breakpoint
*b
;
226 breakpoint_notify(b
, "enable");
230 gdbtk_disable_breakpoint(b
)
231 struct breakpoint
*b
;
233 breakpoint_notify(b
, "disable");
236 /* This implements the TCL command `gdb_loc', which returns a list consisting
237 of the source and line number associated with the current pc. */
240 gdb_loc (clientData
, interp
, argc
, argv
)
241 ClientData clientData
;
248 struct symtab_and_line sal
;
254 struct frame_info
*frame
;
257 frame
= get_frame_info (selected_frame
);
259 pc
= frame
? frame
->pc
: stop_pc
;
261 sal
= find_pc_line (pc
, 0);
265 struct symtabs_and_lines sals
;
268 sals
= decode_line_spec (argv
[1], 1);
276 Tcl_SetResult (interp
, "Ambiguous line spec", TCL_STATIC
);
284 Tcl_SetResult (interp
, "wrong # args", TCL_STATIC
);
289 Tcl_AppendElement (interp
, sal
.symtab
->filename
);
291 Tcl_AppendElement (interp
, "");
293 find_pc_partial_function (pc
, &funcname
, NULL
, NULL
);
294 Tcl_AppendElement (interp
, funcname
);
296 filename
= full_filename (sal
.symtab
);
297 Tcl_AppendElement (interp
, filename
);
299 sprintf (buf
, "%d", sal
.line
);
300 Tcl_AppendElement (interp
, buf
); /* line number */
302 sprintf (buf
, "0x%x", pc
);
303 Tcl_AppendElement (interp
, buf
); /* PC */
315 execute_command (cmd
, 1);
317 return 1; /* Indicate success */
320 /* This implements the TCL command `gdb_cmd', which sends it's argument into
321 the GDB command scanner. */
324 gdb_cmd (clientData
, interp
, argc
, argv
)
325 ClientData clientData
;
331 struct cleanup
*old_chain
;
335 Tcl_SetResult (interp
, "wrong # args", TCL_STATIC
);
339 old_chain
= make_cleanup (null_routine
, 0);
341 val
= catch_errors (gdb_cmd_stub
, argv
[1], "", RETURN_MASK_ERROR
);
343 bpstat_do_actions (&stop_bpstat
);
344 do_cleanups (old_chain
);
346 /* Drain all buffered command output */
348 gdb_flush (gdb_stderr
);
349 gdb_flush (gdb_stdout
);
351 /* We could base the return value on val, but that would require most users
352 to use catch. Since GDB errors are already being handled elsewhere, I
353 see no reason to pass them up to the caller. */
359 gdb_listfiles (clientData
, interp
, argc
, argv
)
360 ClientData clientData
;
366 struct objfile
*objfile
;
367 struct partial_symtab
*psymtab
;
369 ALL_PSYMTABS (objfile
, psymtab
)
370 Tcl_AppendElement (interp
, psymtab
->filename
);
376 tk_command (cmd
, from_tty
)
380 Tcl_VarEval (interp
, cmd
, NULL
);
382 gdbtk_fputs (interp
->result
);
387 cleanup_init (ignored
)
390 if (mainWindow
!= NULL
)
391 Tk_DestroyWindow (mainWindow
);
395 Tcl_DeleteInterp (interp
);
402 struct cleanup
*old_chain
;
403 char *gdbtk_filename
;
405 old_chain
= make_cleanup (cleanup_init
, 0);
407 /* First init tcl and tk. */
409 interp
= Tcl_CreateInterp ();
412 error ("Tcl_CreateInterp failed");
414 mainWindow
= Tk_CreateMainWindow (interp
, NULL
, "gdb", "Gdb");
417 return; /* DISPLAY probably not set */
419 if (Tcl_Init(interp
) != TCL_OK
)
420 error ("Tcl_Init failed: %s", interp
->result
);
422 if (Tk_Init(interp
) != TCL_OK
)
423 error ("Tk_Init failed: %s", interp
->result
);
425 Tcl_CreateCommand (interp
, "gdb_cmd", gdb_cmd
, NULL
, NULL
);
426 Tcl_CreateCommand (interp
, "gdb_loc", gdb_loc
, NULL
, NULL
);
427 Tcl_CreateCommand (interp
, "gdb_listfiles", gdb_listfiles
, NULL
, NULL
);
429 gdbtk_filename
= getenv ("GDBTK_FILENAME");
431 if (access ("gdbtk.tcl", R_OK
) == 0)
432 gdbtk_filename
= "gdbtk.tcl";
434 gdbtk_filename
= GDBTK_FILENAME
;
436 if (Tcl_EvalFile (interp
, gdbtk_filename
) != TCL_OK
)
437 error ("Failure reading %s: %s", gdbtk_filename
, interp
->result
);
439 command_loop_hook
= Tk_MainLoop
;
440 fputs_unfiltered_hook
= gdbtk_fputs
;
441 print_frame_info_listing_hook
= null_routine
;
442 query_hook
= gdbtk_query
;
443 flush_hook
= gdbtk_flush
;
444 create_breakpoint_hook
= gdbtk_create_breakpoint
;
445 delete_breakpoint_hook
= gdbtk_delete_breakpoint
;
446 enable_breakpoint_hook
= gdbtk_enable_breakpoint
;
447 disable_breakpoint_hook
= gdbtk_disable_breakpoint
;
449 discard_cleanups (old_chain
);
451 add_com ("tk", class_obscure
, tk_command
,
452 "Send a command directly into tk.");
455 /* Come here during initialze_all_files () */
463 /* Tell the rest of the world that Gdbtk is now set up. */
465 init_ui_hook
= gdbtk_init
;