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