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 write_register_bytes() */
50 /* Enumerations of the actions that may result from calling
51 captured_mi_execute_command */
53 enum captured_mi_execute_command_actions
55 EXECUTE_COMMAND_DISPLAY_PROMPT,
56 EXECUTE_COMMAND_SUPRESS_PROMPT,
57 EXECUTE_COMMAND_DISPLAY_ERROR
60 /* This structure is used to pass information from captured_mi_execute_command
61 to mi_execute_command. */
62 struct captured_mi_execute_command_args
64 /* This return result of the MI command (output) */
65 enum mi_cmd_result rc;
67 /* What action to perform when the call is finished (output) */
68 enum captured_mi_execute_command_actions action;
70 /* The command context to be executed (input) */
71 struct mi_parse *command;
75 struct ui_file *raw_stdout;
77 /* The token of the last asynchronous command */
78 static char *last_async_command;
79 static char *previous_async_command;
80 static char *mi_error_message;
81 static char *old_regs;
83 extern void _initialize_mi_main (void);
84 static char *mi_input (char *);
85 static void mi_execute_command (char *cmd, int from_tty);
86 static enum mi_cmd_result mi_cmd_execute (struct mi_parse *parse);
88 static void mi_execute_cli_command (const char *cli, char *args);
89 static enum mi_cmd_result mi_execute_async_cli_command (char *mi, char *args, int from_tty);
90 static void mi_execute_command_wrapper (char *cmd);
92 void mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg);
94 static int register_changed_p (int regnum);
95 static int get_register (int regnum, int format);
96 static void mi_load_progress (const char *section_name,
97 unsigned long sent_so_far,
98 unsigned long total_section,
99 unsigned long total_sent,
100 unsigned long grand_total);
102 /* FIXME: these should go in some .h file, but infcmd.c doesn't have a
103 corresponding .h file. These wrappers will be obsolete anyway, once
104 we pull the plug on the sanitization. */
105 extern void interrupt_target_command_wrapper (char *, int);
106 extern void return_command_wrapper (char *, int);
108 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
109 layer that calls libgdb. Any operation used in the below should be
113 mi_cmd_gdb_exit (char *command, char **argv, int argc)
115 /* We have to print everything right here because we never return */
116 if (last_async_command)
117 fputs_unfiltered (last_async_command, raw_stdout);
118 fputs_unfiltered ("^exit\n", raw_stdout);
119 mi_out_put (uiout, raw_stdout);
120 /* FIXME: The function called is not yet a formal libgdb function */
121 quit_force (NULL, FROM_TTY);
126 mi_cmd_exec_run (char *args, int from_tty)
128 /* FIXME: Should call a libgdb function, not a cli wrapper */
129 return mi_execute_async_cli_command ("run", args, from_tty);
133 mi_cmd_exec_next (char *args, int from_tty)
135 /* FIXME: Should call a libgdb function, not a cli wrapper */
136 return mi_execute_async_cli_command ("next", args, from_tty);
140 mi_cmd_exec_next_instruction (char *args, int from_tty)
142 /* FIXME: Should call a libgdb function, not a cli wrapper */
143 return mi_execute_async_cli_command ("nexti", args, from_tty);
147 mi_cmd_exec_step (char *args, int from_tty)
149 /* FIXME: Should call a libgdb function, not a cli wrapper */
150 return mi_execute_async_cli_command ("step", args, from_tty);
154 mi_cmd_exec_step_instruction (char *args, int from_tty)
156 /* FIXME: Should call a libgdb function, not a cli wrapper */
157 return mi_execute_async_cli_command ("stepi", args, from_tty);
161 mi_cmd_exec_finish (char *args, int from_tty)
163 /* FIXME: Should call a libgdb function, not a cli wrapper */
164 return mi_execute_async_cli_command ("finish", args, from_tty);
168 mi_cmd_exec_until (char *args, int from_tty)
170 /* FIXME: Should call a libgdb function, not a cli wrapper */
171 return mi_execute_async_cli_command ("until", args, from_tty);
175 mi_cmd_exec_return (char *args, int from_tty)
177 /* This command doesn't really execute the target, it just pops the
178 specified number of frames. */
180 /* Call return_command with from_tty argument equal to 0 so as to
181 avoid being queried. */
182 return_command_wrapper (args, 0);
184 /* Call return_command with from_tty argument equal to 0 so as to
185 avoid being queried. */
186 return_command_wrapper (NULL, 0);
188 /* Because we have called return_command with from_tty = 0, we need
189 to print the frame here. */
190 show_and_print_stack_frame (selected_frame,
191 frame_relative_level (selected_frame),
198 mi_cmd_exec_continue (char *args, int from_tty)
200 /* FIXME: Should call a libgdb function, not a cli wrapper */
201 return mi_execute_async_cli_command ("continue", args, from_tty);
204 /* Interrupt the execution of the target. Note how we must play around
205 with the token varialbes, in order to display the current token in
206 the result of the interrupt command, and the previous execution
207 token when the target finally stops. See comments in
210 mi_cmd_exec_interrupt (char *args, int from_tty)
212 if (!target_executing)
214 xasprintf (&mi_error_message,
215 "mi_cmd_exec_interrupt: Inferior not executing.");
218 interrupt_target_command_wrapper (args, from_tty);
219 if (last_async_command)
220 fputs_unfiltered (last_async_command, raw_stdout);
221 fputs_unfiltered ("^done", raw_stdout);
222 xfree (last_async_command);
223 if (previous_async_command)
224 last_async_command = xstrdup (previous_async_command);
225 xfree (previous_async_command);
226 previous_async_command = NULL;
227 mi_out_put (uiout, raw_stdout);
228 mi_out_rewind (uiout);
229 fputs_unfiltered ("\n", raw_stdout);
234 mi_cmd_thread_select (char *command, char **argv, int argc)
240 xasprintf (&mi_error_message,
241 "mi_cmd_thread_select: USAGE: threadnum.");
245 rc = gdb_thread_select (uiout, argv[0]);
247 if (rc == GDB_RC_FAIL)
248 return MI_CMD_CAUGHT_ERROR;
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)
279 /* Note that the test for a valid register must include checking the
280 REGISTER_NAME because NUM_REGS may be allocated for the union of
281 the register sets within a family of related processors. In this
282 case, some entries of REGISTER_NAME will change depending upon
283 the particular processor being debugged. */
285 numregs = NUM_REGS + NUM_PSEUDO_REGS;
287 ui_out_list_begin (uiout, "register-names");
289 if (argc == 0) /* No args, just do all the regs */
295 if (REGISTER_NAME (regnum) == NULL
296 || *(REGISTER_NAME (regnum)) == '\0')
297 ui_out_field_string (uiout, NULL, "");
299 ui_out_field_string (uiout, NULL, REGISTER_NAME (regnum));
303 /* Else, list of register #s, just do listed regs */
304 for (i = 0; i < argc; i++)
306 regnum = atoi (argv[i]);
307 if (regnum < 0 || regnum >= numregs)
309 xasprintf (&mi_error_message, "bad register number");
312 if (REGISTER_NAME (regnum) == NULL
313 || *(REGISTER_NAME (regnum)) == '\0')
314 ui_out_field_string (uiout, NULL, "");
316 ui_out_field_string (uiout, NULL, REGISTER_NAME (regnum));
318 ui_out_list_end (uiout);
323 mi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
325 int regnum, numregs, changed;
328 /* Note that the test for a valid register must include checking the
329 REGISTER_NAME because NUM_REGS may be allocated for the union of
330 the register sets within a family of related processors. In this
331 case, some entries of REGISTER_NAME will change depending upon
332 the particular processor being debugged. */
336 ui_out_list_begin (uiout, "changed-registers");
338 if (argc == 0) /* No args, just do all the regs */
344 if (REGISTER_NAME (regnum) == NULL
345 || *(REGISTER_NAME (regnum)) == '\0')
347 changed = register_changed_p (regnum);
350 xasprintf (&mi_error_message,
351 "mi_cmd_data_list_changed_registers: Unable to read register contents.");
355 ui_out_field_int (uiout, NULL, regnum);
359 /* Else, list of register #s, just do listed regs */
360 for (i = 0; i < argc; i++)
362 regnum = atoi (argv[i]);
366 && REGISTER_NAME (regnum) != NULL
367 && *REGISTER_NAME (regnum) != '\000')
369 changed = register_changed_p (regnum);
372 xasprintf (&mi_error_message,
373 "mi_cmd_data_list_register_change: Unable to read register contents.");
377 ui_out_field_int (uiout, NULL, regnum);
381 xasprintf (&mi_error_message, "bad register number");
385 ui_out_list_end (uiout);
390 register_changed_p (int regnum)
392 char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
394 if (! frame_register_read (selected_frame, regnum, raw_buffer))
397 if (memcmp (&old_regs[REGISTER_BYTE (regnum)], raw_buffer,
398 REGISTER_RAW_SIZE (regnum)) == 0)
401 /* Found a changed register. Return 1. */
403 memcpy (&old_regs[REGISTER_BYTE (regnum)], raw_buffer,
404 REGISTER_RAW_SIZE (regnum));
409 /* Return a list of register number and value pairs. The valid
410 arguments expected are: a letter indicating the format in which to
411 display the registers contents. This can be one of: x (hexadecimal), d
412 (decimal), N (natural), t (binary), o (octal), r (raw). After the
413 format argumetn there can be a sequence of numbers, indicating which
414 registers to fetch the content of. If the format is the only argument,
415 a list of all the registers with their values is returned. */
417 mi_cmd_data_list_register_values (char *command, char **argv, int argc)
419 int regnum, numregs, format, result;
422 /* Note that the test for a valid register must include checking the
423 REGISTER_NAME because NUM_REGS may be allocated for the union of
424 the register sets within a family of related processors. In this
425 case, some entries of REGISTER_NAME will change depending upon
426 the particular processor being debugged. */
432 xasprintf (&mi_error_message,
433 "mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
437 format = (int) argv[0][0];
439 if (!target_has_registers)
441 xasprintf (&mi_error_message,
442 "mi_cmd_data_list_register_values: No registers.");
446 ui_out_list_begin (uiout, "register-values");
448 if (argc == 1) /* No args, beside the format: do all the regs */
454 if (REGISTER_NAME (regnum) == NULL
455 || *(REGISTER_NAME (regnum)) == '\0')
457 ui_out_tuple_begin (uiout, NULL);
458 ui_out_field_int (uiout, "number", regnum);
459 result = get_register (regnum, format);
462 ui_out_tuple_end (uiout);
466 /* Else, list of register #s, just do listed regs */
467 for (i = 1; i < argc; i++)
469 regnum = atoi (argv[i]);
473 && REGISTER_NAME (regnum) != NULL
474 && *REGISTER_NAME (regnum) != '\000')
476 ui_out_tuple_begin (uiout, NULL);
477 ui_out_field_int (uiout, "number", regnum);
478 result = get_register (regnum, format);
481 ui_out_tuple_end (uiout);
485 xasprintf (&mi_error_message, "bad register number");
489 ui_out_list_end (uiout);
493 /* Output one register's contents in the desired format. */
495 get_register (int regnum, int format)
497 char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
498 char *virtual_buffer = alloca (MAX_REGISTER_VIRTUAL_SIZE);
500 static struct ui_stream *stb = NULL;
502 stb = ui_out_stream_new (uiout);
507 get_saved_register (raw_buffer, &optim, (CORE_ADDR *) NULL, selected_frame,
508 regnum, (enum lval_type *) NULL);
511 xasprintf (&mi_error_message, "Optimized out");
515 /* Convert raw data to virtual format if necessary. */
517 if (REGISTER_CONVERTIBLE (regnum))
519 REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
520 raw_buffer, virtual_buffer);
523 memcpy (virtual_buffer, raw_buffer, REGISTER_VIRTUAL_SIZE (regnum));
528 char *ptr, buf[1024];
532 for (j = 0; j < REGISTER_RAW_SIZE (regnum); j++)
534 register int idx = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? j
535 : REGISTER_RAW_SIZE (regnum) - 1 - j;
536 sprintf (ptr, "%02x", (unsigned char) raw_buffer[idx]);
539 ui_out_field_string (uiout, "value", buf);
540 /*fputs_filtered (buf, gdb_stdout); */
544 val_print (REGISTER_VIRTUAL_TYPE (regnum), virtual_buffer, 0, 0,
545 stb->stream, format, 1, 0, Val_pretty_default);
546 ui_out_field_stream (uiout, "value", stb);
547 ui_out_stream_delete (stb);
552 /* Write given values into registers. The registers and values are
553 given as pairs. The corresponding MI command is
554 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
556 mi_cmd_data_write_register_values (char *command, char **argv, int argc)
564 /* Note that the test for a valid register must include checking the
565 REGISTER_NAME because NUM_REGS may be allocated for the union of
566 the register sets within a family of related processors. In this
567 case, some entries of REGISTER_NAME will change depending upon
568 the particular processor being debugged. */
574 xasprintf (&mi_error_message,
575 "mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
579 format = (int) argv[0][0];
581 if (!target_has_registers)
583 xasprintf (&mi_error_message,
584 "mi_cmd_data_write_register_values: No registers.");
590 xasprintf (&mi_error_message,
591 "mi_cmd_data_write_register_values: No regs and values specified.");
597 xasprintf (&mi_error_message,
598 "mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
602 for (i = 1; i < argc; i = i + 2)
604 regnum = atoi (argv[i]);
608 && REGISTER_NAME (regnum) != NULL
609 && *REGISTER_NAME (regnum) != '\000')
612 struct cleanup *old_chain;
614 /* Get the value as a number */
615 value = parse_and_eval_address (argv[i + 1]);
616 /* Get the value into an array */
617 buffer = xmalloc (REGISTER_SIZE);
618 old_chain = make_cleanup (xfree, buffer);
619 store_signed_integer (buffer, REGISTER_SIZE, value);
621 write_register_bytes (REGISTER_BYTE (regnum), buffer, REGISTER_RAW_SIZE (regnum));
622 /* Free the buffer. */
623 do_cleanups (old_chain);
627 xasprintf (&mi_error_message, "bad register number");
635 /*This is commented out because we decided it was not useful. I leave
636 it, just in case. ezannoni:1999-12-08 */
638 /* Assign a value to a variable. The expression argument must be in
639 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
642 mi_cmd_data_assign (char *command, char **argv, int argc)
644 struct expression *expr;
645 struct cleanup *old_chain;
649 xasprintf (&mi_error_message,
650 "mi_cmd_data_assign: Usage: -data-assign expression");
654 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
655 01-12-1999: Need to decide what to do with this for libgdb purposes. */
657 expr = parse_expression (argv[0]);
658 old_chain = make_cleanup (free_current_contents, &expr);
659 evaluate_expression (expr);
660 do_cleanups (old_chain);
665 /* Evaluate the value of the argument. The argument is an
666 expression. If the expression contains spaces it needs to be
667 included in double quotes. */
669 mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
671 struct expression *expr;
672 struct cleanup *old_chain = NULL;
674 struct ui_stream *stb = NULL;
676 stb = ui_out_stream_new (uiout);
680 xasprintf (&mi_error_message,
681 "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
685 expr = parse_expression (argv[0]);
687 old_chain = make_cleanup (free_current_contents, &expr);
689 val = evaluate_expression (expr);
691 /* Print the result of the expression evaluation. */
692 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val),
693 VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
694 stb->stream, 0, 0, 0, 0);
696 ui_out_field_stream (uiout, "value", stb);
697 ui_out_stream_delete (stb);
699 do_cleanups (old_chain);
705 mi_cmd_target_download (char *args, int from_tty)
708 struct cleanup *old_cleanups = NULL;
710 xasprintf (&run, "load %s", args);
711 old_cleanups = make_cleanup (xfree, run);
712 execute_command (run, from_tty);
714 do_cleanups (old_cleanups);
718 /* Connect to the remote target. */
720 mi_cmd_target_select (char *args, int from_tty)
723 struct cleanup *old_cleanups = NULL;
725 xasprintf (&run, "target %s", args);
726 old_cleanups = make_cleanup (xfree, run);
728 /* target-select is always synchronous. once the call has returned
729 we know that we are connected. */
730 /* NOTE: At present all targets that are connected are also
731 (implicitly) talking to a halted target. In the future this may
733 execute_command (run, from_tty);
735 do_cleanups (old_cleanups);
737 /* Issue the completion message here. */
738 if (last_async_command)
739 fputs_unfiltered (last_async_command, raw_stdout);
740 fputs_unfiltered ("^connected", raw_stdout);
741 mi_out_put (uiout, raw_stdout);
742 mi_out_rewind (uiout);
743 fputs_unfiltered ("\n", raw_stdout);
744 do_exec_cleanups (ALL_CLEANUPS);
750 ADDR: start address of data to be dumped.
751 WORD-FORMAT: a char indicating format for the ``word''. See
753 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
754 NR_ROW: Number of rows.
755 NR_COL: The number of colums (words per row).
756 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
757 ASCHAR for unprintable characters.
759 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
760 displayes them. Returns:
762 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
765 The number of bytes read is SIZE*ROW*COL. */
768 mi_cmd_data_read_memory (char *command, char **argv, int argc)
770 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
776 struct type *word_type;
789 static struct mi_opt opts[] =
791 {"o", OFFSET_OPT, 1},
797 int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts,
801 switch ((enum opt) opt)
804 offset = atol (optarg);
811 if (argc < 5 || argc > 6)
813 xasprintf (&mi_error_message,
814 "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
818 /* Extract all the arguments. */
820 /* Start address of the memory dump. */
821 addr = parse_and_eval_address (argv[0]) + offset;
822 /* The format character to use when displaying a memory word. See
823 the ``x'' command. */
824 word_format = argv[1][0];
825 /* The size of the memory word. */
826 word_size = atol (argv[2]);
830 word_type = builtin_type_int8;
834 word_type = builtin_type_int16;
838 word_type = builtin_type_int32;
842 word_type = builtin_type_int64;
846 word_type = builtin_type_int8;
849 /* The number of rows */
850 nr_rows = atol (argv[3]);
853 xasprintf (&mi_error_message,
854 "mi_cmd_data_read_memory: invalid number of rows.");
857 /* number of bytes per row. */
858 nr_cols = atol (argv[4]);
861 xasprintf (&mi_error_message,
862 "mi_cmd_data_read_memory: invalid number of columns.");
864 /* The un-printable character when printing ascii. */
870 /* create a buffer and read it in. */
871 total_bytes = word_size * nr_rows * nr_cols;
872 mbuf = xcalloc (total_bytes, 1);
873 make_cleanup (xfree, mbuf);
876 xasprintf (&mi_error_message,
877 "mi_cmd_data_read_memory: out of memory.");
881 while (nr_bytes < total_bytes)
884 long num = target_read_memory_partial (addr + nr_bytes, mbuf + nr_bytes,
885 total_bytes - nr_bytes,
892 /* output the header information. */
893 ui_out_field_core_addr (uiout, "addr", addr);
894 ui_out_field_int (uiout, "nr-bytes", nr_bytes);
895 ui_out_field_int (uiout, "total-bytes", total_bytes);
896 ui_out_field_core_addr (uiout, "next-row", addr + word_size * nr_cols);
897 ui_out_field_core_addr (uiout, "prev-row", addr - word_size * nr_cols);
898 ui_out_field_core_addr (uiout, "next-page", addr + total_bytes);
899 ui_out_field_core_addr (uiout, "prev-page", addr - total_bytes);
901 /* Build the result as a two dimentional table. */
903 struct ui_stream *stream = ui_out_stream_new (uiout);
906 ui_out_list_begin (uiout, "memory");
907 for (row = 0, row_byte = 0;
909 row++, row_byte += nr_cols * word_size)
913 ui_out_tuple_begin (uiout, NULL);
914 ui_out_field_core_addr (uiout, "addr", addr + row_byte);
915 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
916 ui_out_list_begin (uiout, "data");
917 for (col = 0, col_byte = row_byte;
919 col++, col_byte += word_size)
921 if (col_byte + word_size > nr_bytes)
923 ui_out_field_string (uiout, NULL, "N/A");
927 ui_file_rewind (stream->stream);
928 print_scalar_formatted (mbuf + col_byte, word_type, word_format,
929 word_asize, stream->stream);
930 ui_out_field_stream (uiout, NULL, stream);
933 ui_out_list_end (uiout);
937 ui_file_rewind (stream->stream);
938 for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
940 if (byte >= nr_bytes)
942 fputc_unfiltered ('X', stream->stream);
944 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
946 fputc_unfiltered (aschar, stream->stream);
949 fputc_unfiltered (mbuf[byte], stream->stream);
951 ui_out_field_stream (uiout, "ascii", stream);
953 ui_out_tuple_end (uiout);
955 ui_out_stream_delete (stream);
956 ui_out_list_end (uiout);
958 do_cleanups (cleanups);
962 /* DATA-MEMORY-WRITE:
964 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
965 offset from the beginning of the memory grid row where the cell to
967 ADDR: start address of the row in the memory grid where the memory
968 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
969 the location to write to.
970 FORMAT: a char indicating format for the ``word''. See
972 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
973 VALUE: value to be written into the memory address.
975 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
979 mi_cmd_data_write_memory (char *command, char **argv, int argc)
984 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
985 enough when using a compiler other than GCC. */
988 struct cleanup *old_chain;
996 static struct mi_opt opts[] =
998 {"o", OFFSET_OPT, 1},
1004 int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
1008 switch ((enum opt) opt)
1011 offset = atol (optarg);
1020 xasprintf (&mi_error_message,
1021 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1022 return MI_CMD_ERROR;
1025 /* Extract all the arguments. */
1026 /* Start address of the memory dump. */
1027 addr = parse_and_eval_address (argv[0]);
1028 /* The format character to use when displaying a memory word. See
1029 the ``x'' command. */
1030 word_format = argv[1][0];
1031 /* The size of the memory word. */
1032 word_size = atol (argv[2]);
1034 /* Calculate the real address of the write destination. */
1035 addr += (offset * word_size);
1037 /* Get the value as a number */
1038 value = parse_and_eval_address (argv[3]);
1039 /* Get the value into an array */
1040 buffer = xmalloc (word_size);
1041 old_chain = make_cleanup (xfree, buffer);
1042 store_signed_integer (buffer, word_size, value);
1043 /* Write it down to memory */
1044 write_memory (addr, buffer, word_size);
1045 /* Free the buffer. */
1046 do_cleanups (old_chain);
1051 /* Execute a command within a safe environment.
1052 Return <0 for error; >=0 for ok.
1054 args->action will tell mi_execute_command what action
1055 to perfrom after the given command has executed (display/supress
1056 prompt, display error). */
1059 captured_mi_execute_command (struct ui_out *uiout, void *data)
1061 struct captured_mi_execute_command_args *args =
1062 (struct captured_mi_execute_command_args *) data;
1063 struct mi_parse *context = args->command;
1065 switch (context->op)
1069 /* A MI command was read from the input stream */
1071 /* FIXME: gdb_???? */
1072 fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
1073 context->token, context->command, context->args);
1074 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1075 condition expression, each function should return an
1076 indication of what action is required and then switch on
1078 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1079 args->rc = mi_cmd_execute (context);
1081 if (!target_can_async_p () || !target_executing)
1083 /* print the result if there were no errors */
1084 if (args->rc == MI_CMD_DONE)
1086 fputs_unfiltered (context->token, raw_stdout);
1087 fputs_unfiltered ("^done", raw_stdout);
1088 mi_out_put (uiout, raw_stdout);
1089 mi_out_rewind (uiout);
1090 fputs_unfiltered ("\n", raw_stdout);
1092 else if (args->rc == MI_CMD_ERROR)
1094 if (mi_error_message)
1096 fputs_unfiltered (context->token, raw_stdout);
1097 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1098 fputstr_unfiltered (mi_error_message, '"', raw_stdout);
1099 xfree (mi_error_message);
1100 fputs_unfiltered ("\"\n", raw_stdout);
1102 mi_out_rewind (uiout);
1104 else if (args->rc == MI_CMD_CAUGHT_ERROR)
1106 mi_out_rewind (uiout);
1107 args->action = EXECUTE_COMMAND_DISPLAY_ERROR;
1111 mi_out_rewind (uiout);
1113 else if (sync_execution)
1115 /* Don't print the prompt. We are executing the target in
1116 synchronous mode. */
1117 args->action = EXECUTE_COMMAND_SUPRESS_PROMPT;
1123 /* A CLI command was read from the input stream */
1124 /* This will be removed as soon as we have a complete set of
1126 /* echo the command on the console. */
1127 fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1128 /* FIXME: If the command string has something that looks like
1129 a format spec (e.g. %s) we will get a core dump */
1130 mi_execute_cli_command ("%s", context->command);
1131 /* print the result */
1132 /* FIXME: Check for errors here. */
1133 fputs_unfiltered (context->token, raw_stdout);
1134 fputs_unfiltered ("^done", raw_stdout);
1135 mi_out_put (uiout, raw_stdout);
1136 mi_out_rewind (uiout);
1137 fputs_unfiltered ("\n", raw_stdout);
1138 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1139 args->rc = MI_CMD_DONE;
1149 mi_execute_command (char *cmd, int from_tty)
1151 struct mi_parse *command;
1152 struct captured_mi_execute_command_args args;
1153 struct ui_out *saved_uiout = uiout;
1156 /* This is to handle EOF (^D). We just quit gdb. */
1157 /* FIXME: we should call some API function here. */
1159 quit_force (NULL, from_tty);
1161 command = mi_parse (cmd);
1163 if (command != NULL)
1165 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
1166 be pushed even further down or even eliminated? */
1167 args.command = command;
1168 result = catch_exceptions (uiout, captured_mi_execute_command, &args, "",
1171 if (args.action == EXECUTE_COMMAND_SUPRESS_PROMPT)
1173 /* The command is executing synchronously. Bail out early
1174 suppressing the finished prompt. */
1175 mi_parse_free (command);
1178 if (args.action == EXECUTE_COMMAND_DISPLAY_ERROR || result < 0)
1180 char *msg = error_last_message ();
1181 struct cleanup *cleanup = make_cleanup (xfree, msg);
1182 /* The command execution failed and error() was called
1184 fputs_unfiltered (command->token, raw_stdout);
1185 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1186 fputstr_unfiltered (msg, '"', raw_stdout);
1187 fputs_unfiltered ("\"\n", raw_stdout);
1189 mi_parse_free (command);
1192 fputs_unfiltered ("(gdb) \n", raw_stdout);
1193 gdb_flush (raw_stdout);
1194 /* print any buffered hook code */
1198 static enum mi_cmd_result
1199 mi_cmd_execute (struct mi_parse *parse)
1201 if (parse->cmd->argv_func != NULL
1202 || parse->cmd->args_func != NULL)
1204 /* FIXME: We need to save the token because the command executed
1205 may be asynchronous and need to print the token again.
1206 In the future we can pass the token down to the func
1207 and get rid of the last_async_command */
1208 /* The problem here is to keep the token around when we launch
1209 the target, and we want to interrupt it later on. The
1210 interrupt command will have its own token, but when the
1211 target stops, we must display the token corresponding to the
1212 last execution command given. So we have another string where
1213 we copy the token (previous_async_command), if this was
1214 indeed the token of an execution command, and when we stop we
1215 print that one. This is possible because the interrupt
1216 command, when over, will copy that token back into the
1217 default token string (last_async_command). */
1219 if (target_executing)
1221 if (!previous_async_command)
1222 previous_async_command = xstrdup (last_async_command);
1223 if (strcmp (parse->command, "exec-interrupt"))
1225 fputs_unfiltered (parse->token, raw_stdout);
1226 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1227 fputs_unfiltered ("Cannot execute command ", raw_stdout);
1228 fputstr_unfiltered (parse->command, '"', raw_stdout);
1229 fputs_unfiltered (" while target running", raw_stdout);
1230 fputs_unfiltered ("\"\n", raw_stdout);
1231 return MI_CMD_ERROR;
1234 last_async_command = xstrdup (parse->token);
1235 make_exec_cleanup (free_current_contents, &last_async_command);
1236 /* FIXME: DELETE THIS! */
1237 if (parse->cmd->args_func != NULL)
1238 return parse->cmd->args_func (parse->args, 0 /*from_tty */ );
1239 return parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
1241 else if (parse->cmd->cli != 0)
1243 /* FIXME: DELETE THIS. */
1244 /* The operation is still implemented by a cli command */
1245 /* Must be a synchronous one */
1246 mi_execute_cli_command (parse->cmd->cli, parse->args);
1251 /* FIXME: DELETE THIS. */
1252 fputs_unfiltered (parse->token, raw_stdout);
1253 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1254 fputs_unfiltered ("Undefined mi command: ", raw_stdout);
1255 fputstr_unfiltered (parse->command, '"', raw_stdout);
1256 fputs_unfiltered (" (missing implementation)", raw_stdout);
1257 fputs_unfiltered ("\"\n", raw_stdout);
1258 return MI_CMD_ERROR;
1263 mi_execute_command_wrapper (char *cmd)
1265 mi_execute_command (cmd, stdin == instream);
1268 /* FIXME: This is just a hack so we can get some extra commands going.
1269 We don't want to channel things through the CLI, but call libgdb directly */
1270 /* Use only for synchronous commands */
1273 mi_execute_cli_command (const char *cli, char *args)
1277 struct cleanup *old_cleanups;
1279 xasprintf (&run, cli, args);
1281 /* FIXME: gdb_???? */
1282 fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
1284 old_cleanups = make_cleanup (xfree, run);
1285 execute_command ( /*ui */ run, 0 /*from_tty */ );
1286 do_cleanups (old_cleanups);
1292 mi_execute_async_cli_command (char *mi, char *args, int from_tty)
1294 struct cleanup *old_cleanups;
1298 if (target_can_async_p ())
1300 async_args = (char *) xmalloc (strlen (args) + 2);
1301 make_exec_cleanup (free, async_args);
1302 strcpy (async_args, args);
1303 strcat (async_args, "&");
1304 xasprintf (&run, "%s %s", mi, async_args);
1305 make_exec_cleanup (free, run);
1306 add_continuation (mi_exec_async_cli_cmd_continuation, NULL);
1307 old_cleanups = NULL;
1311 xasprintf (&run, "%s %s", mi, args);
1312 old_cleanups = make_cleanup (xfree, run);
1315 if (!target_can_async_p ())
1317 /* NOTE: For synchronous targets asynchronous behavour is faked by
1318 printing out the GDB prompt before we even try to execute the
1320 if (last_async_command)
1321 fputs_unfiltered (last_async_command, raw_stdout);
1322 fputs_unfiltered ("^running\n", raw_stdout);
1323 fputs_unfiltered ("(gdb) \n", raw_stdout);
1324 gdb_flush (raw_stdout);
1328 /* FIXME: cagney/1999-11-29: Printing this message before
1329 calling execute_command is wrong. It should only be printed
1330 once gdb has confirmed that it really has managed to send a
1331 run command to the target. */
1332 if (last_async_command)
1333 fputs_unfiltered (last_async_command, raw_stdout);
1334 fputs_unfiltered ("^running\n", raw_stdout);
1337 execute_command ( /*ui */ run, 0 /*from_tty */ );
1339 if (!target_can_async_p ())
1341 /* Do this before doing any printing. It would appear that some
1342 print code leaves garbage around in the buffer. */
1343 do_cleanups (old_cleanups);
1344 /* If the target was doing the operation synchronously we fake
1345 the stopped message. */
1346 if (last_async_command)
1347 fputs_unfiltered (last_async_command, raw_stdout);
1348 fputs_unfiltered ("*stopped", raw_stdout);
1349 mi_out_put (uiout, raw_stdout);
1350 mi_out_rewind (uiout);
1351 fputs_unfiltered ("\n", raw_stdout);
1352 return MI_CMD_QUIET;
1358 mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg)
1360 if (last_async_command)
1361 fputs_unfiltered (last_async_command, raw_stdout);
1362 fputs_unfiltered ("*stopped", raw_stdout);
1363 mi_out_put (uiout, raw_stdout);
1364 fputs_unfiltered ("\n", raw_stdout);
1365 fputs_unfiltered ("(gdb) \n", raw_stdout);
1366 gdb_flush (raw_stdout);
1367 do_exec_cleanups (ALL_CLEANUPS);
1371 mi_input (char *buf)
1373 return gdb_readline (NULL);
1377 mi_load_progress (const char *section_name,
1378 unsigned long sent_so_far,
1379 unsigned long total_section,
1380 unsigned long total_sent,
1381 unsigned long grand_total)
1383 struct timeval time_now, delta, update_threshold;
1384 static struct timeval last_update;
1385 static char *previous_sect_name = NULL;
1388 if (!interpreter_p || strncmp (interpreter_p, "mi", 2) != 0)
1391 update_threshold.tv_sec = 0;
1392 update_threshold.tv_usec = 500000;
1393 gettimeofday (&time_now, NULL);
1395 delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
1396 delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
1398 if (delta.tv_usec < 0)
1401 delta.tv_usec += 1000000;
1404 new_section = (previous_sect_name ?
1405 strcmp (previous_sect_name, section_name) : 1);
1408 xfree (previous_sect_name);
1409 previous_sect_name = xstrdup (section_name);
1411 if (last_async_command)
1412 fputs_unfiltered (last_async_command, raw_stdout);
1413 fputs_unfiltered ("+download", raw_stdout);
1414 ui_out_tuple_begin (uiout, NULL);
1415 ui_out_field_string (uiout, "section", section_name);
1416 ui_out_field_int (uiout, "section-size", total_section);
1417 ui_out_field_int (uiout, "total-size", grand_total);
1418 ui_out_tuple_end (uiout);
1419 mi_out_put (uiout, raw_stdout);
1420 fputs_unfiltered ("\n", raw_stdout);
1421 gdb_flush (raw_stdout);
1424 if (delta.tv_sec >= update_threshold.tv_sec &&
1425 delta.tv_usec >= update_threshold.tv_usec)
1427 last_update.tv_sec = time_now.tv_sec;
1428 last_update.tv_usec = time_now.tv_usec;
1429 if (last_async_command)
1430 fputs_unfiltered (last_async_command, raw_stdout);
1431 fputs_unfiltered ("+download", raw_stdout);
1432 ui_out_tuple_begin (uiout, NULL);
1433 ui_out_field_string (uiout, "section", section_name);
1434 ui_out_field_int (uiout, "section-sent", sent_so_far);
1435 ui_out_field_int (uiout, "section-size", total_section);
1436 ui_out_field_int (uiout, "total-sent", total_sent);
1437 ui_out_field_int (uiout, "total-size", grand_total);
1438 ui_out_tuple_end (uiout);
1439 mi_out_put (uiout, raw_stdout);
1440 fputs_unfiltered ("\n", raw_stdout);
1441 gdb_flush (raw_stdout);
1446 mi_command_loop (int mi_version)
1448 /* HACK: Force stdout/stderr to point at the console. This avoids
1449 any potential side effects caused by legacy code that is still
1450 using the TUI / fputs_unfiltered_hook */
1451 raw_stdout = stdio_fileopen (stdout);
1452 /* Route normal output through the MIx */
1453 gdb_stdout = mi_console_file_new (raw_stdout, "~");
1454 /* Route error and log output through the MI */
1455 gdb_stderr = mi_console_file_new (raw_stdout, "&");
1456 gdb_stdlog = gdb_stderr;
1457 /* Route target output through the MI. */
1458 gdb_stdtarg = mi_console_file_new (raw_stdout, "@");
1460 /* HACK: Poke the ui_out table directly. Should we be creating a
1461 mi_out object wired up to the above gdb_stdout / gdb_stderr? */
1462 uiout = mi_out_new (mi_version);
1464 /* HACK: Override any other interpreter hooks. We need to create a
1465 real event table and pass in that. */
1467 /* command_loop_hook = 0; */
1468 print_frame_info_listing_hook = 0;
1471 create_breakpoint_hook = 0;
1472 delete_breakpoint_hook = 0;
1473 modify_breakpoint_hook = 0;
1474 interactive_hook = 0;
1475 registers_changed_hook = 0;
1476 readline_begin_hook = 0;
1478 readline_end_hook = 0;
1479 register_changed_hook = 0;
1480 memory_changed_hook = 0;
1482 target_wait_hook = 0;
1483 call_command_hook = 0;
1485 error_begin_hook = 0;
1486 show_load_progress = mi_load_progress;
1488 /* Turn off 8 bit strings in quoted output. Any character with the
1489 high bit set is printed using C's octal format. */
1490 sevenbit_strings = 1;
1492 /* Tell the world that we're alive */
1493 fputs_unfiltered ("(gdb) \n", raw_stdout);
1494 gdb_flush (raw_stdout);
1497 simplified_command_loop (mi_input, mi_execute_command);
1499 start_event_loop ();
1503 mi0_command_loop (void)
1505 mi_command_loop (0);
1509 mi1_command_loop (void)
1511 mi_command_loop (1);
1515 setup_architecture_data (void)
1517 /* don't trust REGISTER_BYTES to be zero. */
1518 old_regs = xmalloc (REGISTER_BYTES + 1);
1519 memset (old_regs, 0, REGISTER_BYTES + 1);
1523 mi_init_ui (char *arg0)
1525 /* Eventually this will contain code that takes control of the
1530 _initialize_mi_main (void)
1532 if (interpreter_p == NULL)
1535 /* If we're _the_ interpreter, take control. */
1536 if (strcmp (interpreter_p, "mi0") == 0)
1537 command_loop_hook = mi0_command_loop;
1538 else if (strcmp (interpreter_p, "mi") == 0
1539 || strcmp (interpreter_p, "mi1") == 0)
1540 command_loop_hook = mi1_command_loop;
1544 init_ui_hook = mi_init_ui;
1545 setup_architecture_data ();
1546 register_gdbarch_swap (&old_regs, sizeof (old_regs), NULL);
1547 register_gdbarch_swap (NULL, 0, setup_architecture_data);
1550 /* These overwrite some of the initialization done in
1551 _intialize_event_loop. */
1552 call_readline = gdb_readline2;
1553 input_handler = mi_execute_command_wrapper;
1554 add_file_handler (input_fd, stdin_event_handler, 0);
1555 async_command_editing_p = 0;
1557 /* FIXME: Should we notify main that we are here as a possible