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