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