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(). */
51 #if defined HAVE_SYS_RESOURCE_H
52 #include <sys/resource.h>
64 /* Enumerations of the actions that may result from calling
65 captured_mi_execute_command. */
67 enum captured_mi_execute_command_actions
69 EXECUTE_COMMAND_DISPLAY_PROMPT
,
70 EXECUTE_COMMAND_SUPPRESS_PROMPT
73 /* This structure is used to pass information from captured_mi_execute_command
74 to mi_execute_command. */
75 struct captured_mi_execute_command_args
77 /* This return result of the MI command (output). */
78 enum mi_cmd_result rc
;
80 /* What action to perform when the call is finished (output). */
81 enum captured_mi_execute_command_actions action
;
83 /* The command context to be executed (input). */
84 struct mi_parse
*command
;
88 struct ui_file
*raw_stdout
;
90 /* This is used to pass the current command timestamp
91 down to continuation routines. */
92 static struct mi_timestamp
*current_command_ts
;
94 static int do_timings
= 0;
96 static char *current_token
;
98 extern void _initialize_mi_main (void);
99 static enum mi_cmd_result
mi_cmd_execute (struct mi_parse
*parse
);
101 static void mi_execute_cli_command (const char *cmd
, int args_p
,
103 static enum mi_cmd_result
mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
);
105 static void mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
,
108 static int register_changed_p (int regnum
, struct regcache
*,
110 static void get_register (int regnum
, int format
);
112 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
113 layer that calls libgdb. Any operation used in the below should be
116 static void timestamp (struct mi_timestamp
*tv
);
118 static void print_diff_now (struct mi_timestamp
*start
);
119 static void print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
);
122 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
124 /* We have to print everything right here because we never return. */
126 fputs_unfiltered (current_token
, raw_stdout
);
127 fputs_unfiltered ("^exit\n", raw_stdout
);
128 mi_out_put (uiout
, raw_stdout
);
129 /* FIXME: The function called is not yet a formal libgdb function. */
130 quit_force (NULL
, FROM_TTY
);
135 mi_cmd_exec_run (char *args
, int from_tty
)
137 /* FIXME: Should call a libgdb function, not a cli wrapper. */
138 return mi_execute_async_cli_command ("run", args
, from_tty
);
142 mi_cmd_exec_next (char *args
, int from_tty
)
144 /* FIXME: Should call a libgdb function, not a cli wrapper. */
145 return mi_execute_async_cli_command ("next", args
, from_tty
);
149 mi_cmd_exec_next_instruction (char *args
, int from_tty
)
151 /* FIXME: Should call a libgdb function, not a cli wrapper. */
152 return mi_execute_async_cli_command ("nexti", args
, from_tty
);
156 mi_cmd_exec_step (char *args
, int from_tty
)
158 /* FIXME: Should call a libgdb function, not a cli wrapper. */
159 return mi_execute_async_cli_command ("step", args
, from_tty
);
163 mi_cmd_exec_step_instruction (char *args
, int from_tty
)
165 /* FIXME: Should call a libgdb function, not a cli wrapper. */
166 return mi_execute_async_cli_command ("stepi", args
, from_tty
);
170 mi_cmd_exec_finish (char *args
, int from_tty
)
172 /* FIXME: Should call a libgdb function, not a cli wrapper. */
173 return mi_execute_async_cli_command ("finish", args
, from_tty
);
177 mi_cmd_exec_until (char *args
, int from_tty
)
179 /* FIXME: Should call a libgdb function, not a cli wrapper. */
180 return mi_execute_async_cli_command ("until", args
, from_tty
);
184 mi_cmd_exec_return (char *args
, int from_tty
)
186 /* This command doesn't really execute the target, it just pops the
187 specified number of frames. */
189 /* Call return_command with from_tty argument equal to 0 so as to
190 avoid being queried. */
191 return_command (args
, 0);
193 /* Call return_command with from_tty argument equal to 0 so as to
194 avoid being queried. */
195 return_command (NULL
, 0);
197 /* Because we have called return_command with from_tty = 0, we need
198 to print the frame here. */
199 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
);
205 mi_cmd_exec_continue (char *args
, int from_tty
)
207 /* FIXME: Should call a libgdb function, not a cli wrapper. */
208 return mi_execute_async_cli_command ("continue", args
, from_tty
);
211 /* Interrupt the execution of the target. Note how we must play around
212 with the token variables, in order to display the current token in
213 the result of the interrupt command, and the previous execution
214 token when the target finally stops. See comments in
217 mi_cmd_exec_interrupt (char *args
, int from_tty
)
219 if (!target_executing
)
220 error ("mi_cmd_exec_interrupt: Inferior not executing.");
222 interrupt_target_command (args
, from_tty
);
224 fputs_unfiltered (current_token
, raw_stdout
);
225 fputs_unfiltered ("^done", raw_stdout
);
226 mi_out_put (uiout
, raw_stdout
);
227 mi_out_rewind (uiout
);
228 fputs_unfiltered ("\n", raw_stdout
);
233 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
236 char *mi_error_message
;
239 error ("mi_cmd_thread_select: USAGE: threadnum.");
241 rc
= gdb_thread_select (uiout
, argv
[0], &mi_error_message
);
243 if (rc
== GDB_RC_FAIL
)
245 make_cleanup (xfree
, mi_error_message
);
246 error ("%s", mi_error_message
);
253 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
256 char *mi_error_message
;
259 error ("mi_cmd_thread_list_ids: No arguments required.");
261 rc
= gdb_list_thread_ids (uiout
, &mi_error_message
);
263 if (rc
== GDB_RC_FAIL
)
265 make_cleanup (xfree
, mi_error_message
);
266 error ("%s", mi_error_message
);
273 mi_cmd_thread_info (char *command
, char **argv
, int argc
)
277 if (argc
!= 0 && argc
!= 1)
278 error ("Invalid MI command");
281 thread
= atoi (argv
[0]);
283 print_thread_info (uiout
, thread
);
288 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
292 struct cleanup
*cleanup
;
294 /* Note that the test for a valid register must include checking the
295 gdbarch_register_name because gdbarch_num_regs may be allocated for
296 the union of the register sets within a family of related processors.
297 In this case, some entries of gdbarch_register_name will change depending
298 upon the particular processor being debugged. */
300 numregs
= gdbarch_num_regs (current_gdbarch
)
301 + gdbarch_num_pseudo_regs (current_gdbarch
);
303 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
305 if (argc
== 0) /* No args, just do all the regs. */
311 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
312 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
313 ui_out_field_string (uiout
, NULL
, "");
315 ui_out_field_string (uiout
, NULL
,
316 gdbarch_register_name
317 (current_gdbarch
, regnum
));
321 /* Else, list of register #s, just do listed regs. */
322 for (i
= 0; i
< argc
; i
++)
324 regnum
= atoi (argv
[i
]);
325 if (regnum
< 0 || regnum
>= numregs
)
326 error ("bad register number");
328 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
329 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
330 ui_out_field_string (uiout
, NULL
, "");
332 ui_out_field_string (uiout
, NULL
,
333 gdbarch_register_name (current_gdbarch
, regnum
));
335 do_cleanups (cleanup
);
340 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
342 static struct regcache
*this_regs
= NULL
;
343 struct regcache
*prev_regs
;
344 int regnum
, numregs
, changed
;
346 struct cleanup
*cleanup
;
348 /* The last time we visited this function, the current frame's register
349 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
350 and refresh THIS_REGS with the now-current register contents. */
352 prev_regs
= this_regs
;
353 this_regs
= frame_save_as_regcache (get_selected_frame (NULL
));
354 cleanup
= make_cleanup_regcache_xfree (prev_regs
);
356 /* Note that the test for a valid register must include checking the
357 gdbarch_register_name because gdbarch_num_regs may be allocated for
358 the union of the register sets within a family of related processors.
359 In this case, some entries of gdbarch_register_name will change depending
360 upon the particular processor being debugged. */
362 numregs
= gdbarch_num_regs (current_gdbarch
)
363 + gdbarch_num_pseudo_regs (current_gdbarch
);
365 make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
367 if (argc
== 0) /* No args, just do all the regs. */
373 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
374 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
376 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
378 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
380 ui_out_field_int (uiout
, NULL
, regnum
);
384 /* Else, list of register #s, just do listed regs. */
385 for (i
= 0; i
< argc
; i
++)
387 regnum
= atoi (argv
[i
]);
391 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
392 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
394 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
396 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
398 ui_out_field_int (uiout
, NULL
, regnum
);
401 error ("bad register number");
403 do_cleanups (cleanup
);
408 register_changed_p (int regnum
, struct regcache
*prev_regs
,
409 struct regcache
*this_regs
)
411 struct gdbarch
*gdbarch
= get_regcache_arch (this_regs
);
412 gdb_byte prev_buffer
[MAX_REGISTER_SIZE
];
413 gdb_byte this_buffer
[MAX_REGISTER_SIZE
];
415 /* Registers not valid in this frame return count as unchanged. */
416 if (!regcache_valid_p (this_regs
, regnum
))
419 /* First time through or after gdbarch change consider all registers as
420 changed. Same for registers not valid in the previous frame. */
421 if (!prev_regs
|| get_regcache_arch (prev_regs
) != gdbarch
422 || !regcache_valid_p (prev_regs
, regnum
))
425 /* Get register contents and compare. */
426 regcache_cooked_read (prev_regs
, regnum
, prev_buffer
);
427 regcache_cooked_read (this_regs
, regnum
, this_buffer
);
429 return memcmp (prev_buffer
, this_buffer
,
430 register_size (gdbarch
, regnum
)) != 0;
433 /* Return a list of register number and value pairs. The valid
434 arguments expected are: a letter indicating the format in which to
435 display the registers contents. This can be one of: x (hexadecimal), d
436 (decimal), N (natural), t (binary), o (octal), r (raw). After the
437 format argumetn there can be a sequence of numbers, indicating which
438 registers to fetch the content of. If the format is the only argument,
439 a list of all the registers with their values is returned. */
441 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
443 int regnum
, numregs
, format
;
445 struct cleanup
*list_cleanup
, *tuple_cleanup
;
447 /* Note that the test for a valid register must include checking the
448 gdbarch_register_name because gdbarch_num_regs may be allocated for
449 the union of the register sets within a family of related processors.
450 In this case, some entries of gdbarch_register_name will change depending
451 upon the particular processor being debugged. */
453 numregs
= gdbarch_num_regs (current_gdbarch
)
454 + gdbarch_num_pseudo_regs (current_gdbarch
);
457 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
459 format
= (int) argv
[0][0];
461 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
463 if (argc
== 1) /* No args, beside the format: do all the regs. */
469 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
470 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
472 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
473 ui_out_field_int (uiout
, "number", regnum
);
474 get_register (regnum
, format
);
475 do_cleanups (tuple_cleanup
);
479 /* Else, list of register #s, just do listed regs. */
480 for (i
= 1; i
< argc
; i
++)
482 regnum
= atoi (argv
[i
]);
486 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
487 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
489 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
490 ui_out_field_int (uiout
, "number", regnum
);
491 get_register (regnum
, format
);
492 do_cleanups (tuple_cleanup
);
495 error ("bad register number");
497 do_cleanups (list_cleanup
);
501 /* Output one register's contents in the desired format. */
503 get_register (int regnum
, int format
)
505 gdb_byte buffer
[MAX_REGISTER_SIZE
];
510 static struct ui_stream
*stb
= NULL
;
512 stb
= ui_out_stream_new (uiout
);
517 frame_register (get_selected_frame (NULL
), regnum
, &optim
, &lval
, &addr
,
521 error ("Optimized out");
526 char *ptr
, buf
[1024];
530 for (j
= 0; j
< register_size (current_gdbarch
, regnum
); j
++)
532 int idx
= gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
? j
533 : register_size (current_gdbarch
, regnum
) - 1 - j
;
534 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
537 ui_out_field_string (uiout
, "value", buf
);
538 /*fputs_filtered (buf, gdb_stdout); */
542 val_print (register_type (current_gdbarch
, regnum
), buffer
, 0, 0,
543 stb
->stream
, format
, 1, 0, Val_pretty_default
);
544 ui_out_field_stream (uiout
, "value", stb
);
545 ui_out_stream_delete (stb
);
549 /* Write given values into registers. The registers and values are
550 given as pairs. The corresponding MI command is
551 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
553 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
558 /* Note that the test for a valid register must include checking the
559 gdbarch_register_name because gdbarch_num_regs may be allocated for
560 the union of the register sets within a family of related processors.
561 In this case, some entries of gdbarch_register_name will change depending
562 upon the particular processor being debugged. */
564 numregs
= gdbarch_num_regs (current_gdbarch
)
565 + gdbarch_num_pseudo_regs (current_gdbarch
);
568 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
570 format
= (int) argv
[0][0];
572 if (!target_has_registers
)
573 error ("mi_cmd_data_write_register_values: No registers.");
576 error ("mi_cmd_data_write_register_values: No regs and values specified.");
579 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
581 for (i
= 1; i
< argc
; i
= i
+ 2)
583 int regnum
= atoi (argv
[i
]);
585 if (regnum
>= 0 && regnum
< numregs
586 && gdbarch_register_name (current_gdbarch
, regnum
)
587 && *gdbarch_register_name (current_gdbarch
, regnum
))
591 /* Get the value as a number. */
592 value
= parse_and_eval_address (argv
[i
+ 1]);
595 regcache_cooked_write_signed (get_current_regcache (), regnum
, value
);
598 error ("bad register number");
603 /* Evaluate the value of the argument. The argument is an
604 expression. If the expression contains spaces it needs to be
605 included in double quotes. */
607 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
609 struct expression
*expr
;
610 struct cleanup
*old_chain
= NULL
;
612 struct ui_stream
*stb
= NULL
;
614 stb
= ui_out_stream_new (uiout
);
618 ui_out_stream_delete (stb
);
619 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
622 expr
= parse_expression (argv
[0]);
624 old_chain
= make_cleanup (free_current_contents
, &expr
);
626 val
= evaluate_expression (expr
);
628 /* Print the result of the expression evaluation. */
629 val_print (value_type (val
), value_contents (val
),
630 value_embedded_offset (val
), VALUE_ADDRESS (val
),
631 stb
->stream
, 0, 0, 0, 0);
633 ui_out_field_stream (uiout
, "value", stb
);
634 ui_out_stream_delete (stb
);
636 do_cleanups (old_chain
);
642 mi_cmd_target_download (char *args
, int from_tty
)
645 struct cleanup
*old_cleanups
= NULL
;
647 run
= xstrprintf ("load %s", args
);
648 old_cleanups
= make_cleanup (xfree
, run
);
649 execute_command (run
, from_tty
);
651 do_cleanups (old_cleanups
);
655 /* Connect to the remote target. */
657 mi_cmd_target_select (char *args
, int from_tty
)
660 struct cleanup
*old_cleanups
= NULL
;
662 run
= xstrprintf ("target %s", args
);
663 old_cleanups
= make_cleanup (xfree
, run
);
665 /* target-select is always synchronous. Once the call has returned
666 we know that we are connected. */
667 /* NOTE: At present all targets that are connected are also
668 (implicitly) talking to a halted target. In the future this may
670 execute_command (run
, from_tty
);
672 do_cleanups (old_cleanups
);
674 /* Issue the completion message here. */
676 fputs_unfiltered (current_token
, raw_stdout
);
677 fputs_unfiltered ("^connected", raw_stdout
);
678 mi_out_put (uiout
, raw_stdout
);
679 mi_out_rewind (uiout
);
680 fputs_unfiltered ("\n", raw_stdout
);
686 ADDR: start address of data to be dumped.
687 WORD-FORMAT: a char indicating format for the ``word''. See
689 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
690 NR_ROW: Number of rows.
691 NR_COL: The number of colums (words per row).
692 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
693 ASCHAR for unprintable characters.
695 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
696 displayes them. Returns:
698 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
701 The number of bytes read is SIZE*ROW*COL. */
704 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
706 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
712 struct type
*word_type
;
725 static struct mi_opt opts
[] =
727 {"o", OFFSET_OPT
, 1},
733 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
737 switch ((enum opt
) opt
)
740 offset
= atol (optarg
);
747 if (argc
< 5 || argc
> 6)
748 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
750 /* Extract all the arguments. */
752 /* Start address of the memory dump. */
753 addr
= parse_and_eval_address (argv
[0]) + offset
;
754 /* The format character to use when displaying a memory word. See
755 the ``x'' command. */
756 word_format
= argv
[1][0];
757 /* The size of the memory word. */
758 word_size
= atol (argv
[2]);
762 word_type
= builtin_type_int8
;
766 word_type
= builtin_type_int16
;
770 word_type
= builtin_type_int32
;
774 word_type
= builtin_type_int64
;
778 word_type
= builtin_type_int8
;
781 /* The number of rows. */
782 nr_rows
= atol (argv
[3]);
784 error ("mi_cmd_data_read_memory: invalid number of rows.");
786 /* Number of bytes per row. */
787 nr_cols
= atol (argv
[4]);
789 error ("mi_cmd_data_read_memory: invalid number of columns.");
791 /* The un-printable character when printing ascii. */
797 /* Create a buffer and read it in. */
798 total_bytes
= word_size
* nr_rows
* nr_cols
;
799 mbuf
= xcalloc (total_bytes
, 1);
800 make_cleanup (xfree
, mbuf
);
802 nr_bytes
= target_read (¤t_target
, TARGET_OBJECT_MEMORY
, NULL
,
803 mbuf
, addr
, total_bytes
);
805 error ("Unable to read memory.");
807 /* Output the header information. */
808 ui_out_field_core_addr (uiout
, "addr", addr
);
809 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
810 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
811 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
812 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
813 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
814 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
816 /* Build the result as a two dimentional table. */
818 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
819 struct cleanup
*cleanup_list_memory
;
822 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
823 for (row
= 0, row_byte
= 0;
825 row
++, row_byte
+= nr_cols
* word_size
)
829 struct cleanup
*cleanup_tuple
;
830 struct cleanup
*cleanup_list_data
;
831 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
832 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
833 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
834 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
835 for (col
= 0, col_byte
= row_byte
;
837 col
++, col_byte
+= word_size
)
839 if (col_byte
+ word_size
> nr_bytes
)
841 ui_out_field_string (uiout
, NULL
, "N/A");
845 ui_file_rewind (stream
->stream
);
846 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
847 word_asize
, stream
->stream
);
848 ui_out_field_stream (uiout
, NULL
, stream
);
851 do_cleanups (cleanup_list_data
);
855 ui_file_rewind (stream
->stream
);
856 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
858 if (byte
>= nr_bytes
)
860 fputc_unfiltered ('X', stream
->stream
);
862 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
864 fputc_unfiltered (aschar
, stream
->stream
);
867 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
869 ui_out_field_stream (uiout
, "ascii", stream
);
871 do_cleanups (cleanup_tuple
);
873 ui_out_stream_delete (stream
);
874 do_cleanups (cleanup_list_memory
);
876 do_cleanups (cleanups
);
880 /* DATA-MEMORY-WRITE:
882 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
883 offset from the beginning of the memory grid row where the cell to
885 ADDR: start address of the row in the memory grid where the memory
886 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
887 the location to write to.
888 FORMAT: a char indicating format for the ``word''. See
890 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
891 VALUE: value to be written into the memory address.
893 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
897 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
902 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
903 enough when using a compiler other than GCC. */
906 struct cleanup
*old_chain
;
914 static struct mi_opt opts
[] =
916 {"o", OFFSET_OPT
, 1},
922 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
926 switch ((enum opt
) opt
)
929 offset
= atol (optarg
);
937 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
939 /* Extract all the arguments. */
940 /* Start address of the memory dump. */
941 addr
= parse_and_eval_address (argv
[0]);
942 /* The format character to use when displaying a memory word. See
943 the ``x'' command. */
944 word_format
= argv
[1][0];
945 /* The size of the memory word. */
946 word_size
= atol (argv
[2]);
948 /* Calculate the real address of the write destination. */
949 addr
+= (offset
* word_size
);
951 /* Get the value as a number. */
952 value
= parse_and_eval_address (argv
[3]);
953 /* Get the value into an array. */
954 buffer
= xmalloc (word_size
);
955 old_chain
= make_cleanup (xfree
, buffer
);
956 store_signed_integer (buffer
, word_size
, value
);
957 /* Write it down to memory. */
958 write_memory (addr
, buffer
, word_size
);
959 /* Free the buffer. */
960 do_cleanups (old_chain
);
966 mi_cmd_enable_timings (char *command
, char **argv
, int argc
)
972 if (strcmp (argv
[0], "yes") == 0)
974 else if (strcmp (argv
[0], "no") == 0)
985 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command
);
990 mi_cmd_list_features (char *command
, char **argv
, int argc
)
994 struct cleanup
*cleanup
= NULL
;
995 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
997 ui_out_field_string (uiout
, NULL
, "frozen-varobjs");
998 ui_out_field_string (uiout
, NULL
, "pending-breakpoints");
999 ui_out_field_string (uiout
, NULL
, "thread-info");
1001 do_cleanups (cleanup
);
1006 error ("-list-features should be passed no arguments");
1010 /* Execute a command within a safe environment.
1011 Return <0 for error; >=0 for ok.
1013 args->action will tell mi_execute_command what action
1014 to perfrom after the given command has executed (display/suppress
1015 prompt, display error). */
1018 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1020 struct captured_mi_execute_command_args
*args
=
1021 (struct captured_mi_execute_command_args
*) data
;
1022 struct mi_parse
*context
= args
->command
;
1024 struct mi_timestamp cmd_finished
;
1026 switch (context
->op
)
1030 /* A MI command was read from the input stream. */
1032 /* FIXME: gdb_???? */
1033 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1034 context
->token
, context
->command
, context
->args
);
1035 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1036 condition expression, each function should return an
1037 indication of what action is required and then switch on
1039 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1042 current_command_ts
= context
->cmd_start
;
1044 args
->rc
= mi_cmd_execute (context
);
1047 timestamp (&cmd_finished
);
1049 if (!target_can_async_p () || !target_executing
)
1051 /* Print the result if there were no errors.
1053 Remember that on the way out of executing a command, you have
1054 to directly use the mi_interp's uiout, since the command could
1055 have reset the interpreter, in which case the current uiout
1056 will most likely crash in the mi_out_* routines. */
1057 if (args
->rc
== MI_CMD_DONE
)
1059 fputs_unfiltered (context
->token
, raw_stdout
);
1060 fputs_unfiltered ("^done", raw_stdout
);
1061 mi_out_put (uiout
, raw_stdout
);
1062 mi_out_rewind (uiout
);
1063 /* Have to check cmd_start, since the command could be
1065 if (do_timings
&& context
->cmd_start
)
1066 print_diff (context
->cmd_start
, &cmd_finished
);
1067 fputs_unfiltered ("\n", raw_stdout
);
1070 mi_out_rewind (uiout
);
1072 else if (sync_execution
)
1074 /* Don't print the prompt. We are executing the target in
1075 synchronous mode. */
1076 args
->action
= EXECUTE_COMMAND_SUPPRESS_PROMPT
;
1084 /* A CLI command was read from the input stream. */
1085 /* This "feature" will be removed as soon as we have a
1086 complete set of mi commands. */
1087 /* Echo the command on the console. */
1088 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1089 /* Call the "console" interpreter. */
1090 argv
[0] = "console";
1091 argv
[1] = context
->command
;
1092 args
->rc
= mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
1094 /* If we changed interpreters, DON'T print out anything. */
1095 if (current_interp_named_p (INTERP_MI
)
1096 || current_interp_named_p (INTERP_MI1
)
1097 || current_interp_named_p (INTERP_MI2
)
1098 || current_interp_named_p (INTERP_MI3
))
1100 if (args
->rc
== MI_CMD_DONE
)
1102 fputs_unfiltered (context
->token
, raw_stdout
);
1103 fputs_unfiltered ("^done", raw_stdout
);
1104 mi_out_put (uiout
, raw_stdout
);
1105 mi_out_rewind (uiout
);
1106 fputs_unfiltered ("\n", raw_stdout
);
1107 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1110 mi_out_rewind (uiout
);
1122 mi_execute_command (char *cmd
, int from_tty
)
1124 struct mi_parse
*command
;
1125 struct captured_mi_execute_command_args args
;
1126 struct ui_out
*saved_uiout
= uiout
;
1128 /* This is to handle EOF (^D). We just quit gdb. */
1129 /* FIXME: we should call some API function here. */
1131 quit_force (NULL
, from_tty
);
1133 command
= mi_parse (cmd
);
1135 if (command
!= NULL
)
1137 struct gdb_exception result
;
1141 command
->cmd_start
= (struct mi_timestamp
*)
1142 xmalloc (sizeof (struct mi_timestamp
));
1143 timestamp (command
->cmd_start
);
1146 args
.command
= command
;
1147 result
= catch_exception (uiout
, captured_mi_execute_command
, &args
,
1149 if (result
.reason
< 0)
1151 /* The command execution failed and error() was called
1153 fputs_unfiltered (command
->token
, raw_stdout
);
1154 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1155 if (result
.message
== NULL
)
1156 fputs_unfiltered ("unknown error", raw_stdout
);
1158 fputstr_unfiltered (result
.message
, '"', raw_stdout
);
1159 fputs_unfiltered ("\"\n", raw_stdout
);
1160 mi_out_rewind (uiout
);
1163 mi_parse_free (command
);
1165 if (args
.action
== EXECUTE_COMMAND_SUPPRESS_PROMPT
)
1166 /* The command is executing synchronously. Bail out early
1167 suppressing the finished prompt. */
1171 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1172 gdb_flush (raw_stdout
);
1173 /* Print any buffered hook code. */
1177 static enum mi_cmd_result
1178 mi_cmd_execute (struct mi_parse
*parse
)
1180 struct cleanup
*cleanup
;
1181 enum mi_cmd_result r
;
1184 if (parse
->cmd
->argv_func
!= NULL
1185 || parse
->cmd
->args_func
!= NULL
)
1187 if (target_executing
)
1189 if (strcmp (parse
->command
, "exec-interrupt"))
1191 struct ui_file
*stb
;
1192 stb
= mem_fileopen ();
1194 fputs_unfiltered ("Cannot execute command ", stb
);
1195 fputstr_unfiltered (parse
->command
, '"', stb
);
1196 fputs_unfiltered (" while target running", stb
);
1198 make_cleanup_ui_file_delete (stb
);
1202 current_token
= xstrdup (parse
->token
);
1203 cleanup
= make_cleanup (free_current_contents
, ¤t_token
);
1204 /* FIXME: DELETE THIS! */
1205 if (parse
->cmd
->args_func
!= NULL
)
1206 r
= parse
->cmd
->args_func (parse
->args
, 0 /*from_tty */ );
1208 r
= parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1209 do_cleanups (cleanup
);
1212 else if (parse
->cmd
->cli
.cmd
!= 0)
1214 /* FIXME: DELETE THIS. */
1215 /* The operation is still implemented by a cli command. */
1216 /* Must be a synchronous one. */
1217 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1223 /* FIXME: DELETE THIS. */
1224 struct ui_file
*stb
;
1226 stb
= mem_fileopen ();
1228 fputs_unfiltered ("Undefined mi command: ", stb
);
1229 fputstr_unfiltered (parse
->command
, '"', stb
);
1230 fputs_unfiltered (" (missing implementation)", stb
);
1232 make_cleanup_ui_file_delete (stb
);
1240 /* FIXME: This is just a hack so we can get some extra commands going.
1241 We don't want to channel things through the CLI, but call libgdb directly.
1242 Use only for synchronous commands. */
1245 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1249 struct cleanup
*old_cleanups
;
1252 run
= xstrprintf ("%s %s", cmd
, args
);
1254 run
= xstrdup (cmd
);
1256 /* FIXME: gdb_???? */
1257 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1259 old_cleanups
= make_cleanup (xfree
, run
);
1260 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1261 do_cleanups (old_cleanups
);
1267 mi_execute_async_cli_command (char *mi
, char *args
, int from_tty
)
1269 struct cleanup
*old_cleanups
;
1272 if (target_can_async_p ())
1273 run
= xstrprintf ("%s %s&", mi
, args
);
1275 run
= xstrprintf ("%s %s", mi
, args
);
1276 old_cleanups
= make_cleanup (xfree
, run
);
1278 if (!target_can_async_p ())
1280 /* NOTE: For synchronous targets asynchronous behavour is faked by
1281 printing out the GDB prompt before we even try to execute the
1284 fputs_unfiltered (current_token
, raw_stdout
);
1285 fputs_unfiltered ("^running\n", raw_stdout
);
1286 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1287 gdb_flush (raw_stdout
);
1291 /* FIXME: cagney/1999-11-29: Printing this message before
1292 calling execute_command is wrong. It should only be printed
1293 once gdb has confirmed that it really has managed to send a
1294 run command to the target. */
1296 fputs_unfiltered (current_token
, raw_stdout
);
1297 fputs_unfiltered ("^running\n", raw_stdout
);
1299 /* Ideally, we should be intalling continuation only when
1300 the target is already running. However, this will break right now,
1301 because continuation installed by the 'finish' command must be after
1302 the continuation that prints *stopped. This issue will be
1304 add_continuation (mi_exec_async_cli_cmd_continuation
, NULL
);
1307 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1309 if (target_can_async_p ())
1311 /* If we're not executing, an exception should have been throw. */
1312 gdb_assert (target_executing
);
1313 do_cleanups (old_cleanups
);
1317 /* Do this before doing any printing. It would appear that some
1318 print code leaves garbage around in the buffer. */
1319 do_cleanups (old_cleanups
);
1320 /* If the target was doing the operation synchronously we fake
1321 the stopped message. */
1322 fputs_unfiltered ("*stopped", raw_stdout
);
1323 mi_out_put (uiout
, raw_stdout
);
1324 mi_out_rewind (uiout
);
1326 print_diff_now (current_command_ts
);
1327 fputs_unfiltered ("\n", raw_stdout
);
1328 return MI_CMD_QUIET
;
1334 mi_exec_async_cli_cmd_continuation (struct continuation_arg
*arg
, int error_p
)
1336 /* Assume 'error' means that target is stopped, too. */
1337 fputs_unfiltered ("*stopped", raw_stdout
);
1338 mi_out_put (uiout
, raw_stdout
);
1339 fputs_unfiltered ("\n", raw_stdout
);
1340 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1341 gdb_flush (raw_stdout
);
1345 mi_load_progress (const char *section_name
,
1346 unsigned long sent_so_far
,
1347 unsigned long total_section
,
1348 unsigned long total_sent
,
1349 unsigned long grand_total
)
1351 struct timeval time_now
, delta
, update_threshold
;
1352 static struct timeval last_update
;
1353 static char *previous_sect_name
= NULL
;
1355 struct ui_out
*saved_uiout
;
1357 /* This function is called through deprecated_show_load_progress
1358 which means uiout may not be correct. Fix it for the duration
1359 of this function. */
1360 saved_uiout
= uiout
;
1362 if (current_interp_named_p (INTERP_MI
)
1363 || current_interp_named_p (INTERP_MI2
))
1364 uiout
= mi_out_new (2);
1365 else if (current_interp_named_p (INTERP_MI1
))
1366 uiout
= mi_out_new (1);
1367 else if (current_interp_named_p (INTERP_MI3
))
1368 uiout
= mi_out_new (3);
1372 update_threshold
.tv_sec
= 0;
1373 update_threshold
.tv_usec
= 500000;
1374 gettimeofday (&time_now
, NULL
);
1376 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1377 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1379 if (delta
.tv_usec
< 0)
1382 delta
.tv_usec
+= 1000000L;
1385 new_section
= (previous_sect_name
?
1386 strcmp (previous_sect_name
, section_name
) : 1);
1389 struct cleanup
*cleanup_tuple
;
1390 xfree (previous_sect_name
);
1391 previous_sect_name
= xstrdup (section_name
);
1394 fputs_unfiltered (current_token
, raw_stdout
);
1395 fputs_unfiltered ("+download", raw_stdout
);
1396 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1397 ui_out_field_string (uiout
, "section", section_name
);
1398 ui_out_field_int (uiout
, "section-size", total_section
);
1399 ui_out_field_int (uiout
, "total-size", grand_total
);
1400 do_cleanups (cleanup_tuple
);
1401 mi_out_put (uiout
, raw_stdout
);
1402 fputs_unfiltered ("\n", raw_stdout
);
1403 gdb_flush (raw_stdout
);
1406 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1407 delta
.tv_usec
>= update_threshold
.tv_usec
)
1409 struct cleanup
*cleanup_tuple
;
1410 last_update
.tv_sec
= time_now
.tv_sec
;
1411 last_update
.tv_usec
= time_now
.tv_usec
;
1413 fputs_unfiltered (current_token
, raw_stdout
);
1414 fputs_unfiltered ("+download", raw_stdout
);
1415 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1416 ui_out_field_string (uiout
, "section", section_name
);
1417 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1418 ui_out_field_int (uiout
, "section-size", total_section
);
1419 ui_out_field_int (uiout
, "total-sent", total_sent
);
1420 ui_out_field_int (uiout
, "total-size", grand_total
);
1421 do_cleanups (cleanup_tuple
);
1422 mi_out_put (uiout
, raw_stdout
);
1423 fputs_unfiltered ("\n", raw_stdout
);
1424 gdb_flush (raw_stdout
);
1428 uiout
= saved_uiout
;
1432 timestamp (struct mi_timestamp
*tv
)
1435 gettimeofday (&tv
->wallclock
, NULL
);
1436 #ifdef HAVE_GETRUSAGE
1437 getrusage (RUSAGE_SELF
, &rusage
);
1438 tv
->utime
.tv_sec
= rusage
.ru_utime
.tv_sec
;
1439 tv
->utime
.tv_usec
= rusage
.ru_utime
.tv_usec
;
1440 tv
->stime
.tv_sec
= rusage
.ru_stime
.tv_sec
;
1441 tv
->stime
.tv_usec
= rusage
.ru_stime
.tv_usec
;
1443 usec
= get_run_time ();
1444 tv
->utime
.tv_sec
= usec
/1000000L;
1445 tv
->utime
.tv_usec
= usec
- 1000000L*tv
->utime
.tv_sec
;
1446 tv
->stime
.tv_sec
= 0;
1447 tv
->stime
.tv_usec
= 0;
1452 print_diff_now (struct mi_timestamp
*start
)
1454 struct mi_timestamp now
;
1456 print_diff (start
, &now
);
1460 timeval_diff (struct timeval start
, struct timeval end
)
1462 return ((end
.tv_sec
- start
.tv_sec
) * 1000000L)
1463 + (end
.tv_usec
- start
.tv_usec
);
1467 print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
)
1471 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1472 timeval_diff (start
->wallclock
, end
->wallclock
) / 1000000.0,
1473 timeval_diff (start
->utime
, end
->utime
) / 1000000.0,
1474 timeval_diff (start
->stime
, end
->stime
) / 1000000.0);