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