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