2003-06-08 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.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{
d9d9c31f 401 char raw_buffer[MAX_REGISTER_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{
0c92afe8
AC
514 char raw_buffer[MAX_REGISTER_SIZE];
515 char virtual_buffer[MAX_REGISTER_SIZE];
fb40c209 516 int optim;
ac2adee5
AC
517 int realnum;
518 CORE_ADDR addr;
519 enum lval_type lval;
fb40c209
AC
520 static struct ui_stream *stb = NULL;
521
522 stb = ui_out_stream_new (uiout);
523
524 if (format == 'N')
525 format = 0;
526
ac2adee5
AC
527 frame_register (deprecated_selected_frame, regnum, &optim, &lval, &addr,
528 &realnum, raw_buffer);
529
fb40c209
AC
530 if (optim)
531 {
76995688 532 xasprintf (&mi_error_message, "Optimized out");
fb40c209
AC
533 return -1;
534 }
535
536 /* Convert raw data to virtual format if necessary. */
537
538 if (REGISTER_CONVERTIBLE (regnum))
539 {
7b83296f
AC
540 REGISTER_CONVERT_TO_VIRTUAL (regnum,
541 register_type (current_gdbarch, regnum),
fb40c209
AC
542 raw_buffer, virtual_buffer);
543 }
544 else
545 memcpy (virtual_buffer, raw_buffer, REGISTER_VIRTUAL_SIZE (regnum));
546
547 if (format == 'r')
548 {
549 int j;
550 char *ptr, buf[1024];
551
552 strcpy (buf, "0x");
553 ptr = buf + 2;
554 for (j = 0; j < REGISTER_RAW_SIZE (regnum); j++)
555 {
d7449b42 556 register int idx = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? j
fb40c209
AC
557 : REGISTER_RAW_SIZE (regnum) - 1 - j;
558 sprintf (ptr, "%02x", (unsigned char) raw_buffer[idx]);
559 ptr += 2;
560 }
561 ui_out_field_string (uiout, "value", buf);
562 /*fputs_filtered (buf, gdb_stdout); */
563 }
564 else
565 {
7b83296f 566 val_print (register_type (current_gdbarch, regnum), virtual_buffer, 0, 0,
fb40c209
AC
567 stb->stream, format, 1, 0, Val_pretty_default);
568 ui_out_field_stream (uiout, "value", stb);
569 ui_out_stream_delete (stb);
570 }
571 return 1;
572}
573
24e8cecf
EZ
574/* Write given values into registers. The registers and values are
575 given as pairs. The corresponding MI command is
576 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
577enum mi_cmd_result
578mi_cmd_data_write_register_values (char *command, char **argv, int argc)
579{
580 int regnum;
581 int i;
582 int numregs;
24e8cecf
EZ
583 LONGEST value;
584 char format;
585
586 /* Note that the test for a valid register must include checking the
587 REGISTER_NAME because NUM_REGS may be allocated for the union of
588 the register sets within a family of related processors. In this
589 case, some entries of REGISTER_NAME will change depending upon
590 the particular processor being debugged. */
591
a728f042 592 numregs = NUM_REGS;
24e8cecf
EZ
593
594 if (argc == 0)
595 {
76995688
AC
596 xasprintf (&mi_error_message,
597 "mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
24e8cecf
EZ
598 return MI_CMD_ERROR;
599 }
600
601 format = (int) argv[0][0];
602
603 if (!target_has_registers)
604 {
76995688
AC
605 xasprintf (&mi_error_message,
606 "mi_cmd_data_write_register_values: No registers.");
24e8cecf
EZ
607 return MI_CMD_ERROR;
608 }
609
610 if (!(argc - 1))
611 {
76995688
AC
612 xasprintf (&mi_error_message,
613 "mi_cmd_data_write_register_values: No regs and values specified.");
24e8cecf
EZ
614 return MI_CMD_ERROR;
615 }
616
617 if ((argc - 1) % 2)
618 {
76995688
AC
619 xasprintf (&mi_error_message,
620 "mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
24e8cecf
EZ
621 return MI_CMD_ERROR;
622 }
623
624 for (i = 1; i < argc; i = i + 2)
625 {
626 regnum = atoi (argv[i]);
627
628 if (regnum >= 0
629 && regnum < numregs
630 && REGISTER_NAME (regnum) != NULL
631 && *REGISTER_NAME (regnum) != '\000')
632 {
d8bf3afa
KB
633 void *buffer;
634 struct cleanup *old_chain;
635
24e8cecf
EZ
636 /* Get the value as a number */
637 value = parse_and_eval_address (argv[i + 1]);
638 /* Get the value into an array */
b1e29e33 639 buffer = xmalloc (DEPRECATED_REGISTER_SIZE);
d8bf3afa 640 old_chain = make_cleanup (xfree, buffer);
b1e29e33 641 store_signed_integer (buffer, DEPRECATED_REGISTER_SIZE, value);
24e8cecf 642 /* Write it down */
73937e03 643 deprecated_write_register_bytes (REGISTER_BYTE (regnum), buffer, REGISTER_RAW_SIZE (regnum));
d8bf3afa
KB
644 /* Free the buffer. */
645 do_cleanups (old_chain);
24e8cecf
EZ
646 }
647 else
648 {
76995688 649 xasprintf (&mi_error_message, "bad register number");
24e8cecf
EZ
650 return MI_CMD_ERROR;
651 }
652 }
653 return MI_CMD_DONE;
654}
655
fb40c209
AC
656#if 0
657/*This is commented out because we decided it was not useful. I leave
658 it, just in case. ezannoni:1999-12-08 */
659
660/* Assign a value to a variable. The expression argument must be in
661 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
662 quoted. */
663enum mi_cmd_result
664mi_cmd_data_assign (char *command, char **argv, int argc)
665{
666 struct expression *expr;
667 struct cleanup *old_chain;
668
669 if (argc != 1)
670 {
76995688
AC
671 xasprintf (&mi_error_message,
672 "mi_cmd_data_assign: Usage: -data-assign expression");
fb40c209
AC
673 return MI_CMD_ERROR;
674 }
675
676 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
677 01-12-1999: Need to decide what to do with this for libgdb purposes. */
678
679 expr = parse_expression (argv[0]);
47cf603e 680 old_chain = make_cleanup (free_current_contents, &expr);
fb40c209
AC
681 evaluate_expression (expr);
682 do_cleanups (old_chain);
683 return MI_CMD_DONE;
684}
685#endif
686
687/* Evaluate the value of the argument. The argument is an
688 expression. If the expression contains spaces it needs to be
689 included in double quotes. */
690enum mi_cmd_result
691mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
692{
693 struct expression *expr;
694 struct cleanup *old_chain = NULL;
96052a95 695 struct value *val;
fb40c209
AC
696 struct ui_stream *stb = NULL;
697
698 stb = ui_out_stream_new (uiout);
699
700 if (argc != 1)
701 {
76995688
AC
702 xasprintf (&mi_error_message,
703 "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
fb40c209
AC
704 return MI_CMD_ERROR;
705 }
706
707 expr = parse_expression (argv[0]);
708
47cf603e 709 old_chain = make_cleanup (free_current_contents, &expr);
fb40c209
AC
710
711 val = evaluate_expression (expr);
712
713 /* Print the result of the expression evaluation. */
714 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val),
715 VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
716 stb->stream, 0, 0, 0, 0);
717
718 ui_out_field_stream (uiout, "value", stb);
719 ui_out_stream_delete (stb);
720
721 do_cleanups (old_chain);
722
723 return MI_CMD_DONE;
724}
725
726enum mi_cmd_result
727mi_cmd_target_download (char *args, int from_tty)
728{
729 char *run;
730 struct cleanup *old_cleanups = NULL;
731
76995688 732 xasprintf (&run, "load %s", args);
b8c9b27d 733 old_cleanups = make_cleanup (xfree, run);
fb40c209
AC
734 execute_command (run, from_tty);
735
736 do_cleanups (old_cleanups);
737 return MI_CMD_DONE;
738}
739
740/* Connect to the remote target. */
741enum mi_cmd_result
742mi_cmd_target_select (char *args, int from_tty)
743{
744 char *run;
745 struct cleanup *old_cleanups = NULL;
746
76995688 747 xasprintf (&run, "target %s", args);
b8c9b27d 748 old_cleanups = make_cleanup (xfree, run);
fb40c209
AC
749
750 /* target-select is always synchronous. once the call has returned
751 we know that we are connected. */
752 /* NOTE: At present all targets that are connected are also
753 (implicitly) talking to a halted target. In the future this may
754 change. */
755 execute_command (run, from_tty);
756
757 do_cleanups (old_cleanups);
758
759 /* Issue the completion message here. */
760 if (last_async_command)
761 fputs_unfiltered (last_async_command, raw_stdout);
762 fputs_unfiltered ("^connected", raw_stdout);
763 mi_out_put (uiout, raw_stdout);
764 mi_out_rewind (uiout);
765 fputs_unfiltered ("\n", raw_stdout);
766 do_exec_cleanups (ALL_CLEANUPS);
767 return MI_CMD_QUIET;
768}
769
770/* DATA-MEMORY-READ:
771
772 ADDR: start address of data to be dumped.
773 WORD-FORMAT: a char indicating format for the ``word''. See
774 the ``x'' command.
775 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
776 NR_ROW: Number of rows.
777 NR_COL: The number of colums (words per row).
778 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
779 ASCHAR for unprintable characters.
780
781 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
782 displayes them. Returns:
783
784 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
785
786 Returns:
787 The number of bytes read is SIZE*ROW*COL. */
788
789enum mi_cmd_result
790mi_cmd_data_read_memory (char *command, char **argv, int argc)
791{
792 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
793 CORE_ADDR addr;
794 long total_bytes;
795 long nr_cols;
796 long nr_rows;
797 char word_format;
798 struct type *word_type;
799 long word_size;
800 char word_asize;
801 char aschar;
802 char *mbuf;
803 int nr_bytes;
804 long offset = 0;
805 int optind = 0;
806 char *optarg;
807 enum opt
808 {
809 OFFSET_OPT
810 };
811 static struct mi_opt opts[] =
812 {
813 {"o", OFFSET_OPT, 1},
814 0
815 };
816
817 while (1)
818 {
819 int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts,
820 &optind, &optarg);
821 if (opt < 0)
822 break;
823 switch ((enum opt) opt)
824 {
825 case OFFSET_OPT:
826 offset = atol (optarg);
827 break;
828 }
829 }
830 argv += optind;
831 argc -= optind;
832
833 if (argc < 5 || argc > 6)
834 {
76995688
AC
835 xasprintf (&mi_error_message,
836 "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
fb40c209
AC
837 return MI_CMD_ERROR;
838 }
839
840 /* Extract all the arguments. */
841
842 /* Start address of the memory dump. */
843 addr = parse_and_eval_address (argv[0]) + offset;
844 /* The format character to use when displaying a memory word. See
845 the ``x'' command. */
846 word_format = argv[1][0];
847 /* The size of the memory word. */
848 word_size = atol (argv[2]);
849 switch (word_size)
850 {
851 case 1:
852 word_type = builtin_type_int8;
853 word_asize = 'b';
854 break;
855 case 2:
856 word_type = builtin_type_int16;
857 word_asize = 'h';
858 break;
859 case 4:
860 word_type = builtin_type_int32;
861 word_asize = 'w';
862 break;
863 case 8:
864 word_type = builtin_type_int64;
865 word_asize = 'g';
866 break;
867 default:
868 word_type = builtin_type_int8;
869 word_asize = 'b';
870 }
871 /* The number of rows */
872 nr_rows = atol (argv[3]);
873 if (nr_rows <= 0)
874 {
76995688
AC
875 xasprintf (&mi_error_message,
876 "mi_cmd_data_read_memory: invalid number of rows.");
fb40c209
AC
877 return MI_CMD_ERROR;
878 }
879 /* number of bytes per row. */
880 nr_cols = atol (argv[4]);
881 if (nr_cols <= 0)
882 {
76995688
AC
883 xasprintf (&mi_error_message,
884 "mi_cmd_data_read_memory: invalid number of columns.");
fb40c209
AC
885 }
886 /* The un-printable character when printing ascii. */
887 if (argc == 6)
888 aschar = *argv[5];
889 else
890 aschar = 0;
891
892 /* create a buffer and read it in. */
893 total_bytes = word_size * nr_rows * nr_cols;
2e94c453 894 mbuf = xcalloc (total_bytes, 1);
b8c9b27d 895 make_cleanup (xfree, mbuf);
fb40c209
AC
896 if (mbuf == NULL)
897 {
76995688
AC
898 xasprintf (&mi_error_message,
899 "mi_cmd_data_read_memory: out of memory.");
fb40c209
AC
900 return MI_CMD_ERROR;
901 }
902 nr_bytes = 0;
903 while (nr_bytes < total_bytes)
904 {
905 int error;
906 long num = target_read_memory_partial (addr + nr_bytes, mbuf + nr_bytes,
907 total_bytes - nr_bytes,
908 &error);
909 if (num <= 0)
910 break;
911 nr_bytes += num;
912 }
913
914 /* output the header information. */
915 ui_out_field_core_addr (uiout, "addr", addr);
916 ui_out_field_int (uiout, "nr-bytes", nr_bytes);
917 ui_out_field_int (uiout, "total-bytes", total_bytes);
918 ui_out_field_core_addr (uiout, "next-row", addr + word_size * nr_cols);
919 ui_out_field_core_addr (uiout, "prev-row", addr - word_size * nr_cols);
920 ui_out_field_core_addr (uiout, "next-page", addr + total_bytes);
921 ui_out_field_core_addr (uiout, "prev-page", addr - total_bytes);
922
923 /* Build the result as a two dimentional table. */
924 {
925 struct ui_stream *stream = ui_out_stream_new (uiout);
6ad4a2cf 926 struct cleanup *cleanup_list_memory;
fb40c209
AC
927 int row;
928 int row_byte;
6ad4a2cf 929 cleanup_list_memory = make_cleanup_ui_out_list_begin_end (uiout, "memory");
fb40c209
AC
930 for (row = 0, row_byte = 0;
931 row < nr_rows;
932 row++, row_byte += nr_cols * word_size)
933 {
934 int col;
935 int col_byte;
6ad4a2cf
JJ
936 struct cleanup *cleanup_tuple;
937 struct cleanup *cleanup_list_data;
938 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
fb40c209
AC
939 ui_out_field_core_addr (uiout, "addr", addr + row_byte);
940 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
6ad4a2cf 941 cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
fb40c209
AC
942 for (col = 0, col_byte = row_byte;
943 col < nr_cols;
944 col++, col_byte += word_size)
945 {
946 if (col_byte + word_size > nr_bytes)
947 {
948 ui_out_field_string (uiout, NULL, "N/A");
949 }
950 else
951 {
952 ui_file_rewind (stream->stream);
953 print_scalar_formatted (mbuf + col_byte, word_type, word_format,
954 word_asize, stream->stream);
955 ui_out_field_stream (uiout, NULL, stream);
956 }
957 }
6ad4a2cf 958 do_cleanups (cleanup_list_data);
fb40c209
AC
959 if (aschar)
960 {
961 int byte;
962 ui_file_rewind (stream->stream);
963 for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
964 {
965 if (byte >= nr_bytes)
966 {
967 fputc_unfiltered ('X', stream->stream);
968 }
969 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
970 {
971 fputc_unfiltered (aschar, stream->stream);
972 }
973 else
974 fputc_unfiltered (mbuf[byte], stream->stream);
975 }
976 ui_out_field_stream (uiout, "ascii", stream);
977 }
6ad4a2cf 978 do_cleanups (cleanup_tuple);
fb40c209
AC
979 }
980 ui_out_stream_delete (stream);
6ad4a2cf 981 do_cleanups (cleanup_list_memory);
fb40c209
AC
982 }
983 do_cleanups (cleanups);
984 return MI_CMD_DONE;
985}
986
987/* DATA-MEMORY-WRITE:
988
989 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
990 offset from the beginning of the memory grid row where the cell to
991 be written is.
992 ADDR: start address of the row in the memory grid where the memory
993 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
994 the location to write to.
995 FORMAT: a char indicating format for the ``word''. See
996 the ``x'' command.
997 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
998 VALUE: value to be written into the memory address.
999
1000 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1001
1002 Prints nothing. */
1003enum mi_cmd_result
1004mi_cmd_data_write_memory (char *command, char **argv, int argc)
1005{
1006 CORE_ADDR addr;
1007 char word_format;
1008 long word_size;
1009 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1010 enough when using a compiler other than GCC. */
1011 LONGEST value;
d8bf3afa
KB
1012 void *buffer;
1013 struct cleanup *old_chain;
fb40c209
AC
1014 long offset = 0;
1015 int optind = 0;
1016 char *optarg;
1017 enum opt
1018 {
1019 OFFSET_OPT
1020 };
1021 static struct mi_opt opts[] =
1022 {
1023 {"o", OFFSET_OPT, 1},
1024 0
1025 };
1026
1027 while (1)
1028 {
1029 int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
1030 &optind, &optarg);
1031 if (opt < 0)
1032 break;
1033 switch ((enum opt) opt)
1034 {
1035 case OFFSET_OPT:
1036 offset = atol (optarg);
1037 break;
1038 }
1039 }
1040 argv += optind;
1041 argc -= optind;
1042
1043 if (argc != 4)
1044 {
76995688
AC
1045 xasprintf (&mi_error_message,
1046 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
fb40c209
AC
1047 return MI_CMD_ERROR;
1048 }
1049
1050 /* Extract all the arguments. */
1051 /* Start address of the memory dump. */
1052 addr = parse_and_eval_address (argv[0]);
1053 /* The format character to use when displaying a memory word. See
1054 the ``x'' command. */
1055 word_format = argv[1][0];
1056 /* The size of the memory word. */
1057 word_size = atol (argv[2]);
1058
1059 /* Calculate the real address of the write destination. */
1060 addr += (offset * word_size);
1061
1062 /* Get the value as a number */
1063 value = parse_and_eval_address (argv[3]);
1064 /* Get the value into an array */
d8bf3afa
KB
1065 buffer = xmalloc (word_size);
1066 old_chain = make_cleanup (xfree, buffer);
fb40c209
AC
1067 store_signed_integer (buffer, word_size, value);
1068 /* Write it down to memory */
1069 write_memory (addr, buffer, word_size);
d8bf3afa
KB
1070 /* Free the buffer. */
1071 do_cleanups (old_chain);
fb40c209
AC
1072
1073 return MI_CMD_DONE;
1074}
1075
8d34ea23
KS
1076/* Execute a command within a safe environment.
1077 Return <0 for error; >=0 for ok.
1078
1079 args->action will tell mi_execute_command what action
1080 to perfrom after the given command has executed (display/supress
1081 prompt, display error). */
fb40c209
AC
1082
1083static int
8d34ea23 1084captured_mi_execute_command (struct ui_out *uiout, void *data)
fb40c209 1085{
8d34ea23
KS
1086 struct captured_mi_execute_command_args *args =
1087 (struct captured_mi_execute_command_args *) data;
1088 struct mi_parse *context = args->command;
fb40c209
AC
1089
1090 switch (context->op)
1091 {
1092
1093 case MI_COMMAND:
1094 /* A MI command was read from the input stream */
1095 if (mi_debug_p)
1096 /* FIXME: gdb_???? */
1097 fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
1098 context->token, context->command, context->args);
1099 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1100 condition expression, each function should return an
1101 indication of what action is required and then switch on
1102 that. */
8d34ea23
KS
1103 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1104 args->rc = mi_cmd_execute (context);
1105
fb40c209
AC
1106 if (!target_can_async_p () || !target_executing)
1107 {
4389a95a
AC
1108 /* print the result if there were no errors
1109
1110 Remember that on the way out of executing a command, you have
1111 to directly use the mi_interp's uiout, since the command could
1112 have reset the interpreter, in which case the current uiout
1113 will most likely crash in the mi_out_* routines. */
8d34ea23 1114 if (args->rc == MI_CMD_DONE)
fb40c209
AC
1115 {
1116 fputs_unfiltered (context->token, raw_stdout);
1117 fputs_unfiltered ("^done", raw_stdout);
1118 mi_out_put (uiout, raw_stdout);
1119 mi_out_rewind (uiout);
1120 fputs_unfiltered ("\n", raw_stdout);
1121 }
8d34ea23 1122 else if (args->rc == MI_CMD_ERROR)
fb40c209
AC
1123 {
1124 if (mi_error_message)
1125 {
1126 fputs_unfiltered (context->token, raw_stdout);
1127 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1128 fputstr_unfiltered (mi_error_message, '"', raw_stdout);
b8c9b27d 1129 xfree (mi_error_message);
fb40c209
AC
1130 fputs_unfiltered ("\"\n", raw_stdout);
1131 }
1132 mi_out_rewind (uiout);
1133 }
8d34ea23 1134 else if (args->rc == MI_CMD_CAUGHT_ERROR)
fb40c209
AC
1135 {
1136 mi_out_rewind (uiout);
8d34ea23
KS
1137 args->action = EXECUTE_COMMAND_DISPLAY_ERROR;
1138 return 1;
fb40c209
AC
1139 }
1140 else
1141 mi_out_rewind (uiout);
1142 }
1143 else if (sync_execution)
8d34ea23
KS
1144 {
1145 /* Don't print the prompt. We are executing the target in
1146 synchronous mode. */
1147 args->action = EXECUTE_COMMAND_SUPRESS_PROMPT;
1148 return 1;
1149 }
fb40c209
AC
1150 break;
1151
1152 case CLI_COMMAND:
1153 /* A CLI command was read from the input stream */
1154 /* This will be removed as soon as we have a complete set of
1155 mi commands */
1156 /* echo the command on the console. */
1157 fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1158 /* FIXME: If the command string has something that looks like
1159 a format spec (e.g. %s) we will get a core dump */
1160 mi_execute_cli_command ("%s", context->command);
4389a95a
AC
1161
1162 /* If we changed interpreters, DON'T print out anything. */
1163 if (current_interp_named_p (INTERP_MI)
1164 || current_interp_named_p (INTERP_MI1))
1165 {
1166 /* print the result */
1167 /* FIXME: Check for errors here. */
1168 fputs_unfiltered (context->token, raw_stdout);
1169 fputs_unfiltered ("^done", raw_stdout);
1170 mi_out_put (uiout, raw_stdout);
1171 mi_out_rewind (uiout);
1172 fputs_unfiltered ("\n", raw_stdout);
1173 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1174 args->rc = MI_CMD_DONE;
1175 }
fb40c209
AC
1176 break;
1177
1178 }
8d34ea23 1179
fb40c209
AC
1180 return 1;
1181}
1182
1183
1184void
1185mi_execute_command (char *cmd, int from_tty)
1186{
1187 struct mi_parse *command;
8d34ea23
KS
1188 struct captured_mi_execute_command_args args;
1189 struct ui_out *saved_uiout = uiout;
4389a95a 1190 int result;
fb40c209
AC
1191
1192 /* This is to handle EOF (^D). We just quit gdb. */
1193 /* FIXME: we should call some API function here. */
1194 if (cmd == 0)
1195 quit_force (NULL, from_tty);
1196
1197 command = mi_parse (cmd);
1198
1199 if (command != NULL)
1200 {
8d34ea23 1201 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
fb40c209 1202 be pushed even further down or even eliminated? */
8d34ea23
KS
1203 args.command = command;
1204 result = catch_exceptions (uiout, captured_mi_execute_command, &args, "",
1205 RETURN_MASK_ALL);
1206
1207 if (args.action == EXECUTE_COMMAND_SUPRESS_PROMPT)
fb40c209
AC
1208 {
1209 /* The command is executing synchronously. Bail out early
1210 suppressing the finished prompt. */
1211 mi_parse_free (command);
1212 return;
1213 }
8d34ea23 1214 if (args.action == EXECUTE_COMMAND_DISPLAY_ERROR || result < 0)
fb40c209
AC
1215 {
1216 char *msg = error_last_message ();
b8c9b27d 1217 struct cleanup *cleanup = make_cleanup (xfree, msg);
fb40c209
AC
1218 /* The command execution failed and error() was called
1219 somewhere */
1220 fputs_unfiltered (command->token, raw_stdout);
1221 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1222 fputstr_unfiltered (msg, '"', raw_stdout);
1223 fputs_unfiltered ("\"\n", raw_stdout);
1224 }
1225 mi_parse_free (command);
1226 }
1227
fb40c209 1228 fputs_unfiltered ("(gdb) \n", raw_stdout);
a433f9e4 1229 gdb_flush (raw_stdout);
fb40c209
AC
1230 /* print any buffered hook code */
1231 /* ..... */
1232}
1233
1234static enum mi_cmd_result
1235mi_cmd_execute (struct mi_parse *parse)
1236{
1237 if (parse->cmd->argv_func != NULL
1238 || parse->cmd->args_func != NULL)
1239 {
1240 /* FIXME: We need to save the token because the command executed
1241 may be asynchronous and need to print the token again.
1242 In the future we can pass the token down to the func
1243 and get rid of the last_async_command */
1244 /* The problem here is to keep the token around when we launch
1245 the target, and we want to interrupt it later on. The
1246 interrupt command will have its own token, but when the
1247 target stops, we must display the token corresponding to the
1248 last execution command given. So we have another string where
1249 we copy the token (previous_async_command), if this was
1250 indeed the token of an execution command, and when we stop we
1251 print that one. This is possible because the interrupt
1252 command, when over, will copy that token back into the
1253 default token string (last_async_command). */
1254
1255 if (target_executing)
1256 {
1257 if (!previous_async_command)
1258 previous_async_command = xstrdup (last_async_command);
1259 if (strcmp (parse->command, "exec-interrupt"))
1260 {
1261 fputs_unfiltered (parse->token, raw_stdout);
1262 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1263 fputs_unfiltered ("Cannot execute command ", raw_stdout);
1264 fputstr_unfiltered (parse->command, '"', raw_stdout);
1265 fputs_unfiltered (" while target running", raw_stdout);
1266 fputs_unfiltered ("\"\n", raw_stdout);
1267 return MI_CMD_ERROR;
1268 }
1269 }
1270 last_async_command = xstrdup (parse->token);
e2f9c474 1271 make_exec_cleanup (free_current_contents, &last_async_command);
fb40c209
AC
1272 /* FIXME: DELETE THIS! */
1273 if (parse->cmd->args_func != NULL)
1274 return parse->cmd->args_func (parse->args, 0 /*from_tty */ );
1275 return parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
1276 }
1277 else if (parse->cmd->cli != 0)
1278 {
1279 /* FIXME: DELETE THIS. */
1280 /* The operation is still implemented by a cli command */
1281 /* Must be a synchronous one */
1282 mi_execute_cli_command (parse->cmd->cli, parse->args);
1283 return MI_CMD_DONE;
1284 }
1285 else
1286 {
1287 /* FIXME: DELETE THIS. */
1288 fputs_unfiltered (parse->token, raw_stdout);
1289 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1290 fputs_unfiltered ("Undefined mi command: ", raw_stdout);
1291 fputstr_unfiltered (parse->command, '"', raw_stdout);
1292 fputs_unfiltered (" (missing implementation)", raw_stdout);
1293 fputs_unfiltered ("\"\n", raw_stdout);
1294 return MI_CMD_ERROR;
1295 }
1296}
1297
fb40c209
AC
1298/* FIXME: This is just a hack so we can get some extra commands going.
1299 We don't want to channel things through the CLI, but call libgdb directly */
1300/* Use only for synchronous commands */
1301
1302void
1303mi_execute_cli_command (const char *cli, char *args)
1304{
1305 if (cli != 0)
1306 {
1307 struct cleanup *old_cleanups;
1308 char *run;
76995688 1309 xasprintf (&run, cli, args);
fb40c209
AC
1310 if (mi_debug_p)
1311 /* FIXME: gdb_???? */
1312 fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
1313 cli, run);
b8c9b27d 1314 old_cleanups = make_cleanup (xfree, run);
fb40c209
AC
1315 execute_command ( /*ui */ run, 0 /*from_tty */ );
1316 do_cleanups (old_cleanups);
1317 return;
1318 }
1319}
1320
1321enum mi_cmd_result
1322mi_execute_async_cli_command (char *mi, char *args, int from_tty)
1323{
1324 struct cleanup *old_cleanups;
1325 char *run;
1326 char *async_args;
1327
1328 if (target_can_async_p ())
1329 {
1330 async_args = (char *) xmalloc (strlen (args) + 2);
1331 make_exec_cleanup (free, async_args);
1332 strcpy (async_args, args);
1333 strcat (async_args, "&");
76995688 1334 xasprintf (&run, "%s %s", mi, async_args);
fb40c209
AC
1335 make_exec_cleanup (free, run);
1336 add_continuation (mi_exec_async_cli_cmd_continuation, NULL);
6311b07d 1337 old_cleanups = NULL;
fb40c209
AC
1338 }
1339 else
1340 {
76995688 1341 xasprintf (&run, "%s %s", mi, args);
b8c9b27d 1342 old_cleanups = make_cleanup (xfree, run);
fb40c209
AC
1343 }
1344
1345 if (!target_can_async_p ())
1346 {
1347 /* NOTE: For synchronous targets asynchronous behavour is faked by
1348 printing out the GDB prompt before we even try to execute the
1349 command. */
1350 if (last_async_command)
1351 fputs_unfiltered (last_async_command, raw_stdout);
1352 fputs_unfiltered ("^running\n", raw_stdout);
1353 fputs_unfiltered ("(gdb) \n", raw_stdout);
a433f9e4 1354 gdb_flush (raw_stdout);
fb40c209
AC
1355 }
1356 else
1357 {
1358 /* FIXME: cagney/1999-11-29: Printing this message before
1359 calling execute_command is wrong. It should only be printed
1360 once gdb has confirmed that it really has managed to send a
1361 run command to the target. */
1362 if (last_async_command)
1363 fputs_unfiltered (last_async_command, raw_stdout);
1364 fputs_unfiltered ("^running\n", raw_stdout);
1365 }
1366
1367 execute_command ( /*ui */ run, 0 /*from_tty */ );
1368
1369 if (!target_can_async_p ())
1370 {
1371 /* Do this before doing any printing. It would appear that some
1372 print code leaves garbage around in the buffer. */
1373 do_cleanups (old_cleanups);
1374 /* If the target was doing the operation synchronously we fake
1375 the stopped message. */
1376 if (last_async_command)
1377 fputs_unfiltered (last_async_command, raw_stdout);
1378 fputs_unfiltered ("*stopped", raw_stdout);
1379 mi_out_put (uiout, raw_stdout);
1380 mi_out_rewind (uiout);
1381 fputs_unfiltered ("\n", raw_stdout);
1382 return MI_CMD_QUIET;
1383 }
1384 return MI_CMD_DONE;
1385}
1386
1387void
1388mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg)
1389{
1390 if (last_async_command)
1391 fputs_unfiltered (last_async_command, raw_stdout);
1392 fputs_unfiltered ("*stopped", raw_stdout);
1393 mi_out_put (uiout, raw_stdout);
1394 fputs_unfiltered ("\n", raw_stdout);
1395 fputs_unfiltered ("(gdb) \n", raw_stdout);
a433f9e4 1396 gdb_flush (raw_stdout);
fb40c209
AC
1397 do_exec_cleanups (ALL_CLEANUPS);
1398}
1399
4389a95a 1400void
fb40c209
AC
1401mi_load_progress (const char *section_name,
1402 unsigned long sent_so_far,
1403 unsigned long total_section,
1404 unsigned long total_sent,
1405 unsigned long grand_total)
1406{
1407 struct timeval time_now, delta, update_threshold;
1408 static struct timeval last_update;
1409 static char *previous_sect_name = NULL;
1410 int new_section;
1411
4389a95a
AC
1412 if (!current_interp_named_p (INTERP_MI)
1413 && !current_interp_named_p (INTERP_MI1))
fb40c209
AC
1414 return;
1415
1416 update_threshold.tv_sec = 0;
1417 update_threshold.tv_usec = 500000;
1418 gettimeofday (&time_now, NULL);
1419
1420 delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
1421 delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
1422
1423 if (delta.tv_usec < 0)
1424 {
1425 delta.tv_sec -= 1;
1426 delta.tv_usec += 1000000;
1427 }
1428
1429 new_section = (previous_sect_name ?
1430 strcmp (previous_sect_name, section_name) : 1);
1431 if (new_section)
1432 {
6ad4a2cf 1433 struct cleanup *cleanup_tuple;
b8c9b27d 1434 xfree (previous_sect_name);
fb40c209
AC
1435 previous_sect_name = xstrdup (section_name);
1436
1437 if (last_async_command)
1438 fputs_unfiltered (last_async_command, raw_stdout);
1439 fputs_unfiltered ("+download", raw_stdout);
6ad4a2cf 1440 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
fb40c209
AC
1441 ui_out_field_string (uiout, "section", section_name);
1442 ui_out_field_int (uiout, "section-size", total_section);
1443 ui_out_field_int (uiout, "total-size", grand_total);
6ad4a2cf 1444 do_cleanups (cleanup_tuple);
fb40c209
AC
1445 mi_out_put (uiout, raw_stdout);
1446 fputs_unfiltered ("\n", raw_stdout);
1447 gdb_flush (raw_stdout);
1448 }
1449
1450 if (delta.tv_sec >= update_threshold.tv_sec &&
1451 delta.tv_usec >= update_threshold.tv_usec)
1452 {
6ad4a2cf 1453 struct cleanup *cleanup_tuple;
fb40c209
AC
1454 last_update.tv_sec = time_now.tv_sec;
1455 last_update.tv_usec = time_now.tv_usec;
1456 if (last_async_command)
1457 fputs_unfiltered (last_async_command, raw_stdout);
1458 fputs_unfiltered ("+download", raw_stdout);
6ad4a2cf 1459 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
fb40c209
AC
1460 ui_out_field_string (uiout, "section", section_name);
1461 ui_out_field_int (uiout, "section-sent", sent_so_far);
1462 ui_out_field_int (uiout, "section-size", total_section);
1463 ui_out_field_int (uiout, "total-sent", total_sent);
1464 ui_out_field_int (uiout, "total-size", grand_total);
6ad4a2cf 1465 do_cleanups (cleanup_tuple);
fb40c209
AC
1466 mi_out_put (uiout, raw_stdout);
1467 fputs_unfiltered ("\n", raw_stdout);
1468 gdb_flush (raw_stdout);
1469 }
1470}
1471
4389a95a
AC
1472void
1473mi_setup_architecture_data (void)
fb40c209 1474{
b8b527c5
AC
1475 /* don't trust DEPRECATED_REGISTER_BYTES to be zero. */
1476 old_regs = xmalloc (DEPRECATED_REGISTER_BYTES + 1);
1477 memset (old_regs, 0, DEPRECATED_REGISTER_BYTES + 1);
fb40c209
AC
1478}
1479
fb40c209 1480void
fba45db2 1481_initialize_mi_main (void)
fb40c209 1482{
b30bf9ee 1483 register_gdbarch_swap (&old_regs, sizeof (old_regs), NULL);
4389a95a 1484 register_gdbarch_swap (NULL, 0, mi_setup_architecture_data);
fb40c209 1485}
This page took 0.344606 seconds and 4 git commands to generate.