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