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