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