2 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
5 This file is part of GDB.
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.
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.
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. */
22 /* Work in progress */
27 #include "gdb_string.h"
29 #include "gdbthread.h"
32 #include "mi-getopt.h"
33 #include "mi-console.h"
36 #include "event-loop.h"
37 #include "event-top.h"
38 #include "gdbcore.h" /* for write_memory() */
39 #include "value.h" /* for deprecated_write_register_bytes() */
52 /* Enumerations of the actions that may result from calling
53 captured_mi_execute_command */
55 enum captured_mi_execute_command_actions
57 EXECUTE_COMMAND_DISPLAY_PROMPT,
58 EXECUTE_COMMAND_SUPRESS_PROMPT,
59 EXECUTE_COMMAND_DISPLAY_ERROR
62 /* This structure is used to pass information from captured_mi_execute_command
63 to mi_execute_command. */
64 struct captured_mi_execute_command_args
66 /* This return result of the MI command (output) */
67 enum mi_cmd_result rc;
69 /* What action to perform when the call is finished (output) */
70 enum captured_mi_execute_command_actions action;
72 /* The command context to be executed (input) */
73 struct mi_parse *command;
77 struct ui_file *raw_stdout;
79 /* The token of the last asynchronous command */
80 static char *last_async_command;
81 static char *previous_async_command;
82 static char *mi_error_message;
83 static char *old_regs;
85 extern void _initialize_mi_main (void);
86 static char *mi_input (char *);
87 static void mi_execute_command (char *cmd, int from_tty);
88 static enum mi_cmd_result mi_cmd_execute (struct mi_parse *parse);
90 static void mi_execute_cli_command (const char *cli, char *args);
91 static enum mi_cmd_result mi_execute_async_cli_command (char *mi, char *args, int from_tty);
92 static void mi_execute_command_wrapper (char *cmd);
94 void mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg);
96 static int register_changed_p (int regnum);
97 static int get_register (int regnum, int format);
98 static void mi_load_progress (const char *section_name,
99 unsigned long sent_so_far,
100 unsigned long total_section,
101 unsigned long total_sent,
102 unsigned long grand_total);
104 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
105 layer that calls libgdb. Any operation used in the below should be
109 mi_cmd_gdb_exit (char *command, char **argv, int argc)
111 /* We have to print everything right here because we never return */
112 if (last_async_command)
113 fputs_unfiltered (last_async_command, raw_stdout);
114 fputs_unfiltered ("^exit\n", raw_stdout);
115 mi_out_put (uiout, raw_stdout);
116 /* FIXME: The function called is not yet a formal libgdb function */
117 quit_force (NULL, FROM_TTY);
122 mi_cmd_exec_run (char *args, int from_tty)
124 /* FIXME: Should call a libgdb function, not a cli wrapper */
125 return mi_execute_async_cli_command ("run", args, from_tty);
129 mi_cmd_exec_next (char *args, int from_tty)
131 /* FIXME: Should call a libgdb function, not a cli wrapper */
132 return mi_execute_async_cli_command ("next", args, from_tty);
136 mi_cmd_exec_next_instruction (char *args, int from_tty)
138 /* FIXME: Should call a libgdb function, not a cli wrapper */
139 return mi_execute_async_cli_command ("nexti", args, from_tty);
143 mi_cmd_exec_step (char *args, int from_tty)
145 /* FIXME: Should call a libgdb function, not a cli wrapper */
146 return mi_execute_async_cli_command ("step", args, from_tty);
150 mi_cmd_exec_step_instruction (char *args, int from_tty)
152 /* FIXME: Should call a libgdb function, not a cli wrapper */
153 return mi_execute_async_cli_command ("stepi", args, from_tty);
157 mi_cmd_exec_finish (char *args, int from_tty)
159 /* FIXME: Should call a libgdb function, not a cli wrapper */
160 return mi_execute_async_cli_command ("finish", args, from_tty);
164 mi_cmd_exec_until (char *args, int from_tty)
166 /* FIXME: Should call a libgdb function, not a cli wrapper */
167 return mi_execute_async_cli_command ("until", args, from_tty);
171 mi_cmd_exec_return (char *args, int from_tty)
173 /* This command doesn't really execute the target, it just pops the
174 specified number of frames. */
176 /* Call return_command with from_tty argument equal to 0 so as to
177 avoid being queried. */
178 return_command (args, 0);
180 /* Call return_command with from_tty argument equal to 0 so as to
181 avoid being queried. */
182 return_command (NULL, 0);
184 /* Because we have called return_command with from_tty = 0, we need
185 to print the frame here. */
186 show_and_print_stack_frame (deprecated_selected_frame,
187 frame_relative_level (deprecated_selected_frame),
194 mi_cmd_exec_continue (char *args, int from_tty)
196 /* FIXME: Should call a libgdb function, not a cli wrapper */
197 return mi_execute_async_cli_command ("continue", args, from_tty);
200 /* Interrupt the execution of the target. Note how we must play around
201 with the token varialbes, in order to display the current token in
202 the result of the interrupt command, and the previous execution
203 token when the target finally stops. See comments in
206 mi_cmd_exec_interrupt (char *args, int from_tty)
208 if (!target_executing)
210 xasprintf (&mi_error_message,
211 "mi_cmd_exec_interrupt: Inferior not executing.");
214 interrupt_target_command (args, from_tty);
215 if (last_async_command)
216 fputs_unfiltered (last_async_command, raw_stdout);
217 fputs_unfiltered ("^done", raw_stdout);
218 xfree (last_async_command);
219 if (previous_async_command)
220 last_async_command = xstrdup (previous_async_command);
221 xfree (previous_async_command);
222 previous_async_command = NULL;
223 mi_out_put (uiout, raw_stdout);
224 mi_out_rewind (uiout);
225 fputs_unfiltered ("\n", raw_stdout);
230 mi_cmd_thread_select (char *command, char **argv, int argc)
236 xasprintf (&mi_error_message,
237 "mi_cmd_thread_select: USAGE: threadnum.");
241 rc = gdb_thread_select (uiout, argv[0]);
243 /* RC is enum gdb_rc if it is successful (>=0)
244 enum return_reason if not (<0). */
245 if ((int) rc < 0 && (enum return_reason) rc == RETURN_ERROR)
246 return MI_CMD_CAUGHT_ERROR;
247 else if ((int) rc >= 0 && rc == GDB_RC_FAIL)
254 mi_cmd_thread_list_ids (char *command, char **argv, int argc)
256 enum gdb_rc rc = MI_CMD_DONE;
260 xasprintf (&mi_error_message,
261 "mi_cmd_thread_list_ids: No arguments required.");
265 rc = gdb_list_thread_ids (uiout);
267 if (rc == GDB_RC_FAIL)
268 return MI_CMD_CAUGHT_ERROR;
274 mi_cmd_data_list_register_names (char *command, char **argv, int argc)
278 struct cleanup *cleanup;
280 /* Note that the test for a valid register must include checking the
281 REGISTER_NAME because NUM_REGS may be allocated for the union of
282 the register sets within a family of related processors. In this
283 case, some entries of REGISTER_NAME will change depending upon
284 the particular processor being debugged. */
286 numregs = NUM_REGS + NUM_PSEUDO_REGS;
288 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names");
290 if (argc == 0) /* No args, just do all the regs */
296 if (REGISTER_NAME (regnum) == NULL
297 || *(REGISTER_NAME (regnum)) == '\0')
298 ui_out_field_string (uiout, NULL, "");
300 ui_out_field_string (uiout, NULL, REGISTER_NAME (regnum));
304 /* Else, list of register #s, just do listed regs */
305 for (i = 0; i < argc; i++)
307 regnum = atoi (argv[i]);
308 if (regnum < 0 || regnum >= numregs)
310 do_cleanups (cleanup);
311 xasprintf (&mi_error_message, "bad register number");
314 if (REGISTER_NAME (regnum) == NULL
315 || *(REGISTER_NAME (regnum)) == '\0')
316 ui_out_field_string (uiout, NULL, "");
318 ui_out_field_string (uiout, NULL, REGISTER_NAME (regnum));
320 do_cleanups (cleanup);
325 mi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
327 int regnum, numregs, changed;
329 struct cleanup *cleanup;
331 /* Note that the test for a valid register must include checking the
332 REGISTER_NAME because NUM_REGS may be allocated for the union of
333 the register sets within a family of related processors. In this
334 case, some entries of REGISTER_NAME will change depending upon
335 the particular processor being debugged. */
339 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changed-registers");
341 if (argc == 0) /* No args, just do all the regs */
347 if (REGISTER_NAME (regnum) == NULL
348 || *(REGISTER_NAME (regnum)) == '\0')
350 changed = register_changed_p (regnum);
353 do_cleanups (cleanup);
354 xasprintf (&mi_error_message,
355 "mi_cmd_data_list_changed_registers: Unable to read register contents.");
359 ui_out_field_int (uiout, NULL, regnum);
363 /* Else, list of register #s, just do listed regs */
364 for (i = 0; i < argc; i++)
366 regnum = atoi (argv[i]);
370 && REGISTER_NAME (regnum) != NULL
371 && *REGISTER_NAME (regnum) != '\000')
373 changed = register_changed_p (regnum);
376 do_cleanups (cleanup);
377 xasprintf (&mi_error_message,
378 "mi_cmd_data_list_register_change: Unable to read register contents.");
382 ui_out_field_int (uiout, NULL, regnum);
386 do_cleanups (cleanup);
387 xasprintf (&mi_error_message, "bad register number");
391 do_cleanups (cleanup);
396 register_changed_p (int regnum)
398 char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
400 if (! frame_register_read (deprecated_selected_frame, regnum, raw_buffer))
403 if (memcmp (&old_regs[REGISTER_BYTE (regnum)], raw_buffer,
404 REGISTER_RAW_SIZE (regnum)) == 0)
407 /* Found a changed register. Return 1. */
409 memcpy (&old_regs[REGISTER_BYTE (regnum)], raw_buffer,
410 REGISTER_RAW_SIZE (regnum));
415 /* Return a list of register number and value pairs. The valid
416 arguments expected are: a letter indicating the format in which to
417 display the registers contents. This can be one of: x (hexadecimal), d
418 (decimal), N (natural), t (binary), o (octal), r (raw). After the
419 format argumetn there can be a sequence of numbers, indicating which
420 registers to fetch the content of. If the format is the only argument,
421 a list of all the registers with their values is returned. */
423 mi_cmd_data_list_register_values (char *command, char **argv, int argc)
425 int regnum, numregs, format, result;
427 struct cleanup *list_cleanup, *tuple_cleanup;
429 /* Note that the test for a valid register must include checking the
430 REGISTER_NAME because NUM_REGS may be allocated for the union of
431 the register sets within a family of related processors. In this
432 case, some entries of REGISTER_NAME will change depending upon
433 the particular processor being debugged. */
439 xasprintf (&mi_error_message,
440 "mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
444 format = (int) argv[0][0];
446 if (!target_has_registers)
448 xasprintf (&mi_error_message,
449 "mi_cmd_data_list_register_values: No registers.");
453 list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values");
455 if (argc == 1) /* No args, beside the format: do all the regs */
461 if (REGISTER_NAME (regnum) == NULL
462 || *(REGISTER_NAME (regnum)) == '\0')
464 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
465 ui_out_field_int (uiout, "number", regnum);
466 result = get_register (regnum, format);
469 do_cleanups (list_cleanup);
472 do_cleanups (tuple_cleanup);
476 /* Else, list of register #s, just do listed regs */
477 for (i = 1; i < argc; i++)
479 regnum = atoi (argv[i]);
483 && REGISTER_NAME (regnum) != NULL
484 && *REGISTER_NAME (regnum) != '\000')
486 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
487 ui_out_field_int (uiout, "number", regnum);
488 result = get_register (regnum, format);
491 do_cleanups (list_cleanup);
494 do_cleanups (tuple_cleanup);
498 do_cleanups (list_cleanup);
499 xasprintf (&mi_error_message, "bad register number");
503 do_cleanups (list_cleanup);
507 /* Output one register's contents in the desired format. */
509 get_register (int regnum, int format)
511 char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
512 char *virtual_buffer = alloca (MAX_REGISTER_VIRTUAL_SIZE);
514 static struct ui_stream *stb = NULL;
516 stb = ui_out_stream_new (uiout);
521 get_saved_register (raw_buffer, &optim, (CORE_ADDR *) NULL,
522 deprecated_selected_frame,
523 regnum, (enum lval_type *) NULL);
526 xasprintf (&mi_error_message, "Optimized out");
530 /* Convert raw data to virtual format if necessary. */
532 if (REGISTER_CONVERTIBLE (regnum))
534 REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
535 raw_buffer, virtual_buffer);
538 memcpy (virtual_buffer, raw_buffer, REGISTER_VIRTUAL_SIZE (regnum));
543 char *ptr, buf[1024];
547 for (j = 0; j < REGISTER_RAW_SIZE (regnum); j++)
549 register int idx = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? j
550 : REGISTER_RAW_SIZE (regnum) - 1 - j;
551 sprintf (ptr, "%02x", (unsigned char) raw_buffer[idx]);
554 ui_out_field_string (uiout, "value", buf);
555 /*fputs_filtered (buf, gdb_stdout); */
559 val_print (REGISTER_VIRTUAL_TYPE (regnum), virtual_buffer, 0, 0,
560 stb->stream, format, 1, 0, Val_pretty_default);
561 ui_out_field_stream (uiout, "value", stb);
562 ui_out_stream_delete (stb);
567 /* Write given values into registers. The registers and values are
568 given as pairs. The corresponding MI command is
569 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
571 mi_cmd_data_write_register_values (char *command, char **argv, int argc)
579 /* Note that the test for a valid register must include checking the
580 REGISTER_NAME because NUM_REGS may be allocated for the union of
581 the register sets within a family of related processors. In this
582 case, some entries of REGISTER_NAME will change depending upon
583 the particular processor being debugged. */
589 xasprintf (&mi_error_message,
590 "mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
594 format = (int) argv[0][0];
596 if (!target_has_registers)
598 xasprintf (&mi_error_message,
599 "mi_cmd_data_write_register_values: No registers.");
605 xasprintf (&mi_error_message,
606 "mi_cmd_data_write_register_values: No regs and values specified.");
612 xasprintf (&mi_error_message,
613 "mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
617 for (i = 1; i < argc; i = i + 2)
619 regnum = atoi (argv[i]);
623 && REGISTER_NAME (regnum) != NULL
624 && *REGISTER_NAME (regnum) != '\000')
627 struct cleanup *old_chain;
629 /* Get the value as a number */
630 value = parse_and_eval_address (argv[i + 1]);
631 /* Get the value into an array */
632 buffer = xmalloc (REGISTER_SIZE);
633 old_chain = make_cleanup (xfree, buffer);
634 store_signed_integer (buffer, REGISTER_SIZE, value);
636 deprecated_write_register_bytes (REGISTER_BYTE (regnum), buffer, REGISTER_RAW_SIZE (regnum));
637 /* Free the buffer. */
638 do_cleanups (old_chain);
642 xasprintf (&mi_error_message, "bad register number");
650 /*This is commented out because we decided it was not useful. I leave
651 it, just in case. ezannoni:1999-12-08 */
653 /* Assign a value to a variable. The expression argument must be in
654 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
657 mi_cmd_data_assign (char *command, char **argv, int argc)
659 struct expression *expr;
660 struct cleanup *old_chain;
664 xasprintf (&mi_error_message,
665 "mi_cmd_data_assign: Usage: -data-assign expression");
669 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
670 01-12-1999: Need to decide what to do with this for libgdb purposes. */
672 expr = parse_expression (argv[0]);
673 old_chain = make_cleanup (free_current_contents, &expr);
674 evaluate_expression (expr);
675 do_cleanups (old_chain);
680 /* Evaluate the value of the argument. The argument is an
681 expression. If the expression contains spaces it needs to be
682 included in double quotes. */
684 mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
686 struct expression *expr;
687 struct cleanup *old_chain = NULL;
689 struct ui_stream *stb = NULL;
691 stb = ui_out_stream_new (uiout);
695 xasprintf (&mi_error_message,
696 "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
700 expr = parse_expression (argv[0]);
702 old_chain = make_cleanup (free_current_contents, &expr);
704 val = evaluate_expression (expr);
706 /* Print the result of the expression evaluation. */
707 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val),
708 VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
709 stb->stream, 0, 0, 0, 0);
711 ui_out_field_stream (uiout, "value", stb);
712 ui_out_stream_delete (stb);
714 do_cleanups (old_chain);
720 mi_cmd_target_download (char *args, int from_tty)
723 struct cleanup *old_cleanups = NULL;
725 xasprintf (&run, "load %s", args);
726 old_cleanups = make_cleanup (xfree, run);
727 execute_command (run, from_tty);
729 do_cleanups (old_cleanups);
733 /* Connect to the remote target. */
735 mi_cmd_target_select (char *args, int from_tty)
738 struct cleanup *old_cleanups = NULL;
740 xasprintf (&run, "target %s", args);
741 old_cleanups = make_cleanup (xfree, run);
743 /* target-select is always synchronous. once the call has returned
744 we know that we are connected. */
745 /* NOTE: At present all targets that are connected are also
746 (implicitly) talking to a halted target. In the future this may
748 execute_command (run, from_tty);
750 do_cleanups (old_cleanups);
752 /* Issue the completion message here. */
753 if (last_async_command)
754 fputs_unfiltered (last_async_command, raw_stdout);
755 fputs_unfiltered ("^connected", raw_stdout);
756 mi_out_put (uiout, raw_stdout);
757 mi_out_rewind (uiout);
758 fputs_unfiltered ("\n", raw_stdout);
759 do_exec_cleanups (ALL_CLEANUPS);
765 ADDR: start address of data to be dumped.
766 WORD-FORMAT: a char indicating format for the ``word''. See
768 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
769 NR_ROW: Number of rows.
770 NR_COL: The number of colums (words per row).
771 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
772 ASCHAR for unprintable characters.
774 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
775 displayes them. Returns:
777 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
780 The number of bytes read is SIZE*ROW*COL. */
783 mi_cmd_data_read_memory (char *command, char **argv, int argc)
785 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
791 struct type *word_type;
804 static struct mi_opt opts[] =
806 {"o", OFFSET_OPT, 1},
812 int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts,
816 switch ((enum opt) opt)
819 offset = atol (optarg);
826 if (argc < 5 || argc > 6)
828 xasprintf (&mi_error_message,
829 "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
833 /* Extract all the arguments. */
835 /* Start address of the memory dump. */
836 addr = parse_and_eval_address (argv[0]) + offset;
837 /* The format character to use when displaying a memory word. See
838 the ``x'' command. */
839 word_format = argv[1][0];
840 /* The size of the memory word. */
841 word_size = atol (argv[2]);
845 word_type = builtin_type_int8;
849 word_type = builtin_type_int16;
853 word_type = builtin_type_int32;
857 word_type = builtin_type_int64;
861 word_type = builtin_type_int8;
864 /* The number of rows */
865 nr_rows = atol (argv[3]);
868 xasprintf (&mi_error_message,
869 "mi_cmd_data_read_memory: invalid number of rows.");
872 /* number of bytes per row. */
873 nr_cols = atol (argv[4]);
876 xasprintf (&mi_error_message,
877 "mi_cmd_data_read_memory: invalid number of columns.");
879 /* The un-printable character when printing ascii. */
885 /* create a buffer and read it in. */
886 total_bytes = word_size * nr_rows * nr_cols;
887 mbuf = xcalloc (total_bytes, 1);
888 make_cleanup (xfree, mbuf);
891 xasprintf (&mi_error_message,
892 "mi_cmd_data_read_memory: out of memory.");
896 while (nr_bytes < total_bytes)
899 long num = target_read_memory_partial (addr + nr_bytes, mbuf + nr_bytes,
900 total_bytes - nr_bytes,
907 /* output the header information. */
908 ui_out_field_core_addr (uiout, "addr", addr);
909 ui_out_field_int (uiout, "nr-bytes", nr_bytes);
910 ui_out_field_int (uiout, "total-bytes", total_bytes);
911 ui_out_field_core_addr (uiout, "next-row", addr + word_size * nr_cols);
912 ui_out_field_core_addr (uiout, "prev-row", addr - word_size * nr_cols);
913 ui_out_field_core_addr (uiout, "next-page", addr + total_bytes);
914 ui_out_field_core_addr (uiout, "prev-page", addr - total_bytes);
916 /* Build the result as a two dimentional table. */
918 struct ui_stream *stream = ui_out_stream_new (uiout);
919 struct cleanup *cleanup_list_memory;
922 cleanup_list_memory = make_cleanup_ui_out_list_begin_end (uiout, "memory");
923 for (row = 0, row_byte = 0;
925 row++, row_byte += nr_cols * word_size)
929 struct cleanup *cleanup_tuple;
930 struct cleanup *cleanup_list_data;
931 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
932 ui_out_field_core_addr (uiout, "addr", addr + row_byte);
933 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
934 cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
935 for (col = 0, col_byte = row_byte;
937 col++, col_byte += word_size)
939 if (col_byte + word_size > nr_bytes)
941 ui_out_field_string (uiout, NULL, "N/A");
945 ui_file_rewind (stream->stream);
946 print_scalar_formatted (mbuf + col_byte, word_type, word_format,
947 word_asize, stream->stream);
948 ui_out_field_stream (uiout, NULL, stream);
951 do_cleanups (cleanup_list_data);
955 ui_file_rewind (stream->stream);
956 for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
958 if (byte >= nr_bytes)
960 fputc_unfiltered ('X', stream->stream);
962 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
964 fputc_unfiltered (aschar, stream->stream);
967 fputc_unfiltered (mbuf[byte], stream->stream);
969 ui_out_field_stream (uiout, "ascii", stream);
971 do_cleanups (cleanup_tuple);
973 ui_out_stream_delete (stream);
974 do_cleanups (cleanup_list_memory);
976 do_cleanups (cleanups);
980 /* DATA-MEMORY-WRITE:
982 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
983 offset from the beginning of the memory grid row where the cell to
985 ADDR: start address of the row in the memory grid where the memory
986 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
987 the location to write to.
988 FORMAT: a char indicating format for the ``word''. See
990 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
991 VALUE: value to be written into the memory address.
993 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
997 mi_cmd_data_write_memory (char *command, char **argv, int argc)
1002 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1003 enough when using a compiler other than GCC. */
1006 struct cleanup *old_chain;
1014 static struct mi_opt opts[] =
1016 {"o", OFFSET_OPT, 1},
1022 int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
1026 switch ((enum opt) opt)
1029 offset = atol (optarg);
1038 xasprintf (&mi_error_message,
1039 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1040 return MI_CMD_ERROR;
1043 /* Extract all the arguments. */
1044 /* Start address of the memory dump. */
1045 addr = parse_and_eval_address (argv[0]);
1046 /* The format character to use when displaying a memory word. See
1047 the ``x'' command. */
1048 word_format = argv[1][0];
1049 /* The size of the memory word. */
1050 word_size = atol (argv[2]);
1052 /* Calculate the real address of the write destination. */
1053 addr += (offset * word_size);
1055 /* Get the value as a number */
1056 value = parse_and_eval_address (argv[3]);
1057 /* Get the value into an array */
1058 buffer = xmalloc (word_size);
1059 old_chain = make_cleanup (xfree, buffer);
1060 store_signed_integer (buffer, word_size, value);
1061 /* Write it down to memory */
1062 write_memory (addr, buffer, word_size);
1063 /* Free the buffer. */
1064 do_cleanups (old_chain);
1069 /* Execute a command within a safe environment.
1070 Return <0 for error; >=0 for ok.
1072 args->action will tell mi_execute_command what action
1073 to perfrom after the given command has executed (display/supress
1074 prompt, display error). */
1077 captured_mi_execute_command (struct ui_out *uiout, void *data)
1079 struct captured_mi_execute_command_args *args =
1080 (struct captured_mi_execute_command_args *) data;
1081 struct mi_parse *context = args->command;
1083 switch (context->op)
1087 /* A MI command was read from the input stream */
1089 /* FIXME: gdb_???? */
1090 fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
1091 context->token, context->command, context->args);
1092 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1093 condition expression, each function should return an
1094 indication of what action is required and then switch on
1096 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1097 args->rc = mi_cmd_execute (context);
1099 if (!target_can_async_p () || !target_executing)
1101 /* print the result if there were no errors */
1102 if (args->rc == MI_CMD_DONE)
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);
1110 else if (args->rc == MI_CMD_ERROR)
1112 if (mi_error_message)
1114 fputs_unfiltered (context->token, raw_stdout);
1115 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1116 fputstr_unfiltered (mi_error_message, '"', raw_stdout);
1117 xfree (mi_error_message);
1118 fputs_unfiltered ("\"\n", raw_stdout);
1120 mi_out_rewind (uiout);
1122 else if (args->rc == MI_CMD_CAUGHT_ERROR)
1124 mi_out_rewind (uiout);
1125 args->action = EXECUTE_COMMAND_DISPLAY_ERROR;
1129 mi_out_rewind (uiout);
1131 else if (sync_execution)
1133 /* Don't print the prompt. We are executing the target in
1134 synchronous mode. */
1135 args->action = EXECUTE_COMMAND_SUPRESS_PROMPT;
1141 /* A CLI command was read from the input stream */
1142 /* This will be removed as soon as we have a complete set of
1144 /* echo the command on the console. */
1145 fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1146 /* FIXME: If the command string has something that looks like
1147 a format spec (e.g. %s) we will get a core dump */
1148 mi_execute_cli_command ("%s", context->command);
1149 /* print the result */
1150 /* FIXME: Check for errors here. */
1151 fputs_unfiltered (context->token, raw_stdout);
1152 fputs_unfiltered ("^done", raw_stdout);
1153 mi_out_put (uiout, raw_stdout);
1154 mi_out_rewind (uiout);
1155 fputs_unfiltered ("\n", raw_stdout);
1156 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1157 args->rc = MI_CMD_DONE;
1167 mi_execute_command (char *cmd, int from_tty)
1169 struct mi_parse *command;
1170 struct captured_mi_execute_command_args args;
1171 struct ui_out *saved_uiout = uiout;
1174 /* This is to handle EOF (^D). We just quit gdb. */
1175 /* FIXME: we should call some API function here. */
1177 quit_force (NULL, from_tty);
1179 command = mi_parse (cmd);
1181 if (command != NULL)
1183 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
1184 be pushed even further down or even eliminated? */
1185 args.command = command;
1186 result = catch_exceptions (uiout, captured_mi_execute_command, &args, "",
1189 if (args.action == EXECUTE_COMMAND_SUPRESS_PROMPT)
1191 /* The command is executing synchronously. Bail out early
1192 suppressing the finished prompt. */
1193 mi_parse_free (command);
1196 if (args.action == EXECUTE_COMMAND_DISPLAY_ERROR || result < 0)
1198 char *msg = error_last_message ();
1199 struct cleanup *cleanup = make_cleanup (xfree, msg);
1200 /* The command execution failed and error() was called
1202 fputs_unfiltered (command->token, raw_stdout);
1203 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1204 fputstr_unfiltered (msg, '"', raw_stdout);
1205 fputs_unfiltered ("\"\n", raw_stdout);
1207 mi_parse_free (command);
1210 fputs_unfiltered ("(gdb) \n", raw_stdout);
1211 gdb_flush (raw_stdout);
1212 /* print any buffered hook code */
1216 static enum mi_cmd_result
1217 mi_cmd_execute (struct mi_parse *parse)
1219 if (parse->cmd->argv_func != NULL
1220 || parse->cmd->args_func != NULL)
1222 /* FIXME: We need to save the token because the command executed
1223 may be asynchronous and need to print the token again.
1224 In the future we can pass the token down to the func
1225 and get rid of the last_async_command */
1226 /* The problem here is to keep the token around when we launch
1227 the target, and we want to interrupt it later on. The
1228 interrupt command will have its own token, but when the
1229 target stops, we must display the token corresponding to the
1230 last execution command given. So we have another string where
1231 we copy the token (previous_async_command), if this was
1232 indeed the token of an execution command, and when we stop we
1233 print that one. This is possible because the interrupt
1234 command, when over, will copy that token back into the
1235 default token string (last_async_command). */
1237 if (target_executing)
1239 if (!previous_async_command)
1240 previous_async_command = xstrdup (last_async_command);
1241 if (strcmp (parse->command, "exec-interrupt"))
1243 fputs_unfiltered (parse->token, raw_stdout);
1244 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1245 fputs_unfiltered ("Cannot execute command ", raw_stdout);
1246 fputstr_unfiltered (parse->command, '"', raw_stdout);
1247 fputs_unfiltered (" while target running", raw_stdout);
1248 fputs_unfiltered ("\"\n", raw_stdout);
1249 return MI_CMD_ERROR;
1252 last_async_command = xstrdup (parse->token);
1253 make_exec_cleanup (free_current_contents, &last_async_command);
1254 /* FIXME: DELETE THIS! */
1255 if (parse->cmd->args_func != NULL)
1256 return parse->cmd->args_func (parse->args, 0 /*from_tty */ );
1257 return parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
1259 else if (parse->cmd->cli != 0)
1261 /* FIXME: DELETE THIS. */
1262 /* The operation is still implemented by a cli command */
1263 /* Must be a synchronous one */
1264 mi_execute_cli_command (parse->cmd->cli, parse->args);
1269 /* FIXME: DELETE THIS. */
1270 fputs_unfiltered (parse->token, raw_stdout);
1271 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1272 fputs_unfiltered ("Undefined mi command: ", raw_stdout);
1273 fputstr_unfiltered (parse->command, '"', raw_stdout);
1274 fputs_unfiltered (" (missing implementation)", raw_stdout);
1275 fputs_unfiltered ("\"\n", raw_stdout);
1276 return MI_CMD_ERROR;
1281 mi_execute_command_wrapper (char *cmd)
1283 mi_execute_command (cmd, stdin == instream);
1286 /* FIXME: This is just a hack so we can get some extra commands going.
1287 We don't want to channel things through the CLI, but call libgdb directly */
1288 /* Use only for synchronous commands */
1291 mi_execute_cli_command (const char *cli, char *args)
1295 struct cleanup *old_cleanups;
1297 xasprintf (&run, cli, args);
1299 /* FIXME: gdb_???? */
1300 fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
1302 old_cleanups = make_cleanup (xfree, run);
1303 execute_command ( /*ui */ run, 0 /*from_tty */ );
1304 do_cleanups (old_cleanups);
1310 mi_execute_async_cli_command (char *mi, char *args, int from_tty)
1312 struct cleanup *old_cleanups;
1316 if (target_can_async_p ())
1318 async_args = (char *) xmalloc (strlen (args) + 2);
1319 make_exec_cleanup (free, async_args);
1320 strcpy (async_args, args);
1321 strcat (async_args, "&");
1322 xasprintf (&run, "%s %s", mi, async_args);
1323 make_exec_cleanup (free, run);
1324 add_continuation (mi_exec_async_cli_cmd_continuation, NULL);
1325 old_cleanups = NULL;
1329 xasprintf (&run, "%s %s", mi, args);
1330 old_cleanups = make_cleanup (xfree, run);
1333 if (!target_can_async_p ())
1335 /* NOTE: For synchronous targets asynchronous behavour is faked by
1336 printing out the GDB prompt before we even try to execute the
1338 if (last_async_command)
1339 fputs_unfiltered (last_async_command, raw_stdout);
1340 fputs_unfiltered ("^running\n", raw_stdout);
1341 fputs_unfiltered ("(gdb) \n", raw_stdout);
1342 gdb_flush (raw_stdout);
1346 /* FIXME: cagney/1999-11-29: Printing this message before
1347 calling execute_command is wrong. It should only be printed
1348 once gdb has confirmed that it really has managed to send a
1349 run command to the target. */
1350 if (last_async_command)
1351 fputs_unfiltered (last_async_command, raw_stdout);
1352 fputs_unfiltered ("^running\n", raw_stdout);
1355 execute_command ( /*ui */ run, 0 /*from_tty */ );
1357 if (!target_can_async_p ())
1359 /* Do this before doing any printing. It would appear that some
1360 print code leaves garbage around in the buffer. */
1361 do_cleanups (old_cleanups);
1362 /* If the target was doing the operation synchronously we fake
1363 the stopped message. */
1364 if (last_async_command)
1365 fputs_unfiltered (last_async_command, raw_stdout);
1366 fputs_unfiltered ("*stopped", raw_stdout);
1367 mi_out_put (uiout, raw_stdout);
1368 mi_out_rewind (uiout);
1369 fputs_unfiltered ("\n", raw_stdout);
1370 return MI_CMD_QUIET;
1376 mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg)
1378 if (last_async_command)
1379 fputs_unfiltered (last_async_command, raw_stdout);
1380 fputs_unfiltered ("*stopped", raw_stdout);
1381 mi_out_put (uiout, raw_stdout);
1382 fputs_unfiltered ("\n", raw_stdout);
1383 fputs_unfiltered ("(gdb) \n", raw_stdout);
1384 gdb_flush (raw_stdout);
1385 do_exec_cleanups (ALL_CLEANUPS);
1389 mi_input (char *buf)
1391 return gdb_readline (NULL);
1395 mi_load_progress (const char *section_name,
1396 unsigned long sent_so_far,
1397 unsigned long total_section,
1398 unsigned long total_sent,
1399 unsigned long grand_total)
1401 struct timeval time_now, delta, update_threshold;
1402 static struct timeval last_update;
1403 static char *previous_sect_name = NULL;
1406 if (!interpreter_p || strncmp (interpreter_p, "mi", 2) != 0)
1409 update_threshold.tv_sec = 0;
1410 update_threshold.tv_usec = 500000;
1411 gettimeofday (&time_now, NULL);
1413 delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
1414 delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
1416 if (delta.tv_usec < 0)
1419 delta.tv_usec += 1000000;
1422 new_section = (previous_sect_name ?
1423 strcmp (previous_sect_name, section_name) : 1);
1426 struct cleanup *cleanup_tuple;
1427 xfree (previous_sect_name);
1428 previous_sect_name = xstrdup (section_name);
1430 if (last_async_command)
1431 fputs_unfiltered (last_async_command, raw_stdout);
1432 fputs_unfiltered ("+download", raw_stdout);
1433 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1434 ui_out_field_string (uiout, "section", section_name);
1435 ui_out_field_int (uiout, "section-size", total_section);
1436 ui_out_field_int (uiout, "total-size", grand_total);
1437 do_cleanups (cleanup_tuple);
1438 mi_out_put (uiout, raw_stdout);
1439 fputs_unfiltered ("\n", raw_stdout);
1440 gdb_flush (raw_stdout);
1443 if (delta.tv_sec >= update_threshold.tv_sec &&
1444 delta.tv_usec >= update_threshold.tv_usec)
1446 struct cleanup *cleanup_tuple;
1447 last_update.tv_sec = time_now.tv_sec;
1448 last_update.tv_usec = time_now.tv_usec;
1449 if (last_async_command)
1450 fputs_unfiltered (last_async_command, raw_stdout);
1451 fputs_unfiltered ("+download", raw_stdout);
1452 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1453 ui_out_field_string (uiout, "section", section_name);
1454 ui_out_field_int (uiout, "section-sent", sent_so_far);
1455 ui_out_field_int (uiout, "section-size", total_section);
1456 ui_out_field_int (uiout, "total-sent", total_sent);
1457 ui_out_field_int (uiout, "total-size", grand_total);
1458 do_cleanups (cleanup_tuple);
1459 mi_out_put (uiout, raw_stdout);
1460 fputs_unfiltered ("\n", raw_stdout);
1461 gdb_flush (raw_stdout);
1466 mi_command_loop (int mi_version)
1468 if (mi_version <= 1)
1470 /* HACK: Force stdout/stderr to point at the console. This avoids
1471 any potential side effects caused by legacy code that is still
1472 using the TUI / fputs_unfiltered_hook */
1473 raw_stdout = stdio_fileopen (stdout);
1474 /* Route normal output through the MIx */
1475 gdb_stdout = mi_console_file_new (raw_stdout, "~");
1478 /* Route error and log output through the MI */
1479 gdb_stderr = mi_console_file_new (raw_stdout, "&");
1480 gdb_stdlog = gdb_stderr;
1481 /* Route target output through the MI. */
1482 gdb_stdtarg = mi_console_file_new (raw_stdout, "@");
1484 /* HACK: Poke the ui_out table directly. Should we be creating a
1485 mi_out object wired up to the above gdb_stdout / gdb_stderr? */
1486 uiout = mi_out_new (mi_version);
1488 /* HACK: Override any other interpreter hooks. We need to create a
1489 real event table and pass in that. */
1491 /* command_loop_hook = 0; */
1492 print_frame_info_listing_hook = 0;
1495 create_breakpoint_hook = 0;
1496 delete_breakpoint_hook = 0;
1497 modify_breakpoint_hook = 0;
1498 interactive_hook = 0;
1499 registers_changed_hook = 0;
1500 readline_begin_hook = 0;
1502 readline_end_hook = 0;
1503 register_changed_hook = 0;
1504 memory_changed_hook = 0;
1506 target_wait_hook = 0;
1507 call_command_hook = 0;
1509 error_begin_hook = 0;
1510 show_load_progress = mi_load_progress;
1512 /* Turn off 8 bit strings in quoted output. Any character with the
1513 high bit set is printed using C's octal format. */
1514 sevenbit_strings = 1;
1516 /* Tell the world that we're alive */
1517 fputs_unfiltered ("(gdb) \n", raw_stdout);
1518 gdb_flush (raw_stdout);
1521 simplified_command_loop (mi_input, mi_execute_command);
1523 start_event_loop ();
1527 mi1_command_loop (void)
1529 mi_command_loop (1);
1533 mi2_command_loop (void)
1535 mi_command_loop (2);
1539 setup_architecture_data (void)
1541 /* don't trust REGISTER_BYTES to be zero. */
1542 old_regs = xmalloc (REGISTER_BYTES + 1);
1543 memset (old_regs, 0, REGISTER_BYTES + 1);
1547 mi_init_ui (char *arg0)
1549 if (strlen (interpreter_p) <= 2 ||
1550 interpreter_p[2] > '1')
1552 /* HACK: Force stdout/stderr to point at the console. This avoids
1553 any potential side effects caused by legacy code that is still
1554 using the TUI / fputs_unfiltered_hook */
1555 raw_stdout = stdio_fileopen (stdout);
1556 /* Route normal output through the MIx */
1557 gdb_stdout = mi_console_file_new (raw_stdout, "~");
1562 _initialize_mi_main (void)
1564 if (interpreter_p == NULL)
1567 /* If we're _the_ interpreter, take control. */
1568 if (strcmp (interpreter_p, "mi") == 0)
1569 command_loop_hook = mi2_command_loop;
1570 else if (strcmp (interpreter_p, "mi1") == 0)
1571 command_loop_hook = mi1_command_loop;
1572 else if (strcmp (interpreter_p, "mi2") == 0)
1573 command_loop_hook = mi2_command_loop;
1577 init_ui_hook = mi_init_ui;
1578 setup_architecture_data ();
1579 register_gdbarch_swap (&old_regs, sizeof (old_regs), NULL);
1580 register_gdbarch_swap (NULL, 0, setup_architecture_data);
1583 /* These overwrite some of the initialization done in
1584 _intialize_event_loop. */
1585 call_readline = gdb_readline2;
1586 input_handler = mi_execute_command_wrapper;
1587 add_file_handler (input_fd, stdin_event_handler, 0);
1588 async_command_editing_p = 0;
1590 /* FIXME: Should we notify main that we are here as a possible