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_run (char *command
, char **argv
, int argc
)
112 /* FIXME: Should call a libgdb function, not a cli wrapper. */
113 return mi_execute_async_cli_command ("run", argv
, argc
);
117 mi_cmd_exec_next (char *command
, char **argv
, int argc
)
119 /* FIXME: Should call a libgdb function, not a cli wrapper. */
120 return mi_execute_async_cli_command ("next", argv
, argc
);
124 mi_cmd_exec_next_instruction (char *command
, char **argv
, int argc
)
126 /* FIXME: Should call a libgdb function, not a cli wrapper. */
127 return mi_execute_async_cli_command ("nexti", argv
, argc
);
131 mi_cmd_exec_step (char *command
, char **argv
, int argc
)
133 /* FIXME: Should call a libgdb function, not a cli wrapper. */
134 return mi_execute_async_cli_command ("step", argv
, argc
);
138 mi_cmd_exec_step_instruction (char *command
, char **argv
, int argc
)
140 /* FIXME: Should call a libgdb function, not a cli wrapper. */
141 return mi_execute_async_cli_command ("stepi", argv
, argc
);
145 mi_cmd_exec_finish (char *command
, char **argv
, int argc
)
147 /* FIXME: Should call a libgdb function, not a cli wrapper. */
148 return mi_execute_async_cli_command ("finish", argv
, argc
);
152 mi_cmd_exec_until (char *command
, char **argv
, int argc
)
154 /* FIXME: Should call a libgdb function, not a cli wrapper. */
155 return mi_execute_async_cli_command ("until", argv
, argc
);
159 mi_cmd_exec_return (char *command
, char **argv
, int argc
)
161 /* This command doesn't really execute the target, it just pops the
162 specified number of frames. */
164 /* Call return_command with from_tty argument equal to 0 so as to
165 avoid being queried. */
166 return_command (*argv
, 0);
168 /* Call return_command with from_tty argument equal to 0 so as to
169 avoid being queried. */
170 return_command (NULL
, 0);
172 /* Because we have called return_command with from_tty = 0, we need
173 to print the frame here. */
174 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
);
178 mi_cmd_exec_continue (char *command
, char **argv
, int argc
)
180 /* FIXME: Should call a libgdb function, not a cli wrapper. */
181 return mi_execute_async_cli_command ("continue", argv
, argc
);
184 /* Interrupt the execution of the target. Note how we must play around
185 with the token variables, in order to display the current token in
186 the result of the interrupt command, and the previous execution
187 token when the target finally stops. See comments in
190 mi_cmd_exec_interrupt (char *command
, char **argv
, int argc
)
192 if (!target_executing
)
193 error ("mi_cmd_exec_interrupt: Inferior not executing.");
195 interrupt_target_command (NULL
, 0);
199 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
202 char *mi_error_message
;
205 error ("mi_cmd_thread_select: USAGE: threadnum.");
207 rc
= gdb_thread_select (uiout
, argv
[0], &mi_error_message
);
209 if (rc
== GDB_RC_FAIL
)
211 make_cleanup (xfree
, mi_error_message
);
212 error ("%s", mi_error_message
);
217 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
220 char *mi_error_message
;
223 error ("mi_cmd_thread_list_ids: No arguments required.");
225 rc
= gdb_list_thread_ids (uiout
, &mi_error_message
);
227 if (rc
== GDB_RC_FAIL
)
229 make_cleanup (xfree
, mi_error_message
);
230 error ("%s", mi_error_message
);
235 mi_cmd_thread_info (char *command
, char **argv
, int argc
)
239 if (argc
!= 0 && argc
!= 1)
240 error ("Invalid MI command");
243 thread
= atoi (argv
[0]);
245 print_thread_info (uiout
, thread
);
249 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
253 struct cleanup
*cleanup
;
255 /* Note that the test for a valid register must include checking the
256 gdbarch_register_name because gdbarch_num_regs may be allocated for
257 the union of the register sets within a family of related processors.
258 In this case, some entries of gdbarch_register_name will change depending
259 upon the particular processor being debugged. */
261 numregs
= gdbarch_num_regs (current_gdbarch
)
262 + gdbarch_num_pseudo_regs (current_gdbarch
);
264 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
266 if (argc
== 0) /* No args, just do all the regs. */
272 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
273 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
274 ui_out_field_string (uiout
, NULL
, "");
276 ui_out_field_string (uiout
, NULL
,
277 gdbarch_register_name
278 (current_gdbarch
, regnum
));
282 /* Else, list of register #s, just do listed regs. */
283 for (i
= 0; i
< argc
; i
++)
285 regnum
= atoi (argv
[i
]);
286 if (regnum
< 0 || regnum
>= numregs
)
287 error ("bad register number");
289 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
290 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
291 ui_out_field_string (uiout
, NULL
, "");
293 ui_out_field_string (uiout
, NULL
,
294 gdbarch_register_name (current_gdbarch
, regnum
));
296 do_cleanups (cleanup
);
300 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
302 static struct regcache
*this_regs
= NULL
;
303 struct regcache
*prev_regs
;
304 int regnum
, numregs
, changed
;
306 struct cleanup
*cleanup
;
308 /* The last time we visited this function, the current frame's register
309 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
310 and refresh THIS_REGS with the now-current register contents. */
312 prev_regs
= this_regs
;
313 this_regs
= frame_save_as_regcache (get_selected_frame (NULL
));
314 cleanup
= make_cleanup_regcache_xfree (prev_regs
);
316 /* Note that the test for a valid register must include checking the
317 gdbarch_register_name because gdbarch_num_regs may be allocated for
318 the union of the register sets within a family of related processors.
319 In this case, some entries of gdbarch_register_name will change depending
320 upon the particular processor being debugged. */
322 numregs
= gdbarch_num_regs (current_gdbarch
)
323 + gdbarch_num_pseudo_regs (current_gdbarch
);
325 make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
327 if (argc
== 0) /* No args, just do all the regs. */
333 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
334 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
336 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
338 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
340 ui_out_field_int (uiout
, NULL
, regnum
);
344 /* Else, list of register #s, just do listed regs. */
345 for (i
= 0; i
< argc
; i
++)
347 regnum
= atoi (argv
[i
]);
351 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
352 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
354 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
356 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
358 ui_out_field_int (uiout
, NULL
, regnum
);
361 error ("bad register number");
363 do_cleanups (cleanup
);
367 register_changed_p (int regnum
, struct regcache
*prev_regs
,
368 struct regcache
*this_regs
)
370 struct gdbarch
*gdbarch
= get_regcache_arch (this_regs
);
371 gdb_byte prev_buffer
[MAX_REGISTER_SIZE
];
372 gdb_byte this_buffer
[MAX_REGISTER_SIZE
];
374 /* Registers not valid in this frame return count as unchanged. */
375 if (!regcache_valid_p (this_regs
, regnum
))
378 /* First time through or after gdbarch change consider all registers as
379 changed. Same for registers not valid in the previous frame. */
380 if (!prev_regs
|| get_regcache_arch (prev_regs
) != gdbarch
381 || !regcache_valid_p (prev_regs
, regnum
))
384 /* Get register contents and compare. */
385 regcache_cooked_read (prev_regs
, regnum
, prev_buffer
);
386 regcache_cooked_read (this_regs
, regnum
, this_buffer
);
388 return memcmp (prev_buffer
, this_buffer
,
389 register_size (gdbarch
, regnum
)) != 0;
392 /* Return a list of register number and value pairs. The valid
393 arguments expected are: a letter indicating the format in which to
394 display the registers contents. This can be one of: x (hexadecimal), d
395 (decimal), N (natural), t (binary), o (octal), r (raw). After the
396 format argumetn there can be a sequence of numbers, indicating which
397 registers to fetch the content of. If the format is the only argument,
398 a list of all the registers with their values is returned. */
400 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
402 int regnum
, numregs
, format
;
404 struct cleanup
*list_cleanup
, *tuple_cleanup
;
406 /* Note that the test for a valid register must include checking the
407 gdbarch_register_name because gdbarch_num_regs may be allocated for
408 the union of the register sets within a family of related processors.
409 In this case, some entries of gdbarch_register_name will change depending
410 upon the particular processor being debugged. */
412 numregs
= gdbarch_num_regs (current_gdbarch
)
413 + gdbarch_num_pseudo_regs (current_gdbarch
);
416 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
418 format
= (int) argv
[0][0];
420 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
422 if (argc
== 1) /* No args, beside the format: do all the regs. */
428 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
429 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
431 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
432 ui_out_field_int (uiout
, "number", regnum
);
433 get_register (regnum
, format
);
434 do_cleanups (tuple_cleanup
);
438 /* Else, list of register #s, just do listed regs. */
439 for (i
= 1; i
< argc
; i
++)
441 regnum
= atoi (argv
[i
]);
445 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
446 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
448 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
449 ui_out_field_int (uiout
, "number", regnum
);
450 get_register (regnum
, format
);
451 do_cleanups (tuple_cleanup
);
454 error ("bad register number");
456 do_cleanups (list_cleanup
);
459 /* Output one register's contents in the desired format. */
461 get_register (int regnum
, int format
)
463 gdb_byte buffer
[MAX_REGISTER_SIZE
];
468 static struct ui_stream
*stb
= NULL
;
470 stb
= ui_out_stream_new (uiout
);
475 frame_register (get_selected_frame (NULL
), regnum
, &optim
, &lval
, &addr
,
479 error ("Optimized out");
484 char *ptr
, buf
[1024];
488 for (j
= 0; j
< register_size (current_gdbarch
, regnum
); j
++)
490 int idx
= gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
? j
491 : register_size (current_gdbarch
, regnum
) - 1 - j
;
492 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
495 ui_out_field_string (uiout
, "value", buf
);
496 /*fputs_filtered (buf, gdb_stdout); */
500 val_print (register_type (current_gdbarch
, regnum
), buffer
, 0, 0,
501 stb
->stream
, format
, 1, 0, Val_pretty_default
,
503 ui_out_field_stream (uiout
, "value", stb
);
504 ui_out_stream_delete (stb
);
508 /* Write given values into registers. The registers and values are
509 given as pairs. The corresponding MI command is
510 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
512 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
517 /* Note that the test for a valid register must include checking the
518 gdbarch_register_name because gdbarch_num_regs may be allocated for
519 the union of the register sets within a family of related processors.
520 In this case, some entries of gdbarch_register_name will change depending
521 upon the particular processor being debugged. */
523 numregs
= gdbarch_num_regs (current_gdbarch
)
524 + gdbarch_num_pseudo_regs (current_gdbarch
);
527 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
529 format
= (int) argv
[0][0];
531 if (!target_has_registers
)
532 error ("mi_cmd_data_write_register_values: No registers.");
535 error ("mi_cmd_data_write_register_values: No regs and values specified.");
538 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
540 for (i
= 1; i
< argc
; i
= i
+ 2)
542 int regnum
= atoi (argv
[i
]);
544 if (regnum
>= 0 && regnum
< numregs
545 && gdbarch_register_name (current_gdbarch
, regnum
)
546 && *gdbarch_register_name (current_gdbarch
, regnum
))
550 /* Get the value as a number. */
551 value
= parse_and_eval_address (argv
[i
+ 1]);
554 regcache_cooked_write_signed (get_current_regcache (), regnum
, value
);
557 error ("bad register number");
561 /* Evaluate the value of the argument. The argument is an
562 expression. If the expression contains spaces it needs to be
563 included in double quotes. */
565 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
567 struct expression
*expr
;
568 struct cleanup
*old_chain
= NULL
;
570 struct ui_stream
*stb
= NULL
;
572 stb
= ui_out_stream_new (uiout
);
576 ui_out_stream_delete (stb
);
577 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
580 expr
= parse_expression (argv
[0]);
582 old_chain
= make_cleanup (free_current_contents
, &expr
);
584 val
= evaluate_expression (expr
);
586 /* Print the result of the expression evaluation. */
587 val_print (value_type (val
), value_contents (val
),
588 value_embedded_offset (val
), VALUE_ADDRESS (val
),
589 stb
->stream
, 0, 0, 0, 0, current_language
);
591 ui_out_field_stream (uiout
, "value", stb
);
592 ui_out_stream_delete (stb
);
594 do_cleanups (old_chain
);
598 mi_cmd_target_download (char *command
, char **argv
, int argc
)
601 struct cleanup
*old_cleanups
= NULL
;
603 /* There may be at most one parameter -- the name of the
605 run
= xstrprintf ("load %s", argc
? *argv
: "");
606 old_cleanups
= make_cleanup (xfree
, run
);
607 execute_command (run
, 0);
609 do_cleanups (old_cleanups
);
612 /* Connect to the remote target. */
614 mi_cmd_target_select (char *command
, char **argv
, int argc
)
617 struct cleanup
*old_cleanups
= NULL
;
621 error ("no target type specified");
623 for (i
= 0; i
< argc
; ++i
)
626 run
= concat ("target ", argv
[0], NULL
);
630 run
= concat (run
, " ", argv
[i
], NULL
);
635 old_cleanups
= make_cleanup (xfree
, run
);
637 /* target-select is always synchronous. Once the call has returned
638 we know that we are connected. */
639 /* NOTE: At present all targets that are connected are also
640 (implicitly) talking to a halted target. In the future this may
642 execute_command (run
, 0);
644 do_cleanups (old_cleanups
);
649 ADDR: start address of data to be dumped.
650 WORD-FORMAT: a char indicating format for the ``word''. See
652 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
653 NR_ROW: Number of rows.
654 NR_COL: The number of colums (words per row).
655 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
656 ASCHAR for unprintable characters.
658 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
659 displayes them. Returns:
661 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
664 The number of bytes read is SIZE*ROW*COL. */
667 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
669 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
675 struct type
*word_type
;
688 static struct mi_opt opts
[] =
690 {"o", OFFSET_OPT
, 1},
696 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
700 switch ((enum opt
) opt
)
703 offset
= atol (optarg
);
710 if (argc
< 5 || argc
> 6)
711 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
713 /* Extract all the arguments. */
715 /* Start address of the memory dump. */
716 addr
= parse_and_eval_address (argv
[0]) + offset
;
717 /* The format character to use when displaying a memory word. See
718 the ``x'' command. */
719 word_format
= argv
[1][0];
720 /* The size of the memory word. */
721 word_size
= atol (argv
[2]);
725 word_type
= builtin_type_int8
;
729 word_type
= builtin_type_int16
;
733 word_type
= builtin_type_int32
;
737 word_type
= builtin_type_int64
;
741 word_type
= builtin_type_int8
;
744 /* The number of rows. */
745 nr_rows
= atol (argv
[3]);
747 error ("mi_cmd_data_read_memory: invalid number of rows.");
749 /* Number of bytes per row. */
750 nr_cols
= atol (argv
[4]);
752 error ("mi_cmd_data_read_memory: invalid number of columns.");
754 /* The un-printable character when printing ascii. */
760 /* Create a buffer and read it in. */
761 total_bytes
= word_size
* nr_rows
* nr_cols
;
762 mbuf
= xcalloc (total_bytes
, 1);
763 make_cleanup (xfree
, mbuf
);
765 nr_bytes
= target_read_until_error (¤t_target
, TARGET_OBJECT_MEMORY
,
766 NULL
, mbuf
, addr
, total_bytes
);
768 error ("Unable to read memory.");
770 /* Output the header information. */
771 ui_out_field_core_addr (uiout
, "addr", addr
);
772 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
773 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
774 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
775 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
776 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
777 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
779 /* Build the result as a two dimentional table. */
781 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
782 struct cleanup
*cleanup_list_memory
;
785 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
786 for (row
= 0, row_byte
= 0;
788 row
++, row_byte
+= nr_cols
* word_size
)
792 struct cleanup
*cleanup_tuple
;
793 struct cleanup
*cleanup_list_data
;
794 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
795 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
796 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
797 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
798 for (col
= 0, col_byte
= row_byte
;
800 col
++, col_byte
+= word_size
)
802 if (col_byte
+ word_size
> nr_bytes
)
804 ui_out_field_string (uiout
, NULL
, "N/A");
808 ui_file_rewind (stream
->stream
);
809 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
810 word_asize
, stream
->stream
);
811 ui_out_field_stream (uiout
, NULL
, stream
);
814 do_cleanups (cleanup_list_data
);
818 ui_file_rewind (stream
->stream
);
819 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
821 if (byte
>= nr_bytes
)
823 fputc_unfiltered ('X', stream
->stream
);
825 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
827 fputc_unfiltered (aschar
, stream
->stream
);
830 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
832 ui_out_field_stream (uiout
, "ascii", stream
);
834 do_cleanups (cleanup_tuple
);
836 ui_out_stream_delete (stream
);
837 do_cleanups (cleanup_list_memory
);
839 do_cleanups (cleanups
);
842 /* DATA-MEMORY-WRITE:
844 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
845 offset from the beginning of the memory grid row where the cell to
847 ADDR: start address of the row in the memory grid where the memory
848 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
849 the location to write to.
850 FORMAT: a char indicating format for the ``word''. See
852 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
853 VALUE: value to be written into the memory address.
855 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
859 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
864 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
865 enough when using a compiler other than GCC. */
868 struct cleanup
*old_chain
;
876 static struct mi_opt opts
[] =
878 {"o", OFFSET_OPT
, 1},
884 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
888 switch ((enum opt
) opt
)
891 offset
= atol (optarg
);
899 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
901 /* Extract all the arguments. */
902 /* Start address of the memory dump. */
903 addr
= parse_and_eval_address (argv
[0]);
904 /* The format character to use when displaying a memory word. See
905 the ``x'' command. */
906 word_format
= argv
[1][0];
907 /* The size of the memory word. */
908 word_size
= atol (argv
[2]);
910 /* Calculate the real address of the write destination. */
911 addr
+= (offset
* word_size
);
913 /* Get the value as a number. */
914 value
= parse_and_eval_address (argv
[3]);
915 /* Get the value into an array. */
916 buffer
= xmalloc (word_size
);
917 old_chain
= make_cleanup (xfree
, buffer
);
918 store_signed_integer (buffer
, word_size
, value
);
919 /* Write it down to memory. */
920 write_memory (addr
, buffer
, word_size
);
921 /* Free the buffer. */
922 do_cleanups (old_chain
);
926 mi_cmd_enable_timings (char *command
, char **argv
, int argc
)
932 if (strcmp (argv
[0], "yes") == 0)
934 else if (strcmp (argv
[0], "no") == 0)
945 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command
);
949 mi_cmd_list_features (char *command
, char **argv
, int argc
)
953 struct cleanup
*cleanup
= NULL
;
954 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
956 ui_out_field_string (uiout
, NULL
, "frozen-varobjs");
957 ui_out_field_string (uiout
, NULL
, "pending-breakpoints");
958 ui_out_field_string (uiout
, NULL
, "thread-info");
960 do_cleanups (cleanup
);
964 error ("-list-features should be passed no arguments");
967 /* Execute a command within a safe environment.
968 Return <0 for error; >=0 for ok.
970 args->action will tell mi_execute_command what action
971 to perfrom after the given command has executed (display/suppress
972 prompt, display error). */
975 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
977 struct mi_parse
*context
= (struct mi_parse
*) data
;
979 struct mi_timestamp cmd_finished
;
981 running_result_record_printed
= 0;
985 /* A MI command was read from the input stream. */
987 /* FIXME: gdb_???? */
988 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
989 context
->token
, context
->command
, context
->args
);
992 current_command_ts
= context
->cmd_start
;
994 mi_cmd_execute (context
);
997 timestamp (&cmd_finished
);
999 /* Print the result if there were no errors.
1001 Remember that on the way out of executing a command, you have
1002 to directly use the mi_interp's uiout, since the command could
1003 have reset the interpreter, in which case the current uiout
1004 will most likely crash in the mi_out_* routines. */
1005 if (!running_result_record_printed
)
1007 fputs_unfiltered (context
->token
, raw_stdout
);
1008 /* There's no particularly good reason why target-connect results
1009 in not ^done. Should kill ^connected for MI3. */
1010 fputs_unfiltered (strcmp (context
->command
, "target-select") == 0
1011 ? "^connected" : "^done", raw_stdout
);
1012 mi_out_put (uiout
, raw_stdout
);
1013 mi_out_rewind (uiout
);
1014 /* Have to check cmd_start, since the command could be
1016 if (do_timings
&& context
->cmd_start
)
1017 print_diff (context
->cmd_start
, &cmd_finished
);
1018 fputs_unfiltered ("\n", raw_stdout
);
1021 /* The command does not want anything to be printed. In that
1022 case, the command probably should not have written anything
1023 to uiout, but in case it has written something, discard it. */
1024 mi_out_rewind (uiout
);
1030 /* A CLI command was read from the input stream. */
1031 /* This "feature" will be removed as soon as we have a
1032 complete set of mi commands. */
1033 /* Echo the command on the console. */
1034 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1035 /* Call the "console" interpreter. */
1036 argv
[0] = "console";
1037 argv
[1] = context
->command
;
1038 mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
1040 /* If we changed interpreters, DON'T print out anything. */
1041 if (current_interp_named_p (INTERP_MI
)
1042 || current_interp_named_p (INTERP_MI1
)
1043 || current_interp_named_p (INTERP_MI2
)
1044 || current_interp_named_p (INTERP_MI3
))
1046 if (!running_result_record_printed
)
1048 fputs_unfiltered (context
->token
, raw_stdout
);
1049 fputs_unfiltered ("^done", raw_stdout
);
1050 mi_out_put (uiout
, raw_stdout
);
1051 mi_out_rewind (uiout
);
1052 fputs_unfiltered ("\n", raw_stdout
);
1055 mi_out_rewind (uiout
);
1067 mi_execute_command (char *cmd
, int from_tty
)
1069 struct mi_parse
*command
;
1070 struct ui_out
*saved_uiout
= uiout
;
1072 /* This is to handle EOF (^D). We just quit gdb. */
1073 /* FIXME: we should call some API function here. */
1075 quit_force (NULL
, from_tty
);
1077 command
= mi_parse (cmd
);
1079 if (command
!= NULL
)
1081 struct gdb_exception result
;
1085 command
->cmd_start
= (struct mi_timestamp
*)
1086 xmalloc (sizeof (struct mi_timestamp
));
1087 timestamp (command
->cmd_start
);
1090 result
= catch_exception (uiout
, captured_mi_execute_command
, command
,
1092 if (result
.reason
< 0)
1094 /* The command execution failed and error() was called
1096 fputs_unfiltered (command
->token
, raw_stdout
);
1097 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1098 if (result
.message
== NULL
)
1099 fputs_unfiltered ("unknown error", raw_stdout
);
1101 fputstr_unfiltered (result
.message
, '"', raw_stdout
);
1102 fputs_unfiltered ("\"\n", raw_stdout
);
1103 mi_out_rewind (uiout
);
1106 mi_parse_free (command
);
1109 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1110 gdb_flush (raw_stdout
);
1111 /* Print any buffered hook code. */
1116 mi_cmd_execute (struct mi_parse
*parse
)
1118 struct cleanup
*cleanup
;
1121 if (parse
->cmd
->argv_func
!= NULL
)
1123 if (target_executing
)
1125 if (strcmp (parse
->command
, "exec-interrupt"))
1127 struct ui_file
*stb
;
1128 stb
= mem_fileopen ();
1130 fputs_unfiltered ("Cannot execute command ", stb
);
1131 fputstr_unfiltered (parse
->command
, '"', stb
);
1132 fputs_unfiltered (" while target running", stb
);
1134 make_cleanup_ui_file_delete (stb
);
1138 current_token
= xstrdup (parse
->token
);
1139 cleanup
= make_cleanup (free_current_contents
, ¤t_token
);
1140 parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1141 do_cleanups (cleanup
);
1143 else if (parse
->cmd
->cli
.cmd
!= 0)
1145 /* FIXME: DELETE THIS. */
1146 /* The operation is still implemented by a cli command. */
1147 /* Must be a synchronous one. */
1148 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1153 /* FIXME: DELETE THIS. */
1154 struct ui_file
*stb
;
1156 stb
= mem_fileopen ();
1158 fputs_unfiltered ("Undefined mi command: ", stb
);
1159 fputstr_unfiltered (parse
->command
, '"', stb
);
1160 fputs_unfiltered (" (missing implementation)", stb
);
1162 make_cleanup_ui_file_delete (stb
);
1167 /* FIXME: This is just a hack so we can get some extra commands going.
1168 We don't want to channel things through the CLI, but call libgdb directly.
1169 Use only for synchronous commands. */
1172 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1176 struct cleanup
*old_cleanups
;
1179 run
= xstrprintf ("%s %s", cmd
, args
);
1181 run
= xstrdup (cmd
);
1183 /* FIXME: gdb_???? */
1184 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1186 old_cleanups
= make_cleanup (xfree
, run
);
1187 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1188 do_cleanups (old_cleanups
);
1194 mi_execute_async_cli_command (char *cli_command
, char **argv
, int argc
)
1196 struct cleanup
*old_cleanups
;
1199 if (target_can_async_p ())
1200 run
= xstrprintf ("%s %s&", cli_command
, argc
? *argv
: "");
1202 run
= xstrprintf ("%s %s", cli_command
, argc
? *argv
: "");
1203 old_cleanups
= make_cleanup (xfree
, run
);
1205 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1207 if (target_can_async_p ())
1209 /* If we're not executing, an exception should have been throw. */
1210 gdb_assert (target_executing
);
1211 do_cleanups (old_cleanups
);
1215 /* Do this before doing any printing. It would appear that some
1216 print code leaves garbage around in the buffer. */
1217 do_cleanups (old_cleanups
);
1219 print_diff_now (current_command_ts
);
1224 mi_load_progress (const char *section_name
,
1225 unsigned long sent_so_far
,
1226 unsigned long total_section
,
1227 unsigned long total_sent
,
1228 unsigned long grand_total
)
1230 struct timeval time_now
, delta
, update_threshold
;
1231 static struct timeval last_update
;
1232 static char *previous_sect_name
= NULL
;
1234 struct ui_out
*saved_uiout
;
1236 /* This function is called through deprecated_show_load_progress
1237 which means uiout may not be correct. Fix it for the duration
1238 of this function. */
1239 saved_uiout
= uiout
;
1241 if (current_interp_named_p (INTERP_MI
)
1242 || current_interp_named_p (INTERP_MI2
))
1243 uiout
= mi_out_new (2);
1244 else if (current_interp_named_p (INTERP_MI1
))
1245 uiout
= mi_out_new (1);
1246 else if (current_interp_named_p (INTERP_MI3
))
1247 uiout
= mi_out_new (3);
1251 update_threshold
.tv_sec
= 0;
1252 update_threshold
.tv_usec
= 500000;
1253 gettimeofday (&time_now
, NULL
);
1255 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1256 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1258 if (delta
.tv_usec
< 0)
1261 delta
.tv_usec
+= 1000000L;
1264 new_section
= (previous_sect_name
?
1265 strcmp (previous_sect_name
, section_name
) : 1);
1268 struct cleanup
*cleanup_tuple
;
1269 xfree (previous_sect_name
);
1270 previous_sect_name
= xstrdup (section_name
);
1273 fputs_unfiltered (current_token
, raw_stdout
);
1274 fputs_unfiltered ("+download", raw_stdout
);
1275 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1276 ui_out_field_string (uiout
, "section", section_name
);
1277 ui_out_field_int (uiout
, "section-size", total_section
);
1278 ui_out_field_int (uiout
, "total-size", grand_total
);
1279 do_cleanups (cleanup_tuple
);
1280 mi_out_put (uiout
, raw_stdout
);
1281 fputs_unfiltered ("\n", raw_stdout
);
1282 gdb_flush (raw_stdout
);
1285 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1286 delta
.tv_usec
>= update_threshold
.tv_usec
)
1288 struct cleanup
*cleanup_tuple
;
1289 last_update
.tv_sec
= time_now
.tv_sec
;
1290 last_update
.tv_usec
= time_now
.tv_usec
;
1292 fputs_unfiltered (current_token
, raw_stdout
);
1293 fputs_unfiltered ("+download", raw_stdout
);
1294 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1295 ui_out_field_string (uiout
, "section", section_name
);
1296 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1297 ui_out_field_int (uiout
, "section-size", total_section
);
1298 ui_out_field_int (uiout
, "total-sent", total_sent
);
1299 ui_out_field_int (uiout
, "total-size", grand_total
);
1300 do_cleanups (cleanup_tuple
);
1301 mi_out_put (uiout
, raw_stdout
);
1302 fputs_unfiltered ("\n", raw_stdout
);
1303 gdb_flush (raw_stdout
);
1307 uiout
= saved_uiout
;
1311 timestamp (struct mi_timestamp
*tv
)
1314 gettimeofday (&tv
->wallclock
, NULL
);
1315 #ifdef HAVE_GETRUSAGE
1316 getrusage (RUSAGE_SELF
, &rusage
);
1317 tv
->utime
.tv_sec
= rusage
.ru_utime
.tv_sec
;
1318 tv
->utime
.tv_usec
= rusage
.ru_utime
.tv_usec
;
1319 tv
->stime
.tv_sec
= rusage
.ru_stime
.tv_sec
;
1320 tv
->stime
.tv_usec
= rusage
.ru_stime
.tv_usec
;
1322 usec
= get_run_time ();
1323 tv
->utime
.tv_sec
= usec
/1000000L;
1324 tv
->utime
.tv_usec
= usec
- 1000000L*tv
->utime
.tv_sec
;
1325 tv
->stime
.tv_sec
= 0;
1326 tv
->stime
.tv_usec
= 0;
1331 print_diff_now (struct mi_timestamp
*start
)
1333 struct mi_timestamp now
;
1335 print_diff (start
, &now
);
1339 timeval_diff (struct timeval start
, struct timeval end
)
1341 return ((end
.tv_sec
- start
.tv_sec
) * 1000000L)
1342 + (end
.tv_usec
- start
.tv_usec
);
1346 print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
)
1350 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1351 timeval_diff (start
->wallclock
, end
->wallclock
) / 1000000.0,
1352 timeval_diff (start
->utime
, end
->utime
) / 1000000.0,
1353 timeval_diff (start
->stime
, end
->stime
) / 1000000.0);