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