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