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