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