2 Copyright 2000, 2001, 2002, 2003 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"
37 #include "event-loop.h"
38 #include "event-top.h"
39 #include "gdbcore.h" /* for write_memory() */
40 #include "value.h" /* for deprecated_write_register_bytes() */
53 /* Enumerations of the actions that may result from calling
54 captured_mi_execute_command */
56 enum captured_mi_execute_command_actions
58 EXECUTE_COMMAND_DISPLAY_PROMPT
,
59 EXECUTE_COMMAND_SUPRESS_PROMPT
,
60 EXECUTE_COMMAND_DISPLAY_ERROR
63 /* This structure is used to pass information from captured_mi_execute_command
64 to mi_execute_command. */
65 struct captured_mi_execute_command_args
67 /* This return result of the MI command (output) */
68 enum mi_cmd_result rc
;
70 /* What action to perform when the call is finished (output) */
71 enum captured_mi_execute_command_actions action
;
73 /* The command context to be executed (input) */
74 struct mi_parse
*command
;
78 struct ui_file
*raw_stdout
;
80 /* The token of the last asynchronous command */
81 static char *last_async_command
;
82 static char *previous_async_command
;
83 char *mi_error_message
;
84 static char *old_regs
;
86 extern void _initialize_mi_main (void);
87 static enum mi_cmd_result
mi_cmd_execute (struct mi_parse
*parse
);
89 static void mi_execute_cli_command (const char *cli
, char *args
);
90 static enum mi_cmd_result
mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
);
92 static 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
);
97 /* A helper function which will set mi_error_message to
98 error_last_message. */
100 mi_error_last_message (void)
102 char *s
= error_last_message ();
103 xasprintf (&mi_error_message
, s
);
107 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
108 layer that calls libgdb. Any operation used in the below should be
112 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
114 /* We have to print everything right here because we never return */
115 if (last_async_command
)
116 fputs_unfiltered (last_async_command
, raw_stdout
);
117 fputs_unfiltered ("^exit\n", raw_stdout
);
118 mi_out_put (uiout
, raw_stdout
);
119 /* FIXME: The function called is not yet a formal libgdb function */
120 quit_force (NULL
, FROM_TTY
);
125 mi_cmd_exec_run (char *args
, int from_tty
)
127 /* FIXME: Should call a libgdb function, not a cli wrapper */
128 return mi_execute_async_cli_command ("run", args
, from_tty
);
132 mi_cmd_exec_next (char *args
, int from_tty
)
134 /* FIXME: Should call a libgdb function, not a cli wrapper */
135 return mi_execute_async_cli_command ("next", args
, from_tty
);
139 mi_cmd_exec_next_instruction (char *args
, int from_tty
)
141 /* FIXME: Should call a libgdb function, not a cli wrapper */
142 return mi_execute_async_cli_command ("nexti", args
, from_tty
);
146 mi_cmd_exec_step (char *args
, int from_tty
)
148 /* FIXME: Should call a libgdb function, not a cli wrapper */
149 return mi_execute_async_cli_command ("step", args
, from_tty
);
153 mi_cmd_exec_step_instruction (char *args
, int from_tty
)
155 /* FIXME: Should call a libgdb function, not a cli wrapper */
156 return mi_execute_async_cli_command ("stepi", args
, from_tty
);
160 mi_cmd_exec_finish (char *args
, int from_tty
)
162 /* FIXME: Should call a libgdb function, not a cli wrapper */
163 return mi_execute_async_cli_command ("finish", args
, from_tty
);
167 mi_cmd_exec_until (char *args
, int from_tty
)
169 /* FIXME: Should call a libgdb function, not a cli wrapper */
170 return mi_execute_async_cli_command ("until", args
, from_tty
);
174 mi_cmd_exec_return (char *args
, int from_tty
)
176 /* This command doesn't really execute the target, it just pops the
177 specified number of frames. */
179 /* Call return_command with from_tty argument equal to 0 so as to
180 avoid being queried. */
181 return_command (args
, 0);
183 /* Call return_command with from_tty argument equal to 0 so as to
184 avoid being queried. */
185 return_command (NULL
, 0);
187 /* Because we have called return_command with from_tty = 0, we need
188 to print the frame here. */
189 print_stack_frame (deprecated_selected_frame
,
190 frame_relative_level (deprecated_selected_frame
),
197 mi_cmd_exec_continue (char *args
, int from_tty
)
199 /* FIXME: Should call a libgdb function, not a cli wrapper */
200 return mi_execute_async_cli_command ("continue", args
, from_tty
);
203 /* Interrupt the execution of the target. Note how we must play around
204 with the token varialbes, in order to display the current token in
205 the result of the interrupt command, and the previous execution
206 token when the target finally stops. See comments in
209 mi_cmd_exec_interrupt (char *args
, int from_tty
)
211 if (!target_executing
)
213 xasprintf (&mi_error_message
,
214 "mi_cmd_exec_interrupt: Inferior not executing.");
217 interrupt_target_command (args
, from_tty
);
218 if (last_async_command
)
219 fputs_unfiltered (last_async_command
, raw_stdout
);
220 fputs_unfiltered ("^done", raw_stdout
);
221 xfree (last_async_command
);
222 if (previous_async_command
)
223 last_async_command
= xstrdup (previous_async_command
);
224 xfree (previous_async_command
);
225 previous_async_command
= NULL
;
226 mi_out_put (uiout
, raw_stdout
);
227 mi_out_rewind (uiout
);
228 fputs_unfiltered ("\n", raw_stdout
);
233 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
239 xasprintf (&mi_error_message
,
240 "mi_cmd_thread_select: USAGE: threadnum.");
244 rc
= gdb_thread_select (uiout
, argv
[0]);
246 /* RC is enum gdb_rc if it is successful (>=0)
247 enum return_reason if not (<0). */
248 if ((int) rc
< 0 && (enum return_reason
) rc
== RETURN_ERROR
)
249 return MI_CMD_CAUGHT_ERROR
;
250 else if ((int) rc
>= 0 && rc
== GDB_RC_FAIL
)
257 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
259 enum gdb_rc rc
= MI_CMD_DONE
;
263 xasprintf (&mi_error_message
,
264 "mi_cmd_thread_list_ids: No arguments required.");
268 rc
= gdb_list_thread_ids (uiout
);
270 if (rc
== GDB_RC_FAIL
)
271 return MI_CMD_CAUGHT_ERROR
;
277 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
281 struct cleanup
*cleanup
;
283 /* Note that the test for a valid register must include checking the
284 REGISTER_NAME because NUM_REGS may be allocated for the union of
285 the register sets within a family of related processors. In this
286 case, some entries of REGISTER_NAME will change depending upon
287 the particular processor being debugged. */
289 numregs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
291 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
293 if (argc
== 0) /* No args, just do all the regs */
299 if (REGISTER_NAME (regnum
) == NULL
300 || *(REGISTER_NAME (regnum
)) == '\0')
301 ui_out_field_string (uiout
, NULL
, "");
303 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
307 /* Else, list of register #s, just do listed regs */
308 for (i
= 0; i
< argc
; i
++)
310 regnum
= atoi (argv
[i
]);
311 if (regnum
< 0 || regnum
>= numregs
)
313 do_cleanups (cleanup
);
314 xasprintf (&mi_error_message
, "bad register number");
317 if (REGISTER_NAME (regnum
) == NULL
318 || *(REGISTER_NAME (regnum
)) == '\0')
319 ui_out_field_string (uiout
, NULL
, "");
321 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
323 do_cleanups (cleanup
);
328 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
330 int regnum
, numregs
, changed
;
332 struct cleanup
*cleanup
;
334 /* Note that the test for a valid register must include checking the
335 REGISTER_NAME because NUM_REGS may be allocated for the union of
336 the register sets within a family of related processors. In this
337 case, some entries of REGISTER_NAME will change depending upon
338 the particular processor being debugged. */
342 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
344 if (argc
== 0) /* No args, just do all the regs */
350 if (REGISTER_NAME (regnum
) == NULL
351 || *(REGISTER_NAME (regnum
)) == '\0')
353 changed
= register_changed_p (regnum
);
356 do_cleanups (cleanup
);
357 xasprintf (&mi_error_message
,
358 "mi_cmd_data_list_changed_registers: Unable to read register contents.");
362 ui_out_field_int (uiout
, NULL
, regnum
);
366 /* Else, list of register #s, just do listed regs */
367 for (i
= 0; i
< argc
; i
++)
369 regnum
= atoi (argv
[i
]);
373 && REGISTER_NAME (regnum
) != NULL
374 && *REGISTER_NAME (regnum
) != '\000')
376 changed
= register_changed_p (regnum
);
379 do_cleanups (cleanup
);
380 xasprintf (&mi_error_message
,
381 "mi_cmd_data_list_register_change: Unable to read register contents.");
385 ui_out_field_int (uiout
, NULL
, regnum
);
389 do_cleanups (cleanup
);
390 xasprintf (&mi_error_message
, "bad register number");
394 do_cleanups (cleanup
);
399 register_changed_p (int regnum
)
401 char *raw_buffer
= alloca (MAX_REGISTER_RAW_SIZE
);
403 if (! frame_register_read (deprecated_selected_frame
, regnum
, raw_buffer
))
406 if (memcmp (&old_regs
[REGISTER_BYTE (regnum
)], raw_buffer
,
407 REGISTER_RAW_SIZE (regnum
)) == 0)
410 /* Found a changed register. Return 1. */
412 memcpy (&old_regs
[REGISTER_BYTE (regnum
)], raw_buffer
,
413 REGISTER_RAW_SIZE (regnum
));
418 /* Return a list of register number and value pairs. The valid
419 arguments expected are: a letter indicating the format in which to
420 display the registers contents. This can be one of: x (hexadecimal), d
421 (decimal), N (natural), t (binary), o (octal), r (raw). After the
422 format argumetn there can be a sequence of numbers, indicating which
423 registers to fetch the content of. If the format is the only argument,
424 a list of all the registers with their values is returned. */
426 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
428 int regnum
, numregs
, format
, result
;
430 struct cleanup
*list_cleanup
, *tuple_cleanup
;
432 /* Note that the test for a valid register must include checking the
433 REGISTER_NAME because NUM_REGS may be allocated for the union of
434 the register sets within a family of related processors. In this
435 case, some entries of REGISTER_NAME will change depending upon
436 the particular processor being debugged. */
442 xasprintf (&mi_error_message
,
443 "mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
447 format
= (int) argv
[0][0];
449 if (!target_has_registers
)
451 xasprintf (&mi_error_message
,
452 "mi_cmd_data_list_register_values: No registers.");
456 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
458 if (argc
== 1) /* No args, beside the format: do all the regs */
464 if (REGISTER_NAME (regnum
) == NULL
465 || *(REGISTER_NAME (regnum
)) == '\0')
467 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
468 ui_out_field_int (uiout
, "number", regnum
);
469 result
= get_register (regnum
, format
);
472 do_cleanups (list_cleanup
);
475 do_cleanups (tuple_cleanup
);
479 /* Else, list of register #s, just do listed regs */
480 for (i
= 1; i
< argc
; i
++)
482 regnum
= atoi (argv
[i
]);
486 && REGISTER_NAME (regnum
) != NULL
487 && *REGISTER_NAME (regnum
) != '\000')
489 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
490 ui_out_field_int (uiout
, "number", regnum
);
491 result
= get_register (regnum
, format
);
494 do_cleanups (list_cleanup
);
497 do_cleanups (tuple_cleanup
);
501 do_cleanups (list_cleanup
);
502 xasprintf (&mi_error_message
, "bad register number");
506 do_cleanups (list_cleanup
);
510 /* Output one register's contents in the desired format. */
512 get_register (int regnum
, int format
)
514 char *raw_buffer
= alloca (MAX_REGISTER_RAW_SIZE
);
515 char *virtual_buffer
= alloca (MAX_REGISTER_VIRTUAL_SIZE
);
517 static struct ui_stream
*stb
= NULL
;
519 stb
= ui_out_stream_new (uiout
);
524 get_saved_register (raw_buffer
, &optim
, (CORE_ADDR
*) NULL
,
525 deprecated_selected_frame
,
526 regnum
, (enum lval_type
*) NULL
);
529 xasprintf (&mi_error_message
, "Optimized out");
533 /* Convert raw data to virtual format if necessary. */
535 if (REGISTER_CONVERTIBLE (regnum
))
537 REGISTER_CONVERT_TO_VIRTUAL (regnum
, REGISTER_VIRTUAL_TYPE (regnum
),
538 raw_buffer
, virtual_buffer
);
541 memcpy (virtual_buffer
, raw_buffer
, REGISTER_VIRTUAL_SIZE (regnum
));
546 char *ptr
, buf
[1024];
550 for (j
= 0; j
< REGISTER_RAW_SIZE (regnum
); j
++)
552 register int idx
= TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
? j
553 : REGISTER_RAW_SIZE (regnum
) - 1 - j
;
554 sprintf (ptr
, "%02x", (unsigned char) raw_buffer
[idx
]);
557 ui_out_field_string (uiout
, "value", buf
);
558 /*fputs_filtered (buf, gdb_stdout); */
562 val_print (REGISTER_VIRTUAL_TYPE (regnum
), virtual_buffer
, 0, 0,
563 stb
->stream
, format
, 1, 0, Val_pretty_default
);
564 ui_out_field_stream (uiout
, "value", stb
);
565 ui_out_stream_delete (stb
);
570 /* Write given values into registers. The registers and values are
571 given as pairs. The corresponding MI command is
572 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
574 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
582 /* Note that the test for a valid register must include checking the
583 REGISTER_NAME because NUM_REGS may be allocated for the union of
584 the register sets within a family of related processors. In this
585 case, some entries of REGISTER_NAME will change depending upon
586 the particular processor being debugged. */
592 xasprintf (&mi_error_message
,
593 "mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
597 format
= (int) argv
[0][0];
599 if (!target_has_registers
)
601 xasprintf (&mi_error_message
,
602 "mi_cmd_data_write_register_values: No registers.");
608 xasprintf (&mi_error_message
,
609 "mi_cmd_data_write_register_values: No regs and values specified.");
615 xasprintf (&mi_error_message
,
616 "mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
620 for (i
= 1; i
< argc
; i
= i
+ 2)
622 regnum
= atoi (argv
[i
]);
626 && REGISTER_NAME (regnum
) != NULL
627 && *REGISTER_NAME (regnum
) != '\000')
630 struct cleanup
*old_chain
;
632 /* Get the value as a number */
633 value
= parse_and_eval_address (argv
[i
+ 1]);
634 /* Get the value into an array */
635 buffer
= xmalloc (REGISTER_SIZE
);
636 old_chain
= make_cleanup (xfree
, buffer
);
637 store_signed_integer (buffer
, REGISTER_SIZE
, value
);
639 deprecated_write_register_bytes (REGISTER_BYTE (regnum
), buffer
, REGISTER_RAW_SIZE (regnum
));
640 /* Free the buffer. */
641 do_cleanups (old_chain
);
645 xasprintf (&mi_error_message
, "bad register number");
653 /*This is commented out because we decided it was not useful. I leave
654 it, just in case. ezannoni:1999-12-08 */
656 /* Assign a value to a variable. The expression argument must be in
657 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
660 mi_cmd_data_assign (char *command
, char **argv
, int argc
)
662 struct expression
*expr
;
663 struct cleanup
*old_chain
;
667 xasprintf (&mi_error_message
,
668 "mi_cmd_data_assign: Usage: -data-assign expression");
672 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
673 01-12-1999: Need to decide what to do with this for libgdb purposes. */
675 expr
= parse_expression (argv
[0]);
676 old_chain
= make_cleanup (free_current_contents
, &expr
);
677 evaluate_expression (expr
);
678 do_cleanups (old_chain
);
683 /* Evaluate the value of the argument. The argument is an
684 expression. If the expression contains spaces it needs to be
685 included in double quotes. */
687 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
689 struct expression
*expr
;
690 struct cleanup
*old_chain
= NULL
;
692 struct ui_stream
*stb
= NULL
;
694 stb
= ui_out_stream_new (uiout
);
698 xasprintf (&mi_error_message
,
699 "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
703 expr
= parse_expression (argv
[0]);
705 old_chain
= make_cleanup (free_current_contents
, &expr
);
707 val
= evaluate_expression (expr
);
709 /* Print the result of the expression evaluation. */
710 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
),
711 VALUE_EMBEDDED_OFFSET (val
), VALUE_ADDRESS (val
),
712 stb
->stream
, 0, 0, 0, 0);
714 ui_out_field_stream (uiout
, "value", stb
);
715 ui_out_stream_delete (stb
);
717 do_cleanups (old_chain
);
723 mi_cmd_target_download (char *args
, int from_tty
)
726 struct cleanup
*old_cleanups
= NULL
;
728 xasprintf (&run
, "load %s", args
);
729 old_cleanups
= make_cleanup (xfree
, run
);
730 execute_command (run
, from_tty
);
732 do_cleanups (old_cleanups
);
736 /* Connect to the remote target. */
738 mi_cmd_target_select (char *args
, int from_tty
)
741 struct cleanup
*old_cleanups
= NULL
;
743 xasprintf (&run
, "target %s", args
);
744 old_cleanups
= make_cleanup (xfree
, run
);
746 /* target-select is always synchronous. once the call has returned
747 we know that we are connected. */
748 /* NOTE: At present all targets that are connected are also
749 (implicitly) talking to a halted target. In the future this may
751 execute_command (run
, from_tty
);
753 do_cleanups (old_cleanups
);
755 /* Issue the completion message here. */
756 if (last_async_command
)
757 fputs_unfiltered (last_async_command
, raw_stdout
);
758 fputs_unfiltered ("^connected", raw_stdout
);
759 mi_out_put (uiout
, raw_stdout
);
760 mi_out_rewind (uiout
);
761 fputs_unfiltered ("\n", raw_stdout
);
762 do_exec_cleanups (ALL_CLEANUPS
);
768 ADDR: start address of data to be dumped.
769 WORD-FORMAT: a char indicating format for the ``word''. See
771 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
772 NR_ROW: Number of rows.
773 NR_COL: The number of colums (words per row).
774 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
775 ASCHAR for unprintable characters.
777 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
778 displayes them. Returns:
780 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
783 The number of bytes read is SIZE*ROW*COL. */
786 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
788 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
794 struct type
*word_type
;
807 static struct mi_opt opts
[] =
809 {"o", OFFSET_OPT
, 1},
815 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
819 switch ((enum opt
) opt
)
822 offset
= atol (optarg
);
829 if (argc
< 5 || argc
> 6)
831 xasprintf (&mi_error_message
,
832 "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
836 /* Extract all the arguments. */
838 /* Start address of the memory dump. */
839 addr
= parse_and_eval_address (argv
[0]) + offset
;
840 /* The format character to use when displaying a memory word. See
841 the ``x'' command. */
842 word_format
= argv
[1][0];
843 /* The size of the memory word. */
844 word_size
= atol (argv
[2]);
848 word_type
= builtin_type_int8
;
852 word_type
= builtin_type_int16
;
856 word_type
= builtin_type_int32
;
860 word_type
= builtin_type_int64
;
864 word_type
= builtin_type_int8
;
867 /* The number of rows */
868 nr_rows
= atol (argv
[3]);
871 xasprintf (&mi_error_message
,
872 "mi_cmd_data_read_memory: invalid number of rows.");
875 /* number of bytes per row. */
876 nr_cols
= atol (argv
[4]);
879 xasprintf (&mi_error_message
,
880 "mi_cmd_data_read_memory: invalid number of columns.");
882 /* The un-printable character when printing ascii. */
888 /* create a buffer and read it in. */
889 total_bytes
= word_size
* nr_rows
* nr_cols
;
890 mbuf
= xcalloc (total_bytes
, 1);
891 make_cleanup (xfree
, mbuf
);
894 xasprintf (&mi_error_message
,
895 "mi_cmd_data_read_memory: out of memory.");
899 while (nr_bytes
< total_bytes
)
902 long num
= target_read_memory_partial (addr
+ nr_bytes
, mbuf
+ nr_bytes
,
903 total_bytes
- nr_bytes
,
910 /* output the header information. */
911 ui_out_field_core_addr (uiout
, "addr", addr
);
912 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
913 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
914 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
915 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
916 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
917 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
919 /* Build the result as a two dimentional table. */
921 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
922 struct cleanup
*cleanup_list_memory
;
925 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
926 for (row
= 0, row_byte
= 0;
928 row
++, row_byte
+= nr_cols
* word_size
)
932 struct cleanup
*cleanup_tuple
;
933 struct cleanup
*cleanup_list_data
;
934 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
935 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
936 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
937 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
938 for (col
= 0, col_byte
= row_byte
;
940 col
++, col_byte
+= word_size
)
942 if (col_byte
+ word_size
> nr_bytes
)
944 ui_out_field_string (uiout
, NULL
, "N/A");
948 ui_file_rewind (stream
->stream
);
949 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
950 word_asize
, stream
->stream
);
951 ui_out_field_stream (uiout
, NULL
, stream
);
954 do_cleanups (cleanup_list_data
);
958 ui_file_rewind (stream
->stream
);
959 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
961 if (byte
>= nr_bytes
)
963 fputc_unfiltered ('X', stream
->stream
);
965 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
967 fputc_unfiltered (aschar
, stream
->stream
);
970 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
972 ui_out_field_stream (uiout
, "ascii", stream
);
974 do_cleanups (cleanup_tuple
);
976 ui_out_stream_delete (stream
);
977 do_cleanups (cleanup_list_memory
);
979 do_cleanups (cleanups
);
983 /* DATA-MEMORY-WRITE:
985 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
986 offset from the beginning of the memory grid row where the cell to
988 ADDR: start address of the row in the memory grid where the memory
989 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
990 the location to write to.
991 FORMAT: a char indicating format for the ``word''. See
993 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
994 VALUE: value to be written into the memory address.
996 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1000 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
1005 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1006 enough when using a compiler other than GCC. */
1009 struct cleanup
*old_chain
;
1017 static struct mi_opt opts
[] =
1019 {"o", OFFSET_OPT
, 1},
1025 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
1029 switch ((enum opt
) opt
)
1032 offset
= atol (optarg
);
1041 xasprintf (&mi_error_message
,
1042 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1043 return MI_CMD_ERROR
;
1046 /* Extract all the arguments. */
1047 /* Start address of the memory dump. */
1048 addr
= parse_and_eval_address (argv
[0]);
1049 /* The format character to use when displaying a memory word. See
1050 the ``x'' command. */
1051 word_format
= argv
[1][0];
1052 /* The size of the memory word. */
1053 word_size
= atol (argv
[2]);
1055 /* Calculate the real address of the write destination. */
1056 addr
+= (offset
* word_size
);
1058 /* Get the value as a number */
1059 value
= parse_and_eval_address (argv
[3]);
1060 /* Get the value into an array */
1061 buffer
= xmalloc (word_size
);
1062 old_chain
= make_cleanup (xfree
, buffer
);
1063 store_signed_integer (buffer
, word_size
, value
);
1064 /* Write it down to memory */
1065 write_memory (addr
, buffer
, word_size
);
1066 /* Free the buffer. */
1067 do_cleanups (old_chain
);
1072 /* Execute a command within a safe environment.
1073 Return <0 for error; >=0 for ok.
1075 args->action will tell mi_execute_command what action
1076 to perfrom after the given command has executed (display/supress
1077 prompt, display error). */
1080 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1082 struct captured_mi_execute_command_args
*args
=
1083 (struct captured_mi_execute_command_args
*) data
;
1084 struct mi_parse
*context
= args
->command
;
1086 switch (context
->op
)
1090 /* A MI command was read from the input stream */
1092 /* FIXME: gdb_???? */
1093 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1094 context
->token
, context
->command
, context
->args
);
1095 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1096 condition expression, each function should return an
1097 indication of what action is required and then switch on
1099 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1100 args
->rc
= mi_cmd_execute (context
);
1102 if (!target_can_async_p () || !target_executing
)
1104 /* print the result if there were no errors
1106 Remember that on the way out of executing a command, you have
1107 to directly use the mi_interp's uiout, since the command could
1108 have reset the interpreter, in which case the current uiout
1109 will most likely crash in the mi_out_* routines. */
1110 if (args
->rc
== MI_CMD_DONE
)
1112 fputs_unfiltered (context
->token
, raw_stdout
);
1113 fputs_unfiltered ("^done", raw_stdout
);
1114 mi_out_put (uiout
, raw_stdout
);
1115 mi_out_rewind (uiout
);
1116 fputs_unfiltered ("\n", raw_stdout
);
1118 else if (args
->rc
== MI_CMD_ERROR
)
1120 if (mi_error_message
)
1122 fputs_unfiltered (context
->token
, raw_stdout
);
1123 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1124 fputstr_unfiltered (mi_error_message
, '"', raw_stdout
);
1125 xfree (mi_error_message
);
1126 fputs_unfiltered ("\"\n", raw_stdout
);
1128 mi_out_rewind (uiout
);
1130 else if (args
->rc
== MI_CMD_CAUGHT_ERROR
)
1132 mi_out_rewind (uiout
);
1133 args
->action
= EXECUTE_COMMAND_DISPLAY_ERROR
;
1137 mi_out_rewind (uiout
);
1139 else if (sync_execution
)
1141 /* Don't print the prompt. We are executing the target in
1142 synchronous mode. */
1143 args
->action
= EXECUTE_COMMAND_SUPRESS_PROMPT
;
1149 /* A CLI command was read from the input stream */
1150 /* This will be removed as soon as we have a complete set of
1152 /* echo the command on the console. */
1153 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1154 /* FIXME: If the command string has something that looks like
1155 a format spec (e.g. %s) we will get a core dump */
1156 mi_execute_cli_command ("%s", context
->command
);
1158 /* If we changed interpreters, DON'T print out anything. */
1159 if (current_interp_named_p (INTERP_MI
)
1160 || current_interp_named_p (INTERP_MI1
))
1162 /* print the result */
1163 /* FIXME: Check for errors here. */
1164 fputs_unfiltered (context
->token
, raw_stdout
);
1165 fputs_unfiltered ("^done", raw_stdout
);
1166 mi_out_put (uiout
, raw_stdout
);
1167 mi_out_rewind (uiout
);
1168 fputs_unfiltered ("\n", raw_stdout
);
1169 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1170 args
->rc
= MI_CMD_DONE
;
1181 mi_execute_command (char *cmd
, int from_tty
)
1183 struct mi_parse
*command
;
1184 struct captured_mi_execute_command_args args
;
1185 struct ui_out
*saved_uiout
= uiout
;
1188 /* This is to handle EOF (^D). We just quit gdb. */
1189 /* FIXME: we should call some API function here. */
1191 quit_force (NULL
, from_tty
);
1193 command
= mi_parse (cmd
);
1195 if (command
!= NULL
)
1197 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
1198 be pushed even further down or even eliminated? */
1199 args
.command
= command
;
1200 result
= catch_exceptions (uiout
, captured_mi_execute_command
, &args
, "",
1203 if (args
.action
== EXECUTE_COMMAND_SUPRESS_PROMPT
)
1205 /* The command is executing synchronously. Bail out early
1206 suppressing the finished prompt. */
1207 mi_parse_free (command
);
1210 if (args
.action
== EXECUTE_COMMAND_DISPLAY_ERROR
|| result
< 0)
1212 char *msg
= error_last_message ();
1213 struct cleanup
*cleanup
= make_cleanup (xfree
, msg
);
1214 /* The command execution failed and error() was called
1216 fputs_unfiltered (command
->token
, raw_stdout
);
1217 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1218 fputstr_unfiltered (msg
, '"', raw_stdout
);
1219 fputs_unfiltered ("\"\n", raw_stdout
);
1221 mi_parse_free (command
);
1224 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1225 gdb_flush (raw_stdout
);
1226 /* print any buffered hook code */
1230 static enum mi_cmd_result
1231 mi_cmd_execute (struct mi_parse
*parse
)
1233 if (parse
->cmd
->argv_func
!= NULL
1234 || parse
->cmd
->args_func
!= NULL
)
1236 /* FIXME: We need to save the token because the command executed
1237 may be asynchronous and need to print the token again.
1238 In the future we can pass the token down to the func
1239 and get rid of the last_async_command */
1240 /* The problem here is to keep the token around when we launch
1241 the target, and we want to interrupt it later on. The
1242 interrupt command will have its own token, but when the
1243 target stops, we must display the token corresponding to the
1244 last execution command given. So we have another string where
1245 we copy the token (previous_async_command), if this was
1246 indeed the token of an execution command, and when we stop we
1247 print that one. This is possible because the interrupt
1248 command, when over, will copy that token back into the
1249 default token string (last_async_command). */
1251 if (target_executing
)
1253 if (!previous_async_command
)
1254 previous_async_command
= xstrdup (last_async_command
);
1255 if (strcmp (parse
->command
, "exec-interrupt"))
1257 fputs_unfiltered (parse
->token
, raw_stdout
);
1258 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1259 fputs_unfiltered ("Cannot execute command ", raw_stdout
);
1260 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1261 fputs_unfiltered (" while target running", raw_stdout
);
1262 fputs_unfiltered ("\"\n", raw_stdout
);
1263 return MI_CMD_ERROR
;
1266 last_async_command
= xstrdup (parse
->token
);
1267 make_exec_cleanup (free_current_contents
, &last_async_command
);
1268 /* FIXME: DELETE THIS! */
1269 if (parse
->cmd
->args_func
!= NULL
)
1270 return parse
->cmd
->args_func (parse
->args
, 0 /*from_tty */ );
1271 return parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1273 else if (parse
->cmd
->cli
!= 0)
1275 /* FIXME: DELETE THIS. */
1276 /* The operation is still implemented by a cli command */
1277 /* Must be a synchronous one */
1278 mi_execute_cli_command (parse
->cmd
->cli
, parse
->args
);
1283 /* FIXME: DELETE THIS. */
1284 fputs_unfiltered (parse
->token
, raw_stdout
);
1285 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1286 fputs_unfiltered ("Undefined mi command: ", raw_stdout
);
1287 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1288 fputs_unfiltered (" (missing implementation)", raw_stdout
);
1289 fputs_unfiltered ("\"\n", raw_stdout
);
1290 return MI_CMD_ERROR
;
1294 /* FIXME: This is just a hack so we can get some extra commands going.
1295 We don't want to channel things through the CLI, but call libgdb directly */
1296 /* Use only for synchronous commands */
1299 mi_execute_cli_command (const char *cli
, char *args
)
1303 struct cleanup
*old_cleanups
;
1305 xasprintf (&run
, cli
, args
);
1307 /* FIXME: gdb_???? */
1308 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1310 old_cleanups
= make_cleanup (xfree
, run
);
1311 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1312 do_cleanups (old_cleanups
);
1318 mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
)
1320 struct cleanup
*old_cleanups
;
1324 if (target_can_async_p ())
1326 async_args
= (char *) xmalloc (strlen (args
) + 2);
1327 make_exec_cleanup (free
, async_args
);
1328 strcpy (async_args
, args
);
1329 strcat (async_args
, "&");
1330 xasprintf (&run
, "%s %s", mi
, async_args
);
1331 make_exec_cleanup (free
, run
);
1332 add_continuation (mi_exec_async_cli_cmd_continuation
, NULL
);
1333 old_cleanups
= NULL
;
1337 xasprintf (&run
, "%s %s", mi
, args
);
1338 old_cleanups
= make_cleanup (xfree
, run
);
1341 if (!target_can_async_p ())
1343 /* NOTE: For synchronous targets asynchronous behavour is faked by
1344 printing out the GDB prompt before we even try to execute the
1346 if (last_async_command
)
1347 fputs_unfiltered (last_async_command
, raw_stdout
);
1348 fputs_unfiltered ("^running\n", raw_stdout
);
1349 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1350 gdb_flush (raw_stdout
);
1354 /* FIXME: cagney/1999-11-29: Printing this message before
1355 calling execute_command is wrong. It should only be printed
1356 once gdb has confirmed that it really has managed to send a
1357 run command to the target. */
1358 if (last_async_command
)
1359 fputs_unfiltered (last_async_command
, raw_stdout
);
1360 fputs_unfiltered ("^running\n", raw_stdout
);
1363 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1365 if (!target_can_async_p ())
1367 /* Do this before doing any printing. It would appear that some
1368 print code leaves garbage around in the buffer. */
1369 do_cleanups (old_cleanups
);
1370 /* If the target was doing the operation synchronously we fake
1371 the stopped message. */
1372 if (last_async_command
)
1373 fputs_unfiltered (last_async_command
, raw_stdout
);
1374 fputs_unfiltered ("*stopped", raw_stdout
);
1375 mi_out_put (uiout
, raw_stdout
);
1376 mi_out_rewind (uiout
);
1377 fputs_unfiltered ("\n", raw_stdout
);
1378 return MI_CMD_QUIET
;
1384 mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
)
1386 if (last_async_command
)
1387 fputs_unfiltered (last_async_command
, raw_stdout
);
1388 fputs_unfiltered ("*stopped", raw_stdout
);
1389 mi_out_put (uiout
, raw_stdout
);
1390 fputs_unfiltered ("\n", raw_stdout
);
1391 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1392 gdb_flush (raw_stdout
);
1393 do_exec_cleanups (ALL_CLEANUPS
);
1397 mi_load_progress (const char *section_name
,
1398 unsigned long sent_so_far
,
1399 unsigned long total_section
,
1400 unsigned long total_sent
,
1401 unsigned long grand_total
)
1403 struct timeval time_now
, delta
, update_threshold
;
1404 static struct timeval last_update
;
1405 static char *previous_sect_name
= NULL
;
1408 if (!current_interp_named_p (INTERP_MI
)
1409 && !current_interp_named_p (INTERP_MI1
))
1412 update_threshold
.tv_sec
= 0;
1413 update_threshold
.tv_usec
= 500000;
1414 gettimeofday (&time_now
, NULL
);
1416 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1417 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1419 if (delta
.tv_usec
< 0)
1422 delta
.tv_usec
+= 1000000;
1425 new_section
= (previous_sect_name
?
1426 strcmp (previous_sect_name
, section_name
) : 1);
1429 struct cleanup
*cleanup_tuple
;
1430 xfree (previous_sect_name
);
1431 previous_sect_name
= xstrdup (section_name
);
1433 if (last_async_command
)
1434 fputs_unfiltered (last_async_command
, raw_stdout
);
1435 fputs_unfiltered ("+download", raw_stdout
);
1436 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1437 ui_out_field_string (uiout
, "section", section_name
);
1438 ui_out_field_int (uiout
, "section-size", total_section
);
1439 ui_out_field_int (uiout
, "total-size", grand_total
);
1440 do_cleanups (cleanup_tuple
);
1441 mi_out_put (uiout
, raw_stdout
);
1442 fputs_unfiltered ("\n", raw_stdout
);
1443 gdb_flush (raw_stdout
);
1446 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1447 delta
.tv_usec
>= update_threshold
.tv_usec
)
1449 struct cleanup
*cleanup_tuple
;
1450 last_update
.tv_sec
= time_now
.tv_sec
;
1451 last_update
.tv_usec
= time_now
.tv_usec
;
1452 if (last_async_command
)
1453 fputs_unfiltered (last_async_command
, raw_stdout
);
1454 fputs_unfiltered ("+download", raw_stdout
);
1455 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1456 ui_out_field_string (uiout
, "section", section_name
);
1457 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1458 ui_out_field_int (uiout
, "section-size", total_section
);
1459 ui_out_field_int (uiout
, "total-sent", total_sent
);
1460 ui_out_field_int (uiout
, "total-size", grand_total
);
1461 do_cleanups (cleanup_tuple
);
1462 mi_out_put (uiout
, raw_stdout
);
1463 fputs_unfiltered ("\n", raw_stdout
);
1464 gdb_flush (raw_stdout
);
1469 mi_setup_architecture_data (void)
1471 /* don't trust REGISTER_BYTES to be zero. */
1472 old_regs
= xmalloc (REGISTER_BYTES
+ 1);
1473 memset (old_regs
, 0, REGISTER_BYTES
+ 1);
1477 _initialize_mi_main (void)
1479 register_gdbarch_swap (&old_regs
, sizeof (old_regs
), NULL
);
1480 register_gdbarch_swap (NULL
, 0, mi_setup_architecture_data
);