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