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