Mon Jul 27 16:11:42 1998 Michael Snyder <msnyder@cleaver.cygnus.com>
[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
76int (*ui_load_progress_hook) PARAMS ((char *, unsigned long));
77void (*pre_add_symbol_hook) PARAMS ((char *));
78void (*post_add_symbol_hook) PARAMS ((void));
79
80#ifdef __CYGWIN32__
81extern void (*ui_loop_hook) PARAMS ((int));
82#endif
83static void gdbtk_create_tracepoint PARAMS ((struct tracepoint *));
84static void gdbtk_delete_tracepoint PARAMS ((struct tracepoint *));
85static void gdbtk_modify_tracepoint PARAMS ((struct tracepoint *));
86static void gdbtk_create_breakpoint PARAMS ((struct breakpoint *));
87static void gdbtk_delete_breakpoint PARAMS ((struct breakpoint *));
88static void gdbtk_modify_breakpoint PARAMS ((struct breakpoint *));
89static void tk_command_loop PARAMS ((void));
90static void gdbtk_call_command PARAMS ((struct cmd_list_element *, char *, int));
91static int gdbtk_wait PARAMS ((int, struct target_waitstatus *));
92 void x_event PARAMS ((int));
93static int gdbtk_query PARAMS ((const char *, va_list));
94static void gdbtk_warning PARAMS ((const char *, va_list));
95void gdbtk_ignorable_warning PARAMS ((const char *));
96static char* gdbtk_readline PARAMS ((char *));
97static void
98#ifdef ANSI_PROTOTYPES
99gdbtk_readline_begin (char *format, ...);
100#else
101gdbtk_readline_begin ();
102#endif
103static void gdbtk_readline_end PARAMS ((void));
104static void gdbtk_flush PARAMS ((FILE *));
105static void gdbtk_pre_add_symbol PARAMS ((char *));
106static void gdbtk_print_frame_info PARAMS ((struct symtab *, int, int, int));
107static void gdbtk_post_add_symbol PARAMS ((void));
108static void pc_changed PARAMS ((void));
109static void tracepoint_notify PARAMS ((struct tracepoint *, const char *));
110
111/*
112 * gdbtk_fputs can't be static, because we need to call it in gdbtk.c.
113 * See note there for details.
114 */
115
116void gdbtk_fputs PARAMS ((const char *, FILE *));
117int gdbtk_load_hash PARAMS ((char *, unsigned long));
118static void breakpoint_notify PARAMS ((struct breakpoint *, const char *));
119
120/*
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.
123 */
124
125void
126gdbtk_add_hooks(void)
127{
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;
133
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;
138
139 create_breakpoint_hook = gdbtk_create_breakpoint;
140 delete_breakpoint_hook = gdbtk_delete_breakpoint;
141 modify_breakpoint_hook = gdbtk_modify_breakpoint;
142
143 interactive_hook = gdbtk_interactive;
144 target_wait_hook = gdbtk_wait;
145 ui_load_progress_hook = gdbtk_load_hash;
146#ifdef __CYGWIN32__
147 ui_loop_hook = x_event;
148#endif
149 pre_add_symbol_hook = gdbtk_pre_add_symbol;
150 post_add_symbol_hook = gdbtk_post_add_symbol;
151
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;
156
157}
158
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.
162
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
166 execution.
167
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.
172
173 */
174
175gdbtk_result *result_ptr = NULL;
176\f
177
178/* This allows you to Tcl_Eval a tcl command which takes
179 a command word, and then a single argument. */
180
181int gdbtk_two_elem_cmd (cmd_name, argv1)
182 char *cmd_name;
183 char * argv1;
184{
185 char *command;
186 int result, flags_ptr, arg_len, cmd_len;
187
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, " ");
193
194 Tcl_ConvertElement (argv1, command + cmd_len + 1, flags_ptr);
195
196 result = Tcl_Eval (gdbtk_interp, command);
197 free (command);
198 return result;
199
200}
201
202static void
203gdbtk_flush (stream)
204 FILE *stream;
205{
206#if 0
207 /* Force immediate screen update */
208
209 Tcl_VarEval (gdbtk_interp, "gdbtk_tcl_flush", NULL);
210#endif
211}
212
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
217 * window.
218 *
219 * The cases are:
220 *
221 * 1) result_ptr == NULL - This happens when some output comes from gdb which
f3b86a30 222 * is not generated by a command in gdbtk-cmds, usually startup stuff.
ca4e7e14
JI
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.
230 *
231 */
232
233void
234gdbtk_fputs (ptr, stream)
235 const char *ptr;
236 FILE *stream;
237{
238 in_fputs = 1;
239
240 if (result_ptr != NULL)
241 {
242 if (result_ptr->flags & GDBTK_TO_RESULT)
243 {
244 if (result_ptr->flags & GDBTK_MAKES_LIST)
245 Tcl_ListObjAppendElement(NULL, result_ptr->obj_ptr,
246 Tcl_NewStringObj((char *) ptr, -1));
247 else
248 Tcl_AppendToObj (result_ptr->obj_ptr, (char *) ptr, -1);
249 }
250 else if (stream == gdb_stderr)
251 {
252 if (result_ptr->flags & GDBTK_ERROR_STARTED)
253 Tcl_AppendToObj (result_ptr->obj_ptr, (char *) ptr, -1);
254 else
255 {
256 Tcl_SetStringObj (result_ptr->obj_ptr, (char *) ptr, -1);
257 result_ptr->flags |= GDBTK_ERROR_STARTED;
258 }
259 }
260 else
261 {
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", " ");
265 }
266 }
267 else
268 {
269 gdbtk_two_elem_cmd ("gdbtk_tcl_fputs", (char *) ptr);
270 }
271
272 in_fputs = 0;
273}
274
275/*
276 * This routes all warnings to the Tcl function "gdbtk_tcl_warning".
277 */
278
279static void
280gdbtk_warning (warning, args)
281 const char *warning;
282 va_list args;
283{
284 char buf[200];
285
286 vsprintf (buf, warning, args);
287 gdbtk_two_elem_cmd ("gdbtk_tcl_warning", buf);
288
289}
290
291/*
292 * This routes all ignorable warnings to the Tcl function
293 * "gdbtk_tcl_ignorable_warning".
294 */
295
296void
297gdbtk_ignorable_warning (warning)
298 const char *warning;
299{
6f5af15b 300 char buf[512];
ca4e7e14
JI
301 sprintf (buf, warning);
302 gdbtk_two_elem_cmd ("gdbtk_tcl_ignorable_warning", buf);
ca4e7e14
JI
303}
304
ca4e7e14
JI
305static void
306pc_changed()
307{
308 Tcl_Eval (gdbtk_interp, "gdbtk_pc_changed");
309}
310
311\f
312/* This function is called instead of gdb's internal command loop. This is the
313 last chance to do anything before entering the main Tk event loop.
314 At the end of the command, we enter the main loop. */
315
316static void
317tk_command_loop ()
318{
319 extern GDB_FILE *instream;
320
321 /* We no longer want to use stdin as the command input stream */
322 instream = NULL;
323
324 if (Tcl_Eval (gdbtk_interp, "gdbtk_tcl_preloop") != TCL_OK)
325 {
326 char *msg;
327
328 /* Force errorInfo to be set up propertly. */
329 Tcl_AddErrorInfo (gdbtk_interp, "");
330
331 msg = Tcl_GetVar (gdbtk_interp, "errorInfo", TCL_GLOBAL_ONLY);
332#ifdef _WIN32
333 MessageBox (NULL, msg, NULL, MB_OK | MB_ICONERROR | MB_TASKMODAL);
334#else
335 fputs_unfiltered (msg, gdb_stderr);
336#endif
337 }
338
339#ifdef _WIN32
340 close_bfds ();
341#endif
342
343 Tk_MainLoop ();
344}
345
346/* Come here when there is activity on the X file descriptor. */
347
348void
349x_event (signo)
350 int signo;
351{
352 static int in_x_event = 0;
353 static Tcl_Obj *varname = NULL;
354 if (in_x_event || in_fputs)
355 return;
356
357 in_x_event = 1;
358
359#ifdef __CYGWIN32__
360 if (signo == -2)
361 gdbtk_stop_timer ();
362#endif
363
364 /* Process pending events */
365 while (Tcl_DoOneEvent (TCL_DONT_WAIT|TCL_ALL_EVENTS) != 0)
366 ;
367
368 if (load_in_progress)
369 {
370 int val;
371 if (varname == NULL)
372 {
373 Tcl_Obj *varnamestrobj = Tcl_NewStringObj("download_cancel_ok",-1);
374 varname = Tcl_ObjGetVar2(gdbtk_interp,varnamestrobj,NULL,TCL_GLOBAL_ONLY);
375 }
376 if ((Tcl_GetIntFromObj(gdbtk_interp,varname,&val) == TCL_OK) && val)
377 {
378 quit_flag = 1;
379#ifdef REQUEST_QUIT
380 REQUEST_QUIT;
381#else
382 if (immediate_quit)
383 quit ();
384#endif
385 }
386 }
387 in_x_event = 0;
388}
389
390/* VARARGS */
391static void
392#ifdef ANSI_PROTOTYPES
393gdbtk_readline_begin (char *format, ...)
394#else
395gdbtk_readline_begin (va_alist)
396 va_dcl
397#endif
398{
399 va_list args;
6f5af15b 400 char buf[200];
ca4e7e14
JI
401
402#ifdef ANSI_PROTOTYPES
403 va_start (args, format);
404#else
405 char *format;
406 va_start (args);
407 format = va_arg (args, char *);
408#endif
409
410 vsprintf (buf, format, args);
411 gdbtk_two_elem_cmd ("gdbtk_tcl_readline_begin", buf);
412
413}
414
415static char *
416gdbtk_readline (prompt)
417 char *prompt;
418{
419 int result;
420
421#ifdef _WIN32
422 close_bfds ();
423#endif
424
425 result = gdbtk_two_elem_cmd ("gdbtk_tcl_readline", prompt);
426
427 if (result == TCL_OK)
428 {
429 return (strdup (gdbtk_interp -> result));
430 }
431 else
432 {
433 gdbtk_fputs (gdbtk_interp -> result, gdb_stdout);
434 gdbtk_fputs ("\n", gdb_stdout);
435 return (NULL);
436 }
437}
438
439static void
440gdbtk_readline_end ()
441{
442 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_readline_end");
443}
444
445static void
446gdbtk_call_command (cmdblk, arg, from_tty)
447 struct cmd_list_element *cmdblk;
448 char *arg;
449 int from_tty;
450{
451 running_now = 0;
452 if (cmdblk->class == class_run || cmdblk->class == class_trace)
453 {
454
455/* HACK! HACK! This is to get the gui to update the tstart/tstop
456 button only incase of tstart/tstop commands issued from the console
457 We don't want to update the src window, so we need to have specific
458 procedures to do tstart and tstop
459 Unfortunately this will not display errors from tstart or tstop in the
460 console window itself, but as dialogs.*/
461
462 if (!strcmp(cmdblk->name, "tstart") && !No_Update)
463 {
464 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_tstart");
465 (*cmdblk->function.cfunc)(arg, from_tty);
466 }
467 else if (!strcmp(cmdblk->name, "tstop") && !No_Update)
468 {
469 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_tstop");
470 (*cmdblk->function.cfunc)(arg, from_tty);
471 }
472/* end of hack */
473 else
474 {
475 running_now = 1;
476 if (!No_Update)
477 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_busy");
478 (*cmdblk->function.cfunc)(arg, from_tty);
479 running_now = 0;
480 if (!No_Update)
481 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_idle");
482 }
483 }
484 else
485 (*cmdblk->function.cfunc)(arg, from_tty);
486}
487
488/* The next three functions use breakpoint_notify to allow the GUI
489 * to handle creating, deleting and modifying breakpoints. These three
490 * functions are put into the appropriate gdb hooks in gdbtk_init.
491 */
492
493static void
494gdbtk_create_breakpoint(b)
495 struct breakpoint *b;
496{
497 breakpoint_notify (b, "create");
498}
499
500static void
501gdbtk_delete_breakpoint(b)
502 struct breakpoint *b;
503{
504 breakpoint_notify (b, "delete");
505}
506
507static void
508gdbtk_modify_breakpoint(b)
509 struct breakpoint *b;
510{
511 breakpoint_notify (b, "modify");
512}
513
514/* This is the generic function for handling changes in
515 * a breakpoint. It routes the information to the Tcl
516 * command "gdbtk_tcl_breakpoint" in the form:
517 * gdbtk_tcl_breakpoint action b_number b_address b_line b_file
518 * On error, the error string is written to gdb_stdout.
519 */
520
521static void
522breakpoint_notify(b, action)
523 struct breakpoint *b;
524 const char *action;
525{
526 char buf[256];
527 int v;
528 struct symtab_and_line sal;
529 char *filename;
530
531 if (b->type != bp_breakpoint)
532 return;
533
534 /* We ensure that ACTION contains no special Tcl characters, so we
535 can do this. */
536 sal = find_pc_line (b->address, 0);
537 filename = symtab_to_filename (sal.symtab);
538 if (filename == NULL)
539 filename = "";
540
541 sprintf (buf, "gdbtk_tcl_breakpoint %s %d 0x%lx %d {%s}", action, b->number,
542 (long)b->address, b->line_number, filename);
543
544 v = Tcl_Eval (gdbtk_interp, buf);
545
546 if (v != TCL_OK)
547 {
548 gdbtk_fputs (Tcl_GetStringResult (gdbtk_interp), gdb_stdout);
549 gdbtk_fputs ("\n", gdb_stdout);
550 }
551}
552
553int
554gdbtk_load_hash (section, num)
555 char *section;
556 unsigned long num;
557{
558 char buf[128];
559 sprintf (buf, "download_hash %s %ld", section, num);
560 Tcl_Eval (gdbtk_interp, buf);
561 return atoi (gdbtk_interp->result);
562}
563
564
565/* This hook is called whenever we are ready to load a symbol file so that
566 the UI can notify the user... */
567static void
568gdbtk_pre_add_symbol (name)
569 char *name;
570{
571
572 gdbtk_two_elem_cmd("gdbtk_tcl_pre_add_symbol", name);
573
574}
575
576/* This hook is called whenever we finish loading a symbol file. */
577static void
578gdbtk_post_add_symbol ()
579{
580 Tcl_Eval (gdbtk_interp, "gdbtk_tcl_post_add_symbol");
581}
582
583/* This hook function is called whenever we want to wait for the
584 target. */
585
586static int
587gdbtk_wait (pid, ourstatus)
588 int pid;
589 struct target_waitstatus *ourstatus;
590{
591 gdbtk_start_timer ();
592 pid = target_wait (pid, ourstatus);
593 gdbtk_stop_timer ();
594 return pid;
595}
596
597/*
598 * This handles all queries from gdb.
599 * The first argument is a printf style format statement, the rest are its
600 * arguments. The resultant formatted string is passed to the Tcl function
601 * "gdbtk_tcl_query".
602 * It returns the users response to the query, as well as putting the value
603 * in the result field of the Tcl interpreter.
604 */
605
606static int
607gdbtk_query (query, args)
608 const char *query;
609 va_list args;
610{
611 char buf[200];
612 long val;
613
614 vsprintf (buf, query, args);
615 gdbtk_two_elem_cmd ("gdbtk_tcl_query", buf);
616
617 val = atol (gdbtk_interp->result);
618 return val;
619}
620
621
622static void
623gdbtk_print_frame_info (s, line, stopline, noerror)
624 struct symtab *s;
625 int line;
626 int stopline;
627 int noerror;
628{
629 current_source_symtab = s;
630 current_source_line = line;
631}
632
633static void
634gdbtk_create_tracepoint (tp)
635 struct tracepoint *tp;
636{
637 tracepoint_notify (tp, "create");
638}
639
640static void
641gdbtk_delete_tracepoint (tp)
642 struct tracepoint *tp;
643{
644 tracepoint_notify (tp, "delete");
645}
646
647static void
648gdbtk_modify_tracepoint (tp)
649 struct tracepoint *tp;
650{
651 tracepoint_notify (tp, "modify");
652}
653
654static void
655tracepoint_notify(tp, action)
656 struct tracepoint *tp;
657 const char *action;
658{
659 char buf[256];
660 int v;
661 struct symtab_and_line sal;
662 char *filename;
663
664 /* We ensure that ACTION contains no special Tcl characters, so we
665 can do this. */
666 sal = find_pc_line (tp->address, 0);
667
668 filename = symtab_to_filename (sal.symtab);
669 if (filename == NULL)
670 filename = "N/A";
6f5af15b 671 sprintf (buf, "gdbtk_tcl_tracepoint %s %d 0x%lx %d {%s} %d", action, tp->number,
ca4e7e14
JI
672 (long)tp->address, sal.line, filename, tp->pass_count);
673
674 v = Tcl_Eval (gdbtk_interp, buf);
675
676 if (v != TCL_OK)
677 {
678 gdbtk_fputs (gdbtk_interp->result, gdb_stdout);
679 gdbtk_fputs ("\n", gdb_stdout);
680 }
681}
682
683
This page took 0.049981 seconds and 4 git commands to generate.