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
[MAX_REGISTER_SIZE
];
515 char virtual_buffer
[MAX_REGISTER_SIZE
];
520 static struct ui_stream
*stb
= NULL
;
522 stb
= ui_out_stream_new (uiout
);
527 frame_register (deprecated_selected_frame
, regnum
, &optim
, &lval
, &addr
,
528 &realnum
, raw_buffer
);
532 xasprintf (&mi_error_message
, "Optimized out");
536 /* Convert raw data to virtual format if necessary. */
538 if (REGISTER_CONVERTIBLE (regnum
))
540 REGISTER_CONVERT_TO_VIRTUAL (regnum
,
541 register_type (current_gdbarch
, regnum
),
542 raw_buffer
, virtual_buffer
);
545 memcpy (virtual_buffer
, raw_buffer
, REGISTER_VIRTUAL_SIZE (regnum
));
550 char *ptr
, buf
[1024];
554 for (j
= 0; j
< REGISTER_RAW_SIZE (regnum
); j
++)
556 register int idx
= TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
? j
557 : REGISTER_RAW_SIZE (regnum
) - 1 - j
;
558 sprintf (ptr
, "%02x", (unsigned char) raw_buffer
[idx
]);
561 ui_out_field_string (uiout
, "value", buf
);
562 /*fputs_filtered (buf, gdb_stdout); */
566 val_print (register_type (current_gdbarch
, regnum
), virtual_buffer
, 0, 0,
567 stb
->stream
, format
, 1, 0, Val_pretty_default
);
568 ui_out_field_stream (uiout
, "value", stb
);
569 ui_out_stream_delete (stb
);
574 /* Write given values into registers. The registers and values are
575 given as pairs. The corresponding MI command is
576 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
578 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
586 /* Note that the test for a valid register must include checking the
587 REGISTER_NAME because NUM_REGS may be allocated for the union of
588 the register sets within a family of related processors. In this
589 case, some entries of REGISTER_NAME will change depending upon
590 the particular processor being debugged. */
596 xasprintf (&mi_error_message
,
597 "mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
601 format
= (int) argv
[0][0];
603 if (!target_has_registers
)
605 xasprintf (&mi_error_message
,
606 "mi_cmd_data_write_register_values: No registers.");
612 xasprintf (&mi_error_message
,
613 "mi_cmd_data_write_register_values: No regs and values specified.");
619 xasprintf (&mi_error_message
,
620 "mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
624 for (i
= 1; i
< argc
; i
= i
+ 2)
626 regnum
= atoi (argv
[i
]);
630 && REGISTER_NAME (regnum
) != NULL
631 && *REGISTER_NAME (regnum
) != '\000')
634 struct cleanup
*old_chain
;
636 /* Get the value as a number */
637 value
= parse_and_eval_address (argv
[i
+ 1]);
638 /* Get the value into an array */
639 buffer
= xmalloc (DEPRECATED_REGISTER_SIZE
);
640 old_chain
= make_cleanup (xfree
, buffer
);
641 store_signed_integer (buffer
, DEPRECATED_REGISTER_SIZE
, value
);
643 deprecated_write_register_bytes (REGISTER_BYTE (regnum
), buffer
, REGISTER_RAW_SIZE (regnum
));
644 /* Free the buffer. */
645 do_cleanups (old_chain
);
649 xasprintf (&mi_error_message
, "bad register number");
657 /*This is commented out because we decided it was not useful. I leave
658 it, just in case. ezannoni:1999-12-08 */
660 /* Assign a value to a variable. The expression argument must be in
661 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
664 mi_cmd_data_assign (char *command
, char **argv
, int argc
)
666 struct expression
*expr
;
667 struct cleanup
*old_chain
;
671 xasprintf (&mi_error_message
,
672 "mi_cmd_data_assign: Usage: -data-assign expression");
676 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
677 01-12-1999: Need to decide what to do with this for libgdb purposes. */
679 expr
= parse_expression (argv
[0]);
680 old_chain
= make_cleanup (free_current_contents
, &expr
);
681 evaluate_expression (expr
);
682 do_cleanups (old_chain
);
687 /* Evaluate the value of the argument. The argument is an
688 expression. If the expression contains spaces it needs to be
689 included in double quotes. */
691 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
693 struct expression
*expr
;
694 struct cleanup
*old_chain
= NULL
;
696 struct ui_stream
*stb
= NULL
;
698 stb
= ui_out_stream_new (uiout
);
702 xasprintf (&mi_error_message
,
703 "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
707 expr
= parse_expression (argv
[0]);
709 old_chain
= make_cleanup (free_current_contents
, &expr
);
711 val
= evaluate_expression (expr
);
713 /* Print the result of the expression evaluation. */
714 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
),
715 VALUE_EMBEDDED_OFFSET (val
), VALUE_ADDRESS (val
),
716 stb
->stream
, 0, 0, 0, 0);
718 ui_out_field_stream (uiout
, "value", stb
);
719 ui_out_stream_delete (stb
);
721 do_cleanups (old_chain
);
727 mi_cmd_target_download (char *args
, int from_tty
)
730 struct cleanup
*old_cleanups
= NULL
;
732 xasprintf (&run
, "load %s", args
);
733 old_cleanups
= make_cleanup (xfree
, run
);
734 execute_command (run
, from_tty
);
736 do_cleanups (old_cleanups
);
740 /* Connect to the remote target. */
742 mi_cmd_target_select (char *args
, int from_tty
)
745 struct cleanup
*old_cleanups
= NULL
;
747 xasprintf (&run
, "target %s", args
);
748 old_cleanups
= make_cleanup (xfree
, run
);
750 /* target-select is always synchronous. once the call has returned
751 we know that we are connected. */
752 /* NOTE: At present all targets that are connected are also
753 (implicitly) talking to a halted target. In the future this may
755 execute_command (run
, from_tty
);
757 do_cleanups (old_cleanups
);
759 /* Issue the completion message here. */
760 if (last_async_command
)
761 fputs_unfiltered (last_async_command
, raw_stdout
);
762 fputs_unfiltered ("^connected", raw_stdout
);
763 mi_out_put (uiout
, raw_stdout
);
764 mi_out_rewind (uiout
);
765 fputs_unfiltered ("\n", raw_stdout
);
766 do_exec_cleanups (ALL_CLEANUPS
);
772 ADDR: start address of data to be dumped.
773 WORD-FORMAT: a char indicating format for the ``word''. See
775 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
776 NR_ROW: Number of rows.
777 NR_COL: The number of colums (words per row).
778 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
779 ASCHAR for unprintable characters.
781 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
782 displayes them. Returns:
784 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
787 The number of bytes read is SIZE*ROW*COL. */
790 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
792 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
798 struct type
*word_type
;
811 static struct mi_opt opts
[] =
813 {"o", OFFSET_OPT
, 1},
819 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
823 switch ((enum opt
) opt
)
826 offset
= atol (optarg
);
833 if (argc
< 5 || argc
> 6)
835 xasprintf (&mi_error_message
,
836 "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
840 /* Extract all the arguments. */
842 /* Start address of the memory dump. */
843 addr
= parse_and_eval_address (argv
[0]) + offset
;
844 /* The format character to use when displaying a memory word. See
845 the ``x'' command. */
846 word_format
= argv
[1][0];
847 /* The size of the memory word. */
848 word_size
= atol (argv
[2]);
852 word_type
= builtin_type_int8
;
856 word_type
= builtin_type_int16
;
860 word_type
= builtin_type_int32
;
864 word_type
= builtin_type_int64
;
868 word_type
= builtin_type_int8
;
871 /* The number of rows */
872 nr_rows
= atol (argv
[3]);
875 xasprintf (&mi_error_message
,
876 "mi_cmd_data_read_memory: invalid number of rows.");
879 /* number of bytes per row. */
880 nr_cols
= atol (argv
[4]);
883 xasprintf (&mi_error_message
,
884 "mi_cmd_data_read_memory: invalid number of columns.");
886 /* The un-printable character when printing ascii. */
892 /* create a buffer and read it in. */
893 total_bytes
= word_size
* nr_rows
* nr_cols
;
894 mbuf
= xcalloc (total_bytes
, 1);
895 make_cleanup (xfree
, mbuf
);
898 xasprintf (&mi_error_message
,
899 "mi_cmd_data_read_memory: out of memory.");
903 while (nr_bytes
< total_bytes
)
906 long num
= target_read_memory_partial (addr
+ nr_bytes
, mbuf
+ nr_bytes
,
907 total_bytes
- nr_bytes
,
914 /* output the header information. */
915 ui_out_field_core_addr (uiout
, "addr", addr
);
916 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
917 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
918 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
919 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
920 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
921 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
923 /* Build the result as a two dimentional table. */
925 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
926 struct cleanup
*cleanup_list_memory
;
929 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
930 for (row
= 0, row_byte
= 0;
932 row
++, row_byte
+= nr_cols
* word_size
)
936 struct cleanup
*cleanup_tuple
;
937 struct cleanup
*cleanup_list_data
;
938 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
939 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
940 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
941 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
942 for (col
= 0, col_byte
= row_byte
;
944 col
++, col_byte
+= word_size
)
946 if (col_byte
+ word_size
> nr_bytes
)
948 ui_out_field_string (uiout
, NULL
, "N/A");
952 ui_file_rewind (stream
->stream
);
953 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
954 word_asize
, stream
->stream
);
955 ui_out_field_stream (uiout
, NULL
, stream
);
958 do_cleanups (cleanup_list_data
);
962 ui_file_rewind (stream
->stream
);
963 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
965 if (byte
>= nr_bytes
)
967 fputc_unfiltered ('X', stream
->stream
);
969 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
971 fputc_unfiltered (aschar
, stream
->stream
);
974 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
976 ui_out_field_stream (uiout
, "ascii", stream
);
978 do_cleanups (cleanup_tuple
);
980 ui_out_stream_delete (stream
);
981 do_cleanups (cleanup_list_memory
);
983 do_cleanups (cleanups
);
987 /* DATA-MEMORY-WRITE:
989 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
990 offset from the beginning of the memory grid row where the cell to
992 ADDR: start address of the row in the memory grid where the memory
993 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
994 the location to write to.
995 FORMAT: a char indicating format for the ``word''. See
997 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
998 VALUE: value to be written into the memory address.
1000 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1004 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
1009 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1010 enough when using a compiler other than GCC. */
1013 struct cleanup
*old_chain
;
1021 static struct mi_opt opts
[] =
1023 {"o", OFFSET_OPT
, 1},
1029 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
1033 switch ((enum opt
) opt
)
1036 offset
= atol (optarg
);
1045 xasprintf (&mi_error_message
,
1046 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1047 return MI_CMD_ERROR
;
1050 /* Extract all the arguments. */
1051 /* Start address of the memory dump. */
1052 addr
= parse_and_eval_address (argv
[0]);
1053 /* The format character to use when displaying a memory word. See
1054 the ``x'' command. */
1055 word_format
= argv
[1][0];
1056 /* The size of the memory word. */
1057 word_size
= atol (argv
[2]);
1059 /* Calculate the real address of the write destination. */
1060 addr
+= (offset
* word_size
);
1062 /* Get the value as a number */
1063 value
= parse_and_eval_address (argv
[3]);
1064 /* Get the value into an array */
1065 buffer
= xmalloc (word_size
);
1066 old_chain
= make_cleanup (xfree
, buffer
);
1067 store_signed_integer (buffer
, word_size
, value
);
1068 /* Write it down to memory */
1069 write_memory (addr
, buffer
, word_size
);
1070 /* Free the buffer. */
1071 do_cleanups (old_chain
);
1076 /* Execute a command within a safe environment.
1077 Return <0 for error; >=0 for ok.
1079 args->action will tell mi_execute_command what action
1080 to perfrom after the given command has executed (display/supress
1081 prompt, display error). */
1084 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1086 struct captured_mi_execute_command_args
*args
=
1087 (struct captured_mi_execute_command_args
*) data
;
1088 struct mi_parse
*context
= args
->command
;
1090 switch (context
->op
)
1094 /* A MI command was read from the input stream */
1096 /* FIXME: gdb_???? */
1097 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1098 context
->token
, context
->command
, context
->args
);
1099 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1100 condition expression, each function should return an
1101 indication of what action is required and then switch on
1103 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1104 args
->rc
= mi_cmd_execute (context
);
1106 if (!target_can_async_p () || !target_executing
)
1108 /* print the result if there were no errors
1110 Remember that on the way out of executing a command, you have
1111 to directly use the mi_interp's uiout, since the command could
1112 have reset the interpreter, in which case the current uiout
1113 will most likely crash in the mi_out_* routines. */
1114 if (args
->rc
== MI_CMD_DONE
)
1116 fputs_unfiltered (context
->token
, raw_stdout
);
1117 fputs_unfiltered ("^done", raw_stdout
);
1118 mi_out_put (uiout
, raw_stdout
);
1119 mi_out_rewind (uiout
);
1120 fputs_unfiltered ("\n", raw_stdout
);
1122 else if (args
->rc
== MI_CMD_ERROR
)
1124 if (mi_error_message
)
1126 fputs_unfiltered (context
->token
, raw_stdout
);
1127 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1128 fputstr_unfiltered (mi_error_message
, '"', raw_stdout
);
1129 xfree (mi_error_message
);
1130 fputs_unfiltered ("\"\n", raw_stdout
);
1132 mi_out_rewind (uiout
);
1134 else if (args
->rc
== MI_CMD_CAUGHT_ERROR
)
1136 mi_out_rewind (uiout
);
1137 args
->action
= EXECUTE_COMMAND_DISPLAY_ERROR
;
1141 mi_out_rewind (uiout
);
1143 else if (sync_execution
)
1145 /* Don't print the prompt. We are executing the target in
1146 synchronous mode. */
1147 args
->action
= EXECUTE_COMMAND_SUPRESS_PROMPT
;
1153 /* A CLI command was read from the input stream */
1154 /* This will be removed as soon as we have a complete set of
1156 /* echo the command on the console. */
1157 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1158 /* FIXME: If the command string has something that looks like
1159 a format spec (e.g. %s) we will get a core dump */
1160 mi_execute_cli_command ("%s", context
->command
);
1162 /* If we changed interpreters, DON'T print out anything. */
1163 if (current_interp_named_p (INTERP_MI
)
1164 || current_interp_named_p (INTERP_MI1
))
1166 /* print the result */
1167 /* FIXME: Check for errors here. */
1168 fputs_unfiltered (context
->token
, raw_stdout
);
1169 fputs_unfiltered ("^done", raw_stdout
);
1170 mi_out_put (uiout
, raw_stdout
);
1171 mi_out_rewind (uiout
);
1172 fputs_unfiltered ("\n", raw_stdout
);
1173 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1174 args
->rc
= MI_CMD_DONE
;
1185 mi_execute_command (char *cmd
, int from_tty
)
1187 struct mi_parse
*command
;
1188 struct captured_mi_execute_command_args args
;
1189 struct ui_out
*saved_uiout
= uiout
;
1192 /* This is to handle EOF (^D). We just quit gdb. */
1193 /* FIXME: we should call some API function here. */
1195 quit_force (NULL
, from_tty
);
1197 command
= mi_parse (cmd
);
1199 if (command
!= NULL
)
1201 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
1202 be pushed even further down or even eliminated? */
1203 args
.command
= command
;
1204 result
= catch_exceptions (uiout
, captured_mi_execute_command
, &args
, "",
1207 if (args
.action
== EXECUTE_COMMAND_SUPRESS_PROMPT
)
1209 /* The command is executing synchronously. Bail out early
1210 suppressing the finished prompt. */
1211 mi_parse_free (command
);
1214 if (args
.action
== EXECUTE_COMMAND_DISPLAY_ERROR
|| result
< 0)
1216 char *msg
= error_last_message ();
1217 struct cleanup
*cleanup
= make_cleanup (xfree
, msg
);
1218 /* The command execution failed and error() was called
1220 fputs_unfiltered (command
->token
, raw_stdout
);
1221 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1222 fputstr_unfiltered (msg
, '"', raw_stdout
);
1223 fputs_unfiltered ("\"\n", raw_stdout
);
1225 mi_parse_free (command
);
1228 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1229 gdb_flush (raw_stdout
);
1230 /* print any buffered hook code */
1234 static enum mi_cmd_result
1235 mi_cmd_execute (struct mi_parse
*parse
)
1237 if (parse
->cmd
->argv_func
!= NULL
1238 || parse
->cmd
->args_func
!= NULL
)
1240 /* FIXME: We need to save the token because the command executed
1241 may be asynchronous and need to print the token again.
1242 In the future we can pass the token down to the func
1243 and get rid of the last_async_command */
1244 /* The problem here is to keep the token around when we launch
1245 the target, and we want to interrupt it later on. The
1246 interrupt command will have its own token, but when the
1247 target stops, we must display the token corresponding to the
1248 last execution command given. So we have another string where
1249 we copy the token (previous_async_command), if this was
1250 indeed the token of an execution command, and when we stop we
1251 print that one. This is possible because the interrupt
1252 command, when over, will copy that token back into the
1253 default token string (last_async_command). */
1255 if (target_executing
)
1257 if (!previous_async_command
)
1258 previous_async_command
= xstrdup (last_async_command
);
1259 if (strcmp (parse
->command
, "exec-interrupt"))
1261 fputs_unfiltered (parse
->token
, raw_stdout
);
1262 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1263 fputs_unfiltered ("Cannot execute command ", raw_stdout
);
1264 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1265 fputs_unfiltered (" while target running", raw_stdout
);
1266 fputs_unfiltered ("\"\n", raw_stdout
);
1267 return MI_CMD_ERROR
;
1270 last_async_command
= xstrdup (parse
->token
);
1271 make_exec_cleanup (free_current_contents
, &last_async_command
);
1272 /* FIXME: DELETE THIS! */
1273 if (parse
->cmd
->args_func
!= NULL
)
1274 return parse
->cmd
->args_func (parse
->args
, 0 /*from_tty */ );
1275 return parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1277 else if (parse
->cmd
->cli
!= 0)
1279 /* FIXME: DELETE THIS. */
1280 /* The operation is still implemented by a cli command */
1281 /* Must be a synchronous one */
1282 mi_execute_cli_command (parse
->cmd
->cli
, parse
->args
);
1287 /* FIXME: DELETE THIS. */
1288 fputs_unfiltered (parse
->token
, raw_stdout
);
1289 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1290 fputs_unfiltered ("Undefined mi command: ", raw_stdout
);
1291 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1292 fputs_unfiltered (" (missing implementation)", raw_stdout
);
1293 fputs_unfiltered ("\"\n", raw_stdout
);
1294 return MI_CMD_ERROR
;
1298 /* FIXME: This is just a hack so we can get some extra commands going.
1299 We don't want to channel things through the CLI, but call libgdb directly */
1300 /* Use only for synchronous commands */
1303 mi_execute_cli_command (const char *cli
, char *args
)
1307 struct cleanup
*old_cleanups
;
1309 xasprintf (&run
, cli
, args
);
1311 /* FIXME: gdb_???? */
1312 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1314 old_cleanups
= make_cleanup (xfree
, run
);
1315 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1316 do_cleanups (old_cleanups
);
1322 mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
)
1324 struct cleanup
*old_cleanups
;
1328 if (target_can_async_p ())
1330 async_args
= (char *) xmalloc (strlen (args
) + 2);
1331 make_exec_cleanup (free
, async_args
);
1332 strcpy (async_args
, args
);
1333 strcat (async_args
, "&");
1334 xasprintf (&run
, "%s %s", mi
, async_args
);
1335 make_exec_cleanup (free
, run
);
1336 add_continuation (mi_exec_async_cli_cmd_continuation
, NULL
);
1337 old_cleanups
= NULL
;
1341 xasprintf (&run
, "%s %s", mi
, args
);
1342 old_cleanups
= make_cleanup (xfree
, run
);
1345 if (!target_can_async_p ())
1347 /* NOTE: For synchronous targets asynchronous behavour is faked by
1348 printing out the GDB prompt before we even try to execute the
1350 if (last_async_command
)
1351 fputs_unfiltered (last_async_command
, raw_stdout
);
1352 fputs_unfiltered ("^running\n", raw_stdout
);
1353 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1354 gdb_flush (raw_stdout
);
1358 /* FIXME: cagney/1999-11-29: Printing this message before
1359 calling execute_command is wrong. It should only be printed
1360 once gdb has confirmed that it really has managed to send a
1361 run command to the target. */
1362 if (last_async_command
)
1363 fputs_unfiltered (last_async_command
, raw_stdout
);
1364 fputs_unfiltered ("^running\n", raw_stdout
);
1367 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1369 if (!target_can_async_p ())
1371 /* Do this before doing any printing. It would appear that some
1372 print code leaves garbage around in the buffer. */
1373 do_cleanups (old_cleanups
);
1374 /* If the target was doing the operation synchronously we fake
1375 the stopped message. */
1376 if (last_async_command
)
1377 fputs_unfiltered (last_async_command
, raw_stdout
);
1378 fputs_unfiltered ("*stopped", raw_stdout
);
1379 mi_out_put (uiout
, raw_stdout
);
1380 mi_out_rewind (uiout
);
1381 fputs_unfiltered ("\n", raw_stdout
);
1382 return MI_CMD_QUIET
;
1388 mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
)
1390 if (last_async_command
)
1391 fputs_unfiltered (last_async_command
, raw_stdout
);
1392 fputs_unfiltered ("*stopped", raw_stdout
);
1393 mi_out_put (uiout
, raw_stdout
);
1394 fputs_unfiltered ("\n", raw_stdout
);
1395 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1396 gdb_flush (raw_stdout
);
1397 do_exec_cleanups (ALL_CLEANUPS
);
1401 mi_load_progress (const char *section_name
,
1402 unsigned long sent_so_far
,
1403 unsigned long total_section
,
1404 unsigned long total_sent
,
1405 unsigned long grand_total
)
1407 struct timeval time_now
, delta
, update_threshold
;
1408 static struct timeval last_update
;
1409 static char *previous_sect_name
= NULL
;
1412 if (!current_interp_named_p (INTERP_MI
)
1413 && !current_interp_named_p (INTERP_MI1
))
1416 update_threshold
.tv_sec
= 0;
1417 update_threshold
.tv_usec
= 500000;
1418 gettimeofday (&time_now
, NULL
);
1420 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1421 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1423 if (delta
.tv_usec
< 0)
1426 delta
.tv_usec
+= 1000000;
1429 new_section
= (previous_sect_name
?
1430 strcmp (previous_sect_name
, section_name
) : 1);
1433 struct cleanup
*cleanup_tuple
;
1434 xfree (previous_sect_name
);
1435 previous_sect_name
= xstrdup (section_name
);
1437 if (last_async_command
)
1438 fputs_unfiltered (last_async_command
, raw_stdout
);
1439 fputs_unfiltered ("+download", raw_stdout
);
1440 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1441 ui_out_field_string (uiout
, "section", section_name
);
1442 ui_out_field_int (uiout
, "section-size", total_section
);
1443 ui_out_field_int (uiout
, "total-size", grand_total
);
1444 do_cleanups (cleanup_tuple
);
1445 mi_out_put (uiout
, raw_stdout
);
1446 fputs_unfiltered ("\n", raw_stdout
);
1447 gdb_flush (raw_stdout
);
1450 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1451 delta
.tv_usec
>= update_threshold
.tv_usec
)
1453 struct cleanup
*cleanup_tuple
;
1454 last_update
.tv_sec
= time_now
.tv_sec
;
1455 last_update
.tv_usec
= time_now
.tv_usec
;
1456 if (last_async_command
)
1457 fputs_unfiltered (last_async_command
, raw_stdout
);
1458 fputs_unfiltered ("+download", raw_stdout
);
1459 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1460 ui_out_field_string (uiout
, "section", section_name
);
1461 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1462 ui_out_field_int (uiout
, "section-size", total_section
);
1463 ui_out_field_int (uiout
, "total-sent", total_sent
);
1464 ui_out_field_int (uiout
, "total-size", grand_total
);
1465 do_cleanups (cleanup_tuple
);
1466 mi_out_put (uiout
, raw_stdout
);
1467 fputs_unfiltered ("\n", raw_stdout
);
1468 gdb_flush (raw_stdout
);
1473 mi_setup_architecture_data (void)
1475 /* don't trust REGISTER_BYTES to be zero. */
1476 old_regs
= xmalloc (REGISTER_BYTES
+ 1);
1477 memset (old_regs
, 0, REGISTER_BYTES
+ 1);
1481 _initialize_mi_main (void)
1483 register_gdbarch_swap (&old_regs
, sizeof (old_regs
), NULL
);
1484 register_gdbarch_swap (NULL
, 0, mi_setup_architecture_data
);