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