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