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