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