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