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