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