3 Copyright (C) 2000-2017 Free Software Foundation, Inc.
5 Contributed by Cygnus Solutions (a Red Hat company).
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "arch-utils.h"
28 #include "gdbthread.h"
31 #include "mi-getopt.h"
32 #include "mi-console.h"
36 #include "event-loop.h"
37 #include "event-top.h"
38 #include "gdbcore.h" /* For write_memory(). */
44 #include "mi-common.h"
49 #include "splay-tree.h"
50 #include "tracepoint.h"
54 #include "extension.h"
59 #include "run-time-clock.h"
69 /* This is used to pass the current command timestamp down to
70 continuation routines. */
71 static struct mi_timestamp
*current_command_ts
;
73 static int do_timings
= 0;
76 /* Few commands would like to know if options like --thread-group were
77 explicitly specified. This variable keeps the current parsed
78 command including all option, and make it possible. */
79 static struct mi_parse
*current_context
;
81 int running_result_record_printed
= 1;
83 /* Flag indicating that the target has proceeded since the last
84 command was issued. */
87 extern void _initialize_mi_main (void);
88 static void mi_cmd_execute (struct mi_parse
*parse
);
90 static void mi_execute_cli_command (const char *cmd
, int args_p
,
92 static void mi_execute_async_cli_command (char *cli_command
,
93 char **argv
, int argc
);
94 static int register_changed_p (int regnum
, struct regcache
*,
96 static void output_register (struct frame_info
*, int regnum
, int format
,
97 int skip_unavailable
);
99 /* Controls whether the frontend wants MI in async mode. */
100 static int mi_async
= 0;
102 /* The set command writes to this variable. If the inferior is
103 executing, mi_async is *not* updated. */
104 static int mi_async_1
= 0;
107 set_mi_async_command (char *args
, int from_tty
,
108 struct cmd_list_element
*c
)
110 if (have_live_inferiors ())
112 mi_async_1
= mi_async
;
113 error (_("Cannot change this setting while the inferior is running."));
116 mi_async
= mi_async_1
;
120 show_mi_async_command (struct ui_file
*file
, int from_tty
,
121 struct cmd_list_element
*c
,
124 fprintf_filtered (file
,
125 _("Whether MI is in asynchronous mode is %s.\n"),
129 /* A wrapper for target_can_async_p that takes the MI setting into
135 return mi_async
&& target_can_async_p ();
138 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
139 layer that calls libgdb. Any operation used in the below should be
142 static void timestamp (struct mi_timestamp
*tv
);
144 static void print_diff (struct ui_file
*file
, struct mi_timestamp
*start
,
145 struct mi_timestamp
*end
);
148 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
150 struct mi_interp
*mi
= (struct mi_interp
*) current_interpreter ();
152 /* We have to print everything right here because we never return. */
154 fputs_unfiltered (current_token
, mi
->raw_stdout
);
155 fputs_unfiltered ("^exit\n", mi
->raw_stdout
);
156 mi_out_put (current_uiout
, mi
->raw_stdout
);
157 gdb_flush (mi
->raw_stdout
);
158 /* FIXME: The function called is not yet a formal libgdb function. */
159 quit_force (NULL
, FROM_TTY
);
163 mi_cmd_exec_next (char *command
, char **argv
, int argc
)
165 /* FIXME: Should call a libgdb function, not a cli wrapper. */
166 if (argc
> 0 && strcmp(argv
[0], "--reverse") == 0)
167 mi_execute_async_cli_command ("reverse-next", argv
+ 1, argc
- 1);
169 mi_execute_async_cli_command ("next", argv
, argc
);
173 mi_cmd_exec_next_instruction (char *command
, char **argv
, int argc
)
175 /* FIXME: Should call a libgdb function, not a cli wrapper. */
176 if (argc
> 0 && strcmp(argv
[0], "--reverse") == 0)
177 mi_execute_async_cli_command ("reverse-nexti", argv
+ 1, argc
- 1);
179 mi_execute_async_cli_command ("nexti", argv
, argc
);
183 mi_cmd_exec_step (char *command
, char **argv
, int argc
)
185 /* FIXME: Should call a libgdb function, not a cli wrapper. */
186 if (argc
> 0 && strcmp(argv
[0], "--reverse") == 0)
187 mi_execute_async_cli_command ("reverse-step", argv
+ 1, argc
- 1);
189 mi_execute_async_cli_command ("step", argv
, argc
);
193 mi_cmd_exec_step_instruction (char *command
, char **argv
, int argc
)
195 /* FIXME: Should call a libgdb function, not a cli wrapper. */
196 if (argc
> 0 && strcmp(argv
[0], "--reverse") == 0)
197 mi_execute_async_cli_command ("reverse-stepi", argv
+ 1, argc
- 1);
199 mi_execute_async_cli_command ("stepi", argv
, argc
);
203 mi_cmd_exec_finish (char *command
, char **argv
, int argc
)
205 /* FIXME: Should call a libgdb function, not a cli wrapper. */
206 if (argc
> 0 && strcmp(argv
[0], "--reverse") == 0)
207 mi_execute_async_cli_command ("reverse-finish", argv
+ 1, argc
- 1);
209 mi_execute_async_cli_command ("finish", argv
, argc
);
213 mi_cmd_exec_return (char *command
, char **argv
, int argc
)
215 /* This command doesn't really execute the target, it just pops the
216 specified number of frames. */
218 /* Call return_command with from_tty argument equal to 0 so as to
219 avoid being queried. */
220 return_command (*argv
, 0);
222 /* Call return_command with from_tty argument equal to 0 so as to
223 avoid being queried. */
224 return_command (NULL
, 0);
226 /* Because we have called return_command with from_tty = 0, we need
227 to print the frame here. */
228 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
, 1);
232 mi_cmd_exec_jump (char *args
, char **argv
, int argc
)
234 /* FIXME: Should call a libgdb function, not a cli wrapper. */
235 mi_execute_async_cli_command ("jump", argv
, argc
);
239 proceed_thread (struct thread_info
*thread
, int pid
)
241 if (!is_stopped (thread
->ptid
))
244 if (pid
!= 0 && ptid_get_pid (thread
->ptid
) != pid
)
247 switch_to_thread (thread
->ptid
);
248 clear_proceed_status (0);
249 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
253 proceed_thread_callback (struct thread_info
*thread
, void *arg
)
255 int pid
= *(int *)arg
;
257 proceed_thread (thread
, pid
);
262 exec_continue (char **argv
, int argc
)
264 prepare_execution_command (¤t_target
, mi_async_p ());
268 /* In non-stop mode, 'resume' always resumes a single thread.
269 Therefore, to resume all threads of the current inferior, or
270 all threads in all inferiors, we need to iterate over
273 See comment on infcmd.c:proceed_thread_callback for rationale. */
274 if (current_context
->all
|| current_context
->thread_group
!= -1)
277 struct cleanup
*back_to
= make_cleanup_restore_current_thread ();
279 if (!current_context
->all
)
282 = find_inferior_id (current_context
->thread_group
);
286 iterate_over_threads (proceed_thread_callback
, &pid
);
287 do_cleanups (back_to
);
296 scoped_restore save_multi
= make_scoped_restore (&sched_multi
);
298 if (current_context
->all
)
305 /* In all-stop mode, -exec-continue traditionally resumed
306 either all threads, or one thread, depending on the
307 'scheduler-locking' variable. Let's continue to do the
315 exec_direction_forward (void *notused
)
317 execution_direction
= EXEC_FORWARD
;
321 exec_reverse_continue (char **argv
, int argc
)
323 enum exec_direction_kind dir
= execution_direction
;
324 struct cleanup
*old_chain
;
326 if (dir
== EXEC_REVERSE
)
327 error (_("Already in reverse mode."));
329 if (!target_can_execute_reverse
)
330 error (_("Target %s does not support this command."), target_shortname
);
332 old_chain
= make_cleanup (exec_direction_forward
, NULL
);
333 execution_direction
= EXEC_REVERSE
;
334 exec_continue (argv
, argc
);
335 do_cleanups (old_chain
);
339 mi_cmd_exec_continue (char *command
, char **argv
, int argc
)
341 if (argc
> 0 && strcmp (argv
[0], "--reverse") == 0)
342 exec_reverse_continue (argv
+ 1, argc
- 1);
344 exec_continue (argv
, argc
);
348 interrupt_thread_callback (struct thread_info
*thread
, void *arg
)
350 int pid
= *(int *)arg
;
352 if (!is_running (thread
->ptid
))
355 if (ptid_get_pid (thread
->ptid
) != pid
)
358 target_stop (thread
->ptid
);
362 /* Interrupt the execution of the target. Note how we must play
363 around with the token variables, in order to display the current
364 token in the result of the interrupt command, and the previous
365 execution token when the target finally stops. See comments in
369 mi_cmd_exec_interrupt (char *command
, char **argv
, int argc
)
371 /* In all-stop mode, everything stops, so we don't need to try
372 anything specific. */
375 interrupt_target_1 (0);
379 if (current_context
->all
)
381 /* This will interrupt all threads in all inferiors. */
382 interrupt_target_1 (1);
384 else if (current_context
->thread_group
!= -1)
386 struct inferior
*inf
= find_inferior_id (current_context
->thread_group
);
388 iterate_over_threads (interrupt_thread_callback
, &inf
->pid
);
392 /* Interrupt just the current thread -- either explicitly
393 specified via --thread or whatever was current before
394 MI command was sent. */
395 interrupt_target_1 (0);
399 /* Callback for iterate_over_inferiors which starts the execution
400 of the given inferior.
402 ARG is a pointer to an integer whose value, if non-zero, indicates
403 that the program should be stopped when reaching the main subprogram
404 (similar to what the CLI "start" command does). */
407 run_one_inferior (struct inferior
*inf
, void *arg
)
409 int start_p
= *(int *) arg
;
410 const char *run_cmd
= start_p
? "start" : "run";
411 struct target_ops
*run_target
= find_run_target ();
412 int async_p
= mi_async
&& run_target
->to_can_async_p (run_target
);
416 if (inf
->pid
!= ptid_get_pid (inferior_ptid
))
418 struct thread_info
*tp
;
420 tp
= any_thread_of_process (inf
->pid
);
422 error (_("Inferior has no threads."));
424 switch_to_thread (tp
->ptid
);
429 set_current_inferior (inf
);
430 switch_to_thread (null_ptid
);
431 set_current_program_space (inf
->pspace
);
433 mi_execute_cli_command (run_cmd
, async_p
,
434 async_p
? "&" : NULL
);
439 mi_cmd_exec_run (char *command
, char **argv
, int argc
)
443 /* Parse the command options. */
448 static const struct mi_opt opts
[] =
450 {"-start", START_OPT
, 0},
459 int opt
= mi_getopt ("-exec-run", argc
, argv
, opts
, &oind
, &oarg
);
463 switch ((enum opt
) opt
)
471 /* This command does not accept any argument. Make sure the user
472 did not provide any. */
474 error (_("Invalid argument: %s"), argv
[oind
]);
476 if (current_context
->all
)
478 struct cleanup
*back_to
= save_current_space_and_thread ();
480 iterate_over_inferiors (run_one_inferior
, &start_p
);
481 do_cleanups (back_to
);
485 const char *run_cmd
= start_p
? "start" : "run";
486 struct target_ops
*run_target
= find_run_target ();
487 int async_p
= mi_async
&& run_target
->to_can_async_p (run_target
);
489 mi_execute_cli_command (run_cmd
, async_p
,
490 async_p
? "&" : NULL
);
496 find_thread_of_process (struct thread_info
*ti
, void *p
)
500 if (ptid_get_pid (ti
->ptid
) == pid
&& !is_exited (ti
->ptid
))
507 mi_cmd_target_detach (char *command
, char **argv
, int argc
)
509 if (argc
!= 0 && argc
!= 1)
510 error (_("Usage: -target-detach [pid | thread-group]"));
514 struct thread_info
*tp
;
518 /* First see if we are dealing with a thread-group id. */
521 struct inferior
*inf
;
522 int id
= strtoul (argv
[0] + 1, &end
, 0);
525 error (_("Invalid syntax of thread-group id '%s'"), argv
[0]);
527 inf
= find_inferior_id (id
);
529 error (_("Non-existent thread-group id '%d'"), id
);
535 /* We must be dealing with a pid. */
536 pid
= strtol (argv
[0], &end
, 10);
539 error (_("Invalid identifier '%s'"), argv
[0]);
542 /* Pick any thread in the desired process. Current
543 target_detach detaches from the parent of inferior_ptid. */
544 tp
= iterate_over_threads (find_thread_of_process
, &pid
);
546 error (_("Thread group is empty"));
548 switch_to_thread (tp
->ptid
);
551 detach_command (NULL
, 0);
555 mi_cmd_target_flash_erase (char *command
, char **argv
, int argc
)
557 flash_erase_command (NULL
, 0);
561 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
564 char *mi_error_message
;
565 ptid_t previous_ptid
= inferior_ptid
;
568 error (_("-thread-select: USAGE: threadnum."));
570 rc
= gdb_thread_select (current_uiout
, argv
[0], &mi_error_message
);
572 /* If thread switch did not succeed don't notify or print. */
573 if (rc
== GDB_RC_FAIL
)
575 make_cleanup (xfree
, mi_error_message
);
576 error ("%s", mi_error_message
);
579 print_selected_thread_frame (current_uiout
,
580 USER_SELECTED_THREAD
| USER_SELECTED_FRAME
);
582 /* Notify if the thread has effectively changed. */
583 if (!ptid_equal (inferior_ptid
, previous_ptid
))
585 observer_notify_user_selected_context_changed (USER_SELECTED_THREAD
586 | USER_SELECTED_FRAME
);
591 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
594 char *mi_error_message
;
597 error (_("-thread-list-ids: No arguments required."));
599 rc
= gdb_list_thread_ids (current_uiout
, &mi_error_message
);
601 if (rc
== GDB_RC_FAIL
)
603 make_cleanup (xfree
, mi_error_message
);
604 error ("%s", mi_error_message
);
609 mi_cmd_thread_info (char *command
, char **argv
, int argc
)
611 if (argc
!= 0 && argc
!= 1)
612 error (_("Invalid MI command"));
614 print_thread_info (current_uiout
, argv
[0], -1);
617 struct collect_cores_data
625 collect_cores (struct thread_info
*ti
, void *xdata
)
627 struct collect_cores_data
*data
= (struct collect_cores_data
*) xdata
;
629 if (ptid_get_pid (ti
->ptid
) == data
->pid
)
631 int core
= target_core_of_thread (ti
->ptid
);
634 VEC_safe_push (int, data
->cores
, core
);
641 unique (int *b
, int *e
)
651 struct print_one_inferior_data
654 VEC (int) *inferiors
;
658 print_one_inferior (struct inferior
*inferior
, void *xdata
)
660 struct print_one_inferior_data
*top_data
661 = (struct print_one_inferior_data
*) xdata
;
662 struct ui_out
*uiout
= current_uiout
;
664 if (VEC_empty (int, top_data
->inferiors
)
665 || bsearch (&(inferior
->pid
), VEC_address (int, top_data
->inferiors
),
666 VEC_length (int, top_data
->inferiors
), sizeof (int),
667 compare_positive_ints
))
669 struct collect_cores_data data
;
670 struct cleanup
*back_to
671 = make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
673 uiout
->field_fmt ("id", "i%d", inferior
->num
);
674 uiout
->field_string ("type", "process");
675 if (inferior
->has_exit_code
)
676 uiout
->field_string ("exit-code",
677 int_string (inferior
->exit_code
, 8, 0, 0, 1));
678 if (inferior
->pid
!= 0)
679 uiout
->field_int ("pid", inferior
->pid
);
681 if (inferior
->pspace
->pspace_exec_filename
!= NULL
)
683 uiout
->field_string ("executable",
684 inferior
->pspace
->pspace_exec_filename
);
688 if (inferior
->pid
!= 0)
690 data
.pid
= inferior
->pid
;
691 iterate_over_threads (collect_cores
, &data
);
694 if (!VEC_empty (int, data
.cores
))
697 struct cleanup
*back_to_2
=
698 make_cleanup_ui_out_list_begin_end (uiout
, "cores");
700 qsort (VEC_address (int, data
.cores
),
701 VEC_length (int, data
.cores
), sizeof (int),
702 compare_positive_ints
);
704 b
= VEC_address (int, data
.cores
);
705 e
= b
+ VEC_length (int, data
.cores
);
709 uiout
->field_int (NULL
, *b
);
711 do_cleanups (back_to_2
);
714 if (top_data
->recurse
)
715 print_thread_info (uiout
, NULL
, inferior
->pid
);
717 do_cleanups (back_to
);
723 /* Output a field named 'cores' with a list as the value. The
724 elements of the list are obtained by splitting 'cores' on
728 output_cores (struct ui_out
*uiout
, const char *field_name
, const char *xcores
)
730 struct cleanup
*back_to
= make_cleanup_ui_out_list_begin_end (uiout
,
732 char *cores
= xstrdup (xcores
);
735 make_cleanup (xfree
, cores
);
737 for (p
= strtok (p
, ","); p
; p
= strtok (NULL
, ","))
738 uiout
->field_string (NULL
, p
);
740 do_cleanups (back_to
);
744 free_vector_of_ints (void *xvector
)
746 VEC (int) **vector
= (VEC (int) **) xvector
;
748 VEC_free (int, *vector
);
752 do_nothing (splay_tree_key k
)
757 free_vector_of_osdata_items (splay_tree_value xvalue
)
759 VEC (osdata_item_s
) *value
= (VEC (osdata_item_s
) *) xvalue
;
761 /* We don't free the items itself, it will be done separately. */
762 VEC_free (osdata_item_s
, value
);
766 splay_tree_int_comparator (splay_tree_key xa
, splay_tree_key xb
)
775 free_splay_tree (void *xt
)
777 splay_tree t
= (splay_tree
) xt
;
778 splay_tree_delete (t
);
782 list_available_thread_groups (VEC (int) *ids
, int recurse
)
785 struct osdata_item
*item
;
787 struct ui_out
*uiout
= current_uiout
;
788 struct cleanup
*cleanup
;
790 /* This keeps a map from integer (pid) to VEC (struct osdata_item *)*
791 The vector contains information about all threads for the given pid.
792 This is assigned an initial value to avoid "may be used uninitialized"
794 splay_tree tree
= NULL
;
796 /* get_osdata will throw if it cannot return data. */
797 data
= get_osdata ("processes");
798 cleanup
= make_cleanup_osdata_free (data
);
802 struct osdata
*threads
= get_osdata ("threads");
804 make_cleanup_osdata_free (threads
);
805 tree
= splay_tree_new (splay_tree_int_comparator
,
807 free_vector_of_osdata_items
);
808 make_cleanup (free_splay_tree
, tree
);
811 VEC_iterate (osdata_item_s
, threads
->items
,
815 const char *pid
= get_osdata_column (item
, "pid");
816 int pid_i
= strtoul (pid
, NULL
, 0);
817 VEC (osdata_item_s
) *vec
= 0;
819 splay_tree_node n
= splay_tree_lookup (tree
, pid_i
);
822 VEC_safe_push (osdata_item_s
, vec
, item
);
823 splay_tree_insert (tree
, pid_i
, (splay_tree_value
)vec
);
827 vec
= (VEC (osdata_item_s
) *) n
->value
;
828 VEC_safe_push (osdata_item_s
, vec
, item
);
829 n
->value
= (splay_tree_value
) vec
;
834 make_cleanup_ui_out_list_begin_end (uiout
, "groups");
837 VEC_iterate (osdata_item_s
, data
->items
,
841 struct cleanup
*back_to
;
843 const char *pid
= get_osdata_column (item
, "pid");
844 const char *cmd
= get_osdata_column (item
, "command");
845 const char *user
= get_osdata_column (item
, "user");
846 const char *cores
= get_osdata_column (item
, "cores");
848 int pid_i
= strtoul (pid
, NULL
, 0);
850 /* At present, the target will return all available processes
851 and if information about specific ones was required, we filter
852 undesired processes here. */
853 if (ids
&& bsearch (&pid_i
, VEC_address (int, ids
),
854 VEC_length (int, ids
),
855 sizeof (int), compare_positive_ints
) == NULL
)
859 back_to
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
861 uiout
->field_fmt ("id", "%s", pid
);
862 uiout
->field_string ("type", "process");
864 uiout
->field_string ("description", cmd
);
866 uiout
->field_string ("user", user
);
868 output_cores (uiout
, "cores", cores
);
872 splay_tree_node n
= splay_tree_lookup (tree
, pid_i
);
875 VEC (osdata_item_s
) *children
= (VEC (osdata_item_s
) *) n
->value
;
876 struct osdata_item
*child
;
879 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
882 VEC_iterate (osdata_item_s
, children
, ix_child
, child
);
885 struct cleanup
*back_to_2
=
886 make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
887 const char *tid
= get_osdata_column (child
, "tid");
888 const char *tcore
= get_osdata_column (child
, "core");
890 uiout
->field_string ("id", tid
);
892 uiout
->field_string ("core", tcore
);
894 do_cleanups (back_to_2
);
899 do_cleanups (back_to
);
902 do_cleanups (cleanup
);
906 mi_cmd_list_thread_groups (char *command
, char **argv
, int argc
)
908 struct ui_out
*uiout
= current_uiout
;
909 struct cleanup
*back_to
;
916 AVAILABLE_OPT
, RECURSE_OPT
918 static const struct mi_opt opts
[] =
920 {"-available", AVAILABLE_OPT
, 0},
921 {"-recurse", RECURSE_OPT
, 1},
930 int opt
= mi_getopt ("-list-thread-groups", argc
, argv
, opts
,
935 switch ((enum opt
) opt
)
941 if (strcmp (oarg
, "0") == 0)
943 else if (strcmp (oarg
, "1") == 0)
946 error (_("only '0' and '1' are valid values "
947 "for the '--recurse' option"));
952 for (; oind
< argc
; ++oind
)
957 if (*(argv
[oind
]) != 'i')
958 error (_("invalid syntax of group id '%s'"), argv
[oind
]);
960 inf
= strtoul (argv
[oind
] + 1, &end
, 0);
963 error (_("invalid syntax of group id '%s'"), argv
[oind
]);
964 VEC_safe_push (int, ids
, inf
);
966 if (VEC_length (int, ids
) > 1)
967 qsort (VEC_address (int, ids
),
968 VEC_length (int, ids
),
969 sizeof (int), compare_positive_ints
);
971 back_to
= make_cleanup (free_vector_of_ints
, &ids
);
975 list_available_thread_groups (ids
, recurse
);
977 else if (VEC_length (int, ids
) == 1)
979 /* Local thread groups, single id. */
980 int id
= *VEC_address (int, ids
);
981 struct inferior
*inf
= find_inferior_id (id
);
984 error (_("Non-existent thread group id '%d'"), id
);
986 print_thread_info (uiout
, NULL
, inf
->pid
);
990 struct print_one_inferior_data data
;
992 data
.recurse
= recurse
;
993 data
.inferiors
= ids
;
995 /* Local thread groups. Either no explicit ids -- and we
996 print everything, or several explicit ids. In both cases,
997 we print more than one group, and have to use 'groups'
998 as the top-level element. */
999 make_cleanup_ui_out_list_begin_end (uiout
, "groups");
1000 update_thread_list ();
1001 iterate_over_inferiors (print_one_inferior
, &data
);
1004 do_cleanups (back_to
);
1008 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
1010 struct gdbarch
*gdbarch
;
1011 struct ui_out
*uiout
= current_uiout
;
1012 int regnum
, numregs
;
1014 struct cleanup
*cleanup
;
1016 /* Note that the test for a valid register must include checking the
1017 gdbarch_register_name because gdbarch_num_regs may be allocated
1018 for the union of the register sets within a family of related
1019 processors. In this case, some entries of gdbarch_register_name
1020 will change depending upon the particular processor being
1023 gdbarch
= get_current_arch ();
1024 numregs
= gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1026 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
1028 if (argc
== 0) /* No args, just do all the regs. */
1034 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
1035 || *(gdbarch_register_name (gdbarch
, regnum
)) == '\0')
1036 uiout
->field_string (NULL
, "");
1038 uiout
->field_string (NULL
, gdbarch_register_name (gdbarch
, regnum
));
1042 /* Else, list of register #s, just do listed regs. */
1043 for (i
= 0; i
< argc
; i
++)
1045 regnum
= atoi (argv
[i
]);
1046 if (regnum
< 0 || regnum
>= numregs
)
1047 error (_("bad register number"));
1049 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
1050 || *(gdbarch_register_name (gdbarch
, regnum
)) == '\0')
1051 uiout
->field_string (NULL
, "");
1053 uiout
->field_string (NULL
, gdbarch_register_name (gdbarch
, regnum
));
1055 do_cleanups (cleanup
);
1059 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
1061 static struct regcache
*this_regs
= NULL
;
1062 struct ui_out
*uiout
= current_uiout
;
1063 struct regcache
*prev_regs
;
1064 struct gdbarch
*gdbarch
;
1065 int regnum
, numregs
, changed
;
1067 struct cleanup
*cleanup
;
1069 /* The last time we visited this function, the current frame's
1070 register contents were saved in THIS_REGS. Move THIS_REGS over
1071 to PREV_REGS, and refresh THIS_REGS with the now-current register
1074 prev_regs
= this_regs
;
1075 this_regs
= frame_save_as_regcache (get_selected_frame (NULL
));
1076 cleanup
= make_cleanup_regcache_xfree (prev_regs
);
1078 /* Note that the test for a valid register must include checking the
1079 gdbarch_register_name because gdbarch_num_regs may be allocated
1080 for the union of the register sets within a family of related
1081 processors. In this case, some entries of gdbarch_register_name
1082 will change depending upon the particular processor being
1085 gdbarch
= get_regcache_arch (this_regs
);
1086 numregs
= gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1088 make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
1092 /* No args, just do all the regs. */
1097 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
1098 || *(gdbarch_register_name (gdbarch
, regnum
)) == '\0')
1100 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
1102 error (_("-data-list-changed-registers: "
1103 "Unable to read register contents."));
1105 uiout
->field_int (NULL
, regnum
);
1109 /* Else, list of register #s, just do listed regs. */
1110 for (i
= 0; i
< argc
; i
++)
1112 regnum
= atoi (argv
[i
]);
1116 && gdbarch_register_name (gdbarch
, regnum
) != NULL
1117 && *gdbarch_register_name (gdbarch
, regnum
) != '\000')
1119 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
1121 error (_("-data-list-changed-registers: "
1122 "Unable to read register contents."));
1124 uiout
->field_int (NULL
, regnum
);
1127 error (_("bad register number"));
1129 do_cleanups (cleanup
);
1133 register_changed_p (int regnum
, struct regcache
*prev_regs
,
1134 struct regcache
*this_regs
)
1136 struct gdbarch
*gdbarch
= get_regcache_arch (this_regs
);
1137 gdb_byte prev_buffer
[MAX_REGISTER_SIZE
];
1138 gdb_byte this_buffer
[MAX_REGISTER_SIZE
];
1139 enum register_status prev_status
;
1140 enum register_status this_status
;
1142 /* First time through or after gdbarch change consider all registers
1144 if (!prev_regs
|| get_regcache_arch (prev_regs
) != gdbarch
)
1147 /* Get register contents and compare. */
1148 prev_status
= regcache_cooked_read (prev_regs
, regnum
, prev_buffer
);
1149 this_status
= regcache_cooked_read (this_regs
, regnum
, this_buffer
);
1151 if (this_status
!= prev_status
)
1153 else if (this_status
== REG_VALID
)
1154 return memcmp (prev_buffer
, this_buffer
,
1155 register_size (gdbarch
, regnum
)) != 0;
1160 /* Return a list of register number and value pairs. The valid
1161 arguments expected are: a letter indicating the format in which to
1162 display the registers contents. This can be one of: x
1163 (hexadecimal), d (decimal), N (natural), t (binary), o (octal), r
1164 (raw). After the format argument there can be a sequence of
1165 numbers, indicating which registers to fetch the content of. If
1166 the format is the only argument, a list of all the registers with
1167 their values is returned. */
1170 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
1172 struct ui_out
*uiout
= current_uiout
;
1173 struct frame_info
*frame
;
1174 struct gdbarch
*gdbarch
;
1175 int regnum
, numregs
, format
;
1177 struct cleanup
*list_cleanup
;
1178 int skip_unavailable
= 0;
1184 static const struct mi_opt opts
[] =
1186 {"-skip-unavailable", SKIP_UNAVAILABLE
, 0},
1190 /* Note that the test for a valid register must include checking the
1191 gdbarch_register_name because gdbarch_num_regs may be allocated
1192 for the union of the register sets within a family of related
1193 processors. In this case, some entries of gdbarch_register_name
1194 will change depending upon the particular processor being
1200 int opt
= mi_getopt ("-data-list-register-values", argc
, argv
,
1201 opts
, &oind
, &oarg
);
1205 switch ((enum opt
) opt
)
1207 case SKIP_UNAVAILABLE
:
1208 skip_unavailable
= 1;
1213 if (argc
- oind
< 1)
1214 error (_("-data-list-register-values: Usage: "
1215 "-data-list-register-values [--skip-unavailable] <format>"
1216 " [<regnum1>...<regnumN>]"));
1218 format
= (int) argv
[oind
][0];
1220 frame
= get_selected_frame (NULL
);
1221 gdbarch
= get_frame_arch (frame
);
1222 numregs
= gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1224 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
1226 if (argc
- oind
== 1)
1228 /* No args, beside the format: do all the regs. */
1233 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
1234 || *(gdbarch_register_name (gdbarch
, regnum
)) == '\0')
1237 output_register (frame
, regnum
, format
, skip_unavailable
);
1241 /* Else, list of register #s, just do listed regs. */
1242 for (i
= 1 + oind
; i
< argc
; i
++)
1244 regnum
= atoi (argv
[i
]);
1248 && gdbarch_register_name (gdbarch
, regnum
) != NULL
1249 && *gdbarch_register_name (gdbarch
, regnum
) != '\000')
1250 output_register (frame
, regnum
, format
, skip_unavailable
);
1252 error (_("bad register number"));
1254 do_cleanups (list_cleanup
);
1257 /* Output one register REGNUM's contents in the desired FORMAT. If
1258 SKIP_UNAVAILABLE is true, skip the register if it is
1262 output_register (struct frame_info
*frame
, int regnum
, int format
,
1263 int skip_unavailable
)
1265 struct ui_out
*uiout
= current_uiout
;
1266 struct value
*val
= value_of_register (regnum
, frame
);
1267 struct cleanup
*tuple_cleanup
;
1268 struct value_print_options opts
;
1270 if (skip_unavailable
&& !value_entirely_available (val
))
1273 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1274 uiout
->field_int ("number", regnum
);
1284 get_formatted_print_options (&opts
, format
);
1286 val_print (value_type (val
),
1287 value_embedded_offset (val
), 0,
1288 &stb
, 0, val
, &opts
, current_language
);
1289 uiout
->field_stream ("value", stb
);
1291 do_cleanups (tuple_cleanup
);
1294 /* Write given values into registers. The registers and values are
1295 given as pairs. The corresponding MI command is
1296 -data-write-register-values <format>
1297 [<regnum1> <value1>...<regnumN> <valueN>] */
1299 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
1301 struct regcache
*regcache
;
1302 struct gdbarch
*gdbarch
;
1305 /* Note that the test for a valid register must include checking the
1306 gdbarch_register_name because gdbarch_num_regs may be allocated
1307 for the union of the register sets within a family of related
1308 processors. In this case, some entries of gdbarch_register_name
1309 will change depending upon the particular processor being
1312 regcache
= get_current_regcache ();
1313 gdbarch
= get_regcache_arch (regcache
);
1314 numregs
= gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1317 error (_("-data-write-register-values: Usage: -data-write-register-"
1318 "values <format> [<regnum1> <value1>...<regnumN> <valueN>]"));
1320 if (!target_has_registers
)
1321 error (_("-data-write-register-values: No registers."));
1324 error (_("-data-write-register-values: No regs and values specified."));
1327 error (_("-data-write-register-values: "
1328 "Regs and vals are not in pairs."));
1330 for (i
= 1; i
< argc
; i
= i
+ 2)
1332 int regnum
= atoi (argv
[i
]);
1334 if (regnum
>= 0 && regnum
< numregs
1335 && gdbarch_register_name (gdbarch
, regnum
)
1336 && *gdbarch_register_name (gdbarch
, regnum
))
1340 /* Get the value as a number. */
1341 value
= parse_and_eval_address (argv
[i
+ 1]);
1343 /* Write it down. */
1344 regcache_cooked_write_signed (regcache
, regnum
, value
);
1347 error (_("bad register number"));
1351 /* Evaluate the value of the argument. The argument is an
1352 expression. If the expression contains spaces it needs to be
1353 included in double quotes. */
1356 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
1359 struct value_print_options opts
;
1360 struct ui_out
*uiout
= current_uiout
;
1363 error (_("-data-evaluate-expression: "
1364 "Usage: -data-evaluate-expression expression"));
1366 expression_up expr
= parse_expression (argv
[0]);
1368 val
= evaluate_expression (expr
.get ());
1372 /* Print the result of the expression evaluation. */
1373 get_user_print_options (&opts
);
1375 common_val_print (val
, &stb
, 0, &opts
, current_language
);
1377 uiout
->field_stream ("value", stb
);
1380 /* This is the -data-read-memory command.
1382 ADDR: start address of data to be dumped.
1383 WORD-FORMAT: a char indicating format for the ``word''. See
1385 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
1386 NR_ROW: Number of rows.
1387 NR_COL: The number of colums (words per row).
1388 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
1389 ASCHAR for unprintable characters.
1391 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
1392 displayes them. Returns:
1394 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
1397 The number of bytes read is SIZE*ROW*COL. */
1400 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
1402 struct gdbarch
*gdbarch
= get_current_arch ();
1403 struct ui_out
*uiout
= current_uiout
;
1405 long total_bytes
, nr_cols
, nr_rows
;
1407 struct type
*word_type
;
1419 static const struct mi_opt opts
[] =
1421 {"o", OFFSET_OPT
, 1},
1427 int opt
= mi_getopt ("-data-read-memory", argc
, argv
, opts
,
1432 switch ((enum opt
) opt
)
1435 offset
= atol (oarg
);
1442 if (argc
< 5 || argc
> 6)
1443 error (_("-data-read-memory: Usage: "
1444 "ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR]."));
1446 /* Extract all the arguments. */
1448 /* Start address of the memory dump. */
1449 addr
= parse_and_eval_address (argv
[0]) + offset
;
1450 /* The format character to use when displaying a memory word. See
1451 the ``x'' command. */
1452 word_format
= argv
[1][0];
1453 /* The size of the memory word. */
1454 word_size
= atol (argv
[2]);
1458 word_type
= builtin_type (gdbarch
)->builtin_int8
;
1462 word_type
= builtin_type (gdbarch
)->builtin_int16
;
1466 word_type
= builtin_type (gdbarch
)->builtin_int32
;
1470 word_type
= builtin_type (gdbarch
)->builtin_int64
;
1474 word_type
= builtin_type (gdbarch
)->builtin_int8
;
1477 /* The number of rows. */
1478 nr_rows
= atol (argv
[3]);
1480 error (_("-data-read-memory: invalid number of rows."));
1482 /* Number of bytes per row. */
1483 nr_cols
= atol (argv
[4]);
1485 error (_("-data-read-memory: invalid number of columns."));
1487 /* The un-printable character when printing ascii. */
1493 /* Create a buffer and read it in. */
1494 total_bytes
= word_size
* nr_rows
* nr_cols
;
1496 std::unique_ptr
<gdb_byte
[]> mbuf (new gdb_byte
[total_bytes
]);
1498 /* Dispatch memory reads to the topmost target, not the flattened
1500 nr_bytes
= target_read (current_target
.beneath
,
1501 TARGET_OBJECT_MEMORY
, NULL
, mbuf
.get (),
1504 error (_("Unable to read memory."));
1506 /* Output the header information. */
1507 uiout
->field_core_addr ("addr", gdbarch
, addr
);
1508 uiout
->field_int ("nr-bytes", nr_bytes
);
1509 uiout
->field_int ("total-bytes", total_bytes
);
1510 uiout
->field_core_addr ("next-row", gdbarch
, addr
+ word_size
* nr_cols
);
1511 uiout
->field_core_addr ("prev-row", gdbarch
, addr
- word_size
* nr_cols
);
1512 uiout
->field_core_addr ("next-page", gdbarch
, addr
+ total_bytes
);
1513 uiout
->field_core_addr ("prev-page", gdbarch
, addr
- total_bytes
);
1515 /* Build the result as a two dimentional table. */
1519 struct cleanup
*cleanup_list
;
1523 cleanup_list
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
1524 for (row
= 0, row_byte
= 0;
1526 row
++, row_byte
+= nr_cols
* word_size
)
1530 struct cleanup
*cleanup_tuple
;
1531 struct cleanup
*cleanup_list_data
;
1532 struct value_print_options opts
;
1534 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1535 uiout
->field_core_addr ("addr", gdbarch
, addr
+ row_byte
);
1536 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr +
1538 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
1539 get_formatted_print_options (&opts
, word_format
);
1540 for (col
= 0, col_byte
= row_byte
;
1542 col
++, col_byte
+= word_size
)
1544 if (col_byte
+ word_size
> nr_bytes
)
1546 uiout
->field_string (NULL
, "N/A");
1551 print_scalar_formatted (&mbuf
[col_byte
], word_type
, &opts
,
1552 word_asize
, &stream
);
1553 uiout
->field_stream (NULL
, stream
);
1556 do_cleanups (cleanup_list_data
);
1562 for (byte
= row_byte
;
1563 byte
< row_byte
+ word_size
* nr_cols
; byte
++)
1565 if (byte
>= nr_bytes
)
1567 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
1568 stream
.putc (aschar
);
1570 stream
.putc (mbuf
[byte
]);
1572 uiout
->field_stream ("ascii", stream
);
1574 do_cleanups (cleanup_tuple
);
1576 do_cleanups (cleanup_list
);
1581 mi_cmd_data_read_memory_bytes (char *command
, char **argv
, int argc
)
1583 struct gdbarch
*gdbarch
= get_current_arch ();
1584 struct ui_out
*uiout
= current_uiout
;
1585 struct cleanup
*cleanups
;
1588 memory_read_result_s
*read_result
;
1590 VEC(memory_read_result_s
) *result
;
1592 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
1599 static const struct mi_opt opts
[] =
1601 {"o", OFFSET_OPT
, 1},
1607 int opt
= mi_getopt ("-data-read-memory-bytes", argc
, argv
, opts
,
1611 switch ((enum opt
) opt
)
1614 offset
= atol (oarg
);
1622 error (_("Usage: [ -o OFFSET ] ADDR LENGTH."));
1624 addr
= parse_and_eval_address (argv
[0]) + offset
;
1625 length
= atol (argv
[1]);
1627 result
= read_memory_robust (current_target
.beneath
, addr
, length
);
1629 cleanups
= make_cleanup (free_memory_read_result_vector
, &result
);
1631 if (VEC_length (memory_read_result_s
, result
) == 0)
1632 error (_("Unable to read memory."));
1634 make_cleanup_ui_out_list_begin_end (uiout
, "memory");
1636 VEC_iterate (memory_read_result_s
, result
, ix
, read_result
);
1639 struct cleanup
*t
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1644 uiout
->field_core_addr ("begin", gdbarch
, read_result
->begin
);
1645 uiout
->field_core_addr ("offset", gdbarch
, read_result
->begin
- addr
);
1646 uiout
->field_core_addr ("end", gdbarch
, read_result
->end
);
1648 alloc_len
= (read_result
->end
- read_result
->begin
) * 2 * unit_size
+ 1;
1649 data
= (char *) xmalloc (alloc_len
);
1651 for (i
= 0, p
= data
;
1652 i
< ((read_result
->end
- read_result
->begin
) * unit_size
);
1655 sprintf (p
, "%02x", read_result
->data
[i
]);
1657 uiout
->field_string ("contents", data
);
1661 do_cleanups (cleanups
);
1664 /* Implementation of the -data-write_memory command.
1666 COLUMN_OFFSET: optional argument. Must be preceded by '-o'. The
1667 offset from the beginning of the memory grid row where the cell to
1669 ADDR: start address of the row in the memory grid where the memory
1670 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
1671 the location to write to.
1672 FORMAT: a char indicating format for the ``word''. See
1674 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
1675 VALUE: value to be written into the memory address.
1677 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1682 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
1684 struct gdbarch
*gdbarch
= get_current_arch ();
1685 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1688 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1689 enough when using a compiler other than GCC. */
1692 struct cleanup
*old_chain
;
1700 static const struct mi_opt opts
[] =
1702 {"o", OFFSET_OPT
, 1},
1708 int opt
= mi_getopt ("-data-write-memory", argc
, argv
, opts
,
1713 switch ((enum opt
) opt
)
1716 offset
= atol (oarg
);
1724 error (_("-data-write-memory: Usage: "
1725 "[-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE."));
1727 /* Extract all the arguments. */
1728 /* Start address of the memory dump. */
1729 addr
= parse_and_eval_address (argv
[0]);
1730 /* The size of the memory word. */
1731 word_size
= atol (argv
[2]);
1733 /* Calculate the real address of the write destination. */
1734 addr
+= (offset
* word_size
);
1736 /* Get the value as a number. */
1737 value
= parse_and_eval_address (argv
[3]);
1738 /* Get the value into an array. */
1739 buffer
= (gdb_byte
*) xmalloc (word_size
);
1740 old_chain
= make_cleanup (xfree
, buffer
);
1741 store_signed_integer (buffer
, word_size
, byte_order
, value
);
1742 /* Write it down to memory. */
1743 write_memory_with_notification (addr
, buffer
, word_size
);
1744 /* Free the buffer. */
1745 do_cleanups (old_chain
);
1748 /* Implementation of the -data-write-memory-bytes command.
1751 DATA: string of bytes to write at that address
1752 COUNT: number of bytes to be filled (decimal integer). */
1755 mi_cmd_data_write_memory_bytes (char *command
, char **argv
, int argc
)
1761 size_t len_hex
, len_bytes
, len_units
, i
, steps
, remaining_units
;
1762 long int count_units
;
1763 struct cleanup
*back_to
;
1766 if (argc
!= 2 && argc
!= 3)
1767 error (_("Usage: ADDR DATA [COUNT]."));
1769 addr
= parse_and_eval_address (argv
[0]);
1771 len_hex
= strlen (cdata
);
1772 unit_size
= gdbarch_addressable_memory_unit_size (get_current_arch ());
1774 if (len_hex
% (unit_size
* 2) != 0)
1775 error (_("Hex-encoded '%s' must represent an integral number of "
1776 "addressable memory units."),
1779 len_bytes
= len_hex
/ 2;
1780 len_units
= len_bytes
/ unit_size
;
1783 count_units
= strtoul (argv
[2], NULL
, 10);
1785 count_units
= len_units
;
1787 databuf
= XNEWVEC (gdb_byte
, len_bytes
);
1788 back_to
= make_cleanup (xfree
, databuf
);
1790 for (i
= 0; i
< len_bytes
; ++i
)
1793 if (sscanf (cdata
+ i
* 2, "%02x", &x
) != 1)
1794 error (_("Invalid argument"));
1795 databuf
[i
] = (gdb_byte
) x
;
1798 if (len_units
< count_units
)
1800 /* Pattern is made of less units than count:
1801 repeat pattern to fill memory. */
1802 data
= (gdb_byte
*) xmalloc (count_units
* unit_size
);
1803 make_cleanup (xfree
, data
);
1805 /* Number of times the pattern is entirely repeated. */
1806 steps
= count_units
/ len_units
;
1807 /* Number of remaining addressable memory units. */
1808 remaining_units
= count_units
% len_units
;
1809 for (i
= 0; i
< steps
; i
++)
1810 memcpy (data
+ i
* len_bytes
, databuf
, len_bytes
);
1812 if (remaining_units
> 0)
1813 memcpy (data
+ steps
* len_bytes
, databuf
,
1814 remaining_units
* unit_size
);
1818 /* Pattern is longer than or equal to count:
1819 just copy count addressable memory units. */
1823 write_memory_with_notification (addr
, data
, count_units
);
1825 do_cleanups (back_to
);
1829 mi_cmd_enable_timings (char *command
, char **argv
, int argc
)
1835 if (strcmp (argv
[0], "yes") == 0)
1837 else if (strcmp (argv
[0], "no") == 0)
1848 error (_("-enable-timings: Usage: %s {yes|no}"), command
);
1852 mi_cmd_list_features (char *command
, char **argv
, int argc
)
1856 struct cleanup
*cleanup
= NULL
;
1857 struct ui_out
*uiout
= current_uiout
;
1859 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
1860 uiout
->field_string (NULL
, "frozen-varobjs");
1861 uiout
->field_string (NULL
, "pending-breakpoints");
1862 uiout
->field_string (NULL
, "thread-info");
1863 uiout
->field_string (NULL
, "data-read-memory-bytes");
1864 uiout
->field_string (NULL
, "breakpoint-notifications");
1865 uiout
->field_string (NULL
, "ada-task-info");
1866 uiout
->field_string (NULL
, "language-option");
1867 uiout
->field_string (NULL
, "info-gdb-mi-command");
1868 uiout
->field_string (NULL
, "undefined-command-error-code");
1869 uiout
->field_string (NULL
, "exec-run-start-option");
1871 if (ext_lang_initialized_p (get_ext_lang_defn (EXT_LANG_PYTHON
)))
1872 uiout
->field_string (NULL
, "python");
1874 do_cleanups (cleanup
);
1878 error (_("-list-features should be passed no arguments"));
1882 mi_cmd_list_target_features (char *command
, char **argv
, int argc
)
1886 struct cleanup
*cleanup
= NULL
;
1887 struct ui_out
*uiout
= current_uiout
;
1889 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
1891 uiout
->field_string (NULL
, "async");
1892 if (target_can_execute_reverse
)
1893 uiout
->field_string (NULL
, "reverse");
1894 do_cleanups (cleanup
);
1898 error (_("-list-target-features should be passed no arguments"));
1902 mi_cmd_add_inferior (char *command
, char **argv
, int argc
)
1904 struct inferior
*inf
;
1907 error (_("-add-inferior should be passed no arguments"));
1909 inf
= add_inferior_with_spaces ();
1911 current_uiout
->field_fmt ("inferior", "i%d", inf
->num
);
1914 /* Callback used to find the first inferior other than the current
1918 get_other_inferior (struct inferior
*inf
, void *arg
)
1920 if (inf
== current_inferior ())
1927 mi_cmd_remove_inferior (char *command
, char **argv
, int argc
)
1930 struct inferior
*inf
;
1933 error (_("-remove-inferior should be passed a single argument"));
1935 if (sscanf (argv
[0], "i%d", &id
) != 1)
1936 error (_("the thread group id is syntactically invalid"));
1938 inf
= find_inferior_id (id
);
1940 error (_("the specified thread group does not exist"));
1943 error (_("cannot remove an active inferior"));
1945 if (inf
== current_inferior ())
1947 struct thread_info
*tp
= 0;
1948 struct inferior
*new_inferior
1949 = iterate_over_inferiors (get_other_inferior
, NULL
);
1951 if (new_inferior
== NULL
)
1952 error (_("Cannot remove last inferior"));
1954 set_current_inferior (new_inferior
);
1955 if (new_inferior
->pid
!= 0)
1956 tp
= any_thread_of_process (new_inferior
->pid
);
1957 switch_to_thread (tp
? tp
->ptid
: null_ptid
);
1958 set_current_program_space (new_inferior
->pspace
);
1961 delete_inferior (inf
);
1966 /* Execute a command within a safe environment.
1967 Return <0 for error; >=0 for ok.
1969 args->action will tell mi_execute_command what action
1970 to perfrom after the given command has executed (display/suppress
1971 prompt, display error). */
1974 captured_mi_execute_command (struct ui_out
*uiout
, struct mi_parse
*context
)
1976 struct mi_interp
*mi
= (struct mi_interp
*) command_interp ();
1977 struct cleanup
*cleanup
;
1980 current_command_ts
= context
->cmd_start
;
1982 current_token
= xstrdup (context
->token
);
1983 cleanup
= make_cleanup (free_current_contents
, ¤t_token
);
1985 running_result_record_printed
= 0;
1987 switch (context
->op
)
1990 /* A MI command was read from the input stream. */
1992 /* FIXME: gdb_???? */
1993 fprintf_unfiltered (mi
->raw_stdout
,
1994 " token=`%s' command=`%s' args=`%s'\n",
1995 context
->token
, context
->command
, context
->args
);
1997 mi_cmd_execute (context
);
1999 /* Print the result if there were no errors.
2001 Remember that on the way out of executing a command, you have
2002 to directly use the mi_interp's uiout, since the command
2003 could have reset the interpreter, in which case the current
2004 uiout will most likely crash in the mi_out_* routines. */
2005 if (!running_result_record_printed
)
2007 fputs_unfiltered (context
->token
, mi
->raw_stdout
);
2008 /* There's no particularly good reason why target-connect results
2009 in not ^done. Should kill ^connected for MI3. */
2010 fputs_unfiltered (strcmp (context
->command
, "target-select") == 0
2011 ? "^connected" : "^done", mi
->raw_stdout
);
2012 mi_out_put (uiout
, mi
->raw_stdout
);
2013 mi_out_rewind (uiout
);
2014 mi_print_timing_maybe (mi
->raw_stdout
);
2015 fputs_unfiltered ("\n", mi
->raw_stdout
);
2018 /* The command does not want anything to be printed. In that
2019 case, the command probably should not have written anything
2020 to uiout, but in case it has written something, discard it. */
2021 mi_out_rewind (uiout
);
2028 /* A CLI command was read from the input stream. */
2029 /* This "feature" will be removed as soon as we have a
2030 complete set of mi commands. */
2031 /* Echo the command on the console. */
2032 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
2033 /* Call the "console" interpreter. */
2034 argv
[0] = INTERP_CONSOLE
;
2035 argv
[1] = context
->command
;
2036 mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
2038 /* If we changed interpreters, DON'T print out anything. */
2039 if (current_interp_named_p (INTERP_MI
)
2040 || current_interp_named_p (INTERP_MI1
)
2041 || current_interp_named_p (INTERP_MI2
)
2042 || current_interp_named_p (INTERP_MI3
))
2044 if (!running_result_record_printed
)
2046 fputs_unfiltered (context
->token
, mi
->raw_stdout
);
2047 fputs_unfiltered ("^done", mi
->raw_stdout
);
2048 mi_out_put (uiout
, mi
->raw_stdout
);
2049 mi_out_rewind (uiout
);
2050 mi_print_timing_maybe (mi
->raw_stdout
);
2051 fputs_unfiltered ("\n", mi
->raw_stdout
);
2054 mi_out_rewind (uiout
);
2060 do_cleanups (cleanup
);
2063 /* Print a gdb exception to the MI output stream. */
2066 mi_print_exception (const char *token
, struct gdb_exception exception
)
2068 struct mi_interp
*mi
= (struct mi_interp
*) current_interpreter ();
2070 fputs_unfiltered (token
, mi
->raw_stdout
);
2071 fputs_unfiltered ("^error,msg=\"", mi
->raw_stdout
);
2072 if (exception
.message
== NULL
)
2073 fputs_unfiltered ("unknown error", mi
->raw_stdout
);
2075 fputstr_unfiltered (exception
.message
, '"', mi
->raw_stdout
);
2076 fputs_unfiltered ("\"", mi
->raw_stdout
);
2078 switch (exception
.error
)
2080 case UNDEFINED_COMMAND_ERROR
:
2081 fputs_unfiltered (",code=\"undefined-command\"", mi
->raw_stdout
);
2085 fputs_unfiltered ("\n", mi
->raw_stdout
);
2088 /* Determine whether the parsed command already notifies the
2089 user_selected_context_changed observer. */
2092 command_notifies_uscc_observer (struct mi_parse
*command
)
2094 if (command
->op
== CLI_COMMAND
)
2096 /* CLI commands "thread" and "inferior" already send it. */
2097 return (strncmp (command
->command
, "thread ", 7) == 0
2098 || strncmp (command
->command
, "inferior ", 9) == 0);
2100 else /* MI_COMMAND */
2102 if (strcmp (command
->command
, "interpreter-exec") == 0
2103 && command
->argc
> 1)
2105 /* "thread" and "inferior" again, but through -interpreter-exec. */
2106 return (strncmp (command
->argv
[1], "thread ", 7) == 0
2107 || strncmp (command
->argv
[1], "inferior ", 9) == 0);
2111 /* -thread-select already sends it. */
2112 return strcmp (command
->command
, "thread-select") == 0;
2117 mi_execute_command (const char *cmd
, int from_tty
)
2120 struct mi_parse
*command
= NULL
;
2122 /* This is to handle EOF (^D). We just quit gdb. */
2123 /* FIXME: we should call some API function here. */
2125 quit_force (NULL
, from_tty
);
2127 target_log_command (cmd
);
2131 command
= mi_parse (cmd
, &token
);
2133 CATCH (exception
, RETURN_MASK_ALL
)
2135 mi_print_exception (token
, exception
);
2140 if (command
!= NULL
)
2142 ptid_t previous_ptid
= inferior_ptid
;
2143 struct cleanup
*cleanup
= make_cleanup (null_cleanup
, NULL
);
2145 command
->token
= token
;
2147 if (command
->cmd
!= NULL
&& command
->cmd
->suppress_notification
!= NULL
)
2149 make_cleanup_restore_integer (command
->cmd
->suppress_notification
);
2150 *command
->cmd
->suppress_notification
= 1;
2155 command
->cmd_start
= new mi_timestamp ();
2156 timestamp (command
->cmd_start
);
2161 captured_mi_execute_command (current_uiout
, command
);
2163 CATCH (result
, RETURN_MASK_ALL
)
2165 /* Like in start_event_loop, enable input and force display
2166 of the prompt. Otherwise, any command that calls
2167 async_disable_stdin, and then throws, will leave input
2169 async_enable_stdin ();
2170 current_ui
->prompt_state
= PROMPT_NEEDED
;
2172 /* The command execution failed and error() was called
2174 mi_print_exception (command
->token
, result
);
2175 mi_out_rewind (current_uiout
);
2179 bpstat_do_actions ();
2181 if (/* The notifications are only output when the top-level
2182 interpreter (specified on the command line) is MI. */
2183 interp_ui_out (top_level_interpreter ())->is_mi_like_p ()
2184 /* Don't try report anything if there are no threads --
2185 the program is dead. */
2186 && thread_count () != 0
2187 /* If the command already reports the thread change, no need to do it
2189 && !command_notifies_uscc_observer (command
))
2191 struct mi_interp
*mi
= (struct mi_interp
*) top_level_interpreter ();
2192 int report_change
= 0;
2194 if (command
->thread
== -1)
2196 report_change
= (!ptid_equal (previous_ptid
, null_ptid
)
2197 && !ptid_equal (inferior_ptid
, previous_ptid
)
2198 && !ptid_equal (inferior_ptid
, null_ptid
));
2200 else if (!ptid_equal (inferior_ptid
, null_ptid
))
2202 struct thread_info
*ti
= inferior_thread ();
2204 report_change
= (ti
->global_num
!= command
->thread
);
2209 observer_notify_user_selected_context_changed
2210 (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
);
2214 mi_parse_free (command
);
2216 do_cleanups (cleanup
);
2221 mi_cmd_execute (struct mi_parse
*parse
)
2223 struct cleanup
*cleanup
;
2225 cleanup
= prepare_execute_command ();
2227 if (parse
->all
&& parse
->thread_group
!= -1)
2228 error (_("Cannot specify --thread-group together with --all"));
2230 if (parse
->all
&& parse
->thread
!= -1)
2231 error (_("Cannot specify --thread together with --all"));
2233 if (parse
->thread_group
!= -1 && parse
->thread
!= -1)
2234 error (_("Cannot specify --thread together with --thread-group"));
2236 if (parse
->frame
!= -1 && parse
->thread
== -1)
2237 error (_("Cannot specify --frame without --thread"));
2239 if (parse
->thread_group
!= -1)
2241 struct inferior
*inf
= find_inferior_id (parse
->thread_group
);
2242 struct thread_info
*tp
= 0;
2245 error (_("Invalid thread group for the --thread-group option"));
2247 set_current_inferior (inf
);
2248 /* This behaviour means that if --thread-group option identifies
2249 an inferior with multiple threads, then a random one will be
2250 picked. This is not a problem -- frontend should always
2251 provide --thread if it wishes to operate on a specific
2254 tp
= any_live_thread_of_process (inf
->pid
);
2255 switch_to_thread (tp
? tp
->ptid
: null_ptid
);
2256 set_current_program_space (inf
->pspace
);
2259 if (parse
->thread
!= -1)
2261 struct thread_info
*tp
= find_thread_global_id (parse
->thread
);
2264 error (_("Invalid thread id: %d"), parse
->thread
);
2266 if (is_exited (tp
->ptid
))
2267 error (_("Thread id: %d has terminated"), parse
->thread
);
2269 switch_to_thread (tp
->ptid
);
2272 if (parse
->frame
!= -1)
2274 struct frame_info
*fid
;
2275 int frame
= parse
->frame
;
2277 fid
= find_relative_frame (get_current_frame (), &frame
);
2279 /* find_relative_frame was successful */
2282 error (_("Invalid frame id: %d"), frame
);
2285 if (parse
->language
!= language_unknown
)
2287 make_cleanup_restore_current_language ();
2288 set_language (parse
->language
);
2291 current_context
= parse
;
2293 if (parse
->cmd
->argv_func
!= NULL
)
2295 parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
2297 else if (parse
->cmd
->cli
.cmd
!= 0)
2299 /* FIXME: DELETE THIS. */
2300 /* The operation is still implemented by a cli command. */
2301 /* Must be a synchronous one. */
2302 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
2307 /* FIXME: DELETE THIS. */
2310 stb
.puts ("Undefined mi command: ");
2311 stb
.putstr (parse
->command
, '"');
2312 stb
.puts (" (missing implementation)");
2316 do_cleanups (cleanup
);
2319 /* FIXME: This is just a hack so we can get some extra commands going.
2320 We don't want to channel things through the CLI, but call libgdb directly.
2321 Use only for synchronous commands. */
2324 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
2328 struct cleanup
*old_cleanups
;
2332 run
= xstrprintf ("%s %s", cmd
, args
);
2334 run
= xstrdup (cmd
);
2336 /* FIXME: gdb_???? */
2337 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
2339 old_cleanups
= make_cleanup (xfree
, run
);
2340 execute_command (run
, 0 /* from_tty */ );
2341 do_cleanups (old_cleanups
);
2347 mi_execute_async_cli_command (char *cli_command
, char **argv
, int argc
)
2349 struct cleanup
*old_cleanups
;
2353 run
= xstrprintf ("%s %s&", cli_command
, argc
? *argv
: "");
2355 run
= xstrprintf ("%s %s", cli_command
, argc
? *argv
: "");
2356 old_cleanups
= make_cleanup (xfree
, run
);
2358 execute_command (run
, 0 /* from_tty */ );
2360 /* Do this before doing any printing. It would appear that some
2361 print code leaves garbage around in the buffer. */
2362 do_cleanups (old_cleanups
);
2366 mi_load_progress (const char *section_name
,
2367 unsigned long sent_so_far
,
2368 unsigned long total_section
,
2369 unsigned long total_sent
,
2370 unsigned long grand_total
)
2372 using namespace std::chrono
;
2373 static steady_clock::time_point last_update
;
2374 static char *previous_sect_name
= NULL
;
2376 struct ui_out
*saved_uiout
;
2377 struct ui_out
*uiout
;
2378 struct mi_interp
*mi
= (struct mi_interp
*) current_interpreter ();
2380 /* This function is called through deprecated_show_load_progress
2381 which means uiout may not be correct. Fix it for the duration
2382 of this function. */
2383 saved_uiout
= current_uiout
;
2385 if (current_interp_named_p (INTERP_MI
)
2386 || current_interp_named_p (INTERP_MI2
))
2387 current_uiout
= mi_out_new (2);
2388 else if (current_interp_named_p (INTERP_MI1
))
2389 current_uiout
= mi_out_new (1);
2390 else if (current_interp_named_p (INTERP_MI3
))
2391 current_uiout
= mi_out_new (3);
2395 uiout
= current_uiout
;
2397 new_section
= (previous_sect_name
?
2398 strcmp (previous_sect_name
, section_name
) : 1);
2401 struct cleanup
*cleanup_tuple
;
2403 xfree (previous_sect_name
);
2404 previous_sect_name
= xstrdup (section_name
);
2407 fputs_unfiltered (current_token
, mi
->raw_stdout
);
2408 fputs_unfiltered ("+download", mi
->raw_stdout
);
2409 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
2410 uiout
->field_string ("section", section_name
);
2411 uiout
->field_int ("section-size", total_section
);
2412 uiout
->field_int ("total-size", grand_total
);
2413 do_cleanups (cleanup_tuple
);
2414 mi_out_put (uiout
, mi
->raw_stdout
);
2415 fputs_unfiltered ("\n", mi
->raw_stdout
);
2416 gdb_flush (mi
->raw_stdout
);
2419 steady_clock::time_point time_now
= steady_clock::now ();
2420 if (time_now
- last_update
> milliseconds (500))
2422 struct cleanup
*cleanup_tuple
;
2424 last_update
= time_now
;
2426 fputs_unfiltered (current_token
, mi
->raw_stdout
);
2427 fputs_unfiltered ("+download", mi
->raw_stdout
);
2428 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
2429 uiout
->field_string ("section", section_name
);
2430 uiout
->field_int ("section-sent", sent_so_far
);
2431 uiout
->field_int ("section-size", total_section
);
2432 uiout
->field_int ("total-sent", total_sent
);
2433 uiout
->field_int ("total-size", grand_total
);
2434 do_cleanups (cleanup_tuple
);
2435 mi_out_put (uiout
, mi
->raw_stdout
);
2436 fputs_unfiltered ("\n", mi
->raw_stdout
);
2437 gdb_flush (mi
->raw_stdout
);
2441 current_uiout
= saved_uiout
;
2445 timestamp (struct mi_timestamp
*tv
)
2447 using namespace std::chrono
;
2449 tv
->wallclock
= steady_clock::now ();
2450 run_time_clock::now (tv
->utime
, tv
->stime
);
2454 print_diff_now (struct ui_file
*file
, struct mi_timestamp
*start
)
2456 struct mi_timestamp now
;
2459 print_diff (file
, start
, &now
);
2463 mi_print_timing_maybe (struct ui_file
*file
)
2465 /* If the command is -enable-timing then do_timings may be true
2466 whilst current_command_ts is not initialized. */
2467 if (do_timings
&& current_command_ts
)
2468 print_diff_now (file
, current_command_ts
);
2472 print_diff (struct ui_file
*file
, struct mi_timestamp
*start
,
2473 struct mi_timestamp
*end
)
2475 using namespace std::chrono
;
2477 duration
<double> wallclock
= end
->wallclock
- start
->wallclock
;
2478 duration
<double> utime
= end
->utime
- start
->utime
;
2479 duration
<double> stime
= end
->stime
- start
->stime
;
2483 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
2484 wallclock
.count (), utime
.count (), stime
.count ());
2488 mi_cmd_trace_define_variable (char *command
, char **argv
, int argc
)
2490 LONGEST initval
= 0;
2491 struct trace_state_variable
*tsv
;
2494 if (argc
!= 1 && argc
!= 2)
2495 error (_("Usage: -trace-define-variable VARIABLE [VALUE]"));
2499 error (_("Name of trace variable should start with '$'"));
2501 validate_trace_state_variable_name (name
);
2503 tsv
= find_trace_state_variable (name
);
2505 tsv
= create_trace_state_variable (name
);
2508 initval
= value_as_long (parse_and_eval (argv
[1]));
2510 tsv
->initial_value
= initval
;
2514 mi_cmd_trace_list_variables (char *command
, char **argv
, int argc
)
2517 error (_("-trace-list-variables: no arguments allowed"));
2519 tvariables_info_1 ();
2523 mi_cmd_trace_find (char *command
, char **argv
, int argc
)
2528 error (_("trace selection mode is required"));
2532 if (strcmp (mode
, "none") == 0)
2534 tfind_1 (tfind_number
, -1, 0, 0, 0);
2538 check_trace_running (current_trace_status ());
2540 if (strcmp (mode
, "frame-number") == 0)
2543 error (_("frame number is required"));
2544 tfind_1 (tfind_number
, atoi (argv
[1]), 0, 0, 0);
2546 else if (strcmp (mode
, "tracepoint-number") == 0)
2549 error (_("tracepoint number is required"));
2550 tfind_1 (tfind_tp
, atoi (argv
[1]), 0, 0, 0);
2552 else if (strcmp (mode
, "pc") == 0)
2555 error (_("PC is required"));
2556 tfind_1 (tfind_pc
, 0, parse_and_eval_address (argv
[1]), 0, 0);
2558 else if (strcmp (mode
, "pc-inside-range") == 0)
2561 error (_("Start and end PC are required"));
2562 tfind_1 (tfind_range
, 0, parse_and_eval_address (argv
[1]),
2563 parse_and_eval_address (argv
[2]), 0);
2565 else if (strcmp (mode
, "pc-outside-range") == 0)
2568 error (_("Start and end PC are required"));
2569 tfind_1 (tfind_outside
, 0, parse_and_eval_address (argv
[1]),
2570 parse_and_eval_address (argv
[2]), 0);
2572 else if (strcmp (mode
, "line") == 0)
2574 struct symtabs_and_lines sals
;
2575 struct symtab_and_line sal
;
2576 static CORE_ADDR start_pc
, end_pc
;
2577 struct cleanup
*back_to
;
2580 error (_("Line is required"));
2582 sals
= decode_line_with_current_source (argv
[1],
2583 DECODE_LINE_FUNFIRSTLINE
);
2584 back_to
= make_cleanup (xfree
, sals
.sals
);
2588 if (sal
.symtab
== 0)
2589 error (_("Could not find the specified line"));
2591 if (sal
.line
> 0 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
2592 tfind_1 (tfind_range
, 0, start_pc
, end_pc
- 1, 0);
2594 error (_("Could not find the specified line"));
2596 do_cleanups (back_to
);
2599 error (_("Invalid mode '%s'"), mode
);
2601 if (has_stack_frames () || get_traceframe_number () >= 0)
2602 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
, 1);
2606 mi_cmd_trace_save (char *command
, char **argv
, int argc
)
2608 int target_saves
= 0;
2609 int generate_ctf
= 0;
2616 TARGET_SAVE_OPT
, CTF_OPT
2618 static const struct mi_opt opts
[] =
2620 {"r", TARGET_SAVE_OPT
, 0},
2621 {"ctf", CTF_OPT
, 0},
2627 int opt
= mi_getopt ("-trace-save", argc
, argv
, opts
,
2632 switch ((enum opt
) opt
)
2634 case TARGET_SAVE_OPT
:
2643 if (argc
- oind
!= 1)
2644 error (_("Exactly one argument required "
2645 "(file in which to save trace data)"));
2647 filename
= argv
[oind
];
2650 trace_save_ctf (filename
, target_saves
);
2652 trace_save_tfile (filename
, target_saves
);
2656 mi_cmd_trace_start (char *command
, char **argv
, int argc
)
2658 start_tracing (NULL
);
2662 mi_cmd_trace_status (char *command
, char **argv
, int argc
)
2664 trace_status_mi (0);
2668 mi_cmd_trace_stop (char *command
, char **argv
, int argc
)
2670 stop_tracing (NULL
);
2671 trace_status_mi (1);
2674 /* Implement the "-ada-task-info" command. */
2677 mi_cmd_ada_task_info (char *command
, char **argv
, int argc
)
2679 if (argc
!= 0 && argc
!= 1)
2680 error (_("Invalid MI command"));
2682 print_ada_task_info (current_uiout
, argv
[0], current_inferior ());
2685 /* Print EXPRESSION according to VALUES. */
2688 print_variable_or_computed (const char *expression
, enum print_values values
)
2690 struct cleanup
*old_chain
;
2693 struct ui_out
*uiout
= current_uiout
;
2697 expression_up expr
= parse_expression (expression
);
2699 if (values
== PRINT_SIMPLE_VALUES
)
2700 val
= evaluate_type (expr
.get ());
2702 val
= evaluate_expression (expr
.get ());
2704 old_chain
= make_cleanup (null_cleanup
, NULL
);
2705 if (values
!= PRINT_NO_VALUES
)
2706 make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
2707 uiout
->field_string ("name", expression
);
2711 case PRINT_SIMPLE_VALUES
:
2712 type
= check_typedef (value_type (val
));
2713 type_print (value_type (val
), "", &stb
, -1);
2714 uiout
->field_stream ("type", stb
);
2715 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
2716 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
2717 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
2719 struct value_print_options opts
;
2721 get_no_prettyformat_print_options (&opts
);
2723 common_val_print (val
, &stb
, 0, &opts
, current_language
);
2724 uiout
->field_stream ("value", stb
);
2727 case PRINT_ALL_VALUES
:
2729 struct value_print_options opts
;
2731 get_no_prettyformat_print_options (&opts
);
2733 common_val_print (val
, &stb
, 0, &opts
, current_language
);
2734 uiout
->field_stream ("value", stb
);
2739 do_cleanups (old_chain
);
2742 /* Implement the "-trace-frame-collected" command. */
2745 mi_cmd_trace_frame_collected (char *command
, char **argv
, int argc
)
2747 struct cleanup
*old_chain
;
2748 struct bp_location
*tloc
;
2750 struct collection_list
*clist
;
2751 struct collection_list tracepoint_list
, stepping_list
;
2752 struct traceframe_info
*tinfo
;
2754 enum print_values var_print_values
= PRINT_ALL_VALUES
;
2755 enum print_values comp_print_values
= PRINT_ALL_VALUES
;
2756 int registers_format
= 'x';
2757 int memory_contents
= 0;
2758 struct ui_out
*uiout
= current_uiout
;
2766 static const struct mi_opt opts
[] =
2768 {"-var-print-values", VAR_PRINT_VALUES
, 1},
2769 {"-comp-print-values", COMP_PRINT_VALUES
, 1},
2770 {"-registers-format", REGISTERS_FORMAT
, 1},
2771 {"-memory-contents", MEMORY_CONTENTS
, 0},
2778 int opt
= mi_getopt ("-trace-frame-collected", argc
, argv
, opts
,
2782 switch ((enum opt
) opt
)
2784 case VAR_PRINT_VALUES
:
2785 var_print_values
= mi_parse_print_values (oarg
);
2787 case COMP_PRINT_VALUES
:
2788 comp_print_values
= mi_parse_print_values (oarg
);
2790 case REGISTERS_FORMAT
:
2791 registers_format
= oarg
[0];
2792 case MEMORY_CONTENTS
:
2793 memory_contents
= 1;
2799 error (_("Usage: -trace-frame-collected "
2800 "[--var-print-values PRINT_VALUES] "
2801 "[--comp-print-values PRINT_VALUES] "
2802 "[--registers-format FORMAT]"
2803 "[--memory-contents]"));
2805 /* This throws an error is not inspecting a trace frame. */
2806 tloc
= get_traceframe_location (&stepping_frame
);
2808 /* This command only makes sense for the current frame, not the
2810 old_chain
= make_cleanup_restore_current_thread ();
2811 select_frame (get_current_frame ());
2813 encode_actions (tloc
, &tracepoint_list
, &stepping_list
);
2816 clist
= &stepping_list
;
2818 clist
= &tracepoint_list
;
2820 tinfo
= get_traceframe_info ();
2822 /* Explicitly wholly collected variables. */
2824 struct cleanup
*list_cleanup
;
2827 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
,
2828 "explicit-variables");
2830 const std::vector
<std::string
> &wholly_collected
2831 = clist
->wholly_collected ();
2832 for (size_t i
= 0; i
< wholly_collected
.size (); i
++)
2834 const std::string
&str
= wholly_collected
[i
];
2835 print_variable_or_computed (str
.c_str (), var_print_values
);
2838 do_cleanups (list_cleanup
);
2841 /* Computed expressions. */
2843 struct cleanup
*list_cleanup
;
2848 = make_cleanup_ui_out_list_begin_end (uiout
,
2849 "computed-expressions");
2851 const std::vector
<std::string
> &computed
= clist
->computed ();
2852 for (size_t i
= 0; i
< computed
.size (); i
++)
2854 const std::string
&str
= computed
[i
];
2855 print_variable_or_computed (str
.c_str (), comp_print_values
);
2858 do_cleanups (list_cleanup
);
2861 /* Registers. Given pseudo-registers, and that some architectures
2862 (like MIPS) actually hide the raw registers, we don't go through
2863 the trace frame info, but instead consult the register cache for
2864 register availability. */
2866 struct cleanup
*list_cleanup
;
2867 struct frame_info
*frame
;
2868 struct gdbarch
*gdbarch
;
2872 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "registers");
2874 frame
= get_selected_frame (NULL
);
2875 gdbarch
= get_frame_arch (frame
);
2876 numregs
= gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
2878 for (regnum
= 0; regnum
< numregs
; regnum
++)
2880 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
2881 || *(gdbarch_register_name (gdbarch
, regnum
)) == '\0')
2884 output_register (frame
, regnum
, registers_format
, 1);
2887 do_cleanups (list_cleanup
);
2890 /* Trace state variables. */
2892 struct cleanup
*list_cleanup
;
2897 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "tvars");
2900 make_cleanup (free_current_contents
, &tsvname
);
2902 for (i
= 0; VEC_iterate (int, tinfo
->tvars
, i
, tvar
); i
++)
2904 struct cleanup
*cleanup_child
;
2905 struct trace_state_variable
*tsv
;
2907 tsv
= find_trace_state_variable_by_number (tvar
);
2909 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
2913 tsvname
= (char *) xrealloc (tsvname
, strlen (tsv
->name
) + 2);
2915 strcpy (tsvname
+ 1, tsv
->name
);
2916 uiout
->field_string ("name", tsvname
);
2918 tsv
->value_known
= target_get_trace_state_variable_value (tsv
->number
,
2920 uiout
->field_int ("current", tsv
->value
);
2924 uiout
->field_skip ("name");
2925 uiout
->field_skip ("current");
2928 do_cleanups (cleanup_child
);
2931 do_cleanups (list_cleanup
);
2936 struct cleanup
*list_cleanup
;
2937 VEC(mem_range_s
) *available_memory
= NULL
;
2938 struct mem_range
*r
;
2941 traceframe_available_memory (&available_memory
, 0, ULONGEST_MAX
);
2942 make_cleanup (VEC_cleanup(mem_range_s
), &available_memory
);
2944 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
2946 for (i
= 0; VEC_iterate (mem_range_s
, available_memory
, i
, r
); i
++)
2948 struct cleanup
*cleanup_child
;
2950 struct gdbarch
*gdbarch
= target_gdbarch ();
2952 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
2954 uiout
->field_core_addr ("address", gdbarch
, r
->start
);
2955 uiout
->field_int ("length", r
->length
);
2957 data
= (gdb_byte
*) xmalloc (r
->length
);
2958 make_cleanup (xfree
, data
);
2960 if (memory_contents
)
2962 if (target_read_memory (r
->start
, data
, r
->length
) == 0)
2967 data_str
= (char *) xmalloc (r
->length
* 2 + 1);
2968 make_cleanup (xfree
, data_str
);
2970 for (m
= 0, p
= data_str
; m
< r
->length
; ++m
, p
+= 2)
2971 sprintf (p
, "%02x", data
[m
]);
2972 uiout
->field_string ("contents", data_str
);
2975 uiout
->field_skip ("contents");
2977 do_cleanups (cleanup_child
);
2980 do_cleanups (list_cleanup
);
2983 do_cleanups (old_chain
);
2987 _initialize_mi_main (void)
2989 struct cmd_list_element
*c
;
2991 add_setshow_boolean_cmd ("mi-async", class_run
,
2993 Set whether MI asynchronous mode is enabled."), _("\
2994 Show whether MI asynchronous mode is enabled."), _("\
2995 Tells GDB whether MI should be in asynchronous mode."),
2996 set_mi_async_command
,
2997 show_mi_async_command
,
3001 /* Alias old "target-async" to "mi-async". */
3002 c
= add_alias_cmd ("target-async", "mi-async", class_run
, 0, &setlist
);
3003 deprecate_cmd (c
, "set mi-async");
3004 c
= add_alias_cmd ("target-async", "mi-async", class_run
, 0, &showlist
);
3005 deprecate_cmd (c
, "show mi-async");