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