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