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