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