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