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