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. */
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"
56 #if defined HAVE_SYS_RESOURCE_H
57 #include <sys/resource.h>
70 struct ui_file *raw_stdout;
72 /* This is used to pass the current command timestamp
73 down to continuation routines. */
74 static struct mi_timestamp *current_command_ts;
76 static int do_timings = 0;
79 int running_result_record_printed = 1;
81 /* Flag indicating that the target has proceeded since the last
82 command was issued. */
85 extern void _initialize_mi_main (void);
86 static void mi_cmd_execute (struct mi_parse *parse);
88 static void mi_execute_cli_command (const char *cmd, int args_p,
90 static void mi_execute_async_cli_command (char *cli_command,
91 char **argv, int argc);
92 static int register_changed_p (int regnum, struct regcache *,
94 static void get_register (struct frame_info *, int regnum, int format);
96 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
97 layer that calls libgdb. Any operation used in the below should be
100 static void timestamp (struct mi_timestamp *tv);
102 static void print_diff_now (struct mi_timestamp *start);
103 static void print_diff (struct mi_timestamp *start, struct mi_timestamp *end);
106 mi_cmd_gdb_exit (char *command, char **argv, int argc)
108 /* We have to print everything right here because we never return. */
110 fputs_unfiltered (current_token, raw_stdout);
111 fputs_unfiltered ("^exit\n", raw_stdout);
112 mi_out_put (uiout, raw_stdout);
113 /* FIXME: The function called is not yet a formal libgdb function. */
114 quit_force (NULL, FROM_TTY);
118 mi_cmd_exec_next (char *command, char **argv, int argc)
120 /* FIXME: Should call a libgdb function, not a cli wrapper. */
121 mi_execute_async_cli_command ("next", argv, argc);
125 mi_cmd_exec_next_instruction (char *command, char **argv, int argc)
127 /* FIXME: Should call a libgdb function, not a cli wrapper. */
128 mi_execute_async_cli_command ("nexti", argv, argc);
132 mi_cmd_exec_step (char *command, char **argv, int argc)
134 /* FIXME: Should call a libgdb function, not a cli wrapper. */
135 mi_execute_async_cli_command ("step", argv, argc);
139 mi_cmd_exec_step_instruction (char *command, char **argv, int argc)
141 /* FIXME: Should call a libgdb function, not a cli wrapper. */
142 mi_execute_async_cli_command ("stepi", argv, argc);
146 mi_cmd_exec_finish (char *command, char **argv, int argc)
148 /* FIXME: Should call a libgdb function, not a cli wrapper. */
149 mi_execute_async_cli_command ("finish", argv, argc);
153 mi_cmd_exec_return (char *command, char **argv, int argc)
155 /* This command doesn't really execute the target, it just pops the
156 specified number of frames. */
158 /* Call return_command with from_tty argument equal to 0 so as to
159 avoid being queried. */
160 return_command (*argv, 0);
162 /* Call return_command with from_tty argument equal to 0 so as to
163 avoid being queried. */
164 return_command (NULL, 0);
166 /* Because we have called return_command with from_tty = 0, we need
167 to print the frame here. */
168 print_stack_frame (get_selected_frame (NULL), 1, LOC_AND_ADDRESS);
172 mi_cmd_exec_jump (char *args, char **argv, int argc)
174 /* FIXME: Should call a libgdb function, not a cli wrapper. */
175 return mi_execute_async_cli_command ("jump", argv, argc);
179 proceed_thread_callback (struct thread_info *thread, void *arg)
181 int pid = *(int *)arg;
183 if (!is_stopped (thread->ptid))
186 if (PIDGET (thread->ptid) != pid)
189 switch_to_thread (thread->ptid);
190 clear_proceed_status ();
191 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
196 mi_cmd_exec_continue (char *command, char **argv, int argc)
200 else if (argc == 1 && strcmp (argv[0], "--all") == 0)
202 else if (argc == 2 && strcmp (argv[0], "--thread-group") == 0)
204 struct cleanup *old_chain;
206 if (argv[1] == NULL || argv[1] == '\0')
207 error ("Thread group id not specified");
208 pid = atoi (argv[1]);
209 if (!in_inferior_list (pid))
210 error ("Invalid thread group id '%s'", argv[1]);
212 old_chain = make_cleanup_restore_current_thread ();
213 iterate_over_threads (proceed_thread_callback, &pid);
214 do_cleanups (old_chain);
217 error ("Usage: -exec-continue [--all|--thread-group id]");
221 interrupt_thread_callback (struct thread_info *thread, void *arg)
223 int pid = *(int *)arg;
225 if (!is_running (thread->ptid))
228 if (PIDGET (thread->ptid) != pid)
231 target_stop (thread->ptid);
235 /* Interrupt the execution of the target. Note how we must play around
236 with the token variables, in order to display the current token in
237 the result of the interrupt command, and the previous execution
238 token when the target finally stops. See comments in
241 mi_cmd_exec_interrupt (char *command, char **argv, int argc)
245 if (!is_running (inferior_ptid))
246 error ("Current thread is not running.");
248 interrupt_target_1 (0);
250 else if (argc == 1 && strcmp (argv[0], "--all") == 0)
253 error ("Inferior not running.");
255 interrupt_target_1 (1);
257 else if (argc == 2 && strcmp (argv[0], "--thread-group") == 0)
259 struct cleanup *old_chain;
261 if (argv[1] == NULL || argv[1] == '\0')
262 error ("Thread group id not specified");
263 pid = atoi (argv[1]);
264 if (!in_inferior_list (pid))
265 error ("Invalid thread group id '%s'", argv[1]);
267 old_chain = make_cleanup_restore_current_thread ();
268 iterate_over_threads (interrupt_thread_callback, &pid);
269 do_cleanups (old_chain);
272 error ("Usage: -exec-interrupt [--all|--thread-group id]");
276 find_thread_of_process (struct thread_info *ti, void *p)
279 if (PIDGET (ti->ptid) == pid && !is_exited (ti->ptid))
286 mi_cmd_target_detach (char *command, char **argv, int argc)
288 if (argc != 0 && argc != 1)
289 error ("Usage: -target-detach [thread-group]");
293 struct thread_info *tp;
295 int pid = strtol (argv[0], &end, 10);
297 error (_("Cannot parse thread group id '%s'"), argv[0]);
299 /* Pick any thread in the desired process. Current
300 target_detach deteches from the parent of inferior_ptid. */
301 tp = iterate_over_threads (find_thread_of_process, &pid);
303 error (_("Thread group is empty"));
305 switch_to_thread (tp->ptid);
308 detach_command (NULL, 0);
312 mi_cmd_thread_select (char *command, char **argv, int argc)
315 char *mi_error_message;
318 error ("mi_cmd_thread_select: USAGE: threadnum.");
320 rc = gdb_thread_select (uiout, argv[0], &mi_error_message);
322 if (rc == GDB_RC_FAIL)
324 make_cleanup (xfree, mi_error_message);
325 error ("%s", mi_error_message);
330 mi_cmd_thread_list_ids (char *command, char **argv, int argc)
333 char *mi_error_message;
336 error ("mi_cmd_thread_list_ids: No arguments required.");
338 rc = gdb_list_thread_ids (uiout, &mi_error_message);
340 if (rc == GDB_RC_FAIL)
342 make_cleanup (xfree, mi_error_message);
343 error ("%s", mi_error_message);
348 mi_cmd_thread_info (char *command, char **argv, int argc)
352 if (argc != 0 && argc != 1)
353 error ("Invalid MI command");
356 thread = atoi (argv[0]);
358 print_thread_info (uiout, thread, -1);
362 print_one_inferior (struct inferior *inferior, void *arg)
364 struct cleanup *back_to = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
366 ui_out_field_fmt (uiout, "id", "%d", inferior->pid);
367 ui_out_field_string (uiout, "type", "process");
368 ui_out_field_int (uiout, "pid", inferior->pid);
370 do_cleanups (back_to);
375 mi_cmd_list_thread_groups (char *command, char **argv, int argc)
377 struct cleanup *back_to;
381 if (argc > 0 && strcmp (argv[0], "--available") == 0)
391 back_to = make_cleanup (null_cleanup, NULL);
395 error (_("Can only report top-level available thread groups"));
400 struct osdata_item *item;
403 data = get_osdata ("processes");
404 make_cleanup_osdata_free (data);
406 make_cleanup_ui_out_list_begin_end (uiout, "groups");
409 VEC_iterate (osdata_item_s, data->items,
413 struct cleanup *back_to =
414 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
416 const char *pid = get_osdata_column (item, "pid");
417 const char *cmd = get_osdata_column (item, "command");
418 const char *user = get_osdata_column (item, "user");
420 ui_out_field_fmt (uiout, "id", "%s", pid);
421 ui_out_field_string (uiout, "type", "process");
423 ui_out_field_string (uiout, "description", cmd);
425 ui_out_field_string (uiout, "user", user);
427 do_cleanups (back_to);
433 if (!in_inferior_list (pid))
434 error ("Invalid thread group id '%s'", id);
435 print_thread_info (uiout, -1, pid);
439 make_cleanup_ui_out_list_begin_end (uiout, "groups");
440 iterate_over_inferiors (print_one_inferior, NULL);
443 do_cleanups (back_to);
447 mi_cmd_data_list_register_names (char *command, char **argv, int argc)
449 struct frame_info *frame;
450 struct gdbarch *gdbarch;
453 struct cleanup *cleanup;
455 /* Note that the test for a valid register must include checking the
456 gdbarch_register_name because gdbarch_num_regs may be allocated for
457 the union of the register sets within a family of related processors.
458 In this case, some entries of gdbarch_register_name will change depending
459 upon the particular processor being debugged. */
461 frame = get_selected_frame (NULL);
462 gdbarch = get_frame_arch (frame);
463 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
465 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names");
467 if (argc == 0) /* No args, just do all the regs. */
473 if (gdbarch_register_name (gdbarch, regnum) == NULL
474 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
475 ui_out_field_string (uiout, NULL, "");
477 ui_out_field_string (uiout, NULL,
478 gdbarch_register_name (gdbarch, regnum));
482 /* Else, list of register #s, just do listed regs. */
483 for (i = 0; i < argc; i++)
485 regnum = atoi (argv[i]);
486 if (regnum < 0 || regnum >= numregs)
487 error ("bad register number");
489 if (gdbarch_register_name (gdbarch, regnum) == NULL
490 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
491 ui_out_field_string (uiout, NULL, "");
493 ui_out_field_string (uiout, NULL,
494 gdbarch_register_name (gdbarch, regnum));
496 do_cleanups (cleanup);
500 mi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
502 static struct regcache *this_regs = NULL;
503 struct regcache *prev_regs;
504 struct gdbarch *gdbarch;
505 int regnum, numregs, changed;
507 struct cleanup *cleanup;
509 /* The last time we visited this function, the current frame's register
510 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
511 and refresh THIS_REGS with the now-current register contents. */
513 prev_regs = this_regs;
514 this_regs = frame_save_as_regcache (get_selected_frame (NULL));
515 cleanup = make_cleanup_regcache_xfree (prev_regs);
517 /* Note that the test for a valid register must include checking the
518 gdbarch_register_name because gdbarch_num_regs may be allocated for
519 the union of the register sets within a family of related processors.
520 In this case, some entries of gdbarch_register_name will change depending
521 upon the particular processor being debugged. */
523 gdbarch = get_regcache_arch (this_regs);
524 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
526 make_cleanup_ui_out_list_begin_end (uiout, "changed-registers");
528 if (argc == 0) /* No args, just do all the regs. */
534 if (gdbarch_register_name (gdbarch, regnum) == NULL
535 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
537 changed = register_changed_p (regnum, prev_regs, this_regs);
539 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
541 ui_out_field_int (uiout, NULL, regnum);
545 /* Else, list of register #s, just do listed regs. */
546 for (i = 0; i < argc; i++)
548 regnum = atoi (argv[i]);
552 && gdbarch_register_name (gdbarch, regnum) != NULL
553 && *gdbarch_register_name (gdbarch, regnum) != '\000')
555 changed = register_changed_p (regnum, prev_regs, this_regs);
557 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
559 ui_out_field_int (uiout, NULL, regnum);
562 error ("bad register number");
564 do_cleanups (cleanup);
568 register_changed_p (int regnum, struct regcache *prev_regs,
569 struct regcache *this_regs)
571 struct gdbarch *gdbarch = get_regcache_arch (this_regs);
572 gdb_byte prev_buffer[MAX_REGISTER_SIZE];
573 gdb_byte this_buffer[MAX_REGISTER_SIZE];
575 /* Registers not valid in this frame return count as unchanged. */
576 if (!regcache_valid_p (this_regs, regnum))
579 /* First time through or after gdbarch change consider all registers as
580 changed. Same for registers not valid in the previous frame. */
581 if (!prev_regs || get_regcache_arch (prev_regs) != gdbarch
582 || !regcache_valid_p (prev_regs, regnum))
585 /* Get register contents and compare. */
586 regcache_cooked_read (prev_regs, regnum, prev_buffer);
587 regcache_cooked_read (this_regs, regnum, this_buffer);
589 return memcmp (prev_buffer, this_buffer,
590 register_size (gdbarch, regnum)) != 0;
593 /* Return a list of register number and value pairs. The valid
594 arguments expected are: a letter indicating the format in which to
595 display the registers contents. This can be one of: x (hexadecimal), d
596 (decimal), N (natural), t (binary), o (octal), r (raw). After the
597 format argumetn there can be a sequence of numbers, indicating which
598 registers to fetch the content of. If the format is the only argument,
599 a list of all the registers with their values is returned. */
601 mi_cmd_data_list_register_values (char *command, char **argv, int argc)
603 struct frame_info *frame;
604 struct gdbarch *gdbarch;
605 int regnum, numregs, format;
607 struct cleanup *list_cleanup, *tuple_cleanup;
609 /* Note that the test for a valid register must include checking the
610 gdbarch_register_name because gdbarch_num_regs may be allocated for
611 the union of the register sets within a family of related processors.
612 In this case, some entries of gdbarch_register_name will change depending
613 upon the particular processor being debugged. */
616 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
618 format = (int) argv[0][0];
620 frame = get_selected_frame (NULL);
621 gdbarch = get_frame_arch (frame);
622 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
624 list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values");
626 if (argc == 1) /* No args, beside the format: do all the regs. */
632 if (gdbarch_register_name (gdbarch, regnum) == NULL
633 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
635 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
636 ui_out_field_int (uiout, "number", regnum);
637 get_register (frame, regnum, format);
638 do_cleanups (tuple_cleanup);
642 /* Else, list of register #s, just do listed regs. */
643 for (i = 1; i < argc; i++)
645 regnum = atoi (argv[i]);
649 && gdbarch_register_name (gdbarch, regnum) != NULL
650 && *gdbarch_register_name (gdbarch, regnum) != '\000')
652 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
653 ui_out_field_int (uiout, "number", regnum);
654 get_register (frame, regnum, format);
655 do_cleanups (tuple_cleanup);
658 error ("bad register number");
660 do_cleanups (list_cleanup);
663 /* Output one register's contents in the desired format. */
665 get_register (struct frame_info *frame, int regnum, int format)
667 struct gdbarch *gdbarch = get_frame_arch (frame);
668 gdb_byte buffer[MAX_REGISTER_SIZE];
673 static struct ui_stream *stb = NULL;
675 stb = ui_out_stream_new (uiout);
680 frame_register (frame, regnum, &optim, &lval, &addr, &realnum, buffer);
683 error ("Optimized out");
688 char *ptr, buf[1024];
692 for (j = 0; j < register_size (gdbarch, regnum); j++)
694 int idx = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ?
695 j : register_size (gdbarch, regnum) - 1 - j;
696 sprintf (ptr, "%02x", (unsigned char) buffer[idx]);
699 ui_out_field_string (uiout, "value", buf);
700 /*fputs_filtered (buf, gdb_stdout); */
704 struct value_print_options opts;
705 get_formatted_print_options (&opts, format);
707 val_print (register_type (gdbarch, regnum), buffer, 0, 0,
708 stb->stream, 0, &opts, current_language);
709 ui_out_field_stream (uiout, "value", stb);
710 ui_out_stream_delete (stb);
714 /* Write given values into registers. The registers and values are
715 given as pairs. The corresponding MI command is
716 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
718 mi_cmd_data_write_register_values (char *command, char **argv, int argc)
720 struct regcache *regcache;
721 struct gdbarch *gdbarch;
725 /* Note that the test for a valid register must include checking the
726 gdbarch_register_name because gdbarch_num_regs may be allocated for
727 the union of the register sets within a family of related processors.
728 In this case, some entries of gdbarch_register_name will change depending
729 upon the particular processor being debugged. */
731 regcache = get_current_regcache ();
732 gdbarch = get_regcache_arch (regcache);
733 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
736 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
738 format = (int) argv[0][0];
740 if (!target_has_registers)
741 error ("mi_cmd_data_write_register_values: No registers.");
744 error ("mi_cmd_data_write_register_values: No regs and values specified.");
747 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
749 for (i = 1; i < argc; i = i + 2)
751 int regnum = atoi (argv[i]);
753 if (regnum >= 0 && regnum < numregs
754 && gdbarch_register_name (gdbarch, regnum)
755 && *gdbarch_register_name (gdbarch, regnum))
759 /* Get the value as a number. */
760 value = parse_and_eval_address (argv[i + 1]);
763 regcache_cooked_write_signed (regcache, regnum, value);
766 error ("bad register number");
770 /* Evaluate the value of the argument. The argument is an
771 expression. If the expression contains spaces it needs to be
772 included in double quotes. */
774 mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
776 struct expression *expr;
777 struct cleanup *old_chain = NULL;
779 struct ui_stream *stb = NULL;
780 struct value_print_options opts;
782 stb = ui_out_stream_new (uiout);
786 ui_out_stream_delete (stb);
787 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
790 expr = parse_expression (argv[0]);
792 old_chain = make_cleanup (free_current_contents, &expr);
794 val = evaluate_expression (expr);
796 /* Print the result of the expression evaluation. */
797 get_user_print_options (&opts);
799 val_print (value_type (val), value_contents (val),
800 value_embedded_offset (val), value_address (val),
801 stb->stream, 0, &opts, current_language);
803 ui_out_field_stream (uiout, "value", stb);
804 ui_out_stream_delete (stb);
806 do_cleanups (old_chain);
811 ADDR: start address of data to be dumped.
812 WORD-FORMAT: a char indicating format for the ``word''. See
814 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
815 NR_ROW: Number of rows.
816 NR_COL: The number of colums (words per row).
817 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
818 ASCHAR for unprintable characters.
820 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
821 displayes them. Returns:
823 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
826 The number of bytes read is SIZE*ROW*COL. */
829 mi_cmd_data_read_memory (char *command, char **argv, int argc)
831 struct gdbarch *gdbarch = current_gdbarch;
832 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
838 struct type *word_type;
851 static struct mi_opt opts[] =
853 {"o", OFFSET_OPT, 1},
859 int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts,
863 switch ((enum opt) opt)
866 offset = atol (optarg);
873 if (argc < 5 || argc > 6)
874 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
876 /* Extract all the arguments. */
878 /* Start address of the memory dump. */
879 addr = parse_and_eval_address (argv[0]) + offset;
880 /* The format character to use when displaying a memory word. See
881 the ``x'' command. */
882 word_format = argv[1][0];
883 /* The size of the memory word. */
884 word_size = atol (argv[2]);
888 word_type = builtin_type (gdbarch)->builtin_int8;
892 word_type = builtin_type (gdbarch)->builtin_int16;
896 word_type = builtin_type (gdbarch)->builtin_int32;
900 word_type = builtin_type (gdbarch)->builtin_int64;
904 word_type = builtin_type (gdbarch)->builtin_int8;
907 /* The number of rows. */
908 nr_rows = atol (argv[3]);
910 error ("mi_cmd_data_read_memory: invalid number of rows.");
912 /* Number of bytes per row. */
913 nr_cols = atol (argv[4]);
915 error ("mi_cmd_data_read_memory: invalid number of columns.");
917 /* The un-printable character when printing ascii. */
923 /* Create a buffer and read it in. */
924 total_bytes = word_size * nr_rows * nr_cols;
925 mbuf = xcalloc (total_bytes, 1);
926 make_cleanup (xfree, mbuf);
928 /* Dispatch memory reads to the topmost target, not the flattened
930 nr_bytes = target_read_until_error (current_target.beneath,
931 TARGET_OBJECT_MEMORY, NULL, mbuf,
934 error ("Unable to read memory.");
936 /* Output the header information. */
937 ui_out_field_core_addr (uiout, "addr", addr);
938 ui_out_field_int (uiout, "nr-bytes", nr_bytes);
939 ui_out_field_int (uiout, "total-bytes", total_bytes);
940 ui_out_field_core_addr (uiout, "next-row", addr + word_size * nr_cols);
941 ui_out_field_core_addr (uiout, "prev-row", addr - word_size * nr_cols);
942 ui_out_field_core_addr (uiout, "next-page", addr + total_bytes);
943 ui_out_field_core_addr (uiout, "prev-page", addr - total_bytes);
945 /* Build the result as a two dimentional table. */
947 struct ui_stream *stream = ui_out_stream_new (uiout);
948 struct cleanup *cleanup_list_memory;
951 cleanup_list_memory = make_cleanup_ui_out_list_begin_end (uiout, "memory");
952 for (row = 0, row_byte = 0;
954 row++, row_byte += nr_cols * word_size)
958 struct cleanup *cleanup_tuple;
959 struct cleanup *cleanup_list_data;
960 struct value_print_options opts;
962 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
963 ui_out_field_core_addr (uiout, "addr", addr + row_byte);
964 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
965 cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
966 get_formatted_print_options (&opts, word_format);
967 for (col = 0, col_byte = row_byte;
969 col++, col_byte += word_size)
971 if (col_byte + word_size > nr_bytes)
973 ui_out_field_string (uiout, NULL, "N/A");
977 ui_file_rewind (stream->stream);
978 print_scalar_formatted (mbuf + col_byte, word_type, &opts,
979 word_asize, stream->stream);
980 ui_out_field_stream (uiout, NULL, stream);
983 do_cleanups (cleanup_list_data);
987 ui_file_rewind (stream->stream);
988 for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
990 if (byte >= nr_bytes)
992 fputc_unfiltered ('X', stream->stream);
994 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
996 fputc_unfiltered (aschar, stream->stream);
999 fputc_unfiltered (mbuf[byte], stream->stream);
1001 ui_out_field_stream (uiout, "ascii", stream);
1003 do_cleanups (cleanup_tuple);
1005 ui_out_stream_delete (stream);
1006 do_cleanups (cleanup_list_memory);
1008 do_cleanups (cleanups);
1011 /* DATA-MEMORY-WRITE:
1013 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
1014 offset from the beginning of the memory grid row where the cell to
1016 ADDR: start address of the row in the memory grid where the memory
1017 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
1018 the location to write to.
1019 FORMAT: a char indicating format for the ``word''. See
1021 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
1022 VALUE: value to be written into the memory address.
1024 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1028 mi_cmd_data_write_memory (char *command, char **argv, int argc)
1033 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1034 enough when using a compiler other than GCC. */
1037 struct cleanup *old_chain;
1045 static struct mi_opt opts[] =
1047 {"o", OFFSET_OPT, 1},
1053 int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
1057 switch ((enum opt) opt)
1060 offset = atol (optarg);
1068 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1070 /* Extract all the arguments. */
1071 /* Start address of the memory dump. */
1072 addr = parse_and_eval_address (argv[0]);
1073 /* The format character to use when displaying a memory word. See
1074 the ``x'' command. */
1075 word_format = argv[1][0];
1076 /* The size of the memory word. */
1077 word_size = atol (argv[2]);
1079 /* Calculate the real address of the write destination. */
1080 addr += (offset * word_size);
1082 /* Get the value as a number. */
1083 value = parse_and_eval_address (argv[3]);
1084 /* Get the value into an array. */
1085 buffer = xmalloc (word_size);
1086 old_chain = make_cleanup (xfree, buffer);
1087 store_signed_integer (buffer, word_size, value);
1088 /* Write it down to memory. */
1089 write_memory (addr, buffer, word_size);
1090 /* Free the buffer. */
1091 do_cleanups (old_chain);
1095 mi_cmd_enable_timings (char *command, char **argv, int argc)
1101 if (strcmp (argv[0], "yes") == 0)
1103 else if (strcmp (argv[0], "no") == 0)
1114 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command);
1118 mi_cmd_list_features (char *command, char **argv, int argc)
1122 struct cleanup *cleanup = NULL;
1123 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
1125 ui_out_field_string (uiout, NULL, "frozen-varobjs");
1126 ui_out_field_string (uiout, NULL, "pending-breakpoints");
1127 ui_out_field_string (uiout, NULL, "thread-info");
1130 ui_out_field_string (uiout, NULL, "python");
1133 do_cleanups (cleanup);
1137 error ("-list-features should be passed no arguments");
1141 mi_cmd_list_target_features (char *command, char **argv, int argc)
1145 struct cleanup *cleanup = NULL;
1146 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
1148 if (target_can_async_p ())
1149 ui_out_field_string (uiout, NULL, "async");
1151 do_cleanups (cleanup);
1155 error ("-list-target-features should be passed no arguments");
1158 /* Execute a command within a safe environment.
1159 Return <0 for error; >=0 for ok.
1161 args->action will tell mi_execute_command what action
1162 to perfrom after the given command has executed (display/suppress
1163 prompt, display error). */
1166 captured_mi_execute_command (struct ui_out *uiout, void *data)
1168 struct cleanup *cleanup;
1169 struct mi_parse *context = (struct mi_parse *) data;
1172 current_command_ts = context->cmd_start;
1174 current_token = xstrdup (context->token);
1175 cleanup = make_cleanup (free_current_contents, ¤t_token);
1177 running_result_record_printed = 0;
1179 switch (context->op)
1182 /* A MI command was read from the input stream. */
1184 /* FIXME: gdb_???? */
1185 fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
1186 context->token, context->command, context->args);
1189 mi_cmd_execute (context);
1191 /* Print the result if there were no errors.
1193 Remember that on the way out of executing a command, you have
1194 to directly use the mi_interp's uiout, since the command could
1195 have reset the interpreter, in which case the current uiout
1196 will most likely crash in the mi_out_* routines. */
1197 if (!running_result_record_printed)
1199 fputs_unfiltered (context->token, raw_stdout);
1200 /* There's no particularly good reason why target-connect results
1201 in not ^done. Should kill ^connected for MI3. */
1202 fputs_unfiltered (strcmp (context->command, "target-select") == 0
1203 ? "^connected" : "^done", raw_stdout);
1204 mi_out_put (uiout, raw_stdout);
1205 mi_out_rewind (uiout);
1206 mi_print_timing_maybe ();
1207 fputs_unfiltered ("\n", raw_stdout);
1210 /* The command does not want anything to be printed. In that
1211 case, the command probably should not have written anything
1212 to uiout, but in case it has written something, discard it. */
1213 mi_out_rewind (uiout);
1219 /* A CLI command was read from the input stream. */
1220 /* This "feature" will be removed as soon as we have a
1221 complete set of mi commands. */
1222 /* Echo the command on the console. */
1223 fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1224 /* Call the "console" interpreter. */
1225 argv[0] = "console";
1226 argv[1] = context->command;
1227 mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);
1229 /* If we changed interpreters, DON'T print out anything. */
1230 if (current_interp_named_p (INTERP_MI)
1231 || current_interp_named_p (INTERP_MI1)
1232 || current_interp_named_p (INTERP_MI2)
1233 || current_interp_named_p (INTERP_MI3))
1235 if (!running_result_record_printed)
1237 fputs_unfiltered (context->token, raw_stdout);
1238 fputs_unfiltered ("^done", raw_stdout);
1239 mi_out_put (uiout, raw_stdout);
1240 mi_out_rewind (uiout);
1241 mi_print_timing_maybe ();
1242 fputs_unfiltered ("\n", raw_stdout);
1245 mi_out_rewind (uiout);
1252 do_cleanups (cleanup);
1259 mi_execute_command (char *cmd, int from_tty)
1261 struct mi_parse *command;
1262 struct ui_out *saved_uiout = uiout;
1264 /* This is to handle EOF (^D). We just quit gdb. */
1265 /* FIXME: we should call some API function here. */
1267 quit_force (NULL, from_tty);
1269 command = mi_parse (cmd);
1271 if (command != NULL)
1273 struct gdb_exception result;
1274 ptid_t previous_ptid = inferior_ptid;
1278 command->cmd_start = (struct mi_timestamp *)
1279 xmalloc (sizeof (struct mi_timestamp));
1280 timestamp (command->cmd_start);
1283 result = catch_exception (uiout, captured_mi_execute_command, command,
1285 if (result.reason < 0)
1287 /* The command execution failed and error() was called
1289 fputs_unfiltered (command->token, raw_stdout);
1290 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1291 if (result.message == NULL)
1292 fputs_unfiltered ("unknown error", raw_stdout);
1294 fputstr_unfiltered (result.message, '"', raw_stdout);
1295 fputs_unfiltered ("\"\n", raw_stdout);
1296 mi_out_rewind (uiout);
1299 if (/* The notifications are only output when the top-level
1300 interpreter (specified on the command line) is MI. */
1301 ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
1302 /* Don't try report anything if there are no threads --
1303 the program is dead. */
1304 && thread_count () != 0
1305 /* -thread-select explicitly changes thread. If frontend uses that
1306 internally, we don't want to emit =thread-selected, since
1307 =thread-selected is supposed to indicate user's intentions. */
1308 && strcmp (command->command, "thread-select") != 0)
1310 struct mi_interp *mi = top_level_interpreter_data ();
1311 int report_change = 0;
1313 if (command->thread == -1)
1315 report_change = (!ptid_equal (previous_ptid, null_ptid)
1316 && !ptid_equal (inferior_ptid, previous_ptid)
1317 && !ptid_equal (inferior_ptid, null_ptid));
1319 else if (!ptid_equal (inferior_ptid, null_ptid))
1321 struct thread_info *ti = inferior_thread ();
1322 report_change = (ti->num != command->thread);
1327 struct thread_info *ti = inferior_thread ();
1328 target_terminal_ours ();
1329 fprintf_unfiltered (mi->event_channel,
1330 "thread-selected,id=\"%d\"",
1332 gdb_flush (mi->event_channel);
1336 mi_parse_free (command);
1339 fputs_unfiltered ("(gdb) \n", raw_stdout);
1340 gdb_flush (raw_stdout);
1341 /* Print any buffered hook code. */
1346 mi_cmd_execute (struct mi_parse *parse)
1348 struct cleanup *cleanup;
1352 cleanup = make_cleanup (null_cleanup, NULL);
1354 if (parse->frame != -1 && parse->thread == -1)
1355 error (_("Cannot specify --frame without --thread"));
1357 if (parse->thread != -1)
1359 struct thread_info *tp = find_thread_id (parse->thread);
1361 error (_("Invalid thread id: %d"), parse->thread);
1363 if (is_exited (tp->ptid))
1364 error (_("Thread id: %d has terminated"), parse->thread);
1366 switch_to_thread (tp->ptid);
1369 if (parse->frame != -1)
1371 struct frame_info *fid;
1372 int frame = parse->frame;
1373 fid = find_relative_frame (get_current_frame (), &frame);
1375 /* find_relative_frame was successful */
1378 error (_("Invalid frame id: %d"), frame);
1381 if (parse->cmd->argv_func != NULL)
1382 parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
1383 else if (parse->cmd->cli.cmd != 0)
1385 /* FIXME: DELETE THIS. */
1386 /* The operation is still implemented by a cli command. */
1387 /* Must be a synchronous one. */
1388 mi_execute_cli_command (parse->cmd->cli.cmd, parse->cmd->cli.args_p,
1393 /* FIXME: DELETE THIS. */
1394 struct ui_file *stb;
1396 stb = mem_fileopen ();
1398 fputs_unfiltered ("Undefined mi command: ", stb);
1399 fputstr_unfiltered (parse->command, '"', stb);
1400 fputs_unfiltered (" (missing implementation)", stb);
1402 make_cleanup_ui_file_delete (stb);
1405 do_cleanups (cleanup);
1408 /* FIXME: This is just a hack so we can get some extra commands going.
1409 We don't want to channel things through the CLI, but call libgdb directly.
1410 Use only for synchronous commands. */
1413 mi_execute_cli_command (const char *cmd, int args_p, const char *args)
1417 struct cleanup *old_cleanups;
1420 run = xstrprintf ("%s %s", cmd, args);
1422 run = xstrdup (cmd);
1424 /* FIXME: gdb_???? */
1425 fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
1427 old_cleanups = make_cleanup (xfree, run);
1428 execute_command ( /*ui */ run, 0 /*from_tty */ );
1429 do_cleanups (old_cleanups);
1435 mi_execute_async_cli_command (char *cli_command, char **argv, int argc)
1437 struct cleanup *old_cleanups;
1440 if (target_can_async_p ())
1441 run = xstrprintf ("%s %s&", cli_command, argc ? *argv : "");
1443 run = xstrprintf ("%s %s", cli_command, argc ? *argv : "");
1444 old_cleanups = make_cleanup (xfree, run);
1446 execute_command ( /*ui */ run, 0 /*from_tty */ );
1448 if (target_can_async_p ())
1450 /* If we're not executing, an exception should have been throw. */
1451 gdb_assert (is_running (inferior_ptid));
1452 do_cleanups (old_cleanups);
1456 /* Do this before doing any printing. It would appear that some
1457 print code leaves garbage around in the buffer. */
1458 do_cleanups (old_cleanups);
1463 mi_load_progress (const char *section_name,
1464 unsigned long sent_so_far,
1465 unsigned long total_section,
1466 unsigned long total_sent,
1467 unsigned long grand_total)
1469 struct timeval time_now, delta, update_threshold;
1470 static struct timeval last_update;
1471 static char *previous_sect_name = NULL;
1473 struct ui_out *saved_uiout;
1475 /* This function is called through deprecated_show_load_progress
1476 which means uiout may not be correct. Fix it for the duration
1477 of this function. */
1478 saved_uiout = uiout;
1480 if (current_interp_named_p (INTERP_MI)
1481 || current_interp_named_p (INTERP_MI2))
1482 uiout = mi_out_new (2);
1483 else if (current_interp_named_p (INTERP_MI1))
1484 uiout = mi_out_new (1);
1485 else if (current_interp_named_p (INTERP_MI3))
1486 uiout = mi_out_new (3);
1490 update_threshold.tv_sec = 0;
1491 update_threshold.tv_usec = 500000;
1492 gettimeofday (&time_now, NULL);
1494 delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
1495 delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
1497 if (delta.tv_usec < 0)
1500 delta.tv_usec += 1000000L;
1503 new_section = (previous_sect_name ?
1504 strcmp (previous_sect_name, section_name) : 1);
1507 struct cleanup *cleanup_tuple;
1508 xfree (previous_sect_name);
1509 previous_sect_name = xstrdup (section_name);
1512 fputs_unfiltered (current_token, raw_stdout);
1513 fputs_unfiltered ("+download", raw_stdout);
1514 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1515 ui_out_field_string (uiout, "section", section_name);
1516 ui_out_field_int (uiout, "section-size", total_section);
1517 ui_out_field_int (uiout, "total-size", grand_total);
1518 do_cleanups (cleanup_tuple);
1519 mi_out_put (uiout, raw_stdout);
1520 fputs_unfiltered ("\n", raw_stdout);
1521 gdb_flush (raw_stdout);
1524 if (delta.tv_sec >= update_threshold.tv_sec &&
1525 delta.tv_usec >= update_threshold.tv_usec)
1527 struct cleanup *cleanup_tuple;
1528 last_update.tv_sec = time_now.tv_sec;
1529 last_update.tv_usec = time_now.tv_usec;
1531 fputs_unfiltered (current_token, raw_stdout);
1532 fputs_unfiltered ("+download", raw_stdout);
1533 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1534 ui_out_field_string (uiout, "section", section_name);
1535 ui_out_field_int (uiout, "section-sent", sent_so_far);
1536 ui_out_field_int (uiout, "section-size", total_section);
1537 ui_out_field_int (uiout, "total-sent", total_sent);
1538 ui_out_field_int (uiout, "total-size", grand_total);
1539 do_cleanups (cleanup_tuple);
1540 mi_out_put (uiout, raw_stdout);
1541 fputs_unfiltered ("\n", raw_stdout);
1542 gdb_flush (raw_stdout);
1546 uiout = saved_uiout;
1550 timestamp (struct mi_timestamp *tv)
1553 gettimeofday (&tv->wallclock, NULL);
1554 #ifdef HAVE_GETRUSAGE
1555 getrusage (RUSAGE_SELF, &rusage);
1556 tv->utime.tv_sec = rusage.ru_utime.tv_sec;
1557 tv->utime.tv_usec = rusage.ru_utime.tv_usec;
1558 tv->stime.tv_sec = rusage.ru_stime.tv_sec;
1559 tv->stime.tv_usec = rusage.ru_stime.tv_usec;
1561 usec = get_run_time ();
1562 tv->utime.tv_sec = usec/1000000L;
1563 tv->utime.tv_usec = usec - 1000000L*tv->utime.tv_sec;
1564 tv->stime.tv_sec = 0;
1565 tv->stime.tv_usec = 0;
1570 print_diff_now (struct mi_timestamp *start)
1572 struct mi_timestamp now;
1574 print_diff (start, &now);
1578 mi_print_timing_maybe (void)
1580 /* If the command is -enable-timing then do_timings may be
1581 true whilst current_command_ts is not initialized. */
1582 if (do_timings && current_command_ts)
1583 print_diff_now (current_command_ts);
1587 timeval_diff (struct timeval start, struct timeval end)
1589 return ((end.tv_sec - start.tv_sec) * 1000000L)
1590 + (end.tv_usec - start.tv_usec);
1594 print_diff (struct mi_timestamp *start, struct mi_timestamp *end)
1598 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1599 timeval_diff (start->wallclock, end->wallclock) / 1000000.0,
1600 timeval_diff (start->utime, end->utime) / 1000000.0,
1601 timeval_diff (start->stime, end->stime) / 1000000.0);