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