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