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