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