3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
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(). */
47 #include "mi-common.h"
55 #if defined HAVE_SYS_RESOURCE_H
56 #include <sys/resource.h>
69 struct ui_file *raw_stdout;
71 /* This is used to pass the current command timestamp
72 down to continuation routines. */
73 static struct mi_timestamp *current_command_ts;
75 static int do_timings = 0;
78 int running_result_record_printed = 1;
80 extern void _initialize_mi_main (void);
81 static void mi_cmd_execute (struct mi_parse *parse);
83 static void mi_execute_cli_command (const char *cmd, int args_p,
85 static void mi_execute_async_cli_command (char *cli_command,
86 char **argv, int argc);
87 static int register_changed_p (int regnum, struct regcache *,
89 static void get_register (int regnum, int format);
91 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
92 layer that calls libgdb. Any operation used in the below should be
95 static void timestamp (struct mi_timestamp *tv);
97 static void print_diff_now (struct mi_timestamp *start);
98 static void print_diff (struct mi_timestamp *start, struct mi_timestamp *end);
101 mi_cmd_gdb_exit (char *command, char **argv, int argc)
103 /* We have to print everything right here because we never return. */
105 fputs_unfiltered (current_token, raw_stdout);
106 fputs_unfiltered ("^exit\n", raw_stdout);
107 mi_out_put (uiout, raw_stdout);
108 /* FIXME: The function called is not yet a formal libgdb function. */
109 quit_force (NULL, FROM_TTY);
113 mi_cmd_exec_next (char *command, char **argv, int argc)
115 /* FIXME: Should call a libgdb function, not a cli wrapper. */
116 mi_execute_async_cli_command ("next", argv, argc);
120 mi_cmd_exec_next_instruction (char *command, char **argv, int argc)
122 /* FIXME: Should call a libgdb function, not a cli wrapper. */
123 mi_execute_async_cli_command ("nexti", argv, argc);
127 mi_cmd_exec_step (char *command, char **argv, int argc)
129 /* FIXME: Should call a libgdb function, not a cli wrapper. */
130 mi_execute_async_cli_command ("step", argv, argc);
134 mi_cmd_exec_step_instruction (char *command, char **argv, int argc)
136 /* FIXME: Should call a libgdb function, not a cli wrapper. */
137 mi_execute_async_cli_command ("stepi", argv, argc);
141 mi_cmd_exec_finish (char *command, char **argv, int argc)
143 /* FIXME: Should call a libgdb function, not a cli wrapper. */
144 mi_execute_async_cli_command ("finish", argv, argc);
148 mi_cmd_exec_return (char *command, char **argv, int argc)
150 /* This command doesn't really execute the target, it just pops the
151 specified number of frames. */
153 /* Call return_command with from_tty argument equal to 0 so as to
154 avoid being queried. */
155 return_command (*argv, 0);
157 /* Call return_command with from_tty argument equal to 0 so as to
158 avoid being queried. */
159 return_command (NULL, 0);
161 /* Because we have called return_command with from_tty = 0, we need
162 to print the frame here. */
163 print_stack_frame (get_selected_frame (NULL), 1, LOC_AND_ADDRESS);
167 proceed_thread_callback (struct thread_info *thread, void *arg)
169 int pid = *(int *)arg;
171 if (!is_stopped (thread->ptid))
174 if (PIDGET (thread->ptid) != pid)
177 switch_to_thread (thread->ptid);
178 clear_proceed_status ();
179 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
184 mi_cmd_exec_continue (char *command, char **argv, int argc)
188 else if (argc == 1 && strcmp (argv[0], "--all") == 0)
190 else if (argc == 2 && strcmp (argv[0], "--thread-group") == 0)
192 struct cleanup *old_chain;
194 if (argv[1] == NULL || argv[1] == '\0')
195 error ("Thread group id not specified");
196 pid = atoi (argv[1] + 1);
197 if (!in_inferior_list (pid))
198 error ("Invalid thread group id '%s'", argv[1]);
200 old_chain = make_cleanup_restore_current_thread ();
201 iterate_over_threads (proceed_thread_callback, &pid);
202 do_cleanups (old_chain);
205 error ("Usage: -exec-continue [--all|--thread-group id]");
209 interrupt_thread_callback (struct thread_info *thread, void *arg)
211 int pid = *(int *)arg;
213 if (!is_running (thread->ptid))
216 if (PIDGET (thread->ptid) != pid)
219 target_stop (thread->ptid);
223 /* Interrupt the execution of the target. Note how we must play around
224 with the token variables, in order to display the current token in
225 the result of the interrupt command, and the previous execution
226 token when the target finally stops. See comments in
229 mi_cmd_exec_interrupt (char *command, char **argv, int argc)
233 if (!is_running (inferior_ptid))
234 error ("Current thread is not running.");
236 interrupt_target_1 (0);
238 else if (argc == 1 && strcmp (argv[0], "--all") == 0)
241 error ("Inferior not running.");
243 interrupt_target_1 (1);
245 else if (argc == 2 && strcmp (argv[0], "--thread-group") == 0)
247 struct cleanup *old_chain;
249 if (argv[1] == NULL || argv[1] == '\0')
250 error ("Thread group id not specified");
251 pid = atoi (argv[1] + 1);
252 if (!in_inferior_list (pid))
253 error ("Invalid thread group id '%s'", argv[1]);
255 old_chain = make_cleanup_restore_current_thread ();
256 iterate_over_threads (interrupt_thread_callback, &pid);
257 do_cleanups (old_chain);
260 error ("Usage: -exec-interrupt [--all|--thread-group id]");
264 find_thread_of_process (struct thread_info *ti, void *p)
267 if (PIDGET (ti->ptid) == pid && !is_exited (ti->ptid))
274 mi_cmd_target_detach (char *command, char **argv, int argc)
276 if (argc != 0 && argc != 1)
277 error ("Usage: -target-detach [thread-group]");
281 struct thread_info *tp;
283 int pid = strtol (argv[0], &end, 10);
285 error (_("Cannot parse thread group id '%s'"), argv[0]);
287 /* Pick any thread in the desired process. Current
288 target_detach deteches from the parent of inferior_ptid. */
289 tp = iterate_over_threads (find_thread_of_process, &pid);
291 error (_("Thread group is empty"));
293 switch_to_thread (tp->ptid);
296 detach_command (NULL, 0);
300 mi_cmd_thread_select (char *command, char **argv, int argc)
303 char *mi_error_message;
306 error ("mi_cmd_thread_select: USAGE: threadnum.");
308 rc = gdb_thread_select (uiout, argv[0], &mi_error_message);
310 if (rc == GDB_RC_FAIL)
312 make_cleanup (xfree, mi_error_message);
313 error ("%s", mi_error_message);
318 mi_cmd_thread_list_ids (char *command, char **argv, int argc)
321 char *mi_error_message;
324 error ("mi_cmd_thread_list_ids: No arguments required.");
326 rc = gdb_list_thread_ids (uiout, &mi_error_message);
328 if (rc == GDB_RC_FAIL)
330 make_cleanup (xfree, mi_error_message);
331 error ("%s", mi_error_message);
336 mi_cmd_thread_info (char *command, char **argv, int argc)
340 if (argc != 0 && argc != 1)
341 error ("Invalid MI command");
344 thread = atoi (argv[0]);
346 print_thread_info (uiout, thread, -1);
350 print_one_inferior (struct inferior *inferior, void *arg)
352 struct cleanup *back_to = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
354 ui_out_field_fmt (uiout, "id", "%d", inferior->pid);
355 ui_out_field_string (uiout, "type", "process");
356 ui_out_field_int (uiout, "pid", inferior->pid);
358 do_cleanups (back_to);
363 mi_cmd_list_thread_groups (char *command, char **argv, int argc)
365 struct cleanup *back_to;
369 if (argc > 0 && strcmp (argv[0], "--available") == 0)
379 back_to = make_cleanup (&null_cleanup, NULL);
384 if (!in_inferior_list (pid))
385 error ("Invalid thread group id '%s'", id);
386 print_thread_info (uiout, -1, pid);
390 make_cleanup_ui_out_list_begin_end (uiout, "groups");
391 iterate_over_inferiors (print_one_inferior, NULL);
394 do_cleanups (back_to);
398 mi_cmd_data_list_register_names (char *command, char **argv, int argc)
402 struct cleanup *cleanup;
404 /* Note that the test for a valid register must include checking the
405 gdbarch_register_name because gdbarch_num_regs may be allocated for
406 the union of the register sets within a family of related processors.
407 In this case, some entries of gdbarch_register_name will change depending
408 upon the particular processor being debugged. */
410 numregs = gdbarch_num_regs (current_gdbarch)
411 + gdbarch_num_pseudo_regs (current_gdbarch);
413 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names");
415 if (argc == 0) /* No args, just do all the regs. */
421 if (gdbarch_register_name (current_gdbarch, regnum) == NULL
422 || *(gdbarch_register_name (current_gdbarch, regnum)) == '\0')
423 ui_out_field_string (uiout, NULL, "");
425 ui_out_field_string (uiout, NULL,
426 gdbarch_register_name
427 (current_gdbarch, regnum));
431 /* Else, list of register #s, just do listed regs. */
432 for (i = 0; i < argc; i++)
434 regnum = atoi (argv[i]);
435 if (regnum < 0 || regnum >= numregs)
436 error ("bad register number");
438 if (gdbarch_register_name (current_gdbarch, regnum) == NULL
439 || *(gdbarch_register_name (current_gdbarch, regnum)) == '\0')
440 ui_out_field_string (uiout, NULL, "");
442 ui_out_field_string (uiout, NULL,
443 gdbarch_register_name (current_gdbarch, regnum));
445 do_cleanups (cleanup);
449 mi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
451 static struct regcache *this_regs = NULL;
452 struct regcache *prev_regs;
453 int regnum, numregs, changed;
455 struct cleanup *cleanup;
457 /* The last time we visited this function, the current frame's register
458 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
459 and refresh THIS_REGS with the now-current register contents. */
461 prev_regs = this_regs;
462 this_regs = frame_save_as_regcache (get_selected_frame (NULL));
463 cleanup = make_cleanup_regcache_xfree (prev_regs);
465 /* Note that the test for a valid register must include checking the
466 gdbarch_register_name because gdbarch_num_regs may be allocated for
467 the union of the register sets within a family of related processors.
468 In this case, some entries of gdbarch_register_name will change depending
469 upon the particular processor being debugged. */
471 numregs = gdbarch_num_regs (current_gdbarch)
472 + gdbarch_num_pseudo_regs (current_gdbarch);
474 make_cleanup_ui_out_list_begin_end (uiout, "changed-registers");
476 if (argc == 0) /* No args, just do all the regs. */
482 if (gdbarch_register_name (current_gdbarch, regnum) == NULL
483 || *(gdbarch_register_name (current_gdbarch, regnum)) == '\0')
485 changed = register_changed_p (regnum, prev_regs, this_regs);
487 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
489 ui_out_field_int (uiout, NULL, regnum);
493 /* Else, list of register #s, just do listed regs. */
494 for (i = 0; i < argc; i++)
496 regnum = atoi (argv[i]);
500 && gdbarch_register_name (current_gdbarch, regnum) != NULL
501 && *gdbarch_register_name (current_gdbarch, regnum) != '\000')
503 changed = register_changed_p (regnum, prev_regs, this_regs);
505 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
507 ui_out_field_int (uiout, NULL, regnum);
510 error ("bad register number");
512 do_cleanups (cleanup);
516 register_changed_p (int regnum, struct regcache *prev_regs,
517 struct regcache *this_regs)
519 struct gdbarch *gdbarch = get_regcache_arch (this_regs);
520 gdb_byte prev_buffer[MAX_REGISTER_SIZE];
521 gdb_byte this_buffer[MAX_REGISTER_SIZE];
523 /* Registers not valid in this frame return count as unchanged. */
524 if (!regcache_valid_p (this_regs, regnum))
527 /* First time through or after gdbarch change consider all registers as
528 changed. Same for registers not valid in the previous frame. */
529 if (!prev_regs || get_regcache_arch (prev_regs) != gdbarch
530 || !regcache_valid_p (prev_regs, regnum))
533 /* Get register contents and compare. */
534 regcache_cooked_read (prev_regs, regnum, prev_buffer);
535 regcache_cooked_read (this_regs, regnum, this_buffer);
537 return memcmp (prev_buffer, this_buffer,
538 register_size (gdbarch, regnum)) != 0;
541 /* Return a list of register number and value pairs. The valid
542 arguments expected are: a letter indicating the format in which to
543 display the registers contents. This can be one of: x (hexadecimal), d
544 (decimal), N (natural), t (binary), o (octal), r (raw). After the
545 format argumetn there can be a sequence of numbers, indicating which
546 registers to fetch the content of. If the format is the only argument,
547 a list of all the registers with their values is returned. */
549 mi_cmd_data_list_register_values (char *command, char **argv, int argc)
551 int regnum, numregs, format;
553 struct cleanup *list_cleanup, *tuple_cleanup;
555 /* Note that the test for a valid register must include checking the
556 gdbarch_register_name because gdbarch_num_regs may be allocated for
557 the union of the register sets within a family of related processors.
558 In this case, some entries of gdbarch_register_name will change depending
559 upon the particular processor being debugged. */
561 numregs = gdbarch_num_regs (current_gdbarch)
562 + gdbarch_num_pseudo_regs (current_gdbarch);
565 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
567 format = (int) argv[0][0];
569 list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values");
571 if (argc == 1) /* No args, beside the format: do all the regs. */
577 if (gdbarch_register_name (current_gdbarch, regnum) == NULL
578 || *(gdbarch_register_name (current_gdbarch, regnum)) == '\0')
580 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
581 ui_out_field_int (uiout, "number", regnum);
582 get_register (regnum, format);
583 do_cleanups (tuple_cleanup);
587 /* Else, list of register #s, just do listed regs. */
588 for (i = 1; i < argc; i++)
590 regnum = atoi (argv[i]);
594 && gdbarch_register_name (current_gdbarch, regnum) != NULL
595 && *gdbarch_register_name (current_gdbarch, regnum) != '\000')
597 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
598 ui_out_field_int (uiout, "number", regnum);
599 get_register (regnum, format);
600 do_cleanups (tuple_cleanup);
603 error ("bad register number");
605 do_cleanups (list_cleanup);
608 /* Output one register's contents in the desired format. */
610 get_register (int regnum, int format)
612 gdb_byte buffer[MAX_REGISTER_SIZE];
617 static struct ui_stream *stb = NULL;
619 stb = ui_out_stream_new (uiout);
624 frame_register (get_selected_frame (NULL), regnum, &optim, &lval, &addr,
628 error ("Optimized out");
633 char *ptr, buf[1024];
637 for (j = 0; j < register_size (current_gdbarch, regnum); j++)
639 int idx = gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG ? j
640 : register_size (current_gdbarch, regnum) - 1 - j;
641 sprintf (ptr, "%02x", (unsigned char) buffer[idx]);
644 ui_out_field_string (uiout, "value", buf);
645 /*fputs_filtered (buf, gdb_stdout); */
649 struct value_print_options opts;
650 get_formatted_print_options (&opts, format);
652 val_print (register_type (current_gdbarch, regnum), buffer, 0, 0,
653 stb->stream, 0, &opts, current_language);
654 ui_out_field_stream (uiout, "value", stb);
655 ui_out_stream_delete (stb);
659 /* Write given values into registers. The registers and values are
660 given as pairs. The corresponding MI command is
661 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
663 mi_cmd_data_write_register_values (char *command, char **argv, int argc)
668 /* Note that the test for a valid register must include checking the
669 gdbarch_register_name because gdbarch_num_regs may be allocated for
670 the union of the register sets within a family of related processors.
671 In this case, some entries of gdbarch_register_name will change depending
672 upon the particular processor being debugged. */
674 numregs = gdbarch_num_regs (current_gdbarch)
675 + gdbarch_num_pseudo_regs (current_gdbarch);
678 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
680 format = (int) argv[0][0];
682 if (!target_has_registers)
683 error ("mi_cmd_data_write_register_values: No registers.");
686 error ("mi_cmd_data_write_register_values: No regs and values specified.");
689 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
691 for (i = 1; i < argc; i = i + 2)
693 int regnum = atoi (argv[i]);
695 if (regnum >= 0 && regnum < numregs
696 && gdbarch_register_name (current_gdbarch, regnum)
697 && *gdbarch_register_name (current_gdbarch, regnum))
701 /* Get the value as a number. */
702 value = parse_and_eval_address (argv[i + 1]);
705 regcache_cooked_write_signed (get_current_regcache (), regnum, value);
708 error ("bad register number");
712 /* Evaluate the value of the argument. The argument is an
713 expression. If the expression contains spaces it needs to be
714 included in double quotes. */
716 mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
718 struct expression *expr;
719 struct cleanup *old_chain = NULL;
721 struct ui_stream *stb = NULL;
722 struct value_print_options opts;
724 stb = ui_out_stream_new (uiout);
728 ui_out_stream_delete (stb);
729 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
732 expr = parse_expression (argv[0]);
734 old_chain = make_cleanup (free_current_contents, &expr);
736 val = evaluate_expression (expr);
738 /* Print the result of the expression evaluation. */
739 get_user_print_options (&opts);
741 val_print (value_type (val), value_contents (val),
742 value_embedded_offset (val), VALUE_ADDRESS (val),
743 stb->stream, 0, &opts, current_language);
745 ui_out_field_stream (uiout, "value", stb);
746 ui_out_stream_delete (stb);
748 do_cleanups (old_chain);
753 ADDR: start address of data to be dumped.
754 WORD-FORMAT: a char indicating format for the ``word''. See
756 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
757 NR_ROW: Number of rows.
758 NR_COL: The number of colums (words per row).
759 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
760 ASCHAR for unprintable characters.
762 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
763 displayes them. Returns:
765 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
768 The number of bytes read is SIZE*ROW*COL. */
771 mi_cmd_data_read_memory (char *command, char **argv, int argc)
773 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
779 struct type *word_type;
792 static struct mi_opt opts[] =
794 {"o", OFFSET_OPT, 1},
800 int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts,
804 switch ((enum opt) opt)
807 offset = atol (optarg);
814 if (argc < 5 || argc > 6)
815 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
817 /* Extract all the arguments. */
819 /* Start address of the memory dump. */
820 addr = parse_and_eval_address (argv[0]) + offset;
821 /* The format character to use when displaying a memory word. See
822 the ``x'' command. */
823 word_format = argv[1][0];
824 /* The size of the memory word. */
825 word_size = atol (argv[2]);
829 word_type = builtin_type_int8;
833 word_type = builtin_type_int16;
837 word_type = builtin_type_int32;
841 word_type = builtin_type_int64;
845 word_type = builtin_type_int8;
848 /* The number of rows. */
849 nr_rows = atol (argv[3]);
851 error ("mi_cmd_data_read_memory: invalid number of rows.");
853 /* Number of bytes per row. */
854 nr_cols = atol (argv[4]);
856 error ("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);
869 nr_bytes = target_read_until_error (¤t_target, TARGET_OBJECT_MEMORY,
870 NULL, mbuf, addr, total_bytes);
872 error ("Unable to read memory.");
874 /* Output the header information. */
875 ui_out_field_core_addr (uiout, "addr", addr);
876 ui_out_field_int (uiout, "nr-bytes", nr_bytes);
877 ui_out_field_int (uiout, "total-bytes", total_bytes);
878 ui_out_field_core_addr (uiout, "next-row", addr + word_size * nr_cols);
879 ui_out_field_core_addr (uiout, "prev-row", addr - word_size * nr_cols);
880 ui_out_field_core_addr (uiout, "next-page", addr + total_bytes);
881 ui_out_field_core_addr (uiout, "prev-page", addr - total_bytes);
883 /* Build the result as a two dimentional table. */
885 struct ui_stream *stream = ui_out_stream_new (uiout);
886 struct cleanup *cleanup_list_memory;
889 cleanup_list_memory = make_cleanup_ui_out_list_begin_end (uiout, "memory");
890 for (row = 0, row_byte = 0;
892 row++, row_byte += nr_cols * word_size)
896 struct cleanup *cleanup_tuple;
897 struct cleanup *cleanup_list_data;
898 struct value_print_options opts;
900 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
901 ui_out_field_core_addr (uiout, "addr", addr + row_byte);
902 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
903 cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
904 get_formatted_print_options (&opts, word_format);
905 for (col = 0, col_byte = row_byte;
907 col++, col_byte += word_size)
909 if (col_byte + word_size > nr_bytes)
911 ui_out_field_string (uiout, NULL, "N/A");
915 ui_file_rewind (stream->stream);
916 print_scalar_formatted (mbuf + col_byte, word_type, &opts,
917 word_asize, stream->stream);
918 ui_out_field_stream (uiout, NULL, stream);
921 do_cleanups (cleanup_list_data);
925 ui_file_rewind (stream->stream);
926 for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
928 if (byte >= nr_bytes)
930 fputc_unfiltered ('X', stream->stream);
932 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
934 fputc_unfiltered (aschar, stream->stream);
937 fputc_unfiltered (mbuf[byte], stream->stream);
939 ui_out_field_stream (uiout, "ascii", stream);
941 do_cleanups (cleanup_tuple);
943 ui_out_stream_delete (stream);
944 do_cleanups (cleanup_list_memory);
946 do_cleanups (cleanups);
949 /* DATA-MEMORY-WRITE:
951 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
952 offset from the beginning of the memory grid row where the cell to
954 ADDR: start address of the row in the memory grid where the memory
955 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
956 the location to write to.
957 FORMAT: a char indicating format for the ``word''. See
959 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
960 VALUE: value to be written into the memory address.
962 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
966 mi_cmd_data_write_memory (char *command, char **argv, int argc)
971 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
972 enough when using a compiler other than GCC. */
975 struct cleanup *old_chain;
983 static struct mi_opt opts[] =
985 {"o", OFFSET_OPT, 1},
991 int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
995 switch ((enum opt) opt)
998 offset = atol (optarg);
1006 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1008 /* Extract all the arguments. */
1009 /* Start address of the memory dump. */
1010 addr = parse_and_eval_address (argv[0]);
1011 /* The format character to use when displaying a memory word. See
1012 the ``x'' command. */
1013 word_format = argv[1][0];
1014 /* The size of the memory word. */
1015 word_size = atol (argv[2]);
1017 /* Calculate the real address of the write destination. */
1018 addr += (offset * word_size);
1020 /* Get the value as a number. */
1021 value = parse_and_eval_address (argv[3]);
1022 /* Get the value into an array. */
1023 buffer = xmalloc (word_size);
1024 old_chain = make_cleanup (xfree, buffer);
1025 store_signed_integer (buffer, word_size, value);
1026 /* Write it down to memory. */
1027 write_memory (addr, buffer, word_size);
1028 /* Free the buffer. */
1029 do_cleanups (old_chain);
1033 mi_cmd_enable_timings (char *command, char **argv, int argc)
1039 if (strcmp (argv[0], "yes") == 0)
1041 else if (strcmp (argv[0], "no") == 0)
1052 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command);
1056 mi_cmd_list_features (char *command, char **argv, int argc)
1060 struct cleanup *cleanup = NULL;
1061 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
1063 ui_out_field_string (uiout, NULL, "frozen-varobjs");
1064 ui_out_field_string (uiout, NULL, "pending-breakpoints");
1065 ui_out_field_string (uiout, NULL, "thread-info");
1067 do_cleanups (cleanup);
1071 error ("-list-features should be passed no arguments");
1075 mi_cmd_list_target_features (char *command, char **argv, int argc)
1079 struct cleanup *cleanup = NULL;
1080 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
1082 if (target_can_async_p ())
1083 ui_out_field_string (uiout, NULL, "async");
1085 do_cleanups (cleanup);
1089 error ("-list-target-features should be passed no arguments");
1092 /* Execute a command within a safe environment.
1093 Return <0 for error; >=0 for ok.
1095 args->action will tell mi_execute_command what action
1096 to perfrom after the given command has executed (display/suppress
1097 prompt, display error). */
1100 captured_mi_execute_command (struct ui_out *uiout, void *data)
1102 struct mi_parse *context = (struct mi_parse *) data;
1104 struct mi_timestamp cmd_finished;
1106 running_result_record_printed = 0;
1107 switch (context->op)
1110 /* A MI command was read from the input stream. */
1112 /* FIXME: gdb_???? */
1113 fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
1114 context->token, context->command, context->args);
1117 current_command_ts = context->cmd_start;
1119 mi_cmd_execute (context);
1122 timestamp (&cmd_finished);
1124 /* Print the result if there were no errors.
1126 Remember that on the way out of executing a command, you have
1127 to directly use the mi_interp's uiout, since the command could
1128 have reset the interpreter, in which case the current uiout
1129 will most likely crash in the mi_out_* routines. */
1130 if (!running_result_record_printed)
1132 fputs_unfiltered (context->token, raw_stdout);
1133 /* There's no particularly good reason why target-connect results
1134 in not ^done. Should kill ^connected for MI3. */
1135 fputs_unfiltered (strcmp (context->command, "target-select") == 0
1136 ? "^connected" : "^done", raw_stdout);
1137 mi_out_put (uiout, raw_stdout);
1138 mi_out_rewind (uiout);
1139 /* Have to check cmd_start, since the command could be
1141 if (do_timings && context->cmd_start)
1142 print_diff (context->cmd_start, &cmd_finished);
1143 fputs_unfiltered ("\n", raw_stdout);
1146 /* The command does not want anything to be printed. In that
1147 case, the command probably should not have written anything
1148 to uiout, but in case it has written something, discard it. */
1149 mi_out_rewind (uiout);
1155 /* A CLI command was read from the input stream. */
1156 /* This "feature" will be removed as soon as we have a
1157 complete set of mi commands. */
1158 /* Echo the command on the console. */
1159 fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1160 /* Call the "console" interpreter. */
1161 argv[0] = "console";
1162 argv[1] = context->command;
1163 mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);
1165 /* If we changed interpreters, DON'T print out anything. */
1166 if (current_interp_named_p (INTERP_MI)
1167 || current_interp_named_p (INTERP_MI1)
1168 || current_interp_named_p (INTERP_MI2)
1169 || current_interp_named_p (INTERP_MI3))
1171 if (!running_result_record_printed)
1173 fputs_unfiltered (context->token, raw_stdout);
1174 fputs_unfiltered ("^done", raw_stdout);
1175 mi_out_put (uiout, raw_stdout);
1176 mi_out_rewind (uiout);
1177 fputs_unfiltered ("\n", raw_stdout);
1180 mi_out_rewind (uiout);
1192 mi_execute_command (char *cmd, int from_tty)
1194 struct mi_parse *command;
1195 struct ui_out *saved_uiout = uiout;
1197 /* This is to handle EOF (^D). We just quit gdb. */
1198 /* FIXME: we should call some API function here. */
1200 quit_force (NULL, from_tty);
1202 command = mi_parse (cmd);
1204 if (command != NULL)
1206 struct gdb_exception result;
1207 ptid_t previous_ptid = inferior_ptid;
1211 command->cmd_start = (struct mi_timestamp *)
1212 xmalloc (sizeof (struct mi_timestamp));
1213 timestamp (command->cmd_start);
1216 result = catch_exception (uiout, captured_mi_execute_command, command,
1218 if (result.reason < 0)
1220 /* The command execution failed and error() was called
1222 fputs_unfiltered (command->token, raw_stdout);
1223 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1224 if (result.message == NULL)
1225 fputs_unfiltered ("unknown error", raw_stdout);
1227 fputstr_unfiltered (result.message, '"', raw_stdout);
1228 fputs_unfiltered ("\"\n", raw_stdout);
1229 mi_out_rewind (uiout);
1232 if (/* The notifications are only output when the top-level
1233 interpreter (specified on the command line) is MI. */
1234 ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
1235 /* Don't try report anything if there are no threads --
1236 the program is dead. */
1237 && thread_count () != 0
1238 /* -thread-select explicitly changes thread. If frontend uses that
1239 internally, we don't want to emit =thread-selected, since
1240 =thread-selected is supposed to indicate user's intentions. */
1241 && strcmp (command->command, "thread-select") != 0)
1243 struct mi_interp *mi = top_level_interpreter_data ();
1244 struct thread_info *ti = inferior_thread ();
1247 if (command->thread == -1)
1249 report_change = !ptid_equal (previous_ptid, null_ptid)
1250 && !ptid_equal (inferior_ptid, previous_ptid);
1254 report_change = (ti->num != command->thread);
1259 target_terminal_ours ();
1260 fprintf_unfiltered (mi->event_channel,
1261 "thread-selected,id=\"%d\"",
1263 gdb_flush (mi->event_channel);
1267 mi_parse_free (command);
1270 fputs_unfiltered ("(gdb) \n", raw_stdout);
1271 gdb_flush (raw_stdout);
1272 /* Print any buffered hook code. */
1277 mi_cmd_execute (struct mi_parse *parse)
1279 struct cleanup *cleanup;
1283 current_token = xstrdup (parse->token);
1284 cleanup = make_cleanup (free_current_contents, ¤t_token);
1286 if (parse->frame != -1 && parse->thread == -1)
1287 error (_("Cannot specify --frame without --thread"));
1289 if (parse->thread != -1)
1291 struct thread_info *tp = find_thread_id (parse->thread);
1293 error (_("Invalid thread id: %d"), parse->thread);
1295 if (is_exited (tp->ptid))
1296 error (_("Thread id: %d has terminated"), parse->thread);
1298 switch_to_thread (tp->ptid);
1301 if (parse->frame != -1)
1303 struct frame_info *fid;
1304 int frame = parse->frame;
1305 fid = find_relative_frame (get_current_frame (), &frame);
1307 /* find_relative_frame was successful */
1310 error (_("Invalid frame id: %d"), frame);
1313 if (parse->cmd->argv_func != NULL)
1315 if (target_can_async_p ()
1316 && target_has_execution
1317 && (is_exited (inferior_ptid))
1318 && (strcmp (parse->command, "thread-info") != 0
1319 && strcmp (parse->command, "thread-list-ids") != 0
1320 && strcmp (parse->command, "thread-select") != 0))
1322 struct ui_file *stb;
1323 stb = mem_fileopen ();
1325 fputs_unfiltered ("Cannot execute command ", stb);
1326 fputstr_unfiltered (parse->command, '"', stb);
1327 fputs_unfiltered (" without a selected thread", stb);
1329 make_cleanup_ui_file_delete (stb);
1333 parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
1335 else if (parse->cmd->cli.cmd != 0)
1337 /* FIXME: DELETE THIS. */
1338 /* The operation is still implemented by a cli command. */
1339 /* Must be a synchronous one. */
1340 mi_execute_cli_command (parse->cmd->cli.cmd, parse->cmd->cli.args_p,
1345 /* FIXME: DELETE THIS. */
1346 struct ui_file *stb;
1348 stb = mem_fileopen ();
1350 fputs_unfiltered ("Undefined mi command: ", stb);
1351 fputstr_unfiltered (parse->command, '"', stb);
1352 fputs_unfiltered (" (missing implementation)", stb);
1354 make_cleanup_ui_file_delete (stb);
1357 do_cleanups (cleanup);
1360 /* FIXME: This is just a hack so we can get some extra commands going.
1361 We don't want to channel things through the CLI, but call libgdb directly.
1362 Use only for synchronous commands. */
1365 mi_execute_cli_command (const char *cmd, int args_p, const char *args)
1369 struct cleanup *old_cleanups;
1372 run = xstrprintf ("%s %s", cmd, args);
1374 run = xstrdup (cmd);
1376 /* FIXME: gdb_???? */
1377 fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
1379 old_cleanups = make_cleanup (xfree, run);
1380 execute_command ( /*ui */ run, 0 /*from_tty */ );
1381 do_cleanups (old_cleanups);
1387 mi_execute_async_cli_command (char *cli_command, char **argv, int argc)
1389 struct cleanup *old_cleanups;
1392 if (target_can_async_p ())
1393 run = xstrprintf ("%s %s&", cli_command, argc ? *argv : "");
1395 run = xstrprintf ("%s %s", cli_command, argc ? *argv : "");
1396 old_cleanups = make_cleanup (xfree, run);
1398 execute_command ( /*ui */ run, 0 /*from_tty */ );
1400 if (target_can_async_p ())
1402 /* If we're not executing, an exception should have been throw. */
1403 gdb_assert (is_running (inferior_ptid));
1404 do_cleanups (old_cleanups);
1408 /* Do this before doing any printing. It would appear that some
1409 print code leaves garbage around in the buffer. */
1410 do_cleanups (old_cleanups);
1412 print_diff_now (current_command_ts);
1417 mi_load_progress (const char *section_name,
1418 unsigned long sent_so_far,
1419 unsigned long total_section,
1420 unsigned long total_sent,
1421 unsigned long grand_total)
1423 struct timeval time_now, delta, update_threshold;
1424 static struct timeval last_update;
1425 static char *previous_sect_name = NULL;
1427 struct ui_out *saved_uiout;
1429 /* This function is called through deprecated_show_load_progress
1430 which means uiout may not be correct. Fix it for the duration
1431 of this function. */
1432 saved_uiout = uiout;
1434 if (current_interp_named_p (INTERP_MI)
1435 || current_interp_named_p (INTERP_MI2))
1436 uiout = mi_out_new (2);
1437 else if (current_interp_named_p (INTERP_MI1))
1438 uiout = mi_out_new (1);
1439 else if (current_interp_named_p (INTERP_MI3))
1440 uiout = mi_out_new (3);
1444 update_threshold.tv_sec = 0;
1445 update_threshold.tv_usec = 500000;
1446 gettimeofday (&time_now, NULL);
1448 delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
1449 delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
1451 if (delta.tv_usec < 0)
1454 delta.tv_usec += 1000000L;
1457 new_section = (previous_sect_name ?
1458 strcmp (previous_sect_name, section_name) : 1);
1461 struct cleanup *cleanup_tuple;
1462 xfree (previous_sect_name);
1463 previous_sect_name = xstrdup (section_name);
1466 fputs_unfiltered (current_token, raw_stdout);
1467 fputs_unfiltered ("+download", raw_stdout);
1468 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1469 ui_out_field_string (uiout, "section", section_name);
1470 ui_out_field_int (uiout, "section-size", total_section);
1471 ui_out_field_int (uiout, "total-size", grand_total);
1472 do_cleanups (cleanup_tuple);
1473 mi_out_put (uiout, raw_stdout);
1474 fputs_unfiltered ("\n", raw_stdout);
1475 gdb_flush (raw_stdout);
1478 if (delta.tv_sec >= update_threshold.tv_sec &&
1479 delta.tv_usec >= update_threshold.tv_usec)
1481 struct cleanup *cleanup_tuple;
1482 last_update.tv_sec = time_now.tv_sec;
1483 last_update.tv_usec = time_now.tv_usec;
1485 fputs_unfiltered (current_token, raw_stdout);
1486 fputs_unfiltered ("+download", raw_stdout);
1487 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1488 ui_out_field_string (uiout, "section", section_name);
1489 ui_out_field_int (uiout, "section-sent", sent_so_far);
1490 ui_out_field_int (uiout, "section-size", total_section);
1491 ui_out_field_int (uiout, "total-sent", total_sent);
1492 ui_out_field_int (uiout, "total-size", grand_total);
1493 do_cleanups (cleanup_tuple);
1494 mi_out_put (uiout, raw_stdout);
1495 fputs_unfiltered ("\n", raw_stdout);
1496 gdb_flush (raw_stdout);
1500 uiout = saved_uiout;
1504 timestamp (struct mi_timestamp *tv)
1507 gettimeofday (&tv->wallclock, NULL);
1508 #ifdef HAVE_GETRUSAGE
1509 getrusage (RUSAGE_SELF, &rusage);
1510 tv->utime.tv_sec = rusage.ru_utime.tv_sec;
1511 tv->utime.tv_usec = rusage.ru_utime.tv_usec;
1512 tv->stime.tv_sec = rusage.ru_stime.tv_sec;
1513 tv->stime.tv_usec = rusage.ru_stime.tv_usec;
1515 usec = get_run_time ();
1516 tv->utime.tv_sec = usec/1000000L;
1517 tv->utime.tv_usec = usec - 1000000L*tv->utime.tv_sec;
1518 tv->stime.tv_sec = 0;
1519 tv->stime.tv_usec = 0;
1524 print_diff_now (struct mi_timestamp *start)
1526 struct mi_timestamp now;
1528 print_diff (start, &now);
1532 timeval_diff (struct timeval start, struct timeval end)
1534 return ((end.tv_sec - start.tv_sec) * 1000000L)
1535 + (end.tv_usec - start.tv_usec);
1539 print_diff (struct mi_timestamp *start, struct mi_timestamp *end)
1543 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1544 timeval_diff (start->wallclock, end->wallclock) / 1000000.0,
1545 timeval_diff (start->utime, end->utime) / 1000000.0,
1546 timeval_diff (start->stime, end->stime) / 1000000.0);