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