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(). */
53 #if defined HAVE_SYS_RESOURCE_H
54 #include <sys/resource.h>
67 struct ui_file
*raw_stdout
;
69 /* This is used to pass the current command timestamp
70 down to continuation routines. */
71 static struct mi_timestamp
*current_command_ts
;
73 static int do_timings
= 0;
76 int running_result_record_printed
= 1;
78 extern void _initialize_mi_main (void);
79 static void mi_cmd_execute (struct mi_parse
*parse
);
81 static void mi_execute_cli_command (const char *cmd
, int args_p
,
83 static void mi_execute_async_cli_command (char *cli_command
,
84 char **argv
, int argc
);
85 static int register_changed_p (int regnum
, struct regcache
*,
87 static void get_register (int regnum
, int format
);
89 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
90 layer that calls libgdb. Any operation used in the below should be
93 static void timestamp (struct mi_timestamp
*tv
);
95 static void print_diff_now (struct mi_timestamp
*start
);
96 static void print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
);
99 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
101 /* We have to print everything right here because we never return. */
103 fputs_unfiltered (current_token
, raw_stdout
);
104 fputs_unfiltered ("^exit\n", raw_stdout
);
105 mi_out_put (uiout
, raw_stdout
);
106 /* FIXME: The function called is not yet a formal libgdb function. */
107 quit_force (NULL
, FROM_TTY
);
111 mi_cmd_exec_next (char *command
, char **argv
, int argc
)
113 /* FIXME: Should call a libgdb function, not a cli wrapper. */
114 mi_execute_async_cli_command ("next", argv
, argc
);
118 mi_cmd_exec_next_instruction (char *command
, char **argv
, int argc
)
120 /* FIXME: Should call a libgdb function, not a cli wrapper. */
121 mi_execute_async_cli_command ("nexti", argv
, argc
);
125 mi_cmd_exec_step (char *command
, char **argv
, int argc
)
127 /* FIXME: Should call a libgdb function, not a cli wrapper. */
128 mi_execute_async_cli_command ("step", argv
, argc
);
132 mi_cmd_exec_step_instruction (char *command
, char **argv
, int argc
)
134 /* FIXME: Should call a libgdb function, not a cli wrapper. */
135 mi_execute_async_cli_command ("stepi", argv
, argc
);
139 mi_cmd_exec_finish (char *command
, char **argv
, int argc
)
141 /* FIXME: Should call a libgdb function, not a cli wrapper. */
142 mi_execute_async_cli_command ("finish", argv
, argc
);
146 mi_cmd_exec_return (char *command
, char **argv
, int argc
)
148 /* This command doesn't really execute the target, it just pops the
149 specified number of frames. */
151 /* Call return_command with from_tty argument equal to 0 so as to
152 avoid being queried. */
153 return_command (*argv
, 0);
155 /* Call return_command with from_tty argument equal to 0 so as to
156 avoid being queried. */
157 return_command (NULL
, 0);
159 /* Because we have called return_command with from_tty = 0, we need
160 to print the frame here. */
161 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
);
165 mi_cmd_exec_continue (char *command
, char **argv
, int argc
)
169 else if (argc
== 1 && strcmp (argv
[0], "--all") == 0)
172 error ("Usage: -exec-continue [--all]");
175 /* Interrupt the execution of the target. Note how we must play around
176 with the token variables, in order to display the current token in
177 the result of the interrupt command, and the previous execution
178 token when the target finally stops. See comments in
181 mi_cmd_exec_interrupt (char *command
, char **argv
, int argc
)
185 if (!is_running (inferior_ptid
))
186 error ("Current thread is not running.");
188 interrupt_target_1 (0);
190 else if (argc
== 1 && strcmp (argv
[0], "--all") == 0)
193 error ("Inferior not running.");
195 interrupt_target_1 (1);
198 error ("Usage: -exec-interrupt [--all]");
202 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
205 char *mi_error_message
;
208 error ("mi_cmd_thread_select: USAGE: threadnum.");
210 rc
= gdb_thread_select (uiout
, argv
[0], &mi_error_message
);
212 if (rc
== GDB_RC_FAIL
)
214 make_cleanup (xfree
, mi_error_message
);
215 error ("%s", mi_error_message
);
220 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
223 char *mi_error_message
;
226 error ("mi_cmd_thread_list_ids: No arguments required.");
228 rc
= gdb_list_thread_ids (uiout
, &mi_error_message
);
230 if (rc
== GDB_RC_FAIL
)
232 make_cleanup (xfree
, mi_error_message
);
233 error ("%s", mi_error_message
);
238 mi_cmd_thread_info (char *command
, char **argv
, int argc
)
242 if (argc
!= 0 && argc
!= 1)
243 error ("Invalid MI command");
246 thread
= atoi (argv
[0]);
248 print_thread_info (uiout
, thread
);
252 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
256 struct cleanup
*cleanup
;
258 /* Note that the test for a valid register must include checking the
259 gdbarch_register_name because gdbarch_num_regs may be allocated for
260 the union of the register sets within a family of related processors.
261 In this case, some entries of gdbarch_register_name will change depending
262 upon the particular processor being debugged. */
264 numregs
= gdbarch_num_regs (current_gdbarch
)
265 + gdbarch_num_pseudo_regs (current_gdbarch
);
267 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
269 if (argc
== 0) /* No args, just do all the regs. */
275 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
276 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
277 ui_out_field_string (uiout
, NULL
, "");
279 ui_out_field_string (uiout
, NULL
,
280 gdbarch_register_name
281 (current_gdbarch
, regnum
));
285 /* Else, list of register #s, just do listed regs. */
286 for (i
= 0; i
< argc
; i
++)
288 regnum
= atoi (argv
[i
]);
289 if (regnum
< 0 || regnum
>= numregs
)
290 error ("bad register number");
292 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
293 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
294 ui_out_field_string (uiout
, NULL
, "");
296 ui_out_field_string (uiout
, NULL
,
297 gdbarch_register_name (current_gdbarch
, regnum
));
299 do_cleanups (cleanup
);
303 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
305 static struct regcache
*this_regs
= NULL
;
306 struct regcache
*prev_regs
;
307 int regnum
, numregs
, changed
;
309 struct cleanup
*cleanup
;
311 /* The last time we visited this function, the current frame's register
312 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
313 and refresh THIS_REGS with the now-current register contents. */
315 prev_regs
= this_regs
;
316 this_regs
= frame_save_as_regcache (get_selected_frame (NULL
));
317 cleanup
= make_cleanup_regcache_xfree (prev_regs
);
319 /* Note that the test for a valid register must include checking the
320 gdbarch_register_name because gdbarch_num_regs may be allocated for
321 the union of the register sets within a family of related processors.
322 In this case, some entries of gdbarch_register_name will change depending
323 upon the particular processor being debugged. */
325 numregs
= gdbarch_num_regs (current_gdbarch
)
326 + gdbarch_num_pseudo_regs (current_gdbarch
);
328 make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
330 if (argc
== 0) /* No args, just do all the regs. */
336 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
337 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
339 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
341 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
343 ui_out_field_int (uiout
, NULL
, regnum
);
347 /* Else, list of register #s, just do listed regs. */
348 for (i
= 0; i
< argc
; i
++)
350 regnum
= atoi (argv
[i
]);
354 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
355 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
357 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
359 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
361 ui_out_field_int (uiout
, NULL
, regnum
);
364 error ("bad register number");
366 do_cleanups (cleanup
);
370 register_changed_p (int regnum
, struct regcache
*prev_regs
,
371 struct regcache
*this_regs
)
373 struct gdbarch
*gdbarch
= get_regcache_arch (this_regs
);
374 gdb_byte prev_buffer
[MAX_REGISTER_SIZE
];
375 gdb_byte this_buffer
[MAX_REGISTER_SIZE
];
377 /* Registers not valid in this frame return count as unchanged. */
378 if (!regcache_valid_p (this_regs
, regnum
))
381 /* First time through or after gdbarch change consider all registers as
382 changed. Same for registers not valid in the previous frame. */
383 if (!prev_regs
|| get_regcache_arch (prev_regs
) != gdbarch
384 || !regcache_valid_p (prev_regs
, regnum
))
387 /* Get register contents and compare. */
388 regcache_cooked_read (prev_regs
, regnum
, prev_buffer
);
389 regcache_cooked_read (this_regs
, regnum
, this_buffer
);
391 return memcmp (prev_buffer
, this_buffer
,
392 register_size (gdbarch
, regnum
)) != 0;
395 /* Return a list of register number and value pairs. The valid
396 arguments expected are: a letter indicating the format in which to
397 display the registers contents. This can be one of: x (hexadecimal), d
398 (decimal), N (natural), t (binary), o (octal), r (raw). After the
399 format argumetn there can be a sequence of numbers, indicating which
400 registers to fetch the content of. If the format is the only argument,
401 a list of all the registers with their values is returned. */
403 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
405 int regnum
, numregs
, format
;
407 struct cleanup
*list_cleanup
, *tuple_cleanup
;
409 /* Note that the test for a valid register must include checking the
410 gdbarch_register_name because gdbarch_num_regs may be allocated for
411 the union of the register sets within a family of related processors.
412 In this case, some entries of gdbarch_register_name will change depending
413 upon the particular processor being debugged. */
415 numregs
= gdbarch_num_regs (current_gdbarch
)
416 + gdbarch_num_pseudo_regs (current_gdbarch
);
419 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
421 format
= (int) argv
[0][0];
423 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
425 if (argc
== 1) /* No args, beside the format: do all the regs. */
431 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
432 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
434 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
435 ui_out_field_int (uiout
, "number", regnum
);
436 get_register (regnum
, format
);
437 do_cleanups (tuple_cleanup
);
441 /* Else, list of register #s, just do listed regs. */
442 for (i
= 1; i
< argc
; i
++)
444 regnum
= atoi (argv
[i
]);
448 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
449 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
451 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
452 ui_out_field_int (uiout
, "number", regnum
);
453 get_register (regnum
, format
);
454 do_cleanups (tuple_cleanup
);
457 error ("bad register number");
459 do_cleanups (list_cleanup
);
462 /* Output one register's contents in the desired format. */
464 get_register (int regnum
, int format
)
466 gdb_byte buffer
[MAX_REGISTER_SIZE
];
471 static struct ui_stream
*stb
= NULL
;
473 stb
= ui_out_stream_new (uiout
);
478 frame_register (get_selected_frame (NULL
), regnum
, &optim
, &lval
, &addr
,
482 error ("Optimized out");
487 char *ptr
, buf
[1024];
491 for (j
= 0; j
< register_size (current_gdbarch
, regnum
); j
++)
493 int idx
= gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
? j
494 : register_size (current_gdbarch
, regnum
) - 1 - j
;
495 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
498 ui_out_field_string (uiout
, "value", buf
);
499 /*fputs_filtered (buf, gdb_stdout); */
503 struct value_print_options opts
;
504 get_user_print_options (&opts
);
506 val_print (register_type (current_gdbarch
, regnum
), buffer
, 0, 0,
507 stb
->stream
, 0, &opts
, current_language
);
508 ui_out_field_stream (uiout
, "value", stb
);
509 ui_out_stream_delete (stb
);
513 /* Write given values into registers. The registers and values are
514 given as pairs. The corresponding MI command is
515 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
517 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
522 /* Note that the test for a valid register must include checking the
523 gdbarch_register_name because gdbarch_num_regs may be allocated for
524 the union of the register sets within a family of related processors.
525 In this case, some entries of gdbarch_register_name will change depending
526 upon the particular processor being debugged. */
528 numregs
= gdbarch_num_regs (current_gdbarch
)
529 + gdbarch_num_pseudo_regs (current_gdbarch
);
532 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
534 format
= (int) argv
[0][0];
536 if (!target_has_registers
)
537 error ("mi_cmd_data_write_register_values: No registers.");
540 error ("mi_cmd_data_write_register_values: No regs and values specified.");
543 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
545 for (i
= 1; i
< argc
; i
= i
+ 2)
547 int regnum
= atoi (argv
[i
]);
549 if (regnum
>= 0 && regnum
< numregs
550 && gdbarch_register_name (current_gdbarch
, regnum
)
551 && *gdbarch_register_name (current_gdbarch
, regnum
))
555 /* Get the value as a number. */
556 value
= parse_and_eval_address (argv
[i
+ 1]);
559 regcache_cooked_write_signed (get_current_regcache (), regnum
, value
);
562 error ("bad register number");
566 /* Evaluate the value of the argument. The argument is an
567 expression. If the expression contains spaces it needs to be
568 included in double quotes. */
570 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
572 struct expression
*expr
;
573 struct cleanup
*old_chain
= NULL
;
575 struct ui_stream
*stb
= NULL
;
576 struct value_print_options opts
;
578 stb
= ui_out_stream_new (uiout
);
582 ui_out_stream_delete (stb
);
583 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
586 expr
= parse_expression (argv
[0]);
588 old_chain
= make_cleanup (free_current_contents
, &expr
);
590 val
= evaluate_expression (expr
);
592 /* Print the result of the expression evaluation. */
593 get_user_print_options (&opts
);
595 val_print (value_type (val
), value_contents (val
),
596 value_embedded_offset (val
), VALUE_ADDRESS (val
),
597 stb
->stream
, 0, &opts
, current_language
);
599 ui_out_field_stream (uiout
, "value", stb
);
600 ui_out_stream_delete (stb
);
602 do_cleanups (old_chain
);
607 ADDR: start address of data to be dumped.
608 WORD-FORMAT: a char indicating format for the ``word''. See
610 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
611 NR_ROW: Number of rows.
612 NR_COL: The number of colums (words per row).
613 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
614 ASCHAR for unprintable characters.
616 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
617 displayes them. Returns:
619 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
622 The number of bytes read is SIZE*ROW*COL. */
625 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
627 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
633 struct type
*word_type
;
646 static struct mi_opt opts
[] =
648 {"o", OFFSET_OPT
, 1},
654 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
658 switch ((enum opt
) opt
)
661 offset
= atol (optarg
);
668 if (argc
< 5 || argc
> 6)
669 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
671 /* Extract all the arguments. */
673 /* Start address of the memory dump. */
674 addr
= parse_and_eval_address (argv
[0]) + offset
;
675 /* The format character to use when displaying a memory word. See
676 the ``x'' command. */
677 word_format
= argv
[1][0];
678 /* The size of the memory word. */
679 word_size
= atol (argv
[2]);
683 word_type
= builtin_type_int8
;
687 word_type
= builtin_type_int16
;
691 word_type
= builtin_type_int32
;
695 word_type
= builtin_type_int64
;
699 word_type
= builtin_type_int8
;
702 /* The number of rows. */
703 nr_rows
= atol (argv
[3]);
705 error ("mi_cmd_data_read_memory: invalid number of rows.");
707 /* Number of bytes per row. */
708 nr_cols
= atol (argv
[4]);
710 error ("mi_cmd_data_read_memory: invalid number of columns.");
712 /* The un-printable character when printing ascii. */
718 /* Create a buffer and read it in. */
719 total_bytes
= word_size
* nr_rows
* nr_cols
;
720 mbuf
= xcalloc (total_bytes
, 1);
721 make_cleanup (xfree
, mbuf
);
723 nr_bytes
= target_read_until_error (¤t_target
, TARGET_OBJECT_MEMORY
,
724 NULL
, mbuf
, addr
, total_bytes
);
726 error ("Unable to read memory.");
728 /* Output the header information. */
729 ui_out_field_core_addr (uiout
, "addr", addr
);
730 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
731 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
732 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
733 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
734 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
735 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
737 /* Build the result as a two dimentional table. */
739 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
740 struct cleanup
*cleanup_list_memory
;
743 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
744 for (row
= 0, row_byte
= 0;
746 row
++, row_byte
+= nr_cols
* word_size
)
750 struct cleanup
*cleanup_tuple
;
751 struct cleanup
*cleanup_list_data
;
752 struct value_print_options opts
;
754 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
755 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
756 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
757 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
758 get_formatted_print_options (&opts
, word_format
);
759 for (col
= 0, col_byte
= row_byte
;
761 col
++, col_byte
+= word_size
)
763 if (col_byte
+ word_size
> nr_bytes
)
765 ui_out_field_string (uiout
, NULL
, "N/A");
769 ui_file_rewind (stream
->stream
);
770 print_scalar_formatted (mbuf
+ col_byte
, word_type
, &opts
,
771 word_asize
, stream
->stream
);
772 ui_out_field_stream (uiout
, NULL
, stream
);
775 do_cleanups (cleanup_list_data
);
779 ui_file_rewind (stream
->stream
);
780 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
782 if (byte
>= nr_bytes
)
784 fputc_unfiltered ('X', stream
->stream
);
786 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
788 fputc_unfiltered (aschar
, stream
->stream
);
791 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
793 ui_out_field_stream (uiout
, "ascii", stream
);
795 do_cleanups (cleanup_tuple
);
797 ui_out_stream_delete (stream
);
798 do_cleanups (cleanup_list_memory
);
800 do_cleanups (cleanups
);
803 /* DATA-MEMORY-WRITE:
805 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
806 offset from the beginning of the memory grid row where the cell to
808 ADDR: start address of the row in the memory grid where the memory
809 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
810 the location to write to.
811 FORMAT: a char indicating format for the ``word''. See
813 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
814 VALUE: value to be written into the memory address.
816 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
820 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
825 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
826 enough when using a compiler other than GCC. */
829 struct cleanup
*old_chain
;
837 static struct mi_opt opts
[] =
839 {"o", OFFSET_OPT
, 1},
845 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
849 switch ((enum opt
) opt
)
852 offset
= atol (optarg
);
860 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
862 /* Extract all the arguments. */
863 /* Start address of the memory dump. */
864 addr
= parse_and_eval_address (argv
[0]);
865 /* The format character to use when displaying a memory word. See
866 the ``x'' command. */
867 word_format
= argv
[1][0];
868 /* The size of the memory word. */
869 word_size
= atol (argv
[2]);
871 /* Calculate the real address of the write destination. */
872 addr
+= (offset
* word_size
);
874 /* Get the value as a number. */
875 value
= parse_and_eval_address (argv
[3]);
876 /* Get the value into an array. */
877 buffer
= xmalloc (word_size
);
878 old_chain
= make_cleanup (xfree
, buffer
);
879 store_signed_integer (buffer
, word_size
, value
);
880 /* Write it down to memory. */
881 write_memory (addr
, buffer
, word_size
);
882 /* Free the buffer. */
883 do_cleanups (old_chain
);
887 mi_cmd_enable_timings (char *command
, char **argv
, int argc
)
893 if (strcmp (argv
[0], "yes") == 0)
895 else if (strcmp (argv
[0], "no") == 0)
906 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command
);
910 mi_cmd_list_features (char *command
, char **argv
, int argc
)
914 struct cleanup
*cleanup
= NULL
;
915 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
917 ui_out_field_string (uiout
, NULL
, "frozen-varobjs");
918 ui_out_field_string (uiout
, NULL
, "pending-breakpoints");
919 ui_out_field_string (uiout
, NULL
, "thread-info");
921 do_cleanups (cleanup
);
925 error ("-list-features should be passed no arguments");
929 mi_cmd_list_target_features (char *command
, char **argv
, int argc
)
933 struct cleanup
*cleanup
= NULL
;
934 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
936 if (target_can_async_p ())
937 ui_out_field_string (uiout
, NULL
, "async");
939 do_cleanups (cleanup
);
943 error ("-list-target-features should be passed no arguments");
946 /* Execute a command within a safe environment.
947 Return <0 for error; >=0 for ok.
949 args->action will tell mi_execute_command what action
950 to perfrom after the given command has executed (display/suppress
951 prompt, display error). */
954 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
956 struct mi_parse
*context
= (struct mi_parse
*) data
;
958 struct mi_timestamp cmd_finished
;
960 running_result_record_printed
= 0;
964 /* A MI command was read from the input stream. */
966 /* FIXME: gdb_???? */
967 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
968 context
->token
, context
->command
, context
->args
);
971 current_command_ts
= context
->cmd_start
;
973 mi_cmd_execute (context
);
976 timestamp (&cmd_finished
);
978 /* Print the result if there were no errors.
980 Remember that on the way out of executing a command, you have
981 to directly use the mi_interp's uiout, since the command could
982 have reset the interpreter, in which case the current uiout
983 will most likely crash in the mi_out_* routines. */
984 if (!running_result_record_printed
)
986 fputs_unfiltered (context
->token
, raw_stdout
);
987 /* There's no particularly good reason why target-connect results
988 in not ^done. Should kill ^connected for MI3. */
989 fputs_unfiltered (strcmp (context
->command
, "target-select") == 0
990 ? "^connected" : "^done", raw_stdout
);
991 mi_out_put (uiout
, raw_stdout
);
992 mi_out_rewind (uiout
);
993 /* Have to check cmd_start, since the command could be
995 if (do_timings
&& context
->cmd_start
)
996 print_diff (context
->cmd_start
, &cmd_finished
);
997 fputs_unfiltered ("\n", raw_stdout
);
1000 /* The command does not want anything to be printed. In that
1001 case, the command probably should not have written anything
1002 to uiout, but in case it has written something, discard it. */
1003 mi_out_rewind (uiout
);
1009 /* A CLI command was read from the input stream. */
1010 /* This "feature" will be removed as soon as we have a
1011 complete set of mi commands. */
1012 /* Echo the command on the console. */
1013 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1014 /* Call the "console" interpreter. */
1015 argv
[0] = "console";
1016 argv
[1] = context
->command
;
1017 mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
1019 /* If we changed interpreters, DON'T print out anything. */
1020 if (current_interp_named_p (INTERP_MI
)
1021 || current_interp_named_p (INTERP_MI1
)
1022 || current_interp_named_p (INTERP_MI2
)
1023 || current_interp_named_p (INTERP_MI3
))
1025 if (!running_result_record_printed
)
1027 fputs_unfiltered (context
->token
, raw_stdout
);
1028 fputs_unfiltered ("^done", raw_stdout
);
1029 mi_out_put (uiout
, raw_stdout
);
1030 mi_out_rewind (uiout
);
1031 fputs_unfiltered ("\n", raw_stdout
);
1034 mi_out_rewind (uiout
);
1046 mi_execute_command (char *cmd
, int from_tty
)
1048 struct mi_parse
*command
;
1049 struct ui_out
*saved_uiout
= uiout
;
1051 /* This is to handle EOF (^D). We just quit gdb. */
1052 /* FIXME: we should call some API function here. */
1054 quit_force (NULL
, from_tty
);
1056 command
= mi_parse (cmd
);
1058 if (command
!= NULL
)
1060 struct gdb_exception result
;
1064 command
->cmd_start
= (struct mi_timestamp
*)
1065 xmalloc (sizeof (struct mi_timestamp
));
1066 timestamp (command
->cmd_start
);
1069 result
= catch_exception (uiout
, captured_mi_execute_command
, command
,
1071 if (result
.reason
< 0)
1073 /* The command execution failed and error() was called
1075 fputs_unfiltered (command
->token
, raw_stdout
);
1076 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1077 if (result
.message
== NULL
)
1078 fputs_unfiltered ("unknown error", raw_stdout
);
1080 fputstr_unfiltered (result
.message
, '"', raw_stdout
);
1081 fputs_unfiltered ("\"\n", raw_stdout
);
1082 mi_out_rewind (uiout
);
1085 mi_parse_free (command
);
1088 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1089 gdb_flush (raw_stdout
);
1090 /* Print any buffered hook code. */
1095 mi_cmd_execute (struct mi_parse
*parse
)
1097 struct cleanup
*cleanup
;
1101 current_token
= xstrdup (parse
->token
);
1102 cleanup
= make_cleanup (free_current_contents
, ¤t_token
);
1104 if (parse
->frame
!= -1 && parse
->thread
== -1)
1105 error (_("Cannot specify --frame without --thread"));
1107 if (parse
->thread
!= -1)
1109 struct thread_info
*tp
= find_thread_id (parse
->thread
);
1111 error (_("Invalid thread id: %d"), parse
->thread
);
1113 if (is_exited (tp
->ptid
))
1114 error (_("Thread id: %d has terminated"), parse
->thread
);
1116 switch_to_thread (tp
->ptid
);
1119 if (parse
->frame
!= -1)
1121 struct frame_info
*fid
;
1122 int frame
= parse
->frame
;
1123 fid
= find_relative_frame (get_current_frame (), &frame
);
1125 /* find_relative_frame was successful */
1128 error (_("Invalid frame id: %d"), frame
);
1131 if (parse
->cmd
->argv_func
!= NULL
)
1133 if (target_can_async_p ()
1134 && target_has_execution
1135 && (is_exited (inferior_ptid
))
1136 && (strcmp (parse
->command
, "thread-info") != 0
1137 && strcmp (parse
->command
, "thread-list-ids") != 0
1138 && strcmp (parse
->command
, "thread-select") != 0))
1140 struct ui_file
*stb
;
1141 stb
= mem_fileopen ();
1143 fputs_unfiltered ("Cannot execute command ", stb
);
1144 fputstr_unfiltered (parse
->command
, '"', stb
);
1145 fputs_unfiltered (" without a selected thread", stb
);
1147 make_cleanup_ui_file_delete (stb
);
1151 parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1153 else if (parse
->cmd
->cli
.cmd
!= 0)
1155 /* FIXME: DELETE THIS. */
1156 /* The operation is still implemented by a cli command. */
1157 /* Must be a synchronous one. */
1158 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1163 /* FIXME: DELETE THIS. */
1164 struct ui_file
*stb
;
1166 stb
= mem_fileopen ();
1168 fputs_unfiltered ("Undefined mi command: ", stb
);
1169 fputstr_unfiltered (parse
->command
, '"', stb
);
1170 fputs_unfiltered (" (missing implementation)", stb
);
1172 make_cleanup_ui_file_delete (stb
);
1175 do_cleanups (cleanup
);
1178 /* FIXME: This is just a hack so we can get some extra commands going.
1179 We don't want to channel things through the CLI, but call libgdb directly.
1180 Use only for synchronous commands. */
1183 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1187 struct cleanup
*old_cleanups
;
1190 run
= xstrprintf ("%s %s", cmd
, args
);
1192 run
= xstrdup (cmd
);
1194 /* FIXME: gdb_???? */
1195 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1197 old_cleanups
= make_cleanup (xfree
, run
);
1198 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1199 do_cleanups (old_cleanups
);
1205 mi_execute_async_cli_command (char *cli_command
, char **argv
, int argc
)
1207 struct cleanup
*old_cleanups
;
1210 if (target_can_async_p ())
1211 run
= xstrprintf ("%s %s&", cli_command
, argc
? *argv
: "");
1213 run
= xstrprintf ("%s %s", cli_command
, argc
? *argv
: "");
1214 old_cleanups
= make_cleanup (xfree
, run
);
1216 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1218 if (target_can_async_p ())
1220 /* If we're not executing, an exception should have been throw. */
1221 gdb_assert (is_running (inferior_ptid
));
1222 do_cleanups (old_cleanups
);
1226 /* Do this before doing any printing. It would appear that some
1227 print code leaves garbage around in the buffer. */
1228 do_cleanups (old_cleanups
);
1230 print_diff_now (current_command_ts
);
1235 mi_load_progress (const char *section_name
,
1236 unsigned long sent_so_far
,
1237 unsigned long total_section
,
1238 unsigned long total_sent
,
1239 unsigned long grand_total
)
1241 struct timeval time_now
, delta
, update_threshold
;
1242 static struct timeval last_update
;
1243 static char *previous_sect_name
= NULL
;
1245 struct ui_out
*saved_uiout
;
1247 /* This function is called through deprecated_show_load_progress
1248 which means uiout may not be correct. Fix it for the duration
1249 of this function. */
1250 saved_uiout
= uiout
;
1252 if (current_interp_named_p (INTERP_MI
)
1253 || current_interp_named_p (INTERP_MI2
))
1254 uiout
= mi_out_new (2);
1255 else if (current_interp_named_p (INTERP_MI1
))
1256 uiout
= mi_out_new (1);
1257 else if (current_interp_named_p (INTERP_MI3
))
1258 uiout
= mi_out_new (3);
1262 update_threshold
.tv_sec
= 0;
1263 update_threshold
.tv_usec
= 500000;
1264 gettimeofday (&time_now
, NULL
);
1266 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1267 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1269 if (delta
.tv_usec
< 0)
1272 delta
.tv_usec
+= 1000000L;
1275 new_section
= (previous_sect_name
?
1276 strcmp (previous_sect_name
, section_name
) : 1);
1279 struct cleanup
*cleanup_tuple
;
1280 xfree (previous_sect_name
);
1281 previous_sect_name
= xstrdup (section_name
);
1284 fputs_unfiltered (current_token
, raw_stdout
);
1285 fputs_unfiltered ("+download", raw_stdout
);
1286 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1287 ui_out_field_string (uiout
, "section", section_name
);
1288 ui_out_field_int (uiout
, "section-size", total_section
);
1289 ui_out_field_int (uiout
, "total-size", grand_total
);
1290 do_cleanups (cleanup_tuple
);
1291 mi_out_put (uiout
, raw_stdout
);
1292 fputs_unfiltered ("\n", raw_stdout
);
1293 gdb_flush (raw_stdout
);
1296 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1297 delta
.tv_usec
>= update_threshold
.tv_usec
)
1299 struct cleanup
*cleanup_tuple
;
1300 last_update
.tv_sec
= time_now
.tv_sec
;
1301 last_update
.tv_usec
= time_now
.tv_usec
;
1303 fputs_unfiltered (current_token
, raw_stdout
);
1304 fputs_unfiltered ("+download", raw_stdout
);
1305 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1306 ui_out_field_string (uiout
, "section", section_name
);
1307 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1308 ui_out_field_int (uiout
, "section-size", total_section
);
1309 ui_out_field_int (uiout
, "total-sent", total_sent
);
1310 ui_out_field_int (uiout
, "total-size", grand_total
);
1311 do_cleanups (cleanup_tuple
);
1312 mi_out_put (uiout
, raw_stdout
);
1313 fputs_unfiltered ("\n", raw_stdout
);
1314 gdb_flush (raw_stdout
);
1318 uiout
= saved_uiout
;
1322 timestamp (struct mi_timestamp
*tv
)
1325 gettimeofday (&tv
->wallclock
, NULL
);
1326 #ifdef HAVE_GETRUSAGE
1327 getrusage (RUSAGE_SELF
, &rusage
);
1328 tv
->utime
.tv_sec
= rusage
.ru_utime
.tv_sec
;
1329 tv
->utime
.tv_usec
= rusage
.ru_utime
.tv_usec
;
1330 tv
->stime
.tv_sec
= rusage
.ru_stime
.tv_sec
;
1331 tv
->stime
.tv_usec
= rusage
.ru_stime
.tv_usec
;
1333 usec
= get_run_time ();
1334 tv
->utime
.tv_sec
= usec
/1000000L;
1335 tv
->utime
.tv_usec
= usec
- 1000000L*tv
->utime
.tv_sec
;
1336 tv
->stime
.tv_sec
= 0;
1337 tv
->stime
.tv_usec
= 0;
1342 print_diff_now (struct mi_timestamp
*start
)
1344 struct mi_timestamp now
;
1346 print_diff (start
, &now
);
1350 timeval_diff (struct timeval start
, struct timeval end
)
1352 return ((end
.tv_sec
- start
.tv_sec
) * 1000000L)
1353 + (end
.tv_usec
- start
.tv_usec
);
1357 print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
)
1361 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1362 timeval_diff (start
->wallclock
, end
->wallclock
) / 1000000.0,
1363 timeval_diff (start
->utime
, end
->utime
) / 1000000.0,
1364 timeval_diff (start
->stime
, end
->stime
) / 1000000.0);