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>
66 struct ui_file
*raw_stdout
;
68 /* This is used to pass the current command timestamp
69 down to continuation routines. */
70 static struct mi_timestamp
*current_command_ts
;
72 static int do_timings
= 0;
75 int running_result_record_printed
= 1;
77 extern void _initialize_mi_main (void);
78 static void mi_cmd_execute (struct mi_parse
*parse
);
80 static void mi_execute_cli_command (const char *cmd
, int args_p
,
82 static void mi_execute_async_cli_command (char *cli_command
,
83 char **argv
, int argc
);
84 static int register_changed_p (int regnum
, struct regcache
*,
86 static void get_register (int regnum
, int format
);
88 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
89 layer that calls libgdb. Any operation used in the below should be
92 static void timestamp (struct mi_timestamp
*tv
);
94 static void print_diff_now (struct mi_timestamp
*start
);
95 static void print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
);
98 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
100 /* We have to print everything right here because we never return. */
102 fputs_unfiltered (current_token
, raw_stdout
);
103 fputs_unfiltered ("^exit\n", raw_stdout
);
104 mi_out_put (uiout
, raw_stdout
);
105 /* FIXME: The function called is not yet a formal libgdb function. */
106 quit_force (NULL
, FROM_TTY
);
110 mi_cmd_exec_next (char *command
, char **argv
, int argc
)
112 /* FIXME: Should call a libgdb function, not a cli wrapper. */
113 return mi_execute_async_cli_command ("next", argv
, argc
);
117 mi_cmd_exec_next_instruction (char *command
, char **argv
, int argc
)
119 /* FIXME: Should call a libgdb function, not a cli wrapper. */
120 return mi_execute_async_cli_command ("nexti", argv
, argc
);
124 mi_cmd_exec_step (char *command
, char **argv
, int argc
)
126 /* FIXME: Should call a libgdb function, not a cli wrapper. */
127 return mi_execute_async_cli_command ("step", argv
, argc
);
131 mi_cmd_exec_step_instruction (char *command
, char **argv
, int argc
)
133 /* FIXME: Should call a libgdb function, not a cli wrapper. */
134 return mi_execute_async_cli_command ("stepi", argv
, argc
);
138 mi_cmd_exec_finish (char *command
, char **argv
, int argc
)
140 /* FIXME: Should call a libgdb function, not a cli wrapper. */
141 return mi_execute_async_cli_command ("finish", argv
, argc
);
145 mi_cmd_exec_return (char *command
, char **argv
, int argc
)
147 /* This command doesn't really execute the target, it just pops the
148 specified number of frames. */
150 /* Call return_command with from_tty argument equal to 0 so as to
151 avoid being queried. */
152 return_command (*argv
, 0);
154 /* Call return_command with from_tty argument equal to 0 so as to
155 avoid being queried. */
156 return_command (NULL
, 0);
158 /* Because we have called return_command with from_tty = 0, we need
159 to print the frame here. */
160 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
);
164 mi_cmd_exec_continue (char *command
, char **argv
, int argc
)
166 /* FIXME: Should call a libgdb function, not a cli wrapper. */
167 return mi_execute_async_cli_command ("continue", argv
, argc
);
170 /* Interrupt the execution of the target. Note how we must play around
171 with the token variables, in order to display the current token in
172 the result of the interrupt command, and the previous execution
173 token when the target finally stops. See comments in
176 mi_cmd_exec_interrupt (char *command
, char **argv
, int argc
)
178 if (!is_running (inferior_ptid
))
179 error ("mi_cmd_exec_interrupt: Inferior not running.");
181 interrupt_target_command (NULL
, 0);
185 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
188 char *mi_error_message
;
191 error ("mi_cmd_thread_select: USAGE: threadnum.");
193 rc
= gdb_thread_select (uiout
, argv
[0], &mi_error_message
);
195 if (rc
== GDB_RC_FAIL
)
197 make_cleanup (xfree
, mi_error_message
);
198 error ("%s", mi_error_message
);
203 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
206 char *mi_error_message
;
209 error ("mi_cmd_thread_list_ids: No arguments required.");
211 rc
= gdb_list_thread_ids (uiout
, &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_info (char *command
, char **argv
, int argc
)
225 if (argc
!= 0 && argc
!= 1)
226 error ("Invalid MI command");
229 thread
= atoi (argv
[0]);
231 print_thread_info (uiout
, thread
);
235 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
239 struct cleanup
*cleanup
;
241 /* Note that the test for a valid register must include checking the
242 gdbarch_register_name because gdbarch_num_regs may be allocated for
243 the union of the register sets within a family of related processors.
244 In this case, some entries of gdbarch_register_name will change depending
245 upon the particular processor being debugged. */
247 numregs
= gdbarch_num_regs (current_gdbarch
)
248 + gdbarch_num_pseudo_regs (current_gdbarch
);
250 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
252 if (argc
== 0) /* No args, just do all the regs. */
258 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
259 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
260 ui_out_field_string (uiout
, NULL
, "");
262 ui_out_field_string (uiout
, NULL
,
263 gdbarch_register_name
264 (current_gdbarch
, regnum
));
268 /* Else, list of register #s, just do listed regs. */
269 for (i
= 0; i
< argc
; i
++)
271 regnum
= atoi (argv
[i
]);
272 if (regnum
< 0 || regnum
>= numregs
)
273 error ("bad register number");
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 (current_gdbarch
, regnum
));
282 do_cleanups (cleanup
);
286 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
288 static struct regcache
*this_regs
= NULL
;
289 struct regcache
*prev_regs
;
290 int regnum
, numregs
, changed
;
292 struct cleanup
*cleanup
;
294 /* The last time we visited this function, the current frame's register
295 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
296 and refresh THIS_REGS with the now-current register contents. */
298 prev_regs
= this_regs
;
299 this_regs
= frame_save_as_regcache (get_selected_frame (NULL
));
300 cleanup
= make_cleanup_regcache_xfree (prev_regs
);
302 /* Note that the test for a valid register must include checking the
303 gdbarch_register_name because gdbarch_num_regs may be allocated for
304 the union of the register sets within a family of related processors.
305 In this case, some entries of gdbarch_register_name will change depending
306 upon the particular processor being debugged. */
308 numregs
= gdbarch_num_regs (current_gdbarch
)
309 + gdbarch_num_pseudo_regs (current_gdbarch
);
311 make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
313 if (argc
== 0) /* No args, just do all the regs. */
319 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
320 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
322 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
324 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
326 ui_out_field_int (uiout
, NULL
, regnum
);
330 /* Else, list of register #s, just do listed regs. */
331 for (i
= 0; i
< argc
; i
++)
333 regnum
= atoi (argv
[i
]);
337 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
338 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
340 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
342 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
344 ui_out_field_int (uiout
, NULL
, regnum
);
347 error ("bad register number");
349 do_cleanups (cleanup
);
353 register_changed_p (int regnum
, struct regcache
*prev_regs
,
354 struct regcache
*this_regs
)
356 struct gdbarch
*gdbarch
= get_regcache_arch (this_regs
);
357 gdb_byte prev_buffer
[MAX_REGISTER_SIZE
];
358 gdb_byte this_buffer
[MAX_REGISTER_SIZE
];
360 /* Registers not valid in this frame return count as unchanged. */
361 if (!regcache_valid_p (this_regs
, regnum
))
364 /* First time through or after gdbarch change consider all registers as
365 changed. Same for registers not valid in the previous frame. */
366 if (!prev_regs
|| get_regcache_arch (prev_regs
) != gdbarch
367 || !regcache_valid_p (prev_regs
, regnum
))
370 /* Get register contents and compare. */
371 regcache_cooked_read (prev_regs
, regnum
, prev_buffer
);
372 regcache_cooked_read (this_regs
, regnum
, this_buffer
);
374 return memcmp (prev_buffer
, this_buffer
,
375 register_size (gdbarch
, regnum
)) != 0;
378 /* Return a list of register number and value pairs. The valid
379 arguments expected are: a letter indicating the format in which to
380 display the registers contents. This can be one of: x (hexadecimal), d
381 (decimal), N (natural), t (binary), o (octal), r (raw). After the
382 format argumetn there can be a sequence of numbers, indicating which
383 registers to fetch the content of. If the format is the only argument,
384 a list of all the registers with their values is returned. */
386 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
388 int regnum
, numregs
, format
;
390 struct cleanup
*list_cleanup
, *tuple_cleanup
;
392 /* Note that the test for a valid register must include checking the
393 gdbarch_register_name because gdbarch_num_regs may be allocated for
394 the union of the register sets within a family of related processors.
395 In this case, some entries of gdbarch_register_name will change depending
396 upon the particular processor being debugged. */
398 numregs
= gdbarch_num_regs (current_gdbarch
)
399 + gdbarch_num_pseudo_regs (current_gdbarch
);
402 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
404 format
= (int) argv
[0][0];
406 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
408 if (argc
== 1) /* No args, beside the format: do all the regs. */
414 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
415 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
417 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
418 ui_out_field_int (uiout
, "number", regnum
);
419 get_register (regnum
, format
);
420 do_cleanups (tuple_cleanup
);
424 /* Else, list of register #s, just do listed regs. */
425 for (i
= 1; i
< argc
; i
++)
427 regnum
= atoi (argv
[i
]);
431 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
432 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
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
);
440 error ("bad register number");
442 do_cleanups (list_cleanup
);
445 /* Output one register's contents in the desired format. */
447 get_register (int regnum
, int format
)
449 gdb_byte buffer
[MAX_REGISTER_SIZE
];
454 static struct ui_stream
*stb
= NULL
;
456 stb
= ui_out_stream_new (uiout
);
461 frame_register (get_selected_frame (NULL
), regnum
, &optim
, &lval
, &addr
,
465 error ("Optimized out");
470 char *ptr
, buf
[1024];
474 for (j
= 0; j
< register_size (current_gdbarch
, regnum
); j
++)
476 int idx
= gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
? j
477 : register_size (current_gdbarch
, regnum
) - 1 - j
;
478 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
481 ui_out_field_string (uiout
, "value", buf
);
482 /*fputs_filtered (buf, gdb_stdout); */
486 val_print (register_type (current_gdbarch
, regnum
), buffer
, 0, 0,
487 stb
->stream
, format
, 1, 0, Val_pretty_default
,
489 ui_out_field_stream (uiout
, "value", stb
);
490 ui_out_stream_delete (stb
);
494 /* Write given values into registers. The registers and values are
495 given as pairs. The corresponding MI command is
496 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
498 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
503 /* Note that the test for a valid register must include checking the
504 gdbarch_register_name because gdbarch_num_regs may be allocated for
505 the union of the register sets within a family of related processors.
506 In this case, some entries of gdbarch_register_name will change depending
507 upon the particular processor being debugged. */
509 numregs
= gdbarch_num_regs (current_gdbarch
)
510 + gdbarch_num_pseudo_regs (current_gdbarch
);
513 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
515 format
= (int) argv
[0][0];
517 if (!target_has_registers
)
518 error ("mi_cmd_data_write_register_values: No registers.");
521 error ("mi_cmd_data_write_register_values: No regs and values specified.");
524 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
526 for (i
= 1; i
< argc
; i
= i
+ 2)
528 int regnum
= atoi (argv
[i
]);
530 if (regnum
>= 0 && regnum
< numregs
531 && gdbarch_register_name (current_gdbarch
, regnum
)
532 && *gdbarch_register_name (current_gdbarch
, regnum
))
536 /* Get the value as a number. */
537 value
= parse_and_eval_address (argv
[i
+ 1]);
540 regcache_cooked_write_signed (get_current_regcache (), regnum
, value
);
543 error ("bad register number");
547 /* Evaluate the value of the argument. The argument is an
548 expression. If the expression contains spaces it needs to be
549 included in double quotes. */
551 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
553 struct expression
*expr
;
554 struct cleanup
*old_chain
= NULL
;
556 struct ui_stream
*stb
= NULL
;
558 stb
= ui_out_stream_new (uiout
);
562 ui_out_stream_delete (stb
);
563 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
566 expr
= parse_expression (argv
[0]);
568 old_chain
= make_cleanup (free_current_contents
, &expr
);
570 val
= evaluate_expression (expr
);
572 /* Print the result of the expression evaluation. */
573 val_print (value_type (val
), value_contents (val
),
574 value_embedded_offset (val
), VALUE_ADDRESS (val
),
575 stb
->stream
, 0, 0, 0, 0, current_language
);
577 ui_out_field_stream (uiout
, "value", stb
);
578 ui_out_stream_delete (stb
);
580 do_cleanups (old_chain
);
585 ADDR: start address of data to be dumped.
586 WORD-FORMAT: a char indicating format for the ``word''. See
588 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
589 NR_ROW: Number of rows.
590 NR_COL: The number of colums (words per row).
591 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
592 ASCHAR for unprintable characters.
594 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
595 displayes them. Returns:
597 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
600 The number of bytes read is SIZE*ROW*COL. */
603 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
605 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
611 struct type
*word_type
;
624 static struct mi_opt opts
[] =
626 {"o", OFFSET_OPT
, 1},
632 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
636 switch ((enum opt
) opt
)
639 offset
= atol (optarg
);
646 if (argc
< 5 || argc
> 6)
647 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
649 /* Extract all the arguments. */
651 /* Start address of the memory dump. */
652 addr
= parse_and_eval_address (argv
[0]) + offset
;
653 /* The format character to use when displaying a memory word. See
654 the ``x'' command. */
655 word_format
= argv
[1][0];
656 /* The size of the memory word. */
657 word_size
= atol (argv
[2]);
661 word_type
= builtin_type_int8
;
665 word_type
= builtin_type_int16
;
669 word_type
= builtin_type_int32
;
673 word_type
= builtin_type_int64
;
677 word_type
= builtin_type_int8
;
680 /* The number of rows. */
681 nr_rows
= atol (argv
[3]);
683 error ("mi_cmd_data_read_memory: invalid number of rows.");
685 /* Number of bytes per row. */
686 nr_cols
= atol (argv
[4]);
688 error ("mi_cmd_data_read_memory: invalid number of columns.");
690 /* The un-printable character when printing ascii. */
696 /* Create a buffer and read it in. */
697 total_bytes
= word_size
* nr_rows
* nr_cols
;
698 mbuf
= xcalloc (total_bytes
, 1);
699 make_cleanup (xfree
, mbuf
);
701 nr_bytes
= target_read_until_error (¤t_target
, TARGET_OBJECT_MEMORY
,
702 NULL
, mbuf
, addr
, total_bytes
);
704 error ("Unable to read memory.");
706 /* Output the header information. */
707 ui_out_field_core_addr (uiout
, "addr", addr
);
708 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
709 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
710 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
711 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
712 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
713 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
715 /* Build the result as a two dimentional table. */
717 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
718 struct cleanup
*cleanup_list_memory
;
721 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
722 for (row
= 0, row_byte
= 0;
724 row
++, row_byte
+= nr_cols
* word_size
)
728 struct cleanup
*cleanup_tuple
;
729 struct cleanup
*cleanup_list_data
;
730 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
731 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
732 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
733 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
734 for (col
= 0, col_byte
= row_byte
;
736 col
++, col_byte
+= word_size
)
738 if (col_byte
+ word_size
> nr_bytes
)
740 ui_out_field_string (uiout
, NULL
, "N/A");
744 ui_file_rewind (stream
->stream
);
745 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
746 word_asize
, stream
->stream
);
747 ui_out_field_stream (uiout
, NULL
, stream
);
750 do_cleanups (cleanup_list_data
);
754 ui_file_rewind (stream
->stream
);
755 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
757 if (byte
>= nr_bytes
)
759 fputc_unfiltered ('X', stream
->stream
);
761 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
763 fputc_unfiltered (aschar
, stream
->stream
);
766 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
768 ui_out_field_stream (uiout
, "ascii", stream
);
770 do_cleanups (cleanup_tuple
);
772 ui_out_stream_delete (stream
);
773 do_cleanups (cleanup_list_memory
);
775 do_cleanups (cleanups
);
778 /* DATA-MEMORY-WRITE:
780 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
781 offset from the beginning of the memory grid row where the cell to
783 ADDR: start address of the row in the memory grid where the memory
784 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
785 the location to write to.
786 FORMAT: a char indicating format for the ``word''. See
788 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
789 VALUE: value to be written into the memory address.
791 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
795 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
800 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
801 enough when using a compiler other than GCC. */
804 struct cleanup
*old_chain
;
812 static struct mi_opt opts
[] =
814 {"o", OFFSET_OPT
, 1},
820 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
824 switch ((enum opt
) opt
)
827 offset
= atol (optarg
);
835 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
837 /* Extract all the arguments. */
838 /* Start address of the memory dump. */
839 addr
= parse_and_eval_address (argv
[0]);
840 /* The format character to use when displaying a memory word. See
841 the ``x'' command. */
842 word_format
= argv
[1][0];
843 /* The size of the memory word. */
844 word_size
= atol (argv
[2]);
846 /* Calculate the real address of the write destination. */
847 addr
+= (offset
* word_size
);
849 /* Get the value as a number. */
850 value
= parse_and_eval_address (argv
[3]);
851 /* Get the value into an array. */
852 buffer
= xmalloc (word_size
);
853 old_chain
= make_cleanup (xfree
, buffer
);
854 store_signed_integer (buffer
, word_size
, value
);
855 /* Write it down to memory. */
856 write_memory (addr
, buffer
, word_size
);
857 /* Free the buffer. */
858 do_cleanups (old_chain
);
862 mi_cmd_enable_timings (char *command
, char **argv
, int argc
)
868 if (strcmp (argv
[0], "yes") == 0)
870 else if (strcmp (argv
[0], "no") == 0)
881 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command
);
885 mi_cmd_list_features (char *command
, char **argv
, int argc
)
889 struct cleanup
*cleanup
= NULL
;
890 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
892 ui_out_field_string (uiout
, NULL
, "frozen-varobjs");
893 ui_out_field_string (uiout
, NULL
, "pending-breakpoints");
894 ui_out_field_string (uiout
, NULL
, "thread-info");
896 do_cleanups (cleanup
);
900 error ("-list-features should be passed no arguments");
903 /* Execute a command within a safe environment.
904 Return <0 for error; >=0 for ok.
906 args->action will tell mi_execute_command what action
907 to perfrom after the given command has executed (display/suppress
908 prompt, display error). */
911 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
913 struct mi_parse
*context
= (struct mi_parse
*) data
;
915 struct mi_timestamp cmd_finished
;
917 running_result_record_printed
= 0;
921 /* A MI command was read from the input stream. */
923 /* FIXME: gdb_???? */
924 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
925 context
->token
, context
->command
, context
->args
);
928 current_command_ts
= context
->cmd_start
;
930 mi_cmd_execute (context
);
933 timestamp (&cmd_finished
);
935 /* Print the result if there were no errors.
937 Remember that on the way out of executing a command, you have
938 to directly use the mi_interp's uiout, since the command could
939 have reset the interpreter, in which case the current uiout
940 will most likely crash in the mi_out_* routines. */
941 if (!running_result_record_printed
)
943 fputs_unfiltered (context
->token
, raw_stdout
);
944 /* There's no particularly good reason why target-connect results
945 in not ^done. Should kill ^connected for MI3. */
946 fputs_unfiltered (strcmp (context
->command
, "target-select") == 0
947 ? "^connected" : "^done", raw_stdout
);
948 mi_out_put (uiout
, raw_stdout
);
949 mi_out_rewind (uiout
);
950 /* Have to check cmd_start, since the command could be
952 if (do_timings
&& context
->cmd_start
)
953 print_diff (context
->cmd_start
, &cmd_finished
);
954 fputs_unfiltered ("\n", raw_stdout
);
957 /* The command does not want anything to be printed. In that
958 case, the command probably should not have written anything
959 to uiout, but in case it has written something, discard it. */
960 mi_out_rewind (uiout
);
966 /* A CLI command was read from the input stream. */
967 /* This "feature" will be removed as soon as we have a
968 complete set of mi commands. */
969 /* Echo the command on the console. */
970 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
971 /* Call the "console" interpreter. */
973 argv
[1] = context
->command
;
974 mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
976 /* If we changed interpreters, DON'T print out anything. */
977 if (current_interp_named_p (INTERP_MI
)
978 || current_interp_named_p (INTERP_MI1
)
979 || current_interp_named_p (INTERP_MI2
)
980 || current_interp_named_p (INTERP_MI3
))
982 if (!running_result_record_printed
)
984 fputs_unfiltered (context
->token
, raw_stdout
);
985 fputs_unfiltered ("^done", raw_stdout
);
986 mi_out_put (uiout
, raw_stdout
);
987 mi_out_rewind (uiout
);
988 fputs_unfiltered ("\n", raw_stdout
);
991 mi_out_rewind (uiout
);
1003 mi_execute_command (char *cmd
, int from_tty
)
1005 struct mi_parse
*command
;
1006 struct ui_out
*saved_uiout
= uiout
;
1008 /* This is to handle EOF (^D). We just quit gdb. */
1009 /* FIXME: we should call some API function here. */
1011 quit_force (NULL
, from_tty
);
1013 command
= mi_parse (cmd
);
1015 if (command
!= NULL
)
1017 struct gdb_exception result
;
1021 command
->cmd_start
= (struct mi_timestamp
*)
1022 xmalloc (sizeof (struct mi_timestamp
));
1023 timestamp (command
->cmd_start
);
1026 result
= catch_exception (uiout
, captured_mi_execute_command
, command
,
1028 if (result
.reason
< 0)
1030 /* The command execution failed and error() was called
1032 fputs_unfiltered (command
->token
, raw_stdout
);
1033 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1034 if (result
.message
== NULL
)
1035 fputs_unfiltered ("unknown error", raw_stdout
);
1037 fputstr_unfiltered (result
.message
, '"', raw_stdout
);
1038 fputs_unfiltered ("\"\n", raw_stdout
);
1039 mi_out_rewind (uiout
);
1042 mi_parse_free (command
);
1045 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1046 gdb_flush (raw_stdout
);
1047 /* Print any buffered hook code. */
1052 mi_cmd_execute (struct mi_parse
*parse
)
1054 struct cleanup
*cleanup
;
1057 current_token
= xstrdup (parse
->token
);
1058 cleanup
= make_cleanup (free_current_contents
, ¤t_token
);
1060 if (parse
->cmd
->argv_func
!= NULL
)
1062 if (target_can_async_p ()
1063 && target_has_execution
1064 && (is_exited (inferior_ptid
))
1065 && (strcmp (parse
->command
, "thread-info") != 0
1066 && strcmp (parse
->command
, "thread-list-ids") != 0
1067 && strcmp (parse
->command
, "thread-select") != 0))
1069 struct ui_file
*stb
;
1070 stb
= mem_fileopen ();
1072 fputs_unfiltered ("Cannot execute command ", stb
);
1073 fputstr_unfiltered (parse
->command
, '"', stb
);
1074 fputs_unfiltered (" without a selected thread", stb
);
1076 make_cleanup_ui_file_delete (stb
);
1080 if ((!non_stop
&& any_running ())
1081 || (non_stop
&& is_running (inferior_ptid
)))
1083 if (strcmp (parse
->command
, "exec-interrupt"))
1085 struct ui_file
*stb
;
1086 stb
= mem_fileopen ();
1088 fputs_unfiltered ("Cannot execute command ", stb
);
1089 fputstr_unfiltered (parse
->command
, '"', stb
);
1090 fputs_unfiltered (" while target running", stb
);
1092 make_cleanup_ui_file_delete (stb
);
1096 parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1098 else if (parse
->cmd
->cli
.cmd
!= 0)
1100 /* FIXME: DELETE THIS. */
1101 /* The operation is still implemented by a cli command. */
1102 /* Must be a synchronous one. */
1103 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1108 /* FIXME: DELETE THIS. */
1109 struct ui_file
*stb
;
1111 stb
= mem_fileopen ();
1113 fputs_unfiltered ("Undefined mi command: ", stb
);
1114 fputstr_unfiltered (parse
->command
, '"', stb
);
1115 fputs_unfiltered (" (missing implementation)", stb
);
1117 make_cleanup_ui_file_delete (stb
);
1120 do_cleanups (cleanup
);
1123 /* FIXME: This is just a hack so we can get some extra commands going.
1124 We don't want to channel things through the CLI, but call libgdb directly.
1125 Use only for synchronous commands. */
1128 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1132 struct cleanup
*old_cleanups
;
1135 run
= xstrprintf ("%s %s", cmd
, args
);
1137 run
= xstrdup (cmd
);
1139 /* FIXME: gdb_???? */
1140 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1142 old_cleanups
= make_cleanup (xfree
, run
);
1143 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1144 do_cleanups (old_cleanups
);
1150 mi_execute_async_cli_command (char *cli_command
, char **argv
, int argc
)
1152 struct cleanup
*old_cleanups
;
1155 if (target_can_async_p ())
1156 run
= xstrprintf ("%s %s&", cli_command
, argc
? *argv
: "");
1158 run
= xstrprintf ("%s %s", cli_command
, argc
? *argv
: "");
1159 old_cleanups
= make_cleanup (xfree
, run
);
1161 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1163 if (target_can_async_p ())
1165 /* If we're not executing, an exception should have been throw. */
1166 gdb_assert (is_running (inferior_ptid
));
1167 do_cleanups (old_cleanups
);
1171 /* Do this before doing any printing. It would appear that some
1172 print code leaves garbage around in the buffer. */
1173 do_cleanups (old_cleanups
);
1175 print_diff_now (current_command_ts
);
1180 mi_load_progress (const char *section_name
,
1181 unsigned long sent_so_far
,
1182 unsigned long total_section
,
1183 unsigned long total_sent
,
1184 unsigned long grand_total
)
1186 struct timeval time_now
, delta
, update_threshold
;
1187 static struct timeval last_update
;
1188 static char *previous_sect_name
= NULL
;
1190 struct ui_out
*saved_uiout
;
1192 /* This function is called through deprecated_show_load_progress
1193 which means uiout may not be correct. Fix it for the duration
1194 of this function. */
1195 saved_uiout
= uiout
;
1197 if (current_interp_named_p (INTERP_MI
)
1198 || current_interp_named_p (INTERP_MI2
))
1199 uiout
= mi_out_new (2);
1200 else if (current_interp_named_p (INTERP_MI1
))
1201 uiout
= mi_out_new (1);
1202 else if (current_interp_named_p (INTERP_MI3
))
1203 uiout
= mi_out_new (3);
1207 update_threshold
.tv_sec
= 0;
1208 update_threshold
.tv_usec
= 500000;
1209 gettimeofday (&time_now
, NULL
);
1211 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1212 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1214 if (delta
.tv_usec
< 0)
1217 delta
.tv_usec
+= 1000000L;
1220 new_section
= (previous_sect_name
?
1221 strcmp (previous_sect_name
, section_name
) : 1);
1224 struct cleanup
*cleanup_tuple
;
1225 xfree (previous_sect_name
);
1226 previous_sect_name
= xstrdup (section_name
);
1229 fputs_unfiltered (current_token
, raw_stdout
);
1230 fputs_unfiltered ("+download", raw_stdout
);
1231 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1232 ui_out_field_string (uiout
, "section", section_name
);
1233 ui_out_field_int (uiout
, "section-size", total_section
);
1234 ui_out_field_int (uiout
, "total-size", grand_total
);
1235 do_cleanups (cleanup_tuple
);
1236 mi_out_put (uiout
, raw_stdout
);
1237 fputs_unfiltered ("\n", raw_stdout
);
1238 gdb_flush (raw_stdout
);
1241 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1242 delta
.tv_usec
>= update_threshold
.tv_usec
)
1244 struct cleanup
*cleanup_tuple
;
1245 last_update
.tv_sec
= time_now
.tv_sec
;
1246 last_update
.tv_usec
= time_now
.tv_usec
;
1248 fputs_unfiltered (current_token
, raw_stdout
);
1249 fputs_unfiltered ("+download", raw_stdout
);
1250 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1251 ui_out_field_string (uiout
, "section", section_name
);
1252 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1253 ui_out_field_int (uiout
, "section-size", total_section
);
1254 ui_out_field_int (uiout
, "total-sent", total_sent
);
1255 ui_out_field_int (uiout
, "total-size", grand_total
);
1256 do_cleanups (cleanup_tuple
);
1257 mi_out_put (uiout
, raw_stdout
);
1258 fputs_unfiltered ("\n", raw_stdout
);
1259 gdb_flush (raw_stdout
);
1263 uiout
= saved_uiout
;
1267 timestamp (struct mi_timestamp
*tv
)
1270 gettimeofday (&tv
->wallclock
, NULL
);
1271 #ifdef HAVE_GETRUSAGE
1272 getrusage (RUSAGE_SELF
, &rusage
);
1273 tv
->utime
.tv_sec
= rusage
.ru_utime
.tv_sec
;
1274 tv
->utime
.tv_usec
= rusage
.ru_utime
.tv_usec
;
1275 tv
->stime
.tv_sec
= rusage
.ru_stime
.tv_sec
;
1276 tv
->stime
.tv_usec
= rusage
.ru_stime
.tv_usec
;
1278 usec
= get_run_time ();
1279 tv
->utime
.tv_sec
= usec
/1000000L;
1280 tv
->utime
.tv_usec
= usec
- 1000000L*tv
->utime
.tv_sec
;
1281 tv
->stime
.tv_sec
= 0;
1282 tv
->stime
.tv_usec
= 0;
1287 print_diff_now (struct mi_timestamp
*start
)
1289 struct mi_timestamp now
;
1291 print_diff (start
, &now
);
1295 timeval_diff (struct timeval start
, struct timeval end
)
1297 return ((end
.tv_sec
- start
.tv_sec
) * 1000000L)
1298 + (end
.tv_usec
- start
.tv_usec
);
1302 print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
)
1306 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1307 timeval_diff (start
->wallclock
, end
->wallclock
) / 1000000.0,
1308 timeval_diff (start
->utime
, end
->utime
) / 1000000.0,
1309 timeval_diff (start
->stime
, end
->stime
) / 1000000.0);