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