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