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