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