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(). */
54 #if defined HAVE_SYS_RESOURCE_H
55 #include <sys/resource.h>
68 struct ui_file
*raw_stdout
;
70 /* This is used to pass the current command timestamp
71 down to continuation routines. */
72 static struct mi_timestamp
*current_command_ts
;
74 static int do_timings
= 0;
77 int running_result_record_printed
= 1;
79 extern void _initialize_mi_main (void);
80 static void mi_cmd_execute (struct mi_parse
*parse
);
82 static void mi_execute_cli_command (const char *cmd
, int args_p
,
84 static void mi_execute_async_cli_command (char *cli_command
,
85 char **argv
, int argc
);
86 static int register_changed_p (int regnum
, struct regcache
*,
88 static void get_register (int regnum
, int format
);
90 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
91 layer that calls libgdb. Any operation used in the below should be
94 static void timestamp (struct mi_timestamp
*tv
);
96 static void print_diff_now (struct mi_timestamp
*start
);
97 static void print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
);
100 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
102 /* We have to print everything right here because we never return. */
104 fputs_unfiltered (current_token
, raw_stdout
);
105 fputs_unfiltered ("^exit\n", raw_stdout
);
106 mi_out_put (uiout
, raw_stdout
);
107 /* FIXME: The function called is not yet a formal libgdb function. */
108 quit_force (NULL
, FROM_TTY
);
112 mi_cmd_exec_next (char *command
, char **argv
, int argc
)
114 /* FIXME: Should call a libgdb function, not a cli wrapper. */
115 mi_execute_async_cli_command ("next", argv
, argc
);
119 mi_cmd_exec_next_instruction (char *command
, char **argv
, int argc
)
121 /* FIXME: Should call a libgdb function, not a cli wrapper. */
122 mi_execute_async_cli_command ("nexti", argv
, argc
);
126 mi_cmd_exec_step (char *command
, char **argv
, int argc
)
128 /* FIXME: Should call a libgdb function, not a cli wrapper. */
129 mi_execute_async_cli_command ("step", argv
, argc
);
133 mi_cmd_exec_step_instruction (char *command
, char **argv
, int argc
)
135 /* FIXME: Should call a libgdb function, not a cli wrapper. */
136 mi_execute_async_cli_command ("stepi", argv
, argc
);
140 mi_cmd_exec_finish (char *command
, char **argv
, int argc
)
142 /* FIXME: Should call a libgdb function, not a cli wrapper. */
143 mi_execute_async_cli_command ("finish", argv
, argc
);
147 mi_cmd_exec_return (char *command
, char **argv
, int argc
)
149 /* This command doesn't really execute the target, it just pops the
150 specified number of frames. */
152 /* Call return_command with from_tty argument equal to 0 so as to
153 avoid being queried. */
154 return_command (*argv
, 0);
156 /* Call return_command with from_tty argument equal to 0 so as to
157 avoid being queried. */
158 return_command (NULL
, 0);
160 /* Because we have called return_command with from_tty = 0, we need
161 to print the frame here. */
162 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
);
166 mi_cmd_exec_continue (char *command
, char **argv
, int argc
)
170 else if (argc
== 1 && strcmp (argv
[0], "--all") == 0)
173 error ("Usage: -exec-continue [--all]");
176 /* Interrupt the execution of the target. Note how we must play around
177 with the token variables, in order to display the current token in
178 the result of the interrupt command, and the previous execution
179 token when the target finally stops. See comments in
182 mi_cmd_exec_interrupt (char *command
, char **argv
, int argc
)
186 if (!is_running (inferior_ptid
))
187 error ("Current thread is not running.");
189 interrupt_target_1 (0);
191 else if (argc
== 1 && strcmp (argv
[0], "--all") == 0)
194 error ("Inferior not running.");
196 interrupt_target_1 (1);
199 error ("Usage: -exec-interrupt [--all]");
203 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
206 char *mi_error_message
;
209 error ("mi_cmd_thread_select: USAGE: threadnum.");
211 rc
= gdb_thread_select (uiout
, argv
[0], &mi_error_message
);
213 if (rc
== GDB_RC_FAIL
)
215 make_cleanup (xfree
, mi_error_message
);
216 error ("%s", mi_error_message
);
221 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
224 char *mi_error_message
;
227 error ("mi_cmd_thread_list_ids: No arguments required.");
229 rc
= gdb_list_thread_ids (uiout
, &mi_error_message
);
231 if (rc
== GDB_RC_FAIL
)
233 make_cleanup (xfree
, mi_error_message
);
234 error ("%s", mi_error_message
);
239 mi_cmd_thread_info (char *command
, char **argv
, int argc
)
243 if (argc
!= 0 && argc
!= 1)
244 error ("Invalid MI command");
247 thread
= atoi (argv
[0]);
249 print_thread_info (uiout
, thread
, -1);
253 print_one_inferior (struct inferior
*inferior
, void *arg
)
255 struct cleanup
*back_to
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
257 ui_out_field_fmt (uiout
, "id", "%d", inferior
->pid
);
258 ui_out_field_string (uiout
, "type", "process");
259 ui_out_field_int (uiout
, "pid", inferior
->pid
);
261 do_cleanups (back_to
);
266 mi_cmd_list_thread_groups (char *command
, char **argv
, int argc
)
268 struct cleanup
*back_to
;
272 if (argc
> 0 && strcmp (argv
[0], "--available") == 0)
282 back_to
= make_cleanup (&null_cleanup
, NULL
);
287 if (!in_inferior_list (pid
))
288 error ("Invalid thread group id '%s'", id
);
289 print_thread_info (uiout
, -1, pid
);
293 make_cleanup_ui_out_list_begin_end (uiout
, "groups");
294 iterate_over_inferiors (print_one_inferior
, NULL
);
297 do_cleanups (back_to
);
301 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
305 struct cleanup
*cleanup
;
307 /* Note that the test for a valid register must include checking the
308 gdbarch_register_name because gdbarch_num_regs may be allocated for
309 the union of the register sets within a family of related processors.
310 In this case, some entries of gdbarch_register_name will change depending
311 upon the particular processor being debugged. */
313 numregs
= gdbarch_num_regs (current_gdbarch
)
314 + gdbarch_num_pseudo_regs (current_gdbarch
);
316 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
318 if (argc
== 0) /* No args, just do all the regs. */
324 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
325 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
326 ui_out_field_string (uiout
, NULL
, "");
328 ui_out_field_string (uiout
, NULL
,
329 gdbarch_register_name
330 (current_gdbarch
, regnum
));
334 /* Else, list of register #s, just do listed regs. */
335 for (i
= 0; i
< argc
; i
++)
337 regnum
= atoi (argv
[i
]);
338 if (regnum
< 0 || regnum
>= numregs
)
339 error ("bad register number");
341 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
342 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
343 ui_out_field_string (uiout
, NULL
, "");
345 ui_out_field_string (uiout
, NULL
,
346 gdbarch_register_name (current_gdbarch
, regnum
));
348 do_cleanups (cleanup
);
352 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
354 static struct regcache
*this_regs
= NULL
;
355 struct regcache
*prev_regs
;
356 int regnum
, numregs
, changed
;
358 struct cleanup
*cleanup
;
360 /* The last time we visited this function, the current frame's register
361 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
362 and refresh THIS_REGS with the now-current register contents. */
364 prev_regs
= this_regs
;
365 this_regs
= frame_save_as_regcache (get_selected_frame (NULL
));
366 cleanup
= make_cleanup_regcache_xfree (prev_regs
);
368 /* Note that the test for a valid register must include checking the
369 gdbarch_register_name because gdbarch_num_regs may be allocated for
370 the union of the register sets within a family of related processors.
371 In this case, some entries of gdbarch_register_name will change depending
372 upon the particular processor being debugged. */
374 numregs
= gdbarch_num_regs (current_gdbarch
)
375 + gdbarch_num_pseudo_regs (current_gdbarch
);
377 make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
379 if (argc
== 0) /* No args, just do all the regs. */
385 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
386 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
388 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
390 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
392 ui_out_field_int (uiout
, NULL
, regnum
);
396 /* Else, list of register #s, just do listed regs. */
397 for (i
= 0; i
< argc
; i
++)
399 regnum
= atoi (argv
[i
]);
403 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
404 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
406 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
408 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
410 ui_out_field_int (uiout
, NULL
, regnum
);
413 error ("bad register number");
415 do_cleanups (cleanup
);
419 register_changed_p (int regnum
, struct regcache
*prev_regs
,
420 struct regcache
*this_regs
)
422 struct gdbarch
*gdbarch
= get_regcache_arch (this_regs
);
423 gdb_byte prev_buffer
[MAX_REGISTER_SIZE
];
424 gdb_byte this_buffer
[MAX_REGISTER_SIZE
];
426 /* Registers not valid in this frame return count as unchanged. */
427 if (!regcache_valid_p (this_regs
, regnum
))
430 /* First time through or after gdbarch change consider all registers as
431 changed. Same for registers not valid in the previous frame. */
432 if (!prev_regs
|| get_regcache_arch (prev_regs
) != gdbarch
433 || !regcache_valid_p (prev_regs
, regnum
))
436 /* Get register contents and compare. */
437 regcache_cooked_read (prev_regs
, regnum
, prev_buffer
);
438 regcache_cooked_read (this_regs
, regnum
, this_buffer
);
440 return memcmp (prev_buffer
, this_buffer
,
441 register_size (gdbarch
, regnum
)) != 0;
444 /* Return a list of register number and value pairs. The valid
445 arguments expected are: a letter indicating the format in which to
446 display the registers contents. This can be one of: x (hexadecimal), d
447 (decimal), N (natural), t (binary), o (octal), r (raw). After the
448 format argumetn there can be a sequence of numbers, indicating which
449 registers to fetch the content of. If the format is the only argument,
450 a list of all the registers with their values is returned. */
452 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
454 int regnum
, numregs
, format
;
456 struct cleanup
*list_cleanup
, *tuple_cleanup
;
458 /* Note that the test for a valid register must include checking the
459 gdbarch_register_name because gdbarch_num_regs may be allocated for
460 the union of the register sets within a family of related processors.
461 In this case, some entries of gdbarch_register_name will change depending
462 upon the particular processor being debugged. */
464 numregs
= gdbarch_num_regs (current_gdbarch
)
465 + gdbarch_num_pseudo_regs (current_gdbarch
);
468 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
470 format
= (int) argv
[0][0];
472 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
474 if (argc
== 1) /* No args, beside the format: do all the regs. */
480 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
481 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
483 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
484 ui_out_field_int (uiout
, "number", regnum
);
485 get_register (regnum
, format
);
486 do_cleanups (tuple_cleanup
);
490 /* Else, list of register #s, just do listed regs. */
491 for (i
= 1; i
< argc
; i
++)
493 regnum
= atoi (argv
[i
]);
497 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
498 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
500 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
501 ui_out_field_int (uiout
, "number", regnum
);
502 get_register (regnum
, format
);
503 do_cleanups (tuple_cleanup
);
506 error ("bad register number");
508 do_cleanups (list_cleanup
);
511 /* Output one register's contents in the desired format. */
513 get_register (int regnum
, int format
)
515 gdb_byte buffer
[MAX_REGISTER_SIZE
];
520 static struct ui_stream
*stb
= NULL
;
522 stb
= ui_out_stream_new (uiout
);
527 frame_register (get_selected_frame (NULL
), regnum
, &optim
, &lval
, &addr
,
531 error ("Optimized out");
536 char *ptr
, buf
[1024];
540 for (j
= 0; j
< register_size (current_gdbarch
, regnum
); j
++)
542 int idx
= gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
? j
543 : register_size (current_gdbarch
, regnum
) - 1 - j
;
544 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
547 ui_out_field_string (uiout
, "value", buf
);
548 /*fputs_filtered (buf, gdb_stdout); */
552 struct value_print_options opts
;
553 get_formatted_print_options (&opts
, format
);
555 val_print (register_type (current_gdbarch
, regnum
), buffer
, 0, 0,
556 stb
->stream
, 0, &opts
, current_language
);
557 ui_out_field_stream (uiout
, "value", stb
);
558 ui_out_stream_delete (stb
);
562 /* Write given values into registers. The registers and values are
563 given as pairs. The corresponding MI command is
564 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
566 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
571 /* Note that the test for a valid register must include checking the
572 gdbarch_register_name because gdbarch_num_regs may be allocated for
573 the union of the register sets within a family of related processors.
574 In this case, some entries of gdbarch_register_name will change depending
575 upon the particular processor being debugged. */
577 numregs
= gdbarch_num_regs (current_gdbarch
)
578 + gdbarch_num_pseudo_regs (current_gdbarch
);
581 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
583 format
= (int) argv
[0][0];
585 if (!target_has_registers
)
586 error ("mi_cmd_data_write_register_values: No registers.");
589 error ("mi_cmd_data_write_register_values: No regs and values specified.");
592 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
594 for (i
= 1; i
< argc
; i
= i
+ 2)
596 int regnum
= atoi (argv
[i
]);
598 if (regnum
>= 0 && regnum
< numregs
599 && gdbarch_register_name (current_gdbarch
, regnum
)
600 && *gdbarch_register_name (current_gdbarch
, regnum
))
604 /* Get the value as a number. */
605 value
= parse_and_eval_address (argv
[i
+ 1]);
608 regcache_cooked_write_signed (get_current_regcache (), regnum
, value
);
611 error ("bad register number");
615 /* Evaluate the value of the argument. The argument is an
616 expression. If the expression contains spaces it needs to be
617 included in double quotes. */
619 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
621 struct expression
*expr
;
622 struct cleanup
*old_chain
= NULL
;
624 struct ui_stream
*stb
= NULL
;
625 struct value_print_options opts
;
627 stb
= ui_out_stream_new (uiout
);
631 ui_out_stream_delete (stb
);
632 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
635 expr
= parse_expression (argv
[0]);
637 old_chain
= make_cleanup (free_current_contents
, &expr
);
639 val
= evaluate_expression (expr
);
641 /* Print the result of the expression evaluation. */
642 get_user_print_options (&opts
);
644 val_print (value_type (val
), value_contents (val
),
645 value_embedded_offset (val
), VALUE_ADDRESS (val
),
646 stb
->stream
, 0, &opts
, current_language
);
648 ui_out_field_stream (uiout
, "value", stb
);
649 ui_out_stream_delete (stb
);
651 do_cleanups (old_chain
);
656 ADDR: start address of data to be dumped.
657 WORD-FORMAT: a char indicating format for the ``word''. See
659 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
660 NR_ROW: Number of rows.
661 NR_COL: The number of colums (words per row).
662 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
663 ASCHAR for unprintable characters.
665 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
666 displayes them. Returns:
668 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
671 The number of bytes read is SIZE*ROW*COL. */
674 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
676 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
682 struct type
*word_type
;
695 static struct mi_opt opts
[] =
697 {"o", OFFSET_OPT
, 1},
703 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
707 switch ((enum opt
) opt
)
710 offset
= atol (optarg
);
717 if (argc
< 5 || argc
> 6)
718 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
720 /* Extract all the arguments. */
722 /* Start address of the memory dump. */
723 addr
= parse_and_eval_address (argv
[0]) + offset
;
724 /* The format character to use when displaying a memory word. See
725 the ``x'' command. */
726 word_format
= argv
[1][0];
727 /* The size of the memory word. */
728 word_size
= atol (argv
[2]);
732 word_type
= builtin_type_int8
;
736 word_type
= builtin_type_int16
;
740 word_type
= builtin_type_int32
;
744 word_type
= builtin_type_int64
;
748 word_type
= builtin_type_int8
;
751 /* The number of rows. */
752 nr_rows
= atol (argv
[3]);
754 error ("mi_cmd_data_read_memory: invalid number of rows.");
756 /* Number of bytes per row. */
757 nr_cols
= atol (argv
[4]);
759 error ("mi_cmd_data_read_memory: invalid number of columns.");
761 /* The un-printable character when printing ascii. */
767 /* Create a buffer and read it in. */
768 total_bytes
= word_size
* nr_rows
* nr_cols
;
769 mbuf
= xcalloc (total_bytes
, 1);
770 make_cleanup (xfree
, mbuf
);
772 nr_bytes
= target_read_until_error (¤t_target
, TARGET_OBJECT_MEMORY
,
773 NULL
, mbuf
, addr
, total_bytes
);
775 error ("Unable to read memory.");
777 /* Output the header information. */
778 ui_out_field_core_addr (uiout
, "addr", addr
);
779 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
780 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
781 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
782 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
783 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
784 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
786 /* Build the result as a two dimentional table. */
788 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
789 struct cleanup
*cleanup_list_memory
;
792 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
793 for (row
= 0, row_byte
= 0;
795 row
++, row_byte
+= nr_cols
* word_size
)
799 struct cleanup
*cleanup_tuple
;
800 struct cleanup
*cleanup_list_data
;
801 struct value_print_options opts
;
803 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
804 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
805 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
806 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
807 get_formatted_print_options (&opts
, word_format
);
808 for (col
= 0, col_byte
= row_byte
;
810 col
++, col_byte
+= word_size
)
812 if (col_byte
+ word_size
> nr_bytes
)
814 ui_out_field_string (uiout
, NULL
, "N/A");
818 ui_file_rewind (stream
->stream
);
819 print_scalar_formatted (mbuf
+ col_byte
, word_type
, &opts
,
820 word_asize
, stream
->stream
);
821 ui_out_field_stream (uiout
, NULL
, stream
);
824 do_cleanups (cleanup_list_data
);
828 ui_file_rewind (stream
->stream
);
829 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
831 if (byte
>= nr_bytes
)
833 fputc_unfiltered ('X', stream
->stream
);
835 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
837 fputc_unfiltered (aschar
, stream
->stream
);
840 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
842 ui_out_field_stream (uiout
, "ascii", stream
);
844 do_cleanups (cleanup_tuple
);
846 ui_out_stream_delete (stream
);
847 do_cleanups (cleanup_list_memory
);
849 do_cleanups (cleanups
);
852 /* DATA-MEMORY-WRITE:
854 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
855 offset from the beginning of the memory grid row where the cell to
857 ADDR: start address of the row in the memory grid where the memory
858 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
859 the location to write to.
860 FORMAT: a char indicating format for the ``word''. See
862 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
863 VALUE: value to be written into the memory address.
865 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
869 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
874 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
875 enough when using a compiler other than GCC. */
878 struct cleanup
*old_chain
;
886 static struct mi_opt opts
[] =
888 {"o", OFFSET_OPT
, 1},
894 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
898 switch ((enum opt
) opt
)
901 offset
= atol (optarg
);
909 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
911 /* Extract all the arguments. */
912 /* Start address of the memory dump. */
913 addr
= parse_and_eval_address (argv
[0]);
914 /* The format character to use when displaying a memory word. See
915 the ``x'' command. */
916 word_format
= argv
[1][0];
917 /* The size of the memory word. */
918 word_size
= atol (argv
[2]);
920 /* Calculate the real address of the write destination. */
921 addr
+= (offset
* word_size
);
923 /* Get the value as a number. */
924 value
= parse_and_eval_address (argv
[3]);
925 /* Get the value into an array. */
926 buffer
= xmalloc (word_size
);
927 old_chain
= make_cleanup (xfree
, buffer
);
928 store_signed_integer (buffer
, word_size
, value
);
929 /* Write it down to memory. */
930 write_memory (addr
, buffer
, word_size
);
931 /* Free the buffer. */
932 do_cleanups (old_chain
);
936 mi_cmd_enable_timings (char *command
, char **argv
, int argc
)
942 if (strcmp (argv
[0], "yes") == 0)
944 else if (strcmp (argv
[0], "no") == 0)
955 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command
);
959 mi_cmd_list_features (char *command
, char **argv
, int argc
)
963 struct cleanup
*cleanup
= NULL
;
964 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
966 ui_out_field_string (uiout
, NULL
, "frozen-varobjs");
967 ui_out_field_string (uiout
, NULL
, "pending-breakpoints");
968 ui_out_field_string (uiout
, NULL
, "thread-info");
970 do_cleanups (cleanup
);
974 error ("-list-features should be passed no arguments");
978 mi_cmd_list_target_features (char *command
, char **argv
, int argc
)
982 struct cleanup
*cleanup
= NULL
;
983 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
985 if (target_can_async_p ())
986 ui_out_field_string (uiout
, NULL
, "async");
988 do_cleanups (cleanup
);
992 error ("-list-target-features should be passed no arguments");
995 /* Execute a command within a safe environment.
996 Return <0 for error; >=0 for ok.
998 args->action will tell mi_execute_command what action
999 to perfrom after the given command has executed (display/suppress
1000 prompt, display error). */
1003 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1005 struct mi_parse
*context
= (struct mi_parse
*) data
;
1007 struct mi_timestamp cmd_finished
;
1009 running_result_record_printed
= 0;
1010 switch (context
->op
)
1013 /* A MI command was read from the input stream. */
1015 /* FIXME: gdb_???? */
1016 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1017 context
->token
, context
->command
, context
->args
);
1020 current_command_ts
= context
->cmd_start
;
1022 mi_cmd_execute (context
);
1025 timestamp (&cmd_finished
);
1027 /* Print the result if there were no errors.
1029 Remember that on the way out of executing a command, you have
1030 to directly use the mi_interp's uiout, since the command could
1031 have reset the interpreter, in which case the current uiout
1032 will most likely crash in the mi_out_* routines. */
1033 if (!running_result_record_printed
)
1035 fputs_unfiltered (context
->token
, raw_stdout
);
1036 /* There's no particularly good reason why target-connect results
1037 in not ^done. Should kill ^connected for MI3. */
1038 fputs_unfiltered (strcmp (context
->command
, "target-select") == 0
1039 ? "^connected" : "^done", raw_stdout
);
1040 mi_out_put (uiout
, raw_stdout
);
1041 mi_out_rewind (uiout
);
1042 /* Have to check cmd_start, since the command could be
1044 if (do_timings
&& context
->cmd_start
)
1045 print_diff (context
->cmd_start
, &cmd_finished
);
1046 fputs_unfiltered ("\n", raw_stdout
);
1049 /* The command does not want anything to be printed. In that
1050 case, the command probably should not have written anything
1051 to uiout, but in case it has written something, discard it. */
1052 mi_out_rewind (uiout
);
1058 /* A CLI command was read from the input stream. */
1059 /* This "feature" will be removed as soon as we have a
1060 complete set of mi commands. */
1061 /* Echo the command on the console. */
1062 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1063 /* Call the "console" interpreter. */
1064 argv
[0] = "console";
1065 argv
[1] = context
->command
;
1066 mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
1068 /* If we changed interpreters, DON'T print out anything. */
1069 if (current_interp_named_p (INTERP_MI
)
1070 || current_interp_named_p (INTERP_MI1
)
1071 || current_interp_named_p (INTERP_MI2
)
1072 || current_interp_named_p (INTERP_MI3
))
1074 if (!running_result_record_printed
)
1076 fputs_unfiltered (context
->token
, raw_stdout
);
1077 fputs_unfiltered ("^done", raw_stdout
);
1078 mi_out_put (uiout
, raw_stdout
);
1079 mi_out_rewind (uiout
);
1080 fputs_unfiltered ("\n", raw_stdout
);
1083 mi_out_rewind (uiout
);
1095 mi_execute_command (char *cmd
, int from_tty
)
1097 struct mi_parse
*command
;
1098 struct ui_out
*saved_uiout
= uiout
;
1100 /* This is to handle EOF (^D). We just quit gdb. */
1101 /* FIXME: we should call some API function here. */
1103 quit_force (NULL
, from_tty
);
1105 command
= mi_parse (cmd
);
1107 if (command
!= NULL
)
1109 struct gdb_exception result
;
1113 command
->cmd_start
= (struct mi_timestamp
*)
1114 xmalloc (sizeof (struct mi_timestamp
));
1115 timestamp (command
->cmd_start
);
1118 result
= catch_exception (uiout
, captured_mi_execute_command
, command
,
1120 if (result
.reason
< 0)
1122 /* The command execution failed and error() was called
1124 fputs_unfiltered (command
->token
, raw_stdout
);
1125 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1126 if (result
.message
== NULL
)
1127 fputs_unfiltered ("unknown error", raw_stdout
);
1129 fputstr_unfiltered (result
.message
, '"', raw_stdout
);
1130 fputs_unfiltered ("\"\n", raw_stdout
);
1131 mi_out_rewind (uiout
);
1134 mi_parse_free (command
);
1137 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1138 gdb_flush (raw_stdout
);
1139 /* Print any buffered hook code. */
1144 mi_cmd_execute (struct mi_parse
*parse
)
1146 struct cleanup
*cleanup
;
1150 current_token
= xstrdup (parse
->token
);
1151 cleanup
= make_cleanup (free_current_contents
, ¤t_token
);
1153 if (parse
->frame
!= -1 && parse
->thread
== -1)
1154 error (_("Cannot specify --frame without --thread"));
1156 if (parse
->thread
!= -1)
1158 struct thread_info
*tp
= find_thread_id (parse
->thread
);
1160 error (_("Invalid thread id: %d"), parse
->thread
);
1162 if (is_exited (tp
->ptid
))
1163 error (_("Thread id: %d has terminated"), parse
->thread
);
1165 switch_to_thread (tp
->ptid
);
1168 if (parse
->frame
!= -1)
1170 struct frame_info
*fid
;
1171 int frame
= parse
->frame
;
1172 fid
= find_relative_frame (get_current_frame (), &frame
);
1174 /* find_relative_frame was successful */
1177 error (_("Invalid frame id: %d"), frame
);
1180 if (parse
->cmd
->argv_func
!= NULL
)
1182 if (target_can_async_p ()
1183 && target_has_execution
1184 && (is_exited (inferior_ptid
))
1185 && (strcmp (parse
->command
, "thread-info") != 0
1186 && strcmp (parse
->command
, "thread-list-ids") != 0
1187 && strcmp (parse
->command
, "thread-select") != 0))
1189 struct ui_file
*stb
;
1190 stb
= mem_fileopen ();
1192 fputs_unfiltered ("Cannot execute command ", stb
);
1193 fputstr_unfiltered (parse
->command
, '"', stb
);
1194 fputs_unfiltered (" without a selected thread", stb
);
1196 make_cleanup_ui_file_delete (stb
);
1200 parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1202 else if (parse
->cmd
->cli
.cmd
!= 0)
1204 /* FIXME: DELETE THIS. */
1205 /* The operation is still implemented by a cli command. */
1206 /* Must be a synchronous one. */
1207 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1212 /* FIXME: DELETE THIS. */
1213 struct ui_file
*stb
;
1215 stb
= mem_fileopen ();
1217 fputs_unfiltered ("Undefined mi command: ", stb
);
1218 fputstr_unfiltered (parse
->command
, '"', stb
);
1219 fputs_unfiltered (" (missing implementation)", stb
);
1221 make_cleanup_ui_file_delete (stb
);
1224 do_cleanups (cleanup
);
1227 /* FIXME: This is just a hack so we can get some extra commands going.
1228 We don't want to channel things through the CLI, but call libgdb directly.
1229 Use only for synchronous commands. */
1232 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1236 struct cleanup
*old_cleanups
;
1239 run
= xstrprintf ("%s %s", cmd
, args
);
1241 run
= xstrdup (cmd
);
1243 /* FIXME: gdb_???? */
1244 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1246 old_cleanups
= make_cleanup (xfree
, run
);
1247 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1248 do_cleanups (old_cleanups
);
1254 mi_execute_async_cli_command (char *cli_command
, char **argv
, int argc
)
1256 struct cleanup
*old_cleanups
;
1259 if (target_can_async_p ())
1260 run
= xstrprintf ("%s %s&", cli_command
, argc
? *argv
: "");
1262 run
= xstrprintf ("%s %s", cli_command
, argc
? *argv
: "");
1263 old_cleanups
= make_cleanup (xfree
, run
);
1265 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1267 if (target_can_async_p ())
1269 /* If we're not executing, an exception should have been throw. */
1270 gdb_assert (is_running (inferior_ptid
));
1271 do_cleanups (old_cleanups
);
1275 /* Do this before doing any printing. It would appear that some
1276 print code leaves garbage around in the buffer. */
1277 do_cleanups (old_cleanups
);
1279 print_diff_now (current_command_ts
);
1284 mi_load_progress (const char *section_name
,
1285 unsigned long sent_so_far
,
1286 unsigned long total_section
,
1287 unsigned long total_sent
,
1288 unsigned long grand_total
)
1290 struct timeval time_now
, delta
, update_threshold
;
1291 static struct timeval last_update
;
1292 static char *previous_sect_name
= NULL
;
1294 struct ui_out
*saved_uiout
;
1296 /* This function is called through deprecated_show_load_progress
1297 which means uiout may not be correct. Fix it for the duration
1298 of this function. */
1299 saved_uiout
= uiout
;
1301 if (current_interp_named_p (INTERP_MI
)
1302 || current_interp_named_p (INTERP_MI2
))
1303 uiout
= mi_out_new (2);
1304 else if (current_interp_named_p (INTERP_MI1
))
1305 uiout
= mi_out_new (1);
1306 else if (current_interp_named_p (INTERP_MI3
))
1307 uiout
= mi_out_new (3);
1311 update_threshold
.tv_sec
= 0;
1312 update_threshold
.tv_usec
= 500000;
1313 gettimeofday (&time_now
, NULL
);
1315 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1316 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1318 if (delta
.tv_usec
< 0)
1321 delta
.tv_usec
+= 1000000L;
1324 new_section
= (previous_sect_name
?
1325 strcmp (previous_sect_name
, section_name
) : 1);
1328 struct cleanup
*cleanup_tuple
;
1329 xfree (previous_sect_name
);
1330 previous_sect_name
= xstrdup (section_name
);
1333 fputs_unfiltered (current_token
, raw_stdout
);
1334 fputs_unfiltered ("+download", raw_stdout
);
1335 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1336 ui_out_field_string (uiout
, "section", section_name
);
1337 ui_out_field_int (uiout
, "section-size", total_section
);
1338 ui_out_field_int (uiout
, "total-size", grand_total
);
1339 do_cleanups (cleanup_tuple
);
1340 mi_out_put (uiout
, raw_stdout
);
1341 fputs_unfiltered ("\n", raw_stdout
);
1342 gdb_flush (raw_stdout
);
1345 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1346 delta
.tv_usec
>= update_threshold
.tv_usec
)
1348 struct cleanup
*cleanup_tuple
;
1349 last_update
.tv_sec
= time_now
.tv_sec
;
1350 last_update
.tv_usec
= time_now
.tv_usec
;
1352 fputs_unfiltered (current_token
, raw_stdout
);
1353 fputs_unfiltered ("+download", raw_stdout
);
1354 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1355 ui_out_field_string (uiout
, "section", section_name
);
1356 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1357 ui_out_field_int (uiout
, "section-size", total_section
);
1358 ui_out_field_int (uiout
, "total-sent", total_sent
);
1359 ui_out_field_int (uiout
, "total-size", grand_total
);
1360 do_cleanups (cleanup_tuple
);
1361 mi_out_put (uiout
, raw_stdout
);
1362 fputs_unfiltered ("\n", raw_stdout
);
1363 gdb_flush (raw_stdout
);
1367 uiout
= saved_uiout
;
1371 timestamp (struct mi_timestamp
*tv
)
1374 gettimeofday (&tv
->wallclock
, NULL
);
1375 #ifdef HAVE_GETRUSAGE
1376 getrusage (RUSAGE_SELF
, &rusage
);
1377 tv
->utime
.tv_sec
= rusage
.ru_utime
.tv_sec
;
1378 tv
->utime
.tv_usec
= rusage
.ru_utime
.tv_usec
;
1379 tv
->stime
.tv_sec
= rusage
.ru_stime
.tv_sec
;
1380 tv
->stime
.tv_usec
= rusage
.ru_stime
.tv_usec
;
1382 usec
= get_run_time ();
1383 tv
->utime
.tv_sec
= usec
/1000000L;
1384 tv
->utime
.tv_usec
= usec
- 1000000L*tv
->utime
.tv_sec
;
1385 tv
->stime
.tv_sec
= 0;
1386 tv
->stime
.tv_usec
= 0;
1391 print_diff_now (struct mi_timestamp
*start
)
1393 struct mi_timestamp now
;
1395 print_diff (start
, &now
);
1399 timeval_diff (struct timeval start
, struct timeval end
)
1401 return ((end
.tv_sec
- start
.tv_sec
) * 1000000L)
1402 + (end
.tv_usec
- start
.tv_usec
);
1406 print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
)
1410 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1411 timeval_diff (start
->wallclock
, end
->wallclock
) / 1000000.0,
1412 timeval_diff (start
->utime
, end
->utime
) / 1000000.0,
1413 timeval_diff (start
->stime
, end
->stime
) / 1000000.0);