2 Copyright (C) 2000, Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions.
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"
36 #include "event-loop.h"
37 #include "event-top.h"
38 #include "gdbcore.h" /* for write_memory() */
42 /* Convenience macro for allocting typesafe memory. */
45 #define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE))
54 struct ui_file
*raw_stdout
;
56 /* The token of the last asynchronous command */
57 static char *last_async_command
;
58 static char *previous_async_command
;
59 static char *mi_error_message
;
60 static char *old_regs
;
62 extern void _initialize_mi_main (void);
63 static char *mi_input (char *);
64 static void mi_execute_command (char *cmd
, int from_tty
);
65 static enum mi_cmd_result
mi_cmd_execute (struct mi_parse
*parse
);
67 static void mi_execute_cli_command (const char *cli
, char *args
);
68 static enum mi_cmd_result
mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
);
69 static void mi_execute_command_wrapper (char *cmd
);
71 void mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
);
72 static void free_and_reset (char **arg
);
74 static int register_changed_p (int regnum
);
75 static int get_register (int regnum
, int format
);
76 static void mi_load_progress (const char *section_name
,
77 unsigned long sent_so_far
,
78 unsigned long total_section
,
79 unsigned long total_sent
,
80 unsigned long grand_total
);
83 /* FIXME: these should go in some .h file, but infcmd.c doesn't have a
84 corresponding .h file. These wrappers will be obsolete anyway, once
85 we pull the plug on the sanitization. */
86 extern void interrupt_target_command_wrapper (char *, int);
87 extern void return_command_wrapper (char *, int);
90 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
91 layer that calls libgdb. Any operation used in the below should be
95 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
97 /* We have to print everything right here because we never return */
98 if (last_async_command
)
99 fputs_unfiltered (last_async_command
, raw_stdout
);
100 fputs_unfiltered ("^exit\n", raw_stdout
);
101 mi_out_put (uiout
, raw_stdout
);
102 /* FIXME: The function called is not yet a formal libgdb function */
103 quit_force (NULL
, FROM_TTY
);
108 mi_cmd_exec_run (char *args
, int from_tty
)
110 /* FIXME: Should call a libgdb function, not a cli wrapper */
111 return mi_execute_async_cli_command ("run", args
, from_tty
);
115 mi_cmd_exec_next (char *args
, int from_tty
)
117 /* FIXME: Should call a libgdb function, not a cli wrapper */
118 return mi_execute_async_cli_command ("next", args
, from_tty
);
122 mi_cmd_exec_next_instruction (char *args
, int from_tty
)
124 /* FIXME: Should call a libgdb function, not a cli wrapper */
125 return mi_execute_async_cli_command ("nexti", args
, from_tty
);
129 mi_cmd_exec_step (char *args
, int from_tty
)
131 /* FIXME: Should call a libgdb function, not a cli wrapper */
132 return mi_execute_async_cli_command ("step", args
, from_tty
);
136 mi_cmd_exec_step_instruction (char *args
, int from_tty
)
138 /* FIXME: Should call a libgdb function, not a cli wrapper */
139 return mi_execute_async_cli_command ("stepi", args
, from_tty
);
143 mi_cmd_exec_finish (char *args
, int from_tty
)
145 /* FIXME: Should call a libgdb function, not a cli wrapper */
146 return mi_execute_async_cli_command ("finish", args
, from_tty
);
150 mi_cmd_exec_until (char *args
, int from_tty
)
152 /* FIXME: Should call a libgdb function, not a cli wrapper */
153 return mi_execute_async_cli_command ("until", args
, from_tty
);
157 mi_cmd_exec_return (char *args
, int from_tty
)
160 /* This command doesn't really execute the target, it just pops the
161 specified number of frames. */
163 /* Call return_command with from_tty argument equal to 0 so as to
164 avoid being queried. */
165 return_command_wrapper (args
, 0);
167 /* Call return_command with from_tty argument equal to 0 so as to
168 avoid being queried. */
169 return_command_wrapper (NULL
, 0);
171 /* Because we have called return_command with from_tty = 0, we need
172 to print the frame here. */
173 show_and_print_stack_frame (selected_frame
,
174 selected_frame_level
,
182 mi_cmd_exec_continue (char *args
, int from_tty
)
184 /* FIXME: Should call a libgdb function, not a cli wrapper */
185 return mi_execute_async_cli_command ("continue", args
, from_tty
);
188 /* Interrupt the execution of the target. Note how we must play around
189 with the token varialbes, in order to display the current token in
190 the result of the interrupt command, and the previous execution
191 token when the target finally stops. See comments in
194 mi_cmd_exec_interrupt (char *args
, int from_tty
)
197 if (!target_executing
)
199 asprintf (&mi_error_message
, "mi_cmd_exec_interrupt: Inferior not executing.");
202 interrupt_target_command_wrapper (args
, from_tty
);
203 if (last_async_command
)
204 fputs_unfiltered (last_async_command
, raw_stdout
);
205 fputs_unfiltered ("^done", raw_stdout
);
206 free (last_async_command
);
207 if (previous_async_command
)
208 last_async_command
= xstrdup (previous_async_command
);
209 free (previous_async_command
);
210 previous_async_command
= NULL
;
211 mi_out_put (uiout
, raw_stdout
);
212 mi_out_rewind (uiout
);
213 fputs_unfiltered ("\n", raw_stdout
);
219 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
225 asprintf (&mi_error_message
,
226 "mi_cmd_thread_select: USAGE: threadnum.");
230 rc
= gdb_thread_select (argv
[0]);
232 if (rc
== GDB_RC_FAIL
)
233 return MI_CMD_CAUGHT_ERROR
;
239 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
241 enum gdb_rc rc
= MI_CMD_DONE
;
245 asprintf (&mi_error_message
,
246 "mi_cmd_thread_list_ids: No arguments required.");
251 rc
= gdb_list_thread_ids ();
254 if (rc
== GDB_RC_FAIL
)
255 return MI_CMD_CAUGHT_ERROR
;
261 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
266 /* Note that the test for a valid register must include checking the
267 REGISTER_NAME because NUM_REGS may be allocated for the union of
268 the register sets within a family of related processors. In this
269 case, some entries of REGISTER_NAME will change depending upon
270 the particular processor being debugged. */
272 numregs
= ARCH_NUM_REGS
;
274 ui_out_list_begin (uiout
, "register-names");
276 if (argc
== 0) /* No args, just do all the regs */
282 if (REGISTER_NAME (regnum
) == NULL
283 || *(REGISTER_NAME (regnum
)) == '\0')
286 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
290 /* Else, list of register #s, just do listed regs */
291 for (i
= 0; i
< argc
; i
++)
293 regnum
= atoi (argv
[i
]);
297 && REGISTER_NAME (regnum
) != NULL
298 && *REGISTER_NAME (regnum
) != '\000')
299 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
302 asprintf (&mi_error_message
, "bad register number");
306 ui_out_list_end (uiout
);
311 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
313 int regnum
, numregs
, changed
;
316 /* Note that the test for a valid register must include checking the
317 REGISTER_NAME because NUM_REGS may be allocated for the union of
318 the register sets within a family of related processors. In this
319 case, some entries of REGISTER_NAME will change depending upon
320 the particular processor being debugged. */
322 numregs
= ARCH_NUM_REGS
;
324 ui_out_list_begin (uiout
, "changed-registers");
326 if (argc
== 0) /* No args, just do all the regs */
332 if (REGISTER_NAME (regnum
) == NULL
333 || *(REGISTER_NAME (regnum
)) == '\0')
335 changed
= register_changed_p (regnum
);
338 asprintf (&mi_error_message
,
339 "mi_cmd_data_list_changed_registers: Unable to read register contents.");
343 ui_out_field_int (uiout
, NULL
, regnum
);
347 /* Else, list of register #s, just do listed regs */
348 for (i
= 0; i
< argc
; i
++)
350 regnum
= atoi (argv
[i
]);
354 && REGISTER_NAME (regnum
) != NULL
355 && *REGISTER_NAME (regnum
) != '\000')
357 changed
= register_changed_p (regnum
);
360 asprintf (&mi_error_message
,
361 "mi_cmd_data_list_register_change: Unable to read register contents.");
365 ui_out_field_int (uiout
, NULL
, regnum
);
369 asprintf (&mi_error_message
, "bad register number");
373 ui_out_list_end (uiout
);
378 register_changed_p (int regnum
)
380 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
382 if (read_relative_register_raw_bytes (regnum
, raw_buffer
))
385 if (memcmp (&old_regs
[REGISTER_BYTE (regnum
)], raw_buffer
,
386 REGISTER_RAW_SIZE (regnum
)) == 0)
389 /* Found a changed register. Return 1. */
391 memcpy (&old_regs
[REGISTER_BYTE (regnum
)], raw_buffer
,
392 REGISTER_RAW_SIZE (regnum
));
397 /* Return a list of register number and value pairs. The valid
398 arguments expected are: a letter indicating the format in which to
399 display the registers contents. This can be one of: x (hexadecimal), d
400 (decimal), N (natural), t (binary), o (octal), r (raw). After the
401 format argumetn there can be a sequence of numbers, indicating which
402 registers to fetch the content of. If the format is the only argument,
403 a list of all the registers with their values is returned. */
405 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
407 int regnum
, numregs
, format
, result
;
410 /* Note that the test for a valid register must include checking the
411 REGISTER_NAME because NUM_REGS may be allocated for the union of
412 the register sets within a family of related processors. In this
413 case, some entries of REGISTER_NAME will change depending upon
414 the particular processor being debugged. */
416 numregs
= ARCH_NUM_REGS
;
420 asprintf (&mi_error_message
,
421 "mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
425 format
= (int) argv
[0][0];
427 if (!target_has_registers
)
429 asprintf (&mi_error_message
, "mi_cmd_data_list_register_values: No registers.");
433 ui_out_list_begin (uiout
, "register-values");
435 if (argc
== 1) /* No args, beside the format: do all the regs */
441 if (REGISTER_NAME (regnum
) == NULL
442 || *(REGISTER_NAME (regnum
)) == '\0')
444 ui_out_list_begin (uiout
, NULL
);
445 ui_out_field_int (uiout
, "number", regnum
);
446 result
= get_register (regnum
, format
);
449 ui_out_list_end (uiout
);
453 /* Else, list of register #s, just do listed regs */
454 for (i
= 1; i
< argc
; i
++)
456 regnum
= atoi (argv
[i
]);
460 && REGISTER_NAME (regnum
) != NULL
461 && *REGISTER_NAME (regnum
) != '\000')
463 ui_out_list_begin (uiout
, NULL
);
464 ui_out_field_int (uiout
, "number", regnum
);
465 result
= get_register (regnum
, format
);
468 ui_out_list_end (uiout
);
472 asprintf (&mi_error_message
, "bad register number");
476 ui_out_list_end (uiout
);
480 /* Output one register's contents in the desired format. */
482 get_register (int regnum
, int format
)
484 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
485 char virtual_buffer
[MAX_REGISTER_VIRTUAL_SIZE
];
487 static struct ui_stream
*stb
= NULL
;
489 stb
= ui_out_stream_new (uiout
);
494 /* read_relative_register_raw_bytes returns a virtual frame pointer
495 (FRAME_FP (selected_frame)) if regnum == FP_REGNUM instead
496 of the real contents of the register. To get around this,
497 use get_saved_register instead. */
498 get_saved_register (raw_buffer
, &optim
, (CORE_ADDR
*) NULL
, selected_frame
,
499 regnum
, (enum lval_type
*) NULL
);
502 asprintf (&mi_error_message
, "Optimized out");
506 /* Convert raw data to virtual format if necessary. */
508 if (REGISTER_CONVERTIBLE (regnum
))
510 REGISTER_CONVERT_TO_VIRTUAL (regnum
, REGISTER_VIRTUAL_TYPE (regnum
),
511 raw_buffer
, virtual_buffer
);
514 memcpy (virtual_buffer
, raw_buffer
, REGISTER_VIRTUAL_SIZE (regnum
));
519 char *ptr
, buf
[1024];
523 for (j
= 0; j
< REGISTER_RAW_SIZE (regnum
); j
++)
525 register int idx
= TARGET_BYTE_ORDER
== BIG_ENDIAN
? j
526 : REGISTER_RAW_SIZE (regnum
) - 1 - j
;
527 sprintf (ptr
, "%02x", (unsigned char) raw_buffer
[idx
]);
530 ui_out_field_string (uiout
, "value", buf
);
531 /*fputs_filtered (buf, gdb_stdout); */
535 val_print (REGISTER_VIRTUAL_TYPE (regnum
), virtual_buffer
, 0, 0,
536 stb
->stream
, format
, 1, 0, Val_pretty_default
);
537 ui_out_field_stream (uiout
, "value", stb
);
538 ui_out_stream_delete (stb
);
544 /*This is commented out because we decided it was not useful. I leave
545 it, just in case. ezannoni:1999-12-08 */
547 /* Assign a value to a variable. The expression argument must be in
548 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
551 mi_cmd_data_assign (char *command
, char **argv
, int argc
)
553 struct expression
*expr
;
554 struct cleanup
*old_chain
;
558 asprintf (&mi_error_message
,
559 "mi_cmd_data_assign: Usage: -data-assign expression");
563 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
564 01-12-1999: Need to decide what to do with this for libgdb purposes. */
566 expr
= parse_expression (argv
[0]);
567 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
568 evaluate_expression (expr
);
569 do_cleanups (old_chain
);
574 /* Evaluate the value of the argument. The argument is an
575 expression. If the expression contains spaces it needs to be
576 included in double quotes. */
578 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
580 struct expression
*expr
;
581 struct cleanup
*old_chain
= NULL
;
583 struct ui_stream
*stb
= NULL
;
585 stb
= ui_out_stream_new (uiout
);
589 asprintf (&mi_error_message
,
590 "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
594 expr
= parse_expression (argv
[0]);
596 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
598 val
= evaluate_expression (expr
);
600 /* Print the result of the expression evaluation. */
601 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
),
602 VALUE_EMBEDDED_OFFSET (val
), VALUE_ADDRESS (val
),
603 stb
->stream
, 0, 0, 0, 0);
605 ui_out_field_stream (uiout
, "value", stb
);
606 ui_out_stream_delete (stb
);
608 do_cleanups (old_chain
);
614 mi_cmd_target_download (char *args
, int from_tty
)
617 struct cleanup
*old_cleanups
= NULL
;
619 asprintf (&run
, "load %s", args
);
621 internal_error ("mi_cmd_target_download: no memory");
622 old_cleanups
= make_cleanup (free
, run
);
623 execute_command (run
, from_tty
);
625 do_cleanups (old_cleanups
);
629 /* Connect to the remote target. */
631 mi_cmd_target_select (char *args
, int from_tty
)
634 struct cleanup
*old_cleanups
= NULL
;
636 asprintf (&run
, "target %s", args
);
638 internal_error ("mi_cmd_target_select: no memory");
639 old_cleanups
= make_cleanup (free
, run
);
641 /* target-select is always synchronous. once the call has returned
642 we know that we are connected. */
643 /* NOTE: At present all targets that are connected are also
644 (implicitly) talking to a halted target. In the future this may
646 execute_command (run
, from_tty
);
648 do_cleanups (old_cleanups
);
650 /* Issue the completion message here. */
651 if (last_async_command
)
652 fputs_unfiltered (last_async_command
, raw_stdout
);
653 fputs_unfiltered ("^connected", raw_stdout
);
654 mi_out_put (uiout
, raw_stdout
);
655 mi_out_rewind (uiout
);
656 fputs_unfiltered ("\n", raw_stdout
);
657 do_exec_cleanups (ALL_CLEANUPS
);
663 ADDR: start address of data to be dumped.
664 WORD-FORMAT: a char indicating format for the ``word''. See
666 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
667 NR_ROW: Number of rows.
668 NR_COL: The number of colums (words per row).
669 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
670 ASCHAR for unprintable characters.
672 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
673 displayes them. Returns:
675 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
678 The number of bytes read is SIZE*ROW*COL. */
681 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
683 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
689 struct type
*word_type
;
702 static struct mi_opt opts
[] =
704 {"o", OFFSET_OPT
, 1},
710 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
714 switch ((enum opt
) opt
)
717 offset
= atol (optarg
);
724 if (argc
< 5 || argc
> 6)
726 asprintf (&mi_error_message
,
727 "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
731 /* Extract all the arguments. */
733 /* Start address of the memory dump. */
734 addr
= parse_and_eval_address (argv
[0]) + offset
;
735 /* The format character to use when displaying a memory word. See
736 the ``x'' command. */
737 word_format
= argv
[1][0];
738 /* The size of the memory word. */
739 word_size
= atol (argv
[2]);
743 word_type
= builtin_type_int8
;
747 word_type
= builtin_type_int16
;
751 word_type
= builtin_type_int32
;
755 word_type
= builtin_type_int64
;
759 word_type
= builtin_type_int8
;
762 /* The number of rows */
763 nr_rows
= atol (argv
[3]);
766 asprintf (&mi_error_message
,
767 "mi_cmd_data_read_memory: invalid number of rows.");
770 /* number of bytes per row. */
771 nr_cols
= atol (argv
[4]);
774 asprintf (&mi_error_message
,
775 "mi_cmd_data_read_memory: invalid number of columns.");
777 /* The un-printable character when printing ascii. */
783 /* create a buffer and read it in. */
784 total_bytes
= word_size
* nr_rows
* nr_cols
;
785 mbuf
= calloc (total_bytes
, 1);
786 make_cleanup (free
, mbuf
);
789 asprintf (&mi_error_message
,
790 "mi_cmd_data_read_memory: out of memory.");
794 while (nr_bytes
< total_bytes
)
797 long num
= target_read_memory_partial (addr
+ nr_bytes
, mbuf
+ nr_bytes
,
798 total_bytes
- nr_bytes
,
805 /* output the header information. */
806 ui_out_field_core_addr (uiout
, "addr", addr
);
807 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
808 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
809 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
810 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
811 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
812 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
814 /* Build the result as a two dimentional table. */
816 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
819 ui_out_list_begin (uiout
, "memory");
820 for (row
= 0, row_byte
= 0;
822 row
++, row_byte
+= nr_cols
* word_size
)
826 ui_out_list_begin (uiout
, NULL
);
827 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
828 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
829 ui_out_list_begin (uiout
, "data");
830 for (col
= 0, col_byte
= row_byte
;
832 col
++, col_byte
+= word_size
)
834 if (col_byte
+ word_size
> nr_bytes
)
836 ui_out_field_string (uiout
, NULL
, "N/A");
840 ui_file_rewind (stream
->stream
);
841 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
842 word_asize
, stream
->stream
);
843 ui_out_field_stream (uiout
, NULL
, stream
);
846 ui_out_list_end (uiout
);
850 ui_file_rewind (stream
->stream
);
851 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
853 if (byte
>= nr_bytes
)
855 fputc_unfiltered ('X', stream
->stream
);
857 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
859 fputc_unfiltered (aschar
, stream
->stream
);
862 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
864 ui_out_field_stream (uiout
, "ascii", stream
);
866 ui_out_list_end (uiout
);
868 ui_out_stream_delete (stream
);
869 ui_out_list_end (uiout
);
871 do_cleanups (cleanups
);
875 /* DATA-MEMORY-WRITE:
877 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
878 offset from the beginning of the memory grid row where the cell to
880 ADDR: start address of the row in the memory grid where the memory
881 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
882 the location to write to.
883 FORMAT: a char indicating format for the ``word''. See
885 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
886 VALUE: value to be written into the memory address.
888 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
892 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
897 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
898 enough when using a compiler other than GCC. */
900 unsigned char *buffer
;
908 static struct mi_opt opts
[] =
910 {"o", OFFSET_OPT
, 1},
916 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
920 switch ((enum opt
) opt
)
923 offset
= atol (optarg
);
932 asprintf (&mi_error_message
,
933 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
937 /* Extract all the arguments. */
938 /* Start address of the memory dump. */
939 addr
= parse_and_eval_address (argv
[0]);
940 /* The format character to use when displaying a memory word. See
941 the ``x'' command. */
942 word_format
= argv
[1][0];
943 /* The size of the memory word. */
944 word_size
= atol (argv
[2]);
946 /* Calculate the real address of the write destination. */
947 addr
+= (offset
* word_size
);
949 /* Get the value as a number */
950 value
= parse_and_eval_address (argv
[3]);
951 /* Get the value into an array */
952 buffer
= (unsigned char *) xmalloc (word_size
);
953 store_signed_integer (buffer
, word_size
, value
);
954 /* Write it down to memory */
955 write_memory (addr
, buffer
, word_size
);
960 /* Execute a command within a safe environment. Return >0 for
961 ok. Return <0 for supress prompt. Return 0 to have the error
962 extracted from error_last_message(). */
965 captured_mi_execute_command (void *data
)
967 struct mi_parse
*context
= data
;
968 enum mi_cmd_result rc
;
974 /* A MI command was read from the input stream */
976 /* FIXME: gdb_???? */
977 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
978 context
->token
, context
->command
, context
->args
);
979 /* FIXME: cagney/1999-09-25: Rather than this convoluted
980 condition expression, each function should return an
981 indication of what action is required and then switch on
983 rc
= mi_cmd_execute (context
);
984 if (!target_can_async_p () || !target_executing
)
986 /* print the result if there were no errors */
987 if (rc
== MI_CMD_DONE
)
989 fputs_unfiltered (context
->token
, raw_stdout
);
990 fputs_unfiltered ("^done", raw_stdout
);
991 mi_out_put (uiout
, raw_stdout
);
992 mi_out_rewind (uiout
);
993 fputs_unfiltered ("\n", raw_stdout
);
995 else if (rc
== MI_CMD_ERROR
)
997 if (mi_error_message
)
999 fputs_unfiltered (context
->token
, raw_stdout
);
1000 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1001 fputstr_unfiltered (mi_error_message
, '"', raw_stdout
);
1002 free (mi_error_message
);
1003 fputs_unfiltered ("\"\n", raw_stdout
);
1005 mi_out_rewind (uiout
);
1007 else if (rc
== MI_CMD_CAUGHT_ERROR
)
1009 mi_out_rewind (uiout
);
1013 mi_out_rewind (uiout
);
1015 else if (sync_execution
)
1016 /* Don't print the prompt. We are executing the target in
1017 synchronous mode. */
1022 /* A CLI command was read from the input stream */
1023 /* This will be removed as soon as we have a complete set of
1025 /* echo the command on the console. */
1026 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1027 /* FIXME: If the command string has something that looks like
1028 a format spec (e.g. %s) we will get a core dump */
1029 mi_execute_cli_command ("%s", context
->command
);
1030 /* print the result */
1031 /* FIXME: Check for errors here. */
1032 fputs_unfiltered (context
->token
, raw_stdout
);
1033 fputs_unfiltered ("^done", raw_stdout
);
1034 mi_out_put (uiout
, raw_stdout
);
1035 mi_out_rewind (uiout
);
1036 fputs_unfiltered ("\n", raw_stdout
);
1045 mi_execute_command (char *cmd
, int from_tty
)
1047 struct mi_parse
*command
;
1049 /* This is to handle EOF (^D). We just quit gdb. */
1050 /* FIXME: we should call some API function here. */
1052 quit_force (NULL
, from_tty
);
1054 command
= mi_parse (cmd
);
1056 if (command
!= NULL
)
1058 /* FIXME: cagney/1999-11-04: Can this use of catch_errors either
1059 be pushed even further down or even eliminated? */
1060 int rc
= catch_errors (captured_mi_execute_command
, command
, "",
1064 /* The command is executing synchronously. Bail out early
1065 suppressing the finished prompt. */
1066 mi_parse_free (command
);
1071 char *msg
= error_last_message ();
1072 struct cleanup
*cleanup
= make_cleanup (free
, msg
);
1073 /* The command execution failed and error() was called
1075 fputs_unfiltered (command
->token
, raw_stdout
);
1076 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1077 fputstr_unfiltered (msg
, '"', raw_stdout
);
1078 fputs_unfiltered ("\"\n", raw_stdout
);
1080 mi_parse_free (command
);
1083 gdb_flush (raw_stdout
);
1084 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1085 /* print any buffered hook code */
1089 static enum mi_cmd_result
1090 mi_cmd_execute (struct mi_parse
*parse
)
1092 if (parse
->cmd
->argv_func
!= NULL
1093 || parse
->cmd
->args_func
!= NULL
)
1095 /* FIXME: We need to save the token because the command executed
1096 may be asynchronous and need to print the token again.
1097 In the future we can pass the token down to the func
1098 and get rid of the last_async_command */
1099 /* The problem here is to keep the token around when we launch
1100 the target, and we want to interrupt it later on. The
1101 interrupt command will have its own token, but when the
1102 target stops, we must display the token corresponding to the
1103 last execution command given. So we have another string where
1104 we copy the token (previous_async_command), if this was
1105 indeed the token of an execution command, and when we stop we
1106 print that one. This is possible because the interrupt
1107 command, when over, will copy that token back into the
1108 default token string (last_async_command). */
1110 if (target_executing
)
1112 if (!previous_async_command
)
1113 previous_async_command
= xstrdup (last_async_command
);
1114 if (strcmp (parse
->command
, "exec-interrupt"))
1116 fputs_unfiltered (parse
->token
, raw_stdout
);
1117 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1118 fputs_unfiltered ("Cannot execute command ", raw_stdout
);
1119 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1120 fputs_unfiltered (" while target running", raw_stdout
);
1121 fputs_unfiltered ("\"\n", raw_stdout
);
1122 return MI_CMD_ERROR
;
1125 last_async_command
= xstrdup (parse
->token
);
1126 make_exec_cleanup ((make_cleanup_func
) free_and_reset
, &last_async_command
);
1127 /* FIXME: DELETE THIS! */
1128 if (parse
->cmd
->args_func
!= NULL
)
1129 return parse
->cmd
->args_func (parse
->args
, 0 /*from_tty */ );
1130 return parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1132 else if (parse
->cmd
->cli
!= 0)
1134 /* FIXME: DELETE THIS. */
1135 /* The operation is still implemented by a cli command */
1136 /* Must be a synchronous one */
1137 mi_execute_cli_command (parse
->cmd
->cli
, parse
->args
);
1142 /* FIXME: DELETE THIS. */
1143 fputs_unfiltered (parse
->token
, raw_stdout
);
1144 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1145 fputs_unfiltered ("Undefined mi command: ", raw_stdout
);
1146 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1147 fputs_unfiltered (" (missing implementation)", raw_stdout
);
1148 fputs_unfiltered ("\"\n", raw_stdout
);
1149 return MI_CMD_ERROR
;
1154 free_and_reset (char **arg
)
1161 mi_execute_command_wrapper (char *cmd
)
1163 mi_execute_command (cmd
, stdin
== instream
);
1166 /* FIXME: This is just a hack so we can get some extra commands going.
1167 We don't want to channel things through the CLI, but call libgdb directly */
1168 /* Use only for synchronous commands */
1171 mi_execute_cli_command (const char *cli
, char *args
)
1175 struct cleanup
*old_cleanups
;
1177 asprintf (&run
, cli
, args
);
1179 /* FIXME: gdb_???? */
1180 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1184 old_cleanups
= make_cleanup (free
, run
);
1185 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1186 do_cleanups (old_cleanups
);
1192 mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
)
1194 struct cleanup
*old_cleanups
;
1198 if (target_can_async_p ())
1200 async_args
= (char *) xmalloc (strlen (args
) + 2);
1201 make_exec_cleanup (free
, async_args
);
1202 strcpy (async_args
, args
);
1203 strcat (async_args
, "&");
1204 asprintf (&run
, "%s %s", mi
, async_args
);
1206 internal_error ("mi_execute_async_cli_command: no memory");
1207 make_exec_cleanup (free
, run
);
1208 add_continuation (mi_exec_async_cli_cmd_continuation
, NULL
);
1212 asprintf (&run
, "%s %s", mi
, args
);
1214 internal_error ("mi_execute_async_cli_command: no memory");
1215 old_cleanups
= make_cleanup (free
, run
);
1218 if (!target_can_async_p ())
1220 /* NOTE: For synchronous targets asynchronous behavour is faked by
1221 printing out the GDB prompt before we even try to execute the
1223 if (last_async_command
)
1224 fputs_unfiltered (last_async_command
, raw_stdout
);
1225 fputs_unfiltered ("^running\n", raw_stdout
);
1226 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1230 /* FIXME: cagney/1999-11-29: Printing this message before
1231 calling execute_command is wrong. It should only be printed
1232 once gdb has confirmed that it really has managed to send a
1233 run command to the target. */
1234 if (last_async_command
)
1235 fputs_unfiltered (last_async_command
, raw_stdout
);
1236 fputs_unfiltered ("^running\n", raw_stdout
);
1239 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1241 if (!target_can_async_p ())
1243 /* Do this before doing any printing. It would appear that some
1244 print code leaves garbage around in the buffer. */
1245 do_cleanups (old_cleanups
);
1246 /* If the target was doing the operation synchronously we fake
1247 the stopped message. */
1248 if (last_async_command
)
1249 fputs_unfiltered (last_async_command
, raw_stdout
);
1250 fputs_unfiltered ("*stopped", raw_stdout
);
1251 mi_out_put (uiout
, raw_stdout
);
1252 mi_out_rewind (uiout
);
1253 fputs_unfiltered ("\n", raw_stdout
);
1254 return MI_CMD_QUIET
;
1260 mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
)
1262 if (last_async_command
)
1263 fputs_unfiltered (last_async_command
, raw_stdout
);
1264 fputs_unfiltered ("*stopped", raw_stdout
);
1265 mi_out_put (uiout
, raw_stdout
);
1266 fputs_unfiltered ("\n", raw_stdout
);
1267 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1268 do_exec_cleanups (ALL_CLEANUPS
);
1272 mi_input (char *buf
)
1274 return gdb_readline (NULL
);
1278 mi_load_progress (const char *section_name
,
1279 unsigned long sent_so_far
,
1280 unsigned long total_section
,
1281 unsigned long total_sent
,
1282 unsigned long grand_total
)
1284 struct timeval time_now
, delta
, update_threshold
;
1285 static struct timeval last_update
;
1286 static char *previous_sect_name
= NULL
;
1289 if (!interpreter_p
|| strcmp (interpreter_p
, "mi") != 0)
1292 update_threshold
.tv_sec
= 0;
1293 update_threshold
.tv_usec
= 500000;
1294 gettimeofday (&time_now
, NULL
);
1296 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1297 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1299 if (delta
.tv_usec
< 0)
1302 delta
.tv_usec
+= 1000000;
1305 new_section
= (previous_sect_name
?
1306 strcmp (previous_sect_name
, section_name
) : 1);
1309 free (previous_sect_name
);
1310 previous_sect_name
= xstrdup (section_name
);
1312 if (last_async_command
)
1313 fputs_unfiltered (last_async_command
, raw_stdout
);
1314 fputs_unfiltered ("+download", raw_stdout
);
1315 ui_out_list_begin (uiout
, NULL
);
1316 ui_out_field_string (uiout
, "section", section_name
);
1317 ui_out_field_int (uiout
, "section-size", total_section
);
1318 ui_out_field_int (uiout
, "total-size", grand_total
);
1319 ui_out_list_end (uiout
);
1320 mi_out_put (uiout
, raw_stdout
);
1321 fputs_unfiltered ("\n", raw_stdout
);
1322 gdb_flush (raw_stdout
);
1325 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1326 delta
.tv_usec
>= update_threshold
.tv_usec
)
1328 last_update
.tv_sec
= time_now
.tv_sec
;
1329 last_update
.tv_usec
= time_now
.tv_usec
;
1330 if (last_async_command
)
1331 fputs_unfiltered (last_async_command
, raw_stdout
);
1332 fputs_unfiltered ("+download", raw_stdout
);
1333 ui_out_list_begin (uiout
, NULL
);
1334 ui_out_field_string (uiout
, "section", section_name
);
1335 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1336 ui_out_field_int (uiout
, "section-size", total_section
);
1337 ui_out_field_int (uiout
, "total-sent", total_sent
);
1338 ui_out_field_int (uiout
, "total-size", grand_total
);
1339 ui_out_list_end (uiout
);
1340 mi_out_put (uiout
, raw_stdout
);
1341 fputs_unfiltered ("\n", raw_stdout
);
1342 gdb_flush (raw_stdout
);
1349 /* HACK: Force stdout/stderr to point at the console. This avoids
1350 any potential side effects caused by legacy code that is still
1351 using the TUI / fputs_unfiltered_hook */
1352 raw_stdout
= stdio_fileopen (stdout
);
1353 /* Route normal output through the MIx */
1354 gdb_stdout
= mi_console_file_new (raw_stdout
, "~");
1355 /* Route error and log output through the MI */
1356 gdb_stderr
= mi_console_file_new (raw_stdout
, "&");
1357 gdb_stdlog
= gdb_stderr
;
1358 /* Route target output through the MI. */
1359 gdb_stdtarg
= mi_console_file_new (raw_stdout
, "@");
1361 /* HACK: Poke the ui_out table directly. Should we be creating a
1362 mi_out object wired up to the above gdb_stdout / gdb_stderr? */
1363 uiout
= mi_out_new ();
1365 /* HACK: Override any other interpreter hooks. We need to create a
1366 real event table and pass in that. */
1368 /* command_loop_hook = 0; */
1369 print_frame_info_listing_hook
= 0;
1372 create_breakpoint_hook
= 0;
1373 delete_breakpoint_hook
= 0;
1374 modify_breakpoint_hook
= 0;
1375 interactive_hook
= 0;
1376 registers_changed_hook
= 0;
1377 readline_begin_hook
= 0;
1379 readline_end_hook
= 0;
1380 register_changed_hook
= 0;
1381 memory_changed_hook
= 0;
1383 target_wait_hook
= 0;
1384 call_command_hook
= 0;
1386 error_begin_hook
= 0;
1387 show_load_progress
= mi_load_progress
;
1389 /* Turn off 8 bit strings in quoted output. Any character with the
1390 high bit set is printed using C's octal format. */
1391 sevenbit_strings
= 1;
1393 /* Tell the world that we're alive */
1394 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1397 simplified_command_loop (mi_input
, mi_execute_command
);
1399 start_event_loop ();
1403 setup_architecture_data ()
1405 /* don't trust REGISTER_BYTES to be zero. */
1406 old_regs
= xmalloc (REGISTER_BYTES
+ 1);
1407 memset (old_regs
, 0, REGISTER_BYTES
+ 1);
1414 /* Eventually this will contain code that takes control of the
1419 _initialize_mi_main ()
1421 /* If we're _the_ interpreter, take control. */
1423 && strcmp (interpreter_p
, "mi") == 0)
1425 init_ui_hook
= mi_init_ui
;
1426 command_loop_hook
= mi_command_loop
;
1427 setup_architecture_data ();
1428 register_gdbarch_swap (&old_regs
, sizeof (old_regs
), NULL
);
1429 register_gdbarch_swap (NULL
, 0, setup_architecture_data
);
1432 /* These overwrite some of the initialization done in
1433 _intialize_event_loop. */
1434 call_readline
= gdb_readline2
;
1435 input_handler
= mi_execute_command_wrapper
;
1436 add_file_handler (input_fd
, stdin_event_handler
, 0);
1437 async_command_editing_p
= 0;
1440 /* FIXME: Should we notify main that we are here as a possible
1444 /* Local variables: */
1445 /* change-log-default-name: "ChangeLog-mi" */