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