]> Git Repo - binutils.git/blob - gdb/mi/mi-main.c
From Jimi X <[email protected]>:
[binutils.git] / gdb / mi / mi-main.c
1 /* MI Command Set.
2    Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
3    Contributed by Cygnus Solutions (a Red Hat company).
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 /* Work in progress */
23
24 #include "defs.h"
25 #include "target.h"
26 #include "inferior.h"
27 #include "gdb_string.h"
28 #include "top.h"
29 #include "gdbthread.h"
30 #include "mi-cmds.h"
31 #include "mi-parse.h"
32 #include "mi-getopt.h"
33 #include "mi-console.h"
34 #include "ui-out.h"
35 #include "mi-out.h"
36 #include "event-loop.h"
37 #include "event-top.h"
38 #include "gdbcore.h"            /* for write_memory() */
39 #include "value.h"              /* for write_register_bytes() */
40 #include "regcache.h"
41 #include "gdb.h"
42 #include <ctype.h>
43 #include <sys/time.h>
44
45 enum
46   {
47     FROM_TTY = 0
48   };
49
50
51 int mi_debug_p;
52 struct ui_file *raw_stdout;
53
54 /* The token of the last asynchronous command */
55 static char *last_async_command;
56 static char *previous_async_command;
57 static char *mi_error_message;
58 static char *old_regs;
59
60 extern void _initialize_mi_main (void);
61 static char *mi_input (char *);
62 static void mi_execute_command (char *cmd, int from_tty);
63 static enum mi_cmd_result mi_cmd_execute (struct mi_parse *parse);
64
65 static void mi_execute_cli_command (const char *cli, char *args);
66 static enum mi_cmd_result mi_execute_async_cli_command (char *mi, char *args, int from_tty);
67 static void mi_execute_command_wrapper (char *cmd);
68
69 void mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg);
70
71 static int register_changed_p (int regnum);
72 static int get_register (int regnum, int format);
73 static void mi_load_progress (const char *section_name,
74                               unsigned long sent_so_far,
75                               unsigned long total_section,
76                               unsigned long total_sent,
77                               unsigned long grand_total);
78
79 /* FIXME: these should go in some .h file, but infcmd.c doesn't have a
80    corresponding .h file. These wrappers will be obsolete anyway, once
81    we pull the plug on the sanitization. */
82 extern void interrupt_target_command_wrapper (char *, int);
83 extern void return_command_wrapper (char *, int);
84
85 /* Command implementations. FIXME: Is this libgdb? No.  This is the MI
86    layer that calls libgdb.  Any operation used in the below should be
87    formalized. */
88
89 enum mi_cmd_result
90 mi_cmd_gdb_exit (char *command, char **argv, int argc)
91 {
92   /* We have to print everything right here because we never return */
93   if (last_async_command)
94     fputs_unfiltered (last_async_command, raw_stdout);
95   fputs_unfiltered ("^exit\n", raw_stdout);
96   mi_out_put (uiout, raw_stdout);
97   /* FIXME: The function called is not yet a formal libgdb function */
98   quit_force (NULL, FROM_TTY);
99   return MI_CMD_DONE;
100 }
101
102 enum mi_cmd_result
103 mi_cmd_exec_run (char *args, int from_tty)
104 {
105   /* FIXME: Should call a libgdb function, not a cli wrapper */
106   return mi_execute_async_cli_command ("run", args, from_tty);
107 }
108
109 enum mi_cmd_result
110 mi_cmd_exec_next (char *args, int from_tty)
111 {
112   /* FIXME: Should call a libgdb function, not a cli wrapper */
113   return mi_execute_async_cli_command ("next", args, from_tty);
114 }
115
116 enum mi_cmd_result
117 mi_cmd_exec_next_instruction (char *args, int from_tty)
118 {
119   /* FIXME: Should call a libgdb function, not a cli wrapper */
120   return mi_execute_async_cli_command ("nexti", args, from_tty);
121 }
122
123 enum mi_cmd_result
124 mi_cmd_exec_step (char *args, int from_tty)
125 {
126   /* FIXME: Should call a libgdb function, not a cli wrapper */
127   return mi_execute_async_cli_command ("step", args, from_tty);
128 }
129
130 enum mi_cmd_result
131 mi_cmd_exec_step_instruction (char *args, int from_tty)
132 {
133   /* FIXME: Should call a libgdb function, not a cli wrapper */
134   return mi_execute_async_cli_command ("stepi", args, from_tty);
135 }
136
137 enum mi_cmd_result
138 mi_cmd_exec_finish (char *args, int from_tty)
139 {
140   /* FIXME: Should call a libgdb function, not a cli wrapper */
141   return mi_execute_async_cli_command ("finish", args, from_tty);
142 }
143
144 enum mi_cmd_result
145 mi_cmd_exec_until (char *args, int from_tty)
146 {
147   /* FIXME: Should call a libgdb function, not a cli wrapper */
148   return mi_execute_async_cli_command ("until", args, from_tty);
149 }
150
151 enum mi_cmd_result
152 mi_cmd_exec_return (char *args, int from_tty)
153 {
154   /* This command doesn't really execute the target, it just pops the
155      specified number of frames. */
156   if (*args)
157     /* Call return_command with from_tty argument equal to 0 so as to
158        avoid being queried. */
159     return_command_wrapper (args, 0);
160   else
161     /* Call return_command with from_tty argument equal to 0 so as to
162        avoid being queried. */
163     return_command_wrapper (NULL, 0);
164
165   /* Because we have called return_command with from_tty = 0, we need
166      to print the frame here. */
167   show_and_print_stack_frame (selected_frame,
168                               selected_frame_level,
169                               LOC_AND_ADDRESS);
170
171   return MI_CMD_DONE;
172 }
173
174 enum mi_cmd_result
175 mi_cmd_exec_continue (char *args, int from_tty)
176 {
177   /* FIXME: Should call a libgdb function, not a cli wrapper */
178   return mi_execute_async_cli_command ("continue", args, from_tty);
179 }
180
181 /* Interrupt the execution of the target. Note how we must play around
182    with the token varialbes, in order to display the current token in
183    the result of the interrupt command, and the previous execution
184    token when the target finally stops. See comments in
185    mi_cmd_execute. */
186 enum mi_cmd_result
187 mi_cmd_exec_interrupt (char *args, int from_tty)
188 {
189   if (!target_executing)
190     {
191       xasprintf (&mi_error_message,
192                  "mi_cmd_exec_interrupt: Inferior not executing.");
193       return MI_CMD_ERROR;
194     }
195   interrupt_target_command_wrapper (args, from_tty);
196   if (last_async_command)
197     fputs_unfiltered (last_async_command, raw_stdout);
198   fputs_unfiltered ("^done", raw_stdout);
199   xfree (last_async_command);
200   if (previous_async_command)
201     last_async_command = xstrdup (previous_async_command);
202   xfree (previous_async_command);
203   previous_async_command = NULL;
204   mi_out_put (uiout, raw_stdout);
205   mi_out_rewind (uiout);
206   fputs_unfiltered ("\n", raw_stdout);
207   return MI_CMD_QUIET;
208 }
209
210 enum mi_cmd_result
211 mi_cmd_thread_select (char *command, char **argv, int argc)
212 {
213   enum gdb_rc rc;
214
215   if (argc != 1)
216     {
217       xasprintf (&mi_error_message,
218                  "mi_cmd_thread_select: USAGE: threadnum.");
219       return MI_CMD_ERROR;
220     }
221   else
222     rc = gdb_thread_select (uiout, argv[0]);
223
224   if (rc == GDB_RC_FAIL)
225     return MI_CMD_CAUGHT_ERROR;
226   else
227     return MI_CMD_DONE;
228 }
229
230 enum mi_cmd_result
231 mi_cmd_thread_list_ids (char *command, char **argv, int argc)
232 {
233   enum gdb_rc rc = MI_CMD_DONE;
234
235   if (argc != 0)
236     {
237       xasprintf (&mi_error_message,
238                  "mi_cmd_thread_list_ids: No arguments required.");
239       return MI_CMD_ERROR;
240     }
241   else
242     rc = gdb_list_thread_ids (uiout);
243
244   if (rc == GDB_RC_FAIL)
245     return MI_CMD_CAUGHT_ERROR;
246   else
247     return MI_CMD_DONE;
248 }
249
250 enum mi_cmd_result
251 mi_cmd_data_list_register_names (char *command, char **argv, int argc)
252 {
253   int regnum, numregs;
254   int i;
255
256   /* Note that the test for a valid register must include checking the
257      REGISTER_NAME because NUM_REGS may be allocated for the union of
258      the register sets within a family of related processors.  In this
259      case, some entries of REGISTER_NAME will change depending upon
260      the particular processor being debugged.  */
261
262   numregs = NUM_REGS + NUM_PSEUDO_REGS;
263
264   ui_out_list_begin (uiout, "register-names");
265
266   if (argc == 0)                /* No args, just do all the regs */
267     {
268       for (regnum = 0;
269            regnum < numregs;
270            regnum++)
271         {
272           if (REGISTER_NAME (regnum) == NULL
273               || *(REGISTER_NAME (regnum)) == '\0')
274             ui_out_field_string (uiout, NULL, "");
275           else
276             ui_out_field_string (uiout, NULL, REGISTER_NAME (regnum));
277         }
278     }
279
280   /* Else, list of register #s, just do listed regs */
281   for (i = 0; i < argc; i++)
282     {
283       regnum = atoi (argv[i]);
284       if (regnum < 0 || regnum >= numregs)
285         {
286           xasprintf (&mi_error_message, "bad register number");
287           return MI_CMD_ERROR;
288         }
289       if (REGISTER_NAME (regnum) == NULL
290           || *(REGISTER_NAME (regnum)) == '\0')
291         ui_out_field_string (uiout, NULL, "");
292       else
293         ui_out_field_string (uiout, NULL, REGISTER_NAME (regnum));
294     }
295   ui_out_list_end (uiout);
296   return MI_CMD_DONE;
297 }
298
299 enum mi_cmd_result
300 mi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
301 {
302   int regnum, numregs, changed;
303   int i;
304
305   /* Note that the test for a valid register must include checking the
306      REGISTER_NAME because NUM_REGS may be allocated for the union of
307      the register sets within a family of related processors.  In this
308      case, some entries of REGISTER_NAME will change depending upon
309      the particular processor being debugged.  */
310
311   numregs = NUM_REGS;
312
313   ui_out_list_begin (uiout, "changed-registers");
314
315   if (argc == 0)                /* No args, just do all the regs */
316     {
317       for (regnum = 0;
318            regnum < numregs;
319            regnum++)
320         {
321           if (REGISTER_NAME (regnum) == NULL
322               || *(REGISTER_NAME (regnum)) == '\0')
323             continue;
324           changed = register_changed_p (regnum);
325           if (changed < 0)
326             {
327               xasprintf (&mi_error_message,
328                          "mi_cmd_data_list_changed_registers: Unable to read register contents.");
329               return MI_CMD_ERROR;
330             }
331           else if (changed)
332             ui_out_field_int (uiout, NULL, regnum);
333         }
334     }
335
336   /* Else, list of register #s, just do listed regs */
337   for (i = 0; i < argc; i++)
338     {
339       regnum = atoi (argv[i]);
340
341       if (regnum >= 0
342           && regnum < numregs
343           && REGISTER_NAME (regnum) != NULL
344           && *REGISTER_NAME (regnum) != '\000')
345         {
346           changed = register_changed_p (regnum);
347           if (changed < 0)
348             {
349               xasprintf (&mi_error_message,
350                          "mi_cmd_data_list_register_change: Unable to read register contents.");
351               return MI_CMD_ERROR;
352             }
353           else if (changed)
354             ui_out_field_int (uiout, NULL, regnum);
355         }
356       else
357         {
358           xasprintf (&mi_error_message, "bad register number");
359           return MI_CMD_ERROR;
360         }
361     }
362   ui_out_list_end (uiout);
363   return MI_CMD_DONE;
364 }
365
366 static int
367 register_changed_p (int regnum)
368 {
369   char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
370
371   if (read_relative_register_raw_bytes (regnum, raw_buffer))
372     return -1;
373
374   if (memcmp (&old_regs[REGISTER_BYTE (regnum)], raw_buffer,
375               REGISTER_RAW_SIZE (regnum)) == 0)
376     return 0;
377
378   /* Found a changed register. Return 1. */
379
380   memcpy (&old_regs[REGISTER_BYTE (regnum)], raw_buffer,
381           REGISTER_RAW_SIZE (regnum));
382
383   return 1;
384 }
385
386 /* Return a list of register number and value pairs. The valid
387    arguments expected are: a letter indicating the format in which to
388    display the registers contents. This can be one of: x (hexadecimal), d
389    (decimal), N (natural), t (binary), o (octal), r (raw).  After the
390    format argumetn there can be a sequence of numbers, indicating which
391    registers to fetch the content of. If the format is the only argument,
392    a list of all the registers with their values is returned. */
393 enum mi_cmd_result
394 mi_cmd_data_list_register_values (char *command, char **argv, int argc)
395 {
396   int regnum, numregs, format, result;
397   int i;
398
399   /* Note that the test for a valid register must include checking the
400      REGISTER_NAME because NUM_REGS may be allocated for the union of
401      the register sets within a family of related processors.  In this
402      case, some entries of REGISTER_NAME will change depending upon
403      the particular processor being debugged.  */
404
405   numregs = NUM_REGS;
406
407   if (argc == 0)
408     {
409       xasprintf (&mi_error_message,
410                  "mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
411       return MI_CMD_ERROR;
412     }
413
414   format = (int) argv[0][0];
415
416   if (!target_has_registers)
417     {
418       xasprintf (&mi_error_message,
419                  "mi_cmd_data_list_register_values: No registers.");
420       return MI_CMD_ERROR;
421     }
422
423   ui_out_list_begin (uiout, "register-values");
424
425   if (argc == 1)                /* No args, beside the format: do all the regs */
426     {
427       for (regnum = 0;
428            regnum < numregs;
429            regnum++)
430         {
431           if (REGISTER_NAME (regnum) == NULL
432               || *(REGISTER_NAME (regnum)) == '\0')
433             continue;
434           ui_out_tuple_begin (uiout, NULL);
435           ui_out_field_int (uiout, "number", regnum);
436           result = get_register (regnum, format);
437           if (result == -1)
438             return MI_CMD_ERROR;
439           ui_out_tuple_end (uiout);
440         }
441     }
442
443   /* Else, list of register #s, just do listed regs */
444   for (i = 1; i < argc; i++)
445     {
446       regnum = atoi (argv[i]);
447
448       if (regnum >= 0
449           && regnum < numregs
450           && REGISTER_NAME (regnum) != NULL
451           && *REGISTER_NAME (regnum) != '\000')
452         {
453           ui_out_tuple_begin (uiout, NULL);
454           ui_out_field_int (uiout, "number", regnum);
455           result = get_register (regnum, format);
456           if (result == -1)
457             return MI_CMD_ERROR;
458           ui_out_tuple_end (uiout);
459         }
460       else
461         {
462           xasprintf (&mi_error_message, "bad register number");
463           return MI_CMD_ERROR;
464         }
465     }
466   ui_out_list_end (uiout);
467   return MI_CMD_DONE;
468 }
469
470 /* Output one register's contents in the desired format. */
471 static int
472 get_register (int regnum, int format)
473 {
474   char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
475   char *virtual_buffer = alloca (MAX_REGISTER_VIRTUAL_SIZE);
476   int optim;
477   static struct ui_stream *stb = NULL;
478
479   stb = ui_out_stream_new (uiout);
480
481   if (format == 'N')
482     format = 0;
483
484   /* read_relative_register_raw_bytes returns a virtual frame pointer
485      (FRAME_FP (selected_frame)) if regnum == FP_REGNUM instead
486      of the real contents of the register. To get around this,
487      use get_saved_register instead. */
488   get_saved_register (raw_buffer, &optim, (CORE_ADDR *) NULL, selected_frame,
489                       regnum, (enum lval_type *) NULL);
490   if (optim)
491     {
492       xasprintf (&mi_error_message, "Optimized out");
493       return -1;
494     }
495
496   /* Convert raw data to virtual format if necessary.  */
497
498   if (REGISTER_CONVERTIBLE (regnum))
499     {
500       REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
501                                    raw_buffer, virtual_buffer);
502     }
503   else
504     memcpy (virtual_buffer, raw_buffer, REGISTER_VIRTUAL_SIZE (regnum));
505
506   if (format == 'r')
507     {
508       int j;
509       char *ptr, buf[1024];
510
511       strcpy (buf, "0x");
512       ptr = buf + 2;
513       for (j = 0; j < REGISTER_RAW_SIZE (regnum); j++)
514         {
515           register int idx = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? j
516           : REGISTER_RAW_SIZE (regnum) - 1 - j;
517           sprintf (ptr, "%02x", (unsigned char) raw_buffer[idx]);
518           ptr += 2;
519         }
520       ui_out_field_string (uiout, "value", buf);
521       /*fputs_filtered (buf, gdb_stdout); */
522     }
523   else
524     {
525       val_print (REGISTER_VIRTUAL_TYPE (regnum), virtual_buffer, 0, 0,
526                  stb->stream, format, 1, 0, Val_pretty_default);
527       ui_out_field_stream (uiout, "value", stb);
528       ui_out_stream_delete (stb);
529     }
530   return 1;
531 }
532
533 /* Write given values into registers. The registers and values are
534    given as pairs. The corresponding MI command is 
535    -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
536 enum mi_cmd_result
537 mi_cmd_data_write_register_values (char *command, char **argv, int argc)
538 {
539   int regnum;
540   int i;
541   int numregs;
542   LONGEST value;
543   char format;
544
545   /* Note that the test for a valid register must include checking the
546      REGISTER_NAME because NUM_REGS may be allocated for the union of
547      the register sets within a family of related processors.  In this
548      case, some entries of REGISTER_NAME will change depending upon
549      the particular processor being debugged.  */
550
551   numregs = NUM_REGS;
552
553   if (argc == 0)
554     {
555       xasprintf (&mi_error_message,
556                  "mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
557       return MI_CMD_ERROR;
558     }
559
560   format = (int) argv[0][0];
561
562   if (!target_has_registers)
563     {
564       xasprintf (&mi_error_message,
565                  "mi_cmd_data_write_register_values: No registers.");
566       return MI_CMD_ERROR;
567     }
568
569   if (!(argc - 1))
570     {
571       xasprintf (&mi_error_message,
572                  "mi_cmd_data_write_register_values: No regs and values specified.");
573       return MI_CMD_ERROR;
574     }
575
576   if ((argc - 1) % 2)
577     {
578       xasprintf (&mi_error_message,
579                  "mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
580       return MI_CMD_ERROR;
581     }
582
583   for (i = 1; i < argc; i = i + 2)
584     {
585       regnum = atoi (argv[i]);
586
587       if (regnum >= 0
588           && regnum < numregs
589           && REGISTER_NAME (regnum) != NULL
590           && *REGISTER_NAME (regnum) != '\000')
591         {
592           void *buffer;
593           struct cleanup *old_chain;
594
595           /* Get the value as a number */
596           value = parse_and_eval_address (argv[i + 1]);
597           /* Get the value into an array */
598           buffer = xmalloc (REGISTER_SIZE);
599           old_chain = make_cleanup (xfree, buffer);
600           store_signed_integer (buffer, REGISTER_SIZE, value);
601           /* Write it down */
602           write_register_bytes (REGISTER_BYTE (regnum), buffer, REGISTER_RAW_SIZE (regnum));
603           /* Free the buffer.  */
604           do_cleanups (old_chain);
605         }
606       else
607         {
608           xasprintf (&mi_error_message, "bad register number");
609           return MI_CMD_ERROR;
610         }
611     }
612   return MI_CMD_DONE;
613 }
614
615 #if 0
616 /*This is commented out because we decided it was not useful. I leave
617    it, just in case. ezannoni:1999-12-08 */
618
619 /* Assign a value to a variable. The expression argument must be in
620    the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
621    quoted. */
622 enum mi_cmd_result
623 mi_cmd_data_assign (char *command, char **argv, int argc)
624 {
625   struct expression *expr;
626   struct cleanup *old_chain;
627
628   if (argc != 1)
629     {
630       xasprintf (&mi_error_message,
631                  "mi_cmd_data_assign: Usage: -data-assign expression");
632       return MI_CMD_ERROR;
633     }
634
635   /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
636      01-12-1999: Need to decide what to do with this for libgdb purposes. */
637
638   expr = parse_expression (argv[0]);
639   old_chain = make_cleanup (free_current_contents, &expr);
640   evaluate_expression (expr);
641   do_cleanups (old_chain);
642   return MI_CMD_DONE;
643 }
644 #endif
645
646 /* Evaluate the value of the argument. The argument is an
647    expression. If the expression contains spaces it needs to be
648    included in double quotes. */
649 enum mi_cmd_result
650 mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
651 {
652   struct expression *expr;
653   struct cleanup *old_chain = NULL;
654   struct value *val;
655   struct ui_stream *stb = NULL;
656
657   stb = ui_out_stream_new (uiout);
658
659   if (argc != 1)
660     {
661       xasprintf (&mi_error_message,
662                  "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
663       return MI_CMD_ERROR;
664     }
665
666   expr = parse_expression (argv[0]);
667
668   old_chain = make_cleanup (free_current_contents, &expr);
669
670   val = evaluate_expression (expr);
671
672   /* Print the result of the expression evaluation. */
673   val_print (VALUE_TYPE (val), VALUE_CONTENTS (val),
674              VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
675              stb->stream, 0, 0, 0, 0);
676
677   ui_out_field_stream (uiout, "value", stb);
678   ui_out_stream_delete (stb);
679
680   do_cleanups (old_chain);
681
682   return MI_CMD_DONE;
683 }
684
685 enum mi_cmd_result
686 mi_cmd_target_download (char *args, int from_tty)
687 {
688   char *run;
689   struct cleanup *old_cleanups = NULL;
690
691   xasprintf (&run, "load %s", args);
692   old_cleanups = make_cleanup (xfree, run);
693   execute_command (run, from_tty);
694
695   do_cleanups (old_cleanups);
696   return MI_CMD_DONE;
697 }
698
699 /* Connect to the remote target. */
700 enum mi_cmd_result
701 mi_cmd_target_select (char *args, int from_tty)
702 {
703   char *run;
704   struct cleanup *old_cleanups = NULL;
705
706   xasprintf (&run, "target %s", args);
707   old_cleanups = make_cleanup (xfree, run);
708
709   /* target-select is always synchronous.  once the call has returned
710      we know that we are connected. */
711   /* NOTE: At present all targets that are connected are also
712      (implicitly) talking to a halted target.  In the future this may
713      change. */
714   execute_command (run, from_tty);
715
716   do_cleanups (old_cleanups);
717
718   /* Issue the completion message here. */
719   if (last_async_command)
720     fputs_unfiltered (last_async_command, raw_stdout);
721   fputs_unfiltered ("^connected", raw_stdout);
722   mi_out_put (uiout, raw_stdout);
723   mi_out_rewind (uiout);
724   fputs_unfiltered ("\n", raw_stdout);
725   do_exec_cleanups (ALL_CLEANUPS);
726   return MI_CMD_QUIET;
727 }
728
729 /* DATA-MEMORY-READ:
730
731    ADDR: start address of data to be dumped.
732    WORD-FORMAT: a char indicating format for the ``word''. See 
733    the ``x'' command.
734    WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
735    NR_ROW: Number of rows.
736    NR_COL: The number of colums (words per row).
737    ASCHAR: (OPTIONAL) Append an ascii character dump to each row.  Use
738    ASCHAR for unprintable characters.
739
740    Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
741    displayes them.  Returns:
742
743    {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
744
745    Returns: 
746    The number of bytes read is SIZE*ROW*COL. */
747
748 enum mi_cmd_result
749 mi_cmd_data_read_memory (char *command, char **argv, int argc)
750 {
751   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
752   CORE_ADDR addr;
753   long total_bytes;
754   long nr_cols;
755   long nr_rows;
756   char word_format;
757   struct type *word_type;
758   long word_size;
759   char word_asize;
760   char aschar;
761   char *mbuf;
762   int nr_bytes;
763   long offset = 0;
764   int optind = 0;
765   char *optarg;
766   enum opt
767     {
768       OFFSET_OPT
769     };
770   static struct mi_opt opts[] =
771   {
772     {"o", OFFSET_OPT, 1},
773     0
774   };
775
776   while (1)
777     {
778       int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts,
779                            &optind, &optarg);
780       if (opt < 0)
781         break;
782       switch ((enum opt) opt)
783         {
784         case OFFSET_OPT:
785           offset = atol (optarg);
786           break;
787         }
788     }
789   argv += optind;
790   argc -= optind;
791
792   if (argc < 5 || argc > 6)
793     {
794       xasprintf (&mi_error_message,
795                  "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
796       return MI_CMD_ERROR;
797     }
798
799   /* Extract all the arguments. */
800
801   /* Start address of the memory dump. */
802   addr = parse_and_eval_address (argv[0]) + offset;
803   /* The format character to use when displaying a memory word. See
804      the ``x'' command. */
805   word_format = argv[1][0];
806   /* The size of the memory word. */
807   word_size = atol (argv[2]);
808   switch (word_size)
809     {
810     case 1:
811       word_type = builtin_type_int8;
812       word_asize = 'b';
813       break;
814     case 2:
815       word_type = builtin_type_int16;
816       word_asize = 'h';
817       break;
818     case 4:
819       word_type = builtin_type_int32;
820       word_asize = 'w';
821       break;
822     case 8:
823       word_type = builtin_type_int64;
824       word_asize = 'g';
825       break;
826     default:
827       word_type = builtin_type_int8;
828       word_asize = 'b';
829     }
830   /* The number of rows */
831   nr_rows = atol (argv[3]);
832   if (nr_rows <= 0)
833     {
834       xasprintf (&mi_error_message,
835                  "mi_cmd_data_read_memory: invalid number of rows.");
836       return MI_CMD_ERROR;
837     }
838   /* number of bytes per row. */
839   nr_cols = atol (argv[4]);
840   if (nr_cols <= 0)
841     {
842       xasprintf (&mi_error_message,
843                  "mi_cmd_data_read_memory: invalid number of columns.");
844     }
845   /* The un-printable character when printing ascii. */
846   if (argc == 6)
847     aschar = *argv[5];
848   else
849     aschar = 0;
850
851   /* create a buffer and read it in. */
852   total_bytes = word_size * nr_rows * nr_cols;
853   mbuf = xcalloc (total_bytes, 1);
854   make_cleanup (xfree, mbuf);
855   if (mbuf == NULL)
856     {
857       xasprintf (&mi_error_message,
858                  "mi_cmd_data_read_memory: out of memory.");
859       return MI_CMD_ERROR;
860     }
861   nr_bytes = 0;
862   while (nr_bytes < total_bytes)
863     {
864       int error;
865       long num = target_read_memory_partial (addr + nr_bytes, mbuf + nr_bytes,
866                                              total_bytes - nr_bytes,
867                                              &error);
868       if (num <= 0)
869         break;
870       nr_bytes += num;
871     }
872
873   /* output the header information. */
874   ui_out_field_core_addr (uiout, "addr", addr);
875   ui_out_field_int (uiout, "nr-bytes", nr_bytes);
876   ui_out_field_int (uiout, "total-bytes", total_bytes);
877   ui_out_field_core_addr (uiout, "next-row", addr + word_size * nr_cols);
878   ui_out_field_core_addr (uiout, "prev-row", addr - word_size * nr_cols);
879   ui_out_field_core_addr (uiout, "next-page", addr + total_bytes);
880   ui_out_field_core_addr (uiout, "prev-page", addr - total_bytes);
881
882   /* Build the result as a two dimentional table. */
883   {
884     struct ui_stream *stream = ui_out_stream_new (uiout);
885     int row;
886     int row_byte;
887     ui_out_list_begin (uiout, "memory");
888     for (row = 0, row_byte = 0;
889          row < nr_rows;
890          row++, row_byte += nr_cols * word_size)
891       {
892         int col;
893         int col_byte;
894         ui_out_tuple_begin (uiout, NULL);
895         ui_out_field_core_addr (uiout, "addr", addr + row_byte);
896         /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
897         ui_out_list_begin (uiout, "data");
898         for (col = 0, col_byte = row_byte;
899              col < nr_cols;
900              col++, col_byte += word_size)
901           {
902             if (col_byte + word_size > nr_bytes)
903               {
904                 ui_out_field_string (uiout, NULL, "N/A");
905               }
906             else
907               {
908                 ui_file_rewind (stream->stream);
909                 print_scalar_formatted (mbuf + col_byte, word_type, word_format,
910                                         word_asize, stream->stream);
911                 ui_out_field_stream (uiout, NULL, stream);
912               }
913           }
914         ui_out_list_end (uiout);
915         if (aschar)
916           {
917             int byte;
918             ui_file_rewind (stream->stream);
919             for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
920               {
921                 if (byte >= nr_bytes)
922                   {
923                     fputc_unfiltered ('X', stream->stream);
924                   }
925                 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
926                   {
927                     fputc_unfiltered (aschar, stream->stream);
928                   }
929                 else
930                   fputc_unfiltered (mbuf[byte], stream->stream);
931               }
932             ui_out_field_stream (uiout, "ascii", stream);
933           }
934         ui_out_tuple_end (uiout);
935       }
936     ui_out_stream_delete (stream);
937     ui_out_list_end (uiout);
938   }
939   do_cleanups (cleanups);
940   return MI_CMD_DONE;
941 }
942
943 /* DATA-MEMORY-WRITE:
944
945    COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
946    offset from the beginning of the memory grid row where the cell to
947    be written is.
948    ADDR: start address of the row in the memory grid where the memory
949    cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
950    the location to write to.
951    FORMAT: a char indicating format for the ``word''. See 
952    the ``x'' command.
953    WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
954    VALUE: value to be written into the memory address.
955
956    Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
957
958    Prints nothing. */
959 enum mi_cmd_result
960 mi_cmd_data_write_memory (char *command, char **argv, int argc)
961 {
962   CORE_ADDR addr;
963   char word_format;
964   long word_size;
965   /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
966      enough when using a compiler other than GCC. */
967   LONGEST value;
968   void *buffer;
969   struct cleanup *old_chain;
970   long offset = 0;
971   int optind = 0;
972   char *optarg;
973   enum opt
974     {
975       OFFSET_OPT
976     };
977   static struct mi_opt opts[] =
978   {
979     {"o", OFFSET_OPT, 1},
980     0
981   };
982
983   while (1)
984     {
985       int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
986                            &optind, &optarg);
987       if (opt < 0)
988         break;
989       switch ((enum opt) opt)
990         {
991         case OFFSET_OPT:
992           offset = atol (optarg);
993           break;
994         }
995     }
996   argv += optind;
997   argc -= optind;
998
999   if (argc != 4)
1000     {
1001       xasprintf (&mi_error_message,
1002                  "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1003       return MI_CMD_ERROR;
1004     }
1005
1006   /* Extract all the arguments. */
1007   /* Start address of the memory dump. */
1008   addr = parse_and_eval_address (argv[0]);
1009   /* The format character to use when displaying a memory word. See
1010      the ``x'' command. */
1011   word_format = argv[1][0];
1012   /* The size of the memory word. */
1013   word_size = atol (argv[2]);
1014
1015   /* Calculate the real address of the write destination. */
1016   addr += (offset * word_size);
1017
1018   /* Get the value as a number */
1019   value = parse_and_eval_address (argv[3]);
1020   /* Get the value into an array */
1021   buffer = xmalloc (word_size);
1022   old_chain = make_cleanup (xfree, buffer);
1023   store_signed_integer (buffer, word_size, value);
1024   /* Write it down to memory */
1025   write_memory (addr, buffer, word_size);
1026   /* Free the buffer.  */
1027   do_cleanups (old_chain);
1028
1029   return MI_CMD_DONE;
1030 }
1031
1032 /* Execute a command within a safe environment.  Return >0 for
1033    ok. Return <0 for supress prompt.  Return 0 to have the error
1034    extracted from error_last_message(). */
1035
1036 static int
1037 captured_mi_execute_command (void *data)
1038 {
1039   struct mi_parse *context = data;
1040   enum mi_cmd_result rc;
1041
1042   switch (context->op)
1043     {
1044
1045     case MI_COMMAND:
1046       /* A MI command was read from the input stream */
1047       if (mi_debug_p)
1048         /* FIXME: gdb_???? */
1049         fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
1050                             context->token, context->command, context->args);
1051       /* FIXME: cagney/1999-09-25: Rather than this convoluted
1052          condition expression, each function should return an
1053          indication of what action is required and then switch on
1054          that. */
1055       rc = mi_cmd_execute (context);
1056       if (!target_can_async_p () || !target_executing)
1057         {
1058           /* print the result if there were no errors */
1059           if (rc == MI_CMD_DONE)
1060             {
1061               fputs_unfiltered (context->token, raw_stdout);
1062               fputs_unfiltered ("^done", raw_stdout);
1063               mi_out_put (uiout, raw_stdout);
1064               mi_out_rewind (uiout);
1065               fputs_unfiltered ("\n", raw_stdout);
1066             }
1067           else if (rc == MI_CMD_ERROR)
1068             {
1069               if (mi_error_message)
1070                 {
1071                   fputs_unfiltered (context->token, raw_stdout);
1072                   fputs_unfiltered ("^error,msg=\"", raw_stdout);
1073                   fputstr_unfiltered (mi_error_message, '"', raw_stdout);
1074                   xfree (mi_error_message);
1075                   fputs_unfiltered ("\"\n", raw_stdout);
1076                 }
1077               mi_out_rewind (uiout);
1078             }
1079           else if (rc == MI_CMD_CAUGHT_ERROR)
1080             {
1081               mi_out_rewind (uiout);
1082               return 0;
1083             }
1084           else
1085             mi_out_rewind (uiout);
1086         }
1087       else if (sync_execution)
1088         /* Don't print the prompt. We are executing the target in
1089            synchronous mode. */
1090         return -1;
1091       break;
1092
1093     case CLI_COMMAND:
1094       /* A CLI command was read from the input stream */
1095       /* This will be removed as soon as we have a complete set of
1096          mi commands */
1097       /* echo the command on the console. */
1098       fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1099       /* FIXME: If the command string has something that looks like 
1100          a format spec (e.g. %s) we will get a core dump */
1101       mi_execute_cli_command ("%s", context->command);
1102       /* print the result */
1103       /* FIXME: Check for errors here. */
1104       fputs_unfiltered (context->token, raw_stdout);
1105       fputs_unfiltered ("^done", raw_stdout);
1106       mi_out_put (uiout, raw_stdout);
1107       mi_out_rewind (uiout);
1108       fputs_unfiltered ("\n", raw_stdout);
1109       break;
1110
1111     }
1112   return 1;
1113 }
1114
1115
1116 void
1117 mi_execute_command (char *cmd, int from_tty)
1118 {
1119   struct mi_parse *command;
1120
1121   /* This is to handle EOF (^D). We just quit gdb. */
1122   /* FIXME: we should call some API function here. */
1123   if (cmd == 0)
1124     quit_force (NULL, from_tty);
1125
1126   command = mi_parse (cmd);
1127
1128   if (command != NULL)
1129     {
1130       /* FIXME: cagney/1999-11-04: Can this use of catch_errors either
1131          be pushed even further down or even eliminated? */
1132       int rc = catch_errors (captured_mi_execute_command, command, "",
1133                              RETURN_MASK_ALL);
1134       if (rc < 0)
1135         {
1136           /* The command is executing synchronously.  Bail out early
1137              suppressing the finished prompt. */
1138           mi_parse_free (command);
1139           return;
1140         }
1141       if (rc == 0)
1142         {
1143           char *msg = error_last_message ();
1144           struct cleanup *cleanup = make_cleanup (xfree, msg);
1145           /* The command execution failed and error() was called
1146              somewhere */
1147           fputs_unfiltered (command->token, raw_stdout);
1148           fputs_unfiltered ("^error,msg=\"", raw_stdout);
1149           fputstr_unfiltered (msg, '"', raw_stdout);
1150           fputs_unfiltered ("\"\n", raw_stdout);
1151         }
1152       mi_parse_free (command);
1153     }
1154
1155   fputs_unfiltered ("(gdb) \n", raw_stdout);
1156   gdb_flush (raw_stdout);
1157   /* print any buffered hook code */
1158   /* ..... */
1159 }
1160
1161 static enum mi_cmd_result
1162 mi_cmd_execute (struct mi_parse *parse)
1163 {
1164   if (parse->cmd->argv_func != NULL
1165       || parse->cmd->args_func != NULL)
1166     {
1167       /* FIXME: We need to save the token because the command executed
1168          may be asynchronous and need to print the token again.
1169          In the future we can pass the token down to the func
1170          and get rid of the last_async_command */
1171       /* The problem here is to keep the token around when we launch
1172          the target, and we want to interrupt it later on.  The
1173          interrupt command will have its own token, but when the
1174          target stops, we must display the token corresponding to the
1175          last execution command given. So we have another string where
1176          we copy the token (previous_async_command), if this was
1177          indeed the token of an execution command, and when we stop we
1178          print that one. This is possible because the interrupt
1179          command, when over, will copy that token back into the
1180          default token string (last_async_command). */
1181
1182       if (target_executing)
1183         {
1184           if (!previous_async_command)
1185             previous_async_command = xstrdup (last_async_command);
1186           if (strcmp (parse->command, "exec-interrupt"))
1187             {
1188               fputs_unfiltered (parse->token, raw_stdout);
1189               fputs_unfiltered ("^error,msg=\"", raw_stdout);
1190               fputs_unfiltered ("Cannot execute command ", raw_stdout);
1191               fputstr_unfiltered (parse->command, '"', raw_stdout);
1192               fputs_unfiltered (" while target running", raw_stdout);
1193               fputs_unfiltered ("\"\n", raw_stdout);
1194               return MI_CMD_ERROR;
1195             }
1196         }
1197       last_async_command = xstrdup (parse->token);
1198       make_exec_cleanup (free_current_contents, &last_async_command);
1199       /* FIXME: DELETE THIS! */
1200       if (parse->cmd->args_func != NULL)
1201         return parse->cmd->args_func (parse->args, 0 /*from_tty */ );
1202       return parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
1203     }
1204   else if (parse->cmd->cli != 0)
1205     {
1206       /* FIXME: DELETE THIS. */
1207       /* The operation is still implemented by a cli command */
1208       /* Must be a synchronous one */
1209       mi_execute_cli_command (parse->cmd->cli, parse->args);
1210       return MI_CMD_DONE;
1211     }
1212   else
1213     {
1214       /* FIXME: DELETE THIS. */
1215       fputs_unfiltered (parse->token, raw_stdout);
1216       fputs_unfiltered ("^error,msg=\"", raw_stdout);
1217       fputs_unfiltered ("Undefined mi command: ", raw_stdout);
1218       fputstr_unfiltered (parse->command, '"', raw_stdout);
1219       fputs_unfiltered (" (missing implementation)", raw_stdout);
1220       fputs_unfiltered ("\"\n", raw_stdout);
1221       return MI_CMD_ERROR;
1222     }
1223 }
1224
1225 static void
1226 mi_execute_command_wrapper (char *cmd)
1227 {
1228   mi_execute_command (cmd, stdin == instream);
1229 }
1230
1231 /* FIXME: This is just a hack so we can get some extra commands going.
1232    We don't want to channel things through the CLI, but call libgdb directly */
1233 /* Use only for synchronous commands */
1234
1235 void
1236 mi_execute_cli_command (const char *cli, char *args)
1237 {
1238   if (cli != 0)
1239     {
1240       struct cleanup *old_cleanups;
1241       char *run;
1242       xasprintf (&run, cli, args);
1243       if (mi_debug_p)
1244         /* FIXME: gdb_???? */
1245         fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
1246                             cli, run);
1247       old_cleanups = make_cleanup (xfree, run);
1248       execute_command ( /*ui */ run, 0 /*from_tty */ );
1249       do_cleanups (old_cleanups);
1250       return;
1251     }
1252 }
1253
1254 enum mi_cmd_result
1255 mi_execute_async_cli_command (char *mi, char *args, int from_tty)
1256 {
1257   struct cleanup *old_cleanups;
1258   char *run;
1259   char *async_args;
1260
1261   if (target_can_async_p ())
1262     {
1263       async_args = (char *) xmalloc (strlen (args) + 2);
1264       make_exec_cleanup (free, async_args);
1265       strcpy (async_args, args);
1266       strcat (async_args, "&");
1267       xasprintf (&run, "%s %s", mi, async_args);
1268       make_exec_cleanup (free, run);
1269       add_continuation (mi_exec_async_cli_cmd_continuation, NULL);
1270       old_cleanups = NULL;
1271     }
1272   else
1273     {
1274       xasprintf (&run, "%s %s", mi, args);
1275       old_cleanups = make_cleanup (xfree, run);
1276     }
1277
1278   if (!target_can_async_p ())
1279     {
1280       /* NOTE: For synchronous targets asynchronous behavour is faked by
1281          printing out the GDB prompt before we even try to execute the
1282          command. */
1283       if (last_async_command)
1284         fputs_unfiltered (last_async_command, raw_stdout);
1285       fputs_unfiltered ("^running\n", raw_stdout);
1286       fputs_unfiltered ("(gdb) \n", raw_stdout);
1287       gdb_flush (raw_stdout);
1288     }
1289   else
1290     {
1291       /* FIXME: cagney/1999-11-29: Printing this message before
1292          calling execute_command is wrong.  It should only be printed
1293          once gdb has confirmed that it really has managed to send a
1294          run command to the target. */
1295       if (last_async_command)
1296         fputs_unfiltered (last_async_command, raw_stdout);
1297       fputs_unfiltered ("^running\n", raw_stdout);
1298     }
1299
1300   execute_command ( /*ui */ run, 0 /*from_tty */ );
1301
1302   if (!target_can_async_p ())
1303     {
1304       /* Do this before doing any printing.  It would appear that some
1305          print code leaves garbage around in the buffer. */
1306       do_cleanups (old_cleanups);
1307       /* If the target was doing the operation synchronously we fake
1308          the stopped message. */
1309       if (last_async_command)
1310         fputs_unfiltered (last_async_command, raw_stdout);
1311       fputs_unfiltered ("*stopped", raw_stdout);
1312       mi_out_put (uiout, raw_stdout);
1313       mi_out_rewind (uiout);
1314       fputs_unfiltered ("\n", raw_stdout);
1315       return MI_CMD_QUIET;
1316     }
1317   return MI_CMD_DONE;
1318 }
1319
1320 void
1321 mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg)
1322 {
1323   if (last_async_command)
1324     fputs_unfiltered (last_async_command, raw_stdout);
1325   fputs_unfiltered ("*stopped", raw_stdout);
1326   mi_out_put (uiout, raw_stdout);
1327   fputs_unfiltered ("\n", raw_stdout);
1328   fputs_unfiltered ("(gdb) \n", raw_stdout);
1329   gdb_flush (raw_stdout);
1330   do_exec_cleanups (ALL_CLEANUPS);
1331 }
1332
1333 static char *
1334 mi_input (char *buf)
1335 {
1336   return gdb_readline (NULL);
1337 }
1338
1339 static void
1340 mi_load_progress (const char *section_name,
1341                   unsigned long sent_so_far,
1342                   unsigned long total_section,
1343                   unsigned long total_sent,
1344                   unsigned long grand_total)
1345 {
1346   struct timeval time_now, delta, update_threshold;
1347   static struct timeval last_update;
1348   static char *previous_sect_name = NULL;
1349   int new_section;
1350
1351   if (!interpreter_p || strncmp (interpreter_p, "mi", 2) != 0)
1352     return;
1353
1354   update_threshold.tv_sec = 0;
1355   update_threshold.tv_usec = 500000;
1356   gettimeofday (&time_now, NULL);
1357
1358   delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
1359   delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
1360
1361   if (delta.tv_usec < 0)
1362     {
1363       delta.tv_sec -= 1;
1364       delta.tv_usec += 1000000;
1365     }
1366
1367   new_section = (previous_sect_name ?
1368                  strcmp (previous_sect_name, section_name) : 1);
1369   if (new_section)
1370     {
1371       xfree (previous_sect_name);
1372       previous_sect_name = xstrdup (section_name);
1373
1374       if (last_async_command)
1375         fputs_unfiltered (last_async_command, raw_stdout);
1376       fputs_unfiltered ("+download", raw_stdout);
1377       ui_out_tuple_begin (uiout, NULL);
1378       ui_out_field_string (uiout, "section", section_name);
1379       ui_out_field_int (uiout, "section-size", total_section);
1380       ui_out_field_int (uiout, "total-size", grand_total);
1381       ui_out_tuple_end (uiout);
1382       mi_out_put (uiout, raw_stdout);
1383       fputs_unfiltered ("\n", raw_stdout);
1384       gdb_flush (raw_stdout);
1385     }
1386
1387   if (delta.tv_sec >= update_threshold.tv_sec &&
1388       delta.tv_usec >= update_threshold.tv_usec)
1389     {
1390       last_update.tv_sec = time_now.tv_sec;
1391       last_update.tv_usec = time_now.tv_usec;
1392       if (last_async_command)
1393         fputs_unfiltered (last_async_command, raw_stdout);
1394       fputs_unfiltered ("+download", raw_stdout);
1395       ui_out_tuple_begin (uiout, NULL);
1396       ui_out_field_string (uiout, "section", section_name);
1397       ui_out_field_int (uiout, "section-sent", sent_so_far);
1398       ui_out_field_int (uiout, "section-size", total_section);
1399       ui_out_field_int (uiout, "total-sent", total_sent);
1400       ui_out_field_int (uiout, "total-size", grand_total);
1401       ui_out_tuple_end (uiout);
1402       mi_out_put (uiout, raw_stdout);
1403       fputs_unfiltered ("\n", raw_stdout);
1404       gdb_flush (raw_stdout);
1405     }
1406 }
1407
1408 static void
1409 mi_command_loop (int mi_version)
1410 {
1411   /* HACK: Force stdout/stderr to point at the console.  This avoids
1412      any potential side effects caused by legacy code that is still
1413      using the TUI / fputs_unfiltered_hook */
1414   raw_stdout = stdio_fileopen (stdout);
1415   /* Route normal output through the MIx */
1416   gdb_stdout = mi_console_file_new (raw_stdout, "~");
1417   /* Route error and log output through the MI */
1418   gdb_stderr = mi_console_file_new (raw_stdout, "&");
1419   gdb_stdlog = gdb_stderr;
1420   /* Route target output through the MI. */
1421   gdb_stdtarg = mi_console_file_new (raw_stdout, "@");
1422
1423   /* HACK: Poke the ui_out table directly.  Should we be creating a
1424      mi_out object wired up to the above gdb_stdout / gdb_stderr? */
1425   uiout = mi_out_new (mi_version);
1426
1427   /* HACK: Override any other interpreter hooks.  We need to create a
1428      real event table and pass in that. */
1429   init_ui_hook = 0;
1430   /* command_loop_hook = 0; */
1431   print_frame_info_listing_hook = 0;
1432   query_hook = 0;
1433   warning_hook = 0;
1434   create_breakpoint_hook = 0;
1435   delete_breakpoint_hook = 0;
1436   modify_breakpoint_hook = 0;
1437   interactive_hook = 0;
1438   registers_changed_hook = 0;
1439   readline_begin_hook = 0;
1440   readline_hook = 0;
1441   readline_end_hook = 0;
1442   register_changed_hook = 0;
1443   memory_changed_hook = 0;
1444   context_hook = 0;
1445   target_wait_hook = 0;
1446   call_command_hook = 0;
1447   error_hook = 0;
1448   error_begin_hook = 0;
1449   show_load_progress = mi_load_progress;
1450
1451   /* Turn off 8 bit strings in quoted output.  Any character with the
1452      high bit set is printed using C's octal format. */
1453   sevenbit_strings = 1;
1454
1455   /* Tell the world that we're alive */
1456   fputs_unfiltered ("(gdb) \n", raw_stdout);
1457   gdb_flush (raw_stdout);
1458
1459   if (!event_loop_p)
1460     simplified_command_loop (mi_input, mi_execute_command);
1461   else
1462     start_event_loop ();
1463 }
1464
1465 static void
1466 mi0_command_loop (void)
1467 {
1468   mi_command_loop (0);
1469 }
1470
1471 static void
1472 mi1_command_loop (void)
1473 {
1474   mi_command_loop (1);
1475 }
1476
1477 static void
1478 setup_architecture_data (void)
1479 {
1480   /* don't trust REGISTER_BYTES to be zero. */
1481   old_regs = xmalloc (REGISTER_BYTES + 1);
1482   memset (old_regs, 0, REGISTER_BYTES + 1);
1483 }
1484
1485 static void
1486 mi_init_ui (char *arg0)
1487 {
1488   /* Eventually this will contain code that takes control of the
1489      console. */
1490 }
1491
1492 void
1493 _initialize_mi_main (void)
1494 {
1495   if (interpreter_p == NULL)
1496     return;
1497
1498   /* If we're _the_ interpreter, take control. */
1499   if (strcmp (interpreter_p, "mi0") == 0)
1500     command_loop_hook = mi0_command_loop;
1501   else if (strcmp (interpreter_p, "mi") == 0
1502            || strcmp (interpreter_p, "mi1") == 0)
1503     command_loop_hook = mi1_command_loop;
1504   else
1505     return;
1506
1507   init_ui_hook = mi_init_ui;
1508   setup_architecture_data ();
1509   register_gdbarch_swap (&old_regs, sizeof (old_regs), NULL);
1510   register_gdbarch_swap (NULL, 0, setup_architecture_data);
1511   if (event_loop_p)
1512     {
1513       /* These overwrite some of the initialization done in
1514          _intialize_event_loop. */
1515       call_readline = gdb_readline2;
1516       input_handler = mi_execute_command_wrapper;
1517       add_file_handler (input_fd, stdin_event_handler, 0);
1518       async_command_editing_p = 0;
1519     }
1520   /* FIXME: Should we notify main that we are here as a possible
1521      interpreter? */
1522 }
This page took 0.125448 seconds and 4 git commands to generate.