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>
65 /* Enumerations of the actions that may result from calling
66 captured_mi_execute_command. */
68 enum captured_mi_execute_command_actions
70 EXECUTE_COMMAND_DISPLAY_PROMPT
,
71 EXECUTE_COMMAND_SUPPRESS_PROMPT
74 /* This structure is used to pass information from captured_mi_execute_command
75 to mi_execute_command. */
76 struct captured_mi_execute_command_args
78 /* What action to perform when the call is finished (output). */
79 enum captured_mi_execute_command_actions action
;
81 /* The command context to be executed (input). */
82 struct mi_parse
*command
;
86 struct ui_file
*raw_stdout
;
88 /* This is used to pass the current command timestamp
89 down to continuation routines. */
90 static struct mi_timestamp
*current_command_ts
;
92 static int do_timings
= 0;
95 int running_result_record_printed
= 1;
97 extern void _initialize_mi_main (void);
98 static void mi_cmd_execute (struct mi_parse
*parse
);
100 static void mi_execute_cli_command (const char *cmd
, int args_p
,
102 static void mi_execute_async_cli_command (char *cli_command
,
103 char **argv
, int argc
);
104 static int register_changed_p (int regnum
, struct regcache
*,
106 static void get_register (int regnum
, int format
);
108 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
109 layer that calls libgdb. Any operation used in the below should be
112 static void timestamp (struct mi_timestamp
*tv
);
114 static void print_diff_now (struct mi_timestamp
*start
);
115 static void print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
);
118 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
120 /* We have to print everything right here because we never return. */
122 fputs_unfiltered (current_token
, raw_stdout
);
123 fputs_unfiltered ("^exit\n", raw_stdout
);
124 mi_out_put (uiout
, raw_stdout
);
125 /* FIXME: The function called is not yet a formal libgdb function. */
126 quit_force (NULL
, FROM_TTY
);
130 mi_cmd_exec_run (char *command
, char **argv
, int argc
)
132 /* FIXME: Should call a libgdb function, not a cli wrapper. */
133 return mi_execute_async_cli_command ("run", argv
, argc
);
137 mi_cmd_exec_next (char *command
, char **argv
, int argc
)
139 /* FIXME: Should call a libgdb function, not a cli wrapper. */
140 return mi_execute_async_cli_command ("next", argv
, argc
);
144 mi_cmd_exec_next_instruction (char *command
, char **argv
, int argc
)
146 /* FIXME: Should call a libgdb function, not a cli wrapper. */
147 return mi_execute_async_cli_command ("nexti", argv
, argc
);
151 mi_cmd_exec_step (char *command
, char **argv
, int argc
)
153 /* FIXME: Should call a libgdb function, not a cli wrapper. */
154 return mi_execute_async_cli_command ("step", argv
, argc
);
158 mi_cmd_exec_step_instruction (char *command
, char **argv
, int argc
)
160 /* FIXME: Should call a libgdb function, not a cli wrapper. */
161 return mi_execute_async_cli_command ("stepi", argv
, argc
);
165 mi_cmd_exec_finish (char *command
, char **argv
, int argc
)
167 /* FIXME: Should call a libgdb function, not a cli wrapper. */
168 return mi_execute_async_cli_command ("finish", argv
, argc
);
172 mi_cmd_exec_until (char *command
, char **argv
, int argc
)
174 /* FIXME: Should call a libgdb function, not a cli wrapper. */
175 return mi_execute_async_cli_command ("until", argv
, argc
);
179 mi_cmd_exec_return (char *command
, char **argv
, int argc
)
181 /* This command doesn't really execute the target, it just pops the
182 specified number of frames. */
184 /* Call return_command with from_tty argument equal to 0 so as to
185 avoid being queried. */
186 return_command (*argv
, 0);
188 /* Call return_command with from_tty argument equal to 0 so as to
189 avoid being queried. */
190 return_command (NULL
, 0);
192 /* Because we have called return_command with from_tty = 0, we need
193 to print the frame here. */
194 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
);
198 mi_cmd_exec_continue (char *command
, char **argv
, int argc
)
200 /* FIXME: Should call a libgdb function, not a cli wrapper. */
201 return mi_execute_async_cli_command ("continue", argv
, argc
);
204 /* Interrupt the execution of the target. Note how we must play around
205 with the token variables, in order to display the current token in
206 the result of the interrupt command, and the previous execution
207 token when the target finally stops. See comments in
210 mi_cmd_exec_interrupt (char *command
, char **argv
, int argc
)
212 if (!target_executing
)
213 error ("mi_cmd_exec_interrupt: Inferior not executing.");
215 interrupt_target_command (NULL
, 0);
219 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
222 char *mi_error_message
;
225 error ("mi_cmd_thread_select: USAGE: threadnum.");
227 rc
= gdb_thread_select (uiout
, argv
[0], &mi_error_message
);
229 if (rc
== GDB_RC_FAIL
)
231 make_cleanup (xfree
, mi_error_message
);
232 error ("%s", mi_error_message
);
237 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
240 char *mi_error_message
;
243 error ("mi_cmd_thread_list_ids: No arguments required.");
245 rc
= gdb_list_thread_ids (uiout
, &mi_error_message
);
247 if (rc
== GDB_RC_FAIL
)
249 make_cleanup (xfree
, mi_error_message
);
250 error ("%s", mi_error_message
);
255 mi_cmd_thread_info (char *command
, char **argv
, int argc
)
259 if (argc
!= 0 && argc
!= 1)
260 error ("Invalid MI command");
263 thread
= atoi (argv
[0]);
265 print_thread_info (uiout
, thread
);
269 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
273 struct cleanup
*cleanup
;
275 /* Note that the test for a valid register must include checking the
276 gdbarch_register_name because gdbarch_num_regs may be allocated for
277 the union of the register sets within a family of related processors.
278 In this case, some entries of gdbarch_register_name will change depending
279 upon the particular processor being debugged. */
281 numregs
= gdbarch_num_regs (current_gdbarch
)
282 + gdbarch_num_pseudo_regs (current_gdbarch
);
284 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
286 if (argc
== 0) /* No args, just do all the regs. */
292 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
293 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
294 ui_out_field_string (uiout
, NULL
, "");
296 ui_out_field_string (uiout
, NULL
,
297 gdbarch_register_name
298 (current_gdbarch
, regnum
));
302 /* Else, list of register #s, just do listed regs. */
303 for (i
= 0; i
< argc
; i
++)
305 regnum
= atoi (argv
[i
]);
306 if (regnum
< 0 || regnum
>= numregs
)
307 error ("bad register number");
309 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
310 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
311 ui_out_field_string (uiout
, NULL
, "");
313 ui_out_field_string (uiout
, NULL
,
314 gdbarch_register_name (current_gdbarch
, regnum
));
316 do_cleanups (cleanup
);
320 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
322 static struct regcache
*this_regs
= NULL
;
323 struct regcache
*prev_regs
;
324 int regnum
, numregs
, changed
;
326 struct cleanup
*cleanup
;
328 /* The last time we visited this function, the current frame's register
329 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
330 and refresh THIS_REGS with the now-current register contents. */
332 prev_regs
= this_regs
;
333 this_regs
= frame_save_as_regcache (get_selected_frame (NULL
));
334 cleanup
= make_cleanup_regcache_xfree (prev_regs
);
336 /* Note that the test for a valid register must include checking the
337 gdbarch_register_name because gdbarch_num_regs may be allocated for
338 the union of the register sets within a family of related processors.
339 In this case, some entries of gdbarch_register_name will change depending
340 upon the particular processor being debugged. */
342 numregs
= gdbarch_num_regs (current_gdbarch
)
343 + gdbarch_num_pseudo_regs (current_gdbarch
);
345 make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
347 if (argc
== 0) /* No args, just do all the regs. */
353 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
354 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
356 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
358 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
360 ui_out_field_int (uiout
, NULL
, regnum
);
364 /* Else, list of register #s, just do listed regs. */
365 for (i
= 0; i
< argc
; i
++)
367 regnum
= atoi (argv
[i
]);
371 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
372 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
374 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
376 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
378 ui_out_field_int (uiout
, NULL
, regnum
);
381 error ("bad register number");
383 do_cleanups (cleanup
);
387 register_changed_p (int regnum
, struct regcache
*prev_regs
,
388 struct regcache
*this_regs
)
390 struct gdbarch
*gdbarch
= get_regcache_arch (this_regs
);
391 gdb_byte prev_buffer
[MAX_REGISTER_SIZE
];
392 gdb_byte this_buffer
[MAX_REGISTER_SIZE
];
394 /* Registers not valid in this frame return count as unchanged. */
395 if (!regcache_valid_p (this_regs
, regnum
))
398 /* First time through or after gdbarch change consider all registers as
399 changed. Same for registers not valid in the previous frame. */
400 if (!prev_regs
|| get_regcache_arch (prev_regs
) != gdbarch
401 || !regcache_valid_p (prev_regs
, regnum
))
404 /* Get register contents and compare. */
405 regcache_cooked_read (prev_regs
, regnum
, prev_buffer
);
406 regcache_cooked_read (this_regs
, regnum
, this_buffer
);
408 return memcmp (prev_buffer
, this_buffer
,
409 register_size (gdbarch
, regnum
)) != 0;
412 /* Return a list of register number and value pairs. The valid
413 arguments expected are: a letter indicating the format in which to
414 display the registers contents. This can be one of: x (hexadecimal), d
415 (decimal), N (natural), t (binary), o (octal), r (raw). After the
416 format argumetn there can be a sequence of numbers, indicating which
417 registers to fetch the content of. If the format is the only argument,
418 a list of all the registers with their values is returned. */
420 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
422 int regnum
, numregs
, format
;
424 struct cleanup
*list_cleanup
, *tuple_cleanup
;
426 /* Note that the test for a valid register must include checking the
427 gdbarch_register_name because gdbarch_num_regs may be allocated for
428 the union of the register sets within a family of related processors.
429 In this case, some entries of gdbarch_register_name will change depending
430 upon the particular processor being debugged. */
432 numregs
= gdbarch_num_regs (current_gdbarch
)
433 + gdbarch_num_pseudo_regs (current_gdbarch
);
436 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
438 format
= (int) argv
[0][0];
440 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
442 if (argc
== 1) /* No args, beside the format: do all the regs. */
448 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
449 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
451 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
452 ui_out_field_int (uiout
, "number", regnum
);
453 get_register (regnum
, format
);
454 do_cleanups (tuple_cleanup
);
458 /* Else, list of register #s, just do listed regs. */
459 for (i
= 1; i
< argc
; i
++)
461 regnum
= atoi (argv
[i
]);
465 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
466 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
468 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
469 ui_out_field_int (uiout
, "number", regnum
);
470 get_register (regnum
, format
);
471 do_cleanups (tuple_cleanup
);
474 error ("bad register number");
476 do_cleanups (list_cleanup
);
479 /* Output one register's contents in the desired format. */
481 get_register (int regnum
, int format
)
483 gdb_byte buffer
[MAX_REGISTER_SIZE
];
488 static struct ui_stream
*stb
= NULL
;
490 stb
= ui_out_stream_new (uiout
);
495 frame_register (get_selected_frame (NULL
), regnum
, &optim
, &lval
, &addr
,
499 error ("Optimized out");
504 char *ptr
, buf
[1024];
508 for (j
= 0; j
< register_size (current_gdbarch
, regnum
); j
++)
510 int idx
= gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
? j
511 : register_size (current_gdbarch
, regnum
) - 1 - j
;
512 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
515 ui_out_field_string (uiout
, "value", buf
);
516 /*fputs_filtered (buf, gdb_stdout); */
520 val_print (register_type (current_gdbarch
, regnum
), buffer
, 0, 0,
521 stb
->stream
, format
, 1, 0, Val_pretty_default
,
523 ui_out_field_stream (uiout
, "value", stb
);
524 ui_out_stream_delete (stb
);
528 /* Write given values into registers. The registers and values are
529 given as pairs. The corresponding MI command is
530 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
532 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
537 /* Note that the test for a valid register must include checking the
538 gdbarch_register_name because gdbarch_num_regs may be allocated for
539 the union of the register sets within a family of related processors.
540 In this case, some entries of gdbarch_register_name will change depending
541 upon the particular processor being debugged. */
543 numregs
= gdbarch_num_regs (current_gdbarch
)
544 + gdbarch_num_pseudo_regs (current_gdbarch
);
547 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
549 format
= (int) argv
[0][0];
551 if (!target_has_registers
)
552 error ("mi_cmd_data_write_register_values: No registers.");
555 error ("mi_cmd_data_write_register_values: No regs and values specified.");
558 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
560 for (i
= 1; i
< argc
; i
= i
+ 2)
562 int regnum
= atoi (argv
[i
]);
564 if (regnum
>= 0 && regnum
< numregs
565 && gdbarch_register_name (current_gdbarch
, regnum
)
566 && *gdbarch_register_name (current_gdbarch
, regnum
))
570 /* Get the value as a number. */
571 value
= parse_and_eval_address (argv
[i
+ 1]);
574 regcache_cooked_write_signed (get_current_regcache (), regnum
, value
);
577 error ("bad register number");
581 /* Evaluate the value of the argument. The argument is an
582 expression. If the expression contains spaces it needs to be
583 included in double quotes. */
585 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
587 struct expression
*expr
;
588 struct cleanup
*old_chain
= NULL
;
590 struct ui_stream
*stb
= NULL
;
592 stb
= ui_out_stream_new (uiout
);
596 ui_out_stream_delete (stb
);
597 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
600 expr
= parse_expression (argv
[0]);
602 old_chain
= make_cleanup (free_current_contents
, &expr
);
604 val
= evaluate_expression (expr
);
606 /* Print the result of the expression evaluation. */
607 val_print (value_type (val
), value_contents (val
),
608 value_embedded_offset (val
), VALUE_ADDRESS (val
),
609 stb
->stream
, 0, 0, 0, 0, current_language
);
611 ui_out_field_stream (uiout
, "value", stb
);
612 ui_out_stream_delete (stb
);
614 do_cleanups (old_chain
);
618 mi_cmd_target_download (char *command
, char **argv
, int argc
)
621 struct cleanup
*old_cleanups
= NULL
;
623 /* There may be at most one parameter -- the name of the
625 run
= xstrprintf ("load %s", argc
? *argv
: "");
626 old_cleanups
= make_cleanup (xfree
, run
);
627 execute_command (run
, 0);
629 do_cleanups (old_cleanups
);
632 /* Connect to the remote target. */
634 mi_cmd_target_select (char *command
, char **argv
, int argc
)
637 struct cleanup
*old_cleanups
= NULL
;
641 error ("no target type specified");
643 for (i
= 0; i
< argc
; ++i
)
646 run
= concat ("target ", argv
[0], NULL
);
650 run
= concat (run
, " ", argv
[i
], NULL
);
655 old_cleanups
= make_cleanup (xfree
, run
);
657 /* target-select is always synchronous. Once the call has returned
658 we know that we are connected. */
659 /* NOTE: At present all targets that are connected are also
660 (implicitly) talking to a halted target. In the future this may
662 execute_command (run
, 0);
664 do_cleanups (old_cleanups
);
669 ADDR: start address of data to be dumped.
670 WORD-FORMAT: a char indicating format for the ``word''. See
672 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
673 NR_ROW: Number of rows.
674 NR_COL: The number of colums (words per row).
675 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
676 ASCHAR for unprintable characters.
678 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
679 displayes them. Returns:
681 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
684 The number of bytes read is SIZE*ROW*COL. */
687 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
689 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
695 struct type
*word_type
;
708 static struct mi_opt opts
[] =
710 {"o", OFFSET_OPT
, 1},
716 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
720 switch ((enum opt
) opt
)
723 offset
= atol (optarg
);
730 if (argc
< 5 || argc
> 6)
731 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
733 /* Extract all the arguments. */
735 /* Start address of the memory dump. */
736 addr
= parse_and_eval_address (argv
[0]) + offset
;
737 /* The format character to use when displaying a memory word. See
738 the ``x'' command. */
739 word_format
= argv
[1][0];
740 /* The size of the memory word. */
741 word_size
= atol (argv
[2]);
745 word_type
= builtin_type_int8
;
749 word_type
= builtin_type_int16
;
753 word_type
= builtin_type_int32
;
757 word_type
= builtin_type_int64
;
761 word_type
= builtin_type_int8
;
764 /* The number of rows. */
765 nr_rows
= atol (argv
[3]);
767 error ("mi_cmd_data_read_memory: invalid number of rows.");
769 /* Number of bytes per row. */
770 nr_cols
= atol (argv
[4]);
772 error ("mi_cmd_data_read_memory: invalid number of columns.");
774 /* The un-printable character when printing ascii. */
780 /* Create a buffer and read it in. */
781 total_bytes
= word_size
* nr_rows
* nr_cols
;
782 mbuf
= xcalloc (total_bytes
, 1);
783 make_cleanup (xfree
, mbuf
);
785 nr_bytes
= target_read (¤t_target
, TARGET_OBJECT_MEMORY
, NULL
,
786 mbuf
, addr
, total_bytes
);
788 error ("Unable to read memory.");
790 /* Output the header information. */
791 ui_out_field_core_addr (uiout
, "addr", addr
);
792 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
793 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
794 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
795 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
796 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
797 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
799 /* Build the result as a two dimentional table. */
801 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
802 struct cleanup
*cleanup_list_memory
;
805 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
806 for (row
= 0, row_byte
= 0;
808 row
++, row_byte
+= nr_cols
* word_size
)
812 struct cleanup
*cleanup_tuple
;
813 struct cleanup
*cleanup_list_data
;
814 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
815 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
816 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
817 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
818 for (col
= 0, col_byte
= row_byte
;
820 col
++, col_byte
+= word_size
)
822 if (col_byte
+ word_size
> nr_bytes
)
824 ui_out_field_string (uiout
, NULL
, "N/A");
828 ui_file_rewind (stream
->stream
);
829 print_scalar_formatted (mbuf
+ col_byte
, word_type
, word_format
,
830 word_asize
, stream
->stream
);
831 ui_out_field_stream (uiout
, NULL
, stream
);
834 do_cleanups (cleanup_list_data
);
838 ui_file_rewind (stream
->stream
);
839 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
841 if (byte
>= nr_bytes
)
843 fputc_unfiltered ('X', stream
->stream
);
845 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
847 fputc_unfiltered (aschar
, stream
->stream
);
850 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
852 ui_out_field_stream (uiout
, "ascii", stream
);
854 do_cleanups (cleanup_tuple
);
856 ui_out_stream_delete (stream
);
857 do_cleanups (cleanup_list_memory
);
859 do_cleanups (cleanups
);
862 /* DATA-MEMORY-WRITE:
864 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
865 offset from the beginning of the memory grid row where the cell to
867 ADDR: start address of the row in the memory grid where the memory
868 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
869 the location to write to.
870 FORMAT: a char indicating format for the ``word''. See
872 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
873 VALUE: value to be written into the memory address.
875 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
879 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
884 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
885 enough when using a compiler other than GCC. */
888 struct cleanup
*old_chain
;
896 static struct mi_opt opts
[] =
898 {"o", OFFSET_OPT
, 1},
904 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
908 switch ((enum opt
) opt
)
911 offset
= atol (optarg
);
919 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
921 /* Extract all the arguments. */
922 /* Start address of the memory dump. */
923 addr
= parse_and_eval_address (argv
[0]);
924 /* The format character to use when displaying a memory word. See
925 the ``x'' command. */
926 word_format
= argv
[1][0];
927 /* The size of the memory word. */
928 word_size
= atol (argv
[2]);
930 /* Calculate the real address of the write destination. */
931 addr
+= (offset
* word_size
);
933 /* Get the value as a number. */
934 value
= parse_and_eval_address (argv
[3]);
935 /* Get the value into an array. */
936 buffer
= xmalloc (word_size
);
937 old_chain
= make_cleanup (xfree
, buffer
);
938 store_signed_integer (buffer
, word_size
, value
);
939 /* Write it down to memory. */
940 write_memory (addr
, buffer
, word_size
);
941 /* Free the buffer. */
942 do_cleanups (old_chain
);
946 mi_cmd_enable_timings (char *command
, char **argv
, int argc
)
952 if (strcmp (argv
[0], "yes") == 0)
954 else if (strcmp (argv
[0], "no") == 0)
965 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command
);
969 mi_cmd_list_features (char *command
, char **argv
, int argc
)
973 struct cleanup
*cleanup
= NULL
;
974 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
976 ui_out_field_string (uiout
, NULL
, "frozen-varobjs");
977 ui_out_field_string (uiout
, NULL
, "pending-breakpoints");
978 ui_out_field_string (uiout
, NULL
, "thread-info");
980 do_cleanups (cleanup
);
984 error ("-list-features should be passed no arguments");
987 /* Execute a command within a safe environment.
988 Return <0 for error; >=0 for ok.
990 args->action will tell mi_execute_command what action
991 to perfrom after the given command has executed (display/suppress
992 prompt, display error). */
995 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
997 struct captured_mi_execute_command_args
*args
=
998 (struct captured_mi_execute_command_args
*) data
;
999 struct mi_parse
*context
= args
->command
;
1001 struct mi_timestamp cmd_finished
;
1003 running_result_record_printed
= 0;
1004 switch (context
->op
)
1007 /* A MI command was read from the input stream. */
1009 /* FIXME: gdb_???? */
1010 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1011 context
->token
, context
->command
, context
->args
);
1012 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1013 condition expression, each function should return an
1014 indication of what action is required and then switch on
1016 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1019 current_command_ts
= context
->cmd_start
;
1021 mi_cmd_execute (context
);
1024 timestamp (&cmd_finished
);
1026 /* Print the result if there were no errors.
1028 Remember that on the way out of executing a command, you have
1029 to directly use the mi_interp's uiout, since the command could
1030 have reset the interpreter, in which case the current uiout
1031 will most likely crash in the mi_out_* routines. */
1032 if (!running_result_record_printed
)
1034 fputs_unfiltered (context
->token
, raw_stdout
);
1035 /* There's no particularly good reason why target-connect results
1036 in not ^done. Should kill ^connected for MI3. */
1037 fputs_unfiltered (strcmp (context
->command
, "target-select") == 0
1038 ? "^connected" : "^done", raw_stdout
);
1039 mi_out_put (uiout
, raw_stdout
);
1040 mi_out_rewind (uiout
);
1041 /* Have to check cmd_start, since the command could be
1043 if (do_timings
&& context
->cmd_start
)
1044 print_diff (context
->cmd_start
, &cmd_finished
);
1045 fputs_unfiltered ("\n", raw_stdout
);
1048 /* The command does not want anything to be printed. In that
1049 case, the command probably should not have written anything
1050 to uiout, but in case it has written something, discard it. */
1051 mi_out_rewind (uiout
);
1057 /* A CLI command was read from the input stream. */
1058 /* This "feature" will be removed as soon as we have a
1059 complete set of mi commands. */
1060 /* Echo the command on the console. */
1061 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1062 /* Call the "console" interpreter. */
1063 argv
[0] = "console";
1064 argv
[1] = context
->command
;
1065 mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
1067 /* If we changed interpreters, DON'T print out anything. */
1068 if (current_interp_named_p (INTERP_MI
)
1069 || current_interp_named_p (INTERP_MI1
)
1070 || current_interp_named_p (INTERP_MI2
)
1071 || current_interp_named_p (INTERP_MI3
))
1073 if (!running_result_record_printed
)
1075 fputs_unfiltered (context
->token
, raw_stdout
);
1076 fputs_unfiltered ("^done", raw_stdout
);
1077 mi_out_put (uiout
, raw_stdout
);
1078 mi_out_rewind (uiout
);
1079 fputs_unfiltered ("\n", raw_stdout
);
1080 args
->action
= EXECUTE_COMMAND_DISPLAY_PROMPT
;
1083 mi_out_rewind (uiout
);
1095 mi_execute_command (char *cmd
, int from_tty
)
1097 struct mi_parse
*command
;
1098 struct captured_mi_execute_command_args args
;
1099 struct ui_out
*saved_uiout
= uiout
;
1101 /* This is to handle EOF (^D). We just quit gdb. */
1102 /* FIXME: we should call some API function here. */
1104 quit_force (NULL
, from_tty
);
1106 command
= mi_parse (cmd
);
1108 if (command
!= NULL
)
1110 struct gdb_exception result
;
1114 command
->cmd_start
= (struct mi_timestamp
*)
1115 xmalloc (sizeof (struct mi_timestamp
));
1116 timestamp (command
->cmd_start
);
1119 args
.command
= command
;
1120 result
= catch_exception (uiout
, captured_mi_execute_command
, &args
,
1122 if (result
.reason
< 0)
1124 /* The command execution failed and error() was called
1126 fputs_unfiltered (command
->token
, raw_stdout
);
1127 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1128 if (result
.message
== NULL
)
1129 fputs_unfiltered ("unknown error", raw_stdout
);
1131 fputstr_unfiltered (result
.message
, '"', raw_stdout
);
1132 fputs_unfiltered ("\"\n", raw_stdout
);
1133 mi_out_rewind (uiout
);
1136 mi_parse_free (command
);
1138 if (args
.action
== EXECUTE_COMMAND_SUPPRESS_PROMPT
)
1139 /* The command is executing synchronously. Bail out early
1140 suppressing the finished prompt. */
1144 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1145 gdb_flush (raw_stdout
);
1146 /* Print any buffered hook code. */
1151 mi_cmd_execute (struct mi_parse
*parse
)
1153 struct cleanup
*cleanup
;
1156 if (parse
->cmd
->argv_func
!= NULL
)
1158 if (target_executing
)
1160 if (strcmp (parse
->command
, "exec-interrupt"))
1162 struct ui_file
*stb
;
1163 stb
= mem_fileopen ();
1165 fputs_unfiltered ("Cannot execute command ", stb
);
1166 fputstr_unfiltered (parse
->command
, '"', stb
);
1167 fputs_unfiltered (" while target running", stb
);
1169 make_cleanup_ui_file_delete (stb
);
1173 current_token
= xstrdup (parse
->token
);
1174 cleanup
= make_cleanup (free_current_contents
, ¤t_token
);
1175 parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1176 do_cleanups (cleanup
);
1178 else if (parse
->cmd
->cli
.cmd
!= 0)
1180 /* FIXME: DELETE THIS. */
1181 /* The operation is still implemented by a cli command. */
1182 /* Must be a synchronous one. */
1183 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1188 /* FIXME: DELETE THIS. */
1189 struct ui_file
*stb
;
1191 stb
= mem_fileopen ();
1193 fputs_unfiltered ("Undefined mi command: ", stb
);
1194 fputstr_unfiltered (parse
->command
, '"', stb
);
1195 fputs_unfiltered (" (missing implementation)", stb
);
1197 make_cleanup_ui_file_delete (stb
);
1202 /* FIXME: This is just a hack so we can get some extra commands going.
1203 We don't want to channel things through the CLI, but call libgdb directly.
1204 Use only for synchronous commands. */
1207 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1211 struct cleanup
*old_cleanups
;
1214 run
= xstrprintf ("%s %s", cmd
, args
);
1216 run
= xstrdup (cmd
);
1218 /* FIXME: gdb_???? */
1219 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1221 old_cleanups
= make_cleanup (xfree
, run
);
1222 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1223 do_cleanups (old_cleanups
);
1229 mi_execute_async_cli_command (char *cli_command
, char **argv
, int argc
)
1231 struct cleanup
*old_cleanups
;
1234 if (target_can_async_p ())
1235 run
= xstrprintf ("%s %s&", cli_command
, argc
? *argv
: "");
1237 run
= xstrprintf ("%s %s", cli_command
, argc
? *argv
: "");
1238 old_cleanups
= make_cleanup (xfree
, run
);
1240 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1242 if (target_can_async_p ())
1244 /* If we're not executing, an exception should have been throw. */
1245 gdb_assert (target_executing
);
1246 do_cleanups (old_cleanups
);
1250 /* Do this before doing any printing. It would appear that some
1251 print code leaves garbage around in the buffer. */
1252 do_cleanups (old_cleanups
);
1254 print_diff_now (current_command_ts
);
1259 mi_load_progress (const char *section_name
,
1260 unsigned long sent_so_far
,
1261 unsigned long total_section
,
1262 unsigned long total_sent
,
1263 unsigned long grand_total
)
1265 struct timeval time_now
, delta
, update_threshold
;
1266 static struct timeval last_update
;
1267 static char *previous_sect_name
= NULL
;
1269 struct ui_out
*saved_uiout
;
1271 /* This function is called through deprecated_show_load_progress
1272 which means uiout may not be correct. Fix it for the duration
1273 of this function. */
1274 saved_uiout
= uiout
;
1276 if (current_interp_named_p (INTERP_MI
)
1277 || current_interp_named_p (INTERP_MI2
))
1278 uiout
= mi_out_new (2);
1279 else if (current_interp_named_p (INTERP_MI1
))
1280 uiout
= mi_out_new (1);
1281 else if (current_interp_named_p (INTERP_MI3
))
1282 uiout
= mi_out_new (3);
1286 update_threshold
.tv_sec
= 0;
1287 update_threshold
.tv_usec
= 500000;
1288 gettimeofday (&time_now
, NULL
);
1290 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1291 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1293 if (delta
.tv_usec
< 0)
1296 delta
.tv_usec
+= 1000000L;
1299 new_section
= (previous_sect_name
?
1300 strcmp (previous_sect_name
, section_name
) : 1);
1303 struct cleanup
*cleanup_tuple
;
1304 xfree (previous_sect_name
);
1305 previous_sect_name
= xstrdup (section_name
);
1308 fputs_unfiltered (current_token
, raw_stdout
);
1309 fputs_unfiltered ("+download", raw_stdout
);
1310 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1311 ui_out_field_string (uiout
, "section", section_name
);
1312 ui_out_field_int (uiout
, "section-size", total_section
);
1313 ui_out_field_int (uiout
, "total-size", grand_total
);
1314 do_cleanups (cleanup_tuple
);
1315 mi_out_put (uiout
, raw_stdout
);
1316 fputs_unfiltered ("\n", raw_stdout
);
1317 gdb_flush (raw_stdout
);
1320 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1321 delta
.tv_usec
>= update_threshold
.tv_usec
)
1323 struct cleanup
*cleanup_tuple
;
1324 last_update
.tv_sec
= time_now
.tv_sec
;
1325 last_update
.tv_usec
= time_now
.tv_usec
;
1327 fputs_unfiltered (current_token
, raw_stdout
);
1328 fputs_unfiltered ("+download", raw_stdout
);
1329 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1330 ui_out_field_string (uiout
, "section", section_name
);
1331 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1332 ui_out_field_int (uiout
, "section-size", total_section
);
1333 ui_out_field_int (uiout
, "total-sent", total_sent
);
1334 ui_out_field_int (uiout
, "total-size", grand_total
);
1335 do_cleanups (cleanup_tuple
);
1336 mi_out_put (uiout
, raw_stdout
);
1337 fputs_unfiltered ("\n", raw_stdout
);
1338 gdb_flush (raw_stdout
);
1342 uiout
= saved_uiout
;
1346 timestamp (struct mi_timestamp
*tv
)
1349 gettimeofday (&tv
->wallclock
, NULL
);
1350 #ifdef HAVE_GETRUSAGE
1351 getrusage (RUSAGE_SELF
, &rusage
);
1352 tv
->utime
.tv_sec
= rusage
.ru_utime
.tv_sec
;
1353 tv
->utime
.tv_usec
= rusage
.ru_utime
.tv_usec
;
1354 tv
->stime
.tv_sec
= rusage
.ru_stime
.tv_sec
;
1355 tv
->stime
.tv_usec
= rusage
.ru_stime
.tv_usec
;
1357 usec
= get_run_time ();
1358 tv
->utime
.tv_sec
= usec
/1000000L;
1359 tv
->utime
.tv_usec
= usec
- 1000000L*tv
->utime
.tv_sec
;
1360 tv
->stime
.tv_sec
= 0;
1361 tv
->stime
.tv_usec
= 0;
1366 print_diff_now (struct mi_timestamp
*start
)
1368 struct mi_timestamp now
;
1370 print_diff (start
, &now
);
1374 timeval_diff (struct timeval start
, struct timeval end
)
1376 return ((end
.tv_sec
- start
.tv_sec
) * 1000000L)
1377 + (end
.tv_usec
- start
.tv_usec
);
1381 print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
)
1385 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1386 timeval_diff (start
->wallclock
, end
->wallclock
) / 1000000.0,
1387 timeval_diff (start
->utime
, end
->utime
) / 1000000.0,
1388 timeval_diff (start
->stime
, end
->stime
) / 1000000.0);