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