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