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