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