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