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