]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mi/mi-main.c
Eliminate make_cleanup_ui_file_delete / make ui_file a class hierarchy
[thirdparty/binutils-gdb.git] / gdb / mi / mi-main.c
CommitLineData
fb40c209 1/* MI Command Set.
cd0bfa36 2
61baf725 3 Copyright (C) 2000-2017 Free Software Foundation, Inc.
cd0bfa36 4
ab91fdd5 5 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
6
7 This file is part of GDB.
8
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
fb40c209
AC
12 (at your option) any later version.
13
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.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fb40c209 21
fb40c209 22#include "defs.h"
e17c207e 23#include "arch-utils.h"
fb40c209
AC
24#include "target.h"
25#include "inferior.h"
45741a9c 26#include "infrun.h"
fb40c209
AC
27#include "top.h"
28#include "gdbthread.h"
29#include "mi-cmds.h"
30#include "mi-parse.h"
31#include "mi-getopt.h"
32#include "mi-console.h"
33#include "ui-out.h"
34#include "mi-out.h"
4389a95a 35#include "interps.h"
fb40c209
AC
36#include "event-loop.h"
37#include "event-top.h"
41296c92 38#include "gdbcore.h" /* For write_memory(). */
56178203 39#include "value.h"
4e052eda 40#include "regcache.h"
5b7f31a4 41#include "gdb.h"
36dc181b 42#include "frame.h"
b9362cc7 43#include "mi-main.h"
66bb093b 44#include "mi-common.h"
d8ca156b 45#include "language.h"
79a45b7d 46#include "valprint.h"
3ee1c036 47#include "inferior.h"
07e059b5 48#include "osdata.h"
dc146f7c 49#include "splay-tree.h"
f224b49d 50#include "tracepoint.h"
d0353e76 51#include "ctf.h"
75082e8c 52#include "ada-lang.h"
f8eba3c6 53#include "linespec.h"
6dddc817 54#include "extension.h"
329ea579 55#include "gdbcmd.h"
4034d0ff 56#include "observer.h"
36dc181b 57
fb40c209 58#include <ctype.h>
dcb07cfa
PA
59#include "run-time-clock.h"
60#include <chrono>
d8c83789 61
fb40c209
AC
62enum
63 {
64 FROM_TTY = 0
65 };
66
fb40c209 67int mi_debug_p;
2b03b41d 68
2b03b41d
SS
69/* This is used to pass the current command timestamp down to
70 continuation routines. */
d8c83789
NR
71static struct mi_timestamp *current_command_ts;
72
73static int do_timings = 0;
74
a2840c35 75char *current_token;
2b03b41d
SS
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. */
a79b8f6e
VP
79static struct mi_parse *current_context;
80
a2840c35 81int running_result_record_printed = 1;
fb40c209 82
f3b1572e
PA
83/* Flag indicating that the target has proceeded since the last
84 command was issued. */
85int mi_proceeded;
86
fb40c209 87extern void _initialize_mi_main (void);
ce8f13f8 88static void mi_cmd_execute (struct mi_parse *parse);
fb40c209 89
b2af646b
AC
90static void mi_execute_cli_command (const char *cmd, int args_p,
91 const char *args);
c1244769 92static void mi_execute_async_cli_command (char *cli_command,
9a2b4c1b 93 char **argv, int argc);
6ed7ea50
UW
94static int register_changed_p (int regnum, struct regcache *,
95 struct regcache *);
c898adb7
YQ
96static void output_register (struct frame_info *, int regnum, int format,
97 int skip_unavailable);
4389a95a 98
329ea579
PA
99/* Controls whether the frontend wants MI in async mode. */
100static int mi_async = 0;
101
102/* The set command writes to this variable. If the inferior is
103 executing, mi_async is *not* updated. */
104static int mi_async_1 = 0;
105
106static void
107set_mi_async_command (char *args, int from_tty,
108 struct cmd_list_element *c)
109{
110 if (have_live_inferiors ())
111 {
112 mi_async_1 = mi_async;
113 error (_("Cannot change this setting while the inferior is running."));
114 }
115
116 mi_async = mi_async_1;
117}
118
119static void
120show_mi_async_command (struct ui_file *file, int from_tty,
121 struct cmd_list_element *c,
122 const char *value)
123{
124 fprintf_filtered (file,
125 _("Whether MI is in asynchronous mode is %s.\n"),
126 value);
127}
128
129/* A wrapper for target_can_async_p that takes the MI setting into
130 account. */
131
132int
133mi_async_p (void)
134{
135 return mi_async && target_can_async_p ();
136}
137
41296c92 138/* Command implementations. FIXME: Is this libgdb? No. This is the MI
fb40c209 139 layer that calls libgdb. Any operation used in the below should be
41296c92 140 formalized. */
fb40c209 141
d8c83789
NR
142static void timestamp (struct mi_timestamp *tv);
143
9204d692
PA
144static void print_diff (struct ui_file *file, struct mi_timestamp *start,
145 struct mi_timestamp *end);
d8c83789 146
ce8f13f8 147void
fb40c209
AC
148mi_cmd_gdb_exit (char *command, char **argv, int argc)
149{
9204d692
PA
150 struct mi_interp *mi
151 = (struct mi_interp *) interp_data (current_interpreter ());
152
41296c92 153 /* We have to print everything right here because we never return. */
721c02de 154 if (current_token)
9204d692
PA
155 fputs_unfiltered (current_token, mi->raw_stdout);
156 fputs_unfiltered ("^exit\n", mi->raw_stdout);
157 mi_out_put (current_uiout, mi->raw_stdout);
158 gdb_flush (mi->raw_stdout);
41296c92 159 /* FIXME: The function called is not yet a formal libgdb function. */
fb40c209 160 quit_force (NULL, FROM_TTY);
fb40c209
AC
161}
162
ce8f13f8 163void
9e22b03a 164mi_cmd_exec_next (char *command, char **argv, int argc)
fb40c209 165{
41296c92 166 /* FIXME: Should call a libgdb function, not a cli wrapper. */
e5829bee
MS
167 if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
168 mi_execute_async_cli_command ("reverse-next", argv + 1, argc - 1);
169 else
170 mi_execute_async_cli_command ("next", argv, argc);
fb40c209
AC
171}
172
ce8f13f8 173void
9e22b03a 174mi_cmd_exec_next_instruction (char *command, char **argv, int argc)
fb40c209 175{
41296c92 176 /* FIXME: Should call a libgdb function, not a cli wrapper. */
e5829bee
MS
177 if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
178 mi_execute_async_cli_command ("reverse-nexti", argv + 1, argc - 1);
179 else
180 mi_execute_async_cli_command ("nexti", argv, argc);
fb40c209
AC
181}
182
ce8f13f8 183void
9e22b03a 184mi_cmd_exec_step (char *command, char **argv, int argc)
fb40c209 185{
41296c92 186 /* FIXME: Should call a libgdb function, not a cli wrapper. */
e5829bee
MS
187 if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
188 mi_execute_async_cli_command ("reverse-step", argv + 1, argc - 1);
189 else
190 mi_execute_async_cli_command ("step", argv, argc);
fb40c209
AC
191}
192
ce8f13f8 193void
9e22b03a 194mi_cmd_exec_step_instruction (char *command, char **argv, int argc)
fb40c209 195{
41296c92 196 /* FIXME: Should call a libgdb function, not a cli wrapper. */
e5829bee
MS
197 if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
198 mi_execute_async_cli_command ("reverse-stepi", argv + 1, argc - 1);
199 else
200 mi_execute_async_cli_command ("stepi", argv, argc);
fb40c209
AC
201}
202
ce8f13f8 203void
9e22b03a 204mi_cmd_exec_finish (char *command, char **argv, int argc)
fb40c209 205{
41296c92 206 /* FIXME: Should call a libgdb function, not a cli wrapper. */
e5829bee
MS
207 if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
208 mi_execute_async_cli_command ("reverse-finish", argv + 1, argc - 1);
209 else
210 mi_execute_async_cli_command ("finish", argv, argc);
fb40c209
AC
211}
212
ce8f13f8 213void
9e22b03a 214mi_cmd_exec_return (char *command, char **argv, int argc)
fb40c209 215{
fb40c209 216 /* This command doesn't really execute the target, it just pops the
2b03b41d 217 specified number of frames. */
9e22b03a 218 if (argc)
fb40c209 219 /* Call return_command with from_tty argument equal to 0 so as to
41296c92 220 avoid being queried. */
9e22b03a 221 return_command (*argv, 0);
fb40c209
AC
222 else
223 /* Call return_command with from_tty argument equal to 0 so as to
41296c92 224 avoid being queried. */
36dc181b 225 return_command (NULL, 0);
fb40c209
AC
226
227 /* Because we have called return_command with from_tty = 0, we need
41296c92 228 to print the frame here. */
08d72866 229 print_stack_frame (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 1);
fb40c209
AC
230}
231
143260c9
VP
232void
233mi_cmd_exec_jump (char *args, char **argv, int argc)
234{
235 /* FIXME: Should call a libgdb function, not a cli wrapper. */
202b96c1 236 mi_execute_async_cli_command ("jump", argv, argc);
143260c9 237}
c1244769 238
a79b8f6e
VP
239static void
240proceed_thread (struct thread_info *thread, int pid)
8dd4f202 241{
8dd4f202 242 if (!is_stopped (thread->ptid))
a79b8f6e 243 return;
8dd4f202 244
dfd4cc63 245 if (pid != 0 && ptid_get_pid (thread->ptid) != pid)
a79b8f6e 246 return;
8dd4f202
VP
247
248 switch_to_thread (thread->ptid);
70509625 249 clear_proceed_status (0);
64ce06e4 250 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
a79b8f6e
VP
251}
252
a79b8f6e
VP
253static int
254proceed_thread_callback (struct thread_info *thread, void *arg)
255{
256 int pid = *(int *)arg;
102040f0 257
a79b8f6e 258 proceed_thread (thread, pid);
8dd4f202
VP
259 return 0;
260}
261
e5829bee
MS
262static void
263exec_continue (char **argv, int argc)
fb40c209 264{
329ea579
PA
265 prepare_execution_command (&current_target, mi_async_p ());
266
a79b8f6e 267 if (non_stop)
8dd4f202 268 {
2b03b41d
SS
269 /* In non-stop mode, 'resume' always resumes a single thread.
270 Therefore, to resume all threads of the current inferior, or
271 all threads in all inferiors, we need to iterate over
272 threads.
a79b8f6e
VP
273
274 See comment on infcmd.c:proceed_thread_callback for rationale. */
275 if (current_context->all || current_context->thread_group != -1)
276 {
277 int pid = 0;
278 struct cleanup *back_to = make_cleanup_restore_current_thread ();
8dd4f202 279
a79b8f6e
VP
280 if (!current_context->all)
281 {
9a2b4c1b
MS
282 struct inferior *inf
283 = find_inferior_id (current_context->thread_group);
284
a79b8f6e
VP
285 pid = inf->pid;
286 }
287 iterate_over_threads (proceed_thread_callback, &pid);
288 do_cleanups (back_to);
289 }
290 else
291 {
292 continue_1 (0);
293 }
8dd4f202 294 }
77ebaa5a 295 else
a79b8f6e 296 {
b7b633e9 297 scoped_restore save_multi = make_scoped_restore (&sched_multi);
102040f0 298
a79b8f6e
VP
299 if (current_context->all)
300 {
301 sched_multi = 1;
302 continue_1 (0);
303 }
304 else
305 {
2b03b41d
SS
306 /* In all-stop mode, -exec-continue traditionally resumed
307 either all threads, or one thread, depending on the
308 'scheduler-locking' variable. Let's continue to do the
309 same. */
a79b8f6e
VP
310 continue_1 (1);
311 }
a79b8f6e 312 }
e5829bee
MS
313}
314
e5829bee 315static void
a79b8f6e 316exec_direction_forward (void *notused)
e5829bee 317{
e5829bee
MS
318 execution_direction = EXEC_FORWARD;
319}
320
321static void
322exec_reverse_continue (char **argv, int argc)
323{
324 enum exec_direction_kind dir = execution_direction;
325 struct cleanup *old_chain;
326
e5829bee
MS
327 if (dir == EXEC_REVERSE)
328 error (_("Already in reverse mode."));
329
330 if (!target_can_execute_reverse)
331 error (_("Target %s does not support this command."), target_shortname);
332
a79b8f6e 333 old_chain = make_cleanup (exec_direction_forward, NULL);
e5829bee
MS
334 execution_direction = EXEC_REVERSE;
335 exec_continue (argv, argc);
336 do_cleanups (old_chain);
337}
338
339void
340mi_cmd_exec_continue (char *command, char **argv, int argc)
341{
a79b8f6e 342 if (argc > 0 && strcmp (argv[0], "--reverse") == 0)
e5829bee
MS
343 exec_reverse_continue (argv + 1, argc - 1);
344 else
345 exec_continue (argv, argc);
8dd4f202
VP
346}
347
348static int
349interrupt_thread_callback (struct thread_info *thread, void *arg)
350{
351 int pid = *(int *)arg;
352
353 if (!is_running (thread->ptid))
354 return 0;
355
dfd4cc63 356 if (ptid_get_pid (thread->ptid) != pid)
8dd4f202
VP
357 return 0;
358
359 target_stop (thread->ptid);
360 return 0;
fb40c209
AC
361}
362
2b03b41d
SS
363/* Interrupt the execution of the target. Note how we must play
364 around with the token variables, in order to display the current
365 token in the result of the interrupt command, and the previous
366 execution token when the target finally stops. See comments in
41296c92 367 mi_cmd_execute. */
2b03b41d 368
ce8f13f8 369void
9e22b03a 370mi_cmd_exec_interrupt (char *command, char **argv, int argc)
fb40c209 371{
a79b8f6e
VP
372 /* In all-stop mode, everything stops, so we don't need to try
373 anything specific. */
374 if (!non_stop)
77ebaa5a 375 {
77ebaa5a 376 interrupt_target_1 (0);
a79b8f6e 377 return;
77ebaa5a 378 }
a79b8f6e
VP
379
380 if (current_context->all)
77ebaa5a 381 {
a79b8f6e 382 /* This will interrupt all threads in all inferiors. */
77ebaa5a
VP
383 interrupt_target_1 (1);
384 }
a79b8f6e 385 else if (current_context->thread_group != -1)
8dd4f202 386 {
a79b8f6e 387 struct inferior *inf = find_inferior_id (current_context->thread_group);
102040f0 388
a79b8f6e
VP
389 iterate_over_threads (interrupt_thread_callback, &inf->pid);
390 }
391 else
392 {
393 /* Interrupt just the current thread -- either explicitly
394 specified via --thread or whatever was current before
395 MI command was sent. */
396 interrupt_target_1 (0);
397 }
398}
399
5713b9b5
JB
400/* Callback for iterate_over_inferiors which starts the execution
401 of the given inferior.
402
403 ARG is a pointer to an integer whose value, if non-zero, indicates
404 that the program should be stopped when reaching the main subprogram
405 (similar to what the CLI "start" command does). */
406
a79b8f6e
VP
407static int
408run_one_inferior (struct inferior *inf, void *arg)
409{
5713b9b5
JB
410 int start_p = *(int *) arg;
411 const char *run_cmd = start_p ? "start" : "run";
61c6156d
SM
412 struct target_ops *run_target = find_run_target ();
413 int async_p = mi_async && run_target->to_can_async_p (run_target);
5713b9b5 414
a79b8f6e
VP
415 if (inf->pid != 0)
416 {
417 if (inf->pid != ptid_get_pid (inferior_ptid))
418 {
419 struct thread_info *tp;
8dd4f202 420
a79b8f6e
VP
421 tp = any_thread_of_process (inf->pid);
422 if (!tp)
423 error (_("Inferior has no threads."));
424
425 switch_to_thread (tp->ptid);
426 }
8dd4f202 427 }
77ebaa5a 428 else
a79b8f6e
VP
429 {
430 set_current_inferior (inf);
431 switch_to_thread (null_ptid);
432 set_current_program_space (inf->pspace);
433 }
61c6156d
SM
434 mi_execute_cli_command (run_cmd, async_p,
435 async_p ? "&" : NULL);
a79b8f6e 436 return 0;
fb40c209
AC
437}
438
115d30f9
VP
439void
440mi_cmd_exec_run (char *command, char **argv, int argc)
441{
5713b9b5
JB
442 int start_p = 0;
443
444 /* Parse the command options. */
445 enum opt
446 {
447 START_OPT,
448 };
449 static const struct mi_opt opts[] =
450 {
451 {"-start", START_OPT, 0},
452 {NULL, 0, 0},
453 };
454
455 int oind = 0;
456 char *oarg;
457
458 while (1)
459 {
460 int opt = mi_getopt ("-exec-run", argc, argv, opts, &oind, &oarg);
461
462 if (opt < 0)
463 break;
464 switch ((enum opt) opt)
465 {
466 case START_OPT:
467 start_p = 1;
468 break;
469 }
470 }
471
472 /* This command does not accept any argument. Make sure the user
473 did not provide any. */
474 if (oind != argc)
475 error (_("Invalid argument: %s"), argv[oind]);
476
a79b8f6e
VP
477 if (current_context->all)
478 {
479 struct cleanup *back_to = save_current_space_and_thread ();
102040f0 480
5713b9b5 481 iterate_over_inferiors (run_one_inferior, &start_p);
a79b8f6e
VP
482 do_cleanups (back_to);
483 }
484 else
485 {
5713b9b5 486 const char *run_cmd = start_p ? "start" : "run";
61c6156d
SM
487 struct target_ops *run_target = find_run_target ();
488 int async_p = mi_async && run_target->to_can_async_p (run_target);
5713b9b5 489
61c6156d
SM
490 mi_execute_cli_command (run_cmd, async_p,
491 async_p ? "&" : NULL);
a79b8f6e 492 }
115d30f9
VP
493}
494
a79b8f6e 495
6418d433
VP
496static int
497find_thread_of_process (struct thread_info *ti, void *p)
498{
499 int pid = *(int *)p;
102040f0 500
dfd4cc63 501 if (ptid_get_pid (ti->ptid) == pid && !is_exited (ti->ptid))
6418d433
VP
502 return 1;
503
504 return 0;
505}
506
507void
508mi_cmd_target_detach (char *command, char **argv, int argc)
509{
510 if (argc != 0 && argc != 1)
9b20d036 511 error (_("Usage: -target-detach [pid | thread-group]"));
6418d433
VP
512
513 if (argc == 1)
514 {
515 struct thread_info *tp;
516 char *end = argv[0];
f1b9e6e7 517 int pid;
102040f0 518
f1b9e6e7
MK
519 /* First see if we are dealing with a thread-group id. */
520 if (*argv[0] == 'i')
521 {
522 struct inferior *inf;
523 int id = strtoul (argv[0] + 1, &end, 0);
524
525 if (*end != '\0')
526 error (_("Invalid syntax of thread-group id '%s'"), argv[0]);
527
528 inf = find_inferior_id (id);
529 if (!inf)
530 error (_("Non-existent thread-group id '%d'"), id);
531
532 pid = inf->pid;
533 }
534 else
535 {
536 /* We must be dealing with a pid. */
537 pid = strtol (argv[0], &end, 10);
538
539 if (*end != '\0')
540 error (_("Invalid identifier '%s'"), argv[0]);
541 }
6418d433
VP
542
543 /* Pick any thread in the desired process. Current
f1b9e6e7 544 target_detach detaches from the parent of inferior_ptid. */
6418d433
VP
545 tp = iterate_over_threads (find_thread_of_process, &pid);
546 if (!tp)
547 error (_("Thread group is empty"));
548
549 switch_to_thread (tp->ptid);
550 }
551
552 detach_command (NULL, 0);
553}
554
78cbbba8
LM
555void
556mi_cmd_target_flash_erase (char *command, char **argv, int argc)
557{
558 flash_erase_command (NULL, 0);
559}
560
ce8f13f8 561void
fb40c209
AC
562mi_cmd_thread_select (char *command, char **argv, int argc)
563{
564 enum gdb_rc rc;
a13e061a 565 char *mi_error_message;
4034d0ff 566 ptid_t previous_ptid = inferior_ptid;
fb40c209
AC
567
568 if (argc != 1)
1b05df00 569 error (_("-thread-select: USAGE: threadnum."));
a13e061a 570
79a45e25 571 rc = gdb_thread_select (current_uiout, argv[0], &mi_error_message);
a13e061a 572
4034d0ff 573 /* If thread switch did not succeed don't notify or print. */
a13e061a 574 if (rc == GDB_RC_FAIL)
fb40c209 575 {
a13e061a
PA
576 make_cleanup (xfree, mi_error_message);
577 error ("%s", mi_error_message);
fb40c209 578 }
4034d0ff
AT
579
580 print_selected_thread_frame (current_uiout,
581 USER_SELECTED_THREAD | USER_SELECTED_FRAME);
582
583 /* Notify if the thread has effectively changed. */
584 if (!ptid_equal (inferior_ptid, previous_ptid))
585 {
586 observer_notify_user_selected_context_changed (USER_SELECTED_THREAD
587 | USER_SELECTED_FRAME);
588 }
fb40c209
AC
589}
590
ce8f13f8 591void
fb40c209
AC
592mi_cmd_thread_list_ids (char *command, char **argv, int argc)
593{
b0b13bb4 594 enum gdb_rc rc;
a13e061a 595 char *mi_error_message;
fb40c209
AC
596
597 if (argc != 0)
7ea6d463 598 error (_("-thread-list-ids: No arguments required."));
a13e061a 599
79a45e25 600 rc = gdb_list_thread_ids (current_uiout, &mi_error_message);
a13e061a
PA
601
602 if (rc == GDB_RC_FAIL)
fb40c209 603 {
a13e061a
PA
604 make_cleanup (xfree, mi_error_message);
605 error ("%s", mi_error_message);
fb40c209 606 }
fb40c209
AC
607}
608
ce8f13f8 609void
8e8901c5
VP
610mi_cmd_thread_info (char *command, char **argv, int argc)
611{
8e8901c5 612 if (argc != 0 && argc != 1)
7ea6d463 613 error (_("Invalid MI command"));
8e8901c5 614
79a45e25 615 print_thread_info (current_uiout, argv[0], -1);
3ee1c036
VP
616}
617
dc146f7c
VP
618struct collect_cores_data
619{
620 int pid;
621
622 VEC (int) *cores;
623};
624
3ee1c036 625static int
dc146f7c 626collect_cores (struct thread_info *ti, void *xdata)
3ee1c036 627{
19ba03f4 628 struct collect_cores_data *data = (struct collect_cores_data *) xdata;
dc146f7c
VP
629
630 if (ptid_get_pid (ti->ptid) == data->pid)
6c95b8df 631 {
dc146f7c 632 int core = target_core_of_thread (ti->ptid);
102040f0 633
dc146f7c
VP
634 if (core != -1)
635 VEC_safe_push (int, data->cores, core);
636 }
637
638 return 0;
639}
640
641static int *
642unique (int *b, int *e)
643{
644 int *d = b;
102040f0 645
dc146f7c
VP
646 while (++b != e)
647 if (*d != *b)
648 *++d = *b;
649 return ++d;
650}
651
652struct print_one_inferior_data
653{
654 int recurse;
655 VEC (int) *inferiors;
656};
657
658static int
659print_one_inferior (struct inferior *inferior, void *xdata)
660{
19ba03f4
SM
661 struct print_one_inferior_data *top_data
662 = (struct print_one_inferior_data *) xdata;
79a45e25 663 struct ui_out *uiout = current_uiout;
dc146f7c
VP
664
665 if (VEC_empty (int, top_data->inferiors)
666 || bsearch (&(inferior->pid), VEC_address (int, top_data->inferiors),
667 VEC_length (int, top_data->inferiors), sizeof (int),
668 compare_positive_ints))
669 {
670 struct collect_cores_data data;
6c95b8df
PA
671 struct cleanup *back_to
672 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
673
112e8700
SM
674 uiout->field_fmt ("id", "i%d", inferior->num);
675 uiout->field_string ("type", "process");
2ddf4301 676 if (inferior->has_exit_code)
112e8700 677 uiout->field_string ("exit-code",
2ddf4301 678 int_string (inferior->exit_code, 8, 0, 0, 1));
a79b8f6e 679 if (inferior->pid != 0)
112e8700 680 uiout->field_int ("pid", inferior->pid);
a79b8f6e 681
1f0c4988 682 if (inferior->pspace->pspace_exec_filename != NULL)
a79b8f6e 683 {
112e8700 684 uiout->field_string ("executable",
1f0c4988 685 inferior->pspace->pspace_exec_filename);
a79b8f6e 686 }
6c95b8df 687
dc146f7c 688 data.cores = 0;
a79b8f6e
VP
689 if (inferior->pid != 0)
690 {
691 data.pid = inferior->pid;
692 iterate_over_threads (collect_cores, &data);
693 }
dc146f7c
VP
694
695 if (!VEC_empty (int, data.cores))
696 {
dc146f7c
VP
697 int *b, *e;
698 struct cleanup *back_to_2 =
699 make_cleanup_ui_out_list_begin_end (uiout, "cores");
700
701 qsort (VEC_address (int, data.cores),
702 VEC_length (int, data.cores), sizeof (int),
703 compare_positive_ints);
704
705 b = VEC_address (int, data.cores);
706 e = b + VEC_length (int, data.cores);
707 e = unique (b, e);
708
709 for (; b != e; ++b)
112e8700 710 uiout->field_int (NULL, *b);
dc146f7c
VP
711
712 do_cleanups (back_to_2);
713 }
714
715 if (top_data->recurse)
aea5b279 716 print_thread_info (uiout, NULL, inferior->pid);
dc146f7c 717
6c95b8df
PA
718 do_cleanups (back_to);
719 }
3ee1c036 720
3ee1c036
VP
721 return 0;
722}
723
2b03b41d
SS
724/* Output a field named 'cores' with a list as the value. The
725 elements of the list are obtained by splitting 'cores' on
726 comma. */
dc146f7c
VP
727
728static void
729output_cores (struct ui_out *uiout, const char *field_name, const char *xcores)
3ee1c036 730{
dc146f7c
VP
731 struct cleanup *back_to = make_cleanup_ui_out_list_begin_end (uiout,
732 field_name);
733 char *cores = xstrdup (xcores);
734 char *p = cores;
3ee1c036 735
dc146f7c 736 make_cleanup (xfree, cores);
3ee1c036 737
dc146f7c 738 for (p = strtok (p, ","); p; p = strtok (NULL, ","))
112e8700 739 uiout->field_string (NULL, p);
3ee1c036 740
dc146f7c
VP
741 do_cleanups (back_to);
742}
3ee1c036 743
dc146f7c
VP
744static void
745free_vector_of_ints (void *xvector)
746{
19ba03f4 747 VEC (int) **vector = (VEC (int) **) xvector;
102040f0 748
dc146f7c
VP
749 VEC_free (int, *vector);
750}
751
752static void
753do_nothing (splay_tree_key k)
754{
755}
07e059b5 756
dc146f7c
VP
757static void
758free_vector_of_osdata_items (splay_tree_value xvalue)
759{
760 VEC (osdata_item_s) *value = (VEC (osdata_item_s) *) xvalue;
102040f0 761
dc146f7c
VP
762 /* We don't free the items itself, it will be done separately. */
763 VEC_free (osdata_item_s, value);
764}
e0665bc8 765
dc146f7c
VP
766static int
767splay_tree_int_comparator (splay_tree_key xa, splay_tree_key xb)
768{
769 int a = xa;
770 int b = xb;
102040f0 771
dc146f7c
VP
772 return a - b;
773}
774
775static void
776free_splay_tree (void *xt)
777{
19ba03f4 778 splay_tree t = (splay_tree) xt;
dc146f7c
VP
779 splay_tree_delete (t);
780}
781
782static void
783list_available_thread_groups (VEC (int) *ids, int recurse)
784{
785 struct osdata *data;
786 struct osdata_item *item;
787 int ix_items;
79a45e25 788 struct ui_out *uiout = current_uiout;
b9635925 789 struct cleanup *cleanup;
102040f0 790
dc146f7c 791 /* This keeps a map from integer (pid) to VEC (struct osdata_item *)*
8eee9c5a
DE
792 The vector contains information about all threads for the given pid.
793 This is assigned an initial value to avoid "may be used uninitialized"
794 warning from gcc. */
795 splay_tree tree = NULL;
dc146f7c
VP
796
797 /* get_osdata will throw if it cannot return data. */
798 data = get_osdata ("processes");
b9635925 799 cleanup = make_cleanup_osdata_free (data);
dc146f7c
VP
800
801 if (recurse)
802 {
803 struct osdata *threads = get_osdata ("threads");
dc146f7c 804
102040f0 805 make_cleanup_osdata_free (threads);
dc146f7c
VP
806 tree = splay_tree_new (splay_tree_int_comparator,
807 do_nothing,
808 free_vector_of_osdata_items);
809 make_cleanup (free_splay_tree, tree);
e0665bc8 810
07e059b5 811 for (ix_items = 0;
dc146f7c 812 VEC_iterate (osdata_item_s, threads->items,
e0665bc8 813 ix_items, item);
07e059b5
VP
814 ix_items++)
815 {
07e059b5 816 const char *pid = get_osdata_column (item, "pid");
dc146f7c
VP
817 int pid_i = strtoul (pid, NULL, 0);
818 VEC (osdata_item_s) *vec = 0;
819
820 splay_tree_node n = splay_tree_lookup (tree, pid_i);
821 if (!n)
822 {
823 VEC_safe_push (osdata_item_s, vec, item);
824 splay_tree_insert (tree, pid_i, (splay_tree_value)vec);
825 }
826 else
827 {
828 vec = (VEC (osdata_item_s) *) n->value;
829 VEC_safe_push (osdata_item_s, vec, item);
830 n->value = (splay_tree_value) vec;
831 }
832 }
833 }
834
835 make_cleanup_ui_out_list_begin_end (uiout, "groups");
07e059b5 836
dc146f7c
VP
837 for (ix_items = 0;
838 VEC_iterate (osdata_item_s, data->items,
839 ix_items, item);
840 ix_items++)
841 {
842 struct cleanup *back_to;
e0665bc8 843
dc146f7c
VP
844 const char *pid = get_osdata_column (item, "pid");
845 const char *cmd = get_osdata_column (item, "command");
846 const char *user = get_osdata_column (item, "user");
847 const char *cores = get_osdata_column (item, "cores");
848
849 int pid_i = strtoul (pid, NULL, 0);
850
851 /* At present, the target will return all available processes
852 and if information about specific ones was required, we filter
853 undesired processes here. */
854 if (ids && bsearch (&pid_i, VEC_address (int, ids),
855 VEC_length (int, ids),
856 sizeof (int), compare_positive_ints) == NULL)
857 continue;
858
859
860 back_to = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
861
112e8700
SM
862 uiout->field_fmt ("id", "%s", pid);
863 uiout->field_string ("type", "process");
dc146f7c 864 if (cmd)
112e8700 865 uiout->field_string ("description", cmd);
dc146f7c 866 if (user)
112e8700 867 uiout->field_string ("user", user);
dc146f7c
VP
868 if (cores)
869 output_cores (uiout, "cores", cores);
870
871 if (recurse)
872 {
873 splay_tree_node n = splay_tree_lookup (tree, pid_i);
874 if (n)
875 {
876 VEC (osdata_item_s) *children = (VEC (osdata_item_s) *) n->value;
877 struct osdata_item *child;
878 int ix_child;
879
880 make_cleanup_ui_out_list_begin_end (uiout, "threads");
881
882 for (ix_child = 0;
883 VEC_iterate (osdata_item_s, children, ix_child, child);
884 ++ix_child)
885 {
886 struct cleanup *back_to_2 =
887 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
dc146f7c
VP
888 const char *tid = get_osdata_column (child, "tid");
889 const char *tcore = get_osdata_column (child, "core");
102040f0 890
112e8700 891 uiout->field_string ("id", tid);
dc146f7c 892 if (tcore)
112e8700 893 uiout->field_string ("core", tcore);
dc146f7c
VP
894
895 do_cleanups (back_to_2);
896 }
897 }
07e059b5 898 }
dc146f7c
VP
899
900 do_cleanups (back_to);
07e059b5 901 }
b9635925
TT
902
903 do_cleanups (cleanup);
dc146f7c
VP
904}
905
906void
907mi_cmd_list_thread_groups (char *command, char **argv, int argc)
908{
79a45e25 909 struct ui_out *uiout = current_uiout;
dc146f7c
VP
910 struct cleanup *back_to;
911 int available = 0;
912 int recurse = 0;
913 VEC (int) *ids = 0;
914
915 enum opt
dc146f7c 916 {
2b03b41d 917 AVAILABLE_OPT, RECURSE_OPT
dc146f7c 918 };
2b03b41d
SS
919 static const struct mi_opt opts[] =
920 {
921 {"-available", AVAILABLE_OPT, 0},
922 {"-recurse", RECURSE_OPT, 1},
923 { 0, 0, 0 }
924 };
dc146f7c 925
56934ab1
AS
926 int oind = 0;
927 char *oarg;
dc146f7c
VP
928
929 while (1)
930 {
931 int opt = mi_getopt ("-list-thread-groups", argc, argv, opts,
56934ab1 932 &oind, &oarg);
102040f0 933
dc146f7c
VP
934 if (opt < 0)
935 break;
936 switch ((enum opt) opt)
937 {
938 case AVAILABLE_OPT:
939 available = 1;
940 break;
941 case RECURSE_OPT:
56934ab1 942 if (strcmp (oarg, "0") == 0)
dc146f7c 943 ;
56934ab1 944 else if (strcmp (oarg, "1") == 0)
dc146f7c
VP
945 recurse = 1;
946 else
7ea6d463
PM
947 error (_("only '0' and '1' are valid values "
948 "for the '--recurse' option"));
dc146f7c
VP
949 break;
950 }
951 }
952
56934ab1 953 for (; oind < argc; ++oind)
dc146f7c
VP
954 {
955 char *end;
2f296114
VP
956 int inf;
957
56934ab1
AS
958 if (*(argv[oind]) != 'i')
959 error (_("invalid syntax of group id '%s'"), argv[oind]);
2f296114 960
56934ab1 961 inf = strtoul (argv[oind] + 1, &end, 0);
102040f0 962
dc146f7c 963 if (*end != '\0')
56934ab1 964 error (_("invalid syntax of group id '%s'"), argv[oind]);
dc146f7c
VP
965 VEC_safe_push (int, ids, inf);
966 }
967 if (VEC_length (int, ids) > 1)
968 qsort (VEC_address (int, ids),
969 VEC_length (int, ids),
970 sizeof (int), compare_positive_ints);
971
972 back_to = make_cleanup (free_vector_of_ints, &ids);
973
974 if (available)
975 {
976 list_available_thread_groups (ids, recurse);
977 }
978 else if (VEC_length (int, ids) == 1)
3ee1c036 979 {
2b03b41d 980 /* Local thread groups, single id. */
2f296114
VP
981 int id = *VEC_address (int, ids);
982 struct inferior *inf = find_inferior_id (id);
102040f0 983
2f296114 984 if (!inf)
7ea6d463 985 error (_("Non-existent thread group id '%d'"), id);
c1244769 986
aea5b279 987 print_thread_info (uiout, NULL, inf->pid);
3ee1c036
VP
988 }
989 else
990 {
dc146f7c 991 struct print_one_inferior_data data;
102040f0 992
dc146f7c
VP
993 data.recurse = recurse;
994 data.inferiors = ids;
995
996 /* Local thread groups. Either no explicit ids -- and we
997 print everything, or several explicit ids. In both cases,
998 we print more than one group, and have to use 'groups'
999 as the top-level element. */
3ee1c036 1000 make_cleanup_ui_out_list_begin_end (uiout, "groups");
dc146f7c
VP
1001 update_thread_list ();
1002 iterate_over_inferiors (print_one_inferior, &data);
3ee1c036 1003 }
dc146f7c 1004
3ee1c036 1005 do_cleanups (back_to);
8e8901c5
VP
1006}
1007
ce8f13f8 1008void
fb40c209
AC
1009mi_cmd_data_list_register_names (char *command, char **argv, int argc)
1010{
7ccb0be9 1011 struct gdbarch *gdbarch;
79a45e25 1012 struct ui_out *uiout = current_uiout;
fb40c209
AC
1013 int regnum, numregs;
1014 int i;
4060713b 1015 struct cleanup *cleanup;
fb40c209
AC
1016
1017 /* Note that the test for a valid register must include checking the
2b03b41d
SS
1018 gdbarch_register_name because gdbarch_num_regs may be allocated
1019 for the union of the register sets within a family of related
1020 processors. In this case, some entries of gdbarch_register_name
1021 will change depending upon the particular processor being
1022 debugged. */
fb40c209 1023
441b986a 1024 gdbarch = get_current_arch ();
7ccb0be9 1025 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
fb40c209 1026
4060713b 1027 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names");
fb40c209 1028
41296c92 1029 if (argc == 0) /* No args, just do all the regs. */
fb40c209
AC
1030 {
1031 for (regnum = 0;
1032 regnum < numregs;
1033 regnum++)
1034 {
7ccb0be9
UW
1035 if (gdbarch_register_name (gdbarch, regnum) == NULL
1036 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
112e8700 1037 uiout->field_string (NULL, "");
173d6894 1038 else
112e8700 1039 uiout->field_string (NULL, gdbarch_register_name (gdbarch, regnum));
fb40c209
AC
1040 }
1041 }
1042
41296c92 1043 /* Else, list of register #s, just do listed regs. */
fb40c209
AC
1044 for (i = 0; i < argc; i++)
1045 {
1046 regnum = atoi (argv[i]);
173d6894 1047 if (regnum < 0 || regnum >= numregs)
7ea6d463 1048 error (_("bad register number"));
a13e061a 1049
7ccb0be9
UW
1050 if (gdbarch_register_name (gdbarch, regnum) == NULL
1051 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
112e8700 1052 uiout->field_string (NULL, "");
173d6894 1053 else
112e8700 1054 uiout->field_string (NULL, gdbarch_register_name (gdbarch, regnum));
fb40c209 1055 }
4060713b 1056 do_cleanups (cleanup);
fb40c209
AC
1057}
1058
ce8f13f8 1059void
fb40c209
AC
1060mi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
1061{
6ed7ea50 1062 static struct regcache *this_regs = NULL;
79a45e25 1063 struct ui_out *uiout = current_uiout;
6ed7ea50 1064 struct regcache *prev_regs;
7ccb0be9 1065 struct gdbarch *gdbarch;
fb40c209
AC
1066 int regnum, numregs, changed;
1067 int i;
4060713b 1068 struct cleanup *cleanup;
fb40c209 1069
2b03b41d
SS
1070 /* The last time we visited this function, the current frame's
1071 register contents were saved in THIS_REGS. Move THIS_REGS over
1072 to PREV_REGS, and refresh THIS_REGS with the now-current register
1073 contents. */
6ed7ea50
UW
1074
1075 prev_regs = this_regs;
1076 this_regs = frame_save_as_regcache (get_selected_frame (NULL));
1077 cleanup = make_cleanup_regcache_xfree (prev_regs);
1078
fb40c209 1079 /* Note that the test for a valid register must include checking the
2b03b41d
SS
1080 gdbarch_register_name because gdbarch_num_regs may be allocated
1081 for the union of the register sets within a family of related
1082 processors. In this case, some entries of gdbarch_register_name
1083 will change depending upon the particular processor being
1084 debugged. */
fb40c209 1085
7ccb0be9
UW
1086 gdbarch = get_regcache_arch (this_regs);
1087 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
fb40c209 1088
6ed7ea50 1089 make_cleanup_ui_out_list_begin_end (uiout, "changed-registers");
fb40c209 1090
2b03b41d 1091 if (argc == 0)
fb40c209 1092 {
2b03b41d 1093 /* No args, just do all the regs. */
fb40c209
AC
1094 for (regnum = 0;
1095 regnum < numregs;
1096 regnum++)
1097 {
7ccb0be9
UW
1098 if (gdbarch_register_name (gdbarch, regnum) == NULL
1099 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
fb40c209 1100 continue;
6ed7ea50 1101 changed = register_changed_p (regnum, prev_regs, this_regs);
fb40c209 1102 if (changed < 0)
7ea6d463
PM
1103 error (_("-data-list-changed-registers: "
1104 "Unable to read register contents."));
fb40c209 1105 else if (changed)
112e8700 1106 uiout->field_int (NULL, regnum);
fb40c209
AC
1107 }
1108 }
1109
41296c92 1110 /* Else, list of register #s, just do listed regs. */
fb40c209
AC
1111 for (i = 0; i < argc; i++)
1112 {
1113 regnum = atoi (argv[i]);
1114
1115 if (regnum >= 0
1116 && regnum < numregs
7ccb0be9
UW
1117 && gdbarch_register_name (gdbarch, regnum) != NULL
1118 && *gdbarch_register_name (gdbarch, regnum) != '\000')
fb40c209 1119 {
6ed7ea50 1120 changed = register_changed_p (regnum, prev_regs, this_regs);
fb40c209 1121 if (changed < 0)
7ea6d463
PM
1122 error (_("-data-list-changed-registers: "
1123 "Unable to read register contents."));
fb40c209 1124 else if (changed)
112e8700 1125 uiout->field_int (NULL, regnum);
fb40c209
AC
1126 }
1127 else
7ea6d463 1128 error (_("bad register number"));
fb40c209 1129 }
4060713b 1130 do_cleanups (cleanup);
fb40c209
AC
1131}
1132
1133static int
6ed7ea50
UW
1134register_changed_p (int regnum, struct regcache *prev_regs,
1135 struct regcache *this_regs)
fb40c209 1136{
6ed7ea50
UW
1137 struct gdbarch *gdbarch = get_regcache_arch (this_regs);
1138 gdb_byte prev_buffer[MAX_REGISTER_SIZE];
1139 gdb_byte this_buffer[MAX_REGISTER_SIZE];
e69aa73e
PA
1140 enum register_status prev_status;
1141 enum register_status this_status;
fb40c209 1142
e69aa73e
PA
1143 /* First time through or after gdbarch change consider all registers
1144 as changed. */
1145 if (!prev_regs || get_regcache_arch (prev_regs) != gdbarch)
6ed7ea50 1146 return 1;
fb40c209 1147
6ed7ea50 1148 /* Get register contents and compare. */
e69aa73e
PA
1149 prev_status = regcache_cooked_read (prev_regs, regnum, prev_buffer);
1150 this_status = regcache_cooked_read (this_regs, regnum, this_buffer);
fb40c209 1151
e69aa73e
PA
1152 if (this_status != prev_status)
1153 return 1;
1154 else if (this_status == REG_VALID)
1155 return memcmp (prev_buffer, this_buffer,
1156 register_size (gdbarch, regnum)) != 0;
1157 else
1158 return 0;
fb40c209
AC
1159}
1160
41296c92 1161/* Return a list of register number and value pairs. The valid
fb40c209 1162 arguments expected are: a letter indicating the format in which to
2b03b41d
SS
1163 display the registers contents. This can be one of: x
1164 (hexadecimal), d (decimal), N (natural), t (binary), o (octal), r
1165 (raw). After the format argument there can be a sequence of
1166 numbers, indicating which registers to fetch the content of. If
1167 the format is the only argument, a list of all the registers with
1168 their values is returned. */
1169
ce8f13f8 1170void
fb40c209
AC
1171mi_cmd_data_list_register_values (char *command, char **argv, int argc)
1172{
79a45e25 1173 struct ui_out *uiout = current_uiout;
7ccb0be9
UW
1174 struct frame_info *frame;
1175 struct gdbarch *gdbarch;
a13e061a 1176 int regnum, numregs, format;
fb40c209 1177 int i;
1edebdbf 1178 struct cleanup *list_cleanup;
c898adb7
YQ
1179 int skip_unavailable = 0;
1180 int oind = 0;
1181 enum opt
1182 {
1183 SKIP_UNAVAILABLE,
1184 };
1185 static const struct mi_opt opts[] =
1186 {
1187 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
1188 { 0, 0, 0 }
1189 };
fb40c209
AC
1190
1191 /* Note that the test for a valid register must include checking the
2b03b41d
SS
1192 gdbarch_register_name because gdbarch_num_regs may be allocated
1193 for the union of the register sets within a family of related
1194 processors. In this case, some entries of gdbarch_register_name
1195 will change depending upon the particular processor being
1196 debugged. */
fb40c209 1197
c898adb7
YQ
1198 while (1)
1199 {
1200 char *oarg;
1201 int opt = mi_getopt ("-data-list-register-values", argc, argv,
1202 opts, &oind, &oarg);
1203
1204 if (opt < 0)
1205 break;
1206 switch ((enum opt) opt)
1207 {
1208 case SKIP_UNAVAILABLE:
1209 skip_unavailable = 1;
1210 break;
1211 }
1212 }
1213
1214 if (argc - oind < 1)
7ea6d463 1215 error (_("-data-list-register-values: Usage: "
c898adb7
YQ
1216 "-data-list-register-values [--skip-unavailable] <format>"
1217 " [<regnum1>...<regnumN>]"));
fb40c209 1218
c898adb7 1219 format = (int) argv[oind][0];
fb40c209 1220
7ccb0be9
UW
1221 frame = get_selected_frame (NULL);
1222 gdbarch = get_frame_arch (frame);
1223 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1224
4060713b 1225 list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values");
fb40c209 1226
c898adb7 1227 if (argc - oind == 1)
fb40c209 1228 {
2b03b41d 1229 /* No args, beside the format: do all the regs. */
fb40c209
AC
1230 for (regnum = 0;
1231 regnum < numregs;
1232 regnum++)
1233 {
7ccb0be9
UW
1234 if (gdbarch_register_name (gdbarch, regnum) == NULL
1235 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
fb40c209 1236 continue;
1edebdbf 1237
c898adb7 1238 output_register (frame, regnum, format, skip_unavailable);
fb40c209
AC
1239 }
1240 }
1241
41296c92 1242 /* Else, list of register #s, just do listed regs. */
c898adb7 1243 for (i = 1 + oind; i < argc; i++)
fb40c209
AC
1244 {
1245 regnum = atoi (argv[i]);
1246
1247 if (regnum >= 0
1248 && regnum < numregs
7ccb0be9
UW
1249 && gdbarch_register_name (gdbarch, regnum) != NULL
1250 && *gdbarch_register_name (gdbarch, regnum) != '\000')
c898adb7 1251 output_register (frame, regnum, format, skip_unavailable);
fb40c209 1252 else
7ea6d463 1253 error (_("bad register number"));
fb40c209 1254 }
4060713b 1255 do_cleanups (list_cleanup);
fb40c209
AC
1256}
1257
c898adb7
YQ
1258/* Output one register REGNUM's contents in the desired FORMAT. If
1259 SKIP_UNAVAILABLE is true, skip the register if it is
1260 unavailable. */
2b03b41d 1261
a13e061a 1262static void
c898adb7
YQ
1263output_register (struct frame_info *frame, int regnum, int format,
1264 int skip_unavailable)
fb40c209 1265{
79a45e25 1266 struct ui_out *uiout = current_uiout;
901461f8 1267 struct value *val = value_of_register (regnum, frame);
1edebdbf 1268 struct cleanup *tuple_cleanup;
fdc8aae8 1269 struct value_print_options opts;
1edebdbf 1270
c898adb7
YQ
1271 if (skip_unavailable && !value_entirely_available (val))
1272 return;
1273
1edebdbf 1274 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
112e8700 1275 uiout->field_int ("number", regnum);
fb40c209 1276
fb40c209
AC
1277 if (format == 'N')
1278 format = 0;
1279
fb40c209 1280 if (format == 'r')
fdc8aae8
AB
1281 format = 'z';
1282
d7e74731 1283 string_file stb;
fdc8aae8
AB
1284
1285 get_formatted_print_options (&opts, format);
1286 opts.deref_ref = 1;
1287 val_print (value_type (val),
fdc8aae8 1288 value_embedded_offset (val), 0,
d7e74731 1289 &stb, 0, val, &opts, current_language);
112e8700 1290 uiout->field_stream ("value", stb);
1edebdbf
YQ
1291
1292 do_cleanups (tuple_cleanup);
fb40c209
AC
1293}
1294
24e8cecf 1295/* Write given values into registers. The registers and values are
c1244769 1296 given as pairs. The corresponding MI command is
9a2b4c1b
MS
1297 -data-write-register-values <format>
1298 [<regnum1> <value1>...<regnumN> <valueN>] */
ce8f13f8 1299void
24e8cecf
EZ
1300mi_cmd_data_write_register_values (char *command, char **argv, int argc)
1301{
7ccb0be9
UW
1302 struct regcache *regcache;
1303 struct gdbarch *gdbarch;
9f3a1602 1304 int numregs, i;
24e8cecf
EZ
1305
1306 /* Note that the test for a valid register must include checking the
2b03b41d
SS
1307 gdbarch_register_name because gdbarch_num_regs may be allocated
1308 for the union of the register sets within a family of related
1309 processors. In this case, some entries of gdbarch_register_name
1310 will change depending upon the particular processor being
1311 debugged. */
24e8cecf 1312
7ccb0be9
UW
1313 regcache = get_current_regcache ();
1314 gdbarch = get_regcache_arch (regcache);
1315 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
24e8cecf
EZ
1316
1317 if (argc == 0)
7ea6d463
PM
1318 error (_("-data-write-register-values: Usage: -data-write-register-"
1319 "values <format> [<regnum1> <value1>...<regnumN> <valueN>]"));
24e8cecf 1320
24e8cecf 1321 if (!target_has_registers)
7ea6d463 1322 error (_("-data-write-register-values: No registers."));
24e8cecf
EZ
1323
1324 if (!(argc - 1))
7ea6d463 1325 error (_("-data-write-register-values: No regs and values specified."));
24e8cecf
EZ
1326
1327 if ((argc - 1) % 2)
7ea6d463
PM
1328 error (_("-data-write-register-values: "
1329 "Regs and vals are not in pairs."));
24e8cecf
EZ
1330
1331 for (i = 1; i < argc; i = i + 2)
1332 {
9f3a1602 1333 int regnum = atoi (argv[i]);
24e8cecf 1334
9f3a1602 1335 if (regnum >= 0 && regnum < numregs
7ccb0be9
UW
1336 && gdbarch_register_name (gdbarch, regnum)
1337 && *gdbarch_register_name (gdbarch, regnum))
24e8cecf 1338 {
9f3a1602 1339 LONGEST value;
d8bf3afa 1340
9f3a1602 1341 /* Get the value as a number. */
24e8cecf 1342 value = parse_and_eval_address (argv[i + 1]);
9f3a1602 1343
41296c92 1344 /* Write it down. */
7ccb0be9 1345 regcache_cooked_write_signed (regcache, regnum, value);
24e8cecf
EZ
1346 }
1347 else
7ea6d463 1348 error (_("bad register number"));
24e8cecf 1349 }
24e8cecf
EZ
1350}
1351
41296c92 1352/* Evaluate the value of the argument. The argument is an
fb40c209 1353 expression. If the expression contains spaces it needs to be
41296c92 1354 included in double quotes. */
2b03b41d 1355
ce8f13f8 1356void
fb40c209
AC
1357mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
1358{
96052a95 1359 struct value *val;
79a45b7d 1360 struct value_print_options opts;
79a45e25 1361 struct ui_out *uiout = current_uiout;
fb40c209 1362
fb40c209 1363 if (argc != 1)
f99d8bf4
PA
1364 error (_("-data-evaluate-expression: "
1365 "Usage: -data-evaluate-expression expression"));
fb40c209 1366
4d01a485 1367 expression_up expr = parse_expression (argv[0]);
fb40c209 1368
4d01a485 1369 val = evaluate_expression (expr.get ());
fb40c209 1370
d7e74731
PA
1371 string_file stb;
1372
41296c92 1373 /* Print the result of the expression evaluation. */
79a45b7d
TT
1374 get_user_print_options (&opts);
1375 opts.deref_ref = 0;
d7e74731 1376 common_val_print (val, &stb, 0, &opts, current_language);
fb40c209 1377
112e8700 1378 uiout->field_stream ("value", stb);
fb40c209
AC
1379}
1380
2b03b41d 1381/* This is the -data-read-memory command.
fb40c209
AC
1382
1383 ADDR: start address of data to be dumped.
c1244769 1384 WORD-FORMAT: a char indicating format for the ``word''. See
fb40c209 1385 the ``x'' command.
41296c92 1386 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
fb40c209
AC
1387 NR_ROW: Number of rows.
1388 NR_COL: The number of colums (words per row).
1389 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
1390 ASCHAR for unprintable characters.
1391
1392 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
1393 displayes them. Returns:
1394
1395 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
1396
c1244769 1397 Returns:
2b03b41d 1398 The number of bytes read is SIZE*ROW*COL. */
fb40c209 1399
ce8f13f8 1400void
fb40c209
AC
1401mi_cmd_data_read_memory (char *command, char **argv, int argc)
1402{
e17c207e 1403 struct gdbarch *gdbarch = get_current_arch ();
79a45e25 1404 struct ui_out *uiout = current_uiout;
fb40c209 1405 CORE_ADDR addr;
2b03b41d 1406 long total_bytes, nr_cols, nr_rows;
fb40c209
AC
1407 char word_format;
1408 struct type *word_type;
1409 long word_size;
1410 char word_asize;
1411 char aschar;
fb40c209
AC
1412 int nr_bytes;
1413 long offset = 0;
56934ab1
AS
1414 int oind = 0;
1415 char *oarg;
fb40c209 1416 enum opt
fb40c209 1417 {
2b03b41d 1418 OFFSET_OPT
fb40c209 1419 };
2b03b41d
SS
1420 static const struct mi_opt opts[] =
1421 {
1422 {"o", OFFSET_OPT, 1},
1423 { 0, 0, 0 }
1424 };
fb40c209
AC
1425
1426 while (1)
1427 {
1b05df00 1428 int opt = mi_getopt ("-data-read-memory", argc, argv, opts,
56934ab1 1429 &oind, &oarg);
102040f0 1430
fb40c209
AC
1431 if (opt < 0)
1432 break;
1433 switch ((enum opt) opt)
1434 {
1435 case OFFSET_OPT:
56934ab1 1436 offset = atol (oarg);
fb40c209
AC
1437 break;
1438 }
1439 }
56934ab1
AS
1440 argv += oind;
1441 argc -= oind;
fb40c209
AC
1442
1443 if (argc < 5 || argc > 6)
7ea6d463
PM
1444 error (_("-data-read-memory: Usage: "
1445 "ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR]."));
fb40c209
AC
1446
1447 /* Extract all the arguments. */
1448
41296c92 1449 /* Start address of the memory dump. */
fb40c209 1450 addr = parse_and_eval_address (argv[0]) + offset;
41296c92 1451 /* The format character to use when displaying a memory word. See
2b03b41d 1452 the ``x'' command. */
fb40c209 1453 word_format = argv[1][0];
41296c92 1454 /* The size of the memory word. */
fb40c209
AC
1455 word_size = atol (argv[2]);
1456 switch (word_size)
1457 {
1458 case 1:
df4df182 1459 word_type = builtin_type (gdbarch)->builtin_int8;
fb40c209
AC
1460 word_asize = 'b';
1461 break;
1462 case 2:
df4df182 1463 word_type = builtin_type (gdbarch)->builtin_int16;
fb40c209
AC
1464 word_asize = 'h';
1465 break;
1466 case 4:
df4df182 1467 word_type = builtin_type (gdbarch)->builtin_int32;
fb40c209
AC
1468 word_asize = 'w';
1469 break;
1470 case 8:
df4df182 1471 word_type = builtin_type (gdbarch)->builtin_int64;
fb40c209
AC
1472 word_asize = 'g';
1473 break;
1474 default:
df4df182 1475 word_type = builtin_type (gdbarch)->builtin_int8;
fb40c209
AC
1476 word_asize = 'b';
1477 }
41296c92 1478 /* The number of rows. */
fb40c209
AC
1479 nr_rows = atol (argv[3]);
1480 if (nr_rows <= 0)
7ea6d463 1481 error (_("-data-read-memory: invalid number of rows."));
a13e061a 1482
41296c92 1483 /* Number of bytes per row. */
fb40c209
AC
1484 nr_cols = atol (argv[4]);
1485 if (nr_cols <= 0)
7ea6d463 1486 error (_("-data-read-memory: invalid number of columns."));
a13e061a 1487
41296c92 1488 /* The un-printable character when printing ascii. */
fb40c209
AC
1489 if (argc == 6)
1490 aschar = *argv[5];
1491 else
1492 aschar = 0;
1493
41296c92 1494 /* Create a buffer and read it in. */
fb40c209 1495 total_bytes = word_size * nr_rows * nr_cols;
6fc31fc7 1496
b22e99fd 1497 std::unique_ptr<gdb_byte[]> mbuf (new gdb_byte[total_bytes]);
cf7a04e8 1498
a4261689
PA
1499 /* Dispatch memory reads to the topmost target, not the flattened
1500 current_target. */
8dedea02 1501 nr_bytes = target_read (current_target.beneath,
6fc31fc7 1502 TARGET_OBJECT_MEMORY, NULL, mbuf.get (),
8dedea02 1503 addr, total_bytes);
cf7a04e8 1504 if (nr_bytes <= 0)
7ea6d463 1505 error (_("Unable to read memory."));
fb40c209 1506
41296c92 1507 /* Output the header information. */
112e8700
SM
1508 uiout->field_core_addr ("addr", gdbarch, addr);
1509 uiout->field_int ("nr-bytes", nr_bytes);
1510 uiout->field_int ("total-bytes", total_bytes);
1511 uiout->field_core_addr ("next-row", gdbarch, addr + word_size * nr_cols);
1512 uiout->field_core_addr ("prev-row", gdbarch, addr - word_size * nr_cols);
1513 uiout->field_core_addr ("next-page", gdbarch, addr + total_bytes);
1514 uiout->field_core_addr ("prev-page", gdbarch, addr - total_bytes);
fb40c209 1515
41296c92 1516 /* Build the result as a two dimentional table. */
fb40c209 1517 {
fb40c209
AC
1518 int row;
1519 int row_byte;
d7e74731 1520 struct cleanup *cleanup_list;
102040f0 1521
d7e74731 1522 string_file stream;
f99d8bf4 1523
d7e74731 1524 cleanup_list = make_cleanup_ui_out_list_begin_end (uiout, "memory");
fb40c209
AC
1525 for (row = 0, row_byte = 0;
1526 row < nr_rows;
1527 row++, row_byte += nr_cols * word_size)
1528 {
1529 int col;
1530 int col_byte;
6ad4a2cf
JJ
1531 struct cleanup *cleanup_tuple;
1532 struct cleanup *cleanup_list_data;
79a45b7d
TT
1533 struct value_print_options opts;
1534
6ad4a2cf 1535 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
112e8700 1536 uiout->field_core_addr ("addr", gdbarch, addr + row_byte);
9a2b4c1b
MS
1537 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr +
1538 row_byte); */
6ad4a2cf 1539 cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
79a45b7d 1540 get_formatted_print_options (&opts, word_format);
fb40c209
AC
1541 for (col = 0, col_byte = row_byte;
1542 col < nr_cols;
1543 col++, col_byte += word_size)
1544 {
1545 if (col_byte + word_size > nr_bytes)
1546 {
112e8700 1547 uiout->field_string (NULL, "N/A");
fb40c209
AC
1548 }
1549 else
1550 {
d7e74731 1551 stream.clear ();
6fc31fc7 1552 print_scalar_formatted (&mbuf[col_byte], word_type, &opts,
d7e74731 1553 word_asize, &stream);
112e8700 1554 uiout->field_stream (NULL, stream);
fb40c209
AC
1555 }
1556 }
6ad4a2cf 1557 do_cleanups (cleanup_list_data);
fb40c209
AC
1558 if (aschar)
1559 {
1560 int byte;
102040f0 1561
d7e74731 1562 stream.clear ();
9a2b4c1b
MS
1563 for (byte = row_byte;
1564 byte < row_byte + word_size * nr_cols; byte++)
fb40c209
AC
1565 {
1566 if (byte >= nr_bytes)
d7e74731 1567 stream.putc ('X');
fb40c209 1568 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
d7e74731 1569 stream.putc (aschar);
fb40c209 1570 else
d7e74731 1571 stream.putc (mbuf[byte]);
fb40c209 1572 }
112e8700 1573 uiout->field_stream ("ascii", stream);
fb40c209 1574 }
6ad4a2cf 1575 do_cleanups (cleanup_tuple);
fb40c209 1576 }
d7e74731 1577 do_cleanups (cleanup_list);
fb40c209 1578 }
fb40c209
AC
1579}
1580
8dedea02
VP
1581void
1582mi_cmd_data_read_memory_bytes (char *command, char **argv, int argc)
1583{
1584 struct gdbarch *gdbarch = get_current_arch ();
79a45e25 1585 struct ui_out *uiout = current_uiout;
8dedea02
VP
1586 struct cleanup *cleanups;
1587 CORE_ADDR addr;
1588 LONGEST length;
1589 memory_read_result_s *read_result;
1590 int ix;
1591 VEC(memory_read_result_s) *result;
1592 long offset = 0;
cfc32360 1593 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
56934ab1
AS
1594 int oind = 0;
1595 char *oarg;
8dedea02 1596 enum opt
8dedea02 1597 {
2b03b41d 1598 OFFSET_OPT
8dedea02 1599 };
2b03b41d
SS
1600 static const struct mi_opt opts[] =
1601 {
1602 {"o", OFFSET_OPT, 1},
1603 { 0, 0, 0 }
1604 };
8dedea02
VP
1605
1606 while (1)
1607 {
1b05df00 1608 int opt = mi_getopt ("-data-read-memory-bytes", argc, argv, opts,
56934ab1 1609 &oind, &oarg);
8dedea02
VP
1610 if (opt < 0)
1611 break;
1612 switch ((enum opt) opt)
1613 {
1614 case OFFSET_OPT:
56934ab1 1615 offset = atol (oarg);
8dedea02
VP
1616 break;
1617 }
1618 }
56934ab1
AS
1619 argv += oind;
1620 argc -= oind;
8dedea02
VP
1621
1622 if (argc != 2)
7ea6d463 1623 error (_("Usage: [ -o OFFSET ] ADDR LENGTH."));
8dedea02
VP
1624
1625 addr = parse_and_eval_address (argv[0]) + offset;
1626 length = atol (argv[1]);
1627
1628 result = read_memory_robust (current_target.beneath, addr, length);
1629
9d78f827 1630 cleanups = make_cleanup (free_memory_read_result_vector, &result);
8dedea02
VP
1631
1632 if (VEC_length (memory_read_result_s, result) == 0)
7ea6d463 1633 error (_("Unable to read memory."));
8dedea02
VP
1634
1635 make_cleanup_ui_out_list_begin_end (uiout, "memory");
1636 for (ix = 0;
1637 VEC_iterate (memory_read_result_s, result, ix, read_result);
1638 ++ix)
1639 {
1640 struct cleanup *t = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1641 char *data, *p;
1642 int i;
224c3ddb 1643 int alloc_len;
8dedea02 1644
112e8700
SM
1645 uiout->field_core_addr ("begin", gdbarch, read_result->begin);
1646 uiout->field_core_addr ("offset", gdbarch, read_result->begin - addr);
1647 uiout->field_core_addr ("end", gdbarch, read_result->end);
8dedea02 1648
224c3ddb
SM
1649 alloc_len = (read_result->end - read_result->begin) * 2 * unit_size + 1;
1650 data = (char *) xmalloc (alloc_len);
8dedea02
VP
1651
1652 for (i = 0, p = data;
cfc32360 1653 i < ((read_result->end - read_result->begin) * unit_size);
8dedea02
VP
1654 ++i, p += 2)
1655 {
1656 sprintf (p, "%02x", read_result->data[i]);
1657 }
112e8700 1658 uiout->field_string ("contents", data);
8dedea02
VP
1659 xfree (data);
1660 do_cleanups (t);
1661 }
1662 do_cleanups (cleanups);
1663}
1664
2b03b41d 1665/* Implementation of the -data-write_memory command.
fb40c209 1666
177b42fe 1667 COLUMN_OFFSET: optional argument. Must be preceded by '-o'. The
fb40c209
AC
1668 offset from the beginning of the memory grid row where the cell to
1669 be written is.
1670 ADDR: start address of the row in the memory grid where the memory
41296c92 1671 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
fb40c209 1672 the location to write to.
c1244769 1673 FORMAT: a char indicating format for the ``word''. See
fb40c209
AC
1674 the ``x'' command.
1675 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
1676 VALUE: value to be written into the memory address.
1677
1678 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1679
41296c92 1680 Prints nothing. */
2b03b41d 1681
ce8f13f8 1682void
fb40c209
AC
1683mi_cmd_data_write_memory (char *command, char **argv, int argc)
1684{
e17a4113
UW
1685 struct gdbarch *gdbarch = get_current_arch ();
1686 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
fb40c209 1687 CORE_ADDR addr;
fb40c209
AC
1688 long word_size;
1689 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
41296c92 1690 enough when using a compiler other than GCC. */
fb40c209 1691 LONGEST value;
7c543f7b 1692 gdb_byte *buffer;
d8bf3afa 1693 struct cleanup *old_chain;
fb40c209 1694 long offset = 0;
56934ab1
AS
1695 int oind = 0;
1696 char *oarg;
fb40c209 1697 enum opt
fb40c209 1698 {
2b03b41d 1699 OFFSET_OPT
fb40c209 1700 };
2b03b41d
SS
1701 static const struct mi_opt opts[] =
1702 {
1703 {"o", OFFSET_OPT, 1},
1704 { 0, 0, 0 }
1705 };
fb40c209
AC
1706
1707 while (1)
1708 {
1b05df00 1709 int opt = mi_getopt ("-data-write-memory", argc, argv, opts,
56934ab1 1710 &oind, &oarg);
102040f0 1711
fb40c209
AC
1712 if (opt < 0)
1713 break;
1714 switch ((enum opt) opt)
1715 {
1716 case OFFSET_OPT:
56934ab1 1717 offset = atol (oarg);
fb40c209
AC
1718 break;
1719 }
1720 }
56934ab1
AS
1721 argv += oind;
1722 argc -= oind;
fb40c209
AC
1723
1724 if (argc != 4)
7ea6d463
PM
1725 error (_("-data-write-memory: Usage: "
1726 "[-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE."));
fb40c209 1727
41296c92
NR
1728 /* Extract all the arguments. */
1729 /* Start address of the memory dump. */
fb40c209 1730 addr = parse_and_eval_address (argv[0]);
2b03b41d 1731 /* The size of the memory word. */
fb40c209
AC
1732 word_size = atol (argv[2]);
1733
41296c92 1734 /* Calculate the real address of the write destination. */
fb40c209
AC
1735 addr += (offset * word_size);
1736
41296c92 1737 /* Get the value as a number. */
fb40c209 1738 value = parse_and_eval_address (argv[3]);
41296c92 1739 /* Get the value into an array. */
7c543f7b 1740 buffer = (gdb_byte *) xmalloc (word_size);
d8bf3afa 1741 old_chain = make_cleanup (xfree, buffer);
e17a4113 1742 store_signed_integer (buffer, word_size, byte_order, value);
41296c92 1743 /* Write it down to memory. */
4c2786ba 1744 write_memory_with_notification (addr, buffer, word_size);
d8bf3afa
KB
1745 /* Free the buffer. */
1746 do_cleanups (old_chain);
fb40c209
AC
1747}
1748
2b03b41d 1749/* Implementation of the -data-write-memory-bytes command.
8dedea02
VP
1750
1751 ADDR: start address
62747a60
TT
1752 DATA: string of bytes to write at that address
1753 COUNT: number of bytes to be filled (decimal integer). */
2b03b41d 1754
8dedea02
VP
1755void
1756mi_cmd_data_write_memory_bytes (char *command, char **argv, int argc)
1757{
1758 CORE_ADDR addr;
1759 char *cdata;
1760 gdb_byte *data;
62747a60 1761 gdb_byte *databuf;
cfc32360
SM
1762 size_t len_hex, len_bytes, len_units, i, steps, remaining_units;
1763 long int count_units;
8dedea02 1764 struct cleanup *back_to;
cfc32360 1765 int unit_size;
8dedea02 1766
62747a60
TT
1767 if (argc != 2 && argc != 3)
1768 error (_("Usage: ADDR DATA [COUNT]."));
8dedea02
VP
1769
1770 addr = parse_and_eval_address (argv[0]);
1771 cdata = argv[1];
cfc32360
SM
1772 len_hex = strlen (cdata);
1773 unit_size = gdbarch_addressable_memory_unit_size (get_current_arch ());
1774
1775 if (len_hex % (unit_size * 2) != 0)
1776 error (_("Hex-encoded '%s' must represent an integral number of "
1777 "addressable memory units."),
1ae0c35e
YQ
1778 cdata);
1779
cfc32360
SM
1780 len_bytes = len_hex / 2;
1781 len_units = len_bytes / unit_size;
1782
62747a60 1783 if (argc == 3)
cfc32360 1784 count_units = strtoul (argv[2], NULL, 10);
62747a60 1785 else
cfc32360 1786 count_units = len_units;
8dedea02 1787
224c3ddb 1788 databuf = XNEWVEC (gdb_byte, len_bytes);
62747a60 1789 back_to = make_cleanup (xfree, databuf);
8dedea02 1790
cfc32360 1791 for (i = 0; i < len_bytes; ++i)
8dedea02
VP
1792 {
1793 int x;
62747a60
TT
1794 if (sscanf (cdata + i * 2, "%02x", &x) != 1)
1795 error (_("Invalid argument"));
1796 databuf[i] = (gdb_byte) x;
1797 }
1798
cfc32360 1799 if (len_units < count_units)
62747a60 1800 {
cfc32360 1801 /* Pattern is made of less units than count:
62747a60 1802 repeat pattern to fill memory. */
224c3ddb 1803 data = (gdb_byte *) xmalloc (count_units * unit_size);
62747a60 1804 make_cleanup (xfree, data);
c1244769 1805
cfc32360
SM
1806 /* Number of times the pattern is entirely repeated. */
1807 steps = count_units / len_units;
1808 /* Number of remaining addressable memory units. */
1809 remaining_units = count_units % len_units;
1810 for (i = 0; i < steps; i++)
1811 memcpy (data + i * len_bytes, databuf, len_bytes);
62747a60 1812
cfc32360
SM
1813 if (remaining_units > 0)
1814 memcpy (data + steps * len_bytes, databuf,
1815 remaining_units * unit_size);
62747a60 1816 }
c1244769 1817 else
62747a60 1818 {
c1244769 1819 /* Pattern is longer than or equal to count:
cfc32360 1820 just copy count addressable memory units. */
62747a60 1821 data = databuf;
8dedea02
VP
1822 }
1823
cfc32360 1824 write_memory_with_notification (addr, data, count_units);
8dedea02
VP
1825
1826 do_cleanups (back_to);
1827}
1828
ce8f13f8 1829void
d8c83789
NR
1830mi_cmd_enable_timings (char *command, char **argv, int argc)
1831{
1832 if (argc == 0)
1833 do_timings = 1;
1834 else if (argc == 1)
1835 {
1836 if (strcmp (argv[0], "yes") == 0)
1837 do_timings = 1;
1838 else if (strcmp (argv[0], "no") == 0)
1839 do_timings = 0;
1840 else
1841 goto usage_error;
1842 }
1843 else
1844 goto usage_error;
c1244769 1845
ce8f13f8 1846 return;
d8c83789
NR
1847
1848 usage_error:
7ea6d463 1849 error (_("-enable-timings: Usage: %s {yes|no}"), command);
d8c83789
NR
1850}
1851
ce8f13f8 1852void
084344da
VP
1853mi_cmd_list_features (char *command, char **argv, int argc)
1854{
1855 if (argc == 0)
1856 {
1857 struct cleanup *cleanup = NULL;
79a45e25 1858 struct ui_out *uiout = current_uiout;
084344da 1859
c1244769 1860 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
112e8700
SM
1861 uiout->field_string (NULL, "frozen-varobjs");
1862 uiout->field_string (NULL, "pending-breakpoints");
1863 uiout->field_string (NULL, "thread-info");
1864 uiout->field_string (NULL, "data-read-memory-bytes");
1865 uiout->field_string (NULL, "breakpoint-notifications");
1866 uiout->field_string (NULL, "ada-task-info");
1867 uiout->field_string (NULL, "language-option");
1868 uiout->field_string (NULL, "info-gdb-mi-command");
1869 uiout->field_string (NULL, "undefined-command-error-code");
1870 uiout->field_string (NULL, "exec-run-start-option");
c1244769 1871
6dddc817 1872 if (ext_lang_initialized_p (get_ext_lang_defn (EXT_LANG_PYTHON)))
112e8700 1873 uiout->field_string (NULL, "python");
c1244769 1874
084344da 1875 do_cleanups (cleanup);
ce8f13f8 1876 return;
084344da
VP
1877 }
1878
7ea6d463 1879 error (_("-list-features should be passed no arguments"));
084344da 1880}
c6ebd6cf
VP
1881
1882void
1883mi_cmd_list_target_features (char *command, char **argv, int argc)
1884{
1885 if (argc == 0)
1886 {
1887 struct cleanup *cleanup = NULL;
79a45e25 1888 struct ui_out *uiout = current_uiout;
c6ebd6cf 1889
c1244769 1890 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
329ea579 1891 if (mi_async_p ())
112e8700 1892 uiout->field_string (NULL, "async");
f75d858b 1893 if (target_can_execute_reverse)
112e8700 1894 uiout->field_string (NULL, "reverse");
c6ebd6cf
VP
1895 do_cleanups (cleanup);
1896 return;
1897 }
1898
7ea6d463 1899 error (_("-list-target-features should be passed no arguments"));
c6ebd6cf
VP
1900}
1901
a79b8f6e
VP
1902void
1903mi_cmd_add_inferior (char *command, char **argv, int argc)
1904{
1905 struct inferior *inf;
1906
1907 if (argc != 0)
1908 error (_("-add-inferior should be passed no arguments"));
1909
1910 inf = add_inferior_with_spaces ();
1911
112e8700 1912 current_uiout->field_fmt ("inferior", "i%d", inf->num);
a79b8f6e
VP
1913}
1914
2b03b41d
SS
1915/* Callback used to find the first inferior other than the current
1916 one. */
c1244769 1917
57bf2d7e
MK
1918static int
1919get_other_inferior (struct inferior *inf, void *arg)
1920{
1921 if (inf == current_inferior ())
1922 return 0;
1923
1924 return 1;
1925}
1926
a79b8f6e
VP
1927void
1928mi_cmd_remove_inferior (char *command, char **argv, int argc)
1929{
1930 int id;
1931 struct inferior *inf;
1932
1933 if (argc != 1)
7ea6d463 1934 error (_("-remove-inferior should be passed a single argument"));
a79b8f6e 1935
e2b4a699 1936 if (sscanf (argv[0], "i%d", &id) != 1)
7ea6d463 1937 error (_("the thread group id is syntactically invalid"));
a79b8f6e
VP
1938
1939 inf = find_inferior_id (id);
1940 if (!inf)
7ea6d463 1941 error (_("the specified thread group does not exist"));
a79b8f6e 1942
8fa067af 1943 if (inf->pid != 0)
81ec3cce 1944 error (_("cannot remove an active inferior"));
8fa067af 1945
57bf2d7e
MK
1946 if (inf == current_inferior ())
1947 {
1948 struct thread_info *tp = 0;
c1244769 1949 struct inferior *new_inferior
57bf2d7e
MK
1950 = iterate_over_inferiors (get_other_inferior, NULL);
1951
1952 if (new_inferior == NULL)
1953 error (_("Cannot remove last inferior"));
1954
1955 set_current_inferior (new_inferior);
1956 if (new_inferior->pid != 0)
1957 tp = any_thread_of_process (new_inferior->pid);
1958 switch_to_thread (tp ? tp->ptid : null_ptid);
1959 set_current_program_space (new_inferior->pspace);
1960 }
1961
7a41607e 1962 delete_inferior (inf);
a79b8f6e
VP
1963}
1964
1965\f
1966
8d34ea23
KS
1967/* Execute a command within a safe environment.
1968 Return <0 for error; >=0 for ok.
1969
1970 args->action will tell mi_execute_command what action
42972f50 1971 to perfrom after the given command has executed (display/suppress
2b03b41d 1972 prompt, display error). */
fb40c209 1973
f30f06b8 1974static void
04bd08de 1975captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
fb40c209 1976{
9204d692 1977 struct mi_interp *mi = (struct mi_interp *) interp_data (command_interp ());
1f31650a 1978 struct cleanup *cleanup;
fb40c209 1979
4333ada3
VP
1980 if (do_timings)
1981 current_command_ts = context->cmd_start;
d8c83789 1982
1f31650a
VP
1983 current_token = xstrdup (context->token);
1984 cleanup = make_cleanup (free_current_contents, &current_token);
1985
a2840c35 1986 running_result_record_printed = 0;
f3b1572e 1987 mi_proceeded = 0;
fb40c209
AC
1988 switch (context->op)
1989 {
fb40c209 1990 case MI_COMMAND:
41296c92 1991 /* A MI command was read from the input stream. */
fb40c209
AC
1992 if (mi_debug_p)
1993 /* FIXME: gdb_???? */
9204d692
PA
1994 fprintf_unfiltered (mi->raw_stdout,
1995 " token=`%s' command=`%s' args=`%s'\n",
fb40c209 1996 context->token, context->command, context->args);
d8c83789 1997
ce8f13f8 1998 mi_cmd_execute (context);
8d34ea23 1999
a2840c35 2000 /* Print the result if there were no errors.
4389a95a 2001
a2840c35 2002 Remember that on the way out of executing a command, you have
2b03b41d
SS
2003 to directly use the mi_interp's uiout, since the command
2004 could have reset the interpreter, in which case the current
2005 uiout will most likely crash in the mi_out_* routines. */
ce8f13f8 2006 if (!running_result_record_printed)
a2840c35 2007 {
9204d692 2008 fputs_unfiltered (context->token, mi->raw_stdout);
ce8f13f8
VP
2009 /* There's no particularly good reason why target-connect results
2010 in not ^done. Should kill ^connected for MI3. */
2011 fputs_unfiltered (strcmp (context->command, "target-select") == 0
9204d692
PA
2012 ? "^connected" : "^done", mi->raw_stdout);
2013 mi_out_put (uiout, mi->raw_stdout);
a2840c35 2014 mi_out_rewind (uiout);
9204d692
PA
2015 mi_print_timing_maybe (mi->raw_stdout);
2016 fputs_unfiltered ("\n", mi->raw_stdout);
a2840c35
VP
2017 }
2018 else
2b03b41d
SS
2019 /* The command does not want anything to be printed. In that
2020 case, the command probably should not have written anything
2021 to uiout, but in case it has written something, discard it. */
a2840c35 2022 mi_out_rewind (uiout);
fb40c209
AC
2023 break;
2024
2025 case CLI_COMMAND:
78f5381d
AC
2026 {
2027 char *argv[2];
102040f0 2028
78f5381d
AC
2029 /* A CLI command was read from the input stream. */
2030 /* This "feature" will be removed as soon as we have a
2031 complete set of mi commands. */
2032 /* Echo the command on the console. */
2033 fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
2034 /* Call the "console" interpreter. */
26cde2cc 2035 argv[0] = INTERP_CONSOLE;
78f5381d 2036 argv[1] = context->command;
ce8f13f8 2037 mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);
78f5381d 2038
eec01795 2039 /* If we changed interpreters, DON'T print out anything. */
78f5381d
AC
2040 if (current_interp_named_p (INTERP_MI)
2041 || current_interp_named_p (INTERP_MI1)
2042 || current_interp_named_p (INTERP_MI2)
2043 || current_interp_named_p (INTERP_MI3))
2044 {
ce8f13f8 2045 if (!running_result_record_printed)
eec01795 2046 {
9204d692
PA
2047 fputs_unfiltered (context->token, mi->raw_stdout);
2048 fputs_unfiltered ("^done", mi->raw_stdout);
2049 mi_out_put (uiout, mi->raw_stdout);
eec01795 2050 mi_out_rewind (uiout);
9204d692
PA
2051 mi_print_timing_maybe (mi->raw_stdout);
2052 fputs_unfiltered ("\n", mi->raw_stdout);
eec01795 2053 }
eec01795
DJ
2054 else
2055 mi_out_rewind (uiout);
78f5381d
AC
2056 }
2057 break;
2058 }
fb40c209 2059 }
8d34ea23 2060
1f31650a 2061 do_cleanups (cleanup);
fb40c209
AC
2062}
2063
305aeedc
TT
2064/* Print a gdb exception to the MI output stream. */
2065
2066static void
2067mi_print_exception (const char *token, struct gdb_exception exception)
2068{
9204d692
PA
2069 struct mi_interp *mi
2070 = (struct mi_interp *) interp_data (current_interpreter ());
2071
2072 fputs_unfiltered (token, mi->raw_stdout);
2073 fputs_unfiltered ("^error,msg=\"", mi->raw_stdout);
305aeedc 2074 if (exception.message == NULL)
9204d692 2075 fputs_unfiltered ("unknown error", mi->raw_stdout);
305aeedc 2076 else
9204d692
PA
2077 fputstr_unfiltered (exception.message, '"', mi->raw_stdout);
2078 fputs_unfiltered ("\"", mi->raw_stdout);
2ea126fa
JB
2079
2080 switch (exception.error)
2081 {
2082 case UNDEFINED_COMMAND_ERROR:
9204d692 2083 fputs_unfiltered (",code=\"undefined-command\"", mi->raw_stdout);
2ea126fa
JB
2084 break;
2085 }
2086
9204d692 2087 fputs_unfiltered ("\n", mi->raw_stdout);
305aeedc 2088}
fb40c209 2089
4034d0ff
AT
2090/* Determine whether the parsed command already notifies the
2091 user_selected_context_changed observer. */
2092
2093static int
2094command_notifies_uscc_observer (struct mi_parse *command)
2095{
2096 if (command->op == CLI_COMMAND)
2097 {
2098 /* CLI commands "thread" and "inferior" already send it. */
2099 return (strncmp (command->command, "thread ", 7) == 0
2100 || strncmp (command->command, "inferior ", 9) == 0);
2101 }
2102 else /* MI_COMMAND */
2103 {
2104 if (strcmp (command->command, "interpreter-exec") == 0
2105 && command->argc > 1)
2106 {
2107 /* "thread" and "inferior" again, but through -interpreter-exec. */
2108 return (strncmp (command->argv[1], "thread ", 7) == 0
2109 || strncmp (command->argv[1], "inferior ", 9) == 0);
2110 }
2111
2112 else
2113 /* -thread-select already sends it. */
2114 return strcmp (command->command, "thread-select") == 0;
2115 }
2116}
2117
fb40c209 2118void
ee047554 2119mi_execute_command (const char *cmd, int from_tty)
fb40c209 2120{
305aeedc
TT
2121 char *token;
2122 struct mi_parse *command = NULL;
fb40c209 2123
41296c92
NR
2124 /* This is to handle EOF (^D). We just quit gdb. */
2125 /* FIXME: we should call some API function here. */
fb40c209
AC
2126 if (cmd == 0)
2127 quit_force (NULL, from_tty);
2128
11334b82
VP
2129 target_log_command (cmd);
2130
492d29ea 2131 TRY
305aeedc
TT
2132 {
2133 command = mi_parse (cmd, &token);
2134 }
492d29ea 2135 CATCH (exception, RETURN_MASK_ALL)
305aeedc
TT
2136 {
2137 mi_print_exception (token, exception);
2138 xfree (token);
2139 }
492d29ea
PA
2140 END_CATCH
2141
2142 if (command != NULL)
fb40c209 2143 {
66bb093b 2144 ptid_t previous_ptid = inferior_ptid;
4034d0ff 2145 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
d8c83789 2146
305aeedc
TT
2147 command->token = token;
2148
4034d0ff
AT
2149 if (command->cmd != NULL && command->cmd->suppress_notification != NULL)
2150 {
2151 make_cleanup_restore_integer (command->cmd->suppress_notification);
2152 *command->cmd->suppress_notification = 1;
2153 }
2154
d8c83789
NR
2155 if (do_timings)
2156 {
dcb07cfa 2157 command->cmd_start = new mi_timestamp ();
d8c83789
NR
2158 timestamp (command->cmd_start);
2159 }
2160
492d29ea 2161 TRY
04bd08de 2162 {
79a45e25 2163 captured_mi_execute_command (current_uiout, command);
04bd08de 2164 }
492d29ea 2165 CATCH (result, RETURN_MASK_ALL)
fb40c209 2166 {
80614914
PA
2167 /* Like in start_event_loop, enable input and force display
2168 of the prompt. Otherwise, any command that calls
2169 async_disable_stdin, and then throws, will leave input
2170 disabled. */
2171 async_enable_stdin ();
2172 current_ui->prompt_state = PROMPT_NEEDED;
2173
fb40c209 2174 /* The command execution failed and error() was called
589e074d 2175 somewhere. */
305aeedc 2176 mi_print_exception (command->token, result);
79a45e25 2177 mi_out_rewind (current_uiout);
fb40c209 2178 }
492d29ea 2179 END_CATCH
a13e061a 2180
5d4e2b76
VP
2181 bpstat_do_actions ();
2182
66bb093b 2183 if (/* The notifications are only output when the top-level
c1244769 2184 interpreter (specified on the command line) is MI. */
112e8700 2185 interp_ui_out (top_level_interpreter ())->is_mi_like_p ()
c1244769 2186 /* Don't try report anything if there are no threads --
66bb093b
VP
2187 the program is dead. */
2188 && thread_count () != 0
4034d0ff
AT
2189 /* If the command already reports the thread change, no need to do it
2190 again. */
2191 && !command_notifies_uscc_observer (command))
66bb093b 2192 {
19ba03f4
SM
2193 struct mi_interp *mi
2194 = (struct mi_interp *) top_level_interpreter_data ();
d729566a 2195 int report_change = 0;
66bb093b
VP
2196
2197 if (command->thread == -1)
2198 {
d729566a
PA
2199 report_change = (!ptid_equal (previous_ptid, null_ptid)
2200 && !ptid_equal (inferior_ptid, previous_ptid)
2201 && !ptid_equal (inferior_ptid, null_ptid));
66bb093b 2202 }
d729566a 2203 else if (!ptid_equal (inferior_ptid, null_ptid))
66bb093b 2204 {
d729566a 2205 struct thread_info *ti = inferior_thread ();
102040f0 2206
5d5658a1 2207 report_change = (ti->global_num != command->thread);
66bb093b
VP
2208 }
2209
2210 if (report_change)
c1244769 2211 {
4034d0ff
AT
2212 observer_notify_user_selected_context_changed
2213 (USER_SELECTED_THREAD | USER_SELECTED_FRAME);
66bb093b
VP
2214 }
2215 }
2216
fb40c209 2217 mi_parse_free (command);
4034d0ff
AT
2218
2219 do_cleanups (cleanup);
fb40c209 2220 }
fb40c209
AC
2221}
2222
ce8f13f8 2223static void
fb40c209
AC
2224mi_cmd_execute (struct mi_parse *parse)
2225{
f107f563 2226 struct cleanup *cleanup;
e23110bb 2227
028d0ed5 2228 cleanup = prepare_execute_command ();
1b98914a 2229
a79b8f6e
VP
2230 if (parse->all && parse->thread_group != -1)
2231 error (_("Cannot specify --thread-group together with --all"));
2232
2233 if (parse->all && parse->thread != -1)
2234 error (_("Cannot specify --thread together with --all"));
2235
2236 if (parse->thread_group != -1 && parse->thread != -1)
2237 error (_("Cannot specify --thread together with --thread-group"));
2238
1e92afda
VP
2239 if (parse->frame != -1 && parse->thread == -1)
2240 error (_("Cannot specify --frame without --thread"));
dcf4fbde 2241
a79b8f6e
VP
2242 if (parse->thread_group != -1)
2243 {
2244 struct inferior *inf = find_inferior_id (parse->thread_group);
2245 struct thread_info *tp = 0;
2246
2247 if (!inf)
46ef47e5 2248 error (_("Invalid thread group for the --thread-group option"));
a79b8f6e
VP
2249
2250 set_current_inferior (inf);
2251 /* This behaviour means that if --thread-group option identifies
2b03b41d
SS
2252 an inferior with multiple threads, then a random one will be
2253 picked. This is not a problem -- frontend should always
2254 provide --thread if it wishes to operate on a specific
2255 thread. */
a79b8f6e 2256 if (inf->pid != 0)
4734f50e 2257 tp = any_live_thread_of_process (inf->pid);
a79b8f6e
VP
2258 switch_to_thread (tp ? tp->ptid : null_ptid);
2259 set_current_program_space (inf->pspace);
2260 }
2261
1e92afda
VP
2262 if (parse->thread != -1)
2263 {
5d5658a1 2264 struct thread_info *tp = find_thread_global_id (parse->thread);
102040f0 2265
1e92afda
VP
2266 if (!tp)
2267 error (_("Invalid thread id: %d"), parse->thread);
dcf4fbde
PA
2268
2269 if (is_exited (tp->ptid))
2270 error (_("Thread id: %d has terminated"), parse->thread);
2271
2272 switch_to_thread (tp->ptid);
1e92afda 2273 }
dcf4fbde 2274
1e92afda
VP
2275 if (parse->frame != -1)
2276 {
2277 struct frame_info *fid;
2278 int frame = parse->frame;
102040f0 2279
1e92afda
VP
2280 fid = find_relative_frame (get_current_frame (), &frame);
2281 if (frame == 0)
2282 /* find_relative_frame was successful */
2283 select_frame (fid);
2284 else
ea069267 2285 error (_("Invalid frame id: %d"), frame);
1e92afda 2286 }
dcf4fbde 2287
403cb6b1
JB
2288 if (parse->language != language_unknown)
2289 {
2290 make_cleanup_restore_current_language ();
2291 set_language (parse->language);
2292 }
2293
a79b8f6e
VP
2294 current_context = parse;
2295
9e22b03a 2296 if (parse->cmd->argv_func != NULL)
8d3788bd
VP
2297 {
2298 parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
2299 }
b2af646b 2300 else if (parse->cmd->cli.cmd != 0)
fb40c209
AC
2301 {
2302 /* FIXME: DELETE THIS. */
41296c92
NR
2303 /* The operation is still implemented by a cli command. */
2304 /* Must be a synchronous one. */
b2af646b
AC
2305 mi_execute_cli_command (parse->cmd->cli.cmd, parse->cmd->cli.args_p,
2306 parse->args);
fb40c209
AC
2307 }
2308 else
2309 {
41296c92 2310 /* FIXME: DELETE THIS. */
d7e74731 2311 string_file stb;
a13e061a 2312
d7e74731
PA
2313 stb.puts ("Undefined mi command: ");
2314 stb.putstr (parse->command, '"');
2315 stb.puts (" (missing implementation)");
a13e061a 2316
a13e061a 2317 error_stream (stb);
fb40c209 2318 }
1b98914a 2319 do_cleanups (cleanup);
fb40c209
AC
2320}
2321
fb40c209 2322/* FIXME: This is just a hack so we can get some extra commands going.
41296c92
NR
2323 We don't want to channel things through the CLI, but call libgdb directly.
2324 Use only for synchronous commands. */
fb40c209
AC
2325
2326void
b2af646b 2327mi_execute_cli_command (const char *cmd, int args_p, const char *args)
fb40c209 2328{
b2af646b 2329 if (cmd != 0)
fb40c209
AC
2330 {
2331 struct cleanup *old_cleanups;
2332 char *run;
102040f0 2333
b2af646b 2334 if (args_p)
c6902d46 2335 run = xstrprintf ("%s %s", cmd, args);
b2af646b
AC
2336 else
2337 run = xstrdup (cmd);
fb40c209
AC
2338 if (mi_debug_p)
2339 /* FIXME: gdb_???? */
2340 fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
b2af646b 2341 cmd, run);
b8c9b27d 2342 old_cleanups = make_cleanup (xfree, run);
2b03b41d 2343 execute_command (run, 0 /* from_tty */ );
fb40c209
AC
2344 do_cleanups (old_cleanups);
2345 return;
2346 }
2347}
2348
ce8f13f8 2349void
9e22b03a 2350mi_execute_async_cli_command (char *cli_command, char **argv, int argc)
fb40c209
AC
2351{
2352 struct cleanup *old_cleanups;
2353 char *run;
fb40c209 2354
329ea579 2355 if (mi_async_p ())
9e22b03a 2356 run = xstrprintf ("%s %s&", cli_command, argc ? *argv : "");
fb40c209 2357 else
9e22b03a 2358 run = xstrprintf ("%s %s", cli_command, argc ? *argv : "");
c1244769 2359 old_cleanups = make_cleanup (xfree, run);
fb40c209 2360
2b03b41d 2361 execute_command (run, 0 /* from_tty */ );
fb40c209 2362
09cee04b
PA
2363 /* Do this before doing any printing. It would appear that some
2364 print code leaves garbage around in the buffer. */
2365 do_cleanups (old_cleanups);
fb40c209
AC
2366}
2367
2368void
fb40c209
AC
2369mi_load_progress (const char *section_name,
2370 unsigned long sent_so_far,
2371 unsigned long total_section,
2372 unsigned long total_sent,
2373 unsigned long grand_total)
2374{
dcb07cfa
PA
2375 using namespace std::chrono;
2376 static steady_clock::time_point last_update;
fb40c209
AC
2377 static char *previous_sect_name = NULL;
2378 int new_section;
0be75e02 2379 struct ui_out *saved_uiout;
79a45e25 2380 struct ui_out *uiout;
9204d692
PA
2381 struct mi_interp *mi
2382 = (struct mi_interp *) interp_data (current_interpreter ());
fb40c209 2383
0be75e02
AS
2384 /* This function is called through deprecated_show_load_progress
2385 which means uiout may not be correct. Fix it for the duration
2386 of this function. */
79a45e25 2387 saved_uiout = current_uiout;
0be75e02 2388
edff0c0a
DJ
2389 if (current_interp_named_p (INTERP_MI)
2390 || current_interp_named_p (INTERP_MI2))
79a45e25 2391 current_uiout = mi_out_new (2);
0be75e02 2392 else if (current_interp_named_p (INTERP_MI1))
79a45e25 2393 current_uiout = mi_out_new (1);
edff0c0a 2394 else if (current_interp_named_p (INTERP_MI3))
79a45e25 2395 current_uiout = mi_out_new (3);
0be75e02 2396 else
fb40c209
AC
2397 return;
2398
79a45e25
PA
2399 uiout = current_uiout;
2400
fb40c209
AC
2401 new_section = (previous_sect_name ?
2402 strcmp (previous_sect_name, section_name) : 1);
2403 if (new_section)
2404 {
6ad4a2cf 2405 struct cleanup *cleanup_tuple;
102040f0 2406
b8c9b27d 2407 xfree (previous_sect_name);
fb40c209
AC
2408 previous_sect_name = xstrdup (section_name);
2409
721c02de 2410 if (current_token)
9204d692
PA
2411 fputs_unfiltered (current_token, mi->raw_stdout);
2412 fputs_unfiltered ("+download", mi->raw_stdout);
6ad4a2cf 2413 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
112e8700
SM
2414 uiout->field_string ("section", section_name);
2415 uiout->field_int ("section-size", total_section);
2416 uiout->field_int ("total-size", grand_total);
6ad4a2cf 2417 do_cleanups (cleanup_tuple);
9204d692
PA
2418 mi_out_put (uiout, mi->raw_stdout);
2419 fputs_unfiltered ("\n", mi->raw_stdout);
2420 gdb_flush (mi->raw_stdout);
fb40c209
AC
2421 }
2422
dcb07cfa
PA
2423 steady_clock::time_point time_now = steady_clock::now ();
2424 if (time_now - last_update > milliseconds (500))
fb40c209 2425 {
6ad4a2cf 2426 struct cleanup *cleanup_tuple;
102040f0 2427
dcb07cfa 2428 last_update = time_now;
721c02de 2429 if (current_token)
9204d692
PA
2430 fputs_unfiltered (current_token, mi->raw_stdout);
2431 fputs_unfiltered ("+download", mi->raw_stdout);
6ad4a2cf 2432 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
112e8700
SM
2433 uiout->field_string ("section", section_name);
2434 uiout->field_int ("section-sent", sent_so_far);
2435 uiout->field_int ("section-size", total_section);
2436 uiout->field_int ("total-sent", total_sent);
2437 uiout->field_int ("total-size", grand_total);
6ad4a2cf 2438 do_cleanups (cleanup_tuple);
9204d692
PA
2439 mi_out_put (uiout, mi->raw_stdout);
2440 fputs_unfiltered ("\n", mi->raw_stdout);
2441 gdb_flush (mi->raw_stdout);
fb40c209 2442 }
0be75e02
AS
2443
2444 xfree (uiout);
67ba4e42 2445 current_uiout = saved_uiout;
fb40c209
AC
2446}
2447
c1244769 2448static void
d8c83789 2449timestamp (struct mi_timestamp *tv)
2b03b41d 2450{
dcb07cfa 2451 using namespace std::chrono;
a1b7d198 2452
dcb07cfa
PA
2453 tv->wallclock = steady_clock::now ();
2454 run_time_clock::now (tv->utime, tv->stime);
2b03b41d 2455}
d8c83789 2456
c1244769 2457static void
9204d692 2458print_diff_now (struct ui_file *file, struct mi_timestamp *start)
2b03b41d
SS
2459{
2460 struct mi_timestamp now;
102040f0 2461
2b03b41d 2462 timestamp (&now);
9204d692 2463 print_diff (file, start, &now);
2b03b41d 2464}
d8c83789 2465
4333ada3 2466void
9204d692 2467mi_print_timing_maybe (struct ui_file *file)
4333ada3 2468{
2b03b41d
SS
2469 /* If the command is -enable-timing then do_timings may be true
2470 whilst current_command_ts is not initialized. */
4333ada3 2471 if (do_timings && current_command_ts)
9204d692 2472 print_diff_now (file, current_command_ts);
4333ada3
VP
2473}
2474
c1244769 2475static void
9204d692
PA
2476print_diff (struct ui_file *file, struct mi_timestamp *start,
2477 struct mi_timestamp *end)
2b03b41d 2478{
dcb07cfa
PA
2479 using namespace std::chrono;
2480
2481 duration<double> wallclock = end->wallclock - start->wallclock;
2482 duration<double> utime = end->utime - start->utime;
2483 duration<double> stime = end->stime - start->stime;
2484
2b03b41d 2485 fprintf_unfiltered
9204d692 2486 (file,
c1244769 2487 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
dcb07cfa 2488 wallclock.count (), utime.count (), stime.count ());
2b03b41d 2489}
f224b49d 2490
40e1c229
VP
2491void
2492mi_cmd_trace_define_variable (char *command, char **argv, int argc)
2493{
40e1c229
VP
2494 LONGEST initval = 0;
2495 struct trace_state_variable *tsv;
2496 char *name = 0;
2497
2498 if (argc != 1 && argc != 2)
2499 error (_("Usage: -trace-define-variable VARIABLE [VALUE]"));
2500
1773c82c
HAQ
2501 name = argv[0];
2502 if (*name++ != '$')
2503 error (_("Name of trace variable should start with '$'"));
40e1c229 2504
1773c82c 2505 validate_trace_state_variable_name (name);
40e1c229
VP
2506
2507 tsv = find_trace_state_variable (name);
2508 if (!tsv)
2509 tsv = create_trace_state_variable (name);
2510
2511 if (argc == 2)
2512 initval = value_as_long (parse_and_eval (argv[1]));
2513
2514 tsv->initial_value = initval;
40e1c229
VP
2515}
2516
2517void
2518mi_cmd_trace_list_variables (char *command, char **argv, int argc)
2519{
2520 if (argc != 0)
2b03b41d 2521 error (_("-trace-list-variables: no arguments allowed"));
40e1c229
VP
2522
2523 tvariables_info_1 ();
2524}
2525
f197e0f1
VP
2526void
2527mi_cmd_trace_find (char *command, char **argv, int argc)
2528{
2529 char *mode;
2530
2531 if (argc == 0)
2532 error (_("trace selection mode is required"));
2533
2534 mode = argv[0];
2535
2536 if (strcmp (mode, "none") == 0)
2537 {
2538 tfind_1 (tfind_number, -1, 0, 0, 0);
2539 return;
2540 }
2541
cc3da688 2542 check_trace_running (current_trace_status ());
f197e0f1
VP
2543
2544 if (strcmp (mode, "frame-number") == 0)
2545 {
2546 if (argc != 2)
2547 error (_("frame number is required"));
2548 tfind_1 (tfind_number, atoi (argv[1]), 0, 0, 0);
2549 }
2550 else if (strcmp (mode, "tracepoint-number") == 0)
2551 {
2552 if (argc != 2)
2553 error (_("tracepoint number is required"));
2554 tfind_1 (tfind_tp, atoi (argv[1]), 0, 0, 0);
2555 }
2556 else if (strcmp (mode, "pc") == 0)
2557 {
2558 if (argc != 2)
2559 error (_("PC is required"));
2560 tfind_1 (tfind_pc, 0, parse_and_eval_address (argv[1]), 0, 0);
2561 }
2562 else if (strcmp (mode, "pc-inside-range") == 0)
2563 {
2564 if (argc != 3)
2565 error (_("Start and end PC are required"));
2566 tfind_1 (tfind_range, 0, parse_and_eval_address (argv[1]),
2567 parse_and_eval_address (argv[2]), 0);
2568 }
2569 else if (strcmp (mode, "pc-outside-range") == 0)
2570 {
2571 if (argc != 3)
2572 error (_("Start and end PC are required"));
2573 tfind_1 (tfind_outside, 0, parse_and_eval_address (argv[1]),
2574 parse_and_eval_address (argv[2]), 0);
2575 }
2576 else if (strcmp (mode, "line") == 0)
2577 {
2578 struct symtabs_and_lines sals;
2579 struct symtab_and_line sal;
2580 static CORE_ADDR start_pc, end_pc;
2581 struct cleanup *back_to;
2582
2583 if (argc != 2)
2584 error (_("Line is required"));
2585
39cf75f7
DE
2586 sals = decode_line_with_current_source (argv[1],
2587 DECODE_LINE_FUNFIRSTLINE);
f197e0f1
VP
2588 back_to = make_cleanup (xfree, sals.sals);
2589
2590 sal = sals.sals[0];
2591
2592 if (sal.symtab == 0)
2593 error (_("Could not find the specified line"));
2594
2595 if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
2596 tfind_1 (tfind_range, 0, start_pc, end_pc - 1, 0);
2597 else
2598 error (_("Could not find the specified line"));
2599
2600 do_cleanups (back_to);
2601 }
2602 else
2603 error (_("Invalid mode '%s'"), mode);
2604
2605 if (has_stack_frames () || get_traceframe_number () >= 0)
08d72866 2606 print_stack_frame (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 1);
f197e0f1
VP
2607}
2608
011aacb0
VP
2609void
2610mi_cmd_trace_save (char *command, char **argv, int argc)
2611{
2612 int target_saves = 0;
d0353e76 2613 int generate_ctf = 0;
011aacb0 2614 char *filename;
d0353e76
YQ
2615 int oind = 0;
2616 char *oarg;
011aacb0 2617
d0353e76
YQ
2618 enum opt
2619 {
2620 TARGET_SAVE_OPT, CTF_OPT
2621 };
2622 static const struct mi_opt opts[] =
011aacb0 2623 {
d0353e76
YQ
2624 {"r", TARGET_SAVE_OPT, 0},
2625 {"ctf", CTF_OPT, 0},
2626 { 0, 0, 0 }
2627 };
2628
2629 while (1)
011aacb0 2630 {
d0353e76
YQ
2631 int opt = mi_getopt ("-trace-save", argc, argv, opts,
2632 &oind, &oarg);
2633
2634 if (opt < 0)
2635 break;
2636 switch ((enum opt) opt)
2637 {
2638 case TARGET_SAVE_OPT:
2639 target_saves = 1;
2640 break;
2641 case CTF_OPT:
2642 generate_ctf = 1;
2643 break;
2644 }
011aacb0 2645 }
5bad3170
SM
2646
2647 if (argc - oind != 1)
2648 error (_("Exactly one argument required "
2649 "(file in which to save trace data)"));
2650
d0353e76 2651 filename = argv[oind];
011aacb0 2652
d0353e76
YQ
2653 if (generate_ctf)
2654 trace_save_ctf (filename, target_saves);
2655 else
2656 trace_save_tfile (filename, target_saves);
011aacb0
VP
2657}
2658
f224b49d
VP
2659void
2660mi_cmd_trace_start (char *command, char **argv, int argc)
2661{
f196051f 2662 start_tracing (NULL);
f224b49d
VP
2663}
2664
2665void
2666mi_cmd_trace_status (char *command, char **argv, int argc)
2667{
2668 trace_status_mi (0);
2669}
2670
2671void
2672mi_cmd_trace_stop (char *command, char **argv, int argc)
2673{
f196051f 2674 stop_tracing (NULL);
f224b49d
VP
2675 trace_status_mi (1);
2676}
75082e8c 2677
2b03b41d 2678/* Implement the "-ada-task-info" command. */
75082e8c
JB
2679
2680void
2681mi_cmd_ada_task_info (char *command, char **argv, int argc)
2682{
2683 if (argc != 0 && argc != 1)
2684 error (_("Invalid MI command"));
2685
2686 print_ada_task_info (current_uiout, argv[0], current_inferior ());
2687}
dc673c81
YQ
2688
2689/* Print EXPRESSION according to VALUES. */
2690
2691static void
1f45808e 2692print_variable_or_computed (const char *expression, enum print_values values)
dc673c81 2693{
dc673c81
YQ
2694 struct cleanup *old_chain;
2695 struct value *val;
dc673c81
YQ
2696 struct type *type;
2697 struct ui_out *uiout = current_uiout;
2698
d7e74731 2699 string_file stb;
dc673c81 2700
4d01a485 2701 expression_up expr = parse_expression (expression);
dc673c81
YQ
2702
2703 if (values == PRINT_SIMPLE_VALUES)
4d01a485 2704 val = evaluate_type (expr.get ());
dc673c81 2705 else
4d01a485 2706 val = evaluate_expression (expr.get ());
dc673c81 2707
d7e74731 2708 old_chain = make_cleanup (null_cleanup, NULL);
dc673c81
YQ
2709 if (values != PRINT_NO_VALUES)
2710 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
112e8700 2711 uiout->field_string ("name", expression);
dc673c81
YQ
2712
2713 switch (values)
2714 {
2715 case PRINT_SIMPLE_VALUES:
2716 type = check_typedef (value_type (val));
d7e74731 2717 type_print (value_type (val), "", &stb, -1);
112e8700 2718 uiout->field_stream ("type", stb);
dc673c81
YQ
2719 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2720 && TYPE_CODE (type) != TYPE_CODE_STRUCT
2721 && TYPE_CODE (type) != TYPE_CODE_UNION)
2722 {
2723 struct value_print_options opts;
2724
2a998fc0 2725 get_no_prettyformat_print_options (&opts);
dc673c81 2726 opts.deref_ref = 1;
d7e74731 2727 common_val_print (val, &stb, 0, &opts, current_language);
112e8700 2728 uiout->field_stream ("value", stb);
dc673c81
YQ
2729 }
2730 break;
2731 case PRINT_ALL_VALUES:
2732 {
2733 struct value_print_options opts;
2734
2a998fc0 2735 get_no_prettyformat_print_options (&opts);
dc673c81 2736 opts.deref_ref = 1;
d7e74731 2737 common_val_print (val, &stb, 0, &opts, current_language);
112e8700 2738 uiout->field_stream ("value", stb);
dc673c81
YQ
2739 }
2740 break;
2741 }
2742
2743 do_cleanups (old_chain);
2744}
2745
2746/* Implement the "-trace-frame-collected" command. */
2747
2748void
2749mi_cmd_trace_frame_collected (char *command, char **argv, int argc)
2750{
2751 struct cleanup *old_chain;
2752 struct bp_location *tloc;
2753 int stepping_frame;
2754 struct collection_list *clist;
2755 struct collection_list tracepoint_list, stepping_list;
2756 struct traceframe_info *tinfo;
2757 int oind = 0;
f486487f
SM
2758 enum print_values var_print_values = PRINT_ALL_VALUES;
2759 enum print_values comp_print_values = PRINT_ALL_VALUES;
dc673c81
YQ
2760 int registers_format = 'x';
2761 int memory_contents = 0;
2762 struct ui_out *uiout = current_uiout;
2763 enum opt
2764 {
2765 VAR_PRINT_VALUES,
2766 COMP_PRINT_VALUES,
2767 REGISTERS_FORMAT,
2768 MEMORY_CONTENTS,
2769 };
2770 static const struct mi_opt opts[] =
2771 {
2772 {"-var-print-values", VAR_PRINT_VALUES, 1},
2773 {"-comp-print-values", COMP_PRINT_VALUES, 1},
2774 {"-registers-format", REGISTERS_FORMAT, 1},
2775 {"-memory-contents", MEMORY_CONTENTS, 0},
2776 { 0, 0, 0 }
2777 };
2778
2779 while (1)
2780 {
2781 char *oarg;
2782 int opt = mi_getopt ("-trace-frame-collected", argc, argv, opts,
2783 &oind, &oarg);
2784 if (opt < 0)
2785 break;
2786 switch ((enum opt) opt)
2787 {
2788 case VAR_PRINT_VALUES:
2789 var_print_values = mi_parse_print_values (oarg);
2790 break;
2791 case COMP_PRINT_VALUES:
2792 comp_print_values = mi_parse_print_values (oarg);
2793 break;
2794 case REGISTERS_FORMAT:
2795 registers_format = oarg[0];
2796 case MEMORY_CONTENTS:
2797 memory_contents = 1;
2798 break;
2799 }
2800 }
2801
2802 if (oind != argc)
2803 error (_("Usage: -trace-frame-collected "
2804 "[--var-print-values PRINT_VALUES] "
2805 "[--comp-print-values PRINT_VALUES] "
2806 "[--registers-format FORMAT]"
2807 "[--memory-contents]"));
2808
2809 /* This throws an error is not inspecting a trace frame. */
2810 tloc = get_traceframe_location (&stepping_frame);
2811
2812 /* This command only makes sense for the current frame, not the
2813 selected frame. */
2814 old_chain = make_cleanup_restore_current_thread ();
2815 select_frame (get_current_frame ());
2816
1f45808e 2817 encode_actions (tloc, &tracepoint_list, &stepping_list);
dc673c81
YQ
2818
2819 if (stepping_frame)
2820 clist = &stepping_list;
2821 else
2822 clist = &tracepoint_list;
2823
2824 tinfo = get_traceframe_info ();
2825
2826 /* Explicitly wholly collected variables. */
2827 {
2828 struct cleanup *list_cleanup;
dc673c81
YQ
2829 int i;
2830
2831 list_cleanup = make_cleanup_ui_out_list_begin_end (uiout,
2832 "explicit-variables");
1f45808e
PA
2833
2834 const std::vector<std::string> &wholly_collected
2835 = clist->wholly_collected ();
2836 for (size_t i = 0; i < wholly_collected.size (); i++)
2837 {
2838 const std::string &str = wholly_collected[i];
2839 print_variable_or_computed (str.c_str (), var_print_values);
2840 }
2841
dc673c81
YQ
2842 do_cleanups (list_cleanup);
2843 }
2844
2845 /* Computed expressions. */
2846 {
2847 struct cleanup *list_cleanup;
2848 char *p;
2849 int i;
2850
2851 list_cleanup
2852 = make_cleanup_ui_out_list_begin_end (uiout,
2853 "computed-expressions");
1f45808e
PA
2854
2855 const std::vector<std::string> &computed = clist->computed ();
2856 for (size_t i = 0; i < computed.size (); i++)
2857 {
2858 const std::string &str = computed[i];
2859 print_variable_or_computed (str.c_str (), comp_print_values);
2860 }
2861
dc673c81
YQ
2862 do_cleanups (list_cleanup);
2863 }
2864
2865 /* Registers. Given pseudo-registers, and that some architectures
2866 (like MIPS) actually hide the raw registers, we don't go through
2867 the trace frame info, but instead consult the register cache for
2868 register availability. */
2869 {
2870 struct cleanup *list_cleanup;
2871 struct frame_info *frame;
2872 struct gdbarch *gdbarch;
2873 int regnum;
2874 int numregs;
2875
2876 list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "registers");
2877
2878 frame = get_selected_frame (NULL);
2879 gdbarch = get_frame_arch (frame);
2880 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
2881
2882 for (regnum = 0; regnum < numregs; regnum++)
2883 {
2884 if (gdbarch_register_name (gdbarch, regnum) == NULL
2885 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
2886 continue;
2887
2888 output_register (frame, regnum, registers_format, 1);
2889 }
2890
2891 do_cleanups (list_cleanup);
2892 }
2893
2894 /* Trace state variables. */
2895 {
2896 struct cleanup *list_cleanup;
2897 int tvar;
2898 char *tsvname;
2899 int i;
2900
2901 list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "tvars");
2902
2903 tsvname = NULL;
2904 make_cleanup (free_current_contents, &tsvname);
2905
2906 for (i = 0; VEC_iterate (int, tinfo->tvars, i, tvar); i++)
2907 {
2908 struct cleanup *cleanup_child;
2909 struct trace_state_variable *tsv;
2910
2911 tsv = find_trace_state_variable_by_number (tvar);
2912
2913 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
2914
2915 if (tsv != NULL)
2916 {
224c3ddb 2917 tsvname = (char *) xrealloc (tsvname, strlen (tsv->name) + 2);
dc673c81
YQ
2918 tsvname[0] = '$';
2919 strcpy (tsvname + 1, tsv->name);
112e8700 2920 uiout->field_string ("name", tsvname);
dc673c81
YQ
2921
2922 tsv->value_known = target_get_trace_state_variable_value (tsv->number,
2923 &tsv->value);
112e8700 2924 uiout->field_int ("current", tsv->value);
dc673c81
YQ
2925 }
2926 else
2927 {
112e8700
SM
2928 uiout->field_skip ("name");
2929 uiout->field_skip ("current");
dc673c81
YQ
2930 }
2931
2932 do_cleanups (cleanup_child);
2933 }
2934
2935 do_cleanups (list_cleanup);
2936 }
2937
2938 /* Memory. */
2939 {
2940 struct cleanup *list_cleanup;
2941 VEC(mem_range_s) *available_memory = NULL;
2942 struct mem_range *r;
2943 int i;
2944
2945 traceframe_available_memory (&available_memory, 0, ULONGEST_MAX);
2946 make_cleanup (VEC_cleanup(mem_range_s), &available_memory);
2947
2948 list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "memory");
2949
2950 for (i = 0; VEC_iterate (mem_range_s, available_memory, i, r); i++)
2951 {
2952 struct cleanup *cleanup_child;
2953 gdb_byte *data;
2954 struct gdbarch *gdbarch = target_gdbarch ();
2955
2956 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
2957
112e8700
SM
2958 uiout->field_core_addr ("address", gdbarch, r->start);
2959 uiout->field_int ("length", r->length);
dc673c81 2960
224c3ddb 2961 data = (gdb_byte *) xmalloc (r->length);
dc673c81
YQ
2962 make_cleanup (xfree, data);
2963
2964 if (memory_contents)
2965 {
2966 if (target_read_memory (r->start, data, r->length) == 0)
2967 {
2968 int m;
2969 char *data_str, *p;
2970
224c3ddb 2971 data_str = (char *) xmalloc (r->length * 2 + 1);
dc673c81
YQ
2972 make_cleanup (xfree, data_str);
2973
2974 for (m = 0, p = data_str; m < r->length; ++m, p += 2)
2975 sprintf (p, "%02x", data[m]);
112e8700 2976 uiout->field_string ("contents", data_str);
dc673c81
YQ
2977 }
2978 else
112e8700 2979 uiout->field_skip ("contents");
dc673c81
YQ
2980 }
2981 do_cleanups (cleanup_child);
2982 }
2983
2984 do_cleanups (list_cleanup);
2985 }
2986
2987 do_cleanups (old_chain);
2988}
329ea579
PA
2989
2990void
2991_initialize_mi_main (void)
2992{
2993 struct cmd_list_element *c;
2994
2995 add_setshow_boolean_cmd ("mi-async", class_run,
2996 &mi_async_1, _("\
2997Set whether MI asynchronous mode is enabled."), _("\
2998Show whether MI asynchronous mode is enabled."), _("\
2999Tells GDB whether MI should be in asynchronous mode."),
3000 set_mi_async_command,
3001 show_mi_async_command,
3002 &setlist,
3003 &showlist);
3004
3005 /* Alias old "target-async" to "mi-async". */
3006 c = add_alias_cmd ("target-async", "mi-async", class_run, 0, &setlist);
3007 deprecate_cmd (c, "set mi-async");
3008 c = add_alias_cmd ("target-async", "mi-async", class_run, 0, &showlist);
3009 deprecate_cmd (c, "show mi-async");
3010}