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 /* FIXME: The function called is not yet a formal libgdb function. */
115 quit_force (NULL
, FROM_TTY
);
119 mi_cmd_exec_next (char *command
, char **argv
, int argc
)
121 /* FIXME: Should call a libgdb function, not a cli wrapper. */
122 mi_execute_async_cli_command ("next", argv
, argc
);
126 mi_cmd_exec_next_instruction (char *command
, char **argv
, int argc
)
128 /* FIXME: Should call a libgdb function, not a cli wrapper. */
129 mi_execute_async_cli_command ("nexti", argv
, argc
);
133 mi_cmd_exec_step (char *command
, char **argv
, int argc
)
135 /* FIXME: Should call a libgdb function, not a cli wrapper. */
136 mi_execute_async_cli_command ("step", argv
, argc
);
140 mi_cmd_exec_step_instruction (char *command
, char **argv
, int argc
)
142 /* FIXME: Should call a libgdb function, not a cli wrapper. */
143 mi_execute_async_cli_command ("stepi", argv
, argc
);
147 mi_cmd_exec_finish (char *command
, char **argv
, int argc
)
149 /* FIXME: Should call a libgdb function, not a cli wrapper. */
150 mi_execute_async_cli_command ("finish", argv
, argc
);
154 mi_cmd_exec_return (char *command
, char **argv
, int argc
)
156 /* This command doesn't really execute the target, it just pops the
157 specified number of frames. */
159 /* Call return_command with from_tty argument equal to 0 so as to
160 avoid being queried. */
161 return_command (*argv
, 0);
163 /* Call return_command with from_tty argument equal to 0 so as to
164 avoid being queried. */
165 return_command (NULL
, 0);
167 /* Because we have called return_command with from_tty = 0, we need
168 to print the frame here. */
169 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
);
173 mi_cmd_exec_jump (char *args
, char **argv
, int argc
)
175 /* FIXME: Should call a libgdb function, not a cli wrapper. */
176 return mi_execute_async_cli_command ("jump", argv
, argc
);
180 proceed_thread_callback (struct thread_info
*thread
, void *arg
)
182 int pid
= *(int *)arg
;
184 if (!is_stopped (thread
->ptid
))
187 if (PIDGET (thread
->ptid
) != pid
)
190 switch_to_thread (thread
->ptid
);
191 clear_proceed_status ();
192 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_DEFAULT
, 0);
197 mi_cmd_exec_continue (char *command
, char **argv
, int argc
)
201 else if (argc
== 1 && strcmp (argv
[0], "--all") == 0)
203 else if (argc
== 2 && strcmp (argv
[0], "--thread-group") == 0)
205 struct cleanup
*old_chain
;
207 if (argv
[1] == NULL
|| argv
[1] == '\0')
208 error ("Thread group id not specified");
209 pid
= atoi (argv
[1]);
210 if (!in_inferior_list (pid
))
211 error ("Invalid thread group id '%s'", argv
[1]);
213 old_chain
= make_cleanup_restore_current_thread ();
214 iterate_over_threads (proceed_thread_callback
, &pid
);
215 do_cleanups (old_chain
);
218 error ("Usage: -exec-continue [--all|--thread-group id]");
222 interrupt_thread_callback (struct thread_info
*thread
, void *arg
)
224 int pid
= *(int *)arg
;
226 if (!is_running (thread
->ptid
))
229 if (PIDGET (thread
->ptid
) != pid
)
232 target_stop (thread
->ptid
);
236 /* Interrupt the execution of the target. Note how we must play around
237 with the token variables, in order to display the current token in
238 the result of the interrupt command, and the previous execution
239 token when the target finally stops. See comments in
242 mi_cmd_exec_interrupt (char *command
, char **argv
, int argc
)
246 if (!is_running (inferior_ptid
))
247 error ("Current thread is not running.");
249 interrupt_target_1 (0);
251 else if (argc
== 1 && strcmp (argv
[0], "--all") == 0)
254 error ("Inferior not running.");
256 interrupt_target_1 (1);
258 else if (argc
== 2 && strcmp (argv
[0], "--thread-group") == 0)
260 struct cleanup
*old_chain
;
262 if (argv
[1] == NULL
|| argv
[1] == '\0')
263 error ("Thread group id not specified");
264 pid
= atoi (argv
[1]);
265 if (!in_inferior_list (pid
))
266 error ("Invalid thread group id '%s'", argv
[1]);
268 old_chain
= make_cleanup_restore_current_thread ();
269 iterate_over_threads (interrupt_thread_callback
, &pid
);
270 do_cleanups (old_chain
);
273 error ("Usage: -exec-interrupt [--all|--thread-group id]");
277 find_thread_of_process (struct thread_info
*ti
, void *p
)
280 if (PIDGET (ti
->ptid
) == pid
&& !is_exited (ti
->ptid
))
287 mi_cmd_target_detach (char *command
, char **argv
, int argc
)
289 if (argc
!= 0 && argc
!= 1)
290 error ("Usage: -target-detach [thread-group]");
294 struct thread_info
*tp
;
296 int pid
= strtol (argv
[0], &end
, 10);
298 error (_("Cannot parse thread group id '%s'"), argv
[0]);
300 /* Pick any thread in the desired process. Current
301 target_detach deteches from the parent of inferior_ptid. */
302 tp
= iterate_over_threads (find_thread_of_process
, &pid
);
304 error (_("Thread group is empty"));
306 switch_to_thread (tp
->ptid
);
309 detach_command (NULL
, 0);
313 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
316 char *mi_error_message
;
319 error ("mi_cmd_thread_select: USAGE: threadnum.");
321 rc
= gdb_thread_select (uiout
, argv
[0], &mi_error_message
);
323 if (rc
== GDB_RC_FAIL
)
325 make_cleanup (xfree
, mi_error_message
);
326 error ("%s", mi_error_message
);
331 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
334 char *mi_error_message
;
337 error ("mi_cmd_thread_list_ids: No arguments required.");
339 rc
= gdb_list_thread_ids (uiout
, &mi_error_message
);
341 if (rc
== GDB_RC_FAIL
)
343 make_cleanup (xfree
, mi_error_message
);
344 error ("%s", mi_error_message
);
349 mi_cmd_thread_info (char *command
, char **argv
, int argc
)
353 if (argc
!= 0 && argc
!= 1)
354 error ("Invalid MI command");
357 thread
= atoi (argv
[0]);
359 print_thread_info (uiout
, thread
, -1);
363 print_one_inferior (struct inferior
*inferior
, void *arg
)
365 struct cleanup
*back_to
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
367 ui_out_field_fmt (uiout
, "id", "%d", inferior
->pid
);
368 ui_out_field_string (uiout
, "type", "process");
369 ui_out_field_int (uiout
, "pid", inferior
->pid
);
371 do_cleanups (back_to
);
376 mi_cmd_list_thread_groups (char *command
, char **argv
, int argc
)
378 struct cleanup
*back_to
;
382 if (argc
> 0 && strcmp (argv
[0], "--available") == 0)
392 back_to
= make_cleanup (null_cleanup
, NULL
);
396 error (_("Can only report top-level available thread groups"));
401 struct osdata_item
*item
;
404 data
= get_osdata ("processes");
405 make_cleanup_osdata_free (data
);
407 make_cleanup_ui_out_list_begin_end (uiout
, "groups");
410 VEC_iterate (osdata_item_s
, data
->items
,
414 struct cleanup
*back_to
=
415 make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
417 const char *pid
= get_osdata_column (item
, "pid");
418 const char *cmd
= get_osdata_column (item
, "command");
419 const char *user
= get_osdata_column (item
, "user");
421 ui_out_field_fmt (uiout
, "id", "%s", pid
);
422 ui_out_field_string (uiout
, "type", "process");
424 ui_out_field_string (uiout
, "description", cmd
);
426 ui_out_field_string (uiout
, "user", user
);
428 do_cleanups (back_to
);
434 if (!in_inferior_list (pid
))
435 error ("Invalid thread group id '%s'", id
);
436 print_thread_info (uiout
, -1, pid
);
440 make_cleanup_ui_out_list_begin_end (uiout
, "groups");
441 iterate_over_inferiors (print_one_inferior
, NULL
);
444 do_cleanups (back_to
);
448 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
450 struct frame_info
*frame
;
451 struct gdbarch
*gdbarch
;
454 struct cleanup
*cleanup
;
456 /* Note that the test for a valid register must include checking the
457 gdbarch_register_name because gdbarch_num_regs may be allocated for
458 the union of the register sets within a family of related processors.
459 In this case, some entries of gdbarch_register_name will change depending
460 upon the particular processor being debugged. */
462 frame
= get_selected_frame (NULL
);
463 gdbarch
= get_frame_arch (frame
);
464 numregs
= gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
466 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
468 if (argc
== 0) /* No args, just do all the regs. */
474 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
475 || *(gdbarch_register_name (gdbarch
, regnum
)) == '\0')
476 ui_out_field_string (uiout
, NULL
, "");
478 ui_out_field_string (uiout
, NULL
,
479 gdbarch_register_name (gdbarch
, regnum
));
483 /* Else, list of register #s, just do listed regs. */
484 for (i
= 0; i
< argc
; i
++)
486 regnum
= atoi (argv
[i
]);
487 if (regnum
< 0 || regnum
>= numregs
)
488 error ("bad register number");
490 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
491 || *(gdbarch_register_name (gdbarch
, regnum
)) == '\0')
492 ui_out_field_string (uiout
, NULL
, "");
494 ui_out_field_string (uiout
, NULL
,
495 gdbarch_register_name (gdbarch
, regnum
));
497 do_cleanups (cleanup
);
501 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
503 static struct regcache
*this_regs
= NULL
;
504 struct regcache
*prev_regs
;
505 struct gdbarch
*gdbarch
;
506 int regnum
, numregs
, changed
;
508 struct cleanup
*cleanup
;
510 /* The last time we visited this function, the current frame's register
511 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
512 and refresh THIS_REGS with the now-current register contents. */
514 prev_regs
= this_regs
;
515 this_regs
= frame_save_as_regcache (get_selected_frame (NULL
));
516 cleanup
= make_cleanup_regcache_xfree (prev_regs
);
518 /* Note that the test for a valid register must include checking the
519 gdbarch_register_name because gdbarch_num_regs may be allocated for
520 the union of the register sets within a family of related processors.
521 In this case, some entries of gdbarch_register_name will change depending
522 upon the particular processor being debugged. */
524 gdbarch
= get_regcache_arch (this_regs
);
525 numregs
= gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
527 make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
529 if (argc
== 0) /* No args, just do all the regs. */
535 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
536 || *(gdbarch_register_name (gdbarch
, regnum
)) == '\0')
538 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
540 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
542 ui_out_field_int (uiout
, NULL
, regnum
);
546 /* Else, list of register #s, just do listed regs. */
547 for (i
= 0; i
< argc
; i
++)
549 regnum
= atoi (argv
[i
]);
553 && gdbarch_register_name (gdbarch
, regnum
) != NULL
554 && *gdbarch_register_name (gdbarch
, regnum
) != '\000')
556 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
558 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
560 ui_out_field_int (uiout
, NULL
, regnum
);
563 error ("bad register number");
565 do_cleanups (cleanup
);
569 register_changed_p (int regnum
, struct regcache
*prev_regs
,
570 struct regcache
*this_regs
)
572 struct gdbarch
*gdbarch
= get_regcache_arch (this_regs
);
573 gdb_byte prev_buffer
[MAX_REGISTER_SIZE
];
574 gdb_byte this_buffer
[MAX_REGISTER_SIZE
];
576 /* Registers not valid in this frame return count as unchanged. */
577 if (!regcache_valid_p (this_regs
, regnum
))
580 /* First time through or after gdbarch change consider all registers as
581 changed. Same for registers not valid in the previous frame. */
582 if (!prev_regs
|| get_regcache_arch (prev_regs
) != gdbarch
583 || !regcache_valid_p (prev_regs
, regnum
))
586 /* Get register contents and compare. */
587 regcache_cooked_read (prev_regs
, regnum
, prev_buffer
);
588 regcache_cooked_read (this_regs
, regnum
, this_buffer
);
590 return memcmp (prev_buffer
, this_buffer
,
591 register_size (gdbarch
, regnum
)) != 0;
594 /* Return a list of register number and value pairs. The valid
595 arguments expected are: a letter indicating the format in which to
596 display the registers contents. This can be one of: x (hexadecimal), d
597 (decimal), N (natural), t (binary), o (octal), r (raw). After the
598 format argumetn there can be a sequence of numbers, indicating which
599 registers to fetch the content of. If the format is the only argument,
600 a list of all the registers with their values is returned. */
602 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
604 struct frame_info
*frame
;
605 struct gdbarch
*gdbarch
;
606 int regnum
, numregs
, format
;
608 struct cleanup
*list_cleanup
, *tuple_cleanup
;
610 /* Note that the test for a valid register must include checking the
611 gdbarch_register_name because gdbarch_num_regs may be allocated for
612 the union of the register sets within a family of related processors.
613 In this case, some entries of gdbarch_register_name will change depending
614 upon the particular processor being debugged. */
617 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
619 format
= (int) argv
[0][0];
621 frame
= get_selected_frame (NULL
);
622 gdbarch
= get_frame_arch (frame
);
623 numregs
= gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
625 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
627 if (argc
== 1) /* No args, beside the format: do all the regs. */
633 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
634 || *(gdbarch_register_name (gdbarch
, regnum
)) == '\0')
636 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
637 ui_out_field_int (uiout
, "number", regnum
);
638 get_register (frame
, regnum
, format
);
639 do_cleanups (tuple_cleanup
);
643 /* Else, list of register #s, just do listed regs. */
644 for (i
= 1; i
< argc
; i
++)
646 regnum
= atoi (argv
[i
]);
650 && gdbarch_register_name (gdbarch
, regnum
) != NULL
651 && *gdbarch_register_name (gdbarch
, regnum
) != '\000')
653 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
654 ui_out_field_int (uiout
, "number", regnum
);
655 get_register (frame
, regnum
, format
);
656 do_cleanups (tuple_cleanup
);
659 error ("bad register number");
661 do_cleanups (list_cleanup
);
664 /* Output one register's contents in the desired format. */
666 get_register (struct frame_info
*frame
, int regnum
, int format
)
668 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
669 gdb_byte buffer
[MAX_REGISTER_SIZE
];
674 static struct ui_stream
*stb
= NULL
;
676 stb
= ui_out_stream_new (uiout
);
681 frame_register (frame
, regnum
, &optim
, &lval
, &addr
, &realnum
, buffer
);
684 error ("Optimized out");
689 char *ptr
, buf
[1024];
693 for (j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
695 int idx
= gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
?
696 j
: register_size (gdbarch
, regnum
) - 1 - j
;
697 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
700 ui_out_field_string (uiout
, "value", buf
);
701 /*fputs_filtered (buf, gdb_stdout); */
705 struct value_print_options opts
;
706 get_formatted_print_options (&opts
, format
);
708 val_print (register_type (gdbarch
, regnum
), buffer
, 0, 0,
709 stb
->stream
, 0, &opts
, current_language
);
710 ui_out_field_stream (uiout
, "value", stb
);
711 ui_out_stream_delete (stb
);
715 /* Write given values into registers. The registers and values are
716 given as pairs. The corresponding MI command is
717 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
719 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
721 struct regcache
*regcache
;
722 struct gdbarch
*gdbarch
;
726 /* Note that the test for a valid register must include checking the
727 gdbarch_register_name because gdbarch_num_regs may be allocated for
728 the union of the register sets within a family of related processors.
729 In this case, some entries of gdbarch_register_name will change depending
730 upon the particular processor being debugged. */
732 regcache
= get_current_regcache ();
733 gdbarch
= get_regcache_arch (regcache
);
734 numregs
= gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
737 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
739 format
= (int) argv
[0][0];
741 if (!target_has_registers
)
742 error ("mi_cmd_data_write_register_values: No registers.");
745 error ("mi_cmd_data_write_register_values: No regs and values specified.");
748 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
750 for (i
= 1; i
< argc
; i
= i
+ 2)
752 int regnum
= atoi (argv
[i
]);
754 if (regnum
>= 0 && regnum
< numregs
755 && gdbarch_register_name (gdbarch
, regnum
)
756 && *gdbarch_register_name (gdbarch
, regnum
))
760 /* Get the value as a number. */
761 value
= parse_and_eval_address (argv
[i
+ 1]);
764 regcache_cooked_write_signed (regcache
, regnum
, value
);
767 error ("bad register number");
771 /* Evaluate the value of the argument. The argument is an
772 expression. If the expression contains spaces it needs to be
773 included in double quotes. */
775 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
777 struct expression
*expr
;
778 struct cleanup
*old_chain
= NULL
;
780 struct ui_stream
*stb
= NULL
;
781 struct value_print_options opts
;
783 stb
= ui_out_stream_new (uiout
);
787 ui_out_stream_delete (stb
);
788 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
791 expr
= parse_expression (argv
[0]);
793 old_chain
= make_cleanup (free_current_contents
, &expr
);
795 val
= evaluate_expression (expr
);
797 /* Print the result of the expression evaluation. */
798 get_user_print_options (&opts
);
800 val_print (value_type (val
), value_contents (val
),
801 value_embedded_offset (val
), value_address (val
),
802 stb
->stream
, 0, &opts
, current_language
);
804 ui_out_field_stream (uiout
, "value", stb
);
805 ui_out_stream_delete (stb
);
807 do_cleanups (old_chain
);
812 ADDR: start address of data to be dumped.
813 WORD-FORMAT: a char indicating format for the ``word''. See
815 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
816 NR_ROW: Number of rows.
817 NR_COL: The number of colums (words per row).
818 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
819 ASCHAR for unprintable characters.
821 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
822 displayes them. Returns:
824 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
827 The number of bytes read is SIZE*ROW*COL. */
830 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
832 struct gdbarch
*gdbarch
= get_current_arch ();
833 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
839 struct type
*word_type
;
852 static struct mi_opt opts
[] =
854 {"o", OFFSET_OPT
, 1},
860 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
864 switch ((enum opt
) opt
)
867 offset
= atol (optarg
);
874 if (argc
< 5 || argc
> 6)
875 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
877 /* Extract all the arguments. */
879 /* Start address of the memory dump. */
880 addr
= parse_and_eval_address (argv
[0]) + offset
;
881 /* The format character to use when displaying a memory word. See
882 the ``x'' command. */
883 word_format
= argv
[1][0];
884 /* The size of the memory word. */
885 word_size
= atol (argv
[2]);
889 word_type
= builtin_type (gdbarch
)->builtin_int8
;
893 word_type
= builtin_type (gdbarch
)->builtin_int16
;
897 word_type
= builtin_type (gdbarch
)->builtin_int32
;
901 word_type
= builtin_type (gdbarch
)->builtin_int64
;
905 word_type
= builtin_type (gdbarch
)->builtin_int8
;
908 /* The number of rows. */
909 nr_rows
= atol (argv
[3]);
911 error ("mi_cmd_data_read_memory: invalid number of rows.");
913 /* Number of bytes per row. */
914 nr_cols
= atol (argv
[4]);
916 error ("mi_cmd_data_read_memory: invalid number of columns.");
918 /* The un-printable character when printing ascii. */
924 /* Create a buffer and read it in. */
925 total_bytes
= word_size
* nr_rows
* nr_cols
;
926 mbuf
= xcalloc (total_bytes
, 1);
927 make_cleanup (xfree
, mbuf
);
929 /* Dispatch memory reads to the topmost target, not the flattened
931 nr_bytes
= target_read_until_error (current_target
.beneath
,
932 TARGET_OBJECT_MEMORY
, NULL
, mbuf
,
935 error ("Unable to read memory.");
937 /* Output the header information. */
938 ui_out_field_core_addr (uiout
, "addr", gdbarch
, addr
);
939 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
940 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
941 ui_out_field_core_addr (uiout
, "next-row",
942 gdbarch
, addr
+ word_size
* nr_cols
);
943 ui_out_field_core_addr (uiout
, "prev-row",
944 gdbarch
, addr
- word_size
* nr_cols
);
945 ui_out_field_core_addr (uiout
, "next-page", gdbarch
, addr
+ total_bytes
);
946 ui_out_field_core_addr (uiout
, "prev-page", gdbarch
, addr
- total_bytes
);
948 /* Build the result as a two dimentional table. */
950 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
951 struct cleanup
*cleanup_list_memory
;
954 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
955 for (row
= 0, row_byte
= 0;
957 row
++, row_byte
+= nr_cols
* word_size
)
961 struct cleanup
*cleanup_tuple
;
962 struct cleanup
*cleanup_list_data
;
963 struct value_print_options opts
;
965 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
966 ui_out_field_core_addr (uiout
, "addr", gdbarch
, addr
+ row_byte
);
967 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
968 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
969 get_formatted_print_options (&opts
, word_format
);
970 for (col
= 0, col_byte
= row_byte
;
972 col
++, col_byte
+= word_size
)
974 if (col_byte
+ word_size
> nr_bytes
)
976 ui_out_field_string (uiout
, NULL
, "N/A");
980 ui_file_rewind (stream
->stream
);
981 print_scalar_formatted (mbuf
+ col_byte
, word_type
, &opts
,
982 word_asize
, stream
->stream
);
983 ui_out_field_stream (uiout
, NULL
, stream
);
986 do_cleanups (cleanup_list_data
);
990 ui_file_rewind (stream
->stream
);
991 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
993 if (byte
>= nr_bytes
)
995 fputc_unfiltered ('X', stream
->stream
);
997 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
999 fputc_unfiltered (aschar
, stream
->stream
);
1002 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
1004 ui_out_field_stream (uiout
, "ascii", stream
);
1006 do_cleanups (cleanup_tuple
);
1008 ui_out_stream_delete (stream
);
1009 do_cleanups (cleanup_list_memory
);
1011 do_cleanups (cleanups
);
1014 /* DATA-MEMORY-WRITE:
1016 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
1017 offset from the beginning of the memory grid row where the cell to
1019 ADDR: start address of the row in the memory grid where the memory
1020 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
1021 the location to write to.
1022 FORMAT: a char indicating format for the ``word''. See
1024 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
1025 VALUE: value to be written into the memory address.
1027 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1031 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
1033 struct gdbarch
*gdbarch
= get_current_arch ();
1034 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1038 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1039 enough when using a compiler other than GCC. */
1042 struct cleanup
*old_chain
;
1050 static struct mi_opt opts
[] =
1052 {"o", OFFSET_OPT
, 1},
1058 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
1062 switch ((enum opt
) opt
)
1065 offset
= atol (optarg
);
1073 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1075 /* Extract all the arguments. */
1076 /* Start address of the memory dump. */
1077 addr
= parse_and_eval_address (argv
[0]);
1078 /* The format character to use when displaying a memory word. See
1079 the ``x'' command. */
1080 word_format
= argv
[1][0];
1081 /* The size of the memory word. */
1082 word_size
= atol (argv
[2]);
1084 /* Calculate the real address of the write destination. */
1085 addr
+= (offset
* word_size
);
1087 /* Get the value as a number. */
1088 value
= parse_and_eval_address (argv
[3]);
1089 /* Get the value into an array. */
1090 buffer
= xmalloc (word_size
);
1091 old_chain
= make_cleanup (xfree
, buffer
);
1092 store_signed_integer (buffer
, word_size
, byte_order
, value
);
1093 /* Write it down to memory. */
1094 write_memory (addr
, buffer
, word_size
);
1095 /* Free the buffer. */
1096 do_cleanups (old_chain
);
1100 mi_cmd_enable_timings (char *command
, char **argv
, int argc
)
1106 if (strcmp (argv
[0], "yes") == 0)
1108 else if (strcmp (argv
[0], "no") == 0)
1119 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command
);
1123 mi_cmd_list_features (char *command
, char **argv
, int argc
)
1127 struct cleanup
*cleanup
= NULL
;
1128 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
1130 ui_out_field_string (uiout
, NULL
, "frozen-varobjs");
1131 ui_out_field_string (uiout
, NULL
, "pending-breakpoints");
1132 ui_out_field_string (uiout
, NULL
, "thread-info");
1135 ui_out_field_string (uiout
, NULL
, "python");
1138 do_cleanups (cleanup
);
1142 error ("-list-features should be passed no arguments");
1146 mi_cmd_list_target_features (char *command
, char **argv
, int argc
)
1150 struct cleanup
*cleanup
= NULL
;
1151 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
1153 if (target_can_async_p ())
1154 ui_out_field_string (uiout
, NULL
, "async");
1156 do_cleanups (cleanup
);
1160 error ("-list-target-features should be passed no arguments");
1163 /* Execute a command within a safe environment.
1164 Return <0 for error; >=0 for ok.
1166 args->action will tell mi_execute_command what action
1167 to perfrom after the given command has executed (display/suppress
1168 prompt, display error). */
1171 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1173 struct cleanup
*cleanup
;
1174 struct mi_parse
*context
= (struct mi_parse
*) data
;
1177 current_command_ts
= context
->cmd_start
;
1179 current_token
= xstrdup (context
->token
);
1180 cleanup
= make_cleanup (free_current_contents
, ¤t_token
);
1182 running_result_record_printed
= 0;
1184 switch (context
->op
)
1187 /* A MI command was read from the input stream. */
1189 /* FIXME: gdb_???? */
1190 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1191 context
->token
, context
->command
, context
->args
);
1194 mi_cmd_execute (context
);
1196 /* Print the result if there were no errors.
1198 Remember that on the way out of executing a command, you have
1199 to directly use the mi_interp's uiout, since the command could
1200 have reset the interpreter, in which case the current uiout
1201 will most likely crash in the mi_out_* routines. */
1202 if (!running_result_record_printed
)
1204 fputs_unfiltered (context
->token
, raw_stdout
);
1205 /* There's no particularly good reason why target-connect results
1206 in not ^done. Should kill ^connected for MI3. */
1207 fputs_unfiltered (strcmp (context
->command
, "target-select") == 0
1208 ? "^connected" : "^done", raw_stdout
);
1209 mi_out_put (uiout
, raw_stdout
);
1210 mi_out_rewind (uiout
);
1211 mi_print_timing_maybe ();
1212 fputs_unfiltered ("\n", raw_stdout
);
1215 /* The command does not want anything to be printed. In that
1216 case, the command probably should not have written anything
1217 to uiout, but in case it has written something, discard it. */
1218 mi_out_rewind (uiout
);
1224 /* A CLI command was read from the input stream. */
1225 /* This "feature" will be removed as soon as we have a
1226 complete set of mi commands. */
1227 /* Echo the command on the console. */
1228 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1229 /* Call the "console" interpreter. */
1230 argv
[0] = "console";
1231 argv
[1] = context
->command
;
1232 mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
1234 /* If we changed interpreters, DON'T print out anything. */
1235 if (current_interp_named_p (INTERP_MI
)
1236 || current_interp_named_p (INTERP_MI1
)
1237 || current_interp_named_p (INTERP_MI2
)
1238 || current_interp_named_p (INTERP_MI3
))
1240 if (!running_result_record_printed
)
1242 fputs_unfiltered (context
->token
, raw_stdout
);
1243 fputs_unfiltered ("^done", raw_stdout
);
1244 mi_out_put (uiout
, raw_stdout
);
1245 mi_out_rewind (uiout
);
1246 mi_print_timing_maybe ();
1247 fputs_unfiltered ("\n", raw_stdout
);
1250 mi_out_rewind (uiout
);
1257 do_cleanups (cleanup
);
1264 mi_execute_command (char *cmd
, int from_tty
)
1266 struct mi_parse
*command
;
1267 struct ui_out
*saved_uiout
= uiout
;
1269 /* This is to handle EOF (^D). We just quit gdb. */
1270 /* FIXME: we should call some API function here. */
1272 quit_force (NULL
, from_tty
);
1274 command
= mi_parse (cmd
);
1276 if (command
!= NULL
)
1278 struct gdb_exception result
;
1279 ptid_t previous_ptid
= inferior_ptid
;
1283 command
->cmd_start
= (struct mi_timestamp
*)
1284 xmalloc (sizeof (struct mi_timestamp
));
1285 timestamp (command
->cmd_start
);
1288 result
= catch_exception (uiout
, captured_mi_execute_command
, command
,
1290 if (result
.reason
< 0)
1292 /* The command execution failed and error() was called
1294 fputs_unfiltered (command
->token
, raw_stdout
);
1295 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1296 if (result
.message
== NULL
)
1297 fputs_unfiltered ("unknown error", raw_stdout
);
1299 fputstr_unfiltered (result
.message
, '"', raw_stdout
);
1300 fputs_unfiltered ("\"\n", raw_stdout
);
1301 mi_out_rewind (uiout
);
1304 if (/* The notifications are only output when the top-level
1305 interpreter (specified on the command line) is MI. */
1306 ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
1307 /* Don't try report anything if there are no threads --
1308 the program is dead. */
1309 && thread_count () != 0
1310 /* -thread-select explicitly changes thread. If frontend uses that
1311 internally, we don't want to emit =thread-selected, since
1312 =thread-selected is supposed to indicate user's intentions. */
1313 && strcmp (command
->command
, "thread-select") != 0)
1315 struct mi_interp
*mi
= top_level_interpreter_data ();
1316 int report_change
= 0;
1318 if (command
->thread
== -1)
1320 report_change
= (!ptid_equal (previous_ptid
, null_ptid
)
1321 && !ptid_equal (inferior_ptid
, previous_ptid
)
1322 && !ptid_equal (inferior_ptid
, null_ptid
));
1324 else if (!ptid_equal (inferior_ptid
, null_ptid
))
1326 struct thread_info
*ti
= inferior_thread ();
1327 report_change
= (ti
->num
!= command
->thread
);
1332 struct thread_info
*ti
= inferior_thread ();
1333 target_terminal_ours ();
1334 fprintf_unfiltered (mi
->event_channel
,
1335 "thread-selected,id=\"%d\"",
1337 gdb_flush (mi
->event_channel
);
1341 mi_parse_free (command
);
1344 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1345 gdb_flush (raw_stdout
);
1346 /* Print any buffered hook code. */
1351 mi_cmd_execute (struct mi_parse
*parse
)
1353 struct cleanup
*cleanup
;
1356 prepare_execute_command ();
1358 cleanup
= make_cleanup (null_cleanup
, NULL
);
1360 if (parse
->frame
!= -1 && parse
->thread
== -1)
1361 error (_("Cannot specify --frame without --thread"));
1363 if (parse
->thread
!= -1)
1365 struct thread_info
*tp
= find_thread_id (parse
->thread
);
1367 error (_("Invalid thread id: %d"), parse
->thread
);
1369 if (is_exited (tp
->ptid
))
1370 error (_("Thread id: %d has terminated"), parse
->thread
);
1372 switch_to_thread (tp
->ptid
);
1375 if (parse
->frame
!= -1)
1377 struct frame_info
*fid
;
1378 int frame
= parse
->frame
;
1379 fid
= find_relative_frame (get_current_frame (), &frame
);
1381 /* find_relative_frame was successful */
1384 error (_("Invalid frame id: %d"), frame
);
1387 if (parse
->cmd
->argv_func
!= NULL
)
1388 parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1389 else if (parse
->cmd
->cli
.cmd
!= 0)
1391 /* FIXME: DELETE THIS. */
1392 /* The operation is still implemented by a cli command. */
1393 /* Must be a synchronous one. */
1394 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1399 /* FIXME: DELETE THIS. */
1400 struct ui_file
*stb
;
1402 stb
= mem_fileopen ();
1404 fputs_unfiltered ("Undefined mi command: ", stb
);
1405 fputstr_unfiltered (parse
->command
, '"', stb
);
1406 fputs_unfiltered (" (missing implementation)", stb
);
1408 make_cleanup_ui_file_delete (stb
);
1411 do_cleanups (cleanup
);
1414 /* FIXME: This is just a hack so we can get some extra commands going.
1415 We don't want to channel things through the CLI, but call libgdb directly.
1416 Use only for synchronous commands. */
1419 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1423 struct cleanup
*old_cleanups
;
1426 run
= xstrprintf ("%s %s", cmd
, args
);
1428 run
= xstrdup (cmd
);
1430 /* FIXME: gdb_???? */
1431 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1433 old_cleanups
= make_cleanup (xfree
, run
);
1434 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1435 do_cleanups (old_cleanups
);
1441 mi_execute_async_cli_command (char *cli_command
, char **argv
, int argc
)
1443 struct cleanup
*old_cleanups
;
1446 if (target_can_async_p ())
1447 run
= xstrprintf ("%s %s&", cli_command
, argc
? *argv
: "");
1449 run
= xstrprintf ("%s %s", cli_command
, argc
? *argv
: "");
1450 old_cleanups
= make_cleanup (xfree
, run
);
1452 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1454 if (target_can_async_p ())
1456 /* If we're not executing, an exception should have been throw. */
1457 gdb_assert (is_running (inferior_ptid
));
1458 do_cleanups (old_cleanups
);
1462 /* Do this before doing any printing. It would appear that some
1463 print code leaves garbage around in the buffer. */
1464 do_cleanups (old_cleanups
);
1469 mi_load_progress (const char *section_name
,
1470 unsigned long sent_so_far
,
1471 unsigned long total_section
,
1472 unsigned long total_sent
,
1473 unsigned long grand_total
)
1475 struct timeval time_now
, delta
, update_threshold
;
1476 static struct timeval last_update
;
1477 static char *previous_sect_name
= NULL
;
1479 struct ui_out
*saved_uiout
;
1481 /* This function is called through deprecated_show_load_progress
1482 which means uiout may not be correct. Fix it for the duration
1483 of this function. */
1484 saved_uiout
= uiout
;
1486 if (current_interp_named_p (INTERP_MI
)
1487 || current_interp_named_p (INTERP_MI2
))
1488 uiout
= mi_out_new (2);
1489 else if (current_interp_named_p (INTERP_MI1
))
1490 uiout
= mi_out_new (1);
1491 else if (current_interp_named_p (INTERP_MI3
))
1492 uiout
= mi_out_new (3);
1496 update_threshold
.tv_sec
= 0;
1497 update_threshold
.tv_usec
= 500000;
1498 gettimeofday (&time_now
, NULL
);
1500 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1501 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1503 if (delta
.tv_usec
< 0)
1506 delta
.tv_usec
+= 1000000L;
1509 new_section
= (previous_sect_name
?
1510 strcmp (previous_sect_name
, section_name
) : 1);
1513 struct cleanup
*cleanup_tuple
;
1514 xfree (previous_sect_name
);
1515 previous_sect_name
= xstrdup (section_name
);
1518 fputs_unfiltered (current_token
, raw_stdout
);
1519 fputs_unfiltered ("+download", raw_stdout
);
1520 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1521 ui_out_field_string (uiout
, "section", section_name
);
1522 ui_out_field_int (uiout
, "section-size", total_section
);
1523 ui_out_field_int (uiout
, "total-size", grand_total
);
1524 do_cleanups (cleanup_tuple
);
1525 mi_out_put (uiout
, raw_stdout
);
1526 fputs_unfiltered ("\n", raw_stdout
);
1527 gdb_flush (raw_stdout
);
1530 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1531 delta
.tv_usec
>= update_threshold
.tv_usec
)
1533 struct cleanup
*cleanup_tuple
;
1534 last_update
.tv_sec
= time_now
.tv_sec
;
1535 last_update
.tv_usec
= time_now
.tv_usec
;
1537 fputs_unfiltered (current_token
, raw_stdout
);
1538 fputs_unfiltered ("+download", raw_stdout
);
1539 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1540 ui_out_field_string (uiout
, "section", section_name
);
1541 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1542 ui_out_field_int (uiout
, "section-size", total_section
);
1543 ui_out_field_int (uiout
, "total-sent", total_sent
);
1544 ui_out_field_int (uiout
, "total-size", grand_total
);
1545 do_cleanups (cleanup_tuple
);
1546 mi_out_put (uiout
, raw_stdout
);
1547 fputs_unfiltered ("\n", raw_stdout
);
1548 gdb_flush (raw_stdout
);
1552 uiout
= saved_uiout
;
1556 timestamp (struct mi_timestamp
*tv
)
1559 gettimeofday (&tv
->wallclock
, NULL
);
1560 #ifdef HAVE_GETRUSAGE
1561 getrusage (RUSAGE_SELF
, &rusage
);
1562 tv
->utime
.tv_sec
= rusage
.ru_utime
.tv_sec
;
1563 tv
->utime
.tv_usec
= rusage
.ru_utime
.tv_usec
;
1564 tv
->stime
.tv_sec
= rusage
.ru_stime
.tv_sec
;
1565 tv
->stime
.tv_usec
= rusage
.ru_stime
.tv_usec
;
1567 usec
= get_run_time ();
1568 tv
->utime
.tv_sec
= usec
/1000000L;
1569 tv
->utime
.tv_usec
= usec
- 1000000L*tv
->utime
.tv_sec
;
1570 tv
->stime
.tv_sec
= 0;
1571 tv
->stime
.tv_usec
= 0;
1576 print_diff_now (struct mi_timestamp
*start
)
1578 struct mi_timestamp now
;
1580 print_diff (start
, &now
);
1584 mi_print_timing_maybe (void)
1586 /* If the command is -enable-timing then do_timings may be
1587 true whilst current_command_ts is not initialized. */
1588 if (do_timings
&& current_command_ts
)
1589 print_diff_now (current_command_ts
);
1593 timeval_diff (struct timeval start
, struct timeval end
)
1595 return ((end
.tv_sec
- start
.tv_sec
) * 1000000L)
1596 + (end
.tv_usec
- start
.tv_usec
);
1600 print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
)
1604 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1605 timeval_diff (start
->wallclock
, end
->wallclock
) / 1000000.0,
1606 timeval_diff (start
->utime
, end
->utime
) / 1000000.0,
1607 timeval_diff (start
->stime
, end
->stime
) / 1000000.0);