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