3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
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(). */
47 #include "mi-common.h"
56 #if defined HAVE_SYS_RESOURCE_H
57 #include <sys/resource.h>
70 struct ui_file
*raw_stdout
;
72 /* This is used to pass the current command timestamp
73 down to continuation routines. */
74 static struct mi_timestamp
*current_command_ts
;
76 static int do_timings
= 0;
79 int running_result_record_printed
= 1;
81 /* Flag indicating that the target has proceeded since the last
82 command was issued. */
85 extern void _initialize_mi_main (void);
86 static void mi_cmd_execute (struct mi_parse
*parse
);
88 static void mi_execute_cli_command (const char *cmd
, int args_p
,
90 static void mi_execute_async_cli_command (char *cli_command
,
91 char **argv
, int argc
);
92 static int register_changed_p (int regnum
, struct regcache
*,
94 static void get_register (int regnum
, int format
);
96 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
97 layer that calls libgdb. Any operation used in the below should be
100 static void timestamp (struct mi_timestamp
*tv
);
102 static void print_diff_now (struct mi_timestamp
*start
);
103 static void print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
);
106 mi_cmd_gdb_exit (char *command
, char **argv
, int argc
)
108 /* We have to print everything right here because we never return. */
110 fputs_unfiltered (current_token
, raw_stdout
);
111 fputs_unfiltered ("^exit\n", raw_stdout
);
112 mi_out_put (uiout
, raw_stdout
);
113 /* FIXME: The function called is not yet a formal libgdb function. */
114 quit_force (NULL
, FROM_TTY
);
118 mi_cmd_exec_next (char *command
, char **argv
, int argc
)
120 /* FIXME: Should call a libgdb function, not a cli wrapper. */
121 mi_execute_async_cli_command ("next", argv
, argc
);
125 mi_cmd_exec_next_instruction (char *command
, char **argv
, int argc
)
127 /* FIXME: Should call a libgdb function, not a cli wrapper. */
128 mi_execute_async_cli_command ("nexti", argv
, argc
);
132 mi_cmd_exec_step (char *command
, char **argv
, int argc
)
134 /* FIXME: Should call a libgdb function, not a cli wrapper. */
135 mi_execute_async_cli_command ("step", argv
, argc
);
139 mi_cmd_exec_step_instruction (char *command
, char **argv
, int argc
)
141 /* FIXME: Should call a libgdb function, not a cli wrapper. */
142 mi_execute_async_cli_command ("stepi", argv
, argc
);
146 mi_cmd_exec_finish (char *command
, char **argv
, int argc
)
148 /* FIXME: Should call a libgdb function, not a cli wrapper. */
149 mi_execute_async_cli_command ("finish", argv
, argc
);
153 mi_cmd_exec_return (char *command
, char **argv
, int argc
)
155 /* This command doesn't really execute the target, it just pops the
156 specified number of frames. */
158 /* Call return_command with from_tty argument equal to 0 so as to
159 avoid being queried. */
160 return_command (*argv
, 0);
162 /* Call return_command with from_tty argument equal to 0 so as to
163 avoid being queried. */
164 return_command (NULL
, 0);
166 /* Because we have called return_command with from_tty = 0, we need
167 to print the frame here. */
168 print_stack_frame (get_selected_frame (NULL
), 1, LOC_AND_ADDRESS
);
172 proceed_thread_callback (struct thread_info
*thread
, void *arg
)
174 int pid
= *(int *)arg
;
176 if (!is_stopped (thread
->ptid
))
179 if (PIDGET (thread
->ptid
) != pid
)
182 switch_to_thread (thread
->ptid
);
183 clear_proceed_status ();
184 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_DEFAULT
, 0);
189 mi_cmd_exec_continue (char *command
, char **argv
, int argc
)
193 else if (argc
== 1 && strcmp (argv
[0], "--all") == 0)
195 else if (argc
== 2 && strcmp (argv
[0], "--thread-group") == 0)
197 struct cleanup
*old_chain
;
199 if (argv
[1] == NULL
|| argv
[1] == '\0')
200 error ("Thread group id not specified");
201 pid
= atoi (argv
[1] + 1);
202 if (!in_inferior_list (pid
))
203 error ("Invalid thread group id '%s'", argv
[1]);
205 old_chain
= make_cleanup_restore_current_thread ();
206 iterate_over_threads (proceed_thread_callback
, &pid
);
207 do_cleanups (old_chain
);
210 error ("Usage: -exec-continue [--all|--thread-group id]");
214 interrupt_thread_callback (struct thread_info
*thread
, void *arg
)
216 int pid
= *(int *)arg
;
218 if (!is_running (thread
->ptid
))
221 if (PIDGET (thread
->ptid
) != pid
)
224 target_stop (thread
->ptid
);
228 /* Interrupt the execution of the target. Note how we must play around
229 with the token variables, in order to display the current token in
230 the result of the interrupt command, and the previous execution
231 token when the target finally stops. See comments in
234 mi_cmd_exec_interrupt (char *command
, char **argv
, int argc
)
238 if (!is_running (inferior_ptid
))
239 error ("Current thread is not running.");
241 interrupt_target_1 (0);
243 else if (argc
== 1 && strcmp (argv
[0], "--all") == 0)
246 error ("Inferior not running.");
248 interrupt_target_1 (1);
250 else if (argc
== 2 && strcmp (argv
[0], "--thread-group") == 0)
252 struct cleanup
*old_chain
;
254 if (argv
[1] == NULL
|| argv
[1] == '\0')
255 error ("Thread group id not specified");
256 pid
= atoi (argv
[1] + 1);
257 if (!in_inferior_list (pid
))
258 error ("Invalid thread group id '%s'", argv
[1]);
260 old_chain
= make_cleanup_restore_current_thread ();
261 iterate_over_threads (interrupt_thread_callback
, &pid
);
262 do_cleanups (old_chain
);
265 error ("Usage: -exec-interrupt [--all|--thread-group id]");
269 find_thread_of_process (struct thread_info
*ti
, void *p
)
272 if (PIDGET (ti
->ptid
) == pid
&& !is_exited (ti
->ptid
))
279 mi_cmd_target_detach (char *command
, char **argv
, int argc
)
281 if (argc
!= 0 && argc
!= 1)
282 error ("Usage: -target-detach [thread-group]");
286 struct thread_info
*tp
;
288 int pid
= strtol (argv
[0], &end
, 10);
290 error (_("Cannot parse thread group id '%s'"), argv
[0]);
292 /* Pick any thread in the desired process. Current
293 target_detach deteches from the parent of inferior_ptid. */
294 tp
= iterate_over_threads (find_thread_of_process
, &pid
);
296 error (_("Thread group is empty"));
298 switch_to_thread (tp
->ptid
);
301 detach_command (NULL
, 0);
305 mi_cmd_thread_select (char *command
, char **argv
, int argc
)
308 char *mi_error_message
;
311 error ("mi_cmd_thread_select: USAGE: threadnum.");
313 rc
= gdb_thread_select (uiout
, argv
[0], &mi_error_message
);
315 if (rc
== GDB_RC_FAIL
)
317 make_cleanup (xfree
, mi_error_message
);
318 error ("%s", mi_error_message
);
323 mi_cmd_thread_list_ids (char *command
, char **argv
, int argc
)
326 char *mi_error_message
;
329 error ("mi_cmd_thread_list_ids: No arguments required.");
331 rc
= gdb_list_thread_ids (uiout
, &mi_error_message
);
333 if (rc
== GDB_RC_FAIL
)
335 make_cleanup (xfree
, mi_error_message
);
336 error ("%s", mi_error_message
);
341 mi_cmd_thread_info (char *command
, char **argv
, int argc
)
345 if (argc
!= 0 && argc
!= 1)
346 error ("Invalid MI command");
349 thread
= atoi (argv
[0]);
351 print_thread_info (uiout
, thread
, -1);
355 print_one_inferior (struct inferior
*inferior
, void *arg
)
357 struct cleanup
*back_to
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
359 ui_out_field_fmt (uiout
, "id", "%d", inferior
->pid
);
360 ui_out_field_string (uiout
, "type", "process");
361 ui_out_field_int (uiout
, "pid", inferior
->pid
);
363 do_cleanups (back_to
);
368 mi_cmd_list_thread_groups (char *command
, char **argv
, int argc
)
370 struct cleanup
*back_to
;
374 if (argc
> 0 && strcmp (argv
[0], "--available") == 0)
384 back_to
= make_cleanup (null_cleanup
, NULL
);
388 error (_("Can only report top-level available thread groups"));
393 struct osdata_item
*item
;
396 data
= get_osdata ("processes");
397 make_cleanup_osdata_free (data
);
399 make_cleanup_ui_out_list_begin_end (uiout
, "groups");
402 VEC_iterate (osdata_item_s
, data
->items
,
406 struct cleanup
*back_to
=
407 make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
409 const char *pid
= get_osdata_column (item
, "pid");
410 const char *cmd
= get_osdata_column (item
, "command");
411 const char *user
= get_osdata_column (item
, "user");
413 ui_out_field_fmt (uiout
, "id", "%s", pid
);
414 ui_out_field_string (uiout
, "type", "process");
416 ui_out_field_string (uiout
, "description", cmd
);
418 ui_out_field_string (uiout
, "user", user
);
420 do_cleanups (back_to
);
426 if (!in_inferior_list (pid
))
427 error ("Invalid thread group id '%s'", id
);
428 print_thread_info (uiout
, -1, pid
);
432 make_cleanup_ui_out_list_begin_end (uiout
, "groups");
433 iterate_over_inferiors (print_one_inferior
, NULL
);
436 do_cleanups (back_to
);
440 mi_cmd_data_list_register_names (char *command
, char **argv
, int argc
)
444 struct cleanup
*cleanup
;
446 /* Note that the test for a valid register must include checking the
447 gdbarch_register_name because gdbarch_num_regs may be allocated for
448 the union of the register sets within a family of related processors.
449 In this case, some entries of gdbarch_register_name will change depending
450 upon the particular processor being debugged. */
452 numregs
= gdbarch_num_regs (current_gdbarch
)
453 + gdbarch_num_pseudo_regs (current_gdbarch
);
455 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-names");
457 if (argc
== 0) /* No args, just do all the regs. */
463 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
464 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
465 ui_out_field_string (uiout
, NULL
, "");
467 ui_out_field_string (uiout
, NULL
,
468 gdbarch_register_name
469 (current_gdbarch
, regnum
));
473 /* Else, list of register #s, just do listed regs. */
474 for (i
= 0; i
< argc
; i
++)
476 regnum
= atoi (argv
[i
]);
477 if (regnum
< 0 || regnum
>= numregs
)
478 error ("bad register number");
480 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
481 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
482 ui_out_field_string (uiout
, NULL
, "");
484 ui_out_field_string (uiout
, NULL
,
485 gdbarch_register_name (current_gdbarch
, regnum
));
487 do_cleanups (cleanup
);
491 mi_cmd_data_list_changed_registers (char *command
, char **argv
, int argc
)
493 static struct regcache
*this_regs
= NULL
;
494 struct regcache
*prev_regs
;
495 int regnum
, numregs
, changed
;
497 struct cleanup
*cleanup
;
499 /* The last time we visited this function, the current frame's register
500 contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS,
501 and refresh THIS_REGS with the now-current register contents. */
503 prev_regs
= this_regs
;
504 this_regs
= frame_save_as_regcache (get_selected_frame (NULL
));
505 cleanup
= make_cleanup_regcache_xfree (prev_regs
);
507 /* Note that the test for a valid register must include checking the
508 gdbarch_register_name because gdbarch_num_regs may be allocated for
509 the union of the register sets within a family of related processors.
510 In this case, some entries of gdbarch_register_name will change depending
511 upon the particular processor being debugged. */
513 numregs
= gdbarch_num_regs (current_gdbarch
)
514 + gdbarch_num_pseudo_regs (current_gdbarch
);
516 make_cleanup_ui_out_list_begin_end (uiout
, "changed-registers");
518 if (argc
== 0) /* No args, just do all the regs. */
524 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
525 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
527 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
529 error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
531 ui_out_field_int (uiout
, NULL
, regnum
);
535 /* Else, list of register #s, just do listed regs. */
536 for (i
= 0; i
< argc
; i
++)
538 regnum
= atoi (argv
[i
]);
542 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
543 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
545 changed
= register_changed_p (regnum
, prev_regs
, this_regs
);
547 error ("mi_cmd_data_list_register_change: Unable to read register contents.");
549 ui_out_field_int (uiout
, NULL
, regnum
);
552 error ("bad register number");
554 do_cleanups (cleanup
);
558 register_changed_p (int regnum
, struct regcache
*prev_regs
,
559 struct regcache
*this_regs
)
561 struct gdbarch
*gdbarch
= get_regcache_arch (this_regs
);
562 gdb_byte prev_buffer
[MAX_REGISTER_SIZE
];
563 gdb_byte this_buffer
[MAX_REGISTER_SIZE
];
565 /* Registers not valid in this frame return count as unchanged. */
566 if (!regcache_valid_p (this_regs
, regnum
))
569 /* First time through or after gdbarch change consider all registers as
570 changed. Same for registers not valid in the previous frame. */
571 if (!prev_regs
|| get_regcache_arch (prev_regs
) != gdbarch
572 || !regcache_valid_p (prev_regs
, regnum
))
575 /* Get register contents and compare. */
576 regcache_cooked_read (prev_regs
, regnum
, prev_buffer
);
577 regcache_cooked_read (this_regs
, regnum
, this_buffer
);
579 return memcmp (prev_buffer
, this_buffer
,
580 register_size (gdbarch
, regnum
)) != 0;
583 /* Return a list of register number and value pairs. The valid
584 arguments expected are: a letter indicating the format in which to
585 display the registers contents. This can be one of: x (hexadecimal), d
586 (decimal), N (natural), t (binary), o (octal), r (raw). After the
587 format argumetn there can be a sequence of numbers, indicating which
588 registers to fetch the content of. If the format is the only argument,
589 a list of all the registers with their values is returned. */
591 mi_cmd_data_list_register_values (char *command
, char **argv
, int argc
)
593 int regnum
, numregs
, format
;
595 struct cleanup
*list_cleanup
, *tuple_cleanup
;
597 /* Note that the test for a valid register must include checking the
598 gdbarch_register_name because gdbarch_num_regs may be allocated for
599 the union of the register sets within a family of related processors.
600 In this case, some entries of gdbarch_register_name will change depending
601 upon the particular processor being debugged. */
603 numregs
= gdbarch_num_regs (current_gdbarch
)
604 + gdbarch_num_pseudo_regs (current_gdbarch
);
607 error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
609 format
= (int) argv
[0][0];
611 list_cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "register-values");
613 if (argc
== 1) /* No args, beside the format: do all the regs. */
619 if (gdbarch_register_name (current_gdbarch
, regnum
) == NULL
620 || *(gdbarch_register_name (current_gdbarch
, regnum
)) == '\0')
622 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
623 ui_out_field_int (uiout
, "number", regnum
);
624 get_register (regnum
, format
);
625 do_cleanups (tuple_cleanup
);
629 /* Else, list of register #s, just do listed regs. */
630 for (i
= 1; i
< argc
; i
++)
632 regnum
= atoi (argv
[i
]);
636 && gdbarch_register_name (current_gdbarch
, regnum
) != NULL
637 && *gdbarch_register_name (current_gdbarch
, regnum
) != '\000')
639 tuple_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
640 ui_out_field_int (uiout
, "number", regnum
);
641 get_register (regnum
, format
);
642 do_cleanups (tuple_cleanup
);
645 error ("bad register number");
647 do_cleanups (list_cleanup
);
650 /* Output one register's contents in the desired format. */
652 get_register (int regnum
, int format
)
654 gdb_byte buffer
[MAX_REGISTER_SIZE
];
659 static struct ui_stream
*stb
= NULL
;
661 stb
= ui_out_stream_new (uiout
);
666 frame_register (get_selected_frame (NULL
), regnum
, &optim
, &lval
, &addr
,
670 error ("Optimized out");
675 char *ptr
, buf
[1024];
679 for (j
= 0; j
< register_size (current_gdbarch
, regnum
); j
++)
681 int idx
= gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
? j
682 : register_size (current_gdbarch
, regnum
) - 1 - j
;
683 sprintf (ptr
, "%02x", (unsigned char) buffer
[idx
]);
686 ui_out_field_string (uiout
, "value", buf
);
687 /*fputs_filtered (buf, gdb_stdout); */
691 struct value_print_options opts
;
692 get_formatted_print_options (&opts
, format
);
694 val_print (register_type (current_gdbarch
, regnum
), buffer
, 0, 0,
695 stb
->stream
, 0, &opts
, current_language
);
696 ui_out_field_stream (uiout
, "value", stb
);
697 ui_out_stream_delete (stb
);
701 /* Write given values into registers. The registers and values are
702 given as pairs. The corresponding MI command is
703 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
705 mi_cmd_data_write_register_values (char *command
, char **argv
, int argc
)
710 /* Note that the test for a valid register must include checking the
711 gdbarch_register_name because gdbarch_num_regs may be allocated for
712 the union of the register sets within a family of related processors.
713 In this case, some entries of gdbarch_register_name will change depending
714 upon the particular processor being debugged. */
716 numregs
= gdbarch_num_regs (current_gdbarch
)
717 + gdbarch_num_pseudo_regs (current_gdbarch
);
720 error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
722 format
= (int) argv
[0][0];
724 if (!target_has_registers
)
725 error ("mi_cmd_data_write_register_values: No registers.");
728 error ("mi_cmd_data_write_register_values: No regs and values specified.");
731 error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
733 for (i
= 1; i
< argc
; i
= i
+ 2)
735 int regnum
= atoi (argv
[i
]);
737 if (regnum
>= 0 && regnum
< numregs
738 && gdbarch_register_name (current_gdbarch
, regnum
)
739 && *gdbarch_register_name (current_gdbarch
, regnum
))
743 /* Get the value as a number. */
744 value
= parse_and_eval_address (argv
[i
+ 1]);
747 regcache_cooked_write_signed (get_current_regcache (), regnum
, value
);
750 error ("bad register number");
754 /* Evaluate the value of the argument. The argument is an
755 expression. If the expression contains spaces it needs to be
756 included in double quotes. */
758 mi_cmd_data_evaluate_expression (char *command
, char **argv
, int argc
)
760 struct expression
*expr
;
761 struct cleanup
*old_chain
= NULL
;
763 struct ui_stream
*stb
= NULL
;
764 struct value_print_options opts
;
766 stb
= ui_out_stream_new (uiout
);
770 ui_out_stream_delete (stb
);
771 error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
774 expr
= parse_expression (argv
[0]);
776 old_chain
= make_cleanup (free_current_contents
, &expr
);
778 val
= evaluate_expression (expr
);
780 /* Print the result of the expression evaluation. */
781 get_user_print_options (&opts
);
783 val_print (value_type (val
), value_contents (val
),
784 value_embedded_offset (val
), VALUE_ADDRESS (val
),
785 stb
->stream
, 0, &opts
, current_language
);
787 ui_out_field_stream (uiout
, "value", stb
);
788 ui_out_stream_delete (stb
);
790 do_cleanups (old_chain
);
795 ADDR: start address of data to be dumped.
796 WORD-FORMAT: a char indicating format for the ``word''. See
798 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
799 NR_ROW: Number of rows.
800 NR_COL: The number of colums (words per row).
801 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
802 ASCHAR for unprintable characters.
804 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
805 displayes them. Returns:
807 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
810 The number of bytes read is SIZE*ROW*COL. */
813 mi_cmd_data_read_memory (char *command
, char **argv
, int argc
)
815 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
821 struct type
*word_type
;
834 static struct mi_opt opts
[] =
836 {"o", OFFSET_OPT
, 1},
842 int opt
= mi_getopt ("mi_cmd_data_read_memory", argc
, argv
, opts
,
846 switch ((enum opt
) opt
)
849 offset
= atol (optarg
);
856 if (argc
< 5 || argc
> 6)
857 error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
859 /* Extract all the arguments. */
861 /* Start address of the memory dump. */
862 addr
= parse_and_eval_address (argv
[0]) + offset
;
863 /* The format character to use when displaying a memory word. See
864 the ``x'' command. */
865 word_format
= argv
[1][0];
866 /* The size of the memory word. */
867 word_size
= atol (argv
[2]);
871 word_type
= builtin_type_int8
;
875 word_type
= builtin_type_int16
;
879 word_type
= builtin_type_int32
;
883 word_type
= builtin_type_int64
;
887 word_type
= builtin_type_int8
;
890 /* The number of rows. */
891 nr_rows
= atol (argv
[3]);
893 error ("mi_cmd_data_read_memory: invalid number of rows.");
895 /* Number of bytes per row. */
896 nr_cols
= atol (argv
[4]);
898 error ("mi_cmd_data_read_memory: invalid number of columns.");
900 /* The un-printable character when printing ascii. */
906 /* Create a buffer and read it in. */
907 total_bytes
= word_size
* nr_rows
* nr_cols
;
908 mbuf
= xcalloc (total_bytes
, 1);
909 make_cleanup (xfree
, mbuf
);
911 nr_bytes
= target_read_until_error (¤t_target
, TARGET_OBJECT_MEMORY
,
912 NULL
, mbuf
, addr
, total_bytes
);
914 error ("Unable to read memory.");
916 /* Output the header information. */
917 ui_out_field_core_addr (uiout
, "addr", addr
);
918 ui_out_field_int (uiout
, "nr-bytes", nr_bytes
);
919 ui_out_field_int (uiout
, "total-bytes", total_bytes
);
920 ui_out_field_core_addr (uiout
, "next-row", addr
+ word_size
* nr_cols
);
921 ui_out_field_core_addr (uiout
, "prev-row", addr
- word_size
* nr_cols
);
922 ui_out_field_core_addr (uiout
, "next-page", addr
+ total_bytes
);
923 ui_out_field_core_addr (uiout
, "prev-page", addr
- total_bytes
);
925 /* Build the result as a two dimentional table. */
927 struct ui_stream
*stream
= ui_out_stream_new (uiout
);
928 struct cleanup
*cleanup_list_memory
;
931 cleanup_list_memory
= make_cleanup_ui_out_list_begin_end (uiout
, "memory");
932 for (row
= 0, row_byte
= 0;
934 row
++, row_byte
+= nr_cols
* word_size
)
938 struct cleanup
*cleanup_tuple
;
939 struct cleanup
*cleanup_list_data
;
940 struct value_print_options opts
;
942 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
943 ui_out_field_core_addr (uiout
, "addr", addr
+ row_byte
);
944 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
945 cleanup_list_data
= make_cleanup_ui_out_list_begin_end (uiout
, "data");
946 get_formatted_print_options (&opts
, word_format
);
947 for (col
= 0, col_byte
= row_byte
;
949 col
++, col_byte
+= word_size
)
951 if (col_byte
+ word_size
> nr_bytes
)
953 ui_out_field_string (uiout
, NULL
, "N/A");
957 ui_file_rewind (stream
->stream
);
958 print_scalar_formatted (mbuf
+ col_byte
, word_type
, &opts
,
959 word_asize
, stream
->stream
);
960 ui_out_field_stream (uiout
, NULL
, stream
);
963 do_cleanups (cleanup_list_data
);
967 ui_file_rewind (stream
->stream
);
968 for (byte
= row_byte
; byte
< row_byte
+ word_size
* nr_cols
; byte
++)
970 if (byte
>= nr_bytes
)
972 fputc_unfiltered ('X', stream
->stream
);
974 else if (mbuf
[byte
] < 32 || mbuf
[byte
] > 126)
976 fputc_unfiltered (aschar
, stream
->stream
);
979 fputc_unfiltered (mbuf
[byte
], stream
->stream
);
981 ui_out_field_stream (uiout
, "ascii", stream
);
983 do_cleanups (cleanup_tuple
);
985 ui_out_stream_delete (stream
);
986 do_cleanups (cleanup_list_memory
);
988 do_cleanups (cleanups
);
991 /* DATA-MEMORY-WRITE:
993 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
994 offset from the beginning of the memory grid row where the cell to
996 ADDR: start address of the row in the memory grid where the memory
997 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
998 the location to write to.
999 FORMAT: a char indicating format for the ``word''. See
1001 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
1002 VALUE: value to be written into the memory address.
1004 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1008 mi_cmd_data_write_memory (char *command
, char **argv
, int argc
)
1013 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1014 enough when using a compiler other than GCC. */
1017 struct cleanup
*old_chain
;
1025 static struct mi_opt opts
[] =
1027 {"o", OFFSET_OPT
, 1},
1033 int opt
= mi_getopt ("mi_cmd_data_write_memory", argc
, argv
, opts
,
1037 switch ((enum opt
) opt
)
1040 offset
= atol (optarg
);
1048 error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1050 /* Extract all the arguments. */
1051 /* Start address of the memory dump. */
1052 addr
= parse_and_eval_address (argv
[0]);
1053 /* The format character to use when displaying a memory word. See
1054 the ``x'' command. */
1055 word_format
= argv
[1][0];
1056 /* The size of the memory word. */
1057 word_size
= atol (argv
[2]);
1059 /* Calculate the real address of the write destination. */
1060 addr
+= (offset
* word_size
);
1062 /* Get the value as a number. */
1063 value
= parse_and_eval_address (argv
[3]);
1064 /* Get the value into an array. */
1065 buffer
= xmalloc (word_size
);
1066 old_chain
= make_cleanup (xfree
, buffer
);
1067 store_signed_integer (buffer
, word_size
, value
);
1068 /* Write it down to memory. */
1069 write_memory (addr
, buffer
, word_size
);
1070 /* Free the buffer. */
1071 do_cleanups (old_chain
);
1075 mi_cmd_enable_timings (char *command
, char **argv
, int argc
)
1081 if (strcmp (argv
[0], "yes") == 0)
1083 else if (strcmp (argv
[0], "no") == 0)
1094 error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command
);
1098 mi_cmd_list_features (char *command
, char **argv
, int argc
)
1102 struct cleanup
*cleanup
= NULL
;
1103 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
1105 ui_out_field_string (uiout
, NULL
, "frozen-varobjs");
1106 ui_out_field_string (uiout
, NULL
, "pending-breakpoints");
1107 ui_out_field_string (uiout
, NULL
, "thread-info");
1109 do_cleanups (cleanup
);
1113 error ("-list-features should be passed no arguments");
1117 mi_cmd_list_target_features (char *command
, char **argv
, int argc
)
1121 struct cleanup
*cleanup
= NULL
;
1122 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "features");
1124 if (target_can_async_p ())
1125 ui_out_field_string (uiout
, NULL
, "async");
1127 do_cleanups (cleanup
);
1131 error ("-list-target-features should be passed no arguments");
1134 /* Execute a command within a safe environment.
1135 Return <0 for error; >=0 for ok.
1137 args->action will tell mi_execute_command what action
1138 to perfrom after the given command has executed (display/suppress
1139 prompt, display error). */
1142 captured_mi_execute_command (struct ui_out
*uiout
, void *data
)
1144 struct cleanup
*cleanup
;
1145 struct mi_parse
*context
= (struct mi_parse
*) data
;
1148 current_command_ts
= context
->cmd_start
;
1150 current_token
= xstrdup (context
->token
);
1151 cleanup
= make_cleanup (free_current_contents
, ¤t_token
);
1153 running_result_record_printed
= 0;
1155 switch (context
->op
)
1158 /* A MI command was read from the input stream. */
1160 /* FIXME: gdb_???? */
1161 fprintf_unfiltered (raw_stdout
, " token=`%s' command=`%s' args=`%s'\n",
1162 context
->token
, context
->command
, context
->args
);
1165 mi_cmd_execute (context
);
1167 /* Print the result if there were no errors.
1169 Remember that on the way out of executing a command, you have
1170 to directly use the mi_interp's uiout, since the command could
1171 have reset the interpreter, in which case the current uiout
1172 will most likely crash in the mi_out_* routines. */
1173 if (!running_result_record_printed
)
1175 fputs_unfiltered (context
->token
, raw_stdout
);
1176 /* There's no particularly good reason why target-connect results
1177 in not ^done. Should kill ^connected for MI3. */
1178 fputs_unfiltered (strcmp (context
->command
, "target-select") == 0
1179 ? "^connected" : "^done", raw_stdout
);
1180 mi_out_put (uiout
, raw_stdout
);
1181 mi_out_rewind (uiout
);
1182 mi_print_timing_maybe ();
1183 fputs_unfiltered ("\n", raw_stdout
);
1186 /* The command does not want anything to be printed. In that
1187 case, the command probably should not have written anything
1188 to uiout, but in case it has written something, discard it. */
1189 mi_out_rewind (uiout
);
1195 /* A CLI command was read from the input stream. */
1196 /* This "feature" will be removed as soon as we have a
1197 complete set of mi commands. */
1198 /* Echo the command on the console. */
1199 fprintf_unfiltered (gdb_stdlog
, "%s\n", context
->command
);
1200 /* Call the "console" interpreter. */
1201 argv
[0] = "console";
1202 argv
[1] = context
->command
;
1203 mi_cmd_interpreter_exec ("-interpreter-exec", argv
, 2);
1205 /* If we changed interpreters, DON'T print out anything. */
1206 if (current_interp_named_p (INTERP_MI
)
1207 || current_interp_named_p (INTERP_MI1
)
1208 || current_interp_named_p (INTERP_MI2
)
1209 || current_interp_named_p (INTERP_MI3
))
1211 if (!running_result_record_printed
)
1213 fputs_unfiltered (context
->token
, raw_stdout
);
1214 fputs_unfiltered ("^done", raw_stdout
);
1215 mi_out_put (uiout
, raw_stdout
);
1216 mi_out_rewind (uiout
);
1217 mi_print_timing_maybe ();
1218 fputs_unfiltered ("\n", raw_stdout
);
1221 mi_out_rewind (uiout
);
1228 do_cleanups (cleanup
);
1235 mi_execute_command (char *cmd
, int from_tty
)
1237 struct mi_parse
*command
;
1238 struct ui_out
*saved_uiout
= uiout
;
1240 /* This is to handle EOF (^D). We just quit gdb. */
1241 /* FIXME: we should call some API function here. */
1243 quit_force (NULL
, from_tty
);
1245 command
= mi_parse (cmd
);
1247 if (command
!= NULL
)
1249 struct gdb_exception result
;
1250 ptid_t previous_ptid
= inferior_ptid
;
1254 command
->cmd_start
= (struct mi_timestamp
*)
1255 xmalloc (sizeof (struct mi_timestamp
));
1256 timestamp (command
->cmd_start
);
1259 result
= catch_exception (uiout
, captured_mi_execute_command
, command
,
1261 if (result
.reason
< 0)
1263 /* The command execution failed and error() was called
1265 fputs_unfiltered (command
->token
, raw_stdout
);
1266 fputs_unfiltered ("^error,msg=\"", raw_stdout
);
1267 if (result
.message
== NULL
)
1268 fputs_unfiltered ("unknown error", raw_stdout
);
1270 fputstr_unfiltered (result
.message
, '"', raw_stdout
);
1271 fputs_unfiltered ("\"\n", raw_stdout
);
1272 mi_out_rewind (uiout
);
1275 if (/* The notifications are only output when the top-level
1276 interpreter (specified on the command line) is MI. */
1277 ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
1278 /* Don't try report anything if there are no threads --
1279 the program is dead. */
1280 && thread_count () != 0
1281 /* -thread-select explicitly changes thread. If frontend uses that
1282 internally, we don't want to emit =thread-selected, since
1283 =thread-selected is supposed to indicate user's intentions. */
1284 && strcmp (command
->command
, "thread-select") != 0)
1286 struct mi_interp
*mi
= top_level_interpreter_data ();
1287 int report_change
= 0;
1289 if (command
->thread
== -1)
1291 report_change
= (!ptid_equal (previous_ptid
, null_ptid
)
1292 && !ptid_equal (inferior_ptid
, previous_ptid
)
1293 && !ptid_equal (inferior_ptid
, null_ptid
));
1295 else if (!ptid_equal (inferior_ptid
, null_ptid
))
1297 struct thread_info
*ti
= inferior_thread ();
1298 report_change
= (ti
->num
!= command
->thread
);
1303 struct thread_info
*ti
= inferior_thread ();
1304 target_terminal_ours ();
1305 fprintf_unfiltered (mi
->event_channel
,
1306 "thread-selected,id=\"%d\"",
1308 gdb_flush (mi
->event_channel
);
1312 mi_parse_free (command
);
1315 fputs_unfiltered ("(gdb) \n", raw_stdout
);
1316 gdb_flush (raw_stdout
);
1317 /* Print any buffered hook code. */
1322 mi_cmd_execute (struct mi_parse
*parse
)
1324 struct cleanup
*cleanup
;
1328 cleanup
= make_cleanup (null_cleanup
, NULL
);
1330 if (parse
->frame
!= -1 && parse
->thread
== -1)
1331 error (_("Cannot specify --frame without --thread"));
1333 if (parse
->thread
!= -1)
1335 struct thread_info
*tp
= find_thread_id (parse
->thread
);
1337 error (_("Invalid thread id: %d"), parse
->thread
);
1339 if (is_exited (tp
->ptid
))
1340 error (_("Thread id: %d has terminated"), parse
->thread
);
1342 switch_to_thread (tp
->ptid
);
1345 if (parse
->frame
!= -1)
1347 struct frame_info
*fid
;
1348 int frame
= parse
->frame
;
1349 fid
= find_relative_frame (get_current_frame (), &frame
);
1351 /* find_relative_frame was successful */
1354 error (_("Invalid frame id: %d"), frame
);
1357 if (parse
->cmd
->argv_func
!= NULL
)
1358 parse
->cmd
->argv_func (parse
->command
, parse
->argv
, parse
->argc
);
1359 else if (parse
->cmd
->cli
.cmd
!= 0)
1361 /* FIXME: DELETE THIS. */
1362 /* The operation is still implemented by a cli command. */
1363 /* Must be a synchronous one. */
1364 mi_execute_cli_command (parse
->cmd
->cli
.cmd
, parse
->cmd
->cli
.args_p
,
1369 /* FIXME: DELETE THIS. */
1370 struct ui_file
*stb
;
1372 stb
= mem_fileopen ();
1374 fputs_unfiltered ("Undefined mi command: ", stb
);
1375 fputstr_unfiltered (parse
->command
, '"', stb
);
1376 fputs_unfiltered (" (missing implementation)", stb
);
1378 make_cleanup_ui_file_delete (stb
);
1381 do_cleanups (cleanup
);
1384 /* FIXME: This is just a hack so we can get some extra commands going.
1385 We don't want to channel things through the CLI, but call libgdb directly.
1386 Use only for synchronous commands. */
1389 mi_execute_cli_command (const char *cmd
, int args_p
, const char *args
)
1393 struct cleanup
*old_cleanups
;
1396 run
= xstrprintf ("%s %s", cmd
, args
);
1398 run
= xstrdup (cmd
);
1400 /* FIXME: gdb_???? */
1401 fprintf_unfiltered (gdb_stdout
, "cli=%s run=%s\n",
1403 old_cleanups
= make_cleanup (xfree
, run
);
1404 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1405 do_cleanups (old_cleanups
);
1411 mi_execute_async_cli_command (char *cli_command
, char **argv
, int argc
)
1413 struct cleanup
*old_cleanups
;
1416 if (target_can_async_p ())
1417 run
= xstrprintf ("%s %s&", cli_command
, argc
? *argv
: "");
1419 run
= xstrprintf ("%s %s", cli_command
, argc
? *argv
: "");
1420 old_cleanups
= make_cleanup (xfree
, run
);
1422 execute_command ( /*ui */ run
, 0 /*from_tty */ );
1424 if (target_can_async_p ())
1426 /* If we're not executing, an exception should have been throw. */
1427 gdb_assert (is_running (inferior_ptid
));
1428 do_cleanups (old_cleanups
);
1432 /* Do this before doing any printing. It would appear that some
1433 print code leaves garbage around in the buffer. */
1434 do_cleanups (old_cleanups
);
1439 mi_load_progress (const char *section_name
,
1440 unsigned long sent_so_far
,
1441 unsigned long total_section
,
1442 unsigned long total_sent
,
1443 unsigned long grand_total
)
1445 struct timeval time_now
, delta
, update_threshold
;
1446 static struct timeval last_update
;
1447 static char *previous_sect_name
= NULL
;
1449 struct ui_out
*saved_uiout
;
1451 /* This function is called through deprecated_show_load_progress
1452 which means uiout may not be correct. Fix it for the duration
1453 of this function. */
1454 saved_uiout
= uiout
;
1456 if (current_interp_named_p (INTERP_MI
)
1457 || current_interp_named_p (INTERP_MI2
))
1458 uiout
= mi_out_new (2);
1459 else if (current_interp_named_p (INTERP_MI1
))
1460 uiout
= mi_out_new (1);
1461 else if (current_interp_named_p (INTERP_MI3
))
1462 uiout
= mi_out_new (3);
1466 update_threshold
.tv_sec
= 0;
1467 update_threshold
.tv_usec
= 500000;
1468 gettimeofday (&time_now
, NULL
);
1470 delta
.tv_usec
= time_now
.tv_usec
- last_update
.tv_usec
;
1471 delta
.tv_sec
= time_now
.tv_sec
- last_update
.tv_sec
;
1473 if (delta
.tv_usec
< 0)
1476 delta
.tv_usec
+= 1000000L;
1479 new_section
= (previous_sect_name
?
1480 strcmp (previous_sect_name
, section_name
) : 1);
1483 struct cleanup
*cleanup_tuple
;
1484 xfree (previous_sect_name
);
1485 previous_sect_name
= xstrdup (section_name
);
1488 fputs_unfiltered (current_token
, raw_stdout
);
1489 fputs_unfiltered ("+download", raw_stdout
);
1490 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1491 ui_out_field_string (uiout
, "section", section_name
);
1492 ui_out_field_int (uiout
, "section-size", total_section
);
1493 ui_out_field_int (uiout
, "total-size", grand_total
);
1494 do_cleanups (cleanup_tuple
);
1495 mi_out_put (uiout
, raw_stdout
);
1496 fputs_unfiltered ("\n", raw_stdout
);
1497 gdb_flush (raw_stdout
);
1500 if (delta
.tv_sec
>= update_threshold
.tv_sec
&&
1501 delta
.tv_usec
>= update_threshold
.tv_usec
)
1503 struct cleanup
*cleanup_tuple
;
1504 last_update
.tv_sec
= time_now
.tv_sec
;
1505 last_update
.tv_usec
= time_now
.tv_usec
;
1507 fputs_unfiltered (current_token
, raw_stdout
);
1508 fputs_unfiltered ("+download", raw_stdout
);
1509 cleanup_tuple
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1510 ui_out_field_string (uiout
, "section", section_name
);
1511 ui_out_field_int (uiout
, "section-sent", sent_so_far
);
1512 ui_out_field_int (uiout
, "section-size", total_section
);
1513 ui_out_field_int (uiout
, "total-sent", total_sent
);
1514 ui_out_field_int (uiout
, "total-size", grand_total
);
1515 do_cleanups (cleanup_tuple
);
1516 mi_out_put (uiout
, raw_stdout
);
1517 fputs_unfiltered ("\n", raw_stdout
);
1518 gdb_flush (raw_stdout
);
1522 uiout
= saved_uiout
;
1526 timestamp (struct mi_timestamp
*tv
)
1529 gettimeofday (&tv
->wallclock
, NULL
);
1530 #ifdef HAVE_GETRUSAGE
1531 getrusage (RUSAGE_SELF
, &rusage
);
1532 tv
->utime
.tv_sec
= rusage
.ru_utime
.tv_sec
;
1533 tv
->utime
.tv_usec
= rusage
.ru_utime
.tv_usec
;
1534 tv
->stime
.tv_sec
= rusage
.ru_stime
.tv_sec
;
1535 tv
->stime
.tv_usec
= rusage
.ru_stime
.tv_usec
;
1537 usec
= get_run_time ();
1538 tv
->utime
.tv_sec
= usec
/1000000L;
1539 tv
->utime
.tv_usec
= usec
- 1000000L*tv
->utime
.tv_sec
;
1540 tv
->stime
.tv_sec
= 0;
1541 tv
->stime
.tv_usec
= 0;
1546 print_diff_now (struct mi_timestamp
*start
)
1548 struct mi_timestamp now
;
1550 print_diff (start
, &now
);
1554 mi_print_timing_maybe (void)
1556 /* If the command is -enable-timing then do_timings may be
1557 true whilst current_command_ts is not initialized. */
1558 if (do_timings
&& current_command_ts
)
1559 print_diff_now (current_command_ts
);
1563 timeval_diff (struct timeval start
, struct timeval end
)
1565 return ((end
.tv_sec
- start
.tv_sec
) * 1000000L)
1566 + (end
.tv_usec
- start
.tv_usec
);
1570 print_diff (struct mi_timestamp
*start
, struct mi_timestamp
*end
)
1574 ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1575 timeval_diff (start
->wallclock
, end
->wallclock
) / 1000000.0,
1576 timeval_diff (start
->utime
, end
->utime
) / 1000000.0,
1577 timeval_diff (start
->stime
, end
->stime
) / 1000000.0);