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