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