*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / infrun.c
CommitLineData
ca557f44
AC
1/* Target-struct-independent code to start (run) and stop an inferior
2 process.
8926118c 3
6aba47ca
DJ
4 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
5 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
8621d6a9 6 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"
a77053c2 48
9f976b41 49#include "gdb_assert.h"
034dad6f 50#include "mi/mi-common.h"
c906108c
SS
51
52/* Prototypes for local functions */
53
96baa820 54static void signals_info (char *, int);
c906108c 55
96baa820 56static void handle_command (char *, int);
c906108c 57
96baa820 58static void sig_print_info (enum target_signal);
c906108c 59
96baa820 60static void sig_print_header (void);
c906108c 61
74b7792f 62static void resume_cleanups (void *);
c906108c 63
96baa820 64static int hook_stop_stub (void *);
c906108c 65
96baa820
JM
66static int restore_selected_frame (void *);
67
68static void build_infrun (void);
69
4ef3f3be 70static int follow_fork (void);
96baa820
JM
71
72static void set_schedlock_func (char *args, int from_tty,
488f131b 73 struct cmd_list_element *c);
96baa820 74
96baa820
JM
75struct execution_control_state;
76
77static int currently_stepping (struct execution_control_state *ecs);
78
79static void xdb_handle_command (char *args, int from_tty);
80
6a6b96b9 81static int prepare_to_proceed (int);
ea67f13b 82
96baa820 83void _initialize_infrun (void);
43ff13b4 84
c906108c
SS
85int inferior_ignoring_leading_exec_events = 0;
86
5fbbeb29
CF
87/* When set, stop the 'step' command if we enter a function which has
88 no line number information. The normal behavior is that we step
89 over such function. */
90int step_stop_if_no_debug = 0;
920d2a44
AC
91static void
92show_step_stop_if_no_debug (struct ui_file *file, int from_tty,
93 struct cmd_list_element *c, const char *value)
94{
95 fprintf_filtered (file, _("Mode of the step operation is %s.\n"), value);
96}
5fbbeb29 97
43ff13b4 98/* In asynchronous mode, but simulating synchronous execution. */
96baa820 99
43ff13b4
JM
100int sync_execution = 0;
101
c906108c
SS
102/* wait_for_inferior and normal_stop use this to notify the user
103 when the inferior stopped in a different thread than it had been
96baa820
JM
104 running in. */
105
39f77062 106static ptid_t previous_inferior_ptid;
7a292a7a
SS
107
108/* This is true for configurations that may follow through execl() and
109 similar functions. At present this is only true for HP-UX native. */
110
111#ifndef MAY_FOLLOW_EXEC
112#define MAY_FOLLOW_EXEC (0)
c906108c
SS
113#endif
114
7a292a7a
SS
115static int may_follow_exec = MAY_FOLLOW_EXEC;
116
527159b7 117static int debug_infrun = 0;
920d2a44
AC
118static void
119show_debug_infrun (struct ui_file *file, int from_tty,
120 struct cmd_list_element *c, const char *value)
121{
122 fprintf_filtered (file, _("Inferior debugging is %s.\n"), value);
123}
527159b7 124
d4f3574e
SS
125/* If the program uses ELF-style shared libraries, then calls to
126 functions in shared libraries go through stubs, which live in a
127 table called the PLT (Procedure Linkage Table). The first time the
128 function is called, the stub sends control to the dynamic linker,
129 which looks up the function's real address, patches the stub so
130 that future calls will go directly to the function, and then passes
131 control to the function.
132
133 If we are stepping at the source level, we don't want to see any of
134 this --- we just want to skip over the stub and the dynamic linker.
135 The simple approach is to single-step until control leaves the
136 dynamic linker.
137
ca557f44
AC
138 However, on some systems (e.g., Red Hat's 5.2 distribution) the
139 dynamic linker calls functions in the shared C library, so you
140 can't tell from the PC alone whether the dynamic linker is still
141 running. In this case, we use a step-resume breakpoint to get us
142 past the dynamic linker, as if we were using "next" to step over a
143 function call.
d4f3574e
SS
144
145 IN_SOLIB_DYNSYM_RESOLVE_CODE says whether we're in the dynamic
146 linker code or not. Normally, this means we single-step. However,
147 if SKIP_SOLIB_RESOLVER then returns non-zero, then its value is an
148 address where we can place a step-resume breakpoint to get past the
149 linker's symbol resolution function.
150
151 IN_SOLIB_DYNSYM_RESOLVE_CODE can generally be implemented in a
152 pretty portable way, by comparing the PC against the address ranges
153 of the dynamic linker's sections.
154
155 SKIP_SOLIB_RESOLVER is generally going to be system-specific, since
156 it depends on internal details of the dynamic linker. It's usually
157 not too hard to figure out where to put a breakpoint, but it
158 certainly isn't portable. SKIP_SOLIB_RESOLVER should do plenty of
159 sanity checking. If it can't figure things out, returning zero and
160 getting the (possibly confusing) stepping behavior is better than
161 signalling an error, which will obscure the change in the
162 inferior's state. */
c906108c 163
c906108c
SS
164/* This function returns TRUE if pc is the address of an instruction
165 that lies within the dynamic linker (such as the event hook, or the
166 dld itself).
167
168 This function must be used only when a dynamic linker event has
169 been caught, and the inferior is being stepped out of the hook, or
170 undefined results are guaranteed. */
171
172#ifndef SOLIB_IN_DYNAMIC_LINKER
173#define SOLIB_IN_DYNAMIC_LINKER(pid,pc) 0
174#endif
175
c2c6d25f 176
7a292a7a
SS
177/* Convert the #defines into values. This is temporary until wfi control
178 flow is completely sorted out. */
179
692590c1
MS
180#ifndef CANNOT_STEP_HW_WATCHPOINTS
181#define CANNOT_STEP_HW_WATCHPOINTS 0
182#else
183#undef CANNOT_STEP_HW_WATCHPOINTS
184#define CANNOT_STEP_HW_WATCHPOINTS 1
185#endif
186
c906108c
SS
187/* Tables of how to react to signals; the user sets them. */
188
189static unsigned char *signal_stop;
190static unsigned char *signal_print;
191static unsigned char *signal_program;
192
193#define SET_SIGS(nsigs,sigs,flags) \
194 do { \
195 int signum = (nsigs); \
196 while (signum-- > 0) \
197 if ((sigs)[signum]) \
198 (flags)[signum] = 1; \
199 } while (0)
200
201#define UNSET_SIGS(nsigs,sigs,flags) \
202 do { \
203 int signum = (nsigs); \
204 while (signum-- > 0) \
205 if ((sigs)[signum]) \
206 (flags)[signum] = 0; \
207 } while (0)
208
39f77062
KB
209/* Value to pass to target_resume() to cause all threads to resume */
210
211#define RESUME_ALL (pid_to_ptid (-1))
c906108c
SS
212
213/* Command list pointer for the "stop" placeholder. */
214
215static struct cmd_list_element *stop_command;
216
217/* Nonzero if breakpoints are now inserted in the inferior. */
218
219static int breakpoints_inserted;
220
221/* Function inferior was in as of last step command. */
222
223static struct symbol *step_start_function;
224
225/* Nonzero if we are expecting a trace trap and should proceed from it. */
226
227static int trap_expected;
228
c906108c
SS
229/* Nonzero if we want to give control to the user when we're notified
230 of shared library events by the dynamic linker. */
231static int stop_on_solib_events;
920d2a44
AC
232static void
233show_stop_on_solib_events (struct ui_file *file, int from_tty,
234 struct cmd_list_element *c, const char *value)
235{
236 fprintf_filtered (file, _("Stopping for shared library events is %s.\n"),
237 value);
238}
c906108c 239
c906108c
SS
240/* Nonzero means expecting a trace trap
241 and should stop the inferior and return silently when it happens. */
242
243int stop_after_trap;
244
245/* Nonzero means expecting a trap and caller will handle it themselves.
246 It is used after attach, due to attaching to a process;
247 when running in the shell before the child program has been exec'd;
248 and when running some kinds of remote stuff (FIXME?). */
249
c0236d92 250enum stop_kind stop_soon;
c906108c
SS
251
252/* Nonzero if proceed is being used for a "finish" command or a similar
253 situation when stop_registers should be saved. */
254
255int proceed_to_finish;
256
257/* Save register contents here when about to pop a stack dummy frame,
258 if-and-only-if proceed_to_finish is set.
259 Thus this contains the return value from the called function (assuming
260 values are returned in a register). */
261
72cec141 262struct regcache *stop_registers;
c906108c 263
c906108c
SS
264/* Nonzero after stop if current stack frame should be printed. */
265
266static int stop_print_frame;
267
268static struct breakpoint *step_resume_breakpoint = NULL;
c906108c 269
e02bc4cc 270/* This is a cached copy of the pid/waitstatus of the last event
9a4105ab
AC
271 returned by target_wait()/deprecated_target_wait_hook(). This
272 information is returned by get_last_target_status(). */
39f77062 273static ptid_t target_last_wait_ptid;
e02bc4cc
DS
274static struct target_waitstatus target_last_waitstatus;
275
c906108c
SS
276/* This is used to remember when a fork, vfork or exec event
277 was caught by a catchpoint, and thus the event is to be
278 followed at the next resume of the inferior, and not
279 immediately. */
280static struct
488f131b
JB
281{
282 enum target_waitkind kind;
283 struct
c906108c 284 {
488f131b 285 int parent_pid;
488f131b 286 int child_pid;
c906108c 287 }
488f131b
JB
288 fork_event;
289 char *execd_pathname;
290}
c906108c
SS
291pending_follow;
292
53904c9e
AC
293static const char follow_fork_mode_child[] = "child";
294static const char follow_fork_mode_parent[] = "parent";
295
488f131b 296static const char *follow_fork_mode_kind_names[] = {
53904c9e
AC
297 follow_fork_mode_child,
298 follow_fork_mode_parent,
299 NULL
ef346e04 300};
c906108c 301
53904c9e 302static const char *follow_fork_mode_string = follow_fork_mode_parent;
920d2a44
AC
303static void
304show_follow_fork_mode_string (struct ui_file *file, int from_tty,
305 struct cmd_list_element *c, const char *value)
306{
307 fprintf_filtered (file, _("\
308Debugger response to a program call of fork or vfork is \"%s\".\n"),
309 value);
310}
c906108c
SS
311\f
312
6604731b 313static int
4ef3f3be 314follow_fork (void)
c906108c 315{
ea1dd7bc 316 int follow_child = (follow_fork_mode_string == follow_fork_mode_child);
c906108c 317
6604731b 318 return target_follow_fork (follow_child);
c906108c
SS
319}
320
6604731b
DJ
321void
322follow_inferior_reset_breakpoints (void)
c906108c 323{
6604731b
DJ
324 /* Was there a step_resume breakpoint? (There was if the user
325 did a "next" at the fork() call.) If so, explicitly reset its
326 thread number.
327
328 step_resumes are a form of bp that are made to be per-thread.
329 Since we created the step_resume bp when the parent process
330 was being debugged, and now are switching to the child process,
331 from the breakpoint package's viewpoint, that's a switch of
332 "threads". We must update the bp's notion of which thread
333 it is for, or it'll be ignored when it triggers. */
334
335 if (step_resume_breakpoint)
336 breakpoint_re_set_thread (step_resume_breakpoint);
337
338 /* Reinsert all breakpoints in the child. The user may have set
339 breakpoints after catching the fork, in which case those
340 were never set in the child, but only in the parent. This makes
341 sure the inserted breakpoints match the breakpoint list. */
342
343 breakpoint_re_set ();
344 insert_breakpoints ();
c906108c 345}
c906108c 346
1adeb98a
FN
347/* EXECD_PATHNAME is assumed to be non-NULL. */
348
c906108c 349static void
96baa820 350follow_exec (int pid, char *execd_pathname)
c906108c 351{
c906108c 352 int saved_pid = pid;
7a292a7a
SS
353 struct target_ops *tgt;
354
355 if (!may_follow_exec)
356 return;
c906108c 357
c906108c
SS
358 /* This is an exec event that we actually wish to pay attention to.
359 Refresh our symbol table to the newly exec'd program, remove any
360 momentary bp's, etc.
361
362 If there are breakpoints, they aren't really inserted now,
363 since the exec() transformed our inferior into a fresh set
364 of instructions.
365
366 We want to preserve symbolic breakpoints on the list, since
367 we have hopes that they can be reset after the new a.out's
368 symbol table is read.
369
370 However, any "raw" breakpoints must be removed from the list
371 (e.g., the solib bp's), since their address is probably invalid
372 now.
373
374 And, we DON'T want to call delete_breakpoints() here, since
375 that may write the bp's "shadow contents" (the instruction
376 value that was overwritten witha TRAP instruction). Since
377 we now have a new a.out, those shadow contents aren't valid. */
378 update_breakpoints_after_exec ();
379
380 /* If there was one, it's gone now. We cannot truly step-to-next
381 statement through an exec(). */
382 step_resume_breakpoint = NULL;
383 step_range_start = 0;
384 step_range_end = 0;
385
c906108c 386 /* What is this a.out's name? */
a3f17187 387 printf_unfiltered (_("Executing new program: %s\n"), execd_pathname);
c906108c
SS
388
389 /* We've followed the inferior through an exec. Therefore, the
390 inferior has essentially been killed & reborn. */
7a292a7a
SS
391
392 /* First collect the run target in effect. */
393 tgt = find_run_target ();
394 /* If we can't find one, things are in a very strange state... */
395 if (tgt == NULL)
8a3fe4f8 396 error (_("Could find run target to save before following exec"));
7a292a7a 397
c906108c
SS
398 gdb_flush (gdb_stdout);
399 target_mourn_inferior ();
39f77062 400 inferior_ptid = pid_to_ptid (saved_pid);
488f131b 401 /* Because mourn_inferior resets inferior_ptid. */
7a292a7a 402 push_target (tgt);
c906108c
SS
403
404 /* That a.out is now the one to use. */
405 exec_file_attach (execd_pathname, 0);
406
407 /* And also is where symbols can be found. */
1adeb98a 408 symbol_file_add_main (execd_pathname, 0);
c906108c
SS
409
410 /* Reset the shared library package. This ensures that we get
411 a shlib event when the child reaches "_start", at which point
412 the dld will have had a chance to initialize the child. */
7a292a7a 413#if defined(SOLIB_RESTART)
c906108c 414 SOLIB_RESTART ();
7a292a7a
SS
415#endif
416#ifdef SOLIB_CREATE_INFERIOR_HOOK
39f77062 417 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
a77053c2
MK
418#else
419 solib_create_inferior_hook ();
7a292a7a 420#endif
c906108c
SS
421
422 /* Reinsert all breakpoints. (Those which were symbolic have
423 been reset to the proper address in the new a.out, thanks
424 to symbol_file_command...) */
425 insert_breakpoints ();
426
427 /* The next resume of this inferior should bring it to the shlib
428 startup breakpoints. (If the user had also set bp's on
429 "main" from the old (parent) process, then they'll auto-
430 matically get reset there in the new process.) */
c906108c
SS
431}
432
433/* Non-zero if we just simulating a single-step. This is needed
434 because we cannot remove the breakpoints in the inferior process
435 until after the `wait' in `wait_for_inferior'. */
436static int singlestep_breakpoints_inserted_p = 0;
9f976b41
DJ
437
438/* The thread we inserted single-step breakpoints for. */
439static ptid_t singlestep_ptid;
440
fd48f117
DJ
441/* PC when we started this single-step. */
442static CORE_ADDR singlestep_pc;
443
9f976b41
DJ
444/* If another thread hit the singlestep breakpoint, we save the original
445 thread here so that we can resume single-stepping it later. */
446static ptid_t saved_singlestep_ptid;
447static int stepping_past_singlestep_breakpoint;
6a6b96b9
UW
448
449/* Similarly, if we are stepping another thread past a breakpoint,
450 save the original thread here so that we can resume stepping it later. */
451static ptid_t stepping_past_breakpoint_ptid;
452static int stepping_past_breakpoint;
c906108c
SS
453\f
454
455/* Things to clean up if we QUIT out of resume (). */
c906108c 456static void
74b7792f 457resume_cleanups (void *ignore)
c906108c
SS
458{
459 normal_stop ();
460}
461
53904c9e
AC
462static const char schedlock_off[] = "off";
463static const char schedlock_on[] = "on";
464static const char schedlock_step[] = "step";
488f131b 465static const char *scheduler_enums[] = {
ef346e04
AC
466 schedlock_off,
467 schedlock_on,
468 schedlock_step,
469 NULL
470};
920d2a44
AC
471static const char *scheduler_mode = schedlock_off;
472static void
473show_scheduler_mode (struct ui_file *file, int from_tty,
474 struct cmd_list_element *c, const char *value)
475{
476 fprintf_filtered (file, _("\
477Mode for locking scheduler during execution is \"%s\".\n"),
478 value);
479}
c906108c
SS
480
481static void
96baa820 482set_schedlock_func (char *args, int from_tty, struct cmd_list_element *c)
c906108c 483{
eefe576e
AC
484 if (!target_can_lock_scheduler)
485 {
486 scheduler_mode = schedlock_off;
487 error (_("Target '%s' cannot support this command."), target_shortname);
488 }
c906108c
SS
489}
490
491
492/* Resume the inferior, but allow a QUIT. This is useful if the user
493 wants to interrupt some lengthy single-stepping operation
494 (for child processes, the SIGINT goes to the inferior, and so
495 we get a SIGINT random_signal, but for remote debugging and perhaps
496 other targets, that's not true).
497
498 STEP nonzero if we should step (zero to continue instead).
499 SIG is the signal to give the inferior (zero for none). */
500void
96baa820 501resume (int step, enum target_signal sig)
c906108c
SS
502{
503 int should_resume = 1;
74b7792f 504 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
c906108c
SS
505 QUIT;
506
527159b7 507 if (debug_infrun)
8a9de0e4
AC
508 fprintf_unfiltered (gdb_stdlog, "infrun: resume (step=%d, signal=%d)\n",
509 step, sig);
527159b7 510
ef5cf84e
MS
511 /* FIXME: calling breakpoint_here_p (read_pc ()) three times! */
512
c906108c 513
692590c1
MS
514 /* Some targets (e.g. Solaris x86) have a kernel bug when stepping
515 over an instruction that causes a page fault without triggering
516 a hardware watchpoint. The kernel properly notices that it shouldn't
517 stop, because the hardware watchpoint is not triggered, but it forgets
518 the step request and continues the program normally.
519 Work around the problem by removing hardware watchpoints if a step is
520 requested, GDB will check for a hardware watchpoint trigger after the
521 step anyway. */
522 if (CANNOT_STEP_HW_WATCHPOINTS && step && breakpoints_inserted)
523 remove_hw_watchpoints ();
488f131b 524
692590c1 525
c2c6d25f
JM
526 /* Normally, by the time we reach `resume', the breakpoints are either
527 removed or inserted, as appropriate. The exception is if we're sitting
528 at a permanent breakpoint; we need to step over it, but permanent
529 breakpoints can't be removed. So we have to test for it here. */
530 if (breakpoint_here_p (read_pc ()) == permanent_breakpoint_here)
6d350bb5
UW
531 {
532 if (gdbarch_skip_permanent_breakpoint_p (current_gdbarch))
594f7785
UW
533 gdbarch_skip_permanent_breakpoint (current_gdbarch,
534 get_current_regcache ());
6d350bb5
UW
535 else
536 error (_("\
537The program is stopped at a permanent breakpoint, but GDB does not know\n\
538how to step past a permanent breakpoint on this architecture. Try using\n\
539a command like `return' or `jump' to continue execution."));
540 }
c2c6d25f 541
1c0fdd0e 542 if (step && gdbarch_software_single_step_p (current_gdbarch))
c906108c
SS
543 {
544 /* Do it the hard way, w/temp breakpoints */
1c0fdd0e 545 if (gdbarch_software_single_step (current_gdbarch, get_current_frame ()))
e6590a1b
UW
546 {
547 /* ...and don't ask hardware to do it. */
548 step = 0;
549 /* and do not pull these breakpoints until after a `wait' in
550 `wait_for_inferior' */
551 singlestep_breakpoints_inserted_p = 1;
552 singlestep_ptid = inferior_ptid;
553 singlestep_pc = read_pc ();
554 }
c906108c
SS
555 }
556
c906108c 557 /* If there were any forks/vforks/execs that were caught and are
6604731b 558 now to be followed, then do so. */
c906108c
SS
559 switch (pending_follow.kind)
560 {
6604731b
DJ
561 case TARGET_WAITKIND_FORKED:
562 case TARGET_WAITKIND_VFORKED:
c906108c 563 pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
6604731b
DJ
564 if (follow_fork ())
565 should_resume = 0;
c906108c
SS
566 break;
567
6604731b 568 case TARGET_WAITKIND_EXECD:
c906108c 569 /* follow_exec is called as soon as the exec event is seen. */
6604731b 570 pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
c906108c
SS
571 break;
572
573 default:
574 break;
575 }
c906108c
SS
576
577 /* Install inferior's terminal modes. */
578 target_terminal_inferior ();
579
580 if (should_resume)
581 {
39f77062 582 ptid_t resume_ptid;
dfcd3bfb 583
488f131b 584 resume_ptid = RESUME_ALL; /* Default */
ef5cf84e 585
cd76b0b7
VP
586 /* If STEP is set, it's a request to use hardware stepping
587 facilities. But in that case, we should never
588 use singlestep breakpoint. */
589 gdb_assert (!(singlestep_breakpoints_inserted_p && step));
590
591 if (singlestep_breakpoints_inserted_p
592 && stepping_past_singlestep_breakpoint)
c906108c 593 {
cd76b0b7
VP
594 /* The situation here is as follows. In thread T1 we wanted to
595 single-step. Lacking hardware single-stepping we've
596 set breakpoint at the PC of the next instruction -- call it
597 P. After resuming, we've hit that breakpoint in thread T2.
598 Now we've removed original breakpoint, inserted breakpoint
599 at P+1, and try to step to advance T2 past breakpoint.
600 We need to step only T2, as if T1 is allowed to freely run,
601 it can run past P, and if other threads are allowed to run,
602 they can hit breakpoint at P+1, and nested hits of single-step
603 breakpoints is not something we'd want -- that's complicated
604 to support, and has no value. */
605 resume_ptid = inferior_ptid;
606 }
c906108c 607
e842223a
VP
608 if ((step || singlestep_breakpoints_inserted_p)
609 && breakpoint_here_p (read_pc ())
cd76b0b7
VP
610 && !breakpoint_inserted_here_p (read_pc ()))
611 {
612 /* We're stepping, have breakpoint at PC, and it's
613 not inserted. Most likely, proceed has noticed that
614 we have breakpoint and tries to single-step over it,
615 so that it's not hit. In which case, we need to
616 single-step only this thread, and keep others stopped,
617 as they can miss this breakpoint if allowed to run.
618
619 The current code either has all breakpoints inserted,
620 or all removed, so if we let other threads run,
621 we can actually miss any breakpoint, not the one at PC. */
ef5cf84e 622 resume_ptid = inferior_ptid;
c906108c 623 }
ef5cf84e 624
8fb3e588
AC
625 if ((scheduler_mode == schedlock_on)
626 || (scheduler_mode == schedlock_step
627 && (step || singlestep_breakpoints_inserted_p)))
c906108c 628 {
ef5cf84e 629 /* User-settable 'scheduler' mode requires solo thread resume. */
488f131b 630 resume_ptid = inferior_ptid;
c906108c 631 }
ef5cf84e 632
e6cf7916 633 if (gdbarch_cannot_step_breakpoint (current_gdbarch))
c4ed33b9
AC
634 {
635 /* Most targets can step a breakpoint instruction, thus
636 executing it normally. But if this one cannot, just
637 continue and we will hit it anyway. */
638 if (step && breakpoints_inserted && breakpoint_here_p (read_pc ()))
639 step = 0;
640 }
39f77062 641 target_resume (resume_ptid, step, sig);
c906108c
SS
642 }
643
644 discard_cleanups (old_cleanups);
645}
646\f
647
648/* Clear out all variables saying what to do when inferior is continued.
649 First do this, then set the ones you want, then call `proceed'. */
650
651void
96baa820 652clear_proceed_status (void)
c906108c
SS
653{
654 trap_expected = 0;
655 step_range_start = 0;
656 step_range_end = 0;
aa0cd9c1 657 step_frame_id = null_frame_id;
5fbbeb29 658 step_over_calls = STEP_OVER_UNDEBUGGABLE;
c906108c 659 stop_after_trap = 0;
c0236d92 660 stop_soon = NO_STOP_QUIETLY;
c906108c
SS
661 proceed_to_finish = 0;
662 breakpoint_proceeded = 1; /* We're about to proceed... */
663
d5c31457
UW
664 if (stop_registers)
665 {
666 regcache_xfree (stop_registers);
667 stop_registers = NULL;
668 }
669
c906108c
SS
670 /* Discard any remaining commands or status from previous stop. */
671 bpstat_clear (&stop_bpstat);
672}
673
ea67f13b
DJ
674/* This should be suitable for any targets that support threads. */
675
676static int
6a6b96b9 677prepare_to_proceed (int step)
ea67f13b
DJ
678{
679 ptid_t wait_ptid;
680 struct target_waitstatus wait_status;
681
682 /* Get the last target status returned by target_wait(). */
683 get_last_target_status (&wait_ptid, &wait_status);
684
6a6b96b9 685 /* Make sure we were stopped at a breakpoint. */
ea67f13b 686 if (wait_status.kind != TARGET_WAITKIND_STOPPED
6a6b96b9 687 || wait_status.value.sig != TARGET_SIGNAL_TRAP)
ea67f13b
DJ
688 {
689 return 0;
690 }
691
6a6b96b9 692 /* Switched over from WAIT_PID. */
ea67f13b 693 if (!ptid_equal (wait_ptid, minus_one_ptid)
6a6b96b9
UW
694 && !ptid_equal (inferior_ptid, wait_ptid)
695 && breakpoint_here_p (read_pc_pid (wait_ptid)))
ea67f13b 696 {
6a6b96b9
UW
697 /* If stepping, remember current thread to switch back to. */
698 if (step)
ea67f13b 699 {
6a6b96b9
UW
700 stepping_past_breakpoint = 1;
701 stepping_past_breakpoint_ptid = inferior_ptid;
ea67f13b
DJ
702 }
703
6a6b96b9
UW
704 /* Switch back to WAIT_PID thread. */
705 switch_to_thread (wait_ptid);
706
8fb3e588 707 /* We return 1 to indicate that there is a breakpoint here,
6a6b96b9
UW
708 so we need to step over it before continuing to avoid
709 hitting it straight away. */
710 return 1;
ea67f13b
DJ
711 }
712
713 return 0;
ea67f13b 714}
e4846b08
JJ
715
716/* Record the pc of the program the last time it stopped. This is
717 just used internally by wait_for_inferior, but need to be preserved
718 over calls to it and cleared when the inferior is started. */
719static CORE_ADDR prev_pc;
720
c906108c
SS
721/* Basic routine for continuing the program in various fashions.
722
723 ADDR is the address to resume at, or -1 for resume where stopped.
724 SIGGNAL is the signal to give it, or 0 for none,
c5aa993b 725 or -1 for act according to how it stopped.
c906108c 726 STEP is nonzero if should trap after one instruction.
c5aa993b
JM
727 -1 means return after that and print nothing.
728 You should probably set various step_... variables
729 before calling here, if you are stepping.
c906108c
SS
730
731 You should call clear_proceed_status before calling proceed. */
732
733void
96baa820 734proceed (CORE_ADDR addr, enum target_signal siggnal, int step)
c906108c
SS
735{
736 int oneproc = 0;
737
738 if (step > 0)
739 step_start_function = find_pc_function (read_pc ());
740 if (step < 0)
741 stop_after_trap = 1;
742
2acceee2 743 if (addr == (CORE_ADDR) -1)
c906108c 744 {
c906108c 745 if (read_pc () == stop_pc && breakpoint_here_p (read_pc ()))
3352ef37
AC
746 /* There is a breakpoint at the address we will resume at,
747 step one instruction before inserting breakpoints so that
748 we do not stop right away (and report a second hit at this
749 breakpoint). */
c906108c 750 oneproc = 1;
3352ef37
AC
751 else if (gdbarch_single_step_through_delay_p (current_gdbarch)
752 && gdbarch_single_step_through_delay (current_gdbarch,
753 get_current_frame ()))
754 /* We stepped onto an instruction that needs to be stepped
755 again before re-inserting the breakpoint, do so. */
c906108c
SS
756 oneproc = 1;
757 }
758 else
759 {
760 write_pc (addr);
c906108c
SS
761 }
762
527159b7 763 if (debug_infrun)
8a9de0e4
AC
764 fprintf_unfiltered (gdb_stdlog,
765 "infrun: proceed (addr=0x%s, signal=%d, step=%d)\n",
766 paddr_nz (addr), siggnal, step);
527159b7 767
c906108c
SS
768 /* In a multi-threaded task we may select another thread
769 and then continue or step.
770
771 But if the old thread was stopped at a breakpoint, it
772 will immediately cause another breakpoint stop without
773 any execution (i.e. it will report a breakpoint hit
774 incorrectly). So we must step over it first.
775
ea67f13b 776 prepare_to_proceed checks the current thread against the thread
c906108c
SS
777 that reported the most recent event. If a step-over is required
778 it returns TRUE and sets the current thread to the old thread. */
6a6b96b9 779 if (prepare_to_proceed (step))
ea67f13b 780 oneproc = 1;
c906108c 781
c906108c
SS
782 if (oneproc)
783 /* We will get a trace trap after one instruction.
784 Continue it automatically and insert breakpoints then. */
785 trap_expected = 1;
786 else
787 {
81d0cc19
GS
788 insert_breakpoints ();
789 /* If we get here there was no call to error() in
8fb3e588 790 insert breakpoints -- so they were inserted. */
c906108c
SS
791 breakpoints_inserted = 1;
792 }
793
794 if (siggnal != TARGET_SIGNAL_DEFAULT)
795 stop_signal = siggnal;
796 /* If this signal should not be seen by program,
797 give it zero. Used for debugging signals. */
798 else if (!signal_program[stop_signal])
799 stop_signal = TARGET_SIGNAL_0;
800
801 annotate_starting ();
802
803 /* Make sure that output from GDB appears before output from the
804 inferior. */
805 gdb_flush (gdb_stdout);
806
e4846b08
JJ
807 /* Refresh prev_pc value just prior to resuming. This used to be
808 done in stop_stepping, however, setting prev_pc there did not handle
809 scenarios such as inferior function calls or returning from
810 a function via the return command. In those cases, the prev_pc
811 value was not set properly for subsequent commands. The prev_pc value
812 is used to initialize the starting line number in the ecs. With an
813 invalid value, the gdb next command ends up stopping at the position
814 represented by the next line table entry past our start position.
815 On platforms that generate one line table entry per line, this
816 is not a problem. However, on the ia64, the compiler generates
817 extraneous line table entries that do not increase the line number.
818 When we issue the gdb next command on the ia64 after an inferior call
819 or a return command, we often end up a few instructions forward, still
820 within the original line we started.
821
822 An attempt was made to have init_execution_control_state () refresh
823 the prev_pc value before calculating the line number. This approach
824 did not work because on platforms that use ptrace, the pc register
825 cannot be read unless the inferior is stopped. At that point, we
826 are not guaranteed the inferior is stopped and so the read_pc ()
827 call can fail. Setting the prev_pc value here ensures the value is
8fb3e588 828 updated correctly when the inferior is stopped. */
e4846b08
JJ
829 prev_pc = read_pc ();
830
c906108c
SS
831 /* Resume inferior. */
832 resume (oneproc || step || bpstat_should_step (), stop_signal);
833
834 /* Wait for it to stop (if not standalone)
835 and in any case decode why it stopped, and act accordingly. */
43ff13b4
JM
836 /* Do this only if we are not using the event loop, or if the target
837 does not support asynchronous execution. */
362646f5 838 if (!target_can_async_p ())
43ff13b4
JM
839 {
840 wait_for_inferior ();
841 normal_stop ();
842 }
c906108c 843}
c906108c
SS
844\f
845
846/* Start remote-debugging of a machine over a serial link. */
96baa820 847
c906108c 848void
8621d6a9 849start_remote (int from_tty)
c906108c
SS
850{
851 init_thread_list ();
852 init_wait_for_inferior ();
b0f4b84b 853 stop_soon = STOP_QUIETLY_REMOTE;
c906108c 854 trap_expected = 0;
43ff13b4 855
6426a772
JM
856 /* Always go on waiting for the target, regardless of the mode. */
857 /* FIXME: cagney/1999-09-23: At present it isn't possible to
7e73cedf 858 indicate to wait_for_inferior that a target should timeout if
6426a772
JM
859 nothing is returned (instead of just blocking). Because of this,
860 targets expecting an immediate response need to, internally, set
861 things up so that the target_wait() is forced to eventually
862 timeout. */
863 /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
864 differentiate to its caller what the state of the target is after
865 the initial open has been performed. Here we're assuming that
866 the target has stopped. It should be possible to eventually have
867 target_open() return to the caller an indication that the target
868 is currently running and GDB state should be set to the same as
869 for an async run. */
870 wait_for_inferior ();
8621d6a9
DJ
871
872 /* Now that the inferior has stopped, do any bookkeeping like
873 loading shared libraries. We want to do this before normal_stop,
874 so that the displayed frame is up to date. */
875 post_create_inferior (&current_target, from_tty);
876
6426a772 877 normal_stop ();
c906108c
SS
878}
879
880/* Initialize static vars when a new inferior begins. */
881
882void
96baa820 883init_wait_for_inferior (void)
c906108c
SS
884{
885 /* These are meaningless until the first time through wait_for_inferior. */
886 prev_pc = 0;
c906108c 887
c906108c
SS
888 breakpoints_inserted = 0;
889 breakpoint_init_inferior (inf_starting);
890
891 /* Don't confuse first call to proceed(). */
892 stop_signal = TARGET_SIGNAL_0;
893
894 /* The first resume is not following a fork/vfork/exec. */
895 pending_follow.kind = TARGET_WAITKIND_SPURIOUS; /* I.e., none. */
c906108c 896
c906108c 897 clear_proceed_status ();
9f976b41
DJ
898
899 stepping_past_singlestep_breakpoint = 0;
6a6b96b9 900 stepping_past_breakpoint = 0;
c906108c 901}
c906108c 902\f
b83266a0
SS
903/* This enum encodes possible reasons for doing a target_wait, so that
904 wfi can call target_wait in one place. (Ultimately the call will be
905 moved out of the infinite loop entirely.) */
906
c5aa993b
JM
907enum infwait_states
908{
cd0fc7c3
SS
909 infwait_normal_state,
910 infwait_thread_hop_state,
d983da9c 911 infwait_step_watch_state,
cd0fc7c3 912 infwait_nonstep_watch_state
b83266a0
SS
913};
914
11cf8741
JM
915/* Why did the inferior stop? Used to print the appropriate messages
916 to the interface from within handle_inferior_event(). */
917enum inferior_stop_reason
918{
11cf8741
JM
919 /* Step, next, nexti, stepi finished. */
920 END_STEPPING_RANGE,
11cf8741
JM
921 /* Inferior terminated by signal. */
922 SIGNAL_EXITED,
923 /* Inferior exited. */
924 EXITED,
925 /* Inferior received signal, and user asked to be notified. */
926 SIGNAL_RECEIVED
927};
928
cd0fc7c3
SS
929/* This structure contains what used to be local variables in
930 wait_for_inferior. Probably many of them can return to being
931 locals in handle_inferior_event. */
932
c5aa993b 933struct execution_control_state
488f131b
JB
934{
935 struct target_waitstatus ws;
936 struct target_waitstatus *wp;
937 int another_trap;
938 int random_signal;
939 CORE_ADDR stop_func_start;
940 CORE_ADDR stop_func_end;
941 char *stop_func_name;
942 struct symtab_and_line sal;
488f131b
JB
943 int current_line;
944 struct symtab *current_symtab;
945 int handling_longjmp; /* FIXME */
946 ptid_t ptid;
947 ptid_t saved_inferior_ptid;
68f53502 948 int step_after_step_resume_breakpoint;
488f131b
JB
949 int stepping_through_solib_after_catch;
950 bpstat stepping_through_solib_catchpoints;
488f131b
JB
951 int new_thread_event;
952 struct target_waitstatus tmpstatus;
953 enum infwait_states infwait_state;
954 ptid_t waiton_ptid;
955 int wait_some_more;
956};
957
958void init_execution_control_state (struct execution_control_state *ecs);
959
960void handle_inferior_event (struct execution_control_state *ecs);
cd0fc7c3 961
c2c6d25f 962static void step_into_function (struct execution_control_state *ecs);
44cbf7b5 963static void insert_step_resume_breakpoint_at_frame (struct frame_info *step_frame);
14e60db5 964static void insert_step_resume_breakpoint_at_caller (struct frame_info *);
44cbf7b5
AC
965static void insert_step_resume_breakpoint_at_sal (struct symtab_and_line sr_sal,
966 struct frame_id sr_id);
104c1213
JM
967static void stop_stepping (struct execution_control_state *ecs);
968static void prepare_to_wait (struct execution_control_state *ecs);
d4f3574e 969static void keep_going (struct execution_control_state *ecs);
488f131b
JB
970static void print_stop_reason (enum inferior_stop_reason stop_reason,
971 int stop_info);
104c1213 972
cd0fc7c3
SS
973/* Wait for control to return from inferior to debugger.
974 If inferior gets a signal, we may decide to start it up again
975 instead of returning. That is why there is a loop in this function.
976 When this function actually returns it means the inferior
977 should be left stopped and GDB should read more commands. */
978
979void
96baa820 980wait_for_inferior (void)
cd0fc7c3
SS
981{
982 struct cleanup *old_cleanups;
983 struct execution_control_state ecss;
984 struct execution_control_state *ecs;
c906108c 985
527159b7 986 if (debug_infrun)
8a9de0e4 987 fprintf_unfiltered (gdb_stdlog, "infrun: wait_for_inferior\n");
527159b7 988
8601f500 989 old_cleanups = make_cleanup (delete_step_resume_breakpoint,
c906108c 990 &step_resume_breakpoint);
cd0fc7c3
SS
991
992 /* wfi still stays in a loop, so it's OK just to take the address of
993 a local to get the ecs pointer. */
994 ecs = &ecss;
995
996 /* Fill in with reasonable starting values. */
997 init_execution_control_state (ecs);
998
c906108c 999 /* We'll update this if & when we switch to a new thread. */
39f77062 1000 previous_inferior_ptid = inferior_ptid;
c906108c 1001
cd0fc7c3
SS
1002 overlay_cache_invalid = 1;
1003
1004 /* We have to invalidate the registers BEFORE calling target_wait
1005 because they can be loaded from the target while in target_wait.
1006 This makes remote debugging a bit more efficient for those
1007 targets that provide critical registers as part of their normal
1008 status mechanism. */
1009
1010 registers_changed ();
b83266a0 1011
c906108c
SS
1012 while (1)
1013 {
9a4105ab
AC
1014 if (deprecated_target_wait_hook)
1015 ecs->ptid = deprecated_target_wait_hook (ecs->waiton_ptid, ecs->wp);
cd0fc7c3 1016 else
39f77062 1017 ecs->ptid = target_wait (ecs->waiton_ptid, ecs->wp);
c906108c 1018
cd0fc7c3
SS
1019 /* Now figure out what to do with the result of the result. */
1020 handle_inferior_event (ecs);
c906108c 1021
cd0fc7c3
SS
1022 if (!ecs->wait_some_more)
1023 break;
1024 }
1025 do_cleanups (old_cleanups);
1026}
c906108c 1027
43ff13b4
JM
1028/* Asynchronous version of wait_for_inferior. It is called by the
1029 event loop whenever a change of state is detected on the file
1030 descriptor corresponding to the target. It can be called more than
1031 once to complete a single execution command. In such cases we need
1032 to keep the state in a global variable ASYNC_ECSS. If it is the
1033 last time that this function is called for a single execution
1034 command, then report to the user that the inferior has stopped, and
1035 do the necessary cleanups. */
1036
1037struct execution_control_state async_ecss;
1038struct execution_control_state *async_ecs;
1039
1040void
fba45db2 1041fetch_inferior_event (void *client_data)
43ff13b4
JM
1042{
1043 static struct cleanup *old_cleanups;
1044
c5aa993b 1045 async_ecs = &async_ecss;
43ff13b4
JM
1046
1047 if (!async_ecs->wait_some_more)
1048 {
488f131b 1049 old_cleanups = make_exec_cleanup (delete_step_resume_breakpoint,
c5aa993b 1050 &step_resume_breakpoint);
43ff13b4
JM
1051
1052 /* Fill in with reasonable starting values. */
1053 init_execution_control_state (async_ecs);
1054
43ff13b4 1055 /* We'll update this if & when we switch to a new thread. */
39f77062 1056 previous_inferior_ptid = inferior_ptid;
43ff13b4
JM
1057
1058 overlay_cache_invalid = 1;
1059
1060 /* We have to invalidate the registers BEFORE calling target_wait
c5aa993b
JM
1061 because they can be loaded from the target while in target_wait.
1062 This makes remote debugging a bit more efficient for those
1063 targets that provide critical registers as part of their normal
1064 status mechanism. */
43ff13b4
JM
1065
1066 registers_changed ();
1067 }
1068
9a4105ab 1069 if (deprecated_target_wait_hook)
488f131b 1070 async_ecs->ptid =
9a4105ab 1071 deprecated_target_wait_hook (async_ecs->waiton_ptid, async_ecs->wp);
43ff13b4 1072 else
39f77062 1073 async_ecs->ptid = target_wait (async_ecs->waiton_ptid, async_ecs->wp);
43ff13b4
JM
1074
1075 /* Now figure out what to do with the result of the result. */
1076 handle_inferior_event (async_ecs);
1077
1078 if (!async_ecs->wait_some_more)
1079 {
adf40b2e 1080 /* Do only the cleanups that have been added by this
488f131b
JB
1081 function. Let the continuations for the commands do the rest,
1082 if there are any. */
43ff13b4
JM
1083 do_exec_cleanups (old_cleanups);
1084 normal_stop ();
c2d11a7d
JM
1085 if (step_multi && stop_step)
1086 inferior_event_handler (INF_EXEC_CONTINUE, NULL);
1087 else
1088 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
43ff13b4
JM
1089 }
1090}
1091
cd0fc7c3
SS
1092/* Prepare an execution control state for looping through a
1093 wait_for_inferior-type loop. */
1094
1095void
96baa820 1096init_execution_control_state (struct execution_control_state *ecs)
cd0fc7c3 1097{
6ad80df0 1098 ecs->another_trap = 0;
cd0fc7c3 1099 ecs->random_signal = 0;
68f53502 1100 ecs->step_after_step_resume_breakpoint = 0;
cd0fc7c3 1101 ecs->handling_longjmp = 0; /* FIXME */
cd0fc7c3
SS
1102 ecs->stepping_through_solib_after_catch = 0;
1103 ecs->stepping_through_solib_catchpoints = NULL;
cd0fc7c3
SS
1104 ecs->sal = find_pc_line (prev_pc, 0);
1105 ecs->current_line = ecs->sal.line;
1106 ecs->current_symtab = ecs->sal.symtab;
1107 ecs->infwait_state = infwait_normal_state;
39f77062 1108 ecs->waiton_ptid = pid_to_ptid (-1);
cd0fc7c3
SS
1109 ecs->wp = &(ecs->ws);
1110}
1111
e02bc4cc 1112/* Return the cached copy of the last pid/waitstatus returned by
9a4105ab
AC
1113 target_wait()/deprecated_target_wait_hook(). The data is actually
1114 cached by handle_inferior_event(), which gets called immediately
1115 after target_wait()/deprecated_target_wait_hook(). */
e02bc4cc
DS
1116
1117void
488f131b 1118get_last_target_status (ptid_t *ptidp, struct target_waitstatus *status)
e02bc4cc 1119{
39f77062 1120 *ptidp = target_last_wait_ptid;
e02bc4cc
DS
1121 *status = target_last_waitstatus;
1122}
1123
ac264b3b
MS
1124void
1125nullify_last_target_wait_ptid (void)
1126{
1127 target_last_wait_ptid = minus_one_ptid;
1128}
1129
dd80620e
MS
1130/* Switch thread contexts, maintaining "infrun state". */
1131
1132static void
1133context_switch (struct execution_control_state *ecs)
1134{
1135 /* Caution: it may happen that the new thread (or the old one!)
1136 is not in the thread list. In this case we must not attempt
1137 to "switch context", or we run the risk that our context may
1138 be lost. This may happen as a result of the target module
1139 mishandling thread creation. */
1140
fd48f117
DJ
1141 if (debug_infrun)
1142 {
1143 fprintf_unfiltered (gdb_stdlog, "infrun: Switching context from %s ",
1144 target_pid_to_str (inferior_ptid));
1145 fprintf_unfiltered (gdb_stdlog, "to %s\n",
1146 target_pid_to_str (ecs->ptid));
1147 }
1148
dd80620e 1149 if (in_thread_list (inferior_ptid) && in_thread_list (ecs->ptid))
488f131b 1150 { /* Perform infrun state context switch: */
dd80620e 1151 /* Save infrun state for the old thread. */
0ce3d317 1152 save_infrun_state (inferior_ptid, prev_pc,
dd80620e 1153 trap_expected, step_resume_breakpoint,
15960608 1154 step_range_start,
aa0cd9c1 1155 step_range_end, &step_frame_id,
dd80620e
MS
1156 ecs->handling_longjmp, ecs->another_trap,
1157 ecs->stepping_through_solib_after_catch,
1158 ecs->stepping_through_solib_catchpoints,
f2c9ca08 1159 ecs->current_line, ecs->current_symtab);
dd80620e
MS
1160
1161 /* Load infrun state for the new thread. */
0ce3d317 1162 load_infrun_state (ecs->ptid, &prev_pc,
dd80620e 1163 &trap_expected, &step_resume_breakpoint,
15960608 1164 &step_range_start,
aa0cd9c1 1165 &step_range_end, &step_frame_id,
dd80620e
MS
1166 &ecs->handling_longjmp, &ecs->another_trap,
1167 &ecs->stepping_through_solib_after_catch,
1168 &ecs->stepping_through_solib_catchpoints,
f2c9ca08 1169 &ecs->current_line, &ecs->current_symtab);
dd80620e 1170 }
6a6b96b9
UW
1171
1172 switch_to_thread (ecs->ptid);
dd80620e
MS
1173}
1174
4fa8626c
DJ
1175static void
1176adjust_pc_after_break (struct execution_control_state *ecs)
1177{
8aad930b 1178 CORE_ADDR breakpoint_pc;
4fa8626c
DJ
1179
1180 /* If this target does not decrement the PC after breakpoints, then
1181 we have nothing to do. */
b798847d 1182 if (gdbarch_decr_pc_after_break (current_gdbarch) == 0)
4fa8626c
DJ
1183 return;
1184
1185 /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP. If
1186 we aren't, just return.
9709f61c
DJ
1187
1188 We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
b798847d
UW
1189 affected by gdbarch_decr_pc_after_break. Other waitkinds which are
1190 implemented by software breakpoints should be handled through the normal
1191 breakpoint layer.
8fb3e588 1192
4fa8626c
DJ
1193 NOTE drow/2004-01-31: On some targets, breakpoints may generate
1194 different signals (SIGILL or SIGEMT for instance), but it is less
1195 clear where the PC is pointing afterwards. It may not match
b798847d
UW
1196 gdbarch_decr_pc_after_break. I don't know any specific target that
1197 generates these signals at breakpoints (the code has been in GDB since at
1198 least 1992) so I can not guess how to handle them here.
8fb3e588 1199
e6cf7916
UW
1200 In earlier versions of GDB, a target with
1201 gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
b798847d
UW
1202 watchpoint affected by gdbarch_decr_pc_after_break. I haven't found any
1203 target with both of these set in GDB history, and it seems unlikely to be
1204 correct, so gdbarch_have_nonsteppable_watchpoint is not checked here. */
4fa8626c
DJ
1205
1206 if (ecs->ws.kind != TARGET_WAITKIND_STOPPED)
1207 return;
1208
1209 if (ecs->ws.value.sig != TARGET_SIGNAL_TRAP)
1210 return;
1211
8aad930b
AC
1212 /* Find the location where (if we've hit a breakpoint) the
1213 breakpoint would be. */
b798847d
UW
1214 breakpoint_pc = read_pc_pid (ecs->ptid) - gdbarch_decr_pc_after_break
1215 (current_gdbarch);
8aad930b 1216
1c0fdd0e
UW
1217 /* Check whether there actually is a software breakpoint inserted
1218 at that location. */
1219 if (software_breakpoint_inserted_here_p (breakpoint_pc))
8aad930b 1220 {
1c0fdd0e
UW
1221 /* When using hardware single-step, a SIGTRAP is reported for both
1222 a completed single-step and a software breakpoint. Need to
1223 differentiate between the two, as the latter needs adjusting
1224 but the former does not.
1225
1226 The SIGTRAP can be due to a completed hardware single-step only if
1227 - we didn't insert software single-step breakpoints
1228 - the thread to be examined is still the current thread
1229 - this thread is currently being stepped
1230
1231 If any of these events did not occur, we must have stopped due
1232 to hitting a software breakpoint, and have to back up to the
1233 breakpoint address.
1234
1235 As a special case, we could have hardware single-stepped a
1236 software breakpoint. In this case (prev_pc == breakpoint_pc),
1237 we also need to back up to the breakpoint address. */
1238
1239 if (singlestep_breakpoints_inserted_p
1240 || !ptid_equal (ecs->ptid, inferior_ptid)
1241 || !currently_stepping (ecs)
1242 || prev_pc == breakpoint_pc)
8aad930b
AC
1243 write_pc_pid (breakpoint_pc, ecs->ptid);
1244 }
4fa8626c
DJ
1245}
1246
cd0fc7c3
SS
1247/* Given an execution control state that has been freshly filled in
1248 by an event from the inferior, figure out what it means and take
1249 appropriate action. */
c906108c 1250
cd0fc7c3 1251void
96baa820 1252handle_inferior_event (struct execution_control_state *ecs)
cd0fc7c3 1253{
c8edd8b4 1254 int sw_single_step_trap_p = 0;
d983da9c
DJ
1255 int stopped_by_watchpoint;
1256 int stepped_after_stopped_by_watchpoint = 0;
cd0fc7c3 1257
e02bc4cc 1258 /* Cache the last pid/waitstatus. */
39f77062 1259 target_last_wait_ptid = ecs->ptid;
e02bc4cc
DS
1260 target_last_waitstatus = *ecs->wp;
1261
4fa8626c
DJ
1262 adjust_pc_after_break (ecs);
1263
488f131b
JB
1264 switch (ecs->infwait_state)
1265 {
1266 case infwait_thread_hop_state:
527159b7 1267 if (debug_infrun)
8a9de0e4 1268 fprintf_unfiltered (gdb_stdlog, "infrun: infwait_thread_hop_state\n");
488f131b
JB
1269 /* Cancel the waiton_ptid. */
1270 ecs->waiton_ptid = pid_to_ptid (-1);
65e82032 1271 break;
b83266a0 1272
488f131b 1273 case infwait_normal_state:
527159b7 1274 if (debug_infrun)
8a9de0e4 1275 fprintf_unfiltered (gdb_stdlog, "infrun: infwait_normal_state\n");
d983da9c
DJ
1276 break;
1277
1278 case infwait_step_watch_state:
1279 if (debug_infrun)
1280 fprintf_unfiltered (gdb_stdlog,
1281 "infrun: infwait_step_watch_state\n");
1282
1283 stepped_after_stopped_by_watchpoint = 1;
488f131b 1284 break;
b83266a0 1285
488f131b 1286 case infwait_nonstep_watch_state:
527159b7 1287 if (debug_infrun)
8a9de0e4
AC
1288 fprintf_unfiltered (gdb_stdlog,
1289 "infrun: infwait_nonstep_watch_state\n");
488f131b 1290 insert_breakpoints ();
c906108c 1291
488f131b
JB
1292 /* FIXME-maybe: is this cleaner than setting a flag? Does it
1293 handle things like signals arriving and other things happening
1294 in combination correctly? */
1295 stepped_after_stopped_by_watchpoint = 1;
1296 break;
65e82032
AC
1297
1298 default:
e2e0b3e5 1299 internal_error (__FILE__, __LINE__, _("bad switch"));
488f131b
JB
1300 }
1301 ecs->infwait_state = infwait_normal_state;
c906108c 1302
35f196d9 1303 reinit_frame_cache ();
c906108c 1304
488f131b 1305 /* If it's a new process, add it to the thread database */
c906108c 1306
488f131b 1307 ecs->new_thread_event = (!ptid_equal (ecs->ptid, inferior_ptid)
b9b5d7ea 1308 && !ptid_equal (ecs->ptid, minus_one_ptid)
488f131b
JB
1309 && !in_thread_list (ecs->ptid));
1310
1311 if (ecs->ws.kind != TARGET_WAITKIND_EXITED
1312 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED && ecs->new_thread_event)
1313 {
1314 add_thread (ecs->ptid);
c906108c 1315
488f131b
JB
1316 ui_out_text (uiout, "[New ");
1317 ui_out_text (uiout, target_pid_or_tid_to_str (ecs->ptid));
1318 ui_out_text (uiout, "]\n");
488f131b 1319 }
c906108c 1320
488f131b
JB
1321 switch (ecs->ws.kind)
1322 {
1323 case TARGET_WAITKIND_LOADED:
527159b7 1324 if (debug_infrun)
8a9de0e4 1325 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_LOADED\n");
b0f4b84b
DJ
1326 /* Ignore gracefully during startup of the inferior, as it might
1327 be the shell which has just loaded some objects, otherwise
1328 add the symbols for the newly loaded objects. Also ignore at
1329 the beginning of an attach or remote session; we will query
1330 the full list of libraries once the connection is
1331 established. */
c0236d92 1332 if (stop_soon == NO_STOP_QUIETLY)
488f131b 1333 {
b0f4b84b
DJ
1334 int breakpoints_were_inserted;
1335
488f131b
JB
1336 /* Remove breakpoints, SOLIB_ADD might adjust
1337 breakpoint addresses via breakpoint_re_set. */
b0f4b84b 1338 breakpoints_were_inserted = breakpoints_inserted;
488f131b
JB
1339 if (breakpoints_inserted)
1340 remove_breakpoints ();
b0f4b84b 1341 breakpoints_inserted = 0;
c906108c 1342
488f131b
JB
1343 /* Check for any newly added shared libraries if we're
1344 supposed to be adding them automatically. Switch
1345 terminal for any messages produced by
1346 breakpoint_re_set. */
1347 target_terminal_ours_for_output ();
aff6338a 1348 /* NOTE: cagney/2003-11-25: Make certain that the target
8fb3e588
AC
1349 stack's section table is kept up-to-date. Architectures,
1350 (e.g., PPC64), use the section table to perform
1351 operations such as address => section name and hence
1352 require the table to contain all sections (including
1353 those found in shared libraries). */
aff6338a 1354 /* NOTE: cagney/2003-11-25: Pass current_target and not
8fb3e588
AC
1355 exec_ops to SOLIB_ADD. This is because current GDB is
1356 only tooled to propagate section_table changes out from
1357 the "current_target" (see target_resize_to_sections), and
1358 not up from the exec stratum. This, of course, isn't
1359 right. "infrun.c" should only interact with the
1360 exec/process stratum, instead relying on the target stack
1361 to propagate relevant changes (stop, section table
1362 changed, ...) up to other layers. */
b0f4b84b 1363#ifdef SOLIB_ADD
aff6338a 1364 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
b0f4b84b
DJ
1365#else
1366 solib_add (NULL, 0, &current_target, auto_solib_add);
1367#endif
488f131b
JB
1368 target_terminal_inferior ();
1369
b0f4b84b
DJ
1370 /* If requested, stop when the dynamic linker notifies
1371 gdb of events. This allows the user to get control
1372 and place breakpoints in initializer routines for
1373 dynamically loaded objects (among other things). */
1374 if (stop_on_solib_events)
1375 {
1376 stop_stepping (ecs);
1377 return;
1378 }
1379
1380 /* NOTE drow/2007-05-11: This might be a good place to check
1381 for "catch load". */
1382
488f131b 1383 /* Reinsert breakpoints and continue. */
b0f4b84b
DJ
1384 if (breakpoints_were_inserted)
1385 {
1386 insert_breakpoints ();
1387 breakpoints_inserted = 1;
1388 }
488f131b 1389 }
b0f4b84b
DJ
1390
1391 /* If we are skipping through a shell, or through shared library
1392 loading that we aren't interested in, resume the program. If
1393 we're running the program normally, also resume. But stop if
1394 we're attaching or setting up a remote connection. */
1395 if (stop_soon == STOP_QUIETLY || stop_soon == NO_STOP_QUIETLY)
1396 {
1397 resume (0, TARGET_SIGNAL_0);
1398 prepare_to_wait (ecs);
1399 return;
1400 }
1401
1402 break;
c5aa993b 1403
488f131b 1404 case TARGET_WAITKIND_SPURIOUS:
527159b7 1405 if (debug_infrun)
8a9de0e4 1406 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SPURIOUS\n");
488f131b
JB
1407 resume (0, TARGET_SIGNAL_0);
1408 prepare_to_wait (ecs);
1409 return;
c5aa993b 1410
488f131b 1411 case TARGET_WAITKIND_EXITED:
527159b7 1412 if (debug_infrun)
8a9de0e4 1413 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXITED\n");
488f131b
JB
1414 target_terminal_ours (); /* Must do this before mourn anyway */
1415 print_stop_reason (EXITED, ecs->ws.value.integer);
1416
1417 /* Record the exit code in the convenience variable $_exitcode, so
1418 that the user can inspect this again later. */
1419 set_internalvar (lookup_internalvar ("_exitcode"),
1420 value_from_longest (builtin_type_int,
1421 (LONGEST) ecs->ws.value.integer));
1422 gdb_flush (gdb_stdout);
1423 target_mourn_inferior ();
1c0fdd0e 1424 singlestep_breakpoints_inserted_p = 0;
488f131b
JB
1425 stop_print_frame = 0;
1426 stop_stepping (ecs);
1427 return;
c5aa993b 1428
488f131b 1429 case TARGET_WAITKIND_SIGNALLED:
527159b7 1430 if (debug_infrun)
8a9de0e4 1431 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SIGNALLED\n");
488f131b
JB
1432 stop_print_frame = 0;
1433 stop_signal = ecs->ws.value.sig;
1434 target_terminal_ours (); /* Must do this before mourn anyway */
c5aa993b 1435
488f131b
JB
1436 /* Note: By definition of TARGET_WAITKIND_SIGNALLED, we shouldn't
1437 reach here unless the inferior is dead. However, for years
1438 target_kill() was called here, which hints that fatal signals aren't
1439 really fatal on some systems. If that's true, then some changes
1440 may be needed. */
1441 target_mourn_inferior ();
c906108c 1442
488f131b 1443 print_stop_reason (SIGNAL_EXITED, stop_signal);
1c0fdd0e 1444 singlestep_breakpoints_inserted_p = 0;
488f131b
JB
1445 stop_stepping (ecs);
1446 return;
c906108c 1447
488f131b
JB
1448 /* The following are the only cases in which we keep going;
1449 the above cases end in a continue or goto. */
1450 case TARGET_WAITKIND_FORKED:
deb3b17b 1451 case TARGET_WAITKIND_VFORKED:
527159b7 1452 if (debug_infrun)
8a9de0e4 1453 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_FORKED\n");
488f131b
JB
1454 stop_signal = TARGET_SIGNAL_TRAP;
1455 pending_follow.kind = ecs->ws.kind;
1456
8e7d2c16
DJ
1457 pending_follow.fork_event.parent_pid = PIDGET (ecs->ptid);
1458 pending_follow.fork_event.child_pid = ecs->ws.value.related_pid;
c906108c 1459
5a2901d9
DJ
1460 if (!ptid_equal (ecs->ptid, inferior_ptid))
1461 {
1462 context_switch (ecs);
35f196d9 1463 reinit_frame_cache ();
5a2901d9
DJ
1464 }
1465
488f131b 1466 stop_pc = read_pc ();
675bf4cb 1467
d983da9c 1468 stop_bpstat = bpstat_stop_status (stop_pc, ecs->ptid);
675bf4cb 1469
488f131b 1470 ecs->random_signal = !bpstat_explains_signal (stop_bpstat);
04e68871
DJ
1471
1472 /* If no catchpoint triggered for this, then keep going. */
1473 if (ecs->random_signal)
1474 {
1475 stop_signal = TARGET_SIGNAL_0;
1476 keep_going (ecs);
1477 return;
1478 }
488f131b
JB
1479 goto process_event_stop_test;
1480
1481 case TARGET_WAITKIND_EXECD:
527159b7 1482 if (debug_infrun)
fc5261f2 1483 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXECD\n");
488f131b
JB
1484 stop_signal = TARGET_SIGNAL_TRAP;
1485
7d2830a3 1486 /* NOTE drow/2002-12-05: This code should be pushed down into the
8fb3e588
AC
1487 target_wait function. Until then following vfork on HP/UX 10.20
1488 is probably broken by this. Of course, it's broken anyway. */
488f131b
JB
1489 /* Is this a target which reports multiple exec events per actual
1490 call to exec()? (HP-UX using ptrace does, for example.) If so,
1491 ignore all but the last one. Just resume the exec'r, and wait
1492 for the next exec event. */
1493 if (inferior_ignoring_leading_exec_events)
1494 {
1495 inferior_ignoring_leading_exec_events--;
488f131b
JB
1496 target_resume (ecs->ptid, 0, TARGET_SIGNAL_0);
1497 prepare_to_wait (ecs);
1498 return;
1499 }
1500 inferior_ignoring_leading_exec_events =
1501 target_reported_exec_events_per_exec_call () - 1;
1502
1503 pending_follow.execd_pathname =
1504 savestring (ecs->ws.value.execd_pathname,
1505 strlen (ecs->ws.value.execd_pathname));
1506
488f131b
JB
1507 /* This causes the eventpoints and symbol table to be reset. Must
1508 do this now, before trying to determine whether to stop. */
1509 follow_exec (PIDGET (inferior_ptid), pending_follow.execd_pathname);
1510 xfree (pending_follow.execd_pathname);
c906108c 1511
488f131b
JB
1512 stop_pc = read_pc_pid (ecs->ptid);
1513 ecs->saved_inferior_ptid = inferior_ptid;
1514 inferior_ptid = ecs->ptid;
675bf4cb 1515
d983da9c 1516 stop_bpstat = bpstat_stop_status (stop_pc, ecs->ptid);
675bf4cb 1517
488f131b
JB
1518 ecs->random_signal = !bpstat_explains_signal (stop_bpstat);
1519 inferior_ptid = ecs->saved_inferior_ptid;
04e68871 1520
5a2901d9
DJ
1521 if (!ptid_equal (ecs->ptid, inferior_ptid))
1522 {
1523 context_switch (ecs);
35f196d9 1524 reinit_frame_cache ();
5a2901d9
DJ
1525 }
1526
04e68871
DJ
1527 /* If no catchpoint triggered for this, then keep going. */
1528 if (ecs->random_signal)
1529 {
1530 stop_signal = TARGET_SIGNAL_0;
1531 keep_going (ecs);
1532 return;
1533 }
488f131b
JB
1534 goto process_event_stop_test;
1535
b4dc5ffa
MK
1536 /* Be careful not to try to gather much state about a thread
1537 that's in a syscall. It's frequently a losing proposition. */
488f131b 1538 case TARGET_WAITKIND_SYSCALL_ENTRY:
527159b7 1539 if (debug_infrun)
8a9de0e4 1540 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SYSCALL_ENTRY\n");
488f131b
JB
1541 resume (0, TARGET_SIGNAL_0);
1542 prepare_to_wait (ecs);
1543 return;
c906108c 1544
488f131b
JB
1545 /* Before examining the threads further, step this thread to
1546 get it entirely out of the syscall. (We get notice of the
1547 event when the thread is just on the verge of exiting a
1548 syscall. Stepping one instruction seems to get it back
b4dc5ffa 1549 into user code.) */
488f131b 1550 case TARGET_WAITKIND_SYSCALL_RETURN:
527159b7 1551 if (debug_infrun)
8a9de0e4 1552 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SYSCALL_RETURN\n");
488f131b 1553 target_resume (ecs->ptid, 1, TARGET_SIGNAL_0);
488f131b
JB
1554 prepare_to_wait (ecs);
1555 return;
c906108c 1556
488f131b 1557 case TARGET_WAITKIND_STOPPED:
527159b7 1558 if (debug_infrun)
8a9de0e4 1559 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_STOPPED\n");
488f131b
JB
1560 stop_signal = ecs->ws.value.sig;
1561 break;
c906108c 1562
488f131b
JB
1563 /* We had an event in the inferior, but we are not interested
1564 in handling it at this level. The lower layers have already
8e7d2c16 1565 done what needs to be done, if anything.
8fb3e588
AC
1566
1567 One of the possible circumstances for this is when the
1568 inferior produces output for the console. The inferior has
1569 not stopped, and we are ignoring the event. Another possible
1570 circumstance is any event which the lower level knows will be
1571 reported multiple times without an intervening resume. */
488f131b 1572 case TARGET_WAITKIND_IGNORE:
527159b7 1573 if (debug_infrun)
8a9de0e4 1574 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_IGNORE\n");
8e7d2c16 1575 prepare_to_wait (ecs);
488f131b
JB
1576 return;
1577 }
c906108c 1578
488f131b
JB
1579 /* We may want to consider not doing a resume here in order to give
1580 the user a chance to play with the new thread. It might be good
1581 to make that a user-settable option. */
c906108c 1582
488f131b
JB
1583 /* At this point, all threads are stopped (happens automatically in
1584 either the OS or the native code). Therefore we need to continue
1585 all threads in order to make progress. */
1586 if (ecs->new_thread_event)
1587 {
1588 target_resume (RESUME_ALL, 0, TARGET_SIGNAL_0);
1589 prepare_to_wait (ecs);
1590 return;
1591 }
c906108c 1592
488f131b
JB
1593 stop_pc = read_pc_pid (ecs->ptid);
1594
527159b7 1595 if (debug_infrun)
8a9de0e4 1596 fprintf_unfiltered (gdb_stdlog, "infrun: stop_pc = 0x%s\n", paddr_nz (stop_pc));
527159b7 1597
9f976b41
DJ
1598 if (stepping_past_singlestep_breakpoint)
1599 {
1c0fdd0e 1600 gdb_assert (singlestep_breakpoints_inserted_p);
9f976b41
DJ
1601 gdb_assert (ptid_equal (singlestep_ptid, ecs->ptid));
1602 gdb_assert (!ptid_equal (singlestep_ptid, saved_singlestep_ptid));
1603
1604 stepping_past_singlestep_breakpoint = 0;
1605
1606 /* We've either finished single-stepping past the single-step
8fb3e588
AC
1607 breakpoint, or stopped for some other reason. It would be nice if
1608 we could tell, but we can't reliably. */
9f976b41 1609 if (stop_signal == TARGET_SIGNAL_TRAP)
8fb3e588 1610 {
527159b7 1611 if (debug_infrun)
8a9de0e4 1612 fprintf_unfiltered (gdb_stdlog, "infrun: stepping_past_singlestep_breakpoint\n");
9f976b41 1613 /* Pull the single step breakpoints out of the target. */
e0cd558a 1614 remove_single_step_breakpoints ();
9f976b41
DJ
1615 singlestep_breakpoints_inserted_p = 0;
1616
1617 ecs->random_signal = 0;
1618
1619 ecs->ptid = saved_singlestep_ptid;
1620 context_switch (ecs);
9a4105ab
AC
1621 if (deprecated_context_hook)
1622 deprecated_context_hook (pid_to_thread_id (ecs->ptid));
9f976b41
DJ
1623
1624 resume (1, TARGET_SIGNAL_0);
1625 prepare_to_wait (ecs);
1626 return;
1627 }
1628 }
1629
1630 stepping_past_singlestep_breakpoint = 0;
1631
6a6b96b9
UW
1632 if (stepping_past_breakpoint)
1633 {
1634 stepping_past_breakpoint = 0;
1635
1636 /* If we stopped for some other reason than single-stepping, ignore
1637 the fact that we were supposed to switch back. */
1638 if (stop_signal == TARGET_SIGNAL_TRAP)
1639 {
1640 if (debug_infrun)
1641 fprintf_unfiltered (gdb_stdlog,
1642 "infrun: stepping_past_breakpoint\n");
1643
1644 /* Pull the single step breakpoints out of the target. */
1645 if (singlestep_breakpoints_inserted_p)
1646 {
1647 remove_single_step_breakpoints ();
1648 singlestep_breakpoints_inserted_p = 0;
1649 }
1650
1651 /* Note: We do not call context_switch at this point, as the
1652 context is already set up for stepping the original thread. */
1653 switch_to_thread (stepping_past_breakpoint_ptid);
1654 /* Suppress spurious "Switching to ..." message. */
1655 previous_inferior_ptid = inferior_ptid;
1656
1657 resume (1, TARGET_SIGNAL_0);
1658 prepare_to_wait (ecs);
1659 return;
1660 }
1661 }
1662
488f131b
JB
1663 /* See if a thread hit a thread-specific breakpoint that was meant for
1664 another thread. If so, then step that thread past the breakpoint,
1665 and continue it. */
1666
1667 if (stop_signal == TARGET_SIGNAL_TRAP)
1668 {
9f976b41
DJ
1669 int thread_hop_needed = 0;
1670
f8d40ec8
JB
1671 /* Check if a regular breakpoint has been hit before checking
1672 for a potential single step breakpoint. Otherwise, GDB will
1673 not see this breakpoint hit when stepping onto breakpoints. */
4fa8626c 1674 if (breakpoints_inserted && breakpoint_here_p (stop_pc))
488f131b 1675 {
c5aa993b 1676 ecs->random_signal = 0;
4fa8626c 1677 if (!breakpoint_thread_match (stop_pc, ecs->ptid))
9f976b41
DJ
1678 thread_hop_needed = 1;
1679 }
1c0fdd0e 1680 else if (singlestep_breakpoints_inserted_p)
9f976b41 1681 {
fd48f117
DJ
1682 /* We have not context switched yet, so this should be true
1683 no matter which thread hit the singlestep breakpoint. */
1684 gdb_assert (ptid_equal (inferior_ptid, singlestep_ptid));
1685 if (debug_infrun)
1686 fprintf_unfiltered (gdb_stdlog, "infrun: software single step "
1687 "trap for %s\n",
1688 target_pid_to_str (ecs->ptid));
1689
9f976b41
DJ
1690 ecs->random_signal = 0;
1691 /* The call to in_thread_list is necessary because PTIDs sometimes
1692 change when we go from single-threaded to multi-threaded. If
1693 the singlestep_ptid is still in the list, assume that it is
1694 really different from ecs->ptid. */
1695 if (!ptid_equal (singlestep_ptid, ecs->ptid)
1696 && in_thread_list (singlestep_ptid))
1697 {
fd48f117
DJ
1698 /* If the PC of the thread we were trying to single-step
1699 has changed, discard this event (which we were going
1700 to ignore anyway), and pretend we saw that thread
1701 trap. This prevents us continuously moving the
1702 single-step breakpoint forward, one instruction at a
1703 time. If the PC has changed, then the thread we were
1704 trying to single-step has trapped or been signalled,
1705 but the event has not been reported to GDB yet.
1706
1707 There might be some cases where this loses signal
1708 information, if a signal has arrived at exactly the
1709 same time that the PC changed, but this is the best
1710 we can do with the information available. Perhaps we
1711 should arrange to report all events for all threads
1712 when they stop, or to re-poll the remote looking for
1713 this particular thread (i.e. temporarily enable
1714 schedlock). */
1715 if (read_pc_pid (singlestep_ptid) != singlestep_pc)
1716 {
1717 if (debug_infrun)
1718 fprintf_unfiltered (gdb_stdlog, "infrun: unexpected thread,"
1719 " but expected thread advanced also\n");
1720
1721 /* The current context still belongs to
1722 singlestep_ptid. Don't swap here, since that's
1723 the context we want to use. Just fudge our
1724 state and continue. */
1725 ecs->ptid = singlestep_ptid;
1726 stop_pc = read_pc_pid (ecs->ptid);
1727 }
1728 else
1729 {
1730 if (debug_infrun)
1731 fprintf_unfiltered (gdb_stdlog,
1732 "infrun: unexpected thread\n");
1733
1734 thread_hop_needed = 1;
1735 stepping_past_singlestep_breakpoint = 1;
1736 saved_singlestep_ptid = singlestep_ptid;
1737 }
9f976b41
DJ
1738 }
1739 }
1740
1741 if (thread_hop_needed)
8fb3e588
AC
1742 {
1743 int remove_status;
1744
527159b7 1745 if (debug_infrun)
8a9de0e4 1746 fprintf_unfiltered (gdb_stdlog, "infrun: thread_hop_needed\n");
527159b7 1747
8fb3e588
AC
1748 /* Saw a breakpoint, but it was hit by the wrong thread.
1749 Just continue. */
1750
1c0fdd0e 1751 if (singlestep_breakpoints_inserted_p)
488f131b 1752 {
8fb3e588 1753 /* Pull the single step breakpoints out of the target. */
e0cd558a 1754 remove_single_step_breakpoints ();
8fb3e588
AC
1755 singlestep_breakpoints_inserted_p = 0;
1756 }
1757
1758 remove_status = remove_breakpoints ();
1759 /* Did we fail to remove breakpoints? If so, try
1760 to set the PC past the bp. (There's at least
1761 one situation in which we can fail to remove
1762 the bp's: On HP-UX's that use ttrace, we can't
1763 change the address space of a vforking child
1764 process until the child exits (well, okay, not
1765 then either :-) or execs. */
1766 if (remove_status != 0)
1767 {
1768 /* FIXME! This is obviously non-portable! */
1769 write_pc_pid (stop_pc + 4, ecs->ptid);
1770 /* We need to restart all the threads now,
1771 * unles we're running in scheduler-locked mode.
1772 * Use currently_stepping to determine whether to
1773 * step or continue.
1774 */
1775 /* FIXME MVS: is there any reason not to call resume()? */
1776 if (scheduler_mode == schedlock_on)
1777 target_resume (ecs->ptid,
1778 currently_stepping (ecs), TARGET_SIGNAL_0);
488f131b 1779 else
8fb3e588
AC
1780 target_resume (RESUME_ALL,
1781 currently_stepping (ecs), TARGET_SIGNAL_0);
1782 prepare_to_wait (ecs);
1783 return;
1784 }
1785 else
1786 { /* Single step */
1787 breakpoints_inserted = 0;
1788 if (!ptid_equal (inferior_ptid, ecs->ptid))
1789 context_switch (ecs);
1790 ecs->waiton_ptid = ecs->ptid;
1791 ecs->wp = &(ecs->ws);
1792 ecs->another_trap = 1;
1793
1794 ecs->infwait_state = infwait_thread_hop_state;
1795 keep_going (ecs);
1796 registers_changed ();
1797 return;
1798 }
488f131b 1799 }
1c0fdd0e 1800 else if (singlestep_breakpoints_inserted_p)
8fb3e588
AC
1801 {
1802 sw_single_step_trap_p = 1;
1803 ecs->random_signal = 0;
1804 }
488f131b
JB
1805 }
1806 else
1807 ecs->random_signal = 1;
c906108c 1808
488f131b 1809 /* See if something interesting happened to the non-current thread. If
b40c7d58
DJ
1810 so, then switch to that thread. */
1811 if (!ptid_equal (ecs->ptid, inferior_ptid))
488f131b 1812 {
527159b7 1813 if (debug_infrun)
8a9de0e4 1814 fprintf_unfiltered (gdb_stdlog, "infrun: context switch\n");
527159b7 1815
488f131b 1816 context_switch (ecs);
c5aa993b 1817
9a4105ab
AC
1818 if (deprecated_context_hook)
1819 deprecated_context_hook (pid_to_thread_id (ecs->ptid));
488f131b 1820 }
c906108c 1821
1c0fdd0e 1822 if (singlestep_breakpoints_inserted_p)
488f131b
JB
1823 {
1824 /* Pull the single step breakpoints out of the target. */
e0cd558a 1825 remove_single_step_breakpoints ();
488f131b
JB
1826 singlestep_breakpoints_inserted_p = 0;
1827 }
c906108c 1828
d983da9c
DJ
1829 if (stepped_after_stopped_by_watchpoint)
1830 stopped_by_watchpoint = 0;
1831 else
1832 stopped_by_watchpoint = watchpoints_triggered (&ecs->ws);
1833
1834 /* If necessary, step over this watchpoint. We'll be back to display
1835 it in a moment. */
1836 if (stopped_by_watchpoint
1837 && (HAVE_STEPPABLE_WATCHPOINT
1838 || gdbarch_have_nonsteppable_watchpoint (current_gdbarch)))
488f131b 1839 {
527159b7 1840 if (debug_infrun)
8a9de0e4 1841 fprintf_unfiltered (gdb_stdlog, "infrun: STOPPED_BY_WATCHPOINT\n");
c906108c 1842
488f131b
JB
1843 /* At this point, we are stopped at an instruction which has
1844 attempted to write to a piece of memory under control of
1845 a watchpoint. The instruction hasn't actually executed
1846 yet. If we were to evaluate the watchpoint expression
1847 now, we would get the old value, and therefore no change
1848 would seem to have occurred.
1849
1850 In order to make watchpoints work `right', we really need
1851 to complete the memory write, and then evaluate the
d983da9c
DJ
1852 watchpoint expression. We do this by single-stepping the
1853 target.
1854
1855 It may not be necessary to disable the watchpoint to stop over
1856 it. For example, the PA can (with some kernel cooperation)
1857 single step over a watchpoint without disabling the watchpoint.
1858
1859 It is far more common to need to disable a watchpoint to step
1860 the inferior over it. If we have non-steppable watchpoints,
1861 we must disable the current watchpoint; it's simplest to
1862 disable all watchpoints and breakpoints. */
1863
1864 if (!HAVE_STEPPABLE_WATCHPOINT)
1865 remove_breakpoints ();
488f131b
JB
1866 registers_changed ();
1867 target_resume (ecs->ptid, 1, TARGET_SIGNAL_0); /* Single step */
488f131b 1868 ecs->waiton_ptid = ecs->ptid;
d983da9c
DJ
1869 if (HAVE_STEPPABLE_WATCHPOINT)
1870 ecs->infwait_state = infwait_step_watch_state;
1871 else
1872 ecs->infwait_state = infwait_nonstep_watch_state;
488f131b
JB
1873 prepare_to_wait (ecs);
1874 return;
1875 }
1876
488f131b
JB
1877 ecs->stop_func_start = 0;
1878 ecs->stop_func_end = 0;
1879 ecs->stop_func_name = 0;
1880 /* Don't care about return value; stop_func_start and stop_func_name
1881 will both be 0 if it doesn't work. */
1882 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
1883 &ecs->stop_func_start, &ecs->stop_func_end);
cbf3b44a
UW
1884 ecs->stop_func_start
1885 += gdbarch_deprecated_function_start_offset (current_gdbarch);
488f131b
JB
1886 ecs->another_trap = 0;
1887 bpstat_clear (&stop_bpstat);
1888 stop_step = 0;
1889 stop_stack_dummy = 0;
1890 stop_print_frame = 1;
1891 ecs->random_signal = 0;
1892 stopped_by_random_signal = 0;
488f131b 1893
3352ef37
AC
1894 if (stop_signal == TARGET_SIGNAL_TRAP
1895 && trap_expected
1896 && gdbarch_single_step_through_delay_p (current_gdbarch)
1897 && currently_stepping (ecs))
1898 {
1899 /* We're trying to step of a breakpoint. Turns out that we're
1900 also on an instruction that needs to be stepped multiple
1901 times before it's been fully executing. E.g., architectures
1902 with a delay slot. It needs to be stepped twice, once for
1903 the instruction and once for the delay slot. */
1904 int step_through_delay
1905 = gdbarch_single_step_through_delay (current_gdbarch,
1906 get_current_frame ());
527159b7 1907 if (debug_infrun && step_through_delay)
8a9de0e4 1908 fprintf_unfiltered (gdb_stdlog, "infrun: step through delay\n");
3352ef37
AC
1909 if (step_range_end == 0 && step_through_delay)
1910 {
1911 /* The user issued a continue when stopped at a breakpoint.
1912 Set up for another trap and get out of here. */
1913 ecs->another_trap = 1;
1914 keep_going (ecs);
1915 return;
1916 }
1917 else if (step_through_delay)
1918 {
1919 /* The user issued a step when stopped at a breakpoint.
1920 Maybe we should stop, maybe we should not - the delay
1921 slot *might* correspond to a line of source. In any
1922 case, don't decide that here, just set ecs->another_trap,
1923 making sure we single-step again before breakpoints are
1924 re-inserted. */
1925 ecs->another_trap = 1;
1926 }
1927 }
1928
488f131b
JB
1929 /* Look at the cause of the stop, and decide what to do.
1930 The alternatives are:
1931 1) break; to really stop and return to the debugger,
1932 2) drop through to start up again
1933 (set ecs->another_trap to 1 to single step once)
1934 3) set ecs->random_signal to 1, and the decision between 1 and 2
1935 will be made according to the signal handling tables. */
1936
1937 /* First, distinguish signals caused by the debugger from signals
03cebad2
MK
1938 that have to do with the program's own actions. Note that
1939 breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
1940 on the operating system version. Here we detect when a SIGILL or
1941 SIGEMT is really a breakpoint and change it to SIGTRAP. We do
1942 something similar for SIGSEGV, since a SIGSEGV will be generated
1943 when we're trying to execute a breakpoint instruction on a
1944 non-executable stack. This happens for call dummy breakpoints
1945 for architectures like SPARC that place call dummies on the
1946 stack. */
488f131b
JB
1947
1948 if (stop_signal == TARGET_SIGNAL_TRAP
8fb3e588
AC
1949 || (breakpoints_inserted
1950 && (stop_signal == TARGET_SIGNAL_ILL
1951 || stop_signal == TARGET_SIGNAL_SEGV
1952 || stop_signal == TARGET_SIGNAL_EMT))
b0f4b84b
DJ
1953 || stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_NO_SIGSTOP
1954 || stop_soon == STOP_QUIETLY_REMOTE)
488f131b
JB
1955 {
1956 if (stop_signal == TARGET_SIGNAL_TRAP && stop_after_trap)
1957 {
527159b7 1958 if (debug_infrun)
8a9de0e4 1959 fprintf_unfiltered (gdb_stdlog, "infrun: stopped\n");
488f131b
JB
1960 stop_print_frame = 0;
1961 stop_stepping (ecs);
1962 return;
1963 }
c54cfec8
EZ
1964
1965 /* This is originated from start_remote(), start_inferior() and
1966 shared libraries hook functions. */
b0f4b84b 1967 if (stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_REMOTE)
488f131b 1968 {
527159b7 1969 if (debug_infrun)
8a9de0e4 1970 fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
488f131b
JB
1971 stop_stepping (ecs);
1972 return;
1973 }
1974
c54cfec8
EZ
1975 /* This originates from attach_command(). We need to overwrite
1976 the stop_signal here, because some kernels don't ignore a
1977 SIGSTOP in a subsequent ptrace(PTRACE_SONT,SOGSTOP) call.
1978 See more comments in inferior.h. */
c0236d92 1979 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP)
c54cfec8
EZ
1980 {
1981 stop_stepping (ecs);
1982 if (stop_signal == TARGET_SIGNAL_STOP)
1983 stop_signal = TARGET_SIGNAL_0;
1984 return;
1985 }
1986
d303a6c7
AC
1987 /* Don't even think about breakpoints if just proceeded over a
1988 breakpoint. */
1989 if (stop_signal == TARGET_SIGNAL_TRAP && trap_expected)
527159b7
RC
1990 {
1991 if (debug_infrun)
8a9de0e4 1992 fprintf_unfiltered (gdb_stdlog, "infrun: trap expected\n");
527159b7
RC
1993 bpstat_clear (&stop_bpstat);
1994 }
488f131b
JB
1995 else
1996 {
1997 /* See if there is a breakpoint at the current PC. */
d983da9c 1998 stop_bpstat = bpstat_stop_status (stop_pc, ecs->ptid);
488f131b 1999
488f131b
JB
2000 /* Following in case break condition called a
2001 function. */
2002 stop_print_frame = 1;
2003 }
2004
73dd234f 2005 /* NOTE: cagney/2003-03-29: These two checks for a random signal
8fb3e588
AC
2006 at one stage in the past included checks for an inferior
2007 function call's call dummy's return breakpoint. The original
2008 comment, that went with the test, read:
73dd234f 2009
8fb3e588
AC
2010 ``End of a stack dummy. Some systems (e.g. Sony news) give
2011 another signal besides SIGTRAP, so check here as well as
2012 above.''
73dd234f
AC
2013
2014 If someone ever tries to get get call dummys on a
2015 non-executable stack to work (where the target would stop
03cebad2
MK
2016 with something like a SIGSEGV), then those tests might need
2017 to be re-instated. Given, however, that the tests were only
73dd234f 2018 enabled when momentary breakpoints were not being used, I
03cebad2
MK
2019 suspect that it won't be the case.
2020
8fb3e588
AC
2021 NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
2022 be necessary for call dummies on a non-executable stack on
2023 SPARC. */
73dd234f 2024
488f131b
JB
2025 if (stop_signal == TARGET_SIGNAL_TRAP)
2026 ecs->random_signal
2027 = !(bpstat_explains_signal (stop_bpstat)
2028 || trap_expected
488f131b 2029 || (step_range_end && step_resume_breakpoint == NULL));
488f131b
JB
2030 else
2031 {
73dd234f 2032 ecs->random_signal = !bpstat_explains_signal (stop_bpstat);
488f131b
JB
2033 if (!ecs->random_signal)
2034 stop_signal = TARGET_SIGNAL_TRAP;
2035 }
2036 }
2037
2038 /* When we reach this point, we've pretty much decided
2039 that the reason for stopping must've been a random
2040 (unexpected) signal. */
2041
2042 else
2043 ecs->random_signal = 1;
488f131b 2044
04e68871 2045process_event_stop_test:
488f131b
JB
2046 /* For the program's own signals, act according to
2047 the signal handling tables. */
2048
2049 if (ecs->random_signal)
2050 {
2051 /* Signal not for debugging purposes. */
2052 int printed = 0;
2053
527159b7 2054 if (debug_infrun)
8a9de0e4 2055 fprintf_unfiltered (gdb_stdlog, "infrun: random signal %d\n", stop_signal);
527159b7 2056
488f131b
JB
2057 stopped_by_random_signal = 1;
2058
2059 if (signal_print[stop_signal])
2060 {
2061 printed = 1;
2062 target_terminal_ours_for_output ();
2063 print_stop_reason (SIGNAL_RECEIVED, stop_signal);
2064 }
2065 if (signal_stop[stop_signal])
2066 {
2067 stop_stepping (ecs);
2068 return;
2069 }
2070 /* If not going to stop, give terminal back
2071 if we took it away. */
2072 else if (printed)
2073 target_terminal_inferior ();
2074
2075 /* Clear the signal if it should not be passed. */
2076 if (signal_program[stop_signal] == 0)
2077 stop_signal = TARGET_SIGNAL_0;
2078
68f53502
AC
2079 if (prev_pc == read_pc ()
2080 && !breakpoints_inserted
2081 && breakpoint_here_p (read_pc ())
2082 && step_resume_breakpoint == NULL)
2083 {
2084 /* We were just starting a new sequence, attempting to
2085 single-step off of a breakpoint and expecting a SIGTRAP.
2086 Intead this signal arrives. This signal will take us out
2087 of the stepping range so GDB needs to remember to, when
2088 the signal handler returns, resume stepping off that
2089 breakpoint. */
2090 /* To simplify things, "continue" is forced to use the same
2091 code paths as single-step - set a breakpoint at the
2092 signal return address and then, once hit, step off that
2093 breakpoint. */
d3169d93 2094
44cbf7b5 2095 insert_step_resume_breakpoint_at_frame (get_current_frame ());
68f53502 2096 ecs->step_after_step_resume_breakpoint = 1;
9d799f85
AC
2097 keep_going (ecs);
2098 return;
68f53502 2099 }
9d799f85
AC
2100
2101 if (step_range_end != 0
2102 && stop_signal != TARGET_SIGNAL_0
2103 && stop_pc >= step_range_start && stop_pc < step_range_end
2104 && frame_id_eq (get_frame_id (get_current_frame ()),
2105 step_frame_id)
2106 && step_resume_breakpoint == NULL)
d303a6c7
AC
2107 {
2108 /* The inferior is about to take a signal that will take it
2109 out of the single step range. Set a breakpoint at the
2110 current PC (which is presumably where the signal handler
2111 will eventually return) and then allow the inferior to
2112 run free.
2113
2114 Note that this is only needed for a signal delivered
2115 while in the single-step range. Nested signals aren't a
2116 problem as they eventually all return. */
44cbf7b5 2117 insert_step_resume_breakpoint_at_frame (get_current_frame ());
9d799f85
AC
2118 keep_going (ecs);
2119 return;
d303a6c7 2120 }
9d799f85
AC
2121
2122 /* Note: step_resume_breakpoint may be non-NULL. This occures
2123 when either there's a nested signal, or when there's a
2124 pending signal enabled just as the signal handler returns
2125 (leaving the inferior at the step-resume-breakpoint without
2126 actually executing it). Either way continue until the
2127 breakpoint is really hit. */
488f131b
JB
2128 keep_going (ecs);
2129 return;
2130 }
2131
2132 /* Handle cases caused by hitting a breakpoint. */
2133 {
2134 CORE_ADDR jmp_buf_pc;
2135 struct bpstat_what what;
2136
2137 what = bpstat_what (stop_bpstat);
2138
2139 if (what.call_dummy)
2140 {
2141 stop_stack_dummy = 1;
c5aa993b 2142 }
c906108c 2143
488f131b 2144 switch (what.main_action)
c5aa993b 2145 {
488f131b
JB
2146 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
2147 /* If we hit the breakpoint at longjmp, disable it for the
2148 duration of this command. Then, install a temporary
2149 breakpoint at the target of the jmp_buf. */
527159b7 2150 if (debug_infrun)
8802d8ed 2151 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME\n");
488f131b
JB
2152 disable_longjmp_breakpoint ();
2153 remove_breakpoints ();
2154 breakpoints_inserted = 0;
91104499 2155 if (!gdbarch_get_longjmp_target_p (current_gdbarch)
60ade65d
UW
2156 || !gdbarch_get_longjmp_target (current_gdbarch,
2157 get_current_frame (), &jmp_buf_pc))
c5aa993b 2158 {
488f131b 2159 keep_going (ecs);
104c1213 2160 return;
c5aa993b 2161 }
488f131b
JB
2162
2163 /* Need to blow away step-resume breakpoint, as it
2164 interferes with us */
2165 if (step_resume_breakpoint != NULL)
104c1213 2166 {
488f131b 2167 delete_step_resume_breakpoint (&step_resume_breakpoint);
104c1213 2168 }
c906108c 2169
8fb3e588 2170 set_longjmp_resume_breakpoint (jmp_buf_pc, null_frame_id);
488f131b
JB
2171 ecs->handling_longjmp = 1; /* FIXME */
2172 keep_going (ecs);
2173 return;
c906108c 2174
488f131b
JB
2175 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
2176 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE:
527159b7 2177 if (debug_infrun)
8802d8ed 2178 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_CLEAR_LONGJMP_RESUME\n");
488f131b
JB
2179 remove_breakpoints ();
2180 breakpoints_inserted = 0;
488f131b
JB
2181 disable_longjmp_breakpoint ();
2182 ecs->handling_longjmp = 0; /* FIXME */
2183 if (what.main_action == BPSTAT_WHAT_CLEAR_LONGJMP_RESUME)
2184 break;
2185 /* else fallthrough */
2186
2187 case BPSTAT_WHAT_SINGLE:
527159b7 2188 if (debug_infrun)
8802d8ed 2189 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_SINGLE\n");
488f131b 2190 if (breakpoints_inserted)
569631c6 2191 remove_breakpoints ();
488f131b
JB
2192 breakpoints_inserted = 0;
2193 ecs->another_trap = 1;
2194 /* Still need to check other stuff, at least the case
2195 where we are stepping and step out of the right range. */
2196 break;
c906108c 2197
488f131b 2198 case BPSTAT_WHAT_STOP_NOISY:
527159b7 2199 if (debug_infrun)
8802d8ed 2200 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_NOISY\n");
488f131b 2201 stop_print_frame = 1;
c906108c 2202
d303a6c7
AC
2203 /* We are about to nuke the step_resume_breakpointt via the
2204 cleanup chain, so no need to worry about it here. */
c5aa993b 2205
488f131b
JB
2206 stop_stepping (ecs);
2207 return;
c5aa993b 2208
488f131b 2209 case BPSTAT_WHAT_STOP_SILENT:
527159b7 2210 if (debug_infrun)
8802d8ed 2211 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_SILENT\n");
488f131b 2212 stop_print_frame = 0;
c5aa993b 2213
d303a6c7
AC
2214 /* We are about to nuke the step_resume_breakpoin via the
2215 cleanup chain, so no need to worry about it here. */
c5aa993b 2216
488f131b 2217 stop_stepping (ecs);
e441088d 2218 return;
c5aa993b 2219
488f131b
JB
2220 case BPSTAT_WHAT_STEP_RESUME:
2221 /* This proably demands a more elegant solution, but, yeah
2222 right...
c5aa993b 2223
488f131b
JB
2224 This function's use of the simple variable
2225 step_resume_breakpoint doesn't seem to accomodate
2226 simultaneously active step-resume bp's, although the
2227 breakpoint list certainly can.
c5aa993b 2228
488f131b
JB
2229 If we reach here and step_resume_breakpoint is already
2230 NULL, then apparently we have multiple active
2231 step-resume bp's. We'll just delete the breakpoint we
2232 stopped at, and carry on.
2233
2234 Correction: what the code currently does is delete a
2235 step-resume bp, but it makes no effort to ensure that
2236 the one deleted is the one currently stopped at. MVS */
c5aa993b 2237
527159b7 2238 if (debug_infrun)
8802d8ed 2239 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STEP_RESUME\n");
527159b7 2240
488f131b
JB
2241 if (step_resume_breakpoint == NULL)
2242 {
2243 step_resume_breakpoint =
2244 bpstat_find_step_resume_breakpoint (stop_bpstat);
2245 }
2246 delete_step_resume_breakpoint (&step_resume_breakpoint);
68f53502
AC
2247 if (ecs->step_after_step_resume_breakpoint)
2248 {
2249 /* Back when the step-resume breakpoint was inserted, we
2250 were trying to single-step off a breakpoint. Go back
2251 to doing that. */
2252 ecs->step_after_step_resume_breakpoint = 0;
2253 remove_breakpoints ();
2254 breakpoints_inserted = 0;
2255 ecs->another_trap = 1;
2256 keep_going (ecs);
2257 return;
2258 }
488f131b
JB
2259 break;
2260
488f131b
JB
2261 case BPSTAT_WHAT_CHECK_SHLIBS:
2262 case BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK:
c906108c 2263 {
527159b7 2264 if (debug_infrun)
8802d8ed 2265 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_CHECK_SHLIBS\n");
488f131b
JB
2266 /* Remove breakpoints, we eventually want to step over the
2267 shlib event breakpoint, and SOLIB_ADD might adjust
2268 breakpoint addresses via breakpoint_re_set. */
2269 if (breakpoints_inserted)
2270 remove_breakpoints ();
c5aa993b 2271 breakpoints_inserted = 0;
488f131b
JB
2272
2273 /* Check for any newly added shared libraries if we're
2274 supposed to be adding them automatically. Switch
2275 terminal for any messages produced by
2276 breakpoint_re_set. */
2277 target_terminal_ours_for_output ();
aff6338a 2278 /* NOTE: cagney/2003-11-25: Make certain that the target
8fb3e588
AC
2279 stack's section table is kept up-to-date. Architectures,
2280 (e.g., PPC64), use the section table to perform
2281 operations such as address => section name and hence
2282 require the table to contain all sections (including
2283 those found in shared libraries). */
aff6338a 2284 /* NOTE: cagney/2003-11-25: Pass current_target and not
8fb3e588
AC
2285 exec_ops to SOLIB_ADD. This is because current GDB is
2286 only tooled to propagate section_table changes out from
2287 the "current_target" (see target_resize_to_sections), and
2288 not up from the exec stratum. This, of course, isn't
2289 right. "infrun.c" should only interact with the
2290 exec/process stratum, instead relying on the target stack
2291 to propagate relevant changes (stop, section table
2292 changed, ...) up to other layers. */
a77053c2 2293#ifdef SOLIB_ADD
aff6338a 2294 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
a77053c2
MK
2295#else
2296 solib_add (NULL, 0, &current_target, auto_solib_add);
2297#endif
488f131b
JB
2298 target_terminal_inferior ();
2299
488f131b
JB
2300 /* If requested, stop when the dynamic linker notifies
2301 gdb of events. This allows the user to get control
2302 and place breakpoints in initializer routines for
2303 dynamically loaded objects (among other things). */
877522db 2304 if (stop_on_solib_events || stop_stack_dummy)
d4f3574e 2305 {
488f131b 2306 stop_stepping (ecs);
d4f3574e
SS
2307 return;
2308 }
c5aa993b 2309
488f131b
JB
2310 /* If we stopped due to an explicit catchpoint, then the
2311 (see above) call to SOLIB_ADD pulled in any symbols
2312 from a newly-loaded library, if appropriate.
2313
2314 We do want the inferior to stop, but not where it is
2315 now, which is in the dynamic linker callback. Rather,
2316 we would like it stop in the user's program, just after
2317 the call that caused this catchpoint to trigger. That
2318 gives the user a more useful vantage from which to
2319 examine their program's state. */
8fb3e588
AC
2320 else if (what.main_action
2321 == BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK)
c906108c 2322 {
488f131b
JB
2323 /* ??rehrauer: If I could figure out how to get the
2324 right return PC from here, we could just set a temp
2325 breakpoint and resume. I'm not sure we can without
2326 cracking open the dld's shared libraries and sniffing
2327 their unwind tables and text/data ranges, and that's
2328 not a terribly portable notion.
2329
2330 Until that time, we must step the inferior out of the
2331 dld callback, and also out of the dld itself (and any
2332 code or stubs in libdld.sl, such as "shl_load" and
2333 friends) until we reach non-dld code. At that point,
2334 we can stop stepping. */
2335 bpstat_get_triggered_catchpoints (stop_bpstat,
2336 &ecs->
2337 stepping_through_solib_catchpoints);
2338 ecs->stepping_through_solib_after_catch = 1;
2339
2340 /* Be sure to lift all breakpoints, so the inferior does
2341 actually step past this point... */
2342 ecs->another_trap = 1;
2343 break;
c906108c 2344 }
c5aa993b 2345 else
c5aa993b 2346 {
488f131b 2347 /* We want to step over this breakpoint, then keep going. */
c5aa993b 2348 ecs->another_trap = 1;
488f131b 2349 break;
c5aa993b 2350 }
488f131b 2351 }
488f131b 2352 break;
c906108c 2353
488f131b
JB
2354 case BPSTAT_WHAT_LAST:
2355 /* Not a real code, but listed here to shut up gcc -Wall. */
c906108c 2356
488f131b
JB
2357 case BPSTAT_WHAT_KEEP_CHECKING:
2358 break;
2359 }
2360 }
c906108c 2361
488f131b
JB
2362 /* We come here if we hit a breakpoint but should not
2363 stop for it. Possibly we also were stepping
2364 and should stop for that. So fall through and
2365 test for stepping. But, if not stepping,
2366 do not stop. */
c906108c 2367
9d1ff73f
MS
2368 /* Are we stepping to get the inferior out of the dynamic linker's
2369 hook (and possibly the dld itself) after catching a shlib
2370 event? */
488f131b
JB
2371 if (ecs->stepping_through_solib_after_catch)
2372 {
2373#if defined(SOLIB_ADD)
2374 /* Have we reached our destination? If not, keep going. */
2375 if (SOLIB_IN_DYNAMIC_LINKER (PIDGET (ecs->ptid), stop_pc))
2376 {
527159b7 2377 if (debug_infrun)
8a9de0e4 2378 fprintf_unfiltered (gdb_stdlog, "infrun: stepping in dynamic linker\n");
488f131b
JB
2379 ecs->another_trap = 1;
2380 keep_going (ecs);
104c1213 2381 return;
488f131b
JB
2382 }
2383#endif
527159b7 2384 if (debug_infrun)
8a9de0e4 2385 fprintf_unfiltered (gdb_stdlog, "infrun: step past dynamic linker\n");
488f131b
JB
2386 /* Else, stop and report the catchpoint(s) whose triggering
2387 caused us to begin stepping. */
2388 ecs->stepping_through_solib_after_catch = 0;
2389 bpstat_clear (&stop_bpstat);
2390 stop_bpstat = bpstat_copy (ecs->stepping_through_solib_catchpoints);
2391 bpstat_clear (&ecs->stepping_through_solib_catchpoints);
2392 stop_print_frame = 1;
2393 stop_stepping (ecs);
2394 return;
2395 }
c906108c 2396
488f131b
JB
2397 if (step_resume_breakpoint)
2398 {
527159b7 2399 if (debug_infrun)
d3169d93
DJ
2400 fprintf_unfiltered (gdb_stdlog,
2401 "infrun: step-resume breakpoint is inserted\n");
527159b7 2402
488f131b
JB
2403 /* Having a step-resume breakpoint overrides anything
2404 else having to do with stepping commands until
2405 that breakpoint is reached. */
488f131b
JB
2406 keep_going (ecs);
2407 return;
2408 }
c5aa993b 2409
488f131b
JB
2410 if (step_range_end == 0)
2411 {
527159b7 2412 if (debug_infrun)
8a9de0e4 2413 fprintf_unfiltered (gdb_stdlog, "infrun: no stepping, continue\n");
488f131b 2414 /* Likewise if we aren't even stepping. */
488f131b
JB
2415 keep_going (ecs);
2416 return;
2417 }
c5aa993b 2418
488f131b 2419 /* If stepping through a line, keep going if still within it.
c906108c 2420
488f131b
JB
2421 Note that step_range_end is the address of the first instruction
2422 beyond the step range, and NOT the address of the last instruction
2423 within it! */
2424 if (stop_pc >= step_range_start && stop_pc < step_range_end)
2425 {
527159b7 2426 if (debug_infrun)
8a9de0e4 2427 fprintf_unfiltered (gdb_stdlog, "infrun: stepping inside range [0x%s-0x%s]\n",
527159b7
RC
2428 paddr_nz (step_range_start),
2429 paddr_nz (step_range_end));
488f131b
JB
2430 keep_going (ecs);
2431 return;
2432 }
c5aa993b 2433
488f131b 2434 /* We stepped out of the stepping range. */
c906108c 2435
488f131b
JB
2436 /* If we are stepping at the source level and entered the runtime
2437 loader dynamic symbol resolution code, we keep on single stepping
2438 until we exit the run time loader code and reach the callee's
2439 address. */
2440 if (step_over_calls == STEP_OVER_UNDEBUGGABLE
a77053c2
MK
2441#ifdef IN_SOLIB_DYNSYM_RESOLVE_CODE
2442 && IN_SOLIB_DYNSYM_RESOLVE_CODE (stop_pc)
2443#else
2444 && in_solib_dynsym_resolve_code (stop_pc)
2445#endif
2446 )
488f131b 2447 {
4c8c40e6
MK
2448 CORE_ADDR pc_after_resolver =
2449 gdbarch_skip_solib_resolver (current_gdbarch, stop_pc);
c906108c 2450
527159b7 2451 if (debug_infrun)
8a9de0e4 2452 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into dynsym resolve code\n");
527159b7 2453
488f131b
JB
2454 if (pc_after_resolver)
2455 {
2456 /* Set up a step-resume breakpoint at the address
2457 indicated by SKIP_SOLIB_RESOLVER. */
2458 struct symtab_and_line sr_sal;
fe39c653 2459 init_sal (&sr_sal);
488f131b
JB
2460 sr_sal.pc = pc_after_resolver;
2461
44cbf7b5 2462 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
c5aa993b 2463 }
c906108c 2464
488f131b
JB
2465 keep_going (ecs);
2466 return;
2467 }
c906108c 2468
42edda50
AC
2469 if (step_range_end != 1
2470 && (step_over_calls == STEP_OVER_UNDEBUGGABLE
2471 || step_over_calls == STEP_OVER_ALL)
2472 && get_frame_type (get_current_frame ()) == SIGTRAMP_FRAME)
488f131b 2473 {
527159b7 2474 if (debug_infrun)
8a9de0e4 2475 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into signal trampoline\n");
42edda50 2476 /* The inferior, while doing a "step" or "next", has ended up in
8fb3e588
AC
2477 a signal trampoline (either by a signal being delivered or by
2478 the signal handler returning). Just single-step until the
2479 inferior leaves the trampoline (either by calling the handler
2480 or returning). */
488f131b
JB
2481 keep_going (ecs);
2482 return;
2483 }
c906108c 2484
c17eaafe
DJ
2485 /* Check for subroutine calls. The check for the current frame
2486 equalling the step ID is not necessary - the check of the
2487 previous frame's ID is sufficient - but it is a common case and
2488 cheaper than checking the previous frame's ID.
14e60db5
DJ
2489
2490 NOTE: frame_id_eq will never report two invalid frame IDs as
2491 being equal, so to get into this block, both the current and
2492 previous frame must have valid frame IDs. */
c17eaafe
DJ
2493 if (!frame_id_eq (get_frame_id (get_current_frame ()), step_frame_id)
2494 && frame_id_eq (frame_unwind_id (get_current_frame ()), step_frame_id))
488f131b 2495 {
95918acb 2496 CORE_ADDR real_stop_pc;
8fb3e588 2497
527159b7 2498 if (debug_infrun)
8a9de0e4 2499 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into subroutine\n");
527159b7 2500
95918acb
AC
2501 if ((step_over_calls == STEP_OVER_NONE)
2502 || ((step_range_end == 1)
2503 && in_prologue (prev_pc, ecs->stop_func_start)))
2504 {
2505 /* I presume that step_over_calls is only 0 when we're
2506 supposed to be stepping at the assembly language level
2507 ("stepi"). Just stop. */
2508 /* Also, maybe we just did a "nexti" inside a prolog, so we
2509 thought it was a subroutine call but it was not. Stop as
2510 well. FENN */
2511 stop_step = 1;
2512 print_stop_reason (END_STEPPING_RANGE, 0);
2513 stop_stepping (ecs);
2514 return;
2515 }
8fb3e588 2516
8567c30f
AC
2517 if (step_over_calls == STEP_OVER_ALL)
2518 {
2519 /* We're doing a "next", set a breakpoint at callee's return
2520 address (the address at which the caller will
2521 resume). */
14e60db5 2522 insert_step_resume_breakpoint_at_caller (get_current_frame ());
8567c30f
AC
2523 keep_going (ecs);
2524 return;
2525 }
a53c66de 2526
95918acb 2527 /* If we are in a function call trampoline (a stub between the
8fb3e588
AC
2528 calling routine and the real function), locate the real
2529 function. That's what tells us (a) whether we want to step
2530 into it at all, and (b) what prologue we want to run to the
2531 end of, if we do step into it. */
52f729a7 2532 real_stop_pc = skip_language_trampoline (get_current_frame (), stop_pc);
95918acb 2533 if (real_stop_pc == 0)
52f729a7
UW
2534 real_stop_pc = gdbarch_skip_trampoline_code
2535 (current_gdbarch, get_current_frame (), stop_pc);
95918acb
AC
2536 if (real_stop_pc != 0)
2537 ecs->stop_func_start = real_stop_pc;
8fb3e588 2538
a77053c2
MK
2539 if (
2540#ifdef IN_SOLIB_DYNSYM_RESOLVE_CODE
2541 IN_SOLIB_DYNSYM_RESOLVE_CODE (ecs->stop_func_start)
2542#else
2543 in_solib_dynsym_resolve_code (ecs->stop_func_start)
2544#endif
2545)
1b2bfbb9
RC
2546 {
2547 struct symtab_and_line sr_sal;
2548 init_sal (&sr_sal);
2549 sr_sal.pc = ecs->stop_func_start;
2550
44cbf7b5 2551 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
8fb3e588
AC
2552 keep_going (ecs);
2553 return;
1b2bfbb9
RC
2554 }
2555
95918acb 2556 /* If we have line number information for the function we are
8fb3e588 2557 thinking of stepping into, step into it.
95918acb 2558
8fb3e588
AC
2559 If there are several symtabs at that PC (e.g. with include
2560 files), just want to know whether *any* of them have line
2561 numbers. find_pc_line handles this. */
95918acb
AC
2562 {
2563 struct symtab_and_line tmp_sal;
8fb3e588 2564
95918acb
AC
2565 tmp_sal = find_pc_line (ecs->stop_func_start, 0);
2566 if (tmp_sal.line != 0)
2567 {
2568 step_into_function (ecs);
2569 return;
2570 }
2571 }
2572
2573 /* If we have no line number and the step-stop-if-no-debug is
8fb3e588
AC
2574 set, we stop the step so that the user has a chance to switch
2575 in assembly mode. */
95918acb
AC
2576 if (step_over_calls == STEP_OVER_UNDEBUGGABLE && step_stop_if_no_debug)
2577 {
2578 stop_step = 1;
2579 print_stop_reason (END_STEPPING_RANGE, 0);
2580 stop_stepping (ecs);
2581 return;
2582 }
2583
2584 /* Set a breakpoint at callee's return address (the address at
8fb3e588 2585 which the caller will resume). */
14e60db5 2586 insert_step_resume_breakpoint_at_caller (get_current_frame ());
95918acb 2587 keep_going (ecs);
488f131b 2588 return;
488f131b 2589 }
c906108c 2590
488f131b
JB
2591 /* If we're in the return path from a shared library trampoline,
2592 we want to proceed through the trampoline when stepping. */
e76f05fa
UW
2593 if (gdbarch_in_solib_return_trampoline (current_gdbarch,
2594 stop_pc, ecs->stop_func_name))
488f131b 2595 {
488f131b 2596 /* Determine where this trampoline returns. */
52f729a7
UW
2597 CORE_ADDR real_stop_pc;
2598 real_stop_pc = gdbarch_skip_trampoline_code
2599 (current_gdbarch, get_current_frame (), stop_pc);
c906108c 2600
527159b7 2601 if (debug_infrun)
8a9de0e4 2602 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into solib return tramp\n");
527159b7 2603
488f131b 2604 /* Only proceed through if we know where it's going. */
d764a824 2605 if (real_stop_pc)
488f131b
JB
2606 {
2607 /* And put the step-breakpoint there and go until there. */
2608 struct symtab_and_line sr_sal;
2609
fe39c653 2610 init_sal (&sr_sal); /* initialize to zeroes */
d764a824 2611 sr_sal.pc = real_stop_pc;
488f131b 2612 sr_sal.section = find_pc_overlay (sr_sal.pc);
44cbf7b5
AC
2613
2614 /* Do not specify what the fp should be when we stop since
2615 on some machines the prologue is where the new fp value
2616 is established. */
2617 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
c906108c 2618
488f131b
JB
2619 /* Restart without fiddling with the step ranges or
2620 other state. */
2621 keep_going (ecs);
2622 return;
2623 }
2624 }
c906108c 2625
7ed0fe66
DJ
2626 ecs->sal = find_pc_line (stop_pc, 0);
2627
1b2bfbb9
RC
2628 /* NOTE: tausq/2004-05-24: This if block used to be done before all
2629 the trampoline processing logic, however, there are some trampolines
2630 that have no names, so we should do trampoline handling first. */
2631 if (step_over_calls == STEP_OVER_UNDEBUGGABLE
7ed0fe66
DJ
2632 && ecs->stop_func_name == NULL
2633 && ecs->sal.line == 0)
1b2bfbb9 2634 {
527159b7 2635 if (debug_infrun)
8a9de0e4 2636 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into undebuggable function\n");
527159b7 2637
1b2bfbb9 2638 /* The inferior just stepped into, or returned to, an
7ed0fe66
DJ
2639 undebuggable function (where there is no debugging information
2640 and no line number corresponding to the address where the
1b2bfbb9
RC
2641 inferior stopped). Since we want to skip this kind of code,
2642 we keep going until the inferior returns from this
14e60db5
DJ
2643 function - unless the user has asked us not to (via
2644 set step-mode) or we no longer know how to get back
2645 to the call site. */
2646 if (step_stop_if_no_debug
2647 || !frame_id_p (frame_unwind_id (get_current_frame ())))
1b2bfbb9
RC
2648 {
2649 /* If we have no line number and the step-stop-if-no-debug
2650 is set, we stop the step so that the user has a chance to
2651 switch in assembly mode. */
2652 stop_step = 1;
2653 print_stop_reason (END_STEPPING_RANGE, 0);
2654 stop_stepping (ecs);
2655 return;
2656 }
2657 else
2658 {
2659 /* Set a breakpoint at callee's return address (the address
2660 at which the caller will resume). */
14e60db5 2661 insert_step_resume_breakpoint_at_caller (get_current_frame ());
1b2bfbb9
RC
2662 keep_going (ecs);
2663 return;
2664 }
2665 }
2666
2667 if (step_range_end == 1)
2668 {
2669 /* It is stepi or nexti. We always want to stop stepping after
2670 one instruction. */
527159b7 2671 if (debug_infrun)
8a9de0e4 2672 fprintf_unfiltered (gdb_stdlog, "infrun: stepi/nexti\n");
1b2bfbb9
RC
2673 stop_step = 1;
2674 print_stop_reason (END_STEPPING_RANGE, 0);
2675 stop_stepping (ecs);
2676 return;
2677 }
2678
488f131b
JB
2679 if (ecs->sal.line == 0)
2680 {
2681 /* We have no line number information. That means to stop
2682 stepping (does this always happen right after one instruction,
2683 when we do "s" in a function with no line numbers,
2684 or can this happen as a result of a return or longjmp?). */
527159b7 2685 if (debug_infrun)
8a9de0e4 2686 fprintf_unfiltered (gdb_stdlog, "infrun: no line number info\n");
488f131b
JB
2687 stop_step = 1;
2688 print_stop_reason (END_STEPPING_RANGE, 0);
2689 stop_stepping (ecs);
2690 return;
2691 }
c906108c 2692
488f131b
JB
2693 if ((stop_pc == ecs->sal.pc)
2694 && (ecs->current_line != ecs->sal.line
2695 || ecs->current_symtab != ecs->sal.symtab))
2696 {
2697 /* We are at the start of a different line. So stop. Note that
2698 we don't stop if we step into the middle of a different line.
2699 That is said to make things like for (;;) statements work
2700 better. */
527159b7 2701 if (debug_infrun)
8a9de0e4 2702 fprintf_unfiltered (gdb_stdlog, "infrun: stepped to a different line\n");
488f131b
JB
2703 stop_step = 1;
2704 print_stop_reason (END_STEPPING_RANGE, 0);
2705 stop_stepping (ecs);
2706 return;
2707 }
c906108c 2708
488f131b 2709 /* We aren't done stepping.
c906108c 2710
488f131b
JB
2711 Optimize by setting the stepping range to the line.
2712 (We might not be in the original line, but if we entered a
2713 new line in mid-statement, we continue stepping. This makes
2714 things like for(;;) statements work better.) */
c906108c 2715
488f131b 2716 if (ecs->stop_func_end && ecs->sal.end >= ecs->stop_func_end)
c5aa993b 2717 {
488f131b
JB
2718 /* If this is the last line of the function, don't keep stepping
2719 (it would probably step us out of the function).
2720 This is particularly necessary for a one-line function,
2721 in which after skipping the prologue we better stop even though
2722 we will be in mid-line. */
527159b7 2723 if (debug_infrun)
8a9de0e4 2724 fprintf_unfiltered (gdb_stdlog, "infrun: stepped to a different function\n");
488f131b
JB
2725 stop_step = 1;
2726 print_stop_reason (END_STEPPING_RANGE, 0);
2727 stop_stepping (ecs);
2728 return;
c5aa993b 2729 }
488f131b
JB
2730 step_range_start = ecs->sal.pc;
2731 step_range_end = ecs->sal.end;
aa0cd9c1 2732 step_frame_id = get_frame_id (get_current_frame ());
488f131b
JB
2733 ecs->current_line = ecs->sal.line;
2734 ecs->current_symtab = ecs->sal.symtab;
2735
aa0cd9c1
AC
2736 /* In the case where we just stepped out of a function into the
2737 middle of a line of the caller, continue stepping, but
2738 step_frame_id must be modified to current frame */
65815ea1
AC
2739#if 0
2740 /* NOTE: cagney/2003-10-16: I think this frame ID inner test is too
2741 generous. It will trigger on things like a step into a frameless
2742 stackless leaf function. I think the logic should instead look
2743 at the unwound frame ID has that should give a more robust
2744 indication of what happened. */
8fb3e588
AC
2745 if (step - ID == current - ID)
2746 still stepping in same function;
2747 else if (step - ID == unwind (current - ID))
2748 stepped into a function;
2749 else
2750 stepped out of a function;
2751 /* Of course this assumes that the frame ID unwind code is robust
2752 and we're willing to introduce frame unwind logic into this
2753 function. Fortunately, those days are nearly upon us. */
65815ea1 2754#endif
488f131b 2755 {
09a7aba8
UW
2756 struct frame_info *frame = get_current_frame ();
2757 struct frame_id current_frame = get_frame_id (frame);
2758 if (!(frame_id_inner (get_frame_arch (frame), current_frame,
2759 step_frame_id)))
aa0cd9c1 2760 step_frame_id = current_frame;
488f131b 2761 }
c906108c 2762
527159b7 2763 if (debug_infrun)
8a9de0e4 2764 fprintf_unfiltered (gdb_stdlog, "infrun: keep going\n");
488f131b 2765 keep_going (ecs);
104c1213
JM
2766}
2767
2768/* Are we in the middle of stepping? */
2769
2770static int
2771currently_stepping (struct execution_control_state *ecs)
2772{
d303a6c7 2773 return ((!ecs->handling_longjmp
104c1213
JM
2774 && ((step_range_end && step_resume_breakpoint == NULL)
2775 || trap_expected))
2776 || ecs->stepping_through_solib_after_catch
2777 || bpstat_should_step ());
2778}
c906108c 2779
c2c6d25f
JM
2780/* Subroutine call with source code we should not step over. Do step
2781 to the first line of code in it. */
2782
2783static void
2784step_into_function (struct execution_control_state *ecs)
2785{
2786 struct symtab *s;
2787 struct symtab_and_line sr_sal;
2788
2789 s = find_pc_symtab (stop_pc);
2790 if (s && s->language != language_asm)
a433963d
UW
2791 ecs->stop_func_start = gdbarch_skip_prologue
2792 (current_gdbarch, ecs->stop_func_start);
c2c6d25f
JM
2793
2794 ecs->sal = find_pc_line (ecs->stop_func_start, 0);
2795 /* Use the step_resume_break to step until the end of the prologue,
2796 even if that involves jumps (as it seems to on the vax under
2797 4.2). */
2798 /* If the prologue ends in the middle of a source line, continue to
2799 the end of that source line (if it is still within the function).
2800 Otherwise, just go to end of prologue. */
c2c6d25f
JM
2801 if (ecs->sal.end
2802 && ecs->sal.pc != ecs->stop_func_start
2803 && ecs->sal.end < ecs->stop_func_end)
2804 ecs->stop_func_start = ecs->sal.end;
c2c6d25f 2805
2dbd5e30
KB
2806 /* Architectures which require breakpoint adjustment might not be able
2807 to place a breakpoint at the computed address. If so, the test
2808 ``ecs->stop_func_start == stop_pc'' will never succeed. Adjust
2809 ecs->stop_func_start to an address at which a breakpoint may be
2810 legitimately placed.
8fb3e588 2811
2dbd5e30
KB
2812 Note: kevinb/2004-01-19: On FR-V, if this adjustment is not
2813 made, GDB will enter an infinite loop when stepping through
2814 optimized code consisting of VLIW instructions which contain
2815 subinstructions corresponding to different source lines. On
2816 FR-V, it's not permitted to place a breakpoint on any but the
2817 first subinstruction of a VLIW instruction. When a breakpoint is
2818 set, GDB will adjust the breakpoint address to the beginning of
2819 the VLIW instruction. Thus, we need to make the corresponding
2820 adjustment here when computing the stop address. */
8fb3e588 2821
2dbd5e30
KB
2822 if (gdbarch_adjust_breakpoint_address_p (current_gdbarch))
2823 {
2824 ecs->stop_func_start
2825 = gdbarch_adjust_breakpoint_address (current_gdbarch,
8fb3e588 2826 ecs->stop_func_start);
2dbd5e30
KB
2827 }
2828
c2c6d25f
JM
2829 if (ecs->stop_func_start == stop_pc)
2830 {
2831 /* We are already there: stop now. */
2832 stop_step = 1;
488f131b 2833 print_stop_reason (END_STEPPING_RANGE, 0);
c2c6d25f
JM
2834 stop_stepping (ecs);
2835 return;
2836 }
2837 else
2838 {
2839 /* Put the step-breakpoint there and go until there. */
fe39c653 2840 init_sal (&sr_sal); /* initialize to zeroes */
c2c6d25f
JM
2841 sr_sal.pc = ecs->stop_func_start;
2842 sr_sal.section = find_pc_overlay (ecs->stop_func_start);
44cbf7b5 2843
c2c6d25f 2844 /* Do not specify what the fp should be when we stop since on
488f131b
JB
2845 some machines the prologue is where the new fp value is
2846 established. */
44cbf7b5 2847 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
c2c6d25f
JM
2848
2849 /* And make sure stepping stops right away then. */
2850 step_range_end = step_range_start;
2851 }
2852 keep_going (ecs);
2853}
d4f3574e 2854
d3169d93 2855/* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
44cbf7b5
AC
2856 This is used to both functions and to skip over code. */
2857
2858static void
2859insert_step_resume_breakpoint_at_sal (struct symtab_and_line sr_sal,
2860 struct frame_id sr_id)
2861{
2862 /* There should never be more than one step-resume breakpoint per
2863 thread, so we should never be setting a new
2864 step_resume_breakpoint when one is already active. */
2865 gdb_assert (step_resume_breakpoint == NULL);
d3169d93
DJ
2866
2867 if (debug_infrun)
2868 fprintf_unfiltered (gdb_stdlog,
2869 "infrun: inserting step-resume breakpoint at 0x%s\n",
2870 paddr_nz (sr_sal.pc));
2871
44cbf7b5
AC
2872 step_resume_breakpoint = set_momentary_breakpoint (sr_sal, sr_id,
2873 bp_step_resume);
2874 if (breakpoints_inserted)
2875 insert_breakpoints ();
2876}
7ce450bd 2877
d3169d93 2878/* Insert a "step-resume breakpoint" at RETURN_FRAME.pc. This is used
14e60db5 2879 to skip a potential signal handler.
7ce450bd 2880
14e60db5
DJ
2881 This is called with the interrupted function's frame. The signal
2882 handler, when it returns, will resume the interrupted function at
2883 RETURN_FRAME.pc. */
d303a6c7
AC
2884
2885static void
44cbf7b5 2886insert_step_resume_breakpoint_at_frame (struct frame_info *return_frame)
d303a6c7
AC
2887{
2888 struct symtab_and_line sr_sal;
2889
f4c1edd8 2890 gdb_assert (return_frame != NULL);
d303a6c7
AC
2891 init_sal (&sr_sal); /* initialize to zeros */
2892
bf6ae464
UW
2893 sr_sal.pc = gdbarch_addr_bits_remove
2894 (current_gdbarch, get_frame_pc (return_frame));
d303a6c7
AC
2895 sr_sal.section = find_pc_overlay (sr_sal.pc);
2896
44cbf7b5 2897 insert_step_resume_breakpoint_at_sal (sr_sal, get_frame_id (return_frame));
d303a6c7
AC
2898}
2899
14e60db5
DJ
2900/* Similar to insert_step_resume_breakpoint_at_frame, except
2901 but a breakpoint at the previous frame's PC. This is used to
2902 skip a function after stepping into it (for "next" or if the called
2903 function has no debugging information).
2904
2905 The current function has almost always been reached by single
2906 stepping a call or return instruction. NEXT_FRAME belongs to the
2907 current function, and the breakpoint will be set at the caller's
2908 resume address.
2909
2910 This is a separate function rather than reusing
2911 insert_step_resume_breakpoint_at_frame in order to avoid
2912 get_prev_frame, which may stop prematurely (see the implementation
2913 of frame_unwind_id for an example). */
2914
2915static void
2916insert_step_resume_breakpoint_at_caller (struct frame_info *next_frame)
2917{
2918 struct symtab_and_line sr_sal;
2919
2920 /* We shouldn't have gotten here if we don't know where the call site
2921 is. */
2922 gdb_assert (frame_id_p (frame_unwind_id (next_frame)));
2923
2924 init_sal (&sr_sal); /* initialize to zeros */
2925
bf6ae464
UW
2926 sr_sal.pc = gdbarch_addr_bits_remove
2927 (current_gdbarch, frame_pc_unwind (next_frame));
14e60db5
DJ
2928 sr_sal.section = find_pc_overlay (sr_sal.pc);
2929
2930 insert_step_resume_breakpoint_at_sal (sr_sal, frame_unwind_id (next_frame));
2931}
2932
104c1213
JM
2933static void
2934stop_stepping (struct execution_control_state *ecs)
2935{
527159b7 2936 if (debug_infrun)
8a9de0e4 2937 fprintf_unfiltered (gdb_stdlog, "infrun: stop_stepping\n");
527159b7 2938
cd0fc7c3
SS
2939 /* Let callers know we don't want to wait for the inferior anymore. */
2940 ecs->wait_some_more = 0;
2941}
2942
d4f3574e
SS
2943/* This function handles various cases where we need to continue
2944 waiting for the inferior. */
2945/* (Used to be the keep_going: label in the old wait_for_inferior) */
2946
2947static void
2948keep_going (struct execution_control_state *ecs)
2949{
d4f3574e 2950 /* Save the pc before execution, to compare with pc after stop. */
488f131b 2951 prev_pc = read_pc (); /* Might have been DECR_AFTER_BREAK */
d4f3574e 2952
d4f3574e
SS
2953 /* If we did not do break;, it means we should keep running the
2954 inferior and not return to debugger. */
2955
2956 if (trap_expected && stop_signal != TARGET_SIGNAL_TRAP)
2957 {
2958 /* We took a signal (which we are supposed to pass through to
488f131b
JB
2959 the inferior, else we'd have done a break above) and we
2960 haven't yet gotten our trap. Simply continue. */
d4f3574e
SS
2961 resume (currently_stepping (ecs), stop_signal);
2962 }
2963 else
2964 {
2965 /* Either the trap was not expected, but we are continuing
488f131b
JB
2966 anyway (the user asked that this signal be passed to the
2967 child)
2968 -- or --
2969 The signal was SIGTRAP, e.g. it was our signal, but we
2970 decided we should resume from it.
d4f3574e 2971
68f53502 2972 We're going to run this baby now! */
d4f3574e 2973
68f53502 2974 if (!breakpoints_inserted && !ecs->another_trap)
d4f3574e 2975 {
569631c6
UW
2976 /* Stop stepping when inserting breakpoints
2977 has failed. */
2978 if (insert_breakpoints () != 0)
d4f3574e
SS
2979 {
2980 stop_stepping (ecs);
2981 return;
2982 }
2983 breakpoints_inserted = 1;
2984 }
2985
2986 trap_expected = ecs->another_trap;
2987
2988 /* Do not deliver SIGNAL_TRAP (except when the user explicitly
488f131b
JB
2989 specifies that such a signal should be delivered to the
2990 target program).
2991
2992 Typically, this would occure when a user is debugging a
2993 target monitor on a simulator: the target monitor sets a
2994 breakpoint; the simulator encounters this break-point and
2995 halts the simulation handing control to GDB; GDB, noteing
2996 that the break-point isn't valid, returns control back to the
2997 simulator; the simulator then delivers the hardware
2998 equivalent of a SIGNAL_TRAP to the program being debugged. */
2999
3000 if (stop_signal == TARGET_SIGNAL_TRAP && !signal_program[stop_signal])
d4f3574e
SS
3001 stop_signal = TARGET_SIGNAL_0;
3002
d4f3574e
SS
3003
3004 resume (currently_stepping (ecs), stop_signal);
3005 }
3006
488f131b 3007 prepare_to_wait (ecs);
d4f3574e
SS
3008}
3009
104c1213
JM
3010/* This function normally comes after a resume, before
3011 handle_inferior_event exits. It takes care of any last bits of
3012 housekeeping, and sets the all-important wait_some_more flag. */
cd0fc7c3 3013
104c1213
JM
3014static void
3015prepare_to_wait (struct execution_control_state *ecs)
cd0fc7c3 3016{
527159b7 3017 if (debug_infrun)
8a9de0e4 3018 fprintf_unfiltered (gdb_stdlog, "infrun: prepare_to_wait\n");
104c1213
JM
3019 if (ecs->infwait_state == infwait_normal_state)
3020 {
3021 overlay_cache_invalid = 1;
3022
3023 /* We have to invalidate the registers BEFORE calling
488f131b
JB
3024 target_wait because they can be loaded from the target while
3025 in target_wait. This makes remote debugging a bit more
3026 efficient for those targets that provide critical registers
3027 as part of their normal status mechanism. */
104c1213
JM
3028
3029 registers_changed ();
39f77062 3030 ecs->waiton_ptid = pid_to_ptid (-1);
104c1213
JM
3031 ecs->wp = &(ecs->ws);
3032 }
3033 /* This is the old end of the while loop. Let everybody know we
3034 want to wait for the inferior some more and get called again
3035 soon. */
3036 ecs->wait_some_more = 1;
c906108c 3037}
11cf8741
JM
3038
3039/* Print why the inferior has stopped. We always print something when
3040 the inferior exits, or receives a signal. The rest of the cases are
3041 dealt with later on in normal_stop() and print_it_typical(). Ideally
3042 there should be a call to this function from handle_inferior_event()
3043 each time stop_stepping() is called.*/
3044static void
3045print_stop_reason (enum inferior_stop_reason stop_reason, int stop_info)
3046{
3047 switch (stop_reason)
3048 {
11cf8741
JM
3049 case END_STEPPING_RANGE:
3050 /* We are done with a step/next/si/ni command. */
3051 /* For now print nothing. */
fb40c209 3052 /* Print a message only if not in the middle of doing a "step n"
488f131b 3053 operation for n > 1 */
fb40c209 3054 if (!step_multi || !stop_step)
9dc5e2a9 3055 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3056 ui_out_field_string
3057 (uiout, "reason",
3058 async_reason_lookup (EXEC_ASYNC_END_STEPPING_RANGE));
11cf8741 3059 break;
11cf8741
JM
3060 case SIGNAL_EXITED:
3061 /* The inferior was terminated by a signal. */
8b93c638 3062 annotate_signalled ();
9dc5e2a9 3063 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3064 ui_out_field_string
3065 (uiout, "reason",
3066 async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED));
8b93c638
JM
3067 ui_out_text (uiout, "\nProgram terminated with signal ");
3068 annotate_signal_name ();
488f131b
JB
3069 ui_out_field_string (uiout, "signal-name",
3070 target_signal_to_name (stop_info));
8b93c638
JM
3071 annotate_signal_name_end ();
3072 ui_out_text (uiout, ", ");
3073 annotate_signal_string ();
488f131b
JB
3074 ui_out_field_string (uiout, "signal-meaning",
3075 target_signal_to_string (stop_info));
8b93c638
JM
3076 annotate_signal_string_end ();
3077 ui_out_text (uiout, ".\n");
3078 ui_out_text (uiout, "The program no longer exists.\n");
11cf8741
JM
3079 break;
3080 case EXITED:
3081 /* The inferior program is finished. */
8b93c638
JM
3082 annotate_exited (stop_info);
3083 if (stop_info)
3084 {
9dc5e2a9 3085 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3086 ui_out_field_string (uiout, "reason",
3087 async_reason_lookup (EXEC_ASYNC_EXITED));
8b93c638 3088 ui_out_text (uiout, "\nProgram exited with code ");
488f131b
JB
3089 ui_out_field_fmt (uiout, "exit-code", "0%o",
3090 (unsigned int) stop_info);
8b93c638
JM
3091 ui_out_text (uiout, ".\n");
3092 }
3093 else
3094 {
9dc5e2a9 3095 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3096 ui_out_field_string
3097 (uiout, "reason",
3098 async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY));
8b93c638
JM
3099 ui_out_text (uiout, "\nProgram exited normally.\n");
3100 }
f17517ea
AS
3101 /* Support the --return-child-result option. */
3102 return_child_result_value = stop_info;
11cf8741
JM
3103 break;
3104 case SIGNAL_RECEIVED:
3105 /* Signal received. The signal table tells us to print about
3106 it. */
8b93c638
JM
3107 annotate_signal ();
3108 ui_out_text (uiout, "\nProgram received signal ");
3109 annotate_signal_name ();
84c6c83c 3110 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3111 ui_out_field_string
3112 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED));
488f131b
JB
3113 ui_out_field_string (uiout, "signal-name",
3114 target_signal_to_name (stop_info));
8b93c638
JM
3115 annotate_signal_name_end ();
3116 ui_out_text (uiout, ", ");
3117 annotate_signal_string ();
488f131b
JB
3118 ui_out_field_string (uiout, "signal-meaning",
3119 target_signal_to_string (stop_info));
8b93c638
JM
3120 annotate_signal_string_end ();
3121 ui_out_text (uiout, ".\n");
11cf8741
JM
3122 break;
3123 default:
8e65ff28 3124 internal_error (__FILE__, __LINE__,
e2e0b3e5 3125 _("print_stop_reason: unrecognized enum value"));
11cf8741
JM
3126 break;
3127 }
3128}
c906108c 3129\f
43ff13b4 3130
c906108c
SS
3131/* Here to return control to GDB when the inferior stops for real.
3132 Print appropriate messages, remove breakpoints, give terminal our modes.
3133
3134 STOP_PRINT_FRAME nonzero means print the executing frame
3135 (pc, function, args, file, line number and line text).
3136 BREAKPOINTS_FAILED nonzero means stop was due to error
3137 attempting to insert breakpoints. */
3138
3139void
96baa820 3140normal_stop (void)
c906108c 3141{
73b65bb0
DJ
3142 struct target_waitstatus last;
3143 ptid_t last_ptid;
3144
3145 get_last_target_status (&last_ptid, &last);
3146
c906108c
SS
3147 /* As with the notification of thread events, we want to delay
3148 notifying the user that we've switched thread context until
3149 the inferior actually stops.
3150
73b65bb0
DJ
3151 There's no point in saying anything if the inferior has exited.
3152 Note that SIGNALLED here means "exited with a signal", not
3153 "received a signal". */
488f131b 3154 if (!ptid_equal (previous_inferior_ptid, inferior_ptid)
73b65bb0
DJ
3155 && target_has_execution
3156 && last.kind != TARGET_WAITKIND_SIGNALLED
3157 && last.kind != TARGET_WAITKIND_EXITED)
c906108c
SS
3158 {
3159 target_terminal_ours_for_output ();
a3f17187 3160 printf_filtered (_("[Switching to %s]\n"),
39f77062
KB
3161 target_pid_or_tid_to_str (inferior_ptid));
3162 previous_inferior_ptid = inferior_ptid;
c906108c 3163 }
c906108c 3164
4fa8626c 3165 /* NOTE drow/2004-01-17: Is this still necessary? */
c906108c
SS
3166 /* Make sure that the current_frame's pc is correct. This
3167 is a correction for setting up the frame info before doing
b798847d 3168 gdbarch_decr_pc_after_break */
b87efeee
AC
3169 if (target_has_execution)
3170 /* FIXME: cagney/2002-12-06: Has the PC changed? Thanks to
b798847d 3171 gdbarch_decr_pc_after_break, the program counter can change. Ask the
b87efeee 3172 frame code to check for this and sort out any resultant mess.
b798847d 3173 gdbarch_decr_pc_after_break needs to just go away. */
2f107107 3174 deprecated_update_frame_pc_hack (get_current_frame (), read_pc ());
c906108c 3175
c906108c
SS
3176 if (target_has_execution && breakpoints_inserted)
3177 {
3178 if (remove_breakpoints ())
3179 {
3180 target_terminal_ours_for_output ();
a3f17187
AC
3181 printf_filtered (_("\
3182Cannot remove breakpoints because program is no longer writable.\n\
3183It might be running in another process.\n\
3184Further execution is probably impossible.\n"));
c906108c
SS
3185 }
3186 }
3187 breakpoints_inserted = 0;
3188
3189 /* Delete the breakpoint we stopped at, if it wants to be deleted.
3190 Delete any breakpoint that is to be deleted at the next stop. */
3191
3192 breakpoint_auto_delete (stop_bpstat);
3193
3194 /* If an auto-display called a function and that got a signal,
3195 delete that auto-display to avoid an infinite recursion. */
3196
3197 if (stopped_by_random_signal)
3198 disable_current_display ();
3199
3200 /* Don't print a message if in the middle of doing a "step n"
3201 operation for n > 1 */
3202 if (step_multi && stop_step)
3203 goto done;
3204
3205 target_terminal_ours ();
3206
7abfe014
DJ
3207 /* Set the current source location. This will also happen if we
3208 display the frame below, but the current SAL will be incorrect
3209 during a user hook-stop function. */
3210 if (target_has_stack && !stop_stack_dummy)
3211 set_current_sal_from_frame (get_current_frame (), 1);
3212
5913bcb0
AC
3213 /* Look up the hook_stop and run it (CLI internally handles problem
3214 of stop_command's pre-hook not existing). */
3215 if (stop_command)
3216 catch_errors (hook_stop_stub, stop_command,
3217 "Error while running hook_stop:\n", RETURN_MASK_ALL);
c906108c
SS
3218
3219 if (!target_has_stack)
3220 {
3221
3222 goto done;
3223 }
3224
3225 /* Select innermost stack frame - i.e., current frame is frame 0,
3226 and current location is based on that.
3227 Don't do this on return from a stack dummy routine,
3228 or if the program has exited. */
3229
3230 if (!stop_stack_dummy)
3231 {
0f7d239c 3232 select_frame (get_current_frame ());
c906108c
SS
3233
3234 /* Print current location without a level number, if
c5aa993b
JM
3235 we have changed functions or hit a breakpoint.
3236 Print source line if we have one.
3237 bpstat_print() contains the logic deciding in detail
3238 what to print, based on the event(s) that just occurred. */
c906108c 3239
206415a3 3240 if (stop_print_frame)
c906108c
SS
3241 {
3242 int bpstat_ret;
3243 int source_flag;
917317f4 3244 int do_frame_printing = 1;
c906108c
SS
3245
3246 bpstat_ret = bpstat_print (stop_bpstat);
917317f4
JM
3247 switch (bpstat_ret)
3248 {
3249 case PRINT_UNKNOWN:
b0f4b84b
DJ
3250 /* If we had hit a shared library event breakpoint,
3251 bpstat_print would print out this message. If we hit
3252 an OS-level shared library event, do the same
3253 thing. */
3254 if (last.kind == TARGET_WAITKIND_LOADED)
3255 {
3256 printf_filtered (_("Stopped due to shared library event\n"));
3257 source_flag = SRC_LINE; /* something bogus */
3258 do_frame_printing = 0;
3259 break;
3260 }
3261
aa0cd9c1 3262 /* FIXME: cagney/2002-12-01: Given that a frame ID does
8fb3e588
AC
3263 (or should) carry around the function and does (or
3264 should) use that when doing a frame comparison. */
917317f4 3265 if (stop_step
aa0cd9c1
AC
3266 && frame_id_eq (step_frame_id,
3267 get_frame_id (get_current_frame ()))
917317f4 3268 && step_start_function == find_pc_function (stop_pc))
488f131b 3269 source_flag = SRC_LINE; /* finished step, just print source line */
917317f4 3270 else
488f131b 3271 source_flag = SRC_AND_LOC; /* print location and source line */
917317f4
JM
3272 break;
3273 case PRINT_SRC_AND_LOC:
488f131b 3274 source_flag = SRC_AND_LOC; /* print location and source line */
917317f4
JM
3275 break;
3276 case PRINT_SRC_ONLY:
c5394b80 3277 source_flag = SRC_LINE;
917317f4
JM
3278 break;
3279 case PRINT_NOTHING:
488f131b 3280 source_flag = SRC_LINE; /* something bogus */
917317f4
JM
3281 do_frame_printing = 0;
3282 break;
3283 default:
e2e0b3e5 3284 internal_error (__FILE__, __LINE__, _("Unknown value."));
917317f4 3285 }
c906108c 3286
9dc5e2a9 3287 if (ui_out_is_mi_like_p (uiout))
39f77062 3288 ui_out_field_int (uiout, "thread-id",
488f131b 3289 pid_to_thread_id (inferior_ptid));
c906108c
SS
3290 /* The behavior of this routine with respect to the source
3291 flag is:
c5394b80
JM
3292 SRC_LINE: Print only source line
3293 LOCATION: Print only location
3294 SRC_AND_LOC: Print location and source line */
917317f4 3295 if (do_frame_printing)
b04f3ab4 3296 print_stack_frame (get_selected_frame (NULL), 0, source_flag);
c906108c
SS
3297
3298 /* Display the auto-display expressions. */
3299 do_displays ();
3300 }
3301 }
3302
3303 /* Save the function value return registers, if we care.
3304 We might be about to restore their previous contents. */
3305 if (proceed_to_finish)
d5c31457
UW
3306 {
3307 /* This should not be necessary. */
3308 if (stop_registers)
3309 regcache_xfree (stop_registers);
3310
3311 /* NB: The copy goes through to the target picking up the value of
3312 all the registers. */
3313 stop_registers = regcache_dup (get_current_regcache ());
3314 }
c906108c
SS
3315
3316 if (stop_stack_dummy)
3317 {
dbe9fe58
AC
3318 /* Pop the empty frame that contains the stack dummy. POP_FRAME
3319 ends with a setting of the current frame, so we can use that
3320 next. */
3321 frame_pop (get_current_frame ());
c906108c 3322 /* Set stop_pc to what it was before we called the function.
c5aa993b
JM
3323 Can't rely on restore_inferior_status because that only gets
3324 called if we don't stop in the called function. */
c906108c 3325 stop_pc = read_pc ();
0f7d239c 3326 select_frame (get_current_frame ());
c906108c
SS
3327 }
3328
c906108c
SS
3329done:
3330 annotate_stopped ();
7a464420 3331 observer_notify_normal_stop (stop_bpstat);
c906108c
SS
3332}
3333
3334static int
96baa820 3335hook_stop_stub (void *cmd)
c906108c 3336{
5913bcb0 3337 execute_cmd_pre_hook ((struct cmd_list_element *) cmd);
c906108c
SS
3338 return (0);
3339}
3340\f
c5aa993b 3341int
96baa820 3342signal_stop_state (int signo)
c906108c
SS
3343{
3344 return signal_stop[signo];
3345}
3346
c5aa993b 3347int
96baa820 3348signal_print_state (int signo)
c906108c
SS
3349{
3350 return signal_print[signo];
3351}
3352
c5aa993b 3353int
96baa820 3354signal_pass_state (int signo)
c906108c
SS
3355{
3356 return signal_program[signo];
3357}
3358
488f131b 3359int
7bda5e4a 3360signal_stop_update (int signo, int state)
d4f3574e
SS
3361{
3362 int ret = signal_stop[signo];
3363 signal_stop[signo] = state;
3364 return ret;
3365}
3366
488f131b 3367int
7bda5e4a 3368signal_print_update (int signo, int state)
d4f3574e
SS
3369{
3370 int ret = signal_print[signo];
3371 signal_print[signo] = state;
3372 return ret;
3373}
3374
488f131b 3375int
7bda5e4a 3376signal_pass_update (int signo, int state)
d4f3574e
SS
3377{
3378 int ret = signal_program[signo];
3379 signal_program[signo] = state;
3380 return ret;
3381}
3382
c906108c 3383static void
96baa820 3384sig_print_header (void)
c906108c 3385{
a3f17187
AC
3386 printf_filtered (_("\
3387Signal Stop\tPrint\tPass to program\tDescription\n"));
c906108c
SS
3388}
3389
3390static void
96baa820 3391sig_print_info (enum target_signal oursig)
c906108c
SS
3392{
3393 char *name = target_signal_to_name (oursig);
3394 int name_padding = 13 - strlen (name);
96baa820 3395
c906108c
SS
3396 if (name_padding <= 0)
3397 name_padding = 0;
3398
3399 printf_filtered ("%s", name);
488f131b 3400 printf_filtered ("%*.*s ", name_padding, name_padding, " ");
c906108c
SS
3401 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
3402 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
3403 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
3404 printf_filtered ("%s\n", target_signal_to_string (oursig));
3405}
3406
3407/* Specify how various signals in the inferior should be handled. */
3408
3409static void
96baa820 3410handle_command (char *args, int from_tty)
c906108c
SS
3411{
3412 char **argv;
3413 int digits, wordlen;
3414 int sigfirst, signum, siglast;
3415 enum target_signal oursig;
3416 int allsigs;
3417 int nsigs;
3418 unsigned char *sigs;
3419 struct cleanup *old_chain;
3420
3421 if (args == NULL)
3422 {
e2e0b3e5 3423 error_no_arg (_("signal to handle"));
c906108c
SS
3424 }
3425
3426 /* Allocate and zero an array of flags for which signals to handle. */
3427
3428 nsigs = (int) TARGET_SIGNAL_LAST;
3429 sigs = (unsigned char *) alloca (nsigs);
3430 memset (sigs, 0, nsigs);
3431
3432 /* Break the command line up into args. */
3433
3434 argv = buildargv (args);
3435 if (argv == NULL)
3436 {
3437 nomem (0);
3438 }
7a292a7a 3439 old_chain = make_cleanup_freeargv (argv);
c906108c
SS
3440
3441 /* Walk through the args, looking for signal oursigs, signal names, and
3442 actions. Signal numbers and signal names may be interspersed with
3443 actions, with the actions being performed for all signals cumulatively
3444 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
3445
3446 while (*argv != NULL)
3447 {
3448 wordlen = strlen (*argv);
3449 for (digits = 0; isdigit ((*argv)[digits]); digits++)
3450 {;
3451 }
3452 allsigs = 0;
3453 sigfirst = siglast = -1;
3454
3455 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
3456 {
3457 /* Apply action to all signals except those used by the
3458 debugger. Silently skip those. */
3459 allsigs = 1;
3460 sigfirst = 0;
3461 siglast = nsigs - 1;
3462 }
3463 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
3464 {
3465 SET_SIGS (nsigs, sigs, signal_stop);
3466 SET_SIGS (nsigs, sigs, signal_print);
3467 }
3468 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
3469 {
3470 UNSET_SIGS (nsigs, sigs, signal_program);
3471 }
3472 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
3473 {
3474 SET_SIGS (nsigs, sigs, signal_print);
3475 }
3476 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
3477 {
3478 SET_SIGS (nsigs, sigs, signal_program);
3479 }
3480 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
3481 {
3482 UNSET_SIGS (nsigs, sigs, signal_stop);
3483 }
3484 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
3485 {
3486 SET_SIGS (nsigs, sigs, signal_program);
3487 }
3488 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
3489 {
3490 UNSET_SIGS (nsigs, sigs, signal_print);
3491 UNSET_SIGS (nsigs, sigs, signal_stop);
3492 }
3493 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
3494 {
3495 UNSET_SIGS (nsigs, sigs, signal_program);
3496 }
3497 else if (digits > 0)
3498 {
3499 /* It is numeric. The numeric signal refers to our own
3500 internal signal numbering from target.h, not to host/target
3501 signal number. This is a feature; users really should be
3502 using symbolic names anyway, and the common ones like
3503 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
3504
3505 sigfirst = siglast = (int)
3506 target_signal_from_command (atoi (*argv));
3507 if ((*argv)[digits] == '-')
3508 {
3509 siglast = (int)
3510 target_signal_from_command (atoi ((*argv) + digits + 1));
3511 }
3512 if (sigfirst > siglast)
3513 {
3514 /* Bet he didn't figure we'd think of this case... */
3515 signum = sigfirst;
3516 sigfirst = siglast;
3517 siglast = signum;
3518 }
3519 }
3520 else
3521 {
3522 oursig = target_signal_from_name (*argv);
3523 if (oursig != TARGET_SIGNAL_UNKNOWN)
3524 {
3525 sigfirst = siglast = (int) oursig;
3526 }
3527 else
3528 {
3529 /* Not a number and not a recognized flag word => complain. */
8a3fe4f8 3530 error (_("Unrecognized or ambiguous flag word: \"%s\"."), *argv);
c906108c
SS
3531 }
3532 }
3533
3534 /* If any signal numbers or symbol names were found, set flags for
c5aa993b 3535 which signals to apply actions to. */
c906108c
SS
3536
3537 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
3538 {
3539 switch ((enum target_signal) signum)
3540 {
3541 case TARGET_SIGNAL_TRAP:
3542 case TARGET_SIGNAL_INT:
3543 if (!allsigs && !sigs[signum])
3544 {
3545 if (query ("%s is used by the debugger.\n\
488f131b 3546Are you sure you want to change it? ", target_signal_to_name ((enum target_signal) signum)))
c906108c
SS
3547 {
3548 sigs[signum] = 1;
3549 }
3550 else
3551 {
a3f17187 3552 printf_unfiltered (_("Not confirmed, unchanged.\n"));
c906108c
SS
3553 gdb_flush (gdb_stdout);
3554 }
3555 }
3556 break;
3557 case TARGET_SIGNAL_0:
3558 case TARGET_SIGNAL_DEFAULT:
3559 case TARGET_SIGNAL_UNKNOWN:
3560 /* Make sure that "all" doesn't print these. */
3561 break;
3562 default:
3563 sigs[signum] = 1;
3564 break;
3565 }
3566 }
3567
3568 argv++;
3569 }
3570
39f77062 3571 target_notice_signals (inferior_ptid);
c906108c
SS
3572
3573 if (from_tty)
3574 {
3575 /* Show the results. */
3576 sig_print_header ();
3577 for (signum = 0; signum < nsigs; signum++)
3578 {
3579 if (sigs[signum])
3580 {
3581 sig_print_info (signum);
3582 }
3583 }
3584 }
3585
3586 do_cleanups (old_chain);
3587}
3588
3589static void
96baa820 3590xdb_handle_command (char *args, int from_tty)
c906108c
SS
3591{
3592 char **argv;
3593 struct cleanup *old_chain;
3594
3595 /* Break the command line up into args. */
3596
3597 argv = buildargv (args);
3598 if (argv == NULL)
3599 {
3600 nomem (0);
3601 }
7a292a7a 3602 old_chain = make_cleanup_freeargv (argv);
c906108c
SS
3603 if (argv[1] != (char *) NULL)
3604 {
3605 char *argBuf;
3606 int bufLen;
3607
3608 bufLen = strlen (argv[0]) + 20;
3609 argBuf = (char *) xmalloc (bufLen);
3610 if (argBuf)
3611 {
3612 int validFlag = 1;
3613 enum target_signal oursig;
3614
3615 oursig = target_signal_from_name (argv[0]);
3616 memset (argBuf, 0, bufLen);
3617 if (strcmp (argv[1], "Q") == 0)
3618 sprintf (argBuf, "%s %s", argv[0], "noprint");
3619 else
3620 {
3621 if (strcmp (argv[1], "s") == 0)
3622 {
3623 if (!signal_stop[oursig])
3624 sprintf (argBuf, "%s %s", argv[0], "stop");
3625 else
3626 sprintf (argBuf, "%s %s", argv[0], "nostop");
3627 }
3628 else if (strcmp (argv[1], "i") == 0)
3629 {
3630 if (!signal_program[oursig])
3631 sprintf (argBuf, "%s %s", argv[0], "pass");
3632 else
3633 sprintf (argBuf, "%s %s", argv[0], "nopass");
3634 }
3635 else if (strcmp (argv[1], "r") == 0)
3636 {
3637 if (!signal_print[oursig])
3638 sprintf (argBuf, "%s %s", argv[0], "print");
3639 else
3640 sprintf (argBuf, "%s %s", argv[0], "noprint");
3641 }
3642 else
3643 validFlag = 0;
3644 }
3645 if (validFlag)
3646 handle_command (argBuf, from_tty);
3647 else
a3f17187 3648 printf_filtered (_("Invalid signal handling flag.\n"));
c906108c 3649 if (argBuf)
b8c9b27d 3650 xfree (argBuf);
c906108c
SS
3651 }
3652 }
3653 do_cleanups (old_chain);
3654}
3655
3656/* Print current contents of the tables set by the handle command.
3657 It is possible we should just be printing signals actually used
3658 by the current target (but for things to work right when switching
3659 targets, all signals should be in the signal tables). */
3660
3661static void
96baa820 3662signals_info (char *signum_exp, int from_tty)
c906108c
SS
3663{
3664 enum target_signal oursig;
3665 sig_print_header ();
3666
3667 if (signum_exp)
3668 {
3669 /* First see if this is a symbol name. */
3670 oursig = target_signal_from_name (signum_exp);
3671 if (oursig == TARGET_SIGNAL_UNKNOWN)
3672 {
3673 /* No, try numeric. */
3674 oursig =
bb518678 3675 target_signal_from_command (parse_and_eval_long (signum_exp));
c906108c
SS
3676 }
3677 sig_print_info (oursig);
3678 return;
3679 }
3680
3681 printf_filtered ("\n");
3682 /* These ugly casts brought to you by the native VAX compiler. */
3683 for (oursig = TARGET_SIGNAL_FIRST;
3684 (int) oursig < (int) TARGET_SIGNAL_LAST;
3685 oursig = (enum target_signal) ((int) oursig + 1))
3686 {
3687 QUIT;
3688
3689 if (oursig != TARGET_SIGNAL_UNKNOWN
488f131b 3690 && oursig != TARGET_SIGNAL_DEFAULT && oursig != TARGET_SIGNAL_0)
c906108c
SS
3691 sig_print_info (oursig);
3692 }
3693
a3f17187 3694 printf_filtered (_("\nUse the \"handle\" command to change these tables.\n"));
c906108c
SS
3695}
3696\f
7a292a7a
SS
3697struct inferior_status
3698{
3699 enum target_signal stop_signal;
3700 CORE_ADDR stop_pc;
3701 bpstat stop_bpstat;
3702 int stop_step;
3703 int stop_stack_dummy;
3704 int stopped_by_random_signal;
3705 int trap_expected;
3706 CORE_ADDR step_range_start;
3707 CORE_ADDR step_range_end;
aa0cd9c1 3708 struct frame_id step_frame_id;
5fbbeb29 3709 enum step_over_calls_kind step_over_calls;
7a292a7a
SS
3710 CORE_ADDR step_resume_break_address;
3711 int stop_after_trap;
c0236d92 3712 int stop_soon;
7a292a7a
SS
3713
3714 /* These are here because if call_function_by_hand has written some
3715 registers and then decides to call error(), we better not have changed
3716 any registers. */
72cec141 3717 struct regcache *registers;
7a292a7a 3718
101dcfbe
AC
3719 /* A frame unique identifier. */
3720 struct frame_id selected_frame_id;
3721
7a292a7a
SS
3722 int breakpoint_proceeded;
3723 int restore_stack_info;
3724 int proceed_to_finish;
3725};
3726
7a292a7a 3727void
96baa820
JM
3728write_inferior_status_register (struct inferior_status *inf_status, int regno,
3729 LONGEST val)
7a292a7a 3730{
3acba339 3731 int size = register_size (current_gdbarch, regno);
7a292a7a
SS
3732 void *buf = alloca (size);
3733 store_signed_integer (buf, size, val);
0818c12a 3734 regcache_raw_write (inf_status->registers, regno, buf);
7a292a7a
SS
3735}
3736
c906108c
SS
3737/* Save all of the information associated with the inferior<==>gdb
3738 connection. INF_STATUS is a pointer to a "struct inferior_status"
3739 (defined in inferior.h). */
3740
7a292a7a 3741struct inferior_status *
96baa820 3742save_inferior_status (int restore_stack_info)
c906108c 3743{
72cec141 3744 struct inferior_status *inf_status = XMALLOC (struct inferior_status);
7a292a7a 3745
c906108c
SS
3746 inf_status->stop_signal = stop_signal;
3747 inf_status->stop_pc = stop_pc;
3748 inf_status->stop_step = stop_step;
3749 inf_status->stop_stack_dummy = stop_stack_dummy;
3750 inf_status->stopped_by_random_signal = stopped_by_random_signal;
3751 inf_status->trap_expected = trap_expected;
3752 inf_status->step_range_start = step_range_start;
3753 inf_status->step_range_end = step_range_end;
aa0cd9c1 3754 inf_status->step_frame_id = step_frame_id;
c906108c
SS
3755 inf_status->step_over_calls = step_over_calls;
3756 inf_status->stop_after_trap = stop_after_trap;
c0236d92 3757 inf_status->stop_soon = stop_soon;
c906108c
SS
3758 /* Save original bpstat chain here; replace it with copy of chain.
3759 If caller's caller is walking the chain, they'll be happier if we
7a292a7a
SS
3760 hand them back the original chain when restore_inferior_status is
3761 called. */
c906108c
SS
3762 inf_status->stop_bpstat = stop_bpstat;
3763 stop_bpstat = bpstat_copy (stop_bpstat);
3764 inf_status->breakpoint_proceeded = breakpoint_proceeded;
3765 inf_status->restore_stack_info = restore_stack_info;
3766 inf_status->proceed_to_finish = proceed_to_finish;
c5aa993b 3767
594f7785 3768 inf_status->registers = regcache_dup (get_current_regcache ());
c906108c 3769
206415a3 3770 inf_status->selected_frame_id = get_frame_id (get_selected_frame (NULL));
7a292a7a 3771 return inf_status;
c906108c
SS
3772}
3773
c906108c 3774static int
96baa820 3775restore_selected_frame (void *args)
c906108c 3776{
488f131b 3777 struct frame_id *fid = (struct frame_id *) args;
c906108c 3778 struct frame_info *frame;
c906108c 3779
101dcfbe 3780 frame = frame_find_by_id (*fid);
c906108c 3781
aa0cd9c1
AC
3782 /* If inf_status->selected_frame_id is NULL, there was no previously
3783 selected frame. */
101dcfbe 3784 if (frame == NULL)
c906108c 3785 {
8a3fe4f8 3786 warning (_("Unable to restore previously selected frame."));
c906108c
SS
3787 return 0;
3788 }
3789
0f7d239c 3790 select_frame (frame);
c906108c
SS
3791
3792 return (1);
3793}
3794
3795void
96baa820 3796restore_inferior_status (struct inferior_status *inf_status)
c906108c
SS
3797{
3798 stop_signal = inf_status->stop_signal;
3799 stop_pc = inf_status->stop_pc;
3800 stop_step = inf_status->stop_step;
3801 stop_stack_dummy = inf_status->stop_stack_dummy;
3802 stopped_by_random_signal = inf_status->stopped_by_random_signal;
3803 trap_expected = inf_status->trap_expected;
3804 step_range_start = inf_status->step_range_start;
3805 step_range_end = inf_status->step_range_end;
aa0cd9c1 3806 step_frame_id = inf_status->step_frame_id;
c906108c
SS
3807 step_over_calls = inf_status->step_over_calls;
3808 stop_after_trap = inf_status->stop_after_trap;
c0236d92 3809 stop_soon = inf_status->stop_soon;
c906108c
SS
3810 bpstat_clear (&stop_bpstat);
3811 stop_bpstat = inf_status->stop_bpstat;
3812 breakpoint_proceeded = inf_status->breakpoint_proceeded;
3813 proceed_to_finish = inf_status->proceed_to_finish;
3814
c906108c
SS
3815 /* The inferior can be gone if the user types "print exit(0)"
3816 (and perhaps other times). */
3817 if (target_has_execution)
72cec141 3818 /* NB: The register write goes through to the target. */
594f7785 3819 regcache_cpy (get_current_regcache (), inf_status->registers);
72cec141 3820 regcache_xfree (inf_status->registers);
c906108c 3821
c906108c
SS
3822 /* FIXME: If we are being called after stopping in a function which
3823 is called from gdb, we should not be trying to restore the
3824 selected frame; it just prints a spurious error message (The
3825 message is useful, however, in detecting bugs in gdb (like if gdb
3826 clobbers the stack)). In fact, should we be restoring the
3827 inferior status at all in that case? . */
3828
3829 if (target_has_stack && inf_status->restore_stack_info)
3830 {
c906108c 3831 /* The point of catch_errors is that if the stack is clobbered,
101dcfbe
AC
3832 walking the stack might encounter a garbage pointer and
3833 error() trying to dereference it. */
488f131b
JB
3834 if (catch_errors
3835 (restore_selected_frame, &inf_status->selected_frame_id,
3836 "Unable to restore previously selected frame:\n",
3837 RETURN_MASK_ERROR) == 0)
c906108c
SS
3838 /* Error in restoring the selected frame. Select the innermost
3839 frame. */
0f7d239c 3840 select_frame (get_current_frame ());
c906108c
SS
3841
3842 }
c906108c 3843
72cec141 3844 xfree (inf_status);
7a292a7a 3845}
c906108c 3846
74b7792f
AC
3847static void
3848do_restore_inferior_status_cleanup (void *sts)
3849{
3850 restore_inferior_status (sts);
3851}
3852
3853struct cleanup *
3854make_cleanup_restore_inferior_status (struct inferior_status *inf_status)
3855{
3856 return make_cleanup (do_restore_inferior_status_cleanup, inf_status);
3857}
3858
c906108c 3859void
96baa820 3860discard_inferior_status (struct inferior_status *inf_status)
7a292a7a
SS
3861{
3862 /* See save_inferior_status for info on stop_bpstat. */
3863 bpstat_clear (&inf_status->stop_bpstat);
72cec141 3864 regcache_xfree (inf_status->registers);
72cec141 3865 xfree (inf_status);
7a292a7a
SS
3866}
3867
47932f85
DJ
3868int
3869inferior_has_forked (int pid, int *child_pid)
3870{
3871 struct target_waitstatus last;
3872 ptid_t last_ptid;
3873
3874 get_last_target_status (&last_ptid, &last);
3875
3876 if (last.kind != TARGET_WAITKIND_FORKED)
3877 return 0;
3878
3879 if (ptid_get_pid (last_ptid) != pid)
3880 return 0;
3881
3882 *child_pid = last.value.related_pid;
3883 return 1;
3884}
3885
3886int
3887inferior_has_vforked (int pid, int *child_pid)
3888{
3889 struct target_waitstatus last;
3890 ptid_t last_ptid;
3891
3892 get_last_target_status (&last_ptid, &last);
3893
3894 if (last.kind != TARGET_WAITKIND_VFORKED)
3895 return 0;
3896
3897 if (ptid_get_pid (last_ptid) != pid)
3898 return 0;
3899
3900 *child_pid = last.value.related_pid;
3901 return 1;
3902}
3903
3904int
3905inferior_has_execd (int pid, char **execd_pathname)
3906{
3907 struct target_waitstatus last;
3908 ptid_t last_ptid;
3909
3910 get_last_target_status (&last_ptid, &last);
3911
3912 if (last.kind != TARGET_WAITKIND_EXECD)
3913 return 0;
3914
3915 if (ptid_get_pid (last_ptid) != pid)
3916 return 0;
3917
3918 *execd_pathname = xstrdup (last.value.execd_pathname);
3919 return 1;
3920}
3921
ca6724c1
KB
3922/* Oft used ptids */
3923ptid_t null_ptid;
3924ptid_t minus_one_ptid;
3925
3926/* Create a ptid given the necessary PID, LWP, and TID components. */
488f131b 3927
ca6724c1
KB
3928ptid_t
3929ptid_build (int pid, long lwp, long tid)
3930{
3931 ptid_t ptid;
3932
3933 ptid.pid = pid;
3934 ptid.lwp = lwp;
3935 ptid.tid = tid;
3936 return ptid;
3937}
3938
3939/* Create a ptid from just a pid. */
3940
3941ptid_t
3942pid_to_ptid (int pid)
3943{
3944 return ptid_build (pid, 0, 0);
3945}
3946
3947/* Fetch the pid (process id) component from a ptid. */
3948
3949int
3950ptid_get_pid (ptid_t ptid)
3951{
3952 return ptid.pid;
3953}
3954
3955/* Fetch the lwp (lightweight process) component from a ptid. */
3956
3957long
3958ptid_get_lwp (ptid_t ptid)
3959{
3960 return ptid.lwp;
3961}
3962
3963/* Fetch the tid (thread id) component from a ptid. */
3964
3965long
3966ptid_get_tid (ptid_t ptid)
3967{
3968 return ptid.tid;
3969}
3970
3971/* ptid_equal() is used to test equality of two ptids. */
3972
3973int
3974ptid_equal (ptid_t ptid1, ptid_t ptid2)
3975{
3976 return (ptid1.pid == ptid2.pid && ptid1.lwp == ptid2.lwp
488f131b 3977 && ptid1.tid == ptid2.tid);
ca6724c1
KB
3978}
3979
3980/* restore_inferior_ptid() will be used by the cleanup machinery
3981 to restore the inferior_ptid value saved in a call to
3982 save_inferior_ptid(). */
ce696e05
KB
3983
3984static void
3985restore_inferior_ptid (void *arg)
3986{
3987 ptid_t *saved_ptid_ptr = arg;
3988 inferior_ptid = *saved_ptid_ptr;
3989 xfree (arg);
3990}
3991
3992/* Save the value of inferior_ptid so that it may be restored by a
3993 later call to do_cleanups(). Returns the struct cleanup pointer
3994 needed for later doing the cleanup. */
3995
3996struct cleanup *
3997save_inferior_ptid (void)
3998{
3999 ptid_t *saved_ptid_ptr;
4000
4001 saved_ptid_ptr = xmalloc (sizeof (ptid_t));
4002 *saved_ptid_ptr = inferior_ptid;
4003 return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
4004}
c5aa993b 4005\f
488f131b 4006
c906108c 4007void
96baa820 4008_initialize_infrun (void)
c906108c 4009{
52f0bd74
AC
4010 int i;
4011 int numsigs;
c906108c
SS
4012 struct cmd_list_element *c;
4013
1bedd215
AC
4014 add_info ("signals", signals_info, _("\
4015What debugger does when program gets various signals.\n\
4016Specify a signal as argument to print info on that signal only."));
c906108c
SS
4017 add_info_alias ("handle", "signals", 0);
4018
1bedd215
AC
4019 add_com ("handle", class_run, handle_command, _("\
4020Specify how to handle a signal.\n\
c906108c
SS
4021Args are signals and actions to apply to those signals.\n\
4022Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
4023from 1-15 are allowed for compatibility with old versions of GDB.\n\
4024Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
4025The special arg \"all\" is recognized to mean all signals except those\n\
1bedd215
AC
4026used by the debugger, typically SIGTRAP and SIGINT.\n\
4027Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
c906108c
SS
4028\"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
4029Stop means reenter debugger if this signal happens (implies print).\n\
4030Print means print a message if this signal happens.\n\
4031Pass means let program see this signal; otherwise program doesn't know.\n\
4032Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
1bedd215 4033Pass and Stop may be combined."));
c906108c
SS
4034 if (xdb_commands)
4035 {
1bedd215
AC
4036 add_com ("lz", class_info, signals_info, _("\
4037What debugger does when program gets various signals.\n\
4038Specify a signal as argument to print info on that signal only."));
4039 add_com ("z", class_run, xdb_handle_command, _("\
4040Specify how to handle a signal.\n\
c906108c
SS
4041Args are signals and actions to apply to those signals.\n\
4042Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
4043from 1-15 are allowed for compatibility with old versions of GDB.\n\
4044Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
4045The special arg \"all\" is recognized to mean all signals except those\n\
1bedd215
AC
4046used by the debugger, typically SIGTRAP and SIGINT.\n\
4047Recognized actions include \"s\" (toggles between stop and nostop), \n\
c906108c
SS
4048\"r\" (toggles between print and noprint), \"i\" (toggles between pass and \
4049nopass), \"Q\" (noprint)\n\
4050Stop means reenter debugger if this signal happens (implies print).\n\
4051Print means print a message if this signal happens.\n\
4052Pass means let program see this signal; otherwise program doesn't know.\n\
4053Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
1bedd215 4054Pass and Stop may be combined."));
c906108c
SS
4055 }
4056
4057 if (!dbx_commands)
1a966eab
AC
4058 stop_command = add_cmd ("stop", class_obscure,
4059 not_just_help_class_command, _("\
4060There is no `stop' command, but you can set a hook on `stop'.\n\
c906108c 4061This allows you to set a list of commands to be run each time execution\n\
1a966eab 4062of the program stops."), &cmdlist);
c906108c 4063
85c07804
AC
4064 add_setshow_zinteger_cmd ("infrun", class_maintenance, &debug_infrun, _("\
4065Set inferior debugging."), _("\
4066Show inferior debugging."), _("\
4067When non-zero, inferior specific debugging is enabled."),
4068 NULL,
920d2a44 4069 show_debug_infrun,
85c07804 4070 &setdebuglist, &showdebuglist);
527159b7 4071
c906108c 4072 numsigs = (int) TARGET_SIGNAL_LAST;
488f131b 4073 signal_stop = (unsigned char *) xmalloc (sizeof (signal_stop[0]) * numsigs);
c906108c
SS
4074 signal_print = (unsigned char *)
4075 xmalloc (sizeof (signal_print[0]) * numsigs);
4076 signal_program = (unsigned char *)
4077 xmalloc (sizeof (signal_program[0]) * numsigs);
4078 for (i = 0; i < numsigs; i++)
4079 {
4080 signal_stop[i] = 1;
4081 signal_print[i] = 1;
4082 signal_program[i] = 1;
4083 }
4084
4085 /* Signals caused by debugger's own actions
4086 should not be given to the program afterwards. */
4087 signal_program[TARGET_SIGNAL_TRAP] = 0;
4088 signal_program[TARGET_SIGNAL_INT] = 0;
4089
4090 /* Signals that are not errors should not normally enter the debugger. */
4091 signal_stop[TARGET_SIGNAL_ALRM] = 0;
4092 signal_print[TARGET_SIGNAL_ALRM] = 0;
4093 signal_stop[TARGET_SIGNAL_VTALRM] = 0;
4094 signal_print[TARGET_SIGNAL_VTALRM] = 0;
4095 signal_stop[TARGET_SIGNAL_PROF] = 0;
4096 signal_print[TARGET_SIGNAL_PROF] = 0;
4097 signal_stop[TARGET_SIGNAL_CHLD] = 0;
4098 signal_print[TARGET_SIGNAL_CHLD] = 0;
4099 signal_stop[TARGET_SIGNAL_IO] = 0;
4100 signal_print[TARGET_SIGNAL_IO] = 0;
4101 signal_stop[TARGET_SIGNAL_POLL] = 0;
4102 signal_print[TARGET_SIGNAL_POLL] = 0;
4103 signal_stop[TARGET_SIGNAL_URG] = 0;
4104 signal_print[TARGET_SIGNAL_URG] = 0;
4105 signal_stop[TARGET_SIGNAL_WINCH] = 0;
4106 signal_print[TARGET_SIGNAL_WINCH] = 0;
4107
cd0fc7c3
SS
4108 /* These signals are used internally by user-level thread
4109 implementations. (See signal(5) on Solaris.) Like the above
4110 signals, a healthy program receives and handles them as part of
4111 its normal operation. */
4112 signal_stop[TARGET_SIGNAL_LWP] = 0;
4113 signal_print[TARGET_SIGNAL_LWP] = 0;
4114 signal_stop[TARGET_SIGNAL_WAITING] = 0;
4115 signal_print[TARGET_SIGNAL_WAITING] = 0;
4116 signal_stop[TARGET_SIGNAL_CANCEL] = 0;
4117 signal_print[TARGET_SIGNAL_CANCEL] = 0;
4118
85c07804
AC
4119 add_setshow_zinteger_cmd ("stop-on-solib-events", class_support,
4120 &stop_on_solib_events, _("\
4121Set stopping for shared library events."), _("\
4122Show stopping for shared library events."), _("\
c906108c
SS
4123If nonzero, gdb will give control to the user when the dynamic linker\n\
4124notifies gdb of shared library events. The most common event of interest\n\
85c07804
AC
4125to the user would be loading/unloading of a new library."),
4126 NULL,
920d2a44 4127 show_stop_on_solib_events,
85c07804 4128 &setlist, &showlist);
c906108c 4129
7ab04401
AC
4130 add_setshow_enum_cmd ("follow-fork-mode", class_run,
4131 follow_fork_mode_kind_names,
4132 &follow_fork_mode_string, _("\
4133Set debugger response to a program call of fork or vfork."), _("\
4134Show debugger response to a program call of fork or vfork."), _("\
c906108c
SS
4135A fork or vfork creates a new process. follow-fork-mode can be:\n\
4136 parent - the original process is debugged after a fork\n\
4137 child - the new process is debugged after a fork\n\
ea1dd7bc 4138The unfollowed process will continue to run.\n\
7ab04401
AC
4139By default, the debugger will follow the parent process."),
4140 NULL,
920d2a44 4141 show_follow_fork_mode_string,
7ab04401
AC
4142 &setlist, &showlist);
4143
4144 add_setshow_enum_cmd ("scheduler-locking", class_run,
4145 scheduler_enums, &scheduler_mode, _("\
4146Set mode for locking scheduler during execution."), _("\
4147Show mode for locking scheduler during execution."), _("\
c906108c
SS
4148off == no locking (threads may preempt at any time)\n\
4149on == full locking (no thread except the current thread may run)\n\
4150step == scheduler locked during every single-step operation.\n\
4151 In this mode, no other thread may run during a step command.\n\
7ab04401
AC
4152 Other threads may run while stepping over a function call ('next')."),
4153 set_schedlock_func, /* traps on target vector */
920d2a44 4154 show_scheduler_mode,
7ab04401 4155 &setlist, &showlist);
5fbbeb29 4156
5bf193a2
AC
4157 add_setshow_boolean_cmd ("step-mode", class_run, &step_stop_if_no_debug, _("\
4158Set mode of the step operation."), _("\
4159Show mode of the step operation."), _("\
4160When set, doing a step over a function without debug line information\n\
4161will stop at the first instruction of that function. Otherwise, the\n\
4162function is skipped and the step command stops at a different source line."),
4163 NULL,
920d2a44 4164 show_step_stop_if_no_debug,
5bf193a2 4165 &setlist, &showlist);
ca6724c1
KB
4166
4167 /* ptid initializations */
4168 null_ptid = ptid_build (0, 0, 0);
4169 minus_one_ptid = ptid_build (-1, 0, 0);
4170 inferior_ptid = null_ptid;
4171 target_last_wait_ptid = minus_one_ptid;
c906108c 4172}
This page took 1.380944 seconds and 4 git commands to generate.