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