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