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