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