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