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(). */
52 #if defined HAVE_SYS_RESOURCE_H
53 #include <sys/resource.h>
65 /* Enumerations of the actions that may result from calling
66 captured_mi_execute_command. */
68 enum captured_mi_execute_command_actions
70 EXECUTE_COMMAND_DISPLAY_PROMPT
,
71 EXECUTE_COMMAND_SUPPRESS_PROMPT
74 /* This structure is used to pass information from captured_mi_execute_command
75 to mi_execute_command. */
76 struct captured_mi_execute_command_args
78 /* This return result of the MI command (output). */
79 enum mi_cmd_result rc
;
81 /* What action to perform when the call is finished (output). */
82 enum captured_mi_execute_command_actions action
;
84 /* The command context to be executed (input). */
85 struct mi_parse
*command
;
89 struct ui_file
*raw_stdout
;
91 /* This is used to pass the current command timestamp
92 down to continuation routines. */
93 static struct mi_timestamp
*current_command_ts
;
95 static int do_timings
= 0;
98 int running_result_record_printed
= 1;
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 *cli_command
,
106 char **argv
, int argc
);
107 static int register_changed_p (int regnum
, struct regcache
*,
109 static void get_register (int regnum
, int format
);
111 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
112 layer that calls libgdb. Any operation used in the below should be
115 static void timestamp (struct mi_timestamp
*tv
);
117 static void print_diff_now (struct mi_timestamp
*start
);
118 static void print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
);
121 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
123 /* We have to print everything right here because we never return. */
125 fputs_unfiltered (current_token
, raw_stdout
);
126 fputs_unfiltered ("^exit\n", raw_stdout
);
127 mi_out_put (uiout
, raw_stdout
);
128 /* FIXME: The function called is not yet a formal libgdb function. */
129 quit_force (NULL
, FROM_TTY
);
134 mi_cmd_exec_run (char *command
, char **argv
, int argc
)
136 /* FIXME: Should call a libgdb function, not a cli wrapper. */
137 return mi_execute_async_cli_command ("run", argv
, argc
);
141 mi_cmd_exec_next (char *command
, char **argv
, int argc
)
143 /* FIXME: Should call a libgdb function, not a cli wrapper. */
144 return mi_execute_async_cli_command ("next", argv
, argc
);
148 mi_cmd_exec_next_instruction (char *command
, char **argv
, int argc
)
150 /* FIXME: Should call a libgdb function, not a cli wrapper. */
151 return mi_execute_async_cli_command ("nexti", argv
, argc
);
155 mi_cmd_exec_step (char *command
, char **argv
, int argc
)
157 /* FIXME: Should call a libgdb function, not a cli wrapper. */
158 return mi_execute_async_cli_command ("step", argv
, argc
);
162 mi_cmd_exec_step_instruction (char *command
, char **argv
, int argc
)
164 /* FIXME: Should call a libgdb function, not a cli wrapper. */
165 return mi_execute_async_cli_command ("stepi", argv
, argc
);
169 mi_cmd_exec_finish (char *command
, char **argv
, int argc
)
171 /* FIXME: Should call a libgdb function, not a cli wrapper. */
172 return mi_execute_async_cli_command ("finish", argv
, argc
);
176 mi_cmd_exec_until (char *command
, char **argv
, int argc
)
178 /* FIXME: Should call a libgdb function, not a cli wrapper. */
179 return mi_execute_async_cli_command ("until", argv
, argc
);
183 mi_cmd_exec_return (char *command
, char **argv
, int argc
)
185 /* This command doesn't really execute the target, it just pops the
186 specified number of frames. */
188 /* Call return_command with from_tty argument equal to 0 so as to
189 avoid being queried. */
190 return_command (*argv
, 0);
192 /* Call return_command with from_tty argument equal to 0 so as to
193 avoid being queried. */
194 return_command (NULL
, 0);
196 /* Because we have called return_command with from_tty = 0, we need
197 to print the frame here. */
198 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
);
204 mi_cmd_exec_continue (char *command
, char **argv
, int argc
)
206 /* FIXME: Should call a libgdb function, not a cli wrapper. */
207 return mi_execute_async_cli_command ("continue", argv
, argc
);
210 /* Interrupt the execution of the target. Note how we must play around
211 with the token variables, in order to display the current token in
212 the result of the interrupt command, and the previous execution
213 token when the target finally stops. See comments in
216 mi_cmd_exec_interrupt (char *command
, char **argv
, int argc
)
218 if (!target_executing
)
219 error ("mi_cmd_exec_interrupt: Inferior not executing.");
221 interrupt_target_command (NULL
, 0);
223 fputs_unfiltered (current_token
, raw_stdout
);
224 fputs_unfiltered ("^done", raw_stdout
);
225 mi_out_put (uiout
, raw_stdout
);
226 mi_out_rewind (uiout
);
227 fputs_unfiltered ("\n", raw_stdout
);
232 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
235 char *mi_error_message
;
238 error ("mi_cmd_thread_select: USAGE: threadnum.");
240 rc
= gdb_thread_select (uiout
, argv
[0], &mi_error_message
);
242 if (rc
== GDB_RC_FAIL
)
244 make_cleanup (xfree
, mi_error_message
);
245 error ("%s", mi_error_message
);
252 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
255 char *mi_error_message
;
258 error ("mi_cmd_thread_list_ids: No arguments required.");
260 rc
= gdb_list_thread_ids (uiout
, &mi_error_message
);
262 if (rc
== GDB_RC_FAIL
)
264 make_cleanup (xfree
, mi_error_message
);
265 error ("%s", mi_error_message
);
272 mi_cmd_thread_info (char *command
, char **argv
, int argc
)
276 if (argc
!= 0 && argc
!= 1)
277 error ("Invalid MI command");
280 thread
= atoi (argv
[0]);
282 print_thread_info (uiout
, thread
);
287 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
291 struct cleanup
*cleanup
;
293 /* Note that the test for a valid register must include checking the
294 gdbarch_register_name because gdbarch_num_regs may be allocated for
295 the union of the register sets within a family of related processors.
296 In this case, some entries of gdbarch_register_name will change depending
297 upon the particular processor being debugged. */
299 numregs
= gdbarch_num_regs (current_gdbarch
)
300 + gdbarch_num_pseudo_regs (current_gdbarch
);
302 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
304 if (argc
== 0) /* No args, just do all the regs. */
310 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
311 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
312 ui_out_field_string (uiout
, NULL
, "");
314 ui_out_field_string (uiout
, NULL
,
315 gdbarch_register_name
316 (current_gdbarch
, regnum
));
320 /* Else, list of register #s, just do listed regs. */
321 for (i
= 0; i
< argc
; i
++)
323 regnum
= atoi (argv
[i
]);
324 if (regnum
< 0 || regnum
>= numregs
)
325 error ("bad register number");
327 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
328 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
329 ui_out_field_string (uiout
, NULL
, "");
331 ui_out_field_string (uiout
, NULL
,
332 gdbarch_register_name (current_gdbarch
, regnum
));
334 do_cleanups (cleanup
);
339 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
341 static struct regcache
*this_regs
= NULL
;
342 struct regcache
*prev_regs
;
343 int regnum
, numregs
, changed
;
345 struct cleanup
*cleanup
;
347 /* The last time we visited this function, the current frame's register
348 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
349 and refresh THIS_REGS with the now-current register contents. */
351 prev_regs
= this_regs
;
352 this_regs
= frame_save_as_regcache (get_selected_frame (NULL
));
353 cleanup
= make_cleanup_regcache_xfree (prev_regs
);
355 /* Note that the test for a valid register must include checking the
356 gdbarch_register_name because gdbarch_num_regs may be allocated for
357 the union of the register sets within a family of related processors.
358 In this case, some entries of gdbarch_register_name will change depending
359 upon the particular processor being debugged. */
361 numregs
= gdbarch_num_regs (current_gdbarch
)
362 + gdbarch_num_pseudo_regs (current_gdbarch
);
364 make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
366 if (argc
== 0) /* No args, just do all the regs. */
372 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
373 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
375 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
377 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
379 ui_out_field_int (uiout
, NULL
, regnum
);
383 /* Else, list of register #s, just do listed regs. */
384 for (i
= 0; i
< argc
; i
++)
386 regnum
= atoi (argv
[i
]);
390 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
391 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
393 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
395 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
397 ui_out_field_int (uiout
, NULL
, regnum
);
400 error ("bad register number");
402 do_cleanups (cleanup
);
407 register_changed_p (int regnum
, struct regcache
*prev_regs
,
408 struct regcache
*this_regs
)
410 struct gdbarch
*gdbarch
= get_regcache_arch (this_regs
);
411 gdb_byte prev_buffer
[MAX_REGISTER_SIZE
];
412 gdb_byte this_buffer
[MAX_REGISTER_SIZE
];
414 /* Registers not valid in this frame return count as unchanged. */
415 if (!regcache_valid_p (this_regs
, regnum
))
418 /* First time through or after gdbarch change consider all registers as
419 changed. Same for registers not valid in the previous frame. */
420 if (!prev_regs
|| get_regcache_arch (prev_regs
) != gdbarch
421 || !regcache_valid_p (prev_regs
, regnum
))
424 /* Get register contents and compare. */
425 regcache_cooked_read (prev_regs
, regnum
, prev_buffer
);
426 regcache_cooked_read (this_regs
, regnum
, this_buffer
);
428 return memcmp (prev_buffer
, this_buffer
,
429 register_size (gdbarch
, regnum
)) != 0;
432 /* Return a list of register number and value pairs. The valid
433 arguments expected are: a letter indicating the format in which to
434 display the registers contents. This can be one of: x (hexadecimal), d
435 (decimal), N (natural), t (binary), o (octal), r (raw). After the
436 format argumetn there can be a sequence of numbers, indicating which
437 registers to fetch the content of. If the format is the only argument,
438 a list of all the registers with their values is returned. */
440 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
442 int regnum
, numregs
, format
;
444 struct cleanup
*list_cleanup
, *tuple_cleanup
;
446 /* Note that the test for a valid register must include checking the
447 gdbarch_register_name because gdbarch_num_regs may be allocated for
448 the union of the register sets within a family of related processors.
449 In this case, some entries of gdbarch_register_name will change depending
450 upon the particular processor being debugged. */
452 numregs
= gdbarch_num_regs (current_gdbarch
)
453 + gdbarch_num_pseudo_regs (current_gdbarch
);
456 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
458 format
= (int) argv
[0][0];
460 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
462 if (argc
== 1) /* No args, beside the format: do all the regs. */
468 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
469 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
471 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
472 ui_out_field_int (uiout
, "number", regnum
);
473 get_register (regnum
, format
);
474 do_cleanups (tuple_cleanup
);
478 /* Else, list of register #s, just do listed regs. */
479 for (i
= 1; i
< argc
; i
++)
481 regnum
= atoi (argv
[i
]);
485 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
486 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
488 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
489 ui_out_field_int (uiout
, "number", regnum
);
490 get_register (regnum
, format
);
491 do_cleanups (tuple_cleanup
);
494 error ("bad register number");
496 do_cleanups (list_cleanup
);
500 /* Output one register's contents in the desired format. */
502 get_register (int regnum
, int format
)
504 gdb_byte buffer
[MAX_REGISTER_SIZE
];
509 static struct ui_stream
*stb
= NULL
;
511 stb
= ui_out_stream_new (uiout
);
516 frame_register (get_selected_frame (NULL
), regnum
, &optim
, &lval
, &addr
,
520 error ("Optimized out");
525 char *ptr
, buf
[1024];
529 for (j
= 0; j
< register_size (current_gdbarch
, regnum
); j
++)
531 int idx
= gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
? j
532 : register_size (current_gdbarch
, regnum
) - 1 - j
;
533 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
536 ui_out_field_string (uiout
, "value", buf
);
537 /*fputs_filtered (buf, gdb_stdout); */
541 val_print (register_type (current_gdbarch
, regnum
), buffer
, 0, 0,
542 stb
->stream
, format
, 1, 0, Val_pretty_default
,
544 ui_out_field_stream (uiout
, "value", stb
);
545 ui_out_stream_delete (stb
);
549 /* Write given values into registers. The registers and values are
550 given as pairs. The corresponding MI command is
551 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
553 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
558 /* Note that the test for a valid register must include checking the
559 gdbarch_register_name because gdbarch_num_regs may be allocated for
560 the union of the register sets within a family of related processors.
561 In this case, some entries of gdbarch_register_name will change depending
562 upon the particular processor being debugged. */
564 numregs
= gdbarch_num_regs (current_gdbarch
)
565 + gdbarch_num_pseudo_regs (current_gdbarch
);
568 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
570 format
= (int) argv
[0][0];
572 if (!target_has_registers
)
573 error ("mi_cmd_data_write_register_values: No registers.");
576 error ("mi_cmd_data_write_register_values: No regs and values specified.");
579 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
581 for (i
= 1; i
< argc
; i
= i
+ 2)
583 int regnum
= atoi (argv
[i
]);
585 if (regnum
>= 0 && regnum
< numregs
586 && gdbarch_register_name (current_gdbarch
, regnum
)
587 && *gdbarch_register_name (current_gdbarch
, regnum
))
591 /* Get the value as a number. */
592 value
= parse_and_eval_address (argv
[i
+ 1]);
595 regcache_cooked_write_signed (get_current_regcache (), regnum
, value
);
598 error ("bad register number");
603 /* Evaluate the value of the argument. The argument is an
604 expression. If the expression contains spaces it needs to be
605 included in double quotes. */
607 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
609 struct expression
*expr
;
610 struct cleanup
*old_chain
= NULL
;
612 struct ui_stream
*stb
= NULL
;
614 stb
= ui_out_stream_new (uiout
);
618 ui_out_stream_delete (stb
);
619 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
622 expr
= parse_expression (argv
[0]);
624 old_chain
= make_cleanup (free_current_contents
, &expr
);
626 val
= evaluate_expression (expr
);
628 /* Print the result of the expression evaluation. */
629 val_print (value_type (val
), value_contents (val
),
630 value_embedded_offset (val
), VALUE_ADDRESS (val
),
631 stb
->stream
, 0, 0, 0, 0, current_language
);
633 ui_out_field_stream (uiout
, "value", stb
);
634 ui_out_stream_delete (stb
);
636 do_cleanups (old_chain
);
642 mi_cmd_target_download (char *command
, char **argv
, int argc
)
645 struct cleanup
*old_cleanups
= NULL
;
647 /* There may be at most one parameter -- the name of the
649 run
= xstrprintf ("load %s", argc
? *argv
: "");
650 old_cleanups
= make_cleanup (xfree
, run
);
651 execute_command (run
, 0);
653 do_cleanups (old_cleanups
);
657 /* Connect to the remote target. */
659 mi_cmd_target_select (char *command
, char **argv
, int argc
)
662 struct cleanup
*old_cleanups
= NULL
;
666 error ("no target type specified");
668 for (i
= 0; i
< argc
; ++i
)
671 run
= concat ("target ", argv
[0], NULL
);
675 run
= concat (run
, " ", argv
[i
], NULL
);
680 old_cleanups
= make_cleanup (xfree
, run
);
682 /* target-select is always synchronous. Once the call has returned
683 we know that we are connected. */
684 /* NOTE: At present all targets that are connected are also
685 (implicitly) talking to a halted target. In the future this may
687 execute_command (run
, 0);
689 do_cleanups (old_cleanups
);
691 /* Issue the completion message here. */
693 fputs_unfiltered (current_token
, raw_stdout
);
694 fputs_unfiltered ("^connected", raw_stdout
);
695 mi_out_put (uiout
, raw_stdout
);
696 mi_out_rewind (uiout
);
697 fputs_unfiltered ("\n", raw_stdout
);
703 ADDR: start address of data to be dumped.
704 WORD-FORMAT: a char indicating format for the ``word''. See
706 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
707 NR_ROW: Number of rows.
708 NR_COL: The number of colums (words per row).
709 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
710 ASCHAR for unprintable characters.
712 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
713 displayes them. Returns:
715 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
718 The number of bytes read is SIZE*ROW*COL. */
721 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
723 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
729 struct type
*word_type
;
742 static struct mi_opt opts
[] =
744 {"o", OFFSET_OPT
, 1},
750 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
754 switch ((enum opt
) opt
)
757 offset
= atol (optarg
);
764 if (argc
< 5 || argc
> 6)
765 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
767 /* Extract all the arguments. */
769 /* Start address of the memory dump. */
770 addr
= parse_and_eval_address (argv
[0]) + offset
;
771 /* The format character to use when displaying a memory word. See
772 the ``x'' command. */
773 word_format
= argv
[1][0];
774 /* The size of the memory word. */
775 word_size
= atol (argv
[2]);
779 word_type
= builtin_type_int8
;
783 word_type
= builtin_type_int16
;
787 word_type
= builtin_type_int32
;
791 word_type
= builtin_type_int64
;
795 word_type
= builtin_type_int8
;
798 /* The number of rows. */
799 nr_rows
= atol (argv
[3]);
801 error ("mi_cmd_data_read_memory: invalid number of rows.");
803 /* Number of bytes per row. */
804 nr_cols
= atol (argv
[4]);
806 error ("mi_cmd_data_read_memory: invalid number of columns.");
808 /* The un-printable character when printing ascii. */
814 /* Create a buffer and read it in. */
815 total_bytes
= word_size
* nr_rows
* nr_cols
;
816 mbuf
= xcalloc (total_bytes
, 1);
817 make_cleanup (xfree
, mbuf
);
819 nr_bytes
= target_read (¤t_target
, TARGET_OBJECT_MEMORY
, NULL
,
820 mbuf
, addr
, total_bytes
);
822 error ("Unable to read memory.");
824 /* Output the header information. */
825 ui_out_field_core_addr (uiout
, "addr", addr
);
826 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
827 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
828 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
829 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
830 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
831 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
833 /* Build the result as a two dimentional table. */
835 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
836 struct cleanup
*cleanup_list_memory
;
839 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
840 for (row
= 0, row_byte
= 0;
842 row
++, row_byte
+= nr_cols
* word_size
)
846 struct cleanup
*cleanup_tuple
;
847 struct cleanup
*cleanup_list_data
;
848 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
849 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
850 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
851 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
852 for (col
= 0, col_byte
= row_byte
;
854 col
++, col_byte
+= word_size
)
856 if (col_byte
+ word_size
> nr_bytes
)
858 ui_out_field_string (uiout
, NULL
, "N/A");
862 ui_file_rewind (stream
->stream
);
863 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
864 word_asize
, stream
->stream
);
865 ui_out_field_stream (uiout
, NULL
, stream
);
868 do_cleanups (cleanup_list_data
);
872 ui_file_rewind (stream
->stream
);
873 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
875 if (byte
>= nr_bytes
)
877 fputc_unfiltered ('X', stream
->stream
);
879 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
881 fputc_unfiltered (aschar
, stream
->stream
);
884 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
886 ui_out_field_stream (uiout
, "ascii", stream
);
888 do_cleanups (cleanup_tuple
);
890 ui_out_stream_delete (stream
);
891 do_cleanups (cleanup_list_memory
);
893 do_cleanups (cleanups
);
897 /* DATA-MEMORY-WRITE:
899 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
900 offset from the beginning of the memory grid row where the cell to
902 ADDR: start address of the row in the memory grid where the memory
903 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
904 the location to write to.
905 FORMAT: a char indicating format for the ``word''. See
907 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
908 VALUE: value to be written into the memory address.
910 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
914 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
919 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
920 enough when using a compiler other than GCC. */
923 struct cleanup
*old_chain
;
931 static struct mi_opt opts
[] =
933 {"o", OFFSET_OPT
, 1},
939 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
943 switch ((enum opt
) opt
)
946 offset
= atol (optarg
);
954 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
956 /* Extract all the arguments. */
957 /* Start address of the memory dump. */
958 addr
= parse_and_eval_address (argv
[0]);
959 /* The format character to use when displaying a memory word. See
960 the ``x'' command. */
961 word_format
= argv
[1][0];
962 /* The size of the memory word. */
963 word_size
= atol (argv
[2]);
965 /* Calculate the real address of the write destination. */
966 addr
+= (offset
* word_size
);
968 /* Get the value as a number. */
969 value
= parse_and_eval_address (argv
[3]);
970 /* Get the value into an array. */
971 buffer
= xmalloc (word_size
);
972 old_chain
= make_cleanup (xfree
, buffer
);
973 store_signed_integer (buffer
, word_size
, value
);
974 /* Write it down to memory. */
975 write_memory (addr
, buffer
, word_size
);
976 /* Free the buffer. */
977 do_cleanups (old_chain
);
983 mi_cmd_enable_timings (char *command
, char **argv
, int argc
)
989 if (strcmp (argv
[0], "yes") == 0)
991 else if (strcmp (argv
[0], "no") == 0)
1002 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command
);
1007 mi_cmd_list_features (char *command
, char **argv
, int argc
)
1011 struct cleanup
*cleanup
= NULL
;
1012 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
1014 ui_out_field_string (uiout
, NULL
, "frozen-varobjs");
1015 ui_out_field_string (uiout
, NULL
, "pending-breakpoints");
1016 ui_out_field_string (uiout
, NULL
, "thread-info");
1018 do_cleanups (cleanup
);
1023 error ("-list-features should be passed no arguments");
1027 /* Execute a command within a safe environment.
1028 Return <0 for error; >=0 for ok.
1030 args->action will tell mi_execute_command what action
1031 to perfrom after the given command has executed (display/suppress
1032 prompt, display error). */
1035 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1037 struct captured_mi_execute_command_args
*args
=
1038 (struct captured_mi_execute_command_args
*) data
;
1039 struct mi_parse
*context
= args
->command
;
1041 struct mi_timestamp cmd_finished
;
1043 running_result_record_printed
= 0;
1044 switch (context
->op
)
1047 /* A MI command was read from the input stream. */
1049 /* FIXME: gdb_???? */
1050 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1051 context
->token
, context
->command
, context
->args
);
1052 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1053 condition expression, each function should return an
1054 indication of what action is required and then switch on
1056 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1059 current_command_ts
= context
->cmd_start
;
1061 args
->rc
= mi_cmd_execute (context
);
1064 timestamp (&cmd_finished
);
1066 /* Print the result if there were no errors.
1068 Remember that on the way out of executing a command, you have
1069 to directly use the mi_interp's uiout, since the command could
1070 have reset the interpreter, in which case the current uiout
1071 will most likely crash in the mi_out_* routines. */
1072 if (args
->rc
== MI_CMD_DONE
&& !running_result_record_printed
)
1074 fputs_unfiltered (context
->token
, raw_stdout
);
1075 fputs_unfiltered ("^done", raw_stdout
);
1076 mi_out_put (uiout
, raw_stdout
);
1077 mi_out_rewind (uiout
);
1078 /* Have to check cmd_start, since the command could be
1080 if (do_timings
&& context
->cmd_start
)
1081 print_diff (context
->cmd_start
, &cmd_finished
);
1082 fputs_unfiltered ("\n", raw_stdout
);
1085 /* The command does not want anything to be printed. In that
1086 case, the command probably should not have written anything
1087 to uiout, but in case it has written something, discard it. */
1088 mi_out_rewind (uiout
);
1094 /* A CLI command was read from the input stream. */
1095 /* This "feature" will be removed as soon as we have a
1096 complete set of mi commands. */
1097 /* Echo the command on the console. */
1098 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1099 /* Call the "console" interpreter. */
1100 argv
[0] = "console";
1101 argv
[1] = context
->command
;
1102 args
->rc
= mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
1104 /* If we changed interpreters, DON'T print out anything. */
1105 if (current_interp_named_p (INTERP_MI
)
1106 || current_interp_named_p (INTERP_MI1
)
1107 || current_interp_named_p (INTERP_MI2
)
1108 || current_interp_named_p (INTERP_MI3
))
1110 if (args
->rc
== MI_CMD_DONE
&& !running_result_record_printed
)
1112 fputs_unfiltered (context
->token
, raw_stdout
);
1113 fputs_unfiltered ("^done", raw_stdout
);
1114 mi_out_put (uiout
, raw_stdout
);
1115 mi_out_rewind (uiout
);
1116 fputs_unfiltered ("\n", raw_stdout
);
1117 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1120 mi_out_rewind (uiout
);
1132 mi_execute_command (char *cmd
, int from_tty
)
1134 struct mi_parse
*command
;
1135 struct captured_mi_execute_command_args args
;
1136 struct ui_out
*saved_uiout
= uiout
;
1138 /* This is to handle EOF (^D). We just quit gdb. */
1139 /* FIXME: we should call some API function here. */
1141 quit_force (NULL
, from_tty
);
1143 command
= mi_parse (cmd
);
1145 if (command
!= NULL
)
1147 struct gdb_exception result
;
1151 command
->cmd_start
= (struct mi_timestamp
*)
1152 xmalloc (sizeof (struct mi_timestamp
));
1153 timestamp (command
->cmd_start
);
1156 args
.command
= command
;
1157 result
= catch_exception (uiout
, captured_mi_execute_command
, &args
,
1159 if (result
.reason
< 0)
1161 /* The command execution failed and error() was called
1163 fputs_unfiltered (command
->token
, raw_stdout
);
1164 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1165 if (result
.message
== NULL
)
1166 fputs_unfiltered ("unknown error", raw_stdout
);
1168 fputstr_unfiltered (result
.message
, '"', raw_stdout
);
1169 fputs_unfiltered ("\"\n", raw_stdout
);
1170 mi_out_rewind (uiout
);
1173 mi_parse_free (command
);
1175 if (args
.action
== EXECUTE_COMMAND_SUPPRESS_PROMPT
)
1176 /* The command is executing synchronously. Bail out early
1177 suppressing the finished prompt. */
1181 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1182 gdb_flush (raw_stdout
);
1183 /* Print any buffered hook code. */
1187 static enum mi_cmd_result
1188 mi_cmd_execute (struct mi_parse
*parse
)
1190 struct cleanup
*cleanup
;
1191 enum mi_cmd_result r
;
1194 if (parse
->cmd
->argv_func
!= NULL
)
1196 if (target_executing
)
1198 if (strcmp (parse
->command
, "exec-interrupt"))
1200 struct ui_file
*stb
;
1201 stb
= mem_fileopen ();
1203 fputs_unfiltered ("Cannot execute command ", stb
);
1204 fputstr_unfiltered (parse
->command
, '"', stb
);
1205 fputs_unfiltered (" while target running", stb
);
1207 make_cleanup_ui_file_delete (stb
);
1211 current_token
= xstrdup (parse
->token
);
1212 cleanup
= make_cleanup (free_current_contents
, ¤t_token
);
1213 r
= parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1214 do_cleanups (cleanup
);
1217 else if (parse
->cmd
->cli
.cmd
!= 0)
1219 /* FIXME: DELETE THIS. */
1220 /* The operation is still implemented by a cli command. */
1221 /* Must be a synchronous one. */
1222 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1228 /* FIXME: DELETE THIS. */
1229 struct ui_file
*stb
;
1231 stb
= mem_fileopen ();
1233 fputs_unfiltered ("Undefined mi command: ", stb
);
1234 fputstr_unfiltered (parse
->command
, '"', stb
);
1235 fputs_unfiltered (" (missing implementation)", stb
);
1237 make_cleanup_ui_file_delete (stb
);
1245 /* FIXME: This is just a hack so we can get some extra commands going.
1246 We don't want to channel things through the CLI, but call libgdb directly.
1247 Use only for synchronous commands. */
1250 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1254 struct cleanup
*old_cleanups
;
1257 run
= xstrprintf ("%s %s", cmd
, args
);
1259 run
= xstrdup (cmd
);
1261 /* FIXME: gdb_???? */
1262 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1264 old_cleanups
= make_cleanup (xfree
, run
);
1265 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1266 do_cleanups (old_cleanups
);
1272 mi_execute_async_cli_command (char *cli_command
, char **argv
, int argc
)
1274 struct cleanup
*old_cleanups
;
1277 if (target_can_async_p ())
1278 run
= xstrprintf ("%s %s&", cli_command
, argc
? *argv
: "");
1280 run
= xstrprintf ("%s %s", cli_command
, argc
? *argv
: "");
1281 old_cleanups
= make_cleanup (xfree
, run
);
1283 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1285 if (target_can_async_p ())
1287 /* If we're not executing, an exception should have been throw. */
1288 gdb_assert (target_executing
);
1289 do_cleanups (old_cleanups
);
1293 /* Do this before doing any printing. It would appear that some
1294 print code leaves garbage around in the buffer. */
1295 do_cleanups (old_cleanups
);
1297 print_diff_now (current_command_ts
);
1298 return MI_CMD_QUIET
;
1304 mi_load_progress (const char *section_name
,
1305 unsigned long sent_so_far
,
1306 unsigned long total_section
,
1307 unsigned long total_sent
,
1308 unsigned long grand_total
)
1310 struct timeval time_now
, delta
, update_threshold
;
1311 static struct timeval last_update
;
1312 static char *previous_sect_name
= NULL
;
1314 struct ui_out
*saved_uiout
;
1316 /* This function is called through deprecated_show_load_progress
1317 which means uiout may not be correct. Fix it for the duration
1318 of this function. */
1319 saved_uiout
= uiout
;
1321 if (current_interp_named_p (INTERP_MI
)
1322 || current_interp_named_p (INTERP_MI2
))
1323 uiout
= mi_out_new (2);
1324 else if (current_interp_named_p (INTERP_MI1
))
1325 uiout
= mi_out_new (1);
1326 else if (current_interp_named_p (INTERP_MI3
))
1327 uiout
= mi_out_new (3);
1331 update_threshold
.tv_sec
= 0;
1332 update_threshold
.tv_usec
= 500000;
1333 gettimeofday (&time_now
, NULL
);
1335 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1336 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1338 if (delta
.tv_usec
< 0)
1341 delta
.tv_usec
+= 1000000L;
1344 new_section
= (previous_sect_name
?
1345 strcmp (previous_sect_name
, section_name
) : 1);
1348 struct cleanup
*cleanup_tuple
;
1349 xfree (previous_sect_name
);
1350 previous_sect_name
= xstrdup (section_name
);
1353 fputs_unfiltered (current_token
, raw_stdout
);
1354 fputs_unfiltered ("+download", raw_stdout
);
1355 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1356 ui_out_field_string (uiout
, "section", section_name
);
1357 ui_out_field_int (uiout
, "section-size", total_section
);
1358 ui_out_field_int (uiout
, "total-size", grand_total
);
1359 do_cleanups (cleanup_tuple
);
1360 mi_out_put (uiout
, raw_stdout
);
1361 fputs_unfiltered ("\n", raw_stdout
);
1362 gdb_flush (raw_stdout
);
1365 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1366 delta
.tv_usec
>= update_threshold
.tv_usec
)
1368 struct cleanup
*cleanup_tuple
;
1369 last_update
.tv_sec
= time_now
.tv_sec
;
1370 last_update
.tv_usec
= time_now
.tv_usec
;
1372 fputs_unfiltered (current_token
, raw_stdout
);
1373 fputs_unfiltered ("+download", raw_stdout
);
1374 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1375 ui_out_field_string (uiout
, "section", section_name
);
1376 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1377 ui_out_field_int (uiout
, "section-size", total_section
);
1378 ui_out_field_int (uiout
, "total-sent", total_sent
);
1379 ui_out_field_int (uiout
, "total-size", grand_total
);
1380 do_cleanups (cleanup_tuple
);
1381 mi_out_put (uiout
, raw_stdout
);
1382 fputs_unfiltered ("\n", raw_stdout
);
1383 gdb_flush (raw_stdout
);
1387 uiout
= saved_uiout
;
1391 timestamp (struct mi_timestamp
*tv
)
1394 gettimeofday (&tv
->wallclock
, NULL
);
1395 #ifdef HAVE_GETRUSAGE
1396 getrusage (RUSAGE_SELF
, &rusage
);
1397 tv
->utime
.tv_sec
= rusage
.ru_utime
.tv_sec
;
1398 tv
->utime
.tv_usec
= rusage
.ru_utime
.tv_usec
;
1399 tv
->stime
.tv_sec
= rusage
.ru_stime
.tv_sec
;
1400 tv
->stime
.tv_usec
= rusage
.ru_stime
.tv_usec
;
1402 usec
= get_run_time ();
1403 tv
->utime
.tv_sec
= usec
/1000000L;
1404 tv
->utime
.tv_usec
= usec
- 1000000L*tv
->utime
.tv_sec
;
1405 tv
->stime
.tv_sec
= 0;
1406 tv
->stime
.tv_usec
= 0;
1411 print_diff_now (struct mi_timestamp
*start
)
1413 struct mi_timestamp now
;
1415 print_diff (start
, &now
);
1419 timeval_diff (struct timeval start
, struct timeval end
)
1421 return ((end
.tv_sec
- start
.tv_sec
) * 1000000L)
1422 + (end
.tv_usec
- start
.tv_usec
);
1426 print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
)
1430 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1431 timeval_diff (start
->wallclock
, end
->wallclock
) / 1000000.0,
1432 timeval_diff (start
->utime
, end
->utime
) / 1000000.0,
1433 timeval_diff (start
->stime
, end
->stime
) / 1000000.0);