2002-10-02 Andrew Cagney <ac131313@redhat.com>
[deliverable/binutils-gdb.git] / gdb / mi / mi-main.c
CommitLineData
fb40c209 1/* MI Command Set.
8926118c 2 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
ab91fdd5 3 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22/* Work in progress */
23
24#include "defs.h"
25#include "target.h"
26#include "inferior.h"
27#include "gdb_string.h"
28#include "top.h"
29#include "gdbthread.h"
30#include "mi-cmds.h"
31#include "mi-parse.h"
32#include "mi-getopt.h"
33#include "mi-console.h"
34#include "ui-out.h"
35#include "mi-out.h"
36#include "event-loop.h"
37#include "event-top.h"
38#include "gdbcore.h" /* for write_memory() */
24e8cecf 39#include "value.h" /* for write_register_bytes() */
4e052eda 40#include "regcache.h"
5b7f31a4 41#include "gdb.h"
fb40c209
AC
42#include <ctype.h>
43#include <sys/time.h>
44
fb40c209
AC
45enum
46 {
47 FROM_TTY = 0
48 };
49
8d34ea23
KS
50/* Enumerations of the actions that may result from calling
51 captured_mi_execute_command */
52
53enum captured_mi_execute_command_actions
54 {
55 EXECUTE_COMMAND_DISPLAY_PROMPT,
56 EXECUTE_COMMAND_SUPRESS_PROMPT,
57 EXECUTE_COMMAND_DISPLAY_ERROR
58 };
59
60/* This structure is used to pass information from captured_mi_execute_command
61 to mi_execute_command. */
62struct captured_mi_execute_command_args
63{
64 /* This return result of the MI command (output) */
65 enum mi_cmd_result rc;
66
67 /* What action to perform when the call is finished (output) */
68 enum captured_mi_execute_command_actions action;
69
70 /* The command context to be executed (input) */
71 struct mi_parse *command;
72};
fb40c209
AC
73
74int mi_debug_p;
75struct ui_file *raw_stdout;
76
77/* The token of the last asynchronous command */
78static char *last_async_command;
79static char *previous_async_command;
80static char *mi_error_message;
81static char *old_regs;
82
83extern void _initialize_mi_main (void);
84static char *mi_input (char *);
85static void mi_execute_command (char *cmd, int from_tty);
86static enum mi_cmd_result mi_cmd_execute (struct mi_parse *parse);
87
88static void mi_execute_cli_command (const char *cli, char *args);
89static enum mi_cmd_result mi_execute_async_cli_command (char *mi, char *args, int from_tty);
90static void mi_execute_command_wrapper (char *cmd);
91
92void mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg);
fb40c209
AC
93
94static int register_changed_p (int regnum);
95static int get_register (int regnum, int format);
96static void mi_load_progress (const char *section_name,
97 unsigned long sent_so_far,
98 unsigned long total_section,
99 unsigned long total_sent,
100 unsigned long grand_total);
101
fb40c209
AC
102/* FIXME: these should go in some .h file, but infcmd.c doesn't have a
103 corresponding .h file. These wrappers will be obsolete anyway, once
104 we pull the plug on the sanitization. */
105extern void interrupt_target_command_wrapper (char *, int);
106extern void return_command_wrapper (char *, int);
fb40c209
AC
107
108/* Command implementations. FIXME: Is this libgdb? No. This is the MI
109 layer that calls libgdb. Any operation used in the below should be
110 formalized. */
111
112enum mi_cmd_result
113mi_cmd_gdb_exit (char *command, char **argv, int argc)
114{
115 /* We have to print everything right here because we never return */
116 if (last_async_command)
117 fputs_unfiltered (last_async_command, raw_stdout);
118 fputs_unfiltered ("^exit\n", raw_stdout);
119 mi_out_put (uiout, raw_stdout);
120 /* FIXME: The function called is not yet a formal libgdb function */
121 quit_force (NULL, FROM_TTY);
122 return MI_CMD_DONE;
123}
124
125enum mi_cmd_result
126mi_cmd_exec_run (char *args, int from_tty)
127{
128 /* FIXME: Should call a libgdb function, not a cli wrapper */
129 return mi_execute_async_cli_command ("run", args, from_tty);
130}
131
132enum mi_cmd_result
133mi_cmd_exec_next (char *args, int from_tty)
134{
135 /* FIXME: Should call a libgdb function, not a cli wrapper */
136 return mi_execute_async_cli_command ("next", args, from_tty);
137}
138
139enum mi_cmd_result
140mi_cmd_exec_next_instruction (char *args, int from_tty)
141{
142 /* FIXME: Should call a libgdb function, not a cli wrapper */
143 return mi_execute_async_cli_command ("nexti", args, from_tty);
144}
145
146enum mi_cmd_result
147mi_cmd_exec_step (char *args, int from_tty)
148{
149 /* FIXME: Should call a libgdb function, not a cli wrapper */
150 return mi_execute_async_cli_command ("step", args, from_tty);
151}
152
153enum mi_cmd_result
154mi_cmd_exec_step_instruction (char *args, int from_tty)
155{
156 /* FIXME: Should call a libgdb function, not a cli wrapper */
157 return mi_execute_async_cli_command ("stepi", args, from_tty);
158}
159
160enum mi_cmd_result
161mi_cmd_exec_finish (char *args, int from_tty)
162{
163 /* FIXME: Should call a libgdb function, not a cli wrapper */
164 return mi_execute_async_cli_command ("finish", args, from_tty);
165}
166
167enum mi_cmd_result
168mi_cmd_exec_until (char *args, int from_tty)
169{
170 /* FIXME: Should call a libgdb function, not a cli wrapper */
171 return mi_execute_async_cli_command ("until", args, from_tty);
172}
173
174enum mi_cmd_result
175mi_cmd_exec_return (char *args, int from_tty)
176{
fb40c209
AC
177 /* This command doesn't really execute the target, it just pops the
178 specified number of frames. */
179 if (*args)
180 /* Call return_command with from_tty argument equal to 0 so as to
181 avoid being queried. */
182 return_command_wrapper (args, 0);
183 else
184 /* Call return_command with from_tty argument equal to 0 so as to
185 avoid being queried. */
186 return_command_wrapper (NULL, 0);
187
188 /* Because we have called return_command with from_tty = 0, we need
189 to print the frame here. */
190 show_and_print_stack_frame (selected_frame,
b31da25e 191 frame_relative_level (selected_frame),
fb40c209 192 LOC_AND_ADDRESS);
fb40c209
AC
193
194 return MI_CMD_DONE;
195}
196
197enum mi_cmd_result
198mi_cmd_exec_continue (char *args, int from_tty)
199{
200 /* FIXME: Should call a libgdb function, not a cli wrapper */
201 return mi_execute_async_cli_command ("continue", args, from_tty);
202}
203
204/* Interrupt the execution of the target. Note how we must play around
205 with the token varialbes, in order to display the current token in
206 the result of the interrupt command, and the previous execution
207 token when the target finally stops. See comments in
208 mi_cmd_execute. */
209enum mi_cmd_result
210mi_cmd_exec_interrupt (char *args, int from_tty)
211{
fb40c209
AC
212 if (!target_executing)
213 {
76995688
AC
214 xasprintf (&mi_error_message,
215 "mi_cmd_exec_interrupt: Inferior not executing.");
fb40c209
AC
216 return MI_CMD_ERROR;
217 }
218 interrupt_target_command_wrapper (args, from_tty);
219 if (last_async_command)
220 fputs_unfiltered (last_async_command, raw_stdout);
221 fputs_unfiltered ("^done", raw_stdout);
b8c9b27d 222 xfree (last_async_command);
fb40c209
AC
223 if (previous_async_command)
224 last_async_command = xstrdup (previous_async_command);
b8c9b27d 225 xfree (previous_async_command);
fb40c209
AC
226 previous_async_command = NULL;
227 mi_out_put (uiout, raw_stdout);
228 mi_out_rewind (uiout);
229 fputs_unfiltered ("\n", raw_stdout);
fb40c209
AC
230 return MI_CMD_QUIET;
231}
232
233enum mi_cmd_result
234mi_cmd_thread_select (char *command, char **argv, int argc)
235{
236 enum gdb_rc rc;
237
238 if (argc != 1)
239 {
76995688
AC
240 xasprintf (&mi_error_message,
241 "mi_cmd_thread_select: USAGE: threadnum.");
fb40c209
AC
242 return MI_CMD_ERROR;
243 }
244 else
2b65245e 245 rc = gdb_thread_select (uiout, argv[0]);
fb40c209
AC
246
247 if (rc == GDB_RC_FAIL)
248 return MI_CMD_CAUGHT_ERROR;
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);
918 int row;
919 int row_byte;
45399be4 920 ui_out_list_begin (uiout, "memory");
fb40c209
AC
921 for (row = 0, row_byte = 0;
922 row < nr_rows;
923 row++, row_byte += nr_cols * word_size)
924 {
925 int col;
926 int col_byte;
666547aa 927 ui_out_tuple_begin (uiout, NULL);
fb40c209
AC
928 ui_out_field_core_addr (uiout, "addr", addr + row_byte);
929 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
45399be4 930 ui_out_list_begin (uiout, "data");
fb40c209
AC
931 for (col = 0, col_byte = row_byte;
932 col < nr_cols;
933 col++, col_byte += word_size)
934 {
935 if (col_byte + word_size > nr_bytes)
936 {
937 ui_out_field_string (uiout, NULL, "N/A");
938 }
939 else
940 {
941 ui_file_rewind (stream->stream);
942 print_scalar_formatted (mbuf + col_byte, word_type, word_format,
943 word_asize, stream->stream);
944 ui_out_field_stream (uiout, NULL, stream);
945 }
946 }
45399be4 947 ui_out_list_end (uiout);
fb40c209
AC
948 if (aschar)
949 {
950 int byte;
951 ui_file_rewind (stream->stream);
952 for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
953 {
954 if (byte >= nr_bytes)
955 {
956 fputc_unfiltered ('X', stream->stream);
957 }
958 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
959 {
960 fputc_unfiltered (aschar, stream->stream);
961 }
962 else
963 fputc_unfiltered (mbuf[byte], stream->stream);
964 }
965 ui_out_field_stream (uiout, "ascii", stream);
966 }
666547aa 967 ui_out_tuple_end (uiout);
fb40c209
AC
968 }
969 ui_out_stream_delete (stream);
45399be4 970 ui_out_list_end (uiout);
fb40c209
AC
971 }
972 do_cleanups (cleanups);
973 return MI_CMD_DONE;
974}
975
976/* DATA-MEMORY-WRITE:
977
978 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
979 offset from the beginning of the memory grid row where the cell to
980 be written is.
981 ADDR: start address of the row in the memory grid where the memory
982 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
983 the location to write to.
984 FORMAT: a char indicating format for the ``word''. See
985 the ``x'' command.
986 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
987 VALUE: value to be written into the memory address.
988
989 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
990
991 Prints nothing. */
992enum mi_cmd_result
993mi_cmd_data_write_memory (char *command, char **argv, int argc)
994{
995 CORE_ADDR addr;
996 char word_format;
997 long word_size;
998 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
999 enough when using a compiler other than GCC. */
1000 LONGEST value;
d8bf3afa
KB
1001 void *buffer;
1002 struct cleanup *old_chain;
fb40c209
AC
1003 long offset = 0;
1004 int optind = 0;
1005 char *optarg;
1006 enum opt
1007 {
1008 OFFSET_OPT
1009 };
1010 static struct mi_opt opts[] =
1011 {
1012 {"o", OFFSET_OPT, 1},
1013 0
1014 };
1015
1016 while (1)
1017 {
1018 int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
1019 &optind, &optarg);
1020 if (opt < 0)
1021 break;
1022 switch ((enum opt) opt)
1023 {
1024 case OFFSET_OPT:
1025 offset = atol (optarg);
1026 break;
1027 }
1028 }
1029 argv += optind;
1030 argc -= optind;
1031
1032 if (argc != 4)
1033 {
76995688
AC
1034 xasprintf (&mi_error_message,
1035 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
fb40c209
AC
1036 return MI_CMD_ERROR;
1037 }
1038
1039 /* Extract all the arguments. */
1040 /* Start address of the memory dump. */
1041 addr = parse_and_eval_address (argv[0]);
1042 /* The format character to use when displaying a memory word. See
1043 the ``x'' command. */
1044 word_format = argv[1][0];
1045 /* The size of the memory word. */
1046 word_size = atol (argv[2]);
1047
1048 /* Calculate the real address of the write destination. */
1049 addr += (offset * word_size);
1050
1051 /* Get the value as a number */
1052 value = parse_and_eval_address (argv[3]);
1053 /* Get the value into an array */
d8bf3afa
KB
1054 buffer = xmalloc (word_size);
1055 old_chain = make_cleanup (xfree, buffer);
fb40c209
AC
1056 store_signed_integer (buffer, word_size, value);
1057 /* Write it down to memory */
1058 write_memory (addr, buffer, word_size);
d8bf3afa
KB
1059 /* Free the buffer. */
1060 do_cleanups (old_chain);
fb40c209
AC
1061
1062 return MI_CMD_DONE;
1063}
1064
8d34ea23
KS
1065/* Execute a command within a safe environment.
1066 Return <0 for error; >=0 for ok.
1067
1068 args->action will tell mi_execute_command what action
1069 to perfrom after the given command has executed (display/supress
1070 prompt, display error). */
fb40c209
AC
1071
1072static int
8d34ea23 1073captured_mi_execute_command (struct ui_out *uiout, void *data)
fb40c209 1074{
8d34ea23
KS
1075 struct captured_mi_execute_command_args *args =
1076 (struct captured_mi_execute_command_args *) data;
1077 struct mi_parse *context = args->command;
fb40c209
AC
1078
1079 switch (context->op)
1080 {
1081
1082 case MI_COMMAND:
1083 /* A MI command was read from the input stream */
1084 if (mi_debug_p)
1085 /* FIXME: gdb_???? */
1086 fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
1087 context->token, context->command, context->args);
1088 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1089 condition expression, each function should return an
1090 indication of what action is required and then switch on
1091 that. */
8d34ea23
KS
1092 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1093 args->rc = mi_cmd_execute (context);
1094
fb40c209
AC
1095 if (!target_can_async_p () || !target_executing)
1096 {
1097 /* print the result if there were no errors */
8d34ea23 1098 if (args->rc == MI_CMD_DONE)
fb40c209
AC
1099 {
1100 fputs_unfiltered (context->token, raw_stdout);
1101 fputs_unfiltered ("^done", raw_stdout);
1102 mi_out_put (uiout, raw_stdout);
1103 mi_out_rewind (uiout);
1104 fputs_unfiltered ("\n", raw_stdout);
1105 }
8d34ea23 1106 else if (args->rc == MI_CMD_ERROR)
fb40c209
AC
1107 {
1108 if (mi_error_message)
1109 {
1110 fputs_unfiltered (context->token, raw_stdout);
1111 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1112 fputstr_unfiltered (mi_error_message, '"', raw_stdout);
b8c9b27d 1113 xfree (mi_error_message);
fb40c209
AC
1114 fputs_unfiltered ("\"\n", raw_stdout);
1115 }
1116 mi_out_rewind (uiout);
1117 }
8d34ea23 1118 else if (args->rc == MI_CMD_CAUGHT_ERROR)
fb40c209
AC
1119 {
1120 mi_out_rewind (uiout);
8d34ea23
KS
1121 args->action = EXECUTE_COMMAND_DISPLAY_ERROR;
1122 return 1;
fb40c209
AC
1123 }
1124 else
1125 mi_out_rewind (uiout);
1126 }
1127 else if (sync_execution)
8d34ea23
KS
1128 {
1129 /* Don't print the prompt. We are executing the target in
1130 synchronous mode. */
1131 args->action = EXECUTE_COMMAND_SUPRESS_PROMPT;
1132 return 1;
1133 }
fb40c209
AC
1134 break;
1135
1136 case CLI_COMMAND:
1137 /* A CLI command was read from the input stream */
1138 /* This will be removed as soon as we have a complete set of
1139 mi commands */
1140 /* echo the command on the console. */
1141 fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1142 /* FIXME: If the command string has something that looks like
1143 a format spec (e.g. %s) we will get a core dump */
1144 mi_execute_cli_command ("%s", context->command);
1145 /* print the result */
1146 /* FIXME: Check for errors here. */
1147 fputs_unfiltered (context->token, raw_stdout);
1148 fputs_unfiltered ("^done", raw_stdout);
1149 mi_out_put (uiout, raw_stdout);
1150 mi_out_rewind (uiout);
1151 fputs_unfiltered ("\n", raw_stdout);
8d34ea23
KS
1152 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1153 args->rc = MI_CMD_DONE;
fb40c209
AC
1154 break;
1155
1156 }
8d34ea23 1157
fb40c209
AC
1158 return 1;
1159}
1160
1161
1162void
1163mi_execute_command (char *cmd, int from_tty)
1164{
1165 struct mi_parse *command;
8d34ea23
KS
1166 struct captured_mi_execute_command_args args;
1167 struct ui_out *saved_uiout = uiout;
1168 int result, rc;
fb40c209
AC
1169
1170 /* This is to handle EOF (^D). We just quit gdb. */
1171 /* FIXME: we should call some API function here. */
1172 if (cmd == 0)
1173 quit_force (NULL, from_tty);
1174
1175 command = mi_parse (cmd);
1176
1177 if (command != NULL)
1178 {
8d34ea23 1179 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
fb40c209 1180 be pushed even further down or even eliminated? */
8d34ea23
KS
1181 args.command = command;
1182 result = catch_exceptions (uiout, captured_mi_execute_command, &args, "",
1183 RETURN_MASK_ALL);
1184
1185 if (args.action == EXECUTE_COMMAND_SUPRESS_PROMPT)
fb40c209
AC
1186 {
1187 /* The command is executing synchronously. Bail out early
1188 suppressing the finished prompt. */
1189 mi_parse_free (command);
1190 return;
1191 }
8d34ea23 1192 if (args.action == EXECUTE_COMMAND_DISPLAY_ERROR || result < 0)
fb40c209
AC
1193 {
1194 char *msg = error_last_message ();
b8c9b27d 1195 struct cleanup *cleanup = make_cleanup (xfree, msg);
fb40c209
AC
1196 /* The command execution failed and error() was called
1197 somewhere */
1198 fputs_unfiltered (command->token, raw_stdout);
1199 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1200 fputstr_unfiltered (msg, '"', raw_stdout);
1201 fputs_unfiltered ("\"\n", raw_stdout);
1202 }
1203 mi_parse_free (command);
1204 }
1205
fb40c209 1206 fputs_unfiltered ("(gdb) \n", raw_stdout);
a433f9e4 1207 gdb_flush (raw_stdout);
fb40c209
AC
1208 /* print any buffered hook code */
1209 /* ..... */
1210}
1211
1212static enum mi_cmd_result
1213mi_cmd_execute (struct mi_parse *parse)
1214{
1215 if (parse->cmd->argv_func != NULL
1216 || parse->cmd->args_func != NULL)
1217 {
1218 /* FIXME: We need to save the token because the command executed
1219 may be asynchronous and need to print the token again.
1220 In the future we can pass the token down to the func
1221 and get rid of the last_async_command */
1222 /* The problem here is to keep the token around when we launch
1223 the target, and we want to interrupt it later on. The
1224 interrupt command will have its own token, but when the
1225 target stops, we must display the token corresponding to the
1226 last execution command given. So we have another string where
1227 we copy the token (previous_async_command), if this was
1228 indeed the token of an execution command, and when we stop we
1229 print that one. This is possible because the interrupt
1230 command, when over, will copy that token back into the
1231 default token string (last_async_command). */
1232
1233 if (target_executing)
1234 {
1235 if (!previous_async_command)
1236 previous_async_command = xstrdup (last_async_command);
1237 if (strcmp (parse->command, "exec-interrupt"))
1238 {
1239 fputs_unfiltered (parse->token, raw_stdout);
1240 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1241 fputs_unfiltered ("Cannot execute command ", raw_stdout);
1242 fputstr_unfiltered (parse->command, '"', raw_stdout);
1243 fputs_unfiltered (" while target running", raw_stdout);
1244 fputs_unfiltered ("\"\n", raw_stdout);
1245 return MI_CMD_ERROR;
1246 }
1247 }
1248 last_async_command = xstrdup (parse->token);
e2f9c474 1249 make_exec_cleanup (free_current_contents, &last_async_command);
fb40c209
AC
1250 /* FIXME: DELETE THIS! */
1251 if (parse->cmd->args_func != NULL)
1252 return parse->cmd->args_func (parse->args, 0 /*from_tty */ );
1253 return parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
1254 }
1255 else if (parse->cmd->cli != 0)
1256 {
1257 /* FIXME: DELETE THIS. */
1258 /* The operation is still implemented by a cli command */
1259 /* Must be a synchronous one */
1260 mi_execute_cli_command (parse->cmd->cli, parse->args);
1261 return MI_CMD_DONE;
1262 }
1263 else
1264 {
1265 /* FIXME: DELETE THIS. */
1266 fputs_unfiltered (parse->token, raw_stdout);
1267 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1268 fputs_unfiltered ("Undefined mi command: ", raw_stdout);
1269 fputstr_unfiltered (parse->command, '"', raw_stdout);
1270 fputs_unfiltered (" (missing implementation)", raw_stdout);
1271 fputs_unfiltered ("\"\n", raw_stdout);
1272 return MI_CMD_ERROR;
1273 }
1274}
1275
fb40c209
AC
1276static void
1277mi_execute_command_wrapper (char *cmd)
1278{
1279 mi_execute_command (cmd, stdin == instream);
1280}
1281
1282/* FIXME: This is just a hack so we can get some extra commands going.
1283 We don't want to channel things through the CLI, but call libgdb directly */
1284/* Use only for synchronous commands */
1285
1286void
1287mi_execute_cli_command (const char *cli, char *args)
1288{
1289 if (cli != 0)
1290 {
1291 struct cleanup *old_cleanups;
1292 char *run;
76995688 1293 xasprintf (&run, cli, args);
fb40c209
AC
1294 if (mi_debug_p)
1295 /* FIXME: gdb_???? */
1296 fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
1297 cli, run);
b8c9b27d 1298 old_cleanups = make_cleanup (xfree, run);
fb40c209
AC
1299 execute_command ( /*ui */ run, 0 /*from_tty */ );
1300 do_cleanups (old_cleanups);
1301 return;
1302 }
1303}
1304
1305enum mi_cmd_result
1306mi_execute_async_cli_command (char *mi, char *args, int from_tty)
1307{
1308 struct cleanup *old_cleanups;
1309 char *run;
1310 char *async_args;
1311
1312 if (target_can_async_p ())
1313 {
1314 async_args = (char *) xmalloc (strlen (args) + 2);
1315 make_exec_cleanup (free, async_args);
1316 strcpy (async_args, args);
1317 strcat (async_args, "&");
76995688 1318 xasprintf (&run, "%s %s", mi, async_args);
fb40c209
AC
1319 make_exec_cleanup (free, run);
1320 add_continuation (mi_exec_async_cli_cmd_continuation, NULL);
6311b07d 1321 old_cleanups = NULL;
fb40c209
AC
1322 }
1323 else
1324 {
76995688 1325 xasprintf (&run, "%s %s", mi, args);
b8c9b27d 1326 old_cleanups = make_cleanup (xfree, run);
fb40c209
AC
1327 }
1328
1329 if (!target_can_async_p ())
1330 {
1331 /* NOTE: For synchronous targets asynchronous behavour is faked by
1332 printing out the GDB prompt before we even try to execute the
1333 command. */
1334 if (last_async_command)
1335 fputs_unfiltered (last_async_command, raw_stdout);
1336 fputs_unfiltered ("^running\n", raw_stdout);
1337 fputs_unfiltered ("(gdb) \n", raw_stdout);
a433f9e4 1338 gdb_flush (raw_stdout);
fb40c209
AC
1339 }
1340 else
1341 {
1342 /* FIXME: cagney/1999-11-29: Printing this message before
1343 calling execute_command is wrong. It should only be printed
1344 once gdb has confirmed that it really has managed to send a
1345 run command to the target. */
1346 if (last_async_command)
1347 fputs_unfiltered (last_async_command, raw_stdout);
1348 fputs_unfiltered ("^running\n", raw_stdout);
1349 }
1350
1351 execute_command ( /*ui */ run, 0 /*from_tty */ );
1352
1353 if (!target_can_async_p ())
1354 {
1355 /* Do this before doing any printing. It would appear that some
1356 print code leaves garbage around in the buffer. */
1357 do_cleanups (old_cleanups);
1358 /* If the target was doing the operation synchronously we fake
1359 the stopped message. */
1360 if (last_async_command)
1361 fputs_unfiltered (last_async_command, raw_stdout);
1362 fputs_unfiltered ("*stopped", raw_stdout);
1363 mi_out_put (uiout, raw_stdout);
1364 mi_out_rewind (uiout);
1365 fputs_unfiltered ("\n", raw_stdout);
1366 return MI_CMD_QUIET;
1367 }
1368 return MI_CMD_DONE;
1369}
1370
1371void
1372mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg)
1373{
1374 if (last_async_command)
1375 fputs_unfiltered (last_async_command, raw_stdout);
1376 fputs_unfiltered ("*stopped", raw_stdout);
1377 mi_out_put (uiout, raw_stdout);
1378 fputs_unfiltered ("\n", raw_stdout);
1379 fputs_unfiltered ("(gdb) \n", raw_stdout);
a433f9e4 1380 gdb_flush (raw_stdout);
fb40c209
AC
1381 do_exec_cleanups (ALL_CLEANUPS);
1382}
1383
1384static char *
1385mi_input (char *buf)
1386{
1387 return gdb_readline (NULL);
1388}
1389
1390static void
1391mi_load_progress (const char *section_name,
1392 unsigned long sent_so_far,
1393 unsigned long total_section,
1394 unsigned long total_sent,
1395 unsigned long grand_total)
1396{
1397 struct timeval time_now, delta, update_threshold;
1398 static struct timeval last_update;
1399 static char *previous_sect_name = NULL;
1400 int new_section;
1401
b30bf9ee 1402 if (!interpreter_p || strncmp (interpreter_p, "mi", 2) != 0)
fb40c209
AC
1403 return;
1404
1405 update_threshold.tv_sec = 0;
1406 update_threshold.tv_usec = 500000;
1407 gettimeofday (&time_now, NULL);
1408
1409 delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
1410 delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
1411
1412 if (delta.tv_usec < 0)
1413 {
1414 delta.tv_sec -= 1;
1415 delta.tv_usec += 1000000;
1416 }
1417
1418 new_section = (previous_sect_name ?
1419 strcmp (previous_sect_name, section_name) : 1);
1420 if (new_section)
1421 {
b8c9b27d 1422 xfree (previous_sect_name);
fb40c209
AC
1423 previous_sect_name = xstrdup (section_name);
1424
1425 if (last_async_command)
1426 fputs_unfiltered (last_async_command, raw_stdout);
1427 fputs_unfiltered ("+download", raw_stdout);
666547aa 1428 ui_out_tuple_begin (uiout, NULL);
fb40c209
AC
1429 ui_out_field_string (uiout, "section", section_name);
1430 ui_out_field_int (uiout, "section-size", total_section);
1431 ui_out_field_int (uiout, "total-size", grand_total);
666547aa 1432 ui_out_tuple_end (uiout);
fb40c209
AC
1433 mi_out_put (uiout, raw_stdout);
1434 fputs_unfiltered ("\n", raw_stdout);
1435 gdb_flush (raw_stdout);
1436 }
1437
1438 if (delta.tv_sec >= update_threshold.tv_sec &&
1439 delta.tv_usec >= update_threshold.tv_usec)
1440 {
1441 last_update.tv_sec = time_now.tv_sec;
1442 last_update.tv_usec = time_now.tv_usec;
1443 if (last_async_command)
1444 fputs_unfiltered (last_async_command, raw_stdout);
1445 fputs_unfiltered ("+download", raw_stdout);
666547aa 1446 ui_out_tuple_begin (uiout, NULL);
fb40c209
AC
1447 ui_out_field_string (uiout, "section", section_name);
1448 ui_out_field_int (uiout, "section-sent", sent_so_far);
1449 ui_out_field_int (uiout, "section-size", total_section);
1450 ui_out_field_int (uiout, "total-sent", total_sent);
1451 ui_out_field_int (uiout, "total-size", grand_total);
666547aa 1452 ui_out_tuple_end (uiout);
fb40c209
AC
1453 mi_out_put (uiout, raw_stdout);
1454 fputs_unfiltered ("\n", raw_stdout);
1455 gdb_flush (raw_stdout);
1456 }
1457}
1458
1459static void
b30bf9ee 1460mi_command_loop (int mi_version)
fb40c209
AC
1461{
1462 /* HACK: Force stdout/stderr to point at the console. This avoids
1463 any potential side effects caused by legacy code that is still
1464 using the TUI / fputs_unfiltered_hook */
1465 raw_stdout = stdio_fileopen (stdout);
1466 /* Route normal output through the MIx */
1467 gdb_stdout = mi_console_file_new (raw_stdout, "~");
1468 /* Route error and log output through the MI */
1469 gdb_stderr = mi_console_file_new (raw_stdout, "&");
1470 gdb_stdlog = gdb_stderr;
1471 /* Route target output through the MI. */
1472 gdb_stdtarg = mi_console_file_new (raw_stdout, "@");
1473
1474 /* HACK: Poke the ui_out table directly. Should we be creating a
1475 mi_out object wired up to the above gdb_stdout / gdb_stderr? */
b30bf9ee 1476 uiout = mi_out_new (mi_version);
fb40c209
AC
1477
1478 /* HACK: Override any other interpreter hooks. We need to create a
1479 real event table and pass in that. */
1480 init_ui_hook = 0;
1481 /* command_loop_hook = 0; */
1482 print_frame_info_listing_hook = 0;
1483 query_hook = 0;
1484 warning_hook = 0;
1485 create_breakpoint_hook = 0;
1486 delete_breakpoint_hook = 0;
1487 modify_breakpoint_hook = 0;
1488 interactive_hook = 0;
1489 registers_changed_hook = 0;
1490 readline_begin_hook = 0;
1491 readline_hook = 0;
1492 readline_end_hook = 0;
1493 register_changed_hook = 0;
1494 memory_changed_hook = 0;
1495 context_hook = 0;
1496 target_wait_hook = 0;
1497 call_command_hook = 0;
1498 error_hook = 0;
1499 error_begin_hook = 0;
1500 show_load_progress = mi_load_progress;
1501
1502 /* Turn off 8 bit strings in quoted output. Any character with the
1503 high bit set is printed using C's octal format. */
1504 sevenbit_strings = 1;
1505
1506 /* Tell the world that we're alive */
1507 fputs_unfiltered ("(gdb) \n", raw_stdout);
a433f9e4 1508 gdb_flush (raw_stdout);
fb40c209
AC
1509
1510 if (!event_loop_p)
1511 simplified_command_loop (mi_input, mi_execute_command);
1512 else
1513 start_event_loop ();
1514}
1515
b30bf9ee 1516static void
da0f9dcd 1517mi1_command_loop (void)
b30bf9ee 1518{
da0f9dcd 1519 mi_command_loop (1);
b30bf9ee
AC
1520}
1521
1522static void
da0f9dcd 1523mi2_command_loop (void)
b30bf9ee 1524{
da0f9dcd 1525 mi_command_loop (2);
b30bf9ee
AC
1526}
1527
fb40c209 1528static void
fba45db2 1529setup_architecture_data (void)
fb40c209
AC
1530{
1531 /* don't trust REGISTER_BYTES to be zero. */
1532 old_regs = xmalloc (REGISTER_BYTES + 1);
1533 memset (old_regs, 0, REGISTER_BYTES + 1);
1534}
1535
1536static void
fba45db2 1537mi_init_ui (char *arg0)
fb40c209
AC
1538{
1539 /* Eventually this will contain code that takes control of the
1540 console. */
1541}
1542
1543void
fba45db2 1544_initialize_mi_main (void)
fb40c209 1545{
b30bf9ee
AC
1546 if (interpreter_p == NULL)
1547 return;
1548
fb40c209 1549 /* If we're _the_ interpreter, take control. */
da0f9dcd
AC
1550 if (strcmp (interpreter_p, "mi") == 0)
1551 command_loop_hook = mi2_command_loop;
1552 else if (strcmp (interpreter_p, "mi1") == 0)
b30bf9ee 1553 command_loop_hook = mi1_command_loop;
da0f9dcd
AC
1554 else if (strcmp (interpreter_p, "mi2") == 0)
1555 command_loop_hook = mi2_command_loop;
b30bf9ee
AC
1556 else
1557 return;
1558
1559 init_ui_hook = mi_init_ui;
1560 setup_architecture_data ();
1561 register_gdbarch_swap (&old_regs, sizeof (old_regs), NULL);
1562 register_gdbarch_swap (NULL, 0, setup_architecture_data);
1563 if (event_loop_p)
fb40c209 1564 {
b30bf9ee
AC
1565 /* These overwrite some of the initialization done in
1566 _intialize_event_loop. */
1567 call_readline = gdb_readline2;
1568 input_handler = mi_execute_command_wrapper;
1569 add_file_handler (input_fd, stdin_event_handler, 0);
1570 async_command_editing_p = 0;
fb40c209
AC
1571 }
1572 /* FIXME: Should we notify main that we are here as a possible
1573 interpreter? */
1574}
This page took 0.346562 seconds and 4 git commands to generate.