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