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