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