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