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