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