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