2012-12-10 Paul Koning <paul_koning@dell.com>
[deliverable/binutils-gdb.git] / gdb / infrun.c
CommitLineData
ca557f44
AC
1/* Target-struct-independent code to start (run) and stop an inferior
2 process.
8926118c 3
0b302171 4 Copyright (C) 1986-2012 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "defs.h"
22#include "gdb_string.h"
23#include <ctype.h>
24#include "symtab.h"
25#include "frame.h"
26#include "inferior.h"
60250e8b 27#include "exceptions.h"
c906108c 28#include "breakpoint.h"
03f2053f 29#include "gdb_wait.h"
c906108c
SS
30#include "gdbcore.h"
31#include "gdbcmd.h"
210661e7 32#include "cli/cli-script.h"
c906108c
SS
33#include "target.h"
34#include "gdbthread.h"
35#include "annotate.h"
1adeb98a 36#include "symfile.h"
7a292a7a 37#include "top.h"
c906108c 38#include <signal.h>
2acceee2 39#include "inf-loop.h"
4e052eda 40#include "regcache.h"
fd0407d6 41#include "value.h"
06600e06 42#include "observer.h"
f636b87d 43#include "language.h"
a77053c2 44#include "solib.h"
f17517ea 45#include "main.h"
186c406b
TT
46#include "dictionary.h"
47#include "block.h"
9f976b41 48#include "gdb_assert.h"
034dad6f 49#include "mi/mi-common.h"
4f8d22e3 50#include "event-top.h"
96429cc8 51#include "record.h"
edb3359d 52#include "inline-frame.h"
4efc6507 53#include "jit.h"
06cd862c 54#include "tracepoint.h"
be34f849 55#include "continuations.h"
b4a14fd0 56#include "interps.h"
1bfeeb0f 57#include "skip.h"
28106bc2
SDJ
58#include "probe.h"
59#include "objfiles.h"
de0bea00 60#include "completer.h"
9107fc8d 61#include "target-descriptions.h"
c906108c
SS
62
63/* Prototypes for local functions */
64
96baa820 65static void signals_info (char *, int);
c906108c 66
96baa820 67static void handle_command (char *, int);
c906108c 68
2ea28649 69static void sig_print_info (enum gdb_signal);
c906108c 70
96baa820 71static void sig_print_header (void);
c906108c 72
74b7792f 73static void resume_cleanups (void *);
c906108c 74
96baa820 75static int hook_stop_stub (void *);
c906108c 76
96baa820
JM
77static int restore_selected_frame (void *);
78
4ef3f3be 79static int follow_fork (void);
96baa820
JM
80
81static void set_schedlock_func (char *args, int from_tty,
488f131b 82 struct cmd_list_element *c);
96baa820 83
a289b8f6
JK
84static int currently_stepping (struct thread_info *tp);
85
b3444185
PA
86static int currently_stepping_or_nexting_callback (struct thread_info *tp,
87 void *data);
a7212384 88
96baa820
JM
89static void xdb_handle_command (char *args, int from_tty);
90
6a6b96b9 91static int prepare_to_proceed (int);
ea67f13b 92
33d62d64
JK
93static void print_exited_reason (int exitstatus);
94
2ea28649 95static void print_signal_exited_reason (enum gdb_signal siggnal);
33d62d64
JK
96
97static void print_no_history_reason (void);
98
2ea28649 99static void print_signal_received_reason (enum gdb_signal siggnal);
33d62d64
JK
100
101static void print_end_stepping_range_reason (void);
102
96baa820 103void _initialize_infrun (void);
43ff13b4 104
e58b0e63
PA
105void nullify_last_target_wait_ptid (void);
106
2c03e5be 107static void insert_hp_step_resume_breakpoint_at_frame (struct frame_info *);
2484c66b
UW
108
109static void insert_step_resume_breakpoint_at_caller (struct frame_info *);
110
2484c66b
UW
111static void insert_longjmp_resume_breakpoint (struct gdbarch *, CORE_ADDR);
112
5fbbeb29
CF
113/* When set, stop the 'step' command if we enter a function which has
114 no line number information. The normal behavior is that we step
115 over such function. */
116int step_stop_if_no_debug = 0;
920d2a44
AC
117static void
118show_step_stop_if_no_debug (struct ui_file *file, int from_tty,
119 struct cmd_list_element *c, const char *value)
120{
121 fprintf_filtered (file, _("Mode of the step operation is %s.\n"), value);
122}
5fbbeb29 123
1777feb0 124/* In asynchronous mode, but simulating synchronous execution. */
96baa820 125
43ff13b4
JM
126int sync_execution = 0;
127
c906108c
SS
128/* wait_for_inferior and normal_stop use this to notify the user
129 when the inferior stopped in a different thread than it had been
96baa820
JM
130 running in. */
131
39f77062 132static ptid_t previous_inferior_ptid;
7a292a7a 133
6c95b8df
PA
134/* Default behavior is to detach newly forked processes (legacy). */
135int detach_fork = 1;
136
237fc4c9
PA
137int debug_displaced = 0;
138static void
139show_debug_displaced (struct ui_file *file, int from_tty,
140 struct cmd_list_element *c, const char *value)
141{
142 fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
143}
144
ccce17b0 145unsigned int debug_infrun = 0;
920d2a44
AC
146static void
147show_debug_infrun (struct ui_file *file, int from_tty,
148 struct cmd_list_element *c, const char *value)
149{
150 fprintf_filtered (file, _("Inferior debugging is %s.\n"), value);
151}
527159b7 152
03583c20
UW
153
154/* Support for disabling address space randomization. */
155
156int disable_randomization = 1;
157
158static void
159show_disable_randomization (struct ui_file *file, int from_tty,
160 struct cmd_list_element *c, const char *value)
161{
162 if (target_supports_disable_randomization ())
163 fprintf_filtered (file,
164 _("Disabling randomization of debuggee's "
165 "virtual address space is %s.\n"),
166 value);
167 else
168 fputs_filtered (_("Disabling randomization of debuggee's "
169 "virtual address space is unsupported on\n"
170 "this platform.\n"), file);
171}
172
173static void
174set_disable_randomization (char *args, int from_tty,
175 struct cmd_list_element *c)
176{
177 if (!target_supports_disable_randomization ())
178 error (_("Disabling randomization of debuggee's "
179 "virtual address space is unsupported on\n"
180 "this platform."));
181}
182
183
d4f3574e
SS
184/* If the program uses ELF-style shared libraries, then calls to
185 functions in shared libraries go through stubs, which live in a
186 table called the PLT (Procedure Linkage Table). The first time the
187 function is called, the stub sends control to the dynamic linker,
188 which looks up the function's real address, patches the stub so
189 that future calls will go directly to the function, and then passes
190 control to the function.
191
192 If we are stepping at the source level, we don't want to see any of
193 this --- we just want to skip over the stub and the dynamic linker.
194 The simple approach is to single-step until control leaves the
195 dynamic linker.
196
ca557f44
AC
197 However, on some systems (e.g., Red Hat's 5.2 distribution) the
198 dynamic linker calls functions in the shared C library, so you
199 can't tell from the PC alone whether the dynamic linker is still
200 running. In this case, we use a step-resume breakpoint to get us
201 past the dynamic linker, as if we were using "next" to step over a
202 function call.
d4f3574e 203
cfd8ab24 204 in_solib_dynsym_resolve_code() says whether we're in the dynamic
d4f3574e
SS
205 linker code or not. Normally, this means we single-step. However,
206 if SKIP_SOLIB_RESOLVER then returns non-zero, then its value is an
207 address where we can place a step-resume breakpoint to get past the
208 linker's symbol resolution function.
209
cfd8ab24 210 in_solib_dynsym_resolve_code() can generally be implemented in a
d4f3574e
SS
211 pretty portable way, by comparing the PC against the address ranges
212 of the dynamic linker's sections.
213
214 SKIP_SOLIB_RESOLVER is generally going to be system-specific, since
215 it depends on internal details of the dynamic linker. It's usually
216 not too hard to figure out where to put a breakpoint, but it
217 certainly isn't portable. SKIP_SOLIB_RESOLVER should do plenty of
218 sanity checking. If it can't figure things out, returning zero and
219 getting the (possibly confusing) stepping behavior is better than
220 signalling an error, which will obscure the change in the
221 inferior's state. */
c906108c 222
c906108c
SS
223/* This function returns TRUE if pc is the address of an instruction
224 that lies within the dynamic linker (such as the event hook, or the
225 dld itself).
226
227 This function must be used only when a dynamic linker event has
228 been caught, and the inferior is being stepped out of the hook, or
229 undefined results are guaranteed. */
230
231#ifndef SOLIB_IN_DYNAMIC_LINKER
232#define SOLIB_IN_DYNAMIC_LINKER(pid,pc) 0
233#endif
234
d914c394
SS
235/* "Observer mode" is somewhat like a more extreme version of
236 non-stop, in which all GDB operations that might affect the
237 target's execution have been disabled. */
238
239static int non_stop_1 = 0;
240
241int observer_mode = 0;
242static int observer_mode_1 = 0;
243
244static void
245set_observer_mode (char *args, int from_tty,
246 struct cmd_list_element *c)
247{
248 extern int pagination_enabled;
249
250 if (target_has_execution)
251 {
252 observer_mode_1 = observer_mode;
253 error (_("Cannot change this setting while the inferior is running."));
254 }
255
256 observer_mode = observer_mode_1;
257
258 may_write_registers = !observer_mode;
259 may_write_memory = !observer_mode;
260 may_insert_breakpoints = !observer_mode;
261 may_insert_tracepoints = !observer_mode;
262 /* We can insert fast tracepoints in or out of observer mode,
263 but enable them if we're going into this mode. */
264 if (observer_mode)
265 may_insert_fast_tracepoints = 1;
266 may_stop = !observer_mode;
267 update_target_permissions ();
268
269 /* Going *into* observer mode we must force non-stop, then
270 going out we leave it that way. */
271 if (observer_mode)
272 {
273 target_async_permitted = 1;
274 pagination_enabled = 0;
275 non_stop = non_stop_1 = 1;
276 }
277
278 if (from_tty)
279 printf_filtered (_("Observer mode is now %s.\n"),
280 (observer_mode ? "on" : "off"));
281}
282
283static void
284show_observer_mode (struct ui_file *file, int from_tty,
285 struct cmd_list_element *c, const char *value)
286{
287 fprintf_filtered (file, _("Observer mode is %s.\n"), value);
288}
289
290/* This updates the value of observer mode based on changes in
291 permissions. Note that we are deliberately ignoring the values of
292 may-write-registers and may-write-memory, since the user may have
293 reason to enable these during a session, for instance to turn on a
294 debugging-related global. */
295
296void
297update_observer_mode (void)
298{
299 int newval;
300
301 newval = (!may_insert_breakpoints
302 && !may_insert_tracepoints
303 && may_insert_fast_tracepoints
304 && !may_stop
305 && non_stop);
306
307 /* Let the user know if things change. */
308 if (newval != observer_mode)
309 printf_filtered (_("Observer mode is now %s.\n"),
310 (newval ? "on" : "off"));
311
312 observer_mode = observer_mode_1 = newval;
313}
c2c6d25f 314
c906108c
SS
315/* Tables of how to react to signals; the user sets them. */
316
317static unsigned char *signal_stop;
318static unsigned char *signal_print;
319static unsigned char *signal_program;
320
2455069d
UW
321/* Table of signals that the target may silently handle.
322 This is automatically determined from the flags above,
323 and simply cached here. */
324static unsigned char *signal_pass;
325
c906108c
SS
326#define SET_SIGS(nsigs,sigs,flags) \
327 do { \
328 int signum = (nsigs); \
329 while (signum-- > 0) \
330 if ((sigs)[signum]) \
331 (flags)[signum] = 1; \
332 } while (0)
333
334#define UNSET_SIGS(nsigs,sigs,flags) \
335 do { \
336 int signum = (nsigs); \
337 while (signum-- > 0) \
338 if ((sigs)[signum]) \
339 (flags)[signum] = 0; \
340 } while (0)
341
9b224c5e
PA
342/* Update the target's copy of SIGNAL_PROGRAM. The sole purpose of
343 this function is to avoid exporting `signal_program'. */
344
345void
346update_signals_program_target (void)
347{
a493e3e2 348 target_program_signals ((int) GDB_SIGNAL_LAST, signal_program);
9b224c5e
PA
349}
350
1777feb0 351/* Value to pass to target_resume() to cause all threads to resume. */
39f77062 352
edb3359d 353#define RESUME_ALL minus_one_ptid
c906108c
SS
354
355/* Command list pointer for the "stop" placeholder. */
356
357static struct cmd_list_element *stop_command;
358
c906108c
SS
359/* Function inferior was in as of last step command. */
360
361static struct symbol *step_start_function;
362
c906108c
SS
363/* Nonzero if we want to give control to the user when we're notified
364 of shared library events by the dynamic linker. */
628fe4e4 365int stop_on_solib_events;
920d2a44
AC
366static void
367show_stop_on_solib_events (struct ui_file *file, int from_tty,
368 struct cmd_list_element *c, const char *value)
369{
370 fprintf_filtered (file, _("Stopping for shared library events is %s.\n"),
371 value);
372}
c906108c 373
c906108c
SS
374/* Nonzero means expecting a trace trap
375 and should stop the inferior and return silently when it happens. */
376
377int stop_after_trap;
378
642fd101
DE
379/* Save register contents here when executing a "finish" command or are
380 about to pop a stack dummy frame, if-and-only-if proceed_to_finish is set.
c906108c
SS
381 Thus this contains the return value from the called function (assuming
382 values are returned in a register). */
383
72cec141 384struct regcache *stop_registers;
c906108c 385
c906108c
SS
386/* Nonzero after stop if current stack frame should be printed. */
387
388static int stop_print_frame;
389
e02bc4cc 390/* This is a cached copy of the pid/waitstatus of the last event
9a4105ab
AC
391 returned by target_wait()/deprecated_target_wait_hook(). This
392 information is returned by get_last_target_status(). */
39f77062 393static ptid_t target_last_wait_ptid;
e02bc4cc
DS
394static struct target_waitstatus target_last_waitstatus;
395
0d1e5fa7
PA
396static void context_switch (ptid_t ptid);
397
4e1c45ea 398void init_thread_stepping_state (struct thread_info *tss);
0d1e5fa7 399
7a76f5b8 400static void init_infwait_state (void);
a474d7c2 401
53904c9e
AC
402static const char follow_fork_mode_child[] = "child";
403static const char follow_fork_mode_parent[] = "parent";
404
40478521 405static const char *const follow_fork_mode_kind_names[] = {
53904c9e
AC
406 follow_fork_mode_child,
407 follow_fork_mode_parent,
408 NULL
ef346e04 409};
c906108c 410
53904c9e 411static const char *follow_fork_mode_string = follow_fork_mode_parent;
920d2a44
AC
412static void
413show_follow_fork_mode_string (struct ui_file *file, int from_tty,
414 struct cmd_list_element *c, const char *value)
415{
3e43a32a
MS
416 fprintf_filtered (file,
417 _("Debugger response to a program "
418 "call of fork or vfork is \"%s\".\n"),
920d2a44
AC
419 value);
420}
c906108c
SS
421\f
422
e58b0e63
PA
423/* Tell the target to follow the fork we're stopped at. Returns true
424 if the inferior should be resumed; false, if the target for some
425 reason decided it's best not to resume. */
426
6604731b 427static int
4ef3f3be 428follow_fork (void)
c906108c 429{
ea1dd7bc 430 int follow_child = (follow_fork_mode_string == follow_fork_mode_child);
e58b0e63
PA
431 int should_resume = 1;
432 struct thread_info *tp;
433
434 /* Copy user stepping state to the new inferior thread. FIXME: the
435 followed fork child thread should have a copy of most of the
4e3990f4
DE
436 parent thread structure's run control related fields, not just these.
437 Initialized to avoid "may be used uninitialized" warnings from gcc. */
438 struct breakpoint *step_resume_breakpoint = NULL;
186c406b 439 struct breakpoint *exception_resume_breakpoint = NULL;
4e3990f4
DE
440 CORE_ADDR step_range_start = 0;
441 CORE_ADDR step_range_end = 0;
442 struct frame_id step_frame_id = { 0 };
e58b0e63
PA
443
444 if (!non_stop)
445 {
446 ptid_t wait_ptid;
447 struct target_waitstatus wait_status;
448
449 /* Get the last target status returned by target_wait(). */
450 get_last_target_status (&wait_ptid, &wait_status);
451
452 /* If not stopped at a fork event, then there's nothing else to
453 do. */
454 if (wait_status.kind != TARGET_WAITKIND_FORKED
455 && wait_status.kind != TARGET_WAITKIND_VFORKED)
456 return 1;
457
458 /* Check if we switched over from WAIT_PTID, since the event was
459 reported. */
460 if (!ptid_equal (wait_ptid, minus_one_ptid)
461 && !ptid_equal (inferior_ptid, wait_ptid))
462 {
463 /* We did. Switch back to WAIT_PTID thread, to tell the
464 target to follow it (in either direction). We'll
465 afterwards refuse to resume, and inform the user what
466 happened. */
467 switch_to_thread (wait_ptid);
468 should_resume = 0;
469 }
470 }
471
472 tp = inferior_thread ();
473
474 /* If there were any forks/vforks that were caught and are now to be
475 followed, then do so now. */
476 switch (tp->pending_follow.kind)
477 {
478 case TARGET_WAITKIND_FORKED:
479 case TARGET_WAITKIND_VFORKED:
480 {
481 ptid_t parent, child;
482
483 /* If the user did a next/step, etc, over a fork call,
484 preserve the stepping state in the fork child. */
485 if (follow_child && should_resume)
486 {
8358c15c
JK
487 step_resume_breakpoint = clone_momentary_breakpoint
488 (tp->control.step_resume_breakpoint);
16c381f0
JK
489 step_range_start = tp->control.step_range_start;
490 step_range_end = tp->control.step_range_end;
491 step_frame_id = tp->control.step_frame_id;
186c406b
TT
492 exception_resume_breakpoint
493 = clone_momentary_breakpoint (tp->control.exception_resume_breakpoint);
e58b0e63
PA
494
495 /* For now, delete the parent's sr breakpoint, otherwise,
496 parent/child sr breakpoints are considered duplicates,
497 and the child version will not be installed. Remove
498 this when the breakpoints module becomes aware of
499 inferiors and address spaces. */
500 delete_step_resume_breakpoint (tp);
16c381f0
JK
501 tp->control.step_range_start = 0;
502 tp->control.step_range_end = 0;
503 tp->control.step_frame_id = null_frame_id;
186c406b 504 delete_exception_resume_breakpoint (tp);
e58b0e63
PA
505 }
506
507 parent = inferior_ptid;
508 child = tp->pending_follow.value.related_pid;
509
510 /* Tell the target to do whatever is necessary to follow
511 either parent or child. */
512 if (target_follow_fork (follow_child))
513 {
514 /* Target refused to follow, or there's some other reason
515 we shouldn't resume. */
516 should_resume = 0;
517 }
518 else
519 {
520 /* This pending follow fork event is now handled, one way
521 or another. The previous selected thread may be gone
522 from the lists by now, but if it is still around, need
523 to clear the pending follow request. */
e09875d4 524 tp = find_thread_ptid (parent);
e58b0e63
PA
525 if (tp)
526 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
527
528 /* This makes sure we don't try to apply the "Switched
529 over from WAIT_PID" logic above. */
530 nullify_last_target_wait_ptid ();
531
1777feb0 532 /* If we followed the child, switch to it... */
e58b0e63
PA
533 if (follow_child)
534 {
535 switch_to_thread (child);
536
537 /* ... and preserve the stepping state, in case the
538 user was stepping over the fork call. */
539 if (should_resume)
540 {
541 tp = inferior_thread ();
8358c15c
JK
542 tp->control.step_resume_breakpoint
543 = step_resume_breakpoint;
16c381f0
JK
544 tp->control.step_range_start = step_range_start;
545 tp->control.step_range_end = step_range_end;
546 tp->control.step_frame_id = step_frame_id;
186c406b
TT
547 tp->control.exception_resume_breakpoint
548 = exception_resume_breakpoint;
e58b0e63
PA
549 }
550 else
551 {
552 /* If we get here, it was because we're trying to
553 resume from a fork catchpoint, but, the user
554 has switched threads away from the thread that
555 forked. In that case, the resume command
556 issued is most likely not applicable to the
557 child, so just warn, and refuse to resume. */
3e43a32a
MS
558 warning (_("Not resuming: switched threads "
559 "before following fork child.\n"));
e58b0e63
PA
560 }
561
562 /* Reset breakpoints in the child as appropriate. */
563 follow_inferior_reset_breakpoints ();
564 }
565 else
566 switch_to_thread (parent);
567 }
568 }
569 break;
570 case TARGET_WAITKIND_SPURIOUS:
571 /* Nothing to follow. */
572 break;
573 default:
574 internal_error (__FILE__, __LINE__,
575 "Unexpected pending_follow.kind %d\n",
576 tp->pending_follow.kind);
577 break;
578 }
c906108c 579
e58b0e63 580 return should_resume;
c906108c
SS
581}
582
6604731b
DJ
583void
584follow_inferior_reset_breakpoints (void)
c906108c 585{
4e1c45ea
PA
586 struct thread_info *tp = inferior_thread ();
587
6604731b
DJ
588 /* Was there a step_resume breakpoint? (There was if the user
589 did a "next" at the fork() call.) If so, explicitly reset its
590 thread number.
591
592 step_resumes are a form of bp that are made to be per-thread.
593 Since we created the step_resume bp when the parent process
594 was being debugged, and now are switching to the child process,
595 from the breakpoint package's viewpoint, that's a switch of
596 "threads". We must update the bp's notion of which thread
597 it is for, or it'll be ignored when it triggers. */
598
8358c15c
JK
599 if (tp->control.step_resume_breakpoint)
600 breakpoint_re_set_thread (tp->control.step_resume_breakpoint);
6604731b 601
186c406b
TT
602 if (tp->control.exception_resume_breakpoint)
603 breakpoint_re_set_thread (tp->control.exception_resume_breakpoint);
604
6604731b
DJ
605 /* Reinsert all breakpoints in the child. The user may have set
606 breakpoints after catching the fork, in which case those
607 were never set in the child, but only in the parent. This makes
608 sure the inserted breakpoints match the breakpoint list. */
609
610 breakpoint_re_set ();
611 insert_breakpoints ();
c906108c 612}
c906108c 613
6c95b8df
PA
614/* The child has exited or execed: resume threads of the parent the
615 user wanted to be executing. */
616
617static int
618proceed_after_vfork_done (struct thread_info *thread,
619 void *arg)
620{
621 int pid = * (int *) arg;
622
623 if (ptid_get_pid (thread->ptid) == pid
624 && is_running (thread->ptid)
625 && !is_executing (thread->ptid)
626 && !thread->stop_requested
a493e3e2 627 && thread->suspend.stop_signal == GDB_SIGNAL_0)
6c95b8df
PA
628 {
629 if (debug_infrun)
630 fprintf_unfiltered (gdb_stdlog,
631 "infrun: resuming vfork parent thread %s\n",
632 target_pid_to_str (thread->ptid));
633
634 switch_to_thread (thread->ptid);
635 clear_proceed_status ();
a493e3e2 636 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT, 0);
6c95b8df
PA
637 }
638
639 return 0;
640}
641
642/* Called whenever we notice an exec or exit event, to handle
643 detaching or resuming a vfork parent. */
644
645static void
646handle_vfork_child_exec_or_exit (int exec)
647{
648 struct inferior *inf = current_inferior ();
649
650 if (inf->vfork_parent)
651 {
652 int resume_parent = -1;
653
654 /* This exec or exit marks the end of the shared memory region
655 between the parent and the child. If the user wanted to
656 detach from the parent, now is the time. */
657
658 if (inf->vfork_parent->pending_detach)
659 {
660 struct thread_info *tp;
661 struct cleanup *old_chain;
662 struct program_space *pspace;
663 struct address_space *aspace;
664
1777feb0 665 /* follow-fork child, detach-on-fork on. */
6c95b8df 666
68c9da30
PA
667 inf->vfork_parent->pending_detach = 0;
668
f50f4e56
PA
669 if (!exec)
670 {
671 /* If we're handling a child exit, then inferior_ptid
672 points at the inferior's pid, not to a thread. */
673 old_chain = save_inferior_ptid ();
674 save_current_program_space ();
675 save_current_inferior ();
676 }
677 else
678 old_chain = save_current_space_and_thread ();
6c95b8df
PA
679
680 /* We're letting loose of the parent. */
681 tp = any_live_thread_of_process (inf->vfork_parent->pid);
682 switch_to_thread (tp->ptid);
683
684 /* We're about to detach from the parent, which implicitly
685 removes breakpoints from its address space. There's a
686 catch here: we want to reuse the spaces for the child,
687 but, parent/child are still sharing the pspace at this
688 point, although the exec in reality makes the kernel give
689 the child a fresh set of new pages. The problem here is
690 that the breakpoints module being unaware of this, would
691 likely chose the child process to write to the parent
692 address space. Swapping the child temporarily away from
693 the spaces has the desired effect. Yes, this is "sort
694 of" a hack. */
695
696 pspace = inf->pspace;
697 aspace = inf->aspace;
698 inf->aspace = NULL;
699 inf->pspace = NULL;
700
701 if (debug_infrun || info_verbose)
702 {
703 target_terminal_ours ();
704
705 if (exec)
706 fprintf_filtered (gdb_stdlog,
3e43a32a
MS
707 "Detaching vfork parent process "
708 "%d after child exec.\n",
6c95b8df
PA
709 inf->vfork_parent->pid);
710 else
711 fprintf_filtered (gdb_stdlog,
3e43a32a
MS
712 "Detaching vfork parent process "
713 "%d after child exit.\n",
6c95b8df
PA
714 inf->vfork_parent->pid);
715 }
716
717 target_detach (NULL, 0);
718
719 /* Put it back. */
720 inf->pspace = pspace;
721 inf->aspace = aspace;
722
723 do_cleanups (old_chain);
724 }
725 else if (exec)
726 {
727 /* We're staying attached to the parent, so, really give the
728 child a new address space. */
729 inf->pspace = add_program_space (maybe_new_address_space ());
730 inf->aspace = inf->pspace->aspace;
731 inf->removable = 1;
732 set_current_program_space (inf->pspace);
733
734 resume_parent = inf->vfork_parent->pid;
735
736 /* Break the bonds. */
737 inf->vfork_parent->vfork_child = NULL;
738 }
739 else
740 {
741 struct cleanup *old_chain;
742 struct program_space *pspace;
743
744 /* If this is a vfork child exiting, then the pspace and
745 aspaces were shared with the parent. Since we're
746 reporting the process exit, we'll be mourning all that is
747 found in the address space, and switching to null_ptid,
748 preparing to start a new inferior. But, since we don't
749 want to clobber the parent's address/program spaces, we
750 go ahead and create a new one for this exiting
751 inferior. */
752
753 /* Switch to null_ptid, so that clone_program_space doesn't want
754 to read the selected frame of a dead process. */
755 old_chain = save_inferior_ptid ();
756 inferior_ptid = null_ptid;
757
758 /* This inferior is dead, so avoid giving the breakpoints
759 module the option to write through to it (cloning a
760 program space resets breakpoints). */
761 inf->aspace = NULL;
762 inf->pspace = NULL;
763 pspace = add_program_space (maybe_new_address_space ());
764 set_current_program_space (pspace);
765 inf->removable = 1;
7dcd53a0 766 inf->symfile_flags = SYMFILE_NO_READ;
6c95b8df
PA
767 clone_program_space (pspace, inf->vfork_parent->pspace);
768 inf->pspace = pspace;
769 inf->aspace = pspace->aspace;
770
771 /* Put back inferior_ptid. We'll continue mourning this
1777feb0 772 inferior. */
6c95b8df
PA
773 do_cleanups (old_chain);
774
775 resume_parent = inf->vfork_parent->pid;
776 /* Break the bonds. */
777 inf->vfork_parent->vfork_child = NULL;
778 }
779
780 inf->vfork_parent = NULL;
781
782 gdb_assert (current_program_space == inf->pspace);
783
784 if (non_stop && resume_parent != -1)
785 {
786 /* If the user wanted the parent to be running, let it go
787 free now. */
788 struct cleanup *old_chain = make_cleanup_restore_current_thread ();
789
790 if (debug_infrun)
3e43a32a
MS
791 fprintf_unfiltered (gdb_stdlog,
792 "infrun: resuming vfork parent process %d\n",
6c95b8df
PA
793 resume_parent);
794
795 iterate_over_threads (proceed_after_vfork_done, &resume_parent);
796
797 do_cleanups (old_chain);
798 }
799 }
800}
801
802/* Enum strings for "set|show displaced-stepping". */
803
804static const char follow_exec_mode_new[] = "new";
805static const char follow_exec_mode_same[] = "same";
40478521 806static const char *const follow_exec_mode_names[] =
6c95b8df
PA
807{
808 follow_exec_mode_new,
809 follow_exec_mode_same,
810 NULL,
811};
812
813static const char *follow_exec_mode_string = follow_exec_mode_same;
814static void
815show_follow_exec_mode_string (struct ui_file *file, int from_tty,
816 struct cmd_list_element *c, const char *value)
817{
818 fprintf_filtered (file, _("Follow exec mode is \"%s\".\n"), value);
819}
820
1777feb0 821/* EXECD_PATHNAME is assumed to be non-NULL. */
1adeb98a 822
c906108c 823static void
3a3e9ee3 824follow_exec (ptid_t pid, char *execd_pathname)
c906108c 825{
4e1c45ea 826 struct thread_info *th = inferior_thread ();
6c95b8df 827 struct inferior *inf = current_inferior ();
7a292a7a 828
c906108c
SS
829 /* This is an exec event that we actually wish to pay attention to.
830 Refresh our symbol table to the newly exec'd program, remove any
831 momentary bp's, etc.
832
833 If there are breakpoints, they aren't really inserted now,
834 since the exec() transformed our inferior into a fresh set
835 of instructions.
836
837 We want to preserve symbolic breakpoints on the list, since
838 we have hopes that they can be reset after the new a.out's
839 symbol table is read.
840
841 However, any "raw" breakpoints must be removed from the list
842 (e.g., the solib bp's), since their address is probably invalid
843 now.
844
845 And, we DON'T want to call delete_breakpoints() here, since
846 that may write the bp's "shadow contents" (the instruction
847 value that was overwritten witha TRAP instruction). Since
1777feb0 848 we now have a new a.out, those shadow contents aren't valid. */
6c95b8df
PA
849
850 mark_breakpoints_out ();
851
c906108c
SS
852 update_breakpoints_after_exec ();
853
854 /* If there was one, it's gone now. We cannot truly step-to-next
1777feb0 855 statement through an exec(). */
8358c15c 856 th->control.step_resume_breakpoint = NULL;
186c406b 857 th->control.exception_resume_breakpoint = NULL;
16c381f0
JK
858 th->control.step_range_start = 0;
859 th->control.step_range_end = 0;
c906108c 860
a75724bc
PA
861 /* The target reports the exec event to the main thread, even if
862 some other thread does the exec, and even if the main thread was
863 already stopped --- if debugging in non-stop mode, it's possible
864 the user had the main thread held stopped in the previous image
865 --- release it now. This is the same behavior as step-over-exec
866 with scheduler-locking on in all-stop mode. */
867 th->stop_requested = 0;
868
1777feb0 869 /* What is this a.out's name? */
6c95b8df
PA
870 printf_unfiltered (_("%s is executing new program: %s\n"),
871 target_pid_to_str (inferior_ptid),
872 execd_pathname);
c906108c
SS
873
874 /* We've followed the inferior through an exec. Therefore, the
1777feb0 875 inferior has essentially been killed & reborn. */
7a292a7a 876
c906108c 877 gdb_flush (gdb_stdout);
6ca15a4b
PA
878
879 breakpoint_init_inferior (inf_execd);
e85a822c
DJ
880
881 if (gdb_sysroot && *gdb_sysroot)
882 {
883 char *name = alloca (strlen (gdb_sysroot)
884 + strlen (execd_pathname)
885 + 1);
abbb1732 886
e85a822c
DJ
887 strcpy (name, gdb_sysroot);
888 strcat (name, execd_pathname);
889 execd_pathname = name;
890 }
c906108c 891
cce9b6bf
PA
892 /* Reset the shared library package. This ensures that we get a
893 shlib event when the child reaches "_start", at which point the
894 dld will have had a chance to initialize the child. */
895 /* Also, loading a symbol file below may trigger symbol lookups, and
896 we don't want those to be satisfied by the libraries of the
897 previous incarnation of this process. */
898 no_shared_libraries (NULL, 0);
899
6c95b8df
PA
900 if (follow_exec_mode_string == follow_exec_mode_new)
901 {
902 struct program_space *pspace;
6c95b8df
PA
903
904 /* The user wants to keep the old inferior and program spaces
905 around. Create a new fresh one, and switch to it. */
906
907 inf = add_inferior (current_inferior ()->pid);
908 pspace = add_program_space (maybe_new_address_space ());
909 inf->pspace = pspace;
910 inf->aspace = pspace->aspace;
911
912 exit_inferior_num_silent (current_inferior ()->num);
913
914 set_current_inferior (inf);
915 set_current_program_space (pspace);
916 }
9107fc8d
PA
917 else
918 {
919 /* The old description may no longer be fit for the new image.
920 E.g, a 64-bit process exec'ed a 32-bit process. Clear the
921 old description; we'll read a new one below. No need to do
922 this on "follow-exec-mode new", as the old inferior stays
923 around (its description is later cleared/refetched on
924 restart). */
925 target_clear_description ();
926 }
6c95b8df
PA
927
928 gdb_assert (current_program_space == inf->pspace);
929
1777feb0 930 /* That a.out is now the one to use. */
6c95b8df
PA
931 exec_file_attach (execd_pathname, 0);
932
c1e56572
JK
933 /* SYMFILE_DEFER_BP_RESET is used as the proper displacement for PIE
934 (Position Independent Executable) main symbol file will get applied by
935 solib_create_inferior_hook below. breakpoint_re_set would fail to insert
936 the breakpoints with the zero displacement. */
937
7dcd53a0
TT
938 symbol_file_add (execd_pathname,
939 (inf->symfile_flags
940 | SYMFILE_MAINLINE | SYMFILE_DEFER_BP_RESET),
c1e56572
JK
941 NULL, 0);
942
7dcd53a0
TT
943 if ((inf->symfile_flags & SYMFILE_NO_READ) == 0)
944 set_initial_language ();
c906108c 945
9107fc8d
PA
946 /* If the target can specify a description, read it. Must do this
947 after flipping to the new executable (because the target supplied
948 description must be compatible with the executable's
949 architecture, and the old executable may e.g., be 32-bit, while
950 the new one 64-bit), and before anything involving memory or
951 registers. */
952 target_find_description ();
953
7a292a7a 954#ifdef SOLIB_CREATE_INFERIOR_HOOK
39f77062 955 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
a77053c2 956#else
268a4a75 957 solib_create_inferior_hook (0);
7a292a7a 958#endif
c906108c 959
4efc6507
DE
960 jit_inferior_created_hook ();
961
c1e56572
JK
962 breakpoint_re_set ();
963
c906108c
SS
964 /* Reinsert all breakpoints. (Those which were symbolic have
965 been reset to the proper address in the new a.out, thanks
1777feb0 966 to symbol_file_command...). */
c906108c
SS
967 insert_breakpoints ();
968
969 /* The next resume of this inferior should bring it to the shlib
970 startup breakpoints. (If the user had also set bp's on
971 "main" from the old (parent) process, then they'll auto-
1777feb0 972 matically get reset there in the new process.). */
c906108c
SS
973}
974
975/* Non-zero if we just simulating a single-step. This is needed
976 because we cannot remove the breakpoints in the inferior process
977 until after the `wait' in `wait_for_inferior'. */
978static int singlestep_breakpoints_inserted_p = 0;
9f976b41
DJ
979
980/* The thread we inserted single-step breakpoints for. */
981static ptid_t singlestep_ptid;
982
fd48f117
DJ
983/* PC when we started this single-step. */
984static CORE_ADDR singlestep_pc;
985
9f976b41
DJ
986/* If another thread hit the singlestep breakpoint, we save the original
987 thread here so that we can resume single-stepping it later. */
988static ptid_t saved_singlestep_ptid;
989static int stepping_past_singlestep_breakpoint;
6a6b96b9 990
ca67fcb8
VP
991/* If not equal to null_ptid, this means that after stepping over breakpoint
992 is finished, we need to switch to deferred_step_ptid, and step it.
993
994 The use case is when one thread has hit a breakpoint, and then the user
1777feb0 995 has switched to another thread and issued 'step'. We need to step over
ca67fcb8
VP
996 breakpoint in the thread which hit the breakpoint, but then continue
997 stepping the thread user has selected. */
998static ptid_t deferred_step_ptid;
c906108c 999\f
237fc4c9
PA
1000/* Displaced stepping. */
1001
1002/* In non-stop debugging mode, we must take special care to manage
1003 breakpoints properly; in particular, the traditional strategy for
1004 stepping a thread past a breakpoint it has hit is unsuitable.
1005 'Displaced stepping' is a tactic for stepping one thread past a
1006 breakpoint it has hit while ensuring that other threads running
1007 concurrently will hit the breakpoint as they should.
1008
1009 The traditional way to step a thread T off a breakpoint in a
1010 multi-threaded program in all-stop mode is as follows:
1011
1012 a0) Initially, all threads are stopped, and breakpoints are not
1013 inserted.
1014 a1) We single-step T, leaving breakpoints uninserted.
1015 a2) We insert breakpoints, and resume all threads.
1016
1017 In non-stop debugging, however, this strategy is unsuitable: we
1018 don't want to have to stop all threads in the system in order to
1019 continue or step T past a breakpoint. Instead, we use displaced
1020 stepping:
1021
1022 n0) Initially, T is stopped, other threads are running, and
1023 breakpoints are inserted.
1024 n1) We copy the instruction "under" the breakpoint to a separate
1025 location, outside the main code stream, making any adjustments
1026 to the instruction, register, and memory state as directed by
1027 T's architecture.
1028 n2) We single-step T over the instruction at its new location.
1029 n3) We adjust the resulting register and memory state as directed
1030 by T's architecture. This includes resetting T's PC to point
1031 back into the main instruction stream.
1032 n4) We resume T.
1033
1034 This approach depends on the following gdbarch methods:
1035
1036 - gdbarch_max_insn_length and gdbarch_displaced_step_location
1037 indicate where to copy the instruction, and how much space must
1038 be reserved there. We use these in step n1.
1039
1040 - gdbarch_displaced_step_copy_insn copies a instruction to a new
1041 address, and makes any necessary adjustments to the instruction,
1042 register contents, and memory. We use this in step n1.
1043
1044 - gdbarch_displaced_step_fixup adjusts registers and memory after
1045 we have successfuly single-stepped the instruction, to yield the
1046 same effect the instruction would have had if we had executed it
1047 at its original address. We use this in step n3.
1048
1049 - gdbarch_displaced_step_free_closure provides cleanup.
1050
1051 The gdbarch_displaced_step_copy_insn and
1052 gdbarch_displaced_step_fixup functions must be written so that
1053 copying an instruction with gdbarch_displaced_step_copy_insn,
1054 single-stepping across the copied instruction, and then applying
1055 gdbarch_displaced_insn_fixup should have the same effects on the
1056 thread's memory and registers as stepping the instruction in place
1057 would have. Exactly which responsibilities fall to the copy and
1058 which fall to the fixup is up to the author of those functions.
1059
1060 See the comments in gdbarch.sh for details.
1061
1062 Note that displaced stepping and software single-step cannot
1063 currently be used in combination, although with some care I think
1064 they could be made to. Software single-step works by placing
1065 breakpoints on all possible subsequent instructions; if the
1066 displaced instruction is a PC-relative jump, those breakpoints
1067 could fall in very strange places --- on pages that aren't
1068 executable, or at addresses that are not proper instruction
1069 boundaries. (We do generally let other threads run while we wait
1070 to hit the software single-step breakpoint, and they might
1071 encounter such a corrupted instruction.) One way to work around
1072 this would be to have gdbarch_displaced_step_copy_insn fully
1073 simulate the effect of PC-relative instructions (and return NULL)
1074 on architectures that use software single-stepping.
1075
1076 In non-stop mode, we can have independent and simultaneous step
1077 requests, so more than one thread may need to simultaneously step
1078 over a breakpoint. The current implementation assumes there is
1079 only one scratch space per process. In this case, we have to
1080 serialize access to the scratch space. If thread A wants to step
1081 over a breakpoint, but we are currently waiting for some other
1082 thread to complete a displaced step, we leave thread A stopped and
1083 place it in the displaced_step_request_queue. Whenever a displaced
1084 step finishes, we pick the next thread in the queue and start a new
1085 displaced step operation on it. See displaced_step_prepare and
1086 displaced_step_fixup for details. */
1087
237fc4c9
PA
1088struct displaced_step_request
1089{
1090 ptid_t ptid;
1091 struct displaced_step_request *next;
1092};
1093
fc1cf338
PA
1094/* Per-inferior displaced stepping state. */
1095struct displaced_step_inferior_state
1096{
1097 /* Pointer to next in linked list. */
1098 struct displaced_step_inferior_state *next;
1099
1100 /* The process this displaced step state refers to. */
1101 int pid;
1102
1103 /* A queue of pending displaced stepping requests. One entry per
1104 thread that needs to do a displaced step. */
1105 struct displaced_step_request *step_request_queue;
1106
1107 /* If this is not null_ptid, this is the thread carrying out a
1108 displaced single-step in process PID. This thread's state will
1109 require fixing up once it has completed its step. */
1110 ptid_t step_ptid;
1111
1112 /* The architecture the thread had when we stepped it. */
1113 struct gdbarch *step_gdbarch;
1114
1115 /* The closure provided gdbarch_displaced_step_copy_insn, to be used
1116 for post-step cleanup. */
1117 struct displaced_step_closure *step_closure;
1118
1119 /* The address of the original instruction, and the copy we
1120 made. */
1121 CORE_ADDR step_original, step_copy;
1122
1123 /* Saved contents of copy area. */
1124 gdb_byte *step_saved_copy;
1125};
1126
1127/* The list of states of processes involved in displaced stepping
1128 presently. */
1129static struct displaced_step_inferior_state *displaced_step_inferior_states;
1130
1131/* Get the displaced stepping state of process PID. */
1132
1133static struct displaced_step_inferior_state *
1134get_displaced_stepping_state (int pid)
1135{
1136 struct displaced_step_inferior_state *state;
1137
1138 for (state = displaced_step_inferior_states;
1139 state != NULL;
1140 state = state->next)
1141 if (state->pid == pid)
1142 return state;
1143
1144 return NULL;
1145}
1146
1147/* Add a new displaced stepping state for process PID to the displaced
1148 stepping state list, or return a pointer to an already existing
1149 entry, if it already exists. Never returns NULL. */
1150
1151static struct displaced_step_inferior_state *
1152add_displaced_stepping_state (int pid)
1153{
1154 struct displaced_step_inferior_state *state;
1155
1156 for (state = displaced_step_inferior_states;
1157 state != NULL;
1158 state = state->next)
1159 if (state->pid == pid)
1160 return state;
237fc4c9 1161
fc1cf338
PA
1162 state = xcalloc (1, sizeof (*state));
1163 state->pid = pid;
1164 state->next = displaced_step_inferior_states;
1165 displaced_step_inferior_states = state;
237fc4c9 1166
fc1cf338
PA
1167 return state;
1168}
1169
a42244db
YQ
1170/* If inferior is in displaced stepping, and ADDR equals to starting address
1171 of copy area, return corresponding displaced_step_closure. Otherwise,
1172 return NULL. */
1173
1174struct displaced_step_closure*
1175get_displaced_step_closure_by_addr (CORE_ADDR addr)
1176{
1177 struct displaced_step_inferior_state *displaced
1178 = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
1179
1180 /* If checking the mode of displaced instruction in copy area. */
1181 if (displaced && !ptid_equal (displaced->step_ptid, null_ptid)
1182 && (displaced->step_copy == addr))
1183 return displaced->step_closure;
1184
1185 return NULL;
1186}
1187
fc1cf338 1188/* Remove the displaced stepping state of process PID. */
237fc4c9 1189
fc1cf338
PA
1190static void
1191remove_displaced_stepping_state (int pid)
1192{
1193 struct displaced_step_inferior_state *it, **prev_next_p;
237fc4c9 1194
fc1cf338
PA
1195 gdb_assert (pid != 0);
1196
1197 it = displaced_step_inferior_states;
1198 prev_next_p = &displaced_step_inferior_states;
1199 while (it)
1200 {
1201 if (it->pid == pid)
1202 {
1203 *prev_next_p = it->next;
1204 xfree (it);
1205 return;
1206 }
1207
1208 prev_next_p = &it->next;
1209 it = *prev_next_p;
1210 }
1211}
1212
1213static void
1214infrun_inferior_exit (struct inferior *inf)
1215{
1216 remove_displaced_stepping_state (inf->pid);
1217}
237fc4c9 1218
fff08868
HZ
1219/* If ON, and the architecture supports it, GDB will use displaced
1220 stepping to step over breakpoints. If OFF, or if the architecture
1221 doesn't support it, GDB will instead use the traditional
1222 hold-and-step approach. If AUTO (which is the default), GDB will
1223 decide which technique to use to step over breakpoints depending on
1224 which of all-stop or non-stop mode is active --- displaced stepping
1225 in non-stop mode; hold-and-step in all-stop mode. */
1226
72d0e2c5 1227static enum auto_boolean can_use_displaced_stepping = AUTO_BOOLEAN_AUTO;
fff08868 1228
237fc4c9
PA
1229static void
1230show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
1231 struct cmd_list_element *c,
1232 const char *value)
1233{
72d0e2c5 1234 if (can_use_displaced_stepping == AUTO_BOOLEAN_AUTO)
3e43a32a
MS
1235 fprintf_filtered (file,
1236 _("Debugger's willingness to use displaced stepping "
1237 "to step over breakpoints is %s (currently %s).\n"),
fff08868
HZ
1238 value, non_stop ? "on" : "off");
1239 else
3e43a32a
MS
1240 fprintf_filtered (file,
1241 _("Debugger's willingness to use displaced stepping "
1242 "to step over breakpoints is %s.\n"), value);
237fc4c9
PA
1243}
1244
fff08868
HZ
1245/* Return non-zero if displaced stepping can/should be used to step
1246 over breakpoints. */
1247
237fc4c9
PA
1248static int
1249use_displaced_stepping (struct gdbarch *gdbarch)
1250{
72d0e2c5
YQ
1251 return (((can_use_displaced_stepping == AUTO_BOOLEAN_AUTO && non_stop)
1252 || can_use_displaced_stepping == AUTO_BOOLEAN_TRUE)
96429cc8
HZ
1253 && gdbarch_displaced_step_copy_insn_p (gdbarch)
1254 && !RECORD_IS_USED);
237fc4c9
PA
1255}
1256
1257/* Clean out any stray displaced stepping state. */
1258static void
fc1cf338 1259displaced_step_clear (struct displaced_step_inferior_state *displaced)
237fc4c9
PA
1260{
1261 /* Indicate that there is no cleanup pending. */
fc1cf338 1262 displaced->step_ptid = null_ptid;
237fc4c9 1263
fc1cf338 1264 if (displaced->step_closure)
237fc4c9 1265 {
fc1cf338
PA
1266 gdbarch_displaced_step_free_closure (displaced->step_gdbarch,
1267 displaced->step_closure);
1268 displaced->step_closure = NULL;
237fc4c9
PA
1269 }
1270}
1271
1272static void
fc1cf338 1273displaced_step_clear_cleanup (void *arg)
237fc4c9 1274{
fc1cf338
PA
1275 struct displaced_step_inferior_state *state = arg;
1276
1277 displaced_step_clear (state);
237fc4c9
PA
1278}
1279
1280/* Dump LEN bytes at BUF in hex to FILE, followed by a newline. */
1281void
1282displaced_step_dump_bytes (struct ui_file *file,
1283 const gdb_byte *buf,
1284 size_t len)
1285{
1286 int i;
1287
1288 for (i = 0; i < len; i++)
1289 fprintf_unfiltered (file, "%02x ", buf[i]);
1290 fputs_unfiltered ("\n", file);
1291}
1292
1293/* Prepare to single-step, using displaced stepping.
1294
1295 Note that we cannot use displaced stepping when we have a signal to
1296 deliver. If we have a signal to deliver and an instruction to step
1297 over, then after the step, there will be no indication from the
1298 target whether the thread entered a signal handler or ignored the
1299 signal and stepped over the instruction successfully --- both cases
1300 result in a simple SIGTRAP. In the first case we mustn't do a
1301 fixup, and in the second case we must --- but we can't tell which.
1302 Comments in the code for 'random signals' in handle_inferior_event
1303 explain how we handle this case instead.
1304
1305 Returns 1 if preparing was successful -- this thread is going to be
1306 stepped now; or 0 if displaced stepping this thread got queued. */
1307static int
1308displaced_step_prepare (ptid_t ptid)
1309{
ad53cd71 1310 struct cleanup *old_cleanups, *ignore_cleanups;
237fc4c9
PA
1311 struct regcache *regcache = get_thread_regcache (ptid);
1312 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1313 CORE_ADDR original, copy;
1314 ULONGEST len;
1315 struct displaced_step_closure *closure;
fc1cf338 1316 struct displaced_step_inferior_state *displaced;
9e529e1d 1317 int status;
237fc4c9
PA
1318
1319 /* We should never reach this function if the architecture does not
1320 support displaced stepping. */
1321 gdb_assert (gdbarch_displaced_step_copy_insn_p (gdbarch));
1322
fc1cf338
PA
1323 /* We have to displaced step one thread at a time, as we only have
1324 access to a single scratch space per inferior. */
237fc4c9 1325
fc1cf338
PA
1326 displaced = add_displaced_stepping_state (ptid_get_pid (ptid));
1327
1328 if (!ptid_equal (displaced->step_ptid, null_ptid))
237fc4c9
PA
1329 {
1330 /* Already waiting for a displaced step to finish. Defer this
1331 request and place in queue. */
1332 struct displaced_step_request *req, *new_req;
1333
1334 if (debug_displaced)
1335 fprintf_unfiltered (gdb_stdlog,
1336 "displaced: defering step of %s\n",
1337 target_pid_to_str (ptid));
1338
1339 new_req = xmalloc (sizeof (*new_req));
1340 new_req->ptid = ptid;
1341 new_req->next = NULL;
1342
fc1cf338 1343 if (displaced->step_request_queue)
237fc4c9 1344 {
fc1cf338 1345 for (req = displaced->step_request_queue;
237fc4c9
PA
1346 req && req->next;
1347 req = req->next)
1348 ;
1349 req->next = new_req;
1350 }
1351 else
fc1cf338 1352 displaced->step_request_queue = new_req;
237fc4c9
PA
1353
1354 return 0;
1355 }
1356 else
1357 {
1358 if (debug_displaced)
1359 fprintf_unfiltered (gdb_stdlog,
1360 "displaced: stepping %s now\n",
1361 target_pid_to_str (ptid));
1362 }
1363
fc1cf338 1364 displaced_step_clear (displaced);
237fc4c9 1365
ad53cd71
PA
1366 old_cleanups = save_inferior_ptid ();
1367 inferior_ptid = ptid;
1368
515630c5 1369 original = regcache_read_pc (regcache);
237fc4c9
PA
1370
1371 copy = gdbarch_displaced_step_location (gdbarch);
1372 len = gdbarch_max_insn_length (gdbarch);
1373
1374 /* Save the original contents of the copy area. */
fc1cf338 1375 displaced->step_saved_copy = xmalloc (len);
ad53cd71 1376 ignore_cleanups = make_cleanup (free_current_contents,
fc1cf338 1377 &displaced->step_saved_copy);
9e529e1d
JK
1378 status = target_read_memory (copy, displaced->step_saved_copy, len);
1379 if (status != 0)
1380 throw_error (MEMORY_ERROR,
1381 _("Error accessing memory address %s (%s) for "
1382 "displaced-stepping scratch space."),
1383 paddress (gdbarch, copy), safe_strerror (status));
237fc4c9
PA
1384 if (debug_displaced)
1385 {
5af949e3
UW
1386 fprintf_unfiltered (gdb_stdlog, "displaced: saved %s: ",
1387 paddress (gdbarch, copy));
fc1cf338
PA
1388 displaced_step_dump_bytes (gdb_stdlog,
1389 displaced->step_saved_copy,
1390 len);
237fc4c9
PA
1391 };
1392
1393 closure = gdbarch_displaced_step_copy_insn (gdbarch,
ad53cd71 1394 original, copy, regcache);
237fc4c9
PA
1395
1396 /* We don't support the fully-simulated case at present. */
1397 gdb_assert (closure);
1398
9f5a595d
UW
1399 /* Save the information we need to fix things up if the step
1400 succeeds. */
fc1cf338
PA
1401 displaced->step_ptid = ptid;
1402 displaced->step_gdbarch = gdbarch;
1403 displaced->step_closure = closure;
1404 displaced->step_original = original;
1405 displaced->step_copy = copy;
9f5a595d 1406
fc1cf338 1407 make_cleanup (displaced_step_clear_cleanup, displaced);
237fc4c9
PA
1408
1409 /* Resume execution at the copy. */
515630c5 1410 regcache_write_pc (regcache, copy);
237fc4c9 1411
ad53cd71
PA
1412 discard_cleanups (ignore_cleanups);
1413
1414 do_cleanups (old_cleanups);
237fc4c9
PA
1415
1416 if (debug_displaced)
5af949e3
UW
1417 fprintf_unfiltered (gdb_stdlog, "displaced: displaced pc to %s\n",
1418 paddress (gdbarch, copy));
237fc4c9 1419
237fc4c9
PA
1420 return 1;
1421}
1422
237fc4c9 1423static void
3e43a32a
MS
1424write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
1425 const gdb_byte *myaddr, int len)
237fc4c9
PA
1426{
1427 struct cleanup *ptid_cleanup = save_inferior_ptid ();
abbb1732 1428
237fc4c9
PA
1429 inferior_ptid = ptid;
1430 write_memory (memaddr, myaddr, len);
1431 do_cleanups (ptid_cleanup);
1432}
1433
e2d96639
YQ
1434/* Restore the contents of the copy area for thread PTID. */
1435
1436static void
1437displaced_step_restore (struct displaced_step_inferior_state *displaced,
1438 ptid_t ptid)
1439{
1440 ULONGEST len = gdbarch_max_insn_length (displaced->step_gdbarch);
1441
1442 write_memory_ptid (ptid, displaced->step_copy,
1443 displaced->step_saved_copy, len);
1444 if (debug_displaced)
1445 fprintf_unfiltered (gdb_stdlog, "displaced: restored %s %s\n",
1446 target_pid_to_str (ptid),
1447 paddress (displaced->step_gdbarch,
1448 displaced->step_copy));
1449}
1450
237fc4c9 1451static void
2ea28649 1452displaced_step_fixup (ptid_t event_ptid, enum gdb_signal signal)
237fc4c9
PA
1453{
1454 struct cleanup *old_cleanups;
fc1cf338
PA
1455 struct displaced_step_inferior_state *displaced
1456 = get_displaced_stepping_state (ptid_get_pid (event_ptid));
1457
1458 /* Was any thread of this process doing a displaced step? */
1459 if (displaced == NULL)
1460 return;
237fc4c9
PA
1461
1462 /* Was this event for the pid we displaced? */
fc1cf338
PA
1463 if (ptid_equal (displaced->step_ptid, null_ptid)
1464 || ! ptid_equal (displaced->step_ptid, event_ptid))
237fc4c9
PA
1465 return;
1466
fc1cf338 1467 old_cleanups = make_cleanup (displaced_step_clear_cleanup, displaced);
237fc4c9 1468
e2d96639 1469 displaced_step_restore (displaced, displaced->step_ptid);
237fc4c9
PA
1470
1471 /* Did the instruction complete successfully? */
a493e3e2 1472 if (signal == GDB_SIGNAL_TRAP)
237fc4c9
PA
1473 {
1474 /* Fix up the resulting state. */
fc1cf338
PA
1475 gdbarch_displaced_step_fixup (displaced->step_gdbarch,
1476 displaced->step_closure,
1477 displaced->step_original,
1478 displaced->step_copy,
1479 get_thread_regcache (displaced->step_ptid));
237fc4c9
PA
1480 }
1481 else
1482 {
1483 /* Since the instruction didn't complete, all we can do is
1484 relocate the PC. */
515630c5
UW
1485 struct regcache *regcache = get_thread_regcache (event_ptid);
1486 CORE_ADDR pc = regcache_read_pc (regcache);
abbb1732 1487
fc1cf338 1488 pc = displaced->step_original + (pc - displaced->step_copy);
515630c5 1489 regcache_write_pc (regcache, pc);
237fc4c9
PA
1490 }
1491
1492 do_cleanups (old_cleanups);
1493
fc1cf338 1494 displaced->step_ptid = null_ptid;
1c5cfe86 1495
237fc4c9 1496 /* Are there any pending displaced stepping requests? If so, run
fc1cf338
PA
1497 one now. Leave the state object around, since we're likely to
1498 need it again soon. */
1499 while (displaced->step_request_queue)
237fc4c9
PA
1500 {
1501 struct displaced_step_request *head;
1502 ptid_t ptid;
5af949e3 1503 struct regcache *regcache;
929dfd4f 1504 struct gdbarch *gdbarch;
1c5cfe86 1505 CORE_ADDR actual_pc;
6c95b8df 1506 struct address_space *aspace;
237fc4c9 1507
fc1cf338 1508 head = displaced->step_request_queue;
237fc4c9 1509 ptid = head->ptid;
fc1cf338 1510 displaced->step_request_queue = head->next;
237fc4c9
PA
1511 xfree (head);
1512
ad53cd71
PA
1513 context_switch (ptid);
1514
5af949e3
UW
1515 regcache = get_thread_regcache (ptid);
1516 actual_pc = regcache_read_pc (regcache);
6c95b8df 1517 aspace = get_regcache_aspace (regcache);
1c5cfe86 1518
6c95b8df 1519 if (breakpoint_here_p (aspace, actual_pc))
ad53cd71 1520 {
1c5cfe86
PA
1521 if (debug_displaced)
1522 fprintf_unfiltered (gdb_stdlog,
1523 "displaced: stepping queued %s now\n",
1524 target_pid_to_str (ptid));
1525
1526 displaced_step_prepare (ptid);
1527
929dfd4f
JB
1528 gdbarch = get_regcache_arch (regcache);
1529
1c5cfe86
PA
1530 if (debug_displaced)
1531 {
929dfd4f 1532 CORE_ADDR actual_pc = regcache_read_pc (regcache);
1c5cfe86
PA
1533 gdb_byte buf[4];
1534
5af949e3
UW
1535 fprintf_unfiltered (gdb_stdlog, "displaced: run %s: ",
1536 paddress (gdbarch, actual_pc));
1c5cfe86
PA
1537 read_memory (actual_pc, buf, sizeof (buf));
1538 displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
1539 }
1540
fc1cf338
PA
1541 if (gdbarch_displaced_step_hw_singlestep (gdbarch,
1542 displaced->step_closure))
a493e3e2 1543 target_resume (ptid, 1, GDB_SIGNAL_0);
99e40580 1544 else
a493e3e2 1545 target_resume (ptid, 0, GDB_SIGNAL_0);
1c5cfe86
PA
1546
1547 /* Done, we're stepping a thread. */
1548 break;
ad53cd71 1549 }
1c5cfe86
PA
1550 else
1551 {
1552 int step;
1553 struct thread_info *tp = inferior_thread ();
1554
1555 /* The breakpoint we were sitting under has since been
1556 removed. */
16c381f0 1557 tp->control.trap_expected = 0;
1c5cfe86
PA
1558
1559 /* Go back to what we were trying to do. */
1560 step = currently_stepping (tp);
ad53cd71 1561
1c5cfe86 1562 if (debug_displaced)
3e43a32a 1563 fprintf_unfiltered (gdb_stdlog,
27d2932e 1564 "displaced: breakpoint is gone: %s, step(%d)\n",
1c5cfe86
PA
1565 target_pid_to_str (tp->ptid), step);
1566
a493e3e2
PA
1567 target_resume (ptid, step, GDB_SIGNAL_0);
1568 tp->suspend.stop_signal = GDB_SIGNAL_0;
1c5cfe86
PA
1569
1570 /* This request was discarded. See if there's any other
1571 thread waiting for its turn. */
1572 }
237fc4c9
PA
1573 }
1574}
1575
5231c1fd
PA
1576/* Update global variables holding ptids to hold NEW_PTID if they were
1577 holding OLD_PTID. */
1578static void
1579infrun_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1580{
1581 struct displaced_step_request *it;
fc1cf338 1582 struct displaced_step_inferior_state *displaced;
5231c1fd
PA
1583
1584 if (ptid_equal (inferior_ptid, old_ptid))
1585 inferior_ptid = new_ptid;
1586
1587 if (ptid_equal (singlestep_ptid, old_ptid))
1588 singlestep_ptid = new_ptid;
1589
5231c1fd
PA
1590 if (ptid_equal (deferred_step_ptid, old_ptid))
1591 deferred_step_ptid = new_ptid;
1592
fc1cf338
PA
1593 for (displaced = displaced_step_inferior_states;
1594 displaced;
1595 displaced = displaced->next)
1596 {
1597 if (ptid_equal (displaced->step_ptid, old_ptid))
1598 displaced->step_ptid = new_ptid;
1599
1600 for (it = displaced->step_request_queue; it; it = it->next)
1601 if (ptid_equal (it->ptid, old_ptid))
1602 it->ptid = new_ptid;
1603 }
5231c1fd
PA
1604}
1605
237fc4c9
PA
1606\f
1607/* Resuming. */
c906108c
SS
1608
1609/* Things to clean up if we QUIT out of resume (). */
c906108c 1610static void
74b7792f 1611resume_cleanups (void *ignore)
c906108c
SS
1612{
1613 normal_stop ();
1614}
1615
53904c9e
AC
1616static const char schedlock_off[] = "off";
1617static const char schedlock_on[] = "on";
1618static const char schedlock_step[] = "step";
40478521 1619static const char *const scheduler_enums[] = {
ef346e04
AC
1620 schedlock_off,
1621 schedlock_on,
1622 schedlock_step,
1623 NULL
1624};
920d2a44
AC
1625static const char *scheduler_mode = schedlock_off;
1626static void
1627show_scheduler_mode (struct ui_file *file, int from_tty,
1628 struct cmd_list_element *c, const char *value)
1629{
3e43a32a
MS
1630 fprintf_filtered (file,
1631 _("Mode for locking scheduler "
1632 "during execution is \"%s\".\n"),
920d2a44
AC
1633 value);
1634}
c906108c
SS
1635
1636static void
96baa820 1637set_schedlock_func (char *args, int from_tty, struct cmd_list_element *c)
c906108c 1638{
eefe576e
AC
1639 if (!target_can_lock_scheduler)
1640 {
1641 scheduler_mode = schedlock_off;
1642 error (_("Target '%s' cannot support this command."), target_shortname);
1643 }
c906108c
SS
1644}
1645
d4db2f36
PA
1646/* True if execution commands resume all threads of all processes by
1647 default; otherwise, resume only threads of the current inferior
1648 process. */
1649int sched_multi = 0;
1650
2facfe5c
DD
1651/* Try to setup for software single stepping over the specified location.
1652 Return 1 if target_resume() should use hardware single step.
1653
1654 GDBARCH the current gdbarch.
1655 PC the location to step over. */
1656
1657static int
1658maybe_software_singlestep (struct gdbarch *gdbarch, CORE_ADDR pc)
1659{
1660 int hw_step = 1;
1661
f02253f1
HZ
1662 if (execution_direction == EXEC_FORWARD
1663 && gdbarch_software_single_step_p (gdbarch)
99e40580 1664 && gdbarch_software_single_step (gdbarch, get_current_frame ()))
2facfe5c 1665 {
99e40580
UW
1666 hw_step = 0;
1667 /* Do not pull these breakpoints until after a `wait' in
1777feb0 1668 `wait_for_inferior'. */
99e40580
UW
1669 singlestep_breakpoints_inserted_p = 1;
1670 singlestep_ptid = inferior_ptid;
1671 singlestep_pc = pc;
2facfe5c
DD
1672 }
1673 return hw_step;
1674}
c906108c 1675
09cee04b
PA
1676/* Return a ptid representing the set of threads that we will proceed,
1677 in the perspective of the user/frontend. We may actually resume
1678 fewer threads at first, e.g., if a thread is stopped at a
b136cd05
PA
1679 breakpoint that needs stepping-off, but that should not be visible
1680 to the user/frontend, and neither should the frontend/user be
1681 allowed to proceed any of the threads that happen to be stopped for
09cee04b
PA
1682 internal run control handling, if a previous command wanted them
1683 resumed. */
1684
1685ptid_t
1686user_visible_resume_ptid (int step)
1687{
1688 /* By default, resume all threads of all processes. */
1689 ptid_t resume_ptid = RESUME_ALL;
1690
1691 /* Maybe resume only all threads of the current process. */
1692 if (!sched_multi && target_supports_multi_process ())
1693 {
1694 resume_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
1695 }
1696
1697 /* Maybe resume a single thread after all. */
1698 if (non_stop)
1699 {
1700 /* With non-stop mode on, threads are always handled
1701 individually. */
1702 resume_ptid = inferior_ptid;
1703 }
1704 else if ((scheduler_mode == schedlock_on)
1705 || (scheduler_mode == schedlock_step
1706 && (step || singlestep_breakpoints_inserted_p)))
1707 {
1708 /* User-settable 'scheduler' mode requires solo thread resume. */
1709 resume_ptid = inferior_ptid;
1710 }
1711
1712 return resume_ptid;
1713}
1714
c906108c
SS
1715/* Resume the inferior, but allow a QUIT. This is useful if the user
1716 wants to interrupt some lengthy single-stepping operation
1717 (for child processes, the SIGINT goes to the inferior, and so
1718 we get a SIGINT random_signal, but for remote debugging and perhaps
1719 other targets, that's not true).
1720
1721 STEP nonzero if we should step (zero to continue instead).
1722 SIG is the signal to give the inferior (zero for none). */
1723void
2ea28649 1724resume (int step, enum gdb_signal sig)
c906108c
SS
1725{
1726 int should_resume = 1;
74b7792f 1727 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
515630c5
UW
1728 struct regcache *regcache = get_current_regcache ();
1729 struct gdbarch *gdbarch = get_regcache_arch (regcache);
4e1c45ea 1730 struct thread_info *tp = inferior_thread ();
515630c5 1731 CORE_ADDR pc = regcache_read_pc (regcache);
6c95b8df 1732 struct address_space *aspace = get_regcache_aspace (regcache);
c7e8a53c 1733
c906108c
SS
1734 QUIT;
1735
74609e71
YQ
1736 if (current_inferior ()->waiting_for_vfork_done)
1737 {
48f9886d
PA
1738 /* Don't try to single-step a vfork parent that is waiting for
1739 the child to get out of the shared memory region (by exec'ing
1740 or exiting). This is particularly important on software
1741 single-step archs, as the child process would trip on the
1742 software single step breakpoint inserted for the parent
1743 process. Since the parent will not actually execute any
1744 instruction until the child is out of the shared region (such
1745 are vfork's semantics), it is safe to simply continue it.
1746 Eventually, we'll see a TARGET_WAITKIND_VFORK_DONE event for
1747 the parent, and tell it to `keep_going', which automatically
1748 re-sets it stepping. */
74609e71
YQ
1749 if (debug_infrun)
1750 fprintf_unfiltered (gdb_stdlog,
1751 "infrun: resume : clear step\n");
1752 step = 0;
1753 }
1754
527159b7 1755 if (debug_infrun)
237fc4c9
PA
1756 fprintf_unfiltered (gdb_stdlog,
1757 "infrun: resume (step=%d, signal=%d), "
0d9a9a5f
PA
1758 "trap_expected=%d, current thread [%s] at %s\n",
1759 step, sig, tp->control.trap_expected,
1760 target_pid_to_str (inferior_ptid),
1761 paddress (gdbarch, pc));
c906108c 1762
c2c6d25f
JM
1763 /* Normally, by the time we reach `resume', the breakpoints are either
1764 removed or inserted, as appropriate. The exception is if we're sitting
1765 at a permanent breakpoint; we need to step over it, but permanent
1766 breakpoints can't be removed. So we have to test for it here. */
6c95b8df 1767 if (breakpoint_here_p (aspace, pc) == permanent_breakpoint_here)
6d350bb5 1768 {
515630c5
UW
1769 if (gdbarch_skip_permanent_breakpoint_p (gdbarch))
1770 gdbarch_skip_permanent_breakpoint (gdbarch, regcache);
6d350bb5 1771 else
ac74f770
MS
1772 error (_("\
1773The program is stopped at a permanent breakpoint, but GDB does not know\n\
1774how to step past a permanent breakpoint on this architecture. Try using\n\
1775a command like `return' or `jump' to continue execution."));
6d350bb5 1776 }
c2c6d25f 1777
237fc4c9
PA
1778 /* If enabled, step over breakpoints by executing a copy of the
1779 instruction at a different address.
1780
1781 We can't use displaced stepping when we have a signal to deliver;
1782 the comments for displaced_step_prepare explain why. The
1783 comments in the handle_inferior event for dealing with 'random
74609e71
YQ
1784 signals' explain what we do instead.
1785
1786 We can't use displaced stepping when we are waiting for vfork_done
1787 event, displaced stepping breaks the vfork child similarly as single
1788 step software breakpoint. */
515630c5 1789 if (use_displaced_stepping (gdbarch)
16c381f0 1790 && (tp->control.trap_expected
929dfd4f 1791 || (step && gdbarch_software_single_step_p (gdbarch)))
a493e3e2 1792 && sig == GDB_SIGNAL_0
74609e71 1793 && !current_inferior ()->waiting_for_vfork_done)
237fc4c9 1794 {
fc1cf338
PA
1795 struct displaced_step_inferior_state *displaced;
1796
237fc4c9 1797 if (!displaced_step_prepare (inferior_ptid))
d56b7306
VP
1798 {
1799 /* Got placed in displaced stepping queue. Will be resumed
1800 later when all the currently queued displaced stepping
7f7efbd9
VP
1801 requests finish. The thread is not executing at this point,
1802 and the call to set_executing will be made later. But we
1803 need to call set_running here, since from frontend point of view,
1804 the thread is running. */
1805 set_running (inferior_ptid, 1);
d56b7306
VP
1806 discard_cleanups (old_cleanups);
1807 return;
1808 }
99e40580 1809
ca7781d2
LM
1810 /* Update pc to reflect the new address from which we will execute
1811 instructions due to displaced stepping. */
1812 pc = regcache_read_pc (get_thread_regcache (inferior_ptid));
1813
fc1cf338
PA
1814 displaced = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
1815 step = gdbarch_displaced_step_hw_singlestep (gdbarch,
1816 displaced->step_closure);
237fc4c9
PA
1817 }
1818
2facfe5c 1819 /* Do we need to do it the hard way, w/temp breakpoints? */
99e40580 1820 else if (step)
2facfe5c 1821 step = maybe_software_singlestep (gdbarch, pc);
c906108c 1822
30852783
UW
1823 /* Currently, our software single-step implementation leads to different
1824 results than hardware single-stepping in one situation: when stepping
1825 into delivering a signal which has an associated signal handler,
1826 hardware single-step will stop at the first instruction of the handler,
1827 while software single-step will simply skip execution of the handler.
1828
1829 For now, this difference in behavior is accepted since there is no
1830 easy way to actually implement single-stepping into a signal handler
1831 without kernel support.
1832
1833 However, there is one scenario where this difference leads to follow-on
1834 problems: if we're stepping off a breakpoint by removing all breakpoints
1835 and then single-stepping. In this case, the software single-step
1836 behavior means that even if there is a *breakpoint* in the signal
1837 handler, GDB still would not stop.
1838
1839 Fortunately, we can at least fix this particular issue. We detect
1840 here the case where we are about to deliver a signal while software
1841 single-stepping with breakpoints removed. In this situation, we
1842 revert the decisions to remove all breakpoints and insert single-
1843 step breakpoints, and instead we install a step-resume breakpoint
1844 at the current address, deliver the signal without stepping, and
1845 once we arrive back at the step-resume breakpoint, actually step
1846 over the breakpoint we originally wanted to step over. */
1847 if (singlestep_breakpoints_inserted_p
a493e3e2 1848 && tp->control.trap_expected && sig != GDB_SIGNAL_0)
30852783
UW
1849 {
1850 /* If we have nested signals or a pending signal is delivered
1851 immediately after a handler returns, might might already have
1852 a step-resume breakpoint set on the earlier handler. We cannot
1853 set another step-resume breakpoint; just continue on until the
1854 original breakpoint is hit. */
1855 if (tp->control.step_resume_breakpoint == NULL)
1856 {
2c03e5be 1857 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
30852783
UW
1858 tp->step_after_step_resume_breakpoint = 1;
1859 }
1860
1861 remove_single_step_breakpoints ();
1862 singlestep_breakpoints_inserted_p = 0;
1863
1864 insert_breakpoints ();
1865 tp->control.trap_expected = 0;
1866 }
1867
c906108c
SS
1868 if (should_resume)
1869 {
39f77062 1870 ptid_t resume_ptid;
dfcd3bfb 1871
cd76b0b7
VP
1872 /* If STEP is set, it's a request to use hardware stepping
1873 facilities. But in that case, we should never
1874 use singlestep breakpoint. */
1875 gdb_assert (!(singlestep_breakpoints_inserted_p && step));
1876
d4db2f36
PA
1877 /* Decide the set of threads to ask the target to resume. Start
1878 by assuming everything will be resumed, than narrow the set
1879 by applying increasingly restricting conditions. */
09cee04b 1880 resume_ptid = user_visible_resume_ptid (step);
d4db2f36
PA
1881
1882 /* Maybe resume a single thread after all. */
cd76b0b7
VP
1883 if (singlestep_breakpoints_inserted_p
1884 && stepping_past_singlestep_breakpoint)
c906108c 1885 {
cd76b0b7
VP
1886 /* The situation here is as follows. In thread T1 we wanted to
1887 single-step. Lacking hardware single-stepping we've
1888 set breakpoint at the PC of the next instruction -- call it
1889 P. After resuming, we've hit that breakpoint in thread T2.
1890 Now we've removed original breakpoint, inserted breakpoint
1891 at P+1, and try to step to advance T2 past breakpoint.
1892 We need to step only T2, as if T1 is allowed to freely run,
1893 it can run past P, and if other threads are allowed to run,
1894 they can hit breakpoint at P+1, and nested hits of single-step
1895 breakpoints is not something we'd want -- that's complicated
1896 to support, and has no value. */
1897 resume_ptid = inferior_ptid;
1898 }
d4db2f36 1899 else if ((step || singlestep_breakpoints_inserted_p)
16c381f0 1900 && tp->control.trap_expected)
cd76b0b7 1901 {
74960c60
VP
1902 /* We're allowing a thread to run past a breakpoint it has
1903 hit, by single-stepping the thread with the breakpoint
1904 removed. In which case, we need to single-step only this
1905 thread, and keep others stopped, as they can miss this
1906 breakpoint if allowed to run.
1907
1908 The current code actually removes all breakpoints when
1909 doing this, not just the one being stepped over, so if we
1910 let other threads run, we can actually miss any
1911 breakpoint, not just the one at PC. */
ef5cf84e 1912 resume_ptid = inferior_ptid;
c906108c 1913 }
ef5cf84e 1914
515630c5 1915 if (gdbarch_cannot_step_breakpoint (gdbarch))
c4ed33b9
AC
1916 {
1917 /* Most targets can step a breakpoint instruction, thus
1918 executing it normally. But if this one cannot, just
1919 continue and we will hit it anyway. */
6c95b8df 1920 if (step && breakpoint_inserted_here_p (aspace, pc))
c4ed33b9
AC
1921 step = 0;
1922 }
237fc4c9
PA
1923
1924 if (debug_displaced
515630c5 1925 && use_displaced_stepping (gdbarch)
16c381f0 1926 && tp->control.trap_expected)
237fc4c9 1927 {
515630c5 1928 struct regcache *resume_regcache = get_thread_regcache (resume_ptid);
5af949e3 1929 struct gdbarch *resume_gdbarch = get_regcache_arch (resume_regcache);
515630c5 1930 CORE_ADDR actual_pc = regcache_read_pc (resume_regcache);
237fc4c9
PA
1931 gdb_byte buf[4];
1932
5af949e3
UW
1933 fprintf_unfiltered (gdb_stdlog, "displaced: run %s: ",
1934 paddress (resume_gdbarch, actual_pc));
237fc4c9
PA
1935 read_memory (actual_pc, buf, sizeof (buf));
1936 displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
1937 }
1938
e58b0e63
PA
1939 /* Install inferior's terminal modes. */
1940 target_terminal_inferior ();
1941
2020b7ab
PA
1942 /* Avoid confusing the next resume, if the next stop/resume
1943 happens to apply to another thread. */
a493e3e2 1944 tp->suspend.stop_signal = GDB_SIGNAL_0;
607cecd2 1945
2455069d
UW
1946 /* Advise target which signals may be handled silently. If we have
1947 removed breakpoints because we are stepping over one (which can
1948 happen only if we are not using displaced stepping), we need to
1949 receive all signals to avoid accidentally skipping a breakpoint
1950 during execution of a signal handler. */
1951 if ((step || singlestep_breakpoints_inserted_p)
1952 && tp->control.trap_expected
1953 && !use_displaced_stepping (gdbarch))
1954 target_pass_signals (0, NULL);
1955 else
a493e3e2 1956 target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
2455069d 1957
607cecd2 1958 target_resume (resume_ptid, step, sig);
c906108c
SS
1959 }
1960
1961 discard_cleanups (old_cleanups);
1962}
1963\f
237fc4c9 1964/* Proceeding. */
c906108c
SS
1965
1966/* Clear out all variables saying what to do when inferior is continued.
1967 First do this, then set the ones you want, then call `proceed'. */
1968
a7212384
UW
1969static void
1970clear_proceed_status_thread (struct thread_info *tp)
c906108c 1971{
a7212384
UW
1972 if (debug_infrun)
1973 fprintf_unfiltered (gdb_stdlog,
1974 "infrun: clear_proceed_status_thread (%s)\n",
1975 target_pid_to_str (tp->ptid));
d6b48e9c 1976
16c381f0
JK
1977 tp->control.trap_expected = 0;
1978 tp->control.step_range_start = 0;
1979 tp->control.step_range_end = 0;
1980 tp->control.step_frame_id = null_frame_id;
1981 tp->control.step_stack_frame_id = null_frame_id;
1982 tp->control.step_over_calls = STEP_OVER_UNDEBUGGABLE;
a7212384 1983 tp->stop_requested = 0;
4e1c45ea 1984
16c381f0 1985 tp->control.stop_step = 0;
32400beb 1986
16c381f0 1987 tp->control.proceed_to_finish = 0;
414c69f7 1988
a7212384 1989 /* Discard any remaining commands or status from previous stop. */
16c381f0 1990 bpstat_clear (&tp->control.stop_bpstat);
a7212384 1991}
32400beb 1992
a7212384
UW
1993static int
1994clear_proceed_status_callback (struct thread_info *tp, void *data)
1995{
1996 if (is_exited (tp->ptid))
1997 return 0;
d6b48e9c 1998
a7212384
UW
1999 clear_proceed_status_thread (tp);
2000 return 0;
2001}
2002
2003void
2004clear_proceed_status (void)
2005{
6c95b8df
PA
2006 if (!non_stop)
2007 {
2008 /* In all-stop mode, delete the per-thread status of all
2009 threads, even if inferior_ptid is null_ptid, there may be
2010 threads on the list. E.g., we may be launching a new
2011 process, while selecting the executable. */
2012 iterate_over_threads (clear_proceed_status_callback, NULL);
2013 }
2014
a7212384
UW
2015 if (!ptid_equal (inferior_ptid, null_ptid))
2016 {
2017 struct inferior *inferior;
2018
2019 if (non_stop)
2020 {
6c95b8df
PA
2021 /* If in non-stop mode, only delete the per-thread status of
2022 the current thread. */
a7212384
UW
2023 clear_proceed_status_thread (inferior_thread ());
2024 }
6c95b8df 2025
d6b48e9c 2026 inferior = current_inferior ();
16c381f0 2027 inferior->control.stop_soon = NO_STOP_QUIETLY;
4e1c45ea
PA
2028 }
2029
c906108c 2030 stop_after_trap = 0;
f3b1572e
PA
2031
2032 observer_notify_about_to_proceed ();
c906108c 2033
d5c31457
UW
2034 if (stop_registers)
2035 {
2036 regcache_xfree (stop_registers);
2037 stop_registers = NULL;
2038 }
c906108c
SS
2039}
2040
5a437975
DE
2041/* Check the current thread against the thread that reported the most recent
2042 event. If a step-over is required return TRUE and set the current thread
2043 to the old thread. Otherwise return FALSE.
2044
1777feb0 2045 This should be suitable for any targets that support threads. */
ea67f13b
DJ
2046
2047static int
6a6b96b9 2048prepare_to_proceed (int step)
ea67f13b
DJ
2049{
2050 ptid_t wait_ptid;
2051 struct target_waitstatus wait_status;
5a437975
DE
2052 int schedlock_enabled;
2053
2054 /* With non-stop mode on, threads are always handled individually. */
2055 gdb_assert (! non_stop);
ea67f13b
DJ
2056
2057 /* Get the last target status returned by target_wait(). */
2058 get_last_target_status (&wait_ptid, &wait_status);
2059
6a6b96b9 2060 /* Make sure we were stopped at a breakpoint. */
ea67f13b 2061 if (wait_status.kind != TARGET_WAITKIND_STOPPED
a493e3e2
PA
2062 || (wait_status.value.sig != GDB_SIGNAL_TRAP
2063 && wait_status.value.sig != GDB_SIGNAL_ILL
2064 && wait_status.value.sig != GDB_SIGNAL_SEGV
2065 && wait_status.value.sig != GDB_SIGNAL_EMT))
ea67f13b
DJ
2066 {
2067 return 0;
2068 }
2069
5a437975
DE
2070 schedlock_enabled = (scheduler_mode == schedlock_on
2071 || (scheduler_mode == schedlock_step
2072 && step));
2073
d4db2f36
PA
2074 /* Don't switch over to WAIT_PTID if scheduler locking is on. */
2075 if (schedlock_enabled)
2076 return 0;
2077
2078 /* Don't switch over if we're about to resume some other process
2079 other than WAIT_PTID's, and schedule-multiple is off. */
2080 if (!sched_multi
2081 && ptid_get_pid (wait_ptid) != ptid_get_pid (inferior_ptid))
2082 return 0;
2083
6a6b96b9 2084 /* Switched over from WAIT_PID. */
ea67f13b 2085 if (!ptid_equal (wait_ptid, minus_one_ptid)
d4db2f36 2086 && !ptid_equal (inferior_ptid, wait_ptid))
ea67f13b 2087 {
515630c5
UW
2088 struct regcache *regcache = get_thread_regcache (wait_ptid);
2089
6c95b8df
PA
2090 if (breakpoint_here_p (get_regcache_aspace (regcache),
2091 regcache_read_pc (regcache)))
ea67f13b 2092 {
515630c5
UW
2093 /* If stepping, remember current thread to switch back to. */
2094 if (step)
2095 deferred_step_ptid = inferior_ptid;
ea67f13b 2096
515630c5
UW
2097 /* Switch back to WAIT_PID thread. */
2098 switch_to_thread (wait_ptid);
6a6b96b9 2099
0d9a9a5f
PA
2100 if (debug_infrun)
2101 fprintf_unfiltered (gdb_stdlog,
2102 "infrun: prepare_to_proceed (step=%d), "
2103 "switched to [%s]\n",
2104 step, target_pid_to_str (inferior_ptid));
2105
515630c5
UW
2106 /* We return 1 to indicate that there is a breakpoint here,
2107 so we need to step over it before continuing to avoid
1777feb0 2108 hitting it straight away. */
515630c5
UW
2109 return 1;
2110 }
ea67f13b
DJ
2111 }
2112
2113 return 0;
ea67f13b 2114}
e4846b08 2115
c906108c
SS
2116/* Basic routine for continuing the program in various fashions.
2117
2118 ADDR is the address to resume at, or -1 for resume where stopped.
2119 SIGGNAL is the signal to give it, or 0 for none,
c5aa993b 2120 or -1 for act according to how it stopped.
c906108c 2121 STEP is nonzero if should trap after one instruction.
c5aa993b
JM
2122 -1 means return after that and print nothing.
2123 You should probably set various step_... variables
2124 before calling here, if you are stepping.
c906108c
SS
2125
2126 You should call clear_proceed_status before calling proceed. */
2127
2128void
2ea28649 2129proceed (CORE_ADDR addr, enum gdb_signal siggnal, int step)
c906108c 2130{
e58b0e63
PA
2131 struct regcache *regcache;
2132 struct gdbarch *gdbarch;
4e1c45ea 2133 struct thread_info *tp;
e58b0e63 2134 CORE_ADDR pc;
6c95b8df 2135 struct address_space *aspace;
c906108c
SS
2136 int oneproc = 0;
2137
e58b0e63
PA
2138 /* If we're stopped at a fork/vfork, follow the branch set by the
2139 "set follow-fork-mode" command; otherwise, we'll just proceed
2140 resuming the current thread. */
2141 if (!follow_fork ())
2142 {
2143 /* The target for some reason decided not to resume. */
2144 normal_stop ();
f148b27e
PA
2145 if (target_can_async_p ())
2146 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
e58b0e63
PA
2147 return;
2148 }
2149
842951eb
PA
2150 /* We'll update this if & when we switch to a new thread. */
2151 previous_inferior_ptid = inferior_ptid;
2152
e58b0e63
PA
2153 regcache = get_current_regcache ();
2154 gdbarch = get_regcache_arch (regcache);
6c95b8df 2155 aspace = get_regcache_aspace (regcache);
e58b0e63
PA
2156 pc = regcache_read_pc (regcache);
2157
c906108c 2158 if (step > 0)
515630c5 2159 step_start_function = find_pc_function (pc);
c906108c
SS
2160 if (step < 0)
2161 stop_after_trap = 1;
2162
2acceee2 2163 if (addr == (CORE_ADDR) -1)
c906108c 2164 {
6c95b8df 2165 if (pc == stop_pc && breakpoint_here_p (aspace, pc)
b2175913 2166 && execution_direction != EXEC_REVERSE)
3352ef37
AC
2167 /* There is a breakpoint at the address we will resume at,
2168 step one instruction before inserting breakpoints so that
2169 we do not stop right away (and report a second hit at this
b2175913
MS
2170 breakpoint).
2171
2172 Note, we don't do this in reverse, because we won't
2173 actually be executing the breakpoint insn anyway.
2174 We'll be (un-)executing the previous instruction. */
2175
c906108c 2176 oneproc = 1;
515630c5
UW
2177 else if (gdbarch_single_step_through_delay_p (gdbarch)
2178 && gdbarch_single_step_through_delay (gdbarch,
2179 get_current_frame ()))
3352ef37
AC
2180 /* We stepped onto an instruction that needs to be stepped
2181 again before re-inserting the breakpoint, do so. */
c906108c
SS
2182 oneproc = 1;
2183 }
2184 else
2185 {
515630c5 2186 regcache_write_pc (regcache, addr);
c906108c
SS
2187 }
2188
527159b7 2189 if (debug_infrun)
8a9de0e4 2190 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
2191 "infrun: proceed (addr=%s, signal=%d, step=%d)\n",
2192 paddress (gdbarch, addr), siggnal, step);
527159b7 2193
94cc34af
PA
2194 if (non_stop)
2195 /* In non-stop, each thread is handled individually. The context
2196 must already be set to the right thread here. */
2197 ;
2198 else
2199 {
2200 /* In a multi-threaded task we may select another thread and
2201 then continue or step.
c906108c 2202
94cc34af
PA
2203 But if the old thread was stopped at a breakpoint, it will
2204 immediately cause another breakpoint stop without any
2205 execution (i.e. it will report a breakpoint hit incorrectly).
2206 So we must step over it first.
c906108c 2207
94cc34af
PA
2208 prepare_to_proceed checks the current thread against the
2209 thread that reported the most recent event. If a step-over
2210 is required it returns TRUE and sets the current thread to
1777feb0 2211 the old thread. */
94cc34af
PA
2212 if (prepare_to_proceed (step))
2213 oneproc = 1;
2214 }
c906108c 2215
4e1c45ea
PA
2216 /* prepare_to_proceed may change the current thread. */
2217 tp = inferior_thread ();
2218
30852783
UW
2219 if (oneproc)
2220 {
2221 tp->control.trap_expected = 1;
2222 /* If displaced stepping is enabled, we can step over the
2223 breakpoint without hitting it, so leave all breakpoints
2224 inserted. Otherwise we need to disable all breakpoints, step
2225 one instruction, and then re-add them when that step is
2226 finished. */
2227 if (!use_displaced_stepping (gdbarch))
2228 remove_breakpoints ();
2229 }
2230
2231 /* We can insert breakpoints if we're not trying to step over one,
2232 or if we are stepping over one but we're using displaced stepping
2233 to do so. */
2234 if (! tp->control.trap_expected || use_displaced_stepping (gdbarch))
2235 insert_breakpoints ();
2236
2020b7ab
PA
2237 if (!non_stop)
2238 {
2239 /* Pass the last stop signal to the thread we're resuming,
2240 irrespective of whether the current thread is the thread that
2241 got the last event or not. This was historically GDB's
2242 behaviour before keeping a stop_signal per thread. */
2243
2244 struct thread_info *last_thread;
2245 ptid_t last_ptid;
2246 struct target_waitstatus last_status;
2247
2248 get_last_target_status (&last_ptid, &last_status);
2249 if (!ptid_equal (inferior_ptid, last_ptid)
2250 && !ptid_equal (last_ptid, null_ptid)
2251 && !ptid_equal (last_ptid, minus_one_ptid))
2252 {
e09875d4 2253 last_thread = find_thread_ptid (last_ptid);
2020b7ab
PA
2254 if (last_thread)
2255 {
16c381f0 2256 tp->suspend.stop_signal = last_thread->suspend.stop_signal;
a493e3e2 2257 last_thread->suspend.stop_signal = GDB_SIGNAL_0;
2020b7ab
PA
2258 }
2259 }
2260 }
2261
a493e3e2 2262 if (siggnal != GDB_SIGNAL_DEFAULT)
16c381f0 2263 tp->suspend.stop_signal = siggnal;
c906108c
SS
2264 /* If this signal should not be seen by program,
2265 give it zero. Used for debugging signals. */
16c381f0 2266 else if (!signal_program[tp->suspend.stop_signal])
a493e3e2 2267 tp->suspend.stop_signal = GDB_SIGNAL_0;
c906108c
SS
2268
2269 annotate_starting ();
2270
2271 /* Make sure that output from GDB appears before output from the
2272 inferior. */
2273 gdb_flush (gdb_stdout);
2274
e4846b08
JJ
2275 /* Refresh prev_pc value just prior to resuming. This used to be
2276 done in stop_stepping, however, setting prev_pc there did not handle
2277 scenarios such as inferior function calls or returning from
2278 a function via the return command. In those cases, the prev_pc
2279 value was not set properly for subsequent commands. The prev_pc value
2280 is used to initialize the starting line number in the ecs. With an
2281 invalid value, the gdb next command ends up stopping at the position
2282 represented by the next line table entry past our start position.
2283 On platforms that generate one line table entry per line, this
2284 is not a problem. However, on the ia64, the compiler generates
2285 extraneous line table entries that do not increase the line number.
2286 When we issue the gdb next command on the ia64 after an inferior call
2287 or a return command, we often end up a few instructions forward, still
2288 within the original line we started.
2289
d5cd6034
JB
2290 An attempt was made to refresh the prev_pc at the same time the
2291 execution_control_state is initialized (for instance, just before
2292 waiting for an inferior event). But this approach did not work
2293 because of platforms that use ptrace, where the pc register cannot
2294 be read unless the inferior is stopped. At that point, we are not
2295 guaranteed the inferior is stopped and so the regcache_read_pc() call
2296 can fail. Setting the prev_pc value here ensures the value is updated
2297 correctly when the inferior is stopped. */
4e1c45ea 2298 tp->prev_pc = regcache_read_pc (get_current_regcache ());
e4846b08 2299
59f0d5d9 2300 /* Fill in with reasonable starting values. */
4e1c45ea 2301 init_thread_stepping_state (tp);
59f0d5d9 2302
59f0d5d9
PA
2303 /* Reset to normal state. */
2304 init_infwait_state ();
2305
c906108c 2306 /* Resume inferior. */
16c381f0 2307 resume (oneproc || step || bpstat_should_step (), tp->suspend.stop_signal);
c906108c
SS
2308
2309 /* Wait for it to stop (if not standalone)
2310 and in any case decode why it stopped, and act accordingly. */
43ff13b4 2311 /* Do this only if we are not using the event loop, or if the target
1777feb0 2312 does not support asynchronous execution. */
362646f5 2313 if (!target_can_async_p ())
43ff13b4 2314 {
e4c8541f 2315 wait_for_inferior ();
43ff13b4
JM
2316 normal_stop ();
2317 }
c906108c 2318}
c906108c
SS
2319\f
2320
2321/* Start remote-debugging of a machine over a serial link. */
96baa820 2322
c906108c 2323void
8621d6a9 2324start_remote (int from_tty)
c906108c 2325{
d6b48e9c 2326 struct inferior *inferior;
d6b48e9c
PA
2327
2328 inferior = current_inferior ();
16c381f0 2329 inferior->control.stop_soon = STOP_QUIETLY_REMOTE;
43ff13b4 2330
1777feb0 2331 /* Always go on waiting for the target, regardless of the mode. */
6426a772 2332 /* FIXME: cagney/1999-09-23: At present it isn't possible to
7e73cedf 2333 indicate to wait_for_inferior that a target should timeout if
6426a772
JM
2334 nothing is returned (instead of just blocking). Because of this,
2335 targets expecting an immediate response need to, internally, set
2336 things up so that the target_wait() is forced to eventually
1777feb0 2337 timeout. */
6426a772
JM
2338 /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
2339 differentiate to its caller what the state of the target is after
2340 the initial open has been performed. Here we're assuming that
2341 the target has stopped. It should be possible to eventually have
2342 target_open() return to the caller an indication that the target
2343 is currently running and GDB state should be set to the same as
1777feb0 2344 for an async run. */
e4c8541f 2345 wait_for_inferior ();
8621d6a9
DJ
2346
2347 /* Now that the inferior has stopped, do any bookkeeping like
2348 loading shared libraries. We want to do this before normal_stop,
2349 so that the displayed frame is up to date. */
2350 post_create_inferior (&current_target, from_tty);
2351
6426a772 2352 normal_stop ();
c906108c
SS
2353}
2354
2355/* Initialize static vars when a new inferior begins. */
2356
2357void
96baa820 2358init_wait_for_inferior (void)
c906108c
SS
2359{
2360 /* These are meaningless until the first time through wait_for_inferior. */
c906108c 2361
c906108c
SS
2362 breakpoint_init_inferior (inf_starting);
2363
c906108c 2364 clear_proceed_status ();
9f976b41
DJ
2365
2366 stepping_past_singlestep_breakpoint = 0;
ca67fcb8 2367 deferred_step_ptid = null_ptid;
ca005067
DJ
2368
2369 target_last_wait_ptid = minus_one_ptid;
237fc4c9 2370
842951eb 2371 previous_inferior_ptid = inferior_ptid;
0d1e5fa7
PA
2372 init_infwait_state ();
2373
edb3359d
DJ
2374 /* Discard any skipped inlined frames. */
2375 clear_inline_frame_state (minus_one_ptid);
c906108c 2376}
237fc4c9 2377
c906108c 2378\f
b83266a0
SS
2379/* This enum encodes possible reasons for doing a target_wait, so that
2380 wfi can call target_wait in one place. (Ultimately the call will be
2381 moved out of the infinite loop entirely.) */
2382
c5aa993b
JM
2383enum infwait_states
2384{
cd0fc7c3
SS
2385 infwait_normal_state,
2386 infwait_thread_hop_state,
d983da9c 2387 infwait_step_watch_state,
cd0fc7c3 2388 infwait_nonstep_watch_state
b83266a0
SS
2389};
2390
0d1e5fa7
PA
2391/* The PTID we'll do a target_wait on.*/
2392ptid_t waiton_ptid;
2393
2394/* Current inferior wait state. */
8870954f 2395static enum infwait_states infwait_state;
cd0fc7c3 2396
0d1e5fa7
PA
2397/* Data to be passed around while handling an event. This data is
2398 discarded between events. */
c5aa993b 2399struct execution_control_state
488f131b 2400{
0d1e5fa7 2401 ptid_t ptid;
4e1c45ea
PA
2402 /* The thread that got the event, if this was a thread event; NULL
2403 otherwise. */
2404 struct thread_info *event_thread;
2405
488f131b 2406 struct target_waitstatus ws;
488f131b 2407 int random_signal;
7e324e48 2408 int stop_func_filled_in;
488f131b
JB
2409 CORE_ADDR stop_func_start;
2410 CORE_ADDR stop_func_end;
2c02bd72 2411 const char *stop_func_name;
488f131b
JB
2412 int wait_some_more;
2413};
2414
ec9499be 2415static void handle_inferior_event (struct execution_control_state *ecs);
cd0fc7c3 2416
568d6575
UW
2417static void handle_step_into_function (struct gdbarch *gdbarch,
2418 struct execution_control_state *ecs);
2419static void handle_step_into_function_backward (struct gdbarch *gdbarch,
2420 struct execution_control_state *ecs);
186c406b 2421static void check_exception_resume (struct execution_control_state *,
28106bc2 2422 struct frame_info *);
611c83ae 2423
104c1213
JM
2424static void stop_stepping (struct execution_control_state *ecs);
2425static void prepare_to_wait (struct execution_control_state *ecs);
d4f3574e 2426static void keep_going (struct execution_control_state *ecs);
104c1213 2427
252fbfc8
PA
2428/* Callback for iterate over threads. If the thread is stopped, but
2429 the user/frontend doesn't know about that yet, go through
2430 normal_stop, as if the thread had just stopped now. ARG points at
2431 a ptid. If PTID is MINUS_ONE_PTID, applies to all threads. If
2432 ptid_is_pid(PTID) is true, applies to all threads of the process
2433 pointed at by PTID. Otherwise, apply only to the thread pointed by
2434 PTID. */
2435
2436static int
2437infrun_thread_stop_requested_callback (struct thread_info *info, void *arg)
2438{
2439 ptid_t ptid = * (ptid_t *) arg;
2440
2441 if ((ptid_equal (info->ptid, ptid)
2442 || ptid_equal (minus_one_ptid, ptid)
2443 || (ptid_is_pid (ptid)
2444 && ptid_get_pid (ptid) == ptid_get_pid (info->ptid)))
2445 && is_running (info->ptid)
2446 && !is_executing (info->ptid))
2447 {
2448 struct cleanup *old_chain;
2449 struct execution_control_state ecss;
2450 struct execution_control_state *ecs = &ecss;
2451
2452 memset (ecs, 0, sizeof (*ecs));
2453
2454 old_chain = make_cleanup_restore_current_thread ();
2455
252fbfc8
PA
2456 /* Go through handle_inferior_event/normal_stop, so we always
2457 have consistent output as if the stop event had been
2458 reported. */
2459 ecs->ptid = info->ptid;
e09875d4 2460 ecs->event_thread = find_thread_ptid (info->ptid);
252fbfc8 2461 ecs->ws.kind = TARGET_WAITKIND_STOPPED;
a493e3e2 2462 ecs->ws.value.sig = GDB_SIGNAL_0;
252fbfc8
PA
2463
2464 handle_inferior_event (ecs);
2465
2466 if (!ecs->wait_some_more)
2467 {
2468 struct thread_info *tp;
2469
2470 normal_stop ();
2471
fa4cd53f 2472 /* Finish off the continuations. */
252fbfc8 2473 tp = inferior_thread ();
fa4cd53f
PA
2474 do_all_intermediate_continuations_thread (tp, 1);
2475 do_all_continuations_thread (tp, 1);
252fbfc8
PA
2476 }
2477
2478 do_cleanups (old_chain);
2479 }
2480
2481 return 0;
2482}
2483
2484/* This function is attached as a "thread_stop_requested" observer.
2485 Cleanup local state that assumed the PTID was to be resumed, and
2486 report the stop to the frontend. */
2487
2c0b251b 2488static void
252fbfc8
PA
2489infrun_thread_stop_requested (ptid_t ptid)
2490{
fc1cf338 2491 struct displaced_step_inferior_state *displaced;
252fbfc8
PA
2492
2493 /* PTID was requested to stop. Remove it from the displaced
2494 stepping queue, so we don't try to resume it automatically. */
fc1cf338
PA
2495
2496 for (displaced = displaced_step_inferior_states;
2497 displaced;
2498 displaced = displaced->next)
252fbfc8 2499 {
fc1cf338 2500 struct displaced_step_request *it, **prev_next_p;
252fbfc8 2501
fc1cf338
PA
2502 it = displaced->step_request_queue;
2503 prev_next_p = &displaced->step_request_queue;
2504 while (it)
252fbfc8 2505 {
fc1cf338
PA
2506 if (ptid_match (it->ptid, ptid))
2507 {
2508 *prev_next_p = it->next;
2509 it->next = NULL;
2510 xfree (it);
2511 }
252fbfc8 2512 else
fc1cf338
PA
2513 {
2514 prev_next_p = &it->next;
2515 }
252fbfc8 2516
fc1cf338 2517 it = *prev_next_p;
252fbfc8 2518 }
252fbfc8
PA
2519 }
2520
2521 iterate_over_threads (infrun_thread_stop_requested_callback, &ptid);
2522}
2523
a07daef3
PA
2524static void
2525infrun_thread_thread_exit (struct thread_info *tp, int silent)
2526{
2527 if (ptid_equal (target_last_wait_ptid, tp->ptid))
2528 nullify_last_target_wait_ptid ();
2529}
2530
4e1c45ea
PA
2531/* Callback for iterate_over_threads. */
2532
2533static int
2534delete_step_resume_breakpoint_callback (struct thread_info *info, void *data)
2535{
2536 if (is_exited (info->ptid))
2537 return 0;
2538
2539 delete_step_resume_breakpoint (info);
186c406b 2540 delete_exception_resume_breakpoint (info);
4e1c45ea
PA
2541 return 0;
2542}
2543
2544/* In all-stop, delete the step resume breakpoint of any thread that
2545 had one. In non-stop, delete the step resume breakpoint of the
2546 thread that just stopped. */
2547
2548static void
2549delete_step_thread_step_resume_breakpoint (void)
2550{
2551 if (!target_has_execution
2552 || ptid_equal (inferior_ptid, null_ptid))
2553 /* If the inferior has exited, we have already deleted the step
2554 resume breakpoints out of GDB's lists. */
2555 return;
2556
2557 if (non_stop)
2558 {
2559 /* If in non-stop mode, only delete the step-resume or
2560 longjmp-resume breakpoint of the thread that just stopped
2561 stepping. */
2562 struct thread_info *tp = inferior_thread ();
abbb1732 2563
4e1c45ea 2564 delete_step_resume_breakpoint (tp);
186c406b 2565 delete_exception_resume_breakpoint (tp);
4e1c45ea
PA
2566 }
2567 else
2568 /* In all-stop mode, delete all step-resume and longjmp-resume
2569 breakpoints of any thread that had them. */
2570 iterate_over_threads (delete_step_resume_breakpoint_callback, NULL);
2571}
2572
1777feb0 2573/* A cleanup wrapper. */
4e1c45ea
PA
2574
2575static void
2576delete_step_thread_step_resume_breakpoint_cleanup (void *arg)
2577{
2578 delete_step_thread_step_resume_breakpoint ();
2579}
2580
223698f8
DE
2581/* Pretty print the results of target_wait, for debugging purposes. */
2582
2583static void
2584print_target_wait_results (ptid_t waiton_ptid, ptid_t result_ptid,
2585 const struct target_waitstatus *ws)
2586{
2587 char *status_string = target_waitstatus_to_string (ws);
2588 struct ui_file *tmp_stream = mem_fileopen ();
2589 char *text;
223698f8
DE
2590
2591 /* The text is split over several lines because it was getting too long.
2592 Call fprintf_unfiltered (gdb_stdlog) once so that the text is still
2593 output as a unit; we want only one timestamp printed if debug_timestamp
2594 is set. */
2595
2596 fprintf_unfiltered (tmp_stream,
2597 "infrun: target_wait (%d", PIDGET (waiton_ptid));
2598 if (PIDGET (waiton_ptid) != -1)
2599 fprintf_unfiltered (tmp_stream,
2600 " [%s]", target_pid_to_str (waiton_ptid));
2601 fprintf_unfiltered (tmp_stream, ", status) =\n");
2602 fprintf_unfiltered (tmp_stream,
2603 "infrun: %d [%s],\n",
2604 PIDGET (result_ptid), target_pid_to_str (result_ptid));
2605 fprintf_unfiltered (tmp_stream,
2606 "infrun: %s\n",
2607 status_string);
2608
759ef836 2609 text = ui_file_xstrdup (tmp_stream, NULL);
223698f8
DE
2610
2611 /* This uses %s in part to handle %'s in the text, but also to avoid
2612 a gcc error: the format attribute requires a string literal. */
2613 fprintf_unfiltered (gdb_stdlog, "%s", text);
2614
2615 xfree (status_string);
2616 xfree (text);
2617 ui_file_delete (tmp_stream);
2618}
2619
24291992
PA
2620/* Prepare and stabilize the inferior for detaching it. E.g.,
2621 detaching while a thread is displaced stepping is a recipe for
2622 crashing it, as nothing would readjust the PC out of the scratch
2623 pad. */
2624
2625void
2626prepare_for_detach (void)
2627{
2628 struct inferior *inf = current_inferior ();
2629 ptid_t pid_ptid = pid_to_ptid (inf->pid);
2630 struct cleanup *old_chain_1;
2631 struct displaced_step_inferior_state *displaced;
2632
2633 displaced = get_displaced_stepping_state (inf->pid);
2634
2635 /* Is any thread of this process displaced stepping? If not,
2636 there's nothing else to do. */
2637 if (displaced == NULL || ptid_equal (displaced->step_ptid, null_ptid))
2638 return;
2639
2640 if (debug_infrun)
2641 fprintf_unfiltered (gdb_stdlog,
2642 "displaced-stepping in-process while detaching");
2643
2644 old_chain_1 = make_cleanup_restore_integer (&inf->detaching);
2645 inf->detaching = 1;
2646
2647 while (!ptid_equal (displaced->step_ptid, null_ptid))
2648 {
2649 struct cleanup *old_chain_2;
2650 struct execution_control_state ecss;
2651 struct execution_control_state *ecs;
2652
2653 ecs = &ecss;
2654 memset (ecs, 0, sizeof (*ecs));
2655
2656 overlay_cache_invalid = 1;
2657
24291992
PA
2658 if (deprecated_target_wait_hook)
2659 ecs->ptid = deprecated_target_wait_hook (pid_ptid, &ecs->ws, 0);
2660 else
2661 ecs->ptid = target_wait (pid_ptid, &ecs->ws, 0);
2662
2663 if (debug_infrun)
2664 print_target_wait_results (pid_ptid, ecs->ptid, &ecs->ws);
2665
2666 /* If an error happens while handling the event, propagate GDB's
2667 knowledge of the executing state to the frontend/user running
2668 state. */
3e43a32a
MS
2669 old_chain_2 = make_cleanup (finish_thread_state_cleanup,
2670 &minus_one_ptid);
24291992
PA
2671
2672 /* Now figure out what to do with the result of the result. */
2673 handle_inferior_event (ecs);
2674
2675 /* No error, don't finish the state yet. */
2676 discard_cleanups (old_chain_2);
2677
2678 /* Breakpoints and watchpoints are not installed on the target
2679 at this point, and signals are passed directly to the
2680 inferior, so this must mean the process is gone. */
2681 if (!ecs->wait_some_more)
2682 {
2683 discard_cleanups (old_chain_1);
2684 error (_("Program exited while detaching"));
2685 }
2686 }
2687
2688 discard_cleanups (old_chain_1);
2689}
2690
cd0fc7c3 2691/* Wait for control to return from inferior to debugger.
ae123ec6 2692
cd0fc7c3
SS
2693 If inferior gets a signal, we may decide to start it up again
2694 instead of returning. That is why there is a loop in this function.
2695 When this function actually returns it means the inferior
2696 should be left stopped and GDB should read more commands. */
2697
2698void
e4c8541f 2699wait_for_inferior (void)
cd0fc7c3
SS
2700{
2701 struct cleanup *old_cleanups;
c906108c 2702
527159b7 2703 if (debug_infrun)
ae123ec6 2704 fprintf_unfiltered
e4c8541f 2705 (gdb_stdlog, "infrun: wait_for_inferior ()\n");
527159b7 2706
4e1c45ea
PA
2707 old_cleanups =
2708 make_cleanup (delete_step_thread_step_resume_breakpoint_cleanup, NULL);
cd0fc7c3 2709
c906108c
SS
2710 while (1)
2711 {
ae25568b
PA
2712 struct execution_control_state ecss;
2713 struct execution_control_state *ecs = &ecss;
29f49a6a
PA
2714 struct cleanup *old_chain;
2715
ae25568b
PA
2716 memset (ecs, 0, sizeof (*ecs));
2717
ec9499be 2718 overlay_cache_invalid = 1;
ec9499be 2719
9a4105ab 2720 if (deprecated_target_wait_hook)
47608cb1 2721 ecs->ptid = deprecated_target_wait_hook (waiton_ptid, &ecs->ws, 0);
cd0fc7c3 2722 else
47608cb1 2723 ecs->ptid = target_wait (waiton_ptid, &ecs->ws, 0);
c906108c 2724
f00150c9 2725 if (debug_infrun)
223698f8 2726 print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
f00150c9 2727
29f49a6a
PA
2728 /* If an error happens while handling the event, propagate GDB's
2729 knowledge of the executing state to the frontend/user running
2730 state. */
2731 old_chain = make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
2732
cd0fc7c3
SS
2733 /* Now figure out what to do with the result of the result. */
2734 handle_inferior_event (ecs);
c906108c 2735
29f49a6a
PA
2736 /* No error, don't finish the state yet. */
2737 discard_cleanups (old_chain);
2738
cd0fc7c3
SS
2739 if (!ecs->wait_some_more)
2740 break;
2741 }
4e1c45ea 2742
cd0fc7c3
SS
2743 do_cleanups (old_cleanups);
2744}
c906108c 2745
1777feb0 2746/* Asynchronous version of wait_for_inferior. It is called by the
43ff13b4 2747 event loop whenever a change of state is detected on the file
1777feb0
MS
2748 descriptor corresponding to the target. It can be called more than
2749 once to complete a single execution command. In such cases we need
2750 to keep the state in a global variable ECSS. If it is the last time
a474d7c2
PA
2751 that this function is called for a single execution command, then
2752 report to the user that the inferior has stopped, and do the
1777feb0 2753 necessary cleanups. */
43ff13b4
JM
2754
2755void
fba45db2 2756fetch_inferior_event (void *client_data)
43ff13b4 2757{
0d1e5fa7 2758 struct execution_control_state ecss;
a474d7c2 2759 struct execution_control_state *ecs = &ecss;
4f8d22e3 2760 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
29f49a6a 2761 struct cleanup *ts_old_chain;
4f8d22e3 2762 int was_sync = sync_execution;
0f641c01 2763 int cmd_done = 0;
43ff13b4 2764
0d1e5fa7
PA
2765 memset (ecs, 0, sizeof (*ecs));
2766
c5187ac6
PA
2767 /* We're handling a live event, so make sure we're doing live
2768 debugging. If we're looking at traceframes while the target is
2769 running, we're going to need to get back to that mode after
2770 handling the event. */
2771 if (non_stop)
2772 {
2773 make_cleanup_restore_current_traceframe ();
e6e4e701 2774 set_current_traceframe (-1);
c5187ac6
PA
2775 }
2776
4f8d22e3
PA
2777 if (non_stop)
2778 /* In non-stop mode, the user/frontend should not notice a thread
2779 switch due to internal events. Make sure we reverse to the
2780 user selected thread and frame after handling the event and
2781 running any breakpoint commands. */
2782 make_cleanup_restore_current_thread ();
2783
ec9499be 2784 overlay_cache_invalid = 1;
3dd5b83d 2785
32231432
PA
2786 make_cleanup_restore_integer (&execution_direction);
2787 execution_direction = target_execution_direction ();
2788
9a4105ab 2789 if (deprecated_target_wait_hook)
a474d7c2 2790 ecs->ptid =
47608cb1 2791 deprecated_target_wait_hook (waiton_ptid, &ecs->ws, TARGET_WNOHANG);
43ff13b4 2792 else
47608cb1 2793 ecs->ptid = target_wait (waiton_ptid, &ecs->ws, TARGET_WNOHANG);
43ff13b4 2794
f00150c9 2795 if (debug_infrun)
223698f8 2796 print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
f00150c9 2797
29f49a6a
PA
2798 /* If an error happens while handling the event, propagate GDB's
2799 knowledge of the executing state to the frontend/user running
2800 state. */
2801 if (!non_stop)
2802 ts_old_chain = make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
2803 else
2804 ts_old_chain = make_cleanup (finish_thread_state_cleanup, &ecs->ptid);
2805
353d1d73
JK
2806 /* Get executed before make_cleanup_restore_current_thread above to apply
2807 still for the thread which has thrown the exception. */
2808 make_bpstat_clear_actions_cleanup ();
2809
43ff13b4 2810 /* Now figure out what to do with the result of the result. */
a474d7c2 2811 handle_inferior_event (ecs);
43ff13b4 2812
a474d7c2 2813 if (!ecs->wait_some_more)
43ff13b4 2814 {
d6b48e9c
PA
2815 struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
2816
4e1c45ea 2817 delete_step_thread_step_resume_breakpoint ();
f107f563 2818
d6b48e9c 2819 /* We may not find an inferior if this was a process exit. */
16c381f0 2820 if (inf == NULL || inf->control.stop_soon == NO_STOP_QUIETLY)
83c265ab
PA
2821 normal_stop ();
2822
af679fd0 2823 if (target_has_execution
0e5bf2a8 2824 && ecs->ws.kind != TARGET_WAITKIND_NO_RESUMED
af679fd0
PA
2825 && ecs->ws.kind != TARGET_WAITKIND_EXITED
2826 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
2827 && ecs->event_thread->step_multi
16c381f0 2828 && ecs->event_thread->control.stop_step)
c2d11a7d
JM
2829 inferior_event_handler (INF_EXEC_CONTINUE, NULL);
2830 else
0f641c01
PA
2831 {
2832 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
2833 cmd_done = 1;
2834 }
43ff13b4 2835 }
4f8d22e3 2836
29f49a6a
PA
2837 /* No error, don't finish the thread states yet. */
2838 discard_cleanups (ts_old_chain);
2839
4f8d22e3
PA
2840 /* Revert thread and frame. */
2841 do_cleanups (old_chain);
2842
2843 /* If the inferior was in sync execution mode, and now isn't,
0f641c01
PA
2844 restore the prompt (a synchronous execution command has finished,
2845 and we're ready for input). */
b4a14fd0 2846 if (interpreter_async && was_sync && !sync_execution)
4f8d22e3 2847 display_gdb_prompt (0);
0f641c01
PA
2848
2849 if (cmd_done
2850 && !was_sync
2851 && exec_done_display_p
2852 && (ptid_equal (inferior_ptid, null_ptid)
2853 || !is_running (inferior_ptid)))
2854 printf_unfiltered (_("completed.\n"));
43ff13b4
JM
2855}
2856
edb3359d
DJ
2857/* Record the frame and location we're currently stepping through. */
2858void
2859set_step_info (struct frame_info *frame, struct symtab_and_line sal)
2860{
2861 struct thread_info *tp = inferior_thread ();
2862
16c381f0
JK
2863 tp->control.step_frame_id = get_frame_id (frame);
2864 tp->control.step_stack_frame_id = get_stack_frame_id (frame);
edb3359d
DJ
2865
2866 tp->current_symtab = sal.symtab;
2867 tp->current_line = sal.line;
2868}
2869
0d1e5fa7
PA
2870/* Clear context switchable stepping state. */
2871
2872void
4e1c45ea 2873init_thread_stepping_state (struct thread_info *tss)
0d1e5fa7
PA
2874{
2875 tss->stepping_over_breakpoint = 0;
2876 tss->step_after_step_resume_breakpoint = 0;
cd0fc7c3
SS
2877}
2878
e02bc4cc 2879/* Return the cached copy of the last pid/waitstatus returned by
9a4105ab
AC
2880 target_wait()/deprecated_target_wait_hook(). The data is actually
2881 cached by handle_inferior_event(), which gets called immediately
2882 after target_wait()/deprecated_target_wait_hook(). */
e02bc4cc
DS
2883
2884void
488f131b 2885get_last_target_status (ptid_t *ptidp, struct target_waitstatus *status)
e02bc4cc 2886{
39f77062 2887 *ptidp = target_last_wait_ptid;
e02bc4cc
DS
2888 *status = target_last_waitstatus;
2889}
2890
ac264b3b
MS
2891void
2892nullify_last_target_wait_ptid (void)
2893{
2894 target_last_wait_ptid = minus_one_ptid;
2895}
2896
dcf4fbde 2897/* Switch thread contexts. */
dd80620e
MS
2898
2899static void
0d1e5fa7 2900context_switch (ptid_t ptid)
dd80620e 2901{
4b51d87b 2902 if (debug_infrun && !ptid_equal (ptid, inferior_ptid))
fd48f117
DJ
2903 {
2904 fprintf_unfiltered (gdb_stdlog, "infrun: Switching context from %s ",
2905 target_pid_to_str (inferior_ptid));
2906 fprintf_unfiltered (gdb_stdlog, "to %s\n",
0d1e5fa7 2907 target_pid_to_str (ptid));
fd48f117
DJ
2908 }
2909
0d1e5fa7 2910 switch_to_thread (ptid);
dd80620e
MS
2911}
2912
4fa8626c
DJ
2913static void
2914adjust_pc_after_break (struct execution_control_state *ecs)
2915{
24a73cce
UW
2916 struct regcache *regcache;
2917 struct gdbarch *gdbarch;
6c95b8df 2918 struct address_space *aspace;
8aad930b 2919 CORE_ADDR breakpoint_pc;
4fa8626c 2920
4fa8626c
DJ
2921 /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP. If
2922 we aren't, just return.
9709f61c
DJ
2923
2924 We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
b798847d
UW
2925 affected by gdbarch_decr_pc_after_break. Other waitkinds which are
2926 implemented by software breakpoints should be handled through the normal
2927 breakpoint layer.
8fb3e588 2928
4fa8626c
DJ
2929 NOTE drow/2004-01-31: On some targets, breakpoints may generate
2930 different signals (SIGILL or SIGEMT for instance), but it is less
2931 clear where the PC is pointing afterwards. It may not match
b798847d
UW
2932 gdbarch_decr_pc_after_break. I don't know any specific target that
2933 generates these signals at breakpoints (the code has been in GDB since at
2934 least 1992) so I can not guess how to handle them here.
8fb3e588 2935
e6cf7916
UW
2936 In earlier versions of GDB, a target with
2937 gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
b798847d
UW
2938 watchpoint affected by gdbarch_decr_pc_after_break. I haven't found any
2939 target with both of these set in GDB history, and it seems unlikely to be
2940 correct, so gdbarch_have_nonsteppable_watchpoint is not checked here. */
4fa8626c
DJ
2941
2942 if (ecs->ws.kind != TARGET_WAITKIND_STOPPED)
2943 return;
2944
a493e3e2 2945 if (ecs->ws.value.sig != GDB_SIGNAL_TRAP)
4fa8626c
DJ
2946 return;
2947
4058b839
PA
2948 /* In reverse execution, when a breakpoint is hit, the instruction
2949 under it has already been de-executed. The reported PC always
2950 points at the breakpoint address, so adjusting it further would
2951 be wrong. E.g., consider this case on a decr_pc_after_break == 1
2952 architecture:
2953
2954 B1 0x08000000 : INSN1
2955 B2 0x08000001 : INSN2
2956 0x08000002 : INSN3
2957 PC -> 0x08000003 : INSN4
2958
2959 Say you're stopped at 0x08000003 as above. Reverse continuing
2960 from that point should hit B2 as below. Reading the PC when the
2961 SIGTRAP is reported should read 0x08000001 and INSN2 should have
2962 been de-executed already.
2963
2964 B1 0x08000000 : INSN1
2965 B2 PC -> 0x08000001 : INSN2
2966 0x08000002 : INSN3
2967 0x08000003 : INSN4
2968
2969 We can't apply the same logic as for forward execution, because
2970 we would wrongly adjust the PC to 0x08000000, since there's a
2971 breakpoint at PC - 1. We'd then report a hit on B1, although
2972 INSN1 hadn't been de-executed yet. Doing nothing is the correct
2973 behaviour. */
2974 if (execution_direction == EXEC_REVERSE)
2975 return;
2976
24a73cce
UW
2977 /* If this target does not decrement the PC after breakpoints, then
2978 we have nothing to do. */
2979 regcache = get_thread_regcache (ecs->ptid);
2980 gdbarch = get_regcache_arch (regcache);
2981 if (gdbarch_decr_pc_after_break (gdbarch) == 0)
2982 return;
2983
6c95b8df
PA
2984 aspace = get_regcache_aspace (regcache);
2985
8aad930b
AC
2986 /* Find the location where (if we've hit a breakpoint) the
2987 breakpoint would be. */
515630c5
UW
2988 breakpoint_pc = regcache_read_pc (regcache)
2989 - gdbarch_decr_pc_after_break (gdbarch);
8aad930b 2990
1c5cfe86
PA
2991 /* Check whether there actually is a software breakpoint inserted at
2992 that location.
2993
2994 If in non-stop mode, a race condition is possible where we've
2995 removed a breakpoint, but stop events for that breakpoint were
2996 already queued and arrive later. To suppress those spurious
2997 SIGTRAPs, we keep a list of such breakpoint locations for a bit,
2998 and retire them after a number of stop events are reported. */
6c95b8df
PA
2999 if (software_breakpoint_inserted_here_p (aspace, breakpoint_pc)
3000 || (non_stop && moribund_breakpoint_here_p (aspace, breakpoint_pc)))
8aad930b 3001 {
96429cc8 3002 struct cleanup *old_cleanups = NULL;
abbb1732 3003
96429cc8
HZ
3004 if (RECORD_IS_USED)
3005 old_cleanups = record_gdb_operation_disable_set ();
3006
1c0fdd0e
UW
3007 /* When using hardware single-step, a SIGTRAP is reported for both
3008 a completed single-step and a software breakpoint. Need to
3009 differentiate between the two, as the latter needs adjusting
3010 but the former does not.
3011
3012 The SIGTRAP can be due to a completed hardware single-step only if
3013 - we didn't insert software single-step breakpoints
3014 - the thread to be examined is still the current thread
3015 - this thread is currently being stepped
3016
3017 If any of these events did not occur, we must have stopped due
3018 to hitting a software breakpoint, and have to back up to the
3019 breakpoint address.
3020
3021 As a special case, we could have hardware single-stepped a
3022 software breakpoint. In this case (prev_pc == breakpoint_pc),
3023 we also need to back up to the breakpoint address. */
3024
3025 if (singlestep_breakpoints_inserted_p
3026 || !ptid_equal (ecs->ptid, inferior_ptid)
4e1c45ea
PA
3027 || !currently_stepping (ecs->event_thread)
3028 || ecs->event_thread->prev_pc == breakpoint_pc)
515630c5 3029 regcache_write_pc (regcache, breakpoint_pc);
96429cc8
HZ
3030
3031 if (RECORD_IS_USED)
3032 do_cleanups (old_cleanups);
8aad930b 3033 }
4fa8626c
DJ
3034}
3035
7a76f5b8 3036static void
0d1e5fa7
PA
3037init_infwait_state (void)
3038{
3039 waiton_ptid = pid_to_ptid (-1);
3040 infwait_state = infwait_normal_state;
3041}
3042
edb3359d
DJ
3043static int
3044stepped_in_from (struct frame_info *frame, struct frame_id step_frame_id)
3045{
3046 for (frame = get_prev_frame (frame);
3047 frame != NULL;
3048 frame = get_prev_frame (frame))
3049 {
3050 if (frame_id_eq (get_frame_id (frame), step_frame_id))
3051 return 1;
3052 if (get_frame_type (frame) != INLINE_FRAME)
3053 break;
3054 }
3055
3056 return 0;
3057}
3058
a96d9b2e
SDJ
3059/* Auxiliary function that handles syscall entry/return events.
3060 It returns 1 if the inferior should keep going (and GDB
3061 should ignore the event), or 0 if the event deserves to be
3062 processed. */
ca2163eb 3063
a96d9b2e 3064static int
ca2163eb 3065handle_syscall_event (struct execution_control_state *ecs)
a96d9b2e 3066{
ca2163eb
PA
3067 struct regcache *regcache;
3068 struct gdbarch *gdbarch;
3069 int syscall_number;
3070
3071 if (!ptid_equal (ecs->ptid, inferior_ptid))
3072 context_switch (ecs->ptid);
3073
3074 regcache = get_thread_regcache (ecs->ptid);
3075 gdbarch = get_regcache_arch (regcache);
f90263c1 3076 syscall_number = ecs->ws.value.syscall_number;
ca2163eb
PA
3077 stop_pc = regcache_read_pc (regcache);
3078
a96d9b2e
SDJ
3079 if (catch_syscall_enabled () > 0
3080 && catching_syscall_number (syscall_number) > 0)
3081 {
3082 if (debug_infrun)
3083 fprintf_unfiltered (gdb_stdlog, "infrun: syscall number = '%d'\n",
3084 syscall_number);
a96d9b2e 3085
16c381f0 3086 ecs->event_thread->control.stop_bpstat
6c95b8df 3087 = bpstat_stop_status (get_regcache_aspace (regcache),
09ac7c10 3088 stop_pc, ecs->ptid, &ecs->ws);
16c381f0
JK
3089 ecs->random_signal
3090 = !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat);
a96d9b2e 3091
ca2163eb
PA
3092 if (!ecs->random_signal)
3093 {
3094 /* Catchpoint hit. */
a493e3e2 3095 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_TRAP;
ca2163eb
PA
3096 return 0;
3097 }
a96d9b2e 3098 }
ca2163eb
PA
3099
3100 /* If no catchpoint triggered for this, then keep going. */
a493e3e2 3101 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
ca2163eb
PA
3102 keep_going (ecs);
3103 return 1;
a96d9b2e
SDJ
3104}
3105
7e324e48
GB
3106/* Clear the supplied execution_control_state's stop_func_* fields. */
3107
3108static void
3109clear_stop_func (struct execution_control_state *ecs)
3110{
3111 ecs->stop_func_filled_in = 0;
3112 ecs->stop_func_start = 0;
3113 ecs->stop_func_end = 0;
3114 ecs->stop_func_name = NULL;
3115}
3116
3117/* Lazily fill in the execution_control_state's stop_func_* fields. */
3118
3119static void
3120fill_in_stop_func (struct gdbarch *gdbarch,
3121 struct execution_control_state *ecs)
3122{
3123 if (!ecs->stop_func_filled_in)
3124 {
3125 /* Don't care about return value; stop_func_start and stop_func_name
3126 will both be 0 if it doesn't work. */
3127 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
3128 &ecs->stop_func_start, &ecs->stop_func_end);
3129 ecs->stop_func_start
3130 += gdbarch_deprecated_function_start_offset (gdbarch);
3131
3132 ecs->stop_func_filled_in = 1;
3133 }
3134}
3135
cd0fc7c3
SS
3136/* Given an execution control state that has been freshly filled in
3137 by an event from the inferior, figure out what it means and take
3138 appropriate action. */
c906108c 3139
ec9499be 3140static void
96baa820 3141handle_inferior_event (struct execution_control_state *ecs)
cd0fc7c3 3142{
568d6575
UW
3143 struct frame_info *frame;
3144 struct gdbarch *gdbarch;
d983da9c
DJ
3145 int stopped_by_watchpoint;
3146 int stepped_after_stopped_by_watchpoint = 0;
2afb61aa 3147 struct symtab_and_line stop_pc_sal;
d6b48e9c
PA
3148 enum stop_kind stop_soon;
3149
28736962
PA
3150 if (ecs->ws.kind == TARGET_WAITKIND_IGNORE)
3151 {
3152 /* We had an event in the inferior, but we are not interested in
3153 handling it at this level. The lower layers have already
3154 done what needs to be done, if anything.
3155
3156 One of the possible circumstances for this is when the
3157 inferior produces output for the console. The inferior has
3158 not stopped, and we are ignoring the event. Another possible
3159 circumstance is any event which the lower level knows will be
3160 reported multiple times without an intervening resume. */
3161 if (debug_infrun)
3162 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_IGNORE\n");
3163 prepare_to_wait (ecs);
3164 return;
3165 }
3166
0e5bf2a8
PA
3167 if (ecs->ws.kind == TARGET_WAITKIND_NO_RESUMED
3168 && target_can_async_p () && !sync_execution)
3169 {
3170 /* There were no unwaited-for children left in the target, but,
3171 we're not synchronously waiting for events either. Just
3172 ignore. Otherwise, if we were running a synchronous
3173 execution command, we need to cancel it and give the user
3174 back the terminal. */
3175 if (debug_infrun)
3176 fprintf_unfiltered (gdb_stdlog,
3177 "infrun: TARGET_WAITKIND_NO_RESUMED (ignoring)\n");
3178 prepare_to_wait (ecs);
3179 return;
3180 }
3181
d6b48e9c 3182 if (ecs->ws.kind != TARGET_WAITKIND_EXITED
0e5bf2a8
PA
3183 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
3184 && ecs->ws.kind != TARGET_WAITKIND_NO_RESUMED)
d6b48e9c
PA
3185 {
3186 struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
abbb1732 3187
d6b48e9c 3188 gdb_assert (inf);
16c381f0 3189 stop_soon = inf->control.stop_soon;
d6b48e9c
PA
3190 }
3191 else
3192 stop_soon = NO_STOP_QUIETLY;
cd0fc7c3 3193
1777feb0 3194 /* Cache the last pid/waitstatus. */
39f77062 3195 target_last_wait_ptid = ecs->ptid;
0d1e5fa7 3196 target_last_waitstatus = ecs->ws;
e02bc4cc 3197
ca005067 3198 /* Always clear state belonging to the previous time we stopped. */
aa7d318d 3199 stop_stack_dummy = STOP_NONE;
ca005067 3200
0e5bf2a8
PA
3201 if (ecs->ws.kind == TARGET_WAITKIND_NO_RESUMED)
3202 {
3203 /* No unwaited-for children left. IOW, all resumed children
3204 have exited. */
3205 if (debug_infrun)
3206 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_NO_RESUMED\n");
3207
3208 stop_print_frame = 0;
3209 stop_stepping (ecs);
3210 return;
3211 }
3212
8c90c137 3213 if (ecs->ws.kind != TARGET_WAITKIND_EXITED
64776a0b 3214 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED)
359f5fe6
PA
3215 {
3216 ecs->event_thread = find_thread_ptid (ecs->ptid);
3217 /* If it's a new thread, add it to the thread database. */
3218 if (ecs->event_thread == NULL)
3219 ecs->event_thread = add_thread (ecs->ptid);
3220 }
88ed393a
JK
3221
3222 /* Dependent on valid ECS->EVENT_THREAD. */
3223 adjust_pc_after_break (ecs);
3224
3225 /* Dependent on the current PC value modified by adjust_pc_after_break. */
3226 reinit_frame_cache ();
3227
28736962
PA
3228 breakpoint_retire_moribund ();
3229
2b009048
DJ
3230 /* First, distinguish signals caused by the debugger from signals
3231 that have to do with the program's own actions. Note that
3232 breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
3233 on the operating system version. Here we detect when a SIGILL or
3234 SIGEMT is really a breakpoint and change it to SIGTRAP. We do
3235 something similar for SIGSEGV, since a SIGSEGV will be generated
3236 when we're trying to execute a breakpoint instruction on a
3237 non-executable stack. This happens for call dummy breakpoints
3238 for architectures like SPARC that place call dummies on the
3239 stack. */
2b009048 3240 if (ecs->ws.kind == TARGET_WAITKIND_STOPPED
a493e3e2
PA
3241 && (ecs->ws.value.sig == GDB_SIGNAL_ILL
3242 || ecs->ws.value.sig == GDB_SIGNAL_SEGV
3243 || ecs->ws.value.sig == GDB_SIGNAL_EMT))
2b009048 3244 {
de0a0249
UW
3245 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3246
3247 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
3248 regcache_read_pc (regcache)))
3249 {
3250 if (debug_infrun)
3251 fprintf_unfiltered (gdb_stdlog,
3252 "infrun: Treating signal as SIGTRAP\n");
a493e3e2 3253 ecs->ws.value.sig = GDB_SIGNAL_TRAP;
de0a0249 3254 }
2b009048
DJ
3255 }
3256
28736962
PA
3257 /* Mark the non-executing threads accordingly. In all-stop, all
3258 threads of all processes are stopped when we get any event
3259 reported. In non-stop mode, only the event thread stops. If
3260 we're handling a process exit in non-stop mode, there's nothing
3261 to do, as threads of the dead process are gone, and threads of
3262 any other process were left running. */
3263 if (!non_stop)
3264 set_executing (minus_one_ptid, 0);
3265 else if (ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
3266 && ecs->ws.kind != TARGET_WAITKIND_EXITED)
7aee8dc2 3267 set_executing (ecs->ptid, 0);
8c90c137 3268
0d1e5fa7 3269 switch (infwait_state)
488f131b
JB
3270 {
3271 case infwait_thread_hop_state:
527159b7 3272 if (debug_infrun)
8a9de0e4 3273 fprintf_unfiltered (gdb_stdlog, "infrun: infwait_thread_hop_state\n");
65e82032 3274 break;
b83266a0 3275
488f131b 3276 case infwait_normal_state:
527159b7 3277 if (debug_infrun)
8a9de0e4 3278 fprintf_unfiltered (gdb_stdlog, "infrun: infwait_normal_state\n");
d983da9c
DJ
3279 break;
3280
3281 case infwait_step_watch_state:
3282 if (debug_infrun)
3283 fprintf_unfiltered (gdb_stdlog,
3284 "infrun: infwait_step_watch_state\n");
3285
3286 stepped_after_stopped_by_watchpoint = 1;
488f131b 3287 break;
b83266a0 3288
488f131b 3289 case infwait_nonstep_watch_state:
527159b7 3290 if (debug_infrun)
8a9de0e4
AC
3291 fprintf_unfiltered (gdb_stdlog,
3292 "infrun: infwait_nonstep_watch_state\n");
488f131b 3293 insert_breakpoints ();
c906108c 3294
488f131b
JB
3295 /* FIXME-maybe: is this cleaner than setting a flag? Does it
3296 handle things like signals arriving and other things happening
3297 in combination correctly? */
3298 stepped_after_stopped_by_watchpoint = 1;
3299 break;
65e82032
AC
3300
3301 default:
e2e0b3e5 3302 internal_error (__FILE__, __LINE__, _("bad switch"));
488f131b 3303 }
ec9499be 3304
0d1e5fa7 3305 infwait_state = infwait_normal_state;
ec9499be 3306 waiton_ptid = pid_to_ptid (-1);
c906108c 3307
488f131b
JB
3308 switch (ecs->ws.kind)
3309 {
3310 case TARGET_WAITKIND_LOADED:
527159b7 3311 if (debug_infrun)
8a9de0e4 3312 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_LOADED\n");
b0f4b84b
DJ
3313 /* Ignore gracefully during startup of the inferior, as it might
3314 be the shell which has just loaded some objects, otherwise
3315 add the symbols for the newly loaded objects. Also ignore at
3316 the beginning of an attach or remote session; we will query
3317 the full list of libraries once the connection is
3318 established. */
c0236d92 3319 if (stop_soon == NO_STOP_QUIETLY)
488f131b 3320 {
edcc5120
TT
3321 struct regcache *regcache;
3322
3323 if (!ptid_equal (ecs->ptid, inferior_ptid))
3324 context_switch (ecs->ptid);
3325 regcache = get_thread_regcache (ecs->ptid);
3326
3327 handle_solib_event ();
3328
3329 ecs->event_thread->control.stop_bpstat
3330 = bpstat_stop_status (get_regcache_aspace (regcache),
3331 stop_pc, ecs->ptid, &ecs->ws);
3332 ecs->random_signal
3333 = !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat);
3334
3335 if (!ecs->random_signal)
3336 {
3337 /* A catchpoint triggered. */
a493e3e2 3338 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_TRAP;
edcc5120
TT
3339 goto process_event_stop_test;
3340 }
488f131b 3341
b0f4b84b
DJ
3342 /* If requested, stop when the dynamic linker notifies
3343 gdb of events. This allows the user to get control
3344 and place breakpoints in initializer routines for
3345 dynamically loaded objects (among other things). */
a493e3e2 3346 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
b0f4b84b
DJ
3347 if (stop_on_solib_events)
3348 {
55409f9d
DJ
3349 /* Make sure we print "Stopped due to solib-event" in
3350 normal_stop. */
3351 stop_print_frame = 1;
3352
b0f4b84b
DJ
3353 stop_stepping (ecs);
3354 return;
3355 }
488f131b 3356 }
b0f4b84b
DJ
3357
3358 /* If we are skipping through a shell, or through shared library
3359 loading that we aren't interested in, resume the program. If
3360 we're running the program normally, also resume. But stop if
3361 we're attaching or setting up a remote connection. */
3362 if (stop_soon == STOP_QUIETLY || stop_soon == NO_STOP_QUIETLY)
3363 {
8b3ee56d
PA
3364 if (!ptid_equal (ecs->ptid, inferior_ptid))
3365 context_switch (ecs->ptid);
3366
74960c60
VP
3367 /* Loading of shared libraries might have changed breakpoint
3368 addresses. Make sure new breakpoints are inserted. */
0b02b92d
UW
3369 if (stop_soon == NO_STOP_QUIETLY
3370 && !breakpoints_always_inserted_mode ())
74960c60 3371 insert_breakpoints ();
a493e3e2 3372 resume (0, GDB_SIGNAL_0);
b0f4b84b
DJ
3373 prepare_to_wait (ecs);
3374 return;
3375 }
3376
3377 break;
c5aa993b 3378
488f131b 3379 case TARGET_WAITKIND_SPURIOUS:
527159b7 3380 if (debug_infrun)
8a9de0e4 3381 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SPURIOUS\n");
64776a0b 3382 if (!ptid_equal (ecs->ptid, inferior_ptid))
8b3ee56d 3383 context_switch (ecs->ptid);
a493e3e2 3384 resume (0, GDB_SIGNAL_0);
488f131b
JB
3385 prepare_to_wait (ecs);
3386 return;
c5aa993b 3387
488f131b 3388 case TARGET_WAITKIND_EXITED:
940c3c06 3389 case TARGET_WAITKIND_SIGNALLED:
527159b7 3390 if (debug_infrun)
940c3c06
PA
3391 {
3392 if (ecs->ws.kind == TARGET_WAITKIND_EXITED)
3393 fprintf_unfiltered (gdb_stdlog,
3394 "infrun: TARGET_WAITKIND_EXITED\n");
3395 else
3396 fprintf_unfiltered (gdb_stdlog,
3397 "infrun: TARGET_WAITKIND_SIGNALLED\n");
3398 }
3399
fb66883a 3400 inferior_ptid = ecs->ptid;
6c95b8df
PA
3401 set_current_inferior (find_inferior_pid (ptid_get_pid (ecs->ptid)));
3402 set_current_program_space (current_inferior ()->pspace);
3403 handle_vfork_child_exec_or_exit (0);
1777feb0 3404 target_terminal_ours (); /* Must do this before mourn anyway. */
488f131b 3405
940c3c06
PA
3406 if (ecs->ws.kind == TARGET_WAITKIND_EXITED)
3407 {
3408 /* Record the exit code in the convenience variable $_exitcode, so
3409 that the user can inspect this again later. */
3410 set_internalvar_integer (lookup_internalvar ("_exitcode"),
3411 (LONGEST) ecs->ws.value.integer);
3412
3413 /* Also record this in the inferior itself. */
3414 current_inferior ()->has_exit_code = 1;
3415 current_inferior ()->exit_code = (LONGEST) ecs->ws.value.integer;
8cf64490 3416
940c3c06
PA
3417 print_exited_reason (ecs->ws.value.integer);
3418 }
3419 else
3420 print_signal_exited_reason (ecs->ws.value.sig);
8cf64490 3421
488f131b
JB
3422 gdb_flush (gdb_stdout);
3423 target_mourn_inferior ();
1c0fdd0e 3424 singlestep_breakpoints_inserted_p = 0;
d03285ec 3425 cancel_single_step_breakpoints ();
488f131b
JB
3426 stop_print_frame = 0;
3427 stop_stepping (ecs);
3428 return;
c5aa993b 3429
488f131b 3430 /* The following are the only cases in which we keep going;
1777feb0 3431 the above cases end in a continue or goto. */
488f131b 3432 case TARGET_WAITKIND_FORKED:
deb3b17b 3433 case TARGET_WAITKIND_VFORKED:
527159b7 3434 if (debug_infrun)
fed708ed
PA
3435 {
3436 if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
3437 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_FORKED\n");
3438 else
3439 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_VFORKED\n");
3440 }
c906108c 3441
e2d96639
YQ
3442 /* Check whether the inferior is displaced stepping. */
3443 {
3444 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3445 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3446 struct displaced_step_inferior_state *displaced
3447 = get_displaced_stepping_state (ptid_get_pid (ecs->ptid));
3448
3449 /* If checking displaced stepping is supported, and thread
3450 ecs->ptid is displaced stepping. */
3451 if (displaced && ptid_equal (displaced->step_ptid, ecs->ptid))
3452 {
3453 struct inferior *parent_inf
3454 = find_inferior_pid (ptid_get_pid (ecs->ptid));
3455 struct regcache *child_regcache;
3456 CORE_ADDR parent_pc;
3457
3458 /* GDB has got TARGET_WAITKIND_FORKED or TARGET_WAITKIND_VFORKED,
3459 indicating that the displaced stepping of syscall instruction
3460 has been done. Perform cleanup for parent process here. Note
3461 that this operation also cleans up the child process for vfork,
3462 because their pages are shared. */
a493e3e2 3463 displaced_step_fixup (ecs->ptid, GDB_SIGNAL_TRAP);
e2d96639
YQ
3464
3465 if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
3466 {
3467 /* Restore scratch pad for child process. */
3468 displaced_step_restore (displaced, ecs->ws.value.related_pid);
3469 }
3470
3471 /* Since the vfork/fork syscall instruction was executed in the scratchpad,
3472 the child's PC is also within the scratchpad. Set the child's PC
3473 to the parent's PC value, which has already been fixed up.
3474 FIXME: we use the parent's aspace here, although we're touching
3475 the child, because the child hasn't been added to the inferior
3476 list yet at this point. */
3477
3478 child_regcache
3479 = get_thread_arch_aspace_regcache (ecs->ws.value.related_pid,
3480 gdbarch,
3481 parent_inf->aspace);
3482 /* Read PC value of parent process. */
3483 parent_pc = regcache_read_pc (regcache);
3484
3485 if (debug_displaced)
3486 fprintf_unfiltered (gdb_stdlog,
3487 "displaced: write child pc from %s to %s\n",
3488 paddress (gdbarch,
3489 regcache_read_pc (child_regcache)),
3490 paddress (gdbarch, parent_pc));
3491
3492 regcache_write_pc (child_regcache, parent_pc);
3493 }
3494 }
3495
5a2901d9 3496 if (!ptid_equal (ecs->ptid, inferior_ptid))
c3a01a22 3497 context_switch (ecs->ptid);
5a2901d9 3498
b242c3c2
PA
3499 /* Immediately detach breakpoints from the child before there's
3500 any chance of letting the user delete breakpoints from the
3501 breakpoint lists. If we don't do this early, it's easy to
3502 leave left over traps in the child, vis: "break foo; catch
3503 fork; c; <fork>; del; c; <child calls foo>". We only follow
3504 the fork on the last `continue', and by that time the
3505 breakpoint at "foo" is long gone from the breakpoint table.
3506 If we vforked, then we don't need to unpatch here, since both
3507 parent and child are sharing the same memory pages; we'll
3508 need to unpatch at follow/detach time instead to be certain
3509 that new breakpoints added between catchpoint hit time and
3510 vfork follow are detached. */
3511 if (ecs->ws.kind != TARGET_WAITKIND_VFORKED)
3512 {
b242c3c2
PA
3513 /* This won't actually modify the breakpoint list, but will
3514 physically remove the breakpoints from the child. */
d80ee84f 3515 detach_breakpoints (ecs->ws.value.related_pid);
b242c3c2
PA
3516 }
3517
d03285ec
UW
3518 if (singlestep_breakpoints_inserted_p)
3519 {
1777feb0 3520 /* Pull the single step breakpoints out of the target. */
d03285ec
UW
3521 remove_single_step_breakpoints ();
3522 singlestep_breakpoints_inserted_p = 0;
3523 }
3524
e58b0e63
PA
3525 /* In case the event is caught by a catchpoint, remember that
3526 the event is to be followed at the next resume of the thread,
3527 and not immediately. */
3528 ecs->event_thread->pending_follow = ecs->ws;
3529
fb14de7b 3530 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
675bf4cb 3531
16c381f0 3532 ecs->event_thread->control.stop_bpstat
6c95b8df 3533 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
09ac7c10 3534 stop_pc, ecs->ptid, &ecs->ws);
675bf4cb 3535
67822962
PA
3536 /* Note that we're interested in knowing the bpstat actually
3537 causes a stop, not just if it may explain the signal.
3538 Software watchpoints, for example, always appear in the
3539 bpstat. */
16c381f0
JK
3540 ecs->random_signal
3541 = !bpstat_causes_stop (ecs->event_thread->control.stop_bpstat);
04e68871
DJ
3542
3543 /* If no catchpoint triggered for this, then keep going. */
3544 if (ecs->random_signal)
3545 {
6c95b8df
PA
3546 ptid_t parent;
3547 ptid_t child;
e58b0e63 3548 int should_resume;
3e43a32a
MS
3549 int follow_child
3550 = (follow_fork_mode_string == follow_fork_mode_child);
e58b0e63 3551
a493e3e2 3552 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
e58b0e63
PA
3553
3554 should_resume = follow_fork ();
3555
6c95b8df
PA
3556 parent = ecs->ptid;
3557 child = ecs->ws.value.related_pid;
3558
3559 /* In non-stop mode, also resume the other branch. */
3560 if (non_stop && !detach_fork)
3561 {
3562 if (follow_child)
3563 switch_to_thread (parent);
3564 else
3565 switch_to_thread (child);
3566
3567 ecs->event_thread = inferior_thread ();
3568 ecs->ptid = inferior_ptid;
3569 keep_going (ecs);
3570 }
3571
3572 if (follow_child)
3573 switch_to_thread (child);
3574 else
3575 switch_to_thread (parent);
3576
e58b0e63
PA
3577 ecs->event_thread = inferior_thread ();
3578 ecs->ptid = inferior_ptid;
3579
3580 if (should_resume)
3581 keep_going (ecs);
3582 else
3583 stop_stepping (ecs);
04e68871
DJ
3584 return;
3585 }
a493e3e2 3586 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_TRAP;
488f131b
JB
3587 goto process_event_stop_test;
3588
6c95b8df
PA
3589 case TARGET_WAITKIND_VFORK_DONE:
3590 /* Done with the shared memory region. Re-insert breakpoints in
3591 the parent, and keep going. */
3592
3593 if (debug_infrun)
3e43a32a
MS
3594 fprintf_unfiltered (gdb_stdlog,
3595 "infrun: TARGET_WAITKIND_VFORK_DONE\n");
6c95b8df
PA
3596
3597 if (!ptid_equal (ecs->ptid, inferior_ptid))
3598 context_switch (ecs->ptid);
3599
3600 current_inferior ()->waiting_for_vfork_done = 0;
56710373 3601 current_inferior ()->pspace->breakpoints_not_allowed = 0;
6c95b8df
PA
3602 /* This also takes care of reinserting breakpoints in the
3603 previously locked inferior. */
3604 keep_going (ecs);
3605 return;
3606
488f131b 3607 case TARGET_WAITKIND_EXECD:
527159b7 3608 if (debug_infrun)
fc5261f2 3609 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXECD\n");
488f131b 3610
5a2901d9 3611 if (!ptid_equal (ecs->ptid, inferior_ptid))
c3a01a22 3612 context_switch (ecs->ptid);
5a2901d9 3613
d03285ec
UW
3614 singlestep_breakpoints_inserted_p = 0;
3615 cancel_single_step_breakpoints ();
3616
fb14de7b 3617 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
795e548f 3618
6c95b8df
PA
3619 /* Do whatever is necessary to the parent branch of the vfork. */
3620 handle_vfork_child_exec_or_exit (1);
3621
795e548f
PA
3622 /* This causes the eventpoints and symbol table to be reset.
3623 Must do this now, before trying to determine whether to
3624 stop. */
71b43ef8 3625 follow_exec (inferior_ptid, ecs->ws.value.execd_pathname);
795e548f 3626
16c381f0 3627 ecs->event_thread->control.stop_bpstat
6c95b8df 3628 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
09ac7c10 3629 stop_pc, ecs->ptid, &ecs->ws);
16c381f0
JK
3630 ecs->random_signal
3631 = !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat);
795e548f 3632
71b43ef8
PA
3633 /* Note that this may be referenced from inside
3634 bpstat_stop_status above, through inferior_has_execd. */
3635 xfree (ecs->ws.value.execd_pathname);
3636 ecs->ws.value.execd_pathname = NULL;
3637
04e68871
DJ
3638 /* If no catchpoint triggered for this, then keep going. */
3639 if (ecs->random_signal)
3640 {
a493e3e2 3641 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
04e68871
DJ
3642 keep_going (ecs);
3643 return;
3644 }
a493e3e2 3645 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_TRAP;
488f131b
JB
3646 goto process_event_stop_test;
3647
b4dc5ffa
MK
3648 /* Be careful not to try to gather much state about a thread
3649 that's in a syscall. It's frequently a losing proposition. */
488f131b 3650 case TARGET_WAITKIND_SYSCALL_ENTRY:
527159b7 3651 if (debug_infrun)
3e43a32a
MS
3652 fprintf_unfiltered (gdb_stdlog,
3653 "infrun: TARGET_WAITKIND_SYSCALL_ENTRY\n");
1777feb0 3654 /* Getting the current syscall number. */
ca2163eb 3655 if (handle_syscall_event (ecs) != 0)
a96d9b2e
SDJ
3656 return;
3657 goto process_event_stop_test;
c906108c 3658
488f131b
JB
3659 /* Before examining the threads further, step this thread to
3660 get it entirely out of the syscall. (We get notice of the
3661 event when the thread is just on the verge of exiting a
3662 syscall. Stepping one instruction seems to get it back
b4dc5ffa 3663 into user code.) */
488f131b 3664 case TARGET_WAITKIND_SYSCALL_RETURN:
527159b7 3665 if (debug_infrun)
3e43a32a
MS
3666 fprintf_unfiltered (gdb_stdlog,
3667 "infrun: TARGET_WAITKIND_SYSCALL_RETURN\n");
ca2163eb 3668 if (handle_syscall_event (ecs) != 0)
a96d9b2e
SDJ
3669 return;
3670 goto process_event_stop_test;
c906108c 3671
488f131b 3672 case TARGET_WAITKIND_STOPPED:
527159b7 3673 if (debug_infrun)
8a9de0e4 3674 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_STOPPED\n");
16c381f0 3675 ecs->event_thread->suspend.stop_signal = ecs->ws.value.sig;
488f131b 3676 break;
c906108c 3677
b2175913 3678 case TARGET_WAITKIND_NO_HISTORY:
4b4e080e
PA
3679 if (debug_infrun)
3680 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_NO_HISTORY\n");
b2175913 3681 /* Reverse execution: target ran out of history info. */
eab402df
PA
3682
3683 /* Pull the single step breakpoints out of the target. */
3684 if (singlestep_breakpoints_inserted_p)
3685 {
3686 if (!ptid_equal (ecs->ptid, inferior_ptid))
3687 context_switch (ecs->ptid);
3688 remove_single_step_breakpoints ();
3689 singlestep_breakpoints_inserted_p = 0;
3690 }
fb14de7b 3691 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
33d62d64 3692 print_no_history_reason ();
b2175913
MS
3693 stop_stepping (ecs);
3694 return;
488f131b 3695 }
c906108c 3696
2020b7ab 3697 if (ecs->ws.kind == TARGET_WAITKIND_STOPPED)
252fbfc8
PA
3698 {
3699 /* Do we need to clean up the state of a thread that has
3700 completed a displaced single-step? (Doing so usually affects
3701 the PC, so do it here, before we set stop_pc.) */
16c381f0
JK
3702 displaced_step_fixup (ecs->ptid,
3703 ecs->event_thread->suspend.stop_signal);
252fbfc8
PA
3704
3705 /* If we either finished a single-step or hit a breakpoint, but
3706 the user wanted this thread to be stopped, pretend we got a
3707 SIG0 (generic unsignaled stop). */
3708
3709 if (ecs->event_thread->stop_requested
a493e3e2
PA
3710 && ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
3711 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
252fbfc8 3712 }
237fc4c9 3713
515630c5 3714 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
488f131b 3715
527159b7 3716 if (debug_infrun)
237fc4c9 3717 {
5af949e3
UW
3718 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3719 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7f82dfc7
JK
3720 struct cleanup *old_chain = save_inferior_ptid ();
3721
3722 inferior_ptid = ecs->ptid;
5af949e3
UW
3723
3724 fprintf_unfiltered (gdb_stdlog, "infrun: stop_pc = %s\n",
3725 paddress (gdbarch, stop_pc));
d92524f1 3726 if (target_stopped_by_watchpoint ())
237fc4c9
PA
3727 {
3728 CORE_ADDR addr;
abbb1732 3729
237fc4c9
PA
3730 fprintf_unfiltered (gdb_stdlog, "infrun: stopped by watchpoint\n");
3731
3732 if (target_stopped_data_address (&current_target, &addr))
3733 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
3734 "infrun: stopped data address = %s\n",
3735 paddress (gdbarch, addr));
237fc4c9
PA
3736 else
3737 fprintf_unfiltered (gdb_stdlog,
3738 "infrun: (no data address available)\n");
3739 }
7f82dfc7
JK
3740
3741 do_cleanups (old_chain);
237fc4c9 3742 }
527159b7 3743
9f976b41
DJ
3744 if (stepping_past_singlestep_breakpoint)
3745 {
1c0fdd0e 3746 gdb_assert (singlestep_breakpoints_inserted_p);
9f976b41
DJ
3747 gdb_assert (ptid_equal (singlestep_ptid, ecs->ptid));
3748 gdb_assert (!ptid_equal (singlestep_ptid, saved_singlestep_ptid));
3749
3750 stepping_past_singlestep_breakpoint = 0;
3751
3752 /* We've either finished single-stepping past the single-step
8fb3e588
AC
3753 breakpoint, or stopped for some other reason. It would be nice if
3754 we could tell, but we can't reliably. */
a493e3e2 3755 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
8fb3e588 3756 {
527159b7 3757 if (debug_infrun)
3e43a32a
MS
3758 fprintf_unfiltered (gdb_stdlog,
3759 "infrun: stepping_past_"
3760 "singlestep_breakpoint\n");
9f976b41 3761 /* Pull the single step breakpoints out of the target. */
8b3ee56d
PA
3762 if (!ptid_equal (ecs->ptid, inferior_ptid))
3763 context_switch (ecs->ptid);
e0cd558a 3764 remove_single_step_breakpoints ();
9f976b41
DJ
3765 singlestep_breakpoints_inserted_p = 0;
3766
3767 ecs->random_signal = 0;
16c381f0 3768 ecs->event_thread->control.trap_expected = 0;
9f976b41 3769
0d1e5fa7 3770 context_switch (saved_singlestep_ptid);
9a4105ab 3771 if (deprecated_context_hook)
de9f1b68 3772 deprecated_context_hook (pid_to_thread_id (saved_singlestep_ptid));
9f976b41 3773
a493e3e2 3774 resume (1, GDB_SIGNAL_0);
9f976b41
DJ
3775 prepare_to_wait (ecs);
3776 return;
3777 }
3778 }
3779
ca67fcb8 3780 if (!ptid_equal (deferred_step_ptid, null_ptid))
6a6b96b9 3781 {
94cc34af
PA
3782 /* In non-stop mode, there's never a deferred_step_ptid set. */
3783 gdb_assert (!non_stop);
3784
6a6b96b9
UW
3785 /* If we stopped for some other reason than single-stepping, ignore
3786 the fact that we were supposed to switch back. */
a493e3e2 3787 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
6a6b96b9
UW
3788 {
3789 if (debug_infrun)
3790 fprintf_unfiltered (gdb_stdlog,
ca67fcb8 3791 "infrun: handling deferred step\n");
6a6b96b9
UW
3792
3793 /* Pull the single step breakpoints out of the target. */
3794 if (singlestep_breakpoints_inserted_p)
3795 {
8b3ee56d
PA
3796 if (!ptid_equal (ecs->ptid, inferior_ptid))
3797 context_switch (ecs->ptid);
6a6b96b9
UW
3798 remove_single_step_breakpoints ();
3799 singlestep_breakpoints_inserted_p = 0;
3800 }
3801
cd3da28e
PA
3802 ecs->event_thread->control.trap_expected = 0;
3803
d25f45d9 3804 context_switch (deferred_step_ptid);
ca67fcb8 3805 deferred_step_ptid = null_ptid;
6a6b96b9
UW
3806 /* Suppress spurious "Switching to ..." message. */
3807 previous_inferior_ptid = inferior_ptid;
3808
a493e3e2 3809 resume (1, GDB_SIGNAL_0);
6a6b96b9
UW
3810 prepare_to_wait (ecs);
3811 return;
3812 }
ca67fcb8
VP
3813
3814 deferred_step_ptid = null_ptid;
6a6b96b9
UW
3815 }
3816
488f131b
JB
3817 /* See if a thread hit a thread-specific breakpoint that was meant for
3818 another thread. If so, then step that thread past the breakpoint,
3819 and continue it. */
3820
a493e3e2 3821 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
488f131b 3822 {
9f976b41 3823 int thread_hop_needed = 0;
cf00dfa7
VP
3824 struct address_space *aspace =
3825 get_regcache_aspace (get_thread_regcache (ecs->ptid));
9f976b41 3826
f8d40ec8 3827 /* Check if a regular breakpoint has been hit before checking
1777feb0 3828 for a potential single step breakpoint. Otherwise, GDB will
f8d40ec8 3829 not see this breakpoint hit when stepping onto breakpoints. */
6c95b8df 3830 if (regular_breakpoint_inserted_here_p (aspace, stop_pc))
488f131b 3831 {
c5aa993b 3832 ecs->random_signal = 0;
6c95b8df 3833 if (!breakpoint_thread_match (aspace, stop_pc, ecs->ptid))
9f976b41
DJ
3834 thread_hop_needed = 1;
3835 }
1c0fdd0e 3836 else if (singlestep_breakpoints_inserted_p)
9f976b41 3837 {
fd48f117
DJ
3838 /* We have not context switched yet, so this should be true
3839 no matter which thread hit the singlestep breakpoint. */
3840 gdb_assert (ptid_equal (inferior_ptid, singlestep_ptid));
3841 if (debug_infrun)
3842 fprintf_unfiltered (gdb_stdlog, "infrun: software single step "
3843 "trap for %s\n",
3844 target_pid_to_str (ecs->ptid));
3845
9f976b41
DJ
3846 ecs->random_signal = 0;
3847 /* The call to in_thread_list is necessary because PTIDs sometimes
3848 change when we go from single-threaded to multi-threaded. If
3849 the singlestep_ptid is still in the list, assume that it is
3850 really different from ecs->ptid. */
3851 if (!ptid_equal (singlestep_ptid, ecs->ptid)
3852 && in_thread_list (singlestep_ptid))
3853 {
fd48f117
DJ
3854 /* If the PC of the thread we were trying to single-step
3855 has changed, discard this event (which we were going
3856 to ignore anyway), and pretend we saw that thread
3857 trap. This prevents us continuously moving the
3858 single-step breakpoint forward, one instruction at a
3859 time. If the PC has changed, then the thread we were
3860 trying to single-step has trapped or been signalled,
3861 but the event has not been reported to GDB yet.
3862
3863 There might be some cases where this loses signal
3864 information, if a signal has arrived at exactly the
3865 same time that the PC changed, but this is the best
3866 we can do with the information available. Perhaps we
3867 should arrange to report all events for all threads
3868 when they stop, or to re-poll the remote looking for
3869 this particular thread (i.e. temporarily enable
3870 schedlock). */
515630c5
UW
3871
3872 CORE_ADDR new_singlestep_pc
3873 = regcache_read_pc (get_thread_regcache (singlestep_ptid));
3874
3875 if (new_singlestep_pc != singlestep_pc)
fd48f117 3876 {
2ea28649 3877 enum gdb_signal stop_signal;
2020b7ab 3878
fd48f117
DJ
3879 if (debug_infrun)
3880 fprintf_unfiltered (gdb_stdlog, "infrun: unexpected thread,"
3881 " but expected thread advanced also\n");
3882
3883 /* The current context still belongs to
3884 singlestep_ptid. Don't swap here, since that's
3885 the context we want to use. Just fudge our
3886 state and continue. */
16c381f0 3887 stop_signal = ecs->event_thread->suspend.stop_signal;
a493e3e2 3888 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
fd48f117 3889 ecs->ptid = singlestep_ptid;
e09875d4 3890 ecs->event_thread = find_thread_ptid (ecs->ptid);
16c381f0 3891 ecs->event_thread->suspend.stop_signal = stop_signal;
515630c5 3892 stop_pc = new_singlestep_pc;
fd48f117
DJ
3893 }
3894 else
3895 {
3896 if (debug_infrun)
3897 fprintf_unfiltered (gdb_stdlog,
3898 "infrun: unexpected thread\n");
3899
3900 thread_hop_needed = 1;
3901 stepping_past_singlestep_breakpoint = 1;
3902 saved_singlestep_ptid = singlestep_ptid;
3903 }
9f976b41
DJ
3904 }
3905 }
3906
3907 if (thread_hop_needed)
8fb3e588 3908 {
9f5a595d 3909 struct regcache *thread_regcache;
237fc4c9 3910 int remove_status = 0;
8fb3e588 3911
527159b7 3912 if (debug_infrun)
8a9de0e4 3913 fprintf_unfiltered (gdb_stdlog, "infrun: thread_hop_needed\n");
527159b7 3914
b3444185
PA
3915 /* Switch context before touching inferior memory, the
3916 previous thread may have exited. */
3917 if (!ptid_equal (inferior_ptid, ecs->ptid))
3918 context_switch (ecs->ptid);
3919
8fb3e588 3920 /* Saw a breakpoint, but it was hit by the wrong thread.
1777feb0 3921 Just continue. */
8fb3e588 3922
1c0fdd0e 3923 if (singlestep_breakpoints_inserted_p)
488f131b 3924 {
1777feb0 3925 /* Pull the single step breakpoints out of the target. */
e0cd558a 3926 remove_single_step_breakpoints ();
8fb3e588
AC
3927 singlestep_breakpoints_inserted_p = 0;
3928 }
3929
237fc4c9
PA
3930 /* If the arch can displace step, don't remove the
3931 breakpoints. */
9f5a595d
UW
3932 thread_regcache = get_thread_regcache (ecs->ptid);
3933 if (!use_displaced_stepping (get_regcache_arch (thread_regcache)))
237fc4c9
PA
3934 remove_status = remove_breakpoints ();
3935
8fb3e588
AC
3936 /* Did we fail to remove breakpoints? If so, try
3937 to set the PC past the bp. (There's at least
3938 one situation in which we can fail to remove
3939 the bp's: On HP-UX's that use ttrace, we can't
3940 change the address space of a vforking child
3941 process until the child exits (well, okay, not
1777feb0 3942 then either :-) or execs. */
8fb3e588 3943 if (remove_status != 0)
9d9cd7ac 3944 error (_("Cannot step over breakpoint hit in wrong thread"));
8fb3e588
AC
3945 else
3946 { /* Single step */
94cc34af
PA
3947 if (!non_stop)
3948 {
3949 /* Only need to require the next event from this
3950 thread in all-stop mode. */
3951 waiton_ptid = ecs->ptid;
3952 infwait_state = infwait_thread_hop_state;
3953 }
8fb3e588 3954
4e1c45ea 3955 ecs->event_thread->stepping_over_breakpoint = 1;
8fb3e588 3956 keep_going (ecs);
8fb3e588
AC
3957 return;
3958 }
488f131b 3959 }
1c0fdd0e 3960 else if (singlestep_breakpoints_inserted_p)
8fb3e588 3961 {
8fb3e588
AC
3962 ecs->random_signal = 0;
3963 }
488f131b
JB
3964 }
3965 else
3966 ecs->random_signal = 1;
c906108c 3967
488f131b 3968 /* See if something interesting happened to the non-current thread. If
b40c7d58
DJ
3969 so, then switch to that thread. */
3970 if (!ptid_equal (ecs->ptid, inferior_ptid))
488f131b 3971 {
527159b7 3972 if (debug_infrun)
8a9de0e4 3973 fprintf_unfiltered (gdb_stdlog, "infrun: context switch\n");
527159b7 3974
0d1e5fa7 3975 context_switch (ecs->ptid);
c5aa993b 3976
9a4105ab
AC
3977 if (deprecated_context_hook)
3978 deprecated_context_hook (pid_to_thread_id (ecs->ptid));
488f131b 3979 }
c906108c 3980
568d6575
UW
3981 /* At this point, get hold of the now-current thread's frame. */
3982 frame = get_current_frame ();
3983 gdbarch = get_frame_arch (frame);
3984
1c0fdd0e 3985 if (singlestep_breakpoints_inserted_p)
488f131b 3986 {
1777feb0 3987 /* Pull the single step breakpoints out of the target. */
e0cd558a 3988 remove_single_step_breakpoints ();
488f131b
JB
3989 singlestep_breakpoints_inserted_p = 0;
3990 }
c906108c 3991
d983da9c
DJ
3992 if (stepped_after_stopped_by_watchpoint)
3993 stopped_by_watchpoint = 0;
3994 else
3995 stopped_by_watchpoint = watchpoints_triggered (&ecs->ws);
3996
3997 /* If necessary, step over this watchpoint. We'll be back to display
3998 it in a moment. */
3999 if (stopped_by_watchpoint
d92524f1 4000 && (target_have_steppable_watchpoint
568d6575 4001 || gdbarch_have_nonsteppable_watchpoint (gdbarch)))
488f131b 4002 {
488f131b
JB
4003 /* At this point, we are stopped at an instruction which has
4004 attempted to write to a piece of memory under control of
4005 a watchpoint. The instruction hasn't actually executed
4006 yet. If we were to evaluate the watchpoint expression
4007 now, we would get the old value, and therefore no change
4008 would seem to have occurred.
4009
4010 In order to make watchpoints work `right', we really need
4011 to complete the memory write, and then evaluate the
d983da9c
DJ
4012 watchpoint expression. We do this by single-stepping the
4013 target.
4014
4015 It may not be necessary to disable the watchpoint to stop over
4016 it. For example, the PA can (with some kernel cooperation)
4017 single step over a watchpoint without disabling the watchpoint.
4018
4019 It is far more common to need to disable a watchpoint to step
4020 the inferior over it. If we have non-steppable watchpoints,
4021 we must disable the current watchpoint; it's simplest to
4022 disable all watchpoints and breakpoints. */
2facfe5c
DD
4023 int hw_step = 1;
4024
d92524f1 4025 if (!target_have_steppable_watchpoint)
2455069d
UW
4026 {
4027 remove_breakpoints ();
4028 /* See comment in resume why we need to stop bypassing signals
4029 while breakpoints have been removed. */
4030 target_pass_signals (0, NULL);
4031 }
2facfe5c 4032 /* Single step */
568d6575 4033 hw_step = maybe_software_singlestep (gdbarch, stop_pc);
a493e3e2 4034 target_resume (ecs->ptid, hw_step, GDB_SIGNAL_0);
0d1e5fa7 4035 waiton_ptid = ecs->ptid;
d92524f1 4036 if (target_have_steppable_watchpoint)
0d1e5fa7 4037 infwait_state = infwait_step_watch_state;
d983da9c 4038 else
0d1e5fa7 4039 infwait_state = infwait_nonstep_watch_state;
488f131b
JB
4040 prepare_to_wait (ecs);
4041 return;
4042 }
4043
7e324e48 4044 clear_stop_func (ecs);
4e1c45ea 4045 ecs->event_thread->stepping_over_breakpoint = 0;
16c381f0
JK
4046 bpstat_clear (&ecs->event_thread->control.stop_bpstat);
4047 ecs->event_thread->control.stop_step = 0;
488f131b
JB
4048 stop_print_frame = 1;
4049 ecs->random_signal = 0;
4050 stopped_by_random_signal = 0;
488f131b 4051
edb3359d
DJ
4052 /* Hide inlined functions starting here, unless we just performed stepi or
4053 nexti. After stepi and nexti, always show the innermost frame (not any
4054 inline function call sites). */
16c381f0 4055 if (ecs->event_thread->control.step_range_end != 1)
0574c78f
GB
4056 {
4057 struct address_space *aspace =
4058 get_regcache_aspace (get_thread_regcache (ecs->ptid));
4059
4060 /* skip_inline_frames is expensive, so we avoid it if we can
4061 determine that the address is one where functions cannot have
4062 been inlined. This improves performance with inferiors that
4063 load a lot of shared libraries, because the solib event
4064 breakpoint is defined as the address of a function (i.e. not
4065 inline). Note that we have to check the previous PC as well
4066 as the current one to catch cases when we have just
4067 single-stepped off a breakpoint prior to reinstating it.
4068 Note that we're assuming that the code we single-step to is
4069 not inline, but that's not definitive: there's nothing
4070 preventing the event breakpoint function from containing
4071 inlined code, and the single-step ending up there. If the
4072 user had set a breakpoint on that inlined code, the missing
4073 skip_inline_frames call would break things. Fortunately
4074 that's an extremely unlikely scenario. */
09ac7c10 4075 if (!pc_at_non_inline_function (aspace, stop_pc, &ecs->ws)
a210c238
MR
4076 && !(ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4077 && ecs->event_thread->control.trap_expected
4078 && pc_at_non_inline_function (aspace,
4079 ecs->event_thread->prev_pc,
09ac7c10 4080 &ecs->ws)))
1c5a993e
MR
4081 {
4082 skip_inline_frames (ecs->ptid);
4083
4084 /* Re-fetch current thread's frame in case that invalidated
4085 the frame cache. */
4086 frame = get_current_frame ();
4087 gdbarch = get_frame_arch (frame);
4088 }
0574c78f 4089 }
edb3359d 4090
a493e3e2 4091 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
16c381f0 4092 && ecs->event_thread->control.trap_expected
568d6575 4093 && gdbarch_single_step_through_delay_p (gdbarch)
4e1c45ea 4094 && currently_stepping (ecs->event_thread))
3352ef37 4095 {
b50d7442 4096 /* We're trying to step off a breakpoint. Turns out that we're
3352ef37 4097 also on an instruction that needs to be stepped multiple
1777feb0 4098 times before it's been fully executing. E.g., architectures
3352ef37
AC
4099 with a delay slot. It needs to be stepped twice, once for
4100 the instruction and once for the delay slot. */
4101 int step_through_delay
568d6575 4102 = gdbarch_single_step_through_delay (gdbarch, frame);
abbb1732 4103
527159b7 4104 if (debug_infrun && step_through_delay)
8a9de0e4 4105 fprintf_unfiltered (gdb_stdlog, "infrun: step through delay\n");
16c381f0
JK
4106 if (ecs->event_thread->control.step_range_end == 0
4107 && step_through_delay)
3352ef37
AC
4108 {
4109 /* The user issued a continue when stopped at a breakpoint.
4110 Set up for another trap and get out of here. */
4e1c45ea 4111 ecs->event_thread->stepping_over_breakpoint = 1;
3352ef37
AC
4112 keep_going (ecs);
4113 return;
4114 }
4115 else if (step_through_delay)
4116 {
4117 /* The user issued a step when stopped at a breakpoint.
4118 Maybe we should stop, maybe we should not - the delay
4119 slot *might* correspond to a line of source. In any
ca67fcb8
VP
4120 case, don't decide that here, just set
4121 ecs->stepping_over_breakpoint, making sure we
4122 single-step again before breakpoints are re-inserted. */
4e1c45ea 4123 ecs->event_thread->stepping_over_breakpoint = 1;
3352ef37
AC
4124 }
4125 }
4126
488f131b
JB
4127 /* Look at the cause of the stop, and decide what to do.
4128 The alternatives are:
0d1e5fa7
PA
4129 1) stop_stepping and return; to really stop and return to the debugger,
4130 2) keep_going and return to start up again
4e1c45ea 4131 (set ecs->event_thread->stepping_over_breakpoint to 1 to single step once)
488f131b
JB
4132 3) set ecs->random_signal to 1, and the decision between 1 and 2
4133 will be made according to the signal handling tables. */
4134
a493e3e2 4135 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
b0f4b84b
DJ
4136 || stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_NO_SIGSTOP
4137 || stop_soon == STOP_QUIETLY_REMOTE)
488f131b 4138 {
a493e3e2 4139 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
16c381f0 4140 && stop_after_trap)
488f131b 4141 {
527159b7 4142 if (debug_infrun)
8a9de0e4 4143 fprintf_unfiltered (gdb_stdlog, "infrun: stopped\n");
488f131b
JB
4144 stop_print_frame = 0;
4145 stop_stepping (ecs);
4146 return;
4147 }
c54cfec8
EZ
4148
4149 /* This is originated from start_remote(), start_inferior() and
4150 shared libraries hook functions. */
b0f4b84b 4151 if (stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_REMOTE)
488f131b 4152 {
527159b7 4153 if (debug_infrun)
8a9de0e4 4154 fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
488f131b
JB
4155 stop_stepping (ecs);
4156 return;
4157 }
4158
c54cfec8 4159 /* This originates from attach_command(). We need to overwrite
a0d21d28
PA
4160 the stop_signal here, because some kernels don't ignore a
4161 SIGSTOP in a subsequent ptrace(PTRACE_CONT,SIGSTOP) call.
4162 See more comments in inferior.h. On the other hand, if we
a0ef4274 4163 get a non-SIGSTOP, report it to the user - assume the backend
a0d21d28
PA
4164 will handle the SIGSTOP if it should show up later.
4165
4166 Also consider that the attach is complete when we see a
4167 SIGTRAP. Some systems (e.g. Windows), and stubs supporting
4168 target extended-remote report it instead of a SIGSTOP
4169 (e.g. gdbserver). We already rely on SIGTRAP being our
e0ba6746
PA
4170 signal, so this is no exception.
4171
4172 Also consider that the attach is complete when we see a
a493e3e2 4173 GDB_SIGNAL_0. In non-stop mode, GDB will explicitly tell
e0ba6746
PA
4174 the target to stop all threads of the inferior, in case the
4175 low level attach operation doesn't stop them implicitly. If
4176 they weren't stopped implicitly, then the stub will report a
a493e3e2 4177 GDB_SIGNAL_0, meaning: stopped for no particular reason
e0ba6746 4178 other than GDB's request. */
a0ef4274 4179 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
a493e3e2
PA
4180 && (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_STOP
4181 || ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4182 || ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_0))
c54cfec8
EZ
4183 {
4184 stop_stepping (ecs);
a493e3e2 4185 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
c54cfec8
EZ
4186 return;
4187 }
4188
09ac7c10
TT
4189 /* See if there is a breakpoint/watchpoint/catchpoint/etc. that
4190 handles this event. */
16c381f0 4191 ecs->event_thread->control.stop_bpstat
6c95b8df 4192 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
09ac7c10 4193 stop_pc, ecs->ptid, &ecs->ws);
6c95b8df 4194
fba57f8f
VP
4195 /* Following in case break condition called a
4196 function. */
4197 stop_print_frame = 1;
488f131b 4198
db82e815
PA
4199 /* This is where we handle "moribund" watchpoints. Unlike
4200 software breakpoints traps, hardware watchpoint traps are
4201 always distinguishable from random traps. If no high-level
4202 watchpoint is associated with the reported stop data address
4203 anymore, then the bpstat does not explain the signal ---
4204 simply make sure to ignore it if `stopped_by_watchpoint' is
4205 set. */
4206
4207 if (debug_infrun
a493e3e2 4208 && ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
16c381f0 4209 && !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat)
db82e815 4210 && stopped_by_watchpoint)
3e43a32a
MS
4211 fprintf_unfiltered (gdb_stdlog,
4212 "infrun: no user watchpoint explains "
4213 "watchpoint SIGTRAP, ignoring\n");
db82e815 4214
73dd234f 4215 /* NOTE: cagney/2003-03-29: These two checks for a random signal
8fb3e588
AC
4216 at one stage in the past included checks for an inferior
4217 function call's call dummy's return breakpoint. The original
4218 comment, that went with the test, read:
73dd234f 4219
8fb3e588
AC
4220 ``End of a stack dummy. Some systems (e.g. Sony news) give
4221 another signal besides SIGTRAP, so check here as well as
4222 above.''
73dd234f 4223
8002d778 4224 If someone ever tries to get call dummys on a
73dd234f 4225 non-executable stack to work (where the target would stop
03cebad2
MK
4226 with something like a SIGSEGV), then those tests might need
4227 to be re-instated. Given, however, that the tests were only
73dd234f 4228 enabled when momentary breakpoints were not being used, I
03cebad2
MK
4229 suspect that it won't be the case.
4230
8fb3e588
AC
4231 NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
4232 be necessary for call dummies on a non-executable stack on
4233 SPARC. */
73dd234f 4234
a493e3e2 4235 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
488f131b 4236 ecs->random_signal
16c381f0 4237 = !(bpstat_explains_signal (ecs->event_thread->control.stop_bpstat)
db82e815 4238 || stopped_by_watchpoint
16c381f0
JK
4239 || ecs->event_thread->control.trap_expected
4240 || (ecs->event_thread->control.step_range_end
8358c15c
JK
4241 && (ecs->event_thread->control.step_resume_breakpoint
4242 == NULL)));
488f131b
JB
4243 else
4244 {
16c381f0
JK
4245 ecs->random_signal = !bpstat_explains_signal
4246 (ecs->event_thread->control.stop_bpstat);
488f131b 4247 if (!ecs->random_signal)
a493e3e2 4248 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_TRAP;
488f131b
JB
4249 }
4250 }
4251
4252 /* When we reach this point, we've pretty much decided
4253 that the reason for stopping must've been a random
1777feb0 4254 (unexpected) signal. */
488f131b
JB
4255
4256 else
4257 ecs->random_signal = 1;
488f131b 4258
04e68871 4259process_event_stop_test:
568d6575
UW
4260
4261 /* Re-fetch current thread's frame in case we did a
4262 "goto process_event_stop_test" above. */
4263 frame = get_current_frame ();
4264 gdbarch = get_frame_arch (frame);
4265
488f131b
JB
4266 /* For the program's own signals, act according to
4267 the signal handling tables. */
4268
4269 if (ecs->random_signal)
4270 {
4271 /* Signal not for debugging purposes. */
4272 int printed = 0;
24291992 4273 struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
488f131b 4274
527159b7 4275 if (debug_infrun)
2020b7ab 4276 fprintf_unfiltered (gdb_stdlog, "infrun: random signal %d\n",
16c381f0 4277 ecs->event_thread->suspend.stop_signal);
527159b7 4278
488f131b
JB
4279 stopped_by_random_signal = 1;
4280
16c381f0 4281 if (signal_print[ecs->event_thread->suspend.stop_signal])
488f131b
JB
4282 {
4283 printed = 1;
4284 target_terminal_ours_for_output ();
16c381f0
JK
4285 print_signal_received_reason
4286 (ecs->event_thread->suspend.stop_signal);
488f131b 4287 }
252fbfc8
PA
4288 /* Always stop on signals if we're either just gaining control
4289 of the program, or the user explicitly requested this thread
4290 to remain stopped. */
d6b48e9c 4291 if (stop_soon != NO_STOP_QUIETLY
252fbfc8 4292 || ecs->event_thread->stop_requested
24291992 4293 || (!inf->detaching
16c381f0 4294 && signal_stop_state (ecs->event_thread->suspend.stop_signal)))
488f131b
JB
4295 {
4296 stop_stepping (ecs);
4297 return;
4298 }
4299 /* If not going to stop, give terminal back
4300 if we took it away. */
4301 else if (printed)
4302 target_terminal_inferior ();
4303
4304 /* Clear the signal if it should not be passed. */
16c381f0 4305 if (signal_program[ecs->event_thread->suspend.stop_signal] == 0)
a493e3e2 4306 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
488f131b 4307
fb14de7b 4308 if (ecs->event_thread->prev_pc == stop_pc
16c381f0 4309 && ecs->event_thread->control.trap_expected
8358c15c 4310 && ecs->event_thread->control.step_resume_breakpoint == NULL)
68f53502
AC
4311 {
4312 /* We were just starting a new sequence, attempting to
4313 single-step off of a breakpoint and expecting a SIGTRAP.
237fc4c9 4314 Instead this signal arrives. This signal will take us out
68f53502
AC
4315 of the stepping range so GDB needs to remember to, when
4316 the signal handler returns, resume stepping off that
4317 breakpoint. */
4318 /* To simplify things, "continue" is forced to use the same
4319 code paths as single-step - set a breakpoint at the
4320 signal return address and then, once hit, step off that
4321 breakpoint. */
237fc4c9
PA
4322 if (debug_infrun)
4323 fprintf_unfiltered (gdb_stdlog,
4324 "infrun: signal arrived while stepping over "
4325 "breakpoint\n");
d3169d93 4326
2c03e5be 4327 insert_hp_step_resume_breakpoint_at_frame (frame);
4e1c45ea 4328 ecs->event_thread->step_after_step_resume_breakpoint = 1;
2455069d
UW
4329 /* Reset trap_expected to ensure breakpoints are re-inserted. */
4330 ecs->event_thread->control.trap_expected = 0;
9d799f85
AC
4331 keep_going (ecs);
4332 return;
68f53502 4333 }
9d799f85 4334
16c381f0 4335 if (ecs->event_thread->control.step_range_end != 0
a493e3e2 4336 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_0
16c381f0
JK
4337 && (ecs->event_thread->control.step_range_start <= stop_pc
4338 && stop_pc < ecs->event_thread->control.step_range_end)
edb3359d 4339 && frame_id_eq (get_stack_frame_id (frame),
16c381f0 4340 ecs->event_thread->control.step_stack_frame_id)
8358c15c 4341 && ecs->event_thread->control.step_resume_breakpoint == NULL)
d303a6c7
AC
4342 {
4343 /* The inferior is about to take a signal that will take it
4344 out of the single step range. Set a breakpoint at the
4345 current PC (which is presumably where the signal handler
4346 will eventually return) and then allow the inferior to
4347 run free.
4348
4349 Note that this is only needed for a signal delivered
4350 while in the single-step range. Nested signals aren't a
4351 problem as they eventually all return. */
237fc4c9
PA
4352 if (debug_infrun)
4353 fprintf_unfiltered (gdb_stdlog,
4354 "infrun: signal may take us out of "
4355 "single-step range\n");
4356
2c03e5be 4357 insert_hp_step_resume_breakpoint_at_frame (frame);
2455069d
UW
4358 /* Reset trap_expected to ensure breakpoints are re-inserted. */
4359 ecs->event_thread->control.trap_expected = 0;
9d799f85
AC
4360 keep_going (ecs);
4361 return;
d303a6c7 4362 }
9d799f85
AC
4363
4364 /* Note: step_resume_breakpoint may be non-NULL. This occures
4365 when either there's a nested signal, or when there's a
4366 pending signal enabled just as the signal handler returns
4367 (leaving the inferior at the step-resume-breakpoint without
4368 actually executing it). Either way continue until the
4369 breakpoint is really hit. */
488f131b 4370 }
e5ef252a
PA
4371 else
4372 {
4373 /* Handle cases caused by hitting a breakpoint. */
488f131b 4374
e5ef252a
PA
4375 CORE_ADDR jmp_buf_pc;
4376 struct bpstat_what what;
611c83ae 4377
e5ef252a 4378 what = bpstat_what (ecs->event_thread->control.stop_bpstat);
611c83ae 4379
e5ef252a 4380 if (what.call_dummy)
e81a37f7 4381 {
e5ef252a
PA
4382 stop_stack_dummy = what.call_dummy;
4383 }
186c406b 4384
e5ef252a
PA
4385 /* If we hit an internal event that triggers symbol changes, the
4386 current frame will be invalidated within bpstat_what (e.g.,
4387 if we hit an internal solib event). Re-fetch it. */
4388 frame = get_current_frame ();
4389 gdbarch = get_frame_arch (frame);
e2e4d78b 4390
e5ef252a
PA
4391 switch (what.main_action)
4392 {
4393 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
4394 /* If we hit the breakpoint at longjmp while stepping, we
4395 install a momentary breakpoint at the target of the
4396 jmp_buf. */
186c406b 4397
e81a37f7
TT
4398 if (debug_infrun)
4399 fprintf_unfiltered (gdb_stdlog,
e5ef252a 4400 "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME\n");
186c406b 4401
e5ef252a 4402 ecs->event_thread->stepping_over_breakpoint = 1;
611c83ae 4403
e2e4d78b
JK
4404 if (what.is_longjmp)
4405 {
e5ef252a
PA
4406 struct value *arg_value;
4407
4408 /* If we set the longjmp breakpoint via a SystemTap
4409 probe, then use it to extract the arguments. The
4410 destination PC is the third argument to the
4411 probe. */
4412 arg_value = probe_safe_evaluate_at_pc (frame, 2);
4413 if (arg_value)
4414 jmp_buf_pc = value_as_address (arg_value);
4415 else if (!gdbarch_get_longjmp_target_p (gdbarch)
4416 || !gdbarch_get_longjmp_target (gdbarch,
4417 frame, &jmp_buf_pc))
e2e4d78b 4418 {
e5ef252a
PA
4419 if (debug_infrun)
4420 fprintf_unfiltered (gdb_stdlog,
4421 "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME "
4422 "(!gdbarch_get_longjmp_target)\n");
e2e4d78b
JK
4423 keep_going (ecs);
4424 return;
4425 }
e5ef252a
PA
4426
4427 /* Insert a breakpoint at resume address. */
4428 insert_longjmp_resume_breakpoint (gdbarch, jmp_buf_pc);
e2e4d78b 4429 }
e5ef252a
PA
4430 else
4431 check_exception_resume (ecs, frame);
4432 keep_going (ecs);
4433 return;
e2e4d78b 4434
e5ef252a
PA
4435 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
4436 {
4437 struct frame_info *init_frame;
e2e4d78b 4438
e5ef252a 4439 /* There are several cases to consider.
e81a37f7 4440
e5ef252a
PA
4441 1. The initiating frame no longer exists. In this case
4442 we must stop, because the exception or longjmp has gone
4443 too far.
e81a37f7 4444
e5ef252a
PA
4445 2. The initiating frame exists, and is the same as the
4446 current frame. We stop, because the exception or
4447 longjmp has been caught.
c906108c 4448
e5ef252a
PA
4449 3. The initiating frame exists and is different from
4450 the current frame. This means the exception or longjmp
4451 has been caught beneath the initiating frame, so keep
4452 going.
2c03e5be 4453
e5ef252a
PA
4454 4. longjmp breakpoint has been placed just to protect
4455 against stale dummy frames and user is not interested
4456 in stopping around longjmps. */
2c03e5be 4457
e5ef252a
PA
4458 if (debug_infrun)
4459 fprintf_unfiltered (gdb_stdlog,
4460 "infrun: BPSTAT_WHAT_CLEAR_LONGJMP_RESUME\n");
c906108c 4461
e5ef252a
PA
4462 gdb_assert (ecs->event_thread->control.exception_resume_breakpoint
4463 != NULL);
4464 delete_exception_resume_breakpoint (ecs->event_thread);
c5aa993b 4465
e5ef252a
PA
4466 if (what.is_longjmp)
4467 {
4468 check_longjmp_breakpoint_for_call_dummy (ecs->event_thread->num);
c5aa993b 4469
e5ef252a
PA
4470 if (!frame_id_p (ecs->event_thread->initiating_frame))
4471 {
4472 /* Case 4. */
4473 keep_going (ecs);
4474 return;
4475 }
4476 }
c5aa993b 4477
e5ef252a 4478 init_frame = frame_find_by_id (ecs->event_thread->initiating_frame);
c5aa993b 4479
e5ef252a
PA
4480 if (init_frame)
4481 {
4482 struct frame_id current_id
4483 = get_frame_id (get_current_frame ());
4484 if (frame_id_eq (current_id,
4485 ecs->event_thread->initiating_frame))
4486 {
4487 /* Case 2. Fall through. */
4488 }
4489 else
4490 {
4491 /* Case 3. */
4492 keep_going (ecs);
4493 return;
4494 }
4495 }
c5aa993b 4496
e5ef252a
PA
4497 /* For Cases 1 and 2, remove the step-resume breakpoint,
4498 if it exists. */
4499 delete_step_resume_breakpoint (ecs->event_thread);
527159b7 4500
e5ef252a
PA
4501 ecs->event_thread->control.stop_step = 1;
4502 print_end_stepping_range_reason ();
4503 stop_stepping (ecs);
68f53502 4504 }
e5ef252a 4505 return;
488f131b 4506
e5ef252a
PA
4507 case BPSTAT_WHAT_SINGLE:
4508 if (debug_infrun)
4509 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_SINGLE\n");
4510 ecs->event_thread->stepping_over_breakpoint = 1;
4511 /* Still need to check other stuff, at least the case where
4512 we are stepping and step out of the right range. */
4513 break;
4514
4515 case BPSTAT_WHAT_STEP_RESUME:
4516 if (debug_infrun)
4517 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STEP_RESUME\n");
4518
4519 delete_step_resume_breakpoint (ecs->event_thread);
4520 if (ecs->event_thread->control.proceed_to_finish
4521 && execution_direction == EXEC_REVERSE)
4522 {
4523 struct thread_info *tp = ecs->event_thread;
4524
4525 /* We are finishing a function in reverse, and just hit
4526 the step-resume breakpoint at the start address of
4527 the function, and we're almost there -- just need to
4528 back up by one more single-step, which should take us
4529 back to the function call. */
4530 tp->control.step_range_start = tp->control.step_range_end = 1;
4531 keep_going (ecs);
4532 return;
4533 }
4534 fill_in_stop_func (gdbarch, ecs);
4535 if (stop_pc == ecs->stop_func_start
4536 && execution_direction == EXEC_REVERSE)
4537 {
4538 /* We are stepping over a function call in reverse, and
4539 just hit the step-resume breakpoint at the start
4540 address of the function. Go back to single-stepping,
4541 which should take us back to the function call. */
4542 ecs->event_thread->stepping_over_breakpoint = 1;
4543 keep_going (ecs);
4544 return;
4545 }
4546 break;
4547
4548 case BPSTAT_WHAT_STOP_NOISY:
4549 if (debug_infrun)
4550 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_NOISY\n");
4551 stop_print_frame = 1;
4552
4553 /* We are about to nuke the step_resume_breakpointt via the
4554 cleanup chain, so no need to worry about it here. */
4555
4556 stop_stepping (ecs);
4557 return;
4558
4559 case BPSTAT_WHAT_STOP_SILENT:
4560 if (debug_infrun)
4561 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_SILENT\n");
4562 stop_print_frame = 0;
4563
4564 /* We are about to nuke the step_resume_breakpoin via the
4565 cleanup chain, so no need to worry about it here. */
4566
4567 stop_stepping (ecs);
4568 return;
4569
4570 case BPSTAT_WHAT_HP_STEP_RESUME:
4571 if (debug_infrun)
4572 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_HP_STEP_RESUME\n");
4573
4574 delete_step_resume_breakpoint (ecs->event_thread);
4575 if (ecs->event_thread->step_after_step_resume_breakpoint)
4576 {
4577 /* Back when the step-resume breakpoint was inserted, we
4578 were trying to single-step off a breakpoint. Go back
4579 to doing that. */
4580 ecs->event_thread->step_after_step_resume_breakpoint = 0;
4581 ecs->event_thread->stepping_over_breakpoint = 1;
4582 keep_going (ecs);
4583 return;
4584 }
4585 break;
4586
4587 case BPSTAT_WHAT_KEEP_CHECKING:
4588 break;
4589 }
4590 }
c906108c 4591
488f131b
JB
4592 /* We come here if we hit a breakpoint but should not
4593 stop for it. Possibly we also were stepping
4594 and should stop for that. So fall through and
4595 test for stepping. But, if not stepping,
4596 do not stop. */
c906108c 4597
a7212384
UW
4598 /* In all-stop mode, if we're currently stepping but have stopped in
4599 some other thread, we need to switch back to the stepped thread. */
4600 if (!non_stop)
4601 {
4602 struct thread_info *tp;
abbb1732 4603
b3444185 4604 tp = iterate_over_threads (currently_stepping_or_nexting_callback,
a7212384
UW
4605 ecs->event_thread);
4606 if (tp)
4607 {
4608 /* However, if the current thread is blocked on some internal
4609 breakpoint, and we simply need to step over that breakpoint
4610 to get it going again, do that first. */
16c381f0 4611 if ((ecs->event_thread->control.trap_expected
a493e3e2 4612 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_TRAP)
a7212384
UW
4613 || ecs->event_thread->stepping_over_breakpoint)
4614 {
4615 keep_going (ecs);
4616 return;
4617 }
4618
66852e9c
PA
4619 /* If the stepping thread exited, then don't try to switch
4620 back and resume it, which could fail in several different
4621 ways depending on the target. Instead, just keep going.
4622
4623 We can find a stepping dead thread in the thread list in
4624 two cases:
4625
4626 - The target supports thread exit events, and when the
4627 target tries to delete the thread from the thread list,
4628 inferior_ptid pointed at the exiting thread. In such
4629 case, calling delete_thread does not really remove the
4630 thread from the list; instead, the thread is left listed,
4631 with 'exited' state.
4632
4633 - The target's debug interface does not support thread
4634 exit events, and so we have no idea whatsoever if the
4635 previously stepping thread is still alive. For that
4636 reason, we need to synchronously query the target
4637 now. */
b3444185
PA
4638 if (is_exited (tp->ptid)
4639 || !target_thread_alive (tp->ptid))
4640 {
4641 if (debug_infrun)
3e43a32a
MS
4642 fprintf_unfiltered (gdb_stdlog,
4643 "infrun: not switching back to "
4644 "stepped thread, it has vanished\n");
b3444185
PA
4645
4646 delete_thread (tp->ptid);
4647 keep_going (ecs);
4648 return;
4649 }
4650
a7212384
UW
4651 /* Otherwise, we no longer expect a trap in the current thread.
4652 Clear the trap_expected flag before switching back -- this is
4653 what keep_going would do as well, if we called it. */
16c381f0 4654 ecs->event_thread->control.trap_expected = 0;
a7212384
UW
4655
4656 if (debug_infrun)
4657 fprintf_unfiltered (gdb_stdlog,
4658 "infrun: switching back to stepped thread\n");
4659
4660 ecs->event_thread = tp;
4661 ecs->ptid = tp->ptid;
4662 context_switch (ecs->ptid);
4663 keep_going (ecs);
4664 return;
4665 }
4666 }
4667
8358c15c 4668 if (ecs->event_thread->control.step_resume_breakpoint)
488f131b 4669 {
527159b7 4670 if (debug_infrun)
d3169d93
DJ
4671 fprintf_unfiltered (gdb_stdlog,
4672 "infrun: step-resume breakpoint is inserted\n");
527159b7 4673
488f131b
JB
4674 /* Having a step-resume breakpoint overrides anything
4675 else having to do with stepping commands until
4676 that breakpoint is reached. */
488f131b
JB
4677 keep_going (ecs);
4678 return;
4679 }
c5aa993b 4680
16c381f0 4681 if (ecs->event_thread->control.step_range_end == 0)
488f131b 4682 {
527159b7 4683 if (debug_infrun)
8a9de0e4 4684 fprintf_unfiltered (gdb_stdlog, "infrun: no stepping, continue\n");
488f131b 4685 /* Likewise if we aren't even stepping. */
488f131b
JB
4686 keep_going (ecs);
4687 return;
4688 }
c5aa993b 4689
4b7703ad
JB
4690 /* Re-fetch current thread's frame in case the code above caused
4691 the frame cache to be re-initialized, making our FRAME variable
4692 a dangling pointer. */
4693 frame = get_current_frame ();
628fe4e4 4694 gdbarch = get_frame_arch (frame);
7e324e48 4695 fill_in_stop_func (gdbarch, ecs);
4b7703ad 4696
488f131b 4697 /* If stepping through a line, keep going if still within it.
c906108c 4698
488f131b
JB
4699 Note that step_range_end is the address of the first instruction
4700 beyond the step range, and NOT the address of the last instruction
31410e84
MS
4701 within it!
4702
4703 Note also that during reverse execution, we may be stepping
4704 through a function epilogue and therefore must detect when
4705 the current-frame changes in the middle of a line. */
4706
16c381f0
JK
4707 if (stop_pc >= ecs->event_thread->control.step_range_start
4708 && stop_pc < ecs->event_thread->control.step_range_end
31410e84 4709 && (execution_direction != EXEC_REVERSE
388a8562 4710 || frame_id_eq (get_frame_id (frame),
16c381f0 4711 ecs->event_thread->control.step_frame_id)))
488f131b 4712 {
527159b7 4713 if (debug_infrun)
5af949e3
UW
4714 fprintf_unfiltered
4715 (gdb_stdlog, "infrun: stepping inside range [%s-%s]\n",
16c381f0
JK
4716 paddress (gdbarch, ecs->event_thread->control.step_range_start),
4717 paddress (gdbarch, ecs->event_thread->control.step_range_end));
b2175913
MS
4718
4719 /* When stepping backward, stop at beginning of line range
4720 (unless it's the function entry point, in which case
4721 keep going back to the call point). */
16c381f0 4722 if (stop_pc == ecs->event_thread->control.step_range_start
b2175913
MS
4723 && stop_pc != ecs->stop_func_start
4724 && execution_direction == EXEC_REVERSE)
4725 {
16c381f0 4726 ecs->event_thread->control.stop_step = 1;
33d62d64 4727 print_end_stepping_range_reason ();
b2175913
MS
4728 stop_stepping (ecs);
4729 }
4730 else
4731 keep_going (ecs);
4732
488f131b
JB
4733 return;
4734 }
c5aa993b 4735
488f131b 4736 /* We stepped out of the stepping range. */
c906108c 4737
488f131b 4738 /* If we are stepping at the source level and entered the runtime
388a8562
MS
4739 loader dynamic symbol resolution code...
4740
4741 EXEC_FORWARD: we keep on single stepping until we exit the run
4742 time loader code and reach the callee's address.
4743
4744 EXEC_REVERSE: we've already executed the callee (backward), and
4745 the runtime loader code is handled just like any other
4746 undebuggable function call. Now we need only keep stepping
4747 backward through the trampoline code, and that's handled further
4748 down, so there is nothing for us to do here. */
4749
4750 if (execution_direction != EXEC_REVERSE
16c381f0 4751 && ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
cfd8ab24 4752 && in_solib_dynsym_resolve_code (stop_pc))
488f131b 4753 {
4c8c40e6 4754 CORE_ADDR pc_after_resolver =
568d6575 4755 gdbarch_skip_solib_resolver (gdbarch, stop_pc);
c906108c 4756
527159b7 4757 if (debug_infrun)
3e43a32a
MS
4758 fprintf_unfiltered (gdb_stdlog,
4759 "infrun: stepped into dynsym resolve code\n");
527159b7 4760
488f131b
JB
4761 if (pc_after_resolver)
4762 {
4763 /* Set up a step-resume breakpoint at the address
4764 indicated by SKIP_SOLIB_RESOLVER. */
4765 struct symtab_and_line sr_sal;
abbb1732 4766
fe39c653 4767 init_sal (&sr_sal);
488f131b 4768 sr_sal.pc = pc_after_resolver;
6c95b8df 4769 sr_sal.pspace = get_frame_program_space (frame);
488f131b 4770
a6d9a66e
UW
4771 insert_step_resume_breakpoint_at_sal (gdbarch,
4772 sr_sal, null_frame_id);
c5aa993b 4773 }
c906108c 4774
488f131b
JB
4775 keep_going (ecs);
4776 return;
4777 }
c906108c 4778
16c381f0
JK
4779 if (ecs->event_thread->control.step_range_end != 1
4780 && (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
4781 || ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
568d6575 4782 && get_frame_type (frame) == SIGTRAMP_FRAME)
488f131b 4783 {
527159b7 4784 if (debug_infrun)
3e43a32a
MS
4785 fprintf_unfiltered (gdb_stdlog,
4786 "infrun: stepped into signal trampoline\n");
42edda50 4787 /* The inferior, while doing a "step" or "next", has ended up in
8fb3e588
AC
4788 a signal trampoline (either by a signal being delivered or by
4789 the signal handler returning). Just single-step until the
4790 inferior leaves the trampoline (either by calling the handler
4791 or returning). */
488f131b
JB
4792 keep_going (ecs);
4793 return;
4794 }
c906108c 4795
14132e89
MR
4796 /* If we're in the return path from a shared library trampoline,
4797 we want to proceed through the trampoline when stepping. */
4798 /* macro/2012-04-25: This needs to come before the subroutine
4799 call check below as on some targets return trampolines look
4800 like subroutine calls (MIPS16 return thunks). */
4801 if (gdbarch_in_solib_return_trampoline (gdbarch,
4802 stop_pc, ecs->stop_func_name)
4803 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
4804 {
4805 /* Determine where this trampoline returns. */
4806 CORE_ADDR real_stop_pc;
4807
4808 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
4809
4810 if (debug_infrun)
4811 fprintf_unfiltered (gdb_stdlog,
4812 "infrun: stepped into solib return tramp\n");
4813
4814 /* Only proceed through if we know where it's going. */
4815 if (real_stop_pc)
4816 {
4817 /* And put the step-breakpoint there and go until there. */
4818 struct symtab_and_line sr_sal;
4819
4820 init_sal (&sr_sal); /* initialize to zeroes */
4821 sr_sal.pc = real_stop_pc;
4822 sr_sal.section = find_pc_overlay (sr_sal.pc);
4823 sr_sal.pspace = get_frame_program_space (frame);
4824
4825 /* Do not specify what the fp should be when we stop since
4826 on some machines the prologue is where the new fp value
4827 is established. */
4828 insert_step_resume_breakpoint_at_sal (gdbarch,
4829 sr_sal, null_frame_id);
4830
4831 /* Restart without fiddling with the step ranges or
4832 other state. */
4833 keep_going (ecs);
4834 return;
4835 }
4836 }
4837
c17eaafe
DJ
4838 /* Check for subroutine calls. The check for the current frame
4839 equalling the step ID is not necessary - the check of the
4840 previous frame's ID is sufficient - but it is a common case and
4841 cheaper than checking the previous frame's ID.
14e60db5
DJ
4842
4843 NOTE: frame_id_eq will never report two invalid frame IDs as
4844 being equal, so to get into this block, both the current and
4845 previous frame must have valid frame IDs. */
005ca36a
JB
4846 /* The outer_frame_id check is a heuristic to detect stepping
4847 through startup code. If we step over an instruction which
4848 sets the stack pointer from an invalid value to a valid value,
4849 we may detect that as a subroutine call from the mythical
4850 "outermost" function. This could be fixed by marking
4851 outermost frames as !stack_p,code_p,special_p. Then the
4852 initial outermost frame, before sp was valid, would
ce6cca6d 4853 have code_addr == &_start. See the comment in frame_id_eq
005ca36a 4854 for more. */
edb3359d 4855 if (!frame_id_eq (get_stack_frame_id (frame),
16c381f0 4856 ecs->event_thread->control.step_stack_frame_id)
005ca36a 4857 && (frame_id_eq (frame_unwind_caller_id (get_current_frame ()),
16c381f0
JK
4858 ecs->event_thread->control.step_stack_frame_id)
4859 && (!frame_id_eq (ecs->event_thread->control.step_stack_frame_id,
005ca36a
JB
4860 outer_frame_id)
4861 || step_start_function != find_pc_function (stop_pc))))
488f131b 4862 {
95918acb 4863 CORE_ADDR real_stop_pc;
8fb3e588 4864
527159b7 4865 if (debug_infrun)
8a9de0e4 4866 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into subroutine\n");
527159b7 4867
16c381f0
JK
4868 if ((ecs->event_thread->control.step_over_calls == STEP_OVER_NONE)
4869 || ((ecs->event_thread->control.step_range_end == 1)
d80b854b 4870 && in_prologue (gdbarch, ecs->event_thread->prev_pc,
4e1c45ea 4871 ecs->stop_func_start)))
95918acb
AC
4872 {
4873 /* I presume that step_over_calls is only 0 when we're
4874 supposed to be stepping at the assembly language level
4875 ("stepi"). Just stop. */
4876 /* Also, maybe we just did a "nexti" inside a prolog, so we
4877 thought it was a subroutine call but it was not. Stop as
4878 well. FENN */
388a8562 4879 /* And this works the same backward as frontward. MVS */
16c381f0 4880 ecs->event_thread->control.stop_step = 1;
33d62d64 4881 print_end_stepping_range_reason ();
95918acb
AC
4882 stop_stepping (ecs);
4883 return;
4884 }
8fb3e588 4885
388a8562
MS
4886 /* Reverse stepping through solib trampolines. */
4887
4888 if (execution_direction == EXEC_REVERSE
16c381f0 4889 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE
388a8562
MS
4890 && (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
4891 || (ecs->stop_func_start == 0
4892 && in_solib_dynsym_resolve_code (stop_pc))))
4893 {
4894 /* Any solib trampoline code can be handled in reverse
4895 by simply continuing to single-step. We have already
4896 executed the solib function (backwards), and a few
4897 steps will take us back through the trampoline to the
4898 caller. */
4899 keep_going (ecs);
4900 return;
4901 }
4902
16c381f0 4903 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
8567c30f 4904 {
b2175913
MS
4905 /* We're doing a "next".
4906
4907 Normal (forward) execution: set a breakpoint at the
4908 callee's return address (the address at which the caller
4909 will resume).
4910
4911 Reverse (backward) execution. set the step-resume
4912 breakpoint at the start of the function that we just
4913 stepped into (backwards), and continue to there. When we
6130d0b7 4914 get there, we'll need to single-step back to the caller. */
b2175913
MS
4915
4916 if (execution_direction == EXEC_REVERSE)
4917 {
acf9414f
JK
4918 /* If we're already at the start of the function, we've either
4919 just stepped backward into a single instruction function,
4920 or stepped back out of a signal handler to the first instruction
4921 of the function. Just keep going, which will single-step back
4922 to the caller. */
4923 if (ecs->stop_func_start != stop_pc)
4924 {
4925 struct symtab_and_line sr_sal;
4926
4927 /* Normal function call return (static or dynamic). */
4928 init_sal (&sr_sal);
4929 sr_sal.pc = ecs->stop_func_start;
4930 sr_sal.pspace = get_frame_program_space (frame);
4931 insert_step_resume_breakpoint_at_sal (gdbarch,
4932 sr_sal, null_frame_id);
4933 }
b2175913
MS
4934 }
4935 else
568d6575 4936 insert_step_resume_breakpoint_at_caller (frame);
b2175913 4937
8567c30f
AC
4938 keep_going (ecs);
4939 return;
4940 }
a53c66de 4941
95918acb 4942 /* If we are in a function call trampoline (a stub between the
8fb3e588
AC
4943 calling routine and the real function), locate the real
4944 function. That's what tells us (a) whether we want to step
4945 into it at all, and (b) what prologue we want to run to the
4946 end of, if we do step into it. */
568d6575 4947 real_stop_pc = skip_language_trampoline (frame, stop_pc);
95918acb 4948 if (real_stop_pc == 0)
568d6575 4949 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
95918acb
AC
4950 if (real_stop_pc != 0)
4951 ecs->stop_func_start = real_stop_pc;
8fb3e588 4952
db5f024e 4953 if (real_stop_pc != 0 && in_solib_dynsym_resolve_code (real_stop_pc))
1b2bfbb9
RC
4954 {
4955 struct symtab_and_line sr_sal;
abbb1732 4956
1b2bfbb9
RC
4957 init_sal (&sr_sal);
4958 sr_sal.pc = ecs->stop_func_start;
6c95b8df 4959 sr_sal.pspace = get_frame_program_space (frame);
1b2bfbb9 4960
a6d9a66e
UW
4961 insert_step_resume_breakpoint_at_sal (gdbarch,
4962 sr_sal, null_frame_id);
8fb3e588
AC
4963 keep_going (ecs);
4964 return;
1b2bfbb9
RC
4965 }
4966
95918acb 4967 /* If we have line number information for the function we are
1bfeeb0f
JL
4968 thinking of stepping into and the function isn't on the skip
4969 list, step into it.
95918acb 4970
8fb3e588
AC
4971 If there are several symtabs at that PC (e.g. with include
4972 files), just want to know whether *any* of them have line
4973 numbers. find_pc_line handles this. */
95918acb
AC
4974 {
4975 struct symtab_and_line tmp_sal;
8fb3e588 4976
95918acb 4977 tmp_sal = find_pc_line (ecs->stop_func_start, 0);
2b914b52
JB
4978 if (tmp_sal.line != 0
4979 && !function_pc_is_marked_for_skip (ecs->stop_func_start))
95918acb 4980 {
b2175913 4981 if (execution_direction == EXEC_REVERSE)
568d6575 4982 handle_step_into_function_backward (gdbarch, ecs);
b2175913 4983 else
568d6575 4984 handle_step_into_function (gdbarch, ecs);
95918acb
AC
4985 return;
4986 }
4987 }
4988
4989 /* If we have no line number and the step-stop-if-no-debug is
8fb3e588
AC
4990 set, we stop the step so that the user has a chance to switch
4991 in assembly mode. */
16c381f0 4992 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
078130d0 4993 && step_stop_if_no_debug)
95918acb 4994 {
16c381f0 4995 ecs->event_thread->control.stop_step = 1;
33d62d64 4996 print_end_stepping_range_reason ();
95918acb
AC
4997 stop_stepping (ecs);
4998 return;
4999 }
5000
b2175913
MS
5001 if (execution_direction == EXEC_REVERSE)
5002 {
acf9414f
JK
5003 /* If we're already at the start of the function, we've either just
5004 stepped backward into a single instruction function without line
5005 number info, or stepped back out of a signal handler to the first
5006 instruction of the function without line number info. Just keep
5007 going, which will single-step back to the caller. */
5008 if (ecs->stop_func_start != stop_pc)
5009 {
5010 /* Set a breakpoint at callee's start address.
5011 From there we can step once and be back in the caller. */
5012 struct symtab_and_line sr_sal;
abbb1732 5013
acf9414f
JK
5014 init_sal (&sr_sal);
5015 sr_sal.pc = ecs->stop_func_start;
5016 sr_sal.pspace = get_frame_program_space (frame);
5017 insert_step_resume_breakpoint_at_sal (gdbarch,
5018 sr_sal, null_frame_id);
5019 }
b2175913
MS
5020 }
5021 else
5022 /* Set a breakpoint at callee's return address (the address
5023 at which the caller will resume). */
568d6575 5024 insert_step_resume_breakpoint_at_caller (frame);
b2175913 5025
95918acb 5026 keep_going (ecs);
488f131b 5027 return;
488f131b 5028 }
c906108c 5029
fdd654f3
MS
5030 /* Reverse stepping through solib trampolines. */
5031
5032 if (execution_direction == EXEC_REVERSE
16c381f0 5033 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
fdd654f3
MS
5034 {
5035 if (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
5036 || (ecs->stop_func_start == 0
5037 && in_solib_dynsym_resolve_code (stop_pc)))
5038 {
5039 /* Any solib trampoline code can be handled in reverse
5040 by simply continuing to single-step. We have already
5041 executed the solib function (backwards), and a few
5042 steps will take us back through the trampoline to the
5043 caller. */
5044 keep_going (ecs);
5045 return;
5046 }
5047 else if (in_solib_dynsym_resolve_code (stop_pc))
5048 {
5049 /* Stepped backward into the solib dynsym resolver.
5050 Set a breakpoint at its start and continue, then
5051 one more step will take us out. */
5052 struct symtab_and_line sr_sal;
abbb1732 5053
fdd654f3
MS
5054 init_sal (&sr_sal);
5055 sr_sal.pc = ecs->stop_func_start;
9d1807c3 5056 sr_sal.pspace = get_frame_program_space (frame);
fdd654f3
MS
5057 insert_step_resume_breakpoint_at_sal (gdbarch,
5058 sr_sal, null_frame_id);
5059 keep_going (ecs);
5060 return;
5061 }
5062 }
5063
2afb61aa 5064 stop_pc_sal = find_pc_line (stop_pc, 0);
7ed0fe66 5065
1b2bfbb9
RC
5066 /* NOTE: tausq/2004-05-24: This if block used to be done before all
5067 the trampoline processing logic, however, there are some trampolines
5068 that have no names, so we should do trampoline handling first. */
16c381f0 5069 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
7ed0fe66 5070 && ecs->stop_func_name == NULL
2afb61aa 5071 && stop_pc_sal.line == 0)
1b2bfbb9 5072 {
527159b7 5073 if (debug_infrun)
3e43a32a
MS
5074 fprintf_unfiltered (gdb_stdlog,
5075 "infrun: stepped into undebuggable function\n");
527159b7 5076
1b2bfbb9 5077 /* The inferior just stepped into, or returned to, an
7ed0fe66
DJ
5078 undebuggable function (where there is no debugging information
5079 and no line number corresponding to the address where the
1b2bfbb9
RC
5080 inferior stopped). Since we want to skip this kind of code,
5081 we keep going until the inferior returns from this
14e60db5
DJ
5082 function - unless the user has asked us not to (via
5083 set step-mode) or we no longer know how to get back
5084 to the call site. */
5085 if (step_stop_if_no_debug
c7ce8faa 5086 || !frame_id_p (frame_unwind_caller_id (frame)))
1b2bfbb9
RC
5087 {
5088 /* If we have no line number and the step-stop-if-no-debug
5089 is set, we stop the step so that the user has a chance to
5090 switch in assembly mode. */
16c381f0 5091 ecs->event_thread->control.stop_step = 1;
33d62d64 5092 print_end_stepping_range_reason ();
1b2bfbb9
RC
5093 stop_stepping (ecs);
5094 return;
5095 }
5096 else
5097 {
5098 /* Set a breakpoint at callee's return address (the address
5099 at which the caller will resume). */
568d6575 5100 insert_step_resume_breakpoint_at_caller (frame);
1b2bfbb9
RC
5101 keep_going (ecs);
5102 return;
5103 }
5104 }
5105
16c381f0 5106 if (ecs->event_thread->control.step_range_end == 1)
1b2bfbb9
RC
5107 {
5108 /* It is stepi or nexti. We always want to stop stepping after
5109 one instruction. */
527159b7 5110 if (debug_infrun)
8a9de0e4 5111 fprintf_unfiltered (gdb_stdlog, "infrun: stepi/nexti\n");
16c381f0 5112 ecs->event_thread->control.stop_step = 1;
33d62d64 5113 print_end_stepping_range_reason ();
1b2bfbb9
RC
5114 stop_stepping (ecs);
5115 return;
5116 }
5117
2afb61aa 5118 if (stop_pc_sal.line == 0)
488f131b
JB
5119 {
5120 /* We have no line number information. That means to stop
5121 stepping (does this always happen right after one instruction,
5122 when we do "s" in a function with no line numbers,
5123 or can this happen as a result of a return or longjmp?). */
527159b7 5124 if (debug_infrun)
8a9de0e4 5125 fprintf_unfiltered (gdb_stdlog, "infrun: no line number info\n");
16c381f0 5126 ecs->event_thread->control.stop_step = 1;
33d62d64 5127 print_end_stepping_range_reason ();
488f131b
JB
5128 stop_stepping (ecs);
5129 return;
5130 }
c906108c 5131
edb3359d
DJ
5132 /* Look for "calls" to inlined functions, part one. If the inline
5133 frame machinery detected some skipped call sites, we have entered
5134 a new inline function. */
5135
5136 if (frame_id_eq (get_frame_id (get_current_frame ()),
16c381f0 5137 ecs->event_thread->control.step_frame_id)
edb3359d
DJ
5138 && inline_skipped_frames (ecs->ptid))
5139 {
5140 struct symtab_and_line call_sal;
5141
5142 if (debug_infrun)
5143 fprintf_unfiltered (gdb_stdlog,
5144 "infrun: stepped into inlined function\n");
5145
5146 find_frame_sal (get_current_frame (), &call_sal);
5147
16c381f0 5148 if (ecs->event_thread->control.step_over_calls != STEP_OVER_ALL)
edb3359d
DJ
5149 {
5150 /* For "step", we're going to stop. But if the call site
5151 for this inlined function is on the same source line as
5152 we were previously stepping, go down into the function
5153 first. Otherwise stop at the call site. */
5154
5155 if (call_sal.line == ecs->event_thread->current_line
5156 && call_sal.symtab == ecs->event_thread->current_symtab)
5157 step_into_inline_frame (ecs->ptid);
5158
16c381f0 5159 ecs->event_thread->control.stop_step = 1;
33d62d64 5160 print_end_stepping_range_reason ();
edb3359d
DJ
5161 stop_stepping (ecs);
5162 return;
5163 }
5164 else
5165 {
5166 /* For "next", we should stop at the call site if it is on a
5167 different source line. Otherwise continue through the
5168 inlined function. */
5169 if (call_sal.line == ecs->event_thread->current_line
5170 && call_sal.symtab == ecs->event_thread->current_symtab)
5171 keep_going (ecs);
5172 else
5173 {
16c381f0 5174 ecs->event_thread->control.stop_step = 1;
33d62d64 5175 print_end_stepping_range_reason ();
edb3359d
DJ
5176 stop_stepping (ecs);
5177 }
5178 return;
5179 }
5180 }
5181
5182 /* Look for "calls" to inlined functions, part two. If we are still
5183 in the same real function we were stepping through, but we have
5184 to go further up to find the exact frame ID, we are stepping
5185 through a more inlined call beyond its call site. */
5186
5187 if (get_frame_type (get_current_frame ()) == INLINE_FRAME
5188 && !frame_id_eq (get_frame_id (get_current_frame ()),
16c381f0 5189 ecs->event_thread->control.step_frame_id)
edb3359d 5190 && stepped_in_from (get_current_frame (),
16c381f0 5191 ecs->event_thread->control.step_frame_id))
edb3359d
DJ
5192 {
5193 if (debug_infrun)
5194 fprintf_unfiltered (gdb_stdlog,
5195 "infrun: stepping through inlined function\n");
5196
16c381f0 5197 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
edb3359d
DJ
5198 keep_going (ecs);
5199 else
5200 {
16c381f0 5201 ecs->event_thread->control.stop_step = 1;
33d62d64 5202 print_end_stepping_range_reason ();
edb3359d
DJ
5203 stop_stepping (ecs);
5204 }
5205 return;
5206 }
5207
2afb61aa 5208 if ((stop_pc == stop_pc_sal.pc)
4e1c45ea
PA
5209 && (ecs->event_thread->current_line != stop_pc_sal.line
5210 || ecs->event_thread->current_symtab != stop_pc_sal.symtab))
488f131b
JB
5211 {
5212 /* We are at the start of a different line. So stop. Note that
5213 we don't stop if we step into the middle of a different line.
5214 That is said to make things like for (;;) statements work
5215 better. */
527159b7 5216 if (debug_infrun)
3e43a32a
MS
5217 fprintf_unfiltered (gdb_stdlog,
5218 "infrun: stepped to a different line\n");
16c381f0 5219 ecs->event_thread->control.stop_step = 1;
33d62d64 5220 print_end_stepping_range_reason ();
488f131b
JB
5221 stop_stepping (ecs);
5222 return;
5223 }
c906108c 5224
488f131b 5225 /* We aren't done stepping.
c906108c 5226
488f131b
JB
5227 Optimize by setting the stepping range to the line.
5228 (We might not be in the original line, but if we entered a
5229 new line in mid-statement, we continue stepping. This makes
5230 things like for(;;) statements work better.) */
c906108c 5231
16c381f0
JK
5232 ecs->event_thread->control.step_range_start = stop_pc_sal.pc;
5233 ecs->event_thread->control.step_range_end = stop_pc_sal.end;
edb3359d 5234 set_step_info (frame, stop_pc_sal);
488f131b 5235
527159b7 5236 if (debug_infrun)
8a9de0e4 5237 fprintf_unfiltered (gdb_stdlog, "infrun: keep going\n");
488f131b 5238 keep_going (ecs);
104c1213
JM
5239}
5240
b3444185 5241/* Is thread TP in the middle of single-stepping? */
104c1213 5242
a289b8f6 5243static int
b3444185 5244currently_stepping (struct thread_info *tp)
a7212384 5245{
8358c15c
JK
5246 return ((tp->control.step_range_end
5247 && tp->control.step_resume_breakpoint == NULL)
5248 || tp->control.trap_expected
8358c15c 5249 || bpstat_should_step ());
a7212384
UW
5250}
5251
b3444185
PA
5252/* Returns true if any thread *but* the one passed in "data" is in the
5253 middle of stepping or of handling a "next". */
a7212384 5254
104c1213 5255static int
b3444185 5256currently_stepping_or_nexting_callback (struct thread_info *tp, void *data)
104c1213 5257{
b3444185
PA
5258 if (tp == data)
5259 return 0;
5260
16c381f0 5261 return (tp->control.step_range_end
ede1849f 5262 || tp->control.trap_expected);
104c1213 5263}
c906108c 5264
b2175913
MS
5265/* Inferior has stepped into a subroutine call with source code that
5266 we should not step over. Do step to the first line of code in
5267 it. */
c2c6d25f
JM
5268
5269static void
568d6575
UW
5270handle_step_into_function (struct gdbarch *gdbarch,
5271 struct execution_control_state *ecs)
c2c6d25f
JM
5272{
5273 struct symtab *s;
2afb61aa 5274 struct symtab_and_line stop_func_sal, sr_sal;
c2c6d25f 5275
7e324e48
GB
5276 fill_in_stop_func (gdbarch, ecs);
5277
c2c6d25f
JM
5278 s = find_pc_symtab (stop_pc);
5279 if (s && s->language != language_asm)
568d6575 5280 ecs->stop_func_start = gdbarch_skip_prologue (gdbarch,
b2175913 5281 ecs->stop_func_start);
c2c6d25f 5282
2afb61aa 5283 stop_func_sal = find_pc_line (ecs->stop_func_start, 0);
c2c6d25f
JM
5284 /* Use the step_resume_break to step until the end of the prologue,
5285 even if that involves jumps (as it seems to on the vax under
5286 4.2). */
5287 /* If the prologue ends in the middle of a source line, continue to
5288 the end of that source line (if it is still within the function).
5289 Otherwise, just go to end of prologue. */
2afb61aa
PA
5290 if (stop_func_sal.end
5291 && stop_func_sal.pc != ecs->stop_func_start
5292 && stop_func_sal.end < ecs->stop_func_end)
5293 ecs->stop_func_start = stop_func_sal.end;
c2c6d25f 5294
2dbd5e30
KB
5295 /* Architectures which require breakpoint adjustment might not be able
5296 to place a breakpoint at the computed address. If so, the test
5297 ``ecs->stop_func_start == stop_pc'' will never succeed. Adjust
5298 ecs->stop_func_start to an address at which a breakpoint may be
5299 legitimately placed.
8fb3e588 5300
2dbd5e30
KB
5301 Note: kevinb/2004-01-19: On FR-V, if this adjustment is not
5302 made, GDB will enter an infinite loop when stepping through
5303 optimized code consisting of VLIW instructions which contain
5304 subinstructions corresponding to different source lines. On
5305 FR-V, it's not permitted to place a breakpoint on any but the
5306 first subinstruction of a VLIW instruction. When a breakpoint is
5307 set, GDB will adjust the breakpoint address to the beginning of
5308 the VLIW instruction. Thus, we need to make the corresponding
5309 adjustment here when computing the stop address. */
8fb3e588 5310
568d6575 5311 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
2dbd5e30
KB
5312 {
5313 ecs->stop_func_start
568d6575 5314 = gdbarch_adjust_breakpoint_address (gdbarch,
8fb3e588 5315 ecs->stop_func_start);
2dbd5e30
KB
5316 }
5317
c2c6d25f
JM
5318 if (ecs->stop_func_start == stop_pc)
5319 {
5320 /* We are already there: stop now. */
16c381f0 5321 ecs->event_thread->control.stop_step = 1;
33d62d64 5322 print_end_stepping_range_reason ();
c2c6d25f
JM
5323 stop_stepping (ecs);
5324 return;
5325 }
5326 else
5327 {
5328 /* Put the step-breakpoint there and go until there. */
fe39c653 5329 init_sal (&sr_sal); /* initialize to zeroes */
c2c6d25f
JM
5330 sr_sal.pc = ecs->stop_func_start;
5331 sr_sal.section = find_pc_overlay (ecs->stop_func_start);
6c95b8df 5332 sr_sal.pspace = get_frame_program_space (get_current_frame ());
44cbf7b5 5333
c2c6d25f 5334 /* Do not specify what the fp should be when we stop since on
488f131b
JB
5335 some machines the prologue is where the new fp value is
5336 established. */
a6d9a66e 5337 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal, null_frame_id);
c2c6d25f
JM
5338
5339 /* And make sure stepping stops right away then. */
16c381f0
JK
5340 ecs->event_thread->control.step_range_end
5341 = ecs->event_thread->control.step_range_start;
c2c6d25f
JM
5342 }
5343 keep_going (ecs);
5344}
d4f3574e 5345
b2175913
MS
5346/* Inferior has stepped backward into a subroutine call with source
5347 code that we should not step over. Do step to the beginning of the
5348 last line of code in it. */
5349
5350static void
568d6575
UW
5351handle_step_into_function_backward (struct gdbarch *gdbarch,
5352 struct execution_control_state *ecs)
b2175913
MS
5353{
5354 struct symtab *s;
167e4384 5355 struct symtab_and_line stop_func_sal;
b2175913 5356
7e324e48
GB
5357 fill_in_stop_func (gdbarch, ecs);
5358
b2175913
MS
5359 s = find_pc_symtab (stop_pc);
5360 if (s && s->language != language_asm)
568d6575 5361 ecs->stop_func_start = gdbarch_skip_prologue (gdbarch,
b2175913
MS
5362 ecs->stop_func_start);
5363
5364 stop_func_sal = find_pc_line (stop_pc, 0);
5365
5366 /* OK, we're just going to keep stepping here. */
5367 if (stop_func_sal.pc == stop_pc)
5368 {
5369 /* We're there already. Just stop stepping now. */
16c381f0 5370 ecs->event_thread->control.stop_step = 1;
33d62d64 5371 print_end_stepping_range_reason ();
b2175913
MS
5372 stop_stepping (ecs);
5373 }
5374 else
5375 {
5376 /* Else just reset the step range and keep going.
5377 No step-resume breakpoint, they don't work for
5378 epilogues, which can have multiple entry paths. */
16c381f0
JK
5379 ecs->event_thread->control.step_range_start = stop_func_sal.pc;
5380 ecs->event_thread->control.step_range_end = stop_func_sal.end;
b2175913
MS
5381 keep_going (ecs);
5382 }
5383 return;
5384}
5385
d3169d93 5386/* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
44cbf7b5
AC
5387 This is used to both functions and to skip over code. */
5388
5389static void
2c03e5be
PA
5390insert_step_resume_breakpoint_at_sal_1 (struct gdbarch *gdbarch,
5391 struct symtab_and_line sr_sal,
5392 struct frame_id sr_id,
5393 enum bptype sr_type)
44cbf7b5 5394{
611c83ae
PA
5395 /* There should never be more than one step-resume or longjmp-resume
5396 breakpoint per thread, so we should never be setting a new
44cbf7b5 5397 step_resume_breakpoint when one is already active. */
8358c15c 5398 gdb_assert (inferior_thread ()->control.step_resume_breakpoint == NULL);
2c03e5be 5399 gdb_assert (sr_type == bp_step_resume || sr_type == bp_hp_step_resume);
d3169d93
DJ
5400
5401 if (debug_infrun)
5402 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
5403 "infrun: inserting step-resume breakpoint at %s\n",
5404 paddress (gdbarch, sr_sal.pc));
d3169d93 5405
8358c15c 5406 inferior_thread ()->control.step_resume_breakpoint
2c03e5be
PA
5407 = set_momentary_breakpoint (gdbarch, sr_sal, sr_id, sr_type);
5408}
5409
9da8c2a0 5410void
2c03e5be
PA
5411insert_step_resume_breakpoint_at_sal (struct gdbarch *gdbarch,
5412 struct symtab_and_line sr_sal,
5413 struct frame_id sr_id)
5414{
5415 insert_step_resume_breakpoint_at_sal_1 (gdbarch,
5416 sr_sal, sr_id,
5417 bp_step_resume);
44cbf7b5 5418}
7ce450bd 5419
2c03e5be
PA
5420/* Insert a "high-priority step-resume breakpoint" at RETURN_FRAME.pc.
5421 This is used to skip a potential signal handler.
7ce450bd 5422
14e60db5
DJ
5423 This is called with the interrupted function's frame. The signal
5424 handler, when it returns, will resume the interrupted function at
5425 RETURN_FRAME.pc. */
d303a6c7
AC
5426
5427static void
2c03e5be 5428insert_hp_step_resume_breakpoint_at_frame (struct frame_info *return_frame)
d303a6c7
AC
5429{
5430 struct symtab_and_line sr_sal;
a6d9a66e 5431 struct gdbarch *gdbarch;
d303a6c7 5432
f4c1edd8 5433 gdb_assert (return_frame != NULL);
d303a6c7
AC
5434 init_sal (&sr_sal); /* initialize to zeros */
5435
a6d9a66e 5436 gdbarch = get_frame_arch (return_frame);
568d6575 5437 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch, get_frame_pc (return_frame));
d303a6c7 5438 sr_sal.section = find_pc_overlay (sr_sal.pc);
6c95b8df 5439 sr_sal.pspace = get_frame_program_space (return_frame);
d303a6c7 5440
2c03e5be
PA
5441 insert_step_resume_breakpoint_at_sal_1 (gdbarch, sr_sal,
5442 get_stack_frame_id (return_frame),
5443 bp_hp_step_resume);
d303a6c7
AC
5444}
5445
2c03e5be
PA
5446/* Insert a "step-resume breakpoint" at the previous frame's PC. This
5447 is used to skip a function after stepping into it (for "next" or if
5448 the called function has no debugging information).
14e60db5
DJ
5449
5450 The current function has almost always been reached by single
5451 stepping a call or return instruction. NEXT_FRAME belongs to the
5452 current function, and the breakpoint will be set at the caller's
5453 resume address.
5454
5455 This is a separate function rather than reusing
2c03e5be 5456 insert_hp_step_resume_breakpoint_at_frame in order to avoid
14e60db5 5457 get_prev_frame, which may stop prematurely (see the implementation
c7ce8faa 5458 of frame_unwind_caller_id for an example). */
14e60db5
DJ
5459
5460static void
5461insert_step_resume_breakpoint_at_caller (struct frame_info *next_frame)
5462{
5463 struct symtab_and_line sr_sal;
a6d9a66e 5464 struct gdbarch *gdbarch;
14e60db5
DJ
5465
5466 /* We shouldn't have gotten here if we don't know where the call site
5467 is. */
c7ce8faa 5468 gdb_assert (frame_id_p (frame_unwind_caller_id (next_frame)));
14e60db5
DJ
5469
5470 init_sal (&sr_sal); /* initialize to zeros */
5471
a6d9a66e 5472 gdbarch = frame_unwind_caller_arch (next_frame);
c7ce8faa
DJ
5473 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch,
5474 frame_unwind_caller_pc (next_frame));
14e60db5 5475 sr_sal.section = find_pc_overlay (sr_sal.pc);
6c95b8df 5476 sr_sal.pspace = frame_unwind_program_space (next_frame);
14e60db5 5477
a6d9a66e 5478 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal,
c7ce8faa 5479 frame_unwind_caller_id (next_frame));
14e60db5
DJ
5480}
5481
611c83ae
PA
5482/* Insert a "longjmp-resume" breakpoint at PC. This is used to set a
5483 new breakpoint at the target of a jmp_buf. The handling of
5484 longjmp-resume uses the same mechanisms used for handling
5485 "step-resume" breakpoints. */
5486
5487static void
a6d9a66e 5488insert_longjmp_resume_breakpoint (struct gdbarch *gdbarch, CORE_ADDR pc)
611c83ae 5489{
e81a37f7
TT
5490 /* There should never be more than one longjmp-resume breakpoint per
5491 thread, so we should never be setting a new
611c83ae 5492 longjmp_resume_breakpoint when one is already active. */
e81a37f7 5493 gdb_assert (inferior_thread ()->control.exception_resume_breakpoint == NULL);
611c83ae
PA
5494
5495 if (debug_infrun)
5496 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
5497 "infrun: inserting longjmp-resume breakpoint at %s\n",
5498 paddress (gdbarch, pc));
611c83ae 5499
e81a37f7 5500 inferior_thread ()->control.exception_resume_breakpoint =
a6d9a66e 5501 set_momentary_breakpoint_at_pc (gdbarch, pc, bp_longjmp_resume);
611c83ae
PA
5502}
5503
186c406b
TT
5504/* Insert an exception resume breakpoint. TP is the thread throwing
5505 the exception. The block B is the block of the unwinder debug hook
5506 function. FRAME is the frame corresponding to the call to this
5507 function. SYM is the symbol of the function argument holding the
5508 target PC of the exception. */
5509
5510static void
5511insert_exception_resume_breakpoint (struct thread_info *tp,
5512 struct block *b,
5513 struct frame_info *frame,
5514 struct symbol *sym)
5515{
bfd189b1 5516 volatile struct gdb_exception e;
186c406b
TT
5517
5518 /* We want to ignore errors here. */
5519 TRY_CATCH (e, RETURN_MASK_ERROR)
5520 {
5521 struct symbol *vsym;
5522 struct value *value;
5523 CORE_ADDR handler;
5524 struct breakpoint *bp;
5525
5526 vsym = lookup_symbol (SYMBOL_LINKAGE_NAME (sym), b, VAR_DOMAIN, NULL);
5527 value = read_var_value (vsym, frame);
5528 /* If the value was optimized out, revert to the old behavior. */
5529 if (! value_optimized_out (value))
5530 {
5531 handler = value_as_address (value);
5532
5533 if (debug_infrun)
5534 fprintf_unfiltered (gdb_stdlog,
5535 "infrun: exception resume at %lx\n",
5536 (unsigned long) handler);
5537
5538 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
5539 handler, bp_exception_resume);
c70a6932
JK
5540
5541 /* set_momentary_breakpoint_at_pc invalidates FRAME. */
5542 frame = NULL;
5543
186c406b
TT
5544 bp->thread = tp->num;
5545 inferior_thread ()->control.exception_resume_breakpoint = bp;
5546 }
5547 }
5548}
5549
28106bc2
SDJ
5550/* A helper for check_exception_resume that sets an
5551 exception-breakpoint based on a SystemTap probe. */
5552
5553static void
5554insert_exception_resume_from_probe (struct thread_info *tp,
5555 const struct probe *probe,
28106bc2
SDJ
5556 struct frame_info *frame)
5557{
5558 struct value *arg_value;
5559 CORE_ADDR handler;
5560 struct breakpoint *bp;
5561
5562 arg_value = probe_safe_evaluate_at_pc (frame, 1);
5563 if (!arg_value)
5564 return;
5565
5566 handler = value_as_address (arg_value);
5567
5568 if (debug_infrun)
5569 fprintf_unfiltered (gdb_stdlog,
5570 "infrun: exception resume at %s\n",
6bac7473 5571 paddress (get_objfile_arch (probe->objfile),
28106bc2
SDJ
5572 handler));
5573
5574 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
5575 handler, bp_exception_resume);
5576 bp->thread = tp->num;
5577 inferior_thread ()->control.exception_resume_breakpoint = bp;
5578}
5579
186c406b
TT
5580/* This is called when an exception has been intercepted. Check to
5581 see whether the exception's destination is of interest, and if so,
5582 set an exception resume breakpoint there. */
5583
5584static void
5585check_exception_resume (struct execution_control_state *ecs,
28106bc2 5586 struct frame_info *frame)
186c406b 5587{
bfd189b1 5588 volatile struct gdb_exception e;
28106bc2
SDJ
5589 const struct probe *probe;
5590 struct symbol *func;
5591
5592 /* First see if this exception unwinding breakpoint was set via a
5593 SystemTap probe point. If so, the probe has two arguments: the
5594 CFA and the HANDLER. We ignore the CFA, extract the handler, and
5595 set a breakpoint there. */
6bac7473 5596 probe = find_probe_by_pc (get_frame_pc (frame));
28106bc2
SDJ
5597 if (probe)
5598 {
6bac7473 5599 insert_exception_resume_from_probe (ecs->event_thread, probe, frame);
28106bc2
SDJ
5600 return;
5601 }
5602
5603 func = get_frame_function (frame);
5604 if (!func)
5605 return;
186c406b
TT
5606
5607 TRY_CATCH (e, RETURN_MASK_ERROR)
5608 {
5609 struct block *b;
8157b174 5610 struct block_iterator iter;
186c406b
TT
5611 struct symbol *sym;
5612 int argno = 0;
5613
5614 /* The exception breakpoint is a thread-specific breakpoint on
5615 the unwinder's debug hook, declared as:
5616
5617 void _Unwind_DebugHook (void *cfa, void *handler);
5618
5619 The CFA argument indicates the frame to which control is
5620 about to be transferred. HANDLER is the destination PC.
5621
5622 We ignore the CFA and set a temporary breakpoint at HANDLER.
5623 This is not extremely efficient but it avoids issues in gdb
5624 with computing the DWARF CFA, and it also works even in weird
5625 cases such as throwing an exception from inside a signal
5626 handler. */
5627
5628 b = SYMBOL_BLOCK_VALUE (func);
5629 ALL_BLOCK_SYMBOLS (b, iter, sym)
5630 {
5631 if (!SYMBOL_IS_ARGUMENT (sym))
5632 continue;
5633
5634 if (argno == 0)
5635 ++argno;
5636 else
5637 {
5638 insert_exception_resume_breakpoint (ecs->event_thread,
5639 b, frame, sym);
5640 break;
5641 }
5642 }
5643 }
5644}
5645
104c1213
JM
5646static void
5647stop_stepping (struct execution_control_state *ecs)
5648{
527159b7 5649 if (debug_infrun)
8a9de0e4 5650 fprintf_unfiltered (gdb_stdlog, "infrun: stop_stepping\n");
527159b7 5651
cd0fc7c3
SS
5652 /* Let callers know we don't want to wait for the inferior anymore. */
5653 ecs->wait_some_more = 0;
5654}
5655
d4f3574e
SS
5656/* This function handles various cases where we need to continue
5657 waiting for the inferior. */
1777feb0 5658/* (Used to be the keep_going: label in the old wait_for_inferior). */
d4f3574e
SS
5659
5660static void
5661keep_going (struct execution_control_state *ecs)
5662{
c4dbc9af
PA
5663 /* Make sure normal_stop is called if we get a QUIT handled before
5664 reaching resume. */
5665 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
5666
d4f3574e 5667 /* Save the pc before execution, to compare with pc after stop. */
fb14de7b
UW
5668 ecs->event_thread->prev_pc
5669 = regcache_read_pc (get_thread_regcache (ecs->ptid));
d4f3574e 5670
d4f3574e
SS
5671 /* If we did not do break;, it means we should keep running the
5672 inferior and not return to debugger. */
5673
16c381f0 5674 if (ecs->event_thread->control.trap_expected
a493e3e2 5675 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_TRAP)
d4f3574e
SS
5676 {
5677 /* We took a signal (which we are supposed to pass through to
4e1c45ea
PA
5678 the inferior, else we'd not get here) and we haven't yet
5679 gotten our trap. Simply continue. */
c4dbc9af
PA
5680
5681 discard_cleanups (old_cleanups);
2020b7ab 5682 resume (currently_stepping (ecs->event_thread),
16c381f0 5683 ecs->event_thread->suspend.stop_signal);
d4f3574e
SS
5684 }
5685 else
5686 {
5687 /* Either the trap was not expected, but we are continuing
488f131b
JB
5688 anyway (the user asked that this signal be passed to the
5689 child)
5690 -- or --
5691 The signal was SIGTRAP, e.g. it was our signal, but we
5692 decided we should resume from it.
d4f3574e 5693
c36b740a 5694 We're going to run this baby now!
d4f3574e 5695
c36b740a
VP
5696 Note that insert_breakpoints won't try to re-insert
5697 already inserted breakpoints. Therefore, we don't
5698 care if breakpoints were already inserted, or not. */
5699
4e1c45ea 5700 if (ecs->event_thread->stepping_over_breakpoint)
45e8c884 5701 {
9f5a595d 5702 struct regcache *thread_regcache = get_thread_regcache (ecs->ptid);
abbb1732 5703
9f5a595d 5704 if (!use_displaced_stepping (get_regcache_arch (thread_regcache)))
237fc4c9
PA
5705 /* Since we can't do a displaced step, we have to remove
5706 the breakpoint while we step it. To keep things
5707 simple, we remove them all. */
5708 remove_breakpoints ();
45e8c884
VP
5709 }
5710 else
d4f3574e 5711 {
bfd189b1 5712 volatile struct gdb_exception e;
abbb1732 5713
569631c6
UW
5714 /* Stop stepping when inserting breakpoints
5715 has failed. */
e236ba44
VP
5716 TRY_CATCH (e, RETURN_MASK_ERROR)
5717 {
5718 insert_breakpoints ();
5719 }
5720 if (e.reason < 0)
d4f3574e 5721 {
97bd5475 5722 exception_print (gdb_stderr, e);
d4f3574e
SS
5723 stop_stepping (ecs);
5724 return;
5725 }
d4f3574e
SS
5726 }
5727
16c381f0
JK
5728 ecs->event_thread->control.trap_expected
5729 = ecs->event_thread->stepping_over_breakpoint;
d4f3574e
SS
5730
5731 /* Do not deliver SIGNAL_TRAP (except when the user explicitly
488f131b
JB
5732 specifies that such a signal should be delivered to the
5733 target program).
5734
5735 Typically, this would occure when a user is debugging a
5736 target monitor on a simulator: the target monitor sets a
5737 breakpoint; the simulator encounters this break-point and
5738 halts the simulation handing control to GDB; GDB, noteing
5739 that the break-point isn't valid, returns control back to the
5740 simulator; the simulator then delivers the hardware
1777feb0 5741 equivalent of a SIGNAL_TRAP to the program being debugged. */
488f131b 5742
a493e3e2 5743 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
16c381f0 5744 && !signal_program[ecs->event_thread->suspend.stop_signal])
a493e3e2 5745 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
d4f3574e 5746
c4dbc9af 5747 discard_cleanups (old_cleanups);
2020b7ab 5748 resume (currently_stepping (ecs->event_thread),
16c381f0 5749 ecs->event_thread->suspend.stop_signal);
d4f3574e
SS
5750 }
5751
488f131b 5752 prepare_to_wait (ecs);
d4f3574e
SS
5753}
5754
104c1213
JM
5755/* This function normally comes after a resume, before
5756 handle_inferior_event exits. It takes care of any last bits of
5757 housekeeping, and sets the all-important wait_some_more flag. */
cd0fc7c3 5758
104c1213
JM
5759static void
5760prepare_to_wait (struct execution_control_state *ecs)
cd0fc7c3 5761{
527159b7 5762 if (debug_infrun)
8a9de0e4 5763 fprintf_unfiltered (gdb_stdlog, "infrun: prepare_to_wait\n");
104c1213 5764
104c1213
JM
5765 /* This is the old end of the while loop. Let everybody know we
5766 want to wait for the inferior some more and get called again
5767 soon. */
5768 ecs->wait_some_more = 1;
c906108c 5769}
11cf8741 5770
33d62d64
JK
5771/* Several print_*_reason functions to print why the inferior has stopped.
5772 We always print something when the inferior exits, or receives a signal.
5773 The rest of the cases are dealt with later on in normal_stop and
5774 print_it_typical. Ideally there should be a call to one of these
5775 print_*_reason functions functions from handle_inferior_event each time
5776 stop_stepping is called. */
5777
5778/* Print why the inferior has stopped.
5779 We are done with a step/next/si/ni command, print why the inferior has
5780 stopped. For now print nothing. Print a message only if not in the middle
5781 of doing a "step n" operation for n > 1. */
5782
5783static void
5784print_end_stepping_range_reason (void)
5785{
16c381f0
JK
5786 if ((!inferior_thread ()->step_multi
5787 || !inferior_thread ()->control.stop_step)
79a45e25
PA
5788 && ui_out_is_mi_like_p (current_uiout))
5789 ui_out_field_string (current_uiout, "reason",
33d62d64
JK
5790 async_reason_lookup (EXEC_ASYNC_END_STEPPING_RANGE));
5791}
5792
5793/* The inferior was terminated by a signal, print why it stopped. */
5794
11cf8741 5795static void
2ea28649 5796print_signal_exited_reason (enum gdb_signal siggnal)
11cf8741 5797{
79a45e25
PA
5798 struct ui_out *uiout = current_uiout;
5799
33d62d64
JK
5800 annotate_signalled ();
5801 if (ui_out_is_mi_like_p (uiout))
5802 ui_out_field_string
5803 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED));
5804 ui_out_text (uiout, "\nProgram terminated with signal ");
5805 annotate_signal_name ();
5806 ui_out_field_string (uiout, "signal-name",
2ea28649 5807 gdb_signal_to_name (siggnal));
33d62d64
JK
5808 annotate_signal_name_end ();
5809 ui_out_text (uiout, ", ");
5810 annotate_signal_string ();
5811 ui_out_field_string (uiout, "signal-meaning",
2ea28649 5812 gdb_signal_to_string (siggnal));
33d62d64
JK
5813 annotate_signal_string_end ();
5814 ui_out_text (uiout, ".\n");
5815 ui_out_text (uiout, "The program no longer exists.\n");
5816}
5817
5818/* The inferior program is finished, print why it stopped. */
5819
5820static void
5821print_exited_reason (int exitstatus)
5822{
fda326dd
TT
5823 struct inferior *inf = current_inferior ();
5824 const char *pidstr = target_pid_to_str (pid_to_ptid (inf->pid));
79a45e25 5825 struct ui_out *uiout = current_uiout;
fda326dd 5826
33d62d64
JK
5827 annotate_exited (exitstatus);
5828 if (exitstatus)
5829 {
5830 if (ui_out_is_mi_like_p (uiout))
5831 ui_out_field_string (uiout, "reason",
5832 async_reason_lookup (EXEC_ASYNC_EXITED));
fda326dd
TT
5833 ui_out_text (uiout, "[Inferior ");
5834 ui_out_text (uiout, plongest (inf->num));
5835 ui_out_text (uiout, " (");
5836 ui_out_text (uiout, pidstr);
5837 ui_out_text (uiout, ") exited with code ");
33d62d64 5838 ui_out_field_fmt (uiout, "exit-code", "0%o", (unsigned int) exitstatus);
fda326dd 5839 ui_out_text (uiout, "]\n");
33d62d64
JK
5840 }
5841 else
11cf8741 5842 {
9dc5e2a9 5843 if (ui_out_is_mi_like_p (uiout))
034dad6f 5844 ui_out_field_string
33d62d64 5845 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY));
fda326dd
TT
5846 ui_out_text (uiout, "[Inferior ");
5847 ui_out_text (uiout, plongest (inf->num));
5848 ui_out_text (uiout, " (");
5849 ui_out_text (uiout, pidstr);
5850 ui_out_text (uiout, ") exited normally]\n");
33d62d64
JK
5851 }
5852 /* Support the --return-child-result option. */
5853 return_child_result_value = exitstatus;
5854}
5855
5856/* Signal received, print why the inferior has stopped. The signal table
1777feb0 5857 tells us to print about it. */
33d62d64
JK
5858
5859static void
2ea28649 5860print_signal_received_reason (enum gdb_signal siggnal)
33d62d64 5861{
79a45e25
PA
5862 struct ui_out *uiout = current_uiout;
5863
33d62d64
JK
5864 annotate_signal ();
5865
a493e3e2 5866 if (siggnal == GDB_SIGNAL_0 && !ui_out_is_mi_like_p (uiout))
33d62d64
JK
5867 {
5868 struct thread_info *t = inferior_thread ();
5869
5870 ui_out_text (uiout, "\n[");
5871 ui_out_field_string (uiout, "thread-name",
5872 target_pid_to_str (t->ptid));
5873 ui_out_field_fmt (uiout, "thread-id", "] #%d", t->num);
5874 ui_out_text (uiout, " stopped");
5875 }
5876 else
5877 {
5878 ui_out_text (uiout, "\nProgram received signal ");
8b93c638 5879 annotate_signal_name ();
33d62d64
JK
5880 if (ui_out_is_mi_like_p (uiout))
5881 ui_out_field_string
5882 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED));
488f131b 5883 ui_out_field_string (uiout, "signal-name",
2ea28649 5884 gdb_signal_to_name (siggnal));
8b93c638
JM
5885 annotate_signal_name_end ();
5886 ui_out_text (uiout, ", ");
5887 annotate_signal_string ();
488f131b 5888 ui_out_field_string (uiout, "signal-meaning",
2ea28649 5889 gdb_signal_to_string (siggnal));
8b93c638 5890 annotate_signal_string_end ();
33d62d64
JK
5891 }
5892 ui_out_text (uiout, ".\n");
5893}
252fbfc8 5894
33d62d64
JK
5895/* Reverse execution: target ran out of history info, print why the inferior
5896 has stopped. */
252fbfc8 5897
33d62d64
JK
5898static void
5899print_no_history_reason (void)
5900{
79a45e25 5901 ui_out_text (current_uiout, "\nNo more reverse-execution history.\n");
11cf8741 5902}
43ff13b4 5903
c906108c
SS
5904/* Here to return control to GDB when the inferior stops for real.
5905 Print appropriate messages, remove breakpoints, give terminal our modes.
5906
5907 STOP_PRINT_FRAME nonzero means print the executing frame
5908 (pc, function, args, file, line number and line text).
5909 BREAKPOINTS_FAILED nonzero means stop was due to error
5910 attempting to insert breakpoints. */
5911
5912void
96baa820 5913normal_stop (void)
c906108c 5914{
73b65bb0
DJ
5915 struct target_waitstatus last;
5916 ptid_t last_ptid;
29f49a6a 5917 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
73b65bb0
DJ
5918
5919 get_last_target_status (&last_ptid, &last);
5920
29f49a6a
PA
5921 /* If an exception is thrown from this point on, make sure to
5922 propagate GDB's knowledge of the executing state to the
5923 frontend/user running state. A QUIT is an easy exception to see
5924 here, so do this before any filtered output. */
c35b1492
PA
5925 if (!non_stop)
5926 make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
5927 else if (last.kind != TARGET_WAITKIND_SIGNALLED
0e5bf2a8
PA
5928 && last.kind != TARGET_WAITKIND_EXITED
5929 && last.kind != TARGET_WAITKIND_NO_RESUMED)
c35b1492 5930 make_cleanup (finish_thread_state_cleanup, &inferior_ptid);
29f49a6a 5931
4f8d22e3
PA
5932 /* In non-stop mode, we don't want GDB to switch threads behind the
5933 user's back, to avoid races where the user is typing a command to
5934 apply to thread x, but GDB switches to thread y before the user
5935 finishes entering the command. */
5936
c906108c
SS
5937 /* As with the notification of thread events, we want to delay
5938 notifying the user that we've switched thread context until
5939 the inferior actually stops.
5940
73b65bb0
DJ
5941 There's no point in saying anything if the inferior has exited.
5942 Note that SIGNALLED here means "exited with a signal", not
5943 "received a signal". */
4f8d22e3
PA
5944 if (!non_stop
5945 && !ptid_equal (previous_inferior_ptid, inferior_ptid)
73b65bb0
DJ
5946 && target_has_execution
5947 && last.kind != TARGET_WAITKIND_SIGNALLED
0e5bf2a8
PA
5948 && last.kind != TARGET_WAITKIND_EXITED
5949 && last.kind != TARGET_WAITKIND_NO_RESUMED)
c906108c
SS
5950 {
5951 target_terminal_ours_for_output ();
a3f17187 5952 printf_filtered (_("[Switching to %s]\n"),
c95310c6 5953 target_pid_to_str (inferior_ptid));
b8fa951a 5954 annotate_thread_changed ();
39f77062 5955 previous_inferior_ptid = inferior_ptid;
c906108c 5956 }
c906108c 5957
0e5bf2a8
PA
5958 if (last.kind == TARGET_WAITKIND_NO_RESUMED)
5959 {
5960 gdb_assert (sync_execution || !target_can_async_p ());
5961
5962 target_terminal_ours_for_output ();
5963 printf_filtered (_("No unwaited-for children left.\n"));
5964 }
5965
74960c60 5966 if (!breakpoints_always_inserted_mode () && target_has_execution)
c906108c
SS
5967 {
5968 if (remove_breakpoints ())
5969 {
5970 target_terminal_ours_for_output ();
3e43a32a
MS
5971 printf_filtered (_("Cannot remove breakpoints because "
5972 "program is no longer writable.\nFurther "
5973 "execution is probably impossible.\n"));
c906108c
SS
5974 }
5975 }
c906108c 5976
c906108c
SS
5977 /* If an auto-display called a function and that got a signal,
5978 delete that auto-display to avoid an infinite recursion. */
5979
5980 if (stopped_by_random_signal)
5981 disable_current_display ();
5982
5983 /* Don't print a message if in the middle of doing a "step n"
5984 operation for n > 1 */
af679fd0
PA
5985 if (target_has_execution
5986 && last.kind != TARGET_WAITKIND_SIGNALLED
5987 && last.kind != TARGET_WAITKIND_EXITED
5988 && inferior_thread ()->step_multi
16c381f0 5989 && inferior_thread ()->control.stop_step)
c906108c
SS
5990 goto done;
5991
5992 target_terminal_ours ();
0f641c01 5993 async_enable_stdin ();
c906108c 5994
7abfe014
DJ
5995 /* Set the current source location. This will also happen if we
5996 display the frame below, but the current SAL will be incorrect
5997 during a user hook-stop function. */
d729566a 5998 if (has_stack_frames () && !stop_stack_dummy)
7abfe014
DJ
5999 set_current_sal_from_frame (get_current_frame (), 1);
6000
dd7e2d2b
PA
6001 /* Let the user/frontend see the threads as stopped. */
6002 do_cleanups (old_chain);
6003
6004 /* Look up the hook_stop and run it (CLI internally handles problem
6005 of stop_command's pre-hook not existing). */
6006 if (stop_command)
6007 catch_errors (hook_stop_stub, stop_command,
6008 "Error while running hook_stop:\n", RETURN_MASK_ALL);
6009
d729566a 6010 if (!has_stack_frames ())
d51fd4c8 6011 goto done;
c906108c 6012
32400beb
PA
6013 if (last.kind == TARGET_WAITKIND_SIGNALLED
6014 || last.kind == TARGET_WAITKIND_EXITED)
6015 goto done;
6016
c906108c
SS
6017 /* Select innermost stack frame - i.e., current frame is frame 0,
6018 and current location is based on that.
6019 Don't do this on return from a stack dummy routine,
1777feb0 6020 or if the program has exited. */
c906108c
SS
6021
6022 if (!stop_stack_dummy)
6023 {
0f7d239c 6024 select_frame (get_current_frame ());
c906108c
SS
6025
6026 /* Print current location without a level number, if
c5aa993b
JM
6027 we have changed functions or hit a breakpoint.
6028 Print source line if we have one.
6029 bpstat_print() contains the logic deciding in detail
1777feb0 6030 what to print, based on the event(s) that just occurred. */
c906108c 6031
d01a8610
AS
6032 /* If --batch-silent is enabled then there's no need to print the current
6033 source location, and to try risks causing an error message about
6034 missing source files. */
6035 if (stop_print_frame && !batch_silent)
c906108c
SS
6036 {
6037 int bpstat_ret;
6038 int source_flag;
917317f4 6039 int do_frame_printing = 1;
347bddb7 6040 struct thread_info *tp = inferior_thread ();
c906108c 6041
36dfb11c 6042 bpstat_ret = bpstat_print (tp->control.stop_bpstat, last.kind);
917317f4
JM
6043 switch (bpstat_ret)
6044 {
6045 case PRINT_UNKNOWN:
aa0cd9c1 6046 /* FIXME: cagney/2002-12-01: Given that a frame ID does
8fb3e588
AC
6047 (or should) carry around the function and does (or
6048 should) use that when doing a frame comparison. */
16c381f0
JK
6049 if (tp->control.stop_step
6050 && frame_id_eq (tp->control.step_frame_id,
aa0cd9c1 6051 get_frame_id (get_current_frame ()))
917317f4 6052 && step_start_function == find_pc_function (stop_pc))
1777feb0
MS
6053 source_flag = SRC_LINE; /* Finished step, just
6054 print source line. */
917317f4 6055 else
1777feb0
MS
6056 source_flag = SRC_AND_LOC; /* Print location and
6057 source line. */
917317f4
JM
6058 break;
6059 case PRINT_SRC_AND_LOC:
1777feb0
MS
6060 source_flag = SRC_AND_LOC; /* Print location and
6061 source line. */
917317f4
JM
6062 break;
6063 case PRINT_SRC_ONLY:
c5394b80 6064 source_flag = SRC_LINE;
917317f4
JM
6065 break;
6066 case PRINT_NOTHING:
488f131b 6067 source_flag = SRC_LINE; /* something bogus */
917317f4
JM
6068 do_frame_printing = 0;
6069 break;
6070 default:
e2e0b3e5 6071 internal_error (__FILE__, __LINE__, _("Unknown value."));
917317f4 6072 }
c906108c
SS
6073
6074 /* The behavior of this routine with respect to the source
6075 flag is:
c5394b80
JM
6076 SRC_LINE: Print only source line
6077 LOCATION: Print only location
1777feb0 6078 SRC_AND_LOC: Print location and source line. */
917317f4 6079 if (do_frame_printing)
b04f3ab4 6080 print_stack_frame (get_selected_frame (NULL), 0, source_flag);
c906108c
SS
6081
6082 /* Display the auto-display expressions. */
6083 do_displays ();
6084 }
6085 }
6086
6087 /* Save the function value return registers, if we care.
6088 We might be about to restore their previous contents. */
9da8c2a0
PA
6089 if (inferior_thread ()->control.proceed_to_finish
6090 && execution_direction != EXEC_REVERSE)
d5c31457
UW
6091 {
6092 /* This should not be necessary. */
6093 if (stop_registers)
6094 regcache_xfree (stop_registers);
6095
6096 /* NB: The copy goes through to the target picking up the value of
6097 all the registers. */
6098 stop_registers = regcache_dup (get_current_regcache ());
6099 }
c906108c 6100
aa7d318d 6101 if (stop_stack_dummy == STOP_STACK_DUMMY)
c906108c 6102 {
b89667eb
DE
6103 /* Pop the empty frame that contains the stack dummy.
6104 This also restores inferior state prior to the call
16c381f0 6105 (struct infcall_suspend_state). */
b89667eb 6106 struct frame_info *frame = get_current_frame ();
abbb1732 6107
b89667eb
DE
6108 gdb_assert (get_frame_type (frame) == DUMMY_FRAME);
6109 frame_pop (frame);
3e43a32a
MS
6110 /* frame_pop() calls reinit_frame_cache as the last thing it
6111 does which means there's currently no selected frame. We
6112 don't need to re-establish a selected frame if the dummy call
6113 returns normally, that will be done by
6114 restore_infcall_control_state. However, we do have to handle
6115 the case where the dummy call is returning after being
6116 stopped (e.g. the dummy call previously hit a breakpoint).
6117 We can't know which case we have so just always re-establish
6118 a selected frame here. */
0f7d239c 6119 select_frame (get_current_frame ());
c906108c
SS
6120 }
6121
c906108c
SS
6122done:
6123 annotate_stopped ();
41d2bdb4
PA
6124
6125 /* Suppress the stop observer if we're in the middle of:
6126
6127 - a step n (n > 1), as there still more steps to be done.
6128
6129 - a "finish" command, as the observer will be called in
6130 finish_command_continuation, so it can include the inferior
6131 function's return value.
6132
6133 - calling an inferior function, as we pretend we inferior didn't
6134 run at all. The return value of the call is handled by the
6135 expression evaluator, through call_function_by_hand. */
6136
6137 if (!target_has_execution
6138 || last.kind == TARGET_WAITKIND_SIGNALLED
6139 || last.kind == TARGET_WAITKIND_EXITED
0e5bf2a8 6140 || last.kind == TARGET_WAITKIND_NO_RESUMED
2ca0b532
PA
6141 || (!(inferior_thread ()->step_multi
6142 && inferior_thread ()->control.stop_step)
16c381f0
JK
6143 && !(inferior_thread ()->control.stop_bpstat
6144 && inferior_thread ()->control.proceed_to_finish)
6145 && !inferior_thread ()->control.in_infcall))
347bddb7
PA
6146 {
6147 if (!ptid_equal (inferior_ptid, null_ptid))
16c381f0 6148 observer_notify_normal_stop (inferior_thread ()->control.stop_bpstat,
1d33d6ba 6149 stop_print_frame);
347bddb7 6150 else
1d33d6ba 6151 observer_notify_normal_stop (NULL, stop_print_frame);
347bddb7 6152 }
347bddb7 6153
48844aa6
PA
6154 if (target_has_execution)
6155 {
6156 if (last.kind != TARGET_WAITKIND_SIGNALLED
6157 && last.kind != TARGET_WAITKIND_EXITED)
6158 /* Delete the breakpoint we stopped at, if it wants to be deleted.
6159 Delete any breakpoint that is to be deleted at the next stop. */
16c381f0 6160 breakpoint_auto_delete (inferior_thread ()->control.stop_bpstat);
94cc34af 6161 }
6c95b8df
PA
6162
6163 /* Try to get rid of automatically added inferiors that are no
6164 longer needed. Keeping those around slows down things linearly.
6165 Note that this never removes the current inferior. */
6166 prune_inferiors ();
c906108c
SS
6167}
6168
6169static int
96baa820 6170hook_stop_stub (void *cmd)
c906108c 6171{
5913bcb0 6172 execute_cmd_pre_hook ((struct cmd_list_element *) cmd);
c906108c
SS
6173 return (0);
6174}
6175\f
c5aa993b 6176int
96baa820 6177signal_stop_state (int signo)
c906108c 6178{
d6b48e9c 6179 return signal_stop[signo];
c906108c
SS
6180}
6181
c5aa993b 6182int
96baa820 6183signal_print_state (int signo)
c906108c
SS
6184{
6185 return signal_print[signo];
6186}
6187
c5aa993b 6188int
96baa820 6189signal_pass_state (int signo)
c906108c
SS
6190{
6191 return signal_program[signo];
6192}
6193
2455069d
UW
6194static void
6195signal_cache_update (int signo)
6196{
6197 if (signo == -1)
6198 {
a493e3e2 6199 for (signo = 0; signo < (int) GDB_SIGNAL_LAST; signo++)
2455069d
UW
6200 signal_cache_update (signo);
6201
6202 return;
6203 }
6204
6205 signal_pass[signo] = (signal_stop[signo] == 0
6206 && signal_print[signo] == 0
6207 && signal_program[signo] == 1);
6208}
6209
488f131b 6210int
7bda5e4a 6211signal_stop_update (int signo, int state)
d4f3574e
SS
6212{
6213 int ret = signal_stop[signo];
abbb1732 6214
d4f3574e 6215 signal_stop[signo] = state;
2455069d 6216 signal_cache_update (signo);
d4f3574e
SS
6217 return ret;
6218}
6219
488f131b 6220int
7bda5e4a 6221signal_print_update (int signo, int state)
d4f3574e
SS
6222{
6223 int ret = signal_print[signo];
abbb1732 6224
d4f3574e 6225 signal_print[signo] = state;
2455069d 6226 signal_cache_update (signo);
d4f3574e
SS
6227 return ret;
6228}
6229
488f131b 6230int
7bda5e4a 6231signal_pass_update (int signo, int state)
d4f3574e
SS
6232{
6233 int ret = signal_program[signo];
abbb1732 6234
d4f3574e 6235 signal_program[signo] = state;
2455069d 6236 signal_cache_update (signo);
d4f3574e
SS
6237 return ret;
6238}
6239
c906108c 6240static void
96baa820 6241sig_print_header (void)
c906108c 6242{
3e43a32a
MS
6243 printf_filtered (_("Signal Stop\tPrint\tPass "
6244 "to program\tDescription\n"));
c906108c
SS
6245}
6246
6247static void
2ea28649 6248sig_print_info (enum gdb_signal oursig)
c906108c 6249{
2ea28649 6250 const char *name = gdb_signal_to_name (oursig);
c906108c 6251 int name_padding = 13 - strlen (name);
96baa820 6252
c906108c
SS
6253 if (name_padding <= 0)
6254 name_padding = 0;
6255
6256 printf_filtered ("%s", name);
488f131b 6257 printf_filtered ("%*.*s ", name_padding, name_padding, " ");
c906108c
SS
6258 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
6259 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
6260 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
2ea28649 6261 printf_filtered ("%s\n", gdb_signal_to_string (oursig));
c906108c
SS
6262}
6263
6264/* Specify how various signals in the inferior should be handled. */
6265
6266static void
96baa820 6267handle_command (char *args, int from_tty)
c906108c
SS
6268{
6269 char **argv;
6270 int digits, wordlen;
6271 int sigfirst, signum, siglast;
2ea28649 6272 enum gdb_signal oursig;
c906108c
SS
6273 int allsigs;
6274 int nsigs;
6275 unsigned char *sigs;
6276 struct cleanup *old_chain;
6277
6278 if (args == NULL)
6279 {
e2e0b3e5 6280 error_no_arg (_("signal to handle"));
c906108c
SS
6281 }
6282
1777feb0 6283 /* Allocate and zero an array of flags for which signals to handle. */
c906108c 6284
a493e3e2 6285 nsigs = (int) GDB_SIGNAL_LAST;
c906108c
SS
6286 sigs = (unsigned char *) alloca (nsigs);
6287 memset (sigs, 0, nsigs);
6288
1777feb0 6289 /* Break the command line up into args. */
c906108c 6290
d1a41061 6291 argv = gdb_buildargv (args);
7a292a7a 6292 old_chain = make_cleanup_freeargv (argv);
c906108c
SS
6293
6294 /* Walk through the args, looking for signal oursigs, signal names, and
6295 actions. Signal numbers and signal names may be interspersed with
6296 actions, with the actions being performed for all signals cumulatively
1777feb0 6297 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
c906108c
SS
6298
6299 while (*argv != NULL)
6300 {
6301 wordlen = strlen (*argv);
6302 for (digits = 0; isdigit ((*argv)[digits]); digits++)
6303 {;
6304 }
6305 allsigs = 0;
6306 sigfirst = siglast = -1;
6307
6308 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
6309 {
6310 /* Apply action to all signals except those used by the
1777feb0 6311 debugger. Silently skip those. */
c906108c
SS
6312 allsigs = 1;
6313 sigfirst = 0;
6314 siglast = nsigs - 1;
6315 }
6316 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
6317 {
6318 SET_SIGS (nsigs, sigs, signal_stop);
6319 SET_SIGS (nsigs, sigs, signal_print);
6320 }
6321 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
6322 {
6323 UNSET_SIGS (nsigs, sigs, signal_program);
6324 }
6325 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
6326 {
6327 SET_SIGS (nsigs, sigs, signal_print);
6328 }
6329 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
6330 {
6331 SET_SIGS (nsigs, sigs, signal_program);
6332 }
6333 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
6334 {
6335 UNSET_SIGS (nsigs, sigs, signal_stop);
6336 }
6337 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
6338 {
6339 SET_SIGS (nsigs, sigs, signal_program);
6340 }
6341 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
6342 {
6343 UNSET_SIGS (nsigs, sigs, signal_print);
6344 UNSET_SIGS (nsigs, sigs, signal_stop);
6345 }
6346 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
6347 {
6348 UNSET_SIGS (nsigs, sigs, signal_program);
6349 }
6350 else if (digits > 0)
6351 {
6352 /* It is numeric. The numeric signal refers to our own
6353 internal signal numbering from target.h, not to host/target
6354 signal number. This is a feature; users really should be
6355 using symbolic names anyway, and the common ones like
6356 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
6357
6358 sigfirst = siglast = (int)
2ea28649 6359 gdb_signal_from_command (atoi (*argv));
c906108c
SS
6360 if ((*argv)[digits] == '-')
6361 {
6362 siglast = (int)
2ea28649 6363 gdb_signal_from_command (atoi ((*argv) + digits + 1));
c906108c
SS
6364 }
6365 if (sigfirst > siglast)
6366 {
1777feb0 6367 /* Bet he didn't figure we'd think of this case... */
c906108c
SS
6368 signum = sigfirst;
6369 sigfirst = siglast;
6370 siglast = signum;
6371 }
6372 }
6373 else
6374 {
2ea28649 6375 oursig = gdb_signal_from_name (*argv);
a493e3e2 6376 if (oursig != GDB_SIGNAL_UNKNOWN)
c906108c
SS
6377 {
6378 sigfirst = siglast = (int) oursig;
6379 }
6380 else
6381 {
6382 /* Not a number and not a recognized flag word => complain. */
8a3fe4f8 6383 error (_("Unrecognized or ambiguous flag word: \"%s\"."), *argv);
c906108c
SS
6384 }
6385 }
6386
6387 /* If any signal numbers or symbol names were found, set flags for
1777feb0 6388 which signals to apply actions to. */
c906108c
SS
6389
6390 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
6391 {
2ea28649 6392 switch ((enum gdb_signal) signum)
c906108c 6393 {
a493e3e2
PA
6394 case GDB_SIGNAL_TRAP:
6395 case GDB_SIGNAL_INT:
c906108c
SS
6396 if (!allsigs && !sigs[signum])
6397 {
9e2f0ad4 6398 if (query (_("%s is used by the debugger.\n\
3e43a32a 6399Are you sure you want to change it? "),
2ea28649 6400 gdb_signal_to_name ((enum gdb_signal) signum)))
c906108c
SS
6401 {
6402 sigs[signum] = 1;
6403 }
6404 else
6405 {
a3f17187 6406 printf_unfiltered (_("Not confirmed, unchanged.\n"));
c906108c
SS
6407 gdb_flush (gdb_stdout);
6408 }
6409 }
6410 break;
a493e3e2
PA
6411 case GDB_SIGNAL_0:
6412 case GDB_SIGNAL_DEFAULT:
6413 case GDB_SIGNAL_UNKNOWN:
c906108c
SS
6414 /* Make sure that "all" doesn't print these. */
6415 break;
6416 default:
6417 sigs[signum] = 1;
6418 break;
6419 }
6420 }
6421
6422 argv++;
6423 }
6424
3a031f65
PA
6425 for (signum = 0; signum < nsigs; signum++)
6426 if (sigs[signum])
6427 {
2455069d 6428 signal_cache_update (-1);
a493e3e2
PA
6429 target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
6430 target_program_signals ((int) GDB_SIGNAL_LAST, signal_program);
c906108c 6431
3a031f65
PA
6432 if (from_tty)
6433 {
6434 /* Show the results. */
6435 sig_print_header ();
6436 for (; signum < nsigs; signum++)
6437 if (sigs[signum])
6438 sig_print_info (signum);
6439 }
6440
6441 break;
6442 }
c906108c
SS
6443
6444 do_cleanups (old_chain);
6445}
6446
de0bea00
MF
6447/* Complete the "handle" command. */
6448
6449static VEC (char_ptr) *
6450handle_completer (struct cmd_list_element *ignore,
6451 char *text, char *word)
6452{
6453 VEC (char_ptr) *vec_signals, *vec_keywords, *return_val;
6454 static const char * const keywords[] =
6455 {
6456 "all",
6457 "stop",
6458 "ignore",
6459 "print",
6460 "pass",
6461 "nostop",
6462 "noignore",
6463 "noprint",
6464 "nopass",
6465 NULL,
6466 };
6467
6468 vec_signals = signal_completer (ignore, text, word);
6469 vec_keywords = complete_on_enum (keywords, word, word);
6470
6471 return_val = VEC_merge (char_ptr, vec_signals, vec_keywords);
6472 VEC_free (char_ptr, vec_signals);
6473 VEC_free (char_ptr, vec_keywords);
6474 return return_val;
6475}
6476
c906108c 6477static void
96baa820 6478xdb_handle_command (char *args, int from_tty)
c906108c
SS
6479{
6480 char **argv;
6481 struct cleanup *old_chain;
6482
d1a41061
PP
6483 if (args == NULL)
6484 error_no_arg (_("xdb command"));
6485
1777feb0 6486 /* Break the command line up into args. */
c906108c 6487
d1a41061 6488 argv = gdb_buildargv (args);
7a292a7a 6489 old_chain = make_cleanup_freeargv (argv);
c906108c
SS
6490 if (argv[1] != (char *) NULL)
6491 {
6492 char *argBuf;
6493 int bufLen;
6494
6495 bufLen = strlen (argv[0]) + 20;
6496 argBuf = (char *) xmalloc (bufLen);
6497 if (argBuf)
6498 {
6499 int validFlag = 1;
2ea28649 6500 enum gdb_signal oursig;
c906108c 6501
2ea28649 6502 oursig = gdb_signal_from_name (argv[0]);
c906108c
SS
6503 memset (argBuf, 0, bufLen);
6504 if (strcmp (argv[1], "Q") == 0)
6505 sprintf (argBuf, "%s %s", argv[0], "noprint");
6506 else
6507 {
6508 if (strcmp (argv[1], "s") == 0)
6509 {
6510 if (!signal_stop[oursig])
6511 sprintf (argBuf, "%s %s", argv[0], "stop");
6512 else
6513 sprintf (argBuf, "%s %s", argv[0], "nostop");
6514 }
6515 else if (strcmp (argv[1], "i") == 0)
6516 {
6517 if (!signal_program[oursig])
6518 sprintf (argBuf, "%s %s", argv[0], "pass");
6519 else
6520 sprintf (argBuf, "%s %s", argv[0], "nopass");
6521 }
6522 else if (strcmp (argv[1], "r") == 0)
6523 {
6524 if (!signal_print[oursig])
6525 sprintf (argBuf, "%s %s", argv[0], "print");
6526 else
6527 sprintf (argBuf, "%s %s", argv[0], "noprint");
6528 }
6529 else
6530 validFlag = 0;
6531 }
6532 if (validFlag)
6533 handle_command (argBuf, from_tty);
6534 else
a3f17187 6535 printf_filtered (_("Invalid signal handling flag.\n"));
c906108c 6536 if (argBuf)
b8c9b27d 6537 xfree (argBuf);
c906108c
SS
6538 }
6539 }
6540 do_cleanups (old_chain);
6541}
6542
2ea28649
PA
6543enum gdb_signal
6544gdb_signal_from_command (int num)
ed01b82c
PA
6545{
6546 if (num >= 1 && num <= 15)
2ea28649 6547 return (enum gdb_signal) num;
ed01b82c
PA
6548 error (_("Only signals 1-15 are valid as numeric signals.\n\
6549Use \"info signals\" for a list of symbolic signals."));
6550}
6551
c906108c
SS
6552/* Print current contents of the tables set by the handle command.
6553 It is possible we should just be printing signals actually used
6554 by the current target (but for things to work right when switching
6555 targets, all signals should be in the signal tables). */
6556
6557static void
96baa820 6558signals_info (char *signum_exp, int from_tty)
c906108c 6559{
2ea28649 6560 enum gdb_signal oursig;
abbb1732 6561
c906108c
SS
6562 sig_print_header ();
6563
6564 if (signum_exp)
6565 {
6566 /* First see if this is a symbol name. */
2ea28649 6567 oursig = gdb_signal_from_name (signum_exp);
a493e3e2 6568 if (oursig == GDB_SIGNAL_UNKNOWN)
c906108c
SS
6569 {
6570 /* No, try numeric. */
6571 oursig =
2ea28649 6572 gdb_signal_from_command (parse_and_eval_long (signum_exp));
c906108c
SS
6573 }
6574 sig_print_info (oursig);
6575 return;
6576 }
6577
6578 printf_filtered ("\n");
6579 /* These ugly casts brought to you by the native VAX compiler. */
a493e3e2
PA
6580 for (oursig = GDB_SIGNAL_FIRST;
6581 (int) oursig < (int) GDB_SIGNAL_LAST;
2ea28649 6582 oursig = (enum gdb_signal) ((int) oursig + 1))
c906108c
SS
6583 {
6584 QUIT;
6585
a493e3e2
PA
6586 if (oursig != GDB_SIGNAL_UNKNOWN
6587 && oursig != GDB_SIGNAL_DEFAULT && oursig != GDB_SIGNAL_0)
c906108c
SS
6588 sig_print_info (oursig);
6589 }
6590
3e43a32a
MS
6591 printf_filtered (_("\nUse the \"handle\" command "
6592 "to change these tables.\n"));
c906108c 6593}
4aa995e1 6594
c709acd1
PA
6595/* Check if it makes sense to read $_siginfo from the current thread
6596 at this point. If not, throw an error. */
6597
6598static void
6599validate_siginfo_access (void)
6600{
6601 /* No current inferior, no siginfo. */
6602 if (ptid_equal (inferior_ptid, null_ptid))
6603 error (_("No thread selected."));
6604
6605 /* Don't try to read from a dead thread. */
6606 if (is_exited (inferior_ptid))
6607 error (_("The current thread has terminated"));
6608
6609 /* ... or from a spinning thread. */
6610 if (is_running (inferior_ptid))
6611 error (_("Selected thread is running."));
6612}
6613
4aa995e1
PA
6614/* The $_siginfo convenience variable is a bit special. We don't know
6615 for sure the type of the value until we actually have a chance to
7a9dd1b2 6616 fetch the data. The type can change depending on gdbarch, so it is
4aa995e1
PA
6617 also dependent on which thread you have selected.
6618
6619 1. making $_siginfo be an internalvar that creates a new value on
6620 access.
6621
6622 2. making the value of $_siginfo be an lval_computed value. */
6623
6624/* This function implements the lval_computed support for reading a
6625 $_siginfo value. */
6626
6627static void
6628siginfo_value_read (struct value *v)
6629{
6630 LONGEST transferred;
6631
c709acd1
PA
6632 validate_siginfo_access ();
6633
4aa995e1
PA
6634 transferred =
6635 target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO,
6636 NULL,
6637 value_contents_all_raw (v),
6638 value_offset (v),
6639 TYPE_LENGTH (value_type (v)));
6640
6641 if (transferred != TYPE_LENGTH (value_type (v)))
6642 error (_("Unable to read siginfo"));
6643}
6644
6645/* This function implements the lval_computed support for writing a
6646 $_siginfo value. */
6647
6648static void
6649siginfo_value_write (struct value *v, struct value *fromval)
6650{
6651 LONGEST transferred;
6652
c709acd1
PA
6653 validate_siginfo_access ();
6654
4aa995e1
PA
6655 transferred = target_write (&current_target,
6656 TARGET_OBJECT_SIGNAL_INFO,
6657 NULL,
6658 value_contents_all_raw (fromval),
6659 value_offset (v),
6660 TYPE_LENGTH (value_type (fromval)));
6661
6662 if (transferred != TYPE_LENGTH (value_type (fromval)))
6663 error (_("Unable to write siginfo"));
6664}
6665
c8f2448a 6666static const struct lval_funcs siginfo_value_funcs =
4aa995e1
PA
6667 {
6668 siginfo_value_read,
6669 siginfo_value_write
6670 };
6671
6672/* Return a new value with the correct type for the siginfo object of
78267919
UW
6673 the current thread using architecture GDBARCH. Return a void value
6674 if there's no object available. */
4aa995e1 6675
2c0b251b 6676static struct value *
22d2b532
SDJ
6677siginfo_make_value (struct gdbarch *gdbarch, struct internalvar *var,
6678 void *ignore)
4aa995e1 6679{
4aa995e1 6680 if (target_has_stack
78267919
UW
6681 && !ptid_equal (inferior_ptid, null_ptid)
6682 && gdbarch_get_siginfo_type_p (gdbarch))
4aa995e1 6683 {
78267919 6684 struct type *type = gdbarch_get_siginfo_type (gdbarch);
abbb1732 6685
78267919 6686 return allocate_computed_value (type, &siginfo_value_funcs, NULL);
4aa995e1
PA
6687 }
6688
78267919 6689 return allocate_value (builtin_type (gdbarch)->builtin_void);
4aa995e1
PA
6690}
6691
c906108c 6692\f
16c381f0
JK
6693/* infcall_suspend_state contains state about the program itself like its
6694 registers and any signal it received when it last stopped.
6695 This state must be restored regardless of how the inferior function call
6696 ends (either successfully, or after it hits a breakpoint or signal)
6697 if the program is to properly continue where it left off. */
6698
6699struct infcall_suspend_state
7a292a7a 6700{
16c381f0 6701 struct thread_suspend_state thread_suspend;
dd80ea3c 6702#if 0 /* Currently unused and empty structures are not valid C. */
16c381f0 6703 struct inferior_suspend_state inferior_suspend;
dd80ea3c 6704#endif
16c381f0
JK
6705
6706 /* Other fields: */
7a292a7a 6707 CORE_ADDR stop_pc;
b89667eb 6708 struct regcache *registers;
1736ad11 6709
35515841 6710 /* Format of SIGINFO_DATA or NULL if it is not present. */
1736ad11
JK
6711 struct gdbarch *siginfo_gdbarch;
6712
6713 /* The inferior format depends on SIGINFO_GDBARCH and it has a length of
6714 TYPE_LENGTH (gdbarch_get_siginfo_type ()). For different gdbarch the
6715 content would be invalid. */
6716 gdb_byte *siginfo_data;
b89667eb
DE
6717};
6718
16c381f0
JK
6719struct infcall_suspend_state *
6720save_infcall_suspend_state (void)
b89667eb 6721{
16c381f0 6722 struct infcall_suspend_state *inf_state;
b89667eb 6723 struct thread_info *tp = inferior_thread ();
16c381f0 6724 struct inferior *inf = current_inferior ();
1736ad11
JK
6725 struct regcache *regcache = get_current_regcache ();
6726 struct gdbarch *gdbarch = get_regcache_arch (regcache);
6727 gdb_byte *siginfo_data = NULL;
6728
6729 if (gdbarch_get_siginfo_type_p (gdbarch))
6730 {
6731 struct type *type = gdbarch_get_siginfo_type (gdbarch);
6732 size_t len = TYPE_LENGTH (type);
6733 struct cleanup *back_to;
6734
6735 siginfo_data = xmalloc (len);
6736 back_to = make_cleanup (xfree, siginfo_data);
6737
6738 if (target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
6739 siginfo_data, 0, len) == len)
6740 discard_cleanups (back_to);
6741 else
6742 {
6743 /* Errors ignored. */
6744 do_cleanups (back_to);
6745 siginfo_data = NULL;
6746 }
6747 }
6748
16c381f0 6749 inf_state = XZALLOC (struct infcall_suspend_state);
1736ad11
JK
6750
6751 if (siginfo_data)
6752 {
6753 inf_state->siginfo_gdbarch = gdbarch;
6754 inf_state->siginfo_data = siginfo_data;
6755 }
b89667eb 6756
16c381f0 6757 inf_state->thread_suspend = tp->suspend;
dd80ea3c 6758#if 0 /* Currently unused and empty structures are not valid C. */
16c381f0 6759 inf_state->inferior_suspend = inf->suspend;
dd80ea3c 6760#endif
16c381f0 6761
35515841 6762 /* run_inferior_call will not use the signal due to its `proceed' call with
a493e3e2
PA
6763 GDB_SIGNAL_0 anyway. */
6764 tp->suspend.stop_signal = GDB_SIGNAL_0;
35515841 6765
b89667eb
DE
6766 inf_state->stop_pc = stop_pc;
6767
1736ad11 6768 inf_state->registers = regcache_dup (regcache);
b89667eb
DE
6769
6770 return inf_state;
6771}
6772
6773/* Restore inferior session state to INF_STATE. */
6774
6775void
16c381f0 6776restore_infcall_suspend_state (struct infcall_suspend_state *inf_state)
b89667eb
DE
6777{
6778 struct thread_info *tp = inferior_thread ();
16c381f0 6779 struct inferior *inf = current_inferior ();
1736ad11
JK
6780 struct regcache *regcache = get_current_regcache ();
6781 struct gdbarch *gdbarch = get_regcache_arch (regcache);
b89667eb 6782
16c381f0 6783 tp->suspend = inf_state->thread_suspend;
dd80ea3c 6784#if 0 /* Currently unused and empty structures are not valid C. */
16c381f0 6785 inf->suspend = inf_state->inferior_suspend;
dd80ea3c 6786#endif
16c381f0 6787
b89667eb
DE
6788 stop_pc = inf_state->stop_pc;
6789
1736ad11
JK
6790 if (inf_state->siginfo_gdbarch == gdbarch)
6791 {
6792 struct type *type = gdbarch_get_siginfo_type (gdbarch);
1736ad11
JK
6793
6794 /* Errors ignored. */
6795 target_write (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
6acef6cd 6796 inf_state->siginfo_data, 0, TYPE_LENGTH (type));
1736ad11
JK
6797 }
6798
b89667eb
DE
6799 /* The inferior can be gone if the user types "print exit(0)"
6800 (and perhaps other times). */
6801 if (target_has_execution)
6802 /* NB: The register write goes through to the target. */
1736ad11 6803 regcache_cpy (regcache, inf_state->registers);
803b5f95 6804
16c381f0 6805 discard_infcall_suspend_state (inf_state);
b89667eb
DE
6806}
6807
6808static void
16c381f0 6809do_restore_infcall_suspend_state_cleanup (void *state)
b89667eb 6810{
16c381f0 6811 restore_infcall_suspend_state (state);
b89667eb
DE
6812}
6813
6814struct cleanup *
16c381f0
JK
6815make_cleanup_restore_infcall_suspend_state
6816 (struct infcall_suspend_state *inf_state)
b89667eb 6817{
16c381f0 6818 return make_cleanup (do_restore_infcall_suspend_state_cleanup, inf_state);
b89667eb
DE
6819}
6820
6821void
16c381f0 6822discard_infcall_suspend_state (struct infcall_suspend_state *inf_state)
b89667eb
DE
6823{
6824 regcache_xfree (inf_state->registers);
803b5f95 6825 xfree (inf_state->siginfo_data);
b89667eb
DE
6826 xfree (inf_state);
6827}
6828
6829struct regcache *
16c381f0 6830get_infcall_suspend_state_regcache (struct infcall_suspend_state *inf_state)
b89667eb
DE
6831{
6832 return inf_state->registers;
6833}
6834
16c381f0
JK
6835/* infcall_control_state contains state regarding gdb's control of the
6836 inferior itself like stepping control. It also contains session state like
6837 the user's currently selected frame. */
b89667eb 6838
16c381f0 6839struct infcall_control_state
b89667eb 6840{
16c381f0
JK
6841 struct thread_control_state thread_control;
6842 struct inferior_control_state inferior_control;
d82142e2
JK
6843
6844 /* Other fields: */
6845 enum stop_stack_kind stop_stack_dummy;
6846 int stopped_by_random_signal;
7a292a7a 6847 int stop_after_trap;
7a292a7a 6848
b89667eb 6849 /* ID if the selected frame when the inferior function call was made. */
101dcfbe 6850 struct frame_id selected_frame_id;
7a292a7a
SS
6851};
6852
c906108c 6853/* Save all of the information associated with the inferior<==>gdb
b89667eb 6854 connection. */
c906108c 6855
16c381f0
JK
6856struct infcall_control_state *
6857save_infcall_control_state (void)
c906108c 6858{
16c381f0 6859 struct infcall_control_state *inf_status = xmalloc (sizeof (*inf_status));
4e1c45ea 6860 struct thread_info *tp = inferior_thread ();
d6b48e9c 6861 struct inferior *inf = current_inferior ();
7a292a7a 6862
16c381f0
JK
6863 inf_status->thread_control = tp->control;
6864 inf_status->inferior_control = inf->control;
d82142e2 6865
8358c15c 6866 tp->control.step_resume_breakpoint = NULL;
5b79abe7 6867 tp->control.exception_resume_breakpoint = NULL;
8358c15c 6868
16c381f0
JK
6869 /* Save original bpstat chain to INF_STATUS; replace it in TP with copy of
6870 chain. If caller's caller is walking the chain, they'll be happier if we
6871 hand them back the original chain when restore_infcall_control_state is
6872 called. */
6873 tp->control.stop_bpstat = bpstat_copy (tp->control.stop_bpstat);
d82142e2
JK
6874
6875 /* Other fields: */
6876 inf_status->stop_stack_dummy = stop_stack_dummy;
6877 inf_status->stopped_by_random_signal = stopped_by_random_signal;
6878 inf_status->stop_after_trap = stop_after_trap;
c5aa993b 6879
206415a3 6880 inf_status->selected_frame_id = get_frame_id (get_selected_frame (NULL));
b89667eb 6881
7a292a7a 6882 return inf_status;
c906108c
SS
6883}
6884
c906108c 6885static int
96baa820 6886restore_selected_frame (void *args)
c906108c 6887{
488f131b 6888 struct frame_id *fid = (struct frame_id *) args;
c906108c 6889 struct frame_info *frame;
c906108c 6890
101dcfbe 6891 frame = frame_find_by_id (*fid);
c906108c 6892
aa0cd9c1
AC
6893 /* If inf_status->selected_frame_id is NULL, there was no previously
6894 selected frame. */
101dcfbe 6895 if (frame == NULL)
c906108c 6896 {
8a3fe4f8 6897 warning (_("Unable to restore previously selected frame."));
c906108c
SS
6898 return 0;
6899 }
6900
0f7d239c 6901 select_frame (frame);
c906108c
SS
6902
6903 return (1);
6904}
6905
b89667eb
DE
6906/* Restore inferior session state to INF_STATUS. */
6907
c906108c 6908void
16c381f0 6909restore_infcall_control_state (struct infcall_control_state *inf_status)
c906108c 6910{
4e1c45ea 6911 struct thread_info *tp = inferior_thread ();
d6b48e9c 6912 struct inferior *inf = current_inferior ();
4e1c45ea 6913
8358c15c
JK
6914 if (tp->control.step_resume_breakpoint)
6915 tp->control.step_resume_breakpoint->disposition = disp_del_at_next_stop;
6916
5b79abe7
TT
6917 if (tp->control.exception_resume_breakpoint)
6918 tp->control.exception_resume_breakpoint->disposition
6919 = disp_del_at_next_stop;
6920
d82142e2 6921 /* Handle the bpstat_copy of the chain. */
16c381f0 6922 bpstat_clear (&tp->control.stop_bpstat);
d82142e2 6923
16c381f0
JK
6924 tp->control = inf_status->thread_control;
6925 inf->control = inf_status->inferior_control;
d82142e2
JK
6926
6927 /* Other fields: */
6928 stop_stack_dummy = inf_status->stop_stack_dummy;
6929 stopped_by_random_signal = inf_status->stopped_by_random_signal;
6930 stop_after_trap = inf_status->stop_after_trap;
c906108c 6931
b89667eb 6932 if (target_has_stack)
c906108c 6933 {
c906108c 6934 /* The point of catch_errors is that if the stack is clobbered,
101dcfbe
AC
6935 walking the stack might encounter a garbage pointer and
6936 error() trying to dereference it. */
488f131b
JB
6937 if (catch_errors
6938 (restore_selected_frame, &inf_status->selected_frame_id,
6939 "Unable to restore previously selected frame:\n",
6940 RETURN_MASK_ERROR) == 0)
c906108c
SS
6941 /* Error in restoring the selected frame. Select the innermost
6942 frame. */
0f7d239c 6943 select_frame (get_current_frame ());
c906108c 6944 }
c906108c 6945
72cec141 6946 xfree (inf_status);
7a292a7a 6947}
c906108c 6948
74b7792f 6949static void
16c381f0 6950do_restore_infcall_control_state_cleanup (void *sts)
74b7792f 6951{
16c381f0 6952 restore_infcall_control_state (sts);
74b7792f
AC
6953}
6954
6955struct cleanup *
16c381f0
JK
6956make_cleanup_restore_infcall_control_state
6957 (struct infcall_control_state *inf_status)
74b7792f 6958{
16c381f0 6959 return make_cleanup (do_restore_infcall_control_state_cleanup, inf_status);
74b7792f
AC
6960}
6961
c906108c 6962void
16c381f0 6963discard_infcall_control_state (struct infcall_control_state *inf_status)
7a292a7a 6964{
8358c15c
JK
6965 if (inf_status->thread_control.step_resume_breakpoint)
6966 inf_status->thread_control.step_resume_breakpoint->disposition
6967 = disp_del_at_next_stop;
6968
5b79abe7
TT
6969 if (inf_status->thread_control.exception_resume_breakpoint)
6970 inf_status->thread_control.exception_resume_breakpoint->disposition
6971 = disp_del_at_next_stop;
6972
1777feb0 6973 /* See save_infcall_control_state for info on stop_bpstat. */
16c381f0 6974 bpstat_clear (&inf_status->thread_control.stop_bpstat);
8358c15c 6975
72cec141 6976 xfree (inf_status);
7a292a7a 6977}
b89667eb 6978\f
0723dbf5
PA
6979int
6980ptid_match (ptid_t ptid, ptid_t filter)
6981{
0723dbf5
PA
6982 if (ptid_equal (filter, minus_one_ptid))
6983 return 1;
6984 if (ptid_is_pid (filter)
6985 && ptid_get_pid (ptid) == ptid_get_pid (filter))
6986 return 1;
6987 else if (ptid_equal (ptid, filter))
6988 return 1;
6989
6990 return 0;
6991}
6992
ca6724c1
KB
6993/* restore_inferior_ptid() will be used by the cleanup machinery
6994 to restore the inferior_ptid value saved in a call to
6995 save_inferior_ptid(). */
ce696e05
KB
6996
6997static void
6998restore_inferior_ptid (void *arg)
6999{
7000 ptid_t *saved_ptid_ptr = arg;
abbb1732 7001
ce696e05
KB
7002 inferior_ptid = *saved_ptid_ptr;
7003 xfree (arg);
7004}
7005
7006/* Save the value of inferior_ptid so that it may be restored by a
7007 later call to do_cleanups(). Returns the struct cleanup pointer
7008 needed for later doing the cleanup. */
7009
7010struct cleanup *
7011save_inferior_ptid (void)
7012{
7013 ptid_t *saved_ptid_ptr;
7014
7015 saved_ptid_ptr = xmalloc (sizeof (ptid_t));
7016 *saved_ptid_ptr = inferior_ptid;
7017 return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
7018}
c5aa993b 7019\f
488f131b 7020
b2175913
MS
7021/* User interface for reverse debugging:
7022 Set exec-direction / show exec-direction commands
7023 (returns error unless target implements to_set_exec_direction method). */
7024
32231432 7025int execution_direction = EXEC_FORWARD;
b2175913
MS
7026static const char exec_forward[] = "forward";
7027static const char exec_reverse[] = "reverse";
7028static const char *exec_direction = exec_forward;
40478521 7029static const char *const exec_direction_names[] = {
b2175913
MS
7030 exec_forward,
7031 exec_reverse,
7032 NULL
7033};
7034
7035static void
7036set_exec_direction_func (char *args, int from_tty,
7037 struct cmd_list_element *cmd)
7038{
7039 if (target_can_execute_reverse)
7040 {
7041 if (!strcmp (exec_direction, exec_forward))
7042 execution_direction = EXEC_FORWARD;
7043 else if (!strcmp (exec_direction, exec_reverse))
7044 execution_direction = EXEC_REVERSE;
7045 }
8bbed405
MS
7046 else
7047 {
7048 exec_direction = exec_forward;
7049 error (_("Target does not support this operation."));
7050 }
b2175913
MS
7051}
7052
7053static void
7054show_exec_direction_func (struct ui_file *out, int from_tty,
7055 struct cmd_list_element *cmd, const char *value)
7056{
7057 switch (execution_direction) {
7058 case EXEC_FORWARD:
7059 fprintf_filtered (out, _("Forward.\n"));
7060 break;
7061 case EXEC_REVERSE:
7062 fprintf_filtered (out, _("Reverse.\n"));
7063 break;
b2175913 7064 default:
d8b34453
PA
7065 internal_error (__FILE__, __LINE__,
7066 _("bogus execution_direction value: %d"),
7067 (int) execution_direction);
b2175913
MS
7068 }
7069}
7070
7071/* User interface for non-stop mode. */
7072
ad52ddc6 7073int non_stop = 0;
ad52ddc6
PA
7074
7075static void
7076set_non_stop (char *args, int from_tty,
7077 struct cmd_list_element *c)
7078{
7079 if (target_has_execution)
7080 {
7081 non_stop_1 = non_stop;
7082 error (_("Cannot change this setting while the inferior is running."));
7083 }
7084
7085 non_stop = non_stop_1;
7086}
7087
7088static void
7089show_non_stop (struct ui_file *file, int from_tty,
7090 struct cmd_list_element *c, const char *value)
7091{
7092 fprintf_filtered (file,
7093 _("Controlling the inferior in non-stop mode is %s.\n"),
7094 value);
7095}
7096
d4db2f36
PA
7097static void
7098show_schedule_multiple (struct ui_file *file, int from_tty,
7099 struct cmd_list_element *c, const char *value)
7100{
3e43a32a
MS
7101 fprintf_filtered (file, _("Resuming the execution of threads "
7102 "of all processes is %s.\n"), value);
d4db2f36 7103}
ad52ddc6 7104
22d2b532
SDJ
7105/* Implementation of `siginfo' variable. */
7106
7107static const struct internalvar_funcs siginfo_funcs =
7108{
7109 siginfo_make_value,
7110 NULL,
7111 NULL
7112};
7113
c906108c 7114void
96baa820 7115_initialize_infrun (void)
c906108c 7116{
52f0bd74
AC
7117 int i;
7118 int numsigs;
de0bea00 7119 struct cmd_list_element *c;
c906108c 7120
1bedd215
AC
7121 add_info ("signals", signals_info, _("\
7122What debugger does when program gets various signals.\n\
7123Specify a signal as argument to print info on that signal only."));
c906108c
SS
7124 add_info_alias ("handle", "signals", 0);
7125
de0bea00 7126 c = add_com ("handle", class_run, handle_command, _("\
dfbd5e7b 7127Specify how to handle signals.\n\
486c7739 7128Usage: handle SIGNAL [ACTIONS]\n\
c906108c 7129Args are signals and actions to apply to those signals.\n\
dfbd5e7b 7130If no actions are specified, the current settings for the specified signals\n\
486c7739
MF
7131will be displayed instead.\n\
7132\n\
c906108c
SS
7133Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
7134from 1-15 are allowed for compatibility with old versions of GDB.\n\
7135Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
7136The special arg \"all\" is recognized to mean all signals except those\n\
1bedd215 7137used by the debugger, typically SIGTRAP and SIGINT.\n\
486c7739 7138\n\
1bedd215 7139Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
c906108c
SS
7140\"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
7141Stop means reenter debugger if this signal happens (implies print).\n\
7142Print means print a message if this signal happens.\n\
7143Pass means let program see this signal; otherwise program doesn't know.\n\
7144Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
dfbd5e7b
PA
7145Pass and Stop may be combined.\n\
7146\n\
7147Multiple signals may be specified. Signal numbers and signal names\n\
7148may be interspersed with actions, with the actions being performed for\n\
7149all signals cumulatively specified."));
de0bea00 7150 set_cmd_completer (c, handle_completer);
486c7739 7151
c906108c
SS
7152 if (xdb_commands)
7153 {
1bedd215
AC
7154 add_com ("lz", class_info, signals_info, _("\
7155What debugger does when program gets various signals.\n\
7156Specify a signal as argument to print info on that signal only."));
7157 add_com ("z", class_run, xdb_handle_command, _("\
7158Specify how to handle a signal.\n\
c906108c
SS
7159Args are signals and actions to apply to those signals.\n\
7160Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
7161from 1-15 are allowed for compatibility with old versions of GDB.\n\
7162Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
7163The special arg \"all\" is recognized to mean all signals except those\n\
1bedd215 7164used by the debugger, typically SIGTRAP and SIGINT.\n\
cce7e648 7165Recognized actions include \"s\" (toggles between stop and nostop),\n\
c906108c
SS
7166\"r\" (toggles between print and noprint), \"i\" (toggles between pass and \
7167nopass), \"Q\" (noprint)\n\
7168Stop means reenter debugger if this signal happens (implies print).\n\
7169Print means print a message if this signal happens.\n\
7170Pass means let program see this signal; otherwise program doesn't know.\n\
7171Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
1bedd215 7172Pass and Stop may be combined."));
c906108c
SS
7173 }
7174
7175 if (!dbx_commands)
1a966eab
AC
7176 stop_command = add_cmd ("stop", class_obscure,
7177 not_just_help_class_command, _("\
7178There is no `stop' command, but you can set a hook on `stop'.\n\
c906108c 7179This allows you to set a list of commands to be run each time execution\n\
1a966eab 7180of the program stops."), &cmdlist);
c906108c 7181
ccce17b0 7182 add_setshow_zuinteger_cmd ("infrun", class_maintenance, &debug_infrun, _("\
85c07804
AC
7183Set inferior debugging."), _("\
7184Show inferior debugging."), _("\
7185When non-zero, inferior specific debugging is enabled."),
ccce17b0
YQ
7186 NULL,
7187 show_debug_infrun,
7188 &setdebuglist, &showdebuglist);
527159b7 7189
3e43a32a
MS
7190 add_setshow_boolean_cmd ("displaced", class_maintenance,
7191 &debug_displaced, _("\
237fc4c9
PA
7192Set displaced stepping debugging."), _("\
7193Show displaced stepping debugging."), _("\
7194When non-zero, displaced stepping specific debugging is enabled."),
7195 NULL,
7196 show_debug_displaced,
7197 &setdebuglist, &showdebuglist);
7198
ad52ddc6
PA
7199 add_setshow_boolean_cmd ("non-stop", no_class,
7200 &non_stop_1, _("\
7201Set whether gdb controls the inferior in non-stop mode."), _("\
7202Show whether gdb controls the inferior in non-stop mode."), _("\
7203When debugging a multi-threaded program and this setting is\n\
7204off (the default, also called all-stop mode), when one thread stops\n\
7205(for a breakpoint, watchpoint, exception, or similar events), GDB stops\n\
7206all other threads in the program while you interact with the thread of\n\
7207interest. When you continue or step a thread, you can allow the other\n\
7208threads to run, or have them remain stopped, but while you inspect any\n\
7209thread's state, all threads stop.\n\
7210\n\
7211In non-stop mode, when one thread stops, other threads can continue\n\
7212to run freely. You'll be able to step each thread independently,\n\
7213leave it stopped or free to run as needed."),
7214 set_non_stop,
7215 show_non_stop,
7216 &setlist,
7217 &showlist);
7218
a493e3e2 7219 numsigs = (int) GDB_SIGNAL_LAST;
488f131b 7220 signal_stop = (unsigned char *) xmalloc (sizeof (signal_stop[0]) * numsigs);
c906108c
SS
7221 signal_print = (unsigned char *)
7222 xmalloc (sizeof (signal_print[0]) * numsigs);
7223 signal_program = (unsigned char *)
7224 xmalloc (sizeof (signal_program[0]) * numsigs);
2455069d
UW
7225 signal_pass = (unsigned char *)
7226 xmalloc (sizeof (signal_program[0]) * numsigs);
c906108c
SS
7227 for (i = 0; i < numsigs; i++)
7228 {
7229 signal_stop[i] = 1;
7230 signal_print[i] = 1;
7231 signal_program[i] = 1;
7232 }
7233
7234 /* Signals caused by debugger's own actions
7235 should not be given to the program afterwards. */
a493e3e2
PA
7236 signal_program[GDB_SIGNAL_TRAP] = 0;
7237 signal_program[GDB_SIGNAL_INT] = 0;
c906108c
SS
7238
7239 /* Signals that are not errors should not normally enter the debugger. */
a493e3e2
PA
7240 signal_stop[GDB_SIGNAL_ALRM] = 0;
7241 signal_print[GDB_SIGNAL_ALRM] = 0;
7242 signal_stop[GDB_SIGNAL_VTALRM] = 0;
7243 signal_print[GDB_SIGNAL_VTALRM] = 0;
7244 signal_stop[GDB_SIGNAL_PROF] = 0;
7245 signal_print[GDB_SIGNAL_PROF] = 0;
7246 signal_stop[GDB_SIGNAL_CHLD] = 0;
7247 signal_print[GDB_SIGNAL_CHLD] = 0;
7248 signal_stop[GDB_SIGNAL_IO] = 0;
7249 signal_print[GDB_SIGNAL_IO] = 0;
7250 signal_stop[GDB_SIGNAL_POLL] = 0;
7251 signal_print[GDB_SIGNAL_POLL] = 0;
7252 signal_stop[GDB_SIGNAL_URG] = 0;
7253 signal_print[GDB_SIGNAL_URG] = 0;
7254 signal_stop[GDB_SIGNAL_WINCH] = 0;
7255 signal_print[GDB_SIGNAL_WINCH] = 0;
7256 signal_stop[GDB_SIGNAL_PRIO] = 0;
7257 signal_print[GDB_SIGNAL_PRIO] = 0;
c906108c 7258
cd0fc7c3
SS
7259 /* These signals are used internally by user-level thread
7260 implementations. (See signal(5) on Solaris.) Like the above
7261 signals, a healthy program receives and handles them as part of
7262 its normal operation. */
a493e3e2
PA
7263 signal_stop[GDB_SIGNAL_LWP] = 0;
7264 signal_print[GDB_SIGNAL_LWP] = 0;
7265 signal_stop[GDB_SIGNAL_WAITING] = 0;
7266 signal_print[GDB_SIGNAL_WAITING] = 0;
7267 signal_stop[GDB_SIGNAL_CANCEL] = 0;
7268 signal_print[GDB_SIGNAL_CANCEL] = 0;
cd0fc7c3 7269
2455069d
UW
7270 /* Update cached state. */
7271 signal_cache_update (-1);
7272
85c07804
AC
7273 add_setshow_zinteger_cmd ("stop-on-solib-events", class_support,
7274 &stop_on_solib_events, _("\
7275Set stopping for shared library events."), _("\
7276Show stopping for shared library events."), _("\
c906108c
SS
7277If nonzero, gdb will give control to the user when the dynamic linker\n\
7278notifies gdb of shared library events. The most common event of interest\n\
85c07804
AC
7279to the user would be loading/unloading of a new library."),
7280 NULL,
920d2a44 7281 show_stop_on_solib_events,
85c07804 7282 &setlist, &showlist);
c906108c 7283
7ab04401
AC
7284 add_setshow_enum_cmd ("follow-fork-mode", class_run,
7285 follow_fork_mode_kind_names,
7286 &follow_fork_mode_string, _("\
7287Set debugger response to a program call of fork or vfork."), _("\
7288Show debugger response to a program call of fork or vfork."), _("\
c906108c
SS
7289A fork or vfork creates a new process. follow-fork-mode can be:\n\
7290 parent - the original process is debugged after a fork\n\
7291 child - the new process is debugged after a fork\n\
ea1dd7bc 7292The unfollowed process will continue to run.\n\
7ab04401
AC
7293By default, the debugger will follow the parent process."),
7294 NULL,
920d2a44 7295 show_follow_fork_mode_string,
7ab04401
AC
7296 &setlist, &showlist);
7297
6c95b8df
PA
7298 add_setshow_enum_cmd ("follow-exec-mode", class_run,
7299 follow_exec_mode_names,
7300 &follow_exec_mode_string, _("\
7301Set debugger response to a program call of exec."), _("\
7302Show debugger response to a program call of exec."), _("\
7303An exec call replaces the program image of a process.\n\
7304\n\
7305follow-exec-mode can be:\n\
7306\n\
cce7e648 7307 new - the debugger creates a new inferior and rebinds the process\n\
6c95b8df
PA
7308to this new inferior. The program the process was running before\n\
7309the exec call can be restarted afterwards by restarting the original\n\
7310inferior.\n\
7311\n\
7312 same - the debugger keeps the process bound to the same inferior.\n\
7313The new executable image replaces the previous executable loaded in\n\
7314the inferior. Restarting the inferior after the exec call restarts\n\
7315the executable the process was running after the exec call.\n\
7316\n\
7317By default, the debugger will use the same inferior."),
7318 NULL,
7319 show_follow_exec_mode_string,
7320 &setlist, &showlist);
7321
7ab04401
AC
7322 add_setshow_enum_cmd ("scheduler-locking", class_run,
7323 scheduler_enums, &scheduler_mode, _("\
7324Set mode for locking scheduler during execution."), _("\
7325Show mode for locking scheduler during execution."), _("\
c906108c
SS
7326off == no locking (threads may preempt at any time)\n\
7327on == full locking (no thread except the current thread may run)\n\
7328step == scheduler locked during every single-step operation.\n\
7329 In this mode, no other thread may run during a step command.\n\
7ab04401
AC
7330 Other threads may run while stepping over a function call ('next')."),
7331 set_schedlock_func, /* traps on target vector */
920d2a44 7332 show_scheduler_mode,
7ab04401 7333 &setlist, &showlist);
5fbbeb29 7334
d4db2f36
PA
7335 add_setshow_boolean_cmd ("schedule-multiple", class_run, &sched_multi, _("\
7336Set mode for resuming threads of all processes."), _("\
7337Show mode for resuming threads of all processes."), _("\
7338When on, execution commands (such as 'continue' or 'next') resume all\n\
7339threads of all processes. When off (which is the default), execution\n\
7340commands only resume the threads of the current process. The set of\n\
7341threads that are resumed is further refined by the scheduler-locking\n\
7342mode (see help set scheduler-locking)."),
7343 NULL,
7344 show_schedule_multiple,
7345 &setlist, &showlist);
7346
5bf193a2
AC
7347 add_setshow_boolean_cmd ("step-mode", class_run, &step_stop_if_no_debug, _("\
7348Set mode of the step operation."), _("\
7349Show mode of the step operation."), _("\
7350When set, doing a step over a function without debug line information\n\
7351will stop at the first instruction of that function. Otherwise, the\n\
7352function is skipped and the step command stops at a different source line."),
7353 NULL,
920d2a44 7354 show_step_stop_if_no_debug,
5bf193a2 7355 &setlist, &showlist);
ca6724c1 7356
72d0e2c5
YQ
7357 add_setshow_auto_boolean_cmd ("displaced-stepping", class_run,
7358 &can_use_displaced_stepping, _("\
237fc4c9
PA
7359Set debugger's willingness to use displaced stepping."), _("\
7360Show debugger's willingness to use displaced stepping."), _("\
fff08868
HZ
7361If on, gdb will use displaced stepping to step over breakpoints if it is\n\
7362supported by the target architecture. If off, gdb will not use displaced\n\
7363stepping to step over breakpoints, even if such is supported by the target\n\
7364architecture. If auto (which is the default), gdb will use displaced stepping\n\
7365if the target architecture supports it and non-stop mode is active, but will not\n\
7366use it in all-stop mode (see help set non-stop)."),
72d0e2c5
YQ
7367 NULL,
7368 show_can_use_displaced_stepping,
7369 &setlist, &showlist);
237fc4c9 7370
b2175913
MS
7371 add_setshow_enum_cmd ("exec-direction", class_run, exec_direction_names,
7372 &exec_direction, _("Set direction of execution.\n\
7373Options are 'forward' or 'reverse'."),
7374 _("Show direction of execution (forward/reverse)."),
7375 _("Tells gdb whether to execute forward or backward."),
7376 set_exec_direction_func, show_exec_direction_func,
7377 &setlist, &showlist);
7378
6c95b8df
PA
7379 /* Set/show detach-on-fork: user-settable mode. */
7380
7381 add_setshow_boolean_cmd ("detach-on-fork", class_run, &detach_fork, _("\
7382Set whether gdb will detach the child of a fork."), _("\
7383Show whether gdb will detach the child of a fork."), _("\
7384Tells gdb whether to detach the child of a fork."),
7385 NULL, NULL, &setlist, &showlist);
7386
03583c20
UW
7387 /* Set/show disable address space randomization mode. */
7388
7389 add_setshow_boolean_cmd ("disable-randomization", class_support,
7390 &disable_randomization, _("\
7391Set disabling of debuggee's virtual address space randomization."), _("\
7392Show disabling of debuggee's virtual address space randomization."), _("\
7393When this mode is on (which is the default), randomization of the virtual\n\
7394address space is disabled. Standalone programs run with the randomization\n\
7395enabled by default on some platforms."),
7396 &set_disable_randomization,
7397 &show_disable_randomization,
7398 &setlist, &showlist);
7399
ca6724c1 7400 /* ptid initializations */
ca6724c1
KB
7401 inferior_ptid = null_ptid;
7402 target_last_wait_ptid = minus_one_ptid;
5231c1fd
PA
7403
7404 observer_attach_thread_ptid_changed (infrun_thread_ptid_changed);
252fbfc8 7405 observer_attach_thread_stop_requested (infrun_thread_stop_requested);
a07daef3 7406 observer_attach_thread_exit (infrun_thread_thread_exit);
fc1cf338 7407 observer_attach_inferior_exit (infrun_inferior_exit);
4aa995e1
PA
7408
7409 /* Explicitly create without lookup, since that tries to create a
7410 value with a void typed value, and when we get here, gdbarch
7411 isn't initialized yet. At this point, we're quite sure there
7412 isn't another convenience variable of the same name. */
22d2b532 7413 create_internalvar_type_lazy ("_siginfo", &siginfo_funcs, NULL);
d914c394
SS
7414
7415 add_setshow_boolean_cmd ("observer", no_class,
7416 &observer_mode_1, _("\
7417Set whether gdb controls the inferior in observer mode."), _("\
7418Show whether gdb controls the inferior in observer mode."), _("\
7419In observer mode, GDB can get data from the inferior, but not\n\
7420affect its execution. Registers and memory may not be changed,\n\
7421breakpoints may not be set, and the program cannot be interrupted\n\
7422or signalled."),
7423 set_observer_mode,
7424 show_observer_mode,
7425 &setlist,
7426 &showlist);
c906108c 7427}
This page took 1.73128 seconds and 4 git commands to generate.