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