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