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