Make -exec-run a proper MI commands.
[deliverable/binutils-gdb.git] / gdb / mi / mi-main.c
CommitLineData
fb40c209 1/* MI Command Set.
cd0bfa36 2
4c38e0a4 3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
6aba47ca 4 Free Software Foundation, Inc.
cd0bfa36 5
ab91fdd5 6 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
fb40c209
AC
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fb40c209 22
41296c92 23/* Work in progress. */
fb40c209
AC
24
25#include "defs.h"
e17c207e 26#include "arch-utils.h"
fb40c209
AC
27#include "target.h"
28#include "inferior.h"
29#include "gdb_string.h"
60250e8b 30#include "exceptions.h"
fb40c209
AC
31#include "top.h"
32#include "gdbthread.h"
33#include "mi-cmds.h"
34#include "mi-parse.h"
35#include "mi-getopt.h"
36#include "mi-console.h"
37#include "ui-out.h"
38#include "mi-out.h"
4389a95a 39#include "interps.h"
fb40c209
AC
40#include "event-loop.h"
41#include "event-top.h"
41296c92 42#include "gdbcore.h" /* For write_memory(). */
56178203 43#include "value.h"
4e052eda 44#include "regcache.h"
5b7f31a4 45#include "gdb.h"
36dc181b 46#include "frame.h"
b9362cc7 47#include "mi-main.h"
66bb093b 48#include "mi-common.h"
d8ca156b 49#include "language.h"
79a45b7d 50#include "valprint.h"
3ee1c036 51#include "inferior.h"
07e059b5 52#include "osdata.h"
dc146f7c 53#include "splay-tree.h"
36dc181b 54
fb40c209
AC
55#include <ctype.h>
56#include <sys/time.h>
57
d8c83789
NR
58#if defined HAVE_SYS_RESOURCE_H
59#include <sys/resource.h>
60#endif
61
62#ifdef HAVE_GETRUSAGE
63struct rusage rusage;
64#endif
65
fb40c209
AC
66enum
67 {
68 FROM_TTY = 0
69 };
70
fb40c209
AC
71int mi_debug_p;
72struct ui_file *raw_stdout;
73
d8c83789
NR
74/* This is used to pass the current command timestamp
75 down to continuation routines. */
76static struct mi_timestamp *current_command_ts;
77
78static int do_timings = 0;
79
a2840c35
VP
80char *current_token;
81int running_result_record_printed = 1;
fb40c209 82
f3b1572e
PA
83/* Flag indicating that the target has proceeded since the last
84 command was issued. */
85int mi_proceeded;
86
fb40c209 87extern void _initialize_mi_main (void);
ce8f13f8 88static void mi_cmd_execute (struct mi_parse *parse);
fb40c209 89
b2af646b
AC
90static void mi_execute_cli_command (const char *cmd, int args_p,
91 const char *args);
ce8f13f8 92static void mi_execute_async_cli_command (char *cli_command,
9e22b03a 93 char **argv, int argc);
6ed7ea50
UW
94static int register_changed_p (int regnum, struct regcache *,
95 struct regcache *);
7ccb0be9 96static void get_register (struct frame_info *, int regnum, int format);
4389a95a 97
41296c92 98/* Command implementations. FIXME: Is this libgdb? No. This is the MI
fb40c209 99 layer that calls libgdb. Any operation used in the below should be
41296c92 100 formalized. */
fb40c209 101
d8c83789
NR
102static void timestamp (struct mi_timestamp *tv);
103
104static void print_diff_now (struct mi_timestamp *start);
105static void print_diff (struct mi_timestamp *start, struct mi_timestamp *end);
106
ce8f13f8 107void
fb40c209
AC
108mi_cmd_gdb_exit (char *command, char **argv, int argc)
109{
41296c92 110 /* We have to print everything right here because we never return. */
721c02de
VP
111 if (current_token)
112 fputs_unfiltered (current_token, raw_stdout);
fb40c209
AC
113 fputs_unfiltered ("^exit\n", raw_stdout);
114 mi_out_put (uiout, raw_stdout);
a6b29f87 115 gdb_flush (raw_stdout);
41296c92 116 /* FIXME: The function called is not yet a formal libgdb function. */
fb40c209 117 quit_force (NULL, FROM_TTY);
fb40c209
AC
118}
119
ce8f13f8 120void
9e22b03a 121mi_cmd_exec_next (char *command, char **argv, int argc)
fb40c209 122{
41296c92 123 /* FIXME: Should call a libgdb function, not a cli wrapper. */
e5829bee
MS
124 if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
125 mi_execute_async_cli_command ("reverse-next", argv + 1, argc - 1);
126 else
127 mi_execute_async_cli_command ("next", argv, argc);
fb40c209
AC
128}
129
ce8f13f8 130void
9e22b03a 131mi_cmd_exec_next_instruction (char *command, char **argv, int argc)
fb40c209 132{
41296c92 133 /* FIXME: Should call a libgdb function, not a cli wrapper. */
e5829bee
MS
134 if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
135 mi_execute_async_cli_command ("reverse-nexti", argv + 1, argc - 1);
136 else
137 mi_execute_async_cli_command ("nexti", argv, argc);
fb40c209
AC
138}
139
ce8f13f8 140void
9e22b03a 141mi_cmd_exec_step (char *command, char **argv, int argc)
fb40c209 142{
41296c92 143 /* FIXME: Should call a libgdb function, not a cli wrapper. */
e5829bee
MS
144 if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
145 mi_execute_async_cli_command ("reverse-step", argv + 1, argc - 1);
146 else
147 mi_execute_async_cli_command ("step", argv, argc);
fb40c209
AC
148}
149
ce8f13f8 150void
9e22b03a 151mi_cmd_exec_step_instruction (char *command, char **argv, int argc)
fb40c209 152{
41296c92 153 /* FIXME: Should call a libgdb function, not a cli wrapper. */
e5829bee
MS
154 if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
155 mi_execute_async_cli_command ("reverse-stepi", argv + 1, argc - 1);
156 else
157 mi_execute_async_cli_command ("stepi", argv, argc);
fb40c209
AC
158}
159
ce8f13f8 160void
9e22b03a 161mi_cmd_exec_finish (char *command, char **argv, int argc)
fb40c209 162{
41296c92 163 /* FIXME: Should call a libgdb function, not a cli wrapper. */
e5829bee
MS
164 if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
165 mi_execute_async_cli_command ("reverse-finish", argv + 1, argc - 1);
166 else
167 mi_execute_async_cli_command ("finish", argv, argc);
fb40c209
AC
168}
169
ce8f13f8 170void
9e22b03a 171mi_cmd_exec_return (char *command, char **argv, int argc)
fb40c209 172{
fb40c209
AC
173 /* This command doesn't really execute the target, it just pops the
174 specified number of frames. */
9e22b03a 175 if (argc)
fb40c209 176 /* Call return_command with from_tty argument equal to 0 so as to
41296c92 177 avoid being queried. */
9e22b03a 178 return_command (*argv, 0);
fb40c209
AC
179 else
180 /* Call return_command with from_tty argument equal to 0 so as to
41296c92 181 avoid being queried. */
36dc181b 182 return_command (NULL, 0);
fb40c209
AC
183
184 /* Because we have called return_command with from_tty = 0, we need
41296c92 185 to print the frame here. */
b04f3ab4 186 print_stack_frame (get_selected_frame (NULL), 1, LOC_AND_ADDRESS);
fb40c209
AC
187}
188
143260c9
VP
189void
190mi_cmd_exec_jump (char *args, char **argv, int argc)
191{
192 /* FIXME: Should call a libgdb function, not a cli wrapper. */
202b96c1 193 mi_execute_async_cli_command ("jump", argv, argc);
143260c9
VP
194}
195
8dd4f202
VP
196static int
197proceed_thread_callback (struct thread_info *thread, void *arg)
198{
199 int pid = *(int *)arg;
200
201 if (!is_stopped (thread->ptid))
202 return 0;
203
204 if (PIDGET (thread->ptid) != pid)
205 return 0;
206
207 switch_to_thread (thread->ptid);
208 clear_proceed_status ();
209 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
210 return 0;
211}
212
e5829bee
MS
213static void
214exec_continue (char **argv, int argc)
fb40c209 215{
77ebaa5a
VP
216 if (argc == 0)
217 continue_1 (0);
218 else if (argc == 1 && strcmp (argv[0], "--all") == 0)
219 continue_1 (1);
8dd4f202
VP
220 else if (argc == 2 && strcmp (argv[0], "--thread-group") == 0)
221 {
222 struct cleanup *old_chain;
223 int pid;
224 if (argv[1] == NULL || argv[1] == '\0')
225 error ("Thread group id not specified");
4b459977 226 pid = atoi (argv[1]);
8dd4f202
VP
227 if (!in_inferior_list (pid))
228 error ("Invalid thread group id '%s'", argv[1]);
229
230 old_chain = make_cleanup_restore_current_thread ();
231 iterate_over_threads (proceed_thread_callback, &pid);
232 do_cleanups (old_chain);
233 }
77ebaa5a 234 else
e5829bee
MS
235 error ("Usage: -exec-continue [--reverse] [--all|--thread-group id]");
236}
237
238/* continue in reverse direction:
239 XXX: code duplicated from reverse.c */
240
241static void
242exec_direction_default (void *notused)
243{
244 /* Return execution direction to default state. */
245 execution_direction = EXEC_FORWARD;
246}
247
248static void
249exec_reverse_continue (char **argv, int argc)
250{
251 enum exec_direction_kind dir = execution_direction;
252 struct cleanup *old_chain;
253
254 if (dir == EXEC_ERROR)
255 error (_("Target %s does not support this command."), target_shortname);
256
257 if (dir == EXEC_REVERSE)
258 error (_("Already in reverse mode."));
259
260 if (!target_can_execute_reverse)
261 error (_("Target %s does not support this command."), target_shortname);
262
263 old_chain = make_cleanup (exec_direction_default, NULL);
264 execution_direction = EXEC_REVERSE;
265 exec_continue (argv, argc);
266 do_cleanups (old_chain);
267}
268
269void
270mi_cmd_exec_continue (char *command, char **argv, int argc)
271{
272 if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
273 exec_reverse_continue (argv + 1, argc - 1);
274 else
275 exec_continue (argv, argc);
8dd4f202
VP
276}
277
278static int
279interrupt_thread_callback (struct thread_info *thread, void *arg)
280{
281 int pid = *(int *)arg;
282
283 if (!is_running (thread->ptid))
284 return 0;
285
286 if (PIDGET (thread->ptid) != pid)
287 return 0;
288
289 target_stop (thread->ptid);
290 return 0;
fb40c209
AC
291}
292
41296c92 293/* Interrupt the execution of the target. Note how we must play around
d8c83789 294 with the token variables, in order to display the current token in
fb40c209 295 the result of the interrupt command, and the previous execution
41296c92
NR
296 token when the target finally stops. See comments in
297 mi_cmd_execute. */
ce8f13f8 298void
9e22b03a 299mi_cmd_exec_interrupt (char *command, char **argv, int argc)
fb40c209 300{
77ebaa5a
VP
301 if (argc == 0)
302 {
303 if (!is_running (inferior_ptid))
304 error ("Current thread is not running.");
305
306 interrupt_target_1 (0);
307 }
308 else if (argc == 1 && strcmp (argv[0], "--all") == 0)
309 {
310 if (!any_running ())
311 error ("Inferior not running.");
8dd4f202 312
77ebaa5a
VP
313 interrupt_target_1 (1);
314 }
8dd4f202
VP
315 else if (argc == 2 && strcmp (argv[0], "--thread-group") == 0)
316 {
317 struct cleanup *old_chain;
318 int pid;
319 if (argv[1] == NULL || argv[1] == '\0')
320 error ("Thread group id not specified");
4b459977 321 pid = atoi (argv[1]);
8dd4f202
VP
322 if (!in_inferior_list (pid))
323 error ("Invalid thread group id '%s'", argv[1]);
324
325 old_chain = make_cleanup_restore_current_thread ();
326 iterate_over_threads (interrupt_thread_callback, &pid);
327 do_cleanups (old_chain);
328 }
77ebaa5a 329 else
8dd4f202 330 error ("Usage: -exec-interrupt [--all|--thread-group id]");
fb40c209
AC
331}
332
115d30f9
VP
333void
334mi_cmd_exec_run (char *command, char **argv, int argc)
335{
336 mi_execute_cli_command ("run", target_can_async_p (),
337 target_can_async_p () ? "&" : NULL);
338}
339
6418d433
VP
340static int
341find_thread_of_process (struct thread_info *ti, void *p)
342{
343 int pid = *(int *)p;
344 if (PIDGET (ti->ptid) == pid && !is_exited (ti->ptid))
345 return 1;
346
347 return 0;
348}
349
350void
351mi_cmd_target_detach (char *command, char **argv, int argc)
352{
353 if (argc != 0 && argc != 1)
354 error ("Usage: -target-detach [thread-group]");
355
356 if (argc == 1)
357 {
358 struct thread_info *tp;
359 char *end = argv[0];
360 int pid = strtol (argv[0], &end, 10);
361 if (*end != '\0')
362 error (_("Cannot parse thread group id '%s'"), argv[0]);
363
364 /* Pick any thread in the desired process. Current
365 target_detach deteches from the parent of inferior_ptid. */
366 tp = iterate_over_threads (find_thread_of_process, &pid);
367 if (!tp)
368 error (_("Thread group is empty"));
369
370 switch_to_thread (tp->ptid);
371 }
372
373 detach_command (NULL, 0);
374}
375
ce8f13f8 376void
fb40c209
AC
377mi_cmd_thread_select (char *command, char **argv, int argc)
378{
379 enum gdb_rc rc;
a13e061a 380 char *mi_error_message;
fb40c209
AC
381
382 if (argc != 1)
a13e061a
PA
383 error ("mi_cmd_thread_select: USAGE: threadnum.");
384
385 rc = gdb_thread_select (uiout, argv[0], &mi_error_message);
386
387 if (rc == GDB_RC_FAIL)
fb40c209 388 {
a13e061a
PA
389 make_cleanup (xfree, mi_error_message);
390 error ("%s", mi_error_message);
fb40c209 391 }
fb40c209
AC
392}
393
ce8f13f8 394void
fb40c209
AC
395mi_cmd_thread_list_ids (char *command, char **argv, int argc)
396{
b0b13bb4 397 enum gdb_rc rc;
a13e061a 398 char *mi_error_message;
fb40c209
AC
399
400 if (argc != 0)
a13e061a
PA
401 error ("mi_cmd_thread_list_ids: No arguments required.");
402
403 rc = gdb_list_thread_ids (uiout, &mi_error_message);
404
405 if (rc == GDB_RC_FAIL)
fb40c209 406 {
a13e061a
PA
407 make_cleanup (xfree, mi_error_message);
408 error ("%s", mi_error_message);
fb40c209 409 }
fb40c209
AC
410}
411
ce8f13f8 412void
8e8901c5
VP
413mi_cmd_thread_info (char *command, char **argv, int argc)
414{
415 int thread = -1;
416
417 if (argc != 0 && argc != 1)
a13e061a 418 error ("Invalid MI command");
8e8901c5
VP
419
420 if (argc == 1)
421 thread = atoi (argv[0]);
422
3ee1c036
VP
423 print_thread_info (uiout, thread, -1);
424}
425
dc146f7c
VP
426struct collect_cores_data
427{
428 int pid;
429
430 VEC (int) *cores;
431};
432
3ee1c036 433static int
dc146f7c 434collect_cores (struct thread_info *ti, void *xdata)
3ee1c036 435{
dc146f7c
VP
436 struct collect_cores_data *data = xdata;
437
438 if (ptid_get_pid (ti->ptid) == data->pid)
6c95b8df 439 {
dc146f7c
VP
440 int core = target_core_of_thread (ti->ptid);
441 if (core != -1)
442 VEC_safe_push (int, data->cores, core);
443 }
444
445 return 0;
446}
447
448static int *
449unique (int *b, int *e)
450{
451 int *d = b;
452 while (++b != e)
453 if (*d != *b)
454 *++d = *b;
455 return ++d;
456}
457
458struct print_one_inferior_data
459{
460 int recurse;
461 VEC (int) *inferiors;
462};
463
464static int
465print_one_inferior (struct inferior *inferior, void *xdata)
466{
467 struct print_one_inferior_data *top_data = xdata;
468
469 if (VEC_empty (int, top_data->inferiors)
470 || bsearch (&(inferior->pid), VEC_address (int, top_data->inferiors),
471 VEC_length (int, top_data->inferiors), sizeof (int),
472 compare_positive_ints))
473 {
474 struct collect_cores_data data;
6c95b8df
PA
475 struct cleanup *back_to
476 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
477
478 ui_out_field_fmt (uiout, "id", "%d", inferior->pid);
479 ui_out_field_string (uiout, "type", "process");
480 ui_out_field_int (uiout, "pid", inferior->pid);
481
dc146f7c
VP
482 data.pid = inferior->pid;
483 data.cores = 0;
484 iterate_over_threads (collect_cores, &data);
485
486 if (!VEC_empty (int, data.cores))
487 {
488 int elt;
489 int i;
490 int *b, *e;
491 struct cleanup *back_to_2 =
492 make_cleanup_ui_out_list_begin_end (uiout, "cores");
493
494 qsort (VEC_address (int, data.cores),
495 VEC_length (int, data.cores), sizeof (int),
496 compare_positive_ints);
497
498 b = VEC_address (int, data.cores);
499 e = b + VEC_length (int, data.cores);
500 e = unique (b, e);
501
502 for (; b != e; ++b)
503 ui_out_field_int (uiout, NULL, *b);
504
505 do_cleanups (back_to_2);
506 }
507
508 if (top_data->recurse)
509 print_thread_info (uiout, -1, inferior->pid);
510
6c95b8df
PA
511 do_cleanups (back_to);
512 }
3ee1c036 513
3ee1c036
VP
514 return 0;
515}
516
dc146f7c
VP
517/* Output a field named 'cores' with a list as the value. The elements of
518 the list are obtained by splitting 'cores' on comma. */
519
520static void
521output_cores (struct ui_out *uiout, const char *field_name, const char *xcores)
3ee1c036 522{
dc146f7c
VP
523 struct cleanup *back_to = make_cleanup_ui_out_list_begin_end (uiout,
524 field_name);
525 char *cores = xstrdup (xcores);
526 char *p = cores;
3ee1c036 527
dc146f7c 528 make_cleanup (xfree, cores);
3ee1c036 529
dc146f7c
VP
530 for (p = strtok (p, ","); p; p = strtok (NULL, ","))
531 ui_out_field_string (uiout, NULL, p);
3ee1c036 532
dc146f7c
VP
533 do_cleanups (back_to);
534}
3ee1c036 535
dc146f7c
VP
536static void
537free_vector_of_ints (void *xvector)
538{
539 VEC (int) **vector = xvector;
540 VEC_free (int, *vector);
541}
542
543static void
544do_nothing (splay_tree_key k)
545{
546}
07e059b5 547
dc146f7c
VP
548static void
549free_vector_of_osdata_items (splay_tree_value xvalue)
550{
551 VEC (osdata_item_s) *value = (VEC (osdata_item_s) *) xvalue;
552 /* We don't free the items itself, it will be done separately. */
553 VEC_free (osdata_item_s, value);
554}
e0665bc8 555
dc146f7c
VP
556static int
557splay_tree_int_comparator (splay_tree_key xa, splay_tree_key xb)
558{
559 int a = xa;
560 int b = xb;
561 return a - b;
562}
563
564static void
565free_splay_tree (void *xt)
566{
567 splay_tree t = xt;
568 splay_tree_delete (t);
569}
570
571static void
572list_available_thread_groups (VEC (int) *ids, int recurse)
573{
574 struct osdata *data;
575 struct osdata_item *item;
576 int ix_items;
577 /* This keeps a map from integer (pid) to VEC (struct osdata_item *)*
8eee9c5a
DE
578 The vector contains information about all threads for the given pid.
579 This is assigned an initial value to avoid "may be used uninitialized"
580 warning from gcc. */
581 splay_tree tree = NULL;
dc146f7c
VP
582
583 /* get_osdata will throw if it cannot return data. */
584 data = get_osdata ("processes");
585 make_cleanup_osdata_free (data);
586
587 if (recurse)
588 {
589 struct osdata *threads = get_osdata ("threads");
590 make_cleanup_osdata_free (threads);
591
592 tree = splay_tree_new (splay_tree_int_comparator,
593 do_nothing,
594 free_vector_of_osdata_items);
595 make_cleanup (free_splay_tree, tree);
e0665bc8 596
07e059b5 597 for (ix_items = 0;
dc146f7c 598 VEC_iterate (osdata_item_s, threads->items,
e0665bc8 599 ix_items, item);
07e059b5
VP
600 ix_items++)
601 {
07e059b5 602 const char *pid = get_osdata_column (item, "pid");
dc146f7c
VP
603 int pid_i = strtoul (pid, NULL, 0);
604 VEC (osdata_item_s) *vec = 0;
605
606 splay_tree_node n = splay_tree_lookup (tree, pid_i);
607 if (!n)
608 {
609 VEC_safe_push (osdata_item_s, vec, item);
610 splay_tree_insert (tree, pid_i, (splay_tree_value)vec);
611 }
612 else
613 {
614 vec = (VEC (osdata_item_s) *) n->value;
615 VEC_safe_push (osdata_item_s, vec, item);
616 n->value = (splay_tree_value) vec;
617 }
618 }
619 }
620
621 make_cleanup_ui_out_list_begin_end (uiout, "groups");
07e059b5 622
dc146f7c
VP
623 for (ix_items = 0;
624 VEC_iterate (osdata_item_s, data->items,
625 ix_items, item);
626 ix_items++)
627 {
628 struct cleanup *back_to;
e0665bc8 629
dc146f7c
VP
630 const char *pid = get_osdata_column (item, "pid");
631 const char *cmd = get_osdata_column (item, "command");
632 const char *user = get_osdata_column (item, "user");
633 const char *cores = get_osdata_column (item, "cores");
634
635 int pid_i = strtoul (pid, NULL, 0);
636
637 /* At present, the target will return all available processes
638 and if information about specific ones was required, we filter
639 undesired processes here. */
640 if (ids && bsearch (&pid_i, VEC_address (int, ids),
641 VEC_length (int, ids),
642 sizeof (int), compare_positive_ints) == NULL)
643 continue;
644
645
646 back_to = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
647
648 ui_out_field_fmt (uiout, "id", "%s", pid);
649 ui_out_field_string (uiout, "type", "process");
650 if (cmd)
651 ui_out_field_string (uiout, "description", cmd);
652 if (user)
653 ui_out_field_string (uiout, "user", user);
654 if (cores)
655 output_cores (uiout, "cores", cores);
656
657 if (recurse)
658 {
659 splay_tree_node n = splay_tree_lookup (tree, pid_i);
660 if (n)
661 {
662 VEC (osdata_item_s) *children = (VEC (osdata_item_s) *) n->value;
663 struct osdata_item *child;
664 int ix_child;
665
666 make_cleanup_ui_out_list_begin_end (uiout, "threads");
667
668 for (ix_child = 0;
669 VEC_iterate (osdata_item_s, children, ix_child, child);
670 ++ix_child)
671 {
672 struct cleanup *back_to_2 =
673 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
674
675 const char *tid = get_osdata_column (child, "tid");
676 const char *tcore = get_osdata_column (child, "core");
677 ui_out_field_string (uiout, "id", tid);
678 if (tcore)
679 ui_out_field_string (uiout, "core", tcore);
680
681 do_cleanups (back_to_2);
682 }
683 }
07e059b5 684 }
dc146f7c
VP
685
686 do_cleanups (back_to);
07e059b5 687 }
dc146f7c
VP
688}
689
690void
691mi_cmd_list_thread_groups (char *command, char **argv, int argc)
692{
693 struct cleanup *back_to;
694 int available = 0;
695 int recurse = 0;
696 VEC (int) *ids = 0;
697
698 enum opt
699 {
700 AVAILABLE_OPT, RECURSE_OPT
701 };
702 static struct mi_opt opts[] =
703 {
704 {"-available", AVAILABLE_OPT, 0},
705 {"-recurse", RECURSE_OPT, 1},
706 { 0, 0, 0 }
707 };
708
709 int optind = 0;
710 char *optarg;
711
712 while (1)
713 {
714 int opt = mi_getopt ("-list-thread-groups", argc, argv, opts,
715 &optind, &optarg);
716 if (opt < 0)
717 break;
718 switch ((enum opt) opt)
719 {
720 case AVAILABLE_OPT:
721 available = 1;
722 break;
723 case RECURSE_OPT:
724 if (strcmp (optarg, "0") == 0)
725 ;
726 else if (strcmp (optarg, "1") == 0)
727 recurse = 1;
728 else
729 error ("only '0' and '1' are valid values for the '--recurse' option");
730 break;
731 }
732 }
733
734 for (; optind < argc; ++optind)
735 {
736 char *end;
737 int inf = strtoul (argv[optind], &end, 0);
738 if (*end != '\0')
739 error ("invalid group id '%s'", argv[optind]);
740 VEC_safe_push (int, ids, inf);
741 }
742 if (VEC_length (int, ids) > 1)
743 qsort (VEC_address (int, ids),
744 VEC_length (int, ids),
745 sizeof (int), compare_positive_ints);
746
747 back_to = make_cleanup (free_vector_of_ints, &ids);
748
749 if (available)
750 {
751 list_available_thread_groups (ids, recurse);
752 }
753 else if (VEC_length (int, ids) == 1)
3ee1c036 754 {
dc146f7c
VP
755 /* Local thread groups, single id. */
756 int pid = *VEC_address (int, ids);
3ee1c036 757 if (!in_inferior_list (pid))
dc146f7c
VP
758 error ("Invalid thread group id '%d'", pid);
759 print_thread_info (uiout, -1, pid);
3ee1c036
VP
760 }
761 else
762 {
dc146f7c
VP
763 struct print_one_inferior_data data;
764 data.recurse = recurse;
765 data.inferiors = ids;
766
767 /* Local thread groups. Either no explicit ids -- and we
768 print everything, or several explicit ids. In both cases,
769 we print more than one group, and have to use 'groups'
770 as the top-level element. */
3ee1c036 771 make_cleanup_ui_out_list_begin_end (uiout, "groups");
dc146f7c
VP
772 update_thread_list ();
773 iterate_over_inferiors (print_one_inferior, &data);
3ee1c036 774 }
dc146f7c 775
3ee1c036 776 do_cleanups (back_to);
8e8901c5
VP
777}
778
ce8f13f8 779void
fb40c209
AC
780mi_cmd_data_list_register_names (char *command, char **argv, int argc)
781{
7ccb0be9
UW
782 struct frame_info *frame;
783 struct gdbarch *gdbarch;
fb40c209
AC
784 int regnum, numregs;
785 int i;
4060713b 786 struct cleanup *cleanup;
fb40c209
AC
787
788 /* Note that the test for a valid register must include checking the
c9f4d572
UW
789 gdbarch_register_name because gdbarch_num_regs may be allocated for
790 the union of the register sets within a family of related processors.
791 In this case, some entries of gdbarch_register_name will change depending
792 upon the particular processor being debugged. */
fb40c209 793
7ccb0be9
UW
794 frame = get_selected_frame (NULL);
795 gdbarch = get_frame_arch (frame);
796 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
fb40c209 797
4060713b 798 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names");
fb40c209 799
41296c92 800 if (argc == 0) /* No args, just do all the regs. */
fb40c209
AC
801 {
802 for (regnum = 0;
803 regnum < numregs;
804 regnum++)
805 {
7ccb0be9
UW
806 if (gdbarch_register_name (gdbarch, regnum) == NULL
807 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
173d6894
AC
808 ui_out_field_string (uiout, NULL, "");
809 else
c9f4d572 810 ui_out_field_string (uiout, NULL,
7ccb0be9 811 gdbarch_register_name (gdbarch, regnum));
fb40c209
AC
812 }
813 }
814
41296c92 815 /* Else, list of register #s, just do listed regs. */
fb40c209
AC
816 for (i = 0; i < argc; i++)
817 {
818 regnum = atoi (argv[i]);
173d6894 819 if (regnum < 0 || regnum >= numregs)
a13e061a
PA
820 error ("bad register number");
821
7ccb0be9
UW
822 if (gdbarch_register_name (gdbarch, regnum) == NULL
823 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
173d6894
AC
824 ui_out_field_string (uiout, NULL, "");
825 else
c9f4d572 826 ui_out_field_string (uiout, NULL,
7ccb0be9 827 gdbarch_register_name (gdbarch, regnum));
fb40c209 828 }
4060713b 829 do_cleanups (cleanup);
fb40c209
AC
830}
831
ce8f13f8 832void
fb40c209
AC
833mi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
834{
6ed7ea50
UW
835 static struct regcache *this_regs = NULL;
836 struct regcache *prev_regs;
7ccb0be9 837 struct gdbarch *gdbarch;
fb40c209
AC
838 int regnum, numregs, changed;
839 int i;
4060713b 840 struct cleanup *cleanup;
fb40c209 841
6ed7ea50
UW
842 /* The last time we visited this function, the current frame's register
843 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
844 and refresh THIS_REGS with the now-current register contents. */
845
846 prev_regs = this_regs;
847 this_regs = frame_save_as_regcache (get_selected_frame (NULL));
848 cleanup = make_cleanup_regcache_xfree (prev_regs);
849
fb40c209 850 /* Note that the test for a valid register must include checking the
c9f4d572
UW
851 gdbarch_register_name because gdbarch_num_regs may be allocated for
852 the union of the register sets within a family of related processors.
853 In this case, some entries of gdbarch_register_name will change depending
854 upon the particular processor being debugged. */
fb40c209 855
7ccb0be9
UW
856 gdbarch = get_regcache_arch (this_regs);
857 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
fb40c209 858
6ed7ea50 859 make_cleanup_ui_out_list_begin_end (uiout, "changed-registers");
fb40c209 860
41296c92 861 if (argc == 0) /* No args, just do all the regs. */
fb40c209
AC
862 {
863 for (regnum = 0;
864 regnum < numregs;
865 regnum++)
866 {
7ccb0be9
UW
867 if (gdbarch_register_name (gdbarch, regnum) == NULL
868 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
fb40c209 869 continue;
6ed7ea50 870 changed = register_changed_p (regnum, prev_regs, this_regs);
fb40c209 871 if (changed < 0)
a13e061a 872 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
fb40c209
AC
873 else if (changed)
874 ui_out_field_int (uiout, NULL, regnum);
875 }
876 }
877
41296c92 878 /* Else, list of register #s, just do listed regs. */
fb40c209
AC
879 for (i = 0; i < argc; i++)
880 {
881 regnum = atoi (argv[i]);
882
883 if (regnum >= 0
884 && regnum < numregs
7ccb0be9
UW
885 && gdbarch_register_name (gdbarch, regnum) != NULL
886 && *gdbarch_register_name (gdbarch, regnum) != '\000')
fb40c209 887 {
6ed7ea50 888 changed = register_changed_p (regnum, prev_regs, this_regs);
fb40c209 889 if (changed < 0)
a13e061a 890 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
fb40c209
AC
891 else if (changed)
892 ui_out_field_int (uiout, NULL, regnum);
893 }
894 else
a13e061a 895 error ("bad register number");
fb40c209 896 }
4060713b 897 do_cleanups (cleanup);
fb40c209
AC
898}
899
900static int
6ed7ea50
UW
901register_changed_p (int regnum, struct regcache *prev_regs,
902 struct regcache *this_regs)
fb40c209 903{
6ed7ea50
UW
904 struct gdbarch *gdbarch = get_regcache_arch (this_regs);
905 gdb_byte prev_buffer[MAX_REGISTER_SIZE];
906 gdb_byte this_buffer[MAX_REGISTER_SIZE];
fb40c209 907
6ed7ea50
UW
908 /* Registers not valid in this frame return count as unchanged. */
909 if (!regcache_valid_p (this_regs, regnum))
fb40c209
AC
910 return 0;
911
6ed7ea50
UW
912 /* First time through or after gdbarch change consider all registers as
913 changed. Same for registers not valid in the previous frame. */
914 if (!prev_regs || get_regcache_arch (prev_regs) != gdbarch
915 || !regcache_valid_p (prev_regs, regnum))
916 return 1;
fb40c209 917
6ed7ea50
UW
918 /* Get register contents and compare. */
919 regcache_cooked_read (prev_regs, regnum, prev_buffer);
920 regcache_cooked_read (this_regs, regnum, this_buffer);
fb40c209 921
6ed7ea50
UW
922 return memcmp (prev_buffer, this_buffer,
923 register_size (gdbarch, regnum)) != 0;
fb40c209
AC
924}
925
41296c92 926/* Return a list of register number and value pairs. The valid
fb40c209 927 arguments expected are: a letter indicating the format in which to
41296c92 928 display the registers contents. This can be one of: x (hexadecimal), d
fb40c209
AC
929 (decimal), N (natural), t (binary), o (octal), r (raw). After the
930 format argumetn there can be a sequence of numbers, indicating which
41296c92
NR
931 registers to fetch the content of. If the format is the only argument,
932 a list of all the registers with their values is returned. */
ce8f13f8 933void
fb40c209
AC
934mi_cmd_data_list_register_values (char *command, char **argv, int argc)
935{
7ccb0be9
UW
936 struct frame_info *frame;
937 struct gdbarch *gdbarch;
a13e061a 938 int regnum, numregs, format;
fb40c209 939 int i;
4060713b 940 struct cleanup *list_cleanup, *tuple_cleanup;
fb40c209
AC
941
942 /* Note that the test for a valid register must include checking the
c9f4d572
UW
943 gdbarch_register_name because gdbarch_num_regs may be allocated for
944 the union of the register sets within a family of related processors.
945 In this case, some entries of gdbarch_register_name will change depending
946 upon the particular processor being debugged. */
fb40c209 947
fb40c209 948 if (argc == 0)
a13e061a 949 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
fb40c209
AC
950
951 format = (int) argv[0][0];
952
7ccb0be9
UW
953 frame = get_selected_frame (NULL);
954 gdbarch = get_frame_arch (frame);
955 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
956
4060713b 957 list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values");
fb40c209 958
41296c92 959 if (argc == 1) /* No args, beside the format: do all the regs. */
fb40c209
AC
960 {
961 for (regnum = 0;
962 regnum < numregs;
963 regnum++)
964 {
7ccb0be9
UW
965 if (gdbarch_register_name (gdbarch, regnum) == NULL
966 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
fb40c209 967 continue;
4060713b 968 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
fb40c209 969 ui_out_field_int (uiout, "number", regnum);
7ccb0be9 970 get_register (frame, regnum, format);
4060713b 971 do_cleanups (tuple_cleanup);
fb40c209
AC
972 }
973 }
974
41296c92 975 /* Else, list of register #s, just do listed regs. */
fb40c209
AC
976 for (i = 1; i < argc; i++)
977 {
978 regnum = atoi (argv[i]);
979
980 if (regnum >= 0
981 && regnum < numregs
7ccb0be9
UW
982 && gdbarch_register_name (gdbarch, regnum) != NULL
983 && *gdbarch_register_name (gdbarch, regnum) != '\000')
fb40c209 984 {
4060713b 985 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
fb40c209 986 ui_out_field_int (uiout, "number", regnum);
7ccb0be9 987 get_register (frame, regnum, format);
4060713b 988 do_cleanups (tuple_cleanup);
fb40c209
AC
989 }
990 else
a13e061a 991 error ("bad register number");
fb40c209 992 }
4060713b 993 do_cleanups (list_cleanup);
fb40c209
AC
994}
995
41296c92 996/* Output one register's contents in the desired format. */
a13e061a 997static void
7ccb0be9 998get_register (struct frame_info *frame, int regnum, int format)
fb40c209 999{
7ccb0be9 1000 struct gdbarch *gdbarch = get_frame_arch (frame);
10c42a71 1001 gdb_byte buffer[MAX_REGISTER_SIZE];
fb40c209 1002 int optim;
ac2adee5
AC
1003 int realnum;
1004 CORE_ADDR addr;
1005 enum lval_type lval;
fb40c209
AC
1006 static struct ui_stream *stb = NULL;
1007
1008 stb = ui_out_stream_new (uiout);
1009
1010 if (format == 'N')
1011 format = 0;
1012
7ccb0be9 1013 frame_register (frame, regnum, &optim, &lval, &addr, &realnum, buffer);
ac2adee5 1014
fb40c209 1015 if (optim)
a13e061a 1016 error ("Optimized out");
fb40c209 1017
fb40c209
AC
1018 if (format == 'r')
1019 {
1020 int j;
1021 char *ptr, buf[1024];
1022
1023 strcpy (buf, "0x");
1024 ptr = buf + 2;
7ccb0be9 1025 for (j = 0; j < register_size (gdbarch, regnum); j++)
fb40c209 1026 {
7ccb0be9
UW
1027 int idx = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ?
1028 j : register_size (gdbarch, regnum) - 1 - j;
9730f241 1029 sprintf (ptr, "%02x", (unsigned char) buffer[idx]);
fb40c209
AC
1030 ptr += 2;
1031 }
1032 ui_out_field_string (uiout, "value", buf);
1033 /*fputs_filtered (buf, gdb_stdout); */
1034 }
1035 else
1036 {
79a45b7d 1037 struct value_print_options opts;
59669435 1038 get_formatted_print_options (&opts, format);
79a45b7d 1039 opts.deref_ref = 1;
7ccb0be9 1040 val_print (register_type (gdbarch, regnum), buffer, 0, 0,
79a45b7d 1041 stb->stream, 0, &opts, current_language);
fb40c209
AC
1042 ui_out_field_stream (uiout, "value", stb);
1043 ui_out_stream_delete (stb);
1044 }
fb40c209
AC
1045}
1046
24e8cecf 1047/* Write given values into registers. The registers and values are
41296c92 1048 given as pairs. The corresponding MI command is
24e8cecf 1049 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
ce8f13f8 1050void
24e8cecf
EZ
1051mi_cmd_data_write_register_values (char *command, char **argv, int argc)
1052{
7ccb0be9
UW
1053 struct regcache *regcache;
1054 struct gdbarch *gdbarch;
9f3a1602 1055 int numregs, i;
24e8cecf
EZ
1056 char format;
1057
1058 /* Note that the test for a valid register must include checking the
c9f4d572
UW
1059 gdbarch_register_name because gdbarch_num_regs may be allocated for
1060 the union of the register sets within a family of related processors.
1061 In this case, some entries of gdbarch_register_name will change depending
1062 upon the particular processor being debugged. */
24e8cecf 1063
7ccb0be9
UW
1064 regcache = get_current_regcache ();
1065 gdbarch = get_regcache_arch (regcache);
1066 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
24e8cecf
EZ
1067
1068 if (argc == 0)
a13e061a 1069 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
24e8cecf
EZ
1070
1071 format = (int) argv[0][0];
1072
1073 if (!target_has_registers)
a13e061a 1074 error ("mi_cmd_data_write_register_values: No registers.");
24e8cecf
EZ
1075
1076 if (!(argc - 1))
a13e061a 1077 error ("mi_cmd_data_write_register_values: No regs and values specified.");
24e8cecf
EZ
1078
1079 if ((argc - 1) % 2)
a13e061a 1080 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
24e8cecf
EZ
1081
1082 for (i = 1; i < argc; i = i + 2)
1083 {
9f3a1602 1084 int regnum = atoi (argv[i]);
24e8cecf 1085
9f3a1602 1086 if (regnum >= 0 && regnum < numregs
7ccb0be9
UW
1087 && gdbarch_register_name (gdbarch, regnum)
1088 && *gdbarch_register_name (gdbarch, regnum))
24e8cecf 1089 {
9f3a1602 1090 LONGEST value;
d8bf3afa 1091
9f3a1602 1092 /* Get the value as a number. */
24e8cecf 1093 value = parse_and_eval_address (argv[i + 1]);
9f3a1602 1094
41296c92 1095 /* Write it down. */
7ccb0be9 1096 regcache_cooked_write_signed (regcache, regnum, value);
24e8cecf
EZ
1097 }
1098 else
a13e061a 1099 error ("bad register number");
24e8cecf 1100 }
24e8cecf
EZ
1101}
1102
41296c92 1103/* Evaluate the value of the argument. The argument is an
fb40c209 1104 expression. If the expression contains spaces it needs to be
41296c92 1105 included in double quotes. */
ce8f13f8 1106void
fb40c209
AC
1107mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
1108{
1109 struct expression *expr;
1110 struct cleanup *old_chain = NULL;
96052a95 1111 struct value *val;
fb40c209 1112 struct ui_stream *stb = NULL;
79a45b7d 1113 struct value_print_options opts;
fb40c209
AC
1114
1115 stb = ui_out_stream_new (uiout);
1116
1117 if (argc != 1)
1118 {
412bbd6c 1119 ui_out_stream_delete (stb);
a13e061a 1120 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
fb40c209
AC
1121 }
1122
1123 expr = parse_expression (argv[0]);
1124
47cf603e 1125 old_chain = make_cleanup (free_current_contents, &expr);
fb40c209
AC
1126
1127 val = evaluate_expression (expr);
1128
41296c92 1129 /* Print the result of the expression evaluation. */
79a45b7d
TT
1130 get_user_print_options (&opts);
1131 opts.deref_ref = 0;
0fd88904 1132 val_print (value_type (val), value_contents (val),
42ae5230 1133 value_embedded_offset (val), value_address (val),
79a45b7d 1134 stb->stream, 0, &opts, current_language);
fb40c209
AC
1135
1136 ui_out_field_stream (uiout, "value", stb);
1137 ui_out_stream_delete (stb);
1138
1139 do_cleanups (old_chain);
fb40c209
AC
1140}
1141
fb40c209
AC
1142/* DATA-MEMORY-READ:
1143
1144 ADDR: start address of data to be dumped.
41296c92 1145 WORD-FORMAT: a char indicating format for the ``word''. See
fb40c209 1146 the ``x'' command.
41296c92 1147 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
fb40c209
AC
1148 NR_ROW: Number of rows.
1149 NR_COL: The number of colums (words per row).
1150 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
1151 ASCHAR for unprintable characters.
1152
1153 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
1154 displayes them. Returns:
1155
1156 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
1157
1158 Returns:
1159 The number of bytes read is SIZE*ROW*COL. */
1160
ce8f13f8 1161void
fb40c209
AC
1162mi_cmd_data_read_memory (char *command, char **argv, int argc)
1163{
e17c207e 1164 struct gdbarch *gdbarch = get_current_arch ();
fb40c209
AC
1165 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1166 CORE_ADDR addr;
1167 long total_bytes;
1168 long nr_cols;
1169 long nr_rows;
1170 char word_format;
1171 struct type *word_type;
1172 long word_size;
1173 char word_asize;
1174 char aschar;
508416a1 1175 gdb_byte *mbuf;
fb40c209
AC
1176 int nr_bytes;
1177 long offset = 0;
1178 int optind = 0;
1179 char *optarg;
1180 enum opt
1181 {
1182 OFFSET_OPT
1183 };
1184 static struct mi_opt opts[] =
1185 {
1186 {"o", OFFSET_OPT, 1},
d5d6fca5 1187 { 0, 0, 0 }
fb40c209
AC
1188 };
1189
1190 while (1)
1191 {
1192 int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts,
1193 &optind, &optarg);
1194 if (opt < 0)
1195 break;
1196 switch ((enum opt) opt)
1197 {
1198 case OFFSET_OPT:
1199 offset = atol (optarg);
1200 break;
1201 }
1202 }
1203 argv += optind;
1204 argc -= optind;
1205
1206 if (argc < 5 || argc > 6)
a13e061a 1207 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
fb40c209
AC
1208
1209 /* Extract all the arguments. */
1210
41296c92 1211 /* Start address of the memory dump. */
fb40c209 1212 addr = parse_and_eval_address (argv[0]) + offset;
41296c92 1213 /* The format character to use when displaying a memory word. See
fb40c209
AC
1214 the ``x'' command. */
1215 word_format = argv[1][0];
41296c92 1216 /* The size of the memory word. */
fb40c209
AC
1217 word_size = atol (argv[2]);
1218 switch (word_size)
1219 {
1220 case 1:
df4df182 1221 word_type = builtin_type (gdbarch)->builtin_int8;
fb40c209
AC
1222 word_asize = 'b';
1223 break;
1224 case 2:
df4df182 1225 word_type = builtin_type (gdbarch)->builtin_int16;
fb40c209
AC
1226 word_asize = 'h';
1227 break;
1228 case 4:
df4df182 1229 word_type = builtin_type (gdbarch)->builtin_int32;
fb40c209
AC
1230 word_asize = 'w';
1231 break;
1232 case 8:
df4df182 1233 word_type = builtin_type (gdbarch)->builtin_int64;
fb40c209
AC
1234 word_asize = 'g';
1235 break;
1236 default:
df4df182 1237 word_type = builtin_type (gdbarch)->builtin_int8;
fb40c209
AC
1238 word_asize = 'b';
1239 }
41296c92 1240 /* The number of rows. */
fb40c209
AC
1241 nr_rows = atol (argv[3]);
1242 if (nr_rows <= 0)
a13e061a
PA
1243 error ("mi_cmd_data_read_memory: invalid number of rows.");
1244
41296c92 1245 /* Number of bytes per row. */
fb40c209
AC
1246 nr_cols = atol (argv[4]);
1247 if (nr_cols <= 0)
a13e061a
PA
1248 error ("mi_cmd_data_read_memory: invalid number of columns.");
1249
41296c92 1250 /* The un-printable character when printing ascii. */
fb40c209
AC
1251 if (argc == 6)
1252 aschar = *argv[5];
1253 else
1254 aschar = 0;
1255
41296c92 1256 /* Create a buffer and read it in. */
fb40c209 1257 total_bytes = word_size * nr_rows * nr_cols;
2e94c453 1258 mbuf = xcalloc (total_bytes, 1);
b8c9b27d 1259 make_cleanup (xfree, mbuf);
cf7a04e8 1260
a4261689
PA
1261 /* Dispatch memory reads to the topmost target, not the flattened
1262 current_target. */
1263 nr_bytes = target_read_until_error (current_target.beneath,
1264 TARGET_OBJECT_MEMORY, NULL, mbuf,
1265 addr, total_bytes);
cf7a04e8 1266 if (nr_bytes <= 0)
a13e061a 1267 error ("Unable to read memory.");
fb40c209 1268
41296c92 1269 /* Output the header information. */
5af949e3 1270 ui_out_field_core_addr (uiout, "addr", gdbarch, addr);
fb40c209
AC
1271 ui_out_field_int (uiout, "nr-bytes", nr_bytes);
1272 ui_out_field_int (uiout, "total-bytes", total_bytes);
5af949e3
UW
1273 ui_out_field_core_addr (uiout, "next-row",
1274 gdbarch, addr + word_size * nr_cols);
1275 ui_out_field_core_addr (uiout, "prev-row",
1276 gdbarch, addr - word_size * nr_cols);
1277 ui_out_field_core_addr (uiout, "next-page", gdbarch, addr + total_bytes);
1278 ui_out_field_core_addr (uiout, "prev-page", gdbarch, addr - total_bytes);
fb40c209 1279
41296c92 1280 /* Build the result as a two dimentional table. */
fb40c209
AC
1281 {
1282 struct ui_stream *stream = ui_out_stream_new (uiout);
6ad4a2cf 1283 struct cleanup *cleanup_list_memory;
fb40c209
AC
1284 int row;
1285 int row_byte;
6ad4a2cf 1286 cleanup_list_memory = make_cleanup_ui_out_list_begin_end (uiout, "memory");
fb40c209
AC
1287 for (row = 0, row_byte = 0;
1288 row < nr_rows;
1289 row++, row_byte += nr_cols * word_size)
1290 {
1291 int col;
1292 int col_byte;
6ad4a2cf
JJ
1293 struct cleanup *cleanup_tuple;
1294 struct cleanup *cleanup_list_data;
79a45b7d
TT
1295 struct value_print_options opts;
1296
6ad4a2cf 1297 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5af949e3 1298 ui_out_field_core_addr (uiout, "addr", gdbarch, addr + row_byte);
fb40c209 1299 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
6ad4a2cf 1300 cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
79a45b7d 1301 get_formatted_print_options (&opts, word_format);
fb40c209
AC
1302 for (col = 0, col_byte = row_byte;
1303 col < nr_cols;
1304 col++, col_byte += word_size)
1305 {
1306 if (col_byte + word_size > nr_bytes)
1307 {
1308 ui_out_field_string (uiout, NULL, "N/A");
1309 }
1310 else
1311 {
1312 ui_file_rewind (stream->stream);
79a45b7d 1313 print_scalar_formatted (mbuf + col_byte, word_type, &opts,
fb40c209
AC
1314 word_asize, stream->stream);
1315 ui_out_field_stream (uiout, NULL, stream);
1316 }
1317 }
6ad4a2cf 1318 do_cleanups (cleanup_list_data);
fb40c209
AC
1319 if (aschar)
1320 {
1321 int byte;
1322 ui_file_rewind (stream->stream);
1323 for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
1324 {
1325 if (byte >= nr_bytes)
1326 {
1327 fputc_unfiltered ('X', stream->stream);
1328 }
1329 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
1330 {
1331 fputc_unfiltered (aschar, stream->stream);
1332 }
1333 else
1334 fputc_unfiltered (mbuf[byte], stream->stream);
1335 }
1336 ui_out_field_stream (uiout, "ascii", stream);
1337 }
6ad4a2cf 1338 do_cleanups (cleanup_tuple);
fb40c209
AC
1339 }
1340 ui_out_stream_delete (stream);
6ad4a2cf 1341 do_cleanups (cleanup_list_memory);
fb40c209
AC
1342 }
1343 do_cleanups (cleanups);
fb40c209
AC
1344}
1345
1346/* DATA-MEMORY-WRITE:
1347
1348 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
1349 offset from the beginning of the memory grid row where the cell to
1350 be written is.
1351 ADDR: start address of the row in the memory grid where the memory
41296c92 1352 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
fb40c209 1353 the location to write to.
41296c92 1354 FORMAT: a char indicating format for the ``word''. See
fb40c209
AC
1355 the ``x'' command.
1356 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
1357 VALUE: value to be written into the memory address.
1358
1359 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1360
41296c92 1361 Prints nothing. */
ce8f13f8 1362void
fb40c209
AC
1363mi_cmd_data_write_memory (char *command, char **argv, int argc)
1364{
e17a4113
UW
1365 struct gdbarch *gdbarch = get_current_arch ();
1366 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
fb40c209
AC
1367 CORE_ADDR addr;
1368 char word_format;
1369 long word_size;
1370 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
41296c92 1371 enough when using a compiler other than GCC. */
fb40c209 1372 LONGEST value;
d8bf3afa
KB
1373 void *buffer;
1374 struct cleanup *old_chain;
fb40c209
AC
1375 long offset = 0;
1376 int optind = 0;
1377 char *optarg;
1378 enum opt
1379 {
1380 OFFSET_OPT
1381 };
1382 static struct mi_opt opts[] =
1383 {
1384 {"o", OFFSET_OPT, 1},
d5d6fca5 1385 { 0, 0, 0 }
fb40c209
AC
1386 };
1387
1388 while (1)
1389 {
1390 int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
1391 &optind, &optarg);
1392 if (opt < 0)
1393 break;
1394 switch ((enum opt) opt)
1395 {
1396 case OFFSET_OPT:
1397 offset = atol (optarg);
1398 break;
1399 }
1400 }
1401 argv += optind;
1402 argc -= optind;
1403
1404 if (argc != 4)
a13e061a 1405 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
fb40c209 1406
41296c92
NR
1407 /* Extract all the arguments. */
1408 /* Start address of the memory dump. */
fb40c209 1409 addr = parse_and_eval_address (argv[0]);
41296c92
NR
1410 /* The format character to use when displaying a memory word. See
1411 the ``x'' command. */
fb40c209
AC
1412 word_format = argv[1][0];
1413 /* The size of the memory word. */
1414 word_size = atol (argv[2]);
1415
41296c92 1416 /* Calculate the real address of the write destination. */
fb40c209
AC
1417 addr += (offset * word_size);
1418
41296c92 1419 /* Get the value as a number. */
fb40c209 1420 value = parse_and_eval_address (argv[3]);
41296c92 1421 /* Get the value into an array. */
d8bf3afa
KB
1422 buffer = xmalloc (word_size);
1423 old_chain = make_cleanup (xfree, buffer);
e17a4113 1424 store_signed_integer (buffer, word_size, byte_order, value);
41296c92 1425 /* Write it down to memory. */
fb40c209 1426 write_memory (addr, buffer, word_size);
d8bf3afa
KB
1427 /* Free the buffer. */
1428 do_cleanups (old_chain);
fb40c209
AC
1429}
1430
ce8f13f8 1431void
d8c83789
NR
1432mi_cmd_enable_timings (char *command, char **argv, int argc)
1433{
1434 if (argc == 0)
1435 do_timings = 1;
1436 else if (argc == 1)
1437 {
1438 if (strcmp (argv[0], "yes") == 0)
1439 do_timings = 1;
1440 else if (strcmp (argv[0], "no") == 0)
1441 do_timings = 0;
1442 else
1443 goto usage_error;
1444 }
1445 else
1446 goto usage_error;
1447
ce8f13f8 1448 return;
d8c83789
NR
1449
1450 usage_error:
1451 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command);
d8c83789
NR
1452}
1453
ce8f13f8 1454void
084344da
VP
1455mi_cmd_list_features (char *command, char **argv, int argc)
1456{
1457 if (argc == 0)
1458 {
1459 struct cleanup *cleanup = NULL;
1460 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
1461
1462 ui_out_field_string (uiout, NULL, "frozen-varobjs");
8b4ed427 1463 ui_out_field_string (uiout, NULL, "pending-breakpoints");
8e8901c5 1464 ui_out_field_string (uiout, NULL, "thread-info");
084344da 1465
b6313243
TT
1466#if HAVE_PYTHON
1467 ui_out_field_string (uiout, NULL, "python");
1468#endif
1469
084344da 1470 do_cleanups (cleanup);
ce8f13f8 1471 return;
084344da
VP
1472 }
1473
1474 error ("-list-features should be passed no arguments");
084344da 1475}
c6ebd6cf
VP
1476
1477void
1478mi_cmd_list_target_features (char *command, char **argv, int argc)
1479{
1480 if (argc == 0)
1481 {
1482 struct cleanup *cleanup = NULL;
1483 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
1484
1485 if (target_can_async_p ())
1486 ui_out_field_string (uiout, NULL, "async");
1487
1488 do_cleanups (cleanup);
1489 return;
1490 }
1491
1492 error ("-list-target-features should be passed no arguments");
1493}
1494
8d34ea23
KS
1495/* Execute a command within a safe environment.
1496 Return <0 for error; >=0 for ok.
1497
1498 args->action will tell mi_execute_command what action
42972f50 1499 to perfrom after the given command has executed (display/suppress
8d34ea23 1500 prompt, display error). */
fb40c209 1501
f30f06b8 1502static void
8d34ea23 1503captured_mi_execute_command (struct ui_out *uiout, void *data)
fb40c209 1504{
1f31650a 1505 struct cleanup *cleanup;
e111d6c9 1506 struct mi_parse *context = (struct mi_parse *) data;
fb40c209 1507
4333ada3
VP
1508 if (do_timings)
1509 current_command_ts = context->cmd_start;
d8c83789 1510
1f31650a
VP
1511 current_token = xstrdup (context->token);
1512 cleanup = make_cleanup (free_current_contents, &current_token);
1513
a2840c35 1514 running_result_record_printed = 0;
f3b1572e 1515 mi_proceeded = 0;
fb40c209
AC
1516 switch (context->op)
1517 {
fb40c209 1518 case MI_COMMAND:
41296c92 1519 /* A MI command was read from the input stream. */
fb40c209
AC
1520 if (mi_debug_p)
1521 /* FIXME: gdb_???? */
1522 fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
1523 context->token, context->command, context->args);
d8c83789 1524
d8c83789 1525
ce8f13f8 1526 mi_cmd_execute (context);
8d34ea23 1527
a2840c35 1528 /* Print the result if there were no errors.
4389a95a 1529
a2840c35
VP
1530 Remember that on the way out of executing a command, you have
1531 to directly use the mi_interp's uiout, since the command could
1532 have reset the interpreter, in which case the current uiout
1533 will most likely crash in the mi_out_* routines. */
ce8f13f8 1534 if (!running_result_record_printed)
a2840c35
VP
1535 {
1536 fputs_unfiltered (context->token, raw_stdout);
ce8f13f8
VP
1537 /* There's no particularly good reason why target-connect results
1538 in not ^done. Should kill ^connected for MI3. */
1539 fputs_unfiltered (strcmp (context->command, "target-select") == 0
1540 ? "^connected" : "^done", raw_stdout);
a2840c35
VP
1541 mi_out_put (uiout, raw_stdout);
1542 mi_out_rewind (uiout);
4333ada3 1543 mi_print_timing_maybe ();
a2840c35
VP
1544 fputs_unfiltered ("\n", raw_stdout);
1545 }
1546 else
f7f9a841
VP
1547 /* The command does not want anything to be printed. In that
1548 case, the command probably should not have written anything
1549 to uiout, but in case it has written something, discard it. */
a2840c35 1550 mi_out_rewind (uiout);
fb40c209
AC
1551 break;
1552
1553 case CLI_COMMAND:
78f5381d
AC
1554 {
1555 char *argv[2];
1556 /* A CLI command was read from the input stream. */
1557 /* This "feature" will be removed as soon as we have a
1558 complete set of mi commands. */
1559 /* Echo the command on the console. */
1560 fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1561 /* Call the "console" interpreter. */
1562 argv[0] = "console";
1563 argv[1] = context->command;
ce8f13f8 1564 mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);
78f5381d 1565
eec01795 1566 /* If we changed interpreters, DON'T print out anything. */
78f5381d
AC
1567 if (current_interp_named_p (INTERP_MI)
1568 || current_interp_named_p (INTERP_MI1)
1569 || current_interp_named_p (INTERP_MI2)
1570 || current_interp_named_p (INTERP_MI3))
1571 {
ce8f13f8 1572 if (!running_result_record_printed)
eec01795
DJ
1573 {
1574 fputs_unfiltered (context->token, raw_stdout);
1575 fputs_unfiltered ("^done", raw_stdout);
1576 mi_out_put (uiout, raw_stdout);
1577 mi_out_rewind (uiout);
4333ada3
VP
1578 mi_print_timing_maybe ();
1579 fputs_unfiltered ("\n", raw_stdout);
eec01795 1580 }
eec01795
DJ
1581 else
1582 mi_out_rewind (uiout);
78f5381d
AC
1583 }
1584 break;
1585 }
fb40c209
AC
1586
1587 }
8d34ea23 1588
1f31650a
VP
1589 do_cleanups (cleanup);
1590
f30f06b8 1591 return;
fb40c209
AC
1592}
1593
1594
1595void
1596mi_execute_command (char *cmd, int from_tty)
1597{
1598 struct mi_parse *command;
8d34ea23 1599 struct ui_out *saved_uiout = uiout;
fb40c209 1600
41296c92
NR
1601 /* This is to handle EOF (^D). We just quit gdb. */
1602 /* FIXME: we should call some API function here. */
fb40c209
AC
1603 if (cmd == 0)
1604 quit_force (NULL, from_tty);
1605
11334b82
VP
1606 target_log_command (cmd);
1607
fb40c209
AC
1608 command = mi_parse (cmd);
1609
1610 if (command != NULL)
1611 {
71fff37b 1612 struct gdb_exception result;
66bb093b 1613 ptid_t previous_ptid = inferior_ptid;
d8c83789
NR
1614
1615 if (do_timings)
1616 {
1617 command->cmd_start = (struct mi_timestamp *)
1618 xmalloc (sizeof (struct mi_timestamp));
1619 timestamp (command->cmd_start);
1620 }
1621
e111d6c9 1622 result = catch_exception (uiout, captured_mi_execute_command, command,
f30f06b8 1623 RETURN_MASK_ALL);
ce43223b 1624 if (result.reason < 0)
fb40c209 1625 {
fb40c209 1626 /* The command execution failed and error() was called
589e074d 1627 somewhere. */
fb40c209
AC
1628 fputs_unfiltered (command->token, raw_stdout);
1629 fputs_unfiltered ("^error,msg=\"", raw_stdout);
63f06803
DJ
1630 if (result.message == NULL)
1631 fputs_unfiltered ("unknown error", raw_stdout);
1632 else
a13e061a 1633 fputstr_unfiltered (result.message, '"', raw_stdout);
fb40c209 1634 fputs_unfiltered ("\"\n", raw_stdout);
589e074d 1635 mi_out_rewind (uiout);
fb40c209 1636 }
a13e061a 1637
5d4e2b76
VP
1638 bpstat_do_actions ();
1639
66bb093b
VP
1640 if (/* The notifications are only output when the top-level
1641 interpreter (specified on the command line) is MI. */
1642 ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
1643 /* Don't try report anything if there are no threads --
1644 the program is dead. */
1645 && thread_count () != 0
1646 /* -thread-select explicitly changes thread. If frontend uses that
1647 internally, we don't want to emit =thread-selected, since
1648 =thread-selected is supposed to indicate user's intentions. */
1649 && strcmp (command->command, "thread-select") != 0)
1650 {
1651 struct mi_interp *mi = top_level_interpreter_data ();
d729566a 1652 int report_change = 0;
66bb093b
VP
1653
1654 if (command->thread == -1)
1655 {
d729566a
PA
1656 report_change = (!ptid_equal (previous_ptid, null_ptid)
1657 && !ptid_equal (inferior_ptid, previous_ptid)
1658 && !ptid_equal (inferior_ptid, null_ptid));
66bb093b 1659 }
d729566a 1660 else if (!ptid_equal (inferior_ptid, null_ptid))
66bb093b 1661 {
d729566a 1662 struct thread_info *ti = inferior_thread ();
66bb093b
VP
1663 report_change = (ti->num != command->thread);
1664 }
1665
1666 if (report_change)
1667 {
d729566a 1668 struct thread_info *ti = inferior_thread ();
66bb093b
VP
1669 target_terminal_ours ();
1670 fprintf_unfiltered (mi->event_channel,
1671 "thread-selected,id=\"%d\"",
1672 ti->num);
1673 gdb_flush (mi->event_channel);
1674 }
1675 }
1676
fb40c209
AC
1677 mi_parse_free (command);
1678 }
1679
fb40c209 1680 fputs_unfiltered ("(gdb) \n", raw_stdout);
a433f9e4 1681 gdb_flush (raw_stdout);
41296c92 1682 /* Print any buffered hook code. */
fb40c209
AC
1683 /* ..... */
1684}
1685
ce8f13f8 1686static void
fb40c209
AC
1687mi_cmd_execute (struct mi_parse *parse)
1688{
f107f563 1689 struct cleanup *cleanup;
1e92afda 1690 int i;
e23110bb 1691
4e5d721f
DE
1692 prepare_execute_command ();
1693
1f31650a 1694 cleanup = make_cleanup (null_cleanup, NULL);
1b98914a 1695
1e92afda
VP
1696 if (parse->frame != -1 && parse->thread == -1)
1697 error (_("Cannot specify --frame without --thread"));
dcf4fbde 1698
1e92afda
VP
1699 if (parse->thread != -1)
1700 {
1701 struct thread_info *tp = find_thread_id (parse->thread);
1702 if (!tp)
1703 error (_("Invalid thread id: %d"), parse->thread);
dcf4fbde
PA
1704
1705 if (is_exited (tp->ptid))
1706 error (_("Thread id: %d has terminated"), parse->thread);
1707
1708 switch_to_thread (tp->ptid);
1e92afda 1709 }
dcf4fbde 1710
1e92afda
VP
1711 if (parse->frame != -1)
1712 {
1713 struct frame_info *fid;
1714 int frame = parse->frame;
1715 fid = find_relative_frame (get_current_frame (), &frame);
1716 if (frame == 0)
1717 /* find_relative_frame was successful */
1718 select_frame (fid);
1719 else
ea069267 1720 error (_("Invalid frame id: %d"), frame);
1e92afda 1721 }
dcf4fbde 1722
9e22b03a 1723 if (parse->cmd->argv_func != NULL)
d729566a 1724 parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
b2af646b 1725 else if (parse->cmd->cli.cmd != 0)
fb40c209
AC
1726 {
1727 /* FIXME: DELETE THIS. */
41296c92
NR
1728 /* The operation is still implemented by a cli command. */
1729 /* Must be a synchronous one. */
b2af646b
AC
1730 mi_execute_cli_command (parse->cmd->cli.cmd, parse->cmd->cli.args_p,
1731 parse->args);
fb40c209
AC
1732 }
1733 else
1734 {
41296c92 1735 /* FIXME: DELETE THIS. */
a13e061a
PA
1736 struct ui_file *stb;
1737
1738 stb = mem_fileopen ();
1739
1740 fputs_unfiltered ("Undefined mi command: ", stb);
1741 fputstr_unfiltered (parse->command, '"', stb);
1742 fputs_unfiltered (" (missing implementation)", stb);
1743
1744 make_cleanup_ui_file_delete (stb);
1745 error_stream (stb);
fb40c209 1746 }
1b98914a 1747 do_cleanups (cleanup);
fb40c209
AC
1748}
1749
fb40c209 1750/* FIXME: This is just a hack so we can get some extra commands going.
41296c92
NR
1751 We don't want to channel things through the CLI, but call libgdb directly.
1752 Use only for synchronous commands. */
fb40c209
AC
1753
1754void
b2af646b 1755mi_execute_cli_command (const char *cmd, int args_p, const char *args)
fb40c209 1756{
b2af646b 1757 if (cmd != 0)
fb40c209
AC
1758 {
1759 struct cleanup *old_cleanups;
1760 char *run;
b2af646b 1761 if (args_p)
c6902d46 1762 run = xstrprintf ("%s %s", cmd, args);
b2af646b
AC
1763 else
1764 run = xstrdup (cmd);
fb40c209
AC
1765 if (mi_debug_p)
1766 /* FIXME: gdb_???? */
1767 fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
b2af646b 1768 cmd, run);
b8c9b27d 1769 old_cleanups = make_cleanup (xfree, run);
fb40c209
AC
1770 execute_command ( /*ui */ run, 0 /*from_tty */ );
1771 do_cleanups (old_cleanups);
1772 return;
1773 }
1774}
1775
ce8f13f8 1776void
9e22b03a 1777mi_execute_async_cli_command (char *cli_command, char **argv, int argc)
fb40c209
AC
1778{
1779 struct cleanup *old_cleanups;
1780 char *run;
fb40c209
AC
1781
1782 if (target_can_async_p ())
9e22b03a 1783 run = xstrprintf ("%s %s&", cli_command, argc ? *argv : "");
fb40c209 1784 else
9e22b03a 1785 run = xstrprintf ("%s %s", cli_command, argc ? *argv : "");
f107f563 1786 old_cleanups = make_cleanup (xfree, run);
fb40c209 1787
fb40c209
AC
1788 execute_command ( /*ui */ run, 0 /*from_tty */ );
1789
f107f563
VP
1790 if (target_can_async_p ())
1791 {
1792 /* If we're not executing, an exception should have been throw. */
8ea051c5 1793 gdb_assert (is_running (inferior_ptid));
f107f563
VP
1794 do_cleanups (old_cleanups);
1795 }
1796 else
fb40c209
AC
1797 {
1798 /* Do this before doing any printing. It would appear that some
41296c92 1799 print code leaves garbage around in the buffer. */
fb40c209 1800 do_cleanups (old_cleanups);
ce8f13f8 1801 }
fb40c209
AC
1802}
1803
1804void
fb40c209
AC
1805mi_load_progress (const char *section_name,
1806 unsigned long sent_so_far,
1807 unsigned long total_section,
1808 unsigned long total_sent,
1809 unsigned long grand_total)
1810{
1811 struct timeval time_now, delta, update_threshold;
1812 static struct timeval last_update;
1813 static char *previous_sect_name = NULL;
1814 int new_section;
0be75e02 1815 struct ui_out *saved_uiout;
fb40c209 1816
0be75e02
AS
1817 /* This function is called through deprecated_show_load_progress
1818 which means uiout may not be correct. Fix it for the duration
1819 of this function. */
1820 saved_uiout = uiout;
1821
edff0c0a
DJ
1822 if (current_interp_named_p (INTERP_MI)
1823 || current_interp_named_p (INTERP_MI2))
0be75e02
AS
1824 uiout = mi_out_new (2);
1825 else if (current_interp_named_p (INTERP_MI1))
1826 uiout = mi_out_new (1);
edff0c0a
DJ
1827 else if (current_interp_named_p (INTERP_MI3))
1828 uiout = mi_out_new (3);
0be75e02 1829 else
fb40c209
AC
1830 return;
1831
1832 update_threshold.tv_sec = 0;
1833 update_threshold.tv_usec = 500000;
1834 gettimeofday (&time_now, NULL);
1835
1836 delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
1837 delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
1838
1839 if (delta.tv_usec < 0)
1840 {
1841 delta.tv_sec -= 1;
f2395593 1842 delta.tv_usec += 1000000L;
fb40c209
AC
1843 }
1844
1845 new_section = (previous_sect_name ?
1846 strcmp (previous_sect_name, section_name) : 1);
1847 if (new_section)
1848 {
6ad4a2cf 1849 struct cleanup *cleanup_tuple;
b8c9b27d 1850 xfree (previous_sect_name);
fb40c209
AC
1851 previous_sect_name = xstrdup (section_name);
1852
721c02de
VP
1853 if (current_token)
1854 fputs_unfiltered (current_token, raw_stdout);
fb40c209 1855 fputs_unfiltered ("+download", raw_stdout);
6ad4a2cf 1856 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
fb40c209
AC
1857 ui_out_field_string (uiout, "section", section_name);
1858 ui_out_field_int (uiout, "section-size", total_section);
1859 ui_out_field_int (uiout, "total-size", grand_total);
6ad4a2cf 1860 do_cleanups (cleanup_tuple);
fb40c209
AC
1861 mi_out_put (uiout, raw_stdout);
1862 fputs_unfiltered ("\n", raw_stdout);
1863 gdb_flush (raw_stdout);
1864 }
1865
1866 if (delta.tv_sec >= update_threshold.tv_sec &&
1867 delta.tv_usec >= update_threshold.tv_usec)
1868 {
6ad4a2cf 1869 struct cleanup *cleanup_tuple;
fb40c209
AC
1870 last_update.tv_sec = time_now.tv_sec;
1871 last_update.tv_usec = time_now.tv_usec;
721c02de
VP
1872 if (current_token)
1873 fputs_unfiltered (current_token, raw_stdout);
fb40c209 1874 fputs_unfiltered ("+download", raw_stdout);
6ad4a2cf 1875 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
fb40c209
AC
1876 ui_out_field_string (uiout, "section", section_name);
1877 ui_out_field_int (uiout, "section-sent", sent_so_far);
1878 ui_out_field_int (uiout, "section-size", total_section);
1879 ui_out_field_int (uiout, "total-sent", total_sent);
1880 ui_out_field_int (uiout, "total-size", grand_total);
6ad4a2cf 1881 do_cleanups (cleanup_tuple);
fb40c209
AC
1882 mi_out_put (uiout, raw_stdout);
1883 fputs_unfiltered ("\n", raw_stdout);
1884 gdb_flush (raw_stdout);
1885 }
0be75e02
AS
1886
1887 xfree (uiout);
1888 uiout = saved_uiout;
fb40c209
AC
1889}
1890
d8c83789
NR
1891static void
1892timestamp (struct mi_timestamp *tv)
1893 {
1894 long usec;
1895 gettimeofday (&tv->wallclock, NULL);
1896#ifdef HAVE_GETRUSAGE
1897 getrusage (RUSAGE_SELF, &rusage);
1898 tv->utime.tv_sec = rusage.ru_utime.tv_sec;
1899 tv->utime.tv_usec = rusage.ru_utime.tv_usec;
1900 tv->stime.tv_sec = rusage.ru_stime.tv_sec;
1901 tv->stime.tv_usec = rusage.ru_stime.tv_usec;
1902#else
1903 usec = get_run_time ();
f2395593
NR
1904 tv->utime.tv_sec = usec/1000000L;
1905 tv->utime.tv_usec = usec - 1000000L*tv->utime.tv_sec;
d8c83789
NR
1906 tv->stime.tv_sec = 0;
1907 tv->stime.tv_usec = 0;
1908#endif
1909 }
1910
1911static void
1912print_diff_now (struct mi_timestamp *start)
1913 {
1914 struct mi_timestamp now;
1915 timestamp (&now);
1916 print_diff (start, &now);
1917 }
1918
4333ada3
VP
1919void
1920mi_print_timing_maybe (void)
1921{
1922 /* If the command is -enable-timing then do_timings may be
1923 true whilst current_command_ts is not initialized. */
1924 if (do_timings && current_command_ts)
1925 print_diff_now (current_command_ts);
1926}
1927
d8c83789
NR
1928static long
1929timeval_diff (struct timeval start, struct timeval end)
1930 {
f2395593 1931 return ((end.tv_sec - start.tv_sec) * 1000000L)
d8c83789
NR
1932 + (end.tv_usec - start.tv_usec);
1933 }
1934
1935static void
1936print_diff (struct mi_timestamp *start, struct mi_timestamp *end)
1937 {
1938 fprintf_unfiltered
1939 (raw_stdout,
1940 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1941 timeval_diff (start->wallclock, end->wallclock) / 1000000.0,
1942 timeval_diff (start->utime, end->utime) / 1000000.0,
1943 timeval_diff (start->stime, end->stime) / 1000000.0);
1944 }
This page took 0.920319 seconds and 4 git commands to generate.