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