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