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