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