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