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