1998-10-20 Syd Polk <spolk@cygnus.com>
[binutils-gdb.git] / gdb / gdbtk-hooks.c
1 /* Startup code for gdbtk.
2 Copyright 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3
4 Written by Stu Grossman <grossman@cygnus.com> of Cygnus Support.
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
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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"
30 #include "gdbcore.h"
31 #include "tracepoint.h"
32 #include "demangle.h"
33
34 #ifdef _WIN32
35 #include <winuser.h>
36 #endif
37
38 #include <sys/stat.h>
39
40 #include <tcl.h>
41 #include <tk.h>
42 #include <itcl.h>
43 #include <tix.h>
44 #include "guitcl.h"
45 #include "gdbtk.h"
46
47 #ifdef IDE
48 /* start-sanitize-ide */
49 #include "event.h"
50 #include "idetcl.h"
51 #include "ilutk.h"
52 /* end-sanitize-ide */
53 #endif
54
55 #ifdef ANSI_PROTOTYPES
56 #include <stdarg.h>
57 #else
58 #include <varargs.h>
59 #endif
60 #include <signal.h>
61 #include <fcntl.h>
62 #include <unistd.h>
63 #include <setjmp.h>
64 #include "top.h"
65 #include <sys/ioctl.h>
66 #include "gdb_string.h"
67 #include "dis-asm.h"
68 #include <stdio.h>
69 #include "gdbcmd.h"
70
71 #include "annotate.h"
72 #include <sys/time.h>
73
74 int in_fputs = 0;
75
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));
80 #ifdef __CYGWIN32__
81 extern void (*ui_loop_hook) PARAMS ((int));
82 #endif
83
84 static void gdbtk_create_tracepoint PARAMS ((struct tracepoint *));
85 static void gdbtk_delete_tracepoint PARAMS ((struct tracepoint *));
86 static void gdbtk_modify_tracepoint PARAMS ((struct tracepoint *));
87 static void gdbtk_create_breakpoint PARAMS ((struct breakpoint *));
88 static void gdbtk_delete_breakpoint PARAMS ((struct breakpoint *));
89 static void gdbtk_modify_breakpoint PARAMS ((struct breakpoint *));
90 static void gdbtk_file_changed PARAMS ((char *));
91 static void gdbtk_exec_file_display PARAMS ((char *));
92 static void tk_command_loop PARAMS ((void));
93 static void gdbtk_call_command PARAMS ((struct cmd_list_element *, char *, int));
94 static int gdbtk_wait PARAMS ((int, struct target_waitstatus *));
95 void x_event PARAMS ((int));
96 static int gdbtk_query PARAMS ((const char *, va_list));
97 static void gdbtk_warning PARAMS ((const char *, va_list));
98 void gdbtk_ignorable_warning PARAMS ((const char *));
99 static char* gdbtk_readline PARAMS ((char *));
100 static void
101 #ifdef ANSI_PROTOTYPES
102 gdbtk_readline_begin (char *format, ...);
103 #else
104 gdbtk_readline_begin ();
105 #endif
106 static void gdbtk_readline_end PARAMS ((void));
107 static void gdbtk_flush PARAMS ((FILE *));
108 static void gdbtk_pre_add_symbol PARAMS ((char *));
109 static void gdbtk_print_frame_info PARAMS ((struct symtab *, int, int, int));
110 static void gdbtk_post_add_symbol PARAMS ((void));
111 static void pc_changed PARAMS ((void));
112 static void tracepoint_notify PARAMS ((struct tracepoint *, const char *));
113 static void gdbtk_selected_frame_changed PARAMS ((int));
114 static void gdbtk_context_change PARAMS ((int));
115
116 /*
117 * gdbtk_fputs can't be static, because we need to call it in gdbtk.c.
118 * See note there for details.
119 */
120
121 void gdbtk_fputs PARAMS ((const char *, FILE *));
122 int gdbtk_load_hash PARAMS ((char *, unsigned long));
123 static void breakpoint_notify PARAMS ((struct breakpoint *, const char *));
124
125 /*
126 * gdbtk_add_hooks - add all the hooks to gdb. This will get called by the
127 * startup code to fill in the hooks needed by core gdb.
128 */
129
130 void
131 gdbtk_add_hooks(void)
132 {
133 command_loop_hook = tk_command_loop;
134 call_command_hook = gdbtk_call_command;
135 readline_begin_hook = gdbtk_readline_begin;
136 readline_hook = gdbtk_readline;
137 readline_end_hook = gdbtk_readline_end;
138
139 print_frame_info_listing_hook = gdbtk_print_frame_info;
140 query_hook = gdbtk_query;
141 warning_hook = gdbtk_warning;
142 flush_hook = gdbtk_flush;
143
144 create_breakpoint_hook = gdbtk_create_breakpoint;
145 delete_breakpoint_hook = gdbtk_delete_breakpoint;
146 modify_breakpoint_hook = gdbtk_modify_breakpoint;
147
148 interactive_hook = gdbtk_interactive;
149 target_wait_hook = gdbtk_wait;
150 ui_load_progress_hook = gdbtk_load_hash;
151
152 #ifdef __CYGWIN32__
153 ui_loop_hook = x_event;
154 #endif
155 pre_add_symbol_hook = gdbtk_pre_add_symbol;
156 post_add_symbol_hook = gdbtk_post_add_symbol;
157 file_changed_hook = gdbtk_file_changed;
158 exec_file_display_hook = gdbtk_exec_file_display;
159
160 create_tracepoint_hook = gdbtk_create_tracepoint;
161 delete_tracepoint_hook = gdbtk_delete_tracepoint;
162 modify_tracepoint_hook = gdbtk_modify_tracepoint;
163
164 pc_changed_hook = pc_changed;
165 selected_frame_level_changed_hook = gdbtk_selected_frame_changed;
166 context_hook = gdbtk_context_change;
167 }
168
169 /* These control where to put the gdb output which is created by
170 {f}printf_{un}filtered and friends. gdbtk_fputs and gdbtk_flush are the
171 lowest level of these routines and capture all output from the rest of GDB.
172
173 The reason to use the result_ptr rather than the gdbtk_interp's result
174 directly is so that a call_wrapper invoked function can preserve its result
175 across calls into Tcl which might be made in the course of the function's
176 execution.
177
178 * result_ptr->obj_ptr is where to accumulate the result.
179 * GDBTK_TO_RESULT flag means the output goes to the gdbtk_tcl_fputs proc
180 instead of to the result_ptr.
181 * GDBTK_MAKES_LIST flag means add to the result as a list element.
182
183 */
184
185 gdbtk_result *result_ptr = NULL;
186 \f
187
188 /* This allows you to Tcl_Eval a tcl command which takes
189 a command word, and then a single argument. */
190
191 int gdbtk_two_elem_cmd (cmd_name, argv1)
192 char *cmd_name;
193 char * argv1;
194 {
195 char *command;
196 int result, flags_ptr, arg_len, cmd_len;
197
198 arg_len = Tcl_ScanElement (argv1, &flags_ptr);
199 cmd_len = strlen (cmd_name);
200 command = malloc(arg_len + cmd_len + 2);
201 strcpy (command, cmd_name);
202 strcat (command, " ");
203
204 Tcl_ConvertElement (argv1, command + cmd_len + 1, flags_ptr);
205
206 result = Tcl_Eval (gdbtk_interp, command);
207 free (command);
208 return result;
209
210 }
211
212 static void
213 gdbtk_flush (stream)
214 FILE *stream;
215 {
216 #if 0
217 /* Force immediate screen update */
218
219 Tcl_VarEval (gdbtk_interp, "gdbtk_tcl_flush", NULL);
220 #endif
221 }
222
223 /* This handles all the output from gdb. All the gdb printf_xxx functions
224 * eventually end up here. The output is either passed to the result_ptr
225 * where it will go to the result of some gdbtk command, or passed to the
226 * Tcl proc gdbtk_tcl_fputs (where it is usually just dumped to the console
227 * window.
228 *
229 * The cases are:
230 *
231 * 1) result_ptr == NULL - This happens when some output comes from gdb which
232 * is not generated by a command in gdbtk-cmds, usually startup stuff.
233 * In this case we just route the data to gdbtk_tcl_fputs.
234 * 2) The GDBTK_TO_RESULT flag is set - The result is supposed to go to Tcl.
235 * We place the data into the result_ptr, either as a string,
236 * or a list, depending whether the GDBTK_MAKES_LIST bit is set.
237 * 3) The GDBTK_TO_RESULT flag is unset - We route the data to gdbtk_tcl_fputs
238 * UNLESS it was coming to stderr. Then we place it in the result_ptr
239 * anyway, so it can be dealt with.
240 *
241 */
242
243 void
244 gdbtk_fputs (ptr, stream)
245 const char *ptr;
246 FILE *stream;
247 {
248 in_fputs = 1;
249
250 if (result_ptr != NULL)
251 {
252 if (result_ptr->flags & GDBTK_TO_RESULT)
253 {
254 if (result_ptr->flags & GDBTK_MAKES_LIST)
255 Tcl_ListObjAppendElement(NULL, result_ptr->obj_ptr,
256 Tcl_NewStringObj((char *) ptr, -1));
257 else
258 Tcl_AppendToObj (result_ptr->obj_ptr, (char *) ptr, -1);
259 }
260 else if (stream == gdb_stderr)
261 {
262 if (result_ptr->flags & GDBTK_ERROR_STARTED)
263 Tcl_AppendToObj (result_ptr->obj_ptr, (char *) ptr, -1);
264 else
265 {
266 Tcl_SetStringObj (result_ptr->obj_ptr, (char *) ptr, -1);
267 result_ptr->flags |= GDBTK_ERROR_STARTED;
268 }
269 }
270 else
271 {
272 gdbtk_two_elem_cmd ("gdbtk_tcl_fputs", (char *) ptr);
273 if (result_ptr->flags & GDBTK_MAKES_LIST)
274 gdbtk_two_elem_cmd ("gdbtk_tcl_fputs", " ");
275 }
276 }
277 else
278 {
279 gdbtk_two_elem_cmd ("gdbtk_tcl_fputs", (char *) ptr);
280 }
281
282 in_fputs = 0;
283 }
284
285 /*
286 * This routes all warnings to the Tcl function "gdbtk_tcl_warning".
287 */
288
289 static void
290 gdbtk_warning (warning, args)
291 const char *warning;
292 va_list args;
293 {
294 char buf[200];
295
296 vsprintf (buf, warning, args);
297 gdbtk_two_elem_cmd ("gdbtk_tcl_warning", buf);
298
299 }
300
301 /*
302 * This routes all ignorable warnings to the Tcl function
303 * "gdbtk_tcl_ignorable_warning".
304 */
305
306 void
307 gdbtk_ignorable_warning (warning)
308 const char *warning;
309 {
310 char buf[512];
311 sprintf (buf, warning);
312 gdbtk_two_elem_cmd ("gdbtk_tcl_ignorable_warning", buf);
313 }
314
315 static void
316 pc_changed()
317 {
318 Tcl_Eval (gdbtk_interp, "gdbtk_pc_changed");
319 }
320
321 \f
322 /* This function is called instead of gdb's internal command loop. This is the
323 last chance to do anything before entering the main Tk event loop.
324 At the end of the command, we enter the main loop. */
325
326 static void
327 tk_command_loop ()
328 {
329 extern GDB_FILE *instream;
330
331 /* We no longer want to use stdin as the command input stream */
332 instream = NULL;
333
334 if (Tcl_Eval (gdbtk_interp, "gdbtk_tcl_preloop") != TCL_OK)
335 {
336 char *msg;
337
338 /* Force errorInfo to be set up propertly. */
339 Tcl_AddErrorInfo (gdbtk_interp, "");
340
341 msg = Tcl_GetVar (gdbtk_interp, "errorInfo", TCL_GLOBAL_ONLY);
342 #ifdef _WIN32
343 MessageBox (NULL, msg, NULL, MB_OK | MB_ICONERROR | MB_TASKMODAL);
344 #else
345 fputs_unfiltered (msg, gdb_stderr);
346 #endif
347 }
348
349 #ifdef _WIN32
350 close_bfds ();
351 #endif
352
353 Tk_MainLoop ();
354 }
355
356 /* Come here when there is activity on the X file descriptor. */
357
358 void
359 x_event (signo)
360 int signo;
361 {
362 static int in_x_event = 0;
363 static Tcl_Obj *varname = NULL;
364 if (in_x_event || in_fputs)
365 return;
366
367 in_x_event = 1;
368
369 #ifdef __CYGWIN32__
370 if (signo == -2)
371 gdbtk_stop_timer ();
372 #endif
373
374 /* Process pending events */
375 while (Tcl_DoOneEvent (TCL_DONT_WAIT|TCL_ALL_EVENTS) != 0)
376 ;
377
378 if (load_in_progress)
379 {
380 int val;
381 if (varname == NULL)
382 {
383 Tcl_Obj *varnamestrobj = Tcl_NewStringObj("download_cancel_ok",-1);
384 varname = Tcl_ObjGetVar2(gdbtk_interp,varnamestrobj,NULL,TCL_GLOBAL_ONLY);
385 }
386 if ((Tcl_GetIntFromObj(gdbtk_interp,varname,&val) == TCL_OK) && val)
387 {
388 quit_flag = 1;
389 #ifdef REQUEST_QUIT
390 REQUEST_QUIT;
391 #else
392 if (immediate_quit)
393 quit ();
394 #endif
395 }
396 }
397 in_x_event = 0;
398 }
399
400 /* VARARGS */
401 static void
402 #ifdef ANSI_PROTOTYPES
403 gdbtk_readline_begin (char *format, ...)
404 #else
405 gdbtk_readline_begin (va_alist)
406 va_dcl
407 #endif
408 {
409 va_list args;
410 char buf[200];
411
412 #ifdef ANSI_PROTOTYPES
413 va_start (args, format);
414 #else
415 char *format;
416 va_start (args);
417 format = va_arg (args, char *);
418 #endif
419
420 vsprintf (buf, format, args);
421 gdbtk_two_elem_cmd ("gdbtk_tcl_readline_begin", buf);
422
423 }
424
425 static char *
426 gdbtk_readline (prompt)
427 char *prompt;
428 {
429 int result;
430
431 #ifdef _WIN32
432 close_bfds ();
433 #endif
434
435 result = gdbtk_two_elem_cmd ("gdbtk_tcl_readline", prompt);
436
437 if (result == TCL_OK)
438 {
439 return (strdup (gdbtk_interp -> result));
440 }
441 else
442 {
443 gdbtk_fputs (gdbtk_interp -> result, gdb_stdout);
444 gdbtk_fputs ("\n", gdb_stdout);
445 return (NULL);
446 }
447 }
448
449 static void
450 gdbtk_readline_end ()
451 {
452 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_readline_end");
453 }
454
455 static void
456 gdbtk_call_command (cmdblk, arg, from_tty)
457 struct cmd_list_element *cmdblk;
458 char *arg;
459 int from_tty;
460 {
461 running_now = 0;
462 if (cmdblk->class == class_run || cmdblk->class == class_trace)
463 {
464
465 /* HACK! HACK! This is to get the gui to update the tstart/tstop
466 button only incase of tstart/tstop commands issued from the console
467 We don't want to update the src window, so we need to have specific
468 procedures to do tstart and tstop
469 Unfortunately this will not display errors from tstart or tstop in the
470 console window itself, but as dialogs.*/
471
472 if (!strcmp(cmdblk->name, "tstart") && !No_Update)
473 {
474 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_tstart");
475 (*cmdblk->function.cfunc)(arg, from_tty);
476 }
477 else if (!strcmp(cmdblk->name, "tstop") && !No_Update)
478 {
479 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_tstop");
480 (*cmdblk->function.cfunc)(arg, from_tty);
481 }
482 /* end of hack */
483 else
484 {
485 running_now = 1;
486 if (!No_Update)
487 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_busy");
488 (*cmdblk->function.cfunc)(arg, from_tty);
489 running_now = 0;
490 if (!No_Update)
491 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_idle");
492 }
493 }
494 else
495 (*cmdblk->function.cfunc)(arg, from_tty);
496 }
497
498 /* The next three functions use breakpoint_notify to allow the GUI
499 * to handle creating, deleting and modifying breakpoints. These three
500 * functions are put into the appropriate gdb hooks in gdbtk_init.
501 */
502
503 static void
504 gdbtk_create_breakpoint(b)
505 struct breakpoint *b;
506 {
507 breakpoint_notify (b, "create");
508 }
509
510 static void
511 gdbtk_delete_breakpoint(b)
512 struct breakpoint *b;
513 {
514 breakpoint_notify (b, "delete");
515 }
516
517 static void
518 gdbtk_modify_breakpoint(b)
519 struct breakpoint *b;
520 {
521 breakpoint_notify (b, "modify");
522 }
523
524 /* This is the generic function for handling changes in
525 * a breakpoint. It routes the information to the Tcl
526 * command "gdbtk_tcl_breakpoint" in the form:
527 * gdbtk_tcl_breakpoint action b_number b_address b_line b_file
528 * On error, the error string is written to gdb_stdout.
529 */
530
531 static void
532 breakpoint_notify(b, action)
533 struct breakpoint *b;
534 const char *action;
535 {
536 char buf[256];
537 int v;
538 struct symtab_and_line sal;
539 char *filename;
540
541 if (b->type != bp_breakpoint)
542 return;
543
544 /* We ensure that ACTION contains no special Tcl characters, so we
545 can do this. */
546 sal = find_pc_line (b->address, 0);
547 filename = symtab_to_filename (sal.symtab);
548 if (filename == NULL)
549 filename = "";
550
551 sprintf (buf, "gdbtk_tcl_breakpoint %s %d 0x%lx %d {%s}", action, b->number,
552 (long)b->address, b->line_number, filename);
553
554 v = Tcl_Eval (gdbtk_interp, buf);
555
556 if (v != TCL_OK)
557 {
558 gdbtk_fputs (Tcl_GetStringResult (gdbtk_interp), gdb_stdout);
559 gdbtk_fputs ("\n", gdb_stdout);
560 }
561 }
562
563 int
564 gdbtk_load_hash (section, num)
565 char *section;
566 unsigned long num;
567 {
568 char buf[128];
569 sprintf (buf, "download_hash %s %ld", section, num);
570 Tcl_Eval (gdbtk_interp, buf);
571 return atoi (gdbtk_interp->result);
572 }
573
574
575 /* This hook is called whenever we are ready to load a symbol file so that
576 the UI can notify the user... */
577 static void
578 gdbtk_pre_add_symbol (name)
579 char *name;
580 {
581
582 gdbtk_two_elem_cmd("gdbtk_tcl_pre_add_symbol", name);
583
584 }
585
586 /* This hook is called whenever we finish loading a symbol file. */
587 static void
588 gdbtk_post_add_symbol ()
589 {
590 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_post_add_symbol");
591 }
592
593 /* This hook function is called whenever we want to wait for the
594 target. */
595
596 static int
597 gdbtk_wait (pid, ourstatus)
598 int pid;
599 struct target_waitstatus *ourstatus;
600 {
601 /* Don't run the timer on various targets... */
602 if (!STREQ (target_shortname, "ice"))
603 gdbtk_start_timer ();
604 pid = target_wait (pid, ourstatus);
605 gdbtk_stop_timer ();
606 return pid;
607 }
608
609 /*
610 * This handles all queries from gdb.
611 * The first argument is a printf style format statement, the rest are its
612 * arguments. The resultant formatted string is passed to the Tcl function
613 * "gdbtk_tcl_query".
614 * It returns the users response to the query, as well as putting the value
615 * in the result field of the Tcl interpreter.
616 */
617
618 static int
619 gdbtk_query (query, args)
620 const char *query;
621 va_list args;
622 {
623 char buf[200];
624 long val;
625
626 vsprintf (buf, query, args);
627 gdbtk_two_elem_cmd ("gdbtk_tcl_query", buf);
628
629 val = atol (gdbtk_interp->result);
630 return val;
631 }
632
633
634 static void
635 gdbtk_print_frame_info (s, line, stopline, noerror)
636 struct symtab *s;
637 int line;
638 int stopline;
639 int noerror;
640 {
641 current_source_symtab = s;
642 current_source_line = line;
643 }
644
645 static void
646 gdbtk_create_tracepoint (tp)
647 struct tracepoint *tp;
648 {
649 tracepoint_notify (tp, "create");
650 }
651
652 static void
653 gdbtk_delete_tracepoint (tp)
654 struct tracepoint *tp;
655 {
656 tracepoint_notify (tp, "delete");
657 }
658
659 static void
660 gdbtk_modify_tracepoint (tp)
661 struct tracepoint *tp;
662 {
663 tracepoint_notify (tp, "modify");
664 }
665
666 static void
667 tracepoint_notify(tp, action)
668 struct tracepoint *tp;
669 const char *action;
670 {
671 char buf[256];
672 int v;
673 struct symtab_and_line sal;
674 char *filename;
675
676 /* We ensure that ACTION contains no special Tcl characters, so we
677 can do this. */
678 sal = find_pc_line (tp->address, 0);
679
680 filename = symtab_to_filename (sal.symtab);
681 if (filename == NULL)
682 filename = "N/A";
683 sprintf (buf, "gdbtk_tcl_tracepoint %s %d 0x%lx %d {%s} %d", action, tp->number,
684 (long)tp->address, sal.line, filename, tp->pass_count);
685
686 v = Tcl_Eval (gdbtk_interp, buf);
687
688 if (v != TCL_OK)
689 {
690 gdbtk_fputs (gdbtk_interp->result, gdb_stdout);
691 gdbtk_fputs ("\n", gdb_stdout);
692 }
693 }
694
695 static void
696 gdbtk_selected_frame_changed (level)
697 int level;
698 {
699 Tcl_UpdateLinkedVar (gdbtk_interp, "gdb_selected_frame_level");
700 }
701
702 /* Called when the current thread changes. */
703 /* gdb_context is linked to the tcl variable "gdb_context_id" */
704 static void
705 gdbtk_context_change (num)
706 int num;
707 {
708 gdb_context = num;
709 }
710
711 /* Called from file_command */
712 static void
713 gdbtk_file_changed (filename)
714 char *filename;
715 {
716 gdbtk_two_elem_cmd ("gdbtk_tcl_file_changed", filename);
717 }
718
719 /* Called from exec_file_command */
720 static void
721 gdbtk_exec_file_display (filename)
722 char *filename;
723 {
724 gdbtk_two_elem_cmd ("gdbtk_tcl_exec_file_display", filename);
725 }