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