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