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"
32 #include "gdbthread.h"
35 #include "mi-getopt.h"
36 #include "mi-console.h"
40 #include "event-loop.h"
41 #include "event-top.h"
42 #include "gdbcore.h" /* for write_memory() */
43 #include "value.h" /* for deprecated_write_register_bytes() */
57 /* Enumerations of the actions that may result from calling
58 captured_mi_execute_command */
60 enum captured_mi_execute_command_actions
62 EXECUTE_COMMAND_DISPLAY_PROMPT
,
63 EXECUTE_COMMAND_SUPRESS_PROMPT
,
64 EXECUTE_COMMAND_DISPLAY_ERROR
67 /* This structure is used to pass information from captured_mi_execute_command
68 to mi_execute_command. */
69 struct captured_mi_execute_command_args
71 /* This return result of the MI command (output) */
72 enum mi_cmd_result rc
;
74 /* What action to perform when the call is finished (output) */
75 enum captured_mi_execute_command_actions action
;
77 /* The command context to be executed (input) */
78 struct mi_parse
*command
;
82 struct ui_file
*raw_stdout
;
84 /* The token of the last asynchronous command */
85 static char *last_async_command
;
86 static char *previous_async_command
;
87 char *mi_error_message
;
88 static char *old_regs
;
90 extern void _initialize_mi_main (void);
91 static enum mi_cmd_result
mi_cmd_execute (struct mi_parse
*parse
);
93 static void mi_execute_cli_command (const char *cmd
, int args_p
,
95 static enum mi_cmd_result
mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
);
97 static void mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
);
99 static int register_changed_p (int regnum
);
100 static int get_register (int regnum
, int format
);
102 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
103 layer that calls libgdb. Any operation used in the below should be
107 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
109 /* We have to print everything right here because we never return */
110 if (last_async_command
)
111 fputs_unfiltered (last_async_command
, raw_stdout
);
112 fputs_unfiltered ("^exit\n", raw_stdout
);
113 mi_out_put (uiout
, raw_stdout
);
114 /* FIXME: The function called is not yet a formal libgdb function */
115 quit_force (NULL
, FROM_TTY
);
120 mi_cmd_exec_run (char *args
, int from_tty
)
122 /* FIXME: Should call a libgdb function, not a cli wrapper */
123 return mi_execute_async_cli_command ("run", args
, from_tty
);
127 mi_cmd_exec_next (char *args
, int from_tty
)
129 /* FIXME: Should call a libgdb function, not a cli wrapper */
130 return mi_execute_async_cli_command ("next", args
, from_tty
);
134 mi_cmd_exec_next_instruction (char *args
, int from_tty
)
136 /* FIXME: Should call a libgdb function, not a cli wrapper */
137 return mi_execute_async_cli_command ("nexti", args
, from_tty
);
141 mi_cmd_exec_step (char *args
, int from_tty
)
143 /* FIXME: Should call a libgdb function, not a cli wrapper */
144 return mi_execute_async_cli_command ("step", args
, from_tty
);
148 mi_cmd_exec_step_instruction (char *args
, int from_tty
)
150 /* FIXME: Should call a libgdb function, not a cli wrapper */
151 return mi_execute_async_cli_command ("stepi", args
, from_tty
);
155 mi_cmd_exec_finish (char *args
, int from_tty
)
157 /* FIXME: Should call a libgdb function, not a cli wrapper */
158 return mi_execute_async_cli_command ("finish", args
, from_tty
);
162 mi_cmd_exec_until (char *args
, int from_tty
)
164 /* FIXME: Should call a libgdb function, not a cli wrapper */
165 return mi_execute_async_cli_command ("until", args
, from_tty
);
169 mi_cmd_exec_return (char *args
, int from_tty
)
171 /* This command doesn't really execute the target, it just pops the
172 specified number of frames. */
174 /* Call return_command with from_tty argument equal to 0 so as to
175 avoid being queried. */
176 return_command (args
, 0);
178 /* Call return_command with from_tty argument equal to 0 so as to
179 avoid being queried. */
180 return_command (NULL
, 0);
182 /* Because we have called return_command with from_tty = 0, we need
183 to print the frame here. */
184 print_stack_frame (get_selected_frame (), 1, LOC_AND_ADDRESS
);
190 mi_cmd_exec_continue (char *args
, int from_tty
)
192 /* FIXME: Should call a libgdb function, not a cli wrapper */
193 return mi_execute_async_cli_command ("continue", args
, from_tty
);
196 /* Interrupt the execution of the target. Note how we must play around
197 with the token varialbes, in order to display the current token in
198 the result of the interrupt command, and the previous execution
199 token when the target finally stops. See comments in
202 mi_cmd_exec_interrupt (char *args
, int from_tty
)
204 if (!target_executing
)
206 xasprintf (&mi_error_message
,
207 "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 xasprintf (&mi_error_message
,
233 "mi_cmd_thread_select: USAGE: threadnum.");
237 rc
= gdb_thread_select (uiout
, argv
[0]);
239 /* RC is enum gdb_rc if it is successful (>=0)
240 enum return_reason if not (<0). */
241 if ((int) rc
< 0 && (enum return_reason
) rc
== RETURN_ERROR
)
242 return MI_CMD_CAUGHT_ERROR
;
243 else if ((int) rc
>= 0 && rc
== GDB_RC_FAIL
)
250 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
252 enum gdb_rc rc
= MI_CMD_DONE
;
256 xasprintf (&mi_error_message
,
257 "mi_cmd_thread_list_ids: No arguments required.");
261 rc
= gdb_list_thread_ids (uiout
);
263 if (rc
== GDB_RC_FAIL
)
264 return MI_CMD_CAUGHT_ERROR
;
270 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
274 struct cleanup
*cleanup
;
276 /* Note that the test for a valid register must include checking the
277 REGISTER_NAME because NUM_REGS may be allocated for the union of
278 the register sets within a family of related processors. In this
279 case, some entries of REGISTER_NAME will change depending upon
280 the particular processor being debugged. */
282 numregs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
284 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
286 if (argc
== 0) /* No args, just do all the regs */
292 if (REGISTER_NAME (regnum
) == NULL
293 || *(REGISTER_NAME (regnum
)) == '\0')
294 ui_out_field_string (uiout
, NULL
, "");
296 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
300 /* Else, list of register #s, just do listed regs */
301 for (i
= 0; i
< argc
; i
++)
303 regnum
= atoi (argv
[i
]);
304 if (regnum
< 0 || regnum
>= numregs
)
306 do_cleanups (cleanup
);
307 xasprintf (&mi_error_message
, "bad register number");
310 if (REGISTER_NAME (regnum
) == NULL
311 || *(REGISTER_NAME (regnum
)) == '\0')
312 ui_out_field_string (uiout
, NULL
, "");
314 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
316 do_cleanups (cleanup
);
321 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
323 int regnum
, numregs
, changed
;
325 struct cleanup
*cleanup
;
327 /* Note that the test for a valid register must include checking the
328 REGISTER_NAME because NUM_REGS may be allocated for the union of
329 the register sets within a family of related processors. In this
330 case, some entries of REGISTER_NAME will change depending upon
331 the particular processor being debugged. */
335 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
337 if (argc
== 0) /* No args, just do all the regs */
343 if (REGISTER_NAME (regnum
) == NULL
344 || *(REGISTER_NAME (regnum
)) == '\0')
346 changed
= register_changed_p (regnum
);
349 do_cleanups (cleanup
);
350 xasprintf (&mi_error_message
,
351 "mi_cmd_data_list_changed_registers: Unable to read register contents.");
355 ui_out_field_int (uiout
, NULL
, regnum
);
359 /* Else, list of register #s, just do listed regs */
360 for (i
= 0; i
< argc
; i
++)
362 regnum
= atoi (argv
[i
]);
366 && REGISTER_NAME (regnum
) != NULL
367 && *REGISTER_NAME (regnum
) != '\000')
369 changed
= register_changed_p (regnum
);
372 do_cleanups (cleanup
);
373 xasprintf (&mi_error_message
,
374 "mi_cmd_data_list_register_change: Unable to read register contents.");
378 ui_out_field_int (uiout
, NULL
, regnum
);
382 do_cleanups (cleanup
);
383 xasprintf (&mi_error_message
, "bad register number");
387 do_cleanups (cleanup
);
392 register_changed_p (int regnum
)
394 char raw_buffer
[MAX_REGISTER_SIZE
];
396 if (! frame_register_read (deprecated_selected_frame
, regnum
, raw_buffer
))
399 if (memcmp (&old_regs
[DEPRECATED_REGISTER_BYTE (regnum
)], raw_buffer
,
400 DEPRECATED_REGISTER_RAW_SIZE (regnum
)) == 0)
403 /* Found a changed register. Return 1. */
405 memcpy (&old_regs
[DEPRECATED_REGISTER_BYTE (regnum
)], raw_buffer
,
406 DEPRECATED_REGISTER_RAW_SIZE (regnum
));
411 /* Return a list of register number and value pairs. The valid
412 arguments expected are: a letter indicating the format in which to
413 display the registers contents. This can be one of: x (hexadecimal), d
414 (decimal), N (natural), t (binary), o (octal), r (raw). After the
415 format argumetn there can be a sequence of numbers, indicating which
416 registers to fetch the content of. If the format is the only argument,
417 a list of all the registers with their values is returned. */
419 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
421 int regnum
, numregs
, format
, result
;
423 struct cleanup
*list_cleanup
, *tuple_cleanup
;
425 /* Note that the test for a valid register must include checking the
426 REGISTER_NAME because NUM_REGS may be allocated for the union of
427 the register sets within a family of related processors. In this
428 case, some entries of REGISTER_NAME will change depending upon
429 the particular processor being debugged. */
435 xasprintf (&mi_error_message
,
436 "mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
440 format
= (int) argv
[0][0];
442 if (!target_has_registers
)
444 xasprintf (&mi_error_message
,
445 "mi_cmd_data_list_register_values: No registers.");
449 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
451 if (argc
== 1) /* No args, beside the format: do all the regs */
457 if (REGISTER_NAME (regnum
) == NULL
458 || *(REGISTER_NAME (regnum
)) == '\0')
460 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
461 ui_out_field_int (uiout
, "number", regnum
);
462 result
= get_register (regnum
, format
);
465 do_cleanups (list_cleanup
);
468 do_cleanups (tuple_cleanup
);
472 /* Else, list of register #s, just do listed regs */
473 for (i
= 1; i
< argc
; i
++)
475 regnum
= atoi (argv
[i
]);
479 && REGISTER_NAME (regnum
) != NULL
480 && *REGISTER_NAME (regnum
) != '\000')
482 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
483 ui_out_field_int (uiout
, "number", regnum
);
484 result
= get_register (regnum
, format
);
487 do_cleanups (list_cleanup
);
490 do_cleanups (tuple_cleanup
);
494 do_cleanups (list_cleanup
);
495 xasprintf (&mi_error_message
, "bad register number");
499 do_cleanups (list_cleanup
);
503 /* Output one register's contents in the desired format. */
505 get_register (int regnum
, int format
)
507 char raw_buffer
[MAX_REGISTER_SIZE
];
508 char virtual_buffer
[MAX_REGISTER_SIZE
];
513 static struct ui_stream
*stb
= NULL
;
515 stb
= ui_out_stream_new (uiout
);
520 frame_register (deprecated_selected_frame
, regnum
, &optim
, &lval
, &addr
,
521 &realnum
, raw_buffer
);
525 xasprintf (&mi_error_message
, "Optimized out");
529 /* Convert raw data to virtual format if necessary. */
531 if (DEPRECATED_REGISTER_CONVERTIBLE_P ()
532 && DEPRECATED_REGISTER_CONVERTIBLE (regnum
))
534 DEPRECATED_REGISTER_CONVERT_TO_VIRTUAL (regnum
,
535 register_type (current_gdbarch
, regnum
),
536 raw_buffer
, virtual_buffer
);
539 memcpy (virtual_buffer
, raw_buffer
, DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum
));
544 char *ptr
, buf
[1024];
548 for (j
= 0; j
< DEPRECATED_REGISTER_RAW_SIZE (regnum
); j
++)
550 int idx
= TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
? j
551 : DEPRECATED_REGISTER_RAW_SIZE (regnum
) - 1 - j
;
552 sprintf (ptr
, "%02x", (unsigned char) raw_buffer
[idx
]);
555 ui_out_field_string (uiout
, "value", buf
);
556 /*fputs_filtered (buf, gdb_stdout); */
560 val_print (register_type (current_gdbarch
, regnum
), virtual_buffer
, 0, 0,
561 stb
->stream
, format
, 1, 0, Val_pretty_default
);
562 ui_out_field_stream (uiout
, "value", stb
);
563 ui_out_stream_delete (stb
);
568 /* Write given values into registers. The registers and values are
569 given as pairs. The corresponding MI command is
570 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
572 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
580 /* Note that the test for a valid register must include checking the
581 REGISTER_NAME because NUM_REGS may be allocated for the union of
582 the register sets within a family of related processors. In this
583 case, some entries of REGISTER_NAME will change depending upon
584 the particular processor being debugged. */
590 xasprintf (&mi_error_message
,
591 "mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
595 format
= (int) argv
[0][0];
597 if (!target_has_registers
)
599 xasprintf (&mi_error_message
,
600 "mi_cmd_data_write_register_values: No registers.");
606 xasprintf (&mi_error_message
,
607 "mi_cmd_data_write_register_values: No regs and values specified.");
613 xasprintf (&mi_error_message
,
614 "mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
618 for (i
= 1; i
< argc
; i
= i
+ 2)
620 regnum
= atoi (argv
[i
]);
624 && REGISTER_NAME (regnum
) != NULL
625 && *REGISTER_NAME (regnum
) != '\000')
628 struct cleanup
*old_chain
;
630 /* Get the value as a number */
631 value
= parse_and_eval_address (argv
[i
+ 1]);
632 /* Get the value into an array */
633 buffer
= xmalloc (DEPRECATED_REGISTER_SIZE
);
634 old_chain
= make_cleanup (xfree
, buffer
);
635 store_signed_integer (buffer
, DEPRECATED_REGISTER_SIZE
, value
);
637 deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (regnum
), buffer
, DEPRECATED_REGISTER_RAW_SIZE (regnum
));
638 /* Free the buffer. */
639 do_cleanups (old_chain
);
643 xasprintf (&mi_error_message
, "bad register number");
651 /*This is commented out because we decided it was not useful. I leave
652 it, just in case. ezannoni:1999-12-08 */
654 /* Assign a value to a variable. The expression argument must be in
655 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
658 mi_cmd_data_assign (char *command
, char **argv
, int argc
)
660 struct expression
*expr
;
661 struct cleanup
*old_chain
;
665 xasprintf (&mi_error_message
,
666 "mi_cmd_data_assign: Usage: -data-assign expression");
670 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
671 01-12-1999: Need to decide what to do with this for libgdb purposes. */
673 expr
= parse_expression (argv
[0]);
674 old_chain
= make_cleanup (free_current_contents
, &expr
);
675 evaluate_expression (expr
);
676 do_cleanups (old_chain
);
681 /* Evaluate the value of the argument. The argument is an
682 expression. If the expression contains spaces it needs to be
683 included in double quotes. */
685 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
687 struct expression
*expr
;
688 struct cleanup
*old_chain
= NULL
;
690 struct ui_stream
*stb
= NULL
;
692 stb
= ui_out_stream_new (uiout
);
696 xasprintf (&mi_error_message
,
697 "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
701 expr
= parse_expression (argv
[0]);
703 old_chain
= make_cleanup (free_current_contents
, &expr
);
705 val
= evaluate_expression (expr
);
707 /* Print the result of the expression evaluation. */
708 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
),
709 VALUE_EMBEDDED_OFFSET (val
), VALUE_ADDRESS (val
),
710 stb
->stream
, 0, 0, 0, 0);
712 ui_out_field_stream (uiout
, "value", stb
);
713 ui_out_stream_delete (stb
);
715 do_cleanups (old_chain
);
721 mi_cmd_target_download (char *args
, int from_tty
)
724 struct cleanup
*old_cleanups
= NULL
;
726 xasprintf (&run
, "load %s", args
);
727 old_cleanups
= make_cleanup (xfree
, run
);
728 execute_command (run
, from_tty
);
730 do_cleanups (old_cleanups
);
734 /* Connect to the remote target. */
736 mi_cmd_target_select (char *args
, int from_tty
)
739 struct cleanup
*old_cleanups
= NULL
;
741 xasprintf (&run
, "target %s", args
);
742 old_cleanups
= make_cleanup (xfree
, run
);
744 /* target-select is always synchronous. once the call has returned
745 we know that we are connected. */
746 /* NOTE: At present all targets that are connected are also
747 (implicitly) talking to a halted target. In the future this may
749 execute_command (run
, from_tty
);
751 do_cleanups (old_cleanups
);
753 /* Issue the completion message here. */
754 if (last_async_command
)
755 fputs_unfiltered (last_async_command
, raw_stdout
);
756 fputs_unfiltered ("^connected", raw_stdout
);
757 mi_out_put (uiout
, raw_stdout
);
758 mi_out_rewind (uiout
);
759 fputs_unfiltered ("\n", raw_stdout
);
760 do_exec_cleanups (ALL_CLEANUPS
);
766 ADDR: start address of data to be dumped.
767 WORD-FORMAT: a char indicating format for the ``word''. See
769 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
770 NR_ROW: Number of rows.
771 NR_COL: The number of colums (words per row).
772 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
773 ASCHAR for unprintable characters.
775 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
776 displayes them. Returns:
778 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
781 The number of bytes read is SIZE*ROW*COL. */
784 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
786 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
792 struct type
*word_type
;
805 static struct mi_opt opts
[] =
807 {"o", OFFSET_OPT
, 1},
813 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
817 switch ((enum opt
) opt
)
820 offset
= atol (optarg
);
827 if (argc
< 5 || argc
> 6)
829 xasprintf (&mi_error_message
,
830 "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
834 /* Extract all the arguments. */
836 /* Start address of the memory dump. */
837 addr
= parse_and_eval_address (argv
[0]) + offset
;
838 /* The format character to use when displaying a memory word. See
839 the ``x'' command. */
840 word_format
= argv
[1][0];
841 /* The size of the memory word. */
842 word_size
= atol (argv
[2]);
846 word_type
= builtin_type_int8
;
850 word_type
= builtin_type_int16
;
854 word_type
= builtin_type_int32
;
858 word_type
= builtin_type_int64
;
862 word_type
= builtin_type_int8
;
865 /* The number of rows */
866 nr_rows
= atol (argv
[3]);
869 xasprintf (&mi_error_message
,
870 "mi_cmd_data_read_memory: invalid number of rows.");
873 /* number of bytes per row. */
874 nr_cols
= atol (argv
[4]);
877 xasprintf (&mi_error_message
,
878 "mi_cmd_data_read_memory: invalid number of columns.");
881 /* The un-printable character when printing ascii. */
887 /* create a buffer and read it in. */
888 total_bytes
= word_size
* nr_rows
* nr_cols
;
889 mbuf
= xcalloc (total_bytes
, 1);
890 make_cleanup (xfree
, mbuf
);
892 while (nr_bytes
< total_bytes
)
895 long num
= target_read_memory_partial (addr
+ nr_bytes
, mbuf
+ nr_bytes
,
896 total_bytes
- nr_bytes
,
903 /* output the header information. */
904 ui_out_field_core_addr (uiout
, "addr", addr
);
905 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
906 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
907 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
908 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
909 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
910 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
912 /* Build the result as a two dimentional table. */
914 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
915 struct cleanup
*cleanup_list_memory
;
918 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
919 for (row
= 0, row_byte
= 0;
921 row
++, row_byte
+= nr_cols
* word_size
)
925 struct cleanup
*cleanup_tuple
;
926 struct cleanup
*cleanup_list_data
;
927 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
928 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
929 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
930 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
931 for (col
= 0, col_byte
= row_byte
;
933 col
++, col_byte
+= word_size
)
935 if (col_byte
+ word_size
> nr_bytes
)
937 ui_out_field_string (uiout
, NULL
, "N/A");
941 ui_file_rewind (stream
->stream
);
942 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
943 word_asize
, stream
->stream
);
944 ui_out_field_stream (uiout
, NULL
, stream
);
947 do_cleanups (cleanup_list_data
);
951 ui_file_rewind (stream
->stream
);
952 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
954 if (byte
>= nr_bytes
)
956 fputc_unfiltered ('X', stream
->stream
);
958 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
960 fputc_unfiltered (aschar
, stream
->stream
);
963 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
965 ui_out_field_stream (uiout
, "ascii", stream
);
967 do_cleanups (cleanup_tuple
);
969 ui_out_stream_delete (stream
);
970 do_cleanups (cleanup_list_memory
);
972 do_cleanups (cleanups
);
976 /* DATA-MEMORY-WRITE:
978 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
979 offset from the beginning of the memory grid row where the cell to
981 ADDR: start address of the row in the memory grid where the memory
982 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
983 the location to write to.
984 FORMAT: a char indicating format for the ``word''. See
986 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
987 VALUE: value to be written into the memory address.
989 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
993 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
998 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
999 enough when using a compiler other than GCC. */
1002 struct cleanup
*old_chain
;
1010 static struct mi_opt opts
[] =
1012 {"o", OFFSET_OPT
, 1},
1018 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
1022 switch ((enum opt
) opt
)
1025 offset
= atol (optarg
);
1034 xasprintf (&mi_error_message
,
1035 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1036 return MI_CMD_ERROR
;
1039 /* Extract all the arguments. */
1040 /* Start address of the memory dump. */
1041 addr
= parse_and_eval_address (argv
[0]);
1042 /* The format character to use when displaying a memory word. See
1043 the ``x'' command. */
1044 word_format
= argv
[1][0];
1045 /* The size of the memory word. */
1046 word_size
= atol (argv
[2]);
1048 /* Calculate the real address of the write destination. */
1049 addr
+= (offset
* word_size
);
1051 /* Get the value as a number */
1052 value
= parse_and_eval_address (argv
[3]);
1053 /* Get the value into an array */
1054 buffer
= xmalloc (word_size
);
1055 old_chain
= make_cleanup (xfree
, buffer
);
1056 store_signed_integer (buffer
, word_size
, value
);
1057 /* Write it down to memory */
1058 write_memory (addr
, buffer
, word_size
);
1059 /* Free the buffer. */
1060 do_cleanups (old_chain
);
1065 /* Execute a command within a safe environment.
1066 Return <0 for error; >=0 for ok.
1068 args->action will tell mi_execute_command what action
1069 to perfrom after the given command has executed (display/supress
1070 prompt, display error). */
1073 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1075 struct captured_mi_execute_command_args
*args
=
1076 (struct captured_mi_execute_command_args
*) data
;
1077 struct mi_parse
*context
= args
->command
;
1079 switch (context
->op
)
1083 /* A MI command was read from the input stream */
1085 /* FIXME: gdb_???? */
1086 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1087 context
->token
, context
->command
, context
->args
);
1088 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1089 condition expression, each function should return an
1090 indication of what action is required and then switch on
1092 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1093 args
->rc
= mi_cmd_execute (context
);
1095 if (!target_can_async_p () || !target_executing
)
1097 /* print the result if there were no errors
1099 Remember that on the way out of executing a command, you have
1100 to directly use the mi_interp's uiout, since the command could
1101 have reset the interpreter, in which case the current uiout
1102 will most likely crash in the mi_out_* routines. */
1103 if (args
->rc
== MI_CMD_DONE
)
1105 fputs_unfiltered (context
->token
, raw_stdout
);
1106 fputs_unfiltered ("^done", raw_stdout
);
1107 mi_out_put (uiout
, raw_stdout
);
1108 mi_out_rewind (uiout
);
1109 fputs_unfiltered ("\n", raw_stdout
);
1111 else if (args
->rc
== MI_CMD_ERROR
)
1113 if (mi_error_message
)
1115 fputs_unfiltered (context
->token
, raw_stdout
);
1116 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1117 fputstr_unfiltered (mi_error_message
, '"', raw_stdout
);
1118 xfree (mi_error_message
);
1119 fputs_unfiltered ("\"\n", raw_stdout
);
1121 mi_out_rewind (uiout
);
1123 else if (args
->rc
== MI_CMD_CAUGHT_ERROR
)
1125 mi_out_rewind (uiout
);
1126 args
->action
= EXECUTE_COMMAND_DISPLAY_ERROR
;
1130 mi_out_rewind (uiout
);
1132 else if (sync_execution
)
1134 /* Don't print the prompt. We are executing the target in
1135 synchronous mode. */
1136 args
->action
= EXECUTE_COMMAND_SUPRESS_PROMPT
;
1142 /* A CLI command was read from the input stream */
1143 /* This will be removed as soon as we have a complete set of
1145 /* echo the command on the console. */
1146 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1147 mi_execute_cli_command (context
->command
, 0, NULL
);
1149 /* If we changed interpreters, DON'T print out anything. */
1150 if (current_interp_named_p (INTERP_MI
)
1151 || current_interp_named_p (INTERP_MI1
)
1152 || current_interp_named_p (INTERP_MI2
)
1153 || current_interp_named_p (INTERP_MI3
))
1155 /* print the result */
1156 /* FIXME: Check for errors here. */
1157 fputs_unfiltered (context
->token
, raw_stdout
);
1158 fputs_unfiltered ("^done", raw_stdout
);
1159 mi_out_put (uiout
, raw_stdout
);
1160 mi_out_rewind (uiout
);
1161 fputs_unfiltered ("\n", raw_stdout
);
1162 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1163 args
->rc
= MI_CMD_DONE
;
1174 mi_execute_command (char *cmd
, int from_tty
)
1176 struct mi_parse
*command
;
1177 struct captured_mi_execute_command_args args
;
1178 struct ui_out
*saved_uiout
= uiout
;
1181 /* This is to handle EOF (^D). We just quit gdb. */
1182 /* FIXME: we should call some API function here. */
1184 quit_force (NULL
, from_tty
);
1186 command
= mi_parse (cmd
);
1188 if (command
!= NULL
)
1190 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
1191 be pushed even further down or even eliminated? */
1192 args
.command
= command
;
1193 result
= catch_exceptions (uiout
, captured_mi_execute_command
, &args
, "",
1196 if (args
.action
== EXECUTE_COMMAND_SUPRESS_PROMPT
)
1198 /* The command is executing synchronously. Bail out early
1199 suppressing the finished prompt. */
1200 mi_parse_free (command
);
1203 if (args
.action
== EXECUTE_COMMAND_DISPLAY_ERROR
|| result
< 0)
1205 char *msg
= error_last_message ();
1206 struct cleanup
*cleanup
= make_cleanup (xfree
, msg
);
1207 /* The command execution failed and error() was called
1209 fputs_unfiltered (command
->token
, raw_stdout
);
1210 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1211 fputstr_unfiltered (msg
, '"', raw_stdout
);
1212 fputs_unfiltered ("\"\n", raw_stdout
);
1214 mi_parse_free (command
);
1217 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1218 gdb_flush (raw_stdout
);
1219 /* print any buffered hook code */
1223 static enum mi_cmd_result
1224 mi_cmd_execute (struct mi_parse
*parse
)
1226 if (parse
->cmd
->argv_func
!= NULL
1227 || parse
->cmd
->args_func
!= NULL
)
1229 /* FIXME: We need to save the token because the command executed
1230 may be asynchronous and need to print the token again.
1231 In the future we can pass the token down to the func
1232 and get rid of the last_async_command */
1233 /* The problem here is to keep the token around when we launch
1234 the target, and we want to interrupt it later on. The
1235 interrupt command will have its own token, but when the
1236 target stops, we must display the token corresponding to the
1237 last execution command given. So we have another string where
1238 we copy the token (previous_async_command), if this was
1239 indeed the token of an execution command, and when we stop we
1240 print that one. This is possible because the interrupt
1241 command, when over, will copy that token back into the
1242 default token string (last_async_command). */
1244 if (target_executing
)
1246 if (!previous_async_command
)
1247 previous_async_command
= xstrdup (last_async_command
);
1248 if (strcmp (parse
->command
, "exec-interrupt"))
1250 fputs_unfiltered (parse
->token
, raw_stdout
);
1251 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1252 fputs_unfiltered ("Cannot execute command ", raw_stdout
);
1253 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1254 fputs_unfiltered (" while target running", raw_stdout
);
1255 fputs_unfiltered ("\"\n", raw_stdout
);
1256 return MI_CMD_ERROR
;
1259 last_async_command
= xstrdup (parse
->token
);
1260 make_exec_cleanup (free_current_contents
, &last_async_command
);
1261 /* FIXME: DELETE THIS! */
1262 if (parse
->cmd
->args_func
!= NULL
)
1263 return parse
->cmd
->args_func (parse
->args
, 0 /*from_tty */ );
1264 return parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1266 else if (parse
->cmd
->cli
.cmd
!= 0)
1268 /* FIXME: DELETE THIS. */
1269 /* The operation is still implemented by a cli command */
1270 /* Must be a synchronous one */
1271 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1277 /* FIXME: DELETE THIS. */
1278 fputs_unfiltered (parse
->token
, raw_stdout
);
1279 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1280 fputs_unfiltered ("Undefined mi command: ", raw_stdout
);
1281 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1282 fputs_unfiltered (" (missing implementation)", raw_stdout
);
1283 fputs_unfiltered ("\"\n", raw_stdout
);
1284 return MI_CMD_ERROR
;
1288 /* FIXME: This is just a hack so we can get some extra commands going.
1289 We don't want to channel things through the CLI, but call libgdb directly */
1290 /* Use only for synchronous commands */
1293 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1297 struct cleanup
*old_cleanups
;
1300 xasprintf (&run
, "%s %s", cmd
, args
);
1302 run
= xstrdup (cmd
);
1304 /* FIXME: gdb_???? */
1305 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1307 old_cleanups
= make_cleanup (xfree
, run
);
1308 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1309 do_cleanups (old_cleanups
);
1315 mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
)
1317 struct cleanup
*old_cleanups
;
1321 if (target_can_async_p ())
1323 async_args
= (char *) xmalloc (strlen (args
) + 2);
1324 make_exec_cleanup (free
, async_args
);
1325 strcpy (async_args
, args
);
1326 strcat (async_args
, "&");
1327 xasprintf (&run
, "%s %s", mi
, async_args
);
1328 make_exec_cleanup (free
, run
);
1329 add_continuation (mi_exec_async_cli_cmd_continuation
, NULL
);
1330 old_cleanups
= NULL
;
1334 xasprintf (&run
, "%s %s", mi
, args
);
1335 old_cleanups
= make_cleanup (xfree
, run
);
1338 if (!target_can_async_p ())
1340 /* NOTE: For synchronous targets asynchronous behavour is faked by
1341 printing out the GDB prompt before we even try to execute the
1343 if (last_async_command
)
1344 fputs_unfiltered (last_async_command
, raw_stdout
);
1345 fputs_unfiltered ("^running\n", raw_stdout
);
1346 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1347 gdb_flush (raw_stdout
);
1351 /* FIXME: cagney/1999-11-29: Printing this message before
1352 calling execute_command is wrong. It should only be printed
1353 once gdb has confirmed that it really has managed to send a
1354 run command to the target. */
1355 if (last_async_command
)
1356 fputs_unfiltered (last_async_command
, raw_stdout
);
1357 fputs_unfiltered ("^running\n", raw_stdout
);
1360 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1362 if (!target_can_async_p ())
1364 /* Do this before doing any printing. It would appear that some
1365 print code leaves garbage around in the buffer. */
1366 do_cleanups (old_cleanups
);
1367 /* If the target was doing the operation synchronously we fake
1368 the stopped message. */
1369 if (last_async_command
)
1370 fputs_unfiltered (last_async_command
, raw_stdout
);
1371 fputs_unfiltered ("*stopped", raw_stdout
);
1372 mi_out_put (uiout
, raw_stdout
);
1373 mi_out_rewind (uiout
);
1374 fputs_unfiltered ("\n", raw_stdout
);
1375 return MI_CMD_QUIET
;
1381 mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
)
1383 if (last_async_command
)
1384 fputs_unfiltered (last_async_command
, raw_stdout
);
1385 fputs_unfiltered ("*stopped", raw_stdout
);
1386 mi_out_put (uiout
, raw_stdout
);
1387 fputs_unfiltered ("\n", raw_stdout
);
1388 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1389 gdb_flush (raw_stdout
);
1390 do_exec_cleanups (ALL_CLEANUPS
);
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 (!current_interp_named_p (INTERP_MI
)
1406 && !current_interp_named_p (INTERP_MI1
))
1409 update_threshold
.tv_sec
= 0;
1410 update_threshold
.tv_usec
= 500000;
1411 gettimeofday (&time_now
, NULL
);
1413 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1414 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1416 if (delta
.tv_usec
< 0)
1419 delta
.tv_usec
+= 1000000;
1422 new_section
= (previous_sect_name
?
1423 strcmp (previous_sect_name
, section_name
) : 1);
1426 struct cleanup
*cleanup_tuple
;
1427 xfree (previous_sect_name
);
1428 previous_sect_name
= xstrdup (section_name
);
1430 if (last_async_command
)
1431 fputs_unfiltered (last_async_command
, raw_stdout
);
1432 fputs_unfiltered ("+download", raw_stdout
);
1433 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1434 ui_out_field_string (uiout
, "section", section_name
);
1435 ui_out_field_int (uiout
, "section-size", total_section
);
1436 ui_out_field_int (uiout
, "total-size", grand_total
);
1437 do_cleanups (cleanup_tuple
);
1438 mi_out_put (uiout
, raw_stdout
);
1439 fputs_unfiltered ("\n", raw_stdout
);
1440 gdb_flush (raw_stdout
);
1443 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1444 delta
.tv_usec
>= update_threshold
.tv_usec
)
1446 struct cleanup
*cleanup_tuple
;
1447 last_update
.tv_sec
= time_now
.tv_sec
;
1448 last_update
.tv_usec
= time_now
.tv_usec
;
1449 if (last_async_command
)
1450 fputs_unfiltered (last_async_command
, raw_stdout
);
1451 fputs_unfiltered ("+download", raw_stdout
);
1452 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1453 ui_out_field_string (uiout
, "section", section_name
);
1454 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1455 ui_out_field_int (uiout
, "section-size", total_section
);
1456 ui_out_field_int (uiout
, "total-sent", total_sent
);
1457 ui_out_field_int (uiout
, "total-size", grand_total
);
1458 do_cleanups (cleanup_tuple
);
1459 mi_out_put (uiout
, raw_stdout
);
1460 fputs_unfiltered ("\n", raw_stdout
);
1461 gdb_flush (raw_stdout
);
1466 mi_setup_architecture_data (void)
1468 old_regs
= xmalloc ((NUM_REGS
+ NUM_PSEUDO_REGS
) * MAX_REGISTER_SIZE
+ 1);
1469 memset (old_regs
, 0, (NUM_REGS
+ NUM_PSEUDO_REGS
) * MAX_REGISTER_SIZE
+ 1);
1473 _initialize_mi_main (void)
1475 DEPRECATED_REGISTER_GDBARCH_SWAP (old_regs
);
1476 deprecated_register_gdbarch_swap (NULL
, 0, mi_setup_architecture_data
);