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