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