]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/mi/mi-main.c
2002-11-29 Andrew Cagney <ac131313@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / mi / mi-main.c
1 /* MI Command Set.
2 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* Work in progress */
23
24 #include "defs.h"
25 #include "target.h"
26 #include "inferior.h"
27 #include "gdb_string.h"
28 #include "top.h"
29 #include "gdbthread.h"
30 #include "mi-cmds.h"
31 #include "mi-parse.h"
32 #include "mi-getopt.h"
33 #include "mi-console.h"
34 #include "ui-out.h"
35 #include "mi-out.h"
36 #include "event-loop.h"
37 #include "event-top.h"
38 #include "gdbcore.h" /* for write_memory() */
39 #include "value.h" /* for deprecated_write_register_bytes() */
40 #include "regcache.h"
41 #include "gdb.h"
42 #include "frame.h"
43
44 #include <ctype.h>
45 #include <sys/time.h>
46
47 enum
48 {
49 FROM_TTY = 0
50 };
51
52 /* Enumerations of the actions that may result from calling
53 captured_mi_execute_command */
54
55 enum captured_mi_execute_command_actions
56 {
57 EXECUTE_COMMAND_DISPLAY_PROMPT,
58 EXECUTE_COMMAND_SUPRESS_PROMPT,
59 EXECUTE_COMMAND_DISPLAY_ERROR
60 };
61
62 /* This structure is used to pass information from captured_mi_execute_command
63 to mi_execute_command. */
64 struct captured_mi_execute_command_args
65 {
66 /* This return result of the MI command (output) */
67 enum mi_cmd_result rc;
68
69 /* What action to perform when the call is finished (output) */
70 enum captured_mi_execute_command_actions action;
71
72 /* The command context to be executed (input) */
73 struct mi_parse *command;
74 };
75
76 int mi_debug_p;
77 struct ui_file *raw_stdout;
78
79 /* The token of the last asynchronous command */
80 static char *last_async_command;
81 static char *previous_async_command;
82 static char *mi_error_message;
83 static char *old_regs;
84
85 extern void _initialize_mi_main (void);
86 static char *mi_input (char *);
87 static void mi_execute_command (char *cmd, int from_tty);
88 static enum mi_cmd_result mi_cmd_execute (struct mi_parse *parse);
89
90 static void mi_execute_cli_command (const char *cli, char *args);
91 static enum mi_cmd_result mi_execute_async_cli_command (char *mi, char *args, int from_tty);
92 static void mi_execute_command_wrapper (char *cmd);
93
94 void mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg);
95
96 static int register_changed_p (int regnum);
97 static int get_register (int regnum, int format);
98 static void mi_load_progress (const char *section_name,
99 unsigned long sent_so_far,
100 unsigned long total_section,
101 unsigned long total_sent,
102 unsigned long grand_total);
103
104 /* Command implementations. FIXME: Is this libgdb? No. This is the MI
105 layer that calls libgdb. Any operation used in the below should be
106 formalized. */
107
108 enum mi_cmd_result
109 mi_cmd_gdb_exit (char *command, char **argv, int argc)
110 {
111 /* We have to print everything right here because we never return */
112 if (last_async_command)
113 fputs_unfiltered (last_async_command, raw_stdout);
114 fputs_unfiltered ("^exit\n", raw_stdout);
115 mi_out_put (uiout, raw_stdout);
116 /* FIXME: The function called is not yet a formal libgdb function */
117 quit_force (NULL, FROM_TTY);
118 return MI_CMD_DONE;
119 }
120
121 enum mi_cmd_result
122 mi_cmd_exec_run (char *args, int from_tty)
123 {
124 /* FIXME: Should call a libgdb function, not a cli wrapper */
125 return mi_execute_async_cli_command ("run", args, from_tty);
126 }
127
128 enum mi_cmd_result
129 mi_cmd_exec_next (char *args, int from_tty)
130 {
131 /* FIXME: Should call a libgdb function, not a cli wrapper */
132 return mi_execute_async_cli_command ("next", args, from_tty);
133 }
134
135 enum mi_cmd_result
136 mi_cmd_exec_next_instruction (char *args, int from_tty)
137 {
138 /* FIXME: Should call a libgdb function, not a cli wrapper */
139 return mi_execute_async_cli_command ("nexti", args, from_tty);
140 }
141
142 enum mi_cmd_result
143 mi_cmd_exec_step (char *args, int from_tty)
144 {
145 /* FIXME: Should call a libgdb function, not a cli wrapper */
146 return mi_execute_async_cli_command ("step", args, from_tty);
147 }
148
149 enum mi_cmd_result
150 mi_cmd_exec_step_instruction (char *args, int from_tty)
151 {
152 /* FIXME: Should call a libgdb function, not a cli wrapper */
153 return mi_execute_async_cli_command ("stepi", args, from_tty);
154 }
155
156 enum mi_cmd_result
157 mi_cmd_exec_finish (char *args, int from_tty)
158 {
159 /* FIXME: Should call a libgdb function, not a cli wrapper */
160 return mi_execute_async_cli_command ("finish", args, from_tty);
161 }
162
163 enum mi_cmd_result
164 mi_cmd_exec_until (char *args, int from_tty)
165 {
166 /* FIXME: Should call a libgdb function, not a cli wrapper */
167 return mi_execute_async_cli_command ("until", args, from_tty);
168 }
169
170 enum mi_cmd_result
171 mi_cmd_exec_return (char *args, int from_tty)
172 {
173 /* This command doesn't really execute the target, it just pops the
174 specified number of frames. */
175 if (*args)
176 /* Call return_command with from_tty argument equal to 0 so as to
177 avoid being queried. */
178 return_command (args, 0);
179 else
180 /* Call return_command with from_tty argument equal to 0 so as to
181 avoid being queried. */
182 return_command (NULL, 0);
183
184 /* Because we have called return_command with from_tty = 0, we need
185 to print the frame here. */
186 show_and_print_stack_frame (deprecated_selected_frame,
187 frame_relative_level (deprecated_selected_frame),
188 LOC_AND_ADDRESS);
189
190 return MI_CMD_DONE;
191 }
192
193 enum mi_cmd_result
194 mi_cmd_exec_continue (char *args, int from_tty)
195 {
196 /* FIXME: Should call a libgdb function, not a cli wrapper */
197 return mi_execute_async_cli_command ("continue", args, from_tty);
198 }
199
200 /* Interrupt the execution of the target. Note how we must play around
201 with the token varialbes, in order to display the current token in
202 the result of the interrupt command, and the previous execution
203 token when the target finally stops. See comments in
204 mi_cmd_execute. */
205 enum mi_cmd_result
206 mi_cmd_exec_interrupt (char *args, int from_tty)
207 {
208 if (!target_executing)
209 {
210 xasprintf (&mi_error_message,
211 "mi_cmd_exec_interrupt: Inferior not executing.");
212 return MI_CMD_ERROR;
213 }
214 interrupt_target_command (args, from_tty);
215 if (last_async_command)
216 fputs_unfiltered (last_async_command, raw_stdout);
217 fputs_unfiltered ("^done", raw_stdout);
218 xfree (last_async_command);
219 if (previous_async_command)
220 last_async_command = xstrdup (previous_async_command);
221 xfree (previous_async_command);
222 previous_async_command = NULL;
223 mi_out_put (uiout, raw_stdout);
224 mi_out_rewind (uiout);
225 fputs_unfiltered ("\n", raw_stdout);
226 return MI_CMD_QUIET;
227 }
228
229 enum mi_cmd_result
230 mi_cmd_thread_select (char *command, char **argv, int argc)
231 {
232 enum gdb_rc rc;
233
234 if (argc != 1)
235 {
236 xasprintf (&mi_error_message,
237 "mi_cmd_thread_select: USAGE: threadnum.");
238 return MI_CMD_ERROR;
239 }
240 else
241 rc = gdb_thread_select (uiout, argv[0]);
242
243 /* RC is enum gdb_rc if it is successful (>=0)
244 enum return_reason if not (<0). */
245 if ((int) rc < 0 && (enum return_reason) rc == RETURN_ERROR)
246 return MI_CMD_CAUGHT_ERROR;
247 else if ((int) rc >= 0 && rc == GDB_RC_FAIL)
248 return MI_CMD_ERROR;
249 else
250 return MI_CMD_DONE;
251 }
252
253 enum mi_cmd_result
254 mi_cmd_thread_list_ids (char *command, char **argv, int argc)
255 {
256 enum gdb_rc rc = MI_CMD_DONE;
257
258 if (argc != 0)
259 {
260 xasprintf (&mi_error_message,
261 "mi_cmd_thread_list_ids: No arguments required.");
262 return MI_CMD_ERROR;
263 }
264 else
265 rc = gdb_list_thread_ids (uiout);
266
267 if (rc == GDB_RC_FAIL)
268 return MI_CMD_CAUGHT_ERROR;
269 else
270 return MI_CMD_DONE;
271 }
272
273 enum mi_cmd_result
274 mi_cmd_data_list_register_names (char *command, char **argv, int argc)
275 {
276 int regnum, numregs;
277 int i;
278 struct cleanup *cleanup;
279
280 /* Note that the test for a valid register must include checking the
281 REGISTER_NAME because NUM_REGS may be allocated for the union of
282 the register sets within a family of related processors. In this
283 case, some entries of REGISTER_NAME will change depending upon
284 the particular processor being debugged. */
285
286 numregs = NUM_REGS + NUM_PSEUDO_REGS;
287
288 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names");
289
290 if (argc == 0) /* No args, just do all the regs */
291 {
292 for (regnum = 0;
293 regnum < numregs;
294 regnum++)
295 {
296 if (REGISTER_NAME (regnum) == NULL
297 || *(REGISTER_NAME (regnum)) == '\0')
298 ui_out_field_string (uiout, NULL, "");
299 else
300 ui_out_field_string (uiout, NULL, REGISTER_NAME (regnum));
301 }
302 }
303
304 /* Else, list of register #s, just do listed regs */
305 for (i = 0; i < argc; i++)
306 {
307 regnum = atoi (argv[i]);
308 if (regnum < 0 || regnum >= numregs)
309 {
310 do_cleanups (cleanup);
311 xasprintf (&mi_error_message, "bad register number");
312 return MI_CMD_ERROR;
313 }
314 if (REGISTER_NAME (regnum) == NULL
315 || *(REGISTER_NAME (regnum)) == '\0')
316 ui_out_field_string (uiout, NULL, "");
317 else
318 ui_out_field_string (uiout, NULL, REGISTER_NAME (regnum));
319 }
320 do_cleanups (cleanup);
321 return MI_CMD_DONE;
322 }
323
324 enum mi_cmd_result
325 mi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
326 {
327 int regnum, numregs, changed;
328 int i;
329 struct cleanup *cleanup;
330
331 /* Note that the test for a valid register must include checking the
332 REGISTER_NAME because NUM_REGS may be allocated for the union of
333 the register sets within a family of related processors. In this
334 case, some entries of REGISTER_NAME will change depending upon
335 the particular processor being debugged. */
336
337 numregs = NUM_REGS;
338
339 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changed-registers");
340
341 if (argc == 0) /* No args, just do all the regs */
342 {
343 for (regnum = 0;
344 regnum < numregs;
345 regnum++)
346 {
347 if (REGISTER_NAME (regnum) == NULL
348 || *(REGISTER_NAME (regnum)) == '\0')
349 continue;
350 changed = register_changed_p (regnum);
351 if (changed < 0)
352 {
353 do_cleanups (cleanup);
354 xasprintf (&mi_error_message,
355 "mi_cmd_data_list_changed_registers: Unable to read register contents.");
356 return MI_CMD_ERROR;
357 }
358 else if (changed)
359 ui_out_field_int (uiout, NULL, regnum);
360 }
361 }
362
363 /* Else, list of register #s, just do listed regs */
364 for (i = 0; i < argc; i++)
365 {
366 regnum = atoi (argv[i]);
367
368 if (regnum >= 0
369 && regnum < numregs
370 && REGISTER_NAME (regnum) != NULL
371 && *REGISTER_NAME (regnum) != '\000')
372 {
373 changed = register_changed_p (regnum);
374 if (changed < 0)
375 {
376 do_cleanups (cleanup);
377 xasprintf (&mi_error_message,
378 "mi_cmd_data_list_register_change: Unable to read register contents.");
379 return MI_CMD_ERROR;
380 }
381 else if (changed)
382 ui_out_field_int (uiout, NULL, regnum);
383 }
384 else
385 {
386 do_cleanups (cleanup);
387 xasprintf (&mi_error_message, "bad register number");
388 return MI_CMD_ERROR;
389 }
390 }
391 do_cleanups (cleanup);
392 return MI_CMD_DONE;
393 }
394
395 static int
396 register_changed_p (int regnum)
397 {
398 char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
399
400 if (! frame_register_read (deprecated_selected_frame, regnum, raw_buffer))
401 return -1;
402
403 if (memcmp (&old_regs[REGISTER_BYTE (regnum)], raw_buffer,
404 REGISTER_RAW_SIZE (regnum)) == 0)
405 return 0;
406
407 /* Found a changed register. Return 1. */
408
409 memcpy (&old_regs[REGISTER_BYTE (regnum)], raw_buffer,
410 REGISTER_RAW_SIZE (regnum));
411
412 return 1;
413 }
414
415 /* Return a list of register number and value pairs. The valid
416 arguments expected are: a letter indicating the format in which to
417 display the registers contents. This can be one of: x (hexadecimal), d
418 (decimal), N (natural), t (binary), o (octal), r (raw). After the
419 format argumetn there can be a sequence of numbers, indicating which
420 registers to fetch the content of. If the format is the only argument,
421 a list of all the registers with their values is returned. */
422 enum mi_cmd_result
423 mi_cmd_data_list_register_values (char *command, char **argv, int argc)
424 {
425 int regnum, numregs, format, result;
426 int i;
427 struct cleanup *list_cleanup, *tuple_cleanup;
428
429 /* Note that the test for a valid register must include checking the
430 REGISTER_NAME because NUM_REGS may be allocated for the union of
431 the register sets within a family of related processors. In this
432 case, some entries of REGISTER_NAME will change depending upon
433 the particular processor being debugged. */
434
435 numregs = NUM_REGS;
436
437 if (argc == 0)
438 {
439 xasprintf (&mi_error_message,
440 "mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
441 return MI_CMD_ERROR;
442 }
443
444 format = (int) argv[0][0];
445
446 if (!target_has_registers)
447 {
448 xasprintf (&mi_error_message,
449 "mi_cmd_data_list_register_values: No registers.");
450 return MI_CMD_ERROR;
451 }
452
453 list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values");
454
455 if (argc == 1) /* No args, beside the format: do all the regs */
456 {
457 for (regnum = 0;
458 regnum < numregs;
459 regnum++)
460 {
461 if (REGISTER_NAME (regnum) == NULL
462 || *(REGISTER_NAME (regnum)) == '\0')
463 continue;
464 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
465 ui_out_field_int (uiout, "number", regnum);
466 result = get_register (regnum, format);
467 if (result == -1)
468 {
469 do_cleanups (list_cleanup);
470 return MI_CMD_ERROR;
471 }
472 do_cleanups (tuple_cleanup);
473 }
474 }
475
476 /* Else, list of register #s, just do listed regs */
477 for (i = 1; i < argc; i++)
478 {
479 regnum = atoi (argv[i]);
480
481 if (regnum >= 0
482 && regnum < numregs
483 && REGISTER_NAME (regnum) != NULL
484 && *REGISTER_NAME (regnum) != '\000')
485 {
486 tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
487 ui_out_field_int (uiout, "number", regnum);
488 result = get_register (regnum, format);
489 if (result == -1)
490 {
491 do_cleanups (list_cleanup);
492 return MI_CMD_ERROR;
493 }
494 do_cleanups (tuple_cleanup);
495 }
496 else
497 {
498 do_cleanups (list_cleanup);
499 xasprintf (&mi_error_message, "bad register number");
500 return MI_CMD_ERROR;
501 }
502 }
503 do_cleanups (list_cleanup);
504 return MI_CMD_DONE;
505 }
506
507 /* Output one register's contents in the desired format. */
508 static int
509 get_register (int regnum, int format)
510 {
511 char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
512 char *virtual_buffer = alloca (MAX_REGISTER_VIRTUAL_SIZE);
513 int optim;
514 static struct ui_stream *stb = NULL;
515
516 stb = ui_out_stream_new (uiout);
517
518 if (format == 'N')
519 format = 0;
520
521 get_saved_register (raw_buffer, &optim, (CORE_ADDR *) NULL,
522 deprecated_selected_frame,
523 regnum, (enum lval_type *) NULL);
524 if (optim)
525 {
526 xasprintf (&mi_error_message, "Optimized out");
527 return -1;
528 }
529
530 /* Convert raw data to virtual format if necessary. */
531
532 if (REGISTER_CONVERTIBLE (regnum))
533 {
534 REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
535 raw_buffer, virtual_buffer);
536 }
537 else
538 memcpy (virtual_buffer, raw_buffer, REGISTER_VIRTUAL_SIZE (regnum));
539
540 if (format == 'r')
541 {
542 int j;
543 char *ptr, buf[1024];
544
545 strcpy (buf, "0x");
546 ptr = buf + 2;
547 for (j = 0; j < REGISTER_RAW_SIZE (regnum); j++)
548 {
549 register int idx = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? j
550 : REGISTER_RAW_SIZE (regnum) - 1 - j;
551 sprintf (ptr, "%02x", (unsigned char) raw_buffer[idx]);
552 ptr += 2;
553 }
554 ui_out_field_string (uiout, "value", buf);
555 /*fputs_filtered (buf, gdb_stdout); */
556 }
557 else
558 {
559 val_print (REGISTER_VIRTUAL_TYPE (regnum), virtual_buffer, 0, 0,
560 stb->stream, format, 1, 0, Val_pretty_default);
561 ui_out_field_stream (uiout, "value", stb);
562 ui_out_stream_delete (stb);
563 }
564 return 1;
565 }
566
567 /* Write given values into registers. The registers and values are
568 given as pairs. The corresponding MI command is
569 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
570 enum mi_cmd_result
571 mi_cmd_data_write_register_values (char *command, char **argv, int argc)
572 {
573 int regnum;
574 int i;
575 int numregs;
576 LONGEST value;
577 char format;
578
579 /* Note that the test for a valid register must include checking the
580 REGISTER_NAME because NUM_REGS may be allocated for the union of
581 the register sets within a family of related processors. In this
582 case, some entries of REGISTER_NAME will change depending upon
583 the particular processor being debugged. */
584
585 numregs = NUM_REGS;
586
587 if (argc == 0)
588 {
589 xasprintf (&mi_error_message,
590 "mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
591 return MI_CMD_ERROR;
592 }
593
594 format = (int) argv[0][0];
595
596 if (!target_has_registers)
597 {
598 xasprintf (&mi_error_message,
599 "mi_cmd_data_write_register_values: No registers.");
600 return MI_CMD_ERROR;
601 }
602
603 if (!(argc - 1))
604 {
605 xasprintf (&mi_error_message,
606 "mi_cmd_data_write_register_values: No regs and values specified.");
607 return MI_CMD_ERROR;
608 }
609
610 if ((argc - 1) % 2)
611 {
612 xasprintf (&mi_error_message,
613 "mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
614 return MI_CMD_ERROR;
615 }
616
617 for (i = 1; i < argc; i = i + 2)
618 {
619 regnum = atoi (argv[i]);
620
621 if (regnum >= 0
622 && regnum < numregs
623 && REGISTER_NAME (regnum) != NULL
624 && *REGISTER_NAME (regnum) != '\000')
625 {
626 void *buffer;
627 struct cleanup *old_chain;
628
629 /* Get the value as a number */
630 value = parse_and_eval_address (argv[i + 1]);
631 /* Get the value into an array */
632 buffer = xmalloc (REGISTER_SIZE);
633 old_chain = make_cleanup (xfree, buffer);
634 store_signed_integer (buffer, REGISTER_SIZE, value);
635 /* Write it down */
636 deprecated_write_register_bytes (REGISTER_BYTE (regnum), buffer, REGISTER_RAW_SIZE (regnum));
637 /* Free the buffer. */
638 do_cleanups (old_chain);
639 }
640 else
641 {
642 xasprintf (&mi_error_message, "bad register number");
643 return MI_CMD_ERROR;
644 }
645 }
646 return MI_CMD_DONE;
647 }
648
649 #if 0
650 /*This is commented out because we decided it was not useful. I leave
651 it, just in case. ezannoni:1999-12-08 */
652
653 /* Assign a value to a variable. The expression argument must be in
654 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
655 quoted. */
656 enum mi_cmd_result
657 mi_cmd_data_assign (char *command, char **argv, int argc)
658 {
659 struct expression *expr;
660 struct cleanup *old_chain;
661
662 if (argc != 1)
663 {
664 xasprintf (&mi_error_message,
665 "mi_cmd_data_assign: Usage: -data-assign expression");
666 return MI_CMD_ERROR;
667 }
668
669 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
670 01-12-1999: Need to decide what to do with this for libgdb purposes. */
671
672 expr = parse_expression (argv[0]);
673 old_chain = make_cleanup (free_current_contents, &expr);
674 evaluate_expression (expr);
675 do_cleanups (old_chain);
676 return MI_CMD_DONE;
677 }
678 #endif
679
680 /* Evaluate the value of the argument. The argument is an
681 expression. If the expression contains spaces it needs to be
682 included in double quotes. */
683 enum mi_cmd_result
684 mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
685 {
686 struct expression *expr;
687 struct cleanup *old_chain = NULL;
688 struct value *val;
689 struct ui_stream *stb = NULL;
690
691 stb = ui_out_stream_new (uiout);
692
693 if (argc != 1)
694 {
695 xasprintf (&mi_error_message,
696 "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
697 return MI_CMD_ERROR;
698 }
699
700 expr = parse_expression (argv[0]);
701
702 old_chain = make_cleanup (free_current_contents, &expr);
703
704 val = evaluate_expression (expr);
705
706 /* Print the result of the expression evaluation. */
707 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val),
708 VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
709 stb->stream, 0, 0, 0, 0);
710
711 ui_out_field_stream (uiout, "value", stb);
712 ui_out_stream_delete (stb);
713
714 do_cleanups (old_chain);
715
716 return MI_CMD_DONE;
717 }
718
719 enum mi_cmd_result
720 mi_cmd_target_download (char *args, int from_tty)
721 {
722 char *run;
723 struct cleanup *old_cleanups = NULL;
724
725 xasprintf (&run, "load %s", args);
726 old_cleanups = make_cleanup (xfree, run);
727 execute_command (run, from_tty);
728
729 do_cleanups (old_cleanups);
730 return MI_CMD_DONE;
731 }
732
733 /* Connect to the remote target. */
734 enum mi_cmd_result
735 mi_cmd_target_select (char *args, int from_tty)
736 {
737 char *run;
738 struct cleanup *old_cleanups = NULL;
739
740 xasprintf (&run, "target %s", args);
741 old_cleanups = make_cleanup (xfree, run);
742
743 /* target-select is always synchronous. once the call has returned
744 we know that we are connected. */
745 /* NOTE: At present all targets that are connected are also
746 (implicitly) talking to a halted target. In the future this may
747 change. */
748 execute_command (run, from_tty);
749
750 do_cleanups (old_cleanups);
751
752 /* Issue the completion message here. */
753 if (last_async_command)
754 fputs_unfiltered (last_async_command, raw_stdout);
755 fputs_unfiltered ("^connected", raw_stdout);
756 mi_out_put (uiout, raw_stdout);
757 mi_out_rewind (uiout);
758 fputs_unfiltered ("\n", raw_stdout);
759 do_exec_cleanups (ALL_CLEANUPS);
760 return MI_CMD_QUIET;
761 }
762
763 /* DATA-MEMORY-READ:
764
765 ADDR: start address of data to be dumped.
766 WORD-FORMAT: a char indicating format for the ``word''. See
767 the ``x'' command.
768 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
769 NR_ROW: Number of rows.
770 NR_COL: The number of colums (words per row).
771 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
772 ASCHAR for unprintable characters.
773
774 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
775 displayes them. Returns:
776
777 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
778
779 Returns:
780 The number of bytes read is SIZE*ROW*COL. */
781
782 enum mi_cmd_result
783 mi_cmd_data_read_memory (char *command, char **argv, int argc)
784 {
785 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
786 CORE_ADDR addr;
787 long total_bytes;
788 long nr_cols;
789 long nr_rows;
790 char word_format;
791 struct type *word_type;
792 long word_size;
793 char word_asize;
794 char aschar;
795 char *mbuf;
796 int nr_bytes;
797 long offset = 0;
798 int optind = 0;
799 char *optarg;
800 enum opt
801 {
802 OFFSET_OPT
803 };
804 static struct mi_opt opts[] =
805 {
806 {"o", OFFSET_OPT, 1},
807 0
808 };
809
810 while (1)
811 {
812 int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts,
813 &optind, &optarg);
814 if (opt < 0)
815 break;
816 switch ((enum opt) opt)
817 {
818 case OFFSET_OPT:
819 offset = atol (optarg);
820 break;
821 }
822 }
823 argv += optind;
824 argc -= optind;
825
826 if (argc < 5 || argc > 6)
827 {
828 xasprintf (&mi_error_message,
829 "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
830 return MI_CMD_ERROR;
831 }
832
833 /* Extract all the arguments. */
834
835 /* Start address of the memory dump. */
836 addr = parse_and_eval_address (argv[0]) + offset;
837 /* The format character to use when displaying a memory word. See
838 the ``x'' command. */
839 word_format = argv[1][0];
840 /* The size of the memory word. */
841 word_size = atol (argv[2]);
842 switch (word_size)
843 {
844 case 1:
845 word_type = builtin_type_int8;
846 word_asize = 'b';
847 break;
848 case 2:
849 word_type = builtin_type_int16;
850 word_asize = 'h';
851 break;
852 case 4:
853 word_type = builtin_type_int32;
854 word_asize = 'w';
855 break;
856 case 8:
857 word_type = builtin_type_int64;
858 word_asize = 'g';
859 break;
860 default:
861 word_type = builtin_type_int8;
862 word_asize = 'b';
863 }
864 /* The number of rows */
865 nr_rows = atol (argv[3]);
866 if (nr_rows <= 0)
867 {
868 xasprintf (&mi_error_message,
869 "mi_cmd_data_read_memory: invalid number of rows.");
870 return MI_CMD_ERROR;
871 }
872 /* number of bytes per row. */
873 nr_cols = atol (argv[4]);
874 if (nr_cols <= 0)
875 {
876 xasprintf (&mi_error_message,
877 "mi_cmd_data_read_memory: invalid number of columns.");
878 }
879 /* The un-printable character when printing ascii. */
880 if (argc == 6)
881 aschar = *argv[5];
882 else
883 aschar = 0;
884
885 /* create a buffer and read it in. */
886 total_bytes = word_size * nr_rows * nr_cols;
887 mbuf = xcalloc (total_bytes, 1);
888 make_cleanup (xfree, mbuf);
889 if (mbuf == NULL)
890 {
891 xasprintf (&mi_error_message,
892 "mi_cmd_data_read_memory: out of memory.");
893 return MI_CMD_ERROR;
894 }
895 nr_bytes = 0;
896 while (nr_bytes < total_bytes)
897 {
898 int error;
899 long num = target_read_memory_partial (addr + nr_bytes, mbuf + nr_bytes,
900 total_bytes - nr_bytes,
901 &error);
902 if (num <= 0)
903 break;
904 nr_bytes += num;
905 }
906
907 /* output the header information. */
908 ui_out_field_core_addr (uiout, "addr", addr);
909 ui_out_field_int (uiout, "nr-bytes", nr_bytes);
910 ui_out_field_int (uiout, "total-bytes", total_bytes);
911 ui_out_field_core_addr (uiout, "next-row", addr + word_size * nr_cols);
912 ui_out_field_core_addr (uiout, "prev-row", addr - word_size * nr_cols);
913 ui_out_field_core_addr (uiout, "next-page", addr + total_bytes);
914 ui_out_field_core_addr (uiout, "prev-page", addr - total_bytes);
915
916 /* Build the result as a two dimentional table. */
917 {
918 struct ui_stream *stream = ui_out_stream_new (uiout);
919 struct cleanup *cleanup_list_memory;
920 int row;
921 int row_byte;
922 cleanup_list_memory = make_cleanup_ui_out_list_begin_end (uiout, "memory");
923 for (row = 0, row_byte = 0;
924 row < nr_rows;
925 row++, row_byte += nr_cols * word_size)
926 {
927 int col;
928 int col_byte;
929 struct cleanup *cleanup_tuple;
930 struct cleanup *cleanup_list_data;
931 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
932 ui_out_field_core_addr (uiout, "addr", addr + row_byte);
933 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
934 cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
935 for (col = 0, col_byte = row_byte;
936 col < nr_cols;
937 col++, col_byte += word_size)
938 {
939 if (col_byte + word_size > nr_bytes)
940 {
941 ui_out_field_string (uiout, NULL, "N/A");
942 }
943 else
944 {
945 ui_file_rewind (stream->stream);
946 print_scalar_formatted (mbuf + col_byte, word_type, word_format,
947 word_asize, stream->stream);
948 ui_out_field_stream (uiout, NULL, stream);
949 }
950 }
951 do_cleanups (cleanup_list_data);
952 if (aschar)
953 {
954 int byte;
955 ui_file_rewind (stream->stream);
956 for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
957 {
958 if (byte >= nr_bytes)
959 {
960 fputc_unfiltered ('X', stream->stream);
961 }
962 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
963 {
964 fputc_unfiltered (aschar, stream->stream);
965 }
966 else
967 fputc_unfiltered (mbuf[byte], stream->stream);
968 }
969 ui_out_field_stream (uiout, "ascii", stream);
970 }
971 do_cleanups (cleanup_tuple);
972 }
973 ui_out_stream_delete (stream);
974 do_cleanups (cleanup_list_memory);
975 }
976 do_cleanups (cleanups);
977 return MI_CMD_DONE;
978 }
979
980 /* DATA-MEMORY-WRITE:
981
982 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
983 offset from the beginning of the memory grid row where the cell to
984 be written is.
985 ADDR: start address of the row in the memory grid where the memory
986 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
987 the location to write to.
988 FORMAT: a char indicating format for the ``word''. See
989 the ``x'' command.
990 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
991 VALUE: value to be written into the memory address.
992
993 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
994
995 Prints nothing. */
996 enum mi_cmd_result
997 mi_cmd_data_write_memory (char *command, char **argv, int argc)
998 {
999 CORE_ADDR addr;
1000 char word_format;
1001 long word_size;
1002 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1003 enough when using a compiler other than GCC. */
1004 LONGEST value;
1005 void *buffer;
1006 struct cleanup *old_chain;
1007 long offset = 0;
1008 int optind = 0;
1009 char *optarg;
1010 enum opt
1011 {
1012 OFFSET_OPT
1013 };
1014 static struct mi_opt opts[] =
1015 {
1016 {"o", OFFSET_OPT, 1},
1017 0
1018 };
1019
1020 while (1)
1021 {
1022 int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
1023 &optind, &optarg);
1024 if (opt < 0)
1025 break;
1026 switch ((enum opt) opt)
1027 {
1028 case OFFSET_OPT:
1029 offset = atol (optarg);
1030 break;
1031 }
1032 }
1033 argv += optind;
1034 argc -= optind;
1035
1036 if (argc != 4)
1037 {
1038 xasprintf (&mi_error_message,
1039 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1040 return MI_CMD_ERROR;
1041 }
1042
1043 /* Extract all the arguments. */
1044 /* Start address of the memory dump. */
1045 addr = parse_and_eval_address (argv[0]);
1046 /* The format character to use when displaying a memory word. See
1047 the ``x'' command. */
1048 word_format = argv[1][0];
1049 /* The size of the memory word. */
1050 word_size = atol (argv[2]);
1051
1052 /* Calculate the real address of the write destination. */
1053 addr += (offset * word_size);
1054
1055 /* Get the value as a number */
1056 value = parse_and_eval_address (argv[3]);
1057 /* Get the value into an array */
1058 buffer = xmalloc (word_size);
1059 old_chain = make_cleanup (xfree, buffer);
1060 store_signed_integer (buffer, word_size, value);
1061 /* Write it down to memory */
1062 write_memory (addr, buffer, word_size);
1063 /* Free the buffer. */
1064 do_cleanups (old_chain);
1065
1066 return MI_CMD_DONE;
1067 }
1068
1069 /* Execute a command within a safe environment.
1070 Return <0 for error; >=0 for ok.
1071
1072 args->action will tell mi_execute_command what action
1073 to perfrom after the given command has executed (display/supress
1074 prompt, display error). */
1075
1076 static int
1077 captured_mi_execute_command (struct ui_out *uiout, void *data)
1078 {
1079 struct captured_mi_execute_command_args *args =
1080 (struct captured_mi_execute_command_args *) data;
1081 struct mi_parse *context = args->command;
1082
1083 switch (context->op)
1084 {
1085
1086 case MI_COMMAND:
1087 /* A MI command was read from the input stream */
1088 if (mi_debug_p)
1089 /* FIXME: gdb_???? */
1090 fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
1091 context->token, context->command, context->args);
1092 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1093 condition expression, each function should return an
1094 indication of what action is required and then switch on
1095 that. */
1096 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1097 args->rc = mi_cmd_execute (context);
1098
1099 if (!target_can_async_p () || !target_executing)
1100 {
1101 /* print the result if there were no errors */
1102 if (args->rc == MI_CMD_DONE)
1103 {
1104 fputs_unfiltered (context->token, raw_stdout);
1105 fputs_unfiltered ("^done", raw_stdout);
1106 mi_out_put (uiout, raw_stdout);
1107 mi_out_rewind (uiout);
1108 fputs_unfiltered ("\n", raw_stdout);
1109 }
1110 else if (args->rc == MI_CMD_ERROR)
1111 {
1112 if (mi_error_message)
1113 {
1114 fputs_unfiltered (context->token, raw_stdout);
1115 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1116 fputstr_unfiltered (mi_error_message, '"', raw_stdout);
1117 xfree (mi_error_message);
1118 fputs_unfiltered ("\"\n", raw_stdout);
1119 }
1120 mi_out_rewind (uiout);
1121 }
1122 else if (args->rc == MI_CMD_CAUGHT_ERROR)
1123 {
1124 mi_out_rewind (uiout);
1125 args->action = EXECUTE_COMMAND_DISPLAY_ERROR;
1126 return 1;
1127 }
1128 else
1129 mi_out_rewind (uiout);
1130 }
1131 else if (sync_execution)
1132 {
1133 /* Don't print the prompt. We are executing the target in
1134 synchronous mode. */
1135 args->action = EXECUTE_COMMAND_SUPRESS_PROMPT;
1136 return 1;
1137 }
1138 break;
1139
1140 case CLI_COMMAND:
1141 /* A CLI command was read from the input stream */
1142 /* This will be removed as soon as we have a complete set of
1143 mi commands */
1144 /* echo the command on the console. */
1145 fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1146 /* FIXME: If the command string has something that looks like
1147 a format spec (e.g. %s) we will get a core dump */
1148 mi_execute_cli_command ("%s", context->command);
1149 /* print the result */
1150 /* FIXME: Check for errors here. */
1151 fputs_unfiltered (context->token, raw_stdout);
1152 fputs_unfiltered ("^done", raw_stdout);
1153 mi_out_put (uiout, raw_stdout);
1154 mi_out_rewind (uiout);
1155 fputs_unfiltered ("\n", raw_stdout);
1156 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1157 args->rc = MI_CMD_DONE;
1158 break;
1159
1160 }
1161
1162 return 1;
1163 }
1164
1165
1166 void
1167 mi_execute_command (char *cmd, int from_tty)
1168 {
1169 struct mi_parse *command;
1170 struct captured_mi_execute_command_args args;
1171 struct ui_out *saved_uiout = uiout;
1172 int result, rc;
1173
1174 /* This is to handle EOF (^D). We just quit gdb. */
1175 /* FIXME: we should call some API function here. */
1176 if (cmd == 0)
1177 quit_force (NULL, from_tty);
1178
1179 command = mi_parse (cmd);
1180
1181 if (command != NULL)
1182 {
1183 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
1184 be pushed even further down or even eliminated? */
1185 args.command = command;
1186 result = catch_exceptions (uiout, captured_mi_execute_command, &args, "",
1187 RETURN_MASK_ALL);
1188
1189 if (args.action == EXECUTE_COMMAND_SUPRESS_PROMPT)
1190 {
1191 /* The command is executing synchronously. Bail out early
1192 suppressing the finished prompt. */
1193 mi_parse_free (command);
1194 return;
1195 }
1196 if (args.action == EXECUTE_COMMAND_DISPLAY_ERROR || result < 0)
1197 {
1198 char *msg = error_last_message ();
1199 struct cleanup *cleanup = make_cleanup (xfree, msg);
1200 /* The command execution failed and error() was called
1201 somewhere */
1202 fputs_unfiltered (command->token, raw_stdout);
1203 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1204 fputstr_unfiltered (msg, '"', raw_stdout);
1205 fputs_unfiltered ("\"\n", raw_stdout);
1206 }
1207 mi_parse_free (command);
1208 }
1209
1210 fputs_unfiltered ("(gdb) \n", raw_stdout);
1211 gdb_flush (raw_stdout);
1212 /* print any buffered hook code */
1213 /* ..... */
1214 }
1215
1216 static enum mi_cmd_result
1217 mi_cmd_execute (struct mi_parse *parse)
1218 {
1219 if (parse->cmd->argv_func != NULL
1220 || parse->cmd->args_func != NULL)
1221 {
1222 /* FIXME: We need to save the token because the command executed
1223 may be asynchronous and need to print the token again.
1224 In the future we can pass the token down to the func
1225 and get rid of the last_async_command */
1226 /* The problem here is to keep the token around when we launch
1227 the target, and we want to interrupt it later on. The
1228 interrupt command will have its own token, but when the
1229 target stops, we must display the token corresponding to the
1230 last execution command given. So we have another string where
1231 we copy the token (previous_async_command), if this was
1232 indeed the token of an execution command, and when we stop we
1233 print that one. This is possible because the interrupt
1234 command, when over, will copy that token back into the
1235 default token string (last_async_command). */
1236
1237 if (target_executing)
1238 {
1239 if (!previous_async_command)
1240 previous_async_command = xstrdup (last_async_command);
1241 if (strcmp (parse->command, "exec-interrupt"))
1242 {
1243 fputs_unfiltered (parse->token, raw_stdout);
1244 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1245 fputs_unfiltered ("Cannot execute command ", raw_stdout);
1246 fputstr_unfiltered (parse->command, '"', raw_stdout);
1247 fputs_unfiltered (" while target running", raw_stdout);
1248 fputs_unfiltered ("\"\n", raw_stdout);
1249 return MI_CMD_ERROR;
1250 }
1251 }
1252 last_async_command = xstrdup (parse->token);
1253 make_exec_cleanup (free_current_contents, &last_async_command);
1254 /* FIXME: DELETE THIS! */
1255 if (parse->cmd->args_func != NULL)
1256 return parse->cmd->args_func (parse->args, 0 /*from_tty */ );
1257 return parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
1258 }
1259 else if (parse->cmd->cli != 0)
1260 {
1261 /* FIXME: DELETE THIS. */
1262 /* The operation is still implemented by a cli command */
1263 /* Must be a synchronous one */
1264 mi_execute_cli_command (parse->cmd->cli, parse->args);
1265 return MI_CMD_DONE;
1266 }
1267 else
1268 {
1269 /* FIXME: DELETE THIS. */
1270 fputs_unfiltered (parse->token, raw_stdout);
1271 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1272 fputs_unfiltered ("Undefined mi command: ", raw_stdout);
1273 fputstr_unfiltered (parse->command, '"', raw_stdout);
1274 fputs_unfiltered (" (missing implementation)", raw_stdout);
1275 fputs_unfiltered ("\"\n", raw_stdout);
1276 return MI_CMD_ERROR;
1277 }
1278 }
1279
1280 static void
1281 mi_execute_command_wrapper (char *cmd)
1282 {
1283 mi_execute_command (cmd, stdin == instream);
1284 }
1285
1286 /* FIXME: This is just a hack so we can get some extra commands going.
1287 We don't want to channel things through the CLI, but call libgdb directly */
1288 /* Use only for synchronous commands */
1289
1290 void
1291 mi_execute_cli_command (const char *cli, char *args)
1292 {
1293 if (cli != 0)
1294 {
1295 struct cleanup *old_cleanups;
1296 char *run;
1297 xasprintf (&run, cli, args);
1298 if (mi_debug_p)
1299 /* FIXME: gdb_???? */
1300 fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
1301 cli, run);
1302 old_cleanups = make_cleanup (xfree, run);
1303 execute_command ( /*ui */ run, 0 /*from_tty */ );
1304 do_cleanups (old_cleanups);
1305 return;
1306 }
1307 }
1308
1309 enum mi_cmd_result
1310 mi_execute_async_cli_command (char *mi, char *args, int from_tty)
1311 {
1312 struct cleanup *old_cleanups;
1313 char *run;
1314 char *async_args;
1315
1316 if (target_can_async_p ())
1317 {
1318 async_args = (char *) xmalloc (strlen (args) + 2);
1319 make_exec_cleanup (free, async_args);
1320 strcpy (async_args, args);
1321 strcat (async_args, "&");
1322 xasprintf (&run, "%s %s", mi, async_args);
1323 make_exec_cleanup (free, run);
1324 add_continuation (mi_exec_async_cli_cmd_continuation, NULL);
1325 old_cleanups = NULL;
1326 }
1327 else
1328 {
1329 xasprintf (&run, "%s %s", mi, args);
1330 old_cleanups = make_cleanup (xfree, run);
1331 }
1332
1333 if (!target_can_async_p ())
1334 {
1335 /* NOTE: For synchronous targets asynchronous behavour is faked by
1336 printing out the GDB prompt before we even try to execute the
1337 command. */
1338 if (last_async_command)
1339 fputs_unfiltered (last_async_command, raw_stdout);
1340 fputs_unfiltered ("^running\n", raw_stdout);
1341 fputs_unfiltered ("(gdb) \n", raw_stdout);
1342 gdb_flush (raw_stdout);
1343 }
1344 else
1345 {
1346 /* FIXME: cagney/1999-11-29: Printing this message before
1347 calling execute_command is wrong. It should only be printed
1348 once gdb has confirmed that it really has managed to send a
1349 run command to the target. */
1350 if (last_async_command)
1351 fputs_unfiltered (last_async_command, raw_stdout);
1352 fputs_unfiltered ("^running\n", raw_stdout);
1353 }
1354
1355 execute_command ( /*ui */ run, 0 /*from_tty */ );
1356
1357 if (!target_can_async_p ())
1358 {
1359 /* Do this before doing any printing. It would appear that some
1360 print code leaves garbage around in the buffer. */
1361 do_cleanups (old_cleanups);
1362 /* If the target was doing the operation synchronously we fake
1363 the stopped message. */
1364 if (last_async_command)
1365 fputs_unfiltered (last_async_command, raw_stdout);
1366 fputs_unfiltered ("*stopped", raw_stdout);
1367 mi_out_put (uiout, raw_stdout);
1368 mi_out_rewind (uiout);
1369 fputs_unfiltered ("\n", raw_stdout);
1370 return MI_CMD_QUIET;
1371 }
1372 return MI_CMD_DONE;
1373 }
1374
1375 void
1376 mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg)
1377 {
1378 if (last_async_command)
1379 fputs_unfiltered (last_async_command, raw_stdout);
1380 fputs_unfiltered ("*stopped", raw_stdout);
1381 mi_out_put (uiout, raw_stdout);
1382 fputs_unfiltered ("\n", raw_stdout);
1383 fputs_unfiltered ("(gdb) \n", raw_stdout);
1384 gdb_flush (raw_stdout);
1385 do_exec_cleanups (ALL_CLEANUPS);
1386 }
1387
1388 static char *
1389 mi_input (char *buf)
1390 {
1391 return gdb_readline (NULL);
1392 }
1393
1394 static void
1395 mi_load_progress (const char *section_name,
1396 unsigned long sent_so_far,
1397 unsigned long total_section,
1398 unsigned long total_sent,
1399 unsigned long grand_total)
1400 {
1401 struct timeval time_now, delta, update_threshold;
1402 static struct timeval last_update;
1403 static char *previous_sect_name = NULL;
1404 int new_section;
1405
1406 if (!interpreter_p || strncmp (interpreter_p, "mi", 2) != 0)
1407 return;
1408
1409 update_threshold.tv_sec = 0;
1410 update_threshold.tv_usec = 500000;
1411 gettimeofday (&time_now, NULL);
1412
1413 delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
1414 delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
1415
1416 if (delta.tv_usec < 0)
1417 {
1418 delta.tv_sec -= 1;
1419 delta.tv_usec += 1000000;
1420 }
1421
1422 new_section = (previous_sect_name ?
1423 strcmp (previous_sect_name, section_name) : 1);
1424 if (new_section)
1425 {
1426 struct cleanup *cleanup_tuple;
1427 xfree (previous_sect_name);
1428 previous_sect_name = xstrdup (section_name);
1429
1430 if (last_async_command)
1431 fputs_unfiltered (last_async_command, raw_stdout);
1432 fputs_unfiltered ("+download", raw_stdout);
1433 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1434 ui_out_field_string (uiout, "section", section_name);
1435 ui_out_field_int (uiout, "section-size", total_section);
1436 ui_out_field_int (uiout, "total-size", grand_total);
1437 do_cleanups (cleanup_tuple);
1438 mi_out_put (uiout, raw_stdout);
1439 fputs_unfiltered ("\n", raw_stdout);
1440 gdb_flush (raw_stdout);
1441 }
1442
1443 if (delta.tv_sec >= update_threshold.tv_sec &&
1444 delta.tv_usec >= update_threshold.tv_usec)
1445 {
1446 struct cleanup *cleanup_tuple;
1447 last_update.tv_sec = time_now.tv_sec;
1448 last_update.tv_usec = time_now.tv_usec;
1449 if (last_async_command)
1450 fputs_unfiltered (last_async_command, raw_stdout);
1451 fputs_unfiltered ("+download", raw_stdout);
1452 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1453 ui_out_field_string (uiout, "section", section_name);
1454 ui_out_field_int (uiout, "section-sent", sent_so_far);
1455 ui_out_field_int (uiout, "section-size", total_section);
1456 ui_out_field_int (uiout, "total-sent", total_sent);
1457 ui_out_field_int (uiout, "total-size", grand_total);
1458 do_cleanups (cleanup_tuple);
1459 mi_out_put (uiout, raw_stdout);
1460 fputs_unfiltered ("\n", raw_stdout);
1461 gdb_flush (raw_stdout);
1462 }
1463 }
1464
1465 static void
1466 mi_command_loop (int mi_version)
1467 {
1468 if (mi_version <= 1)
1469 {
1470 /* HACK: Force stdout/stderr to point at the console. This avoids
1471 any potential side effects caused by legacy code that is still
1472 using the TUI / fputs_unfiltered_hook */
1473 raw_stdout = stdio_fileopen (stdout);
1474 /* Route normal output through the MIx */
1475 gdb_stdout = mi_console_file_new (raw_stdout, "~");
1476 }
1477
1478 /* Route error and log output through the MI */
1479 gdb_stderr = mi_console_file_new (raw_stdout, "&");
1480 gdb_stdlog = gdb_stderr;
1481 /* Route target output through the MI. */
1482 gdb_stdtarg = mi_console_file_new (raw_stdout, "@");
1483
1484 /* HACK: Poke the ui_out table directly. Should we be creating a
1485 mi_out object wired up to the above gdb_stdout / gdb_stderr? */
1486 uiout = mi_out_new (mi_version);
1487
1488 /* HACK: Override any other interpreter hooks. We need to create a
1489 real event table and pass in that. */
1490 init_ui_hook = 0;
1491 /* command_loop_hook = 0; */
1492 print_frame_info_listing_hook = 0;
1493 query_hook = 0;
1494 warning_hook = 0;
1495 create_breakpoint_hook = 0;
1496 delete_breakpoint_hook = 0;
1497 modify_breakpoint_hook = 0;
1498 interactive_hook = 0;
1499 registers_changed_hook = 0;
1500 readline_begin_hook = 0;
1501 readline_hook = 0;
1502 readline_end_hook = 0;
1503 register_changed_hook = 0;
1504 memory_changed_hook = 0;
1505 context_hook = 0;
1506 target_wait_hook = 0;
1507 call_command_hook = 0;
1508 error_hook = 0;
1509 error_begin_hook = 0;
1510 show_load_progress = mi_load_progress;
1511
1512 /* Turn off 8 bit strings in quoted output. Any character with the
1513 high bit set is printed using C's octal format. */
1514 sevenbit_strings = 1;
1515
1516 /* Tell the world that we're alive */
1517 fputs_unfiltered ("(gdb) \n", raw_stdout);
1518 gdb_flush (raw_stdout);
1519
1520 if (!event_loop_p)
1521 simplified_command_loop (mi_input, mi_execute_command);
1522 else
1523 start_event_loop ();
1524 }
1525
1526 static void
1527 mi1_command_loop (void)
1528 {
1529 mi_command_loop (1);
1530 }
1531
1532 static void
1533 mi2_command_loop (void)
1534 {
1535 mi_command_loop (2);
1536 }
1537
1538 static void
1539 setup_architecture_data (void)
1540 {
1541 /* don't trust REGISTER_BYTES to be zero. */
1542 old_regs = xmalloc (REGISTER_BYTES + 1);
1543 memset (old_regs, 0, REGISTER_BYTES + 1);
1544 }
1545
1546 static void
1547 mi_init_ui (char *arg0)
1548 {
1549 if (strlen (interpreter_p) <= 2 ||
1550 interpreter_p[2] > '1')
1551 {
1552 /* HACK: Force stdout/stderr to point at the console. This avoids
1553 any potential side effects caused by legacy code that is still
1554 using the TUI / fputs_unfiltered_hook */
1555 raw_stdout = stdio_fileopen (stdout);
1556 /* Route normal output through the MIx */
1557 gdb_stdout = mi_console_file_new (raw_stdout, "~");
1558 }
1559 }
1560
1561 void
1562 _initialize_mi_main (void)
1563 {
1564 if (interpreter_p == NULL)
1565 return;
1566
1567 /* If we're _the_ interpreter, take control. */
1568 if (strcmp (interpreter_p, "mi") == 0)
1569 command_loop_hook = mi2_command_loop;
1570 else if (strcmp (interpreter_p, "mi1") == 0)
1571 command_loop_hook = mi1_command_loop;
1572 else if (strcmp (interpreter_p, "mi2") == 0)
1573 command_loop_hook = mi2_command_loop;
1574 else
1575 return;
1576
1577 init_ui_hook = mi_init_ui;
1578 setup_architecture_data ();
1579 register_gdbarch_swap (&old_regs, sizeof (old_regs), NULL);
1580 register_gdbarch_swap (NULL, 0, setup_architecture_data);
1581 if (event_loop_p)
1582 {
1583 /* These overwrite some of the initialization done in
1584 _intialize_event_loop. */
1585 call_readline = gdb_readline2;
1586 input_handler = mi_execute_command_wrapper;
1587 add_file_handler (input_fd, stdin_event_handler, 0);
1588 async_command_editing_p = 0;
1589 }
1590 /* FIXME: Should we notify main that we are here as a possible
1591 interpreter? */
1592 }