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