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