* mn10300.igen (OP_F0F4): Need to load contents of register AN0
[platform/upstream/binutils.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
81 #ifdef __CYGWIN32__
82 extern void (*ui_loop_hook) PARAMS ((int));
83 #endif
84
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 *));
99 static void
100 #ifdef ANSI_PROTOTYPES
101 gdbtk_readline_begin (char *format, ...);
102 #else
103 gdbtk_readline_begin ();
104 #endif
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));
113
114 /*
115  * gdbtk_fputs can't be static, because we need to call it in gdbtk.c.
116  * See note there for details.
117  */
118
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 *));
122
123 /*
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.
126  */
127
128 void
129 gdbtk_add_hooks(void)
130 {
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;
136
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;
141
142   create_breakpoint_hook = gdbtk_create_breakpoint;
143   delete_breakpoint_hook = gdbtk_delete_breakpoint;
144   modify_breakpoint_hook = gdbtk_modify_breakpoint;
145
146   interactive_hook = gdbtk_interactive;
147   target_wait_hook = gdbtk_wait;
148   ui_load_progress_hook = gdbtk_load_hash;
149 #ifdef __CYGWIN32__
150   ui_loop_hook = x_event;
151 #endif
152   pre_add_symbol_hook   = gdbtk_pre_add_symbol;
153   post_add_symbol_hook  = gdbtk_post_add_symbol;
154
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;
160   
161 }
162
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.
166
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
170    execution.
171    
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.
176
177    */
178
179 gdbtk_result *result_ptr = NULL;
180 \f
181
182 /* This allows you to Tcl_Eval a tcl command which takes
183    a command word, and then a single argument. */
184   
185 int gdbtk_two_elem_cmd (cmd_name, argv1)
186     char *cmd_name;
187     char * argv1;
188 {
189   char *command;
190   int result, flags_ptr, arg_len, cmd_len;
191
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, " ");
197   
198   Tcl_ConvertElement (argv1, command + cmd_len + 1, flags_ptr);
199
200   result = Tcl_Eval (gdbtk_interp, command);
201   free (command);
202   return result;
203   
204 }
205
206 static void
207 gdbtk_flush (stream)
208      FILE *stream;
209 {
210 #if 0
211   /* Force immediate screen update */
212
213   Tcl_VarEval (gdbtk_interp, "gdbtk_tcl_flush", NULL);
214 #endif
215 }
216
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
221  * window.
222  *
223  * The cases are:
224  *
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.
234  *
235  */
236
237 void
238 gdbtk_fputs (ptr, stream)
239      const char *ptr;
240      FILE *stream;
241 {
242   in_fputs = 1;
243
244   if (result_ptr != NULL)
245     {
246       if (result_ptr->flags & GDBTK_TO_RESULT)
247         {
248           if (result_ptr->flags & GDBTK_MAKES_LIST)
249             Tcl_ListObjAppendElement(NULL, result_ptr->obj_ptr, 
250                                      Tcl_NewStringObj((char *) ptr, -1));
251           else                           
252             Tcl_AppendToObj (result_ptr->obj_ptr, (char *) ptr, -1);
253         }
254       else if (stream == gdb_stderr)
255         {
256           if (result_ptr->flags & GDBTK_ERROR_STARTED)
257             Tcl_AppendToObj (result_ptr->obj_ptr, (char *) ptr, -1);
258           else
259             {
260               Tcl_SetStringObj (result_ptr->obj_ptr, (char *) ptr, -1);
261               result_ptr->flags |= GDBTK_ERROR_STARTED;
262             }
263         }
264       else
265         {
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", " ");
269         }
270     }
271   else
272     {
273       gdbtk_two_elem_cmd ("gdbtk_tcl_fputs", (char *) ptr);
274     }
275   
276   in_fputs = 0;
277 }
278
279 /*
280  * This routes all warnings to the Tcl function "gdbtk_tcl_warning".
281  */
282
283 static void
284 gdbtk_warning (warning, args)
285      const char *warning;
286      va_list args;
287 {
288   char buf[200];
289
290   vsprintf (buf, warning, args);
291   gdbtk_two_elem_cmd ("gdbtk_tcl_warning", buf);
292
293 }
294
295 /*
296  * This routes all ignorable warnings to the Tcl function
297  * "gdbtk_tcl_ignorable_warning".
298  */
299
300 void
301 gdbtk_ignorable_warning (warning)
302      const char *warning;
303 {
304   char buf[512];
305   sprintf (buf, warning);
306   gdbtk_two_elem_cmd ("gdbtk_tcl_ignorable_warning", buf);
307 }
308
309 static void
310 pc_changed()
311 {
312   Tcl_Eval (gdbtk_interp, "gdbtk_pc_changed");
313 }
314
315 \f
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. */
319
320 static void
321 tk_command_loop ()
322 {
323   extern GDB_FILE *instream;
324
325   /* We no longer want to use stdin as the command input stream */
326   instream = NULL;
327
328   if (Tcl_Eval (gdbtk_interp, "gdbtk_tcl_preloop") != TCL_OK)
329     {
330       char *msg;
331
332       /* Force errorInfo to be set up propertly.  */
333       Tcl_AddErrorInfo (gdbtk_interp, "");
334
335       msg = Tcl_GetVar (gdbtk_interp, "errorInfo", TCL_GLOBAL_ONLY);
336 #ifdef _WIN32
337       MessageBox (NULL, msg, NULL, MB_OK | MB_ICONERROR | MB_TASKMODAL);
338 #else
339       fputs_unfiltered (msg, gdb_stderr);
340 #endif
341     }
342
343 #ifdef _WIN32
344   close_bfds ();
345 #endif
346
347   Tk_MainLoop ();
348 }
349
350 /* Come here when there is activity on the X file descriptor. */
351
352 void
353 x_event (signo)
354      int signo;
355 {
356   static int in_x_event = 0;
357   static Tcl_Obj *varname = NULL;
358   if (in_x_event || in_fputs)
359     return; 
360
361   in_x_event = 1;
362
363 #ifdef __CYGWIN32__
364   if (signo == -2)
365     gdbtk_stop_timer ();
366 #endif
367
368   /* Process pending events */
369   while (Tcl_DoOneEvent (TCL_DONT_WAIT|TCL_ALL_EVENTS) != 0)
370     ;
371
372   if (load_in_progress)
373     {
374       int val;
375       if (varname == NULL)
376         {
377           Tcl_Obj *varnamestrobj = Tcl_NewStringObj("download_cancel_ok",-1);
378           varname = Tcl_ObjGetVar2(gdbtk_interp,varnamestrobj,NULL,TCL_GLOBAL_ONLY);
379         }
380       if ((Tcl_GetIntFromObj(gdbtk_interp,varname,&val) == TCL_OK) && val)
381         {
382           quit_flag = 1;
383 #ifdef REQUEST_QUIT
384           REQUEST_QUIT;
385 #else
386           if (immediate_quit) 
387             quit ();
388 #endif
389         }
390     }
391   in_x_event = 0;
392 }
393
394 /* VARARGS */
395 static void
396 #ifdef ANSI_PROTOTYPES
397 gdbtk_readline_begin (char *format, ...)
398 #else
399 gdbtk_readline_begin (va_alist)
400      va_dcl
401 #endif
402 {
403   va_list args;
404   char buf[200];
405
406 #ifdef ANSI_PROTOTYPES
407   va_start (args, format);
408 #else
409   char *format;
410   va_start (args);
411   format = va_arg (args, char *);
412 #endif
413
414   vsprintf (buf, format, args);
415   gdbtk_two_elem_cmd ("gdbtk_tcl_readline_begin", buf);
416
417 }
418
419 static char *
420 gdbtk_readline (prompt)
421      char *prompt;
422 {
423   int result;
424
425 #ifdef _WIN32
426   close_bfds ();
427 #endif
428
429   result = gdbtk_two_elem_cmd ("gdbtk_tcl_readline", prompt);
430
431   if (result == TCL_OK)
432     {
433       return (strdup (gdbtk_interp -> result));
434     }
435   else
436     {
437       gdbtk_fputs (gdbtk_interp -> result, gdb_stdout);
438       gdbtk_fputs ("\n", gdb_stdout);
439       return (NULL);
440     }
441 }
442
443 static void
444 gdbtk_readline_end ()
445 {
446   Tcl_Eval (gdbtk_interp, "gdbtk_tcl_readline_end");
447 }
448
449 static void
450 gdbtk_call_command (cmdblk, arg, from_tty)
451      struct cmd_list_element *cmdblk;
452      char *arg;
453      int from_tty;
454 {
455   running_now = 0;
456   if (cmdblk->class == class_run || cmdblk->class == class_trace)
457     {
458
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.*/
465
466       if (!strcmp(cmdblk->name, "tstart") && !No_Update)
467         {
468           Tcl_Eval (gdbtk_interp, "gdbtk_tcl_tstart"); 
469           (*cmdblk->function.cfunc)(arg, from_tty);
470         }
471       else if (!strcmp(cmdblk->name, "tstop") && !No_Update) 
472         {
473           Tcl_Eval (gdbtk_interp, "gdbtk_tcl_tstop"); 
474           (*cmdblk->function.cfunc)(arg, from_tty);
475         }
476       /* end of hack */
477       else 
478         {
479           running_now = 1;
480           if (!No_Update)
481             Tcl_Eval (gdbtk_interp, "gdbtk_tcl_busy");
482           (*cmdblk->function.cfunc)(arg, from_tty);
483           running_now = 0;
484           if (!No_Update)
485             Tcl_Eval (gdbtk_interp, "gdbtk_tcl_idle");
486         }
487     }
488   else
489     (*cmdblk->function.cfunc)(arg, from_tty);
490 }
491
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.
495  */
496
497 static void
498 gdbtk_create_breakpoint(b)
499      struct breakpoint *b;
500 {
501   breakpoint_notify (b, "create");
502 }
503
504 static void
505 gdbtk_delete_breakpoint(b)
506      struct breakpoint *b;
507 {
508   breakpoint_notify (b, "delete");
509 }
510
511 static void
512 gdbtk_modify_breakpoint(b)
513      struct breakpoint *b;
514 {
515   breakpoint_notify (b, "modify");
516 }
517
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.
523  */
524
525 static void
526 breakpoint_notify(b, action)
527      struct breakpoint *b;
528      const char *action;
529 {
530   char buf[256];
531   int v;
532   struct symtab_and_line sal;
533   char *filename;
534
535   if (b->type != bp_breakpoint)
536     return;
537
538   /* We ensure that ACTION contains no special Tcl characters, so we
539      can do this.  */
540   sal = find_pc_line (b->address, 0);
541   filename = symtab_to_filename (sal.symtab);
542   if (filename == NULL)
543     filename = "";
544
545   sprintf (buf, "gdbtk_tcl_breakpoint %s %d 0x%lx %d {%s}", action, b->number, 
546            (long)b->address, b->line_number, filename);
547
548   v = Tcl_Eval (gdbtk_interp, buf);
549
550   if (v != TCL_OK)
551     {
552       gdbtk_fputs (Tcl_GetStringResult (gdbtk_interp), gdb_stdout);
553       gdbtk_fputs ("\n", gdb_stdout);
554     }
555 }
556
557 int
558 gdbtk_load_hash (section, num)
559      char *section;
560      unsigned long num;
561 {
562   char buf[128];
563   sprintf (buf, "download_hash %s %ld", section, num);
564   Tcl_Eval (gdbtk_interp, buf); 
565   return  atoi (gdbtk_interp->result);
566 }
567
568
569 /* This hook is called whenever we are ready to load a symbol file so that
570    the UI can notify the user... */
571 static void
572 gdbtk_pre_add_symbol (name)
573   char *name;
574 {
575
576   gdbtk_two_elem_cmd("gdbtk_tcl_pre_add_symbol", name);
577
578 }
579
580 /* This hook is called whenever we finish loading a symbol file. */
581 static void
582 gdbtk_post_add_symbol ()
583 {
584   Tcl_Eval (gdbtk_interp, "gdbtk_tcl_post_add_symbol");
585 }
586
587 /* This hook function is called whenever we want to wait for the
588    target.  */
589
590 static int
591 gdbtk_wait (pid, ourstatus)
592      int pid;
593      struct target_waitstatus *ourstatus;
594 {
595   gdbtk_start_timer ();
596   pid = target_wait (pid, ourstatus);
597   gdbtk_stop_timer ();
598   return pid;
599 }
600
601 /*
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
605  * "gdbtk_tcl_query".  
606  * It returns the users response to the query, as well as putting the value
607  * in the result field of the Tcl interpreter.
608  */
609
610 static int
611 gdbtk_query (query, args)
612      const char *query;
613      va_list args;
614 {
615   char buf[200];
616   long val;
617
618   vsprintf (buf, query, args);
619   gdbtk_two_elem_cmd ("gdbtk_tcl_query", buf);
620  
621   val = atol (gdbtk_interp->result);
622   return val;
623 }
624
625
626 static void
627 gdbtk_print_frame_info (s, line, stopline, noerror)
628   struct symtab *s;
629   int line;
630   int stopline;
631   int noerror;
632 {
633   current_source_symtab = s;
634   current_source_line = line;
635 }
636
637 static void
638 gdbtk_create_tracepoint (tp)
639   struct tracepoint *tp;
640 {
641   tracepoint_notify (tp, "create");
642 }
643
644 static void
645 gdbtk_delete_tracepoint (tp)
646   struct tracepoint *tp;
647 {
648   tracepoint_notify (tp, "delete");
649 }
650
651 static void
652 gdbtk_modify_tracepoint (tp)
653   struct tracepoint *tp;
654 {
655   tracepoint_notify (tp, "modify");
656 }
657
658 static void
659 tracepoint_notify(tp, action)
660      struct tracepoint *tp;
661      const char *action;
662 {
663   char buf[256];
664   int v;
665   struct symtab_and_line sal;
666   char *filename;
667
668   /* We ensure that ACTION contains no special Tcl characters, so we
669      can do this.  */
670   sal = find_pc_line (tp->address, 0);
671
672   filename = symtab_to_filename (sal.symtab);
673   if (filename == NULL)
674     filename = "N/A";
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);
677
678   v = Tcl_Eval (gdbtk_interp, buf);
679
680   if (v != TCL_OK)
681     {
682       gdbtk_fputs (gdbtk_interp->result, gdb_stdout);
683       gdbtk_fputs ("\n", gdb_stdout);
684     }
685 }
686
687 static void
688 gdbtk_selected_frame_changed (level)
689      int level;
690 {
691   Tcl_UpdateLinkedVar (gdbtk_interp, "gdb_selected_frame_level");
692 }