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