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