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