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