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