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