3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
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. */
26 #include "arch-utils.h"
29 #include "gdb_string.h"
30 #include "exceptions.h"
32 #include "gdbthread.h"
35 #include "mi-getopt.h"
36 #include "mi-console.h"
40 #include "event-loop.h"
41 #include "event-top.h"
42 #include "gdbcore.h" /* For write_memory(). */
48 #include "mi-common.h"
57 #if defined HAVE_SYS_RESOURCE_H
58 #include <sys/resource.h>
71 struct ui_file *raw_stdout;
73 /* This is used to pass the current command timestamp
74 down to continuation routines. */
75 static struct mi_timestamp *current_command_ts;
77 static int do_timings = 0;
80 int running_result_record_printed = 1;
82 /* Flag indicating that the target has proceeded since the last
83 command was issued. */
86 extern void _initialize_mi_main (void);
87 static void mi_cmd_execute (struct mi_parse *parse);
89 static void mi_execute_cli_command (const char *cmd, int args_p,
91 static void mi_execute_async_cli_command (char *cli_command,
92 char **argv, int argc);
93 static int register_changed_p (int regnum, struct regcache *,
95 static void get_register (struct frame_info *, int regnum, int format);
97 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
98 layer that calls libgdb. Any operation used in the below should be
101 static void timestamp (struct mi_timestamp *tv);
103 static void print_diff_now (struct mi_timestamp *start);
104 static void print_diff (struct mi_timestamp *start, struct mi_timestamp *end);
107 mi_cmd_gdb_exit (char *command, char **argv, int argc)
109 /* We have to print everything right here because we never return. */
111 fputs_unfiltered (current_token, raw_stdout);
112 fputs_unfiltered ("^exit\n", raw_stdout);
113 mi_out_put (uiout, raw_stdout);
114 gdb_flush (raw_stdout);
115 /* FIXME: The function called is not yet a formal libgdb function. */
116 quit_force (NULL, FROM_TTY);
120 mi_cmd_exec_next (char *command, char **argv, int argc)
122 /* FIXME: Should call a libgdb function, not a cli wrapper. */
123 mi_execute_async_cli_command ("next", argv, argc);
127 mi_cmd_exec_next_instruction (char *command, char **argv, int argc)
129 /* FIXME: Should call a libgdb function, not a cli wrapper. */
130 mi_execute_async_cli_command ("nexti", argv, argc);
134 mi_cmd_exec_step (char *command, char **argv, int argc)
136 /* FIXME: Should call a libgdb function, not a cli wrapper. */
137 mi_execute_async_cli_command ("step", argv, argc);
141 mi_cmd_exec_step_instruction (char *command, char **argv, int argc)
143 /* FIXME: Should call a libgdb function, not a cli wrapper. */
144 mi_execute_async_cli_command ("stepi", argv, argc);
148 mi_cmd_exec_finish (char *command, char **argv, int argc)
150 /* FIXME: Should call a libgdb function, not a cli wrapper. */
151 mi_execute_async_cli_command ("finish", argv, argc);
155 mi_cmd_exec_return (char *command, char **argv, int argc)
157 /* This command doesn't really execute the target, it just pops the
158 specified number of frames. */
160 /* Call return_command with from_tty argument equal to 0 so as to
161 avoid being queried. */
162 return_command (*argv, 0);
164 /* Call return_command with from_tty argument equal to 0 so as to
165 avoid being queried. */
166 return_command (NULL, 0);
168 /* Because we have called return_command with from_tty = 0, we need
169 to print the frame here. */
170 print_stack_frame (get_selected_frame (NULL), 1, LOC_AND_ADDRESS);
174 mi_cmd_exec_jump (char *args, char **argv, int argc)
176 /* FIXME: Should call a libgdb function, not a cli wrapper. */
177 return mi_execute_async_cli_command ("jump", argv, argc);
181 proceed_thread_callback (struct thread_info *thread, void *arg)
183 int pid = *(int *)arg;
185 if (!is_stopped (thread->ptid))
188 if (PIDGET (thread->ptid) != pid)
191 switch_to_thread (thread->ptid);
192 clear_proceed_status ();
193 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
198 mi_cmd_exec_continue (char *command, char **argv, int argc)
202 else if (argc == 1 && strcmp (argv[0], "--all") == 0)
204 else if (argc == 2 && strcmp (argv[0], "--thread-group") == 0)
206 struct cleanup *old_chain;
208 if (argv[1] == NULL || argv[1] == '\0')
209 error ("Thread group id not specified");
210 pid = atoi (argv[1]);
211 if (!in_inferior_list (pid))
212 error ("Invalid thread group id '%s'", argv[1]);
214 old_chain = make_cleanup_restore_current_thread ();
215 iterate_over_threads (proceed_thread_callback, &pid);
216 do_cleanups (old_chain);
219 error ("Usage: -exec-continue [--all|--thread-group id]");
223 interrupt_thread_callback (struct thread_info *thread, void *arg)
225 int pid = *(int *)arg;
227 if (!is_running (thread->ptid))
230 if (PIDGET (thread->ptid) != pid)
233 target_stop (thread->ptid);
237 /* Interrupt the execution of the target. Note how we must play around
238 with the token variables, in order to display the current token in
239 the result of the interrupt command, and the previous execution
240 token when the target finally stops. See comments in
243 mi_cmd_exec_interrupt (char *command, char **argv, int argc)
247 if (!is_running (inferior_ptid))
248 error ("Current thread is not running.");
250 interrupt_target_1 (0);
252 else if (argc == 1 && strcmp (argv[0], "--all") == 0)
255 error ("Inferior not running.");
257 interrupt_target_1 (1);
259 else if (argc == 2 && strcmp (argv[0], "--thread-group") == 0)
261 struct cleanup *old_chain;
263 if (argv[1] == NULL || argv[1] == '\0')
264 error ("Thread group id not specified");
265 pid = atoi (argv[1]);
266 if (!in_inferior_list (pid))
267 error ("Invalid thread group id '%s'", argv[1]);
269 old_chain = make_cleanup_restore_current_thread ();
270 iterate_over_threads (interrupt_thread_callback, &pid);
271 do_cleanups (old_chain);
274 error ("Usage: -exec-interrupt [--all|--thread-group id]");
278 find_thread_of_process (struct thread_info *ti, void *p)
281 if (PIDGET (ti->ptid) == pid && !is_exited (ti->ptid))
288 mi_cmd_target_detach (char *command, char **argv, int argc)
290 if (argc != 0 && argc != 1)
291 error ("Usage: -target-detach [thread-group]");
295 struct thread_info *tp;
297 int pid = strtol (argv[0], &end, 10);
299 error (_("Cannot parse thread group id '%s'"), argv[0]);
301 /* Pick any thread in the desired process. Current
302 target_detach deteches from the parent of inferior_ptid. */
303 tp = iterate_over_threads (find_thread_of_process, &pid);
305 error (_("Thread group is empty"));
307 switch_to_thread (tp->ptid);
310 detach_command (NULL, 0);
314 mi_cmd_thread_select (char *command, char **argv, int argc)
317 char *mi_error_message;
320 error ("mi_cmd_thread_select: USAGE: threadnum.");
322 rc = gdb_thread_select (uiout, argv[0], &mi_error_message);
324 if (rc == GDB_RC_FAIL)
326 make_cleanup (xfree, mi_error_message);
327 error ("%s", mi_error_message);
332 mi_cmd_thread_list_ids (char *command, char **argv, int argc)
335 char *mi_error_message;
338 error ("mi_cmd_thread_list_ids: No arguments required.");
340 rc = gdb_list_thread_ids (uiout, &mi_error_message);
342 if (rc == GDB_RC_FAIL)
344 make_cleanup (xfree, mi_error_message);
345 error ("%s", mi_error_message);
350 mi_cmd_thread_info (char *command, char **argv, int argc)
354 if (argc != 0 && argc != 1)
355 error ("Invalid MI command");
358 thread = atoi (argv[0]);
360 print_thread_info (uiout, thread, -1);
364 print_one_inferior (struct inferior *inferior, void *arg)
366 if (inferior->pid != 0)
368 struct cleanup *back_to
369 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
371 ui_out_field_fmt (uiout, "id", "%d", inferior->pid);
372 ui_out_field_string (uiout, "type", "process");
373 ui_out_field_int (uiout, "pid", inferior->pid);
375 do_cleanups (back_to);
382 mi_cmd_list_thread_groups (char *command, char **argv, int argc)
384 struct cleanup *back_to;
388 if (argc > 0 && strcmp (argv[0], "--available") == 0)
398 back_to = make_cleanup (null_cleanup, NULL);
402 error (_("Can only report top-level available thread groups"));
407 struct osdata_item *item;
410 data = get_osdata ("processes");
411 make_cleanup_osdata_free (data);
413 make_cleanup_ui_out_list_begin_end (uiout, "groups");
416 VEC_iterate (osdata_item_s, data->items,
420 struct cleanup *back_to =
421 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
423 const char *pid = get_osdata_column (item, "pid");
424 const char *cmd = get_osdata_column (item, "command");
425 const char *user = get_osdata_column (item, "user");
427 ui_out_field_fmt (uiout, "id", "%s", pid);
428 ui_out_field_string (uiout, "type", "process");
430 ui_out_field_string (uiout, "description", cmd);
432 ui_out_field_string (uiout, "user", user);
434 do_cleanups (back_to);
440 if (!in_inferior_list (pid))
441 error ("Invalid thread group id '%s'", id);
442 print_thread_info (uiout, -1, pid);
446 make_cleanup_ui_out_list_begin_end (uiout, "groups");
447 iterate_over_inferiors (print_one_inferior, NULL);
450 do_cleanups (back_to);
454 mi_cmd_data_list_register_names (char *command, char **argv, int argc)
456 struct frame_info *frame;
457 struct gdbarch *gdbarch;
460 struct cleanup *cleanup;
462 /* Note that the test for a valid register must include checking the
463 gdbarch_register_name because gdbarch_num_regs may be allocated for
464 the union of the register sets within a family of related processors.
465 In this case, some entries of gdbarch_register_name will change depending
466 upon the particular processor being debugged. */
468 frame = get_selected_frame (NULL);
469 gdbarch = get_frame_arch (frame);
470 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
472 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names");
474 if (argc == 0) /* No args, just do all the regs. */
480 if (gdbarch_register_name (gdbarch, regnum) == NULL
481 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
482 ui_out_field_string (uiout, NULL, "");
484 ui_out_field_string (uiout, NULL,
485 gdbarch_register_name (gdbarch, regnum));
489 /* Else, list of register #s, just do listed regs. */
490 for (i = 0; i < argc; i++)
492 regnum = atoi (argv[i]);
493 if (regnum < 0 || regnum >= numregs)
494 error ("bad register number");
496 if (gdbarch_register_name (gdbarch, regnum) == NULL
497 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
498 ui_out_field_string (uiout, NULL, "");
500 ui_out_field_string (uiout, NULL,
501 gdbarch_register_name (gdbarch, regnum));
503 do_cleanups (cleanup);
507 mi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
509 static struct regcache *this_regs = NULL;
510 struct regcache *prev_regs;
511 struct gdbarch *gdbarch;
512 int regnum, numregs, changed;
514 struct cleanup *cleanup;
516 /* The last time we visited this function, the current frame's register
517 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
518 and refresh THIS_REGS with the now-current register contents. */
520 prev_regs = this_regs;
521 this_regs = frame_save_as_regcache (get_selected_frame (NULL));
522 cleanup = make_cleanup_regcache_xfree (prev_regs);
524 /* Note that the test for a valid register must include checking the
525 gdbarch_register_name because gdbarch_num_regs may be allocated for
526 the union of the register sets within a family of related processors.
527 In this case, some entries of gdbarch_register_name will change depending
528 upon the particular processor being debugged. */
530 gdbarch = get_regcache_arch (this_regs);
531 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
533 make_cleanup_ui_out_list_begin_end (uiout, "changed-registers");
535 if (argc == 0) /* No args, just do all the regs. */
541 if (gdbarch_register_name (gdbarch, regnum) == NULL
542 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
544 changed = register_changed_p (regnum, prev_regs, this_regs);
546 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
548 ui_out_field_int (uiout, NULL, regnum);
552 /* Else, list of register #s, just do listed regs. */
553 for (i = 0; i < argc; i++)
555 regnum = atoi (argv[i]);
559 && gdbarch_register_name (gdbarch, regnum) != NULL
560 && *gdbarch_register_name (gdbarch, regnum) != '\000')
562 changed = register_changed_p (regnum, prev_regs, this_regs);
564 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
566 ui_out_field_int (uiout, NULL, regnum);
569 error ("bad register number");
571 do_cleanups (cleanup);
575 register_changed_p (int regnum, struct regcache *prev_regs,
576 struct regcache *this_regs)
578 struct gdbarch *gdbarch = get_regcache_arch (this_regs);
579 gdb_byte prev_buffer[MAX_REGISTER_SIZE];
580 gdb_byte this_buffer[MAX_REGISTER_SIZE];
582 /* Registers not valid in this frame return count as unchanged. */
583 if (!regcache_valid_p (this_regs, regnum))
586 /* First time through or after gdbarch change consider all registers as
587 changed. Same for registers not valid in the previous frame. */
588 if (!prev_regs || get_regcache_arch (prev_regs) != gdbarch
589 || !regcache_valid_p (prev_regs, regnum))
592 /* Get register contents and compare. */
593 regcache_cooked_read (prev_regs, regnum, prev_buffer);
594 regcache_cooked_read (this_regs, regnum, this_buffer);
596 return memcmp (prev_buffer, this_buffer,
597 register_size (gdbarch, regnum)) != 0;
600 /* Return a list of register number and value pairs. The valid
601 arguments expected are: a letter indicating the format in which to
602 display the registers contents. This can be one of: x (hexadecimal), d
603 (decimal), N (natural), t (binary), o (octal), r (raw). After the
604 format argumetn there can be a sequence of numbers, indicating which
605 registers to fetch the content of. If the format is the only argument,
606 a list of all the registers with their values is returned. */
608 mi_cmd_data_list_register_values (char *command, char **argv, int argc)
610 struct frame_info *frame;
611 struct gdbarch *gdbarch;
612 int regnum, numregs, format;
614 struct cleanup *list_cleanup, *tuple_cleanup;
616 /* Note that the test for a valid register must include checking the
617 gdbarch_register_name because gdbarch_num_regs may be allocated for
618 the union of the register sets within a family of related processors.
619 In this case, some entries of gdbarch_register_name will change depending
620 upon the particular processor being debugged. */
623 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
625 format = (int) argv[0][0];
627 frame = get_selected_frame (NULL);
628 gdbarch = get_frame_arch (frame);
629 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
631 list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values");
633 if (argc == 1) /* No args, beside the format: do all the regs. */
639 if (gdbarch_register_name (gdbarch, regnum) == NULL
640 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
642 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
643 ui_out_field_int (uiout, "number", regnum);
644 get_register (frame, regnum, format);
645 do_cleanups (tuple_cleanup);
649 /* Else, list of register #s, just do listed regs. */
650 for (i = 1; i < argc; i++)
652 regnum = atoi (argv[i]);
656 && gdbarch_register_name (gdbarch, regnum) != NULL
657 && *gdbarch_register_name (gdbarch, regnum) != '\000')
659 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
660 ui_out_field_int (uiout, "number", regnum);
661 get_register (frame, regnum, format);
662 do_cleanups (tuple_cleanup);
665 error ("bad register number");
667 do_cleanups (list_cleanup);
670 /* Output one register's contents in the desired format. */
672 get_register (struct frame_info *frame, int regnum, int format)
674 struct gdbarch *gdbarch = get_frame_arch (frame);
675 gdb_byte buffer[MAX_REGISTER_SIZE];
680 static struct ui_stream *stb = NULL;
682 stb = ui_out_stream_new (uiout);
687 frame_register (frame, regnum, &optim, &lval, &addr, &realnum, buffer);
690 error ("Optimized out");
695 char *ptr, buf[1024];
699 for (j = 0; j < register_size (gdbarch, regnum); j++)
701 int idx = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ?
702 j : register_size (gdbarch, regnum) - 1 - j;
703 sprintf (ptr, "%02x", (unsigned char) buffer[idx]);
706 ui_out_field_string (uiout, "value", buf);
707 /*fputs_filtered (buf, gdb_stdout); */
711 struct value_print_options opts;
712 get_formatted_print_options (&opts, format);
714 val_print (register_type (gdbarch, regnum), buffer, 0, 0,
715 stb->stream, 0, &opts, current_language);
716 ui_out_field_stream (uiout, "value", stb);
717 ui_out_stream_delete (stb);
721 /* Write given values into registers. The registers and values are
722 given as pairs. The corresponding MI command is
723 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
725 mi_cmd_data_write_register_values (char *command, char **argv, int argc)
727 struct regcache *regcache;
728 struct gdbarch *gdbarch;
732 /* Note that the test for a valid register must include checking the
733 gdbarch_register_name because gdbarch_num_regs may be allocated for
734 the union of the register sets within a family of related processors.
735 In this case, some entries of gdbarch_register_name will change depending
736 upon the particular processor being debugged. */
738 regcache = get_current_regcache ();
739 gdbarch = get_regcache_arch (regcache);
740 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
743 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
745 format = (int) argv[0][0];
747 if (!target_has_registers)
748 error ("mi_cmd_data_write_register_values: No registers.");
751 error ("mi_cmd_data_write_register_values: No regs and values specified.");
754 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
756 for (i = 1; i < argc; i = i + 2)
758 int regnum = atoi (argv[i]);
760 if (regnum >= 0 && regnum < numregs
761 && gdbarch_register_name (gdbarch, regnum)
762 && *gdbarch_register_name (gdbarch, regnum))
766 /* Get the value as a number. */
767 value = parse_and_eval_address (argv[i + 1]);
770 regcache_cooked_write_signed (regcache, regnum, value);
773 error ("bad register number");
777 /* Evaluate the value of the argument. The argument is an
778 expression. If the expression contains spaces it needs to be
779 included in double quotes. */
781 mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
783 struct expression *expr;
784 struct cleanup *old_chain = NULL;
786 struct ui_stream *stb = NULL;
787 struct value_print_options opts;
789 stb = ui_out_stream_new (uiout);
793 ui_out_stream_delete (stb);
794 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
797 expr = parse_expression (argv[0]);
799 old_chain = make_cleanup (free_current_contents, &expr);
801 val = evaluate_expression (expr);
803 /* Print the result of the expression evaluation. */
804 get_user_print_options (&opts);
806 val_print (value_type (val), value_contents (val),
807 value_embedded_offset (val), value_address (val),
808 stb->stream, 0, &opts, current_language);
810 ui_out_field_stream (uiout, "value", stb);
811 ui_out_stream_delete (stb);
813 do_cleanups (old_chain);
818 ADDR: start address of data to be dumped.
819 WORD-FORMAT: a char indicating format for the ``word''. See
821 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
822 NR_ROW: Number of rows.
823 NR_COL: The number of colums (words per row).
824 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
825 ASCHAR for unprintable characters.
827 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
828 displayes them. Returns:
830 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
833 The number of bytes read is SIZE*ROW*COL. */
836 mi_cmd_data_read_memory (char *command, char **argv, int argc)
838 struct gdbarch *gdbarch = get_current_arch ();
839 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
845 struct type *word_type;
858 static struct mi_opt opts[] =
860 {"o", OFFSET_OPT, 1},
866 int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts,
870 switch ((enum opt) opt)
873 offset = atol (optarg);
880 if (argc < 5 || argc > 6)
881 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
883 /* Extract all the arguments. */
885 /* Start address of the memory dump. */
886 addr = parse_and_eval_address (argv[0]) + offset;
887 /* The format character to use when displaying a memory word. See
888 the ``x'' command. */
889 word_format = argv[1][0];
890 /* The size of the memory word. */
891 word_size = atol (argv[2]);
895 word_type = builtin_type (gdbarch)->builtin_int8;
899 word_type = builtin_type (gdbarch)->builtin_int16;
903 word_type = builtin_type (gdbarch)->builtin_int32;
907 word_type = builtin_type (gdbarch)->builtin_int64;
911 word_type = builtin_type (gdbarch)->builtin_int8;
914 /* The number of rows. */
915 nr_rows = atol (argv[3]);
917 error ("mi_cmd_data_read_memory: invalid number of rows.");
919 /* Number of bytes per row. */
920 nr_cols = atol (argv[4]);
922 error ("mi_cmd_data_read_memory: invalid number of columns.");
924 /* The un-printable character when printing ascii. */
930 /* Create a buffer and read it in. */
931 total_bytes = word_size * nr_rows * nr_cols;
932 mbuf = xcalloc (total_bytes, 1);
933 make_cleanup (xfree, mbuf);
935 /* Dispatch memory reads to the topmost target, not the flattened
937 nr_bytes = target_read_until_error (current_target.beneath,
938 TARGET_OBJECT_MEMORY, NULL, mbuf,
941 error ("Unable to read memory.");
943 /* Output the header information. */
944 ui_out_field_core_addr (uiout, "addr", gdbarch, addr);
945 ui_out_field_int (uiout, "nr-bytes", nr_bytes);
946 ui_out_field_int (uiout, "total-bytes", total_bytes);
947 ui_out_field_core_addr (uiout, "next-row",
948 gdbarch, addr + word_size * nr_cols);
949 ui_out_field_core_addr (uiout, "prev-row",
950 gdbarch, addr - word_size * nr_cols);
951 ui_out_field_core_addr (uiout, "next-page", gdbarch, addr + total_bytes);
952 ui_out_field_core_addr (uiout, "prev-page", gdbarch, addr - total_bytes);
954 /* Build the result as a two dimentional table. */
956 struct ui_stream *stream = ui_out_stream_new (uiout);
957 struct cleanup *cleanup_list_memory;
960 cleanup_list_memory = make_cleanup_ui_out_list_begin_end (uiout, "memory");
961 for (row = 0, row_byte = 0;
963 row++, row_byte += nr_cols * word_size)
967 struct cleanup *cleanup_tuple;
968 struct cleanup *cleanup_list_data;
969 struct value_print_options opts;
971 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
972 ui_out_field_core_addr (uiout, "addr", gdbarch, addr + row_byte);
973 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
974 cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
975 get_formatted_print_options (&opts, word_format);
976 for (col = 0, col_byte = row_byte;
978 col++, col_byte += word_size)
980 if (col_byte + word_size > nr_bytes)
982 ui_out_field_string (uiout, NULL, "N/A");
986 ui_file_rewind (stream->stream);
987 print_scalar_formatted (mbuf + col_byte, word_type, &opts,
988 word_asize, stream->stream);
989 ui_out_field_stream (uiout, NULL, stream);
992 do_cleanups (cleanup_list_data);
996 ui_file_rewind (stream->stream);
997 for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
999 if (byte >= nr_bytes)
1001 fputc_unfiltered ('X', stream->stream);
1003 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
1005 fputc_unfiltered (aschar, stream->stream);
1008 fputc_unfiltered (mbuf[byte], stream->stream);
1010 ui_out_field_stream (uiout, "ascii", stream);
1012 do_cleanups (cleanup_tuple);
1014 ui_out_stream_delete (stream);
1015 do_cleanups (cleanup_list_memory);
1017 do_cleanups (cleanups);
1020 /* DATA-MEMORY-WRITE:
1022 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
1023 offset from the beginning of the memory grid row where the cell to
1025 ADDR: start address of the row in the memory grid where the memory
1026 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
1027 the location to write to.
1028 FORMAT: a char indicating format for the ``word''. See
1030 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
1031 VALUE: value to be written into the memory address.
1033 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1037 mi_cmd_data_write_memory (char *command, char **argv, int argc)
1039 struct gdbarch *gdbarch = get_current_arch ();
1040 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1044 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1045 enough when using a compiler other than GCC. */
1048 struct cleanup *old_chain;
1056 static struct mi_opt opts[] =
1058 {"o", OFFSET_OPT, 1},
1064 int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
1068 switch ((enum opt) opt)
1071 offset = atol (optarg);
1079 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1081 /* Extract all the arguments. */
1082 /* Start address of the memory dump. */
1083 addr = parse_and_eval_address (argv[0]);
1084 /* The format character to use when displaying a memory word. See
1085 the ``x'' command. */
1086 word_format = argv[1][0];
1087 /* The size of the memory word. */
1088 word_size = atol (argv[2]);
1090 /* Calculate the real address of the write destination. */
1091 addr += (offset * word_size);
1093 /* Get the value as a number. */
1094 value = parse_and_eval_address (argv[3]);
1095 /* Get the value into an array. */
1096 buffer = xmalloc (word_size);
1097 old_chain = make_cleanup (xfree, buffer);
1098 store_signed_integer (buffer, word_size, byte_order, value);
1099 /* Write it down to memory. */
1100 write_memory (addr, buffer, word_size);
1101 /* Free the buffer. */
1102 do_cleanups (old_chain);
1106 mi_cmd_enable_timings (char *command, char **argv, int argc)
1112 if (strcmp (argv[0], "yes") == 0)
1114 else if (strcmp (argv[0], "no") == 0)
1125 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command);
1129 mi_cmd_list_features (char *command, char **argv, int argc)
1133 struct cleanup *cleanup = NULL;
1134 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
1136 ui_out_field_string (uiout, NULL, "frozen-varobjs");
1137 ui_out_field_string (uiout, NULL, "pending-breakpoints");
1138 ui_out_field_string (uiout, NULL, "thread-info");
1141 ui_out_field_string (uiout, NULL, "python");
1144 do_cleanups (cleanup);
1148 error ("-list-features should be passed no arguments");
1152 mi_cmd_list_target_features (char *command, char **argv, int argc)
1156 struct cleanup *cleanup = NULL;
1157 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
1159 if (target_can_async_p ())
1160 ui_out_field_string (uiout, NULL, "async");
1162 do_cleanups (cleanup);
1166 error ("-list-target-features should be passed no arguments");
1169 /* Execute a command within a safe environment.
1170 Return <0 for error; >=0 for ok.
1172 args->action will tell mi_execute_command what action
1173 to perfrom after the given command has executed (display/suppress
1174 prompt, display error). */
1177 captured_mi_execute_command (struct ui_out *uiout, void *data)
1179 struct cleanup *cleanup;
1180 struct mi_parse *context = (struct mi_parse *) data;
1183 current_command_ts = context->cmd_start;
1185 current_token = xstrdup (context->token);
1186 cleanup = make_cleanup (free_current_contents, ¤t_token);
1188 running_result_record_printed = 0;
1190 switch (context->op)
1193 /* A MI command was read from the input stream. */
1195 /* FIXME: gdb_???? */
1196 fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
1197 context->token, context->command, context->args);
1200 mi_cmd_execute (context);
1202 /* Print the result if there were no errors.
1204 Remember that on the way out of executing a command, you have
1205 to directly use the mi_interp's uiout, since the command could
1206 have reset the interpreter, in which case the current uiout
1207 will most likely crash in the mi_out_* routines. */
1208 if (!running_result_record_printed)
1210 fputs_unfiltered (context->token, raw_stdout);
1211 /* There's no particularly good reason why target-connect results
1212 in not ^done. Should kill ^connected for MI3. */
1213 fputs_unfiltered (strcmp (context->command, "target-select") == 0
1214 ? "^connected" : "^done", raw_stdout);
1215 mi_out_put (uiout, raw_stdout);
1216 mi_out_rewind (uiout);
1217 mi_print_timing_maybe ();
1218 fputs_unfiltered ("\n", raw_stdout);
1221 /* The command does not want anything to be printed. In that
1222 case, the command probably should not have written anything
1223 to uiout, but in case it has written something, discard it. */
1224 mi_out_rewind (uiout);
1230 /* A CLI command was read from the input stream. */
1231 /* This "feature" will be removed as soon as we have a
1232 complete set of mi commands. */
1233 /* Echo the command on the console. */
1234 fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1235 /* Call the "console" interpreter. */
1236 argv[0] = "console";
1237 argv[1] = context->command;
1238 mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);
1240 /* If we changed interpreters, DON'T print out anything. */
1241 if (current_interp_named_p (INTERP_MI)
1242 || current_interp_named_p (INTERP_MI1)
1243 || current_interp_named_p (INTERP_MI2)
1244 || current_interp_named_p (INTERP_MI3))
1246 if (!running_result_record_printed)
1248 fputs_unfiltered (context->token, raw_stdout);
1249 fputs_unfiltered ("^done", raw_stdout);
1250 mi_out_put (uiout, raw_stdout);
1251 mi_out_rewind (uiout);
1252 mi_print_timing_maybe ();
1253 fputs_unfiltered ("\n", raw_stdout);
1256 mi_out_rewind (uiout);
1263 do_cleanups (cleanup);
1270 mi_execute_command (char *cmd, int from_tty)
1272 struct mi_parse *command;
1273 struct ui_out *saved_uiout = uiout;
1275 /* This is to handle EOF (^D). We just quit gdb. */
1276 /* FIXME: we should call some API function here. */
1278 quit_force (NULL, from_tty);
1280 command = mi_parse (cmd);
1282 if (command != NULL)
1284 struct gdb_exception result;
1285 ptid_t previous_ptid = inferior_ptid;
1289 command->cmd_start = (struct mi_timestamp *)
1290 xmalloc (sizeof (struct mi_timestamp));
1291 timestamp (command->cmd_start);
1294 result = catch_exception (uiout, captured_mi_execute_command, command,
1296 if (result.reason < 0)
1298 /* The command execution failed and error() was called
1300 fputs_unfiltered (command->token, raw_stdout);
1301 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1302 if (result.message == NULL)
1303 fputs_unfiltered ("unknown error", raw_stdout);
1305 fputstr_unfiltered (result.message, '"', raw_stdout);
1306 fputs_unfiltered ("\"\n", raw_stdout);
1307 mi_out_rewind (uiout);
1310 bpstat_do_actions ();
1312 if (/* The notifications are only output when the top-level
1313 interpreter (specified on the command line) is MI. */
1314 ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
1315 /* Don't try report anything if there are no threads --
1316 the program is dead. */
1317 && thread_count () != 0
1318 /* -thread-select explicitly changes thread. If frontend uses that
1319 internally, we don't want to emit =thread-selected, since
1320 =thread-selected is supposed to indicate user's intentions. */
1321 && strcmp (command->command, "thread-select") != 0)
1323 struct mi_interp *mi = top_level_interpreter_data ();
1324 int report_change = 0;
1326 if (command->thread == -1)
1328 report_change = (!ptid_equal (previous_ptid, null_ptid)
1329 && !ptid_equal (inferior_ptid, previous_ptid)
1330 && !ptid_equal (inferior_ptid, null_ptid));
1332 else if (!ptid_equal (inferior_ptid, null_ptid))
1334 struct thread_info *ti = inferior_thread ();
1335 report_change = (ti->num != command->thread);
1340 struct thread_info *ti = inferior_thread ();
1341 target_terminal_ours ();
1342 fprintf_unfiltered (mi->event_channel,
1343 "thread-selected,id=\"%d\"",
1345 gdb_flush (mi->event_channel);
1349 mi_parse_free (command);
1352 fputs_unfiltered ("(gdb) \n", raw_stdout);
1353 gdb_flush (raw_stdout);
1354 /* Print any buffered hook code. */
1359 mi_cmd_execute (struct mi_parse *parse)
1361 struct cleanup *cleanup;
1364 prepare_execute_command ();
1366 cleanup = make_cleanup (null_cleanup, NULL);
1368 if (parse->frame != -1 && parse->thread == -1)
1369 error (_("Cannot specify --frame without --thread"));
1371 if (parse->thread != -1)
1373 struct thread_info *tp = find_thread_id (parse->thread);
1375 error (_("Invalid thread id: %d"), parse->thread);
1377 if (is_exited (tp->ptid))
1378 error (_("Thread id: %d has terminated"), parse->thread);
1380 switch_to_thread (tp->ptid);
1383 if (parse->frame != -1)
1385 struct frame_info *fid;
1386 int frame = parse->frame;
1387 fid = find_relative_frame (get_current_frame (), &frame);
1389 /* find_relative_frame was successful */
1392 error (_("Invalid frame id: %d"), frame);
1395 if (parse->cmd->argv_func != NULL)
1396 parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
1397 else if (parse->cmd->cli.cmd != 0)
1399 /* FIXME: DELETE THIS. */
1400 /* The operation is still implemented by a cli command. */
1401 /* Must be a synchronous one. */
1402 mi_execute_cli_command (parse->cmd->cli.cmd, parse->cmd->cli.args_p,
1407 /* FIXME: DELETE THIS. */
1408 struct ui_file *stb;
1410 stb = mem_fileopen ();
1412 fputs_unfiltered ("Undefined mi command: ", stb);
1413 fputstr_unfiltered (parse->command, '"', stb);
1414 fputs_unfiltered (" (missing implementation)", stb);
1416 make_cleanup_ui_file_delete (stb);
1419 do_cleanups (cleanup);
1422 /* FIXME: This is just a hack so we can get some extra commands going.
1423 We don't want to channel things through the CLI, but call libgdb directly.
1424 Use only for synchronous commands. */
1427 mi_execute_cli_command (const char *cmd, int args_p, const char *args)
1431 struct cleanup *old_cleanups;
1434 run = xstrprintf ("%s %s", cmd, args);
1436 run = xstrdup (cmd);
1438 /* FIXME: gdb_???? */
1439 fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
1441 old_cleanups = make_cleanup (xfree, run);
1442 execute_command ( /*ui */ run, 0 /*from_tty */ );
1443 do_cleanups (old_cleanups);
1449 mi_execute_async_cli_command (char *cli_command, char **argv, int argc)
1451 struct cleanup *old_cleanups;
1454 if (target_can_async_p ())
1455 run = xstrprintf ("%s %s&", cli_command, argc ? *argv : "");
1457 run = xstrprintf ("%s %s", cli_command, argc ? *argv : "");
1458 old_cleanups = make_cleanup (xfree, run);
1460 execute_command ( /*ui */ run, 0 /*from_tty */ );
1462 if (target_can_async_p ())
1464 /* If we're not executing, an exception should have been throw. */
1465 gdb_assert (is_running (inferior_ptid));
1466 do_cleanups (old_cleanups);
1470 /* Do this before doing any printing. It would appear that some
1471 print code leaves garbage around in the buffer. */
1472 do_cleanups (old_cleanups);
1477 mi_load_progress (const char *section_name,
1478 unsigned long sent_so_far,
1479 unsigned long total_section,
1480 unsigned long total_sent,
1481 unsigned long grand_total)
1483 struct timeval time_now, delta, update_threshold;
1484 static struct timeval last_update;
1485 static char *previous_sect_name = NULL;
1487 struct ui_out *saved_uiout;
1489 /* This function is called through deprecated_show_load_progress
1490 which means uiout may not be correct. Fix it for the duration
1491 of this function. */
1492 saved_uiout = uiout;
1494 if (current_interp_named_p (INTERP_MI)
1495 || current_interp_named_p (INTERP_MI2))
1496 uiout = mi_out_new (2);
1497 else if (current_interp_named_p (INTERP_MI1))
1498 uiout = mi_out_new (1);
1499 else if (current_interp_named_p (INTERP_MI3))
1500 uiout = mi_out_new (3);
1504 update_threshold.tv_sec = 0;
1505 update_threshold.tv_usec = 500000;
1506 gettimeofday (&time_now, NULL);
1508 delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
1509 delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
1511 if (delta.tv_usec < 0)
1514 delta.tv_usec += 1000000L;
1517 new_section = (previous_sect_name ?
1518 strcmp (previous_sect_name, section_name) : 1);
1521 struct cleanup *cleanup_tuple;
1522 xfree (previous_sect_name);
1523 previous_sect_name = xstrdup (section_name);
1526 fputs_unfiltered (current_token, raw_stdout);
1527 fputs_unfiltered ("+download", raw_stdout);
1528 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1529 ui_out_field_string (uiout, "section", section_name);
1530 ui_out_field_int (uiout, "section-size", total_section);
1531 ui_out_field_int (uiout, "total-size", grand_total);
1532 do_cleanups (cleanup_tuple);
1533 mi_out_put (uiout, raw_stdout);
1534 fputs_unfiltered ("\n", raw_stdout);
1535 gdb_flush (raw_stdout);
1538 if (delta.tv_sec >= update_threshold.tv_sec &&
1539 delta.tv_usec >= update_threshold.tv_usec)
1541 struct cleanup *cleanup_tuple;
1542 last_update.tv_sec = time_now.tv_sec;
1543 last_update.tv_usec = time_now.tv_usec;
1545 fputs_unfiltered (current_token, raw_stdout);
1546 fputs_unfiltered ("+download", raw_stdout);
1547 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1548 ui_out_field_string (uiout, "section", section_name);
1549 ui_out_field_int (uiout, "section-sent", sent_so_far);
1550 ui_out_field_int (uiout, "section-size", total_section);
1551 ui_out_field_int (uiout, "total-sent", total_sent);
1552 ui_out_field_int (uiout, "total-size", grand_total);
1553 do_cleanups (cleanup_tuple);
1554 mi_out_put (uiout, raw_stdout);
1555 fputs_unfiltered ("\n", raw_stdout);
1556 gdb_flush (raw_stdout);
1560 uiout = saved_uiout;
1564 timestamp (struct mi_timestamp *tv)
1567 gettimeofday (&tv->wallclock, NULL);
1568 #ifdef HAVE_GETRUSAGE
1569 getrusage (RUSAGE_SELF, &rusage);
1570 tv->utime.tv_sec = rusage.ru_utime.tv_sec;
1571 tv->utime.tv_usec = rusage.ru_utime.tv_usec;
1572 tv->stime.tv_sec = rusage.ru_stime.tv_sec;
1573 tv->stime.tv_usec = rusage.ru_stime.tv_usec;
1575 usec = get_run_time ();
1576 tv->utime.tv_sec = usec/1000000L;
1577 tv->utime.tv_usec = usec - 1000000L*tv->utime.tv_sec;
1578 tv->stime.tv_sec = 0;
1579 tv->stime.tv_usec = 0;
1584 print_diff_now (struct mi_timestamp *start)
1586 struct mi_timestamp now;
1588 print_diff (start, &now);
1592 mi_print_timing_maybe (void)
1594 /* If the command is -enable-timing then do_timings may be
1595 true whilst current_command_ts is not initialized. */
1596 if (do_timings && current_command_ts)
1597 print_diff_now (current_command_ts);
1601 timeval_diff (struct timeval start, struct timeval end)
1603 return ((end.tv_sec - start.tv_sec) * 1000000L)
1604 + (end.tv_usec - start.tv_usec);
1608 print_diff (struct mi_timestamp *start, struct mi_timestamp *end)
1612 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1613 timeval_diff (start->wallclock, end->wallclock) / 1000000.0,
1614 timeval_diff (start->utime, end->utime) / 1000000.0,
1615 timeval_diff (start->stime, end->stime) / 1000000.0);