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 (selected_frame
,
187 frame_relative_level (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 (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
, selected_frame
,
522 regnum
, (enum lval_type
*) NULL
);
525 xasprintf (&mi_error_message
, "Optimized out");
529 /* Convert raw data to virtual format if necessary. */
531 if (REGISTER_CONVERTIBLE (regnum
))
533 REGISTER_CONVERT_TO_VIRTUAL (regnum
, REGISTER_VIRTUAL_TYPE (regnum
),
534 raw_buffer
, virtual_buffer
);
537 memcpy (virtual_buffer
, raw_buffer
, REGISTER_VIRTUAL_SIZE (regnum
));
542 char *ptr
, buf
[1024];
546 for (j
= 0; j
< REGISTER_RAW_SIZE (regnum
); j
++)
548 register int idx
= TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
? j
549 : REGISTER_RAW_SIZE (regnum
) - 1 - j
;
550 sprintf (ptr
, "%02x", (unsigned char) raw_buffer
[idx
]);
553 ui_out_field_string (uiout
, "value", buf
);
554 /*fputs_filtered (buf, gdb_stdout); */
558 val_print (REGISTER_VIRTUAL_TYPE (regnum
), virtual_buffer
, 0, 0,
559 stb
->stream
, format
, 1, 0, Val_pretty_default
);
560 ui_out_field_stream (uiout
, "value", stb
);
561 ui_out_stream_delete (stb
);
566 /* Write given values into registers. The registers and values are
567 given as pairs. The corresponding MI command is
568 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
570 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
578 /* Note that the test for a valid register must include checking the
579 REGISTER_NAME because NUM_REGS may be allocated for the union of
580 the register sets within a family of related processors. In this
581 case, some entries of REGISTER_NAME will change depending upon
582 the particular processor being debugged. */
588 xasprintf (&mi_error_message
,
589 "mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
593 format
= (int) argv
[0][0];
595 if (!target_has_registers
)
597 xasprintf (&mi_error_message
,
598 "mi_cmd_data_write_register_values: No registers.");
604 xasprintf (&mi_error_message
,
605 "mi_cmd_data_write_register_values: No regs and values specified.");
611 xasprintf (&mi_error_message
,
612 "mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
616 for (i
= 1; i
< argc
; i
= i
+ 2)
618 regnum
= atoi (argv
[i
]);
622 && REGISTER_NAME (regnum
) != NULL
623 && *REGISTER_NAME (regnum
) != '\000')
626 struct cleanup
*old_chain
;
628 /* Get the value as a number */
629 value
= parse_and_eval_address (argv
[i
+ 1]);
630 /* Get the value into an array */
631 buffer
= xmalloc (REGISTER_SIZE
);
632 old_chain
= make_cleanup (xfree
, buffer
);
633 store_signed_integer (buffer
, REGISTER_SIZE
, value
);
635 deprecated_write_register_bytes (REGISTER_BYTE (regnum
), buffer
, REGISTER_RAW_SIZE (regnum
));
636 /* Free the buffer. */
637 do_cleanups (old_chain
);
641 xasprintf (&mi_error_message
, "bad register number");
649 /*This is commented out because we decided it was not useful. I leave
650 it, just in case. ezannoni:1999-12-08 */
652 /* Assign a value to a variable. The expression argument must be in
653 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
656 mi_cmd_data_assign (char *command
, char **argv
, int argc
)
658 struct expression
*expr
;
659 struct cleanup
*old_chain
;
663 xasprintf (&mi_error_message
,
664 "mi_cmd_data_assign: Usage: -data-assign expression");
668 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
669 01-12-1999: Need to decide what to do with this for libgdb purposes. */
671 expr
= parse_expression (argv
[0]);
672 old_chain
= make_cleanup (free_current_contents
, &expr
);
673 evaluate_expression (expr
);
674 do_cleanups (old_chain
);
679 /* Evaluate the value of the argument. The argument is an
680 expression. If the expression contains spaces it needs to be
681 included in double quotes. */
683 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
685 struct expression
*expr
;
686 struct cleanup
*old_chain
= NULL
;
688 struct ui_stream
*stb
= NULL
;
690 stb
= ui_out_stream_new (uiout
);
694 xasprintf (&mi_error_message
,
695 "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
699 expr
= parse_expression (argv
[0]);
701 old_chain
= make_cleanup (free_current_contents
, &expr
);
703 val
= evaluate_expression (expr
);
705 /* Print the result of the expression evaluation. */
706 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
),
707 VALUE_EMBEDDED_OFFSET (val
), VALUE_ADDRESS (val
),
708 stb
->stream
, 0, 0, 0, 0);
710 ui_out_field_stream (uiout
, "value", stb
);
711 ui_out_stream_delete (stb
);
713 do_cleanups (old_chain
);
719 mi_cmd_target_download (char *args
, int from_tty
)
722 struct cleanup
*old_cleanups
= NULL
;
724 xasprintf (&run
, "load %s", args
);
725 old_cleanups
= make_cleanup (xfree
, run
);
726 execute_command (run
, from_tty
);
728 do_cleanups (old_cleanups
);
732 /* Connect to the remote target. */
734 mi_cmd_target_select (char *args
, int from_tty
)
737 struct cleanup
*old_cleanups
= NULL
;
739 xasprintf (&run
, "target %s", args
);
740 old_cleanups
= make_cleanup (xfree
, run
);
742 /* target-select is always synchronous. once the call has returned
743 we know that we are connected. */
744 /* NOTE: At present all targets that are connected are also
745 (implicitly) talking to a halted target. In the future this may
747 execute_command (run
, from_tty
);
749 do_cleanups (old_cleanups
);
751 /* Issue the completion message here. */
752 if (last_async_command
)
753 fputs_unfiltered (last_async_command
, raw_stdout
);
754 fputs_unfiltered ("^connected", raw_stdout
);
755 mi_out_put (uiout
, raw_stdout
);
756 mi_out_rewind (uiout
);
757 fputs_unfiltered ("\n", raw_stdout
);
758 do_exec_cleanups (ALL_CLEANUPS
);
764 ADDR: start address of data to be dumped.
765 WORD-FORMAT: a char indicating format for the ``word''. See
767 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
768 NR_ROW: Number of rows.
769 NR_COL: The number of colums (words per row).
770 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
771 ASCHAR for unprintable characters.
773 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
774 displayes them. Returns:
776 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
779 The number of bytes read is SIZE*ROW*COL. */
782 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
784 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
790 struct type
*word_type
;
803 static struct mi_opt opts
[] =
805 {"o", OFFSET_OPT
, 1},
811 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
815 switch ((enum opt
) opt
)
818 offset
= atol (optarg
);
825 if (argc
< 5 || argc
> 6)
827 xasprintf (&mi_error_message
,
828 "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
832 /* Extract all the arguments. */
834 /* Start address of the memory dump. */
835 addr
= parse_and_eval_address (argv
[0]) + offset
;
836 /* The format character to use when displaying a memory word. See
837 the ``x'' command. */
838 word_format
= argv
[1][0];
839 /* The size of the memory word. */
840 word_size
= atol (argv
[2]);
844 word_type
= builtin_type_int8
;
848 word_type
= builtin_type_int16
;
852 word_type
= builtin_type_int32
;
856 word_type
= builtin_type_int64
;
860 word_type
= builtin_type_int8
;
863 /* The number of rows */
864 nr_rows
= atol (argv
[3]);
867 xasprintf (&mi_error_message
,
868 "mi_cmd_data_read_memory: invalid number of rows.");
871 /* number of bytes per row. */
872 nr_cols
= atol (argv
[4]);
875 xasprintf (&mi_error_message
,
876 "mi_cmd_data_read_memory: invalid number of columns.");
878 /* The un-printable character when printing ascii. */
884 /* create a buffer and read it in. */
885 total_bytes
= word_size
* nr_rows
* nr_cols
;
886 mbuf
= xcalloc (total_bytes
, 1);
887 make_cleanup (xfree
, mbuf
);
890 xasprintf (&mi_error_message
,
891 "mi_cmd_data_read_memory: out of memory.");
895 while (nr_bytes
< total_bytes
)
898 long num
= target_read_memory_partial (addr
+ nr_bytes
, mbuf
+ nr_bytes
,
899 total_bytes
- nr_bytes
,
906 /* output the header information. */
907 ui_out_field_core_addr (uiout
, "addr", addr
);
908 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
909 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
910 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
911 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
912 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
913 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
915 /* Build the result as a two dimentional table. */
917 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
918 struct cleanup
*cleanup_list_memory
;
921 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
922 for (row
= 0, row_byte
= 0;
924 row
++, row_byte
+= nr_cols
* word_size
)
928 struct cleanup
*cleanup_tuple
;
929 struct cleanup
*cleanup_list_data
;
930 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
931 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
932 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
933 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
934 for (col
= 0, col_byte
= row_byte
;
936 col
++, col_byte
+= word_size
)
938 if (col_byte
+ word_size
> nr_bytes
)
940 ui_out_field_string (uiout
, NULL
, "N/A");
944 ui_file_rewind (stream
->stream
);
945 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
946 word_asize
, stream
->stream
);
947 ui_out_field_stream (uiout
, NULL
, stream
);
950 do_cleanups (cleanup_list_data
);
954 ui_file_rewind (stream
->stream
);
955 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
957 if (byte
>= nr_bytes
)
959 fputc_unfiltered ('X', stream
->stream
);
961 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
963 fputc_unfiltered (aschar
, stream
->stream
);
966 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
968 ui_out_field_stream (uiout
, "ascii", stream
);
970 do_cleanups (cleanup_tuple
);
972 ui_out_stream_delete (stream
);
973 do_cleanups (cleanup_list_memory
);
975 do_cleanups (cleanups
);
979 /* DATA-MEMORY-WRITE:
981 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
982 offset from the beginning of the memory grid row where the cell to
984 ADDR: start address of the row in the memory grid where the memory
985 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
986 the location to write to.
987 FORMAT: a char indicating format for the ``word''. See
989 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
990 VALUE: value to be written into the memory address.
992 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
996 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
1001 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1002 enough when using a compiler other than GCC. */
1005 struct cleanup
*old_chain
;
1013 static struct mi_opt opts
[] =
1015 {"o", OFFSET_OPT
, 1},
1021 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
1025 switch ((enum opt
) opt
)
1028 offset
= atol (optarg
);
1037 xasprintf (&mi_error_message
,
1038 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1039 return MI_CMD_ERROR
;
1042 /* Extract all the arguments. */
1043 /* Start address of the memory dump. */
1044 addr
= parse_and_eval_address (argv
[0]);
1045 /* The format character to use when displaying a memory word. See
1046 the ``x'' command. */
1047 word_format
= argv
[1][0];
1048 /* The size of the memory word. */
1049 word_size
= atol (argv
[2]);
1051 /* Calculate the real address of the write destination. */
1052 addr
+= (offset
* word_size
);
1054 /* Get the value as a number */
1055 value
= parse_and_eval_address (argv
[3]);
1056 /* Get the value into an array */
1057 buffer
= xmalloc (word_size
);
1058 old_chain
= make_cleanup (xfree
, buffer
);
1059 store_signed_integer (buffer
, word_size
, value
);
1060 /* Write it down to memory */
1061 write_memory (addr
, buffer
, word_size
);
1062 /* Free the buffer. */
1063 do_cleanups (old_chain
);
1068 /* Execute a command within a safe environment.
1069 Return <0 for error; >=0 for ok.
1071 args->action will tell mi_execute_command what action
1072 to perfrom after the given command has executed (display/supress
1073 prompt, display error). */
1076 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1078 struct captured_mi_execute_command_args
*args
=
1079 (struct captured_mi_execute_command_args
*) data
;
1080 struct mi_parse
*context
= args
->command
;
1082 switch (context
->op
)
1086 /* A MI command was read from the input stream */
1088 /* FIXME: gdb_???? */
1089 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1090 context
->token
, context
->command
, context
->args
);
1091 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1092 condition expression, each function should return an
1093 indication of what action is required and then switch on
1095 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1096 args
->rc
= mi_cmd_execute (context
);
1098 if (!target_can_async_p () || !target_executing
)
1100 /* print the result if there were no errors */
1101 if (args
->rc
== MI_CMD_DONE
)
1103 fputs_unfiltered (context
->token
, raw_stdout
);
1104 fputs_unfiltered ("^done", raw_stdout
);
1105 mi_out_put (uiout
, raw_stdout
);
1106 mi_out_rewind (uiout
);
1107 fputs_unfiltered ("\n", raw_stdout
);
1109 else if (args
->rc
== MI_CMD_ERROR
)
1111 if (mi_error_message
)
1113 fputs_unfiltered (context
->token
, raw_stdout
);
1114 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1115 fputstr_unfiltered (mi_error_message
, '"', raw_stdout
);
1116 xfree (mi_error_message
);
1117 fputs_unfiltered ("\"\n", raw_stdout
);
1119 mi_out_rewind (uiout
);
1121 else if (args
->rc
== MI_CMD_CAUGHT_ERROR
)
1123 mi_out_rewind (uiout
);
1124 args
->action
= EXECUTE_COMMAND_DISPLAY_ERROR
;
1128 mi_out_rewind (uiout
);
1130 else if (sync_execution
)
1132 /* Don't print the prompt. We are executing the target in
1133 synchronous mode. */
1134 args
->action
= EXECUTE_COMMAND_SUPRESS_PROMPT
;
1140 /* A CLI command was read from the input stream */
1141 /* This will be removed as soon as we have a complete set of
1143 /* echo the command on the console. */
1144 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1145 /* FIXME: If the command string has something that looks like
1146 a format spec (e.g. %s) we will get a core dump */
1147 mi_execute_cli_command ("%s", context
->command
);
1148 /* print the result */
1149 /* FIXME: Check for errors here. */
1150 fputs_unfiltered (context
->token
, raw_stdout
);
1151 fputs_unfiltered ("^done", raw_stdout
);
1152 mi_out_put (uiout
, raw_stdout
);
1153 mi_out_rewind (uiout
);
1154 fputs_unfiltered ("\n", raw_stdout
);
1155 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1156 args
->rc
= MI_CMD_DONE
;
1166 mi_execute_command (char *cmd
, int from_tty
)
1168 struct mi_parse
*command
;
1169 struct captured_mi_execute_command_args args
;
1170 struct ui_out
*saved_uiout
= uiout
;
1173 /* This is to handle EOF (^D). We just quit gdb. */
1174 /* FIXME: we should call some API function here. */
1176 quit_force (NULL
, from_tty
);
1178 command
= mi_parse (cmd
);
1180 if (command
!= NULL
)
1182 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
1183 be pushed even further down or even eliminated? */
1184 args
.command
= command
;
1185 result
= catch_exceptions (uiout
, captured_mi_execute_command
, &args
, "",
1188 if (args
.action
== EXECUTE_COMMAND_SUPRESS_PROMPT
)
1190 /* The command is executing synchronously. Bail out early
1191 suppressing the finished prompt. */
1192 mi_parse_free (command
);
1195 if (args
.action
== EXECUTE_COMMAND_DISPLAY_ERROR
|| result
< 0)
1197 char *msg
= error_last_message ();
1198 struct cleanup
*cleanup
= make_cleanup (xfree
, msg
);
1199 /* The command execution failed and error() was called
1201 fputs_unfiltered (command
->token
, raw_stdout
);
1202 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1203 fputstr_unfiltered (msg
, '"', raw_stdout
);
1204 fputs_unfiltered ("\"\n", raw_stdout
);
1206 mi_parse_free (command
);
1209 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1210 gdb_flush (raw_stdout
);
1211 /* print any buffered hook code */
1215 static enum mi_cmd_result
1216 mi_cmd_execute (struct mi_parse
*parse
)
1218 if (parse
->cmd
->argv_func
!= NULL
1219 || parse
->cmd
->args_func
!= NULL
)
1221 /* FIXME: We need to save the token because the command executed
1222 may be asynchronous and need to print the token again.
1223 In the future we can pass the token down to the func
1224 and get rid of the last_async_command */
1225 /* The problem here is to keep the token around when we launch
1226 the target, and we want to interrupt it later on. The
1227 interrupt command will have its own token, but when the
1228 target stops, we must display the token corresponding to the
1229 last execution command given. So we have another string where
1230 we copy the token (previous_async_command), if this was
1231 indeed the token of an execution command, and when we stop we
1232 print that one. This is possible because the interrupt
1233 command, when over, will copy that token back into the
1234 default token string (last_async_command). */
1236 if (target_executing
)
1238 if (!previous_async_command
)
1239 previous_async_command
= xstrdup (last_async_command
);
1240 if (strcmp (parse
->command
, "exec-interrupt"))
1242 fputs_unfiltered (parse
->token
, raw_stdout
);
1243 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1244 fputs_unfiltered ("Cannot execute command ", raw_stdout
);
1245 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1246 fputs_unfiltered (" while target running", raw_stdout
);
1247 fputs_unfiltered ("\"\n", raw_stdout
);
1248 return MI_CMD_ERROR
;
1251 last_async_command
= xstrdup (parse
->token
);
1252 make_exec_cleanup (free_current_contents
, &last_async_command
);
1253 /* FIXME: DELETE THIS! */
1254 if (parse
->cmd
->args_func
!= NULL
)
1255 return parse
->cmd
->args_func (parse
->args
, 0 /*from_tty */ );
1256 return parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1258 else if (parse
->cmd
->cli
!= 0)
1260 /* FIXME: DELETE THIS. */
1261 /* The operation is still implemented by a cli command */
1262 /* Must be a synchronous one */
1263 mi_execute_cli_command (parse
->cmd
->cli
, parse
->args
);
1268 /* FIXME: DELETE THIS. */
1269 fputs_unfiltered (parse
->token
, raw_stdout
);
1270 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1271 fputs_unfiltered ("Undefined mi command: ", raw_stdout
);
1272 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1273 fputs_unfiltered (" (missing implementation)", raw_stdout
);
1274 fputs_unfiltered ("\"\n", raw_stdout
);
1275 return MI_CMD_ERROR
;
1280 mi_execute_command_wrapper (char *cmd
)
1282 mi_execute_command (cmd
, stdin
== instream
);
1285 /* FIXME: This is just a hack so we can get some extra commands going.
1286 We don't want to channel things through the CLI, but call libgdb directly */
1287 /* Use only for synchronous commands */
1290 mi_execute_cli_command (const char *cli
, char *args
)
1294 struct cleanup
*old_cleanups
;
1296 xasprintf (&run
, cli
, args
);
1298 /* FIXME: gdb_???? */
1299 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1301 old_cleanups
= make_cleanup (xfree
, run
);
1302 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1303 do_cleanups (old_cleanups
);
1309 mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
)
1311 struct cleanup
*old_cleanups
;
1315 if (target_can_async_p ())
1317 async_args
= (char *) xmalloc (strlen (args
) + 2);
1318 make_exec_cleanup (free
, async_args
);
1319 strcpy (async_args
, args
);
1320 strcat (async_args
, "&");
1321 xasprintf (&run
, "%s %s", mi
, async_args
);
1322 make_exec_cleanup (free
, run
);
1323 add_continuation (mi_exec_async_cli_cmd_continuation
, NULL
);
1324 old_cleanups
= NULL
;
1328 xasprintf (&run
, "%s %s", mi
, args
);
1329 old_cleanups
= make_cleanup (xfree
, run
);
1332 if (!target_can_async_p ())
1334 /* NOTE: For synchronous targets asynchronous behavour is faked by
1335 printing out the GDB prompt before we even try to execute the
1337 if (last_async_command
)
1338 fputs_unfiltered (last_async_command
, raw_stdout
);
1339 fputs_unfiltered ("^running\n", raw_stdout
);
1340 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1341 gdb_flush (raw_stdout
);
1345 /* FIXME: cagney/1999-11-29: Printing this message before
1346 calling execute_command is wrong. It should only be printed
1347 once gdb has confirmed that it really has managed to send a
1348 run command to the target. */
1349 if (last_async_command
)
1350 fputs_unfiltered (last_async_command
, raw_stdout
);
1351 fputs_unfiltered ("^running\n", raw_stdout
);
1354 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1356 if (!target_can_async_p ())
1358 /* Do this before doing any printing. It would appear that some
1359 print code leaves garbage around in the buffer. */
1360 do_cleanups (old_cleanups
);
1361 /* If the target was doing the operation synchronously we fake
1362 the stopped message. */
1363 if (last_async_command
)
1364 fputs_unfiltered (last_async_command
, raw_stdout
);
1365 fputs_unfiltered ("*stopped", raw_stdout
);
1366 mi_out_put (uiout
, raw_stdout
);
1367 mi_out_rewind (uiout
);
1368 fputs_unfiltered ("\n", raw_stdout
);
1369 return MI_CMD_QUIET
;
1375 mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
)
1377 if (last_async_command
)
1378 fputs_unfiltered (last_async_command
, raw_stdout
);
1379 fputs_unfiltered ("*stopped", raw_stdout
);
1380 mi_out_put (uiout
, raw_stdout
);
1381 fputs_unfiltered ("\n", raw_stdout
);
1382 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1383 gdb_flush (raw_stdout
);
1384 do_exec_cleanups (ALL_CLEANUPS
);
1388 mi_input (char *buf
)
1390 return gdb_readline (NULL
);
1394 mi_load_progress (const char *section_name
,
1395 unsigned long sent_so_far
,
1396 unsigned long total_section
,
1397 unsigned long total_sent
,
1398 unsigned long grand_total
)
1400 struct timeval time_now
, delta
, update_threshold
;
1401 static struct timeval last_update
;
1402 static char *previous_sect_name
= NULL
;
1405 if (!interpreter_p
|| strncmp (interpreter_p
, "mi", 2) != 0)
1408 update_threshold
.tv_sec
= 0;
1409 update_threshold
.tv_usec
= 500000;
1410 gettimeofday (&time_now
, NULL
);
1412 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1413 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1415 if (delta
.tv_usec
< 0)
1418 delta
.tv_usec
+= 1000000;
1421 new_section
= (previous_sect_name
?
1422 strcmp (previous_sect_name
, section_name
) : 1);
1425 struct cleanup
*cleanup_tuple
;
1426 xfree (previous_sect_name
);
1427 previous_sect_name
= xstrdup (section_name
);
1429 if (last_async_command
)
1430 fputs_unfiltered (last_async_command
, raw_stdout
);
1431 fputs_unfiltered ("+download", raw_stdout
);
1432 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1433 ui_out_field_string (uiout
, "section", section_name
);
1434 ui_out_field_int (uiout
, "section-size", total_section
);
1435 ui_out_field_int (uiout
, "total-size", grand_total
);
1436 do_cleanups (cleanup_tuple
);
1437 mi_out_put (uiout
, raw_stdout
);
1438 fputs_unfiltered ("\n", raw_stdout
);
1439 gdb_flush (raw_stdout
);
1442 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1443 delta
.tv_usec
>= update_threshold
.tv_usec
)
1445 struct cleanup
*cleanup_tuple
;
1446 last_update
.tv_sec
= time_now
.tv_sec
;
1447 last_update
.tv_usec
= time_now
.tv_usec
;
1448 if (last_async_command
)
1449 fputs_unfiltered (last_async_command
, raw_stdout
);
1450 fputs_unfiltered ("+download", raw_stdout
);
1451 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1452 ui_out_field_string (uiout
, "section", section_name
);
1453 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1454 ui_out_field_int (uiout
, "section-size", total_section
);
1455 ui_out_field_int (uiout
, "total-sent", total_sent
);
1456 ui_out_field_int (uiout
, "total-size", grand_total
);
1457 do_cleanups (cleanup_tuple
);
1458 mi_out_put (uiout
, raw_stdout
);
1459 fputs_unfiltered ("\n", raw_stdout
);
1460 gdb_flush (raw_stdout
);
1465 mi_command_loop (int mi_version
)
1467 if (mi_version
<= 1)
1469 /* HACK: Force stdout/stderr to point at the console. This avoids
1470 any potential side effects caused by legacy code that is still
1471 using the TUI / fputs_unfiltered_hook */
1472 raw_stdout
= stdio_fileopen (stdout
);
1473 /* Route normal output through the MIx */
1474 gdb_stdout
= mi_console_file_new (raw_stdout
, "~");
1477 /* Route error and log output through the MI */
1478 gdb_stderr
= mi_console_file_new (raw_stdout
, "&");
1479 gdb_stdlog
= gdb_stderr
;
1480 /* Route target output through the MI. */
1481 gdb_stdtarg
= mi_console_file_new (raw_stdout
, "@");
1483 /* HACK: Poke the ui_out table directly. Should we be creating a
1484 mi_out object wired up to the above gdb_stdout / gdb_stderr? */
1485 uiout
= mi_out_new (mi_version
);
1487 /* HACK: Override any other interpreter hooks. We need to create a
1488 real event table and pass in that. */
1490 /* command_loop_hook = 0; */
1491 print_frame_info_listing_hook
= 0;
1494 create_breakpoint_hook
= 0;
1495 delete_breakpoint_hook
= 0;
1496 modify_breakpoint_hook
= 0;
1497 interactive_hook
= 0;
1498 registers_changed_hook
= 0;
1499 readline_begin_hook
= 0;
1501 readline_end_hook
= 0;
1502 register_changed_hook
= 0;
1503 memory_changed_hook
= 0;
1505 target_wait_hook
= 0;
1506 call_command_hook
= 0;
1508 error_begin_hook
= 0;
1509 show_load_progress
= mi_load_progress
;
1511 /* Turn off 8 bit strings in quoted output. Any character with the
1512 high bit set is printed using C's octal format. */
1513 sevenbit_strings
= 1;
1515 /* Tell the world that we're alive */
1516 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1517 gdb_flush (raw_stdout
);
1520 simplified_command_loop (mi_input
, mi_execute_command
);
1522 start_event_loop ();
1526 mi1_command_loop (void)
1528 mi_command_loop (1);
1532 mi2_command_loop (void)
1534 mi_command_loop (2);
1538 setup_architecture_data (void)
1540 /* don't trust REGISTER_BYTES to be zero. */
1541 old_regs
= xmalloc (REGISTER_BYTES
+ 1);
1542 memset (old_regs
, 0, REGISTER_BYTES
+ 1);
1546 mi_init_ui (char *arg0
)
1548 if (strlen (interpreter_p
) <= 2 ||
1549 interpreter_p
[2] > '1')
1551 /* HACK: Force stdout/stderr to point at the console. This avoids
1552 any potential side effects caused by legacy code that is still
1553 using the TUI / fputs_unfiltered_hook */
1554 raw_stdout
= stdio_fileopen (stdout
);
1555 /* Route normal output through the MIx */
1556 gdb_stdout
= mi_console_file_new (raw_stdout
, "~");
1561 _initialize_mi_main (void)
1563 if (interpreter_p
== NULL
)
1566 /* If we're _the_ interpreter, take control. */
1567 if (strcmp (interpreter_p
, "mi") == 0)
1568 command_loop_hook
= mi2_command_loop
;
1569 else if (strcmp (interpreter_p
, "mi1") == 0)
1570 command_loop_hook
= mi1_command_loop
;
1571 else if (strcmp (interpreter_p
, "mi2") == 0)
1572 command_loop_hook
= mi2_command_loop
;
1576 init_ui_hook
= mi_init_ui
;
1577 setup_architecture_data ();
1578 register_gdbarch_swap (&old_regs
, sizeof (old_regs
), NULL
);
1579 register_gdbarch_swap (NULL
, 0, setup_architecture_data
);
1582 /* These overwrite some of the initialization done in
1583 _intialize_event_loop. */
1584 call_readline
= gdb_readline2
;
1585 input_handler
= mi_execute_command_wrapper
;
1586 add_file_handler (input_fd
, stdin_event_handler
, 0);
1587 async_command_editing_p
= 0;
1589 /* FIXME: Should we notify main that we are here as a possible