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