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