Teach -data-list-register-values to not include unavailable registers
[deliverable/binutils-gdb.git] / gdb / mi / mi-main.c
... / ...
CommitLineData
1/* MI Command Set.
2
3 Copyright (C) 2000-2013 Free Software Foundation, Inc.
4
5 Contributed by Cygnus Solutions (a Red Hat company).
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
11 the Free Software Foundation; either version 3 of the License, or
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
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22#include "defs.h"
23#include "arch-utils.h"
24#include "target.h"
25#include "inferior.h"
26#include "gdb_string.h"
27#include "exceptions.h"
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"
36#include "interps.h"
37#include "event-loop.h"
38#include "event-top.h"
39#include "gdbcore.h" /* For write_memory(). */
40#include "value.h"
41#include "regcache.h"
42#include "gdb.h"
43#include "frame.h"
44#include "mi-main.h"
45#include "mi-common.h"
46#include "language.h"
47#include "valprint.h"
48#include "inferior.h"
49#include "osdata.h"
50#include "splay-tree.h"
51#include "tracepoint.h"
52#include "ctf.h"
53#include "ada-lang.h"
54#include "linespec.h"
55#ifdef HAVE_PYTHON
56#include "python/python-internal.h"
57#endif
58
59#include <ctype.h>
60#include <sys/time.h>
61
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
70enum
71 {
72 FROM_TTY = 0
73 };
74
75int mi_debug_p;
76
77struct ui_file *raw_stdout;
78
79/* This is used to pass the current command timestamp down to
80 continuation routines. */
81static struct mi_timestamp *current_command_ts;
82
83static int do_timings = 0;
84
85char *current_token;
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. */
89static struct mi_parse *current_context;
90
91int running_result_record_printed = 1;
92
93/* Flag indicating that the target has proceeded since the last
94 command was issued. */
95int mi_proceeded;
96
97extern void _initialize_mi_main (void);
98static void mi_cmd_execute (struct mi_parse *parse);
99
100static void mi_execute_cli_command (const char *cmd, int args_p,
101 const char *args);
102static void mi_execute_async_cli_command (char *cli_command,
103 char **argv, int argc);
104static int register_changed_p (int regnum, struct regcache *,
105 struct regcache *);
106static void output_register (struct frame_info *, int regnum, int format,
107 int skip_unavailable);
108
109/* Command implementations. FIXME: Is this libgdb? No. This is the MI
110 layer that calls libgdb. Any operation used in the below should be
111 formalized. */
112
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
118void
119mi_cmd_gdb_exit (char *command, char **argv, int argc)
120{
121 /* We have to print everything right here because we never return. */
122 if (current_token)
123 fputs_unfiltered (current_token, raw_stdout);
124 fputs_unfiltered ("^exit\n", raw_stdout);
125 mi_out_put (current_uiout, raw_stdout);
126 gdb_flush (raw_stdout);
127 /* FIXME: The function called is not yet a formal libgdb function. */
128 quit_force (NULL, FROM_TTY);
129}
130
131void
132mi_cmd_exec_next (char *command, char **argv, int argc)
133{
134 /* FIXME: Should call a libgdb function, not a cli wrapper. */
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);
139}
140
141void
142mi_cmd_exec_next_instruction (char *command, char **argv, int argc)
143{
144 /* FIXME: Should call a libgdb function, not a cli wrapper. */
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);
149}
150
151void
152mi_cmd_exec_step (char *command, char **argv, int argc)
153{
154 /* FIXME: Should call a libgdb function, not a cli wrapper. */
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);
159}
160
161void
162mi_cmd_exec_step_instruction (char *command, char **argv, int argc)
163{
164 /* FIXME: Should call a libgdb function, not a cli wrapper. */
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);
169}
170
171void
172mi_cmd_exec_finish (char *command, char **argv, int argc)
173{
174 /* FIXME: Should call a libgdb function, not a cli wrapper. */
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);
179}
180
181void
182mi_cmd_exec_return (char *command, char **argv, int argc)
183{
184 /* This command doesn't really execute the target, it just pops the
185 specified number of frames. */
186 if (argc)
187 /* Call return_command with from_tty argument equal to 0 so as to
188 avoid being queried. */
189 return_command (*argv, 0);
190 else
191 /* Call return_command with from_tty argument equal to 0 so as to
192 avoid being queried. */
193 return_command (NULL, 0);
194
195 /* Because we have called return_command with from_tty = 0, we need
196 to print the frame here. */
197 print_stack_frame (get_selected_frame (NULL), 1, LOC_AND_ADDRESS);
198}
199
200void
201mi_cmd_exec_jump (char *args, char **argv, int argc)
202{
203 /* FIXME: Should call a libgdb function, not a cli wrapper. */
204 mi_execute_async_cli_command ("jump", argv, argc);
205}
206
207static void
208proceed_thread (struct thread_info *thread, int pid)
209{
210 if (!is_stopped (thread->ptid))
211 return;
212
213 if (pid != 0 && PIDGET (thread->ptid) != pid)
214 return;
215
216 switch_to_thread (thread->ptid);
217 clear_proceed_status ();
218 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT, 0);
219}
220
221static int
222proceed_thread_callback (struct thread_info *thread, void *arg)
223{
224 int pid = *(int *)arg;
225
226 proceed_thread (thread, pid);
227 return 0;
228}
229
230static void
231exec_continue (char **argv, int argc)
232{
233 if (non_stop)
234 {
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.
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 ();
245
246 if (!current_context->all)
247 {
248 struct inferior *inf
249 = find_inferior_id (current_context->thread_group);
250
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 }
260 }
261 else
262 {
263 struct cleanup *back_to = make_cleanup_restore_integer (&sched_multi);
264
265 if (current_context->all)
266 {
267 sched_multi = 1;
268 continue_1 (0);
269 }
270 else
271 {
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. */
276 continue_1 (1);
277 }
278 do_cleanups (back_to);
279 }
280}
281
282static void
283exec_direction_forward (void *notused)
284{
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
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
300 old_chain = make_cleanup (exec_direction_forward, NULL);
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{
309 if (argc > 0 && strcmp (argv[0], "--reverse") == 0)
310 exec_reverse_continue (argv + 1, argc - 1);
311 else
312 exec_continue (argv, argc);
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;
328}
329
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
334 mi_cmd_execute. */
335
336void
337mi_cmd_exec_interrupt (char *command, char **argv, int argc)
338{
339 /* In all-stop mode, everything stops, so we don't need to try
340 anything specific. */
341 if (!non_stop)
342 {
343 interrupt_target_1 (0);
344 return;
345 }
346
347 if (current_context->all)
348 {
349 /* This will interrupt all threads in all inferiors. */
350 interrupt_target_1 (1);
351 }
352 else if (current_context->thread_group != -1)
353 {
354 struct inferior *inf = find_inferior_id (current_context->thread_group);
355
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{
370 if (inf->pid != 0)
371 {
372 if (inf->pid != ptid_get_pid (inferior_ptid))
373 {
374 struct thread_info *tp;
375
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 }
382 }
383 else
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;
392}
393
394void
395mi_cmd_exec_run (char *command, char **argv, int argc)
396{
397 if (current_context->all)
398 {
399 struct cleanup *back_to = save_current_space_and_thread ();
400
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 }
409}
410
411
412static int
413find_thread_of_process (struct thread_info *ti, void *p)
414{
415 int pid = *(int *)p;
416
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)
427 error (_("Usage: -target-detach [pid | thread-group]"));
428
429 if (argc == 1)
430 {
431 struct thread_info *tp;
432 char *end = argv[0];
433 int pid;
434
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 }
458
459 /* Pick any thread in the desired process. Current
460 target_detach detaches from the parent of inferior_ptid. */
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
471void
472mi_cmd_thread_select (char *command, char **argv, int argc)
473{
474 enum gdb_rc rc;
475 char *mi_error_message;
476
477 if (argc != 1)
478 error (_("-thread-select: USAGE: threadnum."));
479
480 rc = gdb_thread_select (current_uiout, argv[0], &mi_error_message);
481
482 if (rc == GDB_RC_FAIL)
483 {
484 make_cleanup (xfree, mi_error_message);
485 error ("%s", mi_error_message);
486 }
487}
488
489void
490mi_cmd_thread_list_ids (char *command, char **argv, int argc)
491{
492 enum gdb_rc rc;
493 char *mi_error_message;
494
495 if (argc != 0)
496 error (_("-thread-list-ids: No arguments required."));
497
498 rc = gdb_list_thread_ids (current_uiout, &mi_error_message);
499
500 if (rc == GDB_RC_FAIL)
501 {
502 make_cleanup (xfree, mi_error_message);
503 error ("%s", mi_error_message);
504 }
505}
506
507void
508mi_cmd_thread_info (char *command, char **argv, int argc)
509{
510 if (argc != 0 && argc != 1)
511 error (_("Invalid MI command"));
512
513 print_thread_info (current_uiout, argv[0], -1);
514}
515
516DEF_VEC_I(int);
517
518struct collect_cores_data
519{
520 int pid;
521
522 VEC (int) *cores;
523};
524
525static int
526collect_cores (struct thread_info *ti, void *xdata)
527{
528 struct collect_cores_data *data = xdata;
529
530 if (ptid_get_pid (ti->ptid) == data->pid)
531 {
532 int core = target_core_of_thread (ti->ptid);
533
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;
545
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;
562 struct ui_out *uiout = current_uiout;
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;
570 struct cleanup *back_to
571 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
572
573 ui_out_field_fmt (uiout, "id", "i%d", inferior->num);
574 ui_out_field_string (uiout, "type", "process");
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 }
583
584 data.cores = 0;
585 if (inferior->pid != 0)
586 {
587 data.pid = inferior->pid;
588 iterate_over_threads (collect_cores, &data);
589 }
590
591 if (!VEC_empty (int, data.cores))
592 {
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)
612 print_thread_info (uiout, NULL, inferior->pid);
613
614 do_cleanups (back_to);
615 }
616
617 return 0;
618}
619
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. */
623
624static void
625output_cores (struct ui_out *uiout, const char *field_name, const char *xcores)
626{
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;
631
632 make_cleanup (xfree, cores);
633
634 for (p = strtok (p, ","); p; p = strtok (NULL, ","))
635 ui_out_field_string (uiout, NULL, p);
636
637 do_cleanups (back_to);
638}
639
640static void
641free_vector_of_ints (void *xvector)
642{
643 VEC (int) **vector = xvector;
644
645 VEC_free (int, *vector);
646}
647
648static void
649do_nothing (splay_tree_key k)
650{
651}
652
653static void
654free_vector_of_osdata_items (splay_tree_value xvalue)
655{
656 VEC (osdata_item_s) *value = (VEC (osdata_item_s) *) xvalue;
657
658 /* We don't free the items itself, it will be done separately. */
659 VEC_free (osdata_item_s, value);
660}
661
662static int
663splay_tree_int_comparator (splay_tree_key xa, splay_tree_key xb)
664{
665 int a = xa;
666 int b = xb;
667
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;
684 struct ui_out *uiout = current_uiout;
685 struct cleanup *cleanup;
686
687 /* This keeps a map from integer (pid) to VEC (struct osdata_item *)*
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;
692
693 /* get_osdata will throw if it cannot return data. */
694 data = get_osdata ("processes");
695 cleanup = make_cleanup_osdata_free (data);
696
697 if (recurse)
698 {
699 struct osdata *threads = get_osdata ("threads");
700
701 make_cleanup_osdata_free (threads);
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);
706
707 for (ix_items = 0;
708 VEC_iterate (osdata_item_s, threads->items,
709 ix_items, item);
710 ix_items++)
711 {
712 const char *pid = get_osdata_column (item, "pid");
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");
732
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;
739
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);
784 const char *tid = get_osdata_column (child, "tid");
785 const char *tcore = get_osdata_column (child, "core");
786
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 }
794 }
795
796 do_cleanups (back_to);
797 }
798
799 do_cleanups (cleanup);
800}
801
802void
803mi_cmd_list_thread_groups (char *command, char **argv, int argc)
804{
805 struct ui_out *uiout = current_uiout;
806 struct cleanup *back_to;
807 int available = 0;
808 int recurse = 0;
809 VEC (int) *ids = 0;
810
811 enum opt
812 {
813 AVAILABLE_OPT, RECURSE_OPT
814 };
815 static const struct mi_opt opts[] =
816 {
817 {"-available", AVAILABLE_OPT, 0},
818 {"-recurse", RECURSE_OPT, 1},
819 { 0, 0, 0 }
820 };
821
822 int oind = 0;
823 char *oarg;
824
825 while (1)
826 {
827 int opt = mi_getopt ("-list-thread-groups", argc, argv, opts,
828 &oind, &oarg);
829
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:
838 if (strcmp (oarg, "0") == 0)
839 ;
840 else if (strcmp (oarg, "1") == 0)
841 recurse = 1;
842 else
843 error (_("only '0' and '1' are valid values "
844 "for the '--recurse' option"));
845 break;
846 }
847 }
848
849 for (; oind < argc; ++oind)
850 {
851 char *end;
852 int inf;
853
854 if (*(argv[oind]) != 'i')
855 error (_("invalid syntax of group id '%s'"), argv[oind]);
856
857 inf = strtoul (argv[oind] + 1, &end, 0);
858
859 if (*end != '\0')
860 error (_("invalid syntax of group id '%s'"), argv[oind]);
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)
875 {
876 /* Local thread groups, single id. */
877 int id = *VEC_address (int, ids);
878 struct inferior *inf = find_inferior_id (id);
879
880 if (!inf)
881 error (_("Non-existent thread group id '%d'"), id);
882
883 print_thread_info (uiout, NULL, inf->pid);
884 }
885 else
886 {
887 struct print_one_inferior_data data;
888
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. */
896 make_cleanup_ui_out_list_begin_end (uiout, "groups");
897 update_thread_list ();
898 iterate_over_inferiors (print_one_inferior, &data);
899 }
900
901 do_cleanups (back_to);
902}
903
904void
905mi_cmd_data_list_register_names (char *command, char **argv, int argc)
906{
907 struct gdbarch *gdbarch;
908 struct ui_out *uiout = current_uiout;
909 int regnum, numregs;
910 int i;
911 struct cleanup *cleanup;
912
913 /* Note that the test for a valid register must include checking the
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. */
919
920 gdbarch = get_current_arch ();
921 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
922
923 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names");
924
925 if (argc == 0) /* No args, just do all the regs. */
926 {
927 for (regnum = 0;
928 regnum < numregs;
929 regnum++)
930 {
931 if (gdbarch_register_name (gdbarch, regnum) == NULL
932 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
933 ui_out_field_string (uiout, NULL, "");
934 else
935 ui_out_field_string (uiout, NULL,
936 gdbarch_register_name (gdbarch, regnum));
937 }
938 }
939
940 /* Else, list of register #s, just do listed regs. */
941 for (i = 0; i < argc; i++)
942 {
943 regnum = atoi (argv[i]);
944 if (regnum < 0 || regnum >= numregs)
945 error (_("bad register number"));
946
947 if (gdbarch_register_name (gdbarch, regnum) == NULL
948 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
949 ui_out_field_string (uiout, NULL, "");
950 else
951 ui_out_field_string (uiout, NULL,
952 gdbarch_register_name (gdbarch, regnum));
953 }
954 do_cleanups (cleanup);
955}
956
957void
958mi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
959{
960 static struct regcache *this_regs = NULL;
961 struct ui_out *uiout = current_uiout;
962 struct regcache *prev_regs;
963 struct gdbarch *gdbarch;
964 int regnum, numregs, changed;
965 int i;
966 struct cleanup *cleanup;
967
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. */
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
977 /* Note that the test for a valid register must include checking the
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. */
983
984 gdbarch = get_regcache_arch (this_regs);
985 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
986
987 make_cleanup_ui_out_list_begin_end (uiout, "changed-registers");
988
989 if (argc == 0)
990 {
991 /* No args, just do all the regs. */
992 for (regnum = 0;
993 regnum < numregs;
994 regnum++)
995 {
996 if (gdbarch_register_name (gdbarch, regnum) == NULL
997 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
998 continue;
999 changed = register_changed_p (regnum, prev_regs, this_regs);
1000 if (changed < 0)
1001 error (_("-data-list-changed-registers: "
1002 "Unable to read register contents."));
1003 else if (changed)
1004 ui_out_field_int (uiout, NULL, regnum);
1005 }
1006 }
1007
1008 /* Else, list of register #s, just do listed regs. */
1009 for (i = 0; i < argc; i++)
1010 {
1011 regnum = atoi (argv[i]);
1012
1013 if (regnum >= 0
1014 && regnum < numregs
1015 && gdbarch_register_name (gdbarch, regnum) != NULL
1016 && *gdbarch_register_name (gdbarch, regnum) != '\000')
1017 {
1018 changed = register_changed_p (regnum, prev_regs, this_regs);
1019 if (changed < 0)
1020 error (_("-data-list-changed-registers: "
1021 "Unable to read register contents."));
1022 else if (changed)
1023 ui_out_field_int (uiout, NULL, regnum);
1024 }
1025 else
1026 error (_("bad register number"));
1027 }
1028 do_cleanups (cleanup);
1029}
1030
1031static int
1032register_changed_p (int regnum, struct regcache *prev_regs,
1033 struct regcache *this_regs)
1034{
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];
1038 enum register_status prev_status;
1039 enum register_status this_status;
1040
1041 /* First time through or after gdbarch change consider all registers
1042 as changed. */
1043 if (!prev_regs || get_regcache_arch (prev_regs) != gdbarch)
1044 return 1;
1045
1046 /* Get register contents and compare. */
1047 prev_status = regcache_cooked_read (prev_regs, regnum, prev_buffer);
1048 this_status = regcache_cooked_read (this_regs, regnum, this_buffer);
1049
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;
1057}
1058
1059/* Return a list of register number and value pairs. The valid
1060 arguments expected are: a letter indicating the format in which to
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
1068void
1069mi_cmd_data_list_register_values (char *command, char **argv, int argc)
1070{
1071 struct ui_out *uiout = current_uiout;
1072 struct frame_info *frame;
1073 struct gdbarch *gdbarch;
1074 int regnum, numregs, format;
1075 int i;
1076 struct cleanup *list_cleanup;
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 };
1088
1089 /* Note that the test for a valid register must include checking the
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. */
1095
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)
1113 error (_("-data-list-register-values: Usage: "
1114 "-data-list-register-values [--skip-unavailable] <format>"
1115 " [<regnum1>...<regnumN>]"));
1116
1117 format = (int) argv[oind][0];
1118
1119 frame = get_selected_frame (NULL);
1120 gdbarch = get_frame_arch (frame);
1121 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1122
1123 list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values");
1124
1125 if (argc - oind == 1)
1126 {
1127 /* No args, beside the format: do all the regs. */
1128 for (regnum = 0;
1129 regnum < numregs;
1130 regnum++)
1131 {
1132 if (gdbarch_register_name (gdbarch, regnum) == NULL
1133 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
1134 continue;
1135
1136 output_register (frame, regnum, format, skip_unavailable);
1137 }
1138 }
1139
1140 /* Else, list of register #s, just do listed regs. */
1141 for (i = 1 + oind; i < argc; i++)
1142 {
1143 regnum = atoi (argv[i]);
1144
1145 if (regnum >= 0
1146 && regnum < numregs
1147 && gdbarch_register_name (gdbarch, regnum) != NULL
1148 && *gdbarch_register_name (gdbarch, regnum) != '\000')
1149 output_register (frame, regnum, format, skip_unavailable);
1150 else
1151 error (_("bad register number"));
1152 }
1153 do_cleanups (list_cleanup);
1154}
1155
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. */
1159
1160static void
1161output_register (struct frame_info *frame, int regnum, int format,
1162 int skip_unavailable)
1163{
1164 struct gdbarch *gdbarch = get_frame_arch (frame);
1165 struct ui_out *uiout = current_uiout;
1166 struct value *val = get_frame_register_value (frame, regnum);
1167 struct cleanup *tuple_cleanup;
1168
1169 if (skip_unavailable && !value_entirely_available (val))
1170 return;
1171
1172 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1173 ui_out_field_int (uiout, "number", regnum);
1174
1175 if (format == 'N')
1176 format = 0;
1177
1178 if (value_optimized_out (val))
1179 error (_("Optimized out"));
1180
1181 if (format == 'r')
1182 {
1183 int j;
1184 char *ptr, buf[1024];
1185 const gdb_byte *valaddr = value_contents_for_printing (val);
1186
1187 strcpy (buf, "0x");
1188 ptr = buf + 2;
1189 for (j = 0; j < register_size (gdbarch, regnum); j++)
1190 {
1191 int idx = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ?
1192 j : register_size (gdbarch, regnum) - 1 - j;
1193
1194 sprintf (ptr, "%02x", (unsigned char) valaddr[idx]);
1195 ptr += 2;
1196 }
1197 ui_out_field_string (uiout, "value", buf);
1198 }
1199 else
1200 {
1201 struct value_print_options opts;
1202 struct ui_file *stb;
1203 struct cleanup *old_chain;
1204
1205 stb = mem_fileopen ();
1206 old_chain = make_cleanup_ui_file_delete (stb);
1207
1208 get_formatted_print_options (&opts, format);
1209 opts.deref_ref = 1;
1210 val_print (value_type (val),
1211 value_contents_for_printing (val),
1212 value_embedded_offset (val), 0,
1213 stb, 0, val, &opts, current_language);
1214 ui_out_field_stream (uiout, "value", stb);
1215 do_cleanups (old_chain);
1216 }
1217
1218 do_cleanups (tuple_cleanup);
1219}
1220
1221/* Write given values into registers. The registers and values are
1222 given as pairs. The corresponding MI command is
1223 -data-write-register-values <format>
1224 [<regnum1> <value1>...<regnumN> <valueN>] */
1225void
1226mi_cmd_data_write_register_values (char *command, char **argv, int argc)
1227{
1228 struct regcache *regcache;
1229 struct gdbarch *gdbarch;
1230 int numregs, i;
1231
1232 /* Note that the test for a valid register must include checking the
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. */
1238
1239 regcache = get_current_regcache ();
1240 gdbarch = get_regcache_arch (regcache);
1241 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1242
1243 if (argc == 0)
1244 error (_("-data-write-register-values: Usage: -data-write-register-"
1245 "values <format> [<regnum1> <value1>...<regnumN> <valueN>]"));
1246
1247 if (!target_has_registers)
1248 error (_("-data-write-register-values: No registers."));
1249
1250 if (!(argc - 1))
1251 error (_("-data-write-register-values: No regs and values specified."));
1252
1253 if ((argc - 1) % 2)
1254 error (_("-data-write-register-values: "
1255 "Regs and vals are not in pairs."));
1256
1257 for (i = 1; i < argc; i = i + 2)
1258 {
1259 int regnum = atoi (argv[i]);
1260
1261 if (regnum >= 0 && regnum < numregs
1262 && gdbarch_register_name (gdbarch, regnum)
1263 && *gdbarch_register_name (gdbarch, regnum))
1264 {
1265 LONGEST value;
1266
1267 /* Get the value as a number. */
1268 value = parse_and_eval_address (argv[i + 1]);
1269
1270 /* Write it down. */
1271 regcache_cooked_write_signed (regcache, regnum, value);
1272 }
1273 else
1274 error (_("bad register number"));
1275 }
1276}
1277
1278/* Evaluate the value of the argument. The argument is an
1279 expression. If the expression contains spaces it needs to be
1280 included in double quotes. */
1281
1282void
1283mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
1284{
1285 struct expression *expr;
1286 struct cleanup *old_chain;
1287 struct value *val;
1288 struct ui_file *stb;
1289 struct value_print_options opts;
1290 struct ui_out *uiout = current_uiout;
1291
1292 stb = mem_fileopen ();
1293 old_chain = make_cleanup_ui_file_delete (stb);
1294
1295 if (argc != 1)
1296 error (_("-data-evaluate-expression: "
1297 "Usage: -data-evaluate-expression expression"));
1298
1299 expr = parse_expression (argv[0]);
1300
1301 make_cleanup (free_current_contents, &expr);
1302
1303 val = evaluate_expression (expr);
1304
1305 /* Print the result of the expression evaluation. */
1306 get_user_print_options (&opts);
1307 opts.deref_ref = 0;
1308 common_val_print (val, stb, 0, &opts, current_language);
1309
1310 ui_out_field_stream (uiout, "value", stb);
1311
1312 do_cleanups (old_chain);
1313}
1314
1315/* This is the -data-read-memory command.
1316
1317 ADDR: start address of data to be dumped.
1318 WORD-FORMAT: a char indicating format for the ``word''. See
1319 the ``x'' command.
1320 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
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:
1332 The number of bytes read is SIZE*ROW*COL. */
1333
1334void
1335mi_cmd_data_read_memory (char *command, char **argv, int argc)
1336{
1337 struct gdbarch *gdbarch = get_current_arch ();
1338 struct ui_out *uiout = current_uiout;
1339 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1340 CORE_ADDR addr;
1341 long total_bytes, nr_cols, nr_rows;
1342 char word_format;
1343 struct type *word_type;
1344 long word_size;
1345 char word_asize;
1346 char aschar;
1347 gdb_byte *mbuf;
1348 int nr_bytes;
1349 long offset = 0;
1350 int oind = 0;
1351 char *oarg;
1352 enum opt
1353 {
1354 OFFSET_OPT
1355 };
1356 static const struct mi_opt opts[] =
1357 {
1358 {"o", OFFSET_OPT, 1},
1359 { 0, 0, 0 }
1360 };
1361
1362 while (1)
1363 {
1364 int opt = mi_getopt ("-data-read-memory", argc, argv, opts,
1365 &oind, &oarg);
1366
1367 if (opt < 0)
1368 break;
1369 switch ((enum opt) opt)
1370 {
1371 case OFFSET_OPT:
1372 offset = atol (oarg);
1373 break;
1374 }
1375 }
1376 argv += oind;
1377 argc -= oind;
1378
1379 if (argc < 5 || argc > 6)
1380 error (_("-data-read-memory: Usage: "
1381 "ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR]."));
1382
1383 /* Extract all the arguments. */
1384
1385 /* Start address of the memory dump. */
1386 addr = parse_and_eval_address (argv[0]) + offset;
1387 /* The format character to use when displaying a memory word. See
1388 the ``x'' command. */
1389 word_format = argv[1][0];
1390 /* The size of the memory word. */
1391 word_size = atol (argv[2]);
1392 switch (word_size)
1393 {
1394 case 1:
1395 word_type = builtin_type (gdbarch)->builtin_int8;
1396 word_asize = 'b';
1397 break;
1398 case 2:
1399 word_type = builtin_type (gdbarch)->builtin_int16;
1400 word_asize = 'h';
1401 break;
1402 case 4:
1403 word_type = builtin_type (gdbarch)->builtin_int32;
1404 word_asize = 'w';
1405 break;
1406 case 8:
1407 word_type = builtin_type (gdbarch)->builtin_int64;
1408 word_asize = 'g';
1409 break;
1410 default:
1411 word_type = builtin_type (gdbarch)->builtin_int8;
1412 word_asize = 'b';
1413 }
1414 /* The number of rows. */
1415 nr_rows = atol (argv[3]);
1416 if (nr_rows <= 0)
1417 error (_("-data-read-memory: invalid number of rows."));
1418
1419 /* Number of bytes per row. */
1420 nr_cols = atol (argv[4]);
1421 if (nr_cols <= 0)
1422 error (_("-data-read-memory: invalid number of columns."));
1423
1424 /* The un-printable character when printing ascii. */
1425 if (argc == 6)
1426 aschar = *argv[5];
1427 else
1428 aschar = 0;
1429
1430 /* Create a buffer and read it in. */
1431 total_bytes = word_size * nr_rows * nr_cols;
1432 mbuf = xcalloc (total_bytes, 1);
1433 make_cleanup (xfree, mbuf);
1434
1435 /* Dispatch memory reads to the topmost target, not the flattened
1436 current_target. */
1437 nr_bytes = target_read (current_target.beneath,
1438 TARGET_OBJECT_MEMORY, NULL, mbuf,
1439 addr, total_bytes);
1440 if (nr_bytes <= 0)
1441 error (_("Unable to read memory."));
1442
1443 /* Output the header information. */
1444 ui_out_field_core_addr (uiout, "addr", gdbarch, addr);
1445 ui_out_field_int (uiout, "nr-bytes", nr_bytes);
1446 ui_out_field_int (uiout, "total-bytes", total_bytes);
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);
1453
1454 /* Build the result as a two dimentional table. */
1455 {
1456 struct ui_file *stream;
1457 struct cleanup *cleanup_stream;
1458 int row;
1459 int row_byte;
1460
1461 stream = mem_fileopen ();
1462 cleanup_stream = make_cleanup_ui_file_delete (stream);
1463
1464 make_cleanup_ui_out_list_begin_end (uiout, "memory");
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;
1471 struct cleanup *cleanup_tuple;
1472 struct cleanup *cleanup_list_data;
1473 struct value_print_options opts;
1474
1475 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1476 ui_out_field_core_addr (uiout, "addr", gdbarch, addr + row_byte);
1477 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr +
1478 row_byte); */
1479 cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
1480 get_formatted_print_options (&opts, word_format);
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 {
1491 ui_file_rewind (stream);
1492 print_scalar_formatted (mbuf + col_byte, word_type, &opts,
1493 word_asize, stream);
1494 ui_out_field_stream (uiout, NULL, stream);
1495 }
1496 }
1497 do_cleanups (cleanup_list_data);
1498 if (aschar)
1499 {
1500 int byte;
1501
1502 ui_file_rewind (stream);
1503 for (byte = row_byte;
1504 byte < row_byte + word_size * nr_cols; byte++)
1505 {
1506 if (byte >= nr_bytes)
1507 fputc_unfiltered ('X', stream);
1508 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
1509 fputc_unfiltered (aschar, stream);
1510 else
1511 fputc_unfiltered (mbuf[byte], stream);
1512 }
1513 ui_out_field_stream (uiout, "ascii", stream);
1514 }
1515 do_cleanups (cleanup_tuple);
1516 }
1517 do_cleanups (cleanup_stream);
1518 }
1519 do_cleanups (cleanups);
1520}
1521
1522void
1523mi_cmd_data_read_memory_bytes (char *command, char **argv, int argc)
1524{
1525 struct gdbarch *gdbarch = get_current_arch ();
1526 struct ui_out *uiout = current_uiout;
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;
1534 int oind = 0;
1535 char *oarg;
1536 enum opt
1537 {
1538 OFFSET_OPT
1539 };
1540 static const struct mi_opt opts[] =
1541 {
1542 {"o", OFFSET_OPT, 1},
1543 { 0, 0, 0 }
1544 };
1545
1546 while (1)
1547 {
1548 int opt = mi_getopt ("-data-read-memory-bytes", argc, argv, opts,
1549 &oind, &oarg);
1550 if (opt < 0)
1551 break;
1552 switch ((enum opt) opt)
1553 {
1554 case OFFSET_OPT:
1555 offset = atol (oarg);
1556 break;
1557 }
1558 }
1559 argv += oind;
1560 argc -= oind;
1561
1562 if (argc != 2)
1563 error (_("Usage: [ -o OFFSET ] ADDR LENGTH."));
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)
1573 error (_("Unable to read memory."));
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
1604/* Implementation of the -data-write_memory command.
1605
1606 COLUMN_OFFSET: optional argument. Must be preceded by '-o'. The
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
1610 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
1611 the location to write to.
1612 FORMAT: a char indicating format for the ``word''. See
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
1619 Prints nothing. */
1620
1621void
1622mi_cmd_data_write_memory (char *command, char **argv, int argc)
1623{
1624 struct gdbarch *gdbarch = get_current_arch ();
1625 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1626 CORE_ADDR addr;
1627 long word_size;
1628 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1629 enough when using a compiler other than GCC. */
1630 LONGEST value;
1631 void *buffer;
1632 struct cleanup *old_chain;
1633 long offset = 0;
1634 int oind = 0;
1635 char *oarg;
1636 enum opt
1637 {
1638 OFFSET_OPT
1639 };
1640 static const struct mi_opt opts[] =
1641 {
1642 {"o", OFFSET_OPT, 1},
1643 { 0, 0, 0 }
1644 };
1645
1646 while (1)
1647 {
1648 int opt = mi_getopt ("-data-write-memory", argc, argv, opts,
1649 &oind, &oarg);
1650
1651 if (opt < 0)
1652 break;
1653 switch ((enum opt) opt)
1654 {
1655 case OFFSET_OPT:
1656 offset = atol (oarg);
1657 break;
1658 }
1659 }
1660 argv += oind;
1661 argc -= oind;
1662
1663 if (argc != 4)
1664 error (_("-data-write-memory: Usage: "
1665 "[-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE."));
1666
1667 /* Extract all the arguments. */
1668 /* Start address of the memory dump. */
1669 addr = parse_and_eval_address (argv[0]);
1670 /* The size of the memory word. */
1671 word_size = atol (argv[2]);
1672
1673 /* Calculate the real address of the write destination. */
1674 addr += (offset * word_size);
1675
1676 /* Get the value as a number. */
1677 value = parse_and_eval_address (argv[3]);
1678 /* Get the value into an array. */
1679 buffer = xmalloc (word_size);
1680 old_chain = make_cleanup (xfree, buffer);
1681 store_signed_integer (buffer, word_size, byte_order, value);
1682 /* Write it down to memory. */
1683 write_memory_with_notification (addr, buffer, word_size);
1684 /* Free the buffer. */
1685 do_cleanups (old_chain);
1686}
1687
1688/* Implementation of the -data-write-memory-bytes command.
1689
1690 ADDR: start address
1691 DATA: string of bytes to write at that address
1692 COUNT: number of bytes to be filled (decimal integer). */
1693
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;
1700 gdb_byte *databuf;
1701 size_t len, i, steps, remainder;
1702 long int count, j;
1703 struct cleanup *back_to;
1704
1705 if (argc != 2 && argc != 3)
1706 error (_("Usage: ADDR DATA [COUNT]."));
1707
1708 addr = parse_and_eval_address (argv[0]);
1709 cdata = argv[1];
1710 if (strlen (cdata) % 2)
1711 error (_("Hex-encoded '%s' must have an even number of characters."),
1712 cdata);
1713
1714 len = strlen (cdata)/2;
1715 if (argc == 3)
1716 count = strtoul (argv[2], NULL, 10);
1717 else
1718 count = len;
1719
1720 databuf = xmalloc (len * sizeof (gdb_byte));
1721 back_to = make_cleanup (xfree, databuf);
1722
1723 for (i = 0; i < len; ++i)
1724 {
1725 int x;
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;
1751 }
1752
1753 write_memory_with_notification (addr, data, count);
1754
1755 do_cleanups (back_to);
1756}
1757
1758void
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
1775 return;
1776
1777 usage_error:
1778 error (_("-enable-timings: Usage: %s {yes|no}"), command);
1779}
1780
1781void
1782mi_cmd_list_features (char *command, char **argv, int argc)
1783{
1784 if (argc == 0)
1785 {
1786 struct cleanup *cleanup = NULL;
1787 struct ui_out *uiout = current_uiout;
1788
1789 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
1790 ui_out_field_string (uiout, NULL, "frozen-varobjs");
1791 ui_out_field_string (uiout, NULL, "pending-breakpoints");
1792 ui_out_field_string (uiout, NULL, "thread-info");
1793 ui_out_field_string (uiout, NULL, "data-read-memory-bytes");
1794 ui_out_field_string (uiout, NULL, "breakpoint-notifications");
1795 ui_out_field_string (uiout, NULL, "ada-task-info");
1796
1797#if HAVE_PYTHON
1798 if (gdb_python_initialized)
1799 ui_out_field_string (uiout, NULL, "python");
1800#endif
1801
1802 do_cleanups (cleanup);
1803 return;
1804 }
1805
1806 error (_("-list-features should be passed no arguments"));
1807}
1808
1809void
1810mi_cmd_list_target_features (char *command, char **argv, int argc)
1811{
1812 if (argc == 0)
1813 {
1814 struct cleanup *cleanup = NULL;
1815 struct ui_out *uiout = current_uiout;
1816
1817 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
1818 if (target_can_async_p ())
1819 ui_out_field_string (uiout, NULL, "async");
1820 if (target_can_execute_reverse)
1821 ui_out_field_string (uiout, NULL, "reverse");
1822
1823 do_cleanups (cleanup);
1824 return;
1825 }
1826
1827 error (_("-list-target-features should be passed no arguments"));
1828}
1829
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
1840 ui_out_field_fmt (current_uiout, "inferior", "i%d", inf->num);
1841}
1842
1843/* Callback used to find the first inferior other than the current
1844 one. */
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
1855void
1856mi_cmd_remove_inferior (char *command, char **argv, int argc)
1857{
1858 int id;
1859 struct inferior *inf;
1860
1861 if (argc != 1)
1862 error (_("-remove-inferior should be passed a single argument"));
1863
1864 if (sscanf (argv[0], "i%d", &id) != 1)
1865 error (_("the thread group id is syntactically invalid"));
1866
1867 inf = find_inferior_id (id);
1868 if (!inf)
1869 error (_("the specified thread group does not exist"));
1870
1871 if (inf->pid != 0)
1872 error (_("cannot remove an active inferior"));
1873
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
1890 delete_inferior_1 (inf, 1 /* silent */);
1891}
1892
1893\f
1894
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
1899 to perfrom after the given command has executed (display/suppress
1900 prompt, display error). */
1901
1902static void
1903captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
1904{
1905 struct cleanup *cleanup;
1906
1907 if (do_timings)
1908 current_command_ts = context->cmd_start;
1909
1910 current_token = xstrdup (context->token);
1911 cleanup = make_cleanup (free_current_contents, &current_token);
1912
1913 running_result_record_printed = 0;
1914 mi_proceeded = 0;
1915 switch (context->op)
1916 {
1917 case MI_COMMAND:
1918 /* A MI command was read from the input stream. */
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);
1923
1924 mi_cmd_execute (context);
1925
1926 /* Print the result if there were no errors.
1927
1928 Remember that on the way out of executing a command, you have
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. */
1932 if (!running_result_record_printed)
1933 {
1934 fputs_unfiltered (context->token, raw_stdout);
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);
1939 mi_out_put (uiout, raw_stdout);
1940 mi_out_rewind (uiout);
1941 mi_print_timing_maybe ();
1942 fputs_unfiltered ("\n", raw_stdout);
1943 }
1944 else
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. */
1948 mi_out_rewind (uiout);
1949 break;
1950
1951 case CLI_COMMAND:
1952 {
1953 char *argv[2];
1954
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;
1963 mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);
1964
1965 /* If we changed interpreters, DON'T print out anything. */
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 {
1971 if (!running_result_record_printed)
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);
1977 mi_print_timing_maybe ();
1978 fputs_unfiltered ("\n", raw_stdout);
1979 }
1980 else
1981 mi_out_rewind (uiout);
1982 }
1983 break;
1984 }
1985 }
1986
1987 do_cleanups (cleanup);
1988}
1989
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}
2003
2004void
2005mi_execute_command (const char *cmd, int from_tty)
2006{
2007 char *token;
2008 struct mi_parse *command = NULL;
2009 volatile struct gdb_exception exception;
2010
2011 /* This is to handle EOF (^D). We just quit gdb. */
2012 /* FIXME: we should call some API function here. */
2013 if (cmd == 0)
2014 quit_force (NULL, from_tty);
2015
2016 target_log_command (cmd);
2017
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
2028 {
2029 volatile struct gdb_exception result;
2030 ptid_t previous_ptid = inferior_ptid;
2031
2032 command->token = token;
2033
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
2041 TRY_CATCH (result, RETURN_MASK_ALL)
2042 {
2043 captured_mi_execute_command (current_uiout, command);
2044 }
2045 if (result.reason < 0)
2046 {
2047 /* The command execution failed and error() was called
2048 somewhere. */
2049 mi_print_exception (command->token, result);
2050 mi_out_rewind (current_uiout);
2051 }
2052
2053 bpstat_do_actions ();
2054
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 ();
2067 int report_change = 0;
2068
2069 if (command->thread == -1)
2070 {
2071 report_change = (!ptid_equal (previous_ptid, null_ptid)
2072 && !ptid_equal (inferior_ptid, previous_ptid)
2073 && !ptid_equal (inferior_ptid, null_ptid));
2074 }
2075 else if (!ptid_equal (inferior_ptid, null_ptid))
2076 {
2077 struct thread_info *ti = inferior_thread ();
2078
2079 report_change = (ti->num != command->thread);
2080 }
2081
2082 if (report_change)
2083 {
2084 struct thread_info *ti = inferior_thread ();
2085
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
2094 mi_parse_free (command);
2095 }
2096}
2097
2098static void
2099mi_cmd_execute (struct mi_parse *parse)
2100{
2101 struct cleanup *cleanup;
2102
2103 cleanup = prepare_execute_command ();
2104
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
2114 if (parse->frame != -1 && parse->thread == -1)
2115 error (_("Cannot specify --frame without --thread"));
2116
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)
2123 error (_("Invalid thread group for the --thread-group option"));
2124
2125 set_current_inferior (inf);
2126 /* This behaviour means that if --thread-group option identifies
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. */
2131 if (inf->pid != 0)
2132 tp = any_live_thread_of_process (inf->pid);
2133 switch_to_thread (tp ? tp->ptid : null_ptid);
2134 set_current_program_space (inf->pspace);
2135 }
2136
2137 if (parse->thread != -1)
2138 {
2139 struct thread_info *tp = find_thread_id (parse->thread);
2140
2141 if (!tp)
2142 error (_("Invalid thread id: %d"), parse->thread);
2143
2144 if (is_exited (tp->ptid))
2145 error (_("Thread id: %d has terminated"), parse->thread);
2146
2147 switch_to_thread (tp->ptid);
2148 }
2149
2150 if (parse->frame != -1)
2151 {
2152 struct frame_info *fid;
2153 int frame = parse->frame;
2154
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
2160 error (_("Invalid frame id: %d"), frame);
2161 }
2162
2163 current_context = parse;
2164
2165 if (parse->cmd->suppress_notification != NULL)
2166 {
2167 make_cleanup_restore_integer (parse->cmd->suppress_notification);
2168 *parse->cmd->suppress_notification = 1;
2169 }
2170
2171 if (parse->cmd->argv_func != NULL)
2172 {
2173 parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
2174 }
2175 else if (parse->cmd->cli.cmd != 0)
2176 {
2177 /* FIXME: DELETE THIS. */
2178 /* The operation is still implemented by a cli command. */
2179 /* Must be a synchronous one. */
2180 mi_execute_cli_command (parse->cmd->cli.cmd, parse->cmd->cli.args_p,
2181 parse->args);
2182 }
2183 else
2184 {
2185 /* FIXME: DELETE THIS. */
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);
2196 }
2197 do_cleanups (cleanup);
2198}
2199
2200/* FIXME: This is just a hack so we can get some extra commands going.
2201 We don't want to channel things through the CLI, but call libgdb directly.
2202 Use only for synchronous commands. */
2203
2204void
2205mi_execute_cli_command (const char *cmd, int args_p, const char *args)
2206{
2207 if (cmd != 0)
2208 {
2209 struct cleanup *old_cleanups;
2210 char *run;
2211
2212 if (args_p)
2213 run = xstrprintf ("%s %s", cmd, args);
2214 else
2215 run = xstrdup (cmd);
2216 if (mi_debug_p)
2217 /* FIXME: gdb_???? */
2218 fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
2219 cmd, run);
2220 old_cleanups = make_cleanup (xfree, run);
2221 execute_command (run, 0 /* from_tty */ );
2222 do_cleanups (old_cleanups);
2223 return;
2224 }
2225}
2226
2227void
2228mi_execute_async_cli_command (char *cli_command, char **argv, int argc)
2229{
2230 struct cleanup *old_cleanups;
2231 char *run;
2232
2233 if (target_can_async_p ())
2234 run = xstrprintf ("%s %s&", cli_command, argc ? *argv : "");
2235 else
2236 run = xstrprintf ("%s %s", cli_command, argc ? *argv : "");
2237 old_cleanups = make_cleanup (xfree, run);
2238
2239 execute_command (run, 0 /* from_tty */ );
2240
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);
2244}
2245
2246void
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;
2257 struct ui_out *saved_uiout;
2258 struct ui_out *uiout;
2259
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. */
2263 saved_uiout = current_uiout;
2264
2265 if (current_interp_named_p (INTERP_MI)
2266 || current_interp_named_p (INTERP_MI2))
2267 current_uiout = mi_out_new (2);
2268 else if (current_interp_named_p (INTERP_MI1))
2269 current_uiout = mi_out_new (1);
2270 else if (current_interp_named_p (INTERP_MI3))
2271 current_uiout = mi_out_new (3);
2272 else
2273 return;
2274
2275 uiout = current_uiout;
2276
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;
2287 delta.tv_usec += 1000000L;
2288 }
2289
2290 new_section = (previous_sect_name ?
2291 strcmp (previous_sect_name, section_name) : 1);
2292 if (new_section)
2293 {
2294 struct cleanup *cleanup_tuple;
2295
2296 xfree (previous_sect_name);
2297 previous_sect_name = xstrdup (section_name);
2298
2299 if (current_token)
2300 fputs_unfiltered (current_token, raw_stdout);
2301 fputs_unfiltered ("+download", raw_stdout);
2302 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
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);
2306 do_cleanups (cleanup_tuple);
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 {
2315 struct cleanup *cleanup_tuple;
2316
2317 last_update.tv_sec = time_now.tv_sec;
2318 last_update.tv_usec = time_now.tv_usec;
2319 if (current_token)
2320 fputs_unfiltered (current_token, raw_stdout);
2321 fputs_unfiltered ("+download", raw_stdout);
2322 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
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);
2328 do_cleanups (cleanup_tuple);
2329 mi_out_put (uiout, raw_stdout);
2330 fputs_unfiltered ("\n", raw_stdout);
2331 gdb_flush (raw_stdout);
2332 }
2333
2334 xfree (uiout);
2335 current_uiout = saved_uiout;
2336}
2337
2338static void
2339timestamp (struct mi_timestamp *tv)
2340{
2341 gettimeofday (&tv->wallclock, NULL);
2342#ifdef HAVE_GETRUSAGE
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;
2348#else
2349 {
2350 long usec = get_run_time ();
2351
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;
2356 }
2357#endif
2358}
2359
2360static void
2361print_diff_now (struct mi_timestamp *start)
2362{
2363 struct mi_timestamp now;
2364
2365 timestamp (&now);
2366 print_diff (start, &now);
2367}
2368
2369void
2370mi_print_timing_maybe (void)
2371{
2372 /* If the command is -enable-timing then do_timings may be true
2373 whilst current_command_ts is not initialized. */
2374 if (do_timings && current_command_ts)
2375 print_diff_now (current_command_ts);
2376}
2377
2378static long
2379timeval_diff (struct timeval start, struct timeval end)
2380{
2381 return ((end.tv_sec - start.tv_sec) * 1000000L)
2382 + (end.tv_usec - start.tv_usec);
2383}
2384
2385static void
2386print_diff (struct mi_timestamp *start, struct mi_timestamp *end)
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}
2395
2396void
2397mi_cmd_trace_define_variable (char *command, char **argv, int argc)
2398{
2399 struct expression *expr;
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
2407 name = argv[0];
2408 if (*name++ != '$')
2409 error (_("Name of trace variable should start with '$'"));
2410
2411 validate_trace_state_variable_name (name);
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;
2421}
2422
2423void
2424mi_cmd_trace_list_variables (char *command, char **argv, int argc)
2425{
2426 if (argc != 0)
2427 error (_("-trace-list-variables: no arguments allowed"));
2428
2429 tvariables_info_1 ();
2430}
2431
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
2493 sals = decode_line_with_current_source (argv[1],
2494 DECODE_LINE_FUNFIRSTLINE);
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)
2513 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2514}
2515
2516void
2517mi_cmd_trace_save (char *command, char **argv, int argc)
2518{
2519 int target_saves = 0;
2520 int generate_ctf = 0;
2521 char *filename;
2522 int oind = 0;
2523 char *oarg;
2524
2525 enum opt
2526 {
2527 TARGET_SAVE_OPT, CTF_OPT
2528 };
2529 static const struct mi_opt opts[] =
2530 {
2531 {"r", TARGET_SAVE_OPT, 0},
2532 {"ctf", CTF_OPT, 0},
2533 { 0, 0, 0 }
2534 };
2535
2536 while (1)
2537 {
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 }
2552 }
2553 filename = argv[oind];
2554
2555 if (generate_ctf)
2556 trace_save_ctf (filename, target_saves);
2557 else
2558 trace_save_tfile (filename, target_saves);
2559}
2560
2561void
2562mi_cmd_trace_start (char *command, char **argv, int argc)
2563{
2564 start_tracing (NULL);
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{
2576 stop_tracing (NULL);
2577 trace_status_mi (1);
2578}
2579
2580/* Implement the "-ada-task-info" command. */
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 0.032232 seconds and 4 git commands to generate.