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