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