3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007
4 Free Software Foundation, Inc.
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., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
25 /* Work in progress */
30 #include "gdb_string.h"
31 #include "exceptions.h"
33 #include "gdbthread.h"
36 #include "mi-getopt.h"
37 #include "mi-console.h"
41 #include "event-loop.h"
42 #include "event-top.h"
43 #include "gdbcore.h" /* for write_memory() */
44 #include "value.h" /* for deprecated_write_register_bytes() */
58 /* Enumerations of the actions that may result from calling
59 captured_mi_execute_command */
61 enum captured_mi_execute_command_actions
63 EXECUTE_COMMAND_DISPLAY_PROMPT
,
64 EXECUTE_COMMAND_SUPRESS_PROMPT
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 (NULL
), 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 mi_error_message
= xstrprintf ("mi_cmd_exec_interrupt: Inferior not executing.");
209 interrupt_target_command (args
, from_tty
);
210 if (last_async_command
)
211 fputs_unfiltered (last_async_command
, raw_stdout
);
212 fputs_unfiltered ("^done", raw_stdout
);
213 xfree (last_async_command
);
214 if (previous_async_command
)
215 last_async_command
= xstrdup (previous_async_command
);
216 xfree (previous_async_command
);
217 previous_async_command
= NULL
;
218 mi_out_put (uiout
, raw_stdout
);
219 mi_out_rewind (uiout
);
220 fputs_unfiltered ("\n", raw_stdout
);
225 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
231 mi_error_message
= xstrprintf ("mi_cmd_thread_select: USAGE: threadnum.");
235 rc
= gdb_thread_select (uiout
, argv
[0], &mi_error_message
);
237 /* RC is enum gdb_rc if it is successful (>=0)
238 enum return_reason if not (<0). */
239 if ((int) rc
< 0 && (enum return_reason
) rc
== RETURN_ERROR
)
241 else if ((int) rc
>= 0 && rc
== GDB_RC_FAIL
)
248 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
250 enum gdb_rc rc
= MI_CMD_DONE
;
254 mi_error_message
= xstrprintf ("mi_cmd_thread_list_ids: No arguments required.");
258 rc
= gdb_list_thread_ids (uiout
, &mi_error_message
);
260 if (rc
== GDB_RC_FAIL
)
267 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
271 struct cleanup
*cleanup
;
273 /* Note that the test for a valid register must include checking the
274 REGISTER_NAME because NUM_REGS may be allocated for the union of
275 the register sets within a family of related processors. In this
276 case, some entries of REGISTER_NAME will change depending upon
277 the particular processor being debugged. */
279 numregs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
281 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
283 if (argc
== 0) /* No args, just do all the regs */
289 if (REGISTER_NAME (regnum
) == NULL
290 || *(REGISTER_NAME (regnum
)) == '\0')
291 ui_out_field_string (uiout
, NULL
, "");
293 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
297 /* Else, list of register #s, just do listed regs */
298 for (i
= 0; i
< argc
; i
++)
300 regnum
= atoi (argv
[i
]);
301 if (regnum
< 0 || regnum
>= numregs
)
303 do_cleanups (cleanup
);
304 mi_error_message
= xstrprintf ("bad register number");
307 if (REGISTER_NAME (regnum
) == NULL
308 || *(REGISTER_NAME (regnum
)) == '\0')
309 ui_out_field_string (uiout
, NULL
, "");
311 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
313 do_cleanups (cleanup
);
318 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
320 int regnum
, numregs
, changed
;
322 struct cleanup
*cleanup
;
324 /* Note that the test for a valid register must include checking the
325 REGISTER_NAME because NUM_REGS may be allocated for the union of
326 the register sets within a family of related processors. In this
327 case, some entries of REGISTER_NAME will change depending upon
328 the particular processor being debugged. */
330 numregs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
332 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
334 if (argc
== 0) /* No args, just do all the regs */
340 if (REGISTER_NAME (regnum
) == NULL
341 || *(REGISTER_NAME (regnum
)) == '\0')
343 changed
= register_changed_p (regnum
);
346 do_cleanups (cleanup
);
347 mi_error_message
= xstrprintf ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
351 ui_out_field_int (uiout
, NULL
, regnum
);
355 /* Else, list of register #s, just do listed regs */
356 for (i
= 0; i
< argc
; i
++)
358 regnum
= atoi (argv
[i
]);
362 && REGISTER_NAME (regnum
) != NULL
363 && *REGISTER_NAME (regnum
) != '\000')
365 changed
= register_changed_p (regnum
);
368 do_cleanups (cleanup
);
369 mi_error_message
= xstrprintf ("mi_cmd_data_list_register_change: Unable to read register contents.");
373 ui_out_field_int (uiout
, NULL
, regnum
);
377 do_cleanups (cleanup
);
378 mi_error_message
= xstrprintf ("bad register number");
382 do_cleanups (cleanup
);
387 register_changed_p (int regnum
)
389 gdb_byte raw_buffer
[MAX_REGISTER_SIZE
];
391 if (! frame_register_read (get_selected_frame (NULL
), regnum
, raw_buffer
))
394 if (memcmp (&old_regs
[DEPRECATED_REGISTER_BYTE (regnum
)], raw_buffer
,
395 register_size (current_gdbarch
, regnum
)) == 0)
398 /* Found a changed register. Return 1. */
400 memcpy (&old_regs
[DEPRECATED_REGISTER_BYTE (regnum
)], raw_buffer
,
401 register_size (current_gdbarch
, regnum
));
406 /* Return a list of register number and value pairs. The valid
407 arguments expected are: a letter indicating the format in which to
408 display the registers contents. This can be one of: x (hexadecimal), d
409 (decimal), N (natural), t (binary), o (octal), r (raw). After the
410 format argumetn there can be a sequence of numbers, indicating which
411 registers to fetch the content of. If the format is the only argument,
412 a list of all the registers with their values is returned. */
414 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
416 int regnum
, numregs
, format
, result
;
418 struct cleanup
*list_cleanup
, *tuple_cleanup
;
420 /* Note that the test for a valid register must include checking the
421 REGISTER_NAME because NUM_REGS may be allocated for the union of
422 the register sets within a family of related processors. In this
423 case, some entries of REGISTER_NAME will change depending upon
424 the particular processor being debugged. */
426 numregs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
430 mi_error_message
= xstrprintf ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
434 format
= (int) argv
[0][0];
436 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
438 if (argc
== 1) /* No args, beside the format: do all the regs */
444 if (REGISTER_NAME (regnum
) == NULL
445 || *(REGISTER_NAME (regnum
)) == '\0')
447 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
448 ui_out_field_int (uiout
, "number", regnum
);
449 result
= get_register (regnum
, format
);
452 do_cleanups (list_cleanup
);
455 do_cleanups (tuple_cleanup
);
459 /* Else, list of register #s, just do listed regs */
460 for (i
= 1; i
< argc
; i
++)
462 regnum
= atoi (argv
[i
]);
466 && REGISTER_NAME (regnum
) != NULL
467 && *REGISTER_NAME (regnum
) != '\000')
469 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
470 ui_out_field_int (uiout
, "number", regnum
);
471 result
= get_register (regnum
, format
);
474 do_cleanups (list_cleanup
);
477 do_cleanups (tuple_cleanup
);
481 do_cleanups (list_cleanup
);
482 mi_error_message
= xstrprintf ("bad register number");
486 do_cleanups (list_cleanup
);
490 /* Output one register's contents in the desired format. */
492 get_register (int regnum
, int format
)
494 gdb_byte buffer
[MAX_REGISTER_SIZE
];
499 static struct ui_stream
*stb
= NULL
;
501 stb
= ui_out_stream_new (uiout
);
506 frame_register (get_selected_frame (NULL
), regnum
, &optim
, &lval
, &addr
,
511 mi_error_message
= xstrprintf ("Optimized out");
518 char *ptr
, buf
[1024];
522 for (j
= 0; j
< register_size (current_gdbarch
, regnum
); j
++)
524 int idx
= TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
? j
525 : register_size (current_gdbarch
, regnum
) - 1 - j
;
526 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
529 ui_out_field_string (uiout
, "value", buf
);
530 /*fputs_filtered (buf, gdb_stdout); */
534 val_print (register_type (current_gdbarch
, regnum
), buffer
, 0, 0,
535 stb
->stream
, format
, 1, 0, Val_pretty_default
);
536 ui_out_field_stream (uiout
, "value", stb
);
537 ui_out_stream_delete (stb
);
542 /* Write given values into registers. The registers and values are
543 given as pairs. The corresponding MI command is
544 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
546 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
551 /* Note that the test for a valid register must include checking the
552 REGISTER_NAME because NUM_REGS may be allocated for the union of
553 the register sets within a family of related processors. In this
554 case, some entries of REGISTER_NAME will change depending upon
555 the particular processor being debugged. */
557 numregs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
561 mi_error_message
= xstrprintf ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
565 format
= (int) argv
[0][0];
567 if (!target_has_registers
)
569 mi_error_message
= xstrprintf ("mi_cmd_data_write_register_values: No registers.");
575 mi_error_message
= xstrprintf ("mi_cmd_data_write_register_values: No regs and values specified.");
581 mi_error_message
= xstrprintf ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
585 for (i
= 1; i
< argc
; i
= i
+ 2)
587 int regnum
= atoi (argv
[i
]);
589 if (regnum
>= 0 && regnum
< numregs
590 && REGISTER_NAME (regnum
) && *REGISTER_NAME (regnum
))
594 /* Get the value as a number. */
595 value
= parse_and_eval_address (argv
[i
+ 1]);
598 regcache_cooked_write_signed (current_regcache
, regnum
, value
);
602 mi_error_message
= xstrprintf ("bad register number");
610 /*This is commented out because we decided it was not useful. I leave
611 it, just in case. ezannoni:1999-12-08 */
613 /* Assign a value to a variable. The expression argument must be in
614 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
617 mi_cmd_data_assign (char *command
, char **argv
, int argc
)
619 struct expression
*expr
;
620 struct cleanup
*old_chain
;
624 mi_error_message
= xstrprintf ("mi_cmd_data_assign: Usage: -data-assign expression");
628 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
629 01-12-1999: Need to decide what to do with this for libgdb purposes. */
631 expr
= parse_expression (argv
[0]);
632 old_chain
= make_cleanup (free_current_contents
, &expr
);
633 evaluate_expression (expr
);
634 do_cleanups (old_chain
);
639 /* Evaluate the value of the argument. The argument is an
640 expression. If the expression contains spaces it needs to be
641 included in double quotes. */
643 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
645 struct expression
*expr
;
646 struct cleanup
*old_chain
= NULL
;
648 struct ui_stream
*stb
= NULL
;
650 stb
= ui_out_stream_new (uiout
);
654 mi_error_message
= xstrprintf ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
658 expr
= parse_expression (argv
[0]);
660 old_chain
= make_cleanup (free_current_contents
, &expr
);
662 val
= evaluate_expression (expr
);
664 /* Print the result of the expression evaluation. */
665 val_print (value_type (val
), value_contents (val
),
666 value_embedded_offset (val
), VALUE_ADDRESS (val
),
667 stb
->stream
, 0, 0, 0, 0);
669 ui_out_field_stream (uiout
, "value", stb
);
670 ui_out_stream_delete (stb
);
672 do_cleanups (old_chain
);
678 mi_cmd_target_download (char *args
, int from_tty
)
681 struct cleanup
*old_cleanups
= NULL
;
683 run
= xstrprintf ("load %s", args
);
684 old_cleanups
= make_cleanup (xfree
, run
);
685 execute_command (run
, from_tty
);
687 do_cleanups (old_cleanups
);
691 /* Connect to the remote target. */
693 mi_cmd_target_select (char *args
, int from_tty
)
696 struct cleanup
*old_cleanups
= NULL
;
698 run
= xstrprintf ("target %s", args
);
699 old_cleanups
= make_cleanup (xfree
, run
);
701 /* target-select is always synchronous. once the call has returned
702 we know that we are connected. */
703 /* NOTE: At present all targets that are connected are also
704 (implicitly) talking to a halted target. In the future this may
706 execute_command (run
, from_tty
);
708 do_cleanups (old_cleanups
);
710 /* Issue the completion message here. */
711 if (last_async_command
)
712 fputs_unfiltered (last_async_command
, raw_stdout
);
713 fputs_unfiltered ("^connected", raw_stdout
);
714 mi_out_put (uiout
, raw_stdout
);
715 mi_out_rewind (uiout
);
716 fputs_unfiltered ("\n", raw_stdout
);
717 do_exec_cleanups (ALL_CLEANUPS
);
723 ADDR: start address of data to be dumped.
724 WORD-FORMAT: a char indicating format for the ``word''. See
726 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
727 NR_ROW: Number of rows.
728 NR_COL: The number of colums (words per row).
729 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
730 ASCHAR for unprintable characters.
732 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
733 displayes them. Returns:
735 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
738 The number of bytes read is SIZE*ROW*COL. */
741 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
743 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
749 struct type
*word_type
;
762 static struct mi_opt opts
[] =
764 {"o", OFFSET_OPT
, 1},
770 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
774 switch ((enum opt
) opt
)
777 offset
= atol (optarg
);
784 if (argc
< 5 || argc
> 6)
786 mi_error_message
= xstrprintf ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
790 /* Extract all the arguments. */
792 /* Start address of the memory dump. */
793 addr
= parse_and_eval_address (argv
[0]) + offset
;
794 /* The format character to use when displaying a memory word. See
795 the ``x'' command. */
796 word_format
= argv
[1][0];
797 /* The size of the memory word. */
798 word_size
= atol (argv
[2]);
802 word_type
= builtin_type_int8
;
806 word_type
= builtin_type_int16
;
810 word_type
= builtin_type_int32
;
814 word_type
= builtin_type_int64
;
818 word_type
= builtin_type_int8
;
821 /* The number of rows */
822 nr_rows
= atol (argv
[3]);
825 mi_error_message
= xstrprintf ("mi_cmd_data_read_memory: invalid number of rows.");
828 /* number of bytes per row. */
829 nr_cols
= atol (argv
[4]);
832 mi_error_message
= xstrprintf ("mi_cmd_data_read_memory: invalid number of columns.");
835 /* The un-printable character when printing ascii. */
841 /* create a buffer and read it in. */
842 total_bytes
= word_size
* nr_rows
* nr_cols
;
843 mbuf
= xcalloc (total_bytes
, 1);
844 make_cleanup (xfree
, mbuf
);
846 nr_bytes
= target_read (¤t_target
, TARGET_OBJECT_MEMORY
, NULL
,
847 mbuf
, addr
, total_bytes
);
850 do_cleanups (cleanups
);
851 mi_error_message
= xstrdup ("Unable to read memory.");
855 /* output the header information. */
856 ui_out_field_core_addr (uiout
, "addr", addr
);
857 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
858 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
859 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
860 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
861 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
862 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
864 /* Build the result as a two dimentional table. */
866 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
867 struct cleanup
*cleanup_list_memory
;
870 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
871 for (row
= 0, row_byte
= 0;
873 row
++, row_byte
+= nr_cols
* word_size
)
877 struct cleanup
*cleanup_tuple
;
878 struct cleanup
*cleanup_list_data
;
879 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
880 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
881 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
882 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
883 for (col
= 0, col_byte
= row_byte
;
885 col
++, col_byte
+= word_size
)
887 if (col_byte
+ word_size
> nr_bytes
)
889 ui_out_field_string (uiout
, NULL
, "N/A");
893 ui_file_rewind (stream
->stream
);
894 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
895 word_asize
, stream
->stream
);
896 ui_out_field_stream (uiout
, NULL
, stream
);
899 do_cleanups (cleanup_list_data
);
903 ui_file_rewind (stream
->stream
);
904 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
906 if (byte
>= nr_bytes
)
908 fputc_unfiltered ('X', stream
->stream
);
910 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
912 fputc_unfiltered (aschar
, stream
->stream
);
915 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
917 ui_out_field_stream (uiout
, "ascii", stream
);
919 do_cleanups (cleanup_tuple
);
921 ui_out_stream_delete (stream
);
922 do_cleanups (cleanup_list_memory
);
924 do_cleanups (cleanups
);
928 /* DATA-MEMORY-WRITE:
930 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
931 offset from the beginning of the memory grid row where the cell to
933 ADDR: start address of the row in the memory grid where the memory
934 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
935 the location to write to.
936 FORMAT: a char indicating format for the ``word''. See
938 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
939 VALUE: value to be written into the memory address.
941 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
945 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
950 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
951 enough when using a compiler other than GCC. */
954 struct cleanup
*old_chain
;
962 static struct mi_opt opts
[] =
964 {"o", OFFSET_OPT
, 1},
970 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
974 switch ((enum opt
) opt
)
977 offset
= atol (optarg
);
986 mi_error_message
= xstrprintf ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
990 /* Extract all the arguments. */
991 /* Start address of the memory dump. */
992 addr
= parse_and_eval_address (argv
[0]);
993 /* The format character to use when displaying a memory word. See
994 the ``x'' command. */
995 word_format
= argv
[1][0];
996 /* The size of the memory word. */
997 word_size
= atol (argv
[2]);
999 /* Calculate the real address of the write destination. */
1000 addr
+= (offset
* word_size
);
1002 /* Get the value as a number */
1003 value
= parse_and_eval_address (argv
[3]);
1004 /* Get the value into an array */
1005 buffer
= xmalloc (word_size
);
1006 old_chain
= make_cleanup (xfree
, buffer
);
1007 store_signed_integer (buffer
, word_size
, value
);
1008 /* Write it down to memory */
1009 write_memory (addr
, buffer
, word_size
);
1010 /* Free the buffer. */
1011 do_cleanups (old_chain
);
1016 /* Execute a command within a safe environment.
1017 Return <0 for error; >=0 for ok.
1019 args->action will tell mi_execute_command what action
1020 to perfrom after the given command has executed (display/supress
1021 prompt, display error). */
1024 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1026 struct captured_mi_execute_command_args
*args
=
1027 (struct captured_mi_execute_command_args
*) data
;
1028 struct mi_parse
*context
= args
->command
;
1030 switch (context
->op
)
1034 /* A MI command was read from the input stream */
1036 /* FIXME: gdb_???? */
1037 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1038 context
->token
, context
->command
, context
->args
);
1039 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1040 condition expression, each function should return an
1041 indication of what action is required and then switch on
1043 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1044 args
->rc
= mi_cmd_execute (context
);
1046 if (!target_can_async_p () || !target_executing
)
1048 /* print the result if there were no errors
1050 Remember that on the way out of executing a command, you have
1051 to directly use the mi_interp's uiout, since the command could
1052 have reset the interpreter, in which case the current uiout
1053 will most likely crash in the mi_out_* routines. */
1054 if (args
->rc
== MI_CMD_DONE
)
1056 fputs_unfiltered (context
->token
, raw_stdout
);
1057 fputs_unfiltered ("^done", raw_stdout
);
1058 mi_out_put (uiout
, raw_stdout
);
1059 mi_out_rewind (uiout
);
1060 fputs_unfiltered ("\n", raw_stdout
);
1062 else if (args
->rc
== MI_CMD_ERROR
)
1064 if (mi_error_message
)
1066 fputs_unfiltered (context
->token
, raw_stdout
);
1067 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1068 fputstr_unfiltered (mi_error_message
, '"', raw_stdout
);
1069 xfree (mi_error_message
);
1070 fputs_unfiltered ("\"\n", raw_stdout
);
1072 mi_out_rewind (uiout
);
1075 mi_out_rewind (uiout
);
1077 else if (sync_execution
)
1079 /* Don't print the prompt. We are executing the target in
1080 synchronous mode. */
1081 args
->action
= EXECUTE_COMMAND_SUPRESS_PROMPT
;
1089 /* A CLI command was read from the input stream. */
1090 /* This "feature" will be removed as soon as we have a
1091 complete set of mi commands. */
1092 /* Echo the command on the console. */
1093 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1094 /* Call the "console" interpreter. */
1095 argv
[0] = "console";
1096 argv
[1] = context
->command
;
1097 args
->rc
= mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
1099 /* If we changed interpreters, DON'T print out anything. */
1100 if (current_interp_named_p (INTERP_MI
)
1101 || current_interp_named_p (INTERP_MI1
)
1102 || current_interp_named_p (INTERP_MI2
)
1103 || current_interp_named_p (INTERP_MI3
))
1105 if (args
->rc
== MI_CMD_DONE
)
1107 fputs_unfiltered (context
->token
, raw_stdout
);
1108 fputs_unfiltered ("^done", raw_stdout
);
1109 mi_out_put (uiout
, raw_stdout
);
1110 mi_out_rewind (uiout
);
1111 fputs_unfiltered ("\n", raw_stdout
);
1112 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1114 else if (args
->rc
== MI_CMD_ERROR
)
1116 if (mi_error_message
)
1118 fputs_unfiltered (context
->token
, raw_stdout
);
1119 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1120 fputstr_unfiltered (mi_error_message
, '"', raw_stdout
);
1121 xfree (mi_error_message
);
1122 fputs_unfiltered ("\"\n", raw_stdout
);
1124 mi_out_rewind (uiout
);
1127 mi_out_rewind (uiout
);
1139 mi_execute_command (char *cmd
, int from_tty
)
1141 struct mi_parse
*command
;
1142 struct captured_mi_execute_command_args args
;
1143 struct ui_out
*saved_uiout
= uiout
;
1145 /* This is to handle EOF (^D). We just quit gdb. */
1146 /* FIXME: we should call some API function here. */
1148 quit_force (NULL
, from_tty
);
1150 command
= mi_parse (cmd
);
1152 if (command
!= NULL
)
1154 struct gdb_exception result
;
1155 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
1156 be pushed even further down or even eliminated? */
1157 args
.command
= command
;
1158 result
= catch_exception (uiout
, captured_mi_execute_command
, &args
,
1160 exception_print (gdb_stderr
, result
);
1162 if (args
.action
== EXECUTE_COMMAND_SUPRESS_PROMPT
)
1164 /* The command is executing synchronously. Bail out early
1165 suppressing the finished prompt. */
1166 mi_parse_free (command
);
1169 if (result
.reason
< 0)
1171 /* The command execution failed and error() was called
1173 fputs_unfiltered (command
->token
, raw_stdout
);
1174 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1175 if (result
.message
== NULL
)
1176 fputs_unfiltered ("unknown error", raw_stdout
);
1178 fputstr_unfiltered (result
.message
, '"', raw_stdout
);
1179 fputs_unfiltered ("\"\n", raw_stdout
);
1180 mi_out_rewind (uiout
);
1182 mi_parse_free (command
);
1185 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1186 gdb_flush (raw_stdout
);
1187 /* print any buffered hook code */
1191 static enum mi_cmd_result
1192 mi_cmd_execute (struct mi_parse
*parse
)
1194 if (parse
->cmd
->argv_func
!= NULL
1195 || parse
->cmd
->args_func
!= NULL
)
1197 /* FIXME: We need to save the token because the command executed
1198 may be asynchronous and need to print the token again.
1199 In the future we can pass the token down to the func
1200 and get rid of the last_async_command */
1201 /* The problem here is to keep the token around when we launch
1202 the target, and we want to interrupt it later on. The
1203 interrupt command will have its own token, but when the
1204 target stops, we must display the token corresponding to the
1205 last execution command given. So we have another string where
1206 we copy the token (previous_async_command), if this was
1207 indeed the token of an execution command, and when we stop we
1208 print that one. This is possible because the interrupt
1209 command, when over, will copy that token back into the
1210 default token string (last_async_command). */
1212 if (target_executing
)
1214 if (!previous_async_command
)
1215 previous_async_command
= xstrdup (last_async_command
);
1216 if (strcmp (parse
->command
, "exec-interrupt"))
1218 fputs_unfiltered (parse
->token
, raw_stdout
);
1219 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1220 fputs_unfiltered ("Cannot execute command ", raw_stdout
);
1221 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1222 fputs_unfiltered (" while target running", raw_stdout
);
1223 fputs_unfiltered ("\"\n", raw_stdout
);
1224 return MI_CMD_ERROR
;
1227 last_async_command
= xstrdup (parse
->token
);
1228 make_exec_cleanup (free_current_contents
, &last_async_command
);
1229 /* FIXME: DELETE THIS! */
1230 if (parse
->cmd
->args_func
!= NULL
)
1231 return parse
->cmd
->args_func (parse
->args
, 0 /*from_tty */ );
1232 return parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1234 else if (parse
->cmd
->cli
.cmd
!= 0)
1236 /* FIXME: DELETE THIS. */
1237 /* The operation is still implemented by a cli command */
1238 /* Must be a synchronous one */
1239 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1245 /* FIXME: DELETE THIS. */
1246 fputs_unfiltered (parse
->token
, raw_stdout
);
1247 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1248 fputs_unfiltered ("Undefined mi command: ", raw_stdout
);
1249 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1250 fputs_unfiltered (" (missing implementation)", raw_stdout
);
1251 fputs_unfiltered ("\"\n", raw_stdout
);
1252 return MI_CMD_ERROR
;
1256 /* FIXME: This is just a hack so we can get some extra commands going.
1257 We don't want to channel things through the CLI, but call libgdb directly */
1258 /* Use only for synchronous commands */
1261 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1265 struct cleanup
*old_cleanups
;
1268 run
= xstrprintf ("%s %s", cmd
, args
);
1270 run
= xstrdup (cmd
);
1272 /* FIXME: gdb_???? */
1273 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1275 old_cleanups
= make_cleanup (xfree
, run
);
1276 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1277 do_cleanups (old_cleanups
);
1283 mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
)
1285 struct cleanup
*old_cleanups
;
1289 if (target_can_async_p ())
1291 async_args
= (char *) xmalloc (strlen (args
) + 2);
1292 make_exec_cleanup (free
, async_args
);
1293 strcpy (async_args
, args
);
1294 strcat (async_args
, "&");
1295 run
= xstrprintf ("%s %s", mi
, async_args
);
1296 make_exec_cleanup (free
, run
);
1297 add_continuation (mi_exec_async_cli_cmd_continuation
, NULL
);
1298 old_cleanups
= NULL
;
1302 run
= xstrprintf ("%s %s", mi
, args
);
1303 old_cleanups
= make_cleanup (xfree
, run
);
1306 if (!target_can_async_p ())
1308 /* NOTE: For synchronous targets asynchronous behavour is faked by
1309 printing out the GDB prompt before we even try to execute the
1311 if (last_async_command
)
1312 fputs_unfiltered (last_async_command
, raw_stdout
);
1313 fputs_unfiltered ("^running\n", raw_stdout
);
1314 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1315 gdb_flush (raw_stdout
);
1319 /* FIXME: cagney/1999-11-29: Printing this message before
1320 calling execute_command is wrong. It should only be printed
1321 once gdb has confirmed that it really has managed to send a
1322 run command to the target. */
1323 if (last_async_command
)
1324 fputs_unfiltered (last_async_command
, raw_stdout
);
1325 fputs_unfiltered ("^running\n", raw_stdout
);
1328 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1330 if (!target_can_async_p ())
1332 /* Do this before doing any printing. It would appear that some
1333 print code leaves garbage around in the buffer. */
1334 do_cleanups (old_cleanups
);
1335 /* If the target was doing the operation synchronously we fake
1336 the stopped message. */
1337 if (last_async_command
)
1338 fputs_unfiltered (last_async_command
, raw_stdout
);
1339 fputs_unfiltered ("*stopped", raw_stdout
);
1340 mi_out_put (uiout
, raw_stdout
);
1341 mi_out_rewind (uiout
);
1342 fputs_unfiltered ("\n", raw_stdout
);
1343 return MI_CMD_QUIET
;
1349 mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
)
1351 if (last_async_command
)
1352 fputs_unfiltered (last_async_command
, raw_stdout
);
1353 fputs_unfiltered ("*stopped", raw_stdout
);
1354 mi_out_put (uiout
, raw_stdout
);
1355 fputs_unfiltered ("\n", raw_stdout
);
1356 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1357 gdb_flush (raw_stdout
);
1358 do_exec_cleanups (ALL_CLEANUPS
);
1362 mi_load_progress (const char *section_name
,
1363 unsigned long sent_so_far
,
1364 unsigned long total_section
,
1365 unsigned long total_sent
,
1366 unsigned long grand_total
)
1368 struct timeval time_now
, delta
, update_threshold
;
1369 static struct timeval last_update
;
1370 static char *previous_sect_name
= NULL
;
1372 struct ui_out
*saved_uiout
;
1374 /* This function is called through deprecated_show_load_progress
1375 which means uiout may not be correct. Fix it for the duration
1376 of this function. */
1377 saved_uiout
= uiout
;
1379 if (current_interp_named_p (INTERP_MI
))
1380 uiout
= mi_out_new (2);
1381 else if (current_interp_named_p (INTERP_MI1
))
1382 uiout
= mi_out_new (1);
1386 update_threshold
.tv_sec
= 0;
1387 update_threshold
.tv_usec
= 500000;
1388 gettimeofday (&time_now
, NULL
);
1390 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1391 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1393 if (delta
.tv_usec
< 0)
1396 delta
.tv_usec
+= 1000000;
1399 new_section
= (previous_sect_name
?
1400 strcmp (previous_sect_name
, section_name
) : 1);
1403 struct cleanup
*cleanup_tuple
;
1404 xfree (previous_sect_name
);
1405 previous_sect_name
= xstrdup (section_name
);
1407 if (last_async_command
)
1408 fputs_unfiltered (last_async_command
, raw_stdout
);
1409 fputs_unfiltered ("+download", raw_stdout
);
1410 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1411 ui_out_field_string (uiout
, "section", section_name
);
1412 ui_out_field_int (uiout
, "section-size", total_section
);
1413 ui_out_field_int (uiout
, "total-size", grand_total
);
1414 do_cleanups (cleanup_tuple
);
1415 mi_out_put (uiout
, raw_stdout
);
1416 fputs_unfiltered ("\n", raw_stdout
);
1417 gdb_flush (raw_stdout
);
1420 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1421 delta
.tv_usec
>= update_threshold
.tv_usec
)
1423 struct cleanup
*cleanup_tuple
;
1424 last_update
.tv_sec
= time_now
.tv_sec
;
1425 last_update
.tv_usec
= time_now
.tv_usec
;
1426 if (last_async_command
)
1427 fputs_unfiltered (last_async_command
, raw_stdout
);
1428 fputs_unfiltered ("+download", raw_stdout
);
1429 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1430 ui_out_field_string (uiout
, "section", section_name
);
1431 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1432 ui_out_field_int (uiout
, "section-size", total_section
);
1433 ui_out_field_int (uiout
, "total-sent", total_sent
);
1434 ui_out_field_int (uiout
, "total-size", grand_total
);
1435 do_cleanups (cleanup_tuple
);
1436 mi_out_put (uiout
, raw_stdout
);
1437 fputs_unfiltered ("\n", raw_stdout
);
1438 gdb_flush (raw_stdout
);
1442 uiout
= saved_uiout
;
1446 mi_setup_architecture_data (void)
1448 old_regs
= xmalloc ((NUM_REGS
+ NUM_PSEUDO_REGS
) * MAX_REGISTER_SIZE
+ 1);
1449 memset (old_regs
, 0, (NUM_REGS
+ NUM_PSEUDO_REGS
) * MAX_REGISTER_SIZE
+ 1);
1453 _initialize_mi_main (void)
1455 DEPRECATED_REGISTER_GDBARCH_SWAP (old_regs
);
1456 deprecated_register_gdbarch_swap (NULL
, 0, mi_setup_architecture_data
);