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