3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007
4 Free Software Foundation, Inc.
6 Contributed by Cygnus Solutions (a Red Hat company).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* Work in progress. */
28 #include "gdb_string.h"
29 #include "exceptions.h"
31 #include "gdbthread.h"
34 #include "mi-getopt.h"
35 #include "mi-console.h"
39 #include "event-loop.h"
40 #include "event-top.h"
41 #include "gdbcore.h" /* For write_memory(). */
51 #if defined HAVE_SYS_RESOURCE_H
52 #include <sys/resource.h>
64 /* Enumerations of the actions that may result from calling
65 captured_mi_execute_command. */
67 enum captured_mi_execute_command_actions
69 EXECUTE_COMMAND_DISPLAY_PROMPT,
70 EXECUTE_COMMAND_SUPRESS_PROMPT
73 /* This structure is used to pass information from captured_mi_execute_command
74 to mi_execute_command. */
75 struct captured_mi_execute_command_args
77 /* This return result of the MI command (output). */
78 enum mi_cmd_result rc;
80 /* What action to perform when the call is finished (output). */
81 enum captured_mi_execute_command_actions action;
83 /* The command context to be executed (input). */
84 struct mi_parse *command;
88 struct ui_file *raw_stdout;
90 /* This is used to pass the current command timestamp
91 down to continuation routines. */
92 static struct mi_timestamp *current_command_ts;
94 static int do_timings = 0;
96 /* The token of the last asynchronous command. */
97 static char *last_async_command;
98 static char *previous_async_command;
99 char *mi_error_message;
101 extern void _initialize_mi_main (void);
102 static enum mi_cmd_result mi_cmd_execute (struct mi_parse *parse);
104 static void mi_execute_cli_command (const char *cmd, int args_p,
106 static enum mi_cmd_result mi_execute_async_cli_command (char *mi, char *args, int from_tty);
108 static void mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg);
110 static int register_changed_p (int regnum, struct regcache *,
112 static int get_register (int regnum, int format);
114 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
115 layer that calls libgdb. Any operation used in the below should be
118 static void timestamp (struct mi_timestamp *tv);
120 static void print_diff_now (struct mi_timestamp *start);
121 static void print_diff (struct mi_timestamp *start, struct mi_timestamp *end);
124 mi_cmd_gdb_exit (char *command, char **argv, int argc)
126 /* We have to print everything right here because we never return. */
127 if (last_async_command)
128 fputs_unfiltered (last_async_command, raw_stdout);
129 fputs_unfiltered ("^exit\n", raw_stdout);
130 mi_out_put (uiout, raw_stdout);
131 /* FIXME: The function called is not yet a formal libgdb function. */
132 quit_force (NULL, FROM_TTY);
137 mi_cmd_exec_run (char *args, int from_tty)
139 /* FIXME: Should call a libgdb function, not a cli wrapper. */
140 return mi_execute_async_cli_command ("run", args, from_tty);
144 mi_cmd_exec_next (char *args, int from_tty)
146 /* FIXME: Should call a libgdb function, not a cli wrapper. */
147 return mi_execute_async_cli_command ("next", args, from_tty);
151 mi_cmd_exec_next_instruction (char *args, int from_tty)
153 /* FIXME: Should call a libgdb function, not a cli wrapper. */
154 return mi_execute_async_cli_command ("nexti", args, from_tty);
158 mi_cmd_exec_step (char *args, int from_tty)
160 /* FIXME: Should call a libgdb function, not a cli wrapper. */
161 return mi_execute_async_cli_command ("step", args, from_tty);
165 mi_cmd_exec_step_instruction (char *args, int from_tty)
167 /* FIXME: Should call a libgdb function, not a cli wrapper. */
168 return mi_execute_async_cli_command ("stepi", args, from_tty);
172 mi_cmd_exec_finish (char *args, int from_tty)
174 /* FIXME: Should call a libgdb function, not a cli wrapper. */
175 return mi_execute_async_cli_command ("finish", args, from_tty);
179 mi_cmd_exec_until (char *args, int from_tty)
181 /* FIXME: Should call a libgdb function, not a cli wrapper. */
182 return mi_execute_async_cli_command ("until", args, from_tty);
186 mi_cmd_exec_return (char *args, int from_tty)
188 /* This command doesn't really execute the target, it just pops the
189 specified number of frames. */
191 /* Call return_command with from_tty argument equal to 0 so as to
192 avoid being queried. */
193 return_command (args, 0);
195 /* Call return_command with from_tty argument equal to 0 so as to
196 avoid being queried. */
197 return_command (NULL, 0);
199 /* Because we have called return_command with from_tty = 0, we need
200 to print the frame here. */
201 print_stack_frame (get_selected_frame (NULL), 1, LOC_AND_ADDRESS);
207 mi_cmd_exec_continue (char *args, int from_tty)
209 /* FIXME: Should call a libgdb function, not a cli wrapper. */
210 return mi_execute_async_cli_command ("continue", args, from_tty);
213 /* Interrupt the execution of the target. Note how we must play around
214 with the token variables, in order to display the current token in
215 the result of the interrupt command, and the previous execution
216 token when the target finally stops. See comments in
219 mi_cmd_exec_interrupt (char *args, int from_tty)
221 if (!target_executing)
223 mi_error_message = xstrprintf ("mi_cmd_exec_interrupt: Inferior not executing.");
226 interrupt_target_command (args, from_tty);
227 if (last_async_command)
228 fputs_unfiltered (last_async_command, raw_stdout);
229 fputs_unfiltered ("^done", raw_stdout);
230 xfree (last_async_command);
231 if (previous_async_command)
232 last_async_command = xstrdup (previous_async_command);
233 xfree (previous_async_command);
234 previous_async_command = NULL;
235 mi_out_put (uiout, raw_stdout);
236 mi_out_rewind (uiout);
237 fputs_unfiltered ("\n", raw_stdout);
242 mi_cmd_thread_select (char *command, char **argv, int argc)
248 mi_error_message = xstrprintf ("mi_cmd_thread_select: USAGE: threadnum.");
252 rc = gdb_thread_select (uiout, argv[0], &mi_error_message);
254 if (rc == GDB_RC_FAIL)
261 mi_cmd_thread_list_ids (char *command, char **argv, int argc)
267 mi_error_message = xstrprintf ("mi_cmd_thread_list_ids: No arguments required.");
271 rc = gdb_list_thread_ids (uiout, &mi_error_message);
273 if (rc == GDB_RC_FAIL)
280 mi_cmd_data_list_register_names (char *command, char **argv, int argc)
284 struct cleanup *cleanup;
286 /* Note that the test for a valid register must include checking the
287 gdbarch_register_name because gdbarch_num_regs may be allocated for
288 the union of the register sets within a family of related processors.
289 In this case, some entries of gdbarch_register_name will change depending
290 upon the particular processor being debugged. */
292 numregs = gdbarch_num_regs (current_gdbarch)
293 + gdbarch_num_pseudo_regs (current_gdbarch);
295 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names");
297 if (argc == 0) /* No args, just do all the regs. */
303 if (gdbarch_register_name (current_gdbarch, regnum) == NULL
304 || *(gdbarch_register_name (current_gdbarch, regnum)) == '\0')
305 ui_out_field_string (uiout, NULL, "");
307 ui_out_field_string (uiout, NULL,
308 gdbarch_register_name
309 (current_gdbarch, regnum));
313 /* Else, list of register #s, just do listed regs. */
314 for (i = 0; i < argc; i++)
316 regnum = atoi (argv[i]);
317 if (regnum < 0 || regnum >= numregs)
319 do_cleanups (cleanup);
320 mi_error_message = xstrprintf ("bad register number");
323 if (gdbarch_register_name (current_gdbarch, regnum) == NULL
324 || *(gdbarch_register_name (current_gdbarch, regnum)) == '\0')
325 ui_out_field_string (uiout, NULL, "");
327 ui_out_field_string (uiout, NULL,
328 gdbarch_register_name (current_gdbarch, regnum));
330 do_cleanups (cleanup);
335 mi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
337 static struct regcache *this_regs = NULL;
338 struct regcache *prev_regs;
339 int regnum, numregs, changed;
341 struct cleanup *cleanup;
343 /* The last time we visited this function, the current frame's register
344 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
345 and refresh THIS_REGS with the now-current register contents. */
347 prev_regs = this_regs;
348 this_regs = frame_save_as_regcache (get_selected_frame (NULL));
349 cleanup = make_cleanup_regcache_xfree (prev_regs);
351 /* Note that the test for a valid register must include checking the
352 gdbarch_register_name because gdbarch_num_regs may be allocated for
353 the union of the register sets within a family of related processors.
354 In this case, some entries of gdbarch_register_name will change depending
355 upon the particular processor being debugged. */
357 numregs = gdbarch_num_regs (current_gdbarch)
358 + gdbarch_num_pseudo_regs (current_gdbarch);
360 make_cleanup_ui_out_list_begin_end (uiout, "changed-registers");
362 if (argc == 0) /* No args, just do all the regs. */
368 if (gdbarch_register_name (current_gdbarch, regnum) == NULL
369 || *(gdbarch_register_name (current_gdbarch, regnum)) == '\0')
371 changed = register_changed_p (regnum, prev_regs, this_regs);
374 do_cleanups (cleanup);
375 mi_error_message = xstrprintf ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
379 ui_out_field_int (uiout, NULL, regnum);
383 /* Else, list of register #s, just do listed regs. */
384 for (i = 0; i < argc; i++)
386 regnum = atoi (argv[i]);
390 && gdbarch_register_name (current_gdbarch, regnum) != NULL
391 && *gdbarch_register_name (current_gdbarch, regnum) != '\000')
393 changed = register_changed_p (regnum, prev_regs, this_regs);
396 do_cleanups (cleanup);
397 mi_error_message = xstrprintf ("mi_cmd_data_list_register_change: Unable to read register contents.");
401 ui_out_field_int (uiout, NULL, regnum);
405 do_cleanups (cleanup);
406 mi_error_message = xstrprintf ("bad register number");
410 do_cleanups (cleanup);
415 register_changed_p (int regnum, struct regcache *prev_regs,
416 struct regcache *this_regs)
418 struct gdbarch *gdbarch = get_regcache_arch (this_regs);
419 gdb_byte prev_buffer[MAX_REGISTER_SIZE];
420 gdb_byte this_buffer[MAX_REGISTER_SIZE];
422 /* Registers not valid in this frame return count as unchanged. */
423 if (!regcache_valid_p (this_regs, regnum))
426 /* First time through or after gdbarch change consider all registers as
427 changed. Same for registers not valid in the previous frame. */
428 if (!prev_regs || get_regcache_arch (prev_regs) != gdbarch
429 || !regcache_valid_p (prev_regs, regnum))
432 /* Get register contents and compare. */
433 regcache_cooked_read (prev_regs, regnum, prev_buffer);
434 regcache_cooked_read (this_regs, regnum, this_buffer);
436 return memcmp (prev_buffer, this_buffer,
437 register_size (gdbarch, regnum)) != 0;
440 /* Return a list of register number and value pairs. The valid
441 arguments expected are: a letter indicating the format in which to
442 display the registers contents. This can be one of: x (hexadecimal), d
443 (decimal), N (natural), t (binary), o (octal), r (raw). After the
444 format argumetn there can be a sequence of numbers, indicating which
445 registers to fetch the content of. If the format is the only argument,
446 a list of all the registers with their values is returned. */
448 mi_cmd_data_list_register_values (char *command, char **argv, int argc)
450 int regnum, numregs, format, result;
452 struct cleanup *list_cleanup, *tuple_cleanup;
454 /* Note that the test for a valid register must include checking the
455 gdbarch_register_name because gdbarch_num_regs may be allocated for
456 the union of the register sets within a family of related processors.
457 In this case, some entries of gdbarch_register_name will change depending
458 upon the particular processor being debugged. */
460 numregs = gdbarch_num_regs (current_gdbarch)
461 + gdbarch_num_pseudo_regs (current_gdbarch);
465 mi_error_message = xstrprintf ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
469 format = (int) argv[0][0];
471 list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values");
473 if (argc == 1) /* No args, beside the format: do all the regs. */
479 if (gdbarch_register_name (current_gdbarch, regnum) == NULL
480 || *(gdbarch_register_name (current_gdbarch, regnum)) == '\0')
482 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
483 ui_out_field_int (uiout, "number", regnum);
484 result = get_register (regnum, format);
487 do_cleanups (list_cleanup);
490 do_cleanups (tuple_cleanup);
494 /* Else, list of register #s, just do listed regs. */
495 for (i = 1; i < argc; i++)
497 regnum = atoi (argv[i]);
501 && gdbarch_register_name (current_gdbarch, regnum) != NULL
502 && *gdbarch_register_name (current_gdbarch, regnum) != '\000')
504 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
505 ui_out_field_int (uiout, "number", regnum);
506 result = get_register (regnum, format);
509 do_cleanups (list_cleanup);
512 do_cleanups (tuple_cleanup);
516 do_cleanups (list_cleanup);
517 mi_error_message = xstrprintf ("bad register number");
521 do_cleanups (list_cleanup);
525 /* Output one register's contents in the desired format. */
527 get_register (int regnum, int format)
529 gdb_byte buffer[MAX_REGISTER_SIZE];
534 static struct ui_stream *stb = NULL;
536 stb = ui_out_stream_new (uiout);
541 frame_register (get_selected_frame (NULL), regnum, &optim, &lval, &addr,
546 mi_error_message = xstrprintf ("Optimized out");
553 char *ptr, buf[1024];
557 for (j = 0; j < register_size (current_gdbarch, regnum); j++)
559 int idx = gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG ? j
560 : register_size (current_gdbarch, regnum) - 1 - j;
561 sprintf (ptr, "%02x", (unsigned char) buffer[idx]);
564 ui_out_field_string (uiout, "value", buf);
565 /*fputs_filtered (buf, gdb_stdout); */
569 val_print (register_type (current_gdbarch, regnum), buffer, 0, 0,
570 stb->stream, format, 1, 0, Val_pretty_default);
571 ui_out_field_stream (uiout, "value", stb);
572 ui_out_stream_delete (stb);
577 /* Write given values into registers. The registers and values are
578 given as pairs. The corresponding MI command is
579 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
581 mi_cmd_data_write_register_values (char *command, char **argv, int argc)
586 /* Note that the test for a valid register must include checking the
587 gdbarch_register_name because gdbarch_num_regs may be allocated for
588 the union of the register sets within a family of related processors.
589 In this case, some entries of gdbarch_register_name will change depending
590 upon the particular processor being debugged. */
592 numregs = gdbarch_num_regs (current_gdbarch)
593 + gdbarch_num_pseudo_regs (current_gdbarch);
597 mi_error_message = xstrprintf ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
601 format = (int) argv[0][0];
603 if (!target_has_registers)
605 mi_error_message = xstrprintf ("mi_cmd_data_write_register_values: No registers.");
611 mi_error_message = xstrprintf ("mi_cmd_data_write_register_values: No regs and values specified.");
617 mi_error_message = xstrprintf ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
621 for (i = 1; i < argc; i = i + 2)
623 int regnum = atoi (argv[i]);
625 if (regnum >= 0 && regnum < numregs
626 && gdbarch_register_name (current_gdbarch, regnum)
627 && *gdbarch_register_name (current_gdbarch, regnum))
631 /* Get the value as a number. */
632 value = parse_and_eval_address (argv[i + 1]);
635 regcache_cooked_write_signed (get_current_regcache (), regnum, value);
639 mi_error_message = xstrprintf ("bad register number");
647 /* This is commented out because we decided it was not useful. I leave
648 it, just in case. ezannoni:1999-12-08 */
650 /* Assign a value to a variable. The expression argument must be in
651 the form A=2 or "A = 2" i.e. if there are spaces it needs to be
654 mi_cmd_data_assign (char *command, char **argv, int argc)
656 struct expression *expr;
657 struct cleanup *old_chain;
661 mi_error_message = xstrprintf ("mi_cmd_data_assign: Usage: -data-assign expression");
665 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
666 01-12-1999: Need to decide what to do with this for libgdb purposes. */
668 expr = parse_expression (argv[0]);
669 old_chain = make_cleanup (free_current_contents, &expr);
670 evaluate_expression (expr);
671 do_cleanups (old_chain);
676 /* Evaluate the value of the argument. The argument is an
677 expression. If the expression contains spaces it needs to be
678 included in double quotes. */
680 mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
682 struct expression *expr;
683 struct cleanup *old_chain = NULL;
685 struct ui_stream *stb = NULL;
687 stb = ui_out_stream_new (uiout);
691 mi_error_message = xstrprintf ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
692 ui_out_stream_delete (stb);
696 expr = parse_expression (argv[0]);
698 old_chain = make_cleanup (free_current_contents, &expr);
700 val = evaluate_expression (expr);
702 /* Print the result of the expression evaluation. */
703 val_print (value_type (val), value_contents (val),
704 value_embedded_offset (val), VALUE_ADDRESS (val),
705 stb->stream, 0, 0, 0, 0);
707 ui_out_field_stream (uiout, "value", stb);
708 ui_out_stream_delete (stb);
710 do_cleanups (old_chain);
716 mi_cmd_target_download (char *args, int from_tty)
719 struct cleanup *old_cleanups = NULL;
721 run = xstrprintf ("load %s", args);
722 old_cleanups = make_cleanup (xfree, run);
723 execute_command (run, from_tty);
725 do_cleanups (old_cleanups);
729 /* Connect to the remote target. */
731 mi_cmd_target_select (char *args, int from_tty)
734 struct cleanup *old_cleanups = NULL;
736 run = xstrprintf ("target %s", args);
737 old_cleanups = make_cleanup (xfree, run);
739 /* target-select is always synchronous. Once the call has returned
740 we know that we are connected. */
741 /* NOTE: At present all targets that are connected are also
742 (implicitly) talking to a halted target. In the future this may
744 execute_command (run, from_tty);
746 do_cleanups (old_cleanups);
748 /* Issue the completion message here. */
749 if (last_async_command)
750 fputs_unfiltered (last_async_command, raw_stdout);
751 fputs_unfiltered ("^connected", raw_stdout);
752 mi_out_put (uiout, raw_stdout);
753 mi_out_rewind (uiout);
754 fputs_unfiltered ("\n", raw_stdout);
755 do_exec_cleanups (ALL_CLEANUPS);
761 ADDR: start address of data to be dumped.
762 WORD-FORMAT: a char indicating format for the ``word''. See
764 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
765 NR_ROW: Number of rows.
766 NR_COL: The number of colums (words per row).
767 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
768 ASCHAR for unprintable characters.
770 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
771 displayes them. Returns:
773 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
776 The number of bytes read is SIZE*ROW*COL. */
779 mi_cmd_data_read_memory (char *command, char **argv, int argc)
781 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
787 struct type *word_type;
800 static struct mi_opt opts[] =
802 {"o", OFFSET_OPT, 1},
808 int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts,
812 switch ((enum opt) opt)
815 offset = atol (optarg);
822 if (argc < 5 || argc > 6)
824 mi_error_message = xstrprintf ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
828 /* Extract all the arguments. */
830 /* Start address of the memory dump. */
831 addr = parse_and_eval_address (argv[0]) + offset;
832 /* The format character to use when displaying a memory word. See
833 the ``x'' command. */
834 word_format = argv[1][0];
835 /* The size of the memory word. */
836 word_size = atol (argv[2]);
840 word_type = builtin_type_int8;
844 word_type = builtin_type_int16;
848 word_type = builtin_type_int32;
852 word_type = builtin_type_int64;
856 word_type = builtin_type_int8;
859 /* The number of rows. */
860 nr_rows = atol (argv[3]);
863 mi_error_message = xstrprintf ("mi_cmd_data_read_memory: invalid number of rows.");
866 /* Number of bytes per row. */
867 nr_cols = atol (argv[4]);
870 mi_error_message = xstrprintf ("mi_cmd_data_read_memory: invalid number of columns.");
873 /* The un-printable character when printing ascii. */
879 /* Create a buffer and read it in. */
880 total_bytes = word_size * nr_rows * nr_cols;
881 mbuf = xcalloc (total_bytes, 1);
882 make_cleanup (xfree, mbuf);
884 nr_bytes = target_read (¤t_target, TARGET_OBJECT_MEMORY, NULL,
885 mbuf, addr, total_bytes);
888 do_cleanups (cleanups);
889 mi_error_message = xstrdup ("Unable to read memory.");
893 /* Output the header information. */
894 ui_out_field_core_addr (uiout, "addr", addr);
895 ui_out_field_int (uiout, "nr-bytes", nr_bytes);
896 ui_out_field_int (uiout, "total-bytes", total_bytes);
897 ui_out_field_core_addr (uiout, "next-row", addr + word_size * nr_cols);
898 ui_out_field_core_addr (uiout, "prev-row", addr - word_size * nr_cols);
899 ui_out_field_core_addr (uiout, "next-page", addr + total_bytes);
900 ui_out_field_core_addr (uiout, "prev-page", addr - total_bytes);
902 /* Build the result as a two dimentional table. */
904 struct ui_stream *stream = ui_out_stream_new (uiout);
905 struct cleanup *cleanup_list_memory;
908 cleanup_list_memory = make_cleanup_ui_out_list_begin_end (uiout, "memory");
909 for (row = 0, row_byte = 0;
911 row++, row_byte += nr_cols * word_size)
915 struct cleanup *cleanup_tuple;
916 struct cleanup *cleanup_list_data;
917 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
918 ui_out_field_core_addr (uiout, "addr", addr + row_byte);
919 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
920 cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
921 for (col = 0, col_byte = row_byte;
923 col++, col_byte += word_size)
925 if (col_byte + word_size > nr_bytes)
927 ui_out_field_string (uiout, NULL, "N/A");
931 ui_file_rewind (stream->stream);
932 print_scalar_formatted (mbuf + col_byte, word_type, word_format,
933 word_asize, stream->stream);
934 ui_out_field_stream (uiout, NULL, stream);
937 do_cleanups (cleanup_list_data);
941 ui_file_rewind (stream->stream);
942 for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
944 if (byte >= nr_bytes)
946 fputc_unfiltered ('X', stream->stream);
948 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
950 fputc_unfiltered (aschar, stream->stream);
953 fputc_unfiltered (mbuf[byte], stream->stream);
955 ui_out_field_stream (uiout, "ascii", stream);
957 do_cleanups (cleanup_tuple);
959 ui_out_stream_delete (stream);
960 do_cleanups (cleanup_list_memory);
962 do_cleanups (cleanups);
966 /* DATA-MEMORY-WRITE:
968 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
969 offset from the beginning of the memory grid row where the cell to
971 ADDR: start address of the row in the memory grid where the memory
972 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
973 the location to write to.
974 FORMAT: a char indicating format for the ``word''. See
976 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
977 VALUE: value to be written into the memory address.
979 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
983 mi_cmd_data_write_memory (char *command, char **argv, int argc)
988 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
989 enough when using a compiler other than GCC. */
992 struct cleanup *old_chain;
1000 static struct mi_opt opts[] =
1002 {"o", OFFSET_OPT, 1},
1008 int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
1012 switch ((enum opt) opt)
1015 offset = atol (optarg);
1024 mi_error_message = xstrprintf ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1025 return MI_CMD_ERROR;
1028 /* Extract all the arguments. */
1029 /* Start address of the memory dump. */
1030 addr = parse_and_eval_address (argv[0]);
1031 /* The format character to use when displaying a memory word. See
1032 the ``x'' command. */
1033 word_format = argv[1][0];
1034 /* The size of the memory word. */
1035 word_size = atol (argv[2]);
1037 /* Calculate the real address of the write destination. */
1038 addr += (offset * word_size);
1040 /* Get the value as a number. */
1041 value = parse_and_eval_address (argv[3]);
1042 /* Get the value into an array. */
1043 buffer = xmalloc (word_size);
1044 old_chain = make_cleanup (xfree, buffer);
1045 store_signed_integer (buffer, word_size, value);
1046 /* Write it down to memory. */
1047 write_memory (addr, buffer, word_size);
1048 /* Free the buffer. */
1049 do_cleanups (old_chain);
1055 mi_cmd_enable_timings (char *command, char **argv, int argc)
1061 if (strcmp (argv[0], "yes") == 0)
1063 else if (strcmp (argv[0], "no") == 0)
1074 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command);
1075 return MI_CMD_ERROR;
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 struct mi_timestamp cmd_finished;
1094 switch (context->op)
1098 /* A MI command was read from the input stream. */
1100 /* FIXME: gdb_???? */
1101 fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
1102 context->token, context->command, context->args);
1103 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1104 condition expression, each function should return an
1105 indication of what action is required and then switch on
1107 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1110 current_command_ts = context->cmd_start;
1112 args->rc = mi_cmd_execute (context);
1115 timestamp (&cmd_finished);
1117 if (!target_can_async_p () || !target_executing)
1119 /* Print the result if there were no errors.
1121 Remember that on the way out of executing a command, you have
1122 to directly use the mi_interp's uiout, since the command could
1123 have reset the interpreter, in which case the current uiout
1124 will most likely crash in the mi_out_* routines. */
1125 if (args->rc == MI_CMD_DONE)
1127 fputs_unfiltered (context->token, raw_stdout);
1128 fputs_unfiltered ("^done", raw_stdout);
1129 mi_out_put (uiout, raw_stdout);
1130 mi_out_rewind (uiout);
1131 /* Have to check cmd_start, since the command could be
1133 if (do_timings && context->cmd_start)
1134 print_diff (context->cmd_start, &cmd_finished);
1135 fputs_unfiltered ("\n", raw_stdout);
1137 else if (args->rc == MI_CMD_ERROR)
1139 if (mi_error_message)
1141 fputs_unfiltered (context->token, raw_stdout);
1142 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1143 fputstr_unfiltered (mi_error_message, '"', raw_stdout);
1144 xfree (mi_error_message);
1145 mi_error_message = NULL;
1146 fputs_unfiltered ("\"\n", raw_stdout);
1148 mi_out_rewind (uiout);
1151 mi_out_rewind (uiout);
1153 else if (sync_execution)
1155 /* Don't print the prompt. We are executing the target in
1156 synchronous mode. */
1157 args->action = EXECUTE_COMMAND_SUPRESS_PROMPT;
1165 /* A CLI command was read from the input stream. */
1166 /* This "feature" will be removed as soon as we have a
1167 complete set of mi commands. */
1168 /* Echo the command on the console. */
1169 fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1170 /* Call the "console" interpreter. */
1171 argv[0] = "console";
1172 argv[1] = context->command;
1173 args->rc = mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);
1175 /* If we changed interpreters, DON'T print out anything. */
1176 if (current_interp_named_p (INTERP_MI)
1177 || current_interp_named_p (INTERP_MI1)
1178 || current_interp_named_p (INTERP_MI2)
1179 || current_interp_named_p (INTERP_MI3))
1181 if (args->rc == MI_CMD_DONE)
1183 fputs_unfiltered (context->token, raw_stdout);
1184 fputs_unfiltered ("^done", raw_stdout);
1185 mi_out_put (uiout, raw_stdout);
1186 mi_out_rewind (uiout);
1187 fputs_unfiltered ("\n", raw_stdout);
1188 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1190 else if (args->rc == MI_CMD_ERROR)
1192 if (mi_error_message)
1194 fputs_unfiltered (context->token, raw_stdout);
1195 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1196 fputstr_unfiltered (mi_error_message, '"', raw_stdout);
1197 xfree (mi_error_message);
1198 mi_error_message = NULL;
1199 fputs_unfiltered ("\"\n", raw_stdout);
1201 mi_out_rewind (uiout);
1204 mi_out_rewind (uiout);
1216 mi_execute_command (char *cmd, int from_tty)
1218 struct mi_parse *command;
1219 struct captured_mi_execute_command_args args;
1220 struct ui_out *saved_uiout = uiout;
1222 /* This is to handle EOF (^D). We just quit gdb. */
1223 /* FIXME: we should call some API function here. */
1225 quit_force (NULL, from_tty);
1227 command = mi_parse (cmd);
1229 if (command != NULL)
1231 struct gdb_exception result;
1235 command->cmd_start = (struct mi_timestamp *)
1236 xmalloc (sizeof (struct mi_timestamp));
1237 timestamp (command->cmd_start);
1240 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
1241 be pushed even further down or even eliminated? */
1242 args.command = command;
1243 result = catch_exception (uiout, captured_mi_execute_command, &args,
1245 exception_print (gdb_stderr, result);
1247 if (args.action == EXECUTE_COMMAND_SUPRESS_PROMPT)
1249 /* The command is executing synchronously. Bail out early
1250 suppressing the finished prompt. */
1251 mi_parse_free (command);
1254 if (result.reason < 0)
1256 /* The command execution failed and error() was called
1258 fputs_unfiltered (command->token, raw_stdout);
1259 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1260 if (result.message == NULL)
1261 fputs_unfiltered ("unknown error", raw_stdout);
1263 fputstr_unfiltered (result.message, '"', raw_stdout);
1264 fputs_unfiltered ("\"\n", raw_stdout);
1265 mi_out_rewind (uiout);
1267 mi_parse_free (command);
1270 fputs_unfiltered ("(gdb) \n", raw_stdout);
1271 gdb_flush (raw_stdout);
1272 /* Print any buffered hook code. */
1276 static enum mi_cmd_result
1277 mi_cmd_execute (struct mi_parse *parse)
1281 if (parse->cmd->argv_func != NULL
1282 || parse->cmd->args_func != NULL)
1284 /* FIXME: We need to save the token because the command executed
1285 may be asynchronous and need to print the token again.
1286 In the future we can pass the token down to the func
1287 and get rid of the last_async_command. */
1288 /* The problem here is to keep the token around when we launch
1289 the target, and we want to interrupt it later on. The
1290 interrupt command will have its own token, but when the
1291 target stops, we must display the token corresponding to the
1292 last execution command given. So we have another string where
1293 we copy the token (previous_async_command), if this was
1294 indeed the token of an execution command, and when we stop we
1295 print that one. This is possible because the interrupt
1296 command, when over, will copy that token back into the
1297 default token string (last_async_command). */
1299 if (target_executing)
1301 if (!previous_async_command)
1302 previous_async_command = xstrdup (last_async_command);
1303 if (strcmp (parse->command, "exec-interrupt"))
1305 fputs_unfiltered (parse->token, raw_stdout);
1306 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1307 fputs_unfiltered ("Cannot execute command ", raw_stdout);
1308 fputstr_unfiltered (parse->command, '"', raw_stdout);
1309 fputs_unfiltered (" while target running", raw_stdout);
1310 fputs_unfiltered ("\"\n", raw_stdout);
1311 return MI_CMD_ERROR;
1314 last_async_command = xstrdup (parse->token);
1315 make_exec_cleanup (free_current_contents, &last_async_command);
1316 /* FIXME: DELETE THIS! */
1317 if (parse->cmd->args_func != NULL)
1318 return parse->cmd->args_func (parse->args, 0 /*from_tty */ );
1319 return parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
1321 else if (parse->cmd->cli.cmd != 0)
1323 /* FIXME: DELETE THIS. */
1324 /* The operation is still implemented by a cli command. */
1325 /* Must be a synchronous one. */
1326 mi_execute_cli_command (parse->cmd->cli.cmd, parse->cmd->cli.args_p,
1332 /* FIXME: DELETE THIS. */
1333 fputs_unfiltered (parse->token, raw_stdout);
1334 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1335 fputs_unfiltered ("Undefined mi command: ", raw_stdout);
1336 fputstr_unfiltered (parse->command, '"', raw_stdout);
1337 fputs_unfiltered (" (missing implementation)", raw_stdout);
1338 fputs_unfiltered ("\"\n", raw_stdout);
1339 return MI_CMD_ERROR;
1343 /* FIXME: This is just a hack so we can get some extra commands going.
1344 We don't want to channel things through the CLI, but call libgdb directly.
1345 Use only for synchronous commands. */
1348 mi_execute_cli_command (const char *cmd, int args_p, const char *args)
1352 struct cleanup *old_cleanups;
1355 run = xstrprintf ("%s %s", cmd, args);
1357 run = xstrdup (cmd);
1359 /* FIXME: gdb_???? */
1360 fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
1362 old_cleanups = make_cleanup (xfree, run);
1363 execute_command ( /*ui */ run, 0 /*from_tty */ );
1364 do_cleanups (old_cleanups);
1370 mi_execute_async_cli_command (char *mi, char *args, int from_tty)
1372 struct cleanup *old_cleanups;
1376 if (target_can_async_p ())
1378 async_args = (char *) xmalloc (strlen (args) + 2);
1379 make_exec_cleanup (free, async_args);
1380 strcpy (async_args, args);
1381 strcat (async_args, "&");
1382 run = xstrprintf ("%s %s", mi, async_args);
1383 make_exec_cleanup (free, run);
1384 add_continuation (mi_exec_async_cli_cmd_continuation, NULL);
1385 old_cleanups = NULL;
1389 run = xstrprintf ("%s %s", mi, args);
1390 old_cleanups = make_cleanup (xfree, run);
1393 if (!target_can_async_p ())
1395 /* NOTE: For synchronous targets asynchronous behavour is faked by
1396 printing out the GDB prompt before we even try to execute the
1398 if (last_async_command)
1399 fputs_unfiltered (last_async_command, raw_stdout);
1400 fputs_unfiltered ("^running\n", raw_stdout);
1401 fputs_unfiltered ("(gdb) \n", raw_stdout);
1402 gdb_flush (raw_stdout);
1406 /* FIXME: cagney/1999-11-29: Printing this message before
1407 calling execute_command is wrong. It should only be printed
1408 once gdb has confirmed that it really has managed to send a
1409 run command to the target. */
1410 if (last_async_command)
1411 fputs_unfiltered (last_async_command, raw_stdout);
1412 fputs_unfiltered ("^running\n", raw_stdout);
1415 execute_command ( /*ui */ run, 0 /*from_tty */ );
1417 if (!target_can_async_p ())
1419 /* Do this before doing any printing. It would appear that some
1420 print code leaves garbage around in the buffer. */
1421 do_cleanups (old_cleanups);
1422 /* If the target was doing the operation synchronously we fake
1423 the stopped message. */
1424 if (last_async_command)
1425 fputs_unfiltered (last_async_command, raw_stdout);
1426 fputs_unfiltered ("*stopped", raw_stdout);
1427 mi_out_put (uiout, raw_stdout);
1428 mi_out_rewind (uiout);
1430 print_diff_now (current_command_ts);
1431 fputs_unfiltered ("\n", raw_stdout);
1432 return MI_CMD_QUIET;
1438 mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg)
1440 if (last_async_command)
1441 fputs_unfiltered (last_async_command, raw_stdout);
1442 fputs_unfiltered ("*stopped", raw_stdout);
1443 mi_out_put (uiout, raw_stdout);
1444 fputs_unfiltered ("\n", raw_stdout);
1445 fputs_unfiltered ("(gdb) \n", raw_stdout);
1446 gdb_flush (raw_stdout);
1447 do_exec_cleanups (ALL_CLEANUPS);
1451 mi_load_progress (const char *section_name,
1452 unsigned long sent_so_far,
1453 unsigned long total_section,
1454 unsigned long total_sent,
1455 unsigned long grand_total)
1457 struct timeval time_now, delta, update_threshold;
1458 static struct timeval last_update;
1459 static char *previous_sect_name = NULL;
1461 struct ui_out *saved_uiout;
1463 /* This function is called through deprecated_show_load_progress
1464 which means uiout may not be correct. Fix it for the duration
1465 of this function. */
1466 saved_uiout = uiout;
1468 if (current_interp_named_p (INTERP_MI)
1469 || current_interp_named_p (INTERP_MI2))
1470 uiout = mi_out_new (2);
1471 else if (current_interp_named_p (INTERP_MI1))
1472 uiout = mi_out_new (1);
1473 else if (current_interp_named_p (INTERP_MI3))
1474 uiout = mi_out_new (3);
1478 update_threshold.tv_sec = 0;
1479 update_threshold.tv_usec = 500000;
1480 gettimeofday (&time_now, NULL);
1482 delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
1483 delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
1485 if (delta.tv_usec < 0)
1488 delta.tv_usec += 1000000L;
1491 new_section = (previous_sect_name ?
1492 strcmp (previous_sect_name, section_name) : 1);
1495 struct cleanup *cleanup_tuple;
1496 xfree (previous_sect_name);
1497 previous_sect_name = xstrdup (section_name);
1499 if (last_async_command)
1500 fputs_unfiltered (last_async_command, raw_stdout);
1501 fputs_unfiltered ("+download", raw_stdout);
1502 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1503 ui_out_field_string (uiout, "section", section_name);
1504 ui_out_field_int (uiout, "section-size", total_section);
1505 ui_out_field_int (uiout, "total-size", grand_total);
1506 do_cleanups (cleanup_tuple);
1507 mi_out_put (uiout, raw_stdout);
1508 fputs_unfiltered ("\n", raw_stdout);
1509 gdb_flush (raw_stdout);
1512 if (delta.tv_sec >= update_threshold.tv_sec &&
1513 delta.tv_usec >= update_threshold.tv_usec)
1515 struct cleanup *cleanup_tuple;
1516 last_update.tv_sec = time_now.tv_sec;
1517 last_update.tv_usec = time_now.tv_usec;
1518 if (last_async_command)
1519 fputs_unfiltered (last_async_command, raw_stdout);
1520 fputs_unfiltered ("+download", raw_stdout);
1521 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1522 ui_out_field_string (uiout, "section", section_name);
1523 ui_out_field_int (uiout, "section-sent", sent_so_far);
1524 ui_out_field_int (uiout, "section-size", total_section);
1525 ui_out_field_int (uiout, "total-sent", total_sent);
1526 ui_out_field_int (uiout, "total-size", grand_total);
1527 do_cleanups (cleanup_tuple);
1528 mi_out_put (uiout, raw_stdout);
1529 fputs_unfiltered ("\n", raw_stdout);
1530 gdb_flush (raw_stdout);
1534 uiout = saved_uiout;
1538 timestamp (struct mi_timestamp *tv)
1541 gettimeofday (&tv->wallclock, NULL);
1542 #ifdef HAVE_GETRUSAGE
1543 getrusage (RUSAGE_SELF, &rusage);
1544 tv->utime.tv_sec = rusage.ru_utime.tv_sec;
1545 tv->utime.tv_usec = rusage.ru_utime.tv_usec;
1546 tv->stime.tv_sec = rusage.ru_stime.tv_sec;
1547 tv->stime.tv_usec = rusage.ru_stime.tv_usec;
1549 usec = get_run_time ();
1550 tv->utime.tv_sec = usec/1000000L;
1551 tv->utime.tv_usec = usec - 1000000L*tv->utime.tv_sec;
1552 tv->stime.tv_sec = 0;
1553 tv->stime.tv_usec = 0;
1558 print_diff_now (struct mi_timestamp *start)
1560 struct mi_timestamp now;
1562 print_diff (start, &now);
1566 timeval_diff (struct timeval start, struct timeval end)
1568 return ((end.tv_sec - start.tv_sec) * 1000000L)
1569 + (end.tv_usec - start.tv_usec);
1573 print_diff (struct mi_timestamp *start, struct mi_timestamp *end)
1577 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1578 timeval_diff (start->wallclock, end->wallclock) / 1000000.0,
1579 timeval_diff (start->utime, end->utime) / 1000000.0,
1580 timeval_diff (start->stime, end->stime) / 1000000.0);