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