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 int (*ui_load_progress_hook) PARAMS ((char *, unsigned long));
77 void (*pre_add_symbol_hook) PARAMS ((char *));
78 void (*post_add_symbol_hook) PARAMS ((void));
81 extern void (*ui_loop_hook) PARAMS ((int));
83 static void gdbtk_create_tracepoint PARAMS ((struct tracepoint *));
84 static void gdbtk_delete_tracepoint PARAMS ((struct tracepoint *));
85 static void gdbtk_modify_tracepoint PARAMS ((struct tracepoint *));
86 static void gdbtk_create_breakpoint PARAMS ((struct breakpoint *));
87 static void gdbtk_delete_breakpoint PARAMS ((struct breakpoint *));
88 static void gdbtk_modify_breakpoint PARAMS ((struct breakpoint *));
89 static void tk_command_loop PARAMS ((void));
90 static void gdbtk_call_command PARAMS ((struct cmd_list_element *, char *, int));
91 static int gdbtk_wait PARAMS ((int, struct target_waitstatus *));
92 void x_event PARAMS ((int));
93 static int gdbtk_query PARAMS ((const char *, va_list));
94 static void gdbtk_warning PARAMS ((const char *, va_list));
95 void gdbtk_ignorable_warning PARAMS ((const char *));
96 static char* gdbtk_readline PARAMS ((char *));
98 #ifdef ANSI_PROTOTYPES
99 gdbtk_readline_begin (char *format, ...);
101 gdbtk_readline_begin ();
103 static void gdbtk_readline_end PARAMS ((void));
104 static void gdbtk_flush PARAMS ((FILE *));
105 static void gdbtk_pre_add_symbol PARAMS ((char *));
106 static void gdbtk_print_frame_info PARAMS ((struct symtab *, int, int, int));
107 static void gdbtk_post_add_symbol PARAMS ((void));
108 static void pc_changed PARAMS ((void));
109 static void tracepoint_notify PARAMS ((struct tracepoint *, const char *));
112 * gdbtk_fputs can't be static, because we need to call it in gdbtk.c.
113 * See note there for details.
116 void gdbtk_fputs PARAMS ((const char *, FILE *));
117 int gdbtk_load_hash PARAMS ((char *, unsigned long));
118 static void breakpoint_notify PARAMS ((struct breakpoint *, const char *));
121 * gdbtk_add_hooks - add all the hooks to gdb. This will get called by the
122 * startup code to fill in the hooks needed by core gdb.
126 gdbtk_add_hooks(void)
128 command_loop_hook = tk_command_loop;
129 call_command_hook = gdbtk_call_command;
130 readline_begin_hook = gdbtk_readline_begin;
131 readline_hook = gdbtk_readline;
132 readline_end_hook = gdbtk_readline_end;
134 print_frame_info_listing_hook = gdbtk_print_frame_info;
135 query_hook = gdbtk_query;
136 warning_hook = gdbtk_warning;
137 flush_hook = gdbtk_flush;
139 create_breakpoint_hook = gdbtk_create_breakpoint;
140 delete_breakpoint_hook = gdbtk_delete_breakpoint;
141 modify_breakpoint_hook = gdbtk_modify_breakpoint;
143 interactive_hook = gdbtk_interactive;
144 target_wait_hook = gdbtk_wait;
145 ui_load_progress_hook = gdbtk_load_hash;
147 ui_loop_hook = x_event;
149 pre_add_symbol_hook = gdbtk_pre_add_symbol;
150 post_add_symbol_hook = gdbtk_post_add_symbol;
152 create_tracepoint_hook = gdbtk_create_tracepoint;
153 delete_tracepoint_hook = gdbtk_delete_tracepoint;
154 modify_tracepoint_hook = gdbtk_modify_tracepoint;
155 pc_changed_hook = pc_changed;
159 /* These control where to put the gdb output which is created by
160 {f}printf_{un}filtered and friends. gdbtk_fputs and gdbtk_flush are the
161 lowest level of these routines and capture all output from the rest of GDB.
163 The reason to use the result_ptr rather than the gdbtk_interp's result
164 directly is so that a call_wrapper invoked function can preserve its result
165 across calls into Tcl which might be made in the course of the function's
168 * result_ptr->obj_ptr is where to accumulate the result.
169 * GDBTK_TO_RESULT flag means the output goes to the gdbtk_tcl_fputs proc
170 instead of to the result_ptr.
171 * GDBTK_MAKES_LIST flag means add to the result as a list element.
175 gdbtk_result *result_ptr = NULL;
178 /* This allows you to Tcl_Eval a tcl command which takes
179 a command word, and then a single argument. */
181 int gdbtk_two_elem_cmd (cmd_name, argv1)
186 int result, flags_ptr, arg_len, cmd_len;
188 arg_len = Tcl_ScanElement (argv1, &flags_ptr);
189 cmd_len = strlen (cmd_name);
190 command = malloc(arg_len + cmd_len + 2);
191 strcpy (command, cmd_name);
192 strcat (command, " ");
194 Tcl_ConvertElement (argv1, command + cmd_len + 1, flags_ptr);
196 result = Tcl_Eval (gdbtk_interp, command);
207 /* Force immediate screen update */
209 Tcl_VarEval (gdbtk_interp, "gdbtk_tcl_flush", NULL);
213 /* This handles all the output from gdb. All the gdb printf_xxx functions
214 * eventually end up here. The output is either passed to the result_ptr
215 * where it will go to the result of some gdbtk command, or passed to the
216 * Tcl proc gdbtk_tcl_fputs (where it is usually just dumped to the console
221 * 1) result_ptr == NULL - This happens when some output comes from gdb which
222 * is not generated by a command in gdbtk-cmds, usually startup stuff.
223 * In this case we just route the data to gdbtk_tcl_fputs.
224 * 2) The GDBTK_TO_RESULT flag is set - The result is supposed to go to Tcl.
225 * We place the data into the result_ptr, either as a string,
226 * or a list, depending whether the GDBTK_MAKES_LIST bit is set.
227 * 3) The GDBTK_TO_RESULT flag is unset - We route the data to gdbtk_tcl_fputs
228 * UNLESS it was coming to stderr. Then we place it in the result_ptr
229 * anyway, so it can be dealt with.
234 gdbtk_fputs (ptr, stream)
240 if (result_ptr != NULL)
242 if (result_ptr->flags & GDBTK_TO_RESULT)
244 if (result_ptr->flags & GDBTK_MAKES_LIST)
245 Tcl_ListObjAppendElement(NULL, result_ptr->obj_ptr,
246 Tcl_NewStringObj((char *) ptr, -1));
248 Tcl_AppendToObj (result_ptr->obj_ptr, (char *) ptr, -1);
250 else if (stream == gdb_stderr)
252 if (result_ptr->flags & GDBTK_ERROR_STARTED)
253 Tcl_AppendToObj (result_ptr->obj_ptr, (char *) ptr, -1);
256 Tcl_SetStringObj (result_ptr->obj_ptr, (char *) ptr, -1);
257 result_ptr->flags |= GDBTK_ERROR_STARTED;
262 gdbtk_two_elem_cmd ("gdbtk_tcl_fputs", (char *) ptr);
263 if (result_ptr->flags & GDBTK_MAKES_LIST)
264 gdbtk_two_elem_cmd ("gdbtk_tcl_fputs", " ");
269 gdbtk_two_elem_cmd ("gdbtk_tcl_fputs", (char *) ptr);
276 * This routes all warnings to the Tcl function "gdbtk_tcl_warning".
280 gdbtk_warning (warning, args)
286 vsprintf (buf, warning, args);
287 gdbtk_two_elem_cmd ("gdbtk_tcl_warning", buf);
292 * This routes all ignorable warnings to the Tcl function
293 * "gdbtk_tcl_ignorable_warning".
297 gdbtk_ignorable_warning (warning)
300 char buf[200], *merge[2];
303 sprintf (buf, warning);
304 gdbtk_two_elem_cmd ("gdbtk_tcl_ignorable_warning", buf);
313 Tcl_Eval (gdbtk_interp, "gdbtk_pc_changed");
317 /* This function is called instead of gdb's internal command loop. This is the
318 last chance to do anything before entering the main Tk event loop.
319 At the end of the command, we enter the main loop. */
324 extern GDB_FILE *instream;
326 /* We no longer want to use stdin as the command input stream */
329 if (Tcl_Eval (gdbtk_interp, "gdbtk_tcl_preloop") != TCL_OK)
333 /* Force errorInfo to be set up propertly. */
334 Tcl_AddErrorInfo (gdbtk_interp, "");
336 msg = Tcl_GetVar (gdbtk_interp, "errorInfo", TCL_GLOBAL_ONLY);
338 MessageBox (NULL, msg, NULL, MB_OK | MB_ICONERROR | MB_TASKMODAL);
340 fputs_unfiltered (msg, gdb_stderr);
351 /* Come here when there is activity on the X file descriptor. */
357 static int in_x_event = 0;
358 static Tcl_Obj *varname = NULL;
359 if (in_x_event || in_fputs)
369 /* Process pending events */
370 while (Tcl_DoOneEvent (TCL_DONT_WAIT|TCL_ALL_EVENTS) != 0)
373 if (load_in_progress)
378 Tcl_Obj *varnamestrobj = Tcl_NewStringObj("download_cancel_ok",-1);
379 varname = Tcl_ObjGetVar2(gdbtk_interp,varnamestrobj,NULL,TCL_GLOBAL_ONLY);
381 if ((Tcl_GetIntFromObj(gdbtk_interp,varname,&val) == TCL_OK) && val)
397 #ifdef ANSI_PROTOTYPES
398 gdbtk_readline_begin (char *format, ...)
400 gdbtk_readline_begin (va_alist)
405 char buf[200], *merge[2];
408 #ifdef ANSI_PROTOTYPES
409 va_start (args, format);
413 format = va_arg (args, char *);
416 vsprintf (buf, format, args);
417 gdbtk_two_elem_cmd ("gdbtk_tcl_readline_begin", buf);
422 gdbtk_readline (prompt)
431 result = gdbtk_two_elem_cmd ("gdbtk_tcl_readline", prompt);
433 if (result == TCL_OK)
435 return (strdup (gdbtk_interp -> result));
439 gdbtk_fputs (gdbtk_interp -> result, gdb_stdout);
440 gdbtk_fputs ("\n", gdb_stdout);
446 gdbtk_readline_end ()
448 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_readline_end");
452 gdbtk_call_command (cmdblk, arg, from_tty)
453 struct cmd_list_element *cmdblk;
458 if (cmdblk->class == class_run || cmdblk->class == class_trace)
461 /* HACK! HACK! This is to get the gui to update the tstart/tstop
462 button only incase of tstart/tstop commands issued from the console
463 We don't want to update the src window, so we need to have specific
464 procedures to do tstart and tstop
465 Unfortunately this will not display errors from tstart or tstop in the
466 console window itself, but as dialogs.*/
468 if (!strcmp(cmdblk->name, "tstart") && !No_Update)
470 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_tstart");
471 (*cmdblk->function.cfunc)(arg, from_tty);
473 else if (!strcmp(cmdblk->name, "tstop") && !No_Update)
475 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_tstop");
476 (*cmdblk->function.cfunc)(arg, from_tty);
483 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_busy");
484 (*cmdblk->function.cfunc)(arg, from_tty);
487 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_idle");
491 (*cmdblk->function.cfunc)(arg, from_tty);
494 /* The next three functions use breakpoint_notify to allow the GUI
495 * to handle creating, deleting and modifying breakpoints. These three
496 * functions are put into the appropriate gdb hooks in gdbtk_init.
500 gdbtk_create_breakpoint(b)
501 struct breakpoint *b;
503 breakpoint_notify (b, "create");
507 gdbtk_delete_breakpoint(b)
508 struct breakpoint *b;
510 breakpoint_notify (b, "delete");
514 gdbtk_modify_breakpoint(b)
515 struct breakpoint *b;
517 breakpoint_notify (b, "modify");
520 /* This is the generic function for handling changes in
521 * a breakpoint. It routes the information to the Tcl
522 * command "gdbtk_tcl_breakpoint" in the form:
523 * gdbtk_tcl_breakpoint action b_number b_address b_line b_file
524 * On error, the error string is written to gdb_stdout.
528 breakpoint_notify(b, action)
529 struct breakpoint *b;
534 struct symtab_and_line sal;
537 if (b->type != bp_breakpoint)
540 /* We ensure that ACTION contains no special Tcl characters, so we
542 sal = find_pc_line (b->address, 0);
543 filename = symtab_to_filename (sal.symtab);
544 if (filename == NULL)
547 sprintf (buf, "gdbtk_tcl_breakpoint %s %d 0x%lx %d {%s}", action, b->number,
548 (long)b->address, b->line_number, filename);
550 v = Tcl_Eval (gdbtk_interp, buf);
554 gdbtk_fputs (Tcl_GetStringResult (gdbtk_interp), gdb_stdout);
555 gdbtk_fputs ("\n", gdb_stdout);
560 gdbtk_load_hash (section, num)
565 sprintf (buf, "download_hash %s %ld", section, num);
566 Tcl_Eval (gdbtk_interp, buf);
567 return atoi (gdbtk_interp->result);
571 /* This hook is called whenever we are ready to load a symbol file so that
572 the UI can notify the user... */
574 gdbtk_pre_add_symbol (name)
578 gdbtk_two_elem_cmd("gdbtk_tcl_pre_add_symbol", name);
582 /* This hook is called whenever we finish loading a symbol file. */
584 gdbtk_post_add_symbol ()
586 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_post_add_symbol");
589 /* This hook function is called whenever we want to wait for the
593 gdbtk_wait (pid, ourstatus)
595 struct target_waitstatus *ourstatus;
597 gdbtk_start_timer ();
598 pid = target_wait (pid, ourstatus);
604 * This handles all queries from gdb.
605 * The first argument is a printf style format statement, the rest are its
606 * arguments. The resultant formatted string is passed to the Tcl function
608 * It returns the users response to the query, as well as putting the value
609 * in the result field of the Tcl interpreter.
613 gdbtk_query (query, args)
620 vsprintf (buf, query, args);
621 gdbtk_two_elem_cmd ("gdbtk_tcl_query", buf);
623 val = atol (gdbtk_interp->result);
629 gdbtk_print_frame_info (s, line, stopline, noerror)
635 current_source_symtab = s;
636 current_source_line = line;
640 gdbtk_create_tracepoint (tp)
641 struct tracepoint *tp;
643 tracepoint_notify (tp, "create");
647 gdbtk_delete_tracepoint (tp)
648 struct tracepoint *tp;
650 tracepoint_notify (tp, "delete");
654 gdbtk_modify_tracepoint (tp)
655 struct tracepoint *tp;
657 tracepoint_notify (tp, "modify");
661 tracepoint_notify(tp, action)
662 struct tracepoint *tp;
667 struct symtab_and_line sal;
670 /* We ensure that ACTION contains no special Tcl characters, so we
672 sal = find_pc_line (tp->address, 0);
674 filename = symtab_to_filename (sal.symtab);
675 if (filename == NULL)
677 sprintf (buf, "gdbtk_tcl_tracepoint %s %d 0x%lx %d {%s}", action, tp->number,
678 (long)tp->address, sal.line, filename, tp->pass_count);
680 v = Tcl_Eval (gdbtk_interp, buf);
684 gdbtk_fputs (gdbtk_interp->result, gdb_stdout);
685 gdbtk_fputs ("\n", gdb_stdout);