1 /* Startup code for gdbtk.
2 Copyright 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
4 Written by Stu Grossman <grossman@cygnus.com> of Cygnus Support.
6 This file is part of GDB.
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.
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.
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
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
31 #include "tracepoint.h"
48 /* start-sanitize-ide */
52 /* end-sanitize-ide */
55 #ifdef ANSI_PROTOTYPES
65 #include <sys/ioctl.h>
66 #include "gdb_string.h"
76 extern int (*ui_load_progress_hook) PARAMS ((char *, unsigned long));
77 extern void (*pre_add_symbol_hook) PARAMS ((char *));
78 extern void (*post_add_symbol_hook) PARAMS ((void));
79 extern void (*selected_frame_level_changed_hook) PARAMS ((int));
82 extern void (*ui_loop_hook) PARAMS ((int));
85 static void gdbtk_create_tracepoint PARAMS ((struct tracepoint *));
86 static void gdbtk_delete_tracepoint PARAMS ((struct tracepoint *));
87 static void gdbtk_modify_tracepoint PARAMS ((struct tracepoint *));
88 static void gdbtk_create_breakpoint PARAMS ((struct breakpoint *));
89 static void gdbtk_delete_breakpoint PARAMS ((struct breakpoint *));
90 static void gdbtk_modify_breakpoint PARAMS ((struct breakpoint *));
91 static void tk_command_loop PARAMS ((void));
92 static void gdbtk_call_command PARAMS ((struct cmd_list_element *, char *, int));
93 static int gdbtk_wait PARAMS ((int, struct target_waitstatus *));
94 void x_event PARAMS ((int));
95 static int gdbtk_query PARAMS ((const char *, va_list));
96 static void gdbtk_warning PARAMS ((const char *, va_list));
97 void gdbtk_ignorable_warning PARAMS ((const char *));
98 static char* gdbtk_readline PARAMS ((char *));
100 #ifdef ANSI_PROTOTYPES
101 gdbtk_readline_begin (char *format, ...);
103 gdbtk_readline_begin ();
105 static void gdbtk_readline_end PARAMS ((void));
106 static void gdbtk_flush PARAMS ((FILE *));
107 static void gdbtk_pre_add_symbol PARAMS ((char *));
108 static void gdbtk_print_frame_info PARAMS ((struct symtab *, int, int, int));
109 static void gdbtk_post_add_symbol PARAMS ((void));
110 static void pc_changed PARAMS ((void));
111 static void tracepoint_notify PARAMS ((struct tracepoint *, const char *));
112 static void gdbtk_selected_frame_changed PARAMS ((int));
115 * gdbtk_fputs can't be static, because we need to call it in gdbtk.c.
116 * See note there for details.
119 void gdbtk_fputs PARAMS ((const char *, FILE *));
120 int gdbtk_load_hash PARAMS ((char *, unsigned long));
121 static void breakpoint_notify PARAMS ((struct breakpoint *, const char *));
124 * gdbtk_add_hooks - add all the hooks to gdb. This will get called by the
125 * startup code to fill in the hooks needed by core gdb.
129 gdbtk_add_hooks(void)
131 command_loop_hook = tk_command_loop;
132 call_command_hook = gdbtk_call_command;
133 readline_begin_hook = gdbtk_readline_begin;
134 readline_hook = gdbtk_readline;
135 readline_end_hook = gdbtk_readline_end;
137 print_frame_info_listing_hook = gdbtk_print_frame_info;
138 query_hook = gdbtk_query;
139 warning_hook = gdbtk_warning;
140 flush_hook = gdbtk_flush;
142 create_breakpoint_hook = gdbtk_create_breakpoint;
143 delete_breakpoint_hook = gdbtk_delete_breakpoint;
144 modify_breakpoint_hook = gdbtk_modify_breakpoint;
146 interactive_hook = gdbtk_interactive;
147 target_wait_hook = gdbtk_wait;
148 ui_load_progress_hook = gdbtk_load_hash;
150 ui_loop_hook = x_event;
152 pre_add_symbol_hook = gdbtk_pre_add_symbol;
153 post_add_symbol_hook = gdbtk_post_add_symbol;
155 create_tracepoint_hook = gdbtk_create_tracepoint;
156 delete_tracepoint_hook = gdbtk_delete_tracepoint;
157 modify_tracepoint_hook = gdbtk_modify_tracepoint;
158 pc_changed_hook = pc_changed;
159 selected_frame_level_changed_hook = gdbtk_selected_frame_changed;
163 /* These control where to put the gdb output which is created by
164 {f}printf_{un}filtered and friends. gdbtk_fputs and gdbtk_flush are the
165 lowest level of these routines and capture all output from the rest of GDB.
167 The reason to use the result_ptr rather than the gdbtk_interp's result
168 directly is so that a call_wrapper invoked function can preserve its result
169 across calls into Tcl which might be made in the course of the function's
172 * result_ptr->obj_ptr is where to accumulate the result.
173 * GDBTK_TO_RESULT flag means the output goes to the gdbtk_tcl_fputs proc
174 instead of to the result_ptr.
175 * GDBTK_MAKES_LIST flag means add to the result as a list element.
179 gdbtk_result *result_ptr = NULL;
182 /* This allows you to Tcl_Eval a tcl command which takes
183 a command word, and then a single argument. */
185 int gdbtk_two_elem_cmd (cmd_name, argv1)
190 int result, flags_ptr, arg_len, cmd_len;
192 arg_len = Tcl_ScanElement (argv1, &flags_ptr);
193 cmd_len = strlen (cmd_name);
194 command = malloc(arg_len + cmd_len + 2);
195 strcpy (command, cmd_name);
196 strcat (command, " ");
198 Tcl_ConvertElement (argv1, command + cmd_len + 1, flags_ptr);
200 result = Tcl_Eval (gdbtk_interp, command);
211 /* Force immediate screen update */
213 Tcl_VarEval (gdbtk_interp, "gdbtk_tcl_flush", NULL);
217 /* This handles all the output from gdb. All the gdb printf_xxx functions
218 * eventually end up here. The output is either passed to the result_ptr
219 * where it will go to the result of some gdbtk command, or passed to the
220 * Tcl proc gdbtk_tcl_fputs (where it is usually just dumped to the console
225 * 1) result_ptr == NULL - This happens when some output comes from gdb which
226 * is not generated by a command in gdbtk-cmds, usually startup stuff.
227 * In this case we just route the data to gdbtk_tcl_fputs.
228 * 2) The GDBTK_TO_RESULT flag is set - The result is supposed to go to Tcl.
229 * We place the data into the result_ptr, either as a string,
230 * or a list, depending whether the GDBTK_MAKES_LIST bit is set.
231 * 3) The GDBTK_TO_RESULT flag is unset - We route the data to gdbtk_tcl_fputs
232 * UNLESS it was coming to stderr. Then we place it in the result_ptr
233 * anyway, so it can be dealt with.
238 gdbtk_fputs (ptr, stream)
244 if (result_ptr != NULL)
246 if (result_ptr->flags & GDBTK_TO_RESULT)
248 if (result_ptr->flags & GDBTK_MAKES_LIST)
249 Tcl_ListObjAppendElement(NULL, result_ptr->obj_ptr,
250 Tcl_NewStringObj((char *) ptr, -1));
252 Tcl_AppendToObj (result_ptr->obj_ptr, (char *) ptr, -1);
254 else if (stream == gdb_stderr)
256 if (result_ptr->flags & GDBTK_ERROR_STARTED)
257 Tcl_AppendToObj (result_ptr->obj_ptr, (char *) ptr, -1);
260 Tcl_SetStringObj (result_ptr->obj_ptr, (char *) ptr, -1);
261 result_ptr->flags |= GDBTK_ERROR_STARTED;
266 gdbtk_two_elem_cmd ("gdbtk_tcl_fputs", (char *) ptr);
267 if (result_ptr->flags & GDBTK_MAKES_LIST)
268 gdbtk_two_elem_cmd ("gdbtk_tcl_fputs", " ");
273 gdbtk_two_elem_cmd ("gdbtk_tcl_fputs", (char *) ptr);
280 * This routes all warnings to the Tcl function "gdbtk_tcl_warning".
284 gdbtk_warning (warning, args)
290 vsprintf (buf, warning, args);
291 gdbtk_two_elem_cmd ("gdbtk_tcl_warning", buf);
296 * This routes all ignorable warnings to the Tcl function
297 * "gdbtk_tcl_ignorable_warning".
301 gdbtk_ignorable_warning (warning)
305 sprintf (buf, warning);
306 gdbtk_two_elem_cmd ("gdbtk_tcl_ignorable_warning", buf);
312 Tcl_Eval (gdbtk_interp, "gdbtk_pc_changed");
316 /* This function is called instead of gdb's internal command loop. This is the
317 last chance to do anything before entering the main Tk event loop.
318 At the end of the command, we enter the main loop. */
323 extern GDB_FILE *instream;
325 /* We no longer want to use stdin as the command input stream */
328 if (Tcl_Eval (gdbtk_interp, "gdbtk_tcl_preloop") != TCL_OK)
332 /* Force errorInfo to be set up propertly. */
333 Tcl_AddErrorInfo (gdbtk_interp, "");
335 msg = Tcl_GetVar (gdbtk_interp, "errorInfo", TCL_GLOBAL_ONLY);
337 MessageBox (NULL, msg, NULL, MB_OK | MB_ICONERROR | MB_TASKMODAL);
339 fputs_unfiltered (msg, gdb_stderr);
350 /* Come here when there is activity on the X file descriptor. */
356 static int in_x_event = 0;
357 static Tcl_Obj *varname = NULL;
358 if (in_x_event || in_fputs)
368 /* Process pending events */
369 while (Tcl_DoOneEvent (TCL_DONT_WAIT|TCL_ALL_EVENTS) != 0)
372 if (load_in_progress)
377 Tcl_Obj *varnamestrobj = Tcl_NewStringObj("download_cancel_ok",-1);
378 varname = Tcl_ObjGetVar2(gdbtk_interp,varnamestrobj,NULL,TCL_GLOBAL_ONLY);
380 if ((Tcl_GetIntFromObj(gdbtk_interp,varname,&val) == TCL_OK) && val)
396 #ifdef ANSI_PROTOTYPES
397 gdbtk_readline_begin (char *format, ...)
399 gdbtk_readline_begin (va_alist)
406 #ifdef ANSI_PROTOTYPES
407 va_start (args, format);
411 format = va_arg (args, char *);
414 vsprintf (buf, format, args);
415 gdbtk_two_elem_cmd ("gdbtk_tcl_readline_begin", buf);
420 gdbtk_readline (prompt)
429 result = gdbtk_two_elem_cmd ("gdbtk_tcl_readline", prompt);
431 if (result == TCL_OK)
433 return (strdup (gdbtk_interp -> result));
437 gdbtk_fputs (gdbtk_interp -> result, gdb_stdout);
438 gdbtk_fputs ("\n", gdb_stdout);
444 gdbtk_readline_end ()
446 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_readline_end");
450 gdbtk_call_command (cmdblk, arg, from_tty)
451 struct cmd_list_element *cmdblk;
456 if (cmdblk->class == class_run || cmdblk->class == class_trace)
459 /* HACK! HACK! This is to get the gui to update the tstart/tstop
460 button only incase of tstart/tstop commands issued from the console
461 We don't want to update the src window, so we need to have specific
462 procedures to do tstart and tstop
463 Unfortunately this will not display errors from tstart or tstop in the
464 console window itself, but as dialogs.*/
466 if (!strcmp(cmdblk->name, "tstart") && !No_Update)
468 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_tstart");
469 (*cmdblk->function.cfunc)(arg, from_tty);
471 else if (!strcmp(cmdblk->name, "tstop") && !No_Update)
473 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_tstop");
474 (*cmdblk->function.cfunc)(arg, from_tty);
481 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_busy");
482 (*cmdblk->function.cfunc)(arg, from_tty);
485 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_idle");
489 (*cmdblk->function.cfunc)(arg, from_tty);
492 /* The next three functions use breakpoint_notify to allow the GUI
493 * to handle creating, deleting and modifying breakpoints. These three
494 * functions are put into the appropriate gdb hooks in gdbtk_init.
498 gdbtk_create_breakpoint(b)
499 struct breakpoint *b;
501 breakpoint_notify (b, "create");
505 gdbtk_delete_breakpoint(b)
506 struct breakpoint *b;
508 breakpoint_notify (b, "delete");
512 gdbtk_modify_breakpoint(b)
513 struct breakpoint *b;
515 breakpoint_notify (b, "modify");
518 /* This is the generic function for handling changes in
519 * a breakpoint. It routes the information to the Tcl
520 * command "gdbtk_tcl_breakpoint" in the form:
521 * gdbtk_tcl_breakpoint action b_number b_address b_line b_file
522 * On error, the error string is written to gdb_stdout.
526 breakpoint_notify(b, action)
527 struct breakpoint *b;
532 struct symtab_and_line sal;
535 if (b->type != bp_breakpoint)
538 /* We ensure that ACTION contains no special Tcl characters, so we
540 sal = find_pc_line (b->address, 0);
541 filename = symtab_to_filename (sal.symtab);
542 if (filename == NULL)
545 sprintf (buf, "gdbtk_tcl_breakpoint %s %d 0x%lx %d {%s}", action, b->number,
546 (long)b->address, b->line_number, filename);
548 v = Tcl_Eval (gdbtk_interp, buf);
552 gdbtk_fputs (Tcl_GetStringResult (gdbtk_interp), gdb_stdout);
553 gdbtk_fputs ("\n", gdb_stdout);
558 gdbtk_load_hash (section, num)
563 sprintf (buf, "download_hash %s %ld", section, num);
564 Tcl_Eval (gdbtk_interp, buf);
565 return atoi (gdbtk_interp->result);
569 /* This hook is called whenever we are ready to load a symbol file so that
570 the UI can notify the user... */
572 gdbtk_pre_add_symbol (name)
576 gdbtk_two_elem_cmd("gdbtk_tcl_pre_add_symbol", name);
580 /* This hook is called whenever we finish loading a symbol file. */
582 gdbtk_post_add_symbol ()
584 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_post_add_symbol");
587 /* This hook function is called whenever we want to wait for the
591 gdbtk_wait (pid, ourstatus)
593 struct target_waitstatus *ourstatus;
595 gdbtk_start_timer ();
596 pid = target_wait (pid, ourstatus);
602 * This handles all queries from gdb.
603 * The first argument is a printf style format statement, the rest are its
604 * arguments. The resultant formatted string is passed to the Tcl function
606 * It returns the users response to the query, as well as putting the value
607 * in the result field of the Tcl interpreter.
611 gdbtk_query (query, args)
618 vsprintf (buf, query, args);
619 gdbtk_two_elem_cmd ("gdbtk_tcl_query", buf);
621 val = atol (gdbtk_interp->result);
627 gdbtk_print_frame_info (s, line, stopline, noerror)
633 current_source_symtab = s;
634 current_source_line = line;
638 gdbtk_create_tracepoint (tp)
639 struct tracepoint *tp;
641 tracepoint_notify (tp, "create");
645 gdbtk_delete_tracepoint (tp)
646 struct tracepoint *tp;
648 tracepoint_notify (tp, "delete");
652 gdbtk_modify_tracepoint (tp)
653 struct tracepoint *tp;
655 tracepoint_notify (tp, "modify");
659 tracepoint_notify(tp, action)
660 struct tracepoint *tp;
665 struct symtab_and_line sal;
668 /* We ensure that ACTION contains no special Tcl characters, so we
670 sal = find_pc_line (tp->address, 0);
672 filename = symtab_to_filename (sal.symtab);
673 if (filename == NULL)
675 sprintf (buf, "gdbtk_tcl_tracepoint %s %d 0x%lx %d {%s} %d", action, tp->number,
676 (long)tp->address, sal.line, filename, tp->pass_count);
678 v = Tcl_Eval (gdbtk_interp, buf);
682 gdbtk_fputs (gdbtk_interp->result, gdb_stdout);
683 gdbtk_fputs ("\n", gdb_stdout);
688 gdbtk_selected_frame_changed (level)
691 Tcl_UpdateLinkedVar (gdbtk_interp, "gdb_selected_frame_level");