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