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