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