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;
97 static char *current_token
;
99 extern void _initialize_mi_main (void);
100 static enum mi_cmd_result
mi_cmd_execute (struct mi_parse
*parse
);
102 static void mi_execute_cli_command (const char *cmd
, int args_p
,
104 static enum mi_cmd_result
mi_execute_async_cli_command (char *cli_command
,
105 char **argv
, int argc
);
107 static void mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
,
110 static int register_changed_p (int regnum
, struct regcache
*,
112 static void get_register (int regnum
, int format
);
114 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
115 layer that calls libgdb. Any operation used in the below should be
118 static void timestamp (struct mi_timestamp
*tv
);
120 static void print_diff_now (struct mi_timestamp
*start
);
121 static void print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
);
124 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
126 /* We have to print everything right here because we never return. */
128 fputs_unfiltered (current_token
, raw_stdout
);
129 fputs_unfiltered ("^exit\n", raw_stdout
);
130 mi_out_put (uiout
, raw_stdout
);
131 /* FIXME: The function called is not yet a formal libgdb function. */
132 quit_force (NULL
, FROM_TTY
);
137 mi_cmd_exec_run (char *command
, char **argv
, int argc
)
139 /* FIXME: Should call a libgdb function, not a cli wrapper. */
140 return mi_execute_async_cli_command ("run", argv
, argc
);
144 mi_cmd_exec_next (char *command
, char **argv
, int argc
)
146 /* FIXME: Should call a libgdb function, not a cli wrapper. */
147 return mi_execute_async_cli_command ("next", argv
, argc
);
151 mi_cmd_exec_next_instruction (char *command
, char **argv
, int argc
)
153 /* FIXME: Should call a libgdb function, not a cli wrapper. */
154 return mi_execute_async_cli_command ("nexti", argv
, argc
);
158 mi_cmd_exec_step (char *command
, char **argv
, int argc
)
160 /* FIXME: Should call a libgdb function, not a cli wrapper. */
161 return mi_execute_async_cli_command ("step", argv
, argc
);
165 mi_cmd_exec_step_instruction (char *command
, char **argv
, int argc
)
167 /* FIXME: Should call a libgdb function, not a cli wrapper. */
168 return mi_execute_async_cli_command ("stepi", argv
, argc
);
172 mi_cmd_exec_finish (char *command
, char **argv
, int argc
)
174 /* FIXME: Should call a libgdb function, not a cli wrapper. */
175 return mi_execute_async_cli_command ("finish", argv
, argc
);
179 mi_cmd_exec_until (char *command
, char **argv
, int argc
)
181 /* FIXME: Should call a libgdb function, not a cli wrapper. */
182 return mi_execute_async_cli_command ("until", argv
, argc
);
186 mi_cmd_exec_return (char *command
, char **argv
, int argc
)
188 /* This command doesn't really execute the target, it just pops the
189 specified number of frames. */
191 /* Call return_command with from_tty argument equal to 0 so as to
192 avoid being queried. */
193 return_command (*argv
, 0);
195 /* Call return_command with from_tty argument equal to 0 so as to
196 avoid being queried. */
197 return_command (NULL
, 0);
199 /* Because we have called return_command with from_tty = 0, we need
200 to print the frame here. */
201 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
);
207 mi_cmd_exec_continue (char *command
, char **argv
, int argc
)
209 /* FIXME: Should call a libgdb function, not a cli wrapper. */
210 return mi_execute_async_cli_command ("continue", argv
, argc
);
213 /* Interrupt the execution of the target. Note how we must play around
214 with the token variables, in order to display the current token in
215 the result of the interrupt command, and the previous execution
216 token when the target finally stops. See comments in
219 mi_cmd_exec_interrupt (char *command
, char **argv
, int argc
)
221 if (!target_executing
)
222 error ("mi_cmd_exec_interrupt: Inferior not executing.");
224 interrupt_target_command (NULL
, 0);
226 fputs_unfiltered (current_token
, raw_stdout
);
227 fputs_unfiltered ("^done", raw_stdout
);
228 mi_out_put (uiout
, raw_stdout
);
229 mi_out_rewind (uiout
);
230 fputs_unfiltered ("\n", raw_stdout
);
235 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
238 char *mi_error_message
;
241 error ("mi_cmd_thread_select: USAGE: threadnum.");
243 rc
= gdb_thread_select (uiout
, argv
[0], &mi_error_message
);
245 if (rc
== GDB_RC_FAIL
)
247 make_cleanup (xfree
, mi_error_message
);
248 error ("%s", mi_error_message
);
255 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
258 char *mi_error_message
;
261 error ("mi_cmd_thread_list_ids: No arguments required.");
263 rc
= gdb_list_thread_ids (uiout
, &mi_error_message
);
265 if (rc
== GDB_RC_FAIL
)
267 make_cleanup (xfree
, mi_error_message
);
268 error ("%s", mi_error_message
);
275 mi_cmd_thread_info (char *command
, char **argv
, int argc
)
279 if (argc
!= 0 && argc
!= 1)
280 error ("Invalid MI command");
283 thread
= atoi (argv
[0]);
285 print_thread_info (uiout
, thread
);
290 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
294 struct cleanup
*cleanup
;
296 /* Note that the test for a valid register must include checking the
297 gdbarch_register_name because gdbarch_num_regs may be allocated for
298 the union of the register sets within a family of related processors.
299 In this case, some entries of gdbarch_register_name will change depending
300 upon the particular processor being debugged. */
302 numregs
= gdbarch_num_regs (current_gdbarch
)
303 + gdbarch_num_pseudo_regs (current_gdbarch
);
305 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
307 if (argc
== 0) /* No args, just do all the regs. */
313 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
314 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
315 ui_out_field_string (uiout
, NULL
, "");
317 ui_out_field_string (uiout
, NULL
,
318 gdbarch_register_name
319 (current_gdbarch
, regnum
));
323 /* Else, list of register #s, just do listed regs. */
324 for (i
= 0; i
< argc
; i
++)
326 regnum
= atoi (argv
[i
]);
327 if (regnum
< 0 || regnum
>= numregs
)
328 error ("bad register number");
330 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
331 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
332 ui_out_field_string (uiout
, NULL
, "");
334 ui_out_field_string (uiout
, NULL
,
335 gdbarch_register_name (current_gdbarch
, regnum
));
337 do_cleanups (cleanup
);
342 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
344 static struct regcache
*this_regs
= NULL
;
345 struct regcache
*prev_regs
;
346 int regnum
, numregs
, changed
;
348 struct cleanup
*cleanup
;
350 /* The last time we visited this function, the current frame's register
351 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
352 and refresh THIS_REGS with the now-current register contents. */
354 prev_regs
= this_regs
;
355 this_regs
= frame_save_as_regcache (get_selected_frame (NULL
));
356 cleanup
= make_cleanup_regcache_xfree (prev_regs
);
358 /* Note that the test for a valid register must include checking the
359 gdbarch_register_name because gdbarch_num_regs may be allocated for
360 the union of the register sets within a family of related processors.
361 In this case, some entries of gdbarch_register_name will change depending
362 upon the particular processor being debugged. */
364 numregs
= gdbarch_num_regs (current_gdbarch
)
365 + gdbarch_num_pseudo_regs (current_gdbarch
);
367 make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
369 if (argc
== 0) /* No args, just do all the regs. */
375 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
376 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
378 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
380 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
382 ui_out_field_int (uiout
, NULL
, regnum
);
386 /* Else, list of register #s, just do listed regs. */
387 for (i
= 0; i
< argc
; i
++)
389 regnum
= atoi (argv
[i
]);
393 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
394 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
396 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
398 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
400 ui_out_field_int (uiout
, NULL
, regnum
);
403 error ("bad register number");
405 do_cleanups (cleanup
);
410 register_changed_p (int regnum
, struct regcache
*prev_regs
,
411 struct regcache
*this_regs
)
413 struct gdbarch
*gdbarch
= get_regcache_arch (this_regs
);
414 gdb_byte prev_buffer
[MAX_REGISTER_SIZE
];
415 gdb_byte this_buffer
[MAX_REGISTER_SIZE
];
417 /* Registers not valid in this frame return count as unchanged. */
418 if (!regcache_valid_p (this_regs
, regnum
))
421 /* First time through or after gdbarch change consider all registers as
422 changed. Same for registers not valid in the previous frame. */
423 if (!prev_regs
|| get_regcache_arch (prev_regs
) != gdbarch
424 || !regcache_valid_p (prev_regs
, regnum
))
427 /* Get register contents and compare. */
428 regcache_cooked_read (prev_regs
, regnum
, prev_buffer
);
429 regcache_cooked_read (this_regs
, regnum
, this_buffer
);
431 return memcmp (prev_buffer
, this_buffer
,
432 register_size (gdbarch
, regnum
)) != 0;
435 /* Return a list of register number and value pairs. The valid
436 arguments expected are: a letter indicating the format in which to
437 display the registers contents. This can be one of: x (hexadecimal), d
438 (decimal), N (natural), t (binary), o (octal), r (raw). After the
439 format argumetn there can be a sequence of numbers, indicating which
440 registers to fetch the content of. If the format is the only argument,
441 a list of all the registers with their values is returned. */
443 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
445 int regnum
, numregs
, format
;
447 struct cleanup
*list_cleanup
, *tuple_cleanup
;
449 /* Note that the test for a valid register must include checking the
450 gdbarch_register_name because gdbarch_num_regs may be allocated for
451 the union of the register sets within a family of related processors.
452 In this case, some entries of gdbarch_register_name will change depending
453 upon the particular processor being debugged. */
455 numregs
= gdbarch_num_regs (current_gdbarch
)
456 + gdbarch_num_pseudo_regs (current_gdbarch
);
459 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
461 format
= (int) argv
[0][0];
463 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
465 if (argc
== 1) /* No args, beside the format: do all the regs. */
471 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
472 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
474 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
475 ui_out_field_int (uiout
, "number", regnum
);
476 get_register (regnum
, format
);
477 do_cleanups (tuple_cleanup
);
481 /* Else, list of register #s, just do listed regs. */
482 for (i
= 1; i
< argc
; i
++)
484 regnum
= atoi (argv
[i
]);
488 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
489 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
491 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
492 ui_out_field_int (uiout
, "number", regnum
);
493 get_register (regnum
, format
);
494 do_cleanups (tuple_cleanup
);
497 error ("bad register number");
499 do_cleanups (list_cleanup
);
503 /* Output one register's contents in the desired format. */
505 get_register (int regnum
, int format
)
507 gdb_byte buffer
[MAX_REGISTER_SIZE
];
512 static struct ui_stream
*stb
= NULL
;
514 stb
= ui_out_stream_new (uiout
);
519 frame_register (get_selected_frame (NULL
), regnum
, &optim
, &lval
, &addr
,
523 error ("Optimized out");
528 char *ptr
, buf
[1024];
532 for (j
= 0; j
< register_size (current_gdbarch
, regnum
); j
++)
534 int idx
= gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
? j
535 : register_size (current_gdbarch
, regnum
) - 1 - j
;
536 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
539 ui_out_field_string (uiout
, "value", buf
);
540 /*fputs_filtered (buf, gdb_stdout); */
544 val_print (register_type (current_gdbarch
, regnum
), buffer
, 0, 0,
545 stb
->stream
, format
, 1, 0, Val_pretty_default
,
547 ui_out_field_stream (uiout
, "value", stb
);
548 ui_out_stream_delete (stb
);
552 /* Write given values into registers. The registers and values are
553 given as pairs. The corresponding MI command is
554 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
556 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
561 /* Note that the test for a valid register must include checking the
562 gdbarch_register_name because gdbarch_num_regs may be allocated for
563 the union of the register sets within a family of related processors.
564 In this case, some entries of gdbarch_register_name will change depending
565 upon the particular processor being debugged. */
567 numregs
= gdbarch_num_regs (current_gdbarch
)
568 + gdbarch_num_pseudo_regs (current_gdbarch
);
571 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
573 format
= (int) argv
[0][0];
575 if (!target_has_registers
)
576 error ("mi_cmd_data_write_register_values: No registers.");
579 error ("mi_cmd_data_write_register_values: No regs and values specified.");
582 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
584 for (i
= 1; i
< argc
; i
= i
+ 2)
586 int regnum
= atoi (argv
[i
]);
588 if (regnum
>= 0 && regnum
< numregs
589 && gdbarch_register_name (current_gdbarch
, regnum
)
590 && *gdbarch_register_name (current_gdbarch
, regnum
))
594 /* Get the value as a number. */
595 value
= parse_and_eval_address (argv
[i
+ 1]);
598 regcache_cooked_write_signed (get_current_regcache (), regnum
, value
);
601 error ("bad register number");
606 /* Evaluate the value of the argument. The argument is an
607 expression. If the expression contains spaces it needs to be
608 included in double quotes. */
610 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
612 struct expression
*expr
;
613 struct cleanup
*old_chain
= NULL
;
615 struct ui_stream
*stb
= NULL
;
617 stb
= ui_out_stream_new (uiout
);
621 ui_out_stream_delete (stb
);
622 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
625 expr
= parse_expression (argv
[0]);
627 old_chain
= make_cleanup (free_current_contents
, &expr
);
629 val
= evaluate_expression (expr
);
631 /* Print the result of the expression evaluation. */
632 val_print (value_type (val
), value_contents (val
),
633 value_embedded_offset (val
), VALUE_ADDRESS (val
),
634 stb
->stream
, 0, 0, 0, 0, current_language
);
636 ui_out_field_stream (uiout
, "value", stb
);
637 ui_out_stream_delete (stb
);
639 do_cleanups (old_chain
);
645 mi_cmd_target_download (char *command
, char **argv
, int argc
)
648 struct cleanup
*old_cleanups
= NULL
;
650 /* There may be at most one parameter -- the name of the
652 run
= xstrprintf ("load %s", argc
? *argv
: "");
653 old_cleanups
= make_cleanup (xfree
, run
);
654 execute_command (run
, 0);
656 do_cleanups (old_cleanups
);
660 /* Connect to the remote target. */
662 mi_cmd_target_select (char *command
, char **argv
, int argc
)
665 struct cleanup
*old_cleanups
= NULL
;
669 error ("no target type specified");
671 for (i
= 0; i
< argc
; ++i
)
674 run
= concat ("target ", argv
[0], NULL
);
678 run
= concat (run
, " ", argv
[i
], NULL
);
683 old_cleanups
= make_cleanup (xfree
, run
);
685 /* target-select is always synchronous. Once the call has returned
686 we know that we are connected. */
687 /* NOTE: At present all targets that are connected are also
688 (implicitly) talking to a halted target. In the future this may
690 execute_command (run
, 0);
692 do_cleanups (old_cleanups
);
694 /* Issue the completion message here. */
696 fputs_unfiltered (current_token
, raw_stdout
);
697 fputs_unfiltered ("^connected", raw_stdout
);
698 mi_out_put (uiout
, raw_stdout
);
699 mi_out_rewind (uiout
);
700 fputs_unfiltered ("\n", raw_stdout
);
706 ADDR: start address of data to be dumped.
707 WORD-FORMAT: a char indicating format for the ``word''. See
709 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
710 NR_ROW: Number of rows.
711 NR_COL: The number of colums (words per row).
712 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
713 ASCHAR for unprintable characters.
715 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
716 displayes them. Returns:
718 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
721 The number of bytes read is SIZE*ROW*COL. */
724 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
726 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
732 struct type
*word_type
;
745 static struct mi_opt opts
[] =
747 {"o", OFFSET_OPT
, 1},
753 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
757 switch ((enum opt
) opt
)
760 offset
= atol (optarg
);
767 if (argc
< 5 || argc
> 6)
768 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
770 /* Extract all the arguments. */
772 /* Start address of the memory dump. */
773 addr
= parse_and_eval_address (argv
[0]) + offset
;
774 /* The format character to use when displaying a memory word. See
775 the ``x'' command. */
776 word_format
= argv
[1][0];
777 /* The size of the memory word. */
778 word_size
= atol (argv
[2]);
782 word_type
= builtin_type_int8
;
786 word_type
= builtin_type_int16
;
790 word_type
= builtin_type_int32
;
794 word_type
= builtin_type_int64
;
798 word_type
= builtin_type_int8
;
801 /* The number of rows. */
802 nr_rows
= atol (argv
[3]);
804 error ("mi_cmd_data_read_memory: invalid number of rows.");
806 /* Number of bytes per row. */
807 nr_cols
= atol (argv
[4]);
809 error ("mi_cmd_data_read_memory: invalid number of columns.");
811 /* The un-printable character when printing ascii. */
817 /* Create a buffer and read it in. */
818 total_bytes
= word_size
* nr_rows
* nr_cols
;
819 mbuf
= xcalloc (total_bytes
, 1);
820 make_cleanup (xfree
, mbuf
);
822 nr_bytes
= target_read (¤t_target
, TARGET_OBJECT_MEMORY
, NULL
,
823 mbuf
, addr
, total_bytes
);
825 error ("Unable to read memory.");
827 /* Output the header information. */
828 ui_out_field_core_addr (uiout
, "addr", addr
);
829 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
830 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
831 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
832 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
833 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
834 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
836 /* Build the result as a two dimentional table. */
838 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
839 struct cleanup
*cleanup_list_memory
;
842 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
843 for (row
= 0, row_byte
= 0;
845 row
++, row_byte
+= nr_cols
* word_size
)
849 struct cleanup
*cleanup_tuple
;
850 struct cleanup
*cleanup_list_data
;
851 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
852 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
853 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
854 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
855 for (col
= 0, col_byte
= row_byte
;
857 col
++, col_byte
+= word_size
)
859 if (col_byte
+ word_size
> nr_bytes
)
861 ui_out_field_string (uiout
, NULL
, "N/A");
865 ui_file_rewind (stream
->stream
);
866 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
867 word_asize
, stream
->stream
);
868 ui_out_field_stream (uiout
, NULL
, stream
);
871 do_cleanups (cleanup_list_data
);
875 ui_file_rewind (stream
->stream
);
876 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
878 if (byte
>= nr_bytes
)
880 fputc_unfiltered ('X', stream
->stream
);
882 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
884 fputc_unfiltered (aschar
, stream
->stream
);
887 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
889 ui_out_field_stream (uiout
, "ascii", stream
);
891 do_cleanups (cleanup_tuple
);
893 ui_out_stream_delete (stream
);
894 do_cleanups (cleanup_list_memory
);
896 do_cleanups (cleanups
);
900 /* DATA-MEMORY-WRITE:
902 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
903 offset from the beginning of the memory grid row where the cell to
905 ADDR: start address of the row in the memory grid where the memory
906 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
907 the location to write to.
908 FORMAT: a char indicating format for the ``word''. See
910 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
911 VALUE: value to be written into the memory address.
913 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
917 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
922 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
923 enough when using a compiler other than GCC. */
926 struct cleanup
*old_chain
;
934 static struct mi_opt opts
[] =
936 {"o", OFFSET_OPT
, 1},
942 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
946 switch ((enum opt
) opt
)
949 offset
= atol (optarg
);
957 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
959 /* Extract all the arguments. */
960 /* Start address of the memory dump. */
961 addr
= parse_and_eval_address (argv
[0]);
962 /* The format character to use when displaying a memory word. See
963 the ``x'' command. */
964 word_format
= argv
[1][0];
965 /* The size of the memory word. */
966 word_size
= atol (argv
[2]);
968 /* Calculate the real address of the write destination. */
969 addr
+= (offset
* word_size
);
971 /* Get the value as a number. */
972 value
= parse_and_eval_address (argv
[3]);
973 /* Get the value into an array. */
974 buffer
= xmalloc (word_size
);
975 old_chain
= make_cleanup (xfree
, buffer
);
976 store_signed_integer (buffer
, word_size
, value
);
977 /* Write it down to memory. */
978 write_memory (addr
, buffer
, word_size
);
979 /* Free the buffer. */
980 do_cleanups (old_chain
);
986 mi_cmd_enable_timings (char *command
, char **argv
, int argc
)
992 if (strcmp (argv
[0], "yes") == 0)
994 else if (strcmp (argv
[0], "no") == 0)
1005 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command
);
1010 mi_cmd_list_features (char *command
, char **argv
, int argc
)
1014 struct cleanup
*cleanup
= NULL
;
1015 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
1017 ui_out_field_string (uiout
, NULL
, "frozen-varobjs");
1018 ui_out_field_string (uiout
, NULL
, "pending-breakpoints");
1019 ui_out_field_string (uiout
, NULL
, "thread-info");
1021 do_cleanups (cleanup
);
1026 error ("-list-features should be passed no arguments");
1030 /* Execute a command within a safe environment.
1031 Return <0 for error; >=0 for ok.
1033 args->action will tell mi_execute_command what action
1034 to perfrom after the given command has executed (display/suppress
1035 prompt, display error). */
1038 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1040 struct captured_mi_execute_command_args
*args
=
1041 (struct captured_mi_execute_command_args
*) data
;
1042 struct mi_parse
*context
= args
->command
;
1044 struct mi_timestamp cmd_finished
;
1046 switch (context
->op
)
1050 /* A MI command was read from the input stream. */
1052 /* FIXME: gdb_???? */
1053 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1054 context
->token
, context
->command
, context
->args
);
1055 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1056 condition expression, each function should return an
1057 indication of what action is required and then switch on
1059 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1062 current_command_ts
= context
->cmd_start
;
1064 args
->rc
= mi_cmd_execute (context
);
1067 timestamp (&cmd_finished
);
1069 if (!target_can_async_p () || !target_executing
)
1071 /* Print the result if there were no errors.
1073 Remember that on the way out of executing a command, you have
1074 to directly use the mi_interp's uiout, since the command could
1075 have reset the interpreter, in which case the current uiout
1076 will most likely crash in the mi_out_* routines. */
1077 if (args
->rc
== MI_CMD_DONE
)
1079 fputs_unfiltered (context
->token
, raw_stdout
);
1080 fputs_unfiltered ("^done", raw_stdout
);
1081 mi_out_put (uiout
, raw_stdout
);
1082 mi_out_rewind (uiout
);
1083 /* Have to check cmd_start, since the command could be
1085 if (do_timings
&& context
->cmd_start
)
1086 print_diff (context
->cmd_start
, &cmd_finished
);
1087 fputs_unfiltered ("\n", raw_stdout
);
1090 mi_out_rewind (uiout
);
1092 else if (sync_execution
)
1094 /* Don't print the prompt. We are executing the target in
1095 synchronous mode. */
1096 args
->action
= EXECUTE_COMMAND_SUPPRESS_PROMPT
;
1104 /* A CLI command was read from the input stream. */
1105 /* This "feature" will be removed as soon as we have a
1106 complete set of mi commands. */
1107 /* Echo the command on the console. */
1108 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1109 /* Call the "console" interpreter. */
1110 argv
[0] = "console";
1111 argv
[1] = context
->command
;
1112 args
->rc
= mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
1114 /* If we changed interpreters, DON'T print out anything. */
1115 if (current_interp_named_p (INTERP_MI
)
1116 || current_interp_named_p (INTERP_MI1
)
1117 || current_interp_named_p (INTERP_MI2
)
1118 || current_interp_named_p (INTERP_MI3
))
1120 if (args
->rc
== MI_CMD_DONE
)
1122 fputs_unfiltered (context
->token
, raw_stdout
);
1123 fputs_unfiltered ("^done", raw_stdout
);
1124 mi_out_put (uiout
, raw_stdout
);
1125 mi_out_rewind (uiout
);
1126 fputs_unfiltered ("\n", raw_stdout
);
1127 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1130 mi_out_rewind (uiout
);
1142 mi_execute_command (char *cmd
, int from_tty
)
1144 struct mi_parse
*command
;
1145 struct captured_mi_execute_command_args args
;
1146 struct ui_out
*saved_uiout
= uiout
;
1148 /* This is to handle EOF (^D). We just quit gdb. */
1149 /* FIXME: we should call some API function here. */
1151 quit_force (NULL
, from_tty
);
1153 command
= mi_parse (cmd
);
1155 if (command
!= NULL
)
1157 struct gdb_exception result
;
1161 command
->cmd_start
= (struct mi_timestamp
*)
1162 xmalloc (sizeof (struct mi_timestamp
));
1163 timestamp (command
->cmd_start
);
1166 args
.command
= command
;
1167 result
= catch_exception (uiout
, captured_mi_execute_command
, &args
,
1169 if (result
.reason
< 0)
1171 /* The command execution failed and error() was called
1173 fputs_unfiltered (command
->token
, raw_stdout
);
1174 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1175 if (result
.message
== NULL
)
1176 fputs_unfiltered ("unknown error", raw_stdout
);
1178 fputstr_unfiltered (result
.message
, '"', raw_stdout
);
1179 fputs_unfiltered ("\"\n", raw_stdout
);
1180 mi_out_rewind (uiout
);
1183 mi_parse_free (command
);
1185 if (args
.action
== EXECUTE_COMMAND_SUPPRESS_PROMPT
)
1186 /* The command is executing synchronously. Bail out early
1187 suppressing the finished prompt. */
1191 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1192 gdb_flush (raw_stdout
);
1193 /* Print any buffered hook code. */
1197 static enum mi_cmd_result
1198 mi_cmd_execute (struct mi_parse
*parse
)
1200 struct cleanup
*cleanup
;
1201 enum mi_cmd_result r
;
1204 if (parse
->cmd
->argv_func
!= NULL
)
1206 if (target_executing
)
1208 if (strcmp (parse
->command
, "exec-interrupt"))
1210 struct ui_file
*stb
;
1211 stb
= mem_fileopen ();
1213 fputs_unfiltered ("Cannot execute command ", stb
);
1214 fputstr_unfiltered (parse
->command
, '"', stb
);
1215 fputs_unfiltered (" while target running", stb
);
1217 make_cleanup_ui_file_delete (stb
);
1221 current_token
= xstrdup (parse
->token
);
1222 cleanup
= make_cleanup (free_current_contents
, ¤t_token
);
1223 r
= parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1224 do_cleanups (cleanup
);
1227 else if (parse
->cmd
->cli
.cmd
!= 0)
1229 /* FIXME: DELETE THIS. */
1230 /* The operation is still implemented by a cli command. */
1231 /* Must be a synchronous one. */
1232 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1238 /* FIXME: DELETE THIS. */
1239 struct ui_file
*stb
;
1241 stb
= mem_fileopen ();
1243 fputs_unfiltered ("Undefined mi command: ", stb
);
1244 fputstr_unfiltered (parse
->command
, '"', stb
);
1245 fputs_unfiltered (" (missing implementation)", stb
);
1247 make_cleanup_ui_file_delete (stb
);
1255 /* FIXME: This is just a hack so we can get some extra commands going.
1256 We don't want to channel things through the CLI, but call libgdb directly.
1257 Use only for synchronous commands. */
1260 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1264 struct cleanup
*old_cleanups
;
1267 run
= xstrprintf ("%s %s", cmd
, args
);
1269 run
= xstrdup (cmd
);
1271 /* FIXME: gdb_???? */
1272 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1274 old_cleanups
= make_cleanup (xfree
, run
);
1275 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1276 do_cleanups (old_cleanups
);
1282 mi_execute_async_cli_command (char *cli_command
, char **argv
, int argc
)
1284 struct cleanup
*old_cleanups
;
1287 if (target_can_async_p ())
1288 run
= xstrprintf ("%s %s&", cli_command
, argc
? *argv
: "");
1290 run
= xstrprintf ("%s %s", cli_command
, argc
? *argv
: "");
1291 old_cleanups
= make_cleanup (xfree
, run
);
1293 if (!target_can_async_p ())
1295 /* NOTE: For synchronous targets asynchronous behavour is faked by
1296 printing out the GDB prompt before we even try to execute the
1299 fputs_unfiltered (current_token
, raw_stdout
);
1300 fputs_unfiltered ("^running\n", raw_stdout
);
1301 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1302 gdb_flush (raw_stdout
);
1306 /* FIXME: cagney/1999-11-29: Printing this message before
1307 calling execute_command is wrong. It should only be printed
1308 once gdb has confirmed that it really has managed to send a
1309 run command to the target. */
1311 fputs_unfiltered (current_token
, raw_stdout
);
1312 fputs_unfiltered ("^running\n", raw_stdout
);
1314 /* Ideally, we should be intalling continuation only when
1315 the target is already running. However, this will break right now,
1316 because continuation installed by the 'finish' command must be after
1317 the continuation that prints *stopped. This issue will be
1319 add_continuation (mi_exec_async_cli_cmd_continuation
, NULL
);
1322 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1324 if (target_can_async_p ())
1326 /* If we're not executing, an exception should have been throw. */
1327 gdb_assert (target_executing
);
1328 do_cleanups (old_cleanups
);
1332 /* Do this before doing any printing. It would appear that some
1333 print code leaves garbage around in the buffer. */
1334 do_cleanups (old_cleanups
);
1335 /* If the target was doing the operation synchronously we fake
1336 the stopped message. */
1337 fputs_unfiltered ("*stopped", raw_stdout
);
1338 mi_out_put (uiout
, raw_stdout
);
1339 mi_out_rewind (uiout
);
1341 print_diff_now (current_command_ts
);
1342 fputs_unfiltered ("\n", raw_stdout
);
1343 return MI_CMD_QUIET
;
1349 mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
, int error_p
)
1351 /* Assume 'error' means that target is stopped, too. */
1352 fputs_unfiltered ("*stopped", raw_stdout
);
1353 mi_out_put (uiout
, raw_stdout
);
1354 fputs_unfiltered ("\n", raw_stdout
);
1355 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1356 gdb_flush (raw_stdout
);
1360 mi_load_progress (const char *section_name
,
1361 unsigned long sent_so_far
,
1362 unsigned long total_section
,
1363 unsigned long total_sent
,
1364 unsigned long grand_total
)
1366 struct timeval time_now
, delta
, update_threshold
;
1367 static struct timeval last_update
;
1368 static char *previous_sect_name
= NULL
;
1370 struct ui_out
*saved_uiout
;
1372 /* This function is called through deprecated_show_load_progress
1373 which means uiout may not be correct. Fix it for the duration
1374 of this function. */
1375 saved_uiout
= uiout
;
1377 if (current_interp_named_p (INTERP_MI
)
1378 || current_interp_named_p (INTERP_MI2
))
1379 uiout
= mi_out_new (2);
1380 else if (current_interp_named_p (INTERP_MI1
))
1381 uiout
= mi_out_new (1);
1382 else if (current_interp_named_p (INTERP_MI3
))
1383 uiout
= mi_out_new (3);
1387 update_threshold
.tv_sec
= 0;
1388 update_threshold
.tv_usec
= 500000;
1389 gettimeofday (&time_now
, NULL
);
1391 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1392 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1394 if (delta
.tv_usec
< 0)
1397 delta
.tv_usec
+= 1000000L;
1400 new_section
= (previous_sect_name
?
1401 strcmp (previous_sect_name
, section_name
) : 1);
1404 struct cleanup
*cleanup_tuple
;
1405 xfree (previous_sect_name
);
1406 previous_sect_name
= xstrdup (section_name
);
1409 fputs_unfiltered (current_token
, raw_stdout
);
1410 fputs_unfiltered ("+download", raw_stdout
);
1411 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1412 ui_out_field_string (uiout
, "section", section_name
);
1413 ui_out_field_int (uiout
, "section-size", total_section
);
1414 ui_out_field_int (uiout
, "total-size", grand_total
);
1415 do_cleanups (cleanup_tuple
);
1416 mi_out_put (uiout
, raw_stdout
);
1417 fputs_unfiltered ("\n", raw_stdout
);
1418 gdb_flush (raw_stdout
);
1421 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1422 delta
.tv_usec
>= update_threshold
.tv_usec
)
1424 struct cleanup
*cleanup_tuple
;
1425 last_update
.tv_sec
= time_now
.tv_sec
;
1426 last_update
.tv_usec
= time_now
.tv_usec
;
1428 fputs_unfiltered (current_token
, raw_stdout
);
1429 fputs_unfiltered ("+download", raw_stdout
);
1430 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1431 ui_out_field_string (uiout
, "section", section_name
);
1432 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1433 ui_out_field_int (uiout
, "section-size", total_section
);
1434 ui_out_field_int (uiout
, "total-sent", total_sent
);
1435 ui_out_field_int (uiout
, "total-size", grand_total
);
1436 do_cleanups (cleanup_tuple
);
1437 mi_out_put (uiout
, raw_stdout
);
1438 fputs_unfiltered ("\n", raw_stdout
);
1439 gdb_flush (raw_stdout
);
1443 uiout
= saved_uiout
;
1447 timestamp (struct mi_timestamp
*tv
)
1450 gettimeofday (&tv
->wallclock
, NULL
);
1451 #ifdef HAVE_GETRUSAGE
1452 getrusage (RUSAGE_SELF
, &rusage
);
1453 tv
->utime
.tv_sec
= rusage
.ru_utime
.tv_sec
;
1454 tv
->utime
.tv_usec
= rusage
.ru_utime
.tv_usec
;
1455 tv
->stime
.tv_sec
= rusage
.ru_stime
.tv_sec
;
1456 tv
->stime
.tv_usec
= rusage
.ru_stime
.tv_usec
;
1458 usec
= get_run_time ();
1459 tv
->utime
.tv_sec
= usec
/1000000L;
1460 tv
->utime
.tv_usec
= usec
- 1000000L*tv
->utime
.tv_sec
;
1461 tv
->stime
.tv_sec
= 0;
1462 tv
->stime
.tv_usec
= 0;
1467 print_diff_now (struct mi_timestamp
*start
)
1469 struct mi_timestamp now
;
1471 print_diff (start
, &now
);
1475 timeval_diff (struct timeval start
, struct timeval end
)
1477 return ((end
.tv_sec
- start
.tv_sec
) * 1000000L)
1478 + (end
.tv_usec
- start
.tv_usec
);
1482 print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
)
1486 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1487 timeval_diff (start
->wallclock
, end
->wallclock
) / 1000000.0,
1488 timeval_diff (start
->utime
, end
->utime
) / 1000000.0,
1489 timeval_diff (start
->stime
, end
->stime
) / 1000000.0);