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