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