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() */
54 /* Enumerations of the actions that may result from calling
55 captured_mi_execute_command */
57 enum captured_mi_execute_command_actions
59 EXECUTE_COMMAND_DISPLAY_PROMPT
,
60 EXECUTE_COMMAND_SUPRESS_PROMPT
,
61 EXECUTE_COMMAND_DISPLAY_ERROR
64 /* This structure is used to pass information from captured_mi_execute_command
65 to mi_execute_command. */
66 struct captured_mi_execute_command_args
68 /* This return result of the MI command (output) */
69 enum mi_cmd_result rc
;
71 /* What action to perform when the call is finished (output) */
72 enum captured_mi_execute_command_actions action
;
74 /* The command context to be executed (input) */
75 struct mi_parse
*command
;
79 struct ui_file
*raw_stdout
;
81 /* The token of the last asynchronous command */
82 static char *last_async_command
;
83 static char *previous_async_command
;
84 char *mi_error_message
;
85 static char *old_regs
;
87 extern void _initialize_mi_main (void);
88 static enum mi_cmd_result
mi_cmd_execute (struct mi_parse
*parse
);
90 static void mi_execute_cli_command (const char *cli
, char *args
);
91 static enum mi_cmd_result
mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
);
93 static void mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
);
95 static int register_changed_p (int regnum
);
96 static int get_register (int regnum
, int format
);
98 /* A helper function which will set mi_error_message to
99 error_last_message. */
101 mi_error_last_message (void)
103 char *s
= error_last_message ();
104 xasprintf (&mi_error_message
, s
);
108 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
109 layer that calls libgdb. Any operation used in the below should be
113 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
115 /* We have to print everything right here because we never return */
116 if (last_async_command
)
117 fputs_unfiltered (last_async_command
, raw_stdout
);
118 fputs_unfiltered ("^exit\n", raw_stdout
);
119 mi_out_put (uiout
, raw_stdout
);
120 /* FIXME: The function called is not yet a formal libgdb function */
121 quit_force (NULL
, FROM_TTY
);
126 mi_cmd_exec_run (char *args
, int from_tty
)
128 /* FIXME: Should call a libgdb function, not a cli wrapper */
129 return mi_execute_async_cli_command ("run", args
, from_tty
);
133 mi_cmd_exec_next (char *args
, int from_tty
)
135 /* FIXME: Should call a libgdb function, not a cli wrapper */
136 return mi_execute_async_cli_command ("next", args
, from_tty
);
140 mi_cmd_exec_next_instruction (char *args
, int from_tty
)
142 /* FIXME: Should call a libgdb function, not a cli wrapper */
143 return mi_execute_async_cli_command ("nexti", args
, from_tty
);
147 mi_cmd_exec_step (char *args
, int from_tty
)
149 /* FIXME: Should call a libgdb function, not a cli wrapper */
150 return mi_execute_async_cli_command ("step", args
, from_tty
);
154 mi_cmd_exec_step_instruction (char *args
, int from_tty
)
156 /* FIXME: Should call a libgdb function, not a cli wrapper */
157 return mi_execute_async_cli_command ("stepi", args
, from_tty
);
161 mi_cmd_exec_finish (char *args
, int from_tty
)
163 /* FIXME: Should call a libgdb function, not a cli wrapper */
164 return mi_execute_async_cli_command ("finish", args
, from_tty
);
168 mi_cmd_exec_until (char *args
, int from_tty
)
170 /* FIXME: Should call a libgdb function, not a cli wrapper */
171 return mi_execute_async_cli_command ("until", args
, from_tty
);
175 mi_cmd_exec_return (char *args
, int from_tty
)
177 /* This command doesn't really execute the target, it just pops the
178 specified number of frames. */
180 /* Call return_command with from_tty argument equal to 0 so as to
181 avoid being queried. */
182 return_command (args
, 0);
184 /* Call return_command with from_tty argument equal to 0 so as to
185 avoid being queried. */
186 return_command (NULL
, 0);
188 /* Because we have called return_command with from_tty = 0, we need
189 to print the frame here. */
190 print_stack_frame (deprecated_selected_frame
,
191 frame_relative_level (deprecated_selected_frame
),
198 mi_cmd_exec_continue (char *args
, int from_tty
)
200 /* FIXME: Should call a libgdb function, not a cli wrapper */
201 return mi_execute_async_cli_command ("continue", args
, from_tty
);
204 /* Interrupt the execution of the target. Note how we must play around
205 with the token varialbes, in order to display the current token in
206 the result of the interrupt command, and the previous execution
207 token when the target finally stops. See comments in
210 mi_cmd_exec_interrupt (char *args
, int from_tty
)
212 if (!target_executing
)
214 xasprintf (&mi_error_message
,
215 "mi_cmd_exec_interrupt: Inferior not executing.");
218 interrupt_target_command (args
, from_tty
);
219 if (last_async_command
)
220 fputs_unfiltered (last_async_command
, raw_stdout
);
221 fputs_unfiltered ("^done", raw_stdout
);
222 xfree (last_async_command
);
223 if (previous_async_command
)
224 last_async_command
= xstrdup (previous_async_command
);
225 xfree (previous_async_command
);
226 previous_async_command
= NULL
;
227 mi_out_put (uiout
, raw_stdout
);
228 mi_out_rewind (uiout
);
229 fputs_unfiltered ("\n", raw_stdout
);
234 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
240 xasprintf (&mi_error_message
,
241 "mi_cmd_thread_select: USAGE: threadnum.");
245 rc
= gdb_thread_select (uiout
, argv
[0]);
247 /* RC is enum gdb_rc if it is successful (>=0)
248 enum return_reason if not (<0). */
249 if ((int) rc
< 0 && (enum return_reason
) rc
== RETURN_ERROR
)
250 return MI_CMD_CAUGHT_ERROR
;
251 else if ((int) rc
>= 0 && rc
== GDB_RC_FAIL
)
258 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
260 enum gdb_rc rc
= MI_CMD_DONE
;
264 xasprintf (&mi_error_message
,
265 "mi_cmd_thread_list_ids: No arguments required.");
269 rc
= gdb_list_thread_ids (uiout
);
271 if (rc
== GDB_RC_FAIL
)
272 return MI_CMD_CAUGHT_ERROR
;
278 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
282 struct cleanup
*cleanup
;
284 /* Note that the test for a valid register must include checking the
285 REGISTER_NAME because NUM_REGS may be allocated for the union of
286 the register sets within a family of related processors. In this
287 case, some entries of REGISTER_NAME will change depending upon
288 the particular processor being debugged. */
290 numregs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
292 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
294 if (argc
== 0) /* No args, just do all the regs */
300 if (REGISTER_NAME (regnum
) == NULL
301 || *(REGISTER_NAME (regnum
)) == '\0')
302 ui_out_field_string (uiout
, NULL
, "");
304 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
308 /* Else, list of register #s, just do listed regs */
309 for (i
= 0; i
< argc
; i
++)
311 regnum
= atoi (argv
[i
]);
312 if (regnum
< 0 || regnum
>= numregs
)
314 do_cleanups (cleanup
);
315 xasprintf (&mi_error_message
, "bad register number");
318 if (REGISTER_NAME (regnum
) == NULL
319 || *(REGISTER_NAME (regnum
)) == '\0')
320 ui_out_field_string (uiout
, NULL
, "");
322 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
324 do_cleanups (cleanup
);
329 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
331 int regnum
, numregs
, changed
;
333 struct cleanup
*cleanup
;
335 /* Note that the test for a valid register must include checking the
336 REGISTER_NAME because NUM_REGS may be allocated for the union of
337 the register sets within a family of related processors. In this
338 case, some entries of REGISTER_NAME will change depending upon
339 the particular processor being debugged. */
343 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
345 if (argc
== 0) /* No args, just do all the regs */
351 if (REGISTER_NAME (regnum
) == NULL
352 || *(REGISTER_NAME (regnum
)) == '\0')
354 changed
= register_changed_p (regnum
);
357 do_cleanups (cleanup
);
358 xasprintf (&mi_error_message
,
359 "mi_cmd_data_list_changed_registers: Unable to read register contents.");
363 ui_out_field_int (uiout
, NULL
, regnum
);
367 /* Else, list of register #s, just do listed regs */
368 for (i
= 0; i
< argc
; i
++)
370 regnum
= atoi (argv
[i
]);
374 && REGISTER_NAME (regnum
) != NULL
375 && *REGISTER_NAME (regnum
) != '\000')
377 changed
= register_changed_p (regnum
);
380 do_cleanups (cleanup
);
381 xasprintf (&mi_error_message
,
382 "mi_cmd_data_list_register_change: Unable to read register contents.");
386 ui_out_field_int (uiout
, NULL
, regnum
);
390 do_cleanups (cleanup
);
391 xasprintf (&mi_error_message
, "bad register number");
395 do_cleanups (cleanup
);
400 register_changed_p (int regnum
)
402 char raw_buffer
[MAX_REGISTER_SIZE
];
404 if (! frame_register_read (deprecated_selected_frame
, regnum
, raw_buffer
))
407 if (memcmp (&old_regs
[REGISTER_BYTE (regnum
)], raw_buffer
,
408 REGISTER_RAW_SIZE (regnum
)) == 0)
411 /* Found a changed register. Return 1. */
413 memcpy (&old_regs
[REGISTER_BYTE (regnum
)], raw_buffer
,
414 REGISTER_RAW_SIZE (regnum
));
419 /* Return a list of register number and value pairs. The valid
420 arguments expected are: a letter indicating the format in which to
421 display the registers contents. This can be one of: x (hexadecimal), d
422 (decimal), N (natural), t (binary), o (octal), r (raw). After the
423 format argumetn there can be a sequence of numbers, indicating which
424 registers to fetch the content of. If the format is the only argument,
425 a list of all the registers with their values is returned. */
427 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
429 int regnum
, numregs
, format
, result
;
431 struct cleanup
*list_cleanup
, *tuple_cleanup
;
433 /* Note that the test for a valid register must include checking the
434 REGISTER_NAME because NUM_REGS may be allocated for the union of
435 the register sets within a family of related processors. In this
436 case, some entries of REGISTER_NAME will change depending upon
437 the particular processor being debugged. */
443 xasprintf (&mi_error_message
,
444 "mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
448 format
= (int) argv
[0][0];
450 if (!target_has_registers
)
452 xasprintf (&mi_error_message
,
453 "mi_cmd_data_list_register_values: No registers.");
457 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
459 if (argc
== 1) /* No args, beside the format: do all the regs */
465 if (REGISTER_NAME (regnum
) == NULL
466 || *(REGISTER_NAME (regnum
)) == '\0')
468 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
469 ui_out_field_int (uiout
, "number", regnum
);
470 result
= get_register (regnum
, format
);
473 do_cleanups (list_cleanup
);
476 do_cleanups (tuple_cleanup
);
480 /* Else, list of register #s, just do listed regs */
481 for (i
= 1; i
< argc
; i
++)
483 regnum
= atoi (argv
[i
]);
487 && REGISTER_NAME (regnum
) != NULL
488 && *REGISTER_NAME (regnum
) != '\000')
490 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
491 ui_out_field_int (uiout
, "number", regnum
);
492 result
= get_register (regnum
, format
);
495 do_cleanups (list_cleanup
);
498 do_cleanups (tuple_cleanup
);
502 do_cleanups (list_cleanup
);
503 xasprintf (&mi_error_message
, "bad register number");
507 do_cleanups (list_cleanup
);
511 /* Output one register's contents in the desired format. */
513 get_register (int regnum
, int format
)
515 char raw_buffer
[MAX_REGISTER_SIZE
];
516 char virtual_buffer
[MAX_REGISTER_SIZE
];
521 static struct ui_stream
*stb
= NULL
;
523 stb
= ui_out_stream_new (uiout
);
528 frame_register (deprecated_selected_frame
, regnum
, &optim
, &lval
, &addr
,
529 &realnum
, raw_buffer
);
533 xasprintf (&mi_error_message
, "Optimized out");
537 /* Convert raw data to virtual format if necessary. */
539 if (DEPRECATED_REGISTER_CONVERTIBLE (regnum
))
541 DEPRECATED_REGISTER_CONVERT_TO_VIRTUAL (regnum
,
542 register_type (current_gdbarch
, regnum
),
543 raw_buffer
, virtual_buffer
);
546 memcpy (virtual_buffer
, raw_buffer
, REGISTER_VIRTUAL_SIZE (regnum
));
551 char *ptr
, buf
[1024];
555 for (j
= 0; j
< REGISTER_RAW_SIZE (regnum
); j
++)
557 register int idx
= TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
? j
558 : REGISTER_RAW_SIZE (regnum
) - 1 - j
;
559 sprintf (ptr
, "%02x", (unsigned char) raw_buffer
[idx
]);
562 ui_out_field_string (uiout
, "value", buf
);
563 /*fputs_filtered (buf, gdb_stdout); */
567 val_print (register_type (current_gdbarch
, regnum
), virtual_buffer
, 0, 0,
568 stb
->stream
, format
, 1, 0, Val_pretty_default
);
569 ui_out_field_stream (uiout
, "value", stb
);
570 ui_out_stream_delete (stb
);
575 /* Write given values into registers. The registers and values are
576 given as pairs. The corresponding MI command is
577 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
579 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
587 /* Note that the test for a valid register must include checking the
588 REGISTER_NAME because NUM_REGS may be allocated for the union of
589 the register sets within a family of related processors. In this
590 case, some entries of REGISTER_NAME will change depending upon
591 the particular processor being debugged. */
597 xasprintf (&mi_error_message
,
598 "mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
602 format
= (int) argv
[0][0];
604 if (!target_has_registers
)
606 xasprintf (&mi_error_message
,
607 "mi_cmd_data_write_register_values: No registers.");
613 xasprintf (&mi_error_message
,
614 "mi_cmd_data_write_register_values: No regs and values specified.");
620 xasprintf (&mi_error_message
,
621 "mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
625 for (i
= 1; i
< argc
; i
= i
+ 2)
627 regnum
= atoi (argv
[i
]);
631 && REGISTER_NAME (regnum
) != NULL
632 && *REGISTER_NAME (regnum
) != '\000')
635 struct cleanup
*old_chain
;
637 /* Get the value as a number */
638 value
= parse_and_eval_address (argv
[i
+ 1]);
639 /* Get the value into an array */
640 buffer
= xmalloc (DEPRECATED_REGISTER_SIZE
);
641 old_chain
= make_cleanup (xfree
, buffer
);
642 store_signed_integer (buffer
, DEPRECATED_REGISTER_SIZE
, value
);
644 deprecated_write_register_bytes (REGISTER_BYTE (regnum
), buffer
, REGISTER_RAW_SIZE (regnum
));
645 /* Free the buffer. */
646 do_cleanups (old_chain
);
650 xasprintf (&mi_error_message
, "bad register number");
658 /*This is commented out because we decided it was not useful. I leave
659 it, just in case. ezannoni:1999-12-08 */
661 /* Assign a value to a variable. The expression argument must be in
662 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
665 mi_cmd_data_assign (char *command
, char **argv
, int argc
)
667 struct expression
*expr
;
668 struct cleanup
*old_chain
;
672 xasprintf (&mi_error_message
,
673 "mi_cmd_data_assign: Usage: -data-assign expression");
677 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
678 01-12-1999: Need to decide what to do with this for libgdb purposes. */
680 expr
= parse_expression (argv
[0]);
681 old_chain
= make_cleanup (free_current_contents
, &expr
);
682 evaluate_expression (expr
);
683 do_cleanups (old_chain
);
688 /* Evaluate the value of the argument. The argument is an
689 expression. If the expression contains spaces it needs to be
690 included in double quotes. */
692 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
694 struct expression
*expr
;
695 struct cleanup
*old_chain
= NULL
;
697 struct ui_stream
*stb
= NULL
;
699 stb
= ui_out_stream_new (uiout
);
703 xasprintf (&mi_error_message
,
704 "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
708 expr
= parse_expression (argv
[0]);
710 old_chain
= make_cleanup (free_current_contents
, &expr
);
712 val
= evaluate_expression (expr
);
714 /* Print the result of the expression evaluation. */
715 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
),
716 VALUE_EMBEDDED_OFFSET (val
), VALUE_ADDRESS (val
),
717 stb
->stream
, 0, 0, 0, 0);
719 ui_out_field_stream (uiout
, "value", stb
);
720 ui_out_stream_delete (stb
);
722 do_cleanups (old_chain
);
728 mi_cmd_target_download (char *args
, int from_tty
)
731 struct cleanup
*old_cleanups
= NULL
;
733 xasprintf (&run
, "load %s", args
);
734 old_cleanups
= make_cleanup (xfree
, run
);
735 execute_command (run
, from_tty
);
737 do_cleanups (old_cleanups
);
741 /* Connect to the remote target. */
743 mi_cmd_target_select (char *args
, int from_tty
)
746 struct cleanup
*old_cleanups
= NULL
;
748 xasprintf (&run
, "target %s", args
);
749 old_cleanups
= make_cleanup (xfree
, run
);
751 /* target-select is always synchronous. once the call has returned
752 we know that we are connected. */
753 /* NOTE: At present all targets that are connected are also
754 (implicitly) talking to a halted target. In the future this may
756 execute_command (run
, from_tty
);
758 do_cleanups (old_cleanups
);
760 /* Issue the completion message here. */
761 if (last_async_command
)
762 fputs_unfiltered (last_async_command
, raw_stdout
);
763 fputs_unfiltered ("^connected", raw_stdout
);
764 mi_out_put (uiout
, raw_stdout
);
765 mi_out_rewind (uiout
);
766 fputs_unfiltered ("\n", raw_stdout
);
767 do_exec_cleanups (ALL_CLEANUPS
);
773 ADDR: start address of data to be dumped.
774 WORD-FORMAT: a char indicating format for the ``word''. See
776 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
777 NR_ROW: Number of rows.
778 NR_COL: The number of colums (words per row).
779 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
780 ASCHAR for unprintable characters.
782 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
783 displayes them. Returns:
785 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
788 The number of bytes read is SIZE*ROW*COL. */
791 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
793 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
799 struct type
*word_type
;
812 static struct mi_opt opts
[] =
814 {"o", OFFSET_OPT
, 1},
820 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
824 switch ((enum opt
) opt
)
827 offset
= atol (optarg
);
834 if (argc
< 5 || argc
> 6)
836 xasprintf (&mi_error_message
,
837 "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
841 /* Extract all the arguments. */
843 /* Start address of the memory dump. */
844 addr
= parse_and_eval_address (argv
[0]) + offset
;
845 /* The format character to use when displaying a memory word. See
846 the ``x'' command. */
847 word_format
= argv
[1][0];
848 /* The size of the memory word. */
849 word_size
= atol (argv
[2]);
853 word_type
= builtin_type_int8
;
857 word_type
= builtin_type_int16
;
861 word_type
= builtin_type_int32
;
865 word_type
= builtin_type_int64
;
869 word_type
= builtin_type_int8
;
872 /* The number of rows */
873 nr_rows
= atol (argv
[3]);
876 xasprintf (&mi_error_message
,
877 "mi_cmd_data_read_memory: invalid number of rows.");
880 /* number of bytes per row. */
881 nr_cols
= atol (argv
[4]);
884 xasprintf (&mi_error_message
,
885 "mi_cmd_data_read_memory: invalid number of columns.");
887 /* The un-printable character when printing ascii. */
893 /* create a buffer and read it in. */
894 total_bytes
= word_size
* nr_rows
* nr_cols
;
895 mbuf
= xcalloc (total_bytes
, 1);
896 make_cleanup (xfree
, mbuf
);
899 xasprintf (&mi_error_message
,
900 "mi_cmd_data_read_memory: out of memory.");
904 while (nr_bytes
< total_bytes
)
907 long num
= target_read_memory_partial (addr
+ nr_bytes
, mbuf
+ nr_bytes
,
908 total_bytes
- nr_bytes
,
915 /* output the header information. */
916 ui_out_field_core_addr (uiout
, "addr", addr
);
917 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
918 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
919 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
920 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
921 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
922 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
924 /* Build the result as a two dimentional table. */
926 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
927 struct cleanup
*cleanup_list_memory
;
930 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
931 for (row
= 0, row_byte
= 0;
933 row
++, row_byte
+= nr_cols
* word_size
)
937 struct cleanup
*cleanup_tuple
;
938 struct cleanup
*cleanup_list_data
;
939 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
940 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
941 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
942 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
943 for (col
= 0, col_byte
= row_byte
;
945 col
++, col_byte
+= word_size
)
947 if (col_byte
+ word_size
> nr_bytes
)
949 ui_out_field_string (uiout
, NULL
, "N/A");
953 ui_file_rewind (stream
->stream
);
954 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
955 word_asize
, stream
->stream
);
956 ui_out_field_stream (uiout
, NULL
, stream
);
959 do_cleanups (cleanup_list_data
);
963 ui_file_rewind (stream
->stream
);
964 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
966 if (byte
>= nr_bytes
)
968 fputc_unfiltered ('X', stream
->stream
);
970 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
972 fputc_unfiltered (aschar
, stream
->stream
);
975 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
977 ui_out_field_stream (uiout
, "ascii", stream
);
979 do_cleanups (cleanup_tuple
);
981 ui_out_stream_delete (stream
);
982 do_cleanups (cleanup_list_memory
);
984 do_cleanups (cleanups
);
988 /* DATA-MEMORY-WRITE:
990 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
991 offset from the beginning of the memory grid row where the cell to
993 ADDR: start address of the row in the memory grid where the memory
994 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
995 the location to write to.
996 FORMAT: a char indicating format for the ``word''. See
998 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
999 VALUE: value to be written into the memory address.
1001 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1005 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
1010 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1011 enough when using a compiler other than GCC. */
1014 struct cleanup
*old_chain
;
1022 static struct mi_opt opts
[] =
1024 {"o", OFFSET_OPT
, 1},
1030 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
1034 switch ((enum opt
) opt
)
1037 offset
= atol (optarg
);
1046 xasprintf (&mi_error_message
,
1047 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1048 return MI_CMD_ERROR
;
1051 /* Extract all the arguments. */
1052 /* Start address of the memory dump. */
1053 addr
= parse_and_eval_address (argv
[0]);
1054 /* The format character to use when displaying a memory word. See
1055 the ``x'' command. */
1056 word_format
= argv
[1][0];
1057 /* The size of the memory word. */
1058 word_size
= atol (argv
[2]);
1060 /* Calculate the real address of the write destination. */
1061 addr
+= (offset
* word_size
);
1063 /* Get the value as a number */
1064 value
= parse_and_eval_address (argv
[3]);
1065 /* Get the value into an array */
1066 buffer
= xmalloc (word_size
);
1067 old_chain
= make_cleanup (xfree
, buffer
);
1068 store_signed_integer (buffer
, word_size
, value
);
1069 /* Write it down to memory */
1070 write_memory (addr
, buffer
, word_size
);
1071 /* Free the buffer. */
1072 do_cleanups (old_chain
);
1077 /* Execute a command within a safe environment.
1078 Return <0 for error; >=0 for ok.
1080 args->action will tell mi_execute_command what action
1081 to perfrom after the given command has executed (display/supress
1082 prompt, display error). */
1085 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1087 struct captured_mi_execute_command_args
*args
=
1088 (struct captured_mi_execute_command_args
*) data
;
1089 struct mi_parse
*context
= args
->command
;
1091 switch (context
->op
)
1095 /* A MI command was read from the input stream */
1097 /* FIXME: gdb_???? */
1098 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1099 context
->token
, context
->command
, context
->args
);
1100 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1101 condition expression, each function should return an
1102 indication of what action is required and then switch on
1104 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1105 args
->rc
= mi_cmd_execute (context
);
1107 if (!target_can_async_p () || !target_executing
)
1109 /* print the result if there were no errors
1111 Remember that on the way out of executing a command, you have
1112 to directly use the mi_interp's uiout, since the command could
1113 have reset the interpreter, in which case the current uiout
1114 will most likely crash in the mi_out_* routines. */
1115 if (args
->rc
== MI_CMD_DONE
)
1117 fputs_unfiltered (context
->token
, raw_stdout
);
1118 fputs_unfiltered ("^done", raw_stdout
);
1119 mi_out_put (uiout
, raw_stdout
);
1120 mi_out_rewind (uiout
);
1121 fputs_unfiltered ("\n", raw_stdout
);
1123 else if (args
->rc
== MI_CMD_ERROR
)
1125 if (mi_error_message
)
1127 fputs_unfiltered (context
->token
, raw_stdout
);
1128 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1129 fputstr_unfiltered (mi_error_message
, '"', raw_stdout
);
1130 xfree (mi_error_message
);
1131 fputs_unfiltered ("\"\n", raw_stdout
);
1133 mi_out_rewind (uiout
);
1135 else if (args
->rc
== MI_CMD_CAUGHT_ERROR
)
1137 mi_out_rewind (uiout
);
1138 args
->action
= EXECUTE_COMMAND_DISPLAY_ERROR
;
1142 mi_out_rewind (uiout
);
1144 else if (sync_execution
)
1146 /* Don't print the prompt. We are executing the target in
1147 synchronous mode. */
1148 args
->action
= EXECUTE_COMMAND_SUPRESS_PROMPT
;
1154 /* A CLI command was read from the input stream */
1155 /* This will be removed as soon as we have a complete set of
1157 /* echo the command on the console. */
1158 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1159 /* FIXME: If the command string has something that looks like
1160 a format spec (e.g. %s) we will get a core dump */
1161 mi_execute_cli_command ("%s", context
->command
);
1163 /* If we changed interpreters, DON'T print out anything. */
1164 if (current_interp_named_p (INTERP_MI
)
1165 || current_interp_named_p (INTERP_MI1
))
1167 /* print the result */
1168 /* FIXME: Check for errors here. */
1169 fputs_unfiltered (context
->token
, raw_stdout
);
1170 fputs_unfiltered ("^done", raw_stdout
);
1171 mi_out_put (uiout
, raw_stdout
);
1172 mi_out_rewind (uiout
);
1173 fputs_unfiltered ("\n", raw_stdout
);
1174 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1175 args
->rc
= MI_CMD_DONE
;
1186 mi_execute_command (char *cmd
, int from_tty
)
1188 struct mi_parse
*command
;
1189 struct captured_mi_execute_command_args args
;
1190 struct ui_out
*saved_uiout
= uiout
;
1193 /* This is to handle EOF (^D). We just quit gdb. */
1194 /* FIXME: we should call some API function here. */
1196 quit_force (NULL
, from_tty
);
1198 command
= mi_parse (cmd
);
1200 if (command
!= NULL
)
1202 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
1203 be pushed even further down or even eliminated? */
1204 args
.command
= command
;
1205 result
= catch_exceptions (uiout
, captured_mi_execute_command
, &args
, "",
1208 if (args
.action
== EXECUTE_COMMAND_SUPRESS_PROMPT
)
1210 /* The command is executing synchronously. Bail out early
1211 suppressing the finished prompt. */
1212 mi_parse_free (command
);
1215 if (args
.action
== EXECUTE_COMMAND_DISPLAY_ERROR
|| result
< 0)
1217 char *msg
= error_last_message ();
1218 struct cleanup
*cleanup
= make_cleanup (xfree
, msg
);
1219 /* The command execution failed and error() was called
1221 fputs_unfiltered (command
->token
, raw_stdout
);
1222 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1223 fputstr_unfiltered (msg
, '"', raw_stdout
);
1224 fputs_unfiltered ("\"\n", raw_stdout
);
1226 mi_parse_free (command
);
1229 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1230 gdb_flush (raw_stdout
);
1231 /* print any buffered hook code */
1235 static enum mi_cmd_result
1236 mi_cmd_execute (struct mi_parse
*parse
)
1238 if (parse
->cmd
->argv_func
!= NULL
1239 || parse
->cmd
->args_func
!= NULL
)
1241 /* FIXME: We need to save the token because the command executed
1242 may be asynchronous and need to print the token again.
1243 In the future we can pass the token down to the func
1244 and get rid of the last_async_command */
1245 /* The problem here is to keep the token around when we launch
1246 the target, and we want to interrupt it later on. The
1247 interrupt command will have its own token, but when the
1248 target stops, we must display the token corresponding to the
1249 last execution command given. So we have another string where
1250 we copy the token (previous_async_command), if this was
1251 indeed the token of an execution command, and when we stop we
1252 print that one. This is possible because the interrupt
1253 command, when over, will copy that token back into the
1254 default token string (last_async_command). */
1256 if (target_executing
)
1258 if (!previous_async_command
)
1259 previous_async_command
= xstrdup (last_async_command
);
1260 if (strcmp (parse
->command
, "exec-interrupt"))
1262 fputs_unfiltered (parse
->token
, raw_stdout
);
1263 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1264 fputs_unfiltered ("Cannot execute command ", raw_stdout
);
1265 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1266 fputs_unfiltered (" while target running", raw_stdout
);
1267 fputs_unfiltered ("\"\n", raw_stdout
);
1268 return MI_CMD_ERROR
;
1271 last_async_command
= xstrdup (parse
->token
);
1272 make_exec_cleanup (free_current_contents
, &last_async_command
);
1273 /* FIXME: DELETE THIS! */
1274 if (parse
->cmd
->args_func
!= NULL
)
1275 return parse
->cmd
->args_func (parse
->args
, 0 /*from_tty */ );
1276 return parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1278 else if (parse
->cmd
->cli
!= 0)
1280 /* FIXME: DELETE THIS. */
1281 /* The operation is still implemented by a cli command */
1282 /* Must be a synchronous one */
1283 mi_execute_cli_command (parse
->cmd
->cli
, parse
->args
);
1288 /* FIXME: DELETE THIS. */
1289 fputs_unfiltered (parse
->token
, raw_stdout
);
1290 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1291 fputs_unfiltered ("Undefined mi command: ", raw_stdout
);
1292 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1293 fputs_unfiltered (" (missing implementation)", raw_stdout
);
1294 fputs_unfiltered ("\"\n", raw_stdout
);
1295 return MI_CMD_ERROR
;
1299 /* FIXME: This is just a hack so we can get some extra commands going.
1300 We don't want to channel things through the CLI, but call libgdb directly */
1301 /* Use only for synchronous commands */
1304 mi_execute_cli_command (const char *cli
, char *args
)
1308 struct cleanup
*old_cleanups
;
1310 xasprintf (&run
, cli
, args
);
1312 /* FIXME: gdb_???? */
1313 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1315 old_cleanups
= make_cleanup (xfree
, run
);
1316 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1317 do_cleanups (old_cleanups
);
1323 mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
)
1325 struct cleanup
*old_cleanups
;
1329 if (target_can_async_p ())
1331 async_args
= (char *) xmalloc (strlen (args
) + 2);
1332 make_exec_cleanup (free
, async_args
);
1333 strcpy (async_args
, args
);
1334 strcat (async_args
, "&");
1335 xasprintf (&run
, "%s %s", mi
, async_args
);
1336 make_exec_cleanup (free
, run
);
1337 add_continuation (mi_exec_async_cli_cmd_continuation
, NULL
);
1338 old_cleanups
= NULL
;
1342 xasprintf (&run
, "%s %s", mi
, args
);
1343 old_cleanups
= make_cleanup (xfree
, run
);
1346 if (!target_can_async_p ())
1348 /* NOTE: For synchronous targets asynchronous behavour is faked by
1349 printing out the GDB prompt before we even try to execute the
1351 if (last_async_command
)
1352 fputs_unfiltered (last_async_command
, raw_stdout
);
1353 fputs_unfiltered ("^running\n", raw_stdout
);
1354 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1355 gdb_flush (raw_stdout
);
1359 /* FIXME: cagney/1999-11-29: Printing this message before
1360 calling execute_command is wrong. It should only be printed
1361 once gdb has confirmed that it really has managed to send a
1362 run command to the target. */
1363 if (last_async_command
)
1364 fputs_unfiltered (last_async_command
, raw_stdout
);
1365 fputs_unfiltered ("^running\n", raw_stdout
);
1368 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1370 if (!target_can_async_p ())
1372 /* Do this before doing any printing. It would appear that some
1373 print code leaves garbage around in the buffer. */
1374 do_cleanups (old_cleanups
);
1375 /* If the target was doing the operation synchronously we fake
1376 the stopped message. */
1377 if (last_async_command
)
1378 fputs_unfiltered (last_async_command
, raw_stdout
);
1379 fputs_unfiltered ("*stopped", raw_stdout
);
1380 mi_out_put (uiout
, raw_stdout
);
1381 mi_out_rewind (uiout
);
1382 fputs_unfiltered ("\n", raw_stdout
);
1383 return MI_CMD_QUIET
;
1389 mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
)
1391 if (last_async_command
)
1392 fputs_unfiltered (last_async_command
, raw_stdout
);
1393 fputs_unfiltered ("*stopped", raw_stdout
);
1394 mi_out_put (uiout
, raw_stdout
);
1395 fputs_unfiltered ("\n", raw_stdout
);
1396 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1397 gdb_flush (raw_stdout
);
1398 do_exec_cleanups (ALL_CLEANUPS
);
1402 mi_load_progress (const char *section_name
,
1403 unsigned long sent_so_far
,
1404 unsigned long total_section
,
1405 unsigned long total_sent
,
1406 unsigned long grand_total
)
1408 struct timeval time_now
, delta
, update_threshold
;
1409 static struct timeval last_update
;
1410 static char *previous_sect_name
= NULL
;
1413 if (!current_interp_named_p (INTERP_MI
)
1414 && !current_interp_named_p (INTERP_MI1
))
1417 update_threshold
.tv_sec
= 0;
1418 update_threshold
.tv_usec
= 500000;
1419 gettimeofday (&time_now
, NULL
);
1421 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1422 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1424 if (delta
.tv_usec
< 0)
1427 delta
.tv_usec
+= 1000000;
1430 new_section
= (previous_sect_name
?
1431 strcmp (previous_sect_name
, section_name
) : 1);
1434 struct cleanup
*cleanup_tuple
;
1435 xfree (previous_sect_name
);
1436 previous_sect_name
= xstrdup (section_name
);
1438 if (last_async_command
)
1439 fputs_unfiltered (last_async_command
, raw_stdout
);
1440 fputs_unfiltered ("+download", raw_stdout
);
1441 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1442 ui_out_field_string (uiout
, "section", section_name
);
1443 ui_out_field_int (uiout
, "section-size", total_section
);
1444 ui_out_field_int (uiout
, "total-size", grand_total
);
1445 do_cleanups (cleanup_tuple
);
1446 mi_out_put (uiout
, raw_stdout
);
1447 fputs_unfiltered ("\n", raw_stdout
);
1448 gdb_flush (raw_stdout
);
1451 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1452 delta
.tv_usec
>= update_threshold
.tv_usec
)
1454 struct cleanup
*cleanup_tuple
;
1455 last_update
.tv_sec
= time_now
.tv_sec
;
1456 last_update
.tv_usec
= time_now
.tv_usec
;
1457 if (last_async_command
)
1458 fputs_unfiltered (last_async_command
, raw_stdout
);
1459 fputs_unfiltered ("+download", raw_stdout
);
1460 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1461 ui_out_field_string (uiout
, "section", section_name
);
1462 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1463 ui_out_field_int (uiout
, "section-size", total_section
);
1464 ui_out_field_int (uiout
, "total-sent", total_sent
);
1465 ui_out_field_int (uiout
, "total-size", grand_total
);
1466 do_cleanups (cleanup_tuple
);
1467 mi_out_put (uiout
, raw_stdout
);
1468 fputs_unfiltered ("\n", raw_stdout
);
1469 gdb_flush (raw_stdout
);
1474 mi_setup_architecture_data (void)
1476 /* don't trust DEPRECATED_REGISTER_BYTES to be zero. */
1477 old_regs
= xmalloc (DEPRECATED_REGISTER_BYTES
+ 1);
1478 memset (old_regs
, 0, DEPRECATED_REGISTER_BYTES
+ 1);
1482 _initialize_mi_main (void)
1484 register_gdbarch_swap (&old_regs
, sizeof (old_regs
), NULL
);
1485 register_gdbarch_swap (NULL
, 0, mi_setup_architecture_data
);