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