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