]>
Commit | Line | Data |
---|---|---|
fb40c209 | 1 | /* MI Command Set. |
cd0bfa36 | 2 | |
4c38e0a4 | 3 | Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010 |
6aba47ca | 4 | Free Software Foundation, Inc. |
cd0bfa36 | 5 | |
ab91fdd5 | 6 | Contributed by Cygnus Solutions (a Red Hat company). |
fb40c209 AC |
7 | |
8 | This file is part of GDB. | |
9 | ||
10 | This program is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 12 | the Free Software Foundation; either version 3 of the License, or |
fb40c209 AC |
13 | (at your option) any later version. |
14 | ||
15 | This program is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
fb40c209 | 22 | |
41296c92 | 23 | /* Work in progress. */ |
fb40c209 AC |
24 | |
25 | #include "defs.h" | |
e17c207e | 26 | #include "arch-utils.h" |
fb40c209 AC |
27 | #include "target.h" |
28 | #include "inferior.h" | |
29 | #include "gdb_string.h" | |
60250e8b | 30 | #include "exceptions.h" |
fb40c209 AC |
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" | |
41296c92 | 42 | #include "gdbcore.h" /* For write_memory(). */ |
56178203 | 43 | #include "value.h" |
4e052eda | 44 | #include "regcache.h" |
5b7f31a4 | 45 | #include "gdb.h" |
36dc181b | 46 | #include "frame.h" |
b9362cc7 | 47 | #include "mi-main.h" |
66bb093b | 48 | #include "mi-common.h" |
d8ca156b | 49 | #include "language.h" |
79a45b7d | 50 | #include "valprint.h" |
3ee1c036 | 51 | #include "inferior.h" |
07e059b5 | 52 | #include "osdata.h" |
dc146f7c | 53 | #include "splay-tree.h" |
36dc181b | 54 | |
fb40c209 AC |
55 | #include <ctype.h> |
56 | #include <sys/time.h> | |
57 | ||
d8c83789 NR |
58 | #if defined HAVE_SYS_RESOURCE_H |
59 | #include <sys/resource.h> | |
60 | #endif | |
61 | ||
62 | #ifdef HAVE_GETRUSAGE | |
63 | struct rusage rusage; | |
64 | #endif | |
65 | ||
fb40c209 AC |
66 | enum |
67 | { | |
68 | FROM_TTY = 0 | |
69 | }; | |
70 | ||
fb40c209 AC |
71 | int mi_debug_p; |
72 | struct ui_file *raw_stdout; | |
73 | ||
d8c83789 NR |
74 | /* This is used to pass the current command timestamp |
75 | down to continuation routines. */ | |
76 | static struct mi_timestamp *current_command_ts; | |
77 | ||
78 | static int do_timings = 0; | |
79 | ||
a2840c35 VP |
80 | char *current_token; |
81 | int running_result_record_printed = 1; | |
fb40c209 | 82 | |
f3b1572e PA |
83 | /* Flag indicating that the target has proceeded since the last |
84 | command was issued. */ | |
85 | int mi_proceeded; | |
86 | ||
fb40c209 | 87 | extern void _initialize_mi_main (void); |
ce8f13f8 | 88 | static void mi_cmd_execute (struct mi_parse *parse); |
fb40c209 | 89 | |
b2af646b AC |
90 | static void mi_execute_cli_command (const char *cmd, int args_p, |
91 | const char *args); | |
ce8f13f8 | 92 | static void mi_execute_async_cli_command (char *cli_command, |
9e22b03a | 93 | char **argv, int argc); |
6ed7ea50 UW |
94 | static int register_changed_p (int regnum, struct regcache *, |
95 | struct regcache *); | |
7ccb0be9 | 96 | static void get_register (struct frame_info *, int regnum, int format); |
4389a95a | 97 | |
41296c92 | 98 | /* Command implementations. FIXME: Is this libgdb? No. This is the MI |
fb40c209 | 99 | layer that calls libgdb. Any operation used in the below should be |
41296c92 | 100 | formalized. */ |
fb40c209 | 101 | |
d8c83789 NR |
102 | static void timestamp (struct mi_timestamp *tv); |
103 | ||
104 | static void print_diff_now (struct mi_timestamp *start); | |
105 | static void print_diff (struct mi_timestamp *start, struct mi_timestamp *end); | |
106 | ||
ce8f13f8 | 107 | void |
fb40c209 AC |
108 | mi_cmd_gdb_exit (char *command, char **argv, int argc) |
109 | { | |
41296c92 | 110 | /* We have to print everything right here because we never return. */ |
721c02de VP |
111 | if (current_token) |
112 | fputs_unfiltered (current_token, raw_stdout); | |
fb40c209 AC |
113 | fputs_unfiltered ("^exit\n", raw_stdout); |
114 | mi_out_put (uiout, raw_stdout); | |
a6b29f87 | 115 | gdb_flush (raw_stdout); |
41296c92 | 116 | /* FIXME: The function called is not yet a formal libgdb function. */ |
fb40c209 | 117 | quit_force (NULL, FROM_TTY); |
fb40c209 AC |
118 | } |
119 | ||
ce8f13f8 | 120 | void |
9e22b03a | 121 | mi_cmd_exec_next (char *command, char **argv, int argc) |
fb40c209 | 122 | { |
41296c92 | 123 | /* FIXME: Should call a libgdb function, not a cli wrapper. */ |
8931f526 | 124 | mi_execute_async_cli_command ("next", argv, argc); |
fb40c209 AC |
125 | } |
126 | ||
ce8f13f8 | 127 | void |
9e22b03a | 128 | mi_cmd_exec_next_instruction (char *command, char **argv, int argc) |
fb40c209 | 129 | { |
41296c92 | 130 | /* FIXME: Should call a libgdb function, not a cli wrapper. */ |
8931f526 | 131 | mi_execute_async_cli_command ("nexti", argv, argc); |
fb40c209 AC |
132 | } |
133 | ||
ce8f13f8 | 134 | void |
9e22b03a | 135 | mi_cmd_exec_step (char *command, char **argv, int argc) |
fb40c209 | 136 | { |
41296c92 | 137 | /* FIXME: Should call a libgdb function, not a cli wrapper. */ |
8931f526 | 138 | mi_execute_async_cli_command ("step", argv, argc); |
fb40c209 AC |
139 | } |
140 | ||
ce8f13f8 | 141 | void |
9e22b03a | 142 | mi_cmd_exec_step_instruction (char *command, char **argv, int argc) |
fb40c209 | 143 | { |
41296c92 | 144 | /* FIXME: Should call a libgdb function, not a cli wrapper. */ |
8931f526 | 145 | mi_execute_async_cli_command ("stepi", argv, argc); |
fb40c209 AC |
146 | } |
147 | ||
ce8f13f8 | 148 | void |
9e22b03a | 149 | mi_cmd_exec_finish (char *command, char **argv, int argc) |
fb40c209 | 150 | { |
41296c92 | 151 | /* FIXME: Should call a libgdb function, not a cli wrapper. */ |
8931f526 | 152 | mi_execute_async_cli_command ("finish", argv, argc); |
fb40c209 AC |
153 | } |
154 | ||
ce8f13f8 | 155 | void |
9e22b03a | 156 | mi_cmd_exec_return (char *command, char **argv, int argc) |
fb40c209 | 157 | { |
fb40c209 AC |
158 | /* This command doesn't really execute the target, it just pops the |
159 | specified number of frames. */ | |
9e22b03a | 160 | if (argc) |
fb40c209 | 161 | /* Call return_command with from_tty argument equal to 0 so as to |
41296c92 | 162 | avoid being queried. */ |
9e22b03a | 163 | return_command (*argv, 0); |
fb40c209 AC |
164 | else |
165 | /* Call return_command with from_tty argument equal to 0 so as to | |
41296c92 | 166 | avoid being queried. */ |
36dc181b | 167 | return_command (NULL, 0); |
fb40c209 AC |
168 | |
169 | /* Because we have called return_command with from_tty = 0, we need | |
41296c92 | 170 | to print the frame here. */ |
b04f3ab4 | 171 | print_stack_frame (get_selected_frame (NULL), 1, LOC_AND_ADDRESS); |
fb40c209 AC |
172 | } |
173 | ||
143260c9 VP |
174 | void |
175 | mi_cmd_exec_jump (char *args, char **argv, int argc) | |
176 | { | |
177 | /* FIXME: Should call a libgdb function, not a cli wrapper. */ | |
178 | return mi_execute_async_cli_command ("jump", argv, argc); | |
179 | } | |
180 | ||
8dd4f202 VP |
181 | static int |
182 | proceed_thread_callback (struct thread_info *thread, void *arg) | |
183 | { | |
184 | int pid = *(int *)arg; | |
185 | ||
186 | if (!is_stopped (thread->ptid)) | |
187 | return 0; | |
188 | ||
189 | if (PIDGET (thread->ptid) != pid) | |
190 | return 0; | |
191 | ||
192 | switch_to_thread (thread->ptid); | |
193 | clear_proceed_status (); | |
194 | proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0); | |
195 | return 0; | |
196 | } | |
197 | ||
ce8f13f8 | 198 | void |
9e22b03a | 199 | mi_cmd_exec_continue (char *command, char **argv, int argc) |
fb40c209 | 200 | { |
77ebaa5a VP |
201 | if (argc == 0) |
202 | continue_1 (0); | |
203 | else if (argc == 1 && strcmp (argv[0], "--all") == 0) | |
204 | continue_1 (1); | |
8dd4f202 VP |
205 | else if (argc == 2 && strcmp (argv[0], "--thread-group") == 0) |
206 | { | |
207 | struct cleanup *old_chain; | |
208 | int pid; | |
209 | if (argv[1] == NULL || argv[1] == '\0') | |
210 | error ("Thread group id not specified"); | |
4b459977 | 211 | pid = atoi (argv[1]); |
8dd4f202 VP |
212 | if (!in_inferior_list (pid)) |
213 | error ("Invalid thread group id '%s'", argv[1]); | |
214 | ||
215 | old_chain = make_cleanup_restore_current_thread (); | |
216 | iterate_over_threads (proceed_thread_callback, &pid); | |
217 | do_cleanups (old_chain); | |
218 | } | |
77ebaa5a | 219 | else |
8dd4f202 VP |
220 | error ("Usage: -exec-continue [--all|--thread-group id]"); |
221 | } | |
222 | ||
223 | static int | |
224 | interrupt_thread_callback (struct thread_info *thread, void *arg) | |
225 | { | |
226 | int pid = *(int *)arg; | |
227 | ||
228 | if (!is_running (thread->ptid)) | |
229 | return 0; | |
230 | ||
231 | if (PIDGET (thread->ptid) != pid) | |
232 | return 0; | |
233 | ||
234 | target_stop (thread->ptid); | |
235 | return 0; | |
fb40c209 AC |
236 | } |
237 | ||
41296c92 | 238 | /* Interrupt the execution of the target. Note how we must play around |
d8c83789 | 239 | with the token variables, in order to display the current token in |
fb40c209 | 240 | the result of the interrupt command, and the previous execution |
41296c92 NR |
241 | token when the target finally stops. See comments in |
242 | mi_cmd_execute. */ | |
ce8f13f8 | 243 | void |
9e22b03a | 244 | mi_cmd_exec_interrupt (char *command, char **argv, int argc) |
fb40c209 | 245 | { |
77ebaa5a VP |
246 | if (argc == 0) |
247 | { | |
248 | if (!is_running (inferior_ptid)) | |
249 | error ("Current thread is not running."); | |
250 | ||
251 | interrupt_target_1 (0); | |
252 | } | |
253 | else if (argc == 1 && strcmp (argv[0], "--all") == 0) | |
254 | { | |
255 | if (!any_running ()) | |
256 | error ("Inferior not running."); | |
8dd4f202 | 257 | |
77ebaa5a VP |
258 | interrupt_target_1 (1); |
259 | } | |
8dd4f202 VP |
260 | else if (argc == 2 && strcmp (argv[0], "--thread-group") == 0) |
261 | { | |
262 | struct cleanup *old_chain; | |
263 | int pid; | |
264 | if (argv[1] == NULL || argv[1] == '\0') | |
265 | error ("Thread group id not specified"); | |
4b459977 | 266 | pid = atoi (argv[1]); |
8dd4f202 VP |
267 | if (!in_inferior_list (pid)) |
268 | error ("Invalid thread group id '%s'", argv[1]); | |
269 | ||
270 | old_chain = make_cleanup_restore_current_thread (); | |
271 | iterate_over_threads (interrupt_thread_callback, &pid); | |
272 | do_cleanups (old_chain); | |
273 | } | |
77ebaa5a | 274 | else |
8dd4f202 | 275 | error ("Usage: -exec-interrupt [--all|--thread-group id]"); |
fb40c209 AC |
276 | } |
277 | ||
6418d433 VP |
278 | static int |
279 | find_thread_of_process (struct thread_info *ti, void *p) | |
280 | { | |
281 | int pid = *(int *)p; | |
282 | if (PIDGET (ti->ptid) == pid && !is_exited (ti->ptid)) | |
283 | return 1; | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
288 | void | |
289 | mi_cmd_target_detach (char *command, char **argv, int argc) | |
290 | { | |
291 | if (argc != 0 && argc != 1) | |
292 | error ("Usage: -target-detach [thread-group]"); | |
293 | ||
294 | if (argc == 1) | |
295 | { | |
296 | struct thread_info *tp; | |
297 | char *end = argv[0]; | |
298 | int pid = strtol (argv[0], &end, 10); | |
299 | if (*end != '\0') | |
300 | error (_("Cannot parse thread group id '%s'"), argv[0]); | |
301 | ||
302 | /* Pick any thread in the desired process. Current | |
303 | target_detach deteches from the parent of inferior_ptid. */ | |
304 | tp = iterate_over_threads (find_thread_of_process, &pid); | |
305 | if (!tp) | |
306 | error (_("Thread group is empty")); | |
307 | ||
308 | switch_to_thread (tp->ptid); | |
309 | } | |
310 | ||
311 | detach_command (NULL, 0); | |
312 | } | |
313 | ||
ce8f13f8 | 314 | void |
fb40c209 AC |
315 | mi_cmd_thread_select (char *command, char **argv, int argc) |
316 | { | |
317 | enum gdb_rc rc; | |
a13e061a | 318 | char *mi_error_message; |
fb40c209 AC |
319 | |
320 | if (argc != 1) | |
a13e061a PA |
321 | error ("mi_cmd_thread_select: USAGE: threadnum."); |
322 | ||
323 | rc = gdb_thread_select (uiout, argv[0], &mi_error_message); | |
324 | ||
325 | if (rc == GDB_RC_FAIL) | |
fb40c209 | 326 | { |
a13e061a PA |
327 | make_cleanup (xfree, mi_error_message); |
328 | error ("%s", mi_error_message); | |
fb40c209 | 329 | } |
fb40c209 AC |
330 | } |
331 | ||
ce8f13f8 | 332 | void |
fb40c209 AC |
333 | mi_cmd_thread_list_ids (char *command, char **argv, int argc) |
334 | { | |
b0b13bb4 | 335 | enum gdb_rc rc; |
a13e061a | 336 | char *mi_error_message; |
fb40c209 AC |
337 | |
338 | if (argc != 0) | |
a13e061a PA |
339 | error ("mi_cmd_thread_list_ids: No arguments required."); |
340 | ||
341 | rc = gdb_list_thread_ids (uiout, &mi_error_message); | |
342 | ||
343 | if (rc == GDB_RC_FAIL) | |
fb40c209 | 344 | { |
a13e061a PA |
345 | make_cleanup (xfree, mi_error_message); |
346 | error ("%s", mi_error_message); | |
fb40c209 | 347 | } |
fb40c209 AC |
348 | } |
349 | ||
ce8f13f8 | 350 | void |
8e8901c5 VP |
351 | mi_cmd_thread_info (char *command, char **argv, int argc) |
352 | { | |
353 | int thread = -1; | |
354 | ||
355 | if (argc != 0 && argc != 1) | |
a13e061a | 356 | error ("Invalid MI command"); |
8e8901c5 VP |
357 | |
358 | if (argc == 1) | |
359 | thread = atoi (argv[0]); | |
360 | ||
3ee1c036 VP |
361 | print_thread_info (uiout, thread, -1); |
362 | } | |
363 | ||
dc146f7c VP |
364 | struct collect_cores_data |
365 | { | |
366 | int pid; | |
367 | ||
368 | VEC (int) *cores; | |
369 | }; | |
370 | ||
3ee1c036 | 371 | static int |
dc146f7c | 372 | collect_cores (struct thread_info *ti, void *xdata) |
3ee1c036 | 373 | { |
dc146f7c VP |
374 | struct collect_cores_data *data = xdata; |
375 | ||
376 | if (ptid_get_pid (ti->ptid) == data->pid) | |
6c95b8df | 377 | { |
dc146f7c VP |
378 | int core = target_core_of_thread (ti->ptid); |
379 | if (core != -1) | |
380 | VEC_safe_push (int, data->cores, core); | |
381 | } | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
386 | static int * | |
387 | unique (int *b, int *e) | |
388 | { | |
389 | int *d = b; | |
390 | while (++b != e) | |
391 | if (*d != *b) | |
392 | *++d = *b; | |
393 | return ++d; | |
394 | } | |
395 | ||
396 | struct print_one_inferior_data | |
397 | { | |
398 | int recurse; | |
399 | VEC (int) *inferiors; | |
400 | }; | |
401 | ||
402 | static int | |
403 | print_one_inferior (struct inferior *inferior, void *xdata) | |
404 | { | |
405 | struct print_one_inferior_data *top_data = xdata; | |
406 | ||
407 | if (VEC_empty (int, top_data->inferiors) | |
408 | || bsearch (&(inferior->pid), VEC_address (int, top_data->inferiors), | |
409 | VEC_length (int, top_data->inferiors), sizeof (int), | |
410 | compare_positive_ints)) | |
411 | { | |
412 | struct collect_cores_data data; | |
6c95b8df PA |
413 | struct cleanup *back_to |
414 | = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); | |
415 | ||
416 | ui_out_field_fmt (uiout, "id", "%d", inferior->pid); | |
417 | ui_out_field_string (uiout, "type", "process"); | |
418 | ui_out_field_int (uiout, "pid", inferior->pid); | |
419 | ||
dc146f7c VP |
420 | data.pid = inferior->pid; |
421 | data.cores = 0; | |
422 | iterate_over_threads (collect_cores, &data); | |
423 | ||
424 | if (!VEC_empty (int, data.cores)) | |
425 | { | |
426 | int elt; | |
427 | int i; | |
428 | int *b, *e; | |
429 | struct cleanup *back_to_2 = | |
430 | make_cleanup_ui_out_list_begin_end (uiout, "cores"); | |
431 | ||
432 | qsort (VEC_address (int, data.cores), | |
433 | VEC_length (int, data.cores), sizeof (int), | |
434 | compare_positive_ints); | |
435 | ||
436 | b = VEC_address (int, data.cores); | |
437 | e = b + VEC_length (int, data.cores); | |
438 | e = unique (b, e); | |
439 | ||
440 | for (; b != e; ++b) | |
441 | ui_out_field_int (uiout, NULL, *b); | |
442 | ||
443 | do_cleanups (back_to_2); | |
444 | } | |
445 | ||
446 | if (top_data->recurse) | |
447 | print_thread_info (uiout, -1, inferior->pid); | |
448 | ||
6c95b8df PA |
449 | do_cleanups (back_to); |
450 | } | |
3ee1c036 | 451 | |
3ee1c036 VP |
452 | return 0; |
453 | } | |
454 | ||
dc146f7c VP |
455 | /* Output a field named 'cores' with a list as the value. The elements of |
456 | the list are obtained by splitting 'cores' on comma. */ | |
457 | ||
458 | static void | |
459 | output_cores (struct ui_out *uiout, const char *field_name, const char *xcores) | |
3ee1c036 | 460 | { |
dc146f7c VP |
461 | struct cleanup *back_to = make_cleanup_ui_out_list_begin_end (uiout, |
462 | field_name); | |
463 | char *cores = xstrdup (xcores); | |
464 | char *p = cores; | |
3ee1c036 | 465 | |
dc146f7c | 466 | make_cleanup (xfree, cores); |
3ee1c036 | 467 | |
dc146f7c VP |
468 | for (p = strtok (p, ","); p; p = strtok (NULL, ",")) |
469 | ui_out_field_string (uiout, NULL, p); | |
3ee1c036 | 470 | |
dc146f7c VP |
471 | do_cleanups (back_to); |
472 | } | |
3ee1c036 | 473 | |
dc146f7c VP |
474 | static void |
475 | free_vector_of_ints (void *xvector) | |
476 | { | |
477 | VEC (int) **vector = xvector; | |
478 | VEC_free (int, *vector); | |
479 | } | |
480 | ||
481 | static void | |
482 | do_nothing (splay_tree_key k) | |
483 | { | |
484 | } | |
07e059b5 | 485 | |
dc146f7c VP |
486 | static void |
487 | free_vector_of_osdata_items (splay_tree_value xvalue) | |
488 | { | |
489 | VEC (osdata_item_s) *value = (VEC (osdata_item_s) *) xvalue; | |
490 | /* We don't free the items itself, it will be done separately. */ | |
491 | VEC_free (osdata_item_s, value); | |
492 | } | |
e0665bc8 | 493 | |
dc146f7c VP |
494 | static int |
495 | splay_tree_int_comparator (splay_tree_key xa, splay_tree_key xb) | |
496 | { | |
497 | int a = xa; | |
498 | int b = xb; | |
499 | return a - b; | |
500 | } | |
501 | ||
502 | static void | |
503 | free_splay_tree (void *xt) | |
504 | { | |
505 | splay_tree t = xt; | |
506 | splay_tree_delete (t); | |
507 | } | |
508 | ||
509 | static void | |
510 | list_available_thread_groups (VEC (int) *ids, int recurse) | |
511 | { | |
512 | struct osdata *data; | |
513 | struct osdata_item *item; | |
514 | int ix_items; | |
515 | /* This keeps a map from integer (pid) to VEC (struct osdata_item *)* | |
8eee9c5a DE |
516 | The vector contains information about all threads for the given pid. |
517 | This is assigned an initial value to avoid "may be used uninitialized" | |
518 | warning from gcc. */ | |
519 | splay_tree tree = NULL; | |
dc146f7c VP |
520 | |
521 | /* get_osdata will throw if it cannot return data. */ | |
522 | data = get_osdata ("processes"); | |
523 | make_cleanup_osdata_free (data); | |
524 | ||
525 | if (recurse) | |
526 | { | |
527 | struct osdata *threads = get_osdata ("threads"); | |
528 | make_cleanup_osdata_free (threads); | |
529 | ||
530 | tree = splay_tree_new (splay_tree_int_comparator, | |
531 | do_nothing, | |
532 | free_vector_of_osdata_items); | |
533 | make_cleanup (free_splay_tree, tree); | |
e0665bc8 | 534 | |
07e059b5 | 535 | for (ix_items = 0; |
dc146f7c | 536 | VEC_iterate (osdata_item_s, threads->items, |
e0665bc8 | 537 | ix_items, item); |
07e059b5 VP |
538 | ix_items++) |
539 | { | |
07e059b5 | 540 | const char *pid = get_osdata_column (item, "pid"); |
dc146f7c VP |
541 | int pid_i = strtoul (pid, NULL, 0); |
542 | VEC (osdata_item_s) *vec = 0; | |
543 | ||
544 | splay_tree_node n = splay_tree_lookup (tree, pid_i); | |
545 | if (!n) | |
546 | { | |
547 | VEC_safe_push (osdata_item_s, vec, item); | |
548 | splay_tree_insert (tree, pid_i, (splay_tree_value)vec); | |
549 | } | |
550 | else | |
551 | { | |
552 | vec = (VEC (osdata_item_s) *) n->value; | |
553 | VEC_safe_push (osdata_item_s, vec, item); | |
554 | n->value = (splay_tree_value) vec; | |
555 | } | |
556 | } | |
557 | } | |
558 | ||
559 | make_cleanup_ui_out_list_begin_end (uiout, "groups"); | |
07e059b5 | 560 | |
dc146f7c VP |
561 | for (ix_items = 0; |
562 | VEC_iterate (osdata_item_s, data->items, | |
563 | ix_items, item); | |
564 | ix_items++) | |
565 | { | |
566 | struct cleanup *back_to; | |
e0665bc8 | 567 | |
dc146f7c VP |
568 | const char *pid = get_osdata_column (item, "pid"); |
569 | const char *cmd = get_osdata_column (item, "command"); | |
570 | const char *user = get_osdata_column (item, "user"); | |
571 | const char *cores = get_osdata_column (item, "cores"); | |
572 | ||
573 | int pid_i = strtoul (pid, NULL, 0); | |
574 | ||
575 | /* At present, the target will return all available processes | |
576 | and if information about specific ones was required, we filter | |
577 | undesired processes here. */ | |
578 | if (ids && bsearch (&pid_i, VEC_address (int, ids), | |
579 | VEC_length (int, ids), | |
580 | sizeof (int), compare_positive_ints) == NULL) | |
581 | continue; | |
582 | ||
583 | ||
584 | back_to = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); | |
585 | ||
586 | ui_out_field_fmt (uiout, "id", "%s", pid); | |
587 | ui_out_field_string (uiout, "type", "process"); | |
588 | if (cmd) | |
589 | ui_out_field_string (uiout, "description", cmd); | |
590 | if (user) | |
591 | ui_out_field_string (uiout, "user", user); | |
592 | if (cores) | |
593 | output_cores (uiout, "cores", cores); | |
594 | ||
595 | if (recurse) | |
596 | { | |
597 | splay_tree_node n = splay_tree_lookup (tree, pid_i); | |
598 | if (n) | |
599 | { | |
600 | VEC (osdata_item_s) *children = (VEC (osdata_item_s) *) n->value; | |
601 | struct osdata_item *child; | |
602 | int ix_child; | |
603 | ||
604 | make_cleanup_ui_out_list_begin_end (uiout, "threads"); | |
605 | ||
606 | for (ix_child = 0; | |
607 | VEC_iterate (osdata_item_s, children, ix_child, child); | |
608 | ++ix_child) | |
609 | { | |
610 | struct cleanup *back_to_2 = | |
611 | make_cleanup_ui_out_tuple_begin_end (uiout, NULL); | |
612 | ||
613 | const char *tid = get_osdata_column (child, "tid"); | |
614 | const char *tcore = get_osdata_column (child, "core"); | |
615 | ui_out_field_string (uiout, "id", tid); | |
616 | if (tcore) | |
617 | ui_out_field_string (uiout, "core", tcore); | |
618 | ||
619 | do_cleanups (back_to_2); | |
620 | } | |
621 | } | |
07e059b5 | 622 | } |
dc146f7c VP |
623 | |
624 | do_cleanups (back_to); | |
07e059b5 | 625 | } |
dc146f7c VP |
626 | } |
627 | ||
628 | void | |
629 | mi_cmd_list_thread_groups (char *command, char **argv, int argc) | |
630 | { | |
631 | struct cleanup *back_to; | |
632 | int available = 0; | |
633 | int recurse = 0; | |
634 | VEC (int) *ids = 0; | |
635 | ||
636 | enum opt | |
637 | { | |
638 | AVAILABLE_OPT, RECURSE_OPT | |
639 | }; | |
640 | static struct mi_opt opts[] = | |
641 | { | |
642 | {"-available", AVAILABLE_OPT, 0}, | |
643 | {"-recurse", RECURSE_OPT, 1}, | |
644 | { 0, 0, 0 } | |
645 | }; | |
646 | ||
647 | int optind = 0; | |
648 | char *optarg; | |
649 | ||
650 | while (1) | |
651 | { | |
652 | int opt = mi_getopt ("-list-thread-groups", argc, argv, opts, | |
653 | &optind, &optarg); | |
654 | if (opt < 0) | |
655 | break; | |
656 | switch ((enum opt) opt) | |
657 | { | |
658 | case AVAILABLE_OPT: | |
659 | available = 1; | |
660 | break; | |
661 | case RECURSE_OPT: | |
662 | if (strcmp (optarg, "0") == 0) | |
663 | ; | |
664 | else if (strcmp (optarg, "1") == 0) | |
665 | recurse = 1; | |
666 | else | |
667 | error ("only '0' and '1' are valid values for the '--recurse' option"); | |
668 | break; | |
669 | } | |
670 | } | |
671 | ||
672 | for (; optind < argc; ++optind) | |
673 | { | |
674 | char *end; | |
675 | int inf = strtoul (argv[optind], &end, 0); | |
676 | if (*end != '\0') | |
677 | error ("invalid group id '%s'", argv[optind]); | |
678 | VEC_safe_push (int, ids, inf); | |
679 | } | |
680 | if (VEC_length (int, ids) > 1) | |
681 | qsort (VEC_address (int, ids), | |
682 | VEC_length (int, ids), | |
683 | sizeof (int), compare_positive_ints); | |
684 | ||
685 | back_to = make_cleanup (free_vector_of_ints, &ids); | |
686 | ||
687 | if (available) | |
688 | { | |
689 | list_available_thread_groups (ids, recurse); | |
690 | } | |
691 | else if (VEC_length (int, ids) == 1) | |
3ee1c036 | 692 | { |
dc146f7c VP |
693 | /* Local thread groups, single id. */ |
694 | int pid = *VEC_address (int, ids); | |
3ee1c036 | 695 | if (!in_inferior_list (pid)) |
dc146f7c VP |
696 | error ("Invalid thread group id '%d'", pid); |
697 | print_thread_info (uiout, -1, pid); | |
3ee1c036 VP |
698 | } |
699 | else | |
700 | { | |
dc146f7c VP |
701 | struct print_one_inferior_data data; |
702 | data.recurse = recurse; | |
703 | data.inferiors = ids; | |
704 | ||
705 | /* Local thread groups. Either no explicit ids -- and we | |
706 | print everything, or several explicit ids. In both cases, | |
707 | we print more than one group, and have to use 'groups' | |
708 | as the top-level element. */ | |
3ee1c036 | 709 | make_cleanup_ui_out_list_begin_end (uiout, "groups"); |
dc146f7c VP |
710 | update_thread_list (); |
711 | iterate_over_inferiors (print_one_inferior, &data); | |
3ee1c036 | 712 | } |
dc146f7c | 713 | |
3ee1c036 | 714 | do_cleanups (back_to); |
8e8901c5 VP |
715 | } |
716 | ||
ce8f13f8 | 717 | void |
fb40c209 AC |
718 | mi_cmd_data_list_register_names (char *command, char **argv, int argc) |
719 | { | |
7ccb0be9 UW |
720 | struct frame_info *frame; |
721 | struct gdbarch *gdbarch; | |
fb40c209 AC |
722 | int regnum, numregs; |
723 | int i; | |
4060713b | 724 | struct cleanup *cleanup; |
fb40c209 AC |
725 | |
726 | /* Note that the test for a valid register must include checking the | |
c9f4d572 UW |
727 | gdbarch_register_name because gdbarch_num_regs may be allocated for |
728 | the union of the register sets within a family of related processors. | |
729 | In this case, some entries of gdbarch_register_name will change depending | |
730 | upon the particular processor being debugged. */ | |
fb40c209 | 731 | |
7ccb0be9 UW |
732 | frame = get_selected_frame (NULL); |
733 | gdbarch = get_frame_arch (frame); | |
734 | numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch); | |
fb40c209 | 735 | |
4060713b | 736 | cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names"); |
fb40c209 | 737 | |
41296c92 | 738 | if (argc == 0) /* No args, just do all the regs. */ |
fb40c209 AC |
739 | { |
740 | for (regnum = 0; | |
741 | regnum < numregs; | |
742 | regnum++) | |
743 | { | |
7ccb0be9 UW |
744 | if (gdbarch_register_name (gdbarch, regnum) == NULL |
745 | || *(gdbarch_register_name (gdbarch, regnum)) == '\0') | |
173d6894 AC |
746 | ui_out_field_string (uiout, NULL, ""); |
747 | else | |
c9f4d572 | 748 | ui_out_field_string (uiout, NULL, |
7ccb0be9 | 749 | gdbarch_register_name (gdbarch, regnum)); |
fb40c209 AC |
750 | } |
751 | } | |
752 | ||
41296c92 | 753 | /* Else, list of register #s, just do listed regs. */ |
fb40c209 AC |
754 | for (i = 0; i < argc; i++) |
755 | { | |
756 | regnum = atoi (argv[i]); | |
173d6894 | 757 | if (regnum < 0 || regnum >= numregs) |
a13e061a PA |
758 | error ("bad register number"); |
759 | ||
7ccb0be9 UW |
760 | if (gdbarch_register_name (gdbarch, regnum) == NULL |
761 | || *(gdbarch_register_name (gdbarch, regnum)) == '\0') | |
173d6894 AC |
762 | ui_out_field_string (uiout, NULL, ""); |
763 | else | |
c9f4d572 | 764 | ui_out_field_string (uiout, NULL, |
7ccb0be9 | 765 | gdbarch_register_name (gdbarch, regnum)); |
fb40c209 | 766 | } |
4060713b | 767 | do_cleanups (cleanup); |
fb40c209 AC |
768 | } |
769 | ||
ce8f13f8 | 770 | void |
fb40c209 AC |
771 | mi_cmd_data_list_changed_registers (char *command, char **argv, int argc) |
772 | { | |
6ed7ea50 UW |
773 | static struct regcache *this_regs = NULL; |
774 | struct regcache *prev_regs; | |
7ccb0be9 | 775 | struct gdbarch *gdbarch; |
fb40c209 AC |
776 | int regnum, numregs, changed; |
777 | int i; | |
4060713b | 778 | struct cleanup *cleanup; |
fb40c209 | 779 | |
6ed7ea50 UW |
780 | /* The last time we visited this function, the current frame's register |
781 | contents were saved in THIS_REGS. Move THIS_REGS over to PREV_REGS, | |
782 | and refresh THIS_REGS with the now-current register contents. */ | |
783 | ||
784 | prev_regs = this_regs; | |
785 | this_regs = frame_save_as_regcache (get_selected_frame (NULL)); | |
786 | cleanup = make_cleanup_regcache_xfree (prev_regs); | |
787 | ||
fb40c209 | 788 | /* Note that the test for a valid register must include checking the |
c9f4d572 UW |
789 | gdbarch_register_name because gdbarch_num_regs may be allocated for |
790 | the union of the register sets within a family of related processors. | |
791 | In this case, some entries of gdbarch_register_name will change depending | |
792 | upon the particular processor being debugged. */ | |
fb40c209 | 793 | |
7ccb0be9 UW |
794 | gdbarch = get_regcache_arch (this_regs); |
795 | numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch); | |
fb40c209 | 796 | |
6ed7ea50 | 797 | make_cleanup_ui_out_list_begin_end (uiout, "changed-registers"); |
fb40c209 | 798 | |
41296c92 | 799 | if (argc == 0) /* No args, just do all the regs. */ |
fb40c209 AC |
800 | { |
801 | for (regnum = 0; | |
802 | regnum < numregs; | |
803 | regnum++) | |
804 | { | |
7ccb0be9 UW |
805 | if (gdbarch_register_name (gdbarch, regnum) == NULL |
806 | || *(gdbarch_register_name (gdbarch, regnum)) == '\0') | |
fb40c209 | 807 | continue; |
6ed7ea50 | 808 | changed = register_changed_p (regnum, prev_regs, this_regs); |
fb40c209 | 809 | if (changed < 0) |
a13e061a | 810 | error ("mi_cmd_data_list_changed_registers: Unable to read register contents."); |
fb40c209 AC |
811 | else if (changed) |
812 | ui_out_field_int (uiout, NULL, regnum); | |
813 | } | |
814 | } | |
815 | ||
41296c92 | 816 | /* Else, list of register #s, just do listed regs. */ |
fb40c209 AC |
817 | for (i = 0; i < argc; i++) |
818 | { | |
819 | regnum = atoi (argv[i]); | |
820 | ||
821 | if (regnum >= 0 | |
822 | && regnum < numregs | |
7ccb0be9 UW |
823 | && gdbarch_register_name (gdbarch, regnum) != NULL |
824 | && *gdbarch_register_name (gdbarch, regnum) != '\000') | |
fb40c209 | 825 | { |
6ed7ea50 | 826 | changed = register_changed_p (regnum, prev_regs, this_regs); |
fb40c209 | 827 | if (changed < 0) |
a13e061a | 828 | error ("mi_cmd_data_list_register_change: Unable to read register contents."); |
fb40c209 AC |
829 | else if (changed) |
830 | ui_out_field_int (uiout, NULL, regnum); | |
831 | } | |
832 | else | |
a13e061a | 833 | error ("bad register number"); |
fb40c209 | 834 | } |
4060713b | 835 | do_cleanups (cleanup); |
fb40c209 AC |
836 | } |
837 | ||
838 | static int | |
6ed7ea50 UW |
839 | register_changed_p (int regnum, struct regcache *prev_regs, |
840 | struct regcache *this_regs) | |
fb40c209 | 841 | { |
6ed7ea50 UW |
842 | struct gdbarch *gdbarch = get_regcache_arch (this_regs); |
843 | gdb_byte prev_buffer[MAX_REGISTER_SIZE]; | |
844 | gdb_byte this_buffer[MAX_REGISTER_SIZE]; | |
fb40c209 | 845 | |
6ed7ea50 UW |
846 | /* Registers not valid in this frame return count as unchanged. */ |
847 | if (!regcache_valid_p (this_regs, regnum)) | |
fb40c209 AC |
848 | return 0; |
849 | ||
6ed7ea50 UW |
850 | /* First time through or after gdbarch change consider all registers as |
851 | changed. Same for registers not valid in the previous frame. */ | |
852 | if (!prev_regs || get_regcache_arch (prev_regs) != gdbarch | |
853 | || !regcache_valid_p (prev_regs, regnum)) | |
854 | return 1; | |
fb40c209 | 855 | |
6ed7ea50 UW |
856 | /* Get register contents and compare. */ |
857 | regcache_cooked_read (prev_regs, regnum, prev_buffer); | |
858 | regcache_cooked_read (this_regs, regnum, this_buffer); | |
fb40c209 | 859 | |
6ed7ea50 UW |
860 | return memcmp (prev_buffer, this_buffer, |
861 | register_size (gdbarch, regnum)) != 0; | |
fb40c209 AC |
862 | } |
863 | ||
41296c92 | 864 | /* Return a list of register number and value pairs. The valid |
fb40c209 | 865 | arguments expected are: a letter indicating the format in which to |
41296c92 | 866 | display the registers contents. This can be one of: x (hexadecimal), d |
fb40c209 AC |
867 | (decimal), N (natural), t (binary), o (octal), r (raw). After the |
868 | format argumetn there can be a sequence of numbers, indicating which | |
41296c92 NR |
869 | registers to fetch the content of. If the format is the only argument, |
870 | a list of all the registers with their values is returned. */ | |
ce8f13f8 | 871 | void |
fb40c209 AC |
872 | mi_cmd_data_list_register_values (char *command, char **argv, int argc) |
873 | { | |
7ccb0be9 UW |
874 | struct frame_info *frame; |
875 | struct gdbarch *gdbarch; | |
a13e061a | 876 | int regnum, numregs, format; |
fb40c209 | 877 | int i; |
4060713b | 878 | struct cleanup *list_cleanup, *tuple_cleanup; |
fb40c209 AC |
879 | |
880 | /* Note that the test for a valid register must include checking the | |
c9f4d572 UW |
881 | gdbarch_register_name because gdbarch_num_regs may be allocated for |
882 | the union of the register sets within a family of related processors. | |
883 | In this case, some entries of gdbarch_register_name will change depending | |
884 | upon the particular processor being debugged. */ | |
fb40c209 | 885 | |
fb40c209 | 886 | if (argc == 0) |
a13e061a | 887 | error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]"); |
fb40c209 AC |
888 | |
889 | format = (int) argv[0][0]; | |
890 | ||
7ccb0be9 UW |
891 | frame = get_selected_frame (NULL); |
892 | gdbarch = get_frame_arch (frame); | |
893 | numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch); | |
894 | ||
4060713b | 895 | list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values"); |
fb40c209 | 896 | |
41296c92 | 897 | if (argc == 1) /* No args, beside the format: do all the regs. */ |
fb40c209 AC |
898 | { |
899 | for (regnum = 0; | |
900 | regnum < numregs; | |
901 | regnum++) | |
902 | { | |
7ccb0be9 UW |
903 | if (gdbarch_register_name (gdbarch, regnum) == NULL |
904 | || *(gdbarch_register_name (gdbarch, regnum)) == '\0') | |
fb40c209 | 905 | continue; |
4060713b | 906 | tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); |
fb40c209 | 907 | ui_out_field_int (uiout, "number", regnum); |
7ccb0be9 | 908 | get_register (frame, regnum, format); |
4060713b | 909 | do_cleanups (tuple_cleanup); |
fb40c209 AC |
910 | } |
911 | } | |
912 | ||
41296c92 | 913 | /* Else, list of register #s, just do listed regs. */ |
fb40c209 AC |
914 | for (i = 1; i < argc; i++) |
915 | { | |
916 | regnum = atoi (argv[i]); | |
917 | ||
918 | if (regnum >= 0 | |
919 | && regnum < numregs | |
7ccb0be9 UW |
920 | && gdbarch_register_name (gdbarch, regnum) != NULL |
921 | && *gdbarch_register_name (gdbarch, regnum) != '\000') | |
fb40c209 | 922 | { |
4060713b | 923 | tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); |
fb40c209 | 924 | ui_out_field_int (uiout, "number", regnum); |
7ccb0be9 | 925 | get_register (frame, regnum, format); |
4060713b | 926 | do_cleanups (tuple_cleanup); |
fb40c209 AC |
927 | } |
928 | else | |
a13e061a | 929 | error ("bad register number"); |
fb40c209 | 930 | } |
4060713b | 931 | do_cleanups (list_cleanup); |
fb40c209 AC |
932 | } |
933 | ||
41296c92 | 934 | /* Output one register's contents in the desired format. */ |
a13e061a | 935 | static void |
7ccb0be9 | 936 | get_register (struct frame_info *frame, int regnum, int format) |
fb40c209 | 937 | { |
7ccb0be9 | 938 | struct gdbarch *gdbarch = get_frame_arch (frame); |
10c42a71 | 939 | gdb_byte buffer[MAX_REGISTER_SIZE]; |
fb40c209 | 940 | int optim; |
ac2adee5 AC |
941 | int realnum; |
942 | CORE_ADDR addr; | |
943 | enum lval_type lval; | |
fb40c209 AC |
944 | static struct ui_stream *stb = NULL; |
945 | ||
946 | stb = ui_out_stream_new (uiout); | |
947 | ||
948 | if (format == 'N') | |
949 | format = 0; | |
950 | ||
7ccb0be9 | 951 | frame_register (frame, regnum, &optim, &lval, &addr, &realnum, buffer); |
ac2adee5 | 952 | |
fb40c209 | 953 | if (optim) |
a13e061a | 954 | error ("Optimized out"); |
fb40c209 | 955 | |
fb40c209 AC |
956 | if (format == 'r') |
957 | { | |
958 | int j; | |
959 | char *ptr, buf[1024]; | |
960 | ||
961 | strcpy (buf, "0x"); | |
962 | ptr = buf + 2; | |
7ccb0be9 | 963 | for (j = 0; j < register_size (gdbarch, regnum); j++) |
fb40c209 | 964 | { |
7ccb0be9 UW |
965 | int idx = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? |
966 | j : register_size (gdbarch, regnum) - 1 - j; | |
9730f241 | 967 | sprintf (ptr, "%02x", (unsigned char) buffer[idx]); |
fb40c209 AC |
968 | ptr += 2; |
969 | } | |
970 | ui_out_field_string (uiout, "value", buf); | |
971 | /*fputs_filtered (buf, gdb_stdout); */ | |
972 | } | |
973 | else | |
974 | { | |
79a45b7d | 975 | struct value_print_options opts; |
59669435 | 976 | get_formatted_print_options (&opts, format); |
79a45b7d | 977 | opts.deref_ref = 1; |
7ccb0be9 | 978 | val_print (register_type (gdbarch, regnum), buffer, 0, 0, |
79a45b7d | 979 | stb->stream, 0, &opts, current_language); |
fb40c209 AC |
980 | ui_out_field_stream (uiout, "value", stb); |
981 | ui_out_stream_delete (stb); | |
982 | } | |
fb40c209 AC |
983 | } |
984 | ||
24e8cecf | 985 | /* Write given values into registers. The registers and values are |
41296c92 | 986 | given as pairs. The corresponding MI command is |
24e8cecf | 987 | -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/ |
ce8f13f8 | 988 | void |
24e8cecf EZ |
989 | mi_cmd_data_write_register_values (char *command, char **argv, int argc) |
990 | { | |
7ccb0be9 UW |
991 | struct regcache *regcache; |
992 | struct gdbarch *gdbarch; | |
9f3a1602 | 993 | int numregs, i; |
24e8cecf EZ |
994 | char format; |
995 | ||
996 | /* Note that the test for a valid register must include checking the | |
c9f4d572 UW |
997 | gdbarch_register_name because gdbarch_num_regs may be allocated for |
998 | the union of the register sets within a family of related processors. | |
999 | In this case, some entries of gdbarch_register_name will change depending | |
1000 | upon the particular processor being debugged. */ | |
24e8cecf | 1001 | |
7ccb0be9 UW |
1002 | regcache = get_current_regcache (); |
1003 | gdbarch = get_regcache_arch (regcache); | |
1004 | numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch); | |
24e8cecf EZ |
1005 | |
1006 | if (argc == 0) | |
a13e061a | 1007 | error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]"); |
24e8cecf EZ |
1008 | |
1009 | format = (int) argv[0][0]; | |
1010 | ||
1011 | if (!target_has_registers) | |
a13e061a | 1012 | error ("mi_cmd_data_write_register_values: No registers."); |
24e8cecf EZ |
1013 | |
1014 | if (!(argc - 1)) | |
a13e061a | 1015 | error ("mi_cmd_data_write_register_values: No regs and values specified."); |
24e8cecf EZ |
1016 | |
1017 | if ((argc - 1) % 2) | |
a13e061a | 1018 | error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs."); |
24e8cecf EZ |
1019 | |
1020 | for (i = 1; i < argc; i = i + 2) | |
1021 | { | |
9f3a1602 | 1022 | int regnum = atoi (argv[i]); |
24e8cecf | 1023 | |
9f3a1602 | 1024 | if (regnum >= 0 && regnum < numregs |
7ccb0be9 UW |
1025 | && gdbarch_register_name (gdbarch, regnum) |
1026 | && *gdbarch_register_name (gdbarch, regnum)) | |
24e8cecf | 1027 | { |
9f3a1602 | 1028 | LONGEST value; |
d8bf3afa | 1029 | |
9f3a1602 | 1030 | /* Get the value as a number. */ |
24e8cecf | 1031 | value = parse_and_eval_address (argv[i + 1]); |
9f3a1602 | 1032 | |
41296c92 | 1033 | /* Write it down. */ |
7ccb0be9 | 1034 | regcache_cooked_write_signed (regcache, regnum, value); |
24e8cecf EZ |
1035 | } |
1036 | else | |
a13e061a | 1037 | error ("bad register number"); |
24e8cecf | 1038 | } |
24e8cecf EZ |
1039 | } |
1040 | ||
41296c92 | 1041 | /* Evaluate the value of the argument. The argument is an |
fb40c209 | 1042 | expression. If the expression contains spaces it needs to be |
41296c92 | 1043 | included in double quotes. */ |
ce8f13f8 | 1044 | void |
fb40c209 AC |
1045 | mi_cmd_data_evaluate_expression (char *command, char **argv, int argc) |
1046 | { | |
1047 | struct expression *expr; | |
1048 | struct cleanup *old_chain = NULL; | |
96052a95 | 1049 | struct value *val; |
fb40c209 | 1050 | struct ui_stream *stb = NULL; |
79a45b7d | 1051 | struct value_print_options opts; |
fb40c209 AC |
1052 | |
1053 | stb = ui_out_stream_new (uiout); | |
1054 | ||
1055 | if (argc != 1) | |
1056 | { | |
412bbd6c | 1057 | ui_out_stream_delete (stb); |
a13e061a | 1058 | error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression"); |
fb40c209 AC |
1059 | } |
1060 | ||
1061 | expr = parse_expression (argv[0]); | |
1062 | ||
47cf603e | 1063 | old_chain = make_cleanup (free_current_contents, &expr); |
fb40c209 AC |
1064 | |
1065 | val = evaluate_expression (expr); | |
1066 | ||
41296c92 | 1067 | /* Print the result of the expression evaluation. */ |
79a45b7d TT |
1068 | get_user_print_options (&opts); |
1069 | opts.deref_ref = 0; | |
0fd88904 | 1070 | val_print (value_type (val), value_contents (val), |
42ae5230 | 1071 | value_embedded_offset (val), value_address (val), |
79a45b7d | 1072 | stb->stream, 0, &opts, current_language); |
fb40c209 AC |
1073 | |
1074 | ui_out_field_stream (uiout, "value", stb); | |
1075 | ui_out_stream_delete (stb); | |
1076 | ||
1077 | do_cleanups (old_chain); | |
fb40c209 AC |
1078 | } |
1079 | ||
fb40c209 AC |
1080 | /* DATA-MEMORY-READ: |
1081 | ||
1082 | ADDR: start address of data to be dumped. | |
41296c92 | 1083 | WORD-FORMAT: a char indicating format for the ``word''. See |
fb40c209 | 1084 | the ``x'' command. |
41296c92 | 1085 | WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes. |
fb40c209 AC |
1086 | NR_ROW: Number of rows. |
1087 | NR_COL: The number of colums (words per row). | |
1088 | ASCHAR: (OPTIONAL) Append an ascii character dump to each row. Use | |
1089 | ASCHAR for unprintable characters. | |
1090 | ||
1091 | Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and | |
1092 | displayes them. Returns: | |
1093 | ||
1094 | {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...} | |
1095 | ||
1096 | Returns: | |
1097 | The number of bytes read is SIZE*ROW*COL. */ | |
1098 | ||
ce8f13f8 | 1099 | void |
fb40c209 AC |
1100 | mi_cmd_data_read_memory (char *command, char **argv, int argc) |
1101 | { | |
e17c207e | 1102 | struct gdbarch *gdbarch = get_current_arch (); |
fb40c209 AC |
1103 | struct cleanup *cleanups = make_cleanup (null_cleanup, NULL); |
1104 | CORE_ADDR addr; | |
1105 | long total_bytes; | |
1106 | long nr_cols; | |
1107 | long nr_rows; | |
1108 | char word_format; | |
1109 | struct type *word_type; | |
1110 | long word_size; | |
1111 | char word_asize; | |
1112 | char aschar; | |
508416a1 | 1113 | gdb_byte *mbuf; |
fb40c209 AC |
1114 | int nr_bytes; |
1115 | long offset = 0; | |
1116 | int optind = 0; | |
1117 | char *optarg; | |
1118 | enum opt | |
1119 | { | |
1120 | OFFSET_OPT | |
1121 | }; | |
1122 | static struct mi_opt opts[] = | |
1123 | { | |
1124 | {"o", OFFSET_OPT, 1}, | |
d5d6fca5 | 1125 | { 0, 0, 0 } |
fb40c209 AC |
1126 | }; |
1127 | ||
1128 | while (1) | |
1129 | { | |
1130 | int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts, | |
1131 | &optind, &optarg); | |
1132 | if (opt < 0) | |
1133 | break; | |
1134 | switch ((enum opt) opt) | |
1135 | { | |
1136 | case OFFSET_OPT: | |
1137 | offset = atol (optarg); | |
1138 | break; | |
1139 | } | |
1140 | } | |
1141 | argv += optind; | |
1142 | argc -= optind; | |
1143 | ||
1144 | if (argc < 5 || argc > 6) | |
a13e061a | 1145 | error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR]."); |
fb40c209 AC |
1146 | |
1147 | /* Extract all the arguments. */ | |
1148 | ||
41296c92 | 1149 | /* Start address of the memory dump. */ |
fb40c209 | 1150 | addr = parse_and_eval_address (argv[0]) + offset; |
41296c92 | 1151 | /* The format character to use when displaying a memory word. See |
fb40c209 AC |
1152 | the ``x'' command. */ |
1153 | word_format = argv[1][0]; | |
41296c92 | 1154 | /* The size of the memory word. */ |
fb40c209 AC |
1155 | word_size = atol (argv[2]); |
1156 | switch (word_size) | |
1157 | { | |
1158 | case 1: | |
df4df182 | 1159 | word_type = builtin_type (gdbarch)->builtin_int8; |
fb40c209 AC |
1160 | word_asize = 'b'; |
1161 | break; | |
1162 | case 2: | |
df4df182 | 1163 | word_type = builtin_type (gdbarch)->builtin_int16; |
fb40c209 AC |
1164 | word_asize = 'h'; |
1165 | break; | |
1166 | case 4: | |
df4df182 | 1167 | word_type = builtin_type (gdbarch)->builtin_int32; |
fb40c209 AC |
1168 | word_asize = 'w'; |
1169 | break; | |
1170 | case 8: | |
df4df182 | 1171 | word_type = builtin_type (gdbarch)->builtin_int64; |
fb40c209 AC |
1172 | word_asize = 'g'; |
1173 | break; | |
1174 | default: | |
df4df182 | 1175 | word_type = builtin_type (gdbarch)->builtin_int8; |
fb40c209 AC |
1176 | word_asize = 'b'; |
1177 | } | |
41296c92 | 1178 | /* The number of rows. */ |
fb40c209 AC |
1179 | nr_rows = atol (argv[3]); |
1180 | if (nr_rows <= 0) | |
a13e061a PA |
1181 | error ("mi_cmd_data_read_memory: invalid number of rows."); |
1182 | ||
41296c92 | 1183 | /* Number of bytes per row. */ |
fb40c209 AC |
1184 | nr_cols = atol (argv[4]); |
1185 | if (nr_cols <= 0) | |
a13e061a PA |
1186 | error ("mi_cmd_data_read_memory: invalid number of columns."); |
1187 | ||
41296c92 | 1188 | /* The un-printable character when printing ascii. */ |
fb40c209 AC |
1189 | if (argc == 6) |
1190 | aschar = *argv[5]; | |
1191 | else | |
1192 | aschar = 0; | |
1193 | ||
41296c92 | 1194 | /* Create a buffer and read it in. */ |
fb40c209 | 1195 | total_bytes = word_size * nr_rows * nr_cols; |
2e94c453 | 1196 | mbuf = xcalloc (total_bytes, 1); |
b8c9b27d | 1197 | make_cleanup (xfree, mbuf); |
cf7a04e8 | 1198 | |
a4261689 PA |
1199 | /* Dispatch memory reads to the topmost target, not the flattened |
1200 | current_target. */ | |
1201 | nr_bytes = target_read_until_error (current_target.beneath, | |
1202 | TARGET_OBJECT_MEMORY, NULL, mbuf, | |
1203 | addr, total_bytes); | |
cf7a04e8 | 1204 | if (nr_bytes <= 0) |
a13e061a | 1205 | error ("Unable to read memory."); |
fb40c209 | 1206 | |
41296c92 | 1207 | /* Output the header information. */ |
5af949e3 | 1208 | ui_out_field_core_addr (uiout, "addr", gdbarch, addr); |
fb40c209 AC |
1209 | ui_out_field_int (uiout, "nr-bytes", nr_bytes); |
1210 | ui_out_field_int (uiout, "total-bytes", total_bytes); | |
5af949e3 UW |
1211 | ui_out_field_core_addr (uiout, "next-row", |
1212 | gdbarch, addr + word_size * nr_cols); | |
1213 | ui_out_field_core_addr (uiout, "prev-row", | |
1214 | gdbarch, addr - word_size * nr_cols); | |
1215 | ui_out_field_core_addr (uiout, "next-page", gdbarch, addr + total_bytes); | |
1216 | ui_out_field_core_addr (uiout, "prev-page", gdbarch, addr - total_bytes); | |
fb40c209 | 1217 | |
41296c92 | 1218 | /* Build the result as a two dimentional table. */ |
fb40c209 AC |
1219 | { |
1220 | struct ui_stream *stream = ui_out_stream_new (uiout); | |
6ad4a2cf | 1221 | struct cleanup *cleanup_list_memory; |
fb40c209 AC |
1222 | int row; |
1223 | int row_byte; | |
6ad4a2cf | 1224 | cleanup_list_memory = make_cleanup_ui_out_list_begin_end (uiout, "memory"); |
fb40c209 AC |
1225 | for (row = 0, row_byte = 0; |
1226 | row < nr_rows; | |
1227 | row++, row_byte += nr_cols * word_size) | |
1228 | { | |
1229 | int col; | |
1230 | int col_byte; | |
6ad4a2cf JJ |
1231 | struct cleanup *cleanup_tuple; |
1232 | struct cleanup *cleanup_list_data; | |
79a45b7d TT |
1233 | struct value_print_options opts; |
1234 | ||
6ad4a2cf | 1235 | cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); |
5af949e3 | 1236 | ui_out_field_core_addr (uiout, "addr", gdbarch, addr + row_byte); |
fb40c209 | 1237 | /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */ |
6ad4a2cf | 1238 | cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data"); |
79a45b7d | 1239 | get_formatted_print_options (&opts, word_format); |
fb40c209 AC |
1240 | for (col = 0, col_byte = row_byte; |
1241 | col < nr_cols; | |
1242 | col++, col_byte += word_size) | |
1243 | { | |
1244 | if (col_byte + word_size > nr_bytes) | |
1245 | { | |
1246 | ui_out_field_string (uiout, NULL, "N/A"); | |
1247 | } | |
1248 | else | |
1249 | { | |
1250 | ui_file_rewind (stream->stream); | |
79a45b7d | 1251 | print_scalar_formatted (mbuf + col_byte, word_type, &opts, |
fb40c209 AC |
1252 | word_asize, stream->stream); |
1253 | ui_out_field_stream (uiout, NULL, stream); | |
1254 | } | |
1255 | } | |
6ad4a2cf | 1256 | do_cleanups (cleanup_list_data); |
fb40c209 AC |
1257 | if (aschar) |
1258 | { | |
1259 | int byte; | |
1260 | ui_file_rewind (stream->stream); | |
1261 | for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++) | |
1262 | { | |
1263 | if (byte >= nr_bytes) | |
1264 | { | |
1265 | fputc_unfiltered ('X', stream->stream); | |
1266 | } | |
1267 | else if (mbuf[byte] < 32 || mbuf[byte] > 126) | |
1268 | { | |
1269 | fputc_unfiltered (aschar, stream->stream); | |
1270 | } | |
1271 | else | |
1272 | fputc_unfiltered (mbuf[byte], stream->stream); | |
1273 | } | |
1274 | ui_out_field_stream (uiout, "ascii", stream); | |
1275 | } | |
6ad4a2cf | 1276 | do_cleanups (cleanup_tuple); |
fb40c209 AC |
1277 | } |
1278 | ui_out_stream_delete (stream); | |
6ad4a2cf | 1279 | do_cleanups (cleanup_list_memory); |
fb40c209 AC |
1280 | } |
1281 | do_cleanups (cleanups); | |
fb40c209 AC |
1282 | } |
1283 | ||
1284 | /* DATA-MEMORY-WRITE: | |
1285 | ||
1286 | COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The | |
1287 | offset from the beginning of the memory grid row where the cell to | |
1288 | be written is. | |
1289 | ADDR: start address of the row in the memory grid where the memory | |
41296c92 | 1290 | cell is, if OFFSET_COLUMN is specified. Otherwise, the address of |
fb40c209 | 1291 | the location to write to. |
41296c92 | 1292 | FORMAT: a char indicating format for the ``word''. See |
fb40c209 AC |
1293 | the ``x'' command. |
1294 | WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes | |
1295 | VALUE: value to be written into the memory address. | |
1296 | ||
1297 | Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE). | |
1298 | ||
41296c92 | 1299 | Prints nothing. */ |
ce8f13f8 | 1300 | void |
fb40c209 AC |
1301 | mi_cmd_data_write_memory (char *command, char **argv, int argc) |
1302 | { | |
e17a4113 UW |
1303 | struct gdbarch *gdbarch = get_current_arch (); |
1304 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
fb40c209 AC |
1305 | CORE_ADDR addr; |
1306 | char word_format; | |
1307 | long word_size; | |
1308 | /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big | |
41296c92 | 1309 | enough when using a compiler other than GCC. */ |
fb40c209 | 1310 | LONGEST value; |
d8bf3afa KB |
1311 | void *buffer; |
1312 | struct cleanup *old_chain; | |
fb40c209 AC |
1313 | long offset = 0; |
1314 | int optind = 0; | |
1315 | char *optarg; | |
1316 | enum opt | |
1317 | { | |
1318 | OFFSET_OPT | |
1319 | }; | |
1320 | static struct mi_opt opts[] = | |
1321 | { | |
1322 | {"o", OFFSET_OPT, 1}, | |
d5d6fca5 | 1323 | { 0, 0, 0 } |
fb40c209 AC |
1324 | }; |
1325 | ||
1326 | while (1) | |
1327 | { | |
1328 | int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts, | |
1329 | &optind, &optarg); | |
1330 | if (opt < 0) | |
1331 | break; | |
1332 | switch ((enum opt) opt) | |
1333 | { | |
1334 | case OFFSET_OPT: | |
1335 | offset = atol (optarg); | |
1336 | break; | |
1337 | } | |
1338 | } | |
1339 | argv += optind; | |
1340 | argc -= optind; | |
1341 | ||
1342 | if (argc != 4) | |
a13e061a | 1343 | error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE."); |
fb40c209 | 1344 | |
41296c92 NR |
1345 | /* Extract all the arguments. */ |
1346 | /* Start address of the memory dump. */ | |
fb40c209 | 1347 | addr = parse_and_eval_address (argv[0]); |
41296c92 NR |
1348 | /* The format character to use when displaying a memory word. See |
1349 | the ``x'' command. */ | |
fb40c209 AC |
1350 | word_format = argv[1][0]; |
1351 | /* The size of the memory word. */ | |
1352 | word_size = atol (argv[2]); | |
1353 | ||
41296c92 | 1354 | /* Calculate the real address of the write destination. */ |
fb40c209 AC |
1355 | addr += (offset * word_size); |
1356 | ||
41296c92 | 1357 | /* Get the value as a number. */ |
fb40c209 | 1358 | value = parse_and_eval_address (argv[3]); |
41296c92 | 1359 | /* Get the value into an array. */ |
d8bf3afa KB |
1360 | buffer = xmalloc (word_size); |
1361 | old_chain = make_cleanup (xfree, buffer); | |
e17a4113 | 1362 | store_signed_integer (buffer, word_size, byte_order, value); |
41296c92 | 1363 | /* Write it down to memory. */ |
fb40c209 | 1364 | write_memory (addr, buffer, word_size); |
d8bf3afa KB |
1365 | /* Free the buffer. */ |
1366 | do_cleanups (old_chain); | |
fb40c209 AC |
1367 | } |
1368 | ||
ce8f13f8 | 1369 | void |
d8c83789 NR |
1370 | mi_cmd_enable_timings (char *command, char **argv, int argc) |
1371 | { | |
1372 | if (argc == 0) | |
1373 | do_timings = 1; | |
1374 | else if (argc == 1) | |
1375 | { | |
1376 | if (strcmp (argv[0], "yes") == 0) | |
1377 | do_timings = 1; | |
1378 | else if (strcmp (argv[0], "no") == 0) | |
1379 | do_timings = 0; | |
1380 | else | |
1381 | goto usage_error; | |
1382 | } | |
1383 | else | |
1384 | goto usage_error; | |
1385 | ||
ce8f13f8 | 1386 | return; |
d8c83789 NR |
1387 | |
1388 | usage_error: | |
1389 | error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command); | |
d8c83789 NR |
1390 | } |
1391 | ||
ce8f13f8 | 1392 | void |
084344da VP |
1393 | mi_cmd_list_features (char *command, char **argv, int argc) |
1394 | { | |
1395 | if (argc == 0) | |
1396 | { | |
1397 | struct cleanup *cleanup = NULL; | |
1398 | cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features"); | |
1399 | ||
1400 | ui_out_field_string (uiout, NULL, "frozen-varobjs"); | |
8b4ed427 | 1401 | ui_out_field_string (uiout, NULL, "pending-breakpoints"); |
8e8901c5 | 1402 | ui_out_field_string (uiout, NULL, "thread-info"); |
084344da | 1403 | |
b6313243 TT |
1404 | #if HAVE_PYTHON |
1405 | ui_out_field_string (uiout, NULL, "python"); | |
1406 | #endif | |
1407 | ||
084344da | 1408 | do_cleanups (cleanup); |
ce8f13f8 | 1409 | return; |
084344da VP |
1410 | } |
1411 | ||
1412 | error ("-list-features should be passed no arguments"); | |
084344da | 1413 | } |
c6ebd6cf VP |
1414 | |
1415 | void | |
1416 | mi_cmd_list_target_features (char *command, char **argv, int argc) | |
1417 | { | |
1418 | if (argc == 0) | |
1419 | { | |
1420 | struct cleanup *cleanup = NULL; | |
1421 | cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features"); | |
1422 | ||
1423 | if (target_can_async_p ()) | |
1424 | ui_out_field_string (uiout, NULL, "async"); | |
1425 | ||
1426 | do_cleanups (cleanup); | |
1427 | return; | |
1428 | } | |
1429 | ||
1430 | error ("-list-target-features should be passed no arguments"); | |
1431 | } | |
1432 | ||
8d34ea23 KS |
1433 | /* Execute a command within a safe environment. |
1434 | Return <0 for error; >=0 for ok. | |
1435 | ||
1436 | args->action will tell mi_execute_command what action | |
42972f50 | 1437 | to perfrom after the given command has executed (display/suppress |
8d34ea23 | 1438 | prompt, display error). */ |
fb40c209 | 1439 | |
f30f06b8 | 1440 | static void |
8d34ea23 | 1441 | captured_mi_execute_command (struct ui_out *uiout, void *data) |
fb40c209 | 1442 | { |
1f31650a | 1443 | struct cleanup *cleanup; |
e111d6c9 | 1444 | struct mi_parse *context = (struct mi_parse *) data; |
fb40c209 | 1445 | |
4333ada3 VP |
1446 | if (do_timings) |
1447 | current_command_ts = context->cmd_start; | |
d8c83789 | 1448 | |
1f31650a VP |
1449 | current_token = xstrdup (context->token); |
1450 | cleanup = make_cleanup (free_current_contents, ¤t_token); | |
1451 | ||
a2840c35 | 1452 | running_result_record_printed = 0; |
f3b1572e | 1453 | mi_proceeded = 0; |
fb40c209 AC |
1454 | switch (context->op) |
1455 | { | |
fb40c209 | 1456 | case MI_COMMAND: |
41296c92 | 1457 | /* A MI command was read from the input stream. */ |
fb40c209 AC |
1458 | if (mi_debug_p) |
1459 | /* FIXME: gdb_???? */ | |
1460 | fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n", | |
1461 | context->token, context->command, context->args); | |
d8c83789 | 1462 | |
d8c83789 | 1463 | |
ce8f13f8 | 1464 | mi_cmd_execute (context); |
8d34ea23 | 1465 | |
a2840c35 | 1466 | /* Print the result if there were no errors. |
4389a95a | 1467 | |
a2840c35 VP |
1468 | Remember that on the way out of executing a command, you have |
1469 | to directly use the mi_interp's uiout, since the command could | |
1470 | have reset the interpreter, in which case the current uiout | |
1471 | will most likely crash in the mi_out_* routines. */ | |
ce8f13f8 | 1472 | if (!running_result_record_printed) |
a2840c35 VP |
1473 | { |
1474 | fputs_unfiltered (context->token, raw_stdout); | |
ce8f13f8 VP |
1475 | /* There's no particularly good reason why target-connect results |
1476 | in not ^done. Should kill ^connected for MI3. */ | |
1477 | fputs_unfiltered (strcmp (context->command, "target-select") == 0 | |
1478 | ? "^connected" : "^done", raw_stdout); | |
a2840c35 VP |
1479 | mi_out_put (uiout, raw_stdout); |
1480 | mi_out_rewind (uiout); | |
4333ada3 | 1481 | mi_print_timing_maybe (); |
a2840c35 VP |
1482 | fputs_unfiltered ("\n", raw_stdout); |
1483 | } | |
1484 | else | |
f7f9a841 VP |
1485 | /* The command does not want anything to be printed. In that |
1486 | case, the command probably should not have written anything | |
1487 | to uiout, but in case it has written something, discard it. */ | |
a2840c35 | 1488 | mi_out_rewind (uiout); |
fb40c209 AC |
1489 | break; |
1490 | ||
1491 | case CLI_COMMAND: | |
78f5381d AC |
1492 | { |
1493 | char *argv[2]; | |
1494 | /* A CLI command was read from the input stream. */ | |
1495 | /* This "feature" will be removed as soon as we have a | |
1496 | complete set of mi commands. */ | |
1497 | /* Echo the command on the console. */ | |
1498 | fprintf_unfiltered (gdb_stdlog, "%s\n", context->command); | |
1499 | /* Call the "console" interpreter. */ | |
1500 | argv[0] = "console"; | |
1501 | argv[1] = context->command; | |
ce8f13f8 | 1502 | mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2); |
78f5381d | 1503 | |
eec01795 | 1504 | /* If we changed interpreters, DON'T print out anything. */ |
78f5381d AC |
1505 | if (current_interp_named_p (INTERP_MI) |
1506 | || current_interp_named_p (INTERP_MI1) | |
1507 | || current_interp_named_p (INTERP_MI2) | |
1508 | || current_interp_named_p (INTERP_MI3)) | |
1509 | { | |
ce8f13f8 | 1510 | if (!running_result_record_printed) |
eec01795 DJ |
1511 | { |
1512 | fputs_unfiltered (context->token, raw_stdout); | |
1513 | fputs_unfiltered ("^done", raw_stdout); | |
1514 | mi_out_put (uiout, raw_stdout); | |
1515 | mi_out_rewind (uiout); | |
4333ada3 VP |
1516 | mi_print_timing_maybe (); |
1517 | fputs_unfiltered ("\n", raw_stdout); | |
eec01795 | 1518 | } |
eec01795 DJ |
1519 | else |
1520 | mi_out_rewind (uiout); | |
78f5381d AC |
1521 | } |
1522 | break; | |
1523 | } | |
fb40c209 AC |
1524 | |
1525 | } | |
8d34ea23 | 1526 | |
1f31650a VP |
1527 | do_cleanups (cleanup); |
1528 | ||
f30f06b8 | 1529 | return; |
fb40c209 AC |
1530 | } |
1531 | ||
1532 | ||
1533 | void | |
1534 | mi_execute_command (char *cmd, int from_tty) | |
1535 | { | |
1536 | struct mi_parse *command; | |
8d34ea23 | 1537 | struct ui_out *saved_uiout = uiout; |
fb40c209 | 1538 | |
41296c92 NR |
1539 | /* This is to handle EOF (^D). We just quit gdb. */ |
1540 | /* FIXME: we should call some API function here. */ | |
fb40c209 AC |
1541 | if (cmd == 0) |
1542 | quit_force (NULL, from_tty); | |
1543 | ||
11334b82 VP |
1544 | target_log_command (cmd); |
1545 | ||
fb40c209 AC |
1546 | command = mi_parse (cmd); |
1547 | ||
1548 | if (command != NULL) | |
1549 | { | |
71fff37b | 1550 | struct gdb_exception result; |
66bb093b | 1551 | ptid_t previous_ptid = inferior_ptid; |
d8c83789 NR |
1552 | |
1553 | if (do_timings) | |
1554 | { | |
1555 | command->cmd_start = (struct mi_timestamp *) | |
1556 | xmalloc (sizeof (struct mi_timestamp)); | |
1557 | timestamp (command->cmd_start); | |
1558 | } | |
1559 | ||
e111d6c9 | 1560 | result = catch_exception (uiout, captured_mi_execute_command, command, |
f30f06b8 | 1561 | RETURN_MASK_ALL); |
ce43223b | 1562 | if (result.reason < 0) |
fb40c209 | 1563 | { |
fb40c209 | 1564 | /* The command execution failed and error() was called |
589e074d | 1565 | somewhere. */ |
fb40c209 AC |
1566 | fputs_unfiltered (command->token, raw_stdout); |
1567 | fputs_unfiltered ("^error,msg=\"", raw_stdout); | |
63f06803 DJ |
1568 | if (result.message == NULL) |
1569 | fputs_unfiltered ("unknown error", raw_stdout); | |
1570 | else | |
a13e061a | 1571 | fputstr_unfiltered (result.message, '"', raw_stdout); |
fb40c209 | 1572 | fputs_unfiltered ("\"\n", raw_stdout); |
589e074d | 1573 | mi_out_rewind (uiout); |
fb40c209 | 1574 | } |
a13e061a | 1575 | |
5d4e2b76 VP |
1576 | bpstat_do_actions (); |
1577 | ||
66bb093b VP |
1578 | if (/* The notifications are only output when the top-level |
1579 | interpreter (specified on the command line) is MI. */ | |
1580 | ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ())) | |
1581 | /* Don't try report anything if there are no threads -- | |
1582 | the program is dead. */ | |
1583 | && thread_count () != 0 | |
1584 | /* -thread-select explicitly changes thread. If frontend uses that | |
1585 | internally, we don't want to emit =thread-selected, since | |
1586 | =thread-selected is supposed to indicate user's intentions. */ | |
1587 | && strcmp (command->command, "thread-select") != 0) | |
1588 | { | |
1589 | struct mi_interp *mi = top_level_interpreter_data (); | |
d729566a | 1590 | int report_change = 0; |
66bb093b VP |
1591 | |
1592 | if (command->thread == -1) | |
1593 | { | |
d729566a PA |
1594 | report_change = (!ptid_equal (previous_ptid, null_ptid) |
1595 | && !ptid_equal (inferior_ptid, previous_ptid) | |
1596 | && !ptid_equal (inferior_ptid, null_ptid)); | |
66bb093b | 1597 | } |
d729566a | 1598 | else if (!ptid_equal (inferior_ptid, null_ptid)) |
66bb093b | 1599 | { |
d729566a | 1600 | struct thread_info *ti = inferior_thread (); |
66bb093b VP |
1601 | report_change = (ti->num != command->thread); |
1602 | } | |
1603 | ||
1604 | if (report_change) | |
1605 | { | |
d729566a | 1606 | struct thread_info *ti = inferior_thread (); |
66bb093b VP |
1607 | target_terminal_ours (); |
1608 | fprintf_unfiltered (mi->event_channel, | |
1609 | "thread-selected,id=\"%d\"", | |
1610 | ti->num); | |
1611 | gdb_flush (mi->event_channel); | |
1612 | } | |
1613 | } | |
1614 | ||
fb40c209 AC |
1615 | mi_parse_free (command); |
1616 | } | |
1617 | ||
fb40c209 | 1618 | fputs_unfiltered ("(gdb) \n", raw_stdout); |
a433f9e4 | 1619 | gdb_flush (raw_stdout); |
41296c92 | 1620 | /* Print any buffered hook code. */ |
fb40c209 AC |
1621 | /* ..... */ |
1622 | } | |
1623 | ||
ce8f13f8 | 1624 | static void |
fb40c209 AC |
1625 | mi_cmd_execute (struct mi_parse *parse) |
1626 | { | |
f107f563 | 1627 | struct cleanup *cleanup; |
1e92afda | 1628 | int i; |
e23110bb | 1629 | |
4e5d721f DE |
1630 | prepare_execute_command (); |
1631 | ||
1f31650a | 1632 | cleanup = make_cleanup (null_cleanup, NULL); |
1b98914a | 1633 | |
1e92afda VP |
1634 | if (parse->frame != -1 && parse->thread == -1) |
1635 | error (_("Cannot specify --frame without --thread")); | |
dcf4fbde | 1636 | |
1e92afda VP |
1637 | if (parse->thread != -1) |
1638 | { | |
1639 | struct thread_info *tp = find_thread_id (parse->thread); | |
1640 | if (!tp) | |
1641 | error (_("Invalid thread id: %d"), parse->thread); | |
dcf4fbde PA |
1642 | |
1643 | if (is_exited (tp->ptid)) | |
1644 | error (_("Thread id: %d has terminated"), parse->thread); | |
1645 | ||
1646 | switch_to_thread (tp->ptid); | |
1e92afda | 1647 | } |
dcf4fbde | 1648 | |
1e92afda VP |
1649 | if (parse->frame != -1) |
1650 | { | |
1651 | struct frame_info *fid; | |
1652 | int frame = parse->frame; | |
1653 | fid = find_relative_frame (get_current_frame (), &frame); | |
1654 | if (frame == 0) | |
1655 | /* find_relative_frame was successful */ | |
1656 | select_frame (fid); | |
1657 | else | |
ea069267 | 1658 | error (_("Invalid frame id: %d"), frame); |
1e92afda | 1659 | } |
dcf4fbde | 1660 | |
9e22b03a | 1661 | if (parse->cmd->argv_func != NULL) |
d729566a | 1662 | parse->cmd->argv_func (parse->command, parse->argv, parse->argc); |
b2af646b | 1663 | else if (parse->cmd->cli.cmd != 0) |
fb40c209 AC |
1664 | { |
1665 | /* FIXME: DELETE THIS. */ | |
41296c92 NR |
1666 | /* The operation is still implemented by a cli command. */ |
1667 | /* Must be a synchronous one. */ | |
b2af646b AC |
1668 | mi_execute_cli_command (parse->cmd->cli.cmd, parse->cmd->cli.args_p, |
1669 | parse->args); | |
fb40c209 AC |
1670 | } |
1671 | else | |
1672 | { | |
41296c92 | 1673 | /* FIXME: DELETE THIS. */ |
a13e061a PA |
1674 | struct ui_file *stb; |
1675 | ||
1676 | stb = mem_fileopen (); | |
1677 | ||
1678 | fputs_unfiltered ("Undefined mi command: ", stb); | |
1679 | fputstr_unfiltered (parse->command, '"', stb); | |
1680 | fputs_unfiltered (" (missing implementation)", stb); | |
1681 | ||
1682 | make_cleanup_ui_file_delete (stb); | |
1683 | error_stream (stb); | |
fb40c209 | 1684 | } |
1b98914a | 1685 | do_cleanups (cleanup); |
fb40c209 AC |
1686 | } |
1687 | ||
fb40c209 | 1688 | /* FIXME: This is just a hack so we can get some extra commands going. |
41296c92 NR |
1689 | We don't want to channel things through the CLI, but call libgdb directly. |
1690 | Use only for synchronous commands. */ | |
fb40c209 AC |
1691 | |
1692 | void | |
b2af646b | 1693 | mi_execute_cli_command (const char *cmd, int args_p, const char *args) |
fb40c209 | 1694 | { |
b2af646b | 1695 | if (cmd != 0) |
fb40c209 AC |
1696 | { |
1697 | struct cleanup *old_cleanups; | |
1698 | char *run; | |
b2af646b | 1699 | if (args_p) |
c6902d46 | 1700 | run = xstrprintf ("%s %s", cmd, args); |
b2af646b AC |
1701 | else |
1702 | run = xstrdup (cmd); | |
fb40c209 AC |
1703 | if (mi_debug_p) |
1704 | /* FIXME: gdb_???? */ | |
1705 | fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n", | |
b2af646b | 1706 | cmd, run); |
b8c9b27d | 1707 | old_cleanups = make_cleanup (xfree, run); |
fb40c209 AC |
1708 | execute_command ( /*ui */ run, 0 /*from_tty */ ); |
1709 | do_cleanups (old_cleanups); | |
1710 | return; | |
1711 | } | |
1712 | } | |
1713 | ||
ce8f13f8 | 1714 | void |
9e22b03a | 1715 | mi_execute_async_cli_command (char *cli_command, char **argv, int argc) |
fb40c209 AC |
1716 | { |
1717 | struct cleanup *old_cleanups; | |
1718 | char *run; | |
fb40c209 AC |
1719 | |
1720 | if (target_can_async_p ()) | |
9e22b03a | 1721 | run = xstrprintf ("%s %s&", cli_command, argc ? *argv : ""); |
fb40c209 | 1722 | else |
9e22b03a | 1723 | run = xstrprintf ("%s %s", cli_command, argc ? *argv : ""); |
f107f563 | 1724 | old_cleanups = make_cleanup (xfree, run); |
fb40c209 | 1725 | |
fb40c209 AC |
1726 | execute_command ( /*ui */ run, 0 /*from_tty */ ); |
1727 | ||
f107f563 VP |
1728 | if (target_can_async_p ()) |
1729 | { | |
1730 | /* If we're not executing, an exception should have been throw. */ | |
8ea051c5 | 1731 | gdb_assert (is_running (inferior_ptid)); |
f107f563 VP |
1732 | do_cleanups (old_cleanups); |
1733 | } | |
1734 | else | |
fb40c209 AC |
1735 | { |
1736 | /* Do this before doing any printing. It would appear that some | |
41296c92 | 1737 | print code leaves garbage around in the buffer. */ |
fb40c209 | 1738 | do_cleanups (old_cleanups); |
ce8f13f8 | 1739 | } |
fb40c209 AC |
1740 | } |
1741 | ||
1742 | void | |
fb40c209 AC |
1743 | mi_load_progress (const char *section_name, |
1744 | unsigned long sent_so_far, | |
1745 | unsigned long total_section, | |
1746 | unsigned long total_sent, | |
1747 | unsigned long grand_total) | |
1748 | { | |
1749 | struct timeval time_now, delta, update_threshold; | |
1750 | static struct timeval last_update; | |
1751 | static char *previous_sect_name = NULL; | |
1752 | int new_section; | |
0be75e02 | 1753 | struct ui_out *saved_uiout; |
fb40c209 | 1754 | |
0be75e02 AS |
1755 | /* This function is called through deprecated_show_load_progress |
1756 | which means uiout may not be correct. Fix it for the duration | |
1757 | of this function. */ | |
1758 | saved_uiout = uiout; | |
1759 | ||
edff0c0a DJ |
1760 | if (current_interp_named_p (INTERP_MI) |
1761 | || current_interp_named_p (INTERP_MI2)) | |
0be75e02 AS |
1762 | uiout = mi_out_new (2); |
1763 | else if (current_interp_named_p (INTERP_MI1)) | |
1764 | uiout = mi_out_new (1); | |
edff0c0a DJ |
1765 | else if (current_interp_named_p (INTERP_MI3)) |
1766 | uiout = mi_out_new (3); | |
0be75e02 | 1767 | else |
fb40c209 AC |
1768 | return; |
1769 | ||
1770 | update_threshold.tv_sec = 0; | |
1771 | update_threshold.tv_usec = 500000; | |
1772 | gettimeofday (&time_now, NULL); | |
1773 | ||
1774 | delta.tv_usec = time_now.tv_usec - last_update.tv_usec; | |
1775 | delta.tv_sec = time_now.tv_sec - last_update.tv_sec; | |
1776 | ||
1777 | if (delta.tv_usec < 0) | |
1778 | { | |
1779 | delta.tv_sec -= 1; | |
f2395593 | 1780 | delta.tv_usec += 1000000L; |
fb40c209 AC |
1781 | } |
1782 | ||
1783 | new_section = (previous_sect_name ? | |
1784 | strcmp (previous_sect_name, section_name) : 1); | |
1785 | if (new_section) | |
1786 | { | |
6ad4a2cf | 1787 | struct cleanup *cleanup_tuple; |
b8c9b27d | 1788 | xfree (previous_sect_name); |
fb40c209 AC |
1789 | previous_sect_name = xstrdup (section_name); |
1790 | ||
721c02de VP |
1791 | if (current_token) |
1792 | fputs_unfiltered (current_token, raw_stdout); | |
fb40c209 | 1793 | fputs_unfiltered ("+download", raw_stdout); |
6ad4a2cf | 1794 | cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); |
fb40c209 AC |
1795 | ui_out_field_string (uiout, "section", section_name); |
1796 | ui_out_field_int (uiout, "section-size", total_section); | |
1797 | ui_out_field_int (uiout, "total-size", grand_total); | |
6ad4a2cf | 1798 | do_cleanups (cleanup_tuple); |
fb40c209 AC |
1799 | mi_out_put (uiout, raw_stdout); |
1800 | fputs_unfiltered ("\n", raw_stdout); | |
1801 | gdb_flush (raw_stdout); | |
1802 | } | |
1803 | ||
1804 | if (delta.tv_sec >= update_threshold.tv_sec && | |
1805 | delta.tv_usec >= update_threshold.tv_usec) | |
1806 | { | |
6ad4a2cf | 1807 | struct cleanup *cleanup_tuple; |
fb40c209 AC |
1808 | last_update.tv_sec = time_now.tv_sec; |
1809 | last_update.tv_usec = time_now.tv_usec; | |
721c02de VP |
1810 | if (current_token) |
1811 | fputs_unfiltered (current_token, raw_stdout); | |
fb40c209 | 1812 | fputs_unfiltered ("+download", raw_stdout); |
6ad4a2cf | 1813 | cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); |
fb40c209 AC |
1814 | ui_out_field_string (uiout, "section", section_name); |
1815 | ui_out_field_int (uiout, "section-sent", sent_so_far); | |
1816 | ui_out_field_int (uiout, "section-size", total_section); | |
1817 | ui_out_field_int (uiout, "total-sent", total_sent); | |
1818 | ui_out_field_int (uiout, "total-size", grand_total); | |
6ad4a2cf | 1819 | do_cleanups (cleanup_tuple); |
fb40c209 AC |
1820 | mi_out_put (uiout, raw_stdout); |
1821 | fputs_unfiltered ("\n", raw_stdout); | |
1822 | gdb_flush (raw_stdout); | |
1823 | } | |
0be75e02 AS |
1824 | |
1825 | xfree (uiout); | |
1826 | uiout = saved_uiout; | |
fb40c209 AC |
1827 | } |
1828 | ||
d8c83789 NR |
1829 | static void |
1830 | timestamp (struct mi_timestamp *tv) | |
1831 | { | |
1832 | long usec; | |
1833 | gettimeofday (&tv->wallclock, NULL); | |
1834 | #ifdef HAVE_GETRUSAGE | |
1835 | getrusage (RUSAGE_SELF, &rusage); | |
1836 | tv->utime.tv_sec = rusage.ru_utime.tv_sec; | |
1837 | tv->utime.tv_usec = rusage.ru_utime.tv_usec; | |
1838 | tv->stime.tv_sec = rusage.ru_stime.tv_sec; | |
1839 | tv->stime.tv_usec = rusage.ru_stime.tv_usec; | |
1840 | #else | |
1841 | usec = get_run_time (); | |
f2395593 NR |
1842 | tv->utime.tv_sec = usec/1000000L; |
1843 | tv->utime.tv_usec = usec - 1000000L*tv->utime.tv_sec; | |
d8c83789 NR |
1844 | tv->stime.tv_sec = 0; |
1845 | tv->stime.tv_usec = 0; | |
1846 | #endif | |
1847 | } | |
1848 | ||
1849 | static void | |
1850 | print_diff_now (struct mi_timestamp *start) | |
1851 | { | |
1852 | struct mi_timestamp now; | |
1853 | timestamp (&now); | |
1854 | print_diff (start, &now); | |
1855 | } | |
1856 | ||
4333ada3 VP |
1857 | void |
1858 | mi_print_timing_maybe (void) | |
1859 | { | |
1860 | /* If the command is -enable-timing then do_timings may be | |
1861 | true whilst current_command_ts is not initialized. */ | |
1862 | if (do_timings && current_command_ts) | |
1863 | print_diff_now (current_command_ts); | |
1864 | } | |
1865 | ||
d8c83789 NR |
1866 | static long |
1867 | timeval_diff (struct timeval start, struct timeval end) | |
1868 | { | |
f2395593 | 1869 | return ((end.tv_sec - start.tv_sec) * 1000000L) |
d8c83789 NR |
1870 | + (end.tv_usec - start.tv_usec); |
1871 | } | |
1872 | ||
1873 | static void | |
1874 | print_diff (struct mi_timestamp *start, struct mi_timestamp *end) | |
1875 | { | |
1876 | fprintf_unfiltered | |
1877 | (raw_stdout, | |
1878 | ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}", | |
1879 | timeval_diff (start->wallclock, end->wallclock) / 1000000.0, | |
1880 | timeval_diff (start->utime, end->utime) / 1000000.0, | |
1881 | timeval_diff (start->stime, end->stime) / 1000000.0); | |
1882 | } |