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