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