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