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