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