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