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