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