]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mi/mi-main.c
Turn value_contents_eq into a method
[thirdparty/binutils-gdb.git] / gdb / mi / mi-main.c
CommitLineData
fb40c209 1/* MI Command Set.
cd0bfa36 2
213516ef 3 Copyright (C) 2000-2023 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"
ee04bfc0 43#include "mi-interp.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 *);
9efe17a3 98static void output_register (frame_info_ptr, int regnum, int format,
c898adb7 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 {
637b2f86 883 if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
112e8700 884 uiout->field_string (NULL, "");
173d6894 885 else
112e8700 886 uiout->field_string (NULL, gdbarch_register_name (gdbarch, regnum));
fb40c209
AC
887 }
888 }
889
41296c92 890 /* Else, list of register #s, just do listed regs. */
fb40c209
AC
891 for (i = 0; i < argc; i++)
892 {
893 regnum = atoi (argv[i]);
173d6894 894 if (regnum < 0 || regnum >= numregs)
7ea6d463 895 error (_("bad register number"));
a13e061a 896
637b2f86 897 if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
112e8700 898 uiout->field_string (NULL, "");
173d6894 899 else
112e8700 900 uiout->field_string (NULL, gdbarch_register_name (gdbarch, regnum));
fb40c209 901 }
fb40c209
AC
902}
903
ce8f13f8 904void
9f33b8b7 905mi_cmd_data_list_changed_registers (const char *command, char **argv, int argc)
fb40c209 906{
daf6667d 907 static std::unique_ptr<readonly_detached_regcache> this_regs;
79a45e25 908 struct ui_out *uiout = current_uiout;
daf6667d 909 std::unique_ptr<readonly_detached_regcache> prev_regs;
7ccb0be9 910 struct gdbarch *gdbarch;
62ad7ce7 911 int regnum, numregs;
fb40c209
AC
912 int i;
913
2b03b41d
SS
914 /* The last time we visited this function, the current frame's
915 register contents were saved in THIS_REGS. Move THIS_REGS over
916 to PREV_REGS, and refresh THIS_REGS with the now-current register
917 contents. */
6ed7ea50 918
9ac86b52 919 prev_regs = std::move (this_regs);
6ed7ea50 920 this_regs = frame_save_as_regcache (get_selected_frame (NULL));
6ed7ea50 921
fb40c209 922 /* Note that the test for a valid register must include checking the
2b03b41d
SS
923 gdbarch_register_name because gdbarch_num_regs may be allocated
924 for the union of the register sets within a family of related
925 processors. In this case, some entries of gdbarch_register_name
926 will change depending upon the particular processor being
927 debugged. */
fb40c209 928
ac7936df 929 gdbarch = this_regs->arch ();
f6efe3f8 930 numregs = gdbarch_num_cooked_regs (gdbarch);
fb40c209 931
e6a2252a 932 ui_out_emit_list list_emitter (uiout, "changed-registers");
fb40c209 933
2b03b41d 934 if (argc == 0)
fb40c209 935 {
2b03b41d 936 /* No args, just do all the regs. */
fb40c209
AC
937 for (regnum = 0;
938 regnum < numregs;
939 regnum++)
940 {
637b2f86 941 if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
fb40c209 942 continue;
62ad7ce7
YQ
943
944 if (register_changed_p (regnum, prev_regs.get (),
945 this_regs.get ()))
381befee 946 uiout->field_signed (NULL, regnum);
fb40c209
AC
947 }
948 }
949
41296c92 950 /* Else, list of register #s, just do listed regs. */
fb40c209
AC
951 for (i = 0; i < argc; i++)
952 {
953 regnum = atoi (argv[i]);
954
955 if (regnum >= 0
956 && regnum < numregs
7ccb0be9 957 && *gdbarch_register_name (gdbarch, regnum) != '\000')
fb40c209 958 {
62ad7ce7
YQ
959 if (register_changed_p (regnum, prev_regs.get (),
960 this_regs.get ()))
381befee 961 uiout->field_signed (NULL, regnum);
fb40c209
AC
962 }
963 else
7ea6d463 964 error (_("bad register number"));
fb40c209 965 }
fb40c209
AC
966}
967
62ad7ce7 968static bool
daf6667d
YQ
969register_changed_p (int regnum, readonly_detached_regcache *prev_regs,
970 readonly_detached_regcache *this_regs)
fb40c209 971{
ac7936df 972 struct gdbarch *gdbarch = this_regs->arch ();
ff4ca5ac 973 struct value *prev_value, *this_value;
fb40c209 974
e69aa73e
PA
975 /* First time through or after gdbarch change consider all registers
976 as changed. */
ac7936df 977 if (!prev_regs || prev_regs->arch () != gdbarch)
62ad7ce7 978 return true;
fb40c209 979
6ed7ea50 980 /* Get register contents and compare. */
ff4ca5ac
AH
981 prev_value = prev_regs->cooked_read_value (regnum);
982 this_value = this_regs->cooked_read_value (regnum);
983 gdb_assert (prev_value != NULL);
984 gdb_assert (this_value != NULL);
985
02744ba9
TT
986 auto ret = !prev_value->contents_eq (0, this_value, 0,
987 register_size (gdbarch, regnum));
ff4ca5ac
AH
988
989 release_value (prev_value);
990 release_value (this_value);
ff4ca5ac 991 return ret;
fb40c209
AC
992}
993
41296c92 994/* Return a list of register number and value pairs. The valid
fb40c209 995 arguments expected are: a letter indicating the format in which to
2b03b41d
SS
996 display the registers contents. This can be one of: x
997 (hexadecimal), d (decimal), N (natural), t (binary), o (octal), r
998 (raw). After the format argument there can be a sequence of
999 numbers, indicating which registers to fetch the content of. If
1000 the format is the only argument, a list of all the registers with
1001 their values is returned. */
1002
ce8f13f8 1003void
9f33b8b7 1004mi_cmd_data_list_register_values (const char *command, char **argv, int argc)
fb40c209 1005{
79a45e25 1006 struct ui_out *uiout = current_uiout;
bd2b40ac 1007 frame_info_ptr frame;
7ccb0be9 1008 struct gdbarch *gdbarch;
a13e061a 1009 int regnum, numregs, format;
fb40c209 1010 int i;
c898adb7
YQ
1011 int skip_unavailable = 0;
1012 int oind = 0;
1013 enum opt
1014 {
1015 SKIP_UNAVAILABLE,
1016 };
1017 static const struct mi_opt opts[] =
1018 {
1019 {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
1020 { 0, 0, 0 }
1021 };
fb40c209
AC
1022
1023 /* Note that the test for a valid register must include checking the
2b03b41d
SS
1024 gdbarch_register_name because gdbarch_num_regs may be allocated
1025 for the union of the register sets within a family of related
1026 processors. In this case, some entries of gdbarch_register_name
1027 will change depending upon the particular processor being
1028 debugged. */
fb40c209 1029
c898adb7
YQ
1030 while (1)
1031 {
1032 char *oarg;
1033 int opt = mi_getopt ("-data-list-register-values", argc, argv,
1034 opts, &oind, &oarg);
1035
1036 if (opt < 0)
1037 break;
1038 switch ((enum opt) opt)
1039 {
1040 case SKIP_UNAVAILABLE:
1041 skip_unavailable = 1;
1042 break;
1043 }
1044 }
1045
1046 if (argc - oind < 1)
7ea6d463 1047 error (_("-data-list-register-values: Usage: "
c898adb7
YQ
1048 "-data-list-register-values [--skip-unavailable] <format>"
1049 " [<regnum1>...<regnumN>]"));
fb40c209 1050
c898adb7 1051 format = (int) argv[oind][0];
fb40c209 1052
7ccb0be9
UW
1053 frame = get_selected_frame (NULL);
1054 gdbarch = get_frame_arch (frame);
f6efe3f8 1055 numregs = gdbarch_num_cooked_regs (gdbarch);
7ccb0be9 1056
10f489e5 1057 ui_out_emit_list list_emitter (uiout, "register-values");
fb40c209 1058
c898adb7 1059 if (argc - oind == 1)
fb40c209 1060 {
2b03b41d 1061 /* No args, beside the format: do all the regs. */
fb40c209
AC
1062 for (regnum = 0;
1063 regnum < numregs;
1064 regnum++)
1065 {
637b2f86 1066 if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
fb40c209 1067 continue;
1edebdbf 1068
c898adb7 1069 output_register (frame, regnum, format, skip_unavailable);
fb40c209
AC
1070 }
1071 }
1072
41296c92 1073 /* Else, list of register #s, just do listed regs. */
c898adb7 1074 for (i = 1 + oind; i < argc; i++)
fb40c209
AC
1075 {
1076 regnum = atoi (argv[i]);
1077
1078 if (regnum >= 0
1079 && regnum < numregs
7ccb0be9 1080 && *gdbarch_register_name (gdbarch, regnum) != '\000')
c898adb7 1081 output_register (frame, regnum, format, skip_unavailable);
fb40c209 1082 else
7ea6d463 1083 error (_("bad register number"));
fb40c209 1084 }
fb40c209
AC
1085}
1086
c898adb7
YQ
1087/* Output one register REGNUM's contents in the desired FORMAT. If
1088 SKIP_UNAVAILABLE is true, skip the register if it is
1089 unavailable. */
2b03b41d 1090
a13e061a 1091static void
bd2b40ac 1092output_register (frame_info_ptr frame, int regnum, int format,
c898adb7 1093 int skip_unavailable)
fb40c209 1094{
79a45e25 1095 struct ui_out *uiout = current_uiout;
901461f8 1096 struct value *val = value_of_register (regnum, frame);
fdc8aae8 1097 struct value_print_options opts;
1edebdbf 1098
c898adb7
YQ
1099 if (skip_unavailable && !value_entirely_available (val))
1100 return;
1101
2e783024 1102 ui_out_emit_tuple tuple_emitter (uiout, NULL);
381befee 1103 uiout->field_signed ("number", regnum);
fb40c209 1104
fb40c209
AC
1105 if (format == 'N')
1106 format = 0;
1107
fb40c209 1108 if (format == 'r')
fdc8aae8
AB
1109 format = 'z';
1110
d7e74731 1111 string_file stb;
fdc8aae8
AB
1112
1113 get_formatted_print_options (&opts, format);
dad6b350 1114 opts.deref_ref = true;
a6e05a6c 1115 common_val_print (val, &stb, 0, &opts, current_language);
112e8700 1116 uiout->field_stream ("value", stb);
fb40c209
AC
1117}
1118
24e8cecf 1119/* Write given values into registers. The registers and values are
c1244769 1120 given as pairs. The corresponding MI command is
9a2b4c1b 1121 -data-write-register-values <format>
dda83cd7 1122 [<regnum1> <value1>...<regnumN> <valueN>] */
ce8f13f8 1123void
9f33b8b7 1124mi_cmd_data_write_register_values (const char *command, char **argv, int argc)
24e8cecf 1125{
7ccb0be9
UW
1126 struct regcache *regcache;
1127 struct gdbarch *gdbarch;
9f3a1602 1128 int numregs, i;
24e8cecf
EZ
1129
1130 /* Note that the test for a valid register must include checking the
2b03b41d
SS
1131 gdbarch_register_name because gdbarch_num_regs may be allocated
1132 for the union of the register sets within a family of related
1133 processors. In this case, some entries of gdbarch_register_name
1134 will change depending upon the particular processor being
1135 debugged. */
24e8cecf 1136
7ccb0be9 1137 regcache = get_current_regcache ();
ac7936df 1138 gdbarch = regcache->arch ();
f6efe3f8 1139 numregs = gdbarch_num_cooked_regs (gdbarch);
24e8cecf
EZ
1140
1141 if (argc == 0)
7ea6d463
PM
1142 error (_("-data-write-register-values: Usage: -data-write-register-"
1143 "values <format> [<regnum1> <value1>...<regnumN> <valueN>]"));
24e8cecf 1144
9dccd06e 1145 if (!target_has_registers ())
7ea6d463 1146 error (_("-data-write-register-values: No registers."));
24e8cecf
EZ
1147
1148 if (!(argc - 1))
7ea6d463 1149 error (_("-data-write-register-values: No regs and values specified."));
24e8cecf
EZ
1150
1151 if ((argc - 1) % 2)
7ea6d463
PM
1152 error (_("-data-write-register-values: "
1153 "Regs and vals are not in pairs."));
24e8cecf
EZ
1154
1155 for (i = 1; i < argc; i = i + 2)
1156 {
9f3a1602 1157 int regnum = atoi (argv[i]);
24e8cecf 1158
9f3a1602 1159 if (regnum >= 0 && regnum < numregs
637b2f86 1160 && *gdbarch_register_name (gdbarch, regnum) != '\0')
24e8cecf 1161 {
9f3a1602 1162 LONGEST value;
d8bf3afa 1163
9f3a1602 1164 /* Get the value as a number. */
24e8cecf 1165 value = parse_and_eval_address (argv[i + 1]);
9f3a1602 1166
41296c92 1167 /* Write it down. */
7ccb0be9 1168 regcache_cooked_write_signed (regcache, regnum, value);
24e8cecf
EZ
1169 }
1170 else
7ea6d463 1171 error (_("bad register number"));
24e8cecf 1172 }
24e8cecf
EZ
1173}
1174
41296c92 1175/* Evaluate the value of the argument. The argument is an
fb40c209 1176 expression. If the expression contains spaces it needs to be
41296c92 1177 included in double quotes. */
2b03b41d 1178
ce8f13f8 1179void
9f33b8b7 1180mi_cmd_data_evaluate_expression (const char *command, char **argv, int argc)
fb40c209 1181{
96052a95 1182 struct value *val;
79a45b7d 1183 struct value_print_options opts;
79a45e25 1184 struct ui_out *uiout = current_uiout;
fb40c209 1185
fb40c209 1186 if (argc != 1)
f99d8bf4
PA
1187 error (_("-data-evaluate-expression: "
1188 "Usage: -data-evaluate-expression expression"));
fb40c209 1189
4d01a485 1190 expression_up expr = parse_expression (argv[0]);
fb40c209 1191
4d01a485 1192 val = evaluate_expression (expr.get ());
fb40c209 1193
d7e74731
PA
1194 string_file stb;
1195
41296c92 1196 /* Print the result of the expression evaluation. */
79a45b7d 1197 get_user_print_options (&opts);
dad6b350 1198 opts.deref_ref = false;
d7e74731 1199 common_val_print (val, &stb, 0, &opts, current_language);
fb40c209 1200
112e8700 1201 uiout->field_stream ("value", stb);
fb40c209
AC
1202}
1203
2b03b41d 1204/* This is the -data-read-memory command.
fb40c209
AC
1205
1206 ADDR: start address of data to be dumped.
c1244769 1207 WORD-FORMAT: a char indicating format for the ``word''. See
fb40c209 1208 the ``x'' command.
41296c92 1209 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
fb40c209 1210 NR_ROW: Number of rows.
30baf67b 1211 NR_COL: The number of columns (words per row).
fb40c209
AC
1212 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
1213 ASCHAR for unprintable characters.
1214
1215 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
30baf67b 1216 displays them. Returns:
fb40c209
AC
1217
1218 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
1219
c1244769 1220 Returns:
2b03b41d 1221 The number of bytes read is SIZE*ROW*COL. */
fb40c209 1222
ce8f13f8 1223void
9f33b8b7 1224mi_cmd_data_read_memory (const char *command, char **argv, int argc)
fb40c209 1225{
e17c207e 1226 struct gdbarch *gdbarch = get_current_arch ();
79a45e25 1227 struct ui_out *uiout = current_uiout;
fb40c209 1228 CORE_ADDR addr;
2b03b41d 1229 long total_bytes, nr_cols, nr_rows;
fb40c209
AC
1230 char word_format;
1231 struct type *word_type;
1232 long word_size;
1233 char word_asize;
1234 char aschar;
fb40c209
AC
1235 int nr_bytes;
1236 long offset = 0;
56934ab1
AS
1237 int oind = 0;
1238 char *oarg;
fb40c209 1239 enum opt
fb40c209 1240 {
2b03b41d 1241 OFFSET_OPT
fb40c209 1242 };
2b03b41d
SS
1243 static const struct mi_opt opts[] =
1244 {
1245 {"o", OFFSET_OPT, 1},
1246 { 0, 0, 0 }
1247 };
fb40c209
AC
1248
1249 while (1)
1250 {
1b05df00 1251 int opt = mi_getopt ("-data-read-memory", argc, argv, opts,
56934ab1 1252 &oind, &oarg);
102040f0 1253
fb40c209
AC
1254 if (opt < 0)
1255 break;
1256 switch ((enum opt) opt)
1257 {
1258 case OFFSET_OPT:
56934ab1 1259 offset = atol (oarg);
fb40c209
AC
1260 break;
1261 }
1262 }
56934ab1
AS
1263 argv += oind;
1264 argc -= oind;
fb40c209
AC
1265
1266 if (argc < 5 || argc > 6)
7ea6d463
PM
1267 error (_("-data-read-memory: Usage: "
1268 "ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR]."));
fb40c209
AC
1269
1270 /* Extract all the arguments. */
1271
41296c92 1272 /* Start address of the memory dump. */
fb40c209 1273 addr = parse_and_eval_address (argv[0]) + offset;
41296c92 1274 /* The format character to use when displaying a memory word. See
2b03b41d 1275 the ``x'' command. */
fb40c209 1276 word_format = argv[1][0];
41296c92 1277 /* The size of the memory word. */
fb40c209
AC
1278 word_size = atol (argv[2]);
1279 switch (word_size)
1280 {
1281 case 1:
df4df182 1282 word_type = builtin_type (gdbarch)->builtin_int8;
fb40c209
AC
1283 word_asize = 'b';
1284 break;
1285 case 2:
df4df182 1286 word_type = builtin_type (gdbarch)->builtin_int16;
fb40c209
AC
1287 word_asize = 'h';
1288 break;
1289 case 4:
df4df182 1290 word_type = builtin_type (gdbarch)->builtin_int32;
fb40c209
AC
1291 word_asize = 'w';
1292 break;
1293 case 8:
df4df182 1294 word_type = builtin_type (gdbarch)->builtin_int64;
fb40c209
AC
1295 word_asize = 'g';
1296 break;
1297 default:
df4df182 1298 word_type = builtin_type (gdbarch)->builtin_int8;
fb40c209
AC
1299 word_asize = 'b';
1300 }
41296c92 1301 /* The number of rows. */
fb40c209
AC
1302 nr_rows = atol (argv[3]);
1303 if (nr_rows <= 0)
7ea6d463 1304 error (_("-data-read-memory: invalid number of rows."));
a13e061a 1305
41296c92 1306 /* Number of bytes per row. */
fb40c209
AC
1307 nr_cols = atol (argv[4]);
1308 if (nr_cols <= 0)
7ea6d463 1309 error (_("-data-read-memory: invalid number of columns."));
a13e061a 1310
41296c92 1311 /* The un-printable character when printing ascii. */
fb40c209
AC
1312 if (argc == 6)
1313 aschar = *argv[5];
1314 else
1315 aschar = 0;
1316
41296c92 1317 /* Create a buffer and read it in. */
fb40c209 1318 total_bytes = word_size * nr_rows * nr_cols;
6fc31fc7 1319
d5722aa2 1320 gdb::byte_vector mbuf (total_bytes);
cf7a04e8 1321
328d42d8
SM
1322 nr_bytes = target_read (current_inferior ()->top_target (),
1323 TARGET_OBJECT_MEMORY, NULL,
8b88a78e 1324 mbuf.data (), addr, total_bytes);
cf7a04e8 1325 if (nr_bytes <= 0)
7ea6d463 1326 error (_("Unable to read memory."));
fb40c209 1327
41296c92 1328 /* Output the header information. */
112e8700 1329 uiout->field_core_addr ("addr", gdbarch, addr);
381befee
TT
1330 uiout->field_signed ("nr-bytes", nr_bytes);
1331 uiout->field_signed ("total-bytes", total_bytes);
112e8700
SM
1332 uiout->field_core_addr ("next-row", gdbarch, addr + word_size * nr_cols);
1333 uiout->field_core_addr ("prev-row", gdbarch, addr - word_size * nr_cols);
1334 uiout->field_core_addr ("next-page", gdbarch, addr + total_bytes);
1335 uiout->field_core_addr ("prev-page", gdbarch, addr - total_bytes);
fb40c209 1336
30baf67b 1337 /* Build the result as a two dimensional table. */
fb40c209 1338 {
fb40c209
AC
1339 int row;
1340 int row_byte;
102040f0 1341
d7e74731 1342 string_file stream;
f99d8bf4 1343
e6a2252a 1344 ui_out_emit_list list_emitter (uiout, "memory");
fb40c209
AC
1345 for (row = 0, row_byte = 0;
1346 row < nr_rows;
1347 row++, row_byte += nr_cols * word_size)
1348 {
1349 int col;
1350 int col_byte;
b926417a 1351 struct value_print_options print_opts;
79a45b7d 1352
2e783024 1353 ui_out_emit_tuple tuple_emitter (uiout, NULL);
112e8700 1354 uiout->field_core_addr ("addr", gdbarch, addr + row_byte);
9a2b4c1b
MS
1355 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr +
1356 row_byte); */
e6a2252a
TT
1357 {
1358 ui_out_emit_list list_data_emitter (uiout, "data");
b926417a 1359 get_formatted_print_options (&print_opts, word_format);
e6a2252a
TT
1360 for (col = 0, col_byte = row_byte;
1361 col < nr_cols;
1362 col++, col_byte += word_size)
1363 {
1364 if (col_byte + word_size > nr_bytes)
1365 {
1366 uiout->field_string (NULL, "N/A");
1367 }
1368 else
1369 {
1370 stream.clear ();
b926417a
TT
1371 print_scalar_formatted (&mbuf[col_byte], word_type,
1372 &print_opts, word_asize, &stream);
e6a2252a
TT
1373 uiout->field_stream (NULL, stream);
1374 }
1375 }
1376 }
1377
fb40c209
AC
1378 if (aschar)
1379 {
1380 int byte;
102040f0 1381
d7e74731 1382 stream.clear ();
9a2b4c1b
MS
1383 for (byte = row_byte;
1384 byte < row_byte + word_size * nr_cols; byte++)
fb40c209
AC
1385 {
1386 if (byte >= nr_bytes)
d7e74731 1387 stream.putc ('X');
fb40c209 1388 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
d7e74731 1389 stream.putc (aschar);
fb40c209 1390 else
d7e74731 1391 stream.putc (mbuf[byte]);
fb40c209 1392 }
112e8700 1393 uiout->field_stream ("ascii", stream);
fb40c209 1394 }
fb40c209 1395 }
fb40c209 1396 }
fb40c209
AC
1397}
1398
8dedea02 1399void
9f33b8b7 1400mi_cmd_data_read_memory_bytes (const char *command, char **argv, int argc)
8dedea02
VP
1401{
1402 struct gdbarch *gdbarch = get_current_arch ();
79a45e25 1403 struct ui_out *uiout = current_uiout;
8dedea02
VP
1404 CORE_ADDR addr;
1405 LONGEST length;
8dedea02 1406 long offset = 0;
cfc32360 1407 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
56934ab1
AS
1408 int oind = 0;
1409 char *oarg;
8dedea02 1410 enum opt
8dedea02 1411 {
2b03b41d 1412 OFFSET_OPT
8dedea02 1413 };
2b03b41d
SS
1414 static const struct mi_opt opts[] =
1415 {
1416 {"o", OFFSET_OPT, 1},
1417 { 0, 0, 0 }
1418 };
8dedea02
VP
1419
1420 while (1)
1421 {
1b05df00 1422 int opt = mi_getopt ("-data-read-memory-bytes", argc, argv, opts,
56934ab1 1423 &oind, &oarg);
8dedea02
VP
1424 if (opt < 0)
1425 break;
1426 switch ((enum opt) opt)
1427 {
1428 case OFFSET_OPT:
56934ab1 1429 offset = atol (oarg);
8dedea02
VP
1430 break;
1431 }
1432 }
56934ab1
AS
1433 argv += oind;
1434 argc -= oind;
8dedea02
VP
1435
1436 if (argc != 2)
7ea6d463 1437 error (_("Usage: [ -o OFFSET ] ADDR LENGTH."));
8dedea02
VP
1438
1439 addr = parse_and_eval_address (argv[0]) + offset;
1440 length = atol (argv[1]);
1441
386c8614 1442 std::vector<memory_read_result> result
328d42d8 1443 = read_memory_robust (current_inferior ()->top_target (), addr, length);
8dedea02 1444
386c8614 1445 if (result.size () == 0)
7ea6d463 1446 error (_("Unable to read memory."));
8dedea02 1447
e6a2252a 1448 ui_out_emit_list list_emitter (uiout, "memory");
386c8614 1449 for (const memory_read_result &read_result : result)
8dedea02 1450 {
2e783024 1451 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8dedea02 1452
386c8614
TT
1453 uiout->field_core_addr ("begin", gdbarch, read_result.begin);
1454 uiout->field_core_addr ("offset", gdbarch, read_result.begin - addr);
1455 uiout->field_core_addr ("end", gdbarch, read_result.end);
8dedea02 1456
386c8614
TT
1457 std::string data = bin2hex (read_result.data.get (),
1458 (read_result.end - read_result.begin)
1459 * unit_size);
8dd8c8d4 1460 uiout->field_string ("contents", data);
8dedea02 1461 }
8dedea02
VP
1462}
1463
2b03b41d 1464/* Implementation of the -data-write_memory command.
fb40c209 1465
177b42fe 1466 COLUMN_OFFSET: optional argument. Must be preceded by '-o'. The
fb40c209
AC
1467 offset from the beginning of the memory grid row where the cell to
1468 be written is.
1469 ADDR: start address of the row in the memory grid where the memory
41296c92 1470 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
fb40c209 1471 the location to write to.
c1244769 1472 FORMAT: a char indicating format for the ``word''. See
fb40c209
AC
1473 the ``x'' command.
1474 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
1475 VALUE: value to be written into the memory address.
1476
1477 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1478
41296c92 1479 Prints nothing. */
2b03b41d 1480
ce8f13f8 1481void
9f33b8b7 1482mi_cmd_data_write_memory (const char *command, char **argv, int argc)
fb40c209 1483{
e17a4113
UW
1484 struct gdbarch *gdbarch = get_current_arch ();
1485 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
fb40c209 1486 CORE_ADDR addr;
fb40c209
AC
1487 long word_size;
1488 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
41296c92 1489 enough when using a compiler other than GCC. */
fb40c209 1490 LONGEST value;
fb40c209 1491 long offset = 0;
56934ab1
AS
1492 int oind = 0;
1493 char *oarg;
fb40c209 1494 enum opt
fb40c209 1495 {
2b03b41d 1496 OFFSET_OPT
fb40c209 1497 };
2b03b41d
SS
1498 static const struct mi_opt opts[] =
1499 {
1500 {"o", OFFSET_OPT, 1},
1501 { 0, 0, 0 }
1502 };
fb40c209
AC
1503
1504 while (1)
1505 {
1b05df00 1506 int opt = mi_getopt ("-data-write-memory", argc, argv, opts,
56934ab1 1507 &oind, &oarg);
102040f0 1508
fb40c209
AC
1509 if (opt < 0)
1510 break;
1511 switch ((enum opt) opt)
1512 {
1513 case OFFSET_OPT:
56934ab1 1514 offset = atol (oarg);
fb40c209
AC
1515 break;
1516 }
1517 }
56934ab1
AS
1518 argv += oind;
1519 argc -= oind;
fb40c209
AC
1520
1521 if (argc != 4)
7ea6d463
PM
1522 error (_("-data-write-memory: Usage: "
1523 "[-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE."));
fb40c209 1524
41296c92
NR
1525 /* Extract all the arguments. */
1526 /* Start address of the memory dump. */
fb40c209 1527 addr = parse_and_eval_address (argv[0]);
2b03b41d 1528 /* The size of the memory word. */
fb40c209
AC
1529 word_size = atol (argv[2]);
1530
41296c92 1531 /* Calculate the real address of the write destination. */
fb40c209
AC
1532 addr += (offset * word_size);
1533
41296c92 1534 /* Get the value as a number. */
fb40c209 1535 value = parse_and_eval_address (argv[3]);
41296c92 1536 /* Get the value into an array. */
26fcd5d7
TT
1537 gdb::byte_vector buffer (word_size);
1538 store_signed_integer (buffer.data (), word_size, byte_order, value);
41296c92 1539 /* Write it down to memory. */
26fcd5d7 1540 write_memory_with_notification (addr, buffer.data (), word_size);
fb40c209
AC
1541}
1542
2b03b41d 1543/* Implementation of the -data-write-memory-bytes command.
8dedea02
VP
1544
1545 ADDR: start address
62747a60
TT
1546 DATA: string of bytes to write at that address
1547 COUNT: number of bytes to be filled (decimal integer). */
2b03b41d 1548
8dedea02 1549void
9f33b8b7 1550mi_cmd_data_write_memory_bytes (const char *command, char **argv, int argc)
8dedea02
VP
1551{
1552 CORE_ADDR addr;
1553 char *cdata;
cfc32360
SM
1554 size_t len_hex, len_bytes, len_units, i, steps, remaining_units;
1555 long int count_units;
cfc32360 1556 int unit_size;
8dedea02 1557
62747a60
TT
1558 if (argc != 2 && argc != 3)
1559 error (_("Usage: ADDR DATA [COUNT]."));
8dedea02
VP
1560
1561 addr = parse_and_eval_address (argv[0]);
1562 cdata = argv[1];
cfc32360
SM
1563 len_hex = strlen (cdata);
1564 unit_size = gdbarch_addressable_memory_unit_size (get_current_arch ());
1565
1566 if (len_hex % (unit_size * 2) != 0)
1567 error (_("Hex-encoded '%s' must represent an integral number of "
1568 "addressable memory units."),
1ae0c35e
YQ
1569 cdata);
1570
cfc32360
SM
1571 len_bytes = len_hex / 2;
1572 len_units = len_bytes / unit_size;
1573
62747a60 1574 if (argc == 3)
cfc32360 1575 count_units = strtoul (argv[2], NULL, 10);
62747a60 1576 else
cfc32360 1577 count_units = len_units;
8dedea02 1578
45d288cc 1579 gdb::byte_vector databuf (len_bytes);
8dedea02 1580
cfc32360 1581 for (i = 0; i < len_bytes; ++i)
8dedea02
VP
1582 {
1583 int x;
62747a60 1584 if (sscanf (cdata + i * 2, "%02x", &x) != 1)
dda83cd7 1585 error (_("Invalid argument"));
62747a60
TT
1586 databuf[i] = (gdb_byte) x;
1587 }
1588
45d288cc 1589 gdb::byte_vector data;
cfc32360 1590 if (len_units < count_units)
62747a60 1591 {
cfc32360 1592 /* Pattern is made of less units than count:
dda83cd7 1593 repeat pattern to fill memory. */
45d288cc 1594 data = gdb::byte_vector (count_units * unit_size);
c1244769 1595
cfc32360
SM
1596 /* Number of times the pattern is entirely repeated. */
1597 steps = count_units / len_units;
1598 /* Number of remaining addressable memory units. */
1599 remaining_units = count_units % len_units;
1600 for (i = 0; i < steps; i++)
dda83cd7 1601 memcpy (&data[i * len_bytes], &databuf[0], len_bytes);
62747a60 1602
cfc32360 1603 if (remaining_units > 0)
dda83cd7 1604 memcpy (&data[steps * len_bytes], &databuf[0],
cfc32360 1605 remaining_units * unit_size);
62747a60 1606 }
c1244769 1607 else
62747a60 1608 {
c1244769 1609 /* Pattern is longer than or equal to count:
dda83cd7 1610 just copy count addressable memory units. */
45d288cc 1611 data = std::move (databuf);
8dedea02
VP
1612 }
1613
45d288cc 1614 write_memory_with_notification (addr, data.data (), count_units);
8dedea02
VP
1615}
1616
ce8f13f8 1617void
9f33b8b7 1618mi_cmd_enable_timings (const char *command, char **argv, int argc)
d8c83789
NR
1619{
1620 if (argc == 0)
1621 do_timings = 1;
1622 else if (argc == 1)
1623 {
1624 if (strcmp (argv[0], "yes") == 0)
1625 do_timings = 1;
1626 else if (strcmp (argv[0], "no") == 0)
1627 do_timings = 0;
1628 else
1629 goto usage_error;
1630 }
1631 else
1632 goto usage_error;
c1244769 1633
ce8f13f8 1634 return;
d8c83789
NR
1635
1636 usage_error:
7ea6d463 1637 error (_("-enable-timings: Usage: %s {yes|no}"), command);
d8c83789
NR
1638}
1639
ce8f13f8 1640void
9f33b8b7 1641mi_cmd_list_features (const char *command, char **argv, int argc)
084344da
VP
1642{
1643 if (argc == 0)
1644 {
79a45e25 1645 struct ui_out *uiout = current_uiout;
084344da 1646
10f489e5 1647 ui_out_emit_list list_emitter (uiout, "features");
112e8700
SM
1648 uiout->field_string (NULL, "frozen-varobjs");
1649 uiout->field_string (NULL, "pending-breakpoints");
1650 uiout->field_string (NULL, "thread-info");
1651 uiout->field_string (NULL, "data-read-memory-bytes");
1652 uiout->field_string (NULL, "breakpoint-notifications");
1653 uiout->field_string (NULL, "ada-task-info");
1654 uiout->field_string (NULL, "language-option");
1655 uiout->field_string (NULL, "info-gdb-mi-command");
1656 uiout->field_string (NULL, "undefined-command-error-code");
1657 uiout->field_string (NULL, "exec-run-start-option");
26fb3983 1658 uiout->field_string (NULL, "data-disassemble-a-option");
c1244769 1659
6dddc817 1660 if (ext_lang_initialized_p (get_ext_lang_defn (EXT_LANG_PYTHON)))
112e8700 1661 uiout->field_string (NULL, "python");
c1244769 1662
ce8f13f8 1663 return;
084344da
VP
1664 }
1665
7ea6d463 1666 error (_("-list-features should be passed no arguments"));
084344da 1667}
c6ebd6cf
VP
1668
1669void
9f33b8b7 1670mi_cmd_list_target_features (const char *command, char **argv, int argc)
c6ebd6cf
VP
1671{
1672 if (argc == 0)
1673 {
79a45e25 1674 struct ui_out *uiout = current_uiout;
c6ebd6cf 1675
10f489e5 1676 ui_out_emit_list list_emitter (uiout, "features");
329ea579 1677 if (mi_async_p ())
112e8700 1678 uiout->field_string (NULL, "async");
05374cfd 1679 if (target_can_execute_reverse ())
112e8700 1680 uiout->field_string (NULL, "reverse");
c6ebd6cf
VP
1681 return;
1682 }
1683
7ea6d463 1684 error (_("-list-target-features should be passed no arguments"));
c6ebd6cf
VP
1685}
1686
a79b8f6e 1687void
9f33b8b7 1688mi_cmd_add_inferior (const char *command, char **argv, int argc)
a79b8f6e 1689{
6fd90137 1690 bool no_connection = false;
a79b8f6e 1691
6fd90137
AB
1692 /* Parse the command options. */
1693 enum opt
1694 {
1695 NO_CONNECTION_OPT,
1696 };
1697 static const struct mi_opt opts[] =
1698 {
1699 {"-no-connection", NO_CONNECTION_OPT, 0},
1700 {NULL, 0, 0},
1701 };
1702
1703 int oind = 0;
1704 char *oarg;
1705
1706 while (1)
1707 {
1708 int opt = mi_getopt ("-add-inferior", argc, argv, opts, &oind, &oarg);
1709
1710 if (opt < 0)
1711 break;
1712 switch ((enum opt) opt)
1713 {
1714 case NO_CONNECTION_OPT:
1715 no_connection = true;
1716 break;
1717 }
1718 }
a79b8f6e 1719
d43bd54d
US
1720 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1721
6fd90137 1722 inferior *inf = add_inferior_with_spaces ();
a79b8f6e 1723
6fd90137
AB
1724 switch_to_inferior_and_push_target (inf, no_connection,
1725 current_inferior ());
d43bd54d 1726
112e8700 1727 current_uiout->field_fmt ("inferior", "i%d", inf->num);
6fd90137
AB
1728
1729 process_stratum_target *proc_target = inf->process_target ();
1730
1731 if (proc_target != nullptr)
1732 {
1733 ui_out_emit_tuple tuple_emitter (current_uiout, "connection");
1734 current_uiout->field_unsigned ("number", proc_target->connection_number);
1735 current_uiout->field_string ("name", proc_target->shortname ());
1736 }
a79b8f6e
VP
1737}
1738
1739void
9f33b8b7 1740mi_cmd_remove_inferior (const char *command, char **argv, int argc)
a79b8f6e
VP
1741{
1742 int id;
a9ac81b1 1743 struct inferior *inf_to_remove;
a79b8f6e
VP
1744
1745 if (argc != 1)
7ea6d463 1746 error (_("-remove-inferior should be passed a single argument"));
a79b8f6e 1747
e2b4a699 1748 if (sscanf (argv[0], "i%d", &id) != 1)
7ea6d463 1749 error (_("the thread group id is syntactically invalid"));
a79b8f6e 1750
a9ac81b1
SM
1751 inf_to_remove = find_inferior_id (id);
1752 if (inf_to_remove == NULL)
7ea6d463 1753 error (_("the specified thread group does not exist"));
a79b8f6e 1754
a9ac81b1 1755 if (inf_to_remove->pid != 0)
81ec3cce 1756 error (_("cannot remove an active inferior"));
8fa067af 1757
a9ac81b1 1758 if (inf_to_remove == current_inferior ())
57bf2d7e
MK
1759 {
1760 struct thread_info *tp = 0;
a9ac81b1
SM
1761 struct inferior *new_inferior = NULL;
1762
1763 for (inferior *inf : all_inferiors ())
1764 {
1765 if (inf != inf_to_remove)
1766 new_inferior = inf;
1767 }
57bf2d7e
MK
1768
1769 if (new_inferior == NULL)
1770 error (_("Cannot remove last inferior"));
1771
1772 set_current_inferior (new_inferior);
1773 if (new_inferior->pid != 0)
00431a78
PA
1774 tp = any_thread_of_inferior (new_inferior);
1775 if (tp != NULL)
1776 switch_to_thread (tp);
1777 else
1778 switch_to_no_thread ();
57bf2d7e
MK
1779 set_current_program_space (new_inferior->pspace);
1780 }
1781
a9ac81b1 1782 delete_inferior (inf_to_remove);
a79b8f6e
VP
1783}
1784
1785\f
1786
8d34ea23
KS
1787/* Execute a command within a safe environment.
1788 Return <0 for error; >=0 for ok.
1789
1790 args->action will tell mi_execute_command what action
789c4b5e 1791 to perform after the given command has executed (display/suppress
2b03b41d 1792 prompt, display error). */
fb40c209 1793
f30f06b8 1794static void
04bd08de 1795captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
fb40c209 1796{
d6f9b0fb 1797 struct mi_interp *mi = (struct mi_interp *) command_interp ();
fb40c209 1798
4333ada3
VP
1799 if (do_timings)
1800 current_command_ts = context->cmd_start;
d8c83789 1801
789c4b5e
TT
1802 scoped_restore save_token = make_scoped_restore (&current_token,
1803 context->token);
1f31650a 1804
a2840c35 1805 running_result_record_printed = 0;
f3b1572e 1806 mi_proceeded = 0;
fb40c209
AC
1807 switch (context->op)
1808 {
fb40c209 1809 case MI_COMMAND:
41296c92 1810 /* A MI command was read from the input stream. */
fb40c209 1811 if (mi_debug_p)
6cb06a8c
TT
1812 gdb_printf (gdb_stdlog,
1813 " token=`%s' command=`%s' args=`%s'\n",
1814 context->token, context->command, context->args);
d8c83789 1815
ce8f13f8 1816 mi_cmd_execute (context);
8d34ea23 1817
a2840c35 1818 /* Print the result if there were no errors.
4389a95a 1819
a2840c35 1820 Remember that on the way out of executing a command, you have
2b03b41d
SS
1821 to directly use the mi_interp's uiout, since the command
1822 could have reset the interpreter, in which case the current
1823 uiout will most likely crash in the mi_out_* routines. */
ce8f13f8 1824 if (!running_result_record_printed)
a2840c35 1825 {
0426ad51 1826 gdb_puts (context->token, mi->raw_stdout);
ce8f13f8
VP
1827 /* There's no particularly good reason why target-connect results
1828 in not ^done. Should kill ^connected for MI3. */
0426ad51
TT
1829 gdb_puts (strcmp (context->command, "target-select") == 0
1830 ? "^connected" : "^done", mi->raw_stdout);
9204d692 1831 mi_out_put (uiout, mi->raw_stdout);
a2840c35 1832 mi_out_rewind (uiout);
9204d692 1833 mi_print_timing_maybe (mi->raw_stdout);
0426ad51 1834 gdb_puts ("\n", mi->raw_stdout);
a2840c35
VP
1835 }
1836 else
2b03b41d
SS
1837 /* The command does not want anything to be printed. In that
1838 case, the command probably should not have written anything
1839 to uiout, but in case it has written something, discard it. */
a2840c35 1840 mi_out_rewind (uiout);
fb40c209
AC
1841 break;
1842
1843 case CLI_COMMAND:
78f5381d
AC
1844 {
1845 char *argv[2];
102040f0 1846
78f5381d
AC
1847 /* A CLI command was read from the input stream. */
1848 /* This "feature" will be removed as soon as we have a
1849 complete set of mi commands. */
1850 /* Echo the command on the console. */
6cb06a8c 1851 gdb_printf (gdb_stdlog, "%s\n", context->command);
78f5381d 1852 /* Call the "console" interpreter. */
a121b7c1 1853 argv[0] = (char *) INTERP_CONSOLE;
78f5381d 1854 argv[1] = context->command;
ce8f13f8 1855 mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);
78f5381d 1856
eec01795 1857 /* If we changed interpreters, DON'T print out anything. */
78f5381d 1858 if (current_interp_named_p (INTERP_MI)
78f5381d 1859 || current_interp_named_p (INTERP_MI2)
9db0d853
SM
1860 || current_interp_named_p (INTERP_MI3)
1861 || current_interp_named_p (INTERP_MI4))
78f5381d 1862 {
ce8f13f8 1863 if (!running_result_record_printed)
eec01795 1864 {
0426ad51
TT
1865 gdb_puts (context->token, mi->raw_stdout);
1866 gdb_puts ("^done", mi->raw_stdout);
9204d692 1867 mi_out_put (uiout, mi->raw_stdout);
eec01795 1868 mi_out_rewind (uiout);
9204d692 1869 mi_print_timing_maybe (mi->raw_stdout);
0426ad51 1870 gdb_puts ("\n", mi->raw_stdout);
eec01795 1871 }
eec01795
DJ
1872 else
1873 mi_out_rewind (uiout);
78f5381d
AC
1874 }
1875 break;
1876 }
fb40c209 1877 }
fb40c209
AC
1878}
1879
305aeedc
TT
1880/* Print a gdb exception to the MI output stream. */
1881
1882static void
94aeb44b 1883mi_print_exception (const char *token, const struct gdb_exception &exception)
305aeedc 1884{
d6f9b0fb 1885 struct mi_interp *mi = (struct mi_interp *) current_interpreter ();
9204d692 1886
0426ad51
TT
1887 gdb_puts (token, mi->raw_stdout);
1888 gdb_puts ("^error,msg=\"", mi->raw_stdout);
305aeedc 1889 if (exception.message == NULL)
0426ad51 1890 gdb_puts ("unknown error", mi->raw_stdout);
305aeedc 1891 else
d53fd721 1892 mi->raw_stdout->putstr (exception.what (), '"');
0426ad51 1893 gdb_puts ("\"", mi->raw_stdout);
2ea126fa
JB
1894
1895 switch (exception.error)
1896 {
1897 case UNDEFINED_COMMAND_ERROR:
0426ad51 1898 gdb_puts (",code=\"undefined-command\"", mi->raw_stdout);
2ea126fa
JB
1899 break;
1900 }
1901
0426ad51 1902 gdb_puts ("\n", mi->raw_stdout);
305aeedc 1903}
fb40c209
AC
1904
1905void
ee047554 1906mi_execute_command (const char *cmd, int from_tty)
fb40c209 1907{
305aeedc 1908 char *token;
4d89769a 1909 std::unique_ptr<struct mi_parse> command;
fb40c209 1910
41296c92
NR
1911 /* This is to handle EOF (^D). We just quit gdb. */
1912 /* FIXME: we should call some API function here. */
fb40c209
AC
1913 if (cmd == 0)
1914 quit_force (NULL, from_tty);
1915
11334b82
VP
1916 target_log_command (cmd);
1917
a70b8144 1918 try
305aeedc
TT
1919 {
1920 command = mi_parse (cmd, &token);
1921 }
230d2906 1922 catch (const gdb_exception &exception)
305aeedc
TT
1923 {
1924 mi_print_exception (token, exception);
1925 xfree (token);
1926 }
492d29ea
PA
1927
1928 if (command != NULL)
fb40c209 1929 {
156d9eab 1930 command->token = token;
4034d0ff 1931
d8c83789
NR
1932 if (do_timings)
1933 {
dcb07cfa 1934 command->cmd_start = new mi_timestamp ();
d8c83789
NR
1935 timestamp (command->cmd_start);
1936 }
1937
a70b8144 1938 try
04bd08de 1939 {
4d89769a 1940 captured_mi_execute_command (current_uiout, command.get ());
04bd08de 1941 }
230d2906 1942 catch (const gdb_exception &result)
fb40c209 1943 {
80614914
PA
1944 /* Like in start_event_loop, enable input and force display
1945 of the prompt. Otherwise, any command that calls
1946 async_disable_stdin, and then throws, will leave input
1947 disabled. */
1948 async_enable_stdin ();
1949 current_ui->prompt_state = PROMPT_NEEDED;
1950
fb40c209 1951 /* The command execution failed and error() was called
589e074d 1952 somewhere. */
305aeedc 1953 mi_print_exception (command->token, result);
79a45e25 1954 mi_out_rewind (current_uiout);
fb40c209 1955 }
a13e061a 1956
5d4e2b76
VP
1957 bpstat_do_actions ();
1958
fb40c209 1959 }
fb40c209
AC
1960}
1961
a2757c4e
JV
1962/* Captures the current user selected context state, that is the current
1963 thread and frame. Later we can then check if the user selected context
1964 has changed at all. */
1965
1966struct user_selected_context
1967{
1968 /* Constructor. */
1969 user_selected_context ()
9e6a252c
AB
1970 : m_previous_ptid (inferior_ptid)
1971 {
1972 save_selected_frame (&m_previous_frame_id, &m_previous_frame_level);
1973 }
a2757c4e
JV
1974
1975 /* Return true if the user selected context has changed since this object
1976 was created. */
1977 bool has_changed () const
1978 {
9e6a252c
AB
1979 /* Did the selected thread change? */
1980 if (m_previous_ptid != null_ptid && inferior_ptid != null_ptid
1981 && m_previous_ptid != inferior_ptid)
1982 return true;
1983
1984 /* Grab details of the currently selected frame, for comparison. */
1985 frame_id current_frame_id;
1986 int current_frame_level;
1987 save_selected_frame (&current_frame_id, &current_frame_level);
1988
1989 /* Did the selected frame level change? */
1990 if (current_frame_level != m_previous_frame_level)
1991 return true;
1992
1993 /* Did the selected frame id change? If the innermost frame is
1994 selected then the level will be -1, and the frame-id will be
1995 null_frame_id. As comparing null_frame_id with itself always
1996 reports not-equal, we only do the equality test if we have something
1997 other than the innermost frame selected. */
1998 if (current_frame_level != -1
a0cbd650 1999 && current_frame_id != m_previous_frame_id)
9e6a252c
AB
2000 return true;
2001
2002 /* Nothing changed! */
2003 return false;
a2757c4e
JV
2004 }
2005private:
2006 /* The previously selected thread. This might be null_ptid if there was
2007 no previously selected thread. */
2008 ptid_t m_previous_ptid;
2009
9e6a252c
AB
2010 /* The previously selected frame. If the innermost frame is selected, or
2011 no frame is selected, then the frame_id will be null_frame_id, and the
2012 level will be -1. */
2013 frame_id m_previous_frame_id;
2014 int m_previous_frame_level;
a2757c4e
JV
2015};
2016
ce8f13f8 2017static void
fb40c209
AC
2018mi_cmd_execute (struct mi_parse *parse)
2019{
54f70bc1 2020 scoped_value_mark cleanup = prepare_execute_command ();
1b98914a 2021
a79b8f6e
VP
2022 if (parse->all && parse->thread_group != -1)
2023 error (_("Cannot specify --thread-group together with --all"));
2024
2025 if (parse->all && parse->thread != -1)
2026 error (_("Cannot specify --thread together with --all"));
2027
2028 if (parse->thread_group != -1 && parse->thread != -1)
2029 error (_("Cannot specify --thread together with --thread-group"));
2030
1e92afda
VP
2031 if (parse->frame != -1 && parse->thread == -1)
2032 error (_("Cannot specify --frame without --thread"));
dcf4fbde 2033
a79b8f6e
VP
2034 if (parse->thread_group != -1)
2035 {
2036 struct inferior *inf = find_inferior_id (parse->thread_group);
2037 struct thread_info *tp = 0;
2038
2039 if (!inf)
46ef47e5 2040 error (_("Invalid thread group for the --thread-group option"));
a79b8f6e
VP
2041
2042 set_current_inferior (inf);
2043 /* This behaviour means that if --thread-group option identifies
2b03b41d
SS
2044 an inferior with multiple threads, then a random one will be
2045 picked. This is not a problem -- frontend should always
2046 provide --thread if it wishes to operate on a specific
2047 thread. */
a79b8f6e 2048 if (inf->pid != 0)
00431a78
PA
2049 tp = any_live_thread_of_inferior (inf);
2050 if (tp != NULL)
2051 switch_to_thread (tp);
2052 else
2053 switch_to_no_thread ();
a79b8f6e
VP
2054 set_current_program_space (inf->pspace);
2055 }
2056
a2757c4e
JV
2057 user_selected_context current_user_selected_context;
2058
a9c82bc1 2059 gdb::optional<scoped_restore_current_thread> thread_saver;
1e92afda
VP
2060 if (parse->thread != -1)
2061 {
00431a78 2062 thread_info *tp = find_thread_global_id (parse->thread);
102040f0 2063
00431a78 2064 if (tp == NULL)
1e92afda 2065 error (_("Invalid thread id: %d"), parse->thread);
dcf4fbde 2066
00431a78 2067 if (tp->state == THREAD_EXITED)
dcf4fbde
PA
2068 error (_("Thread id: %d has terminated"), parse->thread);
2069
a9c82bc1
JV
2070 if (parse->cmd->preserve_user_selected_context ())
2071 thread_saver.emplace ();
2072
00431a78 2073 switch_to_thread (tp);
1e92afda 2074 }
dcf4fbde 2075
a9c82bc1 2076 gdb::optional<scoped_restore_selected_frame> frame_saver;
1e92afda
VP
2077 if (parse->frame != -1)
2078 {
bd2b40ac 2079 frame_info_ptr fid;
1e92afda 2080 int frame = parse->frame;
102040f0 2081
1e92afda
VP
2082 fid = find_relative_frame (get_current_frame (), &frame);
2083 if (frame == 0)
a9c82bc1
JV
2084 {
2085 if (parse->cmd->preserve_user_selected_context ())
2086 frame_saver.emplace ();
2087
2088 select_frame (fid);
2089 }
1e92afda 2090 else
ea069267 2091 error (_("Invalid frame id: %d"), frame);
1e92afda 2092 }
dcf4fbde 2093
e3ad2841 2094 gdb::optional<scoped_restore_current_language> lang_saver;
403cb6b1
JB
2095 if (parse->language != language_unknown)
2096 {
e3ad2841 2097 lang_saver.emplace ();
403cb6b1
JB
2098 set_language (parse->language);
2099 }
2100
a79b8f6e
VP
2101 current_context = parse;
2102
1f6c8c33 2103 gdb_assert (parse->cmd != nullptr);
a2757c4e
JV
2104
2105 gdb::optional<scoped_restore_tmpl<int>> restore_suppress_notification
2106 = parse->cmd->do_suppress_notification ();
2107
1f6c8c33 2108 parse->cmd->invoke (parse);
a2757c4e
JV
2109
2110 if (!parse->cmd->preserve_user_selected_context ()
2111 && current_user_selected_context.has_changed ())
2112 gdb::observers::user_selected_context_changed.notify
2113 (USER_SELECTED_THREAD | USER_SELECTED_FRAME);
fb40c209
AC
2114}
2115
1f6c8c33 2116/* See mi-main.h. */
fb40c209
AC
2117
2118void
3be0fed6 2119mi_execute_cli_command (const char *cmd, bool args_p, const char *args)
fb40c209 2120{
3be0fed6 2121 if (cmd != nullptr)
fb40c209 2122 {
3be0fed6 2123 std::string run (cmd);
102040f0 2124
b2af646b 2125 if (args_p)
dcd5ddcc 2126 run = run + " " + args;
3be0fed6
AB
2127 else
2128 gdb_assert (args == nullptr);
2129
fb40c209 2130 if (mi_debug_p)
6cb06a8c
TT
2131 gdb_printf (gdb_stdlog, "cli=%s run=%s\n",
2132 cmd, run.c_str ());
3be0fed6 2133
95a6b0a1 2134 execute_command (run.c_str (), 0 /* from_tty */ );
fb40c209
AC
2135 }
2136}
2137
ce8f13f8 2138void
a121b7c1 2139mi_execute_async_cli_command (const char *cli_command, char **argv, int argc)
fb40c209 2140{
dcd5ddcc 2141 std::string run = cli_command;
fb40c209 2142
dcd5ddcc
TT
2143 if (argc)
2144 run = run + " " + *argv;
329ea579 2145 if (mi_async_p ())
dcd5ddcc 2146 run += "&";
fb40c209 2147
95a6b0a1 2148 execute_command (run.c_str (), 0 /* from_tty */ );
fb40c209
AC
2149}
2150
2151void
fb40c209
AC
2152mi_load_progress (const char *section_name,
2153 unsigned long sent_so_far,
2154 unsigned long total_section,
2155 unsigned long total_sent,
2156 unsigned long grand_total)
2157{
dcb07cfa
PA
2158 using namespace std::chrono;
2159 static steady_clock::time_point last_update;
fb40c209
AC
2160 static char *previous_sect_name = NULL;
2161 int new_section;
d6f9b0fb 2162 struct mi_interp *mi = (struct mi_interp *) current_interpreter ();
fb40c209 2163
0be75e02
AS
2164 /* This function is called through deprecated_show_load_progress
2165 which means uiout may not be correct. Fix it for the duration
2166 of this function. */
0be75e02 2167
8e5e5494
SM
2168 std::unique_ptr<ui_out> uiout (mi_out_new (current_interpreter ()->name ()));
2169 if (uiout == nullptr)
fb40c209
AC
2170 return;
2171
5846367a
SM
2172 scoped_restore save_uiout
2173 = make_scoped_restore (&current_uiout, uiout.get ());
79a45e25 2174
fb40c209
AC
2175 new_section = (previous_sect_name ?
2176 strcmp (previous_sect_name, section_name) : 1);
2177 if (new_section)
2178 {
b8c9b27d 2179 xfree (previous_sect_name);
fb40c209
AC
2180 previous_sect_name = xstrdup (section_name);
2181
721c02de 2182 if (current_token)
0426ad51
TT
2183 gdb_puts (current_token, mi->raw_stdout);
2184 gdb_puts ("+download", mi->raw_stdout);
2e783024 2185 {
bd77e8ff 2186 ui_out_emit_tuple tuple_emitter (uiout.get (), NULL);
2e783024 2187 uiout->field_string ("section", section_name);
381befee
TT
2188 uiout->field_signed ("section-size", total_section);
2189 uiout->field_signed ("total-size", grand_total);
2e783024 2190 }
bd77e8ff 2191 mi_out_put (uiout.get (), mi->raw_stdout);
0426ad51 2192 gdb_puts ("\n", mi->raw_stdout);
9204d692 2193 gdb_flush (mi->raw_stdout);
fb40c209
AC
2194 }
2195
dcb07cfa
PA
2196 steady_clock::time_point time_now = steady_clock::now ();
2197 if (time_now - last_update > milliseconds (500))
fb40c209 2198 {
dcb07cfa 2199 last_update = time_now;
721c02de 2200 if (current_token)
0426ad51
TT
2201 gdb_puts (current_token, mi->raw_stdout);
2202 gdb_puts ("+download", mi->raw_stdout);
2e783024 2203 {
bd77e8ff 2204 ui_out_emit_tuple tuple_emitter (uiout.get (), NULL);
2e783024 2205 uiout->field_string ("section", section_name);
381befee
TT
2206 uiout->field_signed ("section-sent", sent_so_far);
2207 uiout->field_signed ("section-size", total_section);
2208 uiout->field_signed ("total-sent", total_sent);
2209 uiout->field_signed ("total-size", grand_total);
2e783024 2210 }
bd77e8ff 2211 mi_out_put (uiout.get (), mi->raw_stdout);
0426ad51 2212 gdb_puts ("\n", mi->raw_stdout);
9204d692 2213 gdb_flush (mi->raw_stdout);
fb40c209
AC
2214 }
2215}
2216
c1244769 2217static void
d8c83789 2218timestamp (struct mi_timestamp *tv)
2b03b41d 2219{
dcb07cfa 2220 using namespace std::chrono;
a1b7d198 2221
dcb07cfa
PA
2222 tv->wallclock = steady_clock::now ();
2223 run_time_clock::now (tv->utime, tv->stime);
2b03b41d 2224}
d8c83789 2225
c1244769 2226static void
9204d692 2227print_diff_now (struct ui_file *file, struct mi_timestamp *start)
2b03b41d
SS
2228{
2229 struct mi_timestamp now;
102040f0 2230
2b03b41d 2231 timestamp (&now);
9204d692 2232 print_diff (file, start, &now);
2b03b41d 2233}
d8c83789 2234
4333ada3 2235void
9204d692 2236mi_print_timing_maybe (struct ui_file *file)
4333ada3 2237{
2b03b41d
SS
2238 /* If the command is -enable-timing then do_timings may be true
2239 whilst current_command_ts is not initialized. */
4333ada3 2240 if (do_timings && current_command_ts)
9204d692 2241 print_diff_now (file, current_command_ts);
4333ada3
VP
2242}
2243
c1244769 2244static void
9204d692
PA
2245print_diff (struct ui_file *file, struct mi_timestamp *start,
2246 struct mi_timestamp *end)
2b03b41d 2247{
dcb07cfa
PA
2248 using namespace std::chrono;
2249
2250 duration<double> wallclock = end->wallclock - start->wallclock;
2251 duration<double> utime = end->utime - start->utime;
2252 duration<double> stime = end->stime - start->stime;
2253
6cb06a8c 2254 gdb_printf
9204d692 2255 (file,
c1244769 2256 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
dcb07cfa 2257 wallclock.count (), utime.count (), stime.count ());
2b03b41d 2258}
f224b49d 2259
40e1c229 2260void
9f33b8b7 2261mi_cmd_trace_define_variable (const char *command, char **argv, int argc)
40e1c229 2262{
40e1c229
VP
2263 LONGEST initval = 0;
2264 struct trace_state_variable *tsv;
2265 char *name = 0;
2266
2267 if (argc != 1 && argc != 2)
2268 error (_("Usage: -trace-define-variable VARIABLE [VALUE]"));
2269
1773c82c
HAQ
2270 name = argv[0];
2271 if (*name++ != '$')
2272 error (_("Name of trace variable should start with '$'"));
40e1c229 2273
1773c82c 2274 validate_trace_state_variable_name (name);
40e1c229
VP
2275
2276 tsv = find_trace_state_variable (name);
2277 if (!tsv)
2278 tsv = create_trace_state_variable (name);
2279
2280 if (argc == 2)
2281 initval = value_as_long (parse_and_eval (argv[1]));
2282
2283 tsv->initial_value = initval;
40e1c229
VP
2284}
2285
2286void
9f33b8b7 2287mi_cmd_trace_list_variables (const char *command, char **argv, int argc)
40e1c229
VP
2288{
2289 if (argc != 0)
2b03b41d 2290 error (_("-trace-list-variables: no arguments allowed"));
40e1c229
VP
2291
2292 tvariables_info_1 ();
2293}
2294
f197e0f1 2295void
9f33b8b7 2296mi_cmd_trace_find (const char *command, char **argv, int argc)
f197e0f1
VP
2297{
2298 char *mode;
2299
2300 if (argc == 0)
2301 error (_("trace selection mode is required"));
2302
2303 mode = argv[0];
2304
2305 if (strcmp (mode, "none") == 0)
2306 {
2307 tfind_1 (tfind_number, -1, 0, 0, 0);
2308 return;
2309 }
2310
cc3da688 2311 check_trace_running (current_trace_status ());
f197e0f1
VP
2312
2313 if (strcmp (mode, "frame-number") == 0)
2314 {
2315 if (argc != 2)
2316 error (_("frame number is required"));
2317 tfind_1 (tfind_number, atoi (argv[1]), 0, 0, 0);
2318 }
2319 else if (strcmp (mode, "tracepoint-number") == 0)
2320 {
2321 if (argc != 2)
2322 error (_("tracepoint number is required"));
2323 tfind_1 (tfind_tp, atoi (argv[1]), 0, 0, 0);
2324 }
2325 else if (strcmp (mode, "pc") == 0)
2326 {
2327 if (argc != 2)
2328 error (_("PC is required"));
2329 tfind_1 (tfind_pc, 0, parse_and_eval_address (argv[1]), 0, 0);
2330 }
2331 else if (strcmp (mode, "pc-inside-range") == 0)
2332 {
2333 if (argc != 3)
2334 error (_("Start and end PC are required"));
2335 tfind_1 (tfind_range, 0, parse_and_eval_address (argv[1]),
2336 parse_and_eval_address (argv[2]), 0);
2337 }
2338 else if (strcmp (mode, "pc-outside-range") == 0)
2339 {
2340 if (argc != 3)
2341 error (_("Start and end PC are required"));
2342 tfind_1 (tfind_outside, 0, parse_and_eval_address (argv[1]),
2343 parse_and_eval_address (argv[2]), 0);
2344 }
2345 else if (strcmp (mode, "line") == 0)
2346 {
f197e0f1
VP
2347 if (argc != 2)
2348 error (_("Line is required"));
2349
6c5b2ebe
PA
2350 std::vector<symtab_and_line> sals
2351 = decode_line_with_current_source (argv[1],
2352 DECODE_LINE_FUNFIRSTLINE);
2353 const symtab_and_line &sal = sals[0];
f197e0f1
VP
2354
2355 if (sal.symtab == 0)
2356 error (_("Could not find the specified line"));
2357
6c5b2ebe 2358 CORE_ADDR start_pc, end_pc;
f197e0f1
VP
2359 if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
2360 tfind_1 (tfind_range, 0, start_pc, end_pc - 1, 0);
2361 else
2362 error (_("Could not find the specified line"));
f197e0f1
VP
2363 }
2364 else
2365 error (_("Invalid mode '%s'"), mode);
2366
2367 if (has_stack_frames () || get_traceframe_number () >= 0)
08d72866 2368 print_stack_frame (get_selected_frame (NULL), 1, LOC_AND_ADDRESS, 1);
f197e0f1
VP
2369}
2370
011aacb0 2371void
9f33b8b7 2372mi_cmd_trace_save (const char *command, char **argv, int argc)
011aacb0
VP
2373{
2374 int target_saves = 0;
d0353e76 2375 int generate_ctf = 0;
011aacb0 2376 char *filename;
d0353e76
YQ
2377 int oind = 0;
2378 char *oarg;
011aacb0 2379
d0353e76
YQ
2380 enum opt
2381 {
2382 TARGET_SAVE_OPT, CTF_OPT
2383 };
2384 static const struct mi_opt opts[] =
011aacb0 2385 {
d0353e76
YQ
2386 {"r", TARGET_SAVE_OPT, 0},
2387 {"ctf", CTF_OPT, 0},
2388 { 0, 0, 0 }
2389 };
2390
2391 while (1)
011aacb0 2392 {
d0353e76
YQ
2393 int opt = mi_getopt ("-trace-save", argc, argv, opts,
2394 &oind, &oarg);
2395
2396 if (opt < 0)
2397 break;
2398 switch ((enum opt) opt)
2399 {
2400 case TARGET_SAVE_OPT:
2401 target_saves = 1;
2402 break;
2403 case CTF_OPT:
2404 generate_ctf = 1;
2405 break;
2406 }
011aacb0 2407 }
5bad3170
SM
2408
2409 if (argc - oind != 1)
2410 error (_("Exactly one argument required "
2411 "(file in which to save trace data)"));
2412
d0353e76 2413 filename = argv[oind];
011aacb0 2414
d0353e76
YQ
2415 if (generate_ctf)
2416 trace_save_ctf (filename, target_saves);
2417 else
2418 trace_save_tfile (filename, target_saves);
011aacb0
VP
2419}
2420
f224b49d 2421void
9f33b8b7 2422mi_cmd_trace_start (const char *command, char **argv, int argc)
f224b49d 2423{
f196051f 2424 start_tracing (NULL);
f224b49d
VP
2425}
2426
2427void
9f33b8b7 2428mi_cmd_trace_status (const char *command, char **argv, int argc)
f224b49d
VP
2429{
2430 trace_status_mi (0);
2431}
2432
2433void
9f33b8b7 2434mi_cmd_trace_stop (const char *command, char **argv, int argc)
f224b49d 2435{
f196051f 2436 stop_tracing (NULL);
f224b49d
VP
2437 trace_status_mi (1);
2438}
75082e8c 2439
2b03b41d 2440/* Implement the "-ada-task-info" command. */
75082e8c
JB
2441
2442void
9f33b8b7 2443mi_cmd_ada_task_info (const char *command, char **argv, int argc)
75082e8c
JB
2444{
2445 if (argc != 0 && argc != 1)
2446 error (_("Invalid MI command"));
2447
2448 print_ada_task_info (current_uiout, argv[0], current_inferior ());
2449}
dc673c81
YQ
2450
2451/* Print EXPRESSION according to VALUES. */
2452
2453static void
1f45808e 2454print_variable_or_computed (const char *expression, enum print_values values)
dc673c81 2455{
dc673c81 2456 struct value *val;
dc673c81
YQ
2457 struct type *type;
2458 struct ui_out *uiout = current_uiout;
2459
d7e74731 2460 string_file stb;
dc673c81 2461
4d01a485 2462 expression_up expr = parse_expression (expression);
dc673c81
YQ
2463
2464 if (values == PRINT_SIMPLE_VALUES)
4d01a485 2465 val = evaluate_type (expr.get ());
dc673c81 2466 else
4d01a485 2467 val = evaluate_expression (expr.get ());
dc673c81 2468
0092b74d 2469 gdb::optional<ui_out_emit_tuple> tuple_emitter;
dc673c81 2470 if (values != PRINT_NO_VALUES)
0092b74d 2471 tuple_emitter.emplace (uiout, nullptr);
112e8700 2472 uiout->field_string ("name", expression);
dc673c81
YQ
2473
2474 switch (values)
2475 {
2476 case PRINT_SIMPLE_VALUES:
d0c97917
TT
2477 type = check_typedef (val->type ());
2478 type_print (val->type (), "", &stb, -1);
112e8700 2479 uiout->field_stream ("type", stb);
78134374
SM
2480 if (type->code () != TYPE_CODE_ARRAY
2481 && type->code () != TYPE_CODE_STRUCT
2482 && type->code () != TYPE_CODE_UNION)
dc673c81
YQ
2483 {
2484 struct value_print_options opts;
2485
2a998fc0 2486 get_no_prettyformat_print_options (&opts);
dad6b350 2487 opts.deref_ref = true;
d7e74731 2488 common_val_print (val, &stb, 0, &opts, current_language);
112e8700 2489 uiout->field_stream ("value", stb);
dc673c81
YQ
2490 }
2491 break;
2492 case PRINT_ALL_VALUES:
2493 {
2494 struct value_print_options opts;
2495
2a998fc0 2496 get_no_prettyformat_print_options (&opts);
dad6b350 2497 opts.deref_ref = true;
d7e74731 2498 common_val_print (val, &stb, 0, &opts, current_language);
112e8700 2499 uiout->field_stream ("value", stb);
dc673c81
YQ
2500 }
2501 break;
2502 }
dc673c81
YQ
2503}
2504
2505/* Implement the "-trace-frame-collected" command. */
2506
2507void
9f33b8b7 2508mi_cmd_trace_frame_collected (const char *command, char **argv, int argc)
dc673c81 2509{
dc673c81
YQ
2510 struct bp_location *tloc;
2511 int stepping_frame;
2512 struct collection_list *clist;
2513 struct collection_list tracepoint_list, stepping_list;
2514 struct traceframe_info *tinfo;
2515 int oind = 0;
f486487f
SM
2516 enum print_values var_print_values = PRINT_ALL_VALUES;
2517 enum print_values comp_print_values = PRINT_ALL_VALUES;
dc673c81
YQ
2518 int registers_format = 'x';
2519 int memory_contents = 0;
2520 struct ui_out *uiout = current_uiout;
2521 enum opt
2522 {
2523 VAR_PRINT_VALUES,
2524 COMP_PRINT_VALUES,
2525 REGISTERS_FORMAT,
2526 MEMORY_CONTENTS,
2527 };
2528 static const struct mi_opt opts[] =
2529 {
2530 {"-var-print-values", VAR_PRINT_VALUES, 1},
2531 {"-comp-print-values", COMP_PRINT_VALUES, 1},
2532 {"-registers-format", REGISTERS_FORMAT, 1},
2533 {"-memory-contents", MEMORY_CONTENTS, 0},
2534 { 0, 0, 0 }
2535 };
2536
2537 while (1)
2538 {
2539 char *oarg;
2540 int opt = mi_getopt ("-trace-frame-collected", argc, argv, opts,
2541 &oind, &oarg);
2542 if (opt < 0)
2543 break;
2544 switch ((enum opt) opt)
2545 {
2546 case VAR_PRINT_VALUES:
2547 var_print_values = mi_parse_print_values (oarg);
2548 break;
2549 case COMP_PRINT_VALUES:
2550 comp_print_values = mi_parse_print_values (oarg);
2551 break;
2552 case REGISTERS_FORMAT:
2553 registers_format = oarg[0];
15c9ffd6 2554 break;
dc673c81
YQ
2555 case MEMORY_CONTENTS:
2556 memory_contents = 1;
2557 break;
2558 }
2559 }
2560
2561 if (oind != argc)
2562 error (_("Usage: -trace-frame-collected "
2563 "[--var-print-values PRINT_VALUES] "
2564 "[--comp-print-values PRINT_VALUES] "
2565 "[--registers-format FORMAT]"
2566 "[--memory-contents]"));
2567
2568 /* This throws an error is not inspecting a trace frame. */
2569 tloc = get_traceframe_location (&stepping_frame);
2570
2571 /* This command only makes sense for the current frame, not the
2572 selected frame. */
5ed8105e 2573 scoped_restore_current_thread restore_thread;
dc673c81
YQ
2574 select_frame (get_current_frame ());
2575
1f45808e 2576 encode_actions (tloc, &tracepoint_list, &stepping_list);
dc673c81
YQ
2577
2578 if (stepping_frame)
2579 clist = &stepping_list;
2580 else
2581 clist = &tracepoint_list;
2582
2583 tinfo = get_traceframe_info ();
2584
2585 /* Explicitly wholly collected variables. */
2586 {
10f489e5 2587 ui_out_emit_list list_emitter (uiout, "explicit-variables");
1f45808e
PA
2588 const std::vector<std::string> &wholly_collected
2589 = clist->wholly_collected ();
2590 for (size_t i = 0; i < wholly_collected.size (); i++)
2591 {
2592 const std::string &str = wholly_collected[i];
2593 print_variable_or_computed (str.c_str (), var_print_values);
2594 }
dc673c81
YQ
2595 }
2596
2597 /* Computed expressions. */
2598 {
10f489e5 2599 ui_out_emit_list list_emitter (uiout, "computed-expressions");
1f45808e
PA
2600
2601 const std::vector<std::string> &computed = clist->computed ();
2602 for (size_t i = 0; i < computed.size (); i++)
2603 {
2604 const std::string &str = computed[i];
2605 print_variable_or_computed (str.c_str (), comp_print_values);
2606 }
dc673c81
YQ
2607 }
2608
2609 /* Registers. Given pseudo-registers, and that some architectures
2610 (like MIPS) actually hide the raw registers, we don't go through
2611 the trace frame info, but instead consult the register cache for
2612 register availability. */
2613 {
bd2b40ac 2614 frame_info_ptr frame;
dc673c81
YQ
2615 struct gdbarch *gdbarch;
2616 int regnum;
2617 int numregs;
2618
10f489e5 2619 ui_out_emit_list list_emitter (uiout, "registers");
dc673c81
YQ
2620
2621 frame = get_selected_frame (NULL);
2622 gdbarch = get_frame_arch (frame);
f6efe3f8 2623 numregs = gdbarch_num_cooked_regs (gdbarch);
dc673c81
YQ
2624
2625 for (regnum = 0; regnum < numregs; regnum++)
2626 {
637b2f86 2627 if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
dc673c81
YQ
2628 continue;
2629
2630 output_register (frame, regnum, registers_format, 1);
2631 }
dc673c81
YQ
2632 }
2633
2634 /* Trace state variables. */
2635 {
e6a2252a 2636 ui_out_emit_list list_emitter (uiout, "tvars");
dc673c81 2637
d0d292a2 2638 for (int tvar : tinfo->tvars)
dc673c81 2639 {
dc673c81
YQ
2640 struct trace_state_variable *tsv;
2641
2642 tsv = find_trace_state_variable_by_number (tvar);
2643
2e783024 2644 ui_out_emit_tuple tuple_emitter (uiout, NULL);
dc673c81
YQ
2645
2646 if (tsv != NULL)
2647 {
c252925c 2648 uiout->field_fmt ("name", "$%s", tsv->name.c_str ());
dc673c81
YQ
2649
2650 tsv->value_known = target_get_trace_state_variable_value (tsv->number,
2651 &tsv->value);
381befee 2652 uiout->field_signed ("current", tsv->value);
dc673c81
YQ
2653 }
2654 else
2655 {
112e8700
SM
2656 uiout->field_skip ("name");
2657 uiout->field_skip ("current");
dc673c81 2658 }
dc673c81 2659 }
dc673c81
YQ
2660 }
2661
2662 /* Memory. */
2663 {
a79b1bc6 2664 std::vector<mem_range> available_memory;
dc673c81
YQ
2665
2666 traceframe_available_memory (&available_memory, 0, ULONGEST_MAX);
dc673c81 2667
e6a2252a 2668 ui_out_emit_list list_emitter (uiout, "memory");
dc673c81 2669
a79b1bc6 2670 for (const mem_range &r : available_memory)
dc673c81 2671 {
dc673c81
YQ
2672 struct gdbarch *gdbarch = target_gdbarch ();
2673
76f9c9cf 2674 ui_out_emit_tuple tuple_emitter (uiout, NULL);
dc673c81 2675
a79b1bc6 2676 uiout->field_core_addr ("address", gdbarch, r.start);
381befee 2677 uiout->field_signed ("length", r.length);
dc673c81 2678
a79b1bc6 2679 gdb::byte_vector data (r.length);
dc673c81
YQ
2680
2681 if (memory_contents)
2682 {
a79b1bc6 2683 if (target_read_memory (r.start, data.data (), r.length) == 0)
dc673c81 2684 {
a79b1bc6 2685 std::string data_str = bin2hex (data.data (), r.length);
8dd8c8d4 2686 uiout->field_string ("contents", data_str);
dc673c81
YQ
2687 }
2688 else
112e8700 2689 uiout->field_skip ("contents");
dc673c81 2690 }
dc673c81 2691 }
dc673c81 2692 }
dc673c81 2693}
329ea579 2694
b4be1b06
SM
2695/* See mi/mi-main.h. */
2696
2697void
2698mi_cmd_fix_multi_location_breakpoint_output (const char *command, char **argv,
2699 int argc)
2700{
13674803 2701 fix_multi_location_breakpoint_output_globally = true;
b4be1b06
SM
2702}
2703
9db0d853
SM
2704/* See mi/mi-main.h. */
2705
2706void
2707mi_cmd_fix_breakpoint_script_output (const char *command, char **argv, int argc)
2708{
2709 fix_breakpoint_script_output_globally = true;
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}