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