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