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