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