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