Fix typo
[deliverable/binutils-gdb.git] / gdb / mi / mi-main.c
CommitLineData
fb40c209 1/* MI Command Set.
cd0bfa36 2
0fd88904
AC
3 Copyright 2000, 2001, 2002, 2003, 2004, 2005 Free Software
4 Foundation, Inc.
cd0bfa36 5
ab91fdd5 6 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
24
25/* Work in progress */
26
27#include "defs.h"
28#include "target.h"
29#include "inferior.h"
30#include "gdb_string.h"
60250e8b 31#include "exceptions.h"
fb40c209
AC
32#include "top.h"
33#include "gdbthread.h"
34#include "mi-cmds.h"
35#include "mi-parse.h"
36#include "mi-getopt.h"
37#include "mi-console.h"
38#include "ui-out.h"
39#include "mi-out.h"
4389a95a 40#include "interps.h"
fb40c209
AC
41#include "event-loop.h"
42#include "event-top.h"
43#include "gdbcore.h" /* for write_memory() */
73937e03 44#include "value.h" /* for deprecated_write_register_bytes() */
4e052eda 45#include "regcache.h"
5b7f31a4 46#include "gdb.h"
36dc181b 47#include "frame.h"
b9362cc7 48#include "mi-main.h"
36dc181b 49
fb40c209
AC
50#include <ctype.h>
51#include <sys/time.h>
52
fb40c209
AC
53enum
54 {
55 FROM_TTY = 0
56 };
57
8d34ea23
KS
58/* Enumerations of the actions that may result from calling
59 captured_mi_execute_command */
60
61enum captured_mi_execute_command_actions
62 {
63 EXECUTE_COMMAND_DISPLAY_PROMPT,
ce43223b 64 EXECUTE_COMMAND_SUPRESS_PROMPT
8d34ea23
KS
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. */
b04f3ab4 184 print_stack_frame (get_selected_frame (NULL), 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
ce43223b 235 rc = gdb_thread_select (uiout, argv[0], &mi_error_message);
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)
ce43223b 240 return MI_CMD_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
ce43223b 258 rc = gdb_list_thread_ids (uiout, &mi_error_message);
fb40c209
AC
259
260 if (rc == GDB_RC_FAIL)
ce43223b 261 return MI_CMD_ERROR;
fb40c209
AC
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
28b41c52 330 numregs = NUM_REGS + NUM_PSEUDO_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,
3acba339 395 register_size (current_gdbarch, 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,
3acba339 401 register_size (current_gdbarch, 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
28b41c52 426 numregs = NUM_REGS + NUM_PSEUDO_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{
9730f241 500 char buffer[MAX_REGISTER_SIZE];
fb40c209 501 int optim;
ac2adee5
AC
502 int realnum;
503 CORE_ADDR addr;
504 enum lval_type lval;
fb40c209
AC
505 static struct ui_stream *stb = NULL;
506
507 stb = ui_out_stream_new (uiout);
508
509 if (format == 'N')
510 format = 0;
511
ac2adee5 512 frame_register (deprecated_selected_frame, regnum, &optim, &lval, &addr,
9730f241 513 &realnum, buffer);
ac2adee5 514
fb40c209
AC
515 if (optim)
516 {
c6902d46 517 mi_error_message = xstrprintf ("Optimized out");
fb40c209
AC
518 return -1;
519 }
520
fb40c209
AC
521 if (format == 'r')
522 {
523 int j;
524 char *ptr, buf[1024];
525
526 strcpy (buf, "0x");
527 ptr = buf + 2;
3acba339 528 for (j = 0; j < register_size (current_gdbarch, regnum); j++)
fb40c209 529 {
d5b5ac79 530 int idx = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? j
3acba339 531 : register_size (current_gdbarch, regnum) - 1 - j;
9730f241 532 sprintf (ptr, "%02x", (unsigned char) buffer[idx]);
fb40c209
AC
533 ptr += 2;
534 }
535 ui_out_field_string (uiout, "value", buf);
536 /*fputs_filtered (buf, gdb_stdout); */
537 }
538 else
539 {
9730f241 540 val_print (register_type (current_gdbarch, regnum), buffer, 0, 0,
fb40c209
AC
541 stb->stream, format, 1, 0, Val_pretty_default);
542 ui_out_field_stream (uiout, "value", stb);
543 ui_out_stream_delete (stb);
544 }
545 return 1;
546}
547
24e8cecf
EZ
548/* Write given values into registers. The registers and values are
549 given as pairs. The corresponding MI command is
550 -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
551enum mi_cmd_result
552mi_cmd_data_write_register_values (char *command, char **argv, int argc)
553{
554 int regnum;
555 int i;
556 int numregs;
24e8cecf
EZ
557 LONGEST value;
558 char format;
559
560 /* Note that the test for a valid register must include checking the
561 REGISTER_NAME because NUM_REGS may be allocated for the union of
562 the register sets within a family of related processors. In this
563 case, some entries of REGISTER_NAME will change depending upon
564 the particular processor being debugged. */
565
28b41c52 566 numregs = NUM_REGS + NUM_PSEUDO_REGS;
24e8cecf
EZ
567
568 if (argc == 0)
569 {
c6902d46 570 mi_error_message = xstrprintf ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
24e8cecf
EZ
571 return MI_CMD_ERROR;
572 }
573
574 format = (int) argv[0][0];
575
576 if (!target_has_registers)
577 {
c6902d46 578 mi_error_message = xstrprintf ("mi_cmd_data_write_register_values: No registers.");
24e8cecf
EZ
579 return MI_CMD_ERROR;
580 }
581
582 if (!(argc - 1))
583 {
c6902d46 584 mi_error_message = xstrprintf ("mi_cmd_data_write_register_values: No regs and values specified.");
24e8cecf
EZ
585 return MI_CMD_ERROR;
586 }
587
588 if ((argc - 1) % 2)
589 {
c6902d46 590 mi_error_message = xstrprintf ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
24e8cecf
EZ
591 return MI_CMD_ERROR;
592 }
593
594 for (i = 1; i < argc; i = i + 2)
595 {
596 regnum = atoi (argv[i]);
597
598 if (regnum >= 0
599 && regnum < numregs
600 && REGISTER_NAME (regnum) != NULL
601 && *REGISTER_NAME (regnum) != '\000')
602 {
d8bf3afa
KB
603 void *buffer;
604 struct cleanup *old_chain;
605
24e8cecf
EZ
606 /* Get the value as a number */
607 value = parse_and_eval_address (argv[i + 1]);
608 /* Get the value into an array */
b1e29e33 609 buffer = xmalloc (DEPRECATED_REGISTER_SIZE);
d8bf3afa 610 old_chain = make_cleanup (xfree, buffer);
b1e29e33 611 store_signed_integer (buffer, DEPRECATED_REGISTER_SIZE, value);
24e8cecf 612 /* Write it down */
3acba339 613 deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (regnum), buffer, register_size (current_gdbarch, regnum));
d8bf3afa
KB
614 /* Free the buffer. */
615 do_cleanups (old_chain);
24e8cecf
EZ
616 }
617 else
618 {
c6902d46 619 mi_error_message = xstrprintf ("bad register number");
24e8cecf
EZ
620 return MI_CMD_ERROR;
621 }
622 }
623 return MI_CMD_DONE;
624}
625
fb40c209
AC
626#if 0
627/*This is commented out because we decided it was not useful. I leave
628 it, just in case. ezannoni:1999-12-08 */
629
630/* Assign a value to a variable. The expression argument must be in
631 the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
632 quoted. */
633enum mi_cmd_result
634mi_cmd_data_assign (char *command, char **argv, int argc)
635{
636 struct expression *expr;
637 struct cleanup *old_chain;
638
639 if (argc != 1)
640 {
c6902d46 641 mi_error_message = xstrprintf ("mi_cmd_data_assign: Usage: -data-assign expression");
fb40c209
AC
642 return MI_CMD_ERROR;
643 }
644
645 /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
646 01-12-1999: Need to decide what to do with this for libgdb purposes. */
647
648 expr = parse_expression (argv[0]);
47cf603e 649 old_chain = make_cleanup (free_current_contents, &expr);
fb40c209
AC
650 evaluate_expression (expr);
651 do_cleanups (old_chain);
652 return MI_CMD_DONE;
653}
654#endif
655
656/* Evaluate the value of the argument. The argument is an
657 expression. If the expression contains spaces it needs to be
658 included in double quotes. */
659enum mi_cmd_result
660mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
661{
662 struct expression *expr;
663 struct cleanup *old_chain = NULL;
96052a95 664 struct value *val;
fb40c209
AC
665 struct ui_stream *stb = NULL;
666
667 stb = ui_out_stream_new (uiout);
668
669 if (argc != 1)
670 {
c6902d46 671 mi_error_message = xstrprintf ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
fb40c209
AC
672 return MI_CMD_ERROR;
673 }
674
675 expr = parse_expression (argv[0]);
676
47cf603e 677 old_chain = make_cleanup (free_current_contents, &expr);
fb40c209
AC
678
679 val = evaluate_expression (expr);
680
681 /* Print the result of the expression evaluation. */
0fd88904 682 val_print (value_type (val), value_contents (val),
13c3b5f5 683 value_embedded_offset (val), VALUE_ADDRESS (val),
fb40c209
AC
684 stb->stream, 0, 0, 0, 0);
685
686 ui_out_field_stream (uiout, "value", stb);
687 ui_out_stream_delete (stb);
688
689 do_cleanups (old_chain);
690
691 return MI_CMD_DONE;
692}
693
694enum mi_cmd_result
695mi_cmd_target_download (char *args, int from_tty)
696{
697 char *run;
698 struct cleanup *old_cleanups = NULL;
699
c6902d46 700 run = xstrprintf ("load %s", args);
b8c9b27d 701 old_cleanups = make_cleanup (xfree, run);
fb40c209
AC
702 execute_command (run, from_tty);
703
704 do_cleanups (old_cleanups);
705 return MI_CMD_DONE;
706}
707
708/* Connect to the remote target. */
709enum mi_cmd_result
710mi_cmd_target_select (char *args, int from_tty)
711{
712 char *run;
713 struct cleanup *old_cleanups = NULL;
714
c6902d46 715 run = xstrprintf ("target %s", args);
b8c9b27d 716 old_cleanups = make_cleanup (xfree, run);
fb40c209
AC
717
718 /* target-select is always synchronous. once the call has returned
719 we know that we are connected. */
720 /* NOTE: At present all targets that are connected are also
721 (implicitly) talking to a halted target. In the future this may
722 change. */
723 execute_command (run, from_tty);
724
725 do_cleanups (old_cleanups);
726
727 /* Issue the completion message here. */
728 if (last_async_command)
729 fputs_unfiltered (last_async_command, raw_stdout);
730 fputs_unfiltered ("^connected", raw_stdout);
731 mi_out_put (uiout, raw_stdout);
732 mi_out_rewind (uiout);
733 fputs_unfiltered ("\n", raw_stdout);
734 do_exec_cleanups (ALL_CLEANUPS);
735 return MI_CMD_QUIET;
736}
737
738/* DATA-MEMORY-READ:
739
740 ADDR: start address of data to be dumped.
741 WORD-FORMAT: a char indicating format for the ``word''. See
742 the ``x'' command.
743 WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
744 NR_ROW: Number of rows.
745 NR_COL: The number of colums (words per row).
746 ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use
747 ASCHAR for unprintable characters.
748
749 Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
750 displayes them. Returns:
751
752 {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
753
754 Returns:
755 The number of bytes read is SIZE*ROW*COL. */
756
757enum mi_cmd_result
758mi_cmd_data_read_memory (char *command, char **argv, int argc)
759{
760 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
761 CORE_ADDR addr;
762 long total_bytes;
763 long nr_cols;
764 long nr_rows;
765 char word_format;
766 struct type *word_type;
767 long word_size;
768 char word_asize;
769 char aschar;
770 char *mbuf;
771 int nr_bytes;
772 long offset = 0;
773 int optind = 0;
774 char *optarg;
775 enum opt
776 {
777 OFFSET_OPT
778 };
779 static struct mi_opt opts[] =
780 {
781 {"o", OFFSET_OPT, 1},
782 0
783 };
784
785 while (1)
786 {
787 int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts,
788 &optind, &optarg);
789 if (opt < 0)
790 break;
791 switch ((enum opt) opt)
792 {
793 case OFFSET_OPT:
794 offset = atol (optarg);
795 break;
796 }
797 }
798 argv += optind;
799 argc -= optind;
800
801 if (argc < 5 || argc > 6)
802 {
c6902d46 803 mi_error_message = xstrprintf ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
fb40c209
AC
804 return MI_CMD_ERROR;
805 }
806
807 /* Extract all the arguments. */
808
809 /* Start address of the memory dump. */
810 addr = parse_and_eval_address (argv[0]) + offset;
811 /* The format character to use when displaying a memory word. See
812 the ``x'' command. */
813 word_format = argv[1][0];
814 /* The size of the memory word. */
815 word_size = atol (argv[2]);
816 switch (word_size)
817 {
818 case 1:
819 word_type = builtin_type_int8;
820 word_asize = 'b';
821 break;
822 case 2:
823 word_type = builtin_type_int16;
824 word_asize = 'h';
825 break;
826 case 4:
827 word_type = builtin_type_int32;
828 word_asize = 'w';
829 break;
830 case 8:
831 word_type = builtin_type_int64;
832 word_asize = 'g';
833 break;
834 default:
835 word_type = builtin_type_int8;
836 word_asize = 'b';
837 }
838 /* The number of rows */
839 nr_rows = atol (argv[3]);
840 if (nr_rows <= 0)
841 {
c6902d46 842 mi_error_message = xstrprintf ("mi_cmd_data_read_memory: invalid number of rows.");
fb40c209
AC
843 return MI_CMD_ERROR;
844 }
845 /* number of bytes per row. */
846 nr_cols = atol (argv[4]);
847 if (nr_cols <= 0)
848 {
c6902d46 849 mi_error_message = xstrprintf ("mi_cmd_data_read_memory: invalid number of columns.");
de169ec9 850 return MI_CMD_ERROR;
fb40c209
AC
851 }
852 /* The un-printable character when printing ascii. */
853 if (argc == 6)
854 aschar = *argv[5];
855 else
856 aschar = 0;
857
858 /* create a buffer and read it in. */
859 total_bytes = word_size * nr_rows * nr_cols;
2e94c453 860 mbuf = xcalloc (total_bytes, 1);
b8c9b27d 861 make_cleanup (xfree, mbuf);
fb40c209
AC
862 nr_bytes = 0;
863 while (nr_bytes < total_bytes)
864 {
865 int error;
866 long num = target_read_memory_partial (addr + nr_bytes, mbuf + nr_bytes,
867 total_bytes - nr_bytes,
868 &error);
869 if (num <= 0)
870 break;
871 nr_bytes += num;
872 }
873
874 /* output the header information. */
875 ui_out_field_core_addr (uiout, "addr", addr);
876 ui_out_field_int (uiout, "nr-bytes", nr_bytes);
877 ui_out_field_int (uiout, "total-bytes", total_bytes);
878 ui_out_field_core_addr (uiout, "next-row", addr + word_size * nr_cols);
879 ui_out_field_core_addr (uiout, "prev-row", addr - word_size * nr_cols);
880 ui_out_field_core_addr (uiout, "next-page", addr + total_bytes);
881 ui_out_field_core_addr (uiout, "prev-page", addr - total_bytes);
882
883 /* Build the result as a two dimentional table. */
884 {
885 struct ui_stream *stream = ui_out_stream_new (uiout);
6ad4a2cf 886 struct cleanup *cleanup_list_memory;
fb40c209
AC
887 int row;
888 int row_byte;
6ad4a2cf 889 cleanup_list_memory = make_cleanup_ui_out_list_begin_end (uiout, "memory");
fb40c209
AC
890 for (row = 0, row_byte = 0;
891 row < nr_rows;
892 row++, row_byte += nr_cols * word_size)
893 {
894 int col;
895 int col_byte;
6ad4a2cf
JJ
896 struct cleanup *cleanup_tuple;
897 struct cleanup *cleanup_list_data;
898 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
fb40c209
AC
899 ui_out_field_core_addr (uiout, "addr", addr + row_byte);
900 /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
6ad4a2cf 901 cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
fb40c209
AC
902 for (col = 0, col_byte = row_byte;
903 col < nr_cols;
904 col++, col_byte += word_size)
905 {
906 if (col_byte + word_size > nr_bytes)
907 {
908 ui_out_field_string (uiout, NULL, "N/A");
909 }
910 else
911 {
912 ui_file_rewind (stream->stream);
913 print_scalar_formatted (mbuf + col_byte, word_type, word_format,
914 word_asize, stream->stream);
915 ui_out_field_stream (uiout, NULL, stream);
916 }
917 }
6ad4a2cf 918 do_cleanups (cleanup_list_data);
fb40c209
AC
919 if (aschar)
920 {
921 int byte;
922 ui_file_rewind (stream->stream);
923 for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
924 {
925 if (byte >= nr_bytes)
926 {
927 fputc_unfiltered ('X', stream->stream);
928 }
929 else if (mbuf[byte] < 32 || mbuf[byte] > 126)
930 {
931 fputc_unfiltered (aschar, stream->stream);
932 }
933 else
934 fputc_unfiltered (mbuf[byte], stream->stream);
935 }
936 ui_out_field_stream (uiout, "ascii", stream);
937 }
6ad4a2cf 938 do_cleanups (cleanup_tuple);
fb40c209
AC
939 }
940 ui_out_stream_delete (stream);
6ad4a2cf 941 do_cleanups (cleanup_list_memory);
fb40c209
AC
942 }
943 do_cleanups (cleanups);
944 return MI_CMD_DONE;
945}
946
947/* DATA-MEMORY-WRITE:
948
949 COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
950 offset from the beginning of the memory grid row where the cell to
951 be written is.
952 ADDR: start address of the row in the memory grid where the memory
953 cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
954 the location to write to.
955 FORMAT: a char indicating format for the ``word''. See
956 the ``x'' command.
957 WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
958 VALUE: value to be written into the memory address.
959
960 Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
961
962 Prints nothing. */
963enum mi_cmd_result
964mi_cmd_data_write_memory (char *command, char **argv, int argc)
965{
966 CORE_ADDR addr;
967 char word_format;
968 long word_size;
969 /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
970 enough when using a compiler other than GCC. */
971 LONGEST value;
d8bf3afa
KB
972 void *buffer;
973 struct cleanup *old_chain;
fb40c209
AC
974 long offset = 0;
975 int optind = 0;
976 char *optarg;
977 enum opt
978 {
979 OFFSET_OPT
980 };
981 static struct mi_opt opts[] =
982 {
983 {"o", OFFSET_OPT, 1},
984 0
985 };
986
987 while (1)
988 {
989 int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
990 &optind, &optarg);
991 if (opt < 0)
992 break;
993 switch ((enum opt) opt)
994 {
995 case OFFSET_OPT:
996 offset = atol (optarg);
997 break;
998 }
999 }
1000 argv += optind;
1001 argc -= optind;
1002
1003 if (argc != 4)
1004 {
c6902d46 1005 mi_error_message = xstrprintf ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
fb40c209
AC
1006 return MI_CMD_ERROR;
1007 }
1008
1009 /* Extract all the arguments. */
1010 /* Start address of the memory dump. */
1011 addr = parse_and_eval_address (argv[0]);
1012 /* The format character to use when displaying a memory word. See
1013 the ``x'' command. */
1014 word_format = argv[1][0];
1015 /* The size of the memory word. */
1016 word_size = atol (argv[2]);
1017
1018 /* Calculate the real address of the write destination. */
1019 addr += (offset * word_size);
1020
1021 /* Get the value as a number */
1022 value = parse_and_eval_address (argv[3]);
1023 /* Get the value into an array */
d8bf3afa
KB
1024 buffer = xmalloc (word_size);
1025 old_chain = make_cleanup (xfree, buffer);
fb40c209
AC
1026 store_signed_integer (buffer, word_size, value);
1027 /* Write it down to memory */
1028 write_memory (addr, buffer, word_size);
d8bf3afa
KB
1029 /* Free the buffer. */
1030 do_cleanups (old_chain);
fb40c209
AC
1031
1032 return MI_CMD_DONE;
1033}
1034
8d34ea23
KS
1035/* Execute a command within a safe environment.
1036 Return <0 for error; >=0 for ok.
1037
1038 args->action will tell mi_execute_command what action
1039 to perfrom after the given command has executed (display/supress
1040 prompt, display error). */
fb40c209 1041
f30f06b8 1042static void
8d34ea23 1043captured_mi_execute_command (struct ui_out *uiout, void *data)
fb40c209 1044{
8d34ea23
KS
1045 struct captured_mi_execute_command_args *args =
1046 (struct captured_mi_execute_command_args *) data;
1047 struct mi_parse *context = args->command;
fb40c209
AC
1048
1049 switch (context->op)
1050 {
1051
1052 case MI_COMMAND:
1053 /* A MI command was read from the input stream */
1054 if (mi_debug_p)
1055 /* FIXME: gdb_???? */
1056 fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
1057 context->token, context->command, context->args);
1058 /* FIXME: cagney/1999-09-25: Rather than this convoluted
1059 condition expression, each function should return an
1060 indication of what action is required and then switch on
1061 that. */
8d34ea23
KS
1062 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1063 args->rc = mi_cmd_execute (context);
1064
fb40c209
AC
1065 if (!target_can_async_p () || !target_executing)
1066 {
4389a95a
AC
1067 /* print the result if there were no errors
1068
1069 Remember that on the way out of executing a command, you have
1070 to directly use the mi_interp's uiout, since the command could
1071 have reset the interpreter, in which case the current uiout
1072 will most likely crash in the mi_out_* routines. */
8d34ea23 1073 if (args->rc == MI_CMD_DONE)
fb40c209
AC
1074 {
1075 fputs_unfiltered (context->token, raw_stdout);
1076 fputs_unfiltered ("^done", raw_stdout);
1077 mi_out_put (uiout, raw_stdout);
1078 mi_out_rewind (uiout);
1079 fputs_unfiltered ("\n", raw_stdout);
1080 }
8d34ea23 1081 else if (args->rc == MI_CMD_ERROR)
fb40c209
AC
1082 {
1083 if (mi_error_message)
1084 {
1085 fputs_unfiltered (context->token, raw_stdout);
1086 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1087 fputstr_unfiltered (mi_error_message, '"', raw_stdout);
b8c9b27d 1088 xfree (mi_error_message);
fb40c209
AC
1089 fputs_unfiltered ("\"\n", raw_stdout);
1090 }
1091 mi_out_rewind (uiout);
1092 }
fb40c209
AC
1093 else
1094 mi_out_rewind (uiout);
1095 }
1096 else if (sync_execution)
8d34ea23
KS
1097 {
1098 /* Don't print the prompt. We are executing the target in
1099 synchronous mode. */
1100 args->action = EXECUTE_COMMAND_SUPRESS_PROMPT;
f30f06b8 1101 return;
8d34ea23 1102 }
fb40c209
AC
1103 break;
1104
1105 case CLI_COMMAND:
78f5381d
AC
1106 {
1107 char *argv[2];
1108 /* A CLI command was read from the input stream. */
1109 /* This "feature" will be removed as soon as we have a
1110 complete set of mi commands. */
1111 /* Echo the command on the console. */
1112 fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1113 /* Call the "console" interpreter. */
1114 argv[0] = "console";
1115 argv[1] = context->command;
1116 mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);
1117
1118 /* If we changed interpreters, DON'T print out anything. */
1119 if (current_interp_named_p (INTERP_MI)
1120 || current_interp_named_p (INTERP_MI1)
1121 || current_interp_named_p (INTERP_MI2)
1122 || current_interp_named_p (INTERP_MI3))
1123 {
1124 /* print the result */
1125 /* FIXME: Check for errors here. */
1126 fputs_unfiltered (context->token, raw_stdout);
1127 fputs_unfiltered ("^done", raw_stdout);
1128 mi_out_put (uiout, raw_stdout);
1129 mi_out_rewind (uiout);
1130 fputs_unfiltered ("\n", raw_stdout);
1131 args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1132 args->rc = MI_CMD_DONE;
1133 }
1134 break;
1135 }
fb40c209
AC
1136
1137 }
8d34ea23 1138
f30f06b8 1139 return;
fb40c209
AC
1140}
1141
1142
1143void
1144mi_execute_command (char *cmd, int from_tty)
1145{
1146 struct mi_parse *command;
8d34ea23
KS
1147 struct captured_mi_execute_command_args args;
1148 struct ui_out *saved_uiout = uiout;
fb40c209
AC
1149
1150 /* This is to handle EOF (^D). We just quit gdb. */
1151 /* FIXME: we should call some API function here. */
1152 if (cmd == 0)
1153 quit_force (NULL, from_tty);
1154
1155 command = mi_parse (cmd);
1156
1157 if (command != NULL)
1158 {
71fff37b 1159 struct gdb_exception result;
8d34ea23 1160 /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
fb40c209 1161 be pushed even further down or even eliminated? */
8d34ea23 1162 args.command = command;
f30f06b8
AC
1163 result = catch_exception (uiout, captured_mi_execute_command, &args,
1164 RETURN_MASK_ALL);
9cbc821d 1165 exception_print (gdb_stderr, result);
8d34ea23
KS
1166
1167 if (args.action == EXECUTE_COMMAND_SUPRESS_PROMPT)
fb40c209
AC
1168 {
1169 /* The command is executing synchronously. Bail out early
1170 suppressing the finished prompt. */
1171 mi_parse_free (command);
1172 return;
1173 }
ce43223b 1174 if (result.reason < 0)
fb40c209 1175 {
fb40c209
AC
1176 /* The command execution failed and error() was called
1177 somewhere */
1178 fputs_unfiltered (command->token, raw_stdout);
1179 fputs_unfiltered ("^error,msg=\"", raw_stdout);
b315da38 1180 fputstr_unfiltered (result.message, '"', raw_stdout);
fb40c209
AC
1181 fputs_unfiltered ("\"\n", raw_stdout);
1182 }
1183 mi_parse_free (command);
1184 }
1185
fb40c209 1186 fputs_unfiltered ("(gdb) \n", raw_stdout);
a433f9e4 1187 gdb_flush (raw_stdout);
fb40c209
AC
1188 /* print any buffered hook code */
1189 /* ..... */
1190}
1191
1192static enum mi_cmd_result
1193mi_cmd_execute (struct mi_parse *parse)
1194{
1195 if (parse->cmd->argv_func != NULL
1196 || parse->cmd->args_func != NULL)
1197 {
1198 /* FIXME: We need to save the token because the command executed
1199 may be asynchronous and need to print the token again.
1200 In the future we can pass the token down to the func
1201 and get rid of the last_async_command */
1202 /* The problem here is to keep the token around when we launch
1203 the target, and we want to interrupt it later on. The
1204 interrupt command will have its own token, but when the
1205 target stops, we must display the token corresponding to the
1206 last execution command given. So we have another string where
1207 we copy the token (previous_async_command), if this was
1208 indeed the token of an execution command, and when we stop we
1209 print that one. This is possible because the interrupt
1210 command, when over, will copy that token back into the
1211 default token string (last_async_command). */
1212
1213 if (target_executing)
1214 {
1215 if (!previous_async_command)
1216 previous_async_command = xstrdup (last_async_command);
1217 if (strcmp (parse->command, "exec-interrupt"))
1218 {
1219 fputs_unfiltered (parse->token, raw_stdout);
1220 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1221 fputs_unfiltered ("Cannot execute command ", raw_stdout);
1222 fputstr_unfiltered (parse->command, '"', raw_stdout);
1223 fputs_unfiltered (" while target running", raw_stdout);
1224 fputs_unfiltered ("\"\n", raw_stdout);
1225 return MI_CMD_ERROR;
1226 }
1227 }
1228 last_async_command = xstrdup (parse->token);
e2f9c474 1229 make_exec_cleanup (free_current_contents, &last_async_command);
fb40c209
AC
1230 /* FIXME: DELETE THIS! */
1231 if (parse->cmd->args_func != NULL)
1232 return parse->cmd->args_func (parse->args, 0 /*from_tty */ );
1233 return parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
1234 }
b2af646b 1235 else if (parse->cmd->cli.cmd != 0)
fb40c209
AC
1236 {
1237 /* FIXME: DELETE THIS. */
1238 /* The operation is still implemented by a cli command */
1239 /* Must be a synchronous one */
b2af646b
AC
1240 mi_execute_cli_command (parse->cmd->cli.cmd, parse->cmd->cli.args_p,
1241 parse->args);
fb40c209
AC
1242 return MI_CMD_DONE;
1243 }
1244 else
1245 {
1246 /* FIXME: DELETE THIS. */
1247 fputs_unfiltered (parse->token, raw_stdout);
1248 fputs_unfiltered ("^error,msg=\"", raw_stdout);
1249 fputs_unfiltered ("Undefined mi command: ", raw_stdout);
1250 fputstr_unfiltered (parse->command, '"', raw_stdout);
1251 fputs_unfiltered (" (missing implementation)", raw_stdout);
1252 fputs_unfiltered ("\"\n", raw_stdout);
1253 return MI_CMD_ERROR;
1254 }
1255}
1256
fb40c209
AC
1257/* FIXME: This is just a hack so we can get some extra commands going.
1258 We don't want to channel things through the CLI, but call libgdb directly */
1259/* Use only for synchronous commands */
1260
1261void
b2af646b 1262mi_execute_cli_command (const char *cmd, int args_p, const char *args)
fb40c209 1263{
b2af646b 1264 if (cmd != 0)
fb40c209
AC
1265 {
1266 struct cleanup *old_cleanups;
1267 char *run;
b2af646b 1268 if (args_p)
c6902d46 1269 run = xstrprintf ("%s %s", cmd, args);
b2af646b
AC
1270 else
1271 run = xstrdup (cmd);
fb40c209
AC
1272 if (mi_debug_p)
1273 /* FIXME: gdb_???? */
1274 fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
b2af646b 1275 cmd, run);
b8c9b27d 1276 old_cleanups = make_cleanup (xfree, run);
fb40c209
AC
1277 execute_command ( /*ui */ run, 0 /*from_tty */ );
1278 do_cleanups (old_cleanups);
1279 return;
1280 }
1281}
1282
1283enum mi_cmd_result
1284mi_execute_async_cli_command (char *mi, char *args, int from_tty)
1285{
1286 struct cleanup *old_cleanups;
1287 char *run;
1288 char *async_args;
1289
1290 if (target_can_async_p ())
1291 {
1292 async_args = (char *) xmalloc (strlen (args) + 2);
1293 make_exec_cleanup (free, async_args);
1294 strcpy (async_args, args);
1295 strcat (async_args, "&");
c6902d46 1296 run = xstrprintf ("%s %s", mi, async_args);
fb40c209
AC
1297 make_exec_cleanup (free, run);
1298 add_continuation (mi_exec_async_cli_cmd_continuation, NULL);
6311b07d 1299 old_cleanups = NULL;
fb40c209
AC
1300 }
1301 else
1302 {
c6902d46 1303 run = xstrprintf ("%s %s", mi, args);
b8c9b27d 1304 old_cleanups = make_cleanup (xfree, run);
fb40c209
AC
1305 }
1306
1307 if (!target_can_async_p ())
1308 {
1309 /* NOTE: For synchronous targets asynchronous behavour is faked by
1310 printing out the GDB prompt before we even try to execute the
1311 command. */
1312 if (last_async_command)
1313 fputs_unfiltered (last_async_command, raw_stdout);
1314 fputs_unfiltered ("^running\n", raw_stdout);
1315 fputs_unfiltered ("(gdb) \n", raw_stdout);
a433f9e4 1316 gdb_flush (raw_stdout);
fb40c209
AC
1317 }
1318 else
1319 {
1320 /* FIXME: cagney/1999-11-29: Printing this message before
1321 calling execute_command is wrong. It should only be printed
1322 once gdb has confirmed that it really has managed to send a
1323 run command to the target. */
1324 if (last_async_command)
1325 fputs_unfiltered (last_async_command, raw_stdout);
1326 fputs_unfiltered ("^running\n", raw_stdout);
1327 }
1328
1329 execute_command ( /*ui */ run, 0 /*from_tty */ );
1330
1331 if (!target_can_async_p ())
1332 {
1333 /* Do this before doing any printing. It would appear that some
1334 print code leaves garbage around in the buffer. */
1335 do_cleanups (old_cleanups);
1336 /* If the target was doing the operation synchronously we fake
1337 the stopped message. */
1338 if (last_async_command)
1339 fputs_unfiltered (last_async_command, raw_stdout);
1340 fputs_unfiltered ("*stopped", raw_stdout);
1341 mi_out_put (uiout, raw_stdout);
1342 mi_out_rewind (uiout);
1343 fputs_unfiltered ("\n", raw_stdout);
1344 return MI_CMD_QUIET;
1345 }
1346 return MI_CMD_DONE;
1347}
1348
1349void
1350mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg)
1351{
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 fputs_unfiltered ("\n", raw_stdout);
1357 fputs_unfiltered ("(gdb) \n", raw_stdout);
a433f9e4 1358 gdb_flush (raw_stdout);
fb40c209
AC
1359 do_exec_cleanups (ALL_CLEANUPS);
1360}
1361
4389a95a 1362void
fb40c209
AC
1363mi_load_progress (const char *section_name,
1364 unsigned long sent_so_far,
1365 unsigned long total_section,
1366 unsigned long total_sent,
1367 unsigned long grand_total)
1368{
1369 struct timeval time_now, delta, update_threshold;
1370 static struct timeval last_update;
1371 static char *previous_sect_name = NULL;
1372 int new_section;
1373
4389a95a
AC
1374 if (!current_interp_named_p (INTERP_MI)
1375 && !current_interp_named_p (INTERP_MI1))
fb40c209
AC
1376 return;
1377
1378 update_threshold.tv_sec = 0;
1379 update_threshold.tv_usec = 500000;
1380 gettimeofday (&time_now, NULL);
1381
1382 delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
1383 delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
1384
1385 if (delta.tv_usec < 0)
1386 {
1387 delta.tv_sec -= 1;
1388 delta.tv_usec += 1000000;
1389 }
1390
1391 new_section = (previous_sect_name ?
1392 strcmp (previous_sect_name, section_name) : 1);
1393 if (new_section)
1394 {
6ad4a2cf 1395 struct cleanup *cleanup_tuple;
b8c9b27d 1396 xfree (previous_sect_name);
fb40c209
AC
1397 previous_sect_name = xstrdup (section_name);
1398
1399 if (last_async_command)
1400 fputs_unfiltered (last_async_command, raw_stdout);
1401 fputs_unfiltered ("+download", raw_stdout);
6ad4a2cf 1402 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
fb40c209
AC
1403 ui_out_field_string (uiout, "section", section_name);
1404 ui_out_field_int (uiout, "section-size", total_section);
1405 ui_out_field_int (uiout, "total-size", grand_total);
6ad4a2cf 1406 do_cleanups (cleanup_tuple);
fb40c209
AC
1407 mi_out_put (uiout, raw_stdout);
1408 fputs_unfiltered ("\n", raw_stdout);
1409 gdb_flush (raw_stdout);
1410 }
1411
1412 if (delta.tv_sec >= update_threshold.tv_sec &&
1413 delta.tv_usec >= update_threshold.tv_usec)
1414 {
6ad4a2cf 1415 struct cleanup *cleanup_tuple;
fb40c209
AC
1416 last_update.tv_sec = time_now.tv_sec;
1417 last_update.tv_usec = time_now.tv_usec;
1418 if (last_async_command)
1419 fputs_unfiltered (last_async_command, raw_stdout);
1420 fputs_unfiltered ("+download", raw_stdout);
6ad4a2cf 1421 cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
fb40c209
AC
1422 ui_out_field_string (uiout, "section", section_name);
1423 ui_out_field_int (uiout, "section-sent", sent_so_far);
1424 ui_out_field_int (uiout, "section-size", total_section);
1425 ui_out_field_int (uiout, "total-sent", total_sent);
1426 ui_out_field_int (uiout, "total-size", grand_total);
6ad4a2cf 1427 do_cleanups (cleanup_tuple);
fb40c209
AC
1428 mi_out_put (uiout, raw_stdout);
1429 fputs_unfiltered ("\n", raw_stdout);
1430 gdb_flush (raw_stdout);
1431 }
1432}
1433
4389a95a
AC
1434void
1435mi_setup_architecture_data (void)
fb40c209 1436{
00a9a2c6
EZ
1437 old_regs = xmalloc ((NUM_REGS + NUM_PSEUDO_REGS) * MAX_REGISTER_SIZE + 1);
1438 memset (old_regs, 0, (NUM_REGS + NUM_PSEUDO_REGS) * MAX_REGISTER_SIZE + 1);
fb40c209
AC
1439}
1440
fb40c209 1441void
fba45db2 1442_initialize_mi_main (void)
fb40c209 1443{
046a4708
AC
1444 DEPRECATED_REGISTER_GDBARCH_SWAP (old_regs);
1445 deprecated_register_gdbarch_swap (NULL, 0, mi_setup_architecture_data);
fb40c209 1446}
This page took 0.520168 seconds and 4 git commands to generate.