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