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