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 *cmd
, int args_p
,
92 static enum mi_cmd_result
mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
);
94 static void mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
);
96 static int register_changed_p (int regnum
);
97 static int get_register (int regnum
, int format
);
99 /* A helper function which will set mi_error_message to
100 error_last_message. */
102 mi_error_last_message (void)
104 char *s
= error_last_message ();
105 xasprintf (&mi_error_message
, "%s", s
);
109 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
110 layer that calls libgdb. Any operation used in the below should be
114 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
116 /* We have to print everything right here because we never return */
117 if (last_async_command
)
118 fputs_unfiltered (last_async_command
, raw_stdout
);
119 fputs_unfiltered ("^exit\n", raw_stdout
);
120 mi_out_put (uiout
, raw_stdout
);
121 /* FIXME: The function called is not yet a formal libgdb function */
122 quit_force (NULL
, FROM_TTY
);
127 mi_cmd_exec_run (char *args
, int from_tty
)
129 /* FIXME: Should call a libgdb function, not a cli wrapper */
130 return mi_execute_async_cli_command ("run", args
, from_tty
);
134 mi_cmd_exec_next (char *args
, int from_tty
)
136 /* FIXME: Should call a libgdb function, not a cli wrapper */
137 return mi_execute_async_cli_command ("next", args
, from_tty
);
141 mi_cmd_exec_next_instruction (char *args
, int from_tty
)
143 /* FIXME: Should call a libgdb function, not a cli wrapper */
144 return mi_execute_async_cli_command ("nexti", args
, from_tty
);
148 mi_cmd_exec_step (char *args
, int from_tty
)
150 /* FIXME: Should call a libgdb function, not a cli wrapper */
151 return mi_execute_async_cli_command ("step", args
, from_tty
);
155 mi_cmd_exec_step_instruction (char *args
, int from_tty
)
157 /* FIXME: Should call a libgdb function, not a cli wrapper */
158 return mi_execute_async_cli_command ("stepi", args
, from_tty
);
162 mi_cmd_exec_finish (char *args
, int from_tty
)
164 /* FIXME: Should call a libgdb function, not a cli wrapper */
165 return mi_execute_async_cli_command ("finish", args
, from_tty
);
169 mi_cmd_exec_until (char *args
, int from_tty
)
171 /* FIXME: Should call a libgdb function, not a cli wrapper */
172 return mi_execute_async_cli_command ("until", args
, from_tty
);
176 mi_cmd_exec_return (char *args
, int from_tty
)
178 /* This command doesn't really execute the target, it just pops the
179 specified number of frames. */
181 /* Call return_command with from_tty argument equal to 0 so as to
182 avoid being queried. */
183 return_command (args
, 0);
185 /* Call return_command with from_tty argument equal to 0 so as to
186 avoid being queried. */
187 return_command (NULL
, 0);
189 /* Because we have called return_command with from_tty = 0, we need
190 to print the frame here. */
191 print_stack_frame (deprecated_selected_frame
,
192 frame_relative_level (deprecated_selected_frame
),
199 mi_cmd_exec_continue (char *args
, int from_tty
)
201 /* FIXME: Should call a libgdb function, not a cli wrapper */
202 return mi_execute_async_cli_command ("continue", args
, from_tty
);
205 /* Interrupt the execution of the target. Note how we must play around
206 with the token varialbes, in order to display the current token in
207 the result of the interrupt command, and the previous execution
208 token when the target finally stops. See comments in
211 mi_cmd_exec_interrupt (char *args
, int from_tty
)
213 if (!target_executing
)
215 xasprintf (&mi_error_message
,
216 "mi_cmd_exec_interrupt: Inferior not executing.");
219 interrupt_target_command (args
, from_tty
);
220 if (last_async_command
)
221 fputs_unfiltered (last_async_command
, raw_stdout
);
222 fputs_unfiltered ("^done", raw_stdout
);
223 xfree (last_async_command
);
224 if (previous_async_command
)
225 last_async_command
= xstrdup (previous_async_command
);
226 xfree (previous_async_command
);
227 previous_async_command
= NULL
;
228 mi_out_put (uiout
, raw_stdout
);
229 mi_out_rewind (uiout
);
230 fputs_unfiltered ("\n", raw_stdout
);
235 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
241 xasprintf (&mi_error_message
,
242 "mi_cmd_thread_select: USAGE: threadnum.");
246 rc
= gdb_thread_select (uiout
, argv
[0]);
248 /* RC is enum gdb_rc if it is successful (>=0)
249 enum return_reason if not (<0). */
250 if ((int) rc
< 0 && (enum return_reason
) rc
== RETURN_ERROR
)
251 return MI_CMD_CAUGHT_ERROR
;
252 else if ((int) rc
>= 0 && rc
== GDB_RC_FAIL
)
259 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
261 enum gdb_rc rc
= MI_CMD_DONE
;
265 xasprintf (&mi_error_message
,
266 "mi_cmd_thread_list_ids: No arguments required.");
270 rc
= gdb_list_thread_ids (uiout
);
272 if (rc
== GDB_RC_FAIL
)
273 return MI_CMD_CAUGHT_ERROR
;
279 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
283 struct cleanup
*cleanup
;
285 /* Note that the test for a valid register must include checking the
286 REGISTER_NAME because NUM_REGS may be allocated for the union of
287 the register sets within a family of related processors. In this
288 case, some entries of REGISTER_NAME will change depending upon
289 the particular processor being debugged. */
291 numregs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
293 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
295 if (argc
== 0) /* No args, just do all the regs */
301 if (REGISTER_NAME (regnum
) == NULL
302 || *(REGISTER_NAME (regnum
)) == '\0')
303 ui_out_field_string (uiout
, NULL
, "");
305 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
309 /* Else, list of register #s, just do listed regs */
310 for (i
= 0; i
< argc
; i
++)
312 regnum
= atoi (argv
[i
]);
313 if (regnum
< 0 || regnum
>= numregs
)
315 do_cleanups (cleanup
);
316 xasprintf (&mi_error_message
, "bad register number");
319 if (REGISTER_NAME (regnum
) == NULL
320 || *(REGISTER_NAME (regnum
)) == '\0')
321 ui_out_field_string (uiout
, NULL
, "");
323 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
325 do_cleanups (cleanup
);
330 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
332 int regnum
, numregs
, changed
;
334 struct cleanup
*cleanup
;
336 /* Note that the test for a valid register must include checking the
337 REGISTER_NAME because NUM_REGS may be allocated for the union of
338 the register sets within a family of related processors. In this
339 case, some entries of REGISTER_NAME will change depending upon
340 the particular processor being debugged. */
344 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
346 if (argc
== 0) /* No args, just do all the regs */
352 if (REGISTER_NAME (regnum
) == NULL
353 || *(REGISTER_NAME (regnum
)) == '\0')
355 changed
= register_changed_p (regnum
);
358 do_cleanups (cleanup
);
359 xasprintf (&mi_error_message
,
360 "mi_cmd_data_list_changed_registers: Unable to read register contents.");
364 ui_out_field_int (uiout
, NULL
, regnum
);
368 /* Else, list of register #s, just do listed regs */
369 for (i
= 0; i
< argc
; i
++)
371 regnum
= atoi (argv
[i
]);
375 && REGISTER_NAME (regnum
) != NULL
376 && *REGISTER_NAME (regnum
) != '\000')
378 changed
= register_changed_p (regnum
);
381 do_cleanups (cleanup
);
382 xasprintf (&mi_error_message
,
383 "mi_cmd_data_list_register_change: Unable to read register contents.");
387 ui_out_field_int (uiout
, NULL
, regnum
);
391 do_cleanups (cleanup
);
392 xasprintf (&mi_error_message
, "bad register number");
396 do_cleanups (cleanup
);
401 register_changed_p (int regnum
)
403 char raw_buffer
[MAX_REGISTER_SIZE
];
405 if (! frame_register_read (deprecated_selected_frame
, regnum
, raw_buffer
))
408 if (memcmp (&old_regs
[DEPRECATED_REGISTER_BYTE (regnum
)], raw_buffer
,
409 REGISTER_RAW_SIZE (regnum
)) == 0)
412 /* Found a changed register. Return 1. */
414 memcpy (&old_regs
[DEPRECATED_REGISTER_BYTE (regnum
)], raw_buffer
,
415 REGISTER_RAW_SIZE (regnum
));
420 /* Return a list of register number and value pairs. The valid
421 arguments expected are: a letter indicating the format in which to
422 display the registers contents. This can be one of: x (hexadecimal), d
423 (decimal), N (natural), t (binary), o (octal), r (raw). After the
424 format argumetn there can be a sequence of numbers, indicating which
425 registers to fetch the content of. If the format is the only argument,
426 a list of all the registers with their values is returned. */
428 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
430 int regnum
, numregs
, format
, result
;
432 struct cleanup
*list_cleanup
, *tuple_cleanup
;
434 /* Note that the test for a valid register must include checking the
435 REGISTER_NAME because NUM_REGS may be allocated for the union of
436 the register sets within a family of related processors. In this
437 case, some entries of REGISTER_NAME will change depending upon
438 the particular processor being debugged. */
444 xasprintf (&mi_error_message
,
445 "mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
449 format
= (int) argv
[0][0];
451 if (!target_has_registers
)
453 xasprintf (&mi_error_message
,
454 "mi_cmd_data_list_register_values: No registers.");
458 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
460 if (argc
== 1) /* No args, beside the format: do all the regs */
466 if (REGISTER_NAME (regnum
) == NULL
467 || *(REGISTER_NAME (regnum
)) == '\0')
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 /* Else, list of register #s, just do listed regs */
482 for (i
= 1; i
< argc
; i
++)
484 regnum
= atoi (argv
[i
]);
488 && REGISTER_NAME (regnum
) != NULL
489 && *REGISTER_NAME (regnum
) != '\000')
491 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
492 ui_out_field_int (uiout
, "number", regnum
);
493 result
= get_register (regnum
, format
);
496 do_cleanups (list_cleanup
);
499 do_cleanups (tuple_cleanup
);
503 do_cleanups (list_cleanup
);
504 xasprintf (&mi_error_message
, "bad register number");
508 do_cleanups (list_cleanup
);
512 /* Output one register's contents in the desired format. */
514 get_register (int regnum
, int format
)
516 char raw_buffer
[MAX_REGISTER_SIZE
];
517 char virtual_buffer
[MAX_REGISTER_SIZE
];
522 static struct ui_stream
*stb
= NULL
;
524 stb
= ui_out_stream_new (uiout
);
529 frame_register (deprecated_selected_frame
, regnum
, &optim
, &lval
, &addr
,
530 &realnum
, raw_buffer
);
534 xasprintf (&mi_error_message
, "Optimized out");
538 /* Convert raw data to virtual format if necessary. */
540 if (DEPRECATED_REGISTER_CONVERTIBLE (regnum
))
542 DEPRECATED_REGISTER_CONVERT_TO_VIRTUAL (regnum
,
543 register_type (current_gdbarch
, regnum
),
544 raw_buffer
, virtual_buffer
);
547 memcpy (virtual_buffer
, raw_buffer
, REGISTER_VIRTUAL_SIZE (regnum
));
552 char *ptr
, buf
[1024];
556 for (j
= 0; j
< REGISTER_RAW_SIZE (regnum
); j
++)
558 register int idx
= TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
? j
559 : REGISTER_RAW_SIZE (regnum
) - 1 - j
;
560 sprintf (ptr
, "%02x", (unsigned char) raw_buffer
[idx
]);
563 ui_out_field_string (uiout
, "value", buf
);
564 /*fputs_filtered (buf, gdb_stdout); */
568 val_print (register_type (current_gdbarch
, regnum
), virtual_buffer
, 0, 0,
569 stb
->stream
, format
, 1, 0, Val_pretty_default
);
570 ui_out_field_stream (uiout
, "value", stb
);
571 ui_out_stream_delete (stb
);
576 /* Write given values into registers. The registers and values are
577 given as pairs. The corresponding MI command is
578 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
580 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
588 /* Note that the test for a valid register must include checking the
589 REGISTER_NAME because NUM_REGS may be allocated for the union of
590 the register sets within a family of related processors. In this
591 case, some entries of REGISTER_NAME will change depending upon
592 the particular processor being debugged. */
598 xasprintf (&mi_error_message
,
599 "mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
603 format
= (int) argv
[0][0];
605 if (!target_has_registers
)
607 xasprintf (&mi_error_message
,
608 "mi_cmd_data_write_register_values: No registers.");
614 xasprintf (&mi_error_message
,
615 "mi_cmd_data_write_register_values: No regs and values specified.");
621 xasprintf (&mi_error_message
,
622 "mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
626 for (i
= 1; i
< argc
; i
= i
+ 2)
628 regnum
= atoi (argv
[i
]);
632 && REGISTER_NAME (regnum
) != NULL
633 && *REGISTER_NAME (regnum
) != '\000')
636 struct cleanup
*old_chain
;
638 /* Get the value as a number */
639 value
= parse_and_eval_address (argv
[i
+ 1]);
640 /* Get the value into an array */
641 buffer
= xmalloc (DEPRECATED_REGISTER_SIZE
);
642 old_chain
= make_cleanup (xfree
, buffer
);
643 store_signed_integer (buffer
, DEPRECATED_REGISTER_SIZE
, value
);
645 deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (regnum
), buffer
, REGISTER_RAW_SIZE (regnum
));
646 /* Free the buffer. */
647 do_cleanups (old_chain
);
651 xasprintf (&mi_error_message
, "bad register number");
659 /*This is commented out because we decided it was not useful. I leave
660 it, just in case. ezannoni:1999-12-08 */
662 /* Assign a value to a variable. The expression argument must be in
663 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
666 mi_cmd_data_assign (char *command
, char **argv
, int argc
)
668 struct expression
*expr
;
669 struct cleanup
*old_chain
;
673 xasprintf (&mi_error_message
,
674 "mi_cmd_data_assign: Usage: -data-assign expression");
678 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
679 01-12-1999: Need to decide what to do with this for libgdb purposes. */
681 expr
= parse_expression (argv
[0]);
682 old_chain
= make_cleanup (free_current_contents
, &expr
);
683 evaluate_expression (expr
);
684 do_cleanups (old_chain
);
689 /* Evaluate the value of the argument. The argument is an
690 expression. If the expression contains spaces it needs to be
691 included in double quotes. */
693 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
695 struct expression
*expr
;
696 struct cleanup
*old_chain
= NULL
;
698 struct ui_stream
*stb
= NULL
;
700 stb
= ui_out_stream_new (uiout
);
704 xasprintf (&mi_error_message
,
705 "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
709 expr
= parse_expression (argv
[0]);
711 old_chain
= make_cleanup (free_current_contents
, &expr
);
713 val
= evaluate_expression (expr
);
715 /* Print the result of the expression evaluation. */
716 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
),
717 VALUE_EMBEDDED_OFFSET (val
), VALUE_ADDRESS (val
),
718 stb
->stream
, 0, 0, 0, 0);
720 ui_out_field_stream (uiout
, "value", stb
);
721 ui_out_stream_delete (stb
);
723 do_cleanups (old_chain
);
729 mi_cmd_target_download (char *args
, int from_tty
)
732 struct cleanup
*old_cleanups
= NULL
;
734 xasprintf (&run
, "load %s", args
);
735 old_cleanups
= make_cleanup (xfree
, run
);
736 execute_command (run
, from_tty
);
738 do_cleanups (old_cleanups
);
742 /* Connect to the remote target. */
744 mi_cmd_target_select (char *args
, int from_tty
)
747 struct cleanup
*old_cleanups
= NULL
;
749 xasprintf (&run
, "target %s", args
);
750 old_cleanups
= make_cleanup (xfree
, run
);
752 /* target-select is always synchronous. once the call has returned
753 we know that we are connected. */
754 /* NOTE: At present all targets that are connected are also
755 (implicitly) talking to a halted target. In the future this may
757 execute_command (run
, from_tty
);
759 do_cleanups (old_cleanups
);
761 /* Issue the completion message here. */
762 if (last_async_command
)
763 fputs_unfiltered (last_async_command
, raw_stdout
);
764 fputs_unfiltered ("^connected", raw_stdout
);
765 mi_out_put (uiout
, raw_stdout
);
766 mi_out_rewind (uiout
);
767 fputs_unfiltered ("\n", raw_stdout
);
768 do_exec_cleanups (ALL_CLEANUPS
);
774 ADDR: start address of data to be dumped.
775 WORD-FORMAT: a char indicating format for the ``word''. See
777 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
778 NR_ROW: Number of rows.
779 NR_COL: The number of colums (words per row).
780 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
781 ASCHAR for unprintable characters.
783 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
784 displayes them. Returns:
786 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
789 The number of bytes read is SIZE*ROW*COL. */
792 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
794 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
800 struct type
*word_type
;
813 static struct mi_opt opts
[] =
815 {"o", OFFSET_OPT
, 1},
821 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
825 switch ((enum opt
) opt
)
828 offset
= atol (optarg
);
835 if (argc
< 5 || argc
> 6)
837 xasprintf (&mi_error_message
,
838 "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
842 /* Extract all the arguments. */
844 /* Start address of the memory dump. */
845 addr
= parse_and_eval_address (argv
[0]) + offset
;
846 /* The format character to use when displaying a memory word. See
847 the ``x'' command. */
848 word_format
= argv
[1][0];
849 /* The size of the memory word. */
850 word_size
= atol (argv
[2]);
854 word_type
= builtin_type_int8
;
858 word_type
= builtin_type_int16
;
862 word_type
= builtin_type_int32
;
866 word_type
= builtin_type_int64
;
870 word_type
= builtin_type_int8
;
873 /* The number of rows */
874 nr_rows
= atol (argv
[3]);
877 xasprintf (&mi_error_message
,
878 "mi_cmd_data_read_memory: invalid number of rows.");
881 /* number of bytes per row. */
882 nr_cols
= atol (argv
[4]);
885 xasprintf (&mi_error_message
,
886 "mi_cmd_data_read_memory: invalid number of columns.");
888 /* The un-printable character when printing ascii. */
894 /* create a buffer and read it in. */
895 total_bytes
= word_size
* nr_rows
* nr_cols
;
896 mbuf
= xcalloc (total_bytes
, 1);
897 make_cleanup (xfree
, mbuf
);
900 xasprintf (&mi_error_message
,
901 "mi_cmd_data_read_memory: out of memory.");
905 while (nr_bytes
< total_bytes
)
908 long num
= target_read_memory_partial (addr
+ nr_bytes
, mbuf
+ nr_bytes
,
909 total_bytes
- nr_bytes
,
916 /* output the header information. */
917 ui_out_field_core_addr (uiout
, "addr", addr
);
918 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
919 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
920 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
921 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
922 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
923 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
925 /* Build the result as a two dimentional table. */
927 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
928 struct cleanup
*cleanup_list_memory
;
931 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
932 for (row
= 0, row_byte
= 0;
934 row
++, row_byte
+= nr_cols
* word_size
)
938 struct cleanup
*cleanup_tuple
;
939 struct cleanup
*cleanup_list_data
;
940 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
941 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
942 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
943 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
944 for (col
= 0, col_byte
= row_byte
;
946 col
++, col_byte
+= word_size
)
948 if (col_byte
+ word_size
> nr_bytes
)
950 ui_out_field_string (uiout
, NULL
, "N/A");
954 ui_file_rewind (stream
->stream
);
955 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
956 word_asize
, stream
->stream
);
957 ui_out_field_stream (uiout
, NULL
, stream
);
960 do_cleanups (cleanup_list_data
);
964 ui_file_rewind (stream
->stream
);
965 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
967 if (byte
>= nr_bytes
)
969 fputc_unfiltered ('X', stream
->stream
);
971 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
973 fputc_unfiltered (aschar
, stream
->stream
);
976 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
978 ui_out_field_stream (uiout
, "ascii", stream
);
980 do_cleanups (cleanup_tuple
);
982 ui_out_stream_delete (stream
);
983 do_cleanups (cleanup_list_memory
);
985 do_cleanups (cleanups
);
989 /* DATA-MEMORY-WRITE:
991 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
992 offset from the beginning of the memory grid row where the cell to
994 ADDR: start address of the row in the memory grid where the memory
995 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
996 the location to write to.
997 FORMAT: a char indicating format for the ``word''. See
999 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
1000 VALUE: value to be written into the memory address.
1002 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1006 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
1011 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1012 enough when using a compiler other than GCC. */
1015 struct cleanup
*old_chain
;
1023 static struct mi_opt opts
[] =
1025 {"o", OFFSET_OPT
, 1},
1031 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
1035 switch ((enum opt
) opt
)
1038 offset
= atol (optarg
);
1047 xasprintf (&mi_error_message
,
1048 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1049 return MI_CMD_ERROR
;
1052 /* Extract all the arguments. */
1053 /* Start address of the memory dump. */
1054 addr
= parse_and_eval_address (argv
[0]);
1055 /* The format character to use when displaying a memory word. See
1056 the ``x'' command. */
1057 word_format
= argv
[1][0];
1058 /* The size of the memory word. */
1059 word_size
= atol (argv
[2]);
1061 /* Calculate the real address of the write destination. */
1062 addr
+= (offset
* word_size
);
1064 /* Get the value as a number */
1065 value
= parse_and_eval_address (argv
[3]);
1066 /* Get the value into an array */
1067 buffer
= xmalloc (word_size
);
1068 old_chain
= make_cleanup (xfree
, buffer
);
1069 store_signed_integer (buffer
, word_size
, value
);
1070 /* Write it down to memory */
1071 write_memory (addr
, buffer
, word_size
);
1072 /* Free the buffer. */
1073 do_cleanups (old_chain
);
1078 /* Execute a command within a safe environment.
1079 Return <0 for error; >=0 for ok.
1081 args->action will tell mi_execute_command what action
1082 to perfrom after the given command has executed (display/supress
1083 prompt, display error). */
1086 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1088 struct captured_mi_execute_command_args
*args
=
1089 (struct captured_mi_execute_command_args
*) data
;
1090 struct mi_parse
*context
= args
->command
;
1092 switch (context
->op
)
1096 /* A MI command was read from the input stream */
1098 /* FIXME: gdb_???? */
1099 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1100 context
->token
, context
->command
, context
->args
);
1101 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1102 condition expression, each function should return an
1103 indication of what action is required and then switch on
1105 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1106 args
->rc
= mi_cmd_execute (context
);
1108 if (!target_can_async_p () || !target_executing
)
1110 /* print the result if there were no errors
1112 Remember that on the way out of executing a command, you have
1113 to directly use the mi_interp's uiout, since the command could
1114 have reset the interpreter, in which case the current uiout
1115 will most likely crash in the mi_out_* routines. */
1116 if (args
->rc
== MI_CMD_DONE
)
1118 fputs_unfiltered (context
->token
, raw_stdout
);
1119 fputs_unfiltered ("^done", raw_stdout
);
1120 mi_out_put (uiout
, raw_stdout
);
1121 mi_out_rewind (uiout
);
1122 fputs_unfiltered ("\n", raw_stdout
);
1124 else if (args
->rc
== MI_CMD_ERROR
)
1126 if (mi_error_message
)
1128 fputs_unfiltered (context
->token
, raw_stdout
);
1129 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1130 fputstr_unfiltered (mi_error_message
, '"', raw_stdout
);
1131 xfree (mi_error_message
);
1132 fputs_unfiltered ("\"\n", raw_stdout
);
1134 mi_out_rewind (uiout
);
1136 else if (args
->rc
== MI_CMD_CAUGHT_ERROR
)
1138 mi_out_rewind (uiout
);
1139 args
->action
= EXECUTE_COMMAND_DISPLAY_ERROR
;
1143 mi_out_rewind (uiout
);
1145 else if (sync_execution
)
1147 /* Don't print the prompt. We are executing the target in
1148 synchronous mode. */
1149 args
->action
= EXECUTE_COMMAND_SUPRESS_PROMPT
;
1155 /* A CLI command was read from the input stream */
1156 /* This will be removed as soon as we have a complete set of
1158 /* echo the command on the console. */
1159 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1160 mi_execute_cli_command (context
->command
, 0, NULL
);
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
)
1165 || current_interp_named_p (INTERP_MI2
)
1166 || current_interp_named_p (INTERP_MI3
))
1168 /* print the result */
1169 /* FIXME: Check for errors here. */
1170 fputs_unfiltered (context
->token
, raw_stdout
);
1171 fputs_unfiltered ("^done", raw_stdout
);
1172 mi_out_put (uiout
, raw_stdout
);
1173 mi_out_rewind (uiout
);
1174 fputs_unfiltered ("\n", raw_stdout
);
1175 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1176 args
->rc
= MI_CMD_DONE
;
1187 mi_execute_command (char *cmd
, int from_tty
)
1189 struct mi_parse
*command
;
1190 struct captured_mi_execute_command_args args
;
1191 struct ui_out
*saved_uiout
= uiout
;
1194 /* This is to handle EOF (^D). We just quit gdb. */
1195 /* FIXME: we should call some API function here. */
1197 quit_force (NULL
, from_tty
);
1199 command
= mi_parse (cmd
);
1201 if (command
!= NULL
)
1203 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
1204 be pushed even further down or even eliminated? */
1205 args
.command
= command
;
1206 result
= catch_exceptions (uiout
, captured_mi_execute_command
, &args
, "",
1209 if (args
.action
== EXECUTE_COMMAND_SUPRESS_PROMPT
)
1211 /* The command is executing synchronously. Bail out early
1212 suppressing the finished prompt. */
1213 mi_parse_free (command
);
1216 if (args
.action
== EXECUTE_COMMAND_DISPLAY_ERROR
|| result
< 0)
1218 char *msg
= error_last_message ();
1219 struct cleanup
*cleanup
= make_cleanup (xfree
, msg
);
1220 /* The command execution failed and error() was called
1222 fputs_unfiltered (command
->token
, raw_stdout
);
1223 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1224 fputstr_unfiltered (msg
, '"', raw_stdout
);
1225 fputs_unfiltered ("\"\n", raw_stdout
);
1227 mi_parse_free (command
);
1230 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1231 gdb_flush (raw_stdout
);
1232 /* print any buffered hook code */
1236 static enum mi_cmd_result
1237 mi_cmd_execute (struct mi_parse
*parse
)
1239 if (parse
->cmd
->argv_func
!= NULL
1240 || parse
->cmd
->args_func
!= NULL
)
1242 /* FIXME: We need to save the token because the command executed
1243 may be asynchronous and need to print the token again.
1244 In the future we can pass the token down to the func
1245 and get rid of the last_async_command */
1246 /* The problem here is to keep the token around when we launch
1247 the target, and we want to interrupt it later on. The
1248 interrupt command will have its own token, but when the
1249 target stops, we must display the token corresponding to the
1250 last execution command given. So we have another string where
1251 we copy the token (previous_async_command), if this was
1252 indeed the token of an execution command, and when we stop we
1253 print that one. This is possible because the interrupt
1254 command, when over, will copy that token back into the
1255 default token string (last_async_command). */
1257 if (target_executing
)
1259 if (!previous_async_command
)
1260 previous_async_command
= xstrdup (last_async_command
);
1261 if (strcmp (parse
->command
, "exec-interrupt"))
1263 fputs_unfiltered (parse
->token
, raw_stdout
);
1264 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1265 fputs_unfiltered ("Cannot execute command ", raw_stdout
);
1266 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1267 fputs_unfiltered (" while target running", raw_stdout
);
1268 fputs_unfiltered ("\"\n", raw_stdout
);
1269 return MI_CMD_ERROR
;
1272 last_async_command
= xstrdup (parse
->token
);
1273 make_exec_cleanup (free_current_contents
, &last_async_command
);
1274 /* FIXME: DELETE THIS! */
1275 if (parse
->cmd
->args_func
!= NULL
)
1276 return parse
->cmd
->args_func (parse
->args
, 0 /*from_tty */ );
1277 return parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1279 else if (parse
->cmd
->cli
.cmd
!= 0)
1281 /* FIXME: DELETE THIS. */
1282 /* The operation is still implemented by a cli command */
1283 /* Must be a synchronous one */
1284 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1290 /* FIXME: DELETE THIS. */
1291 fputs_unfiltered (parse
->token
, raw_stdout
);
1292 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1293 fputs_unfiltered ("Undefined mi command: ", raw_stdout
);
1294 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1295 fputs_unfiltered (" (missing implementation)", raw_stdout
);
1296 fputs_unfiltered ("\"\n", raw_stdout
);
1297 return MI_CMD_ERROR
;
1301 /* FIXME: This is just a hack so we can get some extra commands going.
1302 We don't want to channel things through the CLI, but call libgdb directly */
1303 /* Use only for synchronous commands */
1306 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1310 struct cleanup
*old_cleanups
;
1313 xasprintf (&run
, "%s %s", cmd
, args
);
1315 run
= xstrdup (cmd
);
1317 /* FIXME: gdb_???? */
1318 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1320 old_cleanups
= make_cleanup (xfree
, run
);
1321 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1322 do_cleanups (old_cleanups
);
1328 mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
)
1330 struct cleanup
*old_cleanups
;
1334 if (target_can_async_p ())
1336 async_args
= (char *) xmalloc (strlen (args
) + 2);
1337 make_exec_cleanup (free
, async_args
);
1338 strcpy (async_args
, args
);
1339 strcat (async_args
, "&");
1340 xasprintf (&run
, "%s %s", mi
, async_args
);
1341 make_exec_cleanup (free
, run
);
1342 add_continuation (mi_exec_async_cli_cmd_continuation
, NULL
);
1343 old_cleanups
= NULL
;
1347 xasprintf (&run
, "%s %s", mi
, args
);
1348 old_cleanups
= make_cleanup (xfree
, run
);
1351 if (!target_can_async_p ())
1353 /* NOTE: For synchronous targets asynchronous behavour is faked by
1354 printing out the GDB prompt before we even try to execute the
1356 if (last_async_command
)
1357 fputs_unfiltered (last_async_command
, raw_stdout
);
1358 fputs_unfiltered ("^running\n", raw_stdout
);
1359 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1360 gdb_flush (raw_stdout
);
1364 /* FIXME: cagney/1999-11-29: Printing this message before
1365 calling execute_command is wrong. It should only be printed
1366 once gdb has confirmed that it really has managed to send a
1367 run command to the target. */
1368 if (last_async_command
)
1369 fputs_unfiltered (last_async_command
, raw_stdout
);
1370 fputs_unfiltered ("^running\n", raw_stdout
);
1373 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1375 if (!target_can_async_p ())
1377 /* Do this before doing any printing. It would appear that some
1378 print code leaves garbage around in the buffer. */
1379 do_cleanups (old_cleanups
);
1380 /* If the target was doing the operation synchronously we fake
1381 the stopped message. */
1382 if (last_async_command
)
1383 fputs_unfiltered (last_async_command
, raw_stdout
);
1384 fputs_unfiltered ("*stopped", raw_stdout
);
1385 mi_out_put (uiout
, raw_stdout
);
1386 mi_out_rewind (uiout
);
1387 fputs_unfiltered ("\n", raw_stdout
);
1388 return MI_CMD_QUIET
;
1394 mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
)
1396 if (last_async_command
)
1397 fputs_unfiltered (last_async_command
, raw_stdout
);
1398 fputs_unfiltered ("*stopped", raw_stdout
);
1399 mi_out_put (uiout
, raw_stdout
);
1400 fputs_unfiltered ("\n", raw_stdout
);
1401 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1402 gdb_flush (raw_stdout
);
1403 do_exec_cleanups (ALL_CLEANUPS
);
1407 mi_load_progress (const char *section_name
,
1408 unsigned long sent_so_far
,
1409 unsigned long total_section
,
1410 unsigned long total_sent
,
1411 unsigned long grand_total
)
1413 struct timeval time_now
, delta
, update_threshold
;
1414 static struct timeval last_update
;
1415 static char *previous_sect_name
= NULL
;
1418 if (!current_interp_named_p (INTERP_MI
)
1419 && !current_interp_named_p (INTERP_MI1
))
1422 update_threshold
.tv_sec
= 0;
1423 update_threshold
.tv_usec
= 500000;
1424 gettimeofday (&time_now
, NULL
);
1426 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1427 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1429 if (delta
.tv_usec
< 0)
1432 delta
.tv_usec
+= 1000000;
1435 new_section
= (previous_sect_name
?
1436 strcmp (previous_sect_name
, section_name
) : 1);
1439 struct cleanup
*cleanup_tuple
;
1440 xfree (previous_sect_name
);
1441 previous_sect_name
= xstrdup (section_name
);
1443 if (last_async_command
)
1444 fputs_unfiltered (last_async_command
, raw_stdout
);
1445 fputs_unfiltered ("+download", raw_stdout
);
1446 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1447 ui_out_field_string (uiout
, "section", section_name
);
1448 ui_out_field_int (uiout
, "section-size", total_section
);
1449 ui_out_field_int (uiout
, "total-size", grand_total
);
1450 do_cleanups (cleanup_tuple
);
1451 mi_out_put (uiout
, raw_stdout
);
1452 fputs_unfiltered ("\n", raw_stdout
);
1453 gdb_flush (raw_stdout
);
1456 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1457 delta
.tv_usec
>= update_threshold
.tv_usec
)
1459 struct cleanup
*cleanup_tuple
;
1460 last_update
.tv_sec
= time_now
.tv_sec
;
1461 last_update
.tv_usec
= time_now
.tv_usec
;
1462 if (last_async_command
)
1463 fputs_unfiltered (last_async_command
, raw_stdout
);
1464 fputs_unfiltered ("+download", raw_stdout
);
1465 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1466 ui_out_field_string (uiout
, "section", section_name
);
1467 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1468 ui_out_field_int (uiout
, "section-size", total_section
);
1469 ui_out_field_int (uiout
, "total-sent", total_sent
);
1470 ui_out_field_int (uiout
, "total-size", grand_total
);
1471 do_cleanups (cleanup_tuple
);
1472 mi_out_put (uiout
, raw_stdout
);
1473 fputs_unfiltered ("\n", raw_stdout
);
1474 gdb_flush (raw_stdout
);
1479 mi_setup_architecture_data (void)
1481 old_regs
= xmalloc ((NUM_REGS
+ NUM_PSEUDO_REGS
) * MAX_REGISTER_SIZE
+ 1);
1482 memset (old_regs
, 0, (NUM_REGS
+ NUM_PSEUDO_REGS
) * MAX_REGISTER_SIZE
+ 1);
1486 _initialize_mi_main (void)
1488 register_gdbarch_swap (&old_regs
, sizeof (old_regs
), NULL
);
1489 register_gdbarch_swap (NULL
, 0, mi_setup_architecture_data
);