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