3 Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation,
6 Contributed by Cygnus Solutions (a Red Hat company).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
25 /* Work in progress */
30 #include "gdb_string.h"
31 #include "exceptions.h"
33 #include "gdbthread.h"
36 #include "mi-getopt.h"
37 #include "mi-console.h"
41 #include "event-loop.h"
42 #include "event-top.h"
43 #include "gdbcore.h" /* for write_memory() */
44 #include "value.h" /* for deprecated_write_register_bytes() */
58 /* Enumerations of the actions that may result from calling
59 captured_mi_execute_command */
61 enum captured_mi_execute_command_actions
63 EXECUTE_COMMAND_DISPLAY_PROMPT
,
64 EXECUTE_COMMAND_SUPRESS_PROMPT
,
65 EXECUTE_COMMAND_DISPLAY_ERROR
68 /* This structure is used to pass information from captured_mi_execute_command
69 to mi_execute_command. */
70 struct captured_mi_execute_command_args
72 /* This return result of the MI command (output) */
73 enum mi_cmd_result rc
;
75 /* What action to perform when the call is finished (output) */
76 enum captured_mi_execute_command_actions action
;
78 /* The command context to be executed (input) */
79 struct mi_parse
*command
;
83 struct ui_file
*raw_stdout
;
85 /* The token of the last asynchronous command */
86 static char *last_async_command
;
87 static char *previous_async_command
;
88 char *mi_error_message
;
89 static char *old_regs
;
91 extern void _initialize_mi_main (void);
92 static enum mi_cmd_result
mi_cmd_execute (struct mi_parse
*parse
);
94 static void mi_execute_cli_command (const char *cmd
, int args_p
,
96 static enum mi_cmd_result
mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
);
98 static void mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
);
100 static int register_changed_p (int regnum
);
101 static int get_register (int regnum
, int format
);
103 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
104 layer that calls libgdb. Any operation used in the below should be
108 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
110 /* We have to print everything right here because we never return */
111 if (last_async_command
)
112 fputs_unfiltered (last_async_command
, raw_stdout
);
113 fputs_unfiltered ("^exit\n", raw_stdout
);
114 mi_out_put (uiout
, raw_stdout
);
115 /* FIXME: The function called is not yet a formal libgdb function */
116 quit_force (NULL
, FROM_TTY
);
121 mi_cmd_exec_run (char *args
, int from_tty
)
123 /* FIXME: Should call a libgdb function, not a cli wrapper */
124 return mi_execute_async_cli_command ("run", args
, from_tty
);
128 mi_cmd_exec_next (char *args
, int from_tty
)
130 /* FIXME: Should call a libgdb function, not a cli wrapper */
131 return mi_execute_async_cli_command ("next", args
, from_tty
);
135 mi_cmd_exec_next_instruction (char *args
, int from_tty
)
137 /* FIXME: Should call a libgdb function, not a cli wrapper */
138 return mi_execute_async_cli_command ("nexti", args
, from_tty
);
142 mi_cmd_exec_step (char *args
, int from_tty
)
144 /* FIXME: Should call a libgdb function, not a cli wrapper */
145 return mi_execute_async_cli_command ("step", args
, from_tty
);
149 mi_cmd_exec_step_instruction (char *args
, int from_tty
)
151 /* FIXME: Should call a libgdb function, not a cli wrapper */
152 return mi_execute_async_cli_command ("stepi", args
, from_tty
);
156 mi_cmd_exec_finish (char *args
, int from_tty
)
158 /* FIXME: Should call a libgdb function, not a cli wrapper */
159 return mi_execute_async_cli_command ("finish", args
, from_tty
);
163 mi_cmd_exec_until (char *args
, int from_tty
)
165 /* FIXME: Should call a libgdb function, not a cli wrapper */
166 return mi_execute_async_cli_command ("until", args
, from_tty
);
170 mi_cmd_exec_return (char *args
, int from_tty
)
172 /* This command doesn't really execute the target, it just pops the
173 specified number of frames. */
175 /* Call return_command with from_tty argument equal to 0 so as to
176 avoid being queried. */
177 return_command (args
, 0);
179 /* Call return_command with from_tty argument equal to 0 so as to
180 avoid being queried. */
181 return_command (NULL
, 0);
183 /* Because we have called return_command with from_tty = 0, we need
184 to print the frame here. */
185 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
);
191 mi_cmd_exec_continue (char *args
, int from_tty
)
193 /* FIXME: Should call a libgdb function, not a cli wrapper */
194 return mi_execute_async_cli_command ("continue", args
, from_tty
);
197 /* Interrupt the execution of the target. Note how we must play around
198 with the token varialbes, in order to display the current token in
199 the result of the interrupt command, and the previous execution
200 token when the target finally stops. See comments in
203 mi_cmd_exec_interrupt (char *args
, int from_tty
)
205 if (!target_executing
)
207 mi_error_message
= xstrprintf ("mi_cmd_exec_interrupt: Inferior not executing.");
210 interrupt_target_command (args
, from_tty
);
211 if (last_async_command
)
212 fputs_unfiltered (last_async_command
, raw_stdout
);
213 fputs_unfiltered ("^done", raw_stdout
);
214 xfree (last_async_command
);
215 if (previous_async_command
)
216 last_async_command
= xstrdup (previous_async_command
);
217 xfree (previous_async_command
);
218 previous_async_command
= NULL
;
219 mi_out_put (uiout
, raw_stdout
);
220 mi_out_rewind (uiout
);
221 fputs_unfiltered ("\n", raw_stdout
);
226 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
232 mi_error_message
= xstrprintf ("mi_cmd_thread_select: USAGE: threadnum.");
236 rc
= gdb_thread_select (uiout
, argv
[0]);
238 /* RC is enum gdb_rc if it is successful (>=0)
239 enum return_reason if not (<0). */
240 if ((int) rc
< 0 && (enum return_reason
) rc
== RETURN_ERROR
)
241 return MI_CMD_CAUGHT_ERROR
;
242 else if ((int) rc
>= 0 && rc
== GDB_RC_FAIL
)
249 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
251 enum gdb_rc rc
= MI_CMD_DONE
;
255 mi_error_message
= xstrprintf ("mi_cmd_thread_list_ids: No arguments required.");
259 rc
= gdb_list_thread_ids (uiout
);
261 if (rc
== GDB_RC_FAIL
)
262 return MI_CMD_CAUGHT_ERROR
;
268 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
272 struct cleanup
*cleanup
;
274 /* Note that the test for a valid register must include checking the
275 REGISTER_NAME because NUM_REGS may be allocated for the union of
276 the register sets within a family of related processors. In this
277 case, some entries of REGISTER_NAME will change depending upon
278 the particular processor being debugged. */
280 numregs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
282 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
284 if (argc
== 0) /* No args, just do all the regs */
290 if (REGISTER_NAME (regnum
) == NULL
291 || *(REGISTER_NAME (regnum
)) == '\0')
292 ui_out_field_string (uiout
, NULL
, "");
294 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
298 /* Else, list of register #s, just do listed regs */
299 for (i
= 0; i
< argc
; i
++)
301 regnum
= atoi (argv
[i
]);
302 if (regnum
< 0 || regnum
>= numregs
)
304 do_cleanups (cleanup
);
305 mi_error_message
= xstrprintf ("bad register number");
308 if (REGISTER_NAME (regnum
) == NULL
309 || *(REGISTER_NAME (regnum
)) == '\0')
310 ui_out_field_string (uiout
, NULL
, "");
312 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
314 do_cleanups (cleanup
);
319 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
321 int regnum
, numregs
, changed
;
323 struct cleanup
*cleanup
;
325 /* Note that the test for a valid register must include checking the
326 REGISTER_NAME because NUM_REGS may be allocated for the union of
327 the register sets within a family of related processors. In this
328 case, some entries of REGISTER_NAME will change depending upon
329 the particular processor being debugged. */
331 numregs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
333 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
335 if (argc
== 0) /* No args, just do all the regs */
341 if (REGISTER_NAME (regnum
) == NULL
342 || *(REGISTER_NAME (regnum
)) == '\0')
344 changed
= register_changed_p (regnum
);
347 do_cleanups (cleanup
);
348 mi_error_message
= xstrprintf ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
352 ui_out_field_int (uiout
, NULL
, regnum
);
356 /* Else, list of register #s, just do listed regs */
357 for (i
= 0; i
< argc
; i
++)
359 regnum
= atoi (argv
[i
]);
363 && REGISTER_NAME (regnum
) != NULL
364 && *REGISTER_NAME (regnum
) != '\000')
366 changed
= register_changed_p (regnum
);
369 do_cleanups (cleanup
);
370 mi_error_message
= xstrprintf ("mi_cmd_data_list_register_change: Unable to read register contents.");
374 ui_out_field_int (uiout
, NULL
, regnum
);
378 do_cleanups (cleanup
);
379 mi_error_message
= xstrprintf ("bad register number");
383 do_cleanups (cleanup
);
388 register_changed_p (int regnum
)
390 char raw_buffer
[MAX_REGISTER_SIZE
];
392 if (! frame_register_read (deprecated_selected_frame
, regnum
, raw_buffer
))
395 if (memcmp (&old_regs
[DEPRECATED_REGISTER_BYTE (regnum
)], raw_buffer
,
396 register_size (current_gdbarch
, regnum
)) == 0)
399 /* Found a changed register. Return 1. */
401 memcpy (&old_regs
[DEPRECATED_REGISTER_BYTE (regnum
)], raw_buffer
,
402 register_size (current_gdbarch
, regnum
));
407 /* Return a list of register number and value pairs. The valid
408 arguments expected are: a letter indicating the format in which to
409 display the registers contents. This can be one of: x (hexadecimal), d
410 (decimal), N (natural), t (binary), o (octal), r (raw). After the
411 format argumetn there can be a sequence of numbers, indicating which
412 registers to fetch the content of. If the format is the only argument,
413 a list of all the registers with their values is returned. */
415 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
417 int regnum
, numregs
, format
, result
;
419 struct cleanup
*list_cleanup
, *tuple_cleanup
;
421 /* Note that the test for a valid register must include checking the
422 REGISTER_NAME because NUM_REGS may be allocated for the union of
423 the register sets within a family of related processors. In this
424 case, some entries of REGISTER_NAME will change depending upon
425 the particular processor being debugged. */
427 numregs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
431 mi_error_message
= xstrprintf ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
435 format
= (int) argv
[0][0];
437 if (!target_has_registers
)
439 mi_error_message
= xstrprintf ("mi_cmd_data_list_register_values: No registers.");
443 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
445 if (argc
== 1) /* No args, beside the format: do all the regs */
451 if (REGISTER_NAME (regnum
) == NULL
452 || *(REGISTER_NAME (regnum
)) == '\0')
454 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
455 ui_out_field_int (uiout
, "number", regnum
);
456 result
= get_register (regnum
, format
);
459 do_cleanups (list_cleanup
);
462 do_cleanups (tuple_cleanup
);
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 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
477 ui_out_field_int (uiout
, "number", regnum
);
478 result
= get_register (regnum
, format
);
481 do_cleanups (list_cleanup
);
484 do_cleanups (tuple_cleanup
);
488 do_cleanups (list_cleanup
);
489 mi_error_message
= xstrprintf ("bad register number");
493 do_cleanups (list_cleanup
);
497 /* Output one register's contents in the desired format. */
499 get_register (int regnum
, int format
)
501 char buffer
[MAX_REGISTER_SIZE
];
506 static struct ui_stream
*stb
= NULL
;
508 stb
= ui_out_stream_new (uiout
);
513 frame_register (deprecated_selected_frame
, regnum
, &optim
, &lval
, &addr
,
518 mi_error_message
= xstrprintf ("Optimized out");
525 char *ptr
, buf
[1024];
529 for (j
= 0; j
< register_size (current_gdbarch
, regnum
); j
++)
531 int idx
= TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
? j
532 : register_size (current_gdbarch
, regnum
) - 1 - j
;
533 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
536 ui_out_field_string (uiout
, "value", buf
);
537 /*fputs_filtered (buf, gdb_stdout); */
541 val_print (register_type (current_gdbarch
, regnum
), buffer
, 0, 0,
542 stb
->stream
, format
, 1, 0, Val_pretty_default
);
543 ui_out_field_stream (uiout
, "value", stb
);
544 ui_out_stream_delete (stb
);
549 /* Write given values into registers. The registers and values are
550 given as pairs. The corresponding MI command is
551 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
553 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
561 /* Note that the test for a valid register must include checking the
562 REGISTER_NAME because NUM_REGS may be allocated for the union of
563 the register sets within a family of related processors. In this
564 case, some entries of REGISTER_NAME will change depending upon
565 the particular processor being debugged. */
567 numregs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
571 mi_error_message
= xstrprintf ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
575 format
= (int) argv
[0][0];
577 if (!target_has_registers
)
579 mi_error_message
= xstrprintf ("mi_cmd_data_write_register_values: No registers.");
585 mi_error_message
= xstrprintf ("mi_cmd_data_write_register_values: No regs and values specified.");
591 mi_error_message
= xstrprintf ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
595 for (i
= 1; i
< argc
; i
= i
+ 2)
597 regnum
= atoi (argv
[i
]);
601 && REGISTER_NAME (regnum
) != NULL
602 && *REGISTER_NAME (regnum
) != '\000')
605 struct cleanup
*old_chain
;
607 /* Get the value as a number */
608 value
= parse_and_eval_address (argv
[i
+ 1]);
609 /* Get the value into an array */
610 buffer
= xmalloc (DEPRECATED_REGISTER_SIZE
);
611 old_chain
= make_cleanup (xfree
, buffer
);
612 store_signed_integer (buffer
, DEPRECATED_REGISTER_SIZE
, value
);
614 deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (regnum
), buffer
, register_size (current_gdbarch
, regnum
));
615 /* Free the buffer. */
616 do_cleanups (old_chain
);
620 mi_error_message
= xstrprintf ("bad register number");
628 /*This is commented out because we decided it was not useful. I leave
629 it, just in case. ezannoni:1999-12-08 */
631 /* Assign a value to a variable. The expression argument must be in
632 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
635 mi_cmd_data_assign (char *command
, char **argv
, int argc
)
637 struct expression
*expr
;
638 struct cleanup
*old_chain
;
642 mi_error_message
= xstrprintf ("mi_cmd_data_assign: Usage: -data-assign expression");
646 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
647 01-12-1999: Need to decide what to do with this for libgdb purposes. */
649 expr
= parse_expression (argv
[0]);
650 old_chain
= make_cleanup (free_current_contents
, &expr
);
651 evaluate_expression (expr
);
652 do_cleanups (old_chain
);
657 /* Evaluate the value of the argument. The argument is an
658 expression. If the expression contains spaces it needs to be
659 included in double quotes. */
661 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
663 struct expression
*expr
;
664 struct cleanup
*old_chain
= NULL
;
666 struct ui_stream
*stb
= NULL
;
668 stb
= ui_out_stream_new (uiout
);
672 mi_error_message
= xstrprintf ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
676 expr
= parse_expression (argv
[0]);
678 old_chain
= make_cleanup (free_current_contents
, &expr
);
680 val
= evaluate_expression (expr
);
682 /* Print the result of the expression evaluation. */
683 val_print (value_type (val
), VALUE_CONTENTS (val
),
684 VALUE_EMBEDDED_OFFSET (val
), VALUE_ADDRESS (val
),
685 stb
->stream
, 0, 0, 0, 0);
687 ui_out_field_stream (uiout
, "value", stb
);
688 ui_out_stream_delete (stb
);
690 do_cleanups (old_chain
);
696 mi_cmd_target_download (char *args
, int from_tty
)
699 struct cleanup
*old_cleanups
= NULL
;
701 run
= xstrprintf ("load %s", args
);
702 old_cleanups
= make_cleanup (xfree
, run
);
703 execute_command (run
, from_tty
);
705 do_cleanups (old_cleanups
);
709 /* Connect to the remote target. */
711 mi_cmd_target_select (char *args
, int from_tty
)
714 struct cleanup
*old_cleanups
= NULL
;
716 run
= xstrprintf ("target %s", args
);
717 old_cleanups
= make_cleanup (xfree
, run
);
719 /* target-select is always synchronous. once the call has returned
720 we know that we are connected. */
721 /* NOTE: At present all targets that are connected are also
722 (implicitly) talking to a halted target. In the future this may
724 execute_command (run
, from_tty
);
726 do_cleanups (old_cleanups
);
728 /* Issue the completion message here. */
729 if (last_async_command
)
730 fputs_unfiltered (last_async_command
, raw_stdout
);
731 fputs_unfiltered ("^connected", raw_stdout
);
732 mi_out_put (uiout
, raw_stdout
);
733 mi_out_rewind (uiout
);
734 fputs_unfiltered ("\n", raw_stdout
);
735 do_exec_cleanups (ALL_CLEANUPS
);
741 ADDR: start address of data to be dumped.
742 WORD-FORMAT: a char indicating format for the ``word''. See
744 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
745 NR_ROW: Number of rows.
746 NR_COL: The number of colums (words per row).
747 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
748 ASCHAR for unprintable characters.
750 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
751 displayes them. Returns:
753 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
756 The number of bytes read is SIZE*ROW*COL. */
759 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
761 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
767 struct type
*word_type
;
780 static struct mi_opt opts
[] =
782 {"o", OFFSET_OPT
, 1},
788 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
792 switch ((enum opt
) opt
)
795 offset
= atol (optarg
);
802 if (argc
< 5 || argc
> 6)
804 mi_error_message
= xstrprintf ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
808 /* Extract all the arguments. */
810 /* Start address of the memory dump. */
811 addr
= parse_and_eval_address (argv
[0]) + offset
;
812 /* The format character to use when displaying a memory word. See
813 the ``x'' command. */
814 word_format
= argv
[1][0];
815 /* The size of the memory word. */
816 word_size
= atol (argv
[2]);
820 word_type
= builtin_type_int8
;
824 word_type
= builtin_type_int16
;
828 word_type
= builtin_type_int32
;
832 word_type
= builtin_type_int64
;
836 word_type
= builtin_type_int8
;
839 /* The number of rows */
840 nr_rows
= atol (argv
[3]);
843 mi_error_message
= xstrprintf ("mi_cmd_data_read_memory: invalid number of rows.");
846 /* number of bytes per row. */
847 nr_cols
= atol (argv
[4]);
850 mi_error_message
= xstrprintf ("mi_cmd_data_read_memory: invalid number of columns.");
853 /* The un-printable character when printing ascii. */
859 /* create a buffer and read it in. */
860 total_bytes
= word_size
* nr_rows
* nr_cols
;
861 mbuf
= xcalloc (total_bytes
, 1);
862 make_cleanup (xfree
, mbuf
);
864 while (nr_bytes
< total_bytes
)
867 long num
= target_read_memory_partial (addr
+ nr_bytes
, mbuf
+ nr_bytes
,
868 total_bytes
- nr_bytes
,
875 /* output the header information. */
876 ui_out_field_core_addr (uiout
, "addr", addr
);
877 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
878 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
879 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
880 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
881 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
882 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
884 /* Build the result as a two dimentional table. */
886 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
887 struct cleanup
*cleanup_list_memory
;
890 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
891 for (row
= 0, row_byte
= 0;
893 row
++, row_byte
+= nr_cols
* word_size
)
897 struct cleanup
*cleanup_tuple
;
898 struct cleanup
*cleanup_list_data
;
899 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
900 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
901 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
902 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
903 for (col
= 0, col_byte
= row_byte
;
905 col
++, col_byte
+= word_size
)
907 if (col_byte
+ word_size
> nr_bytes
)
909 ui_out_field_string (uiout
, NULL
, "N/A");
913 ui_file_rewind (stream
->stream
);
914 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
915 word_asize
, stream
->stream
);
916 ui_out_field_stream (uiout
, NULL
, stream
);
919 do_cleanups (cleanup_list_data
);
923 ui_file_rewind (stream
->stream
);
924 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
926 if (byte
>= nr_bytes
)
928 fputc_unfiltered ('X', stream
->stream
);
930 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
932 fputc_unfiltered (aschar
, stream
->stream
);
935 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
937 ui_out_field_stream (uiout
, "ascii", stream
);
939 do_cleanups (cleanup_tuple
);
941 ui_out_stream_delete (stream
);
942 do_cleanups (cleanup_list_memory
);
944 do_cleanups (cleanups
);
948 /* DATA-MEMORY-WRITE:
950 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
951 offset from the beginning of the memory grid row where the cell to
953 ADDR: start address of the row in the memory grid where the memory
954 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
955 the location to write to.
956 FORMAT: a char indicating format for the ``word''. See
958 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
959 VALUE: value to be written into the memory address.
961 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
965 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
970 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
971 enough when using a compiler other than GCC. */
974 struct cleanup
*old_chain
;
982 static struct mi_opt opts
[] =
984 {"o", OFFSET_OPT
, 1},
990 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
994 switch ((enum opt
) opt
)
997 offset
= atol (optarg
);
1006 mi_error_message
= xstrprintf ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1007 return MI_CMD_ERROR
;
1010 /* Extract all the arguments. */
1011 /* Start address of the memory dump. */
1012 addr
= parse_and_eval_address (argv
[0]);
1013 /* The format character to use when displaying a memory word. See
1014 the ``x'' command. */
1015 word_format
= argv
[1][0];
1016 /* The size of the memory word. */
1017 word_size
= atol (argv
[2]);
1019 /* Calculate the real address of the write destination. */
1020 addr
+= (offset
* word_size
);
1022 /* Get the value as a number */
1023 value
= parse_and_eval_address (argv
[3]);
1024 /* Get the value into an array */
1025 buffer
= xmalloc (word_size
);
1026 old_chain
= make_cleanup (xfree
, buffer
);
1027 store_signed_integer (buffer
, word_size
, value
);
1028 /* Write it down to memory */
1029 write_memory (addr
, buffer
, word_size
);
1030 /* Free the buffer. */
1031 do_cleanups (old_chain
);
1036 /* Execute a command within a safe environment.
1037 Return <0 for error; >=0 for ok.
1039 args->action will tell mi_execute_command what action
1040 to perfrom after the given command has executed (display/supress
1041 prompt, display error). */
1044 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1046 struct captured_mi_execute_command_args
*args
=
1047 (struct captured_mi_execute_command_args
*) data
;
1048 struct mi_parse
*context
= args
->command
;
1050 switch (context
->op
)
1054 /* A MI command was read from the input stream */
1056 /* FIXME: gdb_???? */
1057 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1058 context
->token
, context
->command
, context
->args
);
1059 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1060 condition expression, each function should return an
1061 indication of what action is required and then switch on
1063 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1064 args
->rc
= mi_cmd_execute (context
);
1066 if (!target_can_async_p () || !target_executing
)
1068 /* print the result if there were no errors
1070 Remember that on the way out of executing a command, you have
1071 to directly use the mi_interp's uiout, since the command could
1072 have reset the interpreter, in which case the current uiout
1073 will most likely crash in the mi_out_* routines. */
1074 if (args
->rc
== MI_CMD_DONE
)
1076 fputs_unfiltered (context
->token
, raw_stdout
);
1077 fputs_unfiltered ("^done", raw_stdout
);
1078 mi_out_put (uiout
, raw_stdout
);
1079 mi_out_rewind (uiout
);
1080 fputs_unfiltered ("\n", raw_stdout
);
1082 else if (args
->rc
== MI_CMD_ERROR
)
1084 if (mi_error_message
)
1086 fputs_unfiltered (context
->token
, raw_stdout
);
1087 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1088 fputstr_unfiltered (mi_error_message
, '"', raw_stdout
);
1089 xfree (mi_error_message
);
1090 fputs_unfiltered ("\"\n", raw_stdout
);
1092 mi_out_rewind (uiout
);
1094 else if (args
->rc
== MI_CMD_CAUGHT_ERROR
)
1096 mi_out_rewind (uiout
);
1097 args
->action
= EXECUTE_COMMAND_DISPLAY_ERROR
;
1101 mi_out_rewind (uiout
);
1103 else if (sync_execution
)
1105 /* Don't print the prompt. We are executing the target in
1106 synchronous mode. */
1107 args
->action
= EXECUTE_COMMAND_SUPRESS_PROMPT
;
1113 /* A CLI command was read from the input stream */
1114 /* This will be removed as soon as we have a complete set of
1116 /* echo the command on the console. */
1117 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1118 mi_execute_cli_command (context
->command
, 0, NULL
);
1120 /* If we changed interpreters, DON'T print out anything. */
1121 if (current_interp_named_p (INTERP_MI
)
1122 || current_interp_named_p (INTERP_MI1
)
1123 || current_interp_named_p (INTERP_MI2
)
1124 || current_interp_named_p (INTERP_MI3
))
1126 /* print the result */
1127 /* FIXME: Check for errors here. */
1128 fputs_unfiltered (context
->token
, raw_stdout
);
1129 fputs_unfiltered ("^done", raw_stdout
);
1130 mi_out_put (uiout
, raw_stdout
);
1131 mi_out_rewind (uiout
);
1132 fputs_unfiltered ("\n", raw_stdout
);
1133 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1134 args
->rc
= MI_CMD_DONE
;
1145 mi_execute_command (char *cmd
, int from_tty
)
1147 struct mi_parse
*command
;
1148 struct captured_mi_execute_command_args args
;
1149 struct ui_out
*saved_uiout
= uiout
;
1152 /* This is to handle EOF (^D). We just quit gdb. */
1153 /* FIXME: we should call some API function here. */
1155 quit_force (NULL
, from_tty
);
1157 command
= mi_parse (cmd
);
1159 if (command
!= NULL
)
1161 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
1162 be pushed even further down or even eliminated? */
1163 args
.command
= command
;
1164 result
= catch_exceptions (uiout
, captured_mi_execute_command
, &args
, "",
1167 if (args
.action
== EXECUTE_COMMAND_SUPRESS_PROMPT
)
1169 /* The command is executing synchronously. Bail out early
1170 suppressing the finished prompt. */
1171 mi_parse_free (command
);
1174 if (args
.action
== EXECUTE_COMMAND_DISPLAY_ERROR
|| result
< 0)
1176 char *msg
= error_last_message ();
1177 struct cleanup
*cleanup
= make_cleanup (xfree
, msg
);
1178 /* The command execution failed and error() was called
1180 fputs_unfiltered (command
->token
, raw_stdout
);
1181 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1182 fputstr_unfiltered (msg
, '"', raw_stdout
);
1183 fputs_unfiltered ("\"\n", raw_stdout
);
1185 mi_parse_free (command
);
1188 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1189 gdb_flush (raw_stdout
);
1190 /* print any buffered hook code */
1194 static enum mi_cmd_result
1195 mi_cmd_execute (struct mi_parse
*parse
)
1197 if (parse
->cmd
->argv_func
!= NULL
1198 || parse
->cmd
->args_func
!= NULL
)
1200 /* FIXME: We need to save the token because the command executed
1201 may be asynchronous and need to print the token again.
1202 In the future we can pass the token down to the func
1203 and get rid of the last_async_command */
1204 /* The problem here is to keep the token around when we launch
1205 the target, and we want to interrupt it later on. The
1206 interrupt command will have its own token, but when the
1207 target stops, we must display the token corresponding to the
1208 last execution command given. So we have another string where
1209 we copy the token (previous_async_command), if this was
1210 indeed the token of an execution command, and when we stop we
1211 print that one. This is possible because the interrupt
1212 command, when over, will copy that token back into the
1213 default token string (last_async_command). */
1215 if (target_executing
)
1217 if (!previous_async_command
)
1218 previous_async_command
= xstrdup (last_async_command
);
1219 if (strcmp (parse
->command
, "exec-interrupt"))
1221 fputs_unfiltered (parse
->token
, raw_stdout
);
1222 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1223 fputs_unfiltered ("Cannot execute command ", raw_stdout
);
1224 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1225 fputs_unfiltered (" while target running", raw_stdout
);
1226 fputs_unfiltered ("\"\n", raw_stdout
);
1227 return MI_CMD_ERROR
;
1230 last_async_command
= xstrdup (parse
->token
);
1231 make_exec_cleanup (free_current_contents
, &last_async_command
);
1232 /* FIXME: DELETE THIS! */
1233 if (parse
->cmd
->args_func
!= NULL
)
1234 return parse
->cmd
->args_func (parse
->args
, 0 /*from_tty */ );
1235 return parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1237 else if (parse
->cmd
->cli
.cmd
!= 0)
1239 /* FIXME: DELETE THIS. */
1240 /* The operation is still implemented by a cli command */
1241 /* Must be a synchronous one */
1242 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1248 /* FIXME: DELETE THIS. */
1249 fputs_unfiltered (parse
->token
, raw_stdout
);
1250 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1251 fputs_unfiltered ("Undefined mi command: ", raw_stdout
);
1252 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1253 fputs_unfiltered (" (missing implementation)", raw_stdout
);
1254 fputs_unfiltered ("\"\n", raw_stdout
);
1255 return MI_CMD_ERROR
;
1259 /* FIXME: This is just a hack so we can get some extra commands going.
1260 We don't want to channel things through the CLI, but call libgdb directly */
1261 /* Use only for synchronous commands */
1264 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1268 struct cleanup
*old_cleanups
;
1271 run
= xstrprintf ("%s %s", cmd
, args
);
1273 run
= xstrdup (cmd
);
1275 /* FIXME: gdb_???? */
1276 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1278 old_cleanups
= make_cleanup (xfree
, run
);
1279 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1280 do_cleanups (old_cleanups
);
1286 mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
)
1288 struct cleanup
*old_cleanups
;
1292 if (target_can_async_p ())
1294 async_args
= (char *) xmalloc (strlen (args
) + 2);
1295 make_exec_cleanup (free
, async_args
);
1296 strcpy (async_args
, args
);
1297 strcat (async_args
, "&");
1298 run
= xstrprintf ("%s %s", mi
, async_args
);
1299 make_exec_cleanup (free
, run
);
1300 add_continuation (mi_exec_async_cli_cmd_continuation
, NULL
);
1301 old_cleanups
= NULL
;
1305 run
= xstrprintf ("%s %s", mi
, args
);
1306 old_cleanups
= make_cleanup (xfree
, run
);
1309 if (!target_can_async_p ())
1311 /* NOTE: For synchronous targets asynchronous behavour is faked by
1312 printing out the GDB prompt before we even try to execute the
1314 if (last_async_command
)
1315 fputs_unfiltered (last_async_command
, raw_stdout
);
1316 fputs_unfiltered ("^running\n", raw_stdout
);
1317 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1318 gdb_flush (raw_stdout
);
1322 /* FIXME: cagney/1999-11-29: Printing this message before
1323 calling execute_command is wrong. It should only be printed
1324 once gdb has confirmed that it really has managed to send a
1325 run command to the target. */
1326 if (last_async_command
)
1327 fputs_unfiltered (last_async_command
, raw_stdout
);
1328 fputs_unfiltered ("^running\n", raw_stdout
);
1331 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1333 if (!target_can_async_p ())
1335 /* Do this before doing any printing. It would appear that some
1336 print code leaves garbage around in the buffer. */
1337 do_cleanups (old_cleanups
);
1338 /* If the target was doing the operation synchronously we fake
1339 the stopped message. */
1340 if (last_async_command
)
1341 fputs_unfiltered (last_async_command
, raw_stdout
);
1342 fputs_unfiltered ("*stopped", raw_stdout
);
1343 mi_out_put (uiout
, raw_stdout
);
1344 mi_out_rewind (uiout
);
1345 fputs_unfiltered ("\n", raw_stdout
);
1346 return MI_CMD_QUIET
;
1352 mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
)
1354 if (last_async_command
)
1355 fputs_unfiltered (last_async_command
, raw_stdout
);
1356 fputs_unfiltered ("*stopped", raw_stdout
);
1357 mi_out_put (uiout
, raw_stdout
);
1358 fputs_unfiltered ("\n", raw_stdout
);
1359 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1360 gdb_flush (raw_stdout
);
1361 do_exec_cleanups (ALL_CLEANUPS
);
1365 mi_load_progress (const char *section_name
,
1366 unsigned long sent_so_far
,
1367 unsigned long total_section
,
1368 unsigned long total_sent
,
1369 unsigned long grand_total
)
1371 struct timeval time_now
, delta
, update_threshold
;
1372 static struct timeval last_update
;
1373 static char *previous_sect_name
= NULL
;
1376 if (!current_interp_named_p (INTERP_MI
)
1377 && !current_interp_named_p (INTERP_MI1
))
1380 update_threshold
.tv_sec
= 0;
1381 update_threshold
.tv_usec
= 500000;
1382 gettimeofday (&time_now
, NULL
);
1384 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1385 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1387 if (delta
.tv_usec
< 0)
1390 delta
.tv_usec
+= 1000000;
1393 new_section
= (previous_sect_name
?
1394 strcmp (previous_sect_name
, section_name
) : 1);
1397 struct cleanup
*cleanup_tuple
;
1398 xfree (previous_sect_name
);
1399 previous_sect_name
= xstrdup (section_name
);
1401 if (last_async_command
)
1402 fputs_unfiltered (last_async_command
, raw_stdout
);
1403 fputs_unfiltered ("+download", raw_stdout
);
1404 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1405 ui_out_field_string (uiout
, "section", section_name
);
1406 ui_out_field_int (uiout
, "section-size", total_section
);
1407 ui_out_field_int (uiout
, "total-size", grand_total
);
1408 do_cleanups (cleanup_tuple
);
1409 mi_out_put (uiout
, raw_stdout
);
1410 fputs_unfiltered ("\n", raw_stdout
);
1411 gdb_flush (raw_stdout
);
1414 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1415 delta
.tv_usec
>= update_threshold
.tv_usec
)
1417 struct cleanup
*cleanup_tuple
;
1418 last_update
.tv_sec
= time_now
.tv_sec
;
1419 last_update
.tv_usec
= time_now
.tv_usec
;
1420 if (last_async_command
)
1421 fputs_unfiltered (last_async_command
, raw_stdout
);
1422 fputs_unfiltered ("+download", raw_stdout
);
1423 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1424 ui_out_field_string (uiout
, "section", section_name
);
1425 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1426 ui_out_field_int (uiout
, "section-size", total_section
);
1427 ui_out_field_int (uiout
, "total-sent", total_sent
);
1428 ui_out_field_int (uiout
, "total-size", grand_total
);
1429 do_cleanups (cleanup_tuple
);
1430 mi_out_put (uiout
, raw_stdout
);
1431 fputs_unfiltered ("\n", raw_stdout
);
1432 gdb_flush (raw_stdout
);
1437 mi_setup_architecture_data (void)
1439 old_regs
= xmalloc ((NUM_REGS
+ NUM_PSEUDO_REGS
) * MAX_REGISTER_SIZE
+ 1);
1440 memset (old_regs
, 0, (NUM_REGS
+ NUM_PSEUDO_REGS
) * MAX_REGISTER_SIZE
+ 1);
1444 _initialize_mi_main (void)
1446 DEPRECATED_REGISTER_GDBARCH_SWAP (old_regs
);
1447 deprecated_register_gdbarch_swap (NULL
, 0, mi_setup_architecture_data
);