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