2 Copyright 2000, 2001 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"
36 #include "event-loop.h"
37 #include "event-top.h"
38 #include "gdbcore.h" /* for write_memory() */
39 #include "value.h" /* for write_register_bytes() */
45 /* Convenience macro for allocting typesafe memory. */
48 #define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE))
57 struct ui_file
*raw_stdout
;
59 /* The token of the last asynchronous command */
60 static char *last_async_command
;
61 static char *previous_async_command
;
62 static char *mi_error_message
;
63 static char *old_regs
;
65 extern void _initialize_mi_main (void);
66 static char *mi_input (char *);
67 static void mi_execute_command (char *cmd
, int from_tty
);
68 static enum mi_cmd_result
mi_cmd_execute (struct mi_parse
*parse
);
70 static void mi_execute_cli_command (const char *cli
, char *args
);
71 static enum mi_cmd_result
mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
);
72 static void mi_execute_command_wrapper (char *cmd
);
74 void mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
);
76 static int register_changed_p (int regnum
);
77 static int get_register (int regnum
, int format
);
78 static void mi_load_progress (const char *section_name
,
79 unsigned long sent_so_far
,
80 unsigned long total_section
,
81 unsigned long total_sent
,
82 unsigned long grand_total
);
85 /* FIXME: these should go in some .h file, but infcmd.c doesn't have a
86 corresponding .h file. These wrappers will be obsolete anyway, once
87 we pull the plug on the sanitization. */
88 extern void interrupt_target_command_wrapper (char *, int);
89 extern void return_command_wrapper (char *, int);
92 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
93 layer that calls libgdb. Any operation used in the below should be
97 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
99 /* We have to print everything right here because we never return */
100 if (last_async_command
)
101 fputs_unfiltered (last_async_command
, raw_stdout
);
102 fputs_unfiltered ("^exit\n", raw_stdout
);
103 mi_out_put (uiout
, raw_stdout
);
104 /* FIXME: The function called is not yet a formal libgdb function */
105 quit_force (NULL
, FROM_TTY
);
110 mi_cmd_exec_run (char *args
, int from_tty
)
112 /* FIXME: Should call a libgdb function, not a cli wrapper */
113 return mi_execute_async_cli_command ("run", args
, from_tty
);
117 mi_cmd_exec_next (char *args
, int from_tty
)
119 /* FIXME: Should call a libgdb function, not a cli wrapper */
120 return mi_execute_async_cli_command ("next", args
, from_tty
);
124 mi_cmd_exec_next_instruction (char *args
, int from_tty
)
126 /* FIXME: Should call a libgdb function, not a cli wrapper */
127 return mi_execute_async_cli_command ("nexti", args
, from_tty
);
131 mi_cmd_exec_step (char *args
, int from_tty
)
133 /* FIXME: Should call a libgdb function, not a cli wrapper */
134 return mi_execute_async_cli_command ("step", args
, from_tty
);
138 mi_cmd_exec_step_instruction (char *args
, int from_tty
)
140 /* FIXME: Should call a libgdb function, not a cli wrapper */
141 return mi_execute_async_cli_command ("stepi", args
, from_tty
);
145 mi_cmd_exec_finish (char *args
, int from_tty
)
147 /* FIXME: Should call a libgdb function, not a cli wrapper */
148 return mi_execute_async_cli_command ("finish", args
, from_tty
);
152 mi_cmd_exec_until (char *args
, int from_tty
)
154 /* FIXME: Should call a libgdb function, not a cli wrapper */
155 return mi_execute_async_cli_command ("until", args
, from_tty
);
159 mi_cmd_exec_return (char *args
, int from_tty
)
162 /* This command doesn't really execute the target, it just pops the
163 specified number of frames. */
165 /* Call return_command with from_tty argument equal to 0 so as to
166 avoid being queried. */
167 return_command_wrapper (args
, 0);
169 /* Call return_command with from_tty argument equal to 0 so as to
170 avoid being queried. */
171 return_command_wrapper (NULL
, 0);
173 /* Because we have called return_command with from_tty = 0, we need
174 to print the frame here. */
175 show_and_print_stack_frame (selected_frame
,
176 selected_frame_level
,
184 mi_cmd_exec_continue (char *args
, int from_tty
)
186 /* FIXME: Should call a libgdb function, not a cli wrapper */
187 return mi_execute_async_cli_command ("continue", args
, from_tty
);
190 /* Interrupt the execution of the target. Note how we must play around
191 with the token varialbes, in order to display the current token in
192 the result of the interrupt command, and the previous execution
193 token when the target finally stops. See comments in
196 mi_cmd_exec_interrupt (char *args
, int from_tty
)
199 if (!target_executing
)
201 xasprintf (&mi_error_message
,
202 "mi_cmd_exec_interrupt: Inferior not executing.");
205 interrupt_target_command_wrapper (args
, from_tty
);
206 if (last_async_command
)
207 fputs_unfiltered (last_async_command
, raw_stdout
);
208 fputs_unfiltered ("^done", raw_stdout
);
209 xfree (last_async_command
);
210 if (previous_async_command
)
211 last_async_command
= xstrdup (previous_async_command
);
212 xfree (previous_async_command
);
213 previous_async_command
= NULL
;
214 mi_out_put (uiout
, raw_stdout
);
215 mi_out_rewind (uiout
);
216 fputs_unfiltered ("\n", raw_stdout
);
222 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
228 xasprintf (&mi_error_message
,
229 "mi_cmd_thread_select: USAGE: threadnum.");
233 rc
= gdb_thread_select (argv
[0]);
235 if (rc
== GDB_RC_FAIL
)
236 return MI_CMD_CAUGHT_ERROR
;
242 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
244 enum gdb_rc rc
= MI_CMD_DONE
;
248 xasprintf (&mi_error_message
,
249 "mi_cmd_thread_list_ids: No arguments required.");
254 rc
= gdb_list_thread_ids ();
257 if (rc
== GDB_RC_FAIL
)
258 return MI_CMD_CAUGHT_ERROR
;
264 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
269 /* Note that the test for a valid register must include checking the
270 REGISTER_NAME because NUM_REGS may be allocated for the union of
271 the register sets within a family of related processors. In this
272 case, some entries of REGISTER_NAME will change depending upon
273 the particular processor being debugged. */
275 numregs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
277 ui_out_list_begin (uiout
, "register-names");
279 if (argc
== 0) /* No args, just do all the regs */
285 if (REGISTER_NAME (regnum
) == NULL
286 || *(REGISTER_NAME (regnum
)) == '\0')
287 ui_out_field_string (uiout
, NULL
, "");
289 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
293 /* Else, list of register #s, just do listed regs */
294 for (i
= 0; i
< argc
; i
++)
296 regnum
= atoi (argv
[i
]);
297 if (regnum
< 0 || regnum
>= numregs
)
299 xasprintf (&mi_error_message
, "bad register number");
302 if (REGISTER_NAME (regnum
) == NULL
303 || *(REGISTER_NAME (regnum
)) == '\0')
304 ui_out_field_string (uiout
, NULL
, "");
306 ui_out_field_string (uiout
, NULL
, REGISTER_NAME (regnum
));
308 ui_out_list_end (uiout
);
313 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
315 int regnum
, numregs
, changed
;
318 /* Note that the test for a valid register must include checking the
319 REGISTER_NAME because NUM_REGS may be allocated for the union of
320 the register sets within a family of related processors. In this
321 case, some entries of REGISTER_NAME will change depending upon
322 the particular processor being debugged. */
326 ui_out_list_begin (uiout
, "changed-registers");
328 if (argc
== 0) /* No args, just do all the regs */
334 if (REGISTER_NAME (regnum
) == NULL
335 || *(REGISTER_NAME (regnum
)) == '\0')
337 changed
= register_changed_p (regnum
);
340 xasprintf (&mi_error_message
,
341 "mi_cmd_data_list_changed_registers: Unable to read register contents.");
345 ui_out_field_int (uiout
, NULL
, regnum
);
349 /* Else, list of register #s, just do listed regs */
350 for (i
= 0; i
< argc
; i
++)
352 regnum
= atoi (argv
[i
]);
356 && REGISTER_NAME (regnum
) != NULL
357 && *REGISTER_NAME (regnum
) != '\000')
359 changed
= register_changed_p (regnum
);
362 xasprintf (&mi_error_message
,
363 "mi_cmd_data_list_register_change: Unable to read register contents.");
367 ui_out_field_int (uiout
, NULL
, regnum
);
371 xasprintf (&mi_error_message
, "bad register number");
375 ui_out_list_end (uiout
);
380 register_changed_p (int regnum
)
382 char *raw_buffer
= alloca (MAX_REGISTER_RAW_SIZE
);
384 if (read_relative_register_raw_bytes (regnum
, raw_buffer
))
387 if (memcmp (&old_regs
[REGISTER_BYTE (regnum
)], raw_buffer
,
388 REGISTER_RAW_SIZE (regnum
)) == 0)
391 /* Found a changed register. Return 1. */
393 memcpy (&old_regs
[REGISTER_BYTE (regnum
)], raw_buffer
,
394 REGISTER_RAW_SIZE (regnum
));
399 /* Return a list of register number and value pairs. The valid
400 arguments expected are: a letter indicating the format in which to
401 display the registers contents. This can be one of: x (hexadecimal), d
402 (decimal), N (natural), t (binary), o (octal), r (raw). After the
403 format argumetn there can be a sequence of numbers, indicating which
404 registers to fetch the content of. If the format is the only argument,
405 a list of all the registers with their values is returned. */
407 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
409 int regnum
, numregs
, format
, result
;
412 /* Note that the test for a valid register must include checking the
413 REGISTER_NAME because NUM_REGS may be allocated for the union of
414 the register sets within a family of related processors. In this
415 case, some entries of REGISTER_NAME will change depending upon
416 the particular processor being debugged. */
422 xasprintf (&mi_error_message
,
423 "mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
427 format
= (int) argv
[0][0];
429 if (!target_has_registers
)
431 xasprintf (&mi_error_message
,
432 "mi_cmd_data_list_register_values: No registers.");
436 ui_out_list_begin (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 ui_out_tuple_begin (uiout
, NULL
);
448 ui_out_field_int (uiout
, "number", regnum
);
449 result
= get_register (regnum
, format
);
452 ui_out_tuple_end (uiout
);
456 /* Else, list of register #s, just do listed regs */
457 for (i
= 1; i
< argc
; i
++)
459 regnum
= atoi (argv
[i
]);
463 && REGISTER_NAME (regnum
) != NULL
464 && *REGISTER_NAME (regnum
) != '\000')
466 ui_out_tuple_begin (uiout
, NULL
);
467 ui_out_field_int (uiout
, "number", regnum
);
468 result
= get_register (regnum
, format
);
471 ui_out_tuple_end (uiout
);
475 xasprintf (&mi_error_message
, "bad register number");
479 ui_out_list_end (uiout
);
483 /* Output one register's contents in the desired format. */
485 get_register (int regnum
, int format
)
487 char *raw_buffer
= alloca (MAX_REGISTER_RAW_SIZE
);
488 char *virtual_buffer
= alloca (MAX_REGISTER_VIRTUAL_SIZE
);
490 static struct ui_stream
*stb
= NULL
;
492 stb
= ui_out_stream_new (uiout
);
497 /* read_relative_register_raw_bytes returns a virtual frame pointer
498 (FRAME_FP (selected_frame)) if regnum == FP_REGNUM instead
499 of the real contents of the register. To get around this,
500 use get_saved_register instead. */
501 get_saved_register (raw_buffer
, &optim
, (CORE_ADDR
*) NULL
, selected_frame
,
502 regnum
, (enum lval_type
*) NULL
);
505 xasprintf (&mi_error_message
, "Optimized out");
509 /* Convert raw data to virtual format if necessary. */
511 if (REGISTER_CONVERTIBLE (regnum
))
513 REGISTER_CONVERT_TO_VIRTUAL (regnum
, REGISTER_VIRTUAL_TYPE (regnum
),
514 raw_buffer
, virtual_buffer
);
517 memcpy (virtual_buffer
, raw_buffer
, REGISTER_VIRTUAL_SIZE (regnum
));
522 char *ptr
, buf
[1024];
526 for (j
= 0; j
< REGISTER_RAW_SIZE (regnum
); j
++)
528 register int idx
= TARGET_BYTE_ORDER
== BIG_ENDIAN
? j
529 : REGISTER_RAW_SIZE (regnum
) - 1 - j
;
530 sprintf (ptr
, "%02x", (unsigned char) raw_buffer
[idx
]);
533 ui_out_field_string (uiout
, "value", buf
);
534 /*fputs_filtered (buf, gdb_stdout); */
538 val_print (REGISTER_VIRTUAL_TYPE (regnum
), virtual_buffer
, 0, 0,
539 stb
->stream
, format
, 1, 0, Val_pretty_default
);
540 ui_out_field_stream (uiout
, "value", stb
);
541 ui_out_stream_delete (stb
);
546 /* Write given values into registers. The registers and values are
547 given as pairs. The corresponding MI command is
548 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
550 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
558 /* Note that the test for a valid register must include checking the
559 REGISTER_NAME because NUM_REGS may be allocated for the union of
560 the register sets within a family of related processors. In this
561 case, some entries of REGISTER_NAME will change depending upon
562 the particular processor being debugged. */
568 xasprintf (&mi_error_message
,
569 "mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
573 format
= (int) argv
[0][0];
575 if (!target_has_registers
)
577 xasprintf (&mi_error_message
,
578 "mi_cmd_data_write_register_values: No registers.");
584 xasprintf (&mi_error_message
,
585 "mi_cmd_data_write_register_values: No regs and values specified.");
591 xasprintf (&mi_error_message
,
592 "mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
596 for (i
= 1; i
< argc
; i
= i
+ 2)
598 regnum
= atoi (argv
[i
]);
602 && REGISTER_NAME (regnum
) != NULL
603 && *REGISTER_NAME (regnum
) != '\000')
606 struct cleanup
*old_chain
;
608 /* Get the value as a number */
609 value
= parse_and_eval_address (argv
[i
+ 1]);
610 /* Get the value into an array */
611 buffer
= xmalloc (REGISTER_SIZE
);
612 old_chain
= make_cleanup (xfree
, buffer
);
613 store_signed_integer (buffer
, REGISTER_SIZE
, value
);
615 write_register_bytes (REGISTER_BYTE (regnum
), buffer
, REGISTER_RAW_SIZE (regnum
));
616 /* Free the buffer. */
617 do_cleanups (old_chain
);
621 xasprintf (&mi_error_message
, "bad register number");
629 /*This is commented out because we decided it was not useful. I leave
630 it, just in case. ezannoni:1999-12-08 */
632 /* Assign a value to a variable. The expression argument must be in
633 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
636 mi_cmd_data_assign (char *command
, char **argv
, int argc
)
638 struct expression
*expr
;
639 struct cleanup
*old_chain
;
643 xasprintf (&mi_error_message
,
644 "mi_cmd_data_assign: Usage: -data-assign expression");
648 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
649 01-12-1999: Need to decide what to do with this for libgdb purposes. */
651 expr
= parse_expression (argv
[0]);
652 old_chain
= make_cleanup (free_current_contents
, &expr
);
653 evaluate_expression (expr
);
654 do_cleanups (old_chain
);
659 /* Evaluate the value of the argument. The argument is an
660 expression. If the expression contains spaces it needs to be
661 included in double quotes. */
663 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
665 struct expression
*expr
;
666 struct cleanup
*old_chain
= NULL
;
668 struct ui_stream
*stb
= NULL
;
670 stb
= ui_out_stream_new (uiout
);
674 xasprintf (&mi_error_message
,
675 "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
679 expr
= parse_expression (argv
[0]);
681 old_chain
= make_cleanup (free_current_contents
, &expr
);
683 val
= evaluate_expression (expr
);
685 /* Print the result of the expression evaluation. */
686 val_print (VALUE_TYPE (val
), VALUE_CONTENTS (val
),
687 VALUE_EMBEDDED_OFFSET (val
), VALUE_ADDRESS (val
),
688 stb
->stream
, 0, 0, 0, 0);
690 ui_out_field_stream (uiout
, "value", stb
);
691 ui_out_stream_delete (stb
);
693 do_cleanups (old_chain
);
699 mi_cmd_target_download (char *args
, int from_tty
)
702 struct cleanup
*old_cleanups
= NULL
;
704 xasprintf (&run
, "load %s", args
);
705 old_cleanups
= make_cleanup (xfree
, run
);
706 execute_command (run
, from_tty
);
708 do_cleanups (old_cleanups
);
712 /* Connect to the remote target. */
714 mi_cmd_target_select (char *args
, int from_tty
)
717 struct cleanup
*old_cleanups
= NULL
;
719 xasprintf (&run
, "target %s", args
);
720 old_cleanups
= make_cleanup (xfree
, run
);
722 /* target-select is always synchronous. once the call has returned
723 we know that we are connected. */
724 /* NOTE: At present all targets that are connected are also
725 (implicitly) talking to a halted target. In the future this may
727 execute_command (run
, from_tty
);
729 do_cleanups (old_cleanups
);
731 /* Issue the completion message here. */
732 if (last_async_command
)
733 fputs_unfiltered (last_async_command
, raw_stdout
);
734 fputs_unfiltered ("^connected", raw_stdout
);
735 mi_out_put (uiout
, raw_stdout
);
736 mi_out_rewind (uiout
);
737 fputs_unfiltered ("\n", raw_stdout
);
738 do_exec_cleanups (ALL_CLEANUPS
);
744 ADDR: start address of data to be dumped.
745 WORD-FORMAT: a char indicating format for the ``word''. See
747 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
748 NR_ROW: Number of rows.
749 NR_COL: The number of colums (words per row).
750 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
751 ASCHAR for unprintable characters.
753 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
754 displayes them. Returns:
756 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
759 The number of bytes read is SIZE*ROW*COL. */
762 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
764 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
770 struct type
*word_type
;
783 static struct mi_opt opts
[] =
785 {"o", OFFSET_OPT
, 1},
791 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
795 switch ((enum opt
) opt
)
798 offset
= atol (optarg
);
805 if (argc
< 5 || argc
> 6)
807 xasprintf (&mi_error_message
,
808 "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
812 /* Extract all the arguments. */
814 /* Start address of the memory dump. */
815 addr
= parse_and_eval_address (argv
[0]) + offset
;
816 /* The format character to use when displaying a memory word. See
817 the ``x'' command. */
818 word_format
= argv
[1][0];
819 /* The size of the memory word. */
820 word_size
= atol (argv
[2]);
824 word_type
= builtin_type_int8
;
828 word_type
= builtin_type_int16
;
832 word_type
= builtin_type_int32
;
836 word_type
= builtin_type_int64
;
840 word_type
= builtin_type_int8
;
843 /* The number of rows */
844 nr_rows
= atol (argv
[3]);
847 xasprintf (&mi_error_message
,
848 "mi_cmd_data_read_memory: invalid number of rows.");
851 /* number of bytes per row. */
852 nr_cols
= atol (argv
[4]);
855 xasprintf (&mi_error_message
,
856 "mi_cmd_data_read_memory: invalid number of columns.");
858 /* The un-printable character when printing ascii. */
864 /* create a buffer and read it in. */
865 total_bytes
= word_size
* nr_rows
* nr_cols
;
866 mbuf
= xcalloc (total_bytes
, 1);
867 make_cleanup (xfree
, mbuf
);
870 xasprintf (&mi_error_message
,
871 "mi_cmd_data_read_memory: out of memory.");
875 while (nr_bytes
< total_bytes
)
878 long num
= target_read_memory_partial (addr
+ nr_bytes
, mbuf
+ nr_bytes
,
879 total_bytes
- nr_bytes
,
886 /* output the header information. */
887 ui_out_field_core_addr (uiout
, "addr", addr
);
888 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
889 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
890 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
891 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
892 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
893 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
895 /* Build the result as a two dimentional table. */
897 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
900 ui_out_list_begin (uiout
, "memory");
901 for (row
= 0, row_byte
= 0;
903 row
++, row_byte
+= nr_cols
* word_size
)
907 ui_out_tuple_begin (uiout
, NULL
);
908 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
909 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
910 ui_out_list_begin (uiout
, "data");
911 for (col
= 0, col_byte
= row_byte
;
913 col
++, col_byte
+= word_size
)
915 if (col_byte
+ word_size
> nr_bytes
)
917 ui_out_field_string (uiout
, NULL
, "N/A");
921 ui_file_rewind (stream
->stream
);
922 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
923 word_asize
, stream
->stream
);
924 ui_out_field_stream (uiout
, NULL
, stream
);
927 ui_out_list_end (uiout
);
931 ui_file_rewind (stream
->stream
);
932 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
934 if (byte
>= nr_bytes
)
936 fputc_unfiltered ('X', stream
->stream
);
938 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
940 fputc_unfiltered (aschar
, stream
->stream
);
943 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
945 ui_out_field_stream (uiout
, "ascii", stream
);
947 ui_out_tuple_end (uiout
);
949 ui_out_stream_delete (stream
);
950 ui_out_list_end (uiout
);
952 do_cleanups (cleanups
);
956 /* DATA-MEMORY-WRITE:
958 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
959 offset from the beginning of the memory grid row where the cell to
961 ADDR: start address of the row in the memory grid where the memory
962 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
963 the location to write to.
964 FORMAT: a char indicating format for the ``word''. See
966 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
967 VALUE: value to be written into the memory address.
969 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
973 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
978 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
979 enough when using a compiler other than GCC. */
982 struct cleanup
*old_chain
;
990 static struct mi_opt opts
[] =
992 {"o", OFFSET_OPT
, 1},
998 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
1002 switch ((enum opt
) opt
)
1005 offset
= atol (optarg
);
1014 xasprintf (&mi_error_message
,
1015 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1016 return MI_CMD_ERROR
;
1019 /* Extract all the arguments. */
1020 /* Start address of the memory dump. */
1021 addr
= parse_and_eval_address (argv
[0]);
1022 /* The format character to use when displaying a memory word. See
1023 the ``x'' command. */
1024 word_format
= argv
[1][0];
1025 /* The size of the memory word. */
1026 word_size
= atol (argv
[2]);
1028 /* Calculate the real address of the write destination. */
1029 addr
+= (offset
* word_size
);
1031 /* Get the value as a number */
1032 value
= parse_and_eval_address (argv
[3]);
1033 /* Get the value into an array */
1034 buffer
= xmalloc (word_size
);
1035 old_chain
= make_cleanup (xfree
, buffer
);
1036 store_signed_integer (buffer
, word_size
, value
);
1037 /* Write it down to memory */
1038 write_memory (addr
, buffer
, word_size
);
1039 /* Free the buffer. */
1040 do_cleanups (old_chain
);
1045 /* Execute a command within a safe environment. Return >0 for
1046 ok. Return <0 for supress prompt. Return 0 to have the error
1047 extracted from error_last_message(). */
1050 captured_mi_execute_command (void *data
)
1052 struct mi_parse
*context
= data
;
1053 enum mi_cmd_result rc
;
1055 switch (context
->op
)
1059 /* A MI command was read from the input stream */
1061 /* FIXME: gdb_???? */
1062 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1063 context
->token
, context
->command
, context
->args
);
1064 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1065 condition expression, each function should return an
1066 indication of what action is required and then switch on
1068 rc
= mi_cmd_execute (context
);
1069 if (!target_can_async_p () || !target_executing
)
1071 /* print the result if there were no errors */
1072 if (rc
== MI_CMD_DONE
)
1074 fputs_unfiltered (context
->token
, raw_stdout
);
1075 fputs_unfiltered ("^done", raw_stdout
);
1076 mi_out_put (uiout
, raw_stdout
);
1077 mi_out_rewind (uiout
);
1078 fputs_unfiltered ("\n", raw_stdout
);
1080 else if (rc
== MI_CMD_ERROR
)
1082 if (mi_error_message
)
1084 fputs_unfiltered (context
->token
, raw_stdout
);
1085 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1086 fputstr_unfiltered (mi_error_message
, '"', raw_stdout
);
1087 xfree (mi_error_message
);
1088 fputs_unfiltered ("\"\n", raw_stdout
);
1090 mi_out_rewind (uiout
);
1092 else if (rc
== MI_CMD_CAUGHT_ERROR
)
1094 mi_out_rewind (uiout
);
1098 mi_out_rewind (uiout
);
1100 else if (sync_execution
)
1101 /* Don't print the prompt. We are executing the target in
1102 synchronous mode. */
1107 /* A CLI command was read from the input stream */
1108 /* This will be removed as soon as we have a complete set of
1110 /* echo the command on the console. */
1111 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1112 /* FIXME: If the command string has something that looks like
1113 a format spec (e.g. %s) we will get a core dump */
1114 mi_execute_cli_command ("%s", context
->command
);
1115 /* print the result */
1116 /* FIXME: Check for errors here. */
1117 fputs_unfiltered (context
->token
, raw_stdout
);
1118 fputs_unfiltered ("^done", raw_stdout
);
1119 mi_out_put (uiout
, raw_stdout
);
1120 mi_out_rewind (uiout
);
1121 fputs_unfiltered ("\n", raw_stdout
);
1130 mi_execute_command (char *cmd
, int from_tty
)
1132 struct mi_parse
*command
;
1134 /* This is to handle EOF (^D). We just quit gdb. */
1135 /* FIXME: we should call some API function here. */
1137 quit_force (NULL
, from_tty
);
1139 command
= mi_parse (cmd
);
1141 if (command
!= NULL
)
1143 /* FIXME: cagney/1999-11-04: Can this use of catch_errors either
1144 be pushed even further down or even eliminated? */
1145 int rc
= catch_errors (captured_mi_execute_command
, command
, "",
1149 /* The command is executing synchronously. Bail out early
1150 suppressing the finished prompt. */
1151 mi_parse_free (command
);
1156 char *msg
= error_last_message ();
1157 struct cleanup
*cleanup
= make_cleanup (xfree
, msg
);
1158 /* The command execution failed and error() was called
1160 fputs_unfiltered (command
->token
, raw_stdout
);
1161 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1162 fputstr_unfiltered (msg
, '"', raw_stdout
);
1163 fputs_unfiltered ("\"\n", raw_stdout
);
1165 mi_parse_free (command
);
1168 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1169 gdb_flush (raw_stdout
);
1170 /* print any buffered hook code */
1174 static enum mi_cmd_result
1175 mi_cmd_execute (struct mi_parse
*parse
)
1177 if (parse
->cmd
->argv_func
!= NULL
1178 || parse
->cmd
->args_func
!= NULL
)
1180 /* FIXME: We need to save the token because the command executed
1181 may be asynchronous and need to print the token again.
1182 In the future we can pass the token down to the func
1183 and get rid of the last_async_command */
1184 /* The problem here is to keep the token around when we launch
1185 the target, and we want to interrupt it later on. The
1186 interrupt command will have its own token, but when the
1187 target stops, we must display the token corresponding to the
1188 last execution command given. So we have another string where
1189 we copy the token (previous_async_command), if this was
1190 indeed the token of an execution command, and when we stop we
1191 print that one. This is possible because the interrupt
1192 command, when over, will copy that token back into the
1193 default token string (last_async_command). */
1195 if (target_executing
)
1197 if (!previous_async_command
)
1198 previous_async_command
= xstrdup (last_async_command
);
1199 if (strcmp (parse
->command
, "exec-interrupt"))
1201 fputs_unfiltered (parse
->token
, raw_stdout
);
1202 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1203 fputs_unfiltered ("Cannot execute command ", raw_stdout
);
1204 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1205 fputs_unfiltered (" while target running", raw_stdout
);
1206 fputs_unfiltered ("\"\n", raw_stdout
);
1207 return MI_CMD_ERROR
;
1210 last_async_command
= xstrdup (parse
->token
);
1211 make_exec_cleanup (free_current_contents
, &last_async_command
);
1212 /* FIXME: DELETE THIS! */
1213 if (parse
->cmd
->args_func
!= NULL
)
1214 return parse
->cmd
->args_func (parse
->args
, 0 /*from_tty */ );
1215 return parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1217 else if (parse
->cmd
->cli
!= 0)
1219 /* FIXME: DELETE THIS. */
1220 /* The operation is still implemented by a cli command */
1221 /* Must be a synchronous one */
1222 mi_execute_cli_command (parse
->cmd
->cli
, parse
->args
);
1227 /* FIXME: DELETE THIS. */
1228 fputs_unfiltered (parse
->token
, raw_stdout
);
1229 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1230 fputs_unfiltered ("Undefined mi command: ", raw_stdout
);
1231 fputstr_unfiltered (parse
->command
, '"', raw_stdout
);
1232 fputs_unfiltered (" (missing implementation)", raw_stdout
);
1233 fputs_unfiltered ("\"\n", raw_stdout
);
1234 return MI_CMD_ERROR
;
1239 mi_execute_command_wrapper (char *cmd
)
1241 mi_execute_command (cmd
, stdin
== instream
);
1244 /* FIXME: This is just a hack so we can get some extra commands going.
1245 We don't want to channel things through the CLI, but call libgdb directly */
1246 /* Use only for synchronous commands */
1249 mi_execute_cli_command (const char *cli
, char *args
)
1253 struct cleanup
*old_cleanups
;
1255 xasprintf (&run
, cli
, args
);
1257 /* FIXME: gdb_???? */
1258 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1260 old_cleanups
= make_cleanup (xfree
, run
);
1261 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1262 do_cleanups (old_cleanups
);
1268 mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
)
1270 struct cleanup
*old_cleanups
;
1274 if (target_can_async_p ())
1276 async_args
= (char *) xmalloc (strlen (args
) + 2);
1277 make_exec_cleanup (free
, async_args
);
1278 strcpy (async_args
, args
);
1279 strcat (async_args
, "&");
1280 xasprintf (&run
, "%s %s", mi
, async_args
);
1281 make_exec_cleanup (free
, run
);
1282 add_continuation (mi_exec_async_cli_cmd_continuation
, NULL
);
1283 old_cleanups
= NULL
;
1287 xasprintf (&run
, "%s %s", mi
, args
);
1288 old_cleanups
= make_cleanup (xfree
, run
);
1291 if (!target_can_async_p ())
1293 /* NOTE: For synchronous targets asynchronous behavour is faked by
1294 printing out the GDB prompt before we even try to execute the
1296 if (last_async_command
)
1297 fputs_unfiltered (last_async_command
, raw_stdout
);
1298 fputs_unfiltered ("^running\n", raw_stdout
);
1299 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1300 gdb_flush (raw_stdout
);
1304 /* FIXME: cagney/1999-11-29: Printing this message before
1305 calling execute_command is wrong. It should only be printed
1306 once gdb has confirmed that it really has managed to send a
1307 run command to the target. */
1308 if (last_async_command
)
1309 fputs_unfiltered (last_async_command
, raw_stdout
);
1310 fputs_unfiltered ("^running\n", raw_stdout
);
1313 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1315 if (!target_can_async_p ())
1317 /* Do this before doing any printing. It would appear that some
1318 print code leaves garbage around in the buffer. */
1319 do_cleanups (old_cleanups
);
1320 /* If the target was doing the operation synchronously we fake
1321 the stopped message. */
1322 if (last_async_command
)
1323 fputs_unfiltered (last_async_command
, raw_stdout
);
1324 fputs_unfiltered ("*stopped", raw_stdout
);
1325 mi_out_put (uiout
, raw_stdout
);
1326 mi_out_rewind (uiout
);
1327 fputs_unfiltered ("\n", raw_stdout
);
1328 return MI_CMD_QUIET
;
1334 mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
)
1336 if (last_async_command
)
1337 fputs_unfiltered (last_async_command
, raw_stdout
);
1338 fputs_unfiltered ("*stopped", raw_stdout
);
1339 mi_out_put (uiout
, raw_stdout
);
1340 fputs_unfiltered ("\n", raw_stdout
);
1341 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1342 gdb_flush (raw_stdout
);
1343 do_exec_cleanups (ALL_CLEANUPS
);
1347 mi_input (char *buf
)
1349 return gdb_readline (NULL
);
1353 mi_load_progress (const char *section_name
,
1354 unsigned long sent_so_far
,
1355 unsigned long total_section
,
1356 unsigned long total_sent
,
1357 unsigned long grand_total
)
1359 struct timeval time_now
, delta
, update_threshold
;
1360 static struct timeval last_update
;
1361 static char *previous_sect_name
= NULL
;
1364 if (!interpreter_p
|| strncmp (interpreter_p
, "mi", 2) != 0)
1367 update_threshold
.tv_sec
= 0;
1368 update_threshold
.tv_usec
= 500000;
1369 gettimeofday (&time_now
, NULL
);
1371 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1372 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1374 if (delta
.tv_usec
< 0)
1377 delta
.tv_usec
+= 1000000;
1380 new_section
= (previous_sect_name
?
1381 strcmp (previous_sect_name
, section_name
) : 1);
1384 xfree (previous_sect_name
);
1385 previous_sect_name
= xstrdup (section_name
);
1387 if (last_async_command
)
1388 fputs_unfiltered (last_async_command
, raw_stdout
);
1389 fputs_unfiltered ("+download", raw_stdout
);
1390 ui_out_tuple_begin (uiout
, NULL
);
1391 ui_out_field_string (uiout
, "section", section_name
);
1392 ui_out_field_int (uiout
, "section-size", total_section
);
1393 ui_out_field_int (uiout
, "total-size", grand_total
);
1394 ui_out_tuple_end (uiout
);
1395 mi_out_put (uiout
, raw_stdout
);
1396 fputs_unfiltered ("\n", raw_stdout
);
1397 gdb_flush (raw_stdout
);
1400 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1401 delta
.tv_usec
>= update_threshold
.tv_usec
)
1403 last_update
.tv_sec
= time_now
.tv_sec
;
1404 last_update
.tv_usec
= time_now
.tv_usec
;
1405 if (last_async_command
)
1406 fputs_unfiltered (last_async_command
, raw_stdout
);
1407 fputs_unfiltered ("+download", raw_stdout
);
1408 ui_out_tuple_begin (uiout
, NULL
);
1409 ui_out_field_string (uiout
, "section", section_name
);
1410 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1411 ui_out_field_int (uiout
, "section-size", total_section
);
1412 ui_out_field_int (uiout
, "total-sent", total_sent
);
1413 ui_out_field_int (uiout
, "total-size", grand_total
);
1414 ui_out_tuple_end (uiout
);
1415 mi_out_put (uiout
, raw_stdout
);
1416 fputs_unfiltered ("\n", raw_stdout
);
1417 gdb_flush (raw_stdout
);
1422 mi_command_loop (int mi_version
)
1424 /* HACK: Force stdout/stderr to point at the console. This avoids
1425 any potential side effects caused by legacy code that is still
1426 using the TUI / fputs_unfiltered_hook */
1427 raw_stdout
= stdio_fileopen (stdout
);
1428 /* Route normal output through the MIx */
1429 gdb_stdout
= mi_console_file_new (raw_stdout
, "~");
1430 /* Route error and log output through the MI */
1431 gdb_stderr
= mi_console_file_new (raw_stdout
, "&");
1432 gdb_stdlog
= gdb_stderr
;
1433 /* Route target output through the MI. */
1434 gdb_stdtarg
= mi_console_file_new (raw_stdout
, "@");
1436 /* HACK: Poke the ui_out table directly. Should we be creating a
1437 mi_out object wired up to the above gdb_stdout / gdb_stderr? */
1438 uiout
= mi_out_new (mi_version
);
1440 /* HACK: Override any other interpreter hooks. We need to create a
1441 real event table and pass in that. */
1443 /* command_loop_hook = 0; */
1444 print_frame_info_listing_hook
= 0;
1447 create_breakpoint_hook
= 0;
1448 delete_breakpoint_hook
= 0;
1449 modify_breakpoint_hook
= 0;
1450 interactive_hook
= 0;
1451 registers_changed_hook
= 0;
1452 readline_begin_hook
= 0;
1454 readline_end_hook
= 0;
1455 register_changed_hook
= 0;
1456 memory_changed_hook
= 0;
1458 target_wait_hook
= 0;
1459 call_command_hook
= 0;
1461 error_begin_hook
= 0;
1462 show_load_progress
= mi_load_progress
;
1464 /* Turn off 8 bit strings in quoted output. Any character with the
1465 high bit set is printed using C's octal format. */
1466 sevenbit_strings
= 1;
1468 /* Tell the world that we're alive */
1469 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1470 gdb_flush (raw_stdout
);
1473 simplified_command_loop (mi_input
, mi_execute_command
);
1475 start_event_loop ();
1479 mi0_command_loop (void)
1481 mi_command_loop (0);
1485 mi1_command_loop (void)
1487 mi_command_loop (1);
1491 setup_architecture_data (void)
1493 /* don't trust REGISTER_BYTES to be zero. */
1494 old_regs
= xmalloc (REGISTER_BYTES
+ 1);
1495 memset (old_regs
, 0, REGISTER_BYTES
+ 1);
1499 mi_init_ui (char *arg0
)
1501 /* Eventually this will contain code that takes control of the
1506 _initialize_mi_main (void)
1508 if (interpreter_p
== NULL
)
1511 /* If we're _the_ interpreter, take control. */
1512 if (strcmp (interpreter_p
, "mi0") == 0)
1513 command_loop_hook
= mi0_command_loop
;
1514 else if (strcmp (interpreter_p
, "mi") == 0
1515 || strcmp (interpreter_p
, "mi1") == 0)
1516 command_loop_hook
= mi1_command_loop
;
1520 init_ui_hook
= mi_init_ui
;
1521 setup_architecture_data ();
1522 register_gdbarch_swap (&old_regs
, sizeof (old_regs
), NULL
);
1523 register_gdbarch_swap (NULL
, 0, setup_architecture_data
);
1526 /* These overwrite some of the initialization done in
1527 _intialize_event_loop. */
1528 call_readline
= gdb_readline2
;
1529 input_handler
= mi_execute_command_wrapper
;
1530 add_file_handler (input_fd
, stdin_event_handler
, 0);
1531 async_command_editing_p
= 0;
1533 /* FIXME: Should we notify main that we are here as a possible