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