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