3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
4 Free Software Foundation, Inc.
6 Contributed by Cygnus Solutions (a Red Hat company).
8 This file is part of GDB.
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
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* Work in progress. */
28 #include "gdb_string.h"
29 #include "exceptions.h"
31 #include "gdbthread.h"
34 #include "mi-getopt.h"
35 #include "mi-console.h"
39 #include "event-loop.h"
40 #include "event-top.h"
41 #include "gdbcore.h" /* For write_memory(). */
51 #if defined HAVE_SYS_RESOURCE_H
52 #include <sys/resource.h>
64 /* Enumerations of the actions that may result from calling
65 captured_mi_execute_command. */
67 enum captured_mi_execute_command_actions
69 EXECUTE_COMMAND_DISPLAY_PROMPT
,
70 EXECUTE_COMMAND_SUPPRESS_PROMPT
73 /* This structure is used to pass information from captured_mi_execute_command
74 to mi_execute_command. */
75 struct captured_mi_execute_command_args
77 /* This return result of the MI command (output). */
78 enum mi_cmd_result rc
;
80 /* What action to perform when the call is finished (output). */
81 enum captured_mi_execute_command_actions action
;
83 /* The command context to be executed (input). */
84 struct mi_parse
*command
;
88 struct ui_file
*raw_stdout
;
90 /* This is used to pass the current command timestamp
91 down to continuation routines. */
92 static struct mi_timestamp
*current_command_ts
;
94 static int do_timings
= 0;
96 /* The token of the last asynchronous command. */
97 static char *last_async_command
;
98 static char *previous_async_command
;
100 extern void _initialize_mi_main (void);
101 static enum mi_cmd_result
mi_cmd_execute (struct mi_parse
*parse
);
103 static void mi_execute_cli_command (const char *cmd
, int args_p
,
105 static enum mi_cmd_result
mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
);
107 static void mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
);
109 static int register_changed_p (int regnum
, struct regcache
*,
111 static void get_register (int regnum
, int format
);
113 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
114 layer that calls libgdb. Any operation used in the below should be
117 static void timestamp (struct mi_timestamp
*tv
);
119 static void print_diff_now (struct mi_timestamp
*start
);
120 static void print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
);
123 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
125 /* We have to print everything right here because we never return. */
126 if (last_async_command
)
127 fputs_unfiltered (last_async_command
, raw_stdout
);
128 fputs_unfiltered ("^exit\n", raw_stdout
);
129 mi_out_put (uiout
, raw_stdout
);
130 /* FIXME: The function called is not yet a formal libgdb function. */
131 quit_force (NULL
, FROM_TTY
);
136 mi_cmd_exec_run (char *args
, int from_tty
)
138 /* FIXME: Should call a libgdb function, not a cli wrapper. */
139 return mi_execute_async_cli_command ("run", args
, from_tty
);
143 mi_cmd_exec_next (char *args
, int from_tty
)
145 /* FIXME: Should call a libgdb function, not a cli wrapper. */
146 return mi_execute_async_cli_command ("next", args
, from_tty
);
150 mi_cmd_exec_next_instruction (char *args
, int from_tty
)
152 /* FIXME: Should call a libgdb function, not a cli wrapper. */
153 return mi_execute_async_cli_command ("nexti", args
, from_tty
);
157 mi_cmd_exec_step (char *args
, int from_tty
)
159 /* FIXME: Should call a libgdb function, not a cli wrapper. */
160 return mi_execute_async_cli_command ("step", args
, from_tty
);
164 mi_cmd_exec_step_instruction (char *args
, int from_tty
)
166 /* FIXME: Should call a libgdb function, not a cli wrapper. */
167 return mi_execute_async_cli_command ("stepi", args
, from_tty
);
171 mi_cmd_exec_finish (char *args
, int from_tty
)
173 /* FIXME: Should call a libgdb function, not a cli wrapper. */
174 return mi_execute_async_cli_command ("finish", args
, from_tty
);
178 mi_cmd_exec_until (char *args
, int from_tty
)
180 /* FIXME: Should call a libgdb function, not a cli wrapper. */
181 return mi_execute_async_cli_command ("until", args
, from_tty
);
185 mi_cmd_exec_return (char *args
, int from_tty
)
187 /* This command doesn't really execute the target, it just pops the
188 specified number of frames. */
190 /* Call return_command with from_tty argument equal to 0 so as to
191 avoid being queried. */
192 return_command (args
, 0);
194 /* Call return_command with from_tty argument equal to 0 so as to
195 avoid being queried. */
196 return_command (NULL
, 0);
198 /* Because we have called return_command with from_tty = 0, we need
199 to print the frame here. */
200 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
);
206 mi_cmd_exec_continue (char *args
, int from_tty
)
208 /* FIXME: Should call a libgdb function, not a cli wrapper. */
209 return mi_execute_async_cli_command ("continue", args
, from_tty
);
212 /* Interrupt the execution of the target. Note how we must play around
213 with the token variables, in order to display the current token in
214 the result of the interrupt command, and the previous execution
215 token when the target finally stops. See comments in
218 mi_cmd_exec_interrupt (char *args
, int from_tty
)
220 if (!target_executing
)
221 error ("mi_cmd_exec_interrupt: Inferior not executing.");
223 interrupt_target_command (args
, from_tty
);
224 if (last_async_command
)
225 fputs_unfiltered (last_async_command
, raw_stdout
);
226 fputs_unfiltered ("^done", raw_stdout
);
227 xfree (last_async_command
);
228 if (previous_async_command
)
229 last_async_command
= xstrdup (previous_async_command
);
230 xfree (previous_async_command
);
231 previous_async_command
= NULL
;
232 mi_out_put (uiout
, raw_stdout
);
233 mi_out_rewind (uiout
);
234 fputs_unfiltered ("\n", raw_stdout
);
239 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
242 char *mi_error_message
;
245 error ("mi_cmd_thread_select: USAGE: threadnum.");
247 rc
= gdb_thread_select (uiout
, argv
[0], &mi_error_message
);
249 if (rc
== GDB_RC_FAIL
)
251 make_cleanup (xfree
, mi_error_message
);
252 error ("%s", mi_error_message
);
259 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
262 char *mi_error_message
;
265 error ("mi_cmd_thread_list_ids: No arguments required.");
267 rc
= gdb_list_thread_ids (uiout
, &mi_error_message
);
269 if (rc
== GDB_RC_FAIL
)
271 make_cleanup (xfree
, mi_error_message
);
272 error ("%s", mi_error_message
);
279 mi_cmd_thread_info (char *command
, char **argv
, int argc
)
283 if (argc
!= 0 && argc
!= 1)
284 error ("Invalid MI command");
287 thread
= atoi (argv
[0]);
289 print_thread_info (uiout
, thread
);
294 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
298 struct cleanup
*cleanup
;
300 /* Note that the test for a valid register must include checking the
301 gdbarch_register_name because gdbarch_num_regs may be allocated for
302 the union of the register sets within a family of related processors.
303 In this case, some entries of gdbarch_register_name will change depending
304 upon the particular processor being debugged. */
306 numregs
= gdbarch_num_regs (current_gdbarch
)
307 + gdbarch_num_pseudo_regs (current_gdbarch
);
309 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
311 if (argc
== 0) /* No args, just do all the regs. */
317 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
318 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
319 ui_out_field_string (uiout
, NULL
, "");
321 ui_out_field_string (uiout
, NULL
,
322 gdbarch_register_name
323 (current_gdbarch
, regnum
));
327 /* Else, list of register #s, just do listed regs. */
328 for (i
= 0; i
< argc
; i
++)
330 regnum
= atoi (argv
[i
]);
331 if (regnum
< 0 || regnum
>= numregs
)
332 error ("bad register number");
334 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
335 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
336 ui_out_field_string (uiout
, NULL
, "");
338 ui_out_field_string (uiout
, NULL
,
339 gdbarch_register_name (current_gdbarch
, regnum
));
341 do_cleanups (cleanup
);
346 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
348 static struct regcache
*this_regs
= NULL
;
349 struct regcache
*prev_regs
;
350 int regnum
, numregs
, changed
;
352 struct cleanup
*cleanup
;
354 /* The last time we visited this function, the current frame's register
355 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
356 and refresh THIS_REGS with the now-current register contents. */
358 prev_regs
= this_regs
;
359 this_regs
= frame_save_as_regcache (get_selected_frame (NULL
));
360 cleanup
= make_cleanup_regcache_xfree (prev_regs
);
362 /* Note that the test for a valid register must include checking the
363 gdbarch_register_name because gdbarch_num_regs may be allocated for
364 the union of the register sets within a family of related processors.
365 In this case, some entries of gdbarch_register_name will change depending
366 upon the particular processor being debugged. */
368 numregs
= gdbarch_num_regs (current_gdbarch
)
369 + gdbarch_num_pseudo_regs (current_gdbarch
);
371 make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
373 if (argc
== 0) /* No args, just do all the regs. */
379 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
380 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
382 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
384 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
386 ui_out_field_int (uiout
, NULL
, regnum
);
390 /* Else, list of register #s, just do listed regs. */
391 for (i
= 0; i
< argc
; i
++)
393 regnum
= atoi (argv
[i
]);
397 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
398 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
400 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
402 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
404 ui_out_field_int (uiout
, NULL
, regnum
);
407 error ("bad register number");
409 do_cleanups (cleanup
);
414 register_changed_p (int regnum
, struct regcache
*prev_regs
,
415 struct regcache
*this_regs
)
417 struct gdbarch
*gdbarch
= get_regcache_arch (this_regs
);
418 gdb_byte prev_buffer
[MAX_REGISTER_SIZE
];
419 gdb_byte this_buffer
[MAX_REGISTER_SIZE
];
421 /* Registers not valid in this frame return count as unchanged. */
422 if (!regcache_valid_p (this_regs
, regnum
))
425 /* First time through or after gdbarch change consider all registers as
426 changed. Same for registers not valid in the previous frame. */
427 if (!prev_regs
|| get_regcache_arch (prev_regs
) != gdbarch
428 || !regcache_valid_p (prev_regs
, regnum
))
431 /* Get register contents and compare. */
432 regcache_cooked_read (prev_regs
, regnum
, prev_buffer
);
433 regcache_cooked_read (this_regs
, regnum
, this_buffer
);
435 return memcmp (prev_buffer
, this_buffer
,
436 register_size (gdbarch
, regnum
)) != 0;
439 /* Return a list of register number and value pairs. The valid
440 arguments expected are: a letter indicating the format in which to
441 display the registers contents. This can be one of: x (hexadecimal), d
442 (decimal), N (natural), t (binary), o (octal), r (raw). After the
443 format argumetn there can be a sequence of numbers, indicating which
444 registers to fetch the content of. If the format is the only argument,
445 a list of all the registers with their values is returned. */
447 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
449 int regnum
, numregs
, format
;
451 struct cleanup
*list_cleanup
, *tuple_cleanup
;
453 /* Note that the test for a valid register must include checking the
454 gdbarch_register_name because gdbarch_num_regs may be allocated for
455 the union of the register sets within a family of related processors.
456 In this case, some entries of gdbarch_register_name will change depending
457 upon the particular processor being debugged. */
459 numregs
= gdbarch_num_regs (current_gdbarch
)
460 + gdbarch_num_pseudo_regs (current_gdbarch
);
463 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
465 format
= (int) argv
[0][0];
467 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
469 if (argc
== 1) /* No args, beside the format: do all the regs. */
475 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
476 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
478 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
479 ui_out_field_int (uiout
, "number", regnum
);
480 get_register (regnum
, format
);
481 do_cleanups (tuple_cleanup
);
485 /* Else, list of register #s, just do listed regs. */
486 for (i
= 1; i
< argc
; i
++)
488 regnum
= atoi (argv
[i
]);
492 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
493 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
495 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
496 ui_out_field_int (uiout
, "number", regnum
);
497 get_register (regnum
, format
);
498 do_cleanups (tuple_cleanup
);
501 error ("bad register number");
503 do_cleanups (list_cleanup
);
507 /* Output one register's contents in the desired format. */
509 get_register (int regnum
, int format
)
511 gdb_byte buffer
[MAX_REGISTER_SIZE
];
516 static struct ui_stream
*stb
= NULL
;
518 stb
= ui_out_stream_new (uiout
);
523 frame_register (get_selected_frame (NULL
), regnum
, &optim
, &lval
, &addr
,
527 error ("Optimized out");
532 char *ptr
, buf
[1024];
536 for (j
= 0; j
< register_size (current_gdbarch
, regnum
); j
++)
538 int idx
= gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
? j
539 : register_size (current_gdbarch
, regnum
) - 1 - j
;
540 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
543 ui_out_field_string (uiout
, "value", buf
);
544 /*fputs_filtered (buf, gdb_stdout); */
548 val_print (register_type (current_gdbarch
, regnum
), buffer
, 0, 0,
549 stb
->stream
, format
, 1, 0, Val_pretty_default
);
550 ui_out_field_stream (uiout
, "value", stb
);
551 ui_out_stream_delete (stb
);
555 /* Write given values into registers. The registers and values are
556 given as pairs. The corresponding MI command is
557 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
559 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
564 /* Note that the test for a valid register must include checking the
565 gdbarch_register_name because gdbarch_num_regs may be allocated for
566 the union of the register sets within a family of related processors.
567 In this case, some entries of gdbarch_register_name will change depending
568 upon the particular processor being debugged. */
570 numregs
= gdbarch_num_regs (current_gdbarch
)
571 + gdbarch_num_pseudo_regs (current_gdbarch
);
574 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
576 format
= (int) argv
[0][0];
578 if (!target_has_registers
)
579 error ("mi_cmd_data_write_register_values: No registers.");
582 error ("mi_cmd_data_write_register_values: No regs and values specified.");
585 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
587 for (i
= 1; i
< argc
; i
= i
+ 2)
589 int regnum
= atoi (argv
[i
]);
591 if (regnum
>= 0 && regnum
< numregs
592 && gdbarch_register_name (current_gdbarch
, regnum
)
593 && *gdbarch_register_name (current_gdbarch
, regnum
))
597 /* Get the value as a number. */
598 value
= parse_and_eval_address (argv
[i
+ 1]);
601 regcache_cooked_write_signed (get_current_regcache (), regnum
, value
);
604 error ("bad register number");
609 /* Evaluate the value of the argument. The argument is an
610 expression. If the expression contains spaces it needs to be
611 included in double quotes. */
613 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
615 struct expression
*expr
;
616 struct cleanup
*old_chain
= NULL
;
618 struct ui_stream
*stb
= NULL
;
620 stb
= ui_out_stream_new (uiout
);
624 ui_out_stream_delete (stb
);
625 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
628 expr
= parse_expression (argv
[0]);
630 old_chain
= make_cleanup (free_current_contents
, &expr
);
632 val
= evaluate_expression (expr
);
634 /* Print the result of the expression evaluation. */
635 val_print (value_type (val
), value_contents (val
),
636 value_embedded_offset (val
), VALUE_ADDRESS (val
),
637 stb
->stream
, 0, 0, 0, 0);
639 ui_out_field_stream (uiout
, "value", stb
);
640 ui_out_stream_delete (stb
);
642 do_cleanups (old_chain
);
648 mi_cmd_target_download (char *args
, int from_tty
)
651 struct cleanup
*old_cleanups
= NULL
;
653 run
= xstrprintf ("load %s", args
);
654 old_cleanups
= make_cleanup (xfree
, run
);
655 execute_command (run
, from_tty
);
657 do_cleanups (old_cleanups
);
661 /* Connect to the remote target. */
663 mi_cmd_target_select (char *args
, int from_tty
)
666 struct cleanup
*old_cleanups
= NULL
;
668 run
= xstrprintf ("target %s", args
);
669 old_cleanups
= make_cleanup (xfree
, run
);
671 /* target-select is always synchronous. Once the call has returned
672 we know that we are connected. */
673 /* NOTE: At present all targets that are connected are also
674 (implicitly) talking to a halted target. In the future this may
676 execute_command (run
, from_tty
);
678 do_cleanups (old_cleanups
);
680 /* Issue the completion message here. */
681 if (last_async_command
)
682 fputs_unfiltered (last_async_command
, raw_stdout
);
683 fputs_unfiltered ("^connected", raw_stdout
);
684 mi_out_put (uiout
, raw_stdout
);
685 mi_out_rewind (uiout
);
686 fputs_unfiltered ("\n", raw_stdout
);
687 do_exec_cleanups (ALL_CLEANUPS
);
693 ADDR: start address of data to be dumped.
694 WORD-FORMAT: a char indicating format for the ``word''. See
696 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
697 NR_ROW: Number of rows.
698 NR_COL: The number of colums (words per row).
699 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
700 ASCHAR for unprintable characters.
702 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
703 displayes them. Returns:
705 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
708 The number of bytes read is SIZE*ROW*COL. */
711 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
713 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
719 struct type
*word_type
;
732 static struct mi_opt opts
[] =
734 {"o", OFFSET_OPT
, 1},
740 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
744 switch ((enum opt
) opt
)
747 offset
= atol (optarg
);
754 if (argc
< 5 || argc
> 6)
755 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
757 /* Extract all the arguments. */
759 /* Start address of the memory dump. */
760 addr
= parse_and_eval_address (argv
[0]) + offset
;
761 /* The format character to use when displaying a memory word. See
762 the ``x'' command. */
763 word_format
= argv
[1][0];
764 /* The size of the memory word. */
765 word_size
= atol (argv
[2]);
769 word_type
= builtin_type_int8
;
773 word_type
= builtin_type_int16
;
777 word_type
= builtin_type_int32
;
781 word_type
= builtin_type_int64
;
785 word_type
= builtin_type_int8
;
788 /* The number of rows. */
789 nr_rows
= atol (argv
[3]);
791 error ("mi_cmd_data_read_memory: invalid number of rows.");
793 /* Number of bytes per row. */
794 nr_cols
= atol (argv
[4]);
796 error ("mi_cmd_data_read_memory: invalid number of columns.");
798 /* The un-printable character when printing ascii. */
804 /* Create a buffer and read it in. */
805 total_bytes
= word_size
* nr_rows
* nr_cols
;
806 mbuf
= xcalloc (total_bytes
, 1);
807 make_cleanup (xfree
, mbuf
);
809 nr_bytes
= target_read (¤t_target
, TARGET_OBJECT_MEMORY
, NULL
,
810 mbuf
, addr
, total_bytes
);
812 error ("Unable to read memory.");
814 /* Output the header information. */
815 ui_out_field_core_addr (uiout
, "addr", addr
);
816 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
817 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
818 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
819 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
820 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
821 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
823 /* Build the result as a two dimentional table. */
825 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
826 struct cleanup
*cleanup_list_memory
;
829 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
830 for (row
= 0, row_byte
= 0;
832 row
++, row_byte
+= nr_cols
* word_size
)
836 struct cleanup
*cleanup_tuple
;
837 struct cleanup
*cleanup_list_data
;
838 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
839 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
840 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
841 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
842 for (col
= 0, col_byte
= row_byte
;
844 col
++, col_byte
+= word_size
)
846 if (col_byte
+ word_size
> nr_bytes
)
848 ui_out_field_string (uiout
, NULL
, "N/A");
852 ui_file_rewind (stream
->stream
);
853 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
854 word_asize
, stream
->stream
);
855 ui_out_field_stream (uiout
, NULL
, stream
);
858 do_cleanups (cleanup_list_data
);
862 ui_file_rewind (stream
->stream
);
863 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
865 if (byte
>= nr_bytes
)
867 fputc_unfiltered ('X', stream
->stream
);
869 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
871 fputc_unfiltered (aschar
, stream
->stream
);
874 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
876 ui_out_field_stream (uiout
, "ascii", stream
);
878 do_cleanups (cleanup_tuple
);
880 ui_out_stream_delete (stream
);
881 do_cleanups (cleanup_list_memory
);
883 do_cleanups (cleanups
);
887 /* DATA-MEMORY-WRITE:
889 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
890 offset from the beginning of the memory grid row where the cell to
892 ADDR: start address of the row in the memory grid where the memory
893 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
894 the location to write to.
895 FORMAT: a char indicating format for the ``word''. See
897 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
898 VALUE: value to be written into the memory address.
900 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
904 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
909 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
910 enough when using a compiler other than GCC. */
913 struct cleanup
*old_chain
;
921 static struct mi_opt opts
[] =
923 {"o", OFFSET_OPT
, 1},
929 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
933 switch ((enum opt
) opt
)
936 offset
= atol (optarg
);
944 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
946 /* Extract all the arguments. */
947 /* Start address of the memory dump. */
948 addr
= parse_and_eval_address (argv
[0]);
949 /* The format character to use when displaying a memory word. See
950 the ``x'' command. */
951 word_format
= argv
[1][0];
952 /* The size of the memory word. */
953 word_size
= atol (argv
[2]);
955 /* Calculate the real address of the write destination. */
956 addr
+= (offset
* word_size
);
958 /* Get the value as a number. */
959 value
= parse_and_eval_address (argv
[3]);
960 /* Get the value into an array. */
961 buffer
= xmalloc (word_size
);
962 old_chain
= make_cleanup (xfree
, buffer
);
963 store_signed_integer (buffer
, word_size
, value
);
964 /* Write it down to memory. */
965 write_memory (addr
, buffer
, word_size
);
966 /* Free the buffer. */
967 do_cleanups (old_chain
);
973 mi_cmd_enable_timings (char *command
, char **argv
, int argc
)
979 if (strcmp (argv
[0], "yes") == 0)
981 else if (strcmp (argv
[0], "no") == 0)
992 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command
);
997 mi_cmd_list_features (char *command
, char **argv
, int argc
)
1001 struct cleanup
*cleanup
= NULL
;
1002 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
1004 ui_out_field_string (uiout
, NULL
, "frozen-varobjs");
1005 ui_out_field_string (uiout
, NULL
, "pending-breakpoints");
1006 ui_out_field_string (uiout
, NULL
, "thread-info");
1008 do_cleanups (cleanup
);
1013 error ("-list-features should be passed no arguments");
1017 /* Execute a command within a safe environment.
1018 Return <0 for error; >=0 for ok.
1020 args->action will tell mi_execute_command what action
1021 to perfrom after the given command has executed (display/suppress
1022 prompt, display error). */
1025 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1027 struct captured_mi_execute_command_args
*args
=
1028 (struct captured_mi_execute_command_args
*) data
;
1029 struct mi_parse
*context
= args
->command
;
1031 struct mi_timestamp cmd_finished
;
1033 switch (context
->op
)
1037 /* A MI command was read from the input stream. */
1039 /* FIXME: gdb_???? */
1040 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1041 context
->token
, context
->command
, context
->args
);
1042 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1043 condition expression, each function should return an
1044 indication of what action is required and then switch on
1046 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1049 current_command_ts
= context
->cmd_start
;
1051 args
->rc
= mi_cmd_execute (context
);
1054 timestamp (&cmd_finished
);
1056 if (!target_can_async_p () || !target_executing
)
1058 /* Print the result if there were no errors.
1060 Remember that on the way out of executing a command, you have
1061 to directly use the mi_interp's uiout, since the command could
1062 have reset the interpreter, in which case the current uiout
1063 will most likely crash in the mi_out_* routines. */
1064 if (args
->rc
== MI_CMD_DONE
)
1066 fputs_unfiltered (context
->token
, raw_stdout
);
1067 fputs_unfiltered ("^done", raw_stdout
);
1068 mi_out_put (uiout
, raw_stdout
);
1069 mi_out_rewind (uiout
);
1070 /* Have to check cmd_start, since the command could be
1072 if (do_timings
&& context
->cmd_start
)
1073 print_diff (context
->cmd_start
, &cmd_finished
);
1074 fputs_unfiltered ("\n", raw_stdout
);
1077 mi_out_rewind (uiout
);
1079 else if (sync_execution
)
1081 /* Don't print the prompt. We are executing the target in
1082 synchronous mode. */
1083 args
->action
= EXECUTE_COMMAND_SUPPRESS_PROMPT
;
1091 /* A CLI command was read from the input stream. */
1092 /* This "feature" will be removed as soon as we have a
1093 complete set of mi commands. */
1094 /* Echo the command on the console. */
1095 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1096 /* Call the "console" interpreter. */
1097 argv
[0] = "console";
1098 argv
[1] = context
->command
;
1099 args
->rc
= mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
1101 /* If we changed interpreters, DON'T print out anything. */
1102 if (current_interp_named_p (INTERP_MI
)
1103 || current_interp_named_p (INTERP_MI1
)
1104 || current_interp_named_p (INTERP_MI2
)
1105 || current_interp_named_p (INTERP_MI3
))
1107 if (args
->rc
== MI_CMD_DONE
)
1109 fputs_unfiltered (context
->token
, raw_stdout
);
1110 fputs_unfiltered ("^done", raw_stdout
);
1111 mi_out_put (uiout
, raw_stdout
);
1112 mi_out_rewind (uiout
);
1113 fputs_unfiltered ("\n", raw_stdout
);
1114 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1117 mi_out_rewind (uiout
);
1129 mi_execute_command (char *cmd
, int from_tty
)
1131 struct mi_parse
*command
;
1132 struct captured_mi_execute_command_args args
;
1133 struct ui_out
*saved_uiout
= uiout
;
1135 /* This is to handle EOF (^D). We just quit gdb. */
1136 /* FIXME: we should call some API function here. */
1138 quit_force (NULL
, from_tty
);
1140 command
= mi_parse (cmd
);
1142 if (command
!= NULL
)
1144 struct gdb_exception result
;
1148 command
->cmd_start
= (struct mi_timestamp
*)
1149 xmalloc (sizeof (struct mi_timestamp
));
1150 timestamp (command
->cmd_start
);
1153 args
.command
= command
;
1154 result
= catch_exception (uiout
, captured_mi_execute_command
, &args
,
1156 if (result
.reason
< 0)
1158 /* The command execution failed and error() was called
1160 fputs_unfiltered (command
->token
, raw_stdout
);
1161 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1162 if (result
.message
== NULL
)
1163 fputs_unfiltered ("unknown error", raw_stdout
);
1165 fputstr_unfiltered (result
.message
, '"', raw_stdout
);
1166 fputs_unfiltered ("\"\n", raw_stdout
);
1167 mi_out_rewind (uiout
);
1170 mi_parse_free (command
);
1172 if (args
.action
== EXECUTE_COMMAND_SUPPRESS_PROMPT
)
1173 /* The command is executing synchronously. Bail out early
1174 suppressing the finished prompt. */
1178 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1179 gdb_flush (raw_stdout
);
1180 /* Print any buffered hook code. */
1184 static enum mi_cmd_result
1185 mi_cmd_execute (struct mi_parse
*parse
)
1189 if (parse
->cmd
->argv_func
!= NULL
1190 || parse
->cmd
->args_func
!= NULL
)
1192 /* FIXME: We need to save the token because the command executed
1193 may be asynchronous and need to print the token again.
1194 In the future we can pass the token down to the func
1195 and get rid of the last_async_command. */
1196 /* The problem here is to keep the token around when we launch
1197 the target, and we want to interrupt it later on. The
1198 interrupt command will have its own token, but when the
1199 target stops, we must display the token corresponding to the
1200 last execution command given. So we have another string where
1201 we copy the token (previous_async_command), if this was
1202 indeed the token of an execution command, and when we stop we
1203 print that one. This is possible because the interrupt
1204 command, when over, will copy that token back into the
1205 default token string (last_async_command). */
1207 if (target_executing
)
1209 if (!previous_async_command
)
1210 previous_async_command
= xstrdup (last_async_command
);
1211 if (strcmp (parse
->command
, "exec-interrupt"))
1213 struct ui_file
*stb
;
1214 stb
= mem_fileopen ();
1216 fputs_unfiltered ("Cannot execute command ", stb
);
1217 fputstr_unfiltered (parse
->command
, '"', stb
);
1218 fputs_unfiltered (" while target running", stb
);
1220 make_cleanup_ui_file_delete (stb
);
1224 last_async_command
= xstrdup (parse
->token
);
1225 make_exec_cleanup (free_current_contents
, &last_async_command
);
1226 /* FIXME: DELETE THIS! */
1227 if (parse
->cmd
->args_func
!= NULL
)
1228 return parse
->cmd
->args_func (parse
->args
, 0 /*from_tty */ );
1229 return parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1231 else if (parse
->cmd
->cli
.cmd
!= 0)
1233 /* FIXME: DELETE THIS. */
1234 /* The operation is still implemented by a cli command. */
1235 /* Must be a synchronous one. */
1236 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1242 /* FIXME: DELETE THIS. */
1243 struct ui_file
*stb
;
1245 stb
= mem_fileopen ();
1247 fputs_unfiltered ("Undefined mi command: ", stb
);
1248 fputstr_unfiltered (parse
->command
, '"', stb
);
1249 fputs_unfiltered (" (missing implementation)", stb
);
1251 make_cleanup_ui_file_delete (stb
);
1259 /* FIXME: This is just a hack so we can get some extra commands going.
1260 We don't want to channel things through the CLI, but call libgdb directly.
1261 Use only for synchronous commands. */
1264 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1268 struct cleanup
*old_cleanups
;
1271 run
= xstrprintf ("%s %s", cmd
, args
);
1273 run
= xstrdup (cmd
);
1275 /* FIXME: gdb_???? */
1276 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1278 old_cleanups
= make_cleanup (xfree
, run
);
1279 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1280 do_cleanups (old_cleanups
);
1286 mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
)
1288 struct cleanup
*old_cleanups
;
1292 if (target_can_async_p ())
1294 async_args
= (char *) xmalloc (strlen (args
) + 2);
1295 make_exec_cleanup (free
, async_args
);
1296 strcpy (async_args
, args
);
1297 strcat (async_args
, "&");
1298 run
= xstrprintf ("%s %s", mi
, async_args
);
1299 make_exec_cleanup (free
, run
);
1300 add_continuation (mi_exec_async_cli_cmd_continuation
, NULL
);
1301 old_cleanups
= NULL
;
1305 run
= xstrprintf ("%s %s", mi
, args
);
1306 old_cleanups
= make_cleanup (xfree
, run
);
1309 if (!target_can_async_p ())
1311 /* NOTE: For synchronous targets asynchronous behavour is faked by
1312 printing out the GDB prompt before we even try to execute the
1314 if (last_async_command
)
1315 fputs_unfiltered (last_async_command
, raw_stdout
);
1316 fputs_unfiltered ("^running\n", raw_stdout
);
1317 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1318 gdb_flush (raw_stdout
);
1322 /* FIXME: cagney/1999-11-29: Printing this message before
1323 calling execute_command is wrong. It should only be printed
1324 once gdb has confirmed that it really has managed to send a
1325 run command to the target. */
1326 if (last_async_command
)
1327 fputs_unfiltered (last_async_command
, raw_stdout
);
1328 fputs_unfiltered ("^running\n", raw_stdout
);
1331 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1333 if (!target_can_async_p ())
1335 /* Do this before doing any printing. It would appear that some
1336 print code leaves garbage around in the buffer. */
1337 do_cleanups (old_cleanups
);
1338 /* If the target was doing the operation synchronously we fake
1339 the stopped message. */
1340 if (last_async_command
)
1341 fputs_unfiltered (last_async_command
, raw_stdout
);
1342 fputs_unfiltered ("*stopped", raw_stdout
);
1343 mi_out_put (uiout
, raw_stdout
);
1344 mi_out_rewind (uiout
);
1346 print_diff_now (current_command_ts
);
1347 fputs_unfiltered ("\n", raw_stdout
);
1348 return MI_CMD_QUIET
;
1354 mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
)
1356 if (last_async_command
)
1357 fputs_unfiltered (last_async_command
, raw_stdout
);
1358 fputs_unfiltered ("*stopped", raw_stdout
);
1359 mi_out_put (uiout
, raw_stdout
);
1360 fputs_unfiltered ("\n", raw_stdout
);
1361 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1362 gdb_flush (raw_stdout
);
1363 do_exec_cleanups (ALL_CLEANUPS
);
1367 mi_load_progress (const char *section_name
,
1368 unsigned long sent_so_far
,
1369 unsigned long total_section
,
1370 unsigned long total_sent
,
1371 unsigned long grand_total
)
1373 struct timeval time_now
, delta
, update_threshold
;
1374 static struct timeval last_update
;
1375 static char *previous_sect_name
= NULL
;
1377 struct ui_out
*saved_uiout
;
1379 /* This function is called through deprecated_show_load_progress
1380 which means uiout may not be correct. Fix it for the duration
1381 of this function. */
1382 saved_uiout
= uiout
;
1384 if (current_interp_named_p (INTERP_MI
)
1385 || current_interp_named_p (INTERP_MI2
))
1386 uiout
= mi_out_new (2);
1387 else if (current_interp_named_p (INTERP_MI1
))
1388 uiout
= mi_out_new (1);
1389 else if (current_interp_named_p (INTERP_MI3
))
1390 uiout
= mi_out_new (3);
1394 update_threshold
.tv_sec
= 0;
1395 update_threshold
.tv_usec
= 500000;
1396 gettimeofday (&time_now
, NULL
);
1398 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1399 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1401 if (delta
.tv_usec
< 0)
1404 delta
.tv_usec
+= 1000000L;
1407 new_section
= (previous_sect_name
?
1408 strcmp (previous_sect_name
, section_name
) : 1);
1411 struct cleanup
*cleanup_tuple
;
1412 xfree (previous_sect_name
);
1413 previous_sect_name
= xstrdup (section_name
);
1415 if (last_async_command
)
1416 fputs_unfiltered (last_async_command
, raw_stdout
);
1417 fputs_unfiltered ("+download", raw_stdout
);
1418 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1419 ui_out_field_string (uiout
, "section", section_name
);
1420 ui_out_field_int (uiout
, "section-size", total_section
);
1421 ui_out_field_int (uiout
, "total-size", grand_total
);
1422 do_cleanups (cleanup_tuple
);
1423 mi_out_put (uiout
, raw_stdout
);
1424 fputs_unfiltered ("\n", raw_stdout
);
1425 gdb_flush (raw_stdout
);
1428 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1429 delta
.tv_usec
>= update_threshold
.tv_usec
)
1431 struct cleanup
*cleanup_tuple
;
1432 last_update
.tv_sec
= time_now
.tv_sec
;
1433 last_update
.tv_usec
= time_now
.tv_usec
;
1434 if (last_async_command
)
1435 fputs_unfiltered (last_async_command
, raw_stdout
);
1436 fputs_unfiltered ("+download", raw_stdout
);
1437 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1438 ui_out_field_string (uiout
, "section", section_name
);
1439 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1440 ui_out_field_int (uiout
, "section-size", total_section
);
1441 ui_out_field_int (uiout
, "total-sent", total_sent
);
1442 ui_out_field_int (uiout
, "total-size", grand_total
);
1443 do_cleanups (cleanup_tuple
);
1444 mi_out_put (uiout
, raw_stdout
);
1445 fputs_unfiltered ("\n", raw_stdout
);
1446 gdb_flush (raw_stdout
);
1450 uiout
= saved_uiout
;
1454 timestamp (struct mi_timestamp
*tv
)
1457 gettimeofday (&tv
->wallclock
, NULL
);
1458 #ifdef HAVE_GETRUSAGE
1459 getrusage (RUSAGE_SELF
, &rusage
);
1460 tv
->utime
.tv_sec
= rusage
.ru_utime
.tv_sec
;
1461 tv
->utime
.tv_usec
= rusage
.ru_utime
.tv_usec
;
1462 tv
->stime
.tv_sec
= rusage
.ru_stime
.tv_sec
;
1463 tv
->stime
.tv_usec
= rusage
.ru_stime
.tv_usec
;
1465 usec
= get_run_time ();
1466 tv
->utime
.tv_sec
= usec
/1000000L;
1467 tv
->utime
.tv_usec
= usec
- 1000000L*tv
->utime
.tv_sec
;
1468 tv
->stime
.tv_sec
= 0;
1469 tv
->stime
.tv_usec
= 0;
1474 print_diff_now (struct mi_timestamp
*start
)
1476 struct mi_timestamp now
;
1478 print_diff (start
, &now
);
1482 timeval_diff (struct timeval start
, struct timeval end
)
1484 return ((end
.tv_sec
- start
.tv_sec
) * 1000000L)
1485 + (end
.tv_usec
- start
.tv_usec
);
1489 print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
)
1493 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1494 timeval_diff (start
->wallclock
, end
->wallclock
) / 1000000.0,
1495 timeval_diff (start
->utime
, end
->utime
) / 1000000.0,
1496 timeval_diff (start
->stime
, end
->stime
) / 1000000.0);