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