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