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
);
106 static int register_changed_p (int regnum
, struct regcache
*,
108 static void get_register (int regnum
, int format
);
110 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
111 layer that calls libgdb. Any operation used in the below should be
114 static void timestamp (struct mi_timestamp
*tv
);
116 static void print_diff_now (struct mi_timestamp
*start
);
117 static void print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
);
120 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
122 /* We have to print everything right here because we never return. */
124 fputs_unfiltered (current_token
, raw_stdout
);
125 fputs_unfiltered ("^exit\n", raw_stdout
);
126 mi_out_put (uiout
, raw_stdout
);
127 /* FIXME: The function called is not yet a formal libgdb function. */
128 quit_force (NULL
, FROM_TTY
);
133 mi_cmd_exec_run (char *command
, char **argv
, int argc
)
135 /* FIXME: Should call a libgdb function, not a cli wrapper. */
136 return mi_execute_async_cli_command ("run", argv
, argc
);
140 mi_cmd_exec_next (char *command
, char **argv
, int argc
)
142 /* FIXME: Should call a libgdb function, not a cli wrapper. */
143 return mi_execute_async_cli_command ("next", argv
, argc
);
147 mi_cmd_exec_next_instruction (char *command
, char **argv
, int argc
)
149 /* FIXME: Should call a libgdb function, not a cli wrapper. */
150 return mi_execute_async_cli_command ("nexti", argv
, argc
);
154 mi_cmd_exec_step (char *command
, char **argv
, int argc
)
156 /* FIXME: Should call a libgdb function, not a cli wrapper. */
157 return mi_execute_async_cli_command ("step", argv
, argc
);
161 mi_cmd_exec_step_instruction (char *command
, char **argv
, int argc
)
163 /* FIXME: Should call a libgdb function, not a cli wrapper. */
164 return mi_execute_async_cli_command ("stepi", argv
, argc
);
168 mi_cmd_exec_finish (char *command
, char **argv
, int argc
)
170 /* FIXME: Should call a libgdb function, not a cli wrapper. */
171 return mi_execute_async_cli_command ("finish", argv
, argc
);
175 mi_cmd_exec_until (char *command
, char **argv
, int argc
)
177 /* FIXME: Should call a libgdb function, not a cli wrapper. */
178 return mi_execute_async_cli_command ("until", argv
, argc
);
182 mi_cmd_exec_return (char *command
, char **argv
, int argc
)
184 /* This command doesn't really execute the target, it just pops the
185 specified number of frames. */
187 /* Call return_command with from_tty argument equal to 0 so as to
188 avoid being queried. */
189 return_command (*argv
, 0);
191 /* Call return_command with from_tty argument equal to 0 so as to
192 avoid being queried. */
193 return_command (NULL
, 0);
195 /* Because we have called return_command with from_tty = 0, we need
196 to print the frame here. */
197 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
);
203 mi_cmd_exec_continue (char *command
, char **argv
, int argc
)
205 /* FIXME: Should call a libgdb function, not a cli wrapper. */
206 return mi_execute_async_cli_command ("continue", argv
, argc
);
209 /* Interrupt the execution of the target. Note how we must play around
210 with the token variables, in order to display the current token in
211 the result of the interrupt command, and the previous execution
212 token when the target finally stops. See comments in
215 mi_cmd_exec_interrupt (char *command
, char **argv
, int argc
)
217 if (!target_executing
)
218 error ("mi_cmd_exec_interrupt: Inferior not executing.");
220 interrupt_target_command (NULL
, 0);
222 fputs_unfiltered (current_token
, raw_stdout
);
223 fputs_unfiltered ("^done", raw_stdout
);
224 mi_out_put (uiout
, raw_stdout
);
225 mi_out_rewind (uiout
);
226 fputs_unfiltered ("\n", raw_stdout
);
231 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
234 char *mi_error_message
;
237 error ("mi_cmd_thread_select: USAGE: threadnum.");
239 rc
= gdb_thread_select (uiout
, argv
[0], &mi_error_message
);
241 if (rc
== GDB_RC_FAIL
)
243 make_cleanup (xfree
, mi_error_message
);
244 error ("%s", mi_error_message
);
251 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
254 char *mi_error_message
;
257 error ("mi_cmd_thread_list_ids: No arguments required.");
259 rc
= gdb_list_thread_ids (uiout
, &mi_error_message
);
261 if (rc
== GDB_RC_FAIL
)
263 make_cleanup (xfree
, mi_error_message
);
264 error ("%s", mi_error_message
);
271 mi_cmd_thread_info (char *command
, char **argv
, int argc
)
275 if (argc
!= 0 && argc
!= 1)
276 error ("Invalid MI command");
279 thread
= atoi (argv
[0]);
281 print_thread_info (uiout
, thread
);
286 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
290 struct cleanup
*cleanup
;
292 /* Note that the test for a valid register must include checking the
293 gdbarch_register_name because gdbarch_num_regs may be allocated for
294 the union of the register sets within a family of related processors.
295 In this case, some entries of gdbarch_register_name will change depending
296 upon the particular processor being debugged. */
298 numregs
= gdbarch_num_regs (current_gdbarch
)
299 + gdbarch_num_pseudo_regs (current_gdbarch
);
301 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
303 if (argc
== 0) /* No args, just do all the regs. */
309 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
310 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
311 ui_out_field_string (uiout
, NULL
, "");
313 ui_out_field_string (uiout
, NULL
,
314 gdbarch_register_name
315 (current_gdbarch
, regnum
));
319 /* Else, list of register #s, just do listed regs. */
320 for (i
= 0; i
< argc
; i
++)
322 regnum
= atoi (argv
[i
]);
323 if (regnum
< 0 || regnum
>= numregs
)
324 error ("bad register number");
326 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
327 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
328 ui_out_field_string (uiout
, NULL
, "");
330 ui_out_field_string (uiout
, NULL
,
331 gdbarch_register_name (current_gdbarch
, regnum
));
333 do_cleanups (cleanup
);
338 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
340 static struct regcache
*this_regs
= NULL
;
341 struct regcache
*prev_regs
;
342 int regnum
, numregs
, changed
;
344 struct cleanup
*cleanup
;
346 /* The last time we visited this function, the current frame's register
347 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
348 and refresh THIS_REGS with the now-current register contents. */
350 prev_regs
= this_regs
;
351 this_regs
= frame_save_as_regcache (get_selected_frame (NULL
));
352 cleanup
= make_cleanup_regcache_xfree (prev_regs
);
354 /* Note that the test for a valid register must include checking the
355 gdbarch_register_name because gdbarch_num_regs may be allocated for
356 the union of the register sets within a family of related processors.
357 In this case, some entries of gdbarch_register_name will change depending
358 upon the particular processor being debugged. */
360 numregs
= gdbarch_num_regs (current_gdbarch
)
361 + gdbarch_num_pseudo_regs (current_gdbarch
);
363 make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
365 if (argc
== 0) /* No args, just do all the regs. */
371 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
372 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
374 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
376 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
378 ui_out_field_int (uiout
, NULL
, regnum
);
382 /* Else, list of register #s, just do listed regs. */
383 for (i
= 0; i
< argc
; i
++)
385 regnum
= atoi (argv
[i
]);
389 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
390 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
392 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
394 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
396 ui_out_field_int (uiout
, NULL
, regnum
);
399 error ("bad register number");
401 do_cleanups (cleanup
);
406 register_changed_p (int regnum
, struct regcache
*prev_regs
,
407 struct regcache
*this_regs
)
409 struct gdbarch
*gdbarch
= get_regcache_arch (this_regs
);
410 gdb_byte prev_buffer
[MAX_REGISTER_SIZE
];
411 gdb_byte this_buffer
[MAX_REGISTER_SIZE
];
413 /* Registers not valid in this frame return count as unchanged. */
414 if (!regcache_valid_p (this_regs
, regnum
))
417 /* First time through or after gdbarch change consider all registers as
418 changed. Same for registers not valid in the previous frame. */
419 if (!prev_regs
|| get_regcache_arch (prev_regs
) != gdbarch
420 || !regcache_valid_p (prev_regs
, regnum
))
423 /* Get register contents and compare. */
424 regcache_cooked_read (prev_regs
, regnum
, prev_buffer
);
425 regcache_cooked_read (this_regs
, regnum
, this_buffer
);
427 return memcmp (prev_buffer
, this_buffer
,
428 register_size (gdbarch
, regnum
)) != 0;
431 /* Return a list of register number and value pairs. The valid
432 arguments expected are: a letter indicating the format in which to
433 display the registers contents. This can be one of: x (hexadecimal), d
434 (decimal), N (natural), t (binary), o (octal), r (raw). After the
435 format argumetn there can be a sequence of numbers, indicating which
436 registers to fetch the content of. If the format is the only argument,
437 a list of all the registers with their values is returned. */
439 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
441 int regnum
, numregs
, format
;
443 struct cleanup
*list_cleanup
, *tuple_cleanup
;
445 /* Note that the test for a valid register must include checking the
446 gdbarch_register_name because gdbarch_num_regs may be allocated for
447 the union of the register sets within a family of related processors.
448 In this case, some entries of gdbarch_register_name will change depending
449 upon the particular processor being debugged. */
451 numregs
= gdbarch_num_regs (current_gdbarch
)
452 + gdbarch_num_pseudo_regs (current_gdbarch
);
455 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
457 format
= (int) argv
[0][0];
459 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
461 if (argc
== 1) /* No args, beside the format: do all the regs. */
467 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
468 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
470 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
471 ui_out_field_int (uiout
, "number", regnum
);
472 get_register (regnum
, format
);
473 do_cleanups (tuple_cleanup
);
477 /* Else, list of register #s, just do listed regs. */
478 for (i
= 1; i
< argc
; i
++)
480 regnum
= atoi (argv
[i
]);
484 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
485 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
487 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
488 ui_out_field_int (uiout
, "number", regnum
);
489 get_register (regnum
, format
);
490 do_cleanups (tuple_cleanup
);
493 error ("bad register number");
495 do_cleanups (list_cleanup
);
499 /* Output one register's contents in the desired format. */
501 get_register (int regnum
, int format
)
503 gdb_byte buffer
[MAX_REGISTER_SIZE
];
508 static struct ui_stream
*stb
= NULL
;
510 stb
= ui_out_stream_new (uiout
);
515 frame_register (get_selected_frame (NULL
), regnum
, &optim
, &lval
, &addr
,
519 error ("Optimized out");
524 char *ptr
, buf
[1024];
528 for (j
= 0; j
< register_size (current_gdbarch
, regnum
); j
++)
530 int idx
= gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
? j
531 : register_size (current_gdbarch
, regnum
) - 1 - j
;
532 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
535 ui_out_field_string (uiout
, "value", buf
);
536 /*fputs_filtered (buf, gdb_stdout); */
540 val_print (register_type (current_gdbarch
, regnum
), buffer
, 0, 0,
541 stb
->stream
, format
, 1, 0, Val_pretty_default
,
543 ui_out_field_stream (uiout
, "value", stb
);
544 ui_out_stream_delete (stb
);
548 /* Write given values into registers. The registers and values are
549 given as pairs. The corresponding MI command is
550 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
552 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
557 /* Note that the test for a valid register must include checking the
558 gdbarch_register_name because gdbarch_num_regs may be allocated for
559 the union of the register sets within a family of related processors.
560 In this case, some entries of gdbarch_register_name will change depending
561 upon the particular processor being debugged. */
563 numregs
= gdbarch_num_regs (current_gdbarch
)
564 + gdbarch_num_pseudo_regs (current_gdbarch
);
567 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
569 format
= (int) argv
[0][0];
571 if (!target_has_registers
)
572 error ("mi_cmd_data_write_register_values: No registers.");
575 error ("mi_cmd_data_write_register_values: No regs and values specified.");
578 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
580 for (i
= 1; i
< argc
; i
= i
+ 2)
582 int regnum
= atoi (argv
[i
]);
584 if (regnum
>= 0 && regnum
< numregs
585 && gdbarch_register_name (current_gdbarch
, regnum
)
586 && *gdbarch_register_name (current_gdbarch
, regnum
))
590 /* Get the value as a number. */
591 value
= parse_and_eval_address (argv
[i
+ 1]);
594 regcache_cooked_write_signed (get_current_regcache (), regnum
, value
);
597 error ("bad register number");
602 /* Evaluate the value of the argument. The argument is an
603 expression. If the expression contains spaces it needs to be
604 included in double quotes. */
606 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
608 struct expression
*expr
;
609 struct cleanup
*old_chain
= NULL
;
611 struct ui_stream
*stb
= NULL
;
613 stb
= ui_out_stream_new (uiout
);
617 ui_out_stream_delete (stb
);
618 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
621 expr
= parse_expression (argv
[0]);
623 old_chain
= make_cleanup (free_current_contents
, &expr
);
625 val
= evaluate_expression (expr
);
627 /* Print the result of the expression evaluation. */
628 val_print (value_type (val
), value_contents (val
),
629 value_embedded_offset (val
), VALUE_ADDRESS (val
),
630 stb
->stream
, 0, 0, 0, 0, current_language
);
632 ui_out_field_stream (uiout
, "value", stb
);
633 ui_out_stream_delete (stb
);
635 do_cleanups (old_chain
);
641 mi_cmd_target_download (char *command
, char **argv
, int argc
)
644 struct cleanup
*old_cleanups
= NULL
;
646 /* There may be at most one parameter -- the name of the
648 run
= xstrprintf ("load %s", argc
? *argv
: "");
649 old_cleanups
= make_cleanup (xfree
, run
);
650 execute_command (run
, 0);
652 do_cleanups (old_cleanups
);
656 /* Connect to the remote target. */
658 mi_cmd_target_select (char *command
, char **argv
, int argc
)
661 struct cleanup
*old_cleanups
= NULL
;
665 error ("no target type specified");
667 for (i
= 0; i
< argc
; ++i
)
670 run
= concat ("target ", argv
[0], NULL
);
674 run
= concat (run
, " ", argv
[i
], NULL
);
679 old_cleanups
= make_cleanup (xfree
, run
);
681 /* target-select is always synchronous. Once the call has returned
682 we know that we are connected. */
683 /* NOTE: At present all targets that are connected are also
684 (implicitly) talking to a halted target. In the future this may
686 execute_command (run
, 0);
688 do_cleanups (old_cleanups
);
690 /* Issue the completion message here. */
692 fputs_unfiltered (current_token
, raw_stdout
);
693 fputs_unfiltered ("^connected", raw_stdout
);
694 mi_out_put (uiout
, raw_stdout
);
695 mi_out_rewind (uiout
);
696 fputs_unfiltered ("\n", raw_stdout
);
702 ADDR: start address of data to be dumped.
703 WORD-FORMAT: a char indicating format for the ``word''. See
705 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
706 NR_ROW: Number of rows.
707 NR_COL: The number of colums (words per row).
708 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
709 ASCHAR for unprintable characters.
711 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
712 displayes them. Returns:
714 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
717 The number of bytes read is SIZE*ROW*COL. */
720 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
722 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
728 struct type
*word_type
;
741 static struct mi_opt opts
[] =
743 {"o", OFFSET_OPT
, 1},
749 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
753 switch ((enum opt
) opt
)
756 offset
= atol (optarg
);
763 if (argc
< 5 || argc
> 6)
764 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
766 /* Extract all the arguments. */
768 /* Start address of the memory dump. */
769 addr
= parse_and_eval_address (argv
[0]) + offset
;
770 /* The format character to use when displaying a memory word. See
771 the ``x'' command. */
772 word_format
= argv
[1][0];
773 /* The size of the memory word. */
774 word_size
= atol (argv
[2]);
778 word_type
= builtin_type_int8
;
782 word_type
= builtin_type_int16
;
786 word_type
= builtin_type_int32
;
790 word_type
= builtin_type_int64
;
794 word_type
= builtin_type_int8
;
797 /* The number of rows. */
798 nr_rows
= atol (argv
[3]);
800 error ("mi_cmd_data_read_memory: invalid number of rows.");
802 /* Number of bytes per row. */
803 nr_cols
= atol (argv
[4]);
805 error ("mi_cmd_data_read_memory: invalid number of columns.");
807 /* The un-printable character when printing ascii. */
813 /* Create a buffer and read it in. */
814 total_bytes
= word_size
* nr_rows
* nr_cols
;
815 mbuf
= xcalloc (total_bytes
, 1);
816 make_cleanup (xfree
, mbuf
);
818 nr_bytes
= target_read (¤t_target
, TARGET_OBJECT_MEMORY
, NULL
,
819 mbuf
, addr
, total_bytes
);
821 error ("Unable to read memory.");
823 /* Output the header information. */
824 ui_out_field_core_addr (uiout
, "addr", addr
);
825 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
826 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
827 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
828 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
829 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
830 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
832 /* Build the result as a two dimentional table. */
834 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
835 struct cleanup
*cleanup_list_memory
;
838 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
839 for (row
= 0, row_byte
= 0;
841 row
++, row_byte
+= nr_cols
* word_size
)
845 struct cleanup
*cleanup_tuple
;
846 struct cleanup
*cleanup_list_data
;
847 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
848 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
849 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
850 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
851 for (col
= 0, col_byte
= row_byte
;
853 col
++, col_byte
+= word_size
)
855 if (col_byte
+ word_size
> nr_bytes
)
857 ui_out_field_string (uiout
, NULL
, "N/A");
861 ui_file_rewind (stream
->stream
);
862 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
863 word_asize
, stream
->stream
);
864 ui_out_field_stream (uiout
, NULL
, stream
);
867 do_cleanups (cleanup_list_data
);
871 ui_file_rewind (stream
->stream
);
872 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
874 if (byte
>= nr_bytes
)
876 fputc_unfiltered ('X', stream
->stream
);
878 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
880 fputc_unfiltered (aschar
, stream
->stream
);
883 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
885 ui_out_field_stream (uiout
, "ascii", stream
);
887 do_cleanups (cleanup_tuple
);
889 ui_out_stream_delete (stream
);
890 do_cleanups (cleanup_list_memory
);
892 do_cleanups (cleanups
);
896 /* DATA-MEMORY-WRITE:
898 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
899 offset from the beginning of the memory grid row where the cell to
901 ADDR: start address of the row in the memory grid where the memory
902 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
903 the location to write to.
904 FORMAT: a char indicating format for the ``word''. See
906 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
907 VALUE: value to be written into the memory address.
909 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
913 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
918 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
919 enough when using a compiler other than GCC. */
922 struct cleanup
*old_chain
;
930 static struct mi_opt opts
[] =
932 {"o", OFFSET_OPT
, 1},
938 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
942 switch ((enum opt
) opt
)
945 offset
= atol (optarg
);
953 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
955 /* Extract all the arguments. */
956 /* Start address of the memory dump. */
957 addr
= parse_and_eval_address (argv
[0]);
958 /* The format character to use when displaying a memory word. See
959 the ``x'' command. */
960 word_format
= argv
[1][0];
961 /* The size of the memory word. */
962 word_size
= atol (argv
[2]);
964 /* Calculate the real address of the write destination. */
965 addr
+= (offset
* word_size
);
967 /* Get the value as a number. */
968 value
= parse_and_eval_address (argv
[3]);
969 /* Get the value into an array. */
970 buffer
= xmalloc (word_size
);
971 old_chain
= make_cleanup (xfree
, buffer
);
972 store_signed_integer (buffer
, word_size
, value
);
973 /* Write it down to memory. */
974 write_memory (addr
, buffer
, word_size
);
975 /* Free the buffer. */
976 do_cleanups (old_chain
);
982 mi_cmd_enable_timings (char *command
, char **argv
, int argc
)
988 if (strcmp (argv
[0], "yes") == 0)
990 else if (strcmp (argv
[0], "no") == 0)
1001 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command
);
1006 mi_cmd_list_features (char *command
, char **argv
, int argc
)
1010 struct cleanup
*cleanup
= NULL
;
1011 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
1013 ui_out_field_string (uiout
, NULL
, "frozen-varobjs");
1014 ui_out_field_string (uiout
, NULL
, "pending-breakpoints");
1015 ui_out_field_string (uiout
, NULL
, "thread-info");
1017 do_cleanups (cleanup
);
1022 error ("-list-features should be passed no arguments");
1026 /* Execute a command within a safe environment.
1027 Return <0 for error; >=0 for ok.
1029 args->action will tell mi_execute_command what action
1030 to perfrom after the given command has executed (display/suppress
1031 prompt, display error). */
1034 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1036 struct captured_mi_execute_command_args
*args
=
1037 (struct captured_mi_execute_command_args
*) data
;
1038 struct mi_parse
*context
= args
->command
;
1040 struct mi_timestamp cmd_finished
;
1042 switch (context
->op
)
1046 /* A MI command was read from the input stream. */
1048 /* FIXME: gdb_???? */
1049 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1050 context
->token
, context
->command
, context
->args
);
1051 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1052 condition expression, each function should return an
1053 indication of what action is required and then switch on
1055 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1058 current_command_ts
= context
->cmd_start
;
1060 args
->rc
= mi_cmd_execute (context
);
1063 timestamp (&cmd_finished
);
1065 if (!target_can_async_p () || !target_executing
)
1067 /* Print the result if there were no errors.
1069 Remember that on the way out of executing a command, you have
1070 to directly use the mi_interp's uiout, since the command could
1071 have reset the interpreter, in which case the current uiout
1072 will most likely crash in the mi_out_* routines. */
1073 if (args
->rc
== MI_CMD_DONE
)
1075 fputs_unfiltered (context
->token
, raw_stdout
);
1076 fputs_unfiltered ("^done", raw_stdout
);
1077 mi_out_put (uiout
, raw_stdout
);
1078 mi_out_rewind (uiout
);
1079 /* Have to check cmd_start, since the command could be
1081 if (do_timings
&& context
->cmd_start
)
1082 print_diff (context
->cmd_start
, &cmd_finished
);
1083 fputs_unfiltered ("\n", raw_stdout
);
1086 /* The command does not want anything to be printed. In that
1087 case, the command probably should not have written anything
1088 to uiout, but in case it has written something, discard it. */
1089 mi_out_rewind (uiout
);
1096 /* A CLI command was read from the input stream. */
1097 /* This "feature" will be removed as soon as we have a
1098 complete set of mi commands. */
1099 /* Echo the command on the console. */
1100 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1101 /* Call the "console" interpreter. */
1102 argv
[0] = "console";
1103 argv
[1] = context
->command
;
1104 args
->rc
= mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
1106 /* If we changed interpreters, DON'T print out anything. */
1107 if (current_interp_named_p (INTERP_MI
)
1108 || current_interp_named_p (INTERP_MI1
)
1109 || current_interp_named_p (INTERP_MI2
)
1110 || current_interp_named_p (INTERP_MI3
))
1112 if (args
->rc
== MI_CMD_DONE
)
1114 fputs_unfiltered (context
->token
, raw_stdout
);
1115 fputs_unfiltered ("^done", raw_stdout
);
1116 mi_out_put (uiout
, raw_stdout
);
1117 mi_out_rewind (uiout
);
1118 fputs_unfiltered ("\n", raw_stdout
);
1119 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1122 mi_out_rewind (uiout
);
1134 mi_execute_command (char *cmd
, int from_tty
)
1136 struct mi_parse
*command
;
1137 struct captured_mi_execute_command_args args
;
1138 struct ui_out
*saved_uiout
= uiout
;
1140 /* This is to handle EOF (^D). We just quit gdb. */
1141 /* FIXME: we should call some API function here. */
1143 quit_force (NULL
, from_tty
);
1145 command
= mi_parse (cmd
);
1147 if (command
!= NULL
)
1149 struct gdb_exception result
;
1153 command
->cmd_start
= (struct mi_timestamp
*)
1154 xmalloc (sizeof (struct mi_timestamp
));
1155 timestamp (command
->cmd_start
);
1158 args
.command
= command
;
1159 result
= catch_exception (uiout
, captured_mi_execute_command
, &args
,
1161 if (result
.reason
< 0)
1163 /* The command execution failed and error() was called
1165 fputs_unfiltered (command
->token
, raw_stdout
);
1166 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1167 if (result
.message
== NULL
)
1168 fputs_unfiltered ("unknown error", raw_stdout
);
1170 fputstr_unfiltered (result
.message
, '"', raw_stdout
);
1171 fputs_unfiltered ("\"\n", raw_stdout
);
1172 mi_out_rewind (uiout
);
1175 mi_parse_free (command
);
1177 if (args
.action
== EXECUTE_COMMAND_SUPPRESS_PROMPT
)
1178 /* The command is executing synchronously. Bail out early
1179 suppressing the finished prompt. */
1183 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1184 gdb_flush (raw_stdout
);
1185 /* Print any buffered hook code. */
1189 static enum mi_cmd_result
1190 mi_cmd_execute (struct mi_parse
*parse
)
1192 struct cleanup
*cleanup
;
1193 enum mi_cmd_result r
;
1196 if (parse
->cmd
->argv_func
!= NULL
)
1198 if (target_executing
)
1200 if (strcmp (parse
->command
, "exec-interrupt"))
1202 struct ui_file
*stb
;
1203 stb
= mem_fileopen ();
1205 fputs_unfiltered ("Cannot execute command ", stb
);
1206 fputstr_unfiltered (parse
->command
, '"', stb
);
1207 fputs_unfiltered (" while target running", stb
);
1209 make_cleanup_ui_file_delete (stb
);
1213 current_token
= xstrdup (parse
->token
);
1214 cleanup
= make_cleanup (free_current_contents
, ¤t_token
);
1215 r
= parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1216 do_cleanups (cleanup
);
1219 else if (parse
->cmd
->cli
.cmd
!= 0)
1221 /* FIXME: DELETE THIS. */
1222 /* The operation is still implemented by a cli command. */
1223 /* Must be a synchronous one. */
1224 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1230 /* FIXME: DELETE THIS. */
1231 struct ui_file
*stb
;
1233 stb
= mem_fileopen ();
1235 fputs_unfiltered ("Undefined mi command: ", stb
);
1236 fputstr_unfiltered (parse
->command
, '"', stb
);
1237 fputs_unfiltered (" (missing implementation)", stb
);
1239 make_cleanup_ui_file_delete (stb
);
1247 /* FIXME: This is just a hack so we can get some extra commands going.
1248 We don't want to channel things through the CLI, but call libgdb directly.
1249 Use only for synchronous commands. */
1252 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1256 struct cleanup
*old_cleanups
;
1259 run
= xstrprintf ("%s %s", cmd
, args
);
1261 run
= xstrdup (cmd
);
1263 /* FIXME: gdb_???? */
1264 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1266 old_cleanups
= make_cleanup (xfree
, run
);
1267 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1268 do_cleanups (old_cleanups
);
1274 mi_execute_async_cli_command (char *cli_command
, char **argv
, int argc
)
1276 struct cleanup
*old_cleanups
;
1279 if (target_can_async_p ())
1280 run
= xstrprintf ("%s %s&", cli_command
, argc
? *argv
: "");
1282 run
= xstrprintf ("%s %s", cli_command
, argc
? *argv
: "");
1283 old_cleanups
= make_cleanup (xfree
, run
);
1285 if (!target_can_async_p ())
1287 /* NOTE: For synchronous targets asynchronous behavour is faked by
1288 printing out the GDB prompt before we even try to execute the
1291 fputs_unfiltered (current_token
, raw_stdout
);
1292 fputs_unfiltered ("^running\n", raw_stdout
);
1293 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1294 gdb_flush (raw_stdout
);
1298 /* FIXME: cagney/1999-11-29: Printing this message before
1299 calling execute_command is wrong. It should only be printed
1300 once gdb has confirmed that it really has managed to send a
1301 run command to the target. */
1303 fputs_unfiltered (current_token
, raw_stdout
);
1304 fputs_unfiltered ("^running\n", raw_stdout
);
1308 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1310 if (target_can_async_p ())
1312 /* If we're not executing, an exception should have been throw. */
1313 gdb_assert (target_executing
);
1314 do_cleanups (old_cleanups
);
1318 /* Do this before doing any printing. It would appear that some
1319 print code leaves garbage around in the buffer. */
1320 do_cleanups (old_cleanups
);
1322 print_diff_now (current_command_ts
);
1323 return MI_CMD_QUIET
;
1329 mi_load_progress (const char *section_name
,
1330 unsigned long sent_so_far
,
1331 unsigned long total_section
,
1332 unsigned long total_sent
,
1333 unsigned long grand_total
)
1335 struct timeval time_now
, delta
, update_threshold
;
1336 static struct timeval last_update
;
1337 static char *previous_sect_name
= NULL
;
1339 struct ui_out
*saved_uiout
;
1341 /* This function is called through deprecated_show_load_progress
1342 which means uiout may not be correct. Fix it for the duration
1343 of this function. */
1344 saved_uiout
= uiout
;
1346 if (current_interp_named_p (INTERP_MI
)
1347 || current_interp_named_p (INTERP_MI2
))
1348 uiout
= mi_out_new (2);
1349 else if (current_interp_named_p (INTERP_MI1
))
1350 uiout
= mi_out_new (1);
1351 else if (current_interp_named_p (INTERP_MI3
))
1352 uiout
= mi_out_new (3);
1356 update_threshold
.tv_sec
= 0;
1357 update_threshold
.tv_usec
= 500000;
1358 gettimeofday (&time_now
, NULL
);
1360 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1361 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1363 if (delta
.tv_usec
< 0)
1366 delta
.tv_usec
+= 1000000L;
1369 new_section
= (previous_sect_name
?
1370 strcmp (previous_sect_name
, section_name
) : 1);
1373 struct cleanup
*cleanup_tuple
;
1374 xfree (previous_sect_name
);
1375 previous_sect_name
= xstrdup (section_name
);
1378 fputs_unfiltered (current_token
, raw_stdout
);
1379 fputs_unfiltered ("+download", raw_stdout
);
1380 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1381 ui_out_field_string (uiout
, "section", section_name
);
1382 ui_out_field_int (uiout
, "section-size", total_section
);
1383 ui_out_field_int (uiout
, "total-size", grand_total
);
1384 do_cleanups (cleanup_tuple
);
1385 mi_out_put (uiout
, raw_stdout
);
1386 fputs_unfiltered ("\n", raw_stdout
);
1387 gdb_flush (raw_stdout
);
1390 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1391 delta
.tv_usec
>= update_threshold
.tv_usec
)
1393 struct cleanup
*cleanup_tuple
;
1394 last_update
.tv_sec
= time_now
.tv_sec
;
1395 last_update
.tv_usec
= time_now
.tv_usec
;
1397 fputs_unfiltered (current_token
, raw_stdout
);
1398 fputs_unfiltered ("+download", raw_stdout
);
1399 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1400 ui_out_field_string (uiout
, "section", section_name
);
1401 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1402 ui_out_field_int (uiout
, "section-size", total_section
);
1403 ui_out_field_int (uiout
, "total-sent", total_sent
);
1404 ui_out_field_int (uiout
, "total-size", grand_total
);
1405 do_cleanups (cleanup_tuple
);
1406 mi_out_put (uiout
, raw_stdout
);
1407 fputs_unfiltered ("\n", raw_stdout
);
1408 gdb_flush (raw_stdout
);
1412 uiout
= saved_uiout
;
1416 timestamp (struct mi_timestamp
*tv
)
1419 gettimeofday (&tv
->wallclock
, NULL
);
1420 #ifdef HAVE_GETRUSAGE
1421 getrusage (RUSAGE_SELF
, &rusage
);
1422 tv
->utime
.tv_sec
= rusage
.ru_utime
.tv_sec
;
1423 tv
->utime
.tv_usec
= rusage
.ru_utime
.tv_usec
;
1424 tv
->stime
.tv_sec
= rusage
.ru_stime
.tv_sec
;
1425 tv
->stime
.tv_usec
= rusage
.ru_stime
.tv_usec
;
1427 usec
= get_run_time ();
1428 tv
->utime
.tv_sec
= usec
/1000000L;
1429 tv
->utime
.tv_usec
= usec
- 1000000L*tv
->utime
.tv_sec
;
1430 tv
->stime
.tv_sec
= 0;
1431 tv
->stime
.tv_usec
= 0;
1436 print_diff_now (struct mi_timestamp
*start
)
1438 struct mi_timestamp now
;
1440 print_diff (start
, &now
);
1444 timeval_diff (struct timeval start
, struct timeval end
)
1446 return ((end
.tv_sec
- start
.tv_sec
) * 1000000L)
1447 + (end
.tv_usec
- start
.tv_usec
);
1451 print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
)
1455 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1456 timeval_diff (start
->wallclock
, end
->wallclock
) / 1000000.0,
1457 timeval_diff (start
->utime
, end
->utime
) / 1000000.0,
1458 timeval_diff (start
->stime
, end
->stime
) / 1000000.0);