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