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