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