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