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