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