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