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