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