]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Memory-access and commands for "inferior" process, for GDB. |
990a07ab | 2 | |
b811d2c2 | 3 | Copyright (C) 1986-2020 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
19 | |
20 | #include "defs.h" | |
5af949e3 | 21 | #include "arch-utils.h" |
c906108c SS |
22 | #include "symtab.h" |
23 | #include "gdbtypes.h" | |
24 | #include "frame.h" | |
25 | #include "inferior.h" | |
45741a9c | 26 | #include "infrun.h" |
268a13a5 | 27 | #include "gdbsupport/environ.h" |
c906108c SS |
28 | #include "value.h" |
29 | #include "gdbcmd.h" | |
1adeb98a | 30 | #include "symfile.h" |
c906108c SS |
31 | #include "gdbcore.h" |
32 | #include "target.h" | |
33 | #include "language.h" | |
c906108c | 34 | #include "objfiles.h" |
c94fdfd0 | 35 | #include "completer.h" |
8b93c638 | 36 | #include "ui-out.h" |
36160dc4 | 37 | #include "regcache.h" |
f9418c0f | 38 | #include "reggroups.h" |
fe898f56 | 39 | #include "block.h" |
a77053c2 | 40 | #include "solib.h" |
f9418c0f | 41 | #include <ctype.h> |
76727919 | 42 | #include "observable.h" |
424163ea | 43 | #include "target-descriptions.h" |
ad842144 | 44 | #include "user-regs.h" |
a0ef4274 | 45 | #include "gdbthread.h" |
79a45b7d | 46 | #include "valprint.h" |
edb3359d | 47 | #include "inline-frame.h" |
573cda03 | 48 | #include "tracepoint.h" |
09cee04b | 49 | #include "inf-loop.h" |
be34f849 | 50 | #include "continuations.h" |
f8eba3c6 | 51 | #include "linespec.h" |
243a9253 | 52 | #include "thread-fsm.h" |
3b12939d | 53 | #include "top.h" |
8980e177 | 54 | #include "interps.h" |
4a4c04f1 | 55 | #include "skip.h" |
268a13a5 | 56 | #include "gdbsupport/gdb_optional.h" |
b46a8d7c | 57 | #include "source.h" |
7f6aba03 | 58 | #include "cli/cli-style.h" |
d5551862 | 59 | |
a58dd373 EZ |
60 | /* Local functions: */ |
61 | ||
a14ed312 | 62 | static void until_next_command (int); |
c906108c | 63 | |
0b39b52e | 64 | static void step_1 (int, int, const char *); |
c906108c | 65 | |
c906108c | 66 | #define ERROR_NO_INFERIOR \ |
55f6301a | 67 | if (!target_has_execution ()) error (_("The program is not being run.")); |
c906108c | 68 | |
3e43a32a MS |
69 | /* Scratch area where string containing arguments to give to the |
70 | program will be stored by 'set args'. As soon as anything is | |
71 | stored, notice_args_set will move it into per-inferior storage. | |
1777feb0 | 72 | Arguments are separated by spaces. Empty string (pointer to '\0') |
3e43a32a | 73 | means no args. */ |
c906108c | 74 | |
3f81c18a | 75 | static char *inferior_args_scratch; |
c906108c | 76 | |
d092c5a2 SDJ |
77 | /* Scratch area where the new cwd will be stored by 'set cwd'. */ |
78 | ||
79 | static char *inferior_cwd_scratch; | |
80 | ||
3f81c18a VP |
81 | /* Scratch area where 'set inferior-tty' will store user-provided value. |
82 | We'll immediate copy it into per-inferior storage. */ | |
552c04a7 | 83 | |
3f81c18a | 84 | static char *inferior_io_terminal_scratch; |
c906108c SS |
85 | |
86 | /* Pid of our debugged inferior, or 0 if no inferior now. | |
87 | Since various parts of infrun.c test this to see whether there is a program | |
88 | being debugged it should be nonzero (currently 3 is used) for remote | |
89 | debugging. */ | |
90 | ||
39f77062 | 91 | ptid_t inferior_ptid; |
c906108c | 92 | |
c906108c SS |
93 | /* Nonzero if stopped due to completion of a stack dummy routine. */ |
94 | ||
aa7d318d | 95 | enum stop_stack_kind stop_stack_dummy; |
c906108c SS |
96 | |
97 | /* Nonzero if stopped due to a random (unexpected) signal in inferior | |
98 | process. */ | |
99 | ||
100 | int stopped_by_random_signal; | |
101 | ||
c906108c | 102 | \f |
3f81c18a VP |
103 | |
104 | static void | |
eb4c3f4a | 105 | set_inferior_tty_command (const char *args, int from_tty, |
3f81c18a VP |
106 | struct cmd_list_element *c) |
107 | { | |
108 | /* CLI has assigned the user-provided value to inferior_io_terminal_scratch. | |
109 | Now route it to current inferior. */ | |
05779d57 | 110 | current_inferior ()->set_tty (inferior_io_terminal_scratch); |
3f81c18a VP |
111 | } |
112 | ||
113 | static void | |
114 | show_inferior_tty_command (struct ui_file *file, int from_tty, | |
115 | struct cmd_list_element *c, const char *value) | |
116 | { | |
117 | /* Note that we ignore the passed-in value in favor of computing it | |
118 | directly. */ | |
05779d57 | 119 | const char *inferior_tty = current_inferior ()->tty (); |
abbb1732 | 120 | |
05779d57 PA |
121 | if (inferior_tty == nullptr) |
122 | inferior_tty = ""; | |
3f81c18a | 123 | fprintf_filtered (gdb_stdout, |
275f2e57 | 124 | _("Terminal for future runs of program being debugged " |
05779d57 | 125 | "is \"%s\".\n"), inferior_tty); |
3cb3b8df BR |
126 | } |
127 | ||
cbaaa0ca | 128 | const char * |
07091751 FN |
129 | get_inferior_args (void) |
130 | { | |
3f81c18a | 131 | if (current_inferior ()->argc != 0) |
552c04a7 | 132 | { |
8c4b5f3d | 133 | gdb::array_view<char * const> args (current_inferior ()->argv, |
dda83cd7 | 134 | current_inferior ()->argc); |
8c4b5f3d | 135 | std::string n = construct_inferior_arguments (args); |
c699004a | 136 | set_inferior_args (n.c_str ()); |
552c04a7 TT |
137 | } |
138 | ||
3f81c18a VP |
139 | if (current_inferior ()->args == NULL) |
140 | current_inferior ()->args = xstrdup (""); | |
552c04a7 | 141 | |
3f81c18a | 142 | return current_inferior ()->args; |
07091751 FN |
143 | } |
144 | ||
3f81c18a VP |
145 | /* Set the arguments for the current inferior. Ownership of |
146 | NEWARGS is not transferred. */ | |
147 | ||
148 | void | |
0b39b52e | 149 | set_inferior_args (const char *newargs) |
07091751 | 150 | { |
3f81c18a VP |
151 | xfree (current_inferior ()->args); |
152 | current_inferior ()->args = newargs ? xstrdup (newargs) : NULL; | |
153 | current_inferior ()->argc = 0; | |
154 | current_inferior ()->argv = 0; | |
07091751 | 155 | } |
c5aa993b | 156 | |
552c04a7 TT |
157 | void |
158 | set_inferior_args_vector (int argc, char **argv) | |
159 | { | |
3f81c18a VP |
160 | current_inferior ()->argc = argc; |
161 | current_inferior ()->argv = argv; | |
552c04a7 TT |
162 | } |
163 | ||
164 | /* Notice when `set args' is run. */ | |
6339bfc4 | 165 | |
552c04a7 | 166 | static void |
eb4c3f4a | 167 | set_args_command (const char *args, int from_tty, struct cmd_list_element *c) |
552c04a7 | 168 | { |
3f81c18a VP |
169 | /* CLI has assigned the user-provided value to inferior_args_scratch. |
170 | Now route it to current inferior. */ | |
171 | set_inferior_args (inferior_args_scratch); | |
552c04a7 TT |
172 | } |
173 | ||
174 | /* Notice when `show args' is run. */ | |
6339bfc4 | 175 | |
552c04a7 | 176 | static void |
3f81c18a VP |
177 | show_args_command (struct ui_file *file, int from_tty, |
178 | struct cmd_list_element *c, const char *value) | |
552c04a7 | 179 | { |
258c00cc TT |
180 | /* Note that we ignore the passed-in value in favor of computing it |
181 | directly. */ | |
182 | deprecated_show_value_hack (file, from_tty, c, get_inferior_args ()); | |
552c04a7 TT |
183 | } |
184 | ||
268a13a5 | 185 | /* See gdbsupport/common-inferior.h. */ |
d092c5a2 | 186 | |
bc3b087d | 187 | void |
d092c5a2 SDJ |
188 | set_inferior_cwd (const char *cwd) |
189 | { | |
190 | struct inferior *inf = current_inferior (); | |
191 | ||
192 | gdb_assert (inf != NULL); | |
193 | ||
194 | if (cwd == NULL) | |
195 | inf->cwd.reset (); | |
196 | else | |
197 | inf->cwd.reset (xstrdup (cwd)); | |
198 | } | |
199 | ||
268a13a5 | 200 | /* See gdbsupport/common-inferior.h. */ |
d092c5a2 SDJ |
201 | |
202 | const char * | |
203 | get_inferior_cwd () | |
204 | { | |
205 | return current_inferior ()->cwd.get (); | |
206 | } | |
207 | ||
208 | /* Handle the 'set cwd' command. */ | |
209 | ||
210 | static void | |
eb4c3f4a | 211 | set_cwd_command (const char *args, int from_tty, struct cmd_list_element *c) |
d092c5a2 SDJ |
212 | { |
213 | if (*inferior_cwd_scratch == '\0') | |
214 | set_inferior_cwd (NULL); | |
215 | else | |
216 | set_inferior_cwd (inferior_cwd_scratch); | |
217 | } | |
218 | ||
219 | /* Handle the 'show cwd' command. */ | |
220 | ||
221 | static void | |
222 | show_cwd_command (struct ui_file *file, int from_tty, | |
223 | struct cmd_list_element *c, const char *value) | |
224 | { | |
225 | const char *cwd = get_inferior_cwd (); | |
226 | ||
227 | if (cwd == NULL) | |
228 | fprintf_filtered (gdb_stdout, | |
229 | _("\ | |
230 | You have not set the inferior's current working directory.\n\ | |
bc3b087d SDJ |
231 | The inferior will inherit GDB's cwd if native debugging, or the remote\n\ |
232 | server's cwd if remote debugging.\n")); | |
d092c5a2 SDJ |
233 | else |
234 | fprintf_filtered (gdb_stdout, | |
235 | _("Current working directory that will be used " | |
236 | "when starting the inferior is \"%s\".\n"), cwd); | |
237 | } | |
238 | ||
552c04a7 | 239 | |
6c4486e6 PA |
240 | /* This function strips the '&' character (indicating background |
241 | execution) that is added as *the last* of the arguments ARGS of a | |
242 | command. A copy of the incoming ARGS without the '&' is returned, | |
243 | unless the resulting string after stripping is empty, in which case | |
244 | NULL is returned. *BG_CHAR_P is an output boolean that indicates | |
245 | whether the '&' character was found. */ | |
6339bfc4 | 246 | |
6be9a197 | 247 | static gdb::unique_xmalloc_ptr<char> |
6c4486e6 | 248 | strip_bg_char (const char *args, int *bg_char_p) |
43ff13b4 | 249 | { |
6c4486e6 | 250 | const char *p; |
c5aa993b | 251 | |
6c4486e6 PA |
252 | if (args == NULL || *args == '\0') |
253 | { | |
254 | *bg_char_p = 0; | |
255 | return NULL; | |
256 | } | |
c5aa993b | 257 | |
6c4486e6 PA |
258 | p = args + strlen (args); |
259 | if (p[-1] == '&') | |
43ff13b4 | 260 | { |
6c4486e6 PA |
261 | p--; |
262 | while (p > args && isspace (p[-1])) | |
263 | p--; | |
264 | ||
265 | *bg_char_p = 1; | |
266 | if (p != args) | |
6be9a197 TT |
267 | return gdb::unique_xmalloc_ptr<char> |
268 | (savestring (args, p - args)); | |
6c4486e6 | 269 | else |
6be9a197 | 270 | return gdb::unique_xmalloc_ptr<char> (nullptr); |
43ff13b4 | 271 | } |
6c4486e6 PA |
272 | |
273 | *bg_char_p = 0; | |
b02f78f9 | 274 | return make_unique_xstrdup (args); |
43ff13b4 JM |
275 | } |
276 | ||
281b533b DJ |
277 | /* Common actions to take after creating any sort of inferior, by any |
278 | means (running, attaching, connecting, et cetera). The target | |
279 | should be stopped. */ | |
280 | ||
281 | void | |
a7aba266 | 282 | post_create_inferior (int from_tty) |
281b533b | 283 | { |
ce406537 | 284 | |
b79599ff | 285 | /* Be sure we own the terminal in case write operations are performed. */ |
223ffa71 | 286 | target_terminal::ours_for_output (); |
b79599ff | 287 | |
424163ea DJ |
288 | /* If the target hasn't taken care of this already, do it now. |
289 | Targets which need to access registers during to_open, | |
290 | to_create_inferior, or to_attach should do it earlier; but many | |
291 | don't need to. */ | |
292 | target_find_description (); | |
293 | ||
ce406537 PA |
294 | /* Now that we know the register layout, retrieve current PC. But |
295 | if the PC is unavailable (e.g., we're opening a core file with | |
296 | missing registers info), ignore it. */ | |
f2ffa92b PA |
297 | thread_info *thr = inferior_thread (); |
298 | ||
299 | thr->suspend.stop_pc = 0; | |
a70b8144 | 300 | try |
ce406537 | 301 | { |
8dc3273e SM |
302 | regcache *rc = get_thread_regcache (thr); |
303 | thr->suspend.stop_pc = regcache_read_pc (rc); | |
ce406537 | 304 | } |
230d2906 | 305 | catch (const gdb_exception_error &ex) |
7556d4a4 PA |
306 | { |
307 | if (ex.error != NOT_AVAILABLE_ERROR) | |
eedc3f4f | 308 | throw; |
7556d4a4 | 309 | } |
f698437e | 310 | |
7e10abd1 | 311 | if (current_program_space->exec_bfd ()) |
50c71eaf | 312 | { |
2eff07b3 PP |
313 | const unsigned solib_add_generation |
314 | = current_program_space->solib_add_generation; | |
315 | ||
9353355f DJ |
316 | /* Create the hooks to handle shared library load and unload |
317 | events. */ | |
268a4a75 | 318 | solib_create_inferior_hook (from_tty); |
268a4a75 | 319 | |
2eff07b3 PP |
320 | if (current_program_space->solib_add_generation == solib_add_generation) |
321 | { | |
322 | /* The platform-specific hook should load initial shared libraries, | |
323 | but didn't. FROM_TTY will be incorrectly 0 but such solib | |
324 | targets should be fixed anyway. Call it only after the solib | |
325 | target has been initialized by solib_create_inferior_hook. */ | |
326 | ||
327 | if (info_verbose) | |
328 | warning (_("platform-specific solib_create_inferior_hook did " | |
329 | "not load initial shared libraries.")); | |
330 | ||
331 | /* If the solist is global across processes, there's no need to | |
332 | refetch it here. */ | |
f5656ead | 333 | if (!gdbarch_has_global_solist (target_gdbarch ())) |
e696b3ad | 334 | solib_add (NULL, 0, auto_solib_add); |
2eff07b3 | 335 | } |
9353355f DJ |
336 | } |
337 | ||
ea5d7a99 PM |
338 | /* If the user sets watchpoints before execution having started, |
339 | then she gets software watchpoints, because GDB can't know which | |
340 | target will end up being pushed, or if it supports hardware | |
341 | watchpoints or not. breakpoint_re_set takes care of promoting | |
342 | watchpoints to hardware watchpoints if possible, however, if this | |
343 | new inferior doesn't load shared libraries or we don't pull in | |
344 | symbols from any other source on this target/arch, | |
345 | breakpoint_re_set is never called. Call it now so that software | |
346 | watchpoints get a chance to be promoted to hardware watchpoints | |
347 | if the now pushed target supports hardware watchpoints. */ | |
348 | breakpoint_re_set (); | |
349 | ||
a0ff652f | 350 | gdb::observers::inferior_created.notify (current_inferior ()); |
281b533b DJ |
351 | } |
352 | ||
a4d5f2e0 JB |
353 | /* Kill the inferior if already running. This function is designed |
354 | to be called when we are about to start the execution of the program | |
355 | from the beginning. Ask the user to confirm that he wants to restart | |
356 | the program being debugged when FROM_TTY is non-null. */ | |
c906108c | 357 | |
8edfe269 | 358 | static void |
a4d5f2e0 JB |
359 | kill_if_already_running (int from_tty) |
360 | { | |
55f6301a | 361 | if (inferior_ptid != null_ptid && target_has_execution ()) |
c906108c | 362 | { |
8edfe269 DJ |
363 | /* Bail out before killing the program if we will not be able to |
364 | restart it. */ | |
365 | target_require_runnable (); | |
366 | ||
adf40b2e | 367 | if (from_tty |
9e2f0ad4 HZ |
368 | && !query (_("The program being debugged has been started already.\n\ |
369 | Start it from the beginning? "))) | |
8a3fe4f8 | 370 | error (_("Program not restarted.")); |
c906108c | 371 | target_kill (); |
c906108c | 372 | } |
a4d5f2e0 JB |
373 | } |
374 | ||
329ea579 | 375 | /* See inferior.h. */ |
8bc2fe48 | 376 | |
329ea579 | 377 | void |
8bc2fe48 PA |
378 | prepare_execution_command (struct target_ops *target, int background) |
379 | { | |
380 | /* If we get a request for running in the bg but the target | |
381 | doesn't support it, error out. */ | |
f6ac5f3d | 382 | if (background && !target->can_async_p ()) |
8bc2fe48 PA |
383 | error (_("Asynchronous execution not supported on this target.")); |
384 | ||
0b333c5e | 385 | if (!background) |
8bc2fe48 | 386 | { |
0b333c5e PA |
387 | /* If we get a request for running in the fg, then we need to |
388 | simulate synchronous (fg) execution. Note no cleanup is | |
389 | necessary for this. stdin is re-enabled whenever an error | |
390 | reaches the top level. */ | |
a8836c93 | 391 | all_uis_on_sync_execution_starting (); |
8bc2fe48 PA |
392 | } |
393 | } | |
394 | ||
4e5a4f58 JB |
395 | /* Determine how the new inferior will behave. */ |
396 | ||
397 | enum run_how | |
398 | { | |
399 | /* Run program without any explicit stop during startup. */ | |
400 | RUN_NORMAL, | |
401 | ||
402 | /* Stop at the beginning of the program's main function. */ | |
403 | RUN_STOP_AT_MAIN, | |
404 | ||
405 | /* Stop at the first instruction of the program. */ | |
406 | RUN_STOP_AT_FIRST_INSN | |
407 | }; | |
408 | ||
409 | /* Implement the "run" command. Force a stop during program start if | |
410 | requested by RUN_HOW. */ | |
f67a969f | 411 | |
a4d5f2e0 | 412 | static void |
0b39b52e | 413 | run_command_1 (const char *args, int from_tty, enum run_how run_how) |
a4d5f2e0 | 414 | { |
7c5ded6a | 415 | const char *exec_file; |
79a45e25 | 416 | struct ui_out *uiout = current_uiout; |
b3ccfe11 | 417 | struct target_ops *run_target; |
6c4486e6 | 418 | int async_exec; |
c906108c | 419 | |
a4d5f2e0 JB |
420 | dont_repeat (); |
421 | ||
422 | kill_if_already_running (from_tty); | |
3c35e65b UW |
423 | |
424 | init_wait_for_inferior (); | |
c906108c SS |
425 | clear_breakpoint_hit_counts (); |
426 | ||
fd79ecee DJ |
427 | /* Clean up any leftovers from other runs. Some other things from |
428 | this function should probably be moved into target_pre_inferior. */ | |
429 | target_pre_inferior (from_tty); | |
430 | ||
39ad761d JB |
431 | /* The comment here used to read, "The exec file is re-read every |
432 | time we do a generic_mourn_inferior, so we just have to worry | |
433 | about the symbol file." The `generic_mourn_inferior' function | |
434 | gets called whenever the program exits. However, suppose the | |
435 | program exits, and *then* the executable file changes? We need | |
436 | to check again here. Since reopen_exec_file doesn't do anything | |
437 | if the timestamp hasn't changed, I don't see the harm. */ | |
438 | reopen_exec_file (); | |
c906108c SS |
439 | reread_symbols (); |
440 | ||
6be9a197 TT |
441 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec); |
442 | args = stripped.get (); | |
f67a969f | 443 | |
8bc2fe48 PA |
444 | /* Do validation and preparation before possibly changing anything |
445 | in the inferior. */ | |
39ad761d | 446 | |
b3ccfe11 TT |
447 | run_target = find_run_target (); |
448 | ||
8bc2fe48 PA |
449 | prepare_execution_command (run_target, async_exec); |
450 | ||
f6ac5f3d | 451 | if (non_stop && !run_target->supports_non_stop ()) |
9908b566 VP |
452 | error (_("The target does not support running in non-stop mode.")); |
453 | ||
8bc2fe48 PA |
454 | /* Done. Can now set breakpoints, change inferior args, etc. */ |
455 | ||
4e5a4f58 JB |
456 | /* Insert temporary breakpoint in main function if requested. */ |
457 | if (run_how == RUN_STOP_AT_MAIN) | |
e242fd12 SM |
458 | { |
459 | std::string arg = string_printf ("-qualified %s", main_name ()); | |
460 | tbreak_command (arg.c_str (), 0); | |
461 | } | |
8bc2fe48 | 462 | |
7c5ded6a | 463 | exec_file = get_exec_file (0); |
8bc2fe48 | 464 | |
c906108c SS |
465 | /* We keep symbols from add-symbol-file, on the grounds that the |
466 | user might want to add some symbols before running the program | |
467 | (right?). But sometimes (dynamic loading where the user manually | |
468 | introduces the new symbols with add-symbol-file), the code which | |
469 | the symbols describe does not persist between runs. Currently | |
470 | the user has to manually nuke all symbols between runs if they | |
471 | want them to go away (PR 2207). This is probably reasonable. */ | |
472 | ||
8bc2fe48 PA |
473 | /* If there were other args, beside '&', process them. */ |
474 | if (args != NULL) | |
475 | set_inferior_args (args); | |
c906108c SS |
476 | |
477 | if (from_tty) | |
478 | { | |
112e8700 SM |
479 | uiout->field_string (NULL, "Starting program"); |
480 | uiout->text (": "); | |
8b93c638 | 481 | if (exec_file) |
112e8700 SM |
482 | uiout->field_string ("execfile", exec_file); |
483 | uiout->spaces (1); | |
552c04a7 TT |
484 | /* We call get_inferior_args() because we might need to compute |
485 | the value now. */ | |
112e8700 SM |
486 | uiout->field_string ("infargs", get_inferior_args ()); |
487 | uiout->text ("\n"); | |
488 | uiout->flush (); | |
c906108c SS |
489 | } |
490 | ||
552c04a7 TT |
491 | /* We call get_inferior_args() because we might need to compute |
492 | the value now. */ | |
f6ac5f3d PA |
493 | run_target->create_inferior (exec_file, |
494 | std::string (get_inferior_args ()), | |
495 | current_inferior ()->environment.envp (), | |
496 | from_tty); | |
b3ccfe11 TT |
497 | /* to_create_inferior should push the target, so after this point we |
498 | shouldn't refer to run_target again. */ | |
499 | run_target = NULL; | |
281b533b | 500 | |
29f49a6a PA |
501 | /* We're starting off a new process. When we get out of here, in |
502 | non-stop mode, finish the state of all threads of that process, | |
503 | but leave other threads alone, as they may be stopped in internal | |
504 | events --- the frontend shouldn't see them as stopped. In | |
505 | all-stop, always finish the state of all threads, as we may be | |
506 | resuming more than just the new process. */ | |
5b6d1e4f PA |
507 | process_stratum_target *finish_target; |
508 | ptid_t finish_ptid; | |
509 | if (non_stop) | |
510 | { | |
511 | finish_target = current_inferior ()->process_target (); | |
512 | finish_ptid = ptid_t (current_inferior ()->pid); | |
513 | } | |
514 | else | |
515 | { | |
516 | finish_target = nullptr; | |
517 | finish_ptid = minus_one_ptid; | |
518 | } | |
519 | scoped_finish_thread_state finish_state (finish_target, finish_ptid); | |
29f49a6a | 520 | |
de1b3c3d PA |
521 | /* Pass zero for FROM_TTY, because at this point the "run" command |
522 | has done its thing; now we are setting up the running program. */ | |
a7aba266 | 523 | post_create_inferior (0); |
281b533b | 524 | |
4e5a4f58 JB |
525 | /* Queue a pending event so that the program stops immediately. */ |
526 | if (run_how == RUN_STOP_AT_FIRST_INSN) | |
527 | { | |
528 | thread_info *thr = inferior_thread (); | |
529 | thr->suspend.waitstatus_pending_p = 1; | |
530 | thr->suspend.waitstatus.kind = TARGET_WAITKIND_STOPPED; | |
531 | thr->suspend.waitstatus.value.sig = GDB_SIGNAL_0; | |
532 | } | |
533 | ||
74d1f91e JK |
534 | /* Start the target running. Do not use -1 continuation as it would skip |
535 | breakpoint right at the entry point. */ | |
64ce06e4 | 536 | proceed (regcache_read_pc (get_current_regcache ()), GDB_SIGNAL_0); |
c906108c | 537 | |
29f49a6a PA |
538 | /* Since there was no error, there's no need to finish the thread |
539 | states here. */ | |
731f534f | 540 | finish_state.release (); |
29f49a6a | 541 | } |
c906108c | 542 | |
f67a969f | 543 | static void |
0b39b52e | 544 | run_command (const char *args, int from_tty) |
f67a969f | 545 | { |
4e5a4f58 | 546 | run_command_1 (args, from_tty, RUN_NORMAL); |
f67a969f JB |
547 | } |
548 | ||
a4d5f2e0 JB |
549 | /* Start the execution of the program up until the beginning of the main |
550 | program. */ | |
551 | ||
552 | static void | |
0b39b52e | 553 | start_command (const char *args, int from_tty) |
a4d5f2e0 JB |
554 | { |
555 | /* Some languages such as Ada need to search inside the program | |
556 | minimal symbols for the location where to put the temporary | |
557 | breakpoint before starting. */ | |
558 | if (!have_minimal_symbols ()) | |
8a3fe4f8 | 559 | error (_("No symbol table loaded. Use the \"file\" command.")); |
a4d5f2e0 | 560 | |
f67a969f | 561 | /* Run the program until reaching the main procedure... */ |
4e5a4f58 JB |
562 | run_command_1 (args, from_tty, RUN_STOP_AT_MAIN); |
563 | } | |
564 | ||
565 | /* Start the execution of the program stopping at the first | |
566 | instruction. */ | |
567 | ||
568 | static void | |
0b39b52e | 569 | starti_command (const char *args, int from_tty) |
4e5a4f58 JB |
570 | { |
571 | run_command_1 (args, from_tty, RUN_STOP_AT_FIRST_INSN); | |
a4d5f2e0 JB |
572 | } |
573 | ||
8cae4b3f PA |
574 | static int |
575 | proceed_thread_callback (struct thread_info *thread, void *arg) | |
576 | { | |
74531fed PA |
577 | /* We go through all threads individually instead of compressing |
578 | into a single target `resume_all' request, because some threads | |
579 | may be stopped in internal breakpoints/events, or stopped waiting | |
580 | for its turn in the displaced stepping queue (that is, they are | |
581 | running && !executing). The target side has no idea about why | |
582 | the thread is stopped, so a `resume_all' command would resume too | |
583 | much. If/when GDB gains a way to tell the target `hold this | |
584 | thread stopped until I say otherwise', then we can optimize | |
585 | this. */ | |
00431a78 | 586 | if (thread->state != THREAD_STOPPED) |
8cae4b3f PA |
587 | return 0; |
588 | ||
5b6d1e4f PA |
589 | if (!thread->inf->has_execution ()) |
590 | return 0; | |
591 | ||
00431a78 | 592 | switch_to_thread (thread); |
70509625 | 593 | clear_proceed_status (0); |
64ce06e4 | 594 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
8cae4b3f PA |
595 | return 0; |
596 | } | |
597 | ||
70221824 | 598 | static void |
d729566a PA |
599 | ensure_valid_thread (void) |
600 | { | |
00431a78 PA |
601 | if (inferior_ptid == null_ptid |
602 | || inferior_thread ()->state == THREAD_EXITED) | |
3e43a32a | 603 | error (_("Cannot execute this command without a live selected thread.")); |
d729566a PA |
604 | } |
605 | ||
573cda03 | 606 | /* If the user is looking at trace frames, any resumption of execution |
1777feb0 | 607 | is likely to mix up recorded and live target data. So simply |
573cda03 SS |
608 | disallow those commands. */ |
609 | ||
70221824 | 610 | static void |
573cda03 SS |
611 | ensure_not_tfind_mode (void) |
612 | { | |
613 | if (get_traceframe_number () >= 0) | |
3e43a32a | 614 | error (_("Cannot execute this command while looking at trace frames.")); |
573cda03 SS |
615 | } |
616 | ||
3d3fef6b YQ |
617 | /* Throw an error indicating the current thread is running. */ |
618 | ||
619 | static void | |
620 | error_is_running (void) | |
621 | { | |
622 | error (_("Cannot execute this command while " | |
623 | "the selected thread is running.")); | |
624 | } | |
625 | ||
626 | /* Calls error_is_running if the current thread is running. */ | |
627 | ||
628 | static void | |
629 | ensure_not_running (void) | |
630 | { | |
00431a78 | 631 | if (inferior_thread ()->state == THREAD_RUNNING) |
3d3fef6b YQ |
632 | error_is_running (); |
633 | } | |
634 | ||
77ebaa5a VP |
635 | void |
636 | continue_1 (int all_threads) | |
637 | { | |
3d488bfc | 638 | ERROR_NO_INFERIOR; |
573cda03 | 639 | ensure_not_tfind_mode (); |
3d488bfc | 640 | |
77ebaa5a VP |
641 | if (non_stop && all_threads) |
642 | { | |
643 | /* Don't error out if the current thread is running, because | |
abbb1732 | 644 | there may be other stopped threads. */ |
77ebaa5a | 645 | |
5ed8105e PA |
646 | /* Backup current thread and selected frame and restore on scope |
647 | exit. */ | |
648 | scoped_restore_current_thread restore_thread; | |
77ebaa5a VP |
649 | |
650 | iterate_over_threads (proceed_thread_callback, NULL); | |
651 | ||
3b12939d | 652 | if (current_ui->prompt_state == PROMPT_BLOCKED) |
0ff33695 PA |
653 | { |
654 | /* If all threads in the target were already running, | |
655 | proceed_thread_callback ends up never calling proceed, | |
656 | and so nothing calls this to put the inferior's terminal | |
657 | settings in effect and remove stdin from the event loop, | |
658 | which we must when running a foreground command. E.g.: | |
659 | ||
660 | (gdb) c -a& | |
661 | Continuing. | |
662 | <all threads are running now> | |
663 | (gdb) c -a | |
664 | Continuing. | |
665 | <no thread was resumed, but the inferior now owns the terminal> | |
666 | */ | |
223ffa71 | 667 | target_terminal::inferior (); |
0ff33695 | 668 | } |
77ebaa5a VP |
669 | } |
670 | else | |
671 | { | |
d729566a | 672 | ensure_valid_thread (); |
77ebaa5a | 673 | ensure_not_running (); |
70509625 | 674 | clear_proceed_status (0); |
64ce06e4 | 675 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
77ebaa5a VP |
676 | } |
677 | } | |
678 | ||
8cae4b3f | 679 | /* continue [-a] [proceed-count] [&] */ |
6339bfc4 | 680 | |
1dd5fedc | 681 | static void |
0b39b52e | 682 | continue_command (const char *args, int from_tty) |
c906108c | 683 | { |
6c4486e6 | 684 | int async_exec; |
5b6d1e4f | 685 | bool all_threads_p = false; |
6c4486e6 | 686 | |
c906108c SS |
687 | ERROR_NO_INFERIOR; |
688 | ||
1777feb0 | 689 | /* Find out whether we must run in the background. */ |
6be9a197 TT |
690 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec); |
691 | args = stripped.get (); | |
43ff13b4 | 692 | |
8cae4b3f PA |
693 | if (args != NULL) |
694 | { | |
61012eef | 695 | if (startswith (args, "-a")) |
8cae4b3f | 696 | { |
5b6d1e4f | 697 | all_threads_p = true; |
8cae4b3f PA |
698 | args += sizeof ("-a") - 1; |
699 | if (*args == '\0') | |
700 | args = NULL; | |
701 | } | |
702 | } | |
703 | ||
5b6d1e4f | 704 | if (!non_stop && all_threads_p) |
8cae4b3f PA |
705 | error (_("`-a' is meaningless in all-stop mode.")); |
706 | ||
5b6d1e4f | 707 | if (args != NULL && all_threads_p) |
3e43a32a MS |
708 | error (_("Can't resume all threads and specify " |
709 | "proceed count simultaneously.")); | |
8cae4b3f PA |
710 | |
711 | /* If we have an argument left, set proceed count of breakpoint we | |
712 | stopped at. */ | |
713 | if (args != NULL) | |
c906108c | 714 | { |
347bddb7 | 715 | bpstat bs = NULL; |
8671a17b PA |
716 | int num, stat; |
717 | int stopped = 0; | |
347bddb7 PA |
718 | struct thread_info *tp; |
719 | ||
720 | if (non_stop) | |
00431a78 | 721 | tp = inferior_thread (); |
347bddb7 PA |
722 | else |
723 | { | |
5b6d1e4f | 724 | process_stratum_target *last_target; |
347bddb7 | 725 | ptid_t last_ptid; |
347bddb7 | 726 | |
5b6d1e4f PA |
727 | get_last_target_status (&last_target, &last_ptid, nullptr); |
728 | tp = find_thread_ptid (last_target, last_ptid); | |
347bddb7 PA |
729 | } |
730 | if (tp != NULL) | |
16c381f0 | 731 | bs = tp->control.stop_bpstat; |
8671a17b PA |
732 | |
733 | while ((stat = bpstat_num (&bs, &num)) != 0) | |
734 | if (stat > 0) | |
735 | { | |
736 | set_ignore_count (num, | |
8cae4b3f | 737 | parse_and_eval_long (args) - 1, |
8671a17b PA |
738 | from_tty); |
739 | /* set_ignore_count prints a message ending with a period. | |
740 | So print two spaces before "Continuing.". */ | |
741 | if (from_tty) | |
742 | printf_filtered (" "); | |
743 | stopped = 1; | |
744 | } | |
745 | ||
746 | if (!stopped && from_tty) | |
c906108c SS |
747 | { |
748 | printf_filtered | |
749 | ("Not stopped at any breakpoint; argument ignored.\n"); | |
750 | } | |
c906108c SS |
751 | } |
752 | ||
3b12939d PA |
753 | ERROR_NO_INFERIOR; |
754 | ensure_not_tfind_mode (); | |
755 | ||
5b6d1e4f | 756 | if (!non_stop || !all_threads_p) |
3b12939d PA |
757 | { |
758 | ensure_valid_thread (); | |
759 | ensure_not_running (); | |
760 | } | |
761 | ||
8b88a78e | 762 | prepare_execution_command (current_top_target (), async_exec); |
3b12939d | 763 | |
c906108c | 764 | if (from_tty) |
a3f17187 | 765 | printf_filtered (_("Continuing.\n")); |
c906108c | 766 | |
5b6d1e4f | 767 | continue_1 (all_threads_p); |
c906108c SS |
768 | } |
769 | \f | |
29734269 | 770 | /* Record in TP the starting point of a "step" or "next" command. */ |
edb3359d DJ |
771 | |
772 | static void | |
29734269 | 773 | set_step_frame (thread_info *tp) |
edb3359d | 774 | { |
29734269 SM |
775 | /* This can be removed once this function no longer implicitly relies on the |
776 | inferior_ptid value. */ | |
777 | gdb_assert (inferior_ptid == tp->ptid); | |
778 | ||
51abb421 | 779 | frame_info *frame = get_current_frame (); |
edb3359d | 780 | |
51abb421 | 781 | symtab_and_line sal = find_frame_sal (frame); |
29734269 | 782 | set_step_info (tp, frame, sal); |
51abb421 PA |
783 | |
784 | CORE_ADDR pc = get_frame_pc (frame); | |
64ce06e4 | 785 | tp->control.step_start_function = find_pc_function (pc); |
edb3359d DJ |
786 | } |
787 | ||
c906108c SS |
788 | /* Step until outside of current statement. */ |
789 | ||
c906108c | 790 | static void |
0b39b52e | 791 | step_command (const char *count_string, int from_tty) |
c906108c SS |
792 | { |
793 | step_1 (0, 0, count_string); | |
794 | } | |
795 | ||
796 | /* Likewise, but skip over subroutine calls as if single instructions. */ | |
797 | ||
c906108c | 798 | static void |
0b39b52e | 799 | next_command (const char *count_string, int from_tty) |
c906108c SS |
800 | { |
801 | step_1 (1, 0, count_string); | |
802 | } | |
803 | ||
804 | /* Likewise, but step only one instruction. */ | |
805 | ||
1dd5fedc | 806 | static void |
0b39b52e | 807 | stepi_command (const char *count_string, int from_tty) |
c906108c SS |
808 | { |
809 | step_1 (0, 1, count_string); | |
810 | } | |
811 | ||
1dd5fedc | 812 | static void |
0b39b52e | 813 | nexti_command (const char *count_string, int from_tty) |
c906108c SS |
814 | { |
815 | step_1 (1, 1, count_string); | |
816 | } | |
817 | ||
243a9253 PA |
818 | /* Data for the FSM that manages the step/next/stepi/nexti |
819 | commands. */ | |
820 | ||
46e3ed7f | 821 | struct step_command_fsm : public thread_fsm |
243a9253 | 822 | { |
243a9253 PA |
823 | /* How many steps left in a "step N"-like command. */ |
824 | int count; | |
825 | ||
826 | /* If true, this is a next/nexti, otherwise a step/stepi. */ | |
827 | int skip_subroutines; | |
828 | ||
829 | /* If true, this is a stepi/nexti, otherwise a step/step. */ | |
830 | int single_inst; | |
243a9253 | 831 | |
46e3ed7f TT |
832 | explicit step_command_fsm (struct interp *cmd_interp) |
833 | : thread_fsm (cmd_interp) | |
834 | { | |
835 | } | |
243a9253 | 836 | |
46e3ed7f TT |
837 | void clean_up (struct thread_info *thread) override; |
838 | bool should_stop (struct thread_info *thread) override; | |
839 | enum async_reply_reason do_async_reply_reason () override; | |
243a9253 PA |
840 | }; |
841 | ||
243a9253 PA |
842 | /* Prepare for a step/next/etc. command. Any target resource |
843 | allocated here is undone in the FSM's clean_up method. */ | |
844 | ||
845 | static void | |
846 | step_command_fsm_prepare (struct step_command_fsm *sm, | |
847 | int skip_subroutines, int single_inst, | |
848 | int count, struct thread_info *thread) | |
849 | { | |
850 | sm->skip_subroutines = skip_subroutines; | |
851 | sm->single_inst = single_inst; | |
852 | sm->count = count; | |
243a9253 PA |
853 | |
854 | /* Leave the si command alone. */ | |
855 | if (!sm->single_inst || sm->skip_subroutines) | |
856 | set_longjmp_breakpoint (thread, get_frame_id (get_current_frame ())); | |
857 | ||
858 | thread->control.stepping_command = 1; | |
859 | } | |
860 | ||
29734269 | 861 | static int prepare_one_step (thread_info *, struct step_command_fsm *sm); |
243a9253 | 862 | |
c906108c | 863 | static void |
0b39b52e | 864 | step_1 (int skip_subroutines, int single_inst, const char *count_string) |
c906108c | 865 | { |
243a9253 | 866 | int count; |
6c4486e6 | 867 | int async_exec; |
243a9253 PA |
868 | struct thread_info *thr; |
869 | struct step_command_fsm *step_sm; | |
c5aa993b | 870 | |
c906108c | 871 | ERROR_NO_INFERIOR; |
573cda03 | 872 | ensure_not_tfind_mode (); |
d729566a | 873 | ensure_valid_thread (); |
94cc34af | 874 | ensure_not_running (); |
43ff13b4 | 875 | |
6be9a197 TT |
876 | gdb::unique_xmalloc_ptr<char> stripped |
877 | = strip_bg_char (count_string, &async_exec); | |
878 | count_string = stripped.get (); | |
c5aa993b | 879 | |
8b88a78e | 880 | prepare_execution_command (current_top_target (), async_exec); |
43ff13b4 | 881 | |
bb518678 | 882 | count = count_string ? parse_and_eval_long (count_string) : 1; |
c906108c | 883 | |
243a9253 | 884 | clear_proceed_status (1); |
611c83ae | 885 | |
243a9253 PA |
886 | /* Setup the execution command state machine to handle all the COUNT |
887 | steps. */ | |
888 | thr = inferior_thread (); | |
46e3ed7f TT |
889 | step_sm = new step_command_fsm (command_interp ()); |
890 | thr->thread_fsm = step_sm; | |
611c83ae | 891 | |
243a9253 PA |
892 | step_command_fsm_prepare (step_sm, skip_subroutines, |
893 | single_inst, count, thr); | |
c2d11a7d | 894 | |
0b333c5e PA |
895 | /* Do only one step for now, before returning control to the event |
896 | loop. Let the continuation figure out how many other steps we | |
897 | need to do, and handle them one at the time, through | |
898 | step_once. */ | |
29734269 | 899 | if (!prepare_one_step (thr, step_sm)) |
243a9253 PA |
900 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
901 | else | |
902 | { | |
3b12939d PA |
903 | int proceeded; |
904 | ||
243a9253 PA |
905 | /* Stepped into an inline frame. Pretend that we've |
906 | stopped. */ | |
46e3ed7f | 907 | thr->thread_fsm->clean_up (thr); |
3b12939d PA |
908 | proceeded = normal_stop (); |
909 | if (!proceeded) | |
b1a35af2 | 910 | inferior_event_handler (INF_EXEC_COMPLETE); |
3b12939d | 911 | all_uis_check_sync_execution_done (); |
243a9253 | 912 | } |
c2d11a7d | 913 | } |
c906108c | 914 | |
243a9253 PA |
915 | /* Implementation of the 'should_stop' FSM method for stepping |
916 | commands. Called after we are done with one step operation, to | |
917 | check whether we need to step again, before we print the prompt and | |
918 | return control to the user. If count is > 1, returns false, as we | |
919 | will need to keep going. */ | |
6339bfc4 | 920 | |
46e3ed7f TT |
921 | bool |
922 | step_command_fsm::should_stop (struct thread_info *tp) | |
c2d11a7d | 923 | { |
243a9253 | 924 | if (tp->control.stop_step) |
f13468d9 | 925 | { |
243a9253 PA |
926 | /* There are more steps to make, and we did stop due to |
927 | ending a stepping range. Do another step. */ | |
46e3ed7f | 928 | if (--count > 0) |
29734269 | 929 | return prepare_one_step (tp, this); |
af679fd0 | 930 | |
46e3ed7f | 931 | set_finished (); |
f107f563 | 932 | } |
af679fd0 | 933 | |
46e3ed7f | 934 | return true; |
c2d11a7d JM |
935 | } |
936 | ||
243a9253 | 937 | /* Implementation of the 'clean_up' FSM method for stepping commands. */ |
37d94800 | 938 | |
46e3ed7f TT |
939 | void |
940 | step_command_fsm::clean_up (struct thread_info *thread) | |
bfec99b2 | 941 | { |
46e3ed7f | 942 | if (!single_inst || skip_subroutines) |
8980e177 | 943 | delete_longjmp_breakpoint (thread->global_num); |
243a9253 PA |
944 | } |
945 | ||
946 | /* Implementation of the 'async_reply_reason' FSM method for stepping | |
947 | commands. */ | |
948 | ||
46e3ed7f TT |
949 | enum async_reply_reason |
950 | step_command_fsm::do_async_reply_reason () | |
243a9253 PA |
951 | { |
952 | return EXEC_ASYNC_END_STEPPING_RANGE; | |
953 | } | |
954 | ||
955 | /* Prepare for one step in "step N". The actual target resumption is | |
956 | done by the caller. Return true if we're done and should thus | |
957 | report a stop to the user. Returns false if the target needs to be | |
958 | resumed. */ | |
c2d11a7d | 959 | |
243a9253 | 960 | static int |
29734269 | 961 | prepare_one_step (thread_info *tp, struct step_command_fsm *sm) |
243a9253 | 962 | { |
29734269 SM |
963 | /* This can be removed once this function no longer implicitly relies on the |
964 | inferior_ptid value. */ | |
965 | gdb_assert (inferior_ptid == tp->ptid); | |
966 | ||
243a9253 | 967 | if (sm->count > 0) |
c906108c | 968 | { |
243a9253 PA |
969 | struct frame_info *frame = get_current_frame (); |
970 | ||
29734269 | 971 | set_step_frame (tp); |
c906108c | 972 | |
243a9253 | 973 | if (!sm->single_inst) |
c906108c | 974 | { |
1641cfcc PA |
975 | CORE_ADDR pc; |
976 | ||
edb3359d | 977 | /* Step at an inlined function behaves like "down". */ |
243a9253 | 978 | if (!sm->skip_subroutines |
00431a78 | 979 | && inline_skipped_frames (tp)) |
edb3359d | 980 | { |
09cee04b | 981 | ptid_t resume_ptid; |
4a4c04f1 BE |
982 | const char *fn = NULL; |
983 | symtab_and_line sal; | |
984 | struct symbol *sym; | |
09cee04b PA |
985 | |
986 | /* Pretend that we've ran. */ | |
987 | resume_ptid = user_visible_resume_ptid (1); | |
5b6d1e4f | 988 | set_running (tp->inf->process_target (), resume_ptid, true); |
09cee04b | 989 | |
00431a78 | 990 | step_into_inline_frame (tp); |
4a4c04f1 BE |
991 | |
992 | frame = get_current_frame (); | |
993 | sal = find_frame_sal (frame); | |
994 | sym = get_frame_function (frame); | |
995 | ||
996 | if (sym != NULL) | |
997 | fn = sym->print_name (); | |
998 | ||
999 | if (sal.line == 0 | |
1000 | || !function_name_is_marked_for_skip (fn, sal)) | |
1001 | { | |
1002 | sm->count--; | |
29734269 | 1003 | return prepare_one_step (tp, sm); |
4a4c04f1 | 1004 | } |
edb3359d DJ |
1005 | } |
1006 | ||
1641cfcc PA |
1007 | pc = get_frame_pc (frame); |
1008 | find_pc_line_pc_range (pc, | |
16c381f0 JK |
1009 | &tp->control.step_range_start, |
1010 | &tp->control.step_range_end); | |
5fbbeb29 | 1011 | |
c1e36e3e PA |
1012 | tp->control.may_range_step = 1; |
1013 | ||
5fbbeb29 | 1014 | /* If we have no line info, switch to stepi mode. */ |
16c381f0 | 1015 | if (tp->control.step_range_end == 0 && step_stop_if_no_debug) |
c1e36e3e PA |
1016 | { |
1017 | tp->control.step_range_start = tp->control.step_range_end = 1; | |
1018 | tp->control.may_range_step = 0; | |
1019 | } | |
16c381f0 | 1020 | else if (tp->control.step_range_end == 0) |
c906108c | 1021 | { |
2c02bd72 | 1022 | const char *name; |
abbb1732 | 1023 | |
1641cfcc | 1024 | if (find_pc_partial_function (pc, &name, |
16c381f0 JK |
1025 | &tp->control.step_range_start, |
1026 | &tp->control.step_range_end) == 0) | |
8a3fe4f8 | 1027 | error (_("Cannot find bounds of current function")); |
c906108c | 1028 | |
223ffa71 | 1029 | target_terminal::ours_for_output (); |
3e43a32a MS |
1030 | printf_filtered (_("Single stepping until exit from function %s," |
1031 | "\nwhich has no line number information.\n"), | |
1032 | name); | |
c906108c SS |
1033 | } |
1034 | } | |
1035 | else | |
1036 | { | |
1037 | /* Say we are stepping, but stop after one insn whatever it does. */ | |
16c381f0 | 1038 | tp->control.step_range_start = tp->control.step_range_end = 1; |
243a9253 | 1039 | if (!sm->skip_subroutines) |
c906108c SS |
1040 | /* It is stepi. |
1041 | Don't step over function calls, not even to functions lacking | |
1042 | line numbers. */ | |
16c381f0 | 1043 | tp->control.step_over_calls = STEP_OVER_NONE; |
c906108c SS |
1044 | } |
1045 | ||
243a9253 | 1046 | if (sm->skip_subroutines) |
16c381f0 | 1047 | tp->control.step_over_calls = STEP_OVER_ALL; |
c906108c | 1048 | |
243a9253 | 1049 | return 0; |
c906108c | 1050 | } |
243a9253 PA |
1051 | |
1052 | /* Done. */ | |
46e3ed7f | 1053 | sm->set_finished (); |
243a9253 | 1054 | return 1; |
c906108c | 1055 | } |
c2d11a7d | 1056 | |
c906108c SS |
1057 | \f |
1058 | /* Continue program at specified address. */ | |
1059 | ||
1060 | static void | |
0b39b52e | 1061 | jump_command (const char *arg, int from_tty) |
c906108c | 1062 | { |
5af949e3 | 1063 | struct gdbarch *gdbarch = get_current_arch (); |
52f0bd74 | 1064 | CORE_ADDR addr; |
c906108c SS |
1065 | struct symbol *fn; |
1066 | struct symbol *sfn; | |
6c4486e6 | 1067 | int async_exec; |
c5aa993b | 1068 | |
c906108c | 1069 | ERROR_NO_INFERIOR; |
573cda03 | 1070 | ensure_not_tfind_mode (); |
d729566a | 1071 | ensure_valid_thread (); |
94cc34af | 1072 | ensure_not_running (); |
c906108c | 1073 | |
1777feb0 | 1074 | /* Find out whether we must run in the background. */ |
6be9a197 TT |
1075 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec); |
1076 | arg = stripped.get (); | |
43ff13b4 | 1077 | |
8b88a78e | 1078 | prepare_execution_command (current_top_target (), async_exec); |
43ff13b4 | 1079 | |
c906108c | 1080 | if (!arg) |
e2e0b3e5 | 1081 | error_no_arg (_("starting address")); |
c906108c | 1082 | |
6c5b2ebe PA |
1083 | std::vector<symtab_and_line> sals |
1084 | = decode_line_with_last_displayed (arg, DECODE_LINE_FUNFIRSTLINE); | |
1085 | if (sals.size () != 1) | |
1086 | error (_("Unreasonable jump request")); | |
c906108c | 1087 | |
6c5b2ebe PA |
1088 | symtab_and_line &sal = sals[0]; |
1089 | ||
c906108c | 1090 | if (sal.symtab == 0 && sal.pc == 0) |
8a3fe4f8 | 1091 | error (_("No source file has been specified.")); |
c906108c | 1092 | |
1777feb0 | 1093 | resolve_sal_pc (&sal); /* May error out. */ |
c906108c | 1094 | |
1777feb0 | 1095 | /* See if we are trying to jump to another function. */ |
c906108c SS |
1096 | fn = get_frame_function (get_current_frame ()); |
1097 | sfn = find_pc_function (sal.pc); | |
1098 | if (fn != NULL && sfn != fn) | |
1099 | { | |
9e2f0ad4 | 1100 | if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal.line, |
987012b8 | 1101 | fn->print_name ())) |
c906108c | 1102 | { |
8a3fe4f8 | 1103 | error (_("Not confirmed.")); |
c906108c SS |
1104 | /* NOTREACHED */ |
1105 | } | |
1106 | } | |
1107 | ||
c5aa993b | 1108 | if (sfn != NULL) |
c906108c | 1109 | { |
253342b8 DE |
1110 | struct obj_section *section; |
1111 | ||
c906108c | 1112 | fixup_symbol_section (sfn, 0); |
08be3fe3 | 1113 | section = SYMBOL_OBJ_SECTION (symbol_objfile (sfn), sfn); |
253342b8 DE |
1114 | if (section_is_overlay (section) |
1115 | && !section_is_mapped (section)) | |
c906108c | 1116 | { |
3e43a32a MS |
1117 | if (!query (_("WARNING!!! Destination is in " |
1118 | "unmapped overlay! Jump anyway? "))) | |
c906108c | 1119 | { |
8a3fe4f8 | 1120 | error (_("Not confirmed.")); |
c906108c SS |
1121 | /* NOTREACHED */ |
1122 | } | |
1123 | } | |
1124 | } | |
1125 | ||
c906108c SS |
1126 | addr = sal.pc; |
1127 | ||
1128 | if (from_tty) | |
1129 | { | |
a3f17187 | 1130 | printf_filtered (_("Continuing at ")); |
5af949e3 | 1131 | fputs_filtered (paddress (gdbarch, addr), gdb_stdout); |
c906108c SS |
1132 | printf_filtered (".\n"); |
1133 | } | |
1134 | ||
70509625 | 1135 | clear_proceed_status (0); |
64ce06e4 | 1136 | proceed (addr, GDB_SIGNAL_0); |
c906108c | 1137 | } |
c906108c | 1138 | \f |
c906108c SS |
1139 | /* Continue program giving it specified signal. */ |
1140 | ||
1141 | static void | |
0b39b52e | 1142 | signal_command (const char *signum_exp, int from_tty) |
c906108c | 1143 | { |
2ea28649 | 1144 | enum gdb_signal oursig; |
6c4486e6 | 1145 | int async_exec; |
c906108c SS |
1146 | |
1147 | dont_repeat (); /* Too dangerous. */ | |
1148 | ERROR_NO_INFERIOR; | |
573cda03 | 1149 | ensure_not_tfind_mode (); |
d729566a | 1150 | ensure_valid_thread (); |
94cc34af | 1151 | ensure_not_running (); |
c906108c | 1152 | |
32c1e744 | 1153 | /* Find out whether we must run in the background. */ |
6be9a197 TT |
1154 | gdb::unique_xmalloc_ptr<char> stripped |
1155 | = strip_bg_char (signum_exp, &async_exec); | |
1156 | signum_exp = stripped.get (); | |
32c1e744 | 1157 | |
8b88a78e | 1158 | prepare_execution_command (current_top_target (), async_exec); |
32c1e744 | 1159 | |
c906108c | 1160 | if (!signum_exp) |
e2e0b3e5 | 1161 | error_no_arg (_("signal number")); |
c906108c SS |
1162 | |
1163 | /* It would be even slicker to make signal names be valid expressions, | |
1164 | (the type could be "enum $signal" or some such), then the user could | |
1165 | assign them to convenience variables. */ | |
2ea28649 | 1166 | oursig = gdb_signal_from_name (signum_exp); |
c906108c | 1167 | |
a493e3e2 | 1168 | if (oursig == GDB_SIGNAL_UNKNOWN) |
c906108c SS |
1169 | { |
1170 | /* No, try numeric. */ | |
bb518678 | 1171 | int num = parse_and_eval_long (signum_exp); |
c906108c SS |
1172 | |
1173 | if (num == 0) | |
a493e3e2 | 1174 | oursig = GDB_SIGNAL_0; |
c906108c | 1175 | else |
2ea28649 | 1176 | oursig = gdb_signal_from_command (num); |
c906108c SS |
1177 | } |
1178 | ||
70509625 PA |
1179 | /* Look for threads other than the current that this command ends up |
1180 | resuming too (due to schedlock off), and warn if they'll get a | |
1181 | signal delivered. "signal 0" is used to suppress a previous | |
1182 | signal, but if the current thread is no longer the one that got | |
1183 | the signal, then the user is potentially suppressing the signal | |
1184 | of the wrong thread. */ | |
1185 | if (!non_stop) | |
1186 | { | |
70509625 PA |
1187 | int must_confirm = 0; |
1188 | ||
1189 | /* This indicates what will be resumed. Either a single thread, | |
1190 | a whole process, or all threads of all processes. */ | |
08036331 | 1191 | ptid_t resume_ptid = user_visible_resume_ptid (0); |
5b6d1e4f PA |
1192 | process_stratum_target *resume_target |
1193 | = user_visible_resume_target (resume_ptid); | |
70509625 | 1194 | |
5b6d1e4f PA |
1195 | thread_info *current = inferior_thread (); |
1196 | ||
1197 | for (thread_info *tp : all_non_exited_threads (resume_target, resume_ptid)) | |
70509625 | 1198 | { |
5b6d1e4f | 1199 | if (tp == current) |
70509625 | 1200 | continue; |
70509625 PA |
1201 | |
1202 | if (tp->suspend.stop_signal != GDB_SIGNAL_0 | |
1203 | && signal_pass_state (tp->suspend.stop_signal)) | |
1204 | { | |
1205 | if (!must_confirm) | |
1206 | printf_unfiltered (_("Note:\n")); | |
43792cf0 PA |
1207 | printf_unfiltered (_(" Thread %s previously stopped with signal %s, %s.\n"), |
1208 | print_thread_id (tp), | |
70509625 PA |
1209 | gdb_signal_to_name (tp->suspend.stop_signal), |
1210 | gdb_signal_to_string (tp->suspend.stop_signal)); | |
1211 | must_confirm = 1; | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | if (must_confirm | |
43792cf0 | 1216 | && !query (_("Continuing thread %s (the current thread) with specified signal will\n" |
70509625 PA |
1217 | "still deliver the signals noted above to their respective threads.\n" |
1218 | "Continue anyway? "), | |
43792cf0 | 1219 | print_thread_id (inferior_thread ()))) |
70509625 PA |
1220 | error (_("Not confirmed.")); |
1221 | } | |
1222 | ||
c906108c SS |
1223 | if (from_tty) |
1224 | { | |
a493e3e2 | 1225 | if (oursig == GDB_SIGNAL_0) |
a3f17187 | 1226 | printf_filtered (_("Continuing with no signal.\n")); |
c906108c | 1227 | else |
a3f17187 | 1228 | printf_filtered (_("Continuing with signal %s.\n"), |
2ea28649 | 1229 | gdb_signal_to_name (oursig)); |
c906108c SS |
1230 | } |
1231 | ||
70509625 | 1232 | clear_proceed_status (0); |
64ce06e4 | 1233 | proceed ((CORE_ADDR) -1, oursig); |
c906108c SS |
1234 | } |
1235 | ||
81219e53 DE |
1236 | /* Queue a signal to be delivered to the current thread. */ |
1237 | ||
1238 | static void | |
0b39b52e | 1239 | queue_signal_command (const char *signum_exp, int from_tty) |
81219e53 DE |
1240 | { |
1241 | enum gdb_signal oursig; | |
1242 | struct thread_info *tp; | |
1243 | ||
1244 | ERROR_NO_INFERIOR; | |
1245 | ensure_not_tfind_mode (); | |
1246 | ensure_valid_thread (); | |
1247 | ensure_not_running (); | |
1248 | ||
1249 | if (signum_exp == NULL) | |
1250 | error_no_arg (_("signal number")); | |
1251 | ||
1252 | /* It would be even slicker to make signal names be valid expressions, | |
1253 | (the type could be "enum $signal" or some such), then the user could | |
1254 | assign them to convenience variables. */ | |
1255 | oursig = gdb_signal_from_name (signum_exp); | |
1256 | ||
1257 | if (oursig == GDB_SIGNAL_UNKNOWN) | |
1258 | { | |
1259 | /* No, try numeric. */ | |
1260 | int num = parse_and_eval_long (signum_exp); | |
1261 | ||
1262 | if (num == 0) | |
1263 | oursig = GDB_SIGNAL_0; | |
1264 | else | |
1265 | oursig = gdb_signal_from_command (num); | |
1266 | } | |
1267 | ||
1268 | if (oursig != GDB_SIGNAL_0 | |
1269 | && !signal_pass_state (oursig)) | |
1270 | error (_("Signal handling set to not pass this signal to the program.")); | |
1271 | ||
1272 | tp = inferior_thread (); | |
1273 | tp->suspend.stop_signal = oursig; | |
1274 | } | |
1275 | ||
cfc31633 PA |
1276 | /* Data for the FSM that manages the until (with no argument) |
1277 | command. */ | |
1278 | ||
46e3ed7f | 1279 | struct until_next_fsm : public thread_fsm |
fa4cd53f | 1280 | { |
cfc31633 | 1281 | /* The thread that as current when the command was executed. */ |
fa4cd53f | 1282 | int thread; |
cfc31633 | 1283 | |
46e3ed7f TT |
1284 | until_next_fsm (struct interp *cmd_interp, int thread) |
1285 | : thread_fsm (cmd_interp), | |
1286 | thread (thread) | |
1287 | { | |
1288 | } | |
cfc31633 | 1289 | |
46e3ed7f TT |
1290 | bool should_stop (struct thread_info *thread) override; |
1291 | void clean_up (struct thread_info *thread) override; | |
1292 | enum async_reply_reason do_async_reply_reason () override; | |
cfc31633 PA |
1293 | }; |
1294 | ||
cfc31633 PA |
1295 | /* Implementation of the 'should_stop' FSM method for the until (with |
1296 | no arg) command. */ | |
1297 | ||
46e3ed7f TT |
1298 | bool |
1299 | until_next_fsm::should_stop (struct thread_info *tp) | |
cfc31633 | 1300 | { |
cfc31633 | 1301 | if (tp->control.stop_step) |
46e3ed7f | 1302 | set_finished (); |
cfc31633 | 1303 | |
46e3ed7f | 1304 | return true; |
cfc31633 PA |
1305 | } |
1306 | ||
1307 | /* Implementation of the 'clean_up' FSM method for the until (with no | |
1308 | arg) command. */ | |
186c406b | 1309 | |
46e3ed7f TT |
1310 | void |
1311 | until_next_fsm::clean_up (struct thread_info *thread) | |
186c406b | 1312 | { |
8980e177 | 1313 | delete_longjmp_breakpoint (thread->global_num); |
cfc31633 PA |
1314 | } |
1315 | ||
1316 | /* Implementation of the 'async_reply_reason' FSM method for the until | |
1317 | (with no arg) command. */ | |
1318 | ||
46e3ed7f TT |
1319 | enum async_reply_reason |
1320 | until_next_fsm::do_async_reply_reason () | |
cfc31633 PA |
1321 | { |
1322 | return EXEC_ASYNC_END_STEPPING_RANGE; | |
186c406b TT |
1323 | } |
1324 | ||
c906108c SS |
1325 | /* Proceed until we reach a different source line with pc greater than |
1326 | our current one or exit the function. We skip calls in both cases. | |
1327 | ||
1328 | Note that eventually this command should probably be changed so | |
1329 | that only source lines are printed out when we hit the breakpoint | |
1330 | we set. This may involve changes to wait_for_inferior and the | |
1331 | proceed status code. */ | |
1332 | ||
c906108c | 1333 | static void |
fba45db2 | 1334 | until_next_command (int from_tty) |
c906108c SS |
1335 | { |
1336 | struct frame_info *frame; | |
1337 | CORE_ADDR pc; | |
1338 | struct symbol *func; | |
1339 | struct symtab_and_line sal; | |
4e1c45ea | 1340 | struct thread_info *tp = inferior_thread (); |
5d5658a1 | 1341 | int thread = tp->global_num; |
cfc31633 | 1342 | struct until_next_fsm *sm; |
c5aa993b | 1343 | |
70509625 | 1344 | clear_proceed_status (0); |
29734269 | 1345 | set_step_frame (tp); |
c906108c SS |
1346 | |
1347 | frame = get_current_frame (); | |
1348 | ||
1349 | /* Step until either exited from this function or greater | |
1350 | than the current line (if in symbolic section) or pc (if | |
1777feb0 | 1351 | not). */ |
c906108c | 1352 | |
1c7819ef | 1353 | pc = get_frame_pc (frame); |
c906108c | 1354 | func = find_pc_function (pc); |
c5aa993b | 1355 | |
c906108c SS |
1356 | if (!func) |
1357 | { | |
7cbd4a93 | 1358 | struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (pc); |
c5aa993b | 1359 | |
7cbd4a93 | 1360 | if (msymbol.minsym == NULL) |
8a3fe4f8 | 1361 | error (_("Execution is not within a known function.")); |
c5aa993b | 1362 | |
77e371c0 | 1363 | tp->control.step_range_start = BMSYMBOL_VALUE_ADDRESS (msymbol); |
7e09a223 YQ |
1364 | /* The upper-bound of step_range is exclusive. In order to make PC |
1365 | within the range, set the step_range_end with PC + 1. */ | |
1366 | tp->control.step_range_end = pc + 1; | |
c906108c SS |
1367 | } |
1368 | else | |
1369 | { | |
1370 | sal = find_pc_line (pc, 0); | |
c5aa993b | 1371 | |
2b1ffcfd | 1372 | tp->control.step_range_start = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func)); |
16c381f0 | 1373 | tp->control.step_range_end = sal.end; |
c906108c | 1374 | } |
c1e36e3e | 1375 | tp->control.may_range_step = 1; |
c5aa993b | 1376 | |
16c381f0 | 1377 | tp->control.step_over_calls = STEP_OVER_ALL; |
c906108c | 1378 | |
186c406b | 1379 | set_longjmp_breakpoint (tp, get_frame_id (frame)); |
5419bdae | 1380 | delete_longjmp_breakpoint_cleanup lj_deleter (thread); |
186c406b | 1381 | |
46e3ed7f TT |
1382 | sm = new until_next_fsm (command_interp (), tp->global_num); |
1383 | tp->thread_fsm = sm; | |
5419bdae | 1384 | lj_deleter.release (); |
fa4cd53f | 1385 | |
cfc31633 | 1386 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
c906108c SS |
1387 | } |
1388 | ||
c5aa993b | 1389 | static void |
0b39b52e | 1390 | until_command (const char *arg, int from_tty) |
c906108c | 1391 | { |
6c4486e6 | 1392 | int async_exec; |
43ff13b4 | 1393 | |
4247603b | 1394 | ERROR_NO_INFERIOR; |
573cda03 | 1395 | ensure_not_tfind_mode (); |
4247603b PA |
1396 | ensure_valid_thread (); |
1397 | ensure_not_running (); | |
573cda03 | 1398 | |
1777feb0 | 1399 | /* Find out whether we must run in the background. */ |
6be9a197 TT |
1400 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec); |
1401 | arg = stripped.get (); | |
43ff13b4 | 1402 | |
8b88a78e | 1403 | prepare_execution_command (current_top_target (), async_exec); |
43ff13b4 | 1404 | |
c906108c | 1405 | if (arg) |
ae66c1fc | 1406 | until_break_command (arg, from_tty, 0); |
c906108c SS |
1407 | else |
1408 | until_next_command (from_tty); | |
1409 | } | |
ae66c1fc EZ |
1410 | |
1411 | static void | |
0b39b52e | 1412 | advance_command (const char *arg, int from_tty) |
ae66c1fc | 1413 | { |
6c4486e6 | 1414 | int async_exec; |
ae66c1fc | 1415 | |
4247603b | 1416 | ERROR_NO_INFERIOR; |
573cda03 | 1417 | ensure_not_tfind_mode (); |
4247603b PA |
1418 | ensure_valid_thread (); |
1419 | ensure_not_running (); | |
573cda03 | 1420 | |
ae66c1fc | 1421 | if (arg == NULL) |
e2e0b3e5 | 1422 | error_no_arg (_("a location")); |
ae66c1fc EZ |
1423 | |
1424 | /* Find out whether we must run in the background. */ | |
6be9a197 TT |
1425 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec); |
1426 | arg = stripped.get (); | |
ae66c1fc | 1427 | |
8b88a78e | 1428 | prepare_execution_command (current_top_target (), async_exec); |
ae66c1fc EZ |
1429 | |
1430 | until_break_command (arg, from_tty, 1); | |
1431 | } | |
c906108c | 1432 | \f |
cc72b2a2 | 1433 | /* Return the value of the result of a function at the end of a 'finish' |
8a6c4031 JK |
1434 | command/BP. DTOR_DATA (if not NULL) can represent inferior registers |
1435 | right after an inferior call has finished. */ | |
f941662f | 1436 | |
cc72b2a2 | 1437 | struct value * |
0700e23e | 1438 | get_return_value (struct value *function, struct type *value_type) |
11cf8741 | 1439 | { |
215c69dc YQ |
1440 | regcache *stop_regs = get_current_regcache (); |
1441 | struct gdbarch *gdbarch = stop_regs->arch (); | |
44e5158b AC |
1442 | struct value *value; |
1443 | ||
f168693b | 1444 | value_type = check_typedef (value_type); |
78134374 | 1445 | gdb_assert (value_type->code () != TYPE_CODE_VOID); |
11cf8741 | 1446 | |
92ad9cd9 AC |
1447 | /* FIXME: 2003-09-27: When returning from a nested inferior function |
1448 | call, it's possible (with no help from the architecture vector) | |
1449 | to locate and return/print a "struct return" value. This is just | |
7a9dd1b2 | 1450 | a more complicated case of what is already being done in the |
92ad9cd9 AC |
1451 | inferior function call code. In fact, when inferior function |
1452 | calls are made async, this will likely be made the norm. */ | |
31db7b6c | 1453 | |
6a3a010b | 1454 | switch (gdbarch_return_value (gdbarch, function, value_type, |
c055b101 | 1455 | NULL, NULL, NULL)) |
44e5158b | 1456 | { |
750eb019 AC |
1457 | case RETURN_VALUE_REGISTER_CONVENTION: |
1458 | case RETURN_VALUE_ABI_RETURNS_ADDRESS: | |
181fc57c | 1459 | case RETURN_VALUE_ABI_PRESERVES_ADDRESS: |
44e5158b | 1460 | value = allocate_value (value_type); |
215c69dc | 1461 | gdbarch_return_value (gdbarch, function, value_type, stop_regs, |
990a07ab | 1462 | value_contents_raw (value), NULL); |
750eb019 AC |
1463 | break; |
1464 | case RETURN_VALUE_STRUCT_CONVENTION: | |
1465 | value = NULL; | |
1466 | break; | |
1467 | default: | |
e2e0b3e5 | 1468 | internal_error (__FILE__, __LINE__, _("bad switch")); |
44e5158b | 1469 | } |
bb472c1e | 1470 | |
cc72b2a2 KP |
1471 | return value; |
1472 | } | |
1473 | ||
243a9253 PA |
1474 | /* The captured function return value/type and its position in the |
1475 | value history. */ | |
cc72b2a2 | 1476 | |
243a9253 | 1477 | struct return_value_info |
cc72b2a2 | 1478 | { |
243a9253 PA |
1479 | /* The captured return value. May be NULL if we weren't able to |
1480 | retrieve it. See get_return_value. */ | |
1481 | struct value *value; | |
1482 | ||
1483 | /* The return type. In some cases, we'll not be able extract the | |
1484 | return value, but we always know the type. */ | |
1485 | struct type *type; | |
1486 | ||
1487 | /* If we captured a value, this is the value history index. */ | |
1488 | int value_history_index; | |
1489 | }; | |
1490 | ||
1491 | /* Helper for print_return_value. */ | |
cc72b2a2 | 1492 | |
243a9253 PA |
1493 | static void |
1494 | print_return_value_1 (struct ui_out *uiout, struct return_value_info *rv) | |
1495 | { | |
1496 | if (rv->value != NULL) | |
31db7b6c | 1497 | { |
79a45b7d TT |
1498 | struct value_print_options opts; |
1499 | ||
31db7b6c | 1500 | /* Print it. */ |
112e8700 SM |
1501 | uiout->text ("Value returned is "); |
1502 | uiout->field_fmt ("gdb-result-var", "$%d", | |
d7e74731 | 1503 | rv->value_history_index); |
112e8700 | 1504 | uiout->text (" = "); |
5d9a0608 | 1505 | get_user_print_options (&opts); |
d7e74731 | 1506 | |
000439d5 TT |
1507 | if (opts.finish_print) |
1508 | { | |
1509 | string_file stb; | |
1510 | value_print (rv->value, &stb, &opts); | |
1511 | uiout->field_stream ("return-value", stb); | |
1512 | } | |
1513 | else | |
7f6aba03 TT |
1514 | uiout->field_string ("return-value", _("<not displayed>"), |
1515 | metadata_style.style ()); | |
112e8700 | 1516 | uiout->text ("\n"); |
31db7b6c MK |
1517 | } |
1518 | else | |
1519 | { | |
2f408ecb | 1520 | std::string type_name = type_to_string (rv->type); |
112e8700 SM |
1521 | uiout->text ("Value returned has type: "); |
1522 | uiout->field_string ("return-type", type_name.c_str ()); | |
1523 | uiout->text ("."); | |
1524 | uiout->text (" Cannot determine contents\n"); | |
31db7b6c | 1525 | } |
11cf8741 JM |
1526 | } |
1527 | ||
243a9253 PA |
1528 | /* Print the result of a function at the end of a 'finish' command. |
1529 | RV points at an object representing the captured return value/type | |
1530 | and its position in the value history. */ | |
1531 | ||
1532 | void | |
1533 | print_return_value (struct ui_out *uiout, struct return_value_info *rv) | |
1534 | { | |
f097f5ad | 1535 | if (rv->type == NULL |
78134374 | 1536 | || check_typedef (rv->type)->code () == TYPE_CODE_VOID) |
243a9253 PA |
1537 | return; |
1538 | ||
a70b8144 | 1539 | try |
243a9253 PA |
1540 | { |
1541 | /* print_return_value_1 can throw an exception in some | |
1542 | circumstances. We need to catch this so that we still | |
1543 | delete the breakpoint. */ | |
1544 | print_return_value_1 (uiout, rv); | |
1545 | } | |
230d2906 | 1546 | catch (const gdb_exception &ex) |
243a9253 PA |
1547 | { |
1548 | exception_print (gdb_stdout, ex); | |
1549 | } | |
243a9253 PA |
1550 | } |
1551 | ||
1552 | /* Data for the FSM that manages the finish command. */ | |
f941662f | 1553 | |
46e3ed7f | 1554 | struct finish_command_fsm : public thread_fsm |
43ff13b4 | 1555 | { |
243a9253 PA |
1556 | /* The momentary breakpoint set at the function's return address in |
1557 | the caller. */ | |
46e3ed7f | 1558 | breakpoint_up breakpoint; |
243a9253 PA |
1559 | |
1560 | /* The function that we're stepping out of. */ | |
46e3ed7f | 1561 | struct symbol *function = nullptr; |
8a6c4031 | 1562 | |
243a9253 PA |
1563 | /* If the FSM finishes successfully, this stores the function's |
1564 | return value. */ | |
46e3ed7f | 1565 | struct return_value_info return_value_info {}; |
c5aa993b | 1566 | |
46e3ed7f TT |
1567 | explicit finish_command_fsm (struct interp *cmd_interp) |
1568 | : thread_fsm (cmd_interp) | |
1569 | { | |
1570 | } | |
243a9253 | 1571 | |
46e3ed7f TT |
1572 | bool should_stop (struct thread_info *thread) override; |
1573 | void clean_up (struct thread_info *thread) override; | |
1574 | struct return_value_info *return_value () override; | |
1575 | enum async_reply_reason do_async_reply_reason () override; | |
1576 | }; | |
347bddb7 | 1577 | |
243a9253 PA |
1578 | /* Implementation of the 'should_stop' FSM method for the finish |
1579 | commands. Detects whether the thread stepped out of the function | |
1580 | successfully, and if so, captures the function's return value and | |
1581 | marks the FSM finished. */ | |
1582 | ||
46e3ed7f TT |
1583 | bool |
1584 | finish_command_fsm::should_stop (struct thread_info *tp) | |
243a9253 | 1585 | { |
46e3ed7f | 1586 | struct return_value_info *rv = &return_value_info; |
243a9253 | 1587 | |
46e3ed7f | 1588 | if (function != NULL |
243a9253 | 1589 | && bpstat_find_breakpoint (tp->control.stop_bpstat, |
46e3ed7f | 1590 | breakpoint.get ()) != NULL) |
43ff13b4 | 1591 | { |
243a9253 | 1592 | /* We're done. */ |
46e3ed7f | 1593 | set_finished (); |
bfec99b2 | 1594 | |
46e3ed7f | 1595 | rv->type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function)); |
243a9253 PA |
1596 | if (rv->type == NULL) |
1597 | internal_error (__FILE__, __LINE__, | |
1598 | _("finish_command: function has no target type")); | |
f5871ec0 | 1599 | |
78134374 | 1600 | if (check_typedef (rv->type)->code () != TYPE_CODE_VOID) |
40c549d6 | 1601 | { |
243a9253 PA |
1602 | struct value *func; |
1603 | ||
46e3ed7f | 1604 | func = read_var_value (function, NULL, get_current_frame ()); |
0700e23e | 1605 | rv->value = get_return_value (func, rv->type); |
aca20ec4 KB |
1606 | if (rv->value != NULL) |
1607 | rv->value_history_index = record_latest_value (rv->value); | |
243a9253 PA |
1608 | } |
1609 | } | |
1610 | else if (tp->control.stop_step) | |
1611 | { | |
1612 | /* Finishing from an inline frame, or reverse finishing. In | |
1613 | either case, there's no way to retrieve the return value. */ | |
46e3ed7f | 1614 | set_finished (); |
243a9253 | 1615 | } |
fa4cd53f | 1616 | |
46e3ed7f | 1617 | return true; |
243a9253 | 1618 | } |
40c549d6 | 1619 | |
243a9253 PA |
1620 | /* Implementation of the 'clean_up' FSM method for the finish |
1621 | commands. */ | |
fa4cd53f | 1622 | |
46e3ed7f TT |
1623 | void |
1624 | finish_command_fsm::clean_up (struct thread_info *thread) | |
243a9253 | 1625 | { |
46e3ed7f | 1626 | breakpoint.reset (); |
8980e177 | 1627 | delete_longjmp_breakpoint (thread->global_num); |
243a9253 | 1628 | } |
f941662f | 1629 | |
243a9253 PA |
1630 | /* Implementation of the 'return_value' FSM method for the finish |
1631 | commands. */ | |
1632 | ||
46e3ed7f TT |
1633 | struct return_value_info * |
1634 | finish_command_fsm::return_value () | |
243a9253 | 1635 | { |
46e3ed7f | 1636 | return &return_value_info; |
43ff13b4 JM |
1637 | } |
1638 | ||
243a9253 PA |
1639 | /* Implementation of the 'async_reply_reason' FSM method for the |
1640 | finish commands. */ | |
1641 | ||
46e3ed7f TT |
1642 | enum async_reply_reason |
1643 | finish_command_fsm::do_async_reply_reason () | |
604ead4a | 1644 | { |
243a9253 PA |
1645 | if (execution_direction == EXEC_REVERSE) |
1646 | return EXEC_ASYNC_END_STEPPING_RANGE; | |
1647 | else | |
1648 | return EXEC_ASYNC_FUNCTION_FINISHED; | |
604ead4a PA |
1649 | } |
1650 | ||
b2175913 MS |
1651 | /* finish_backward -- helper function for finish_command. */ |
1652 | ||
1653 | static void | |
243a9253 | 1654 | finish_backward (struct finish_command_fsm *sm) |
b2175913 MS |
1655 | { |
1656 | struct symtab_and_line sal; | |
1657 | struct thread_info *tp = inferior_thread (); | |
1c7819ef | 1658 | CORE_ADDR pc; |
b2175913 | 1659 | CORE_ADDR func_addr; |
b2175913 | 1660 | |
1c7819ef PA |
1661 | pc = get_frame_pc (get_current_frame ()); |
1662 | ||
1663 | if (find_pc_partial_function (pc, NULL, &func_addr, NULL) == 0) | |
1f267ae3 | 1664 | error (_("Cannot find bounds of current function")); |
b2175913 MS |
1665 | |
1666 | sal = find_pc_line (func_addr, 0); | |
1667 | ||
9da8c2a0 | 1668 | tp->control.proceed_to_finish = 1; |
b2175913 MS |
1669 | /* Special case: if we're sitting at the function entry point, |
1670 | then all we need to do is take a reverse singlestep. We | |
1671 | don't need to set a breakpoint, and indeed it would do us | |
1672 | no good to do so. | |
1673 | ||
1674 | Note that this can only happen at frame #0, since there's | |
1675 | no way that a function up the stack can have a return address | |
1676 | that's equal to its entry point. */ | |
1677 | ||
1c7819ef | 1678 | if (sal.pc != pc) |
b2175913 | 1679 | { |
a6d9a66e UW |
1680 | struct frame_info *frame = get_selected_frame (NULL); |
1681 | struct gdbarch *gdbarch = get_frame_arch (frame); | |
9da8c2a0 PA |
1682 | |
1683 | /* Set a step-resume at the function's entry point. Once that's | |
1684 | hit, we'll do one more step backwards. */ | |
51abb421 | 1685 | symtab_and_line sr_sal; |
9da8c2a0 PA |
1686 | sr_sal.pc = sal.pc; |
1687 | sr_sal.pspace = get_frame_program_space (frame); | |
1688 | insert_step_resume_breakpoint_at_sal (gdbarch, | |
1689 | sr_sal, null_frame_id); | |
a6d9a66e | 1690 | |
64ce06e4 | 1691 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
b2175913 MS |
1692 | } |
1693 | else | |
b2175913 | 1694 | { |
9da8c2a0 PA |
1695 | /* We're almost there -- we just need to back up by one more |
1696 | single-step. */ | |
16c381f0 | 1697 | tp->control.step_range_start = tp->control.step_range_end = 1; |
64ce06e4 | 1698 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
b2175913 | 1699 | } |
b2175913 MS |
1700 | } |
1701 | ||
243a9253 PA |
1702 | /* finish_forward -- helper function for finish_command. FRAME is the |
1703 | frame that called the function we're about to step out of. */ | |
b2175913 MS |
1704 | |
1705 | static void | |
243a9253 | 1706 | finish_forward (struct finish_command_fsm *sm, struct frame_info *frame) |
b2175913 | 1707 | { |
def166f6 | 1708 | struct frame_id frame_id = get_frame_id (frame); |
a6d9a66e | 1709 | struct gdbarch *gdbarch = get_frame_arch (frame); |
b2175913 MS |
1710 | struct symtab_and_line sal; |
1711 | struct thread_info *tp = inferior_thread (); | |
b2175913 MS |
1712 | |
1713 | sal = find_pc_line (get_frame_pc (frame), 0); | |
1714 | sal.pc = get_frame_pc (frame); | |
1715 | ||
243a9253 PA |
1716 | sm->breakpoint = set_momentary_breakpoint (gdbarch, sal, |
1717 | get_stack_frame_id (frame), | |
46e3ed7f | 1718 | bp_finish); |
b2175913 | 1719 | |
c70a6932 JK |
1720 | /* set_momentary_breakpoint invalidates FRAME. */ |
1721 | frame = NULL; | |
1722 | ||
def166f6 | 1723 | set_longjmp_breakpoint (tp, frame_id); |
186c406b | 1724 | |
46c03469 | 1725 | /* We want to print return value, please... */ |
16c381f0 | 1726 | tp->control.proceed_to_finish = 1; |
b2175913 | 1727 | |
243a9253 | 1728 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
b2175913 MS |
1729 | } |
1730 | ||
e3b5daf9 MM |
1731 | /* Skip frames for "finish". */ |
1732 | ||
1733 | static struct frame_info * | |
1734 | skip_finish_frames (struct frame_info *frame) | |
1735 | { | |
1736 | struct frame_info *start; | |
1737 | ||
1738 | do | |
1739 | { | |
1740 | start = frame; | |
1741 | ||
1742 | frame = skip_tailcall_frames (frame); | |
1743 | if (frame == NULL) | |
1744 | break; | |
1745 | ||
1746 | frame = skip_unwritable_frames (frame); | |
1747 | if (frame == NULL) | |
1748 | break; | |
1749 | } | |
1750 | while (start != frame); | |
1751 | ||
1752 | return frame; | |
1753 | } | |
1754 | ||
f941662f MK |
1755 | /* "finish": Set a temporary breakpoint at the place the selected |
1756 | frame will return to, then continue. */ | |
c906108c SS |
1757 | |
1758 | static void | |
0b39b52e | 1759 | finish_command (const char *arg, int from_tty) |
c906108c | 1760 | { |
52f0bd74 | 1761 | struct frame_info *frame; |
6c4486e6 | 1762 | int async_exec; |
243a9253 PA |
1763 | struct finish_command_fsm *sm; |
1764 | struct thread_info *tp; | |
43ff13b4 | 1765 | |
4247603b | 1766 | ERROR_NO_INFERIOR; |
573cda03 | 1767 | ensure_not_tfind_mode (); |
4247603b PA |
1768 | ensure_valid_thread (); |
1769 | ensure_not_running (); | |
573cda03 | 1770 | |
f941662f | 1771 | /* Find out whether we must run in the background. */ |
6be9a197 TT |
1772 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec); |
1773 | arg = stripped.get (); | |
43ff13b4 | 1774 | |
8b88a78e | 1775 | prepare_execution_command (current_top_target (), async_exec); |
c906108c SS |
1776 | |
1777 | if (arg) | |
8a3fe4f8 | 1778 | error (_("The \"finish\" command does not take any arguments.")); |
c906108c | 1779 | |
206415a3 | 1780 | frame = get_prev_frame (get_selected_frame (_("No selected frame."))); |
c906108c | 1781 | if (frame == 0) |
8a3fe4f8 | 1782 | error (_("\"finish\" not meaningful in the outermost frame.")); |
c906108c | 1783 | |
70509625 | 1784 | clear_proceed_status (0); |
c906108c | 1785 | |
243a9253 PA |
1786 | tp = inferior_thread (); |
1787 | ||
46e3ed7f | 1788 | sm = new finish_command_fsm (command_interp ()); |
243a9253 | 1789 | |
46e3ed7f | 1790 | tp->thread_fsm = sm; |
243a9253 | 1791 | |
edb3359d | 1792 | /* Finishing from an inline frame is completely different. We don't |
243a9253 | 1793 | try to show the "return value" - no way to locate it. */ |
edb3359d DJ |
1794 | if (get_frame_type (get_selected_frame (_("No selected frame."))) |
1795 | == INLINE_FRAME) | |
1796 | { | |
1797 | /* Claim we are stepping in the calling frame. An empty step | |
1798 | range means that we will stop once we aren't in a function | |
1799 | called by that frame. We don't use the magic "1" value for | |
1800 | step_range_end, because then infrun will think this is nexti, | |
1801 | and not step over the rest of this inlined function call. */ | |
29734269 | 1802 | set_step_info (tp, frame, {}); |
16c381f0 JK |
1803 | tp->control.step_range_start = get_frame_pc (frame); |
1804 | tp->control.step_range_end = tp->control.step_range_start; | |
1805 | tp->control.step_over_calls = STEP_OVER_ALL; | |
edb3359d DJ |
1806 | |
1807 | /* Print info on the selected frame, including level number but not | |
1808 | source. */ | |
1809 | if (from_tty) | |
1810 | { | |
1811 | printf_filtered (_("Run till exit from ")); | |
08d72866 | 1812 | print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0); |
edb3359d DJ |
1813 | } |
1814 | ||
64ce06e4 | 1815 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
edb3359d DJ |
1816 | return; |
1817 | } | |
1818 | ||
c906108c SS |
1819 | /* Find the function we will return from. */ |
1820 | ||
243a9253 | 1821 | sm->function = find_pc_function (get_frame_pc (get_selected_frame (NULL))); |
c906108c | 1822 | |
f941662f MK |
1823 | /* Print info on the selected frame, including level number but not |
1824 | source. */ | |
c906108c SS |
1825 | if (from_tty) |
1826 | { | |
b2175913 MS |
1827 | if (execution_direction == EXEC_REVERSE) |
1828 | printf_filtered (_("Run back to call of ")); | |
1829 | else | |
743649fd | 1830 | { |
243a9253 | 1831 | if (sm->function != NULL && TYPE_NO_RETURN (sm->function->type) |
743649fd MW |
1832 | && !query (_("warning: Function %s does not return normally.\n" |
1833 | "Try to finish anyway? "), | |
987012b8 | 1834 | sm->function->print_name ())) |
743649fd MW |
1835 | error (_("Not confirmed.")); |
1836 | printf_filtered (_("Run till exit from ")); | |
1837 | } | |
b2175913 | 1838 | |
08d72866 | 1839 | print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0); |
c906108c SS |
1840 | } |
1841 | ||
b2175913 | 1842 | if (execution_direction == EXEC_REVERSE) |
243a9253 | 1843 | finish_backward (sm); |
b2175913 | 1844 | else |
33b4777c | 1845 | { |
e3b5daf9 | 1846 | frame = skip_finish_frames (frame); |
7eb89530 | 1847 | |
33b4777c MM |
1848 | if (frame == NULL) |
1849 | error (_("Cannot find the caller frame.")); | |
1850 | ||
1851 | finish_forward (sm, frame); | |
1852 | } | |
c906108c SS |
1853 | } |
1854 | \f | |
f941662f | 1855 | |
c906108c | 1856 | static void |
1d12d88f | 1857 | info_program_command (const char *args, int from_tty) |
c906108c | 1858 | { |
347bddb7 PA |
1859 | bpstat bs; |
1860 | int num, stat; | |
347bddb7 | 1861 | ptid_t ptid; |
5b6d1e4f | 1862 | process_stratum_target *proc_target; |
c5aa993b | 1863 | |
55f6301a | 1864 | if (!target_has_execution ()) |
c906108c | 1865 | { |
a3f17187 | 1866 | printf_filtered (_("The program being debugged is not being run.\n")); |
c906108c SS |
1867 | return; |
1868 | } | |
1869 | ||
347bddb7 | 1870 | if (non_stop) |
5b6d1e4f PA |
1871 | { |
1872 | ptid = inferior_ptid; | |
1873 | proc_target = current_inferior ()->process_target (); | |
1874 | } | |
347bddb7 | 1875 | else |
5b6d1e4f | 1876 | get_last_target_status (&proc_target, &ptid, nullptr); |
347bddb7 | 1877 | |
9e7f3bbb | 1878 | if (ptid == null_ptid || ptid == minus_one_ptid) |
00431a78 PA |
1879 | error (_("No selected thread.")); |
1880 | ||
5b6d1e4f | 1881 | thread_info *tp = find_thread_ptid (proc_target, ptid); |
00431a78 PA |
1882 | |
1883 | if (tp->state == THREAD_EXITED) | |
347bddb7 | 1884 | error (_("Invalid selected thread.")); |
00431a78 | 1885 | else if (tp->state == THREAD_RUNNING) |
347bddb7 PA |
1886 | error (_("Selected thread is running.")); |
1887 | ||
16c381f0 | 1888 | bs = tp->control.stop_bpstat; |
347bddb7 PA |
1889 | stat = bpstat_num (&bs, &num); |
1890 | ||
c906108c | 1891 | target_files_info (); |
5af949e3 | 1892 | printf_filtered (_("Program stopped at %s.\n"), |
f2ffa92b | 1893 | paddress (target_gdbarch (), tp->suspend.stop_pc)); |
16c381f0 | 1894 | if (tp->control.stop_step) |
a3f17187 | 1895 | printf_filtered (_("It stopped after being stepped.\n")); |
8671a17b | 1896 | else if (stat != 0) |
c906108c SS |
1897 | { |
1898 | /* There may be several breakpoints in the same place, so this | |
dda83cd7 | 1899 | isn't as strange as it seems. */ |
8671a17b | 1900 | while (stat != 0) |
c906108c | 1901 | { |
8671a17b | 1902 | if (stat < 0) |
c906108c | 1903 | { |
3e43a32a MS |
1904 | printf_filtered (_("It stopped at a breakpoint " |
1905 | "that has since been deleted.\n")); | |
c906108c SS |
1906 | } |
1907 | else | |
a3f17187 | 1908 | printf_filtered (_("It stopped at breakpoint %d.\n"), num); |
8671a17b | 1909 | stat = bpstat_num (&bs, &num); |
c906108c SS |
1910 | } |
1911 | } | |
a493e3e2 | 1912 | else if (tp->suspend.stop_signal != GDB_SIGNAL_0) |
c906108c | 1913 | { |
a3f17187 | 1914 | printf_filtered (_("It stopped with signal %s, %s.\n"), |
2ea28649 PA |
1915 | gdb_signal_to_name (tp->suspend.stop_signal), |
1916 | gdb_signal_to_string (tp->suspend.stop_signal)); | |
c906108c SS |
1917 | } |
1918 | ||
0d41ba00 | 1919 | if (from_tty) |
c906108c | 1920 | { |
3e43a32a MS |
1921 | printf_filtered (_("Type \"info stack\" or \"info " |
1922 | "registers\" for more information.\n")); | |
c906108c SS |
1923 | } |
1924 | } | |
1925 | \f | |
1926 | static void | |
69f476a3 | 1927 | environment_info (const char *var, int from_tty) |
c906108c SS |
1928 | { |
1929 | if (var) | |
1930 | { | |
9a6c7d9c | 1931 | const char *val = current_inferior ()->environment.get (var); |
abbb1732 | 1932 | |
c906108c SS |
1933 | if (val) |
1934 | { | |
1935 | puts_filtered (var); | |
1936 | puts_filtered (" = "); | |
1937 | puts_filtered (val); | |
1938 | puts_filtered ("\n"); | |
1939 | } | |
1940 | else | |
1941 | { | |
1942 | puts_filtered ("Environment variable \""); | |
1943 | puts_filtered (var); | |
1944 | puts_filtered ("\" not defined.\n"); | |
1945 | } | |
1946 | } | |
1947 | else | |
1948 | { | |
9a6c7d9c | 1949 | char **envp = current_inferior ()->environment.envp (); |
abbb1732 | 1950 | |
9a6c7d9c | 1951 | for (int idx = 0; envp[idx] != NULL; ++idx) |
c906108c | 1952 | { |
9a6c7d9c | 1953 | puts_filtered (envp[idx]); |
c906108c SS |
1954 | puts_filtered ("\n"); |
1955 | } | |
1956 | } | |
1957 | } | |
1958 | ||
1959 | static void | |
69f476a3 | 1960 | set_environment_command (const char *arg, int from_tty) |
c906108c | 1961 | { |
69f476a3 | 1962 | const char *p, *val; |
c906108c SS |
1963 | int nullset = 0; |
1964 | ||
1965 | if (arg == 0) | |
e2e0b3e5 | 1966 | error_no_arg (_("environment variable and value")); |
c906108c | 1967 | |
85102364 | 1968 | /* Find separation between variable name and value. */ |
c906108c SS |
1969 | p = (char *) strchr (arg, '='); |
1970 | val = (char *) strchr (arg, ' '); | |
1971 | ||
1972 | if (p != 0 && val != 0) | |
1973 | { | |
1974 | /* We have both a space and an equals. If the space is before the | |
dda83cd7 SM |
1975 | equals, walk forward over the spaces til we see a nonspace |
1976 | (possibly the equals). */ | |
c906108c SS |
1977 | if (p > val) |
1978 | while (*val == ' ') | |
1979 | val++; | |
1980 | ||
1981 | /* Now if the = is after the char following the spaces, | |
dda83cd7 | 1982 | take the char following the spaces. */ |
c906108c SS |
1983 | if (p > val) |
1984 | p = val - 1; | |
1985 | } | |
1986 | else if (val != 0 && p == 0) | |
1987 | p = val; | |
1988 | ||
1989 | if (p == arg) | |
e2e0b3e5 | 1990 | error_no_arg (_("environment variable to set")); |
c906108c SS |
1991 | |
1992 | if (p == 0 || p[1] == 0) | |
1993 | { | |
1994 | nullset = 1; | |
1995 | if (p == 0) | |
1777feb0 | 1996 | p = arg + strlen (arg); /* So that savestring below will work. */ |
c906108c SS |
1997 | } |
1998 | else | |
1999 | { | |
1777feb0 | 2000 | /* Not setting variable value to null. */ |
c906108c SS |
2001 | val = p + 1; |
2002 | while (*val == ' ' || *val == '\t') | |
2003 | val++; | |
2004 | } | |
2005 | ||
c5aa993b JM |
2006 | while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) |
2007 | p--; | |
c906108c | 2008 | |
69f476a3 | 2009 | std::string var (arg, p - arg); |
c906108c SS |
2010 | if (nullset) |
2011 | { | |
3e43a32a MS |
2012 | printf_filtered (_("Setting environment variable " |
2013 | "\"%s\" to null value.\n"), | |
69f476a3 TT |
2014 | var.c_str ()); |
2015 | current_inferior ()->environment.set (var.c_str (), ""); | |
c906108c SS |
2016 | } |
2017 | else | |
69f476a3 | 2018 | current_inferior ()->environment.set (var.c_str (), val); |
c906108c SS |
2019 | } |
2020 | ||
2021 | static void | |
69f476a3 | 2022 | unset_environment_command (const char *var, int from_tty) |
c906108c SS |
2023 | { |
2024 | if (var == 0) | |
2025 | { | |
2026 | /* If there is no argument, delete all environment variables. | |
dda83cd7 | 2027 | Ask for confirmation if reading from the terminal. */ |
e2e0b3e5 | 2028 | if (!from_tty || query (_("Delete all environment variables? "))) |
206726fb | 2029 | current_inferior ()->environment.clear (); |
c906108c SS |
2030 | } |
2031 | else | |
9a6c7d9c | 2032 | current_inferior ()->environment.unset (var); |
c906108c SS |
2033 | } |
2034 | ||
1777feb0 | 2035 | /* Handle the execution path (PATH variable). */ |
c906108c SS |
2036 | |
2037 | static const char path_var_name[] = "PATH"; | |
2038 | ||
c906108c | 2039 | static void |
69f476a3 | 2040 | path_info (const char *args, int from_tty) |
c906108c SS |
2041 | { |
2042 | puts_filtered ("Executable and object file path: "); | |
9a6c7d9c | 2043 | puts_filtered (current_inferior ()->environment.get (path_var_name)); |
c906108c SS |
2044 | puts_filtered ("\n"); |
2045 | } | |
2046 | ||
2047 | /* Add zero or more directories to the front of the execution path. */ | |
2048 | ||
2049 | static void | |
0b39b52e | 2050 | path_command (const char *dirname, int from_tty) |
c906108c SS |
2051 | { |
2052 | char *exec_path; | |
a121b7c1 | 2053 | const char *env; |
abbb1732 | 2054 | |
c906108c | 2055 | dont_repeat (); |
9a6c7d9c | 2056 | env = current_inferior ()->environment.get (path_var_name); |
1777feb0 | 2057 | /* Can be null if path is not set. */ |
c906108c SS |
2058 | if (!env) |
2059 | env = ""; | |
4fcf66da | 2060 | exec_path = xstrdup (env); |
c906108c | 2061 | mod_path (dirname, &exec_path); |
9a6c7d9c | 2062 | current_inferior ()->environment.set (path_var_name, exec_path); |
b8c9b27d | 2063 | xfree (exec_path); |
c906108c | 2064 | if (from_tty) |
cafb3438 | 2065 | path_info (NULL, from_tty); |
c906108c | 2066 | } |
c906108c | 2067 | \f |
c5aa993b | 2068 | |
e813d34a RK |
2069 | static void |
2070 | pad_to_column (string_file &stream, int col) | |
2071 | { | |
2072 | /* At least one space must be printed to separate columns. */ | |
2073 | stream.putc (' '); | |
2074 | const int size = stream.size (); | |
2075 | if (size < col) | |
2076 | stream.puts (n_spaces (col - size)); | |
2077 | } | |
2078 | ||
1292279a PA |
2079 | /* Print out the register NAME with value VAL, to FILE, in the default |
2080 | fashion. */ | |
2081 | ||
2082 | static void | |
2083 | default_print_one_register_info (struct ui_file *file, | |
2084 | const char *name, | |
2085 | struct value *val) | |
2086 | { | |
2087 | struct type *regtype = value_type (val); | |
f69d9aef | 2088 | int print_raw_format; |
e813d34a RK |
2089 | string_file format_stream; |
2090 | enum tab_stops | |
2091 | { | |
2092 | value_column_1 = 15, | |
2093 | /* Give enough room for "0x", 16 hex digits and two spaces in | |
dda83cd7 | 2094 | preceding column. */ |
e813d34a RK |
2095 | value_column_2 = value_column_1 + 2 + 16 + 2, |
2096 | }; | |
1292279a | 2097 | |
e813d34a RK |
2098 | format_stream.puts (name); |
2099 | pad_to_column (format_stream, value_column_1); | |
1292279a | 2100 | |
f69d9aef AB |
2101 | print_raw_format = (value_entirely_available (val) |
2102 | && !value_optimized_out (val)); | |
1292279a PA |
2103 | |
2104 | /* If virtual format is floating, print it that way, and in raw | |
2105 | hex. */ | |
78134374 SM |
2106 | if (regtype->code () == TYPE_CODE_FLT |
2107 | || regtype->code () == TYPE_CODE_DECFLOAT) | |
1292279a | 2108 | { |
1292279a PA |
2109 | struct value_print_options opts; |
2110 | const gdb_byte *valaddr = value_contents_for_printing (val); | |
34877895 | 2111 | enum bfd_endian byte_order = type_byte_order (regtype); |
1292279a PA |
2112 | |
2113 | get_user_print_options (&opts); | |
2114 | opts.deref_ref = 1; | |
2115 | ||
3444c526 | 2116 | common_val_print (val, &format_stream, 0, &opts, current_language); |
1292279a | 2117 | |
f69d9aef AB |
2118 | if (print_raw_format) |
2119 | { | |
e813d34a RK |
2120 | pad_to_column (format_stream, value_column_2); |
2121 | format_stream.puts ("(raw "); | |
2122 | print_hex_chars (&format_stream, valaddr, TYPE_LENGTH (regtype), | |
2123 | byte_order, true); | |
2124 | format_stream.putc (')'); | |
f69d9aef | 2125 | } |
1292279a PA |
2126 | } |
2127 | else | |
2128 | { | |
2129 | struct value_print_options opts; | |
2130 | ||
2131 | /* Print the register in hex. */ | |
2132 | get_formatted_print_options (&opts, 'x'); | |
2133 | opts.deref_ref = 1; | |
3444c526 | 2134 | common_val_print (val, &format_stream, 0, &opts, current_language); |
1292279a PA |
2135 | /* If not a vector register, print it also according to its |
2136 | natural format. */ | |
bd63c870 | 2137 | if (print_raw_format && regtype->is_vector () == 0) |
1292279a | 2138 | { |
e813d34a | 2139 | pad_to_column (format_stream, value_column_2); |
1292279a PA |
2140 | get_user_print_options (&opts); |
2141 | opts.deref_ref = 1; | |
3444c526 | 2142 | common_val_print (val, &format_stream, 0, &opts, current_language); |
1292279a PA |
2143 | } |
2144 | } | |
2145 | ||
e813d34a | 2146 | fputs_filtered (format_stream.c_str (), file); |
1292279a PA |
2147 | fprintf_filtered (file, "\n"); |
2148 | } | |
2149 | ||
1777feb0 | 2150 | /* Print out the machine register regnum. If regnum is -1, print all |
0ab7a791 AC |
2151 | registers (print_all == 1) or all non-float and non-vector |
2152 | registers (print_all == 0). | |
c906108c SS |
2153 | |
2154 | For most machines, having all_registers_info() print the | |
0ab7a791 AC |
2155 | register(s) one per line is good enough. If a different format is |
2156 | required, (eg, for MIPS or Pyramid 90x, which both have lots of | |
2157 | regs), or there is an existing convention for showing all the | |
2158 | registers, define the architecture method PRINT_REGISTERS_INFO to | |
2159 | provide that format. */ | |
c906108c | 2160 | |
666e11c5 | 2161 | void |
0ab7a791 AC |
2162 | default_print_registers_info (struct gdbarch *gdbarch, |
2163 | struct ui_file *file, | |
2164 | struct frame_info *frame, | |
2165 | int regnum, int print_all) | |
c906108c | 2166 | { |
0ab7a791 | 2167 | int i; |
f6efe3f8 | 2168 | const int numregs = gdbarch_num_cooked_regs (gdbarch); |
0ab7a791 | 2169 | |
c906108c SS |
2170 | for (i = 0; i < numregs; i++) |
2171 | { | |
4782dc19 | 2172 | /* Decide between printing all regs, non-float / vector regs, or |
dda83cd7 | 2173 | specific reg. */ |
c5aa993b JM |
2174 | if (regnum == -1) |
2175 | { | |
f9418c0f | 2176 | if (print_all) |
4782dc19 | 2177 | { |
f9418c0f | 2178 | if (!gdbarch_register_reggroup_p (gdbarch, i, all_reggroup)) |
4782dc19 | 2179 | continue; |
f9418c0f AC |
2180 | } |
2181 | else | |
2182 | { | |
2183 | if (!gdbarch_register_reggroup_p (gdbarch, i, general_reggroup)) | |
4782dc19 AC |
2184 | continue; |
2185 | } | |
c5aa993b JM |
2186 | } |
2187 | else | |
2188 | { | |
2189 | if (i != regnum) | |
2190 | continue; | |
2191 | } | |
c906108c SS |
2192 | |
2193 | /* If the register name is empty, it is undefined for this | |
dda83cd7 | 2194 | processor, so don't display anything. */ |
a4bd449d UW |
2195 | if (gdbarch_register_name (gdbarch, i) == NULL |
2196 | || *(gdbarch_register_name (gdbarch, i)) == '\0') | |
c906108c SS |
2197 | continue; |
2198 | ||
1292279a PA |
2199 | default_print_one_register_info (file, |
2200 | gdbarch_register_name (gdbarch, i), | |
901461f8 | 2201 | value_of_register (i, frame)); |
c906108c SS |
2202 | } |
2203 | } | |
c906108c | 2204 | |
c906108c | 2205 | void |
1d12d88f | 2206 | registers_info (const char *addr_exp, int fpregs) |
c906108c | 2207 | { |
206415a3 | 2208 | struct frame_info *frame; |
a4bd449d | 2209 | struct gdbarch *gdbarch; |
c906108c | 2210 | |
9dccd06e | 2211 | if (!target_has_registers ()) |
8a3fe4f8 | 2212 | error (_("The program has no registers now.")); |
206415a3 | 2213 | frame = get_selected_frame (NULL); |
a4bd449d | 2214 | gdbarch = get_frame_arch (frame); |
c906108c SS |
2215 | |
2216 | if (!addr_exp) | |
2217 | { | |
a4bd449d | 2218 | gdbarch_print_registers_info (gdbarch, gdb_stdout, |
206415a3 | 2219 | frame, -1, fpregs); |
c906108c SS |
2220 | return; |
2221 | } | |
2222 | ||
f9418c0f | 2223 | while (*addr_exp != '\0') |
c5aa993b | 2224 | { |
1d12d88f | 2225 | const char *start; |
f9418c0f | 2226 | const char *end; |
c906108c | 2227 | |
529480d0 KS |
2228 | /* Skip leading white space. */ |
2229 | addr_exp = skip_spaces (addr_exp); | |
c906108c | 2230 | |
f9418c0f | 2231 | /* Discard any leading ``$''. Check that there is something |
dda83cd7 | 2232 | resembling a register following it. */ |
f9418c0f AC |
2233 | if (addr_exp[0] == '$') |
2234 | addr_exp++; | |
2235 | if (isspace ((*addr_exp)) || (*addr_exp) == '\0') | |
8a3fe4f8 | 2236 | error (_("Missing register name")); |
c906108c | 2237 | |
f9418c0f AC |
2238 | /* Find the start/end of this register name/num/group. */ |
2239 | start = addr_exp; | |
2240 | while ((*addr_exp) != '\0' && !isspace ((*addr_exp))) | |
2241 | addr_exp++; | |
2242 | end = addr_exp; | |
ad842144 | 2243 | |
f9418c0f AC |
2244 | /* Figure out what we've found and display it. */ |
2245 | ||
2246 | /* A register name? */ | |
2247 | { | |
029a67e4 | 2248 | int regnum = user_reg_map_name_to_regnum (gdbarch, start, end - start); |
abbb1732 | 2249 | |
f9418c0f AC |
2250 | if (regnum >= 0) |
2251 | { | |
ad842144 MR |
2252 | /* User registers lie completely outside of the range of |
2253 | normal registers. Catch them early so that the target | |
2254 | never sees them. */ | |
f6efe3f8 | 2255 | if (regnum >= gdbarch_num_cooked_regs (gdbarch)) |
ad842144 | 2256 | { |
1292279a PA |
2257 | struct value *regval = value_of_user_reg (regnum, frame); |
2258 | const char *regname = user_reg_map_regnum_to_name (gdbarch, | |
2259 | regnum); | |
2260 | ||
2261 | /* Print in the same fashion | |
2262 | gdbarch_print_registers_info's default | |
2263 | implementation prints. */ | |
2264 | default_print_one_register_info (gdb_stdout, | |
2265 | regname, | |
2266 | regval); | |
ad842144 MR |
2267 | } |
2268 | else | |
2269 | gdbarch_print_registers_info (gdbarch, gdb_stdout, | |
2270 | frame, regnum, fpregs); | |
f9418c0f AC |
2271 | continue; |
2272 | } | |
2273 | } | |
ad842144 | 2274 | |
f9418c0f AC |
2275 | /* A register group? */ |
2276 | { | |
6c7d17ba | 2277 | struct reggroup *group; |
abbb1732 | 2278 | |
a4bd449d | 2279 | for (group = reggroup_next (gdbarch, NULL); |
6c7d17ba | 2280 | group != NULL; |
a4bd449d | 2281 | group = reggroup_next (gdbarch, group)) |
f9418c0f AC |
2282 | { |
2283 | /* Don't bother with a length check. Should the user | |
2284 | enter a short register group name, go with the first | |
2285 | group that matches. */ | |
6c7d17ba | 2286 | if (strncmp (start, reggroup_name (group), end - start) == 0) |
f9418c0f AC |
2287 | break; |
2288 | } | |
6c7d17ba | 2289 | if (group != NULL) |
f9418c0f AC |
2290 | { |
2291 | int regnum; | |
abbb1732 | 2292 | |
f57d151a | 2293 | for (regnum = 0; |
f6efe3f8 | 2294 | regnum < gdbarch_num_cooked_regs (gdbarch); |
f57d151a | 2295 | regnum++) |
f9418c0f | 2296 | { |
a4bd449d UW |
2297 | if (gdbarch_register_reggroup_p (gdbarch, regnum, group)) |
2298 | gdbarch_print_registers_info (gdbarch, | |
206415a3 | 2299 | gdb_stdout, frame, |
f9418c0f AC |
2300 | regnum, fpregs); |
2301 | } | |
2302 | continue; | |
2303 | } | |
2304 | } | |
c906108c | 2305 | |
f9418c0f | 2306 | /* Nothing matched. */ |
8a3fe4f8 | 2307 | error (_("Invalid register `%.*s'"), (int) (end - start), start); |
c5aa993b | 2308 | } |
c906108c SS |
2309 | } |
2310 | ||
1dd5fedc | 2311 | static void |
1d12d88f | 2312 | info_all_registers_command (const char *addr_exp, int from_tty) |
c906108c SS |
2313 | { |
2314 | registers_info (addr_exp, 1); | |
2315 | } | |
2316 | ||
a58dd373 | 2317 | static void |
1d12d88f | 2318 | info_registers_command (const char *addr_exp, int from_tty) |
c906108c SS |
2319 | { |
2320 | registers_info (addr_exp, 0); | |
2321 | } | |
e76f1f2e AC |
2322 | |
2323 | static void | |
d80b854b | 2324 | print_vector_info (struct ui_file *file, |
e76f1f2e AC |
2325 | struct frame_info *frame, const char *args) |
2326 | { | |
d80b854b UW |
2327 | struct gdbarch *gdbarch = get_frame_arch (frame); |
2328 | ||
e76f1f2e AC |
2329 | if (gdbarch_print_vector_info_p (gdbarch)) |
2330 | gdbarch_print_vector_info (gdbarch, file, frame, args); | |
2331 | else | |
2332 | { | |
2333 | int regnum; | |
2334 | int printed_something = 0; | |
ab4327e0 | 2335 | |
f6efe3f8 | 2336 | for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++) |
e76f1f2e | 2337 | { |
f9418c0f | 2338 | if (gdbarch_register_reggroup_p (gdbarch, regnum, vector_reggroup)) |
e76f1f2e AC |
2339 | { |
2340 | printed_something = 1; | |
e76f1f2e | 2341 | gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1); |
e76f1f2e AC |
2342 | } |
2343 | } | |
2344 | if (!printed_something) | |
2345 | fprintf_filtered (file, "No vector information\n"); | |
2346 | } | |
2347 | } | |
2348 | ||
2349 | static void | |
1d12d88f | 2350 | info_vector_command (const char *args, int from_tty) |
e76f1f2e | 2351 | { |
9dccd06e | 2352 | if (!target_has_registers ()) |
206415a3 DJ |
2353 | error (_("The program has no registers now.")); |
2354 | ||
d80b854b | 2355 | print_vector_info (gdb_stdout, get_selected_frame (NULL), args); |
e76f1f2e | 2356 | } |
c906108c | 2357 | \f |
5fd62852 PA |
2358 | /* Kill the inferior process. Make us have no inferior. */ |
2359 | ||
2360 | static void | |
981a3fb3 | 2361 | kill_command (const char *arg, int from_tty) |
5fd62852 PA |
2362 | { |
2363 | /* FIXME: This should not really be inferior_ptid (or target_has_execution). | |
2364 | It should be a distinct flag that indicates that a target is active, cuz | |
1777feb0 | 2365 | some targets don't have processes! */ |
5fd62852 | 2366 | |
d7e15655 | 2367 | if (inferior_ptid == null_ptid) |
5fd62852 PA |
2368 | error (_("The program is not being run.")); |
2369 | if (!query (_("Kill the program being debugged? "))) | |
2370 | error (_("Not confirmed.")); | |
f67c0c91 | 2371 | |
249b5733 PA |
2372 | int pid = current_inferior ()->pid; |
2373 | /* Save the pid as a string before killing the inferior, since that | |
2374 | may unpush the current target, and we need the string after. */ | |
f2907e49 | 2375 | std::string pid_str = target_pid_to_str (ptid_t (pid)); |
f67c0c91 SDJ |
2376 | int infnum = current_inferior ()->num; |
2377 | ||
5fd62852 PA |
2378 | target_kill (); |
2379 | ||
f67c0c91 | 2380 | if (print_inferior_events) |
249b5733 | 2381 | printf_unfiltered (_("[Inferior %d (%s) killed]\n"), |
f67c0c91 SDJ |
2382 | infnum, pid_str.c_str ()); |
2383 | ||
5fd62852 PA |
2384 | bfd_cache_close_all (); |
2385 | } | |
c5aa993b | 2386 | |
08036331 | 2387 | /* Used in `attach&' command. Proceed threads of inferior INF iff |
74531fed | 2388 | they stopped due to debugger request, and when they did, they |
08036331 PA |
2389 | reported a clean stop (GDB_SIGNAL_0). Do not proceed threads that |
2390 | have been explicitly been told to stop. */ | |
74531fed PA |
2391 | |
2392 | static void | |
08036331 | 2393 | proceed_after_attach (inferior *inf) |
74531fed PA |
2394 | { |
2395 | /* Don't error out if the current thread is running, because | |
2396 | there may be other stopped threads. */ | |
74531fed PA |
2397 | |
2398 | /* Backup current thread and selected frame. */ | |
5ed8105e | 2399 | scoped_restore_current_thread restore_thread; |
74531fed | 2400 | |
08036331 PA |
2401 | for (thread_info *thread : inf->non_exited_threads ()) |
2402 | if (!thread->executing | |
2403 | && !thread->stop_requested | |
2404 | && thread->suspend.stop_signal == GDB_SIGNAL_0) | |
2405 | { | |
2406 | switch_to_thread (thread); | |
2407 | clear_proceed_status (0); | |
2408 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); | |
2409 | } | |
74531fed PA |
2410 | } |
2411 | ||
6efcd9a8 | 2412 | /* See inferior.h. */ |
c906108c | 2413 | |
6efcd9a8 PA |
2414 | void |
2415 | setup_inferior (int from_tty) | |
9356cf8d | 2416 | { |
d6b48e9c | 2417 | struct inferior *inferior; |
9356cf8d | 2418 | |
d6b48e9c | 2419 | inferior = current_inferior (); |
6efcd9a8 | 2420 | inferior->needs_setup = 0; |
9356cf8d PA |
2421 | |
2422 | /* If no exec file is yet known, try to determine it from the | |
2423 | process itself. */ | |
a10de604 | 2424 | if (get_exec_file (0) == NULL) |
e99b03dc | 2425 | exec_file_locate_attach (inferior_ptid.pid (), 1, from_tty); |
9356cf8d PA |
2426 | else |
2427 | { | |
2428 | reopen_exec_file (); | |
2429 | reread_symbols (); | |
2430 | } | |
2431 | ||
2432 | /* Take any necessary post-attaching actions for this platform. */ | |
e99b03dc | 2433 | target_post_attach (inferior_ptid.pid ()); |
9356cf8d | 2434 | |
a7aba266 | 2435 | post_create_inferior (from_tty); |
6efcd9a8 PA |
2436 | } |
2437 | ||
2438 | /* What to do after the first program stops after attaching. */ | |
2439 | enum attach_post_wait_mode | |
2440 | { | |
2441 | /* Do nothing. Leaves threads as they are. */ | |
2442 | ATTACH_POST_WAIT_NOTHING, | |
2443 | ||
2444 | /* Re-resume threads that are marked running. */ | |
2445 | ATTACH_POST_WAIT_RESUME, | |
2446 | ||
2447 | /* Stop all threads. */ | |
2448 | ATTACH_POST_WAIT_STOP, | |
2449 | }; | |
2450 | ||
2451 | /* Called after we've attached to a process and we've seen it stop for | |
2452 | the first time. If ASYNC_EXEC is true, re-resume threads that | |
2453 | should be running. Else if ATTACH, */ | |
2454 | ||
2455 | static void | |
a121b7c1 | 2456 | attach_post_wait (const char *args, int from_tty, enum attach_post_wait_mode mode) |
6efcd9a8 PA |
2457 | { |
2458 | struct inferior *inferior; | |
9356cf8d | 2459 | |
6efcd9a8 PA |
2460 | inferior = current_inferior (); |
2461 | inferior->control.stop_soon = NO_STOP_QUIETLY; | |
2462 | ||
2463 | if (inferior->needs_setup) | |
2464 | setup_inferior (from_tty); | |
2465 | ||
2466 | if (mode == ATTACH_POST_WAIT_RESUME) | |
74531fed PA |
2467 | { |
2468 | /* The user requested an `attach&', so be sure to leave threads | |
2469 | that didn't get a signal running. */ | |
2470 | ||
2471 | /* Immediatelly resume all suspended threads of this inferior, | |
2472 | and this inferior only. This should have no effect on | |
2473 | already running threads. If a thread has been stopped with a | |
2474 | signal, leave it be. */ | |
2475 | if (non_stop) | |
08036331 | 2476 | proceed_after_attach (inferior); |
74531fed PA |
2477 | else |
2478 | { | |
a493e3e2 | 2479 | if (inferior_thread ()->suspend.stop_signal == GDB_SIGNAL_0) |
74531fed | 2480 | { |
70509625 | 2481 | clear_proceed_status (0); |
64ce06e4 | 2482 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
74531fed PA |
2483 | } |
2484 | } | |
2485 | } | |
6efcd9a8 | 2486 | else if (mode == ATTACH_POST_WAIT_STOP) |
9356cf8d | 2487 | { |
74531fed PA |
2488 | /* The user requested a plain `attach', so be sure to leave |
2489 | the inferior stopped. */ | |
2490 | ||
74531fed PA |
2491 | /* At least the current thread is already stopped. */ |
2492 | ||
2493 | /* In all-stop, by definition, all threads have to be already | |
2494 | stopped at this point. In non-stop, however, although the | |
2495 | selected thread is stopped, others may still be executing. | |
2496 | Be sure to explicitly stop all threads of the process. This | |
2497 | should have no effect on already stopped threads. */ | |
066f6b6e | 2498 | if (non_stop) |
f2907e49 | 2499 | target_stop (ptid_t (inferior->pid)); |
066f6b6e PA |
2500 | else if (target_is_non_stop_p ()) |
2501 | { | |
066f6b6e | 2502 | struct thread_info *lowest = inferior_thread (); |
066f6b6e PA |
2503 | |
2504 | stop_all_threads (); | |
2505 | ||
2506 | /* It's not defined which thread will report the attach | |
2507 | stop. For consistency, always select the thread with | |
2508 | lowest GDB number, which should be the main thread, if it | |
2509 | still exists. */ | |
08036331 PA |
2510 | for (thread_info *thread : current_inferior ()->non_exited_threads ()) |
2511 | if (thread->inf->num < lowest->inf->num | |
2512 | || thread->per_inf_num < lowest->per_inf_num) | |
2513 | lowest = thread; | |
066f6b6e | 2514 | |
00431a78 | 2515 | switch_to_thread (lowest); |
066f6b6e | 2516 | } |
74531fed PA |
2517 | |
2518 | /* Tell the user/frontend where we're stopped. */ | |
9356cf8d PA |
2519 | normal_stop (); |
2520 | if (deprecated_attach_hook) | |
2521 | deprecated_attach_hook (); | |
2522 | } | |
2523 | } | |
2524 | ||
bfec99b2 | 2525 | struct attach_command_continuation_args |
9356cf8d PA |
2526 | { |
2527 | char *args; | |
2528 | int from_tty; | |
6efcd9a8 | 2529 | enum attach_post_wait_mode mode; |
bfec99b2 | 2530 | }; |
9356cf8d | 2531 | |
bfec99b2 | 2532 | static void |
fa4cd53f | 2533 | attach_command_continuation (void *args, int err) |
bfec99b2 | 2534 | { |
9a3c8263 SM |
2535 | struct attach_command_continuation_args *a |
2536 | = (struct attach_command_continuation_args *) args; | |
abbb1732 | 2537 | |
fa4cd53f PA |
2538 | if (err) |
2539 | return; | |
2540 | ||
6efcd9a8 | 2541 | attach_post_wait (a->args, a->from_tty, a->mode); |
9356cf8d PA |
2542 | } |
2543 | ||
604ead4a PA |
2544 | static void |
2545 | attach_command_continuation_free_args (void *args) | |
2546 | { | |
9a3c8263 SM |
2547 | struct attach_command_continuation_args *a |
2548 | = (struct attach_command_continuation_args *) args; | |
abbb1732 | 2549 | |
604ead4a PA |
2550 | xfree (a->args); |
2551 | xfree (a); | |
2552 | } | |
2553 | ||
6efcd9a8 PA |
2554 | /* "attach" command entry point. Takes a program started up outside |
2555 | of gdb and ``attaches'' to it. This stops it cold in its tracks | |
2556 | and allows us to start debugging it. */ | |
2557 | ||
c906108c | 2558 | void |
0b39b52e | 2559 | attach_command (const char *args, int from_tty) |
c906108c | 2560 | { |
6c4486e6 | 2561 | int async_exec; |
b3ccfe11 | 2562 | struct target_ops *attach_target; |
6efcd9a8 PA |
2563 | struct inferior *inferior = current_inferior (); |
2564 | enum attach_post_wait_mode mode; | |
c906108c | 2565 | |
c5aa993b | 2566 | dont_repeat (); /* Not for the faint of heart */ |
c906108c | 2567 | |
f5656ead | 2568 | if (gdbarch_has_global_solist (target_gdbarch ())) |
2567c7d9 PA |
2569 | /* Don't complain if all processes share the same symbol |
2570 | space. */ | |
8a305172 | 2571 | ; |
55f6301a | 2572 | else if (target_has_execution ()) |
c906108c | 2573 | { |
9e2f0ad4 | 2574 | if (query (_("A program is being debugged already. Kill it? "))) |
c906108c SS |
2575 | target_kill (); |
2576 | else | |
8a3fe4f8 | 2577 | error (_("Not killed.")); |
c906108c SS |
2578 | } |
2579 | ||
fd79ecee DJ |
2580 | /* Clean up any leftovers from other runs. Some other things from |
2581 | this function should probably be moved into target_pre_inferior. */ | |
2582 | target_pre_inferior (from_tty); | |
2583 | ||
6be9a197 TT |
2584 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec); |
2585 | args = stripped.get (); | |
8bc2fe48 | 2586 | |
b3ccfe11 TT |
2587 | attach_target = find_attach_target (); |
2588 | ||
8bc2fe48 PA |
2589 | prepare_execution_command (attach_target, async_exec); |
2590 | ||
f6ac5f3d | 2591 | if (non_stop && !attach_target->supports_non_stop ()) |
9908b566 VP |
2592 | error (_("Cannot attach to this target in non-stop mode")); |
2593 | ||
f6ac5f3d | 2594 | attach_target->attach (args, from_tty); |
b3ccfe11 TT |
2595 | /* to_attach should push the target, so after this point we |
2596 | shouldn't refer to attach_target again. */ | |
2597 | attach_target = NULL; | |
c906108c SS |
2598 | |
2599 | /* Set up the "saved terminal modes" of the inferior | |
2600 | based on what modes we are starting it with. */ | |
223ffa71 | 2601 | target_terminal::init (); |
c906108c | 2602 | |
7180e04a PA |
2603 | /* Install inferior's terminal modes. This may look like a no-op, |
2604 | as we've just saved them above, however, this does more than | |
2605 | restore terminal settings: | |
2606 | ||
2607 | - installs a SIGINT handler that forwards SIGINT to the inferior. | |
2608 | Otherwise a Ctrl-C pressed just while waiting for the initial | |
2609 | stop would end up as a spurious Quit. | |
2610 | ||
2611 | - removes stdin from the event loop, which we need if attaching | |
2612 | in the foreground, otherwise on targets that report an initial | |
2613 | stop on attach (which are most) we'd process input/commands | |
2614 | while we're in the event loop waiting for that stop. That is, | |
2615 | before the attach continuation runs and the command is really | |
2616 | finished. */ | |
223ffa71 | 2617 | target_terminal::inferior (); |
7180e04a | 2618 | |
c906108c SS |
2619 | /* Set up execution context to know that we should return from |
2620 | wait_for_inferior as soon as the target reports a stop. */ | |
2621 | init_wait_for_inferior (); | |
c906108c | 2622 | |
6efcd9a8 PA |
2623 | inferior->needs_setup = 1; |
2624 | ||
fbea99ea | 2625 | if (target_is_non_stop_p ()) |
74531fed PA |
2626 | { |
2627 | /* If we find that the current thread isn't stopped, explicitly | |
2628 | do so now, because we're going to install breakpoints and | |
2629 | poke at memory. */ | |
2630 | ||
2631 | if (async_exec) | |
2632 | /* The user requested an `attach&'; stop just one thread. */ | |
2633 | target_stop (inferior_ptid); | |
2634 | else | |
2635 | /* The user requested an `attach', so stop all threads of this | |
2636 | inferior. */ | |
e99b03dc | 2637 | target_stop (ptid_t (inferior_ptid.pid ())); |
74531fed PA |
2638 | } |
2639 | ||
a2fedca9 PW |
2640 | /* Check for exec file mismatch, and let the user solve it. */ |
2641 | validate_exec_file (from_tty); | |
2642 | ||
6efcd9a8 PA |
2643 | mode = async_exec ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_STOP; |
2644 | ||
dc177b7a PA |
2645 | /* Some system don't generate traps when attaching to inferior. |
2646 | E.g. Mach 3 or GNU hurd. */ | |
f6ac5f3d | 2647 | if (!target_attach_no_wait ()) |
c5aa993b | 2648 | { |
0b333c5e | 2649 | struct attach_command_continuation_args *a; |
d6b48e9c | 2650 | |
1777feb0 | 2651 | /* Careful here. See comments in inferior.h. Basically some |
dc177b7a PA |
2652 | OSes don't ignore SIGSTOPs on continue requests anymore. We |
2653 | need a way for handle_inferior_event to reset the stop_signal | |
2654 | variable after an attach, and this is what | |
2655 | STOP_QUIETLY_NO_SIGSTOP is for. */ | |
16c381f0 | 2656 | inferior->control.stop_soon = STOP_QUIETLY_NO_SIGSTOP; |
c5aa993b | 2657 | |
3b12939d | 2658 | /* Wait for stop. */ |
0b333c5e PA |
2659 | a = XNEW (struct attach_command_continuation_args); |
2660 | a->args = xstrdup (args); | |
2661 | a->from_tty = from_tty; | |
6efcd9a8 | 2662 | a->mode = mode; |
0b333c5e PA |
2663 | add_inferior_continuation (attach_command_continuation, a, |
2664 | attach_command_continuation_free_args); | |
0b333c5e | 2665 | |
5b6d1e4f PA |
2666 | /* Let infrun consider waiting for events out of this |
2667 | target. */ | |
2668 | inferior->process_target ()->threads_executing = true; | |
2669 | ||
0b333c5e PA |
2670 | if (!target_is_async_p ()) |
2671 | mark_infrun_async_event_handler (); | |
2672 | return; | |
dc177b7a | 2673 | } |
5b6d1e4f PA |
2674 | else |
2675 | attach_post_wait (args, from_tty, mode); | |
c906108c SS |
2676 | } |
2677 | ||
1941c569 PA |
2678 | /* We had just found out that the target was already attached to an |
2679 | inferior. PTID points at a thread of this new inferior, that is | |
2680 | the most likely to be stopped right now, but not necessarily so. | |
2681 | The new inferior is assumed to be already added to the inferior | |
2682 | list at this point. If LEAVE_RUNNING, then leave the threads of | |
2683 | this inferior running, except those we've explicitly seen reported | |
2684 | as stopped. */ | |
2685 | ||
2686 | void | |
00431a78 | 2687 | notice_new_inferior (thread_info *thr, int leave_running, int from_tty) |
1941c569 | 2688 | { |
5ed8105e PA |
2689 | enum attach_post_wait_mode mode |
2690 | = leave_running ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_NOTHING; | |
1941c569 | 2691 | |
5ed8105e | 2692 | gdb::optional<scoped_restore_current_thread> restore_thread; |
1941c569 | 2693 | |
5ed8105e PA |
2694 | if (inferior_ptid != null_ptid) |
2695 | restore_thread.emplace (); | |
1941c569 | 2696 | |
6efcd9a8 PA |
2697 | /* Avoid reading registers -- we haven't fetched the target |
2698 | description yet. */ | |
00431a78 | 2699 | switch_to_thread_no_regs (thr); |
1941c569 PA |
2700 | |
2701 | /* When we "notice" a new inferior we need to do all the things we | |
2702 | would normally do if we had just attached to it. */ | |
2703 | ||
00431a78 | 2704 | if (thr->executing) |
1941c569 | 2705 | { |
0b333c5e | 2706 | struct attach_command_continuation_args *a; |
1941c569 PA |
2707 | struct inferior *inferior = current_inferior (); |
2708 | ||
2709 | /* We're going to install breakpoints, and poke at memory, | |
2710 | ensure that the inferior is stopped for a moment while we do | |
2711 | that. */ | |
2712 | target_stop (inferior_ptid); | |
2713 | ||
16c381f0 | 2714 | inferior->control.stop_soon = STOP_QUIETLY_REMOTE; |
1941c569 PA |
2715 | |
2716 | /* Wait for stop before proceeding. */ | |
0b333c5e PA |
2717 | a = XNEW (struct attach_command_continuation_args); |
2718 | a->args = xstrdup (""); | |
2719 | a->from_tty = from_tty; | |
6efcd9a8 | 2720 | a->mode = mode; |
0b333c5e PA |
2721 | add_inferior_continuation (attach_command_continuation, a, |
2722 | attach_command_continuation_free_args); | |
1941c569 | 2723 | |
0b333c5e | 2724 | return; |
1941c569 PA |
2725 | } |
2726 | ||
6efcd9a8 | 2727 | attach_post_wait ("" /* args */, from_tty, mode); |
1941c569 PA |
2728 | } |
2729 | ||
c906108c SS |
2730 | /* |
2731 | * detach_command -- | |
2732 | * takes a program previously attached to and detaches it. | |
2733 | * The program resumes execution and will no longer stop | |
2734 | * on signals, etc. We better not have left any breakpoints | |
2735 | * in the program or it'll die when it hits one. For this | |
2736 | * to work, it may be necessary for the process to have been | |
2737 | * previously attached. It *might* work if the program was | |
2738 | * started via the normal ptrace (PTRACE_TRACEME). | |
2739 | */ | |
2740 | ||
6418d433 | 2741 | void |
981a3fb3 | 2742 | detach_command (const char *args, int from_tty) |
c906108c | 2743 | { |
1f5d0fc9 | 2744 | dont_repeat (); /* Not for the faint of heart. */ |
d729566a | 2745 | |
d7e15655 | 2746 | if (inferior_ptid == null_ptid) |
d729566a PA |
2747 | error (_("The program is not being run.")); |
2748 | ||
2f9d54cf PA |
2749 | query_if_trace_running (from_tty); |
2750 | ||
2751 | disconnect_tracing (); | |
d5551862 | 2752 | |
6e1e1966 | 2753 | target_detach (current_inferior (), from_tty); |
50c71eaf | 2754 | |
63000888 PA |
2755 | /* The current inferior process was just detached successfully. Get |
2756 | rid of breakpoints that no longer make sense. Note we don't do | |
2757 | this within target_detach because that is also used when | |
2758 | following child forks, and in that case we will want to transfer | |
2759 | breakpoints to the child, not delete them. */ | |
2760 | breakpoint_init_inferior (inf_exited); | |
2761 | ||
50c71eaf PA |
2762 | /* If the solist is global across inferiors, don't clear it when we |
2763 | detach from a single inferior. */ | |
f5656ead | 2764 | if (!gdbarch_has_global_solist (target_gdbarch ())) |
50c71eaf | 2765 | no_shared_libraries (NULL, from_tty); |
8a305172 | 2766 | |
9a4105ab AC |
2767 | if (deprecated_detach_hook) |
2768 | deprecated_detach_hook (); | |
c906108c SS |
2769 | } |
2770 | ||
6ad8ae5c DJ |
2771 | /* Disconnect from the current target without resuming it (leaving it |
2772 | waiting for a debugger). | |
2773 | ||
2774 | We'd better not have left any breakpoints in the program or the | |
2775 | next debugger will get confused. Currently only supported for some | |
2776 | remote targets, since the normal attach mechanisms don't work on | |
2777 | stopped processes on some native platforms (e.g. GNU/Linux). */ | |
2778 | ||
2779 | static void | |
0b39b52e | 2780 | disconnect_command (const char *args, int from_tty) |
6ad8ae5c | 2781 | { |
1777feb0 | 2782 | dont_repeat (); /* Not for the faint of heart. */ |
2f9d54cf PA |
2783 | query_if_trace_running (from_tty); |
2784 | disconnect_tracing (); | |
6ad8ae5c | 2785 | target_disconnect (args, from_tty); |
e85a822c | 2786 | no_shared_libraries (NULL, from_tty); |
a0ef4274 | 2787 | init_thread_list (); |
9a4105ab AC |
2788 | if (deprecated_detach_hook) |
2789 | deprecated_detach_hook (); | |
6ad8ae5c DJ |
2790 | } |
2791 | ||
5b6d1e4f PA |
2792 | /* Stop PTID in the current target, and tag the PTID threads as having |
2793 | been explicitly requested to stop. PTID can be a thread, a | |
2794 | process, or minus_one_ptid, meaning all threads of all inferiors of | |
2795 | the current target. */ | |
e42de8c7 | 2796 | |
5b6d1e4f PA |
2797 | static void |
2798 | stop_current_target_threads_ns (ptid_t ptid) | |
2799 | { | |
2800 | target_stop (ptid); | |
252fbfc8 PA |
2801 | |
2802 | /* Tag the thread as having been explicitly requested to stop, so | |
2803 | other parts of gdb know not to resume this thread automatically, | |
2804 | if it was stopped due to an internal event. Limit this to | |
2805 | non-stop mode, as when debugging a multi-threaded application in | |
2806 | all-stop mode, we will only get one stop event --- it's undefined | |
2807 | which thread will report the event. */ | |
5b6d1e4f PA |
2808 | set_stop_requested (current_inferior ()->process_target (), |
2809 | ptid, 1); | |
2810 | } | |
2811 | ||
2812 | /* See inferior.h. */ | |
2813 | ||
2814 | void | |
2815 | interrupt_target_1 (bool all_threads) | |
2816 | { | |
252fbfc8 | 2817 | if (non_stop) |
5b6d1e4f PA |
2818 | { |
2819 | if (all_threads) | |
2820 | { | |
2821 | scoped_restore_current_thread restore_thread; | |
2822 | ||
2823 | for (inferior *inf : all_inferiors ()) | |
2824 | { | |
2825 | switch_to_inferior_no_thread (inf); | |
2826 | stop_current_target_threads_ns (minus_one_ptid); | |
2827 | } | |
2828 | } | |
2829 | else | |
2830 | stop_current_target_threads_ns (inferior_ptid); | |
2831 | } | |
2832 | else | |
2833 | target_interrupt (); | |
77ebaa5a VP |
2834 | } |
2835 | ||
6339bfc4 DE |
2836 | /* interrupt [-a] |
2837 | Stop the execution of the target while running in async mode, in | |
17e5269b | 2838 | the background. In all-stop, stop the whole process. In non-stop |
8cae4b3f PA |
2839 | mode, stop the current thread only by default, or stop all threads |
2840 | if the `-a' switch is used. */ | |
2841 | ||
1dd5fedc | 2842 | static void |
0b39b52e | 2843 | interrupt_command (const char *args, int from_tty) |
43ff13b4 | 2844 | { |
362646f5 | 2845 | if (target_can_async_p ()) |
43ff13b4 | 2846 | { |
8cae4b3f PA |
2847 | int all_threads = 0; |
2848 | ||
1777feb0 | 2849 | dont_repeat (); /* Not for the faint of heart. */ |
94cc34af | 2850 | |
8cae4b3f | 2851 | if (args != NULL |
61012eef | 2852 | && startswith (args, "-a")) |
8cae4b3f PA |
2853 | all_threads = 1; |
2854 | ||
2855 | if (!non_stop && all_threads) | |
2856 | error (_("-a is meaningless in all-stop mode.")); | |
2857 | ||
77ebaa5a | 2858 | interrupt_target_1 (all_threads); |
43ff13b4 JM |
2859 | } |
2860 | } | |
2861 | ||
cc86d1cb YQ |
2862 | /* See inferior.h. */ |
2863 | ||
2864 | void | |
2865 | default_print_float_info (struct gdbarch *gdbarch, struct ui_file *file, | |
2866 | struct frame_info *frame, const char *args) | |
c906108c | 2867 | { |
cc86d1cb YQ |
2868 | int regnum; |
2869 | int printed_something = 0; | |
d80b854b | 2870 | |
f6efe3f8 | 2871 | for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++) |
23e3a7ac | 2872 | { |
cc86d1cb | 2873 | if (gdbarch_register_reggroup_p (gdbarch, regnum, float_reggroup)) |
23e3a7ac | 2874 | { |
cc86d1cb YQ |
2875 | printed_something = 1; |
2876 | gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1); | |
23e3a7ac | 2877 | } |
23e3a7ac | 2878 | } |
cc86d1cb YQ |
2879 | if (!printed_something) |
2880 | fprintf_filtered (file, "No floating-point info " | |
2881 | "available for this processor.\n"); | |
23e3a7ac AC |
2882 | } |
2883 | ||
2884 | static void | |
1d12d88f | 2885 | info_float_command (const char *args, int from_tty) |
23e3a7ac | 2886 | { |
cc86d1cb YQ |
2887 | struct frame_info *frame; |
2888 | ||
9dccd06e | 2889 | if (!target_has_registers ()) |
206415a3 DJ |
2890 | error (_("The program has no registers now.")); |
2891 | ||
cc86d1cb YQ |
2892 | frame = get_selected_frame (NULL); |
2893 | gdbarch_print_float_info (get_frame_arch (frame), gdb_stdout, frame, args); | |
c906108c SS |
2894 | } |
2895 | \f | |
145b16a9 UW |
2896 | /* Implement `info proc' family of commands. */ |
2897 | ||
2898 | static void | |
69f476a3 | 2899 | info_proc_cmd_1 (const char *args, enum info_proc_what what, int from_tty) |
145b16a9 | 2900 | { |
3030c96e UW |
2901 | struct gdbarch *gdbarch = get_current_arch (); |
2902 | ||
451b7c33 TT |
2903 | if (!target_info_proc (args, what)) |
2904 | { | |
2905 | if (gdbarch_info_proc_p (gdbarch)) | |
2906 | gdbarch_info_proc (gdbarch, args, what); | |
2907 | else | |
2908 | error (_("Not supported on this target.")); | |
2909 | } | |
145b16a9 UW |
2910 | } |
2911 | ||
85102364 | 2912 | /* Implement `info proc' when given without any further parameters. */ |
145b16a9 UW |
2913 | |
2914 | static void | |
981a3fb3 | 2915 | info_proc_cmd (const char *args, int from_tty) |
145b16a9 UW |
2916 | { |
2917 | info_proc_cmd_1 (args, IP_MINIMAL, from_tty); | |
2918 | } | |
2919 | ||
2920 | /* Implement `info proc mappings'. */ | |
2921 | ||
2922 | static void | |
69f476a3 | 2923 | info_proc_cmd_mappings (const char *args, int from_tty) |
145b16a9 UW |
2924 | { |
2925 | info_proc_cmd_1 (args, IP_MAPPINGS, from_tty); | |
2926 | } | |
2927 | ||
2928 | /* Implement `info proc stat'. */ | |
2929 | ||
2930 | static void | |
69f476a3 | 2931 | info_proc_cmd_stat (const char *args, int from_tty) |
145b16a9 UW |
2932 | { |
2933 | info_proc_cmd_1 (args, IP_STAT, from_tty); | |
2934 | } | |
2935 | ||
2936 | /* Implement `info proc status'. */ | |
2937 | ||
2938 | static void | |
69f476a3 | 2939 | info_proc_cmd_status (const char *args, int from_tty) |
145b16a9 UW |
2940 | { |
2941 | info_proc_cmd_1 (args, IP_STATUS, from_tty); | |
2942 | } | |
2943 | ||
2944 | /* Implement `info proc cwd'. */ | |
2945 | ||
2946 | static void | |
69f476a3 | 2947 | info_proc_cmd_cwd (const char *args, int from_tty) |
145b16a9 UW |
2948 | { |
2949 | info_proc_cmd_1 (args, IP_CWD, from_tty); | |
2950 | } | |
2951 | ||
2952 | /* Implement `info proc cmdline'. */ | |
2953 | ||
2954 | static void | |
69f476a3 | 2955 | info_proc_cmd_cmdline (const char *args, int from_tty) |
145b16a9 UW |
2956 | { |
2957 | info_proc_cmd_1 (args, IP_CMDLINE, from_tty); | |
2958 | } | |
2959 | ||
2960 | /* Implement `info proc exe'. */ | |
2961 | ||
2962 | static void | |
69f476a3 | 2963 | info_proc_cmd_exe (const char *args, int from_tty) |
145b16a9 UW |
2964 | { |
2965 | info_proc_cmd_1 (args, IP_EXE, from_tty); | |
2966 | } | |
2967 | ||
e98ee8c4 JB |
2968 | /* Implement `info proc files'. */ |
2969 | ||
2970 | static void | |
2971 | info_proc_cmd_files (const char *args, int from_tty) | |
2972 | { | |
2973 | info_proc_cmd_1 (args, IP_FILES, from_tty); | |
2974 | } | |
2975 | ||
145b16a9 UW |
2976 | /* Implement `info proc all'. */ |
2977 | ||
2978 | static void | |
69f476a3 | 2979 | info_proc_cmd_all (const char *args, int from_tty) |
145b16a9 UW |
2980 | { |
2981 | info_proc_cmd_1 (args, IP_ALL, from_tty); | |
2982 | } | |
2983 | ||
000439d5 TT |
2984 | /* Implement `show print finish'. */ |
2985 | ||
2986 | static void | |
2987 | show_print_finish (struct ui_file *file, int from_tty, | |
2988 | struct cmd_list_element *c, | |
2989 | const char *value) | |
2990 | { | |
2991 | fprintf_filtered (file, _("\ | |
2992 | Printing of return value after `finish' is %s.\n"), | |
2993 | value); | |
2994 | } | |
2995 | ||
2996 | ||
4e5a4f58 JB |
2997 | /* This help string is used for the run, start, and starti commands. |
2998 | It is defined as a macro to prevent duplication. */ | |
2999 | ||
3000 | #define RUN_ARGS_HELP \ | |
3001 | "You may specify arguments to give it.\n\ | |
3002 | Args may include \"*\", or \"[...]\"; they are expanded using the\n\ | |
3003 | shell that will start the program (specified by the \"$SHELL\" environment\n\ | |
3004 | variable). Input and output redirection with \">\", \"<\", or \">>\"\n\ | |
3005 | are also allowed.\n\ | |
3006 | \n\ | |
3007 | With no arguments, uses arguments last specified (with \"run\" or \n\ | |
3008 | \"set args\"). To cancel previous arguments and run with no arguments,\n\ | |
3009 | use \"set args\" without arguments.\n\ | |
3010 | \n\ | |
3011 | To start the inferior without using a shell, use \"set startup-with-shell off\"." | |
3012 | ||
6c265988 | 3013 | void _initialize_infcmd (); |
c906108c | 3014 | void |
6c265988 | 3015 | _initialize_infcmd () |
c906108c | 3016 | { |
145b16a9 | 3017 | static struct cmd_list_element *info_proc_cmdlist; |
3cb3b8df | 3018 | struct cmd_list_element *c = NULL; |
6f937416 | 3019 | const char *cmd_name; |
3cb3b8df | 3020 | |
1777feb0 | 3021 | /* Add the filename of the terminal connected to inferior I/O. */ |
0a1ddfa6 SM |
3022 | add_setshow_optional_filename_cmd ("inferior-tty", class_run, |
3023 | &inferior_io_terminal_scratch, _("\ | |
3cb3b8df BR |
3024 | Set terminal for future runs of program being debugged."), _("\ |
3025 | Show terminal for future runs of program being debugged."), _("\ | |
0a1ddfa6 SM |
3026 | Usage: set inferior-tty [TTY]\n\n\ |
3027 | If TTY is omitted, the default behavior of using the same terminal as GDB\n\ | |
3028 | is restored."), | |
3029 | set_inferior_tty_command, | |
3030 | show_inferior_tty_command, | |
3031 | &setlist, &showlist); | |
21873064 | 3032 | cmd_name = "inferior-tty"; |
cf00cd6f | 3033 | c = lookup_cmd (&cmd_name, setlist, "", NULL, -1, 1); |
21873064 | 3034 | gdb_assert (c != NULL); |
57b4f16e | 3035 | add_alias_cmd ("tty", c, class_run, 0, &cmdlist); |
c906108c | 3036 | |
6ace3df1 YQ |
3037 | cmd_name = "args"; |
3038 | add_setshow_string_noescape_cmd (cmd_name, class_run, | |
3039 | &inferior_args_scratch, _("\ | |
b4b4ac0b AC |
3040 | Set argument list to give program being debugged when it is started."), _("\ |
3041 | Show argument list to give program being debugged when it is started."), _("\ | |
3042 | Follow this command with any number of args, to be passed to the program."), | |
6ace3df1 YQ |
3043 | set_args_command, |
3044 | show_args_command, | |
3045 | &setlist, &showlist); | |
cf00cd6f | 3046 | c = lookup_cmd (&cmd_name, setlist, "", NULL, -1, 1); |
6ace3df1 YQ |
3047 | gdb_assert (c != NULL); |
3048 | set_cmd_completer (c, filename_completer); | |
c906108c | 3049 | |
d092c5a2 SDJ |
3050 | cmd_name = "cwd"; |
3051 | add_setshow_string_noescape_cmd (cmd_name, class_run, | |
3052 | &inferior_cwd_scratch, _("\ | |
3053 | Set the current working directory to be used when the inferior is started.\n\ | |
3054 | Changing this setting does not have any effect on inferiors that are\n\ | |
3055 | already running."), | |
3056 | _("\ | |
3057 | Show the current working directory that is used when the inferior is started."), | |
3058 | _("\ | |
3059 | Use this command to change the current working directory that will be used\n\ | |
3060 | when the inferior is started. This setting does not affect GDB's current\n\ | |
3061 | working directory."), | |
3062 | set_cwd_command, | |
3063 | show_cwd_command, | |
3064 | &setlist, &showlist); | |
cf00cd6f | 3065 | c = lookup_cmd (&cmd_name, setlist, "", NULL, -1, 1); |
d092c5a2 SDJ |
3066 | gdb_assert (c != NULL); |
3067 | set_cmd_completer (c, filename_completer); | |
3068 | ||
1a966eab AC |
3069 | c = add_cmd ("environment", no_class, environment_info, _("\ |
3070 | The environment to give the program, or one variable's value.\n\ | |
c906108c SS |
3071 | With an argument VAR, prints the value of environment variable VAR to\n\ |
3072 | give the program being debugged. With no arguments, prints the entire\n\ | |
1a966eab | 3073 | environment to be given to the program."), &showlist); |
5ba2abeb | 3074 | set_cmd_completer (c, noop_completer); |
c906108c | 3075 | |
0743fc83 TT |
3076 | add_basic_prefix_cmd ("unset", no_class, |
3077 | _("Complement to certain \"set\" commands."), | |
3078 | &unsetlist, "unset ", 0, &cmdlist); | |
c5aa993b | 3079 | |
1a966eab AC |
3080 | c = add_cmd ("environment", class_run, unset_environment_command, _("\ |
3081 | Cancel environment variable VAR for the program.\n\ | |
3082 | This does not affect the program until the next \"run\" command."), | |
c5aa993b | 3083 | &unsetlist); |
5ba2abeb | 3084 | set_cmd_completer (c, noop_completer); |
c906108c | 3085 | |
1a966eab AC |
3086 | c = add_cmd ("environment", class_run, set_environment_command, _("\ |
3087 | Set environment variable value to give the program.\n\ | |
c906108c SS |
3088 | Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\ |
3089 | VALUES of environment variables are uninterpreted strings.\n\ | |
1a966eab | 3090 | This does not affect the program until the next \"run\" command."), |
c5aa993b | 3091 | &setlist); |
5ba2abeb | 3092 | set_cmd_completer (c, noop_completer); |
c5aa993b | 3093 | |
1bedd215 AC |
3094 | c = add_com ("path", class_files, path_command, _("\ |
3095 | Add directory DIR(s) to beginning of search path for object files.\n\ | |
c906108c SS |
3096 | $cwd in the path means the current working directory.\n\ |
3097 | This path is equivalent to the $PATH shell variable. It is a list of\n\ | |
3098 | directories, separated by colons. These directories are searched to find\n\ | |
3e43a32a MS |
3099 | fully linked executable files and separately compiled object files as \ |
3100 | needed.")); | |
5ba2abeb | 3101 | set_cmd_completer (c, filename_completer); |
c906108c | 3102 | |
1a966eab AC |
3103 | c = add_cmd ("paths", no_class, path_info, _("\ |
3104 | Current search path for finding object files.\n\ | |
c906108c SS |
3105 | $cwd in the path means the current working directory.\n\ |
3106 | This path is equivalent to the $PATH shell variable. It is a list of\n\ | |
3107 | directories, separated by colons. These directories are searched to find\n\ | |
3e43a32a MS |
3108 | fully linked executable files and separately compiled object files as \ |
3109 | needed."), | |
c906108c | 3110 | &showlist); |
5ba2abeb | 3111 | set_cmd_completer (c, noop_completer); |
c906108c | 3112 | |
2277426b PA |
3113 | add_prefix_cmd ("kill", class_run, kill_command, |
3114 | _("Kill execution of program being debugged."), | |
3115 | &killlist, "kill ", 0, &cmdlist); | |
5fd62852 | 3116 | |
1bedd215 AC |
3117 | add_com ("attach", class_run, attach_command, _("\ |
3118 | Attach to a process or file outside of GDB.\n\ | |
c906108c SS |
3119 | This command attaches to another target, of the same type as your last\n\ |
3120 | \"target\" command (\"info files\" will show your target stack).\n\ | |
3121 | The command may take as argument a process id or a device file.\n\ | |
3122 | For a process id, you must have permission to send the process a signal,\n\ | |
3123 | and it must have the same effective uid as the debugger.\n\ | |
3124 | When using \"attach\" with a process id, the debugger finds the\n\ | |
3125 | program running in the process, looking first in the current working\n\ | |
3126 | directory, or (if not found there) using the source file search path\n\ | |
3127 | (see the \"directory\" command). You can also use the \"file\" command\n\ | |
1bedd215 | 3128 | to specify the program, and to load its symbol table.")); |
c906108c | 3129 | |
f73adfeb | 3130 | add_prefix_cmd ("detach", class_run, detach_command, _("\ |
1bedd215 | 3131 | Detach a process or file previously attached.\n\ |
c906108c | 3132 | If a process, it is no longer traced, and it continues its execution. If\n\ |
f73adfeb AS |
3133 | you were debugging a file, the file is closed and gdb no longer accesses it."), |
3134 | &detachlist, "detach ", 0, &cmdlist); | |
c906108c | 3135 | |
1bedd215 AC |
3136 | add_com ("disconnect", class_run, disconnect_command, _("\ |
3137 | Disconnect from a target.\n\ | |
6ad8ae5c | 3138 | The target will wait for another debugger to connect. Not available for\n\ |
1bedd215 | 3139 | all targets.")); |
6ad8ae5c | 3140 | |
de0bea00 | 3141 | c = add_com ("signal", class_run, signal_command, _("\ |
486c7739 MF |
3142 | Continue program with the specified signal.\n\ |
3143 | Usage: signal SIGNAL\n\ | |
2edda2ff | 3144 | The SIGNAL argument is processed the same as the handle command.\n\ |
486c7739 MF |
3145 | \n\ |
3146 | An argument of \"0\" means continue the program without sending it a signal.\n\ | |
3147 | This is useful in cases where the program stopped because of a signal,\n\ | |
81219e53 DE |
3148 | and you want to resume the program while discarding the signal.\n\ |
3149 | \n\ | |
3150 | In a multi-threaded program the signal is delivered to, or discarded from,\n\ | |
3151 | the current thread only.")); | |
3152 | set_cmd_completer (c, signal_completer); | |
3153 | ||
3154 | c = add_com ("queue-signal", class_run, queue_signal_command, _("\ | |
3155 | Queue a signal to be delivered to the current thread when it is resumed.\n\ | |
3156 | Usage: queue-signal SIGNAL\n\ | |
3157 | The SIGNAL argument is processed the same as the handle command.\n\ | |
3158 | It is an error if the handling state of SIGNAL is \"nopass\".\n\ | |
3159 | \n\ | |
3160 | An argument of \"0\" means remove any currently queued signal from\n\ | |
3161 | the current thread. This is useful in cases where the program stopped\n\ | |
3162 | because of a signal, and you want to resume it while discarding the signal.\n\ | |
3163 | \n\ | |
3164 | In a multi-threaded program the signal is queued with, or discarded from,\n\ | |
3165 | the current thread only.")); | |
de0bea00 | 3166 | set_cmd_completer (c, signal_completer); |
c906108c | 3167 | |
1bedd215 AC |
3168 | add_com ("stepi", class_run, stepi_command, _("\ |
3169 | Step one instruction exactly.\n\ | |
486c7739 MF |
3170 | Usage: stepi [N]\n\ |
3171 | Argument N means step N times (or till program stops for another \ | |
3e43a32a | 3172 | reason).")); |
57b4f16e | 3173 | add_com_alias ("si", "stepi", class_run, 0); |
c906108c | 3174 | |
1bedd215 AC |
3175 | add_com ("nexti", class_run, nexti_command, _("\ |
3176 | Step one instruction, but proceed through subroutine calls.\n\ | |
486c7739 MF |
3177 | Usage: nexti [N]\n\ |
3178 | Argument N means step N times (or till program stops for another \ | |
3e43a32a | 3179 | reason).")); |
57b4f16e | 3180 | add_com_alias ("ni", "nexti", class_run, 0); |
c906108c | 3181 | |
1bedd215 AC |
3182 | add_com ("finish", class_run, finish_command, _("\ |
3183 | Execute until selected stack frame returns.\n\ | |
486c7739 | 3184 | Usage: finish\n\ |
1bedd215 | 3185 | Upon return, the value returned is printed and put in the value history.")); |
0e479716 | 3186 | add_com_alias ("fin", "finish", class_run, 1); |
c906108c | 3187 | |
1bedd215 AC |
3188 | add_com ("next", class_run, next_command, _("\ |
3189 | Step program, proceeding through subroutine calls.\n\ | |
486c7739 MF |
3190 | Usage: next [N]\n\ |
3191 | Unlike \"step\", if the current source line calls a subroutine,\n\ | |
3192 | this command does not enter the subroutine, but instead steps over\n\ | |
dbf6a605 | 3193 | the call, in effect treating it as a single source line.")); |
c906108c | 3194 | add_com_alias ("n", "next", class_run, 1); |
c906108c | 3195 | |
1bedd215 AC |
3196 | add_com ("step", class_run, step_command, _("\ |
3197 | Step program until it reaches a different source line.\n\ | |
486c7739 MF |
3198 | Usage: step [N]\n\ |
3199 | Argument N means step N times (or till program stops for another \ | |
3e43a32a | 3200 | reason).")); |
c906108c SS |
3201 | add_com_alias ("s", "step", class_run, 1); |
3202 | ||
1bedd215 | 3203 | c = add_com ("until", class_run, until_command, _("\ |
590042fc | 3204 | Execute until past the current line or past a LOCATION.\n\ |
1bedd215 | 3205 | Execute until the program reaches a source line greater than the current\n\ |
3e43a32a MS |
3206 | or a specified location (same args as break command) within the current \ |
3207 | frame.")); | |
5ba2abeb | 3208 | set_cmd_completer (c, location_completer); |
c906108c | 3209 | add_com_alias ("u", "until", class_run, 1); |
c5aa993b | 3210 | |
1bedd215 | 3211 | c = add_com ("advance", class_run, advance_command, _("\ |
3e43a32a MS |
3212 | Continue the program up to the given location (same form as args for break \ |
3213 | command).\n\ | |
1bedd215 | 3214 | Execution will also stop upon exit from the current stack frame.")); |
ae66c1fc EZ |
3215 | set_cmd_completer (c, location_completer); |
3216 | ||
1bedd215 AC |
3217 | c = add_com ("jump", class_run, jump_command, _("\ |
3218 | Continue program being debugged at specified line or address.\n\ | |
0a8ba311 | 3219 | Usage: jump LOCATION\n\ |
c906108c | 3220 | Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\ |
1bedd215 | 3221 | for an address to start at.")); |
5ba2abeb | 3222 | set_cmd_completer (c, location_completer); |
c1d780c2 | 3223 | add_com_alias ("j", "jump", class_run, 1); |
c906108c | 3224 | |
df983543 | 3225 | add_com ("continue", class_run, continue_command, _("\ |
1bedd215 | 3226 | Continue program being debugged, after signal or breakpoint.\n\ |
486c7739 | 3227 | Usage: continue [N]\n\ |
c906108c SS |
3228 | If proceeding from breakpoint, a number N may be used as an argument,\n\ |
3229 | which means to set the ignore count of that breakpoint to N - 1 (so that\n\ | |
8cae4b3f PA |
3230 | the breakpoint won't break until the Nth time it is reached).\n\ |
3231 | \n\ | |
3232 | If non-stop mode is enabled, continue only the current thread,\n\ | |
3233 | otherwise all the threads in the program are continued. To \n\ | |
3234 | continue all stopped threads in non-stop mode, use the -a option.\n\ | |
3235 | Specifying -a and an ignore count simultaneously is an error.")); | |
c906108c SS |
3236 | add_com_alias ("c", "cont", class_run, 1); |
3237 | add_com_alias ("fg", "cont", class_run, 1); | |
3238 | ||
1bedd215 | 3239 | c = add_com ("run", class_run, run_command, _("\ |
4e5a4f58 JB |
3240 | Start debugged program.\n" |
3241 | RUN_ARGS_HELP)); | |
5ba2abeb | 3242 | set_cmd_completer (c, filename_completer); |
c906108c | 3243 | add_com_alias ("r", "run", class_run, 1); |
c906108c | 3244 | |
1bedd215 | 3245 | c = add_com ("start", class_run, start_command, _("\ |
4e5a4f58 JB |
3246 | Start the debugged program stopping at the beginning of the main procedure.\n" |
3247 | RUN_ARGS_HELP)); | |
3248 | set_cmd_completer (c, filename_completer); | |
3249 | ||
3250 | c = add_com ("starti", class_run, starti_command, _("\ | |
3251 | Start the debugged program stopping at the first instruction.\n" | |
3252 | RUN_ARGS_HELP)); | |
a4d5f2e0 JB |
3253 | set_cmd_completer (c, filename_completer); |
3254 | ||
0a07590b | 3255 | add_com ("interrupt", class_run, interrupt_command, |
df983543 | 3256 | _("Interrupt the execution of the debugged program.\n\ |
8cae4b3f PA |
3257 | If non-stop mode is enabled, interrupt only the current thread,\n\ |
3258 | otherwise all the threads in the program are stopped. To \n\ | |
3259 | interrupt all running threads in non-stop mode, use the -a option.")); | |
43ff13b4 | 3260 | |
11db9430 | 3261 | c = add_info ("registers", info_registers_command, _("\ |
1bedd215 | 3262 | List of integer registers and their contents, for selected stack frame.\n\ |
b67d92b0 SH |
3263 | One or more register names as argument means describe the given registers.\n\ |
3264 | One or more register group names as argument means describe the registers\n\ | |
3265 | in the named register groups.")); | |
7194c49b | 3266 | add_info_alias ("r", "registers", 1); |
71c24708 | 3267 | set_cmd_completer (c, reg_or_group_completer); |
c906108c | 3268 | |
11db9430 | 3269 | c = add_info ("all-registers", info_all_registers_command, _("\ |
1bedd215 | 3270 | List of all registers and their contents, for selected stack frame.\n\ |
b67d92b0 SH |
3271 | One or more register names as argument means describe the given registers.\n\ |
3272 | One or more register group names as argument means describe the registers\n\ | |
3273 | in the named register groups.")); | |
71c24708 | 3274 | set_cmd_completer (c, reg_or_group_completer); |
c906108c | 3275 | |
11db9430 | 3276 | add_info ("program", info_program_command, |
1bedd215 | 3277 | _("Execution status of the program.")); |
c906108c | 3278 | |
11db9430 | 3279 | add_info ("float", info_float_command, |
590042fc | 3280 | _("Print the status of the floating point unit.")); |
c906108c | 3281 | |
11db9430 | 3282 | add_info ("vector", info_vector_command, |
590042fc | 3283 | _("Print the status of the vector unit.")); |
145b16a9 UW |
3284 | |
3285 | add_prefix_cmd ("proc", class_info, info_proc_cmd, | |
3286 | _("\ | |
73f1bd76 | 3287 | Show additional information about a process.\n\ |
145b16a9 UW |
3288 | Specify any process id, or use the program being debugged by default."), |
3289 | &info_proc_cmdlist, "info proc ", | |
3290 | 1/*allow-unknown*/, &infolist); | |
3291 | ||
3292 | add_cmd ("mappings", class_info, info_proc_cmd_mappings, _("\ | |
73f1bd76 | 3293 | List memory regions mapped by the specified process."), |
145b16a9 UW |
3294 | &info_proc_cmdlist); |
3295 | ||
3296 | add_cmd ("stat", class_info, info_proc_cmd_stat, _("\ | |
3297 | List process info from /proc/PID/stat."), | |
3298 | &info_proc_cmdlist); | |
3299 | ||
3300 | add_cmd ("status", class_info, info_proc_cmd_status, _("\ | |
3301 | List process info from /proc/PID/status."), | |
3302 | &info_proc_cmdlist); | |
3303 | ||
3304 | add_cmd ("cwd", class_info, info_proc_cmd_cwd, _("\ | |
73f1bd76 | 3305 | List current working directory of the specified process."), |
145b16a9 UW |
3306 | &info_proc_cmdlist); |
3307 | ||
3308 | add_cmd ("cmdline", class_info, info_proc_cmd_cmdline, _("\ | |
73f1bd76 | 3309 | List command line arguments of the specified process."), |
145b16a9 UW |
3310 | &info_proc_cmdlist); |
3311 | ||
3312 | add_cmd ("exe", class_info, info_proc_cmd_exe, _("\ | |
73f1bd76 | 3313 | List absolute filename for executable of the specified process."), |
145b16a9 UW |
3314 | &info_proc_cmdlist); |
3315 | ||
e98ee8c4 JB |
3316 | add_cmd ("files", class_info, info_proc_cmd_files, _("\ |
3317 | List files opened by the specified process."), | |
3318 | &info_proc_cmdlist); | |
3319 | ||
145b16a9 | 3320 | add_cmd ("all", class_info, info_proc_cmd_all, _("\ |
73f1bd76 | 3321 | List all available info about the specified process."), |
145b16a9 | 3322 | &info_proc_cmdlist); |
000439d5 TT |
3323 | |
3324 | add_setshow_boolean_cmd ("finish", class_support, | |
3325 | &user_print_options.finish_print, _("\ | |
3326 | Set whether `finish' prints the return value."), _("\ | |
3327 | Show whether `finish' prints the return value."), NULL, | |
3328 | NULL, | |
3329 | show_print_finish, | |
3330 | &setprintlist, &showprintlist); | |
c906108c | 3331 | } |