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