* scm-lang.c (builtin_type_scm): Remove.
[deliverable/binutils-gdb.git] / gdb / infrun.c
CommitLineData
ca557f44
AC
1/* Target-struct-independent code to start (run) and stop an inferior
2 process.
8926118c 3
6aba47ca 4 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1 5 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
0fb0cc75 6 2008, 2009 Free Software Foundation, Inc.
c906108c 7
c5aa993b 8 This file is part of GDB.
c906108c 9
c5aa993b
JM
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
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
c5aa993b 13 (at your option) any later version.
c906108c 14
c5aa993b
JM
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.
c906108c 19
c5aa993b 20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
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"
60250e8b 29#include "exceptions.h"
c906108c 30#include "breakpoint.h"
03f2053f 31#include "gdb_wait.h"
c906108c
SS
32#include "gdbcore.h"
33#include "gdbcmd.h"
210661e7 34#include "cli/cli-script.h"
c906108c
SS
35#include "target.h"
36#include "gdbthread.h"
37#include "annotate.h"
1adeb98a 38#include "symfile.h"
7a292a7a 39#include "top.h"
c906108c 40#include <signal.h>
2acceee2 41#include "inf-loop.h"
4e052eda 42#include "regcache.h"
fd0407d6 43#include "value.h"
06600e06 44#include "observer.h"
f636b87d 45#include "language.h"
a77053c2 46#include "solib.h"
f17517ea 47#include "main.h"
9f976b41 48#include "gdb_assert.h"
034dad6f 49#include "mi/mi-common.h"
4f8d22e3 50#include "event-top.h"
96429cc8 51#include "record.h"
c906108c
SS
52
53/* Prototypes for local functions */
54
96baa820 55static void signals_info (char *, int);
c906108c 56
96baa820 57static void handle_command (char *, int);
c906108c 58
96baa820 59static void sig_print_info (enum target_signal);
c906108c 60
96baa820 61static void sig_print_header (void);
c906108c 62
74b7792f 63static void resume_cleanups (void *);
c906108c 64
96baa820 65static int hook_stop_stub (void *);
c906108c 66
96baa820
JM
67static int restore_selected_frame (void *);
68
69static void build_infrun (void);
70
4ef3f3be 71static int follow_fork (void);
96baa820
JM
72
73static void set_schedlock_func (char *args, int from_tty,
488f131b 74 struct cmd_list_element *c);
96baa820 75
4e1c45ea 76static int currently_stepping (struct thread_info *tp);
96baa820 77
b3444185
PA
78static int currently_stepping_or_nexting_callback (struct thread_info *tp,
79 void *data);
a7212384 80
96baa820
JM
81static void xdb_handle_command (char *args, int from_tty);
82
6a6b96b9 83static int prepare_to_proceed (int);
ea67f13b 84
96baa820 85void _initialize_infrun (void);
43ff13b4 86
e58b0e63
PA
87void nullify_last_target_wait_ptid (void);
88
5fbbeb29
CF
89/* When set, stop the 'step' command if we enter a function which has
90 no line number information. The normal behavior is that we step
91 over such function. */
92int step_stop_if_no_debug = 0;
920d2a44
AC
93static void
94show_step_stop_if_no_debug (struct ui_file *file, int from_tty,
95 struct cmd_list_element *c, const char *value)
96{
97 fprintf_filtered (file, _("Mode of the step operation is %s.\n"), value);
98}
5fbbeb29 99
43ff13b4 100/* In asynchronous mode, but simulating synchronous execution. */
96baa820 101
43ff13b4
JM
102int sync_execution = 0;
103
c906108c
SS
104/* wait_for_inferior and normal_stop use this to notify the user
105 when the inferior stopped in a different thread than it had been
96baa820
JM
106 running in. */
107
39f77062 108static ptid_t previous_inferior_ptid;
7a292a7a 109
237fc4c9
PA
110int debug_displaced = 0;
111static void
112show_debug_displaced (struct ui_file *file, int from_tty,
113 struct cmd_list_element *c, const char *value)
114{
115 fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
116}
117
527159b7 118static int debug_infrun = 0;
920d2a44
AC
119static void
120show_debug_infrun (struct ui_file *file, int from_tty,
121 struct cmd_list_element *c, const char *value)
122{
123 fprintf_filtered (file, _("Inferior debugging is %s.\n"), value);
124}
527159b7 125
d4f3574e
SS
126/* If the program uses ELF-style shared libraries, then calls to
127 functions in shared libraries go through stubs, which live in a
128 table called the PLT (Procedure Linkage Table). The first time the
129 function is called, the stub sends control to the dynamic linker,
130 which looks up the function's real address, patches the stub so
131 that future calls will go directly to the function, and then passes
132 control to the function.
133
134 If we are stepping at the source level, we don't want to see any of
135 this --- we just want to skip over the stub and the dynamic linker.
136 The simple approach is to single-step until control leaves the
137 dynamic linker.
138
ca557f44
AC
139 However, on some systems (e.g., Red Hat's 5.2 distribution) the
140 dynamic linker calls functions in the shared C library, so you
141 can't tell from the PC alone whether the dynamic linker is still
142 running. In this case, we use a step-resume breakpoint to get us
143 past the dynamic linker, as if we were using "next" to step over a
144 function call.
d4f3574e 145
cfd8ab24 146 in_solib_dynsym_resolve_code() says whether we're in the dynamic
d4f3574e
SS
147 linker code or not. Normally, this means we single-step. However,
148 if SKIP_SOLIB_RESOLVER then returns non-zero, then its value is an
149 address where we can place a step-resume breakpoint to get past the
150 linker's symbol resolution function.
151
cfd8ab24 152 in_solib_dynsym_resolve_code() can generally be implemented in a
d4f3574e
SS
153 pretty portable way, by comparing the PC against the address ranges
154 of the dynamic linker's sections.
155
156 SKIP_SOLIB_RESOLVER is generally going to be system-specific, since
157 it depends on internal details of the dynamic linker. It's usually
158 not too hard to figure out where to put a breakpoint, but it
159 certainly isn't portable. SKIP_SOLIB_RESOLVER should do plenty of
160 sanity checking. If it can't figure things out, returning zero and
161 getting the (possibly confusing) stepping behavior is better than
162 signalling an error, which will obscure the change in the
163 inferior's state. */
c906108c 164
c906108c
SS
165/* This function returns TRUE if pc is the address of an instruction
166 that lies within the dynamic linker (such as the event hook, or the
167 dld itself).
168
169 This function must be used only when a dynamic linker event has
170 been caught, and the inferior is being stepped out of the hook, or
171 undefined results are guaranteed. */
172
173#ifndef SOLIB_IN_DYNAMIC_LINKER
174#define SOLIB_IN_DYNAMIC_LINKER(pid,pc) 0
175#endif
176
c2c6d25f 177
7a292a7a
SS
178/* Convert the #defines into values. This is temporary until wfi control
179 flow is completely sorted out. */
180
692590c1
MS
181#ifndef CANNOT_STEP_HW_WATCHPOINTS
182#define CANNOT_STEP_HW_WATCHPOINTS 0
183#else
184#undef CANNOT_STEP_HW_WATCHPOINTS
185#define CANNOT_STEP_HW_WATCHPOINTS 1
186#endif
187
c906108c
SS
188/* Tables of how to react to signals; the user sets them. */
189
190static unsigned char *signal_stop;
191static unsigned char *signal_print;
192static unsigned char *signal_program;
193
194#define SET_SIGS(nsigs,sigs,flags) \
195 do { \
196 int signum = (nsigs); \
197 while (signum-- > 0) \
198 if ((sigs)[signum]) \
199 (flags)[signum] = 1; \
200 } while (0)
201
202#define UNSET_SIGS(nsigs,sigs,flags) \
203 do { \
204 int signum = (nsigs); \
205 while (signum-- > 0) \
206 if ((sigs)[signum]) \
207 (flags)[signum] = 0; \
208 } while (0)
209
39f77062
KB
210/* Value to pass to target_resume() to cause all threads to resume */
211
212#define RESUME_ALL (pid_to_ptid (-1))
c906108c
SS
213
214/* Command list pointer for the "stop" placeholder. */
215
216static struct cmd_list_element *stop_command;
217
c906108c
SS
218/* Function inferior was in as of last step command. */
219
220static struct symbol *step_start_function;
221
c906108c
SS
222/* Nonzero if we want to give control to the user when we're notified
223 of shared library events by the dynamic linker. */
224static int stop_on_solib_events;
920d2a44
AC
225static void
226show_stop_on_solib_events (struct ui_file *file, int from_tty,
227 struct cmd_list_element *c, const char *value)
228{
229 fprintf_filtered (file, _("Stopping for shared library events is %s.\n"),
230 value);
231}
c906108c 232
c906108c
SS
233/* Nonzero means expecting a trace trap
234 and should stop the inferior and return silently when it happens. */
235
236int stop_after_trap;
237
642fd101
DE
238/* Save register contents here when executing a "finish" command or are
239 about to pop a stack dummy frame, if-and-only-if proceed_to_finish is set.
c906108c
SS
240 Thus this contains the return value from the called function (assuming
241 values are returned in a register). */
242
72cec141 243struct regcache *stop_registers;
c906108c 244
c906108c
SS
245/* Nonzero after stop if current stack frame should be printed. */
246
247static int stop_print_frame;
248
e02bc4cc 249/* This is a cached copy of the pid/waitstatus of the last event
9a4105ab
AC
250 returned by target_wait()/deprecated_target_wait_hook(). This
251 information is returned by get_last_target_status(). */
39f77062 252static ptid_t target_last_wait_ptid;
e02bc4cc
DS
253static struct target_waitstatus target_last_waitstatus;
254
0d1e5fa7
PA
255static void context_switch (ptid_t ptid);
256
4e1c45ea 257void init_thread_stepping_state (struct thread_info *tss);
0d1e5fa7
PA
258
259void init_infwait_state (void);
a474d7c2 260
53904c9e
AC
261static const char follow_fork_mode_child[] = "child";
262static const char follow_fork_mode_parent[] = "parent";
263
488f131b 264static const char *follow_fork_mode_kind_names[] = {
53904c9e
AC
265 follow_fork_mode_child,
266 follow_fork_mode_parent,
267 NULL
ef346e04 268};
c906108c 269
53904c9e 270static const char *follow_fork_mode_string = follow_fork_mode_parent;
920d2a44
AC
271static void
272show_follow_fork_mode_string (struct ui_file *file, int from_tty,
273 struct cmd_list_element *c, const char *value)
274{
275 fprintf_filtered (file, _("\
276Debugger response to a program call of fork or vfork is \"%s\".\n"),
277 value);
278}
c906108c
SS
279\f
280
e58b0e63
PA
281/* Tell the target to follow the fork we're stopped at. Returns true
282 if the inferior should be resumed; false, if the target for some
283 reason decided it's best not to resume. */
284
6604731b 285static int
4ef3f3be 286follow_fork (void)
c906108c 287{
ea1dd7bc 288 int follow_child = (follow_fork_mode_string == follow_fork_mode_child);
e58b0e63
PA
289 int should_resume = 1;
290 struct thread_info *tp;
291
292 /* Copy user stepping state to the new inferior thread. FIXME: the
293 followed fork child thread should have a copy of most of the
4e3990f4
DE
294 parent thread structure's run control related fields, not just these.
295 Initialized to avoid "may be used uninitialized" warnings from gcc. */
296 struct breakpoint *step_resume_breakpoint = NULL;
297 CORE_ADDR step_range_start = 0;
298 CORE_ADDR step_range_end = 0;
299 struct frame_id step_frame_id = { 0 };
e58b0e63
PA
300
301 if (!non_stop)
302 {
303 ptid_t wait_ptid;
304 struct target_waitstatus wait_status;
305
306 /* Get the last target status returned by target_wait(). */
307 get_last_target_status (&wait_ptid, &wait_status);
308
309 /* If not stopped at a fork event, then there's nothing else to
310 do. */
311 if (wait_status.kind != TARGET_WAITKIND_FORKED
312 && wait_status.kind != TARGET_WAITKIND_VFORKED)
313 return 1;
314
315 /* Check if we switched over from WAIT_PTID, since the event was
316 reported. */
317 if (!ptid_equal (wait_ptid, minus_one_ptid)
318 && !ptid_equal (inferior_ptid, wait_ptid))
319 {
320 /* We did. Switch back to WAIT_PTID thread, to tell the
321 target to follow it (in either direction). We'll
322 afterwards refuse to resume, and inform the user what
323 happened. */
324 switch_to_thread (wait_ptid);
325 should_resume = 0;
326 }
327 }
328
329 tp = inferior_thread ();
330
331 /* If there were any forks/vforks that were caught and are now to be
332 followed, then do so now. */
333 switch (tp->pending_follow.kind)
334 {
335 case TARGET_WAITKIND_FORKED:
336 case TARGET_WAITKIND_VFORKED:
337 {
338 ptid_t parent, child;
339
340 /* If the user did a next/step, etc, over a fork call,
341 preserve the stepping state in the fork child. */
342 if (follow_child && should_resume)
343 {
344 step_resume_breakpoint
345 = clone_momentary_breakpoint (tp->step_resume_breakpoint);
346 step_range_start = tp->step_range_start;
347 step_range_end = tp->step_range_end;
348 step_frame_id = tp->step_frame_id;
349
350 /* For now, delete the parent's sr breakpoint, otherwise,
351 parent/child sr breakpoints are considered duplicates,
352 and the child version will not be installed. Remove
353 this when the breakpoints module becomes aware of
354 inferiors and address spaces. */
355 delete_step_resume_breakpoint (tp);
356 tp->step_range_start = 0;
357 tp->step_range_end = 0;
358 tp->step_frame_id = null_frame_id;
359 }
360
361 parent = inferior_ptid;
362 child = tp->pending_follow.value.related_pid;
363
364 /* Tell the target to do whatever is necessary to follow
365 either parent or child. */
366 if (target_follow_fork (follow_child))
367 {
368 /* Target refused to follow, or there's some other reason
369 we shouldn't resume. */
370 should_resume = 0;
371 }
372 else
373 {
374 /* This pending follow fork event is now handled, one way
375 or another. The previous selected thread may be gone
376 from the lists by now, but if it is still around, need
377 to clear the pending follow request. */
e09875d4 378 tp = find_thread_ptid (parent);
e58b0e63
PA
379 if (tp)
380 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
381
382 /* This makes sure we don't try to apply the "Switched
383 over from WAIT_PID" logic above. */
384 nullify_last_target_wait_ptid ();
385
386 /* If we followed the child, switch to it... */
387 if (follow_child)
388 {
389 switch_to_thread (child);
390
391 /* ... and preserve the stepping state, in case the
392 user was stepping over the fork call. */
393 if (should_resume)
394 {
395 tp = inferior_thread ();
396 tp->step_resume_breakpoint = step_resume_breakpoint;
397 tp->step_range_start = step_range_start;
398 tp->step_range_end = step_range_end;
399 tp->step_frame_id = step_frame_id;
400 }
401 else
402 {
403 /* If we get here, it was because we're trying to
404 resume from a fork catchpoint, but, the user
405 has switched threads away from the thread that
406 forked. In that case, the resume command
407 issued is most likely not applicable to the
408 child, so just warn, and refuse to resume. */
409 warning (_("\
410Not resuming: switched threads before following fork child.\n"));
411 }
412
413 /* Reset breakpoints in the child as appropriate. */
414 follow_inferior_reset_breakpoints ();
415 }
416 else
417 switch_to_thread (parent);
418 }
419 }
420 break;
421 case TARGET_WAITKIND_SPURIOUS:
422 /* Nothing to follow. */
423 break;
424 default:
425 internal_error (__FILE__, __LINE__,
426 "Unexpected pending_follow.kind %d\n",
427 tp->pending_follow.kind);
428 break;
429 }
c906108c 430
e58b0e63 431 return should_resume;
c906108c
SS
432}
433
6604731b
DJ
434void
435follow_inferior_reset_breakpoints (void)
c906108c 436{
4e1c45ea
PA
437 struct thread_info *tp = inferior_thread ();
438
6604731b
DJ
439 /* Was there a step_resume breakpoint? (There was if the user
440 did a "next" at the fork() call.) If so, explicitly reset its
441 thread number.
442
443 step_resumes are a form of bp that are made to be per-thread.
444 Since we created the step_resume bp when the parent process
445 was being debugged, and now are switching to the child process,
446 from the breakpoint package's viewpoint, that's a switch of
447 "threads". We must update the bp's notion of which thread
448 it is for, or it'll be ignored when it triggers. */
449
4e1c45ea
PA
450 if (tp->step_resume_breakpoint)
451 breakpoint_re_set_thread (tp->step_resume_breakpoint);
6604731b
DJ
452
453 /* Reinsert all breakpoints in the child. The user may have set
454 breakpoints after catching the fork, in which case those
455 were never set in the child, but only in the parent. This makes
456 sure the inserted breakpoints match the breakpoint list. */
457
458 breakpoint_re_set ();
459 insert_breakpoints ();
c906108c 460}
c906108c 461
1adeb98a
FN
462/* EXECD_PATHNAME is assumed to be non-NULL. */
463
c906108c 464static void
3a3e9ee3 465follow_exec (ptid_t pid, char *execd_pathname)
c906108c 466{
7a292a7a 467 struct target_ops *tgt;
4e1c45ea 468 struct thread_info *th = inferior_thread ();
7a292a7a 469
c906108c
SS
470 /* This is an exec event that we actually wish to pay attention to.
471 Refresh our symbol table to the newly exec'd program, remove any
472 momentary bp's, etc.
473
474 If there are breakpoints, they aren't really inserted now,
475 since the exec() transformed our inferior into a fresh set
476 of instructions.
477
478 We want to preserve symbolic breakpoints on the list, since
479 we have hopes that they can be reset after the new a.out's
480 symbol table is read.
481
482 However, any "raw" breakpoints must be removed from the list
483 (e.g., the solib bp's), since their address is probably invalid
484 now.
485
486 And, we DON'T want to call delete_breakpoints() here, since
487 that may write the bp's "shadow contents" (the instruction
488 value that was overwritten witha TRAP instruction). Since
489 we now have a new a.out, those shadow contents aren't valid. */
490 update_breakpoints_after_exec ();
491
492 /* If there was one, it's gone now. We cannot truly step-to-next
493 statement through an exec(). */
4e1c45ea
PA
494 th->step_resume_breakpoint = NULL;
495 th->step_range_start = 0;
496 th->step_range_end = 0;
c906108c 497
a75724bc
PA
498 /* The target reports the exec event to the main thread, even if
499 some other thread does the exec, and even if the main thread was
500 already stopped --- if debugging in non-stop mode, it's possible
501 the user had the main thread held stopped in the previous image
502 --- release it now. This is the same behavior as step-over-exec
503 with scheduler-locking on in all-stop mode. */
504 th->stop_requested = 0;
505
c906108c 506 /* What is this a.out's name? */
a3f17187 507 printf_unfiltered (_("Executing new program: %s\n"), execd_pathname);
c906108c
SS
508
509 /* We've followed the inferior through an exec. Therefore, the
510 inferior has essentially been killed & reborn. */
7a292a7a 511
c906108c 512 gdb_flush (gdb_stdout);
6ca15a4b
PA
513
514 breakpoint_init_inferior (inf_execd);
e85a822c
DJ
515
516 if (gdb_sysroot && *gdb_sysroot)
517 {
518 char *name = alloca (strlen (gdb_sysroot)
519 + strlen (execd_pathname)
520 + 1);
521 strcpy (name, gdb_sysroot);
522 strcat (name, execd_pathname);
523 execd_pathname = name;
524 }
c906108c
SS
525
526 /* That a.out is now the one to use. */
527 exec_file_attach (execd_pathname, 0);
528
cce9b6bf
PA
529 /* Reset the shared library package. This ensures that we get a
530 shlib event when the child reaches "_start", at which point the
531 dld will have had a chance to initialize the child. */
532 /* Also, loading a symbol file below may trigger symbol lookups, and
533 we don't want those to be satisfied by the libraries of the
534 previous incarnation of this process. */
535 no_shared_libraries (NULL, 0);
536
537 /* Load the main file's symbols. */
1adeb98a 538 symbol_file_add_main (execd_pathname, 0);
c906108c 539
7a292a7a 540#ifdef SOLIB_CREATE_INFERIOR_HOOK
39f77062 541 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
a77053c2
MK
542#else
543 solib_create_inferior_hook ();
7a292a7a 544#endif
c906108c
SS
545
546 /* Reinsert all breakpoints. (Those which were symbolic have
547 been reset to the proper address in the new a.out, thanks
548 to symbol_file_command...) */
549 insert_breakpoints ();
550
551 /* The next resume of this inferior should bring it to the shlib
552 startup breakpoints. (If the user had also set bp's on
553 "main" from the old (parent) process, then they'll auto-
554 matically get reset there in the new process.) */
c906108c
SS
555}
556
557/* Non-zero if we just simulating a single-step. This is needed
558 because we cannot remove the breakpoints in the inferior process
559 until after the `wait' in `wait_for_inferior'. */
560static int singlestep_breakpoints_inserted_p = 0;
9f976b41
DJ
561
562/* The thread we inserted single-step breakpoints for. */
563static ptid_t singlestep_ptid;
564
fd48f117
DJ
565/* PC when we started this single-step. */
566static CORE_ADDR singlestep_pc;
567
9f976b41
DJ
568/* If another thread hit the singlestep breakpoint, we save the original
569 thread here so that we can resume single-stepping it later. */
570static ptid_t saved_singlestep_ptid;
571static int stepping_past_singlestep_breakpoint;
6a6b96b9 572
ca67fcb8
VP
573/* If not equal to null_ptid, this means that after stepping over breakpoint
574 is finished, we need to switch to deferred_step_ptid, and step it.
575
576 The use case is when one thread has hit a breakpoint, and then the user
577 has switched to another thread and issued 'step'. We need to step over
578 breakpoint in the thread which hit the breakpoint, but then continue
579 stepping the thread user has selected. */
580static ptid_t deferred_step_ptid;
c906108c 581\f
237fc4c9
PA
582/* Displaced stepping. */
583
584/* In non-stop debugging mode, we must take special care to manage
585 breakpoints properly; in particular, the traditional strategy for
586 stepping a thread past a breakpoint it has hit is unsuitable.
587 'Displaced stepping' is a tactic for stepping one thread past a
588 breakpoint it has hit while ensuring that other threads running
589 concurrently will hit the breakpoint as they should.
590
591 The traditional way to step a thread T off a breakpoint in a
592 multi-threaded program in all-stop mode is as follows:
593
594 a0) Initially, all threads are stopped, and breakpoints are not
595 inserted.
596 a1) We single-step T, leaving breakpoints uninserted.
597 a2) We insert breakpoints, and resume all threads.
598
599 In non-stop debugging, however, this strategy is unsuitable: we
600 don't want to have to stop all threads in the system in order to
601 continue or step T past a breakpoint. Instead, we use displaced
602 stepping:
603
604 n0) Initially, T is stopped, other threads are running, and
605 breakpoints are inserted.
606 n1) We copy the instruction "under" the breakpoint to a separate
607 location, outside the main code stream, making any adjustments
608 to the instruction, register, and memory state as directed by
609 T's architecture.
610 n2) We single-step T over the instruction at its new location.
611 n3) We adjust the resulting register and memory state as directed
612 by T's architecture. This includes resetting T's PC to point
613 back into the main instruction stream.
614 n4) We resume T.
615
616 This approach depends on the following gdbarch methods:
617
618 - gdbarch_max_insn_length and gdbarch_displaced_step_location
619 indicate where to copy the instruction, and how much space must
620 be reserved there. We use these in step n1.
621
622 - gdbarch_displaced_step_copy_insn copies a instruction to a new
623 address, and makes any necessary adjustments to the instruction,
624 register contents, and memory. We use this in step n1.
625
626 - gdbarch_displaced_step_fixup adjusts registers and memory after
627 we have successfuly single-stepped the instruction, to yield the
628 same effect the instruction would have had if we had executed it
629 at its original address. We use this in step n3.
630
631 - gdbarch_displaced_step_free_closure provides cleanup.
632
633 The gdbarch_displaced_step_copy_insn and
634 gdbarch_displaced_step_fixup functions must be written so that
635 copying an instruction with gdbarch_displaced_step_copy_insn,
636 single-stepping across the copied instruction, and then applying
637 gdbarch_displaced_insn_fixup should have the same effects on the
638 thread's memory and registers as stepping the instruction in place
639 would have. Exactly which responsibilities fall to the copy and
640 which fall to the fixup is up to the author of those functions.
641
642 See the comments in gdbarch.sh for details.
643
644 Note that displaced stepping and software single-step cannot
645 currently be used in combination, although with some care I think
646 they could be made to. Software single-step works by placing
647 breakpoints on all possible subsequent instructions; if the
648 displaced instruction is a PC-relative jump, those breakpoints
649 could fall in very strange places --- on pages that aren't
650 executable, or at addresses that are not proper instruction
651 boundaries. (We do generally let other threads run while we wait
652 to hit the software single-step breakpoint, and they might
653 encounter such a corrupted instruction.) One way to work around
654 this would be to have gdbarch_displaced_step_copy_insn fully
655 simulate the effect of PC-relative instructions (and return NULL)
656 on architectures that use software single-stepping.
657
658 In non-stop mode, we can have independent and simultaneous step
659 requests, so more than one thread may need to simultaneously step
660 over a breakpoint. The current implementation assumes there is
661 only one scratch space per process. In this case, we have to
662 serialize access to the scratch space. If thread A wants to step
663 over a breakpoint, but we are currently waiting for some other
664 thread to complete a displaced step, we leave thread A stopped and
665 place it in the displaced_step_request_queue. Whenever a displaced
666 step finishes, we pick the next thread in the queue and start a new
667 displaced step operation on it. See displaced_step_prepare and
668 displaced_step_fixup for details. */
669
670/* If this is not null_ptid, this is the thread carrying out a
671 displaced single-step. This thread's state will require fixing up
672 once it has completed its step. */
673static ptid_t displaced_step_ptid;
674
675struct displaced_step_request
676{
677 ptid_t ptid;
678 struct displaced_step_request *next;
679};
680
681/* A queue of pending displaced stepping requests. */
682struct displaced_step_request *displaced_step_request_queue;
683
684/* The architecture the thread had when we stepped it. */
685static struct gdbarch *displaced_step_gdbarch;
686
687/* The closure provided gdbarch_displaced_step_copy_insn, to be used
688 for post-step cleanup. */
689static struct displaced_step_closure *displaced_step_closure;
690
691/* The address of the original instruction, and the copy we made. */
692static CORE_ADDR displaced_step_original, displaced_step_copy;
693
694/* Saved contents of copy area. */
695static gdb_byte *displaced_step_saved_copy;
696
fff08868
HZ
697/* Enum strings for "set|show displaced-stepping". */
698
699static const char can_use_displaced_stepping_auto[] = "auto";
700static const char can_use_displaced_stepping_on[] = "on";
701static const char can_use_displaced_stepping_off[] = "off";
702static const char *can_use_displaced_stepping_enum[] =
703{
704 can_use_displaced_stepping_auto,
705 can_use_displaced_stepping_on,
706 can_use_displaced_stepping_off,
707 NULL,
708};
709
710/* If ON, and the architecture supports it, GDB will use displaced
711 stepping to step over breakpoints. If OFF, or if the architecture
712 doesn't support it, GDB will instead use the traditional
713 hold-and-step approach. If AUTO (which is the default), GDB will
714 decide which technique to use to step over breakpoints depending on
715 which of all-stop or non-stop mode is active --- displaced stepping
716 in non-stop mode; hold-and-step in all-stop mode. */
717
718static const char *can_use_displaced_stepping =
719 can_use_displaced_stepping_auto;
720
237fc4c9
PA
721static void
722show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
723 struct cmd_list_element *c,
724 const char *value)
725{
fff08868
HZ
726 if (can_use_displaced_stepping == can_use_displaced_stepping_auto)
727 fprintf_filtered (file, _("\
728Debugger's willingness to use displaced stepping to step over \
729breakpoints is %s (currently %s).\n"),
730 value, non_stop ? "on" : "off");
731 else
732 fprintf_filtered (file, _("\
733Debugger's willingness to use displaced stepping to step over \
734breakpoints is %s.\n"), value);
237fc4c9
PA
735}
736
fff08868
HZ
737/* Return non-zero if displaced stepping can/should be used to step
738 over breakpoints. */
739
237fc4c9
PA
740static int
741use_displaced_stepping (struct gdbarch *gdbarch)
742{
fff08868
HZ
743 return (((can_use_displaced_stepping == can_use_displaced_stepping_auto
744 && non_stop)
745 || can_use_displaced_stepping == can_use_displaced_stepping_on)
96429cc8
HZ
746 && gdbarch_displaced_step_copy_insn_p (gdbarch)
747 && !RECORD_IS_USED);
237fc4c9
PA
748}
749
750/* Clean out any stray displaced stepping state. */
751static void
752displaced_step_clear (void)
753{
754 /* Indicate that there is no cleanup pending. */
755 displaced_step_ptid = null_ptid;
756
757 if (displaced_step_closure)
758 {
759 gdbarch_displaced_step_free_closure (displaced_step_gdbarch,
760 displaced_step_closure);
761 displaced_step_closure = NULL;
762 }
763}
764
765static void
766cleanup_displaced_step_closure (void *ptr)
767{
768 struct displaced_step_closure *closure = ptr;
769
770 gdbarch_displaced_step_free_closure (current_gdbarch, closure);
771}
772
773/* Dump LEN bytes at BUF in hex to FILE, followed by a newline. */
774void
775displaced_step_dump_bytes (struct ui_file *file,
776 const gdb_byte *buf,
777 size_t len)
778{
779 int i;
780
781 for (i = 0; i < len; i++)
782 fprintf_unfiltered (file, "%02x ", buf[i]);
783 fputs_unfiltered ("\n", file);
784}
785
786/* Prepare to single-step, using displaced stepping.
787
788 Note that we cannot use displaced stepping when we have a signal to
789 deliver. If we have a signal to deliver and an instruction to step
790 over, then after the step, there will be no indication from the
791 target whether the thread entered a signal handler or ignored the
792 signal and stepped over the instruction successfully --- both cases
793 result in a simple SIGTRAP. In the first case we mustn't do a
794 fixup, and in the second case we must --- but we can't tell which.
795 Comments in the code for 'random signals' in handle_inferior_event
796 explain how we handle this case instead.
797
798 Returns 1 if preparing was successful -- this thread is going to be
799 stepped now; or 0 if displaced stepping this thread got queued. */
800static int
801displaced_step_prepare (ptid_t ptid)
802{
ad53cd71 803 struct cleanup *old_cleanups, *ignore_cleanups;
237fc4c9
PA
804 struct regcache *regcache = get_thread_regcache (ptid);
805 struct gdbarch *gdbarch = get_regcache_arch (regcache);
806 CORE_ADDR original, copy;
807 ULONGEST len;
808 struct displaced_step_closure *closure;
809
810 /* We should never reach this function if the architecture does not
811 support displaced stepping. */
812 gdb_assert (gdbarch_displaced_step_copy_insn_p (gdbarch));
813
814 /* For the first cut, we're displaced stepping one thread at a
815 time. */
816
817 if (!ptid_equal (displaced_step_ptid, null_ptid))
818 {
819 /* Already waiting for a displaced step to finish. Defer this
820 request and place in queue. */
821 struct displaced_step_request *req, *new_req;
822
823 if (debug_displaced)
824 fprintf_unfiltered (gdb_stdlog,
825 "displaced: defering step of %s\n",
826 target_pid_to_str (ptid));
827
828 new_req = xmalloc (sizeof (*new_req));
829 new_req->ptid = ptid;
830 new_req->next = NULL;
831
832 if (displaced_step_request_queue)
833 {
834 for (req = displaced_step_request_queue;
835 req && req->next;
836 req = req->next)
837 ;
838 req->next = new_req;
839 }
840 else
841 displaced_step_request_queue = new_req;
842
843 return 0;
844 }
845 else
846 {
847 if (debug_displaced)
848 fprintf_unfiltered (gdb_stdlog,
849 "displaced: stepping %s now\n",
850 target_pid_to_str (ptid));
851 }
852
853 displaced_step_clear ();
854
ad53cd71
PA
855 old_cleanups = save_inferior_ptid ();
856 inferior_ptid = ptid;
857
515630c5 858 original = regcache_read_pc (regcache);
237fc4c9
PA
859
860 copy = gdbarch_displaced_step_location (gdbarch);
861 len = gdbarch_max_insn_length (gdbarch);
862
863 /* Save the original contents of the copy area. */
864 displaced_step_saved_copy = xmalloc (len);
ad53cd71
PA
865 ignore_cleanups = make_cleanup (free_current_contents,
866 &displaced_step_saved_copy);
237fc4c9
PA
867 read_memory (copy, displaced_step_saved_copy, len);
868 if (debug_displaced)
869 {
870 fprintf_unfiltered (gdb_stdlog, "displaced: saved 0x%s: ",
871 paddr_nz (copy));
872 displaced_step_dump_bytes (gdb_stdlog, displaced_step_saved_copy, len);
873 };
874
875 closure = gdbarch_displaced_step_copy_insn (gdbarch,
ad53cd71 876 original, copy, regcache);
237fc4c9
PA
877
878 /* We don't support the fully-simulated case at present. */
879 gdb_assert (closure);
880
881 make_cleanup (cleanup_displaced_step_closure, closure);
882
883 /* Resume execution at the copy. */
515630c5 884 regcache_write_pc (regcache, copy);
237fc4c9 885
ad53cd71
PA
886 discard_cleanups (ignore_cleanups);
887
888 do_cleanups (old_cleanups);
237fc4c9
PA
889
890 if (debug_displaced)
891 fprintf_unfiltered (gdb_stdlog, "displaced: displaced pc to 0x%s\n",
ad53cd71 892 paddr_nz (copy));
237fc4c9
PA
893
894 /* Save the information we need to fix things up if the step
895 succeeds. */
896 displaced_step_ptid = ptid;
897 displaced_step_gdbarch = gdbarch;
898 displaced_step_closure = closure;
899 displaced_step_original = original;
900 displaced_step_copy = copy;
901 return 1;
902}
903
904static void
905displaced_step_clear_cleanup (void *ignore)
906{
907 displaced_step_clear ();
908}
909
910static void
911write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
912{
913 struct cleanup *ptid_cleanup = save_inferior_ptid ();
914 inferior_ptid = ptid;
915 write_memory (memaddr, myaddr, len);
916 do_cleanups (ptid_cleanup);
917}
918
919static void
920displaced_step_fixup (ptid_t event_ptid, enum target_signal signal)
921{
922 struct cleanup *old_cleanups;
923
924 /* Was this event for the pid we displaced? */
925 if (ptid_equal (displaced_step_ptid, null_ptid)
926 || ! ptid_equal (displaced_step_ptid, event_ptid))
927 return;
928
929 old_cleanups = make_cleanup (displaced_step_clear_cleanup, 0);
930
931 /* Restore the contents of the copy area. */
932 {
933 ULONGEST len = gdbarch_max_insn_length (displaced_step_gdbarch);
934 write_memory_ptid (displaced_step_ptid, displaced_step_copy,
935 displaced_step_saved_copy, len);
936 if (debug_displaced)
937 fprintf_unfiltered (gdb_stdlog, "displaced: restored 0x%s\n",
938 paddr_nz (displaced_step_copy));
939 }
940
941 /* Did the instruction complete successfully? */
942 if (signal == TARGET_SIGNAL_TRAP)
943 {
944 /* Fix up the resulting state. */
945 gdbarch_displaced_step_fixup (displaced_step_gdbarch,
946 displaced_step_closure,
947 displaced_step_original,
948 displaced_step_copy,
949 get_thread_regcache (displaced_step_ptid));
950 }
951 else
952 {
953 /* Since the instruction didn't complete, all we can do is
954 relocate the PC. */
515630c5
UW
955 struct regcache *regcache = get_thread_regcache (event_ptid);
956 CORE_ADDR pc = regcache_read_pc (regcache);
237fc4c9 957 pc = displaced_step_original + (pc - displaced_step_copy);
515630c5 958 regcache_write_pc (regcache, pc);
237fc4c9
PA
959 }
960
961 do_cleanups (old_cleanups);
962
1c5cfe86
PA
963 displaced_step_ptid = null_ptid;
964
237fc4c9
PA
965 /* Are there any pending displaced stepping requests? If so, run
966 one now. */
1c5cfe86 967 while (displaced_step_request_queue)
237fc4c9
PA
968 {
969 struct displaced_step_request *head;
970 ptid_t ptid;
1c5cfe86 971 CORE_ADDR actual_pc;
237fc4c9
PA
972
973 head = displaced_step_request_queue;
974 ptid = head->ptid;
975 displaced_step_request_queue = head->next;
976 xfree (head);
977
ad53cd71
PA
978 context_switch (ptid);
979
fb14de7b 980 actual_pc = regcache_read_pc (get_thread_regcache (ptid));
1c5cfe86
PA
981
982 if (breakpoint_here_p (actual_pc))
ad53cd71 983 {
1c5cfe86
PA
984 if (debug_displaced)
985 fprintf_unfiltered (gdb_stdlog,
986 "displaced: stepping queued %s now\n",
987 target_pid_to_str (ptid));
988
989 displaced_step_prepare (ptid);
990
991 if (debug_displaced)
992 {
993 gdb_byte buf[4];
994
995 fprintf_unfiltered (gdb_stdlog, "displaced: run 0x%s: ",
996 paddr_nz (actual_pc));
997 read_memory (actual_pc, buf, sizeof (buf));
998 displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
999 }
1000
1001 target_resume (ptid, 1, TARGET_SIGNAL_0);
1002
1003 /* Done, we're stepping a thread. */
1004 break;
ad53cd71 1005 }
1c5cfe86
PA
1006 else
1007 {
1008 int step;
1009 struct thread_info *tp = inferior_thread ();
1010
1011 /* The breakpoint we were sitting under has since been
1012 removed. */
1013 tp->trap_expected = 0;
1014
1015 /* Go back to what we were trying to do. */
1016 step = currently_stepping (tp);
ad53cd71 1017
1c5cfe86
PA
1018 if (debug_displaced)
1019 fprintf_unfiltered (gdb_stdlog, "breakpoint is gone %s: step(%d)\n",
1020 target_pid_to_str (tp->ptid), step);
1021
1022 target_resume (ptid, step, TARGET_SIGNAL_0);
1023 tp->stop_signal = TARGET_SIGNAL_0;
1024
1025 /* This request was discarded. See if there's any other
1026 thread waiting for its turn. */
1027 }
237fc4c9
PA
1028 }
1029}
1030
5231c1fd
PA
1031/* Update global variables holding ptids to hold NEW_PTID if they were
1032 holding OLD_PTID. */
1033static void
1034infrun_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1035{
1036 struct displaced_step_request *it;
1037
1038 if (ptid_equal (inferior_ptid, old_ptid))
1039 inferior_ptid = new_ptid;
1040
1041 if (ptid_equal (singlestep_ptid, old_ptid))
1042 singlestep_ptid = new_ptid;
1043
1044 if (ptid_equal (displaced_step_ptid, old_ptid))
1045 displaced_step_ptid = new_ptid;
1046
1047 if (ptid_equal (deferred_step_ptid, old_ptid))
1048 deferred_step_ptid = new_ptid;
1049
1050 for (it = displaced_step_request_queue; it; it = it->next)
1051 if (ptid_equal (it->ptid, old_ptid))
1052 it->ptid = new_ptid;
1053}
1054
237fc4c9
PA
1055\f
1056/* Resuming. */
c906108c
SS
1057
1058/* Things to clean up if we QUIT out of resume (). */
c906108c 1059static void
74b7792f 1060resume_cleanups (void *ignore)
c906108c
SS
1061{
1062 normal_stop ();
1063}
1064
53904c9e
AC
1065static const char schedlock_off[] = "off";
1066static const char schedlock_on[] = "on";
1067static const char schedlock_step[] = "step";
488f131b 1068static const char *scheduler_enums[] = {
ef346e04
AC
1069 schedlock_off,
1070 schedlock_on,
1071 schedlock_step,
1072 NULL
1073};
920d2a44
AC
1074static const char *scheduler_mode = schedlock_off;
1075static void
1076show_scheduler_mode (struct ui_file *file, int from_tty,
1077 struct cmd_list_element *c, const char *value)
1078{
1079 fprintf_filtered (file, _("\
1080Mode for locking scheduler during execution is \"%s\".\n"),
1081 value);
1082}
c906108c
SS
1083
1084static void
96baa820 1085set_schedlock_func (char *args, int from_tty, struct cmd_list_element *c)
c906108c 1086{
eefe576e
AC
1087 if (!target_can_lock_scheduler)
1088 {
1089 scheduler_mode = schedlock_off;
1090 error (_("Target '%s' cannot support this command."), target_shortname);
1091 }
c906108c
SS
1092}
1093
2facfe5c
DD
1094/* Try to setup for software single stepping over the specified location.
1095 Return 1 if target_resume() should use hardware single step.
1096
1097 GDBARCH the current gdbarch.
1098 PC the location to step over. */
1099
1100static int
1101maybe_software_singlestep (struct gdbarch *gdbarch, CORE_ADDR pc)
1102{
1103 int hw_step = 1;
1104
1105 if (gdbarch_software_single_step_p (gdbarch)
1106 && gdbarch_software_single_step (gdbarch, get_current_frame ()))
1107 {
1108 hw_step = 0;
1109 /* Do not pull these breakpoints until after a `wait' in
1110 `wait_for_inferior' */
1111 singlestep_breakpoints_inserted_p = 1;
1112 singlestep_ptid = inferior_ptid;
1113 singlestep_pc = pc;
1114 }
1115 return hw_step;
1116}
c906108c
SS
1117
1118/* Resume the inferior, but allow a QUIT. This is useful if the user
1119 wants to interrupt some lengthy single-stepping operation
1120 (for child processes, the SIGINT goes to the inferior, and so
1121 we get a SIGINT random_signal, but for remote debugging and perhaps
1122 other targets, that's not true).
1123
1124 STEP nonzero if we should step (zero to continue instead).
1125 SIG is the signal to give the inferior (zero for none). */
1126void
96baa820 1127resume (int step, enum target_signal sig)
c906108c
SS
1128{
1129 int should_resume = 1;
74b7792f 1130 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
515630c5
UW
1131 struct regcache *regcache = get_current_regcache ();
1132 struct gdbarch *gdbarch = get_regcache_arch (regcache);
4e1c45ea 1133 struct thread_info *tp = inferior_thread ();
515630c5 1134 CORE_ADDR pc = regcache_read_pc (regcache);
c7e8a53c 1135
c906108c
SS
1136 QUIT;
1137
527159b7 1138 if (debug_infrun)
237fc4c9
PA
1139 fprintf_unfiltered (gdb_stdlog,
1140 "infrun: resume (step=%d, signal=%d), "
4e1c45ea
PA
1141 "trap_expected=%d\n",
1142 step, sig, tp->trap_expected);
c906108c 1143
692590c1
MS
1144 /* Some targets (e.g. Solaris x86) have a kernel bug when stepping
1145 over an instruction that causes a page fault without triggering
1146 a hardware watchpoint. The kernel properly notices that it shouldn't
1147 stop, because the hardware watchpoint is not triggered, but it forgets
1148 the step request and continues the program normally.
1149 Work around the problem by removing hardware watchpoints if a step is
1150 requested, GDB will check for a hardware watchpoint trigger after the
1151 step anyway. */
c36b740a 1152 if (CANNOT_STEP_HW_WATCHPOINTS && step)
692590c1 1153 remove_hw_watchpoints ();
488f131b 1154
692590c1 1155
c2c6d25f
JM
1156 /* Normally, by the time we reach `resume', the breakpoints are either
1157 removed or inserted, as appropriate. The exception is if we're sitting
1158 at a permanent breakpoint; we need to step over it, but permanent
1159 breakpoints can't be removed. So we have to test for it here. */
237fc4c9 1160 if (breakpoint_here_p (pc) == permanent_breakpoint_here)
6d350bb5 1161 {
515630c5
UW
1162 if (gdbarch_skip_permanent_breakpoint_p (gdbarch))
1163 gdbarch_skip_permanent_breakpoint (gdbarch, regcache);
6d350bb5
UW
1164 else
1165 error (_("\
1166The program is stopped at a permanent breakpoint, but GDB does not know\n\
1167how to step past a permanent breakpoint on this architecture. Try using\n\
1168a command like `return' or `jump' to continue execution."));
1169 }
c2c6d25f 1170
237fc4c9
PA
1171 /* If enabled, step over breakpoints by executing a copy of the
1172 instruction at a different address.
1173
1174 We can't use displaced stepping when we have a signal to deliver;
1175 the comments for displaced_step_prepare explain why. The
1176 comments in the handle_inferior event for dealing with 'random
1177 signals' explain what we do instead. */
515630c5 1178 if (use_displaced_stepping (gdbarch)
4e1c45ea 1179 && tp->trap_expected
237fc4c9
PA
1180 && sig == TARGET_SIGNAL_0)
1181 {
1182 if (!displaced_step_prepare (inferior_ptid))
d56b7306
VP
1183 {
1184 /* Got placed in displaced stepping queue. Will be resumed
1185 later when all the currently queued displaced stepping
7f7efbd9
VP
1186 requests finish. The thread is not executing at this point,
1187 and the call to set_executing will be made later. But we
1188 need to call set_running here, since from frontend point of view,
1189 the thread is running. */
1190 set_running (inferior_ptid, 1);
d56b7306
VP
1191 discard_cleanups (old_cleanups);
1192 return;
1193 }
237fc4c9
PA
1194 }
1195
2facfe5c
DD
1196 /* Do we need to do it the hard way, w/temp breakpoints? */
1197 if (step)
1198 step = maybe_software_singlestep (gdbarch, pc);
c906108c 1199
c906108c
SS
1200 if (should_resume)
1201 {
39f77062 1202 ptid_t resume_ptid;
dfcd3bfb 1203
488f131b 1204 resume_ptid = RESUME_ALL; /* Default */
ef5cf84e 1205
cd76b0b7
VP
1206 /* If STEP is set, it's a request to use hardware stepping
1207 facilities. But in that case, we should never
1208 use singlestep breakpoint. */
1209 gdb_assert (!(singlestep_breakpoints_inserted_p && step));
1210
1211 if (singlestep_breakpoints_inserted_p
1212 && stepping_past_singlestep_breakpoint)
c906108c 1213 {
cd76b0b7
VP
1214 /* The situation here is as follows. In thread T1 we wanted to
1215 single-step. Lacking hardware single-stepping we've
1216 set breakpoint at the PC of the next instruction -- call it
1217 P. After resuming, we've hit that breakpoint in thread T2.
1218 Now we've removed original breakpoint, inserted breakpoint
1219 at P+1, and try to step to advance T2 past breakpoint.
1220 We need to step only T2, as if T1 is allowed to freely run,
1221 it can run past P, and if other threads are allowed to run,
1222 they can hit breakpoint at P+1, and nested hits of single-step
1223 breakpoints is not something we'd want -- that's complicated
1224 to support, and has no value. */
1225 resume_ptid = inferior_ptid;
1226 }
c906108c 1227
e842223a 1228 if ((step || singlestep_breakpoints_inserted_p)
4e1c45ea 1229 && tp->trap_expected)
cd76b0b7 1230 {
74960c60
VP
1231 /* We're allowing a thread to run past a breakpoint it has
1232 hit, by single-stepping the thread with the breakpoint
1233 removed. In which case, we need to single-step only this
1234 thread, and keep others stopped, as they can miss this
1235 breakpoint if allowed to run.
1236
1237 The current code actually removes all breakpoints when
1238 doing this, not just the one being stepped over, so if we
1239 let other threads run, we can actually miss any
1240 breakpoint, not just the one at PC. */
ef5cf84e 1241 resume_ptid = inferior_ptid;
c906108c 1242 }
ef5cf84e 1243
94cc34af
PA
1244 if (non_stop)
1245 {
1246 /* With non-stop mode on, threads are always handled
1247 individually. */
1248 resume_ptid = inferior_ptid;
1249 }
1250 else if ((scheduler_mode == schedlock_on)
1251 || (scheduler_mode == schedlock_step
1252 && (step || singlestep_breakpoints_inserted_p)))
c906108c 1253 {
ef5cf84e 1254 /* User-settable 'scheduler' mode requires solo thread resume. */
488f131b 1255 resume_ptid = inferior_ptid;
c906108c 1256 }
ef5cf84e 1257
515630c5 1258 if (gdbarch_cannot_step_breakpoint (gdbarch))
c4ed33b9
AC
1259 {
1260 /* Most targets can step a breakpoint instruction, thus
1261 executing it normally. But if this one cannot, just
1262 continue and we will hit it anyway. */
237fc4c9 1263 if (step && breakpoint_inserted_here_p (pc))
c4ed33b9
AC
1264 step = 0;
1265 }
237fc4c9
PA
1266
1267 if (debug_displaced
515630c5 1268 && use_displaced_stepping (gdbarch)
4e1c45ea 1269 && tp->trap_expected)
237fc4c9 1270 {
515630c5
UW
1271 struct regcache *resume_regcache = get_thread_regcache (resume_ptid);
1272 CORE_ADDR actual_pc = regcache_read_pc (resume_regcache);
237fc4c9
PA
1273 gdb_byte buf[4];
1274
1275 fprintf_unfiltered (gdb_stdlog, "displaced: run 0x%s: ",
1276 paddr_nz (actual_pc));
1277 read_memory (actual_pc, buf, sizeof (buf));
1278 displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
1279 }
1280
e58b0e63
PA
1281 /* Install inferior's terminal modes. */
1282 target_terminal_inferior ();
1283
2020b7ab
PA
1284 /* Avoid confusing the next resume, if the next stop/resume
1285 happens to apply to another thread. */
1286 tp->stop_signal = TARGET_SIGNAL_0;
607cecd2
PA
1287
1288 target_resume (resume_ptid, step, sig);
c906108c
SS
1289 }
1290
1291 discard_cleanups (old_cleanups);
1292}
1293\f
237fc4c9 1294/* Proceeding. */
c906108c
SS
1295
1296/* Clear out all variables saying what to do when inferior is continued.
1297 First do this, then set the ones you want, then call `proceed'. */
1298
a7212384
UW
1299static void
1300clear_proceed_status_thread (struct thread_info *tp)
c906108c 1301{
a7212384
UW
1302 if (debug_infrun)
1303 fprintf_unfiltered (gdb_stdlog,
1304 "infrun: clear_proceed_status_thread (%s)\n",
1305 target_pid_to_str (tp->ptid));
d6b48e9c 1306
a7212384
UW
1307 tp->trap_expected = 0;
1308 tp->step_range_start = 0;
1309 tp->step_range_end = 0;
1310 tp->step_frame_id = null_frame_id;
1311 tp->step_over_calls = STEP_OVER_UNDEBUGGABLE;
1312 tp->stop_requested = 0;
4e1c45ea 1313
a7212384 1314 tp->stop_step = 0;
32400beb 1315
a7212384 1316 tp->proceed_to_finish = 0;
414c69f7 1317
a7212384
UW
1318 /* Discard any remaining commands or status from previous stop. */
1319 bpstat_clear (&tp->stop_bpstat);
1320}
32400beb 1321
a7212384
UW
1322static int
1323clear_proceed_status_callback (struct thread_info *tp, void *data)
1324{
1325 if (is_exited (tp->ptid))
1326 return 0;
d6b48e9c 1327
a7212384
UW
1328 clear_proceed_status_thread (tp);
1329 return 0;
1330}
1331
1332void
1333clear_proceed_status (void)
1334{
1335 if (!ptid_equal (inferior_ptid, null_ptid))
1336 {
1337 struct inferior *inferior;
1338
1339 if (non_stop)
1340 {
1341 /* If in non-stop mode, only delete the per-thread status
1342 of the current thread. */
1343 clear_proceed_status_thread (inferior_thread ());
1344 }
1345 else
1346 {
1347 /* In all-stop mode, delete the per-thread status of
1348 *all* threads. */
1349 iterate_over_threads (clear_proceed_status_callback, NULL);
1350 }
1351
d6b48e9c
PA
1352 inferior = current_inferior ();
1353 inferior->stop_soon = NO_STOP_QUIETLY;
4e1c45ea
PA
1354 }
1355
c906108c 1356 stop_after_trap = 0;
f3b1572e
PA
1357
1358 observer_notify_about_to_proceed ();
c906108c 1359
d5c31457
UW
1360 if (stop_registers)
1361 {
1362 regcache_xfree (stop_registers);
1363 stop_registers = NULL;
1364 }
c906108c
SS
1365}
1366
5a437975
DE
1367/* Check the current thread against the thread that reported the most recent
1368 event. If a step-over is required return TRUE and set the current thread
1369 to the old thread. Otherwise return FALSE.
1370
1371 This should be suitable for any targets that support threads. */
ea67f13b
DJ
1372
1373static int
6a6b96b9 1374prepare_to_proceed (int step)
ea67f13b
DJ
1375{
1376 ptid_t wait_ptid;
1377 struct target_waitstatus wait_status;
5a437975
DE
1378 int schedlock_enabled;
1379
1380 /* With non-stop mode on, threads are always handled individually. */
1381 gdb_assert (! non_stop);
ea67f13b
DJ
1382
1383 /* Get the last target status returned by target_wait(). */
1384 get_last_target_status (&wait_ptid, &wait_status);
1385
6a6b96b9 1386 /* Make sure we were stopped at a breakpoint. */
ea67f13b 1387 if (wait_status.kind != TARGET_WAITKIND_STOPPED
6a6b96b9 1388 || wait_status.value.sig != TARGET_SIGNAL_TRAP)
ea67f13b
DJ
1389 {
1390 return 0;
1391 }
1392
5a437975
DE
1393 schedlock_enabled = (scheduler_mode == schedlock_on
1394 || (scheduler_mode == schedlock_step
1395 && step));
1396
6a6b96b9 1397 /* Switched over from WAIT_PID. */
ea67f13b 1398 if (!ptid_equal (wait_ptid, minus_one_ptid)
5a437975
DE
1399 && !ptid_equal (inferior_ptid, wait_ptid)
1400 /* Don't single step WAIT_PID if scheduler locking is on. */
1401 && !schedlock_enabled)
ea67f13b 1402 {
515630c5
UW
1403 struct regcache *regcache = get_thread_regcache (wait_ptid);
1404
1405 if (breakpoint_here_p (regcache_read_pc (regcache)))
ea67f13b 1406 {
515630c5
UW
1407 /* If stepping, remember current thread to switch back to. */
1408 if (step)
1409 deferred_step_ptid = inferior_ptid;
ea67f13b 1410
515630c5
UW
1411 /* Switch back to WAIT_PID thread. */
1412 switch_to_thread (wait_ptid);
6a6b96b9 1413
515630c5
UW
1414 /* We return 1 to indicate that there is a breakpoint here,
1415 so we need to step over it before continuing to avoid
1416 hitting it straight away. */
1417 return 1;
1418 }
ea67f13b
DJ
1419 }
1420
1421 return 0;
ea67f13b 1422}
e4846b08 1423
c906108c
SS
1424/* Basic routine for continuing the program in various fashions.
1425
1426 ADDR is the address to resume at, or -1 for resume where stopped.
1427 SIGGNAL is the signal to give it, or 0 for none,
c5aa993b 1428 or -1 for act according to how it stopped.
c906108c 1429 STEP is nonzero if should trap after one instruction.
c5aa993b
JM
1430 -1 means return after that and print nothing.
1431 You should probably set various step_... variables
1432 before calling here, if you are stepping.
c906108c
SS
1433
1434 You should call clear_proceed_status before calling proceed. */
1435
1436void
96baa820 1437proceed (CORE_ADDR addr, enum target_signal siggnal, int step)
c906108c 1438{
e58b0e63
PA
1439 struct regcache *regcache;
1440 struct gdbarch *gdbarch;
4e1c45ea 1441 struct thread_info *tp;
e58b0e63 1442 CORE_ADDR pc;
c906108c
SS
1443 int oneproc = 0;
1444
e58b0e63
PA
1445 /* If we're stopped at a fork/vfork, follow the branch set by the
1446 "set follow-fork-mode" command; otherwise, we'll just proceed
1447 resuming the current thread. */
1448 if (!follow_fork ())
1449 {
1450 /* The target for some reason decided not to resume. */
1451 normal_stop ();
1452 return;
1453 }
1454
1455 regcache = get_current_regcache ();
1456 gdbarch = get_regcache_arch (regcache);
1457 pc = regcache_read_pc (regcache);
1458
c906108c 1459 if (step > 0)
515630c5 1460 step_start_function = find_pc_function (pc);
c906108c
SS
1461 if (step < 0)
1462 stop_after_trap = 1;
1463
2acceee2 1464 if (addr == (CORE_ADDR) -1)
c906108c 1465 {
b2175913
MS
1466 if (pc == stop_pc && breakpoint_here_p (pc)
1467 && execution_direction != EXEC_REVERSE)
3352ef37
AC
1468 /* There is a breakpoint at the address we will resume at,
1469 step one instruction before inserting breakpoints so that
1470 we do not stop right away (and report a second hit at this
b2175913
MS
1471 breakpoint).
1472
1473 Note, we don't do this in reverse, because we won't
1474 actually be executing the breakpoint insn anyway.
1475 We'll be (un-)executing the previous instruction. */
1476
c906108c 1477 oneproc = 1;
515630c5
UW
1478 else if (gdbarch_single_step_through_delay_p (gdbarch)
1479 && gdbarch_single_step_through_delay (gdbarch,
1480 get_current_frame ()))
3352ef37
AC
1481 /* We stepped onto an instruction that needs to be stepped
1482 again before re-inserting the breakpoint, do so. */
c906108c
SS
1483 oneproc = 1;
1484 }
1485 else
1486 {
515630c5 1487 regcache_write_pc (regcache, addr);
c906108c
SS
1488 }
1489
527159b7 1490 if (debug_infrun)
8a9de0e4
AC
1491 fprintf_unfiltered (gdb_stdlog,
1492 "infrun: proceed (addr=0x%s, signal=%d, step=%d)\n",
1493 paddr_nz (addr), siggnal, step);
527159b7 1494
94cc34af
PA
1495 if (non_stop)
1496 /* In non-stop, each thread is handled individually. The context
1497 must already be set to the right thread here. */
1498 ;
1499 else
1500 {
1501 /* In a multi-threaded task we may select another thread and
1502 then continue or step.
c906108c 1503
94cc34af
PA
1504 But if the old thread was stopped at a breakpoint, it will
1505 immediately cause another breakpoint stop without any
1506 execution (i.e. it will report a breakpoint hit incorrectly).
1507 So we must step over it first.
c906108c 1508
94cc34af
PA
1509 prepare_to_proceed checks the current thread against the
1510 thread that reported the most recent event. If a step-over
1511 is required it returns TRUE and sets the current thread to
1512 the old thread. */
1513 if (prepare_to_proceed (step))
1514 oneproc = 1;
1515 }
c906108c 1516
4e1c45ea
PA
1517 /* prepare_to_proceed may change the current thread. */
1518 tp = inferior_thread ();
1519
c906108c 1520 if (oneproc)
74960c60 1521 {
4e1c45ea 1522 tp->trap_expected = 1;
237fc4c9
PA
1523 /* If displaced stepping is enabled, we can step over the
1524 breakpoint without hitting it, so leave all breakpoints
1525 inserted. Otherwise we need to disable all breakpoints, step
1526 one instruction, and then re-add them when that step is
1527 finished. */
515630c5 1528 if (!use_displaced_stepping (gdbarch))
237fc4c9 1529 remove_breakpoints ();
74960c60 1530 }
237fc4c9
PA
1531
1532 /* We can insert breakpoints if we're not trying to step over one,
1533 or if we are stepping over one but we're using displaced stepping
1534 to do so. */
4e1c45ea 1535 if (! tp->trap_expected || use_displaced_stepping (gdbarch))
c36b740a 1536 insert_breakpoints ();
c906108c 1537
2020b7ab
PA
1538 if (!non_stop)
1539 {
1540 /* Pass the last stop signal to the thread we're resuming,
1541 irrespective of whether the current thread is the thread that
1542 got the last event or not. This was historically GDB's
1543 behaviour before keeping a stop_signal per thread. */
1544
1545 struct thread_info *last_thread;
1546 ptid_t last_ptid;
1547 struct target_waitstatus last_status;
1548
1549 get_last_target_status (&last_ptid, &last_status);
1550 if (!ptid_equal (inferior_ptid, last_ptid)
1551 && !ptid_equal (last_ptid, null_ptid)
1552 && !ptid_equal (last_ptid, minus_one_ptid))
1553 {
e09875d4 1554 last_thread = find_thread_ptid (last_ptid);
2020b7ab
PA
1555 if (last_thread)
1556 {
1557 tp->stop_signal = last_thread->stop_signal;
1558 last_thread->stop_signal = TARGET_SIGNAL_0;
1559 }
1560 }
1561 }
1562
c906108c 1563 if (siggnal != TARGET_SIGNAL_DEFAULT)
2020b7ab 1564 tp->stop_signal = siggnal;
c906108c
SS
1565 /* If this signal should not be seen by program,
1566 give it zero. Used for debugging signals. */
2020b7ab
PA
1567 else if (!signal_program[tp->stop_signal])
1568 tp->stop_signal = TARGET_SIGNAL_0;
c906108c
SS
1569
1570 annotate_starting ();
1571
1572 /* Make sure that output from GDB appears before output from the
1573 inferior. */
1574 gdb_flush (gdb_stdout);
1575
e4846b08
JJ
1576 /* Refresh prev_pc value just prior to resuming. This used to be
1577 done in stop_stepping, however, setting prev_pc there did not handle
1578 scenarios such as inferior function calls or returning from
1579 a function via the return command. In those cases, the prev_pc
1580 value was not set properly for subsequent commands. The prev_pc value
1581 is used to initialize the starting line number in the ecs. With an
1582 invalid value, the gdb next command ends up stopping at the position
1583 represented by the next line table entry past our start position.
1584 On platforms that generate one line table entry per line, this
1585 is not a problem. However, on the ia64, the compiler generates
1586 extraneous line table entries that do not increase the line number.
1587 When we issue the gdb next command on the ia64 after an inferior call
1588 or a return command, we often end up a few instructions forward, still
1589 within the original line we started.
1590
1591 An attempt was made to have init_execution_control_state () refresh
1592 the prev_pc value before calculating the line number. This approach
1593 did not work because on platforms that use ptrace, the pc register
1594 cannot be read unless the inferior is stopped. At that point, we
515630c5 1595 are not guaranteed the inferior is stopped and so the regcache_read_pc ()
e4846b08 1596 call can fail. Setting the prev_pc value here ensures the value is
8fb3e588 1597 updated correctly when the inferior is stopped. */
4e1c45ea 1598 tp->prev_pc = regcache_read_pc (get_current_regcache ());
e4846b08 1599
59f0d5d9 1600 /* Fill in with reasonable starting values. */
4e1c45ea 1601 init_thread_stepping_state (tp);
59f0d5d9 1602
59f0d5d9
PA
1603 /* Reset to normal state. */
1604 init_infwait_state ();
1605
c906108c 1606 /* Resume inferior. */
2020b7ab 1607 resume (oneproc || step || bpstat_should_step (), tp->stop_signal);
c906108c
SS
1608
1609 /* Wait for it to stop (if not standalone)
1610 and in any case decode why it stopped, and act accordingly. */
43ff13b4
JM
1611 /* Do this only if we are not using the event loop, or if the target
1612 does not support asynchronous execution. */
362646f5 1613 if (!target_can_async_p ())
43ff13b4 1614 {
ae123ec6 1615 wait_for_inferior (0);
43ff13b4
JM
1616 normal_stop ();
1617 }
c906108c 1618}
c906108c
SS
1619\f
1620
1621/* Start remote-debugging of a machine over a serial link. */
96baa820 1622
c906108c 1623void
8621d6a9 1624start_remote (int from_tty)
c906108c 1625{
d6b48e9c 1626 struct inferior *inferior;
c906108c 1627 init_wait_for_inferior ();
d6b48e9c
PA
1628
1629 inferior = current_inferior ();
1630 inferior->stop_soon = STOP_QUIETLY_REMOTE;
43ff13b4 1631
6426a772
JM
1632 /* Always go on waiting for the target, regardless of the mode. */
1633 /* FIXME: cagney/1999-09-23: At present it isn't possible to
7e73cedf 1634 indicate to wait_for_inferior that a target should timeout if
6426a772
JM
1635 nothing is returned (instead of just blocking). Because of this,
1636 targets expecting an immediate response need to, internally, set
1637 things up so that the target_wait() is forced to eventually
1638 timeout. */
1639 /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
1640 differentiate to its caller what the state of the target is after
1641 the initial open has been performed. Here we're assuming that
1642 the target has stopped. It should be possible to eventually have
1643 target_open() return to the caller an indication that the target
1644 is currently running and GDB state should be set to the same as
1645 for an async run. */
ae123ec6 1646 wait_for_inferior (0);
8621d6a9
DJ
1647
1648 /* Now that the inferior has stopped, do any bookkeeping like
1649 loading shared libraries. We want to do this before normal_stop,
1650 so that the displayed frame is up to date. */
1651 post_create_inferior (&current_target, from_tty);
1652
6426a772 1653 normal_stop ();
c906108c
SS
1654}
1655
1656/* Initialize static vars when a new inferior begins. */
1657
1658void
96baa820 1659init_wait_for_inferior (void)
c906108c
SS
1660{
1661 /* These are meaningless until the first time through wait_for_inferior. */
c906108c 1662
c906108c
SS
1663 breakpoint_init_inferior (inf_starting);
1664
c906108c 1665 clear_proceed_status ();
9f976b41
DJ
1666
1667 stepping_past_singlestep_breakpoint = 0;
ca67fcb8 1668 deferred_step_ptid = null_ptid;
ca005067
DJ
1669
1670 target_last_wait_ptid = minus_one_ptid;
237fc4c9 1671
0d1e5fa7
PA
1672 previous_inferior_ptid = null_ptid;
1673 init_infwait_state ();
1674
237fc4c9 1675 displaced_step_clear ();
c906108c 1676}
237fc4c9 1677
c906108c 1678\f
b83266a0
SS
1679/* This enum encodes possible reasons for doing a target_wait, so that
1680 wfi can call target_wait in one place. (Ultimately the call will be
1681 moved out of the infinite loop entirely.) */
1682
c5aa993b
JM
1683enum infwait_states
1684{
cd0fc7c3
SS
1685 infwait_normal_state,
1686 infwait_thread_hop_state,
d983da9c 1687 infwait_step_watch_state,
cd0fc7c3 1688 infwait_nonstep_watch_state
b83266a0
SS
1689};
1690
11cf8741
JM
1691/* Why did the inferior stop? Used to print the appropriate messages
1692 to the interface from within handle_inferior_event(). */
1693enum inferior_stop_reason
1694{
11cf8741
JM
1695 /* Step, next, nexti, stepi finished. */
1696 END_STEPPING_RANGE,
11cf8741
JM
1697 /* Inferior terminated by signal. */
1698 SIGNAL_EXITED,
1699 /* Inferior exited. */
1700 EXITED,
1701 /* Inferior received signal, and user asked to be notified. */
b2175913
MS
1702 SIGNAL_RECEIVED,
1703 /* Reverse execution -- target ran out of history info. */
1704 NO_HISTORY
11cf8741
JM
1705};
1706
0d1e5fa7
PA
1707/* The PTID we'll do a target_wait on.*/
1708ptid_t waiton_ptid;
1709
1710/* Current inferior wait state. */
1711enum infwait_states infwait_state;
cd0fc7c3 1712
0d1e5fa7
PA
1713/* Data to be passed around while handling an event. This data is
1714 discarded between events. */
c5aa993b 1715struct execution_control_state
488f131b 1716{
0d1e5fa7 1717 ptid_t ptid;
4e1c45ea
PA
1718 /* The thread that got the event, if this was a thread event; NULL
1719 otherwise. */
1720 struct thread_info *event_thread;
1721
488f131b 1722 struct target_waitstatus ws;
488f131b
JB
1723 int random_signal;
1724 CORE_ADDR stop_func_start;
1725 CORE_ADDR stop_func_end;
1726 char *stop_func_name;
488f131b 1727 int new_thread_event;
488f131b
JB
1728 int wait_some_more;
1729};
1730
1731void init_execution_control_state (struct execution_control_state *ecs);
1732
1733void handle_inferior_event (struct execution_control_state *ecs);
cd0fc7c3 1734
b2175913
MS
1735static void handle_step_into_function (struct execution_control_state *ecs);
1736static void handle_step_into_function_backward (struct execution_control_state *ecs);
44cbf7b5 1737static void insert_step_resume_breakpoint_at_frame (struct frame_info *step_frame);
14e60db5 1738static void insert_step_resume_breakpoint_at_caller (struct frame_info *);
44cbf7b5
AC
1739static void insert_step_resume_breakpoint_at_sal (struct symtab_and_line sr_sal,
1740 struct frame_id sr_id);
611c83ae
PA
1741static void insert_longjmp_resume_breakpoint (CORE_ADDR);
1742
104c1213
JM
1743static void stop_stepping (struct execution_control_state *ecs);
1744static void prepare_to_wait (struct execution_control_state *ecs);
d4f3574e 1745static void keep_going (struct execution_control_state *ecs);
488f131b
JB
1746static void print_stop_reason (enum inferior_stop_reason stop_reason,
1747 int stop_info);
104c1213 1748
252fbfc8
PA
1749/* Callback for iterate over threads. If the thread is stopped, but
1750 the user/frontend doesn't know about that yet, go through
1751 normal_stop, as if the thread had just stopped now. ARG points at
1752 a ptid. If PTID is MINUS_ONE_PTID, applies to all threads. If
1753 ptid_is_pid(PTID) is true, applies to all threads of the process
1754 pointed at by PTID. Otherwise, apply only to the thread pointed by
1755 PTID. */
1756
1757static int
1758infrun_thread_stop_requested_callback (struct thread_info *info, void *arg)
1759{
1760 ptid_t ptid = * (ptid_t *) arg;
1761
1762 if ((ptid_equal (info->ptid, ptid)
1763 || ptid_equal (minus_one_ptid, ptid)
1764 || (ptid_is_pid (ptid)
1765 && ptid_get_pid (ptid) == ptid_get_pid (info->ptid)))
1766 && is_running (info->ptid)
1767 && !is_executing (info->ptid))
1768 {
1769 struct cleanup *old_chain;
1770 struct execution_control_state ecss;
1771 struct execution_control_state *ecs = &ecss;
1772
1773 memset (ecs, 0, sizeof (*ecs));
1774
1775 old_chain = make_cleanup_restore_current_thread ();
1776
1777 switch_to_thread (info->ptid);
1778
1779 /* Go through handle_inferior_event/normal_stop, so we always
1780 have consistent output as if the stop event had been
1781 reported. */
1782 ecs->ptid = info->ptid;
e09875d4 1783 ecs->event_thread = find_thread_ptid (info->ptid);
252fbfc8
PA
1784 ecs->ws.kind = TARGET_WAITKIND_STOPPED;
1785 ecs->ws.value.sig = TARGET_SIGNAL_0;
1786
1787 handle_inferior_event (ecs);
1788
1789 if (!ecs->wait_some_more)
1790 {
1791 struct thread_info *tp;
1792
1793 normal_stop ();
1794
1795 /* Finish off the continuations. The continations
1796 themselves are responsible for realising the thread
1797 didn't finish what it was supposed to do. */
1798 tp = inferior_thread ();
1799 do_all_intermediate_continuations_thread (tp);
1800 do_all_continuations_thread (tp);
1801 }
1802
1803 do_cleanups (old_chain);
1804 }
1805
1806 return 0;
1807}
1808
1809/* This function is attached as a "thread_stop_requested" observer.
1810 Cleanup local state that assumed the PTID was to be resumed, and
1811 report the stop to the frontend. */
1812
2c0b251b 1813static void
252fbfc8
PA
1814infrun_thread_stop_requested (ptid_t ptid)
1815{
1816 struct displaced_step_request *it, *next, *prev = NULL;
1817
1818 /* PTID was requested to stop. Remove it from the displaced
1819 stepping queue, so we don't try to resume it automatically. */
1820 for (it = displaced_step_request_queue; it; it = next)
1821 {
1822 next = it->next;
1823
1824 if (ptid_equal (it->ptid, ptid)
1825 || ptid_equal (minus_one_ptid, ptid)
1826 || (ptid_is_pid (ptid)
1827 && ptid_get_pid (ptid) == ptid_get_pid (it->ptid)))
1828 {
1829 if (displaced_step_request_queue == it)
1830 displaced_step_request_queue = it->next;
1831 else
1832 prev->next = it->next;
1833
1834 xfree (it);
1835 }
1836 else
1837 prev = it;
1838 }
1839
1840 iterate_over_threads (infrun_thread_stop_requested_callback, &ptid);
1841}
1842
a07daef3
PA
1843static void
1844infrun_thread_thread_exit (struct thread_info *tp, int silent)
1845{
1846 if (ptid_equal (target_last_wait_ptid, tp->ptid))
1847 nullify_last_target_wait_ptid ();
1848}
1849
4e1c45ea
PA
1850/* Callback for iterate_over_threads. */
1851
1852static int
1853delete_step_resume_breakpoint_callback (struct thread_info *info, void *data)
1854{
1855 if (is_exited (info->ptid))
1856 return 0;
1857
1858 delete_step_resume_breakpoint (info);
1859 return 0;
1860}
1861
1862/* In all-stop, delete the step resume breakpoint of any thread that
1863 had one. In non-stop, delete the step resume breakpoint of the
1864 thread that just stopped. */
1865
1866static void
1867delete_step_thread_step_resume_breakpoint (void)
1868{
1869 if (!target_has_execution
1870 || ptid_equal (inferior_ptid, null_ptid))
1871 /* If the inferior has exited, we have already deleted the step
1872 resume breakpoints out of GDB's lists. */
1873 return;
1874
1875 if (non_stop)
1876 {
1877 /* If in non-stop mode, only delete the step-resume or
1878 longjmp-resume breakpoint of the thread that just stopped
1879 stepping. */
1880 struct thread_info *tp = inferior_thread ();
1881 delete_step_resume_breakpoint (tp);
1882 }
1883 else
1884 /* In all-stop mode, delete all step-resume and longjmp-resume
1885 breakpoints of any thread that had them. */
1886 iterate_over_threads (delete_step_resume_breakpoint_callback, NULL);
1887}
1888
1889/* A cleanup wrapper. */
1890
1891static void
1892delete_step_thread_step_resume_breakpoint_cleanup (void *arg)
1893{
1894 delete_step_thread_step_resume_breakpoint ();
1895}
1896
223698f8
DE
1897/* Pretty print the results of target_wait, for debugging purposes. */
1898
1899static void
1900print_target_wait_results (ptid_t waiton_ptid, ptid_t result_ptid,
1901 const struct target_waitstatus *ws)
1902{
1903 char *status_string = target_waitstatus_to_string (ws);
1904 struct ui_file *tmp_stream = mem_fileopen ();
1905 char *text;
1906 long len;
1907
1908 /* The text is split over several lines because it was getting too long.
1909 Call fprintf_unfiltered (gdb_stdlog) once so that the text is still
1910 output as a unit; we want only one timestamp printed if debug_timestamp
1911 is set. */
1912
1913 fprintf_unfiltered (tmp_stream,
1914 "infrun: target_wait (%d", PIDGET (waiton_ptid));
1915 if (PIDGET (waiton_ptid) != -1)
1916 fprintf_unfiltered (tmp_stream,
1917 " [%s]", target_pid_to_str (waiton_ptid));
1918 fprintf_unfiltered (tmp_stream, ", status) =\n");
1919 fprintf_unfiltered (tmp_stream,
1920 "infrun: %d [%s],\n",
1921 PIDGET (result_ptid), target_pid_to_str (result_ptid));
1922 fprintf_unfiltered (tmp_stream,
1923 "infrun: %s\n",
1924 status_string);
1925
1926 text = ui_file_xstrdup (tmp_stream, &len);
1927
1928 /* This uses %s in part to handle %'s in the text, but also to avoid
1929 a gcc error: the format attribute requires a string literal. */
1930 fprintf_unfiltered (gdb_stdlog, "%s", text);
1931
1932 xfree (status_string);
1933 xfree (text);
1934 ui_file_delete (tmp_stream);
1935}
1936
cd0fc7c3 1937/* Wait for control to return from inferior to debugger.
ae123ec6
JB
1938
1939 If TREAT_EXEC_AS_SIGTRAP is non-zero, then handle EXEC signals
1940 as if they were SIGTRAP signals. This can be useful during
1941 the startup sequence on some targets such as HP/UX, where
1942 we receive an EXEC event instead of the expected SIGTRAP.
1943
cd0fc7c3
SS
1944 If inferior gets a signal, we may decide to start it up again
1945 instead of returning. That is why there is a loop in this function.
1946 When this function actually returns it means the inferior
1947 should be left stopped and GDB should read more commands. */
1948
1949void
ae123ec6 1950wait_for_inferior (int treat_exec_as_sigtrap)
cd0fc7c3
SS
1951{
1952 struct cleanup *old_cleanups;
0d1e5fa7 1953 struct execution_control_state ecss;
cd0fc7c3 1954 struct execution_control_state *ecs;
c906108c 1955
527159b7 1956 if (debug_infrun)
ae123ec6
JB
1957 fprintf_unfiltered
1958 (gdb_stdlog, "infrun: wait_for_inferior (treat_exec_as_sigtrap=%d)\n",
1959 treat_exec_as_sigtrap);
527159b7 1960
4e1c45ea
PA
1961 old_cleanups =
1962 make_cleanup (delete_step_thread_step_resume_breakpoint_cleanup, NULL);
cd0fc7c3 1963
cd0fc7c3 1964 ecs = &ecss;
0d1e5fa7
PA
1965 memset (ecs, 0, sizeof (*ecs));
1966
cd0fc7c3
SS
1967 overlay_cache_invalid = 1;
1968
e0bb1c1c
PA
1969 /* We'll update this if & when we switch to a new thread. */
1970 previous_inferior_ptid = inferior_ptid;
1971
cd0fc7c3
SS
1972 /* We have to invalidate the registers BEFORE calling target_wait
1973 because they can be loaded from the target while in target_wait.
1974 This makes remote debugging a bit more efficient for those
1975 targets that provide critical registers as part of their normal
1976 status mechanism. */
1977
1978 registers_changed ();
b83266a0 1979
c906108c
SS
1980 while (1)
1981 {
29f49a6a
PA
1982 struct cleanup *old_chain;
1983
9a4105ab 1984 if (deprecated_target_wait_hook)
47608cb1 1985 ecs->ptid = deprecated_target_wait_hook (waiton_ptid, &ecs->ws, 0);
cd0fc7c3 1986 else
47608cb1 1987 ecs->ptid = target_wait (waiton_ptid, &ecs->ws, 0);
c906108c 1988
f00150c9 1989 if (debug_infrun)
223698f8 1990 print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
f00150c9 1991
ae123ec6
JB
1992 if (treat_exec_as_sigtrap && ecs->ws.kind == TARGET_WAITKIND_EXECD)
1993 {
1994 xfree (ecs->ws.value.execd_pathname);
1995 ecs->ws.kind = TARGET_WAITKIND_STOPPED;
1996 ecs->ws.value.sig = TARGET_SIGNAL_TRAP;
1997 }
1998
29f49a6a
PA
1999 /* If an error happens while handling the event, propagate GDB's
2000 knowledge of the executing state to the frontend/user running
2001 state. */
2002 old_chain = make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
2003
cd0fc7c3
SS
2004 /* Now figure out what to do with the result of the result. */
2005 handle_inferior_event (ecs);
c906108c 2006
29f49a6a
PA
2007 /* No error, don't finish the state yet. */
2008 discard_cleanups (old_chain);
2009
cd0fc7c3
SS
2010 if (!ecs->wait_some_more)
2011 break;
2012 }
4e1c45ea 2013
cd0fc7c3
SS
2014 do_cleanups (old_cleanups);
2015}
c906108c 2016
43ff13b4
JM
2017/* Asynchronous version of wait_for_inferior. It is called by the
2018 event loop whenever a change of state is detected on the file
2019 descriptor corresponding to the target. It can be called more than
2020 once to complete a single execution command. In such cases we need
a474d7c2
PA
2021 to keep the state in a global variable ECSS. If it is the last time
2022 that this function is called for a single execution command, then
2023 report to the user that the inferior has stopped, and do the
2024 necessary cleanups. */
43ff13b4
JM
2025
2026void
fba45db2 2027fetch_inferior_event (void *client_data)
43ff13b4 2028{
0d1e5fa7 2029 struct execution_control_state ecss;
a474d7c2 2030 struct execution_control_state *ecs = &ecss;
4f8d22e3 2031 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
29f49a6a 2032 struct cleanup *ts_old_chain;
4f8d22e3 2033 int was_sync = sync_execution;
43ff13b4 2034
0d1e5fa7
PA
2035 memset (ecs, 0, sizeof (*ecs));
2036
59f0d5d9 2037 overlay_cache_invalid = 1;
43ff13b4 2038
e0bb1c1c
PA
2039 /* We can only rely on wait_for_more being correct before handling
2040 the event in all-stop, but previous_inferior_ptid isn't used in
2041 non-stop. */
2042 if (!ecs->wait_some_more)
2043 /* We'll update this if & when we switch to a new thread. */
2044 previous_inferior_ptid = inferior_ptid;
2045
4f8d22e3
PA
2046 if (non_stop)
2047 /* In non-stop mode, the user/frontend should not notice a thread
2048 switch due to internal events. Make sure we reverse to the
2049 user selected thread and frame after handling the event and
2050 running any breakpoint commands. */
2051 make_cleanup_restore_current_thread ();
2052
59f0d5d9
PA
2053 /* We have to invalidate the registers BEFORE calling target_wait
2054 because they can be loaded from the target while in target_wait.
2055 This makes remote debugging a bit more efficient for those
2056 targets that provide critical registers as part of their normal
2057 status mechanism. */
43ff13b4 2058
59f0d5d9 2059 registers_changed ();
43ff13b4 2060
9a4105ab 2061 if (deprecated_target_wait_hook)
a474d7c2 2062 ecs->ptid =
47608cb1 2063 deprecated_target_wait_hook (waiton_ptid, &ecs->ws, TARGET_WNOHANG);
43ff13b4 2064 else
47608cb1 2065 ecs->ptid = target_wait (waiton_ptid, &ecs->ws, TARGET_WNOHANG);
43ff13b4 2066
f00150c9 2067 if (debug_infrun)
223698f8 2068 print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
f00150c9 2069
94cc34af
PA
2070 if (non_stop
2071 && ecs->ws.kind != TARGET_WAITKIND_IGNORE
2072 && ecs->ws.kind != TARGET_WAITKIND_EXITED
2073 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED)
2074 /* In non-stop mode, each thread is handled individually. Switch
2075 early, so the global state is set correctly for this
2076 thread. */
2077 context_switch (ecs->ptid);
2078
29f49a6a
PA
2079 /* If an error happens while handling the event, propagate GDB's
2080 knowledge of the executing state to the frontend/user running
2081 state. */
2082 if (!non_stop)
2083 ts_old_chain = make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
2084 else
2085 ts_old_chain = make_cleanup (finish_thread_state_cleanup, &ecs->ptid);
2086
43ff13b4 2087 /* Now figure out what to do with the result of the result. */
a474d7c2 2088 handle_inferior_event (ecs);
43ff13b4 2089
a474d7c2 2090 if (!ecs->wait_some_more)
43ff13b4 2091 {
d6b48e9c
PA
2092 struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
2093
4e1c45ea 2094 delete_step_thread_step_resume_breakpoint ();
f107f563 2095
d6b48e9c
PA
2096 /* We may not find an inferior if this was a process exit. */
2097 if (inf == NULL || inf->stop_soon == NO_STOP_QUIETLY)
83c265ab
PA
2098 normal_stop ();
2099
af679fd0
PA
2100 if (target_has_execution
2101 && ecs->ws.kind != TARGET_WAITKIND_EXITED
2102 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
2103 && ecs->event_thread->step_multi
414c69f7 2104 && ecs->event_thread->stop_step)
c2d11a7d
JM
2105 inferior_event_handler (INF_EXEC_CONTINUE, NULL);
2106 else
2107 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
43ff13b4 2108 }
4f8d22e3 2109
29f49a6a
PA
2110 /* No error, don't finish the thread states yet. */
2111 discard_cleanups (ts_old_chain);
2112
4f8d22e3
PA
2113 /* Revert thread and frame. */
2114 do_cleanups (old_chain);
2115
2116 /* If the inferior was in sync execution mode, and now isn't,
2117 restore the prompt. */
2118 if (was_sync && !sync_execution)
2119 display_gdb_prompt (0);
43ff13b4
JM
2120}
2121
cd0fc7c3
SS
2122/* Prepare an execution control state for looping through a
2123 wait_for_inferior-type loop. */
2124
2125void
96baa820 2126init_execution_control_state (struct execution_control_state *ecs)
cd0fc7c3
SS
2127{
2128 ecs->random_signal = 0;
0d1e5fa7
PA
2129}
2130
2131/* Clear context switchable stepping state. */
2132
2133void
4e1c45ea 2134init_thread_stepping_state (struct thread_info *tss)
0d1e5fa7 2135{
2afb61aa
PA
2136 struct symtab_and_line sal;
2137
0d1e5fa7
PA
2138 tss->stepping_over_breakpoint = 0;
2139 tss->step_after_step_resume_breakpoint = 0;
2140 tss->stepping_through_solib_after_catch = 0;
2141 tss->stepping_through_solib_catchpoints = NULL;
2afb61aa 2142
4e1c45ea 2143 sal = find_pc_line (tss->prev_pc, 0);
2afb61aa
PA
2144 tss->current_line = sal.line;
2145 tss->current_symtab = sal.symtab;
cd0fc7c3
SS
2146}
2147
e02bc4cc 2148/* Return the cached copy of the last pid/waitstatus returned by
9a4105ab
AC
2149 target_wait()/deprecated_target_wait_hook(). The data is actually
2150 cached by handle_inferior_event(), which gets called immediately
2151 after target_wait()/deprecated_target_wait_hook(). */
e02bc4cc
DS
2152
2153void
488f131b 2154get_last_target_status (ptid_t *ptidp, struct target_waitstatus *status)
e02bc4cc 2155{
39f77062 2156 *ptidp = target_last_wait_ptid;
e02bc4cc
DS
2157 *status = target_last_waitstatus;
2158}
2159
ac264b3b
MS
2160void
2161nullify_last_target_wait_ptid (void)
2162{
2163 target_last_wait_ptid = minus_one_ptid;
2164}
2165
dcf4fbde 2166/* Switch thread contexts. */
dd80620e
MS
2167
2168static void
0d1e5fa7 2169context_switch (ptid_t ptid)
dd80620e 2170{
fd48f117
DJ
2171 if (debug_infrun)
2172 {
2173 fprintf_unfiltered (gdb_stdlog, "infrun: Switching context from %s ",
2174 target_pid_to_str (inferior_ptid));
2175 fprintf_unfiltered (gdb_stdlog, "to %s\n",
0d1e5fa7 2176 target_pid_to_str (ptid));
fd48f117
DJ
2177 }
2178
0d1e5fa7 2179 switch_to_thread (ptid);
dd80620e
MS
2180}
2181
4fa8626c
DJ
2182static void
2183adjust_pc_after_break (struct execution_control_state *ecs)
2184{
24a73cce
UW
2185 struct regcache *regcache;
2186 struct gdbarch *gdbarch;
8aad930b 2187 CORE_ADDR breakpoint_pc;
4fa8626c 2188
4fa8626c
DJ
2189 /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP. If
2190 we aren't, just return.
9709f61c
DJ
2191
2192 We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
b798847d
UW
2193 affected by gdbarch_decr_pc_after_break. Other waitkinds which are
2194 implemented by software breakpoints should be handled through the normal
2195 breakpoint layer.
8fb3e588 2196
4fa8626c
DJ
2197 NOTE drow/2004-01-31: On some targets, breakpoints may generate
2198 different signals (SIGILL or SIGEMT for instance), but it is less
2199 clear where the PC is pointing afterwards. It may not match
b798847d
UW
2200 gdbarch_decr_pc_after_break. I don't know any specific target that
2201 generates these signals at breakpoints (the code has been in GDB since at
2202 least 1992) so I can not guess how to handle them here.
8fb3e588 2203
e6cf7916
UW
2204 In earlier versions of GDB, a target with
2205 gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
b798847d
UW
2206 watchpoint affected by gdbarch_decr_pc_after_break. I haven't found any
2207 target with both of these set in GDB history, and it seems unlikely to be
2208 correct, so gdbarch_have_nonsteppable_watchpoint is not checked here. */
4fa8626c
DJ
2209
2210 if (ecs->ws.kind != TARGET_WAITKIND_STOPPED)
2211 return;
2212
2213 if (ecs->ws.value.sig != TARGET_SIGNAL_TRAP)
2214 return;
2215
4058b839
PA
2216 /* In reverse execution, when a breakpoint is hit, the instruction
2217 under it has already been de-executed. The reported PC always
2218 points at the breakpoint address, so adjusting it further would
2219 be wrong. E.g., consider this case on a decr_pc_after_break == 1
2220 architecture:
2221
2222 B1 0x08000000 : INSN1
2223 B2 0x08000001 : INSN2
2224 0x08000002 : INSN3
2225 PC -> 0x08000003 : INSN4
2226
2227 Say you're stopped at 0x08000003 as above. Reverse continuing
2228 from that point should hit B2 as below. Reading the PC when the
2229 SIGTRAP is reported should read 0x08000001 and INSN2 should have
2230 been de-executed already.
2231
2232 B1 0x08000000 : INSN1
2233 B2 PC -> 0x08000001 : INSN2
2234 0x08000002 : INSN3
2235 0x08000003 : INSN4
2236
2237 We can't apply the same logic as for forward execution, because
2238 we would wrongly adjust the PC to 0x08000000, since there's a
2239 breakpoint at PC - 1. We'd then report a hit on B1, although
2240 INSN1 hadn't been de-executed yet. Doing nothing is the correct
2241 behaviour. */
2242 if (execution_direction == EXEC_REVERSE)
2243 return;
2244
24a73cce
UW
2245 /* If this target does not decrement the PC after breakpoints, then
2246 we have nothing to do. */
2247 regcache = get_thread_regcache (ecs->ptid);
2248 gdbarch = get_regcache_arch (regcache);
2249 if (gdbarch_decr_pc_after_break (gdbarch) == 0)
2250 return;
2251
8aad930b
AC
2252 /* Find the location where (if we've hit a breakpoint) the
2253 breakpoint would be. */
515630c5
UW
2254 breakpoint_pc = regcache_read_pc (regcache)
2255 - gdbarch_decr_pc_after_break (gdbarch);
8aad930b 2256
1c5cfe86
PA
2257 /* Check whether there actually is a software breakpoint inserted at
2258 that location.
2259
2260 If in non-stop mode, a race condition is possible where we've
2261 removed a breakpoint, but stop events for that breakpoint were
2262 already queued and arrive later. To suppress those spurious
2263 SIGTRAPs, we keep a list of such breakpoint locations for a bit,
2264 and retire them after a number of stop events are reported. */
2265 if (software_breakpoint_inserted_here_p (breakpoint_pc)
2266 || (non_stop && moribund_breakpoint_here_p (breakpoint_pc)))
8aad930b 2267 {
96429cc8
HZ
2268 struct cleanup *old_cleanups = NULL;
2269 if (RECORD_IS_USED)
2270 old_cleanups = record_gdb_operation_disable_set ();
2271
1c0fdd0e
UW
2272 /* When using hardware single-step, a SIGTRAP is reported for both
2273 a completed single-step and a software breakpoint. Need to
2274 differentiate between the two, as the latter needs adjusting
2275 but the former does not.
2276
2277 The SIGTRAP can be due to a completed hardware single-step only if
2278 - we didn't insert software single-step breakpoints
2279 - the thread to be examined is still the current thread
2280 - this thread is currently being stepped
2281
2282 If any of these events did not occur, we must have stopped due
2283 to hitting a software breakpoint, and have to back up to the
2284 breakpoint address.
2285
2286 As a special case, we could have hardware single-stepped a
2287 software breakpoint. In this case (prev_pc == breakpoint_pc),
2288 we also need to back up to the breakpoint address. */
2289
2290 if (singlestep_breakpoints_inserted_p
2291 || !ptid_equal (ecs->ptid, inferior_ptid)
4e1c45ea
PA
2292 || !currently_stepping (ecs->event_thread)
2293 || ecs->event_thread->prev_pc == breakpoint_pc)
515630c5 2294 regcache_write_pc (regcache, breakpoint_pc);
96429cc8
HZ
2295
2296 if (RECORD_IS_USED)
2297 do_cleanups (old_cleanups);
8aad930b 2298 }
4fa8626c
DJ
2299}
2300
0d1e5fa7
PA
2301void
2302init_infwait_state (void)
2303{
2304 waiton_ptid = pid_to_ptid (-1);
2305 infwait_state = infwait_normal_state;
2306}
2307
94cc34af
PA
2308void
2309error_is_running (void)
2310{
2311 error (_("\
2312Cannot execute this command while the selected thread is running."));
2313}
2314
2315void
2316ensure_not_running (void)
2317{
2318 if (is_running (inferior_ptid))
2319 error_is_running ();
2320}
2321
cd0fc7c3
SS
2322/* Given an execution control state that has been freshly filled in
2323 by an event from the inferior, figure out what it means and take
2324 appropriate action. */
c906108c 2325
cd0fc7c3 2326void
96baa820 2327handle_inferior_event (struct execution_control_state *ecs)
cd0fc7c3 2328{
c8edd8b4 2329 int sw_single_step_trap_p = 0;
d983da9c
DJ
2330 int stopped_by_watchpoint;
2331 int stepped_after_stopped_by_watchpoint = 0;
2afb61aa 2332 struct symtab_and_line stop_pc_sal;
d6b48e9c
PA
2333 enum stop_kind stop_soon;
2334
2335 if (ecs->ws.kind != TARGET_WAITKIND_EXITED
2336 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
2337 && ecs->ws.kind != TARGET_WAITKIND_IGNORE)
2338 {
2339 struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
2340 gdb_assert (inf);
2341 stop_soon = inf->stop_soon;
2342 }
2343 else
2344 stop_soon = NO_STOP_QUIETLY;
cd0fc7c3 2345
e02bc4cc 2346 /* Cache the last pid/waitstatus. */
39f77062 2347 target_last_wait_ptid = ecs->ptid;
0d1e5fa7 2348 target_last_waitstatus = ecs->ws;
e02bc4cc 2349
ca005067
DJ
2350 /* Always clear state belonging to the previous time we stopped. */
2351 stop_stack_dummy = 0;
2352
8c90c137
LM
2353 /* If it's a new process, add it to the thread database */
2354
2355 ecs->new_thread_event = (!ptid_equal (ecs->ptid, inferior_ptid)
2356 && !ptid_equal (ecs->ptid, minus_one_ptid)
2357 && !in_thread_list (ecs->ptid));
2358
2359 if (ecs->ws.kind != TARGET_WAITKIND_EXITED
2360 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED && ecs->new_thread_event)
2361 add_thread (ecs->ptid);
2362
e09875d4 2363 ecs->event_thread = find_thread_ptid (ecs->ptid);
88ed393a
JK
2364
2365 /* Dependent on valid ECS->EVENT_THREAD. */
2366 adjust_pc_after_break (ecs);
2367
2368 /* Dependent on the current PC value modified by adjust_pc_after_break. */
2369 reinit_frame_cache ();
2370
8c90c137
LM
2371 if (ecs->ws.kind != TARGET_WAITKIND_IGNORE)
2372 {
1c5cfe86
PA
2373 breakpoint_retire_moribund ();
2374
48844aa6
PA
2375 /* Mark the non-executing threads accordingly. In all-stop, all
2376 threads of all processes are stopped when we get any event
2377 reported. In non-stop mode, only the event thread stops. If
2378 we're handling a process exit in non-stop mode, there's
2379 nothing to do, as threads of the dead process are gone, and
2380 threads of any other process were left running. */
2381 if (!non_stop)
2382 set_executing (minus_one_ptid, 0);
2383 else if (ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
2384 && ecs->ws.kind != TARGET_WAITKIND_EXITED)
2385 set_executing (inferior_ptid, 0);
8c90c137
LM
2386 }
2387
0d1e5fa7 2388 switch (infwait_state)
488f131b
JB
2389 {
2390 case infwait_thread_hop_state:
527159b7 2391 if (debug_infrun)
8a9de0e4 2392 fprintf_unfiltered (gdb_stdlog, "infrun: infwait_thread_hop_state\n");
488f131b 2393 /* Cancel the waiton_ptid. */
0d1e5fa7 2394 waiton_ptid = pid_to_ptid (-1);
65e82032 2395 break;
b83266a0 2396
488f131b 2397 case infwait_normal_state:
527159b7 2398 if (debug_infrun)
8a9de0e4 2399 fprintf_unfiltered (gdb_stdlog, "infrun: infwait_normal_state\n");
d983da9c
DJ
2400 break;
2401
2402 case infwait_step_watch_state:
2403 if (debug_infrun)
2404 fprintf_unfiltered (gdb_stdlog,
2405 "infrun: infwait_step_watch_state\n");
2406
2407 stepped_after_stopped_by_watchpoint = 1;
488f131b 2408 break;
b83266a0 2409
488f131b 2410 case infwait_nonstep_watch_state:
527159b7 2411 if (debug_infrun)
8a9de0e4
AC
2412 fprintf_unfiltered (gdb_stdlog,
2413 "infrun: infwait_nonstep_watch_state\n");
488f131b 2414 insert_breakpoints ();
c906108c 2415
488f131b
JB
2416 /* FIXME-maybe: is this cleaner than setting a flag? Does it
2417 handle things like signals arriving and other things happening
2418 in combination correctly? */
2419 stepped_after_stopped_by_watchpoint = 1;
2420 break;
65e82032
AC
2421
2422 default:
e2e0b3e5 2423 internal_error (__FILE__, __LINE__, _("bad switch"));
488f131b 2424 }
0d1e5fa7 2425 infwait_state = infwait_normal_state;
c906108c 2426
488f131b
JB
2427 switch (ecs->ws.kind)
2428 {
2429 case TARGET_WAITKIND_LOADED:
527159b7 2430 if (debug_infrun)
8a9de0e4 2431 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_LOADED\n");
b0f4b84b
DJ
2432 /* Ignore gracefully during startup of the inferior, as it might
2433 be the shell which has just loaded some objects, otherwise
2434 add the symbols for the newly loaded objects. Also ignore at
2435 the beginning of an attach or remote session; we will query
2436 the full list of libraries once the connection is
2437 established. */
c0236d92 2438 if (stop_soon == NO_STOP_QUIETLY)
488f131b 2439 {
488f131b
JB
2440 /* Check for any newly added shared libraries if we're
2441 supposed to be adding them automatically. Switch
2442 terminal for any messages produced by
2443 breakpoint_re_set. */
2444 target_terminal_ours_for_output ();
aff6338a 2445 /* NOTE: cagney/2003-11-25: Make certain that the target
8fb3e588
AC
2446 stack's section table is kept up-to-date. Architectures,
2447 (e.g., PPC64), use the section table to perform
2448 operations such as address => section name and hence
2449 require the table to contain all sections (including
2450 those found in shared libraries). */
aff6338a 2451 /* NOTE: cagney/2003-11-25: Pass current_target and not
8fb3e588
AC
2452 exec_ops to SOLIB_ADD. This is because current GDB is
2453 only tooled to propagate section_table changes out from
2454 the "current_target" (see target_resize_to_sections), and
2455 not up from the exec stratum. This, of course, isn't
2456 right. "infrun.c" should only interact with the
2457 exec/process stratum, instead relying on the target stack
2458 to propagate relevant changes (stop, section table
2459 changed, ...) up to other layers. */
b0f4b84b 2460#ifdef SOLIB_ADD
aff6338a 2461 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
b0f4b84b
DJ
2462#else
2463 solib_add (NULL, 0, &current_target, auto_solib_add);
2464#endif
488f131b
JB
2465 target_terminal_inferior ();
2466
b0f4b84b
DJ
2467 /* If requested, stop when the dynamic linker notifies
2468 gdb of events. This allows the user to get control
2469 and place breakpoints in initializer routines for
2470 dynamically loaded objects (among other things). */
2471 if (stop_on_solib_events)
2472 {
2473 stop_stepping (ecs);
2474 return;
2475 }
2476
2477 /* NOTE drow/2007-05-11: This might be a good place to check
2478 for "catch load". */
488f131b 2479 }
b0f4b84b
DJ
2480
2481 /* If we are skipping through a shell, or through shared library
2482 loading that we aren't interested in, resume the program. If
2483 we're running the program normally, also resume. But stop if
2484 we're attaching or setting up a remote connection. */
2485 if (stop_soon == STOP_QUIETLY || stop_soon == NO_STOP_QUIETLY)
2486 {
74960c60
VP
2487 /* Loading of shared libraries might have changed breakpoint
2488 addresses. Make sure new breakpoints are inserted. */
0b02b92d
UW
2489 if (stop_soon == NO_STOP_QUIETLY
2490 && !breakpoints_always_inserted_mode ())
74960c60 2491 insert_breakpoints ();
b0f4b84b
DJ
2492 resume (0, TARGET_SIGNAL_0);
2493 prepare_to_wait (ecs);
2494 return;
2495 }
2496
2497 break;
c5aa993b 2498
488f131b 2499 case TARGET_WAITKIND_SPURIOUS:
527159b7 2500 if (debug_infrun)
8a9de0e4 2501 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SPURIOUS\n");
488f131b
JB
2502 resume (0, TARGET_SIGNAL_0);
2503 prepare_to_wait (ecs);
2504 return;
c5aa993b 2505
488f131b 2506 case TARGET_WAITKIND_EXITED:
527159b7 2507 if (debug_infrun)
8a9de0e4 2508 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXITED\n");
fb66883a 2509 inferior_ptid = ecs->ptid;
488f131b
JB
2510 target_terminal_ours (); /* Must do this before mourn anyway */
2511 print_stop_reason (EXITED, ecs->ws.value.integer);
2512
2513 /* Record the exit code in the convenience variable $_exitcode, so
2514 that the user can inspect this again later. */
2515 set_internalvar (lookup_internalvar ("_exitcode"),
8b9b9e1a 2516 value_from_longest (builtin_type_int32,
488f131b
JB
2517 (LONGEST) ecs->ws.value.integer));
2518 gdb_flush (gdb_stdout);
2519 target_mourn_inferior ();
1c0fdd0e 2520 singlestep_breakpoints_inserted_p = 0;
488f131b
JB
2521 stop_print_frame = 0;
2522 stop_stepping (ecs);
2523 return;
c5aa993b 2524
488f131b 2525 case TARGET_WAITKIND_SIGNALLED:
527159b7 2526 if (debug_infrun)
8a9de0e4 2527 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SIGNALLED\n");
fb66883a 2528 inferior_ptid = ecs->ptid;
488f131b 2529 stop_print_frame = 0;
488f131b 2530 target_terminal_ours (); /* Must do this before mourn anyway */
c5aa993b 2531
488f131b
JB
2532 /* Note: By definition of TARGET_WAITKIND_SIGNALLED, we shouldn't
2533 reach here unless the inferior is dead. However, for years
2534 target_kill() was called here, which hints that fatal signals aren't
2535 really fatal on some systems. If that's true, then some changes
2536 may be needed. */
2537 target_mourn_inferior ();
c906108c 2538
2020b7ab 2539 print_stop_reason (SIGNAL_EXITED, ecs->ws.value.sig);
1c0fdd0e 2540 singlestep_breakpoints_inserted_p = 0;
488f131b
JB
2541 stop_stepping (ecs);
2542 return;
c906108c 2543
488f131b
JB
2544 /* The following are the only cases in which we keep going;
2545 the above cases end in a continue or goto. */
2546 case TARGET_WAITKIND_FORKED:
deb3b17b 2547 case TARGET_WAITKIND_VFORKED:
527159b7 2548 if (debug_infrun)
8a9de0e4 2549 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_FORKED\n");
c906108c 2550
5a2901d9
DJ
2551 if (!ptid_equal (ecs->ptid, inferior_ptid))
2552 {
0d1e5fa7 2553 context_switch (ecs->ptid);
35f196d9 2554 reinit_frame_cache ();
5a2901d9
DJ
2555 }
2556
b242c3c2
PA
2557 /* Immediately detach breakpoints from the child before there's
2558 any chance of letting the user delete breakpoints from the
2559 breakpoint lists. If we don't do this early, it's easy to
2560 leave left over traps in the child, vis: "break foo; catch
2561 fork; c; <fork>; del; c; <child calls foo>". We only follow
2562 the fork on the last `continue', and by that time the
2563 breakpoint at "foo" is long gone from the breakpoint table.
2564 If we vforked, then we don't need to unpatch here, since both
2565 parent and child are sharing the same memory pages; we'll
2566 need to unpatch at follow/detach time instead to be certain
2567 that new breakpoints added between catchpoint hit time and
2568 vfork follow are detached. */
2569 if (ecs->ws.kind != TARGET_WAITKIND_VFORKED)
2570 {
2571 int child_pid = ptid_get_pid (ecs->ws.value.related_pid);
2572
2573 /* This won't actually modify the breakpoint list, but will
2574 physically remove the breakpoints from the child. */
2575 detach_breakpoints (child_pid);
2576 }
2577
e58b0e63
PA
2578 /* In case the event is caught by a catchpoint, remember that
2579 the event is to be followed at the next resume of the thread,
2580 and not immediately. */
2581 ecs->event_thread->pending_follow = ecs->ws;
2582
fb14de7b 2583 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
675bf4cb 2584
347bddb7 2585 ecs->event_thread->stop_bpstat = bpstat_stop_status (stop_pc, ecs->ptid);
675bf4cb 2586
347bddb7 2587 ecs->random_signal = !bpstat_explains_signal (ecs->event_thread->stop_bpstat);
04e68871
DJ
2588
2589 /* If no catchpoint triggered for this, then keep going. */
2590 if (ecs->random_signal)
2591 {
e58b0e63
PA
2592 int should_resume;
2593
2020b7ab 2594 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
e58b0e63
PA
2595
2596 should_resume = follow_fork ();
2597
2598 ecs->event_thread = inferior_thread ();
2599 ecs->ptid = inferior_ptid;
2600
2601 if (should_resume)
2602 keep_going (ecs);
2603 else
2604 stop_stepping (ecs);
04e68871
DJ
2605 return;
2606 }
2020b7ab 2607 ecs->event_thread->stop_signal = TARGET_SIGNAL_TRAP;
488f131b
JB
2608 goto process_event_stop_test;
2609
2610 case TARGET_WAITKIND_EXECD:
527159b7 2611 if (debug_infrun)
fc5261f2 2612 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXECD\n");
488f131b 2613
5a2901d9
DJ
2614 if (!ptid_equal (ecs->ptid, inferior_ptid))
2615 {
0d1e5fa7 2616 context_switch (ecs->ptid);
35f196d9 2617 reinit_frame_cache ();
5a2901d9
DJ
2618 }
2619
fb14de7b 2620 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
795e548f
PA
2621
2622 /* This causes the eventpoints and symbol table to be reset.
2623 Must do this now, before trying to determine whether to
2624 stop. */
71b43ef8 2625 follow_exec (inferior_ptid, ecs->ws.value.execd_pathname);
795e548f
PA
2626
2627 ecs->event_thread->stop_bpstat = bpstat_stop_status (stop_pc, ecs->ptid);
2628 ecs->random_signal = !bpstat_explains_signal (ecs->event_thread->stop_bpstat);
2629
71b43ef8
PA
2630 /* Note that this may be referenced from inside
2631 bpstat_stop_status above, through inferior_has_execd. */
2632 xfree (ecs->ws.value.execd_pathname);
2633 ecs->ws.value.execd_pathname = NULL;
2634
04e68871
DJ
2635 /* If no catchpoint triggered for this, then keep going. */
2636 if (ecs->random_signal)
2637 {
2020b7ab 2638 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
04e68871
DJ
2639 keep_going (ecs);
2640 return;
2641 }
2020b7ab 2642 ecs->event_thread->stop_signal = TARGET_SIGNAL_TRAP;
488f131b
JB
2643 goto process_event_stop_test;
2644
b4dc5ffa
MK
2645 /* Be careful not to try to gather much state about a thread
2646 that's in a syscall. It's frequently a losing proposition. */
488f131b 2647 case TARGET_WAITKIND_SYSCALL_ENTRY:
527159b7 2648 if (debug_infrun)
8a9de0e4 2649 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SYSCALL_ENTRY\n");
488f131b
JB
2650 resume (0, TARGET_SIGNAL_0);
2651 prepare_to_wait (ecs);
2652 return;
c906108c 2653
488f131b
JB
2654 /* Before examining the threads further, step this thread to
2655 get it entirely out of the syscall. (We get notice of the
2656 event when the thread is just on the verge of exiting a
2657 syscall. Stepping one instruction seems to get it back
b4dc5ffa 2658 into user code.) */
488f131b 2659 case TARGET_WAITKIND_SYSCALL_RETURN:
527159b7 2660 if (debug_infrun)
8a9de0e4 2661 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SYSCALL_RETURN\n");
488f131b 2662 target_resume (ecs->ptid, 1, TARGET_SIGNAL_0);
488f131b
JB
2663 prepare_to_wait (ecs);
2664 return;
c906108c 2665
488f131b 2666 case TARGET_WAITKIND_STOPPED:
527159b7 2667 if (debug_infrun)
8a9de0e4 2668 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_STOPPED\n");
2020b7ab 2669 ecs->event_thread->stop_signal = ecs->ws.value.sig;
488f131b 2670 break;
c906108c 2671
b2175913
MS
2672 case TARGET_WAITKIND_NO_HISTORY:
2673 /* Reverse execution: target ran out of history info. */
fb14de7b 2674 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
b2175913
MS
2675 print_stop_reason (NO_HISTORY, 0);
2676 stop_stepping (ecs);
2677 return;
2678
488f131b
JB
2679 /* We had an event in the inferior, but we are not interested
2680 in handling it at this level. The lower layers have already
8e7d2c16 2681 done what needs to be done, if anything.
8fb3e588
AC
2682
2683 One of the possible circumstances for this is when the
2684 inferior produces output for the console. The inferior has
2685 not stopped, and we are ignoring the event. Another possible
2686 circumstance is any event which the lower level knows will be
2687 reported multiple times without an intervening resume. */
488f131b 2688 case TARGET_WAITKIND_IGNORE:
527159b7 2689 if (debug_infrun)
8a9de0e4 2690 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_IGNORE\n");
8e7d2c16 2691 prepare_to_wait (ecs);
488f131b
JB
2692 return;
2693 }
c906108c 2694
488f131b
JB
2695 if (ecs->new_thread_event)
2696 {
94cc34af
PA
2697 if (non_stop)
2698 /* Non-stop assumes that the target handles adding new threads
2699 to the thread list. */
2700 internal_error (__FILE__, __LINE__, "\
2701targets should add new threads to the thread list themselves in non-stop mode.");
2702
2703 /* We may want to consider not doing a resume here in order to
2704 give the user a chance to play with the new thread. It might
2705 be good to make that a user-settable option. */
2706
2707 /* At this point, all threads are stopped (happens automatically
2708 in either the OS or the native code). Therefore we need to
2709 continue all threads in order to make progress. */
2710
488f131b
JB
2711 target_resume (RESUME_ALL, 0, TARGET_SIGNAL_0);
2712 prepare_to_wait (ecs);
2713 return;
2714 }
c906108c 2715
2020b7ab 2716 if (ecs->ws.kind == TARGET_WAITKIND_STOPPED)
252fbfc8
PA
2717 {
2718 /* Do we need to clean up the state of a thread that has
2719 completed a displaced single-step? (Doing so usually affects
2720 the PC, so do it here, before we set stop_pc.) */
2721 displaced_step_fixup (ecs->ptid, ecs->event_thread->stop_signal);
2722
2723 /* If we either finished a single-step or hit a breakpoint, but
2724 the user wanted this thread to be stopped, pretend we got a
2725 SIG0 (generic unsignaled stop). */
2726
2727 if (ecs->event_thread->stop_requested
2728 && ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP)
2729 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
2730 }
237fc4c9 2731
515630c5 2732 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
488f131b 2733
527159b7 2734 if (debug_infrun)
237fc4c9
PA
2735 {
2736 fprintf_unfiltered (gdb_stdlog, "infrun: stop_pc = 0x%s\n",
2737 paddr_nz (stop_pc));
d92524f1 2738 if (target_stopped_by_watchpoint ())
237fc4c9
PA
2739 {
2740 CORE_ADDR addr;
2741 fprintf_unfiltered (gdb_stdlog, "infrun: stopped by watchpoint\n");
2742
2743 if (target_stopped_data_address (&current_target, &addr))
2744 fprintf_unfiltered (gdb_stdlog,
2745 "infrun: stopped data address = 0x%s\n",
2746 paddr_nz (addr));
2747 else
2748 fprintf_unfiltered (gdb_stdlog,
2749 "infrun: (no data address available)\n");
2750 }
2751 }
527159b7 2752
9f976b41
DJ
2753 if (stepping_past_singlestep_breakpoint)
2754 {
1c0fdd0e 2755 gdb_assert (singlestep_breakpoints_inserted_p);
9f976b41
DJ
2756 gdb_assert (ptid_equal (singlestep_ptid, ecs->ptid));
2757 gdb_assert (!ptid_equal (singlestep_ptid, saved_singlestep_ptid));
2758
2759 stepping_past_singlestep_breakpoint = 0;
2760
2761 /* We've either finished single-stepping past the single-step
8fb3e588
AC
2762 breakpoint, or stopped for some other reason. It would be nice if
2763 we could tell, but we can't reliably. */
2020b7ab 2764 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP)
8fb3e588 2765 {
527159b7 2766 if (debug_infrun)
8a9de0e4 2767 fprintf_unfiltered (gdb_stdlog, "infrun: stepping_past_singlestep_breakpoint\n");
9f976b41 2768 /* Pull the single step breakpoints out of the target. */
e0cd558a 2769 remove_single_step_breakpoints ();
9f976b41
DJ
2770 singlestep_breakpoints_inserted_p = 0;
2771
2772 ecs->random_signal = 0;
2773
0d1e5fa7 2774 context_switch (saved_singlestep_ptid);
9a4105ab
AC
2775 if (deprecated_context_hook)
2776 deprecated_context_hook (pid_to_thread_id (ecs->ptid));
9f976b41
DJ
2777
2778 resume (1, TARGET_SIGNAL_0);
2779 prepare_to_wait (ecs);
2780 return;
2781 }
2782 }
2783
ca67fcb8 2784 if (!ptid_equal (deferred_step_ptid, null_ptid))
6a6b96b9 2785 {
94cc34af
PA
2786 /* In non-stop mode, there's never a deferred_step_ptid set. */
2787 gdb_assert (!non_stop);
2788
6a6b96b9
UW
2789 /* If we stopped for some other reason than single-stepping, ignore
2790 the fact that we were supposed to switch back. */
2020b7ab 2791 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP)
6a6b96b9
UW
2792 {
2793 if (debug_infrun)
2794 fprintf_unfiltered (gdb_stdlog,
ca67fcb8 2795 "infrun: handling deferred step\n");
6a6b96b9
UW
2796
2797 /* Pull the single step breakpoints out of the target. */
2798 if (singlestep_breakpoints_inserted_p)
2799 {
2800 remove_single_step_breakpoints ();
2801 singlestep_breakpoints_inserted_p = 0;
2802 }
2803
2804 /* Note: We do not call context_switch at this point, as the
2805 context is already set up for stepping the original thread. */
ca67fcb8
VP
2806 switch_to_thread (deferred_step_ptid);
2807 deferred_step_ptid = null_ptid;
6a6b96b9
UW
2808 /* Suppress spurious "Switching to ..." message. */
2809 previous_inferior_ptid = inferior_ptid;
2810
2811 resume (1, TARGET_SIGNAL_0);
2812 prepare_to_wait (ecs);
2813 return;
2814 }
ca67fcb8
VP
2815
2816 deferred_step_ptid = null_ptid;
6a6b96b9
UW
2817 }
2818
488f131b
JB
2819 /* See if a thread hit a thread-specific breakpoint that was meant for
2820 another thread. If so, then step that thread past the breakpoint,
2821 and continue it. */
2822
2020b7ab 2823 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP)
488f131b 2824 {
9f976b41
DJ
2825 int thread_hop_needed = 0;
2826
f8d40ec8
JB
2827 /* Check if a regular breakpoint has been hit before checking
2828 for a potential single step breakpoint. Otherwise, GDB will
2829 not see this breakpoint hit when stepping onto breakpoints. */
c36b740a 2830 if (regular_breakpoint_inserted_here_p (stop_pc))
488f131b 2831 {
c5aa993b 2832 ecs->random_signal = 0;
4fa8626c 2833 if (!breakpoint_thread_match (stop_pc, ecs->ptid))
9f976b41
DJ
2834 thread_hop_needed = 1;
2835 }
1c0fdd0e 2836 else if (singlestep_breakpoints_inserted_p)
9f976b41 2837 {
fd48f117
DJ
2838 /* We have not context switched yet, so this should be true
2839 no matter which thread hit the singlestep breakpoint. */
2840 gdb_assert (ptid_equal (inferior_ptid, singlestep_ptid));
2841 if (debug_infrun)
2842 fprintf_unfiltered (gdb_stdlog, "infrun: software single step "
2843 "trap for %s\n",
2844 target_pid_to_str (ecs->ptid));
2845
9f976b41
DJ
2846 ecs->random_signal = 0;
2847 /* The call to in_thread_list is necessary because PTIDs sometimes
2848 change when we go from single-threaded to multi-threaded. If
2849 the singlestep_ptid is still in the list, assume that it is
2850 really different from ecs->ptid. */
2851 if (!ptid_equal (singlestep_ptid, ecs->ptid)
2852 && in_thread_list (singlestep_ptid))
2853 {
fd48f117
DJ
2854 /* If the PC of the thread we were trying to single-step
2855 has changed, discard this event (which we were going
2856 to ignore anyway), and pretend we saw that thread
2857 trap. This prevents us continuously moving the
2858 single-step breakpoint forward, one instruction at a
2859 time. If the PC has changed, then the thread we were
2860 trying to single-step has trapped or been signalled,
2861 but the event has not been reported to GDB yet.
2862
2863 There might be some cases where this loses signal
2864 information, if a signal has arrived at exactly the
2865 same time that the PC changed, but this is the best
2866 we can do with the information available. Perhaps we
2867 should arrange to report all events for all threads
2868 when they stop, or to re-poll the remote looking for
2869 this particular thread (i.e. temporarily enable
2870 schedlock). */
515630c5
UW
2871
2872 CORE_ADDR new_singlestep_pc
2873 = regcache_read_pc (get_thread_regcache (singlestep_ptid));
2874
2875 if (new_singlestep_pc != singlestep_pc)
fd48f117 2876 {
2020b7ab
PA
2877 enum target_signal stop_signal;
2878
fd48f117
DJ
2879 if (debug_infrun)
2880 fprintf_unfiltered (gdb_stdlog, "infrun: unexpected thread,"
2881 " but expected thread advanced also\n");
2882
2883 /* The current context still belongs to
2884 singlestep_ptid. Don't swap here, since that's
2885 the context we want to use. Just fudge our
2886 state and continue. */
2020b7ab
PA
2887 stop_signal = ecs->event_thread->stop_signal;
2888 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
fd48f117 2889 ecs->ptid = singlestep_ptid;
e09875d4 2890 ecs->event_thread = find_thread_ptid (ecs->ptid);
2020b7ab 2891 ecs->event_thread->stop_signal = stop_signal;
515630c5 2892 stop_pc = new_singlestep_pc;
fd48f117
DJ
2893 }
2894 else
2895 {
2896 if (debug_infrun)
2897 fprintf_unfiltered (gdb_stdlog,
2898 "infrun: unexpected thread\n");
2899
2900 thread_hop_needed = 1;
2901 stepping_past_singlestep_breakpoint = 1;
2902 saved_singlestep_ptid = singlestep_ptid;
2903 }
9f976b41
DJ
2904 }
2905 }
2906
2907 if (thread_hop_needed)
8fb3e588 2908 {
237fc4c9 2909 int remove_status = 0;
8fb3e588 2910
527159b7 2911 if (debug_infrun)
8a9de0e4 2912 fprintf_unfiltered (gdb_stdlog, "infrun: thread_hop_needed\n");
527159b7 2913
b3444185
PA
2914 /* Switch context before touching inferior memory, the
2915 previous thread may have exited. */
2916 if (!ptid_equal (inferior_ptid, ecs->ptid))
2917 context_switch (ecs->ptid);
2918
8fb3e588
AC
2919 /* Saw a breakpoint, but it was hit by the wrong thread.
2920 Just continue. */
2921
1c0fdd0e 2922 if (singlestep_breakpoints_inserted_p)
488f131b 2923 {
8fb3e588 2924 /* Pull the single step breakpoints out of the target. */
e0cd558a 2925 remove_single_step_breakpoints ();
8fb3e588
AC
2926 singlestep_breakpoints_inserted_p = 0;
2927 }
2928
237fc4c9
PA
2929 /* If the arch can displace step, don't remove the
2930 breakpoints. */
2931 if (!use_displaced_stepping (current_gdbarch))
2932 remove_status = remove_breakpoints ();
2933
8fb3e588
AC
2934 /* Did we fail to remove breakpoints? If so, try
2935 to set the PC past the bp. (There's at least
2936 one situation in which we can fail to remove
2937 the bp's: On HP-UX's that use ttrace, we can't
2938 change the address space of a vforking child
2939 process until the child exits (well, okay, not
2940 then either :-) or execs. */
2941 if (remove_status != 0)
9d9cd7ac 2942 error (_("Cannot step over breakpoint hit in wrong thread"));
8fb3e588
AC
2943 else
2944 { /* Single step */
94cc34af
PA
2945 if (!non_stop)
2946 {
2947 /* Only need to require the next event from this
2948 thread in all-stop mode. */
2949 waiton_ptid = ecs->ptid;
2950 infwait_state = infwait_thread_hop_state;
2951 }
8fb3e588 2952
4e1c45ea 2953 ecs->event_thread->stepping_over_breakpoint = 1;
8fb3e588
AC
2954 keep_going (ecs);
2955 registers_changed ();
2956 return;
2957 }
488f131b 2958 }
1c0fdd0e 2959 else if (singlestep_breakpoints_inserted_p)
8fb3e588
AC
2960 {
2961 sw_single_step_trap_p = 1;
2962 ecs->random_signal = 0;
2963 }
488f131b
JB
2964 }
2965 else
2966 ecs->random_signal = 1;
c906108c 2967
488f131b 2968 /* See if something interesting happened to the non-current thread. If
b40c7d58
DJ
2969 so, then switch to that thread. */
2970 if (!ptid_equal (ecs->ptid, inferior_ptid))
488f131b 2971 {
527159b7 2972 if (debug_infrun)
8a9de0e4 2973 fprintf_unfiltered (gdb_stdlog, "infrun: context switch\n");
527159b7 2974
0d1e5fa7 2975 context_switch (ecs->ptid);
c5aa993b 2976
9a4105ab
AC
2977 if (deprecated_context_hook)
2978 deprecated_context_hook (pid_to_thread_id (ecs->ptid));
488f131b 2979 }
c906108c 2980
1c0fdd0e 2981 if (singlestep_breakpoints_inserted_p)
488f131b
JB
2982 {
2983 /* Pull the single step breakpoints out of the target. */
e0cd558a 2984 remove_single_step_breakpoints ();
488f131b
JB
2985 singlestep_breakpoints_inserted_p = 0;
2986 }
c906108c 2987
d983da9c
DJ
2988 if (stepped_after_stopped_by_watchpoint)
2989 stopped_by_watchpoint = 0;
2990 else
2991 stopped_by_watchpoint = watchpoints_triggered (&ecs->ws);
2992
2993 /* If necessary, step over this watchpoint. We'll be back to display
2994 it in a moment. */
2995 if (stopped_by_watchpoint
d92524f1 2996 && (target_have_steppable_watchpoint
d983da9c 2997 || gdbarch_have_nonsteppable_watchpoint (current_gdbarch)))
488f131b 2998 {
488f131b
JB
2999 /* At this point, we are stopped at an instruction which has
3000 attempted to write to a piece of memory under control of
3001 a watchpoint. The instruction hasn't actually executed
3002 yet. If we were to evaluate the watchpoint expression
3003 now, we would get the old value, and therefore no change
3004 would seem to have occurred.
3005
3006 In order to make watchpoints work `right', we really need
3007 to complete the memory write, and then evaluate the
d983da9c
DJ
3008 watchpoint expression. We do this by single-stepping the
3009 target.
3010
3011 It may not be necessary to disable the watchpoint to stop over
3012 it. For example, the PA can (with some kernel cooperation)
3013 single step over a watchpoint without disabling the watchpoint.
3014
3015 It is far more common to need to disable a watchpoint to step
3016 the inferior over it. If we have non-steppable watchpoints,
3017 we must disable the current watchpoint; it's simplest to
3018 disable all watchpoints and breakpoints. */
2facfe5c
DD
3019 int hw_step = 1;
3020
d92524f1 3021 if (!target_have_steppable_watchpoint)
d983da9c 3022 remove_breakpoints ();
2facfe5c 3023 /* Single step */
fb14de7b 3024 hw_step = maybe_software_singlestep (current_gdbarch, stop_pc);
2facfe5c 3025 target_resume (ecs->ptid, hw_step, TARGET_SIGNAL_0);
b9412953 3026 registers_changed ();
0d1e5fa7 3027 waiton_ptid = ecs->ptid;
d92524f1 3028 if (target_have_steppable_watchpoint)
0d1e5fa7 3029 infwait_state = infwait_step_watch_state;
d983da9c 3030 else
0d1e5fa7 3031 infwait_state = infwait_nonstep_watch_state;
488f131b
JB
3032 prepare_to_wait (ecs);
3033 return;
3034 }
3035
488f131b
JB
3036 ecs->stop_func_start = 0;
3037 ecs->stop_func_end = 0;
3038 ecs->stop_func_name = 0;
3039 /* Don't care about return value; stop_func_start and stop_func_name
3040 will both be 0 if it doesn't work. */
3041 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
3042 &ecs->stop_func_start, &ecs->stop_func_end);
cbf3b44a
UW
3043 ecs->stop_func_start
3044 += gdbarch_deprecated_function_start_offset (current_gdbarch);
4e1c45ea 3045 ecs->event_thread->stepping_over_breakpoint = 0;
347bddb7 3046 bpstat_clear (&ecs->event_thread->stop_bpstat);
414c69f7 3047 ecs->event_thread->stop_step = 0;
488f131b
JB
3048 stop_print_frame = 1;
3049 ecs->random_signal = 0;
3050 stopped_by_random_signal = 0;
488f131b 3051
2020b7ab 3052 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP
4e1c45ea 3053 && ecs->event_thread->trap_expected
3352ef37 3054 && gdbarch_single_step_through_delay_p (current_gdbarch)
4e1c45ea 3055 && currently_stepping (ecs->event_thread))
3352ef37 3056 {
b50d7442 3057 /* We're trying to step off a breakpoint. Turns out that we're
3352ef37
AC
3058 also on an instruction that needs to be stepped multiple
3059 times before it's been fully executing. E.g., architectures
3060 with a delay slot. It needs to be stepped twice, once for
3061 the instruction and once for the delay slot. */
3062 int step_through_delay
3063 = gdbarch_single_step_through_delay (current_gdbarch,
3064 get_current_frame ());
527159b7 3065 if (debug_infrun && step_through_delay)
8a9de0e4 3066 fprintf_unfiltered (gdb_stdlog, "infrun: step through delay\n");
4e1c45ea 3067 if (ecs->event_thread->step_range_end == 0 && step_through_delay)
3352ef37
AC
3068 {
3069 /* The user issued a continue when stopped at a breakpoint.
3070 Set up for another trap and get out of here. */
4e1c45ea 3071 ecs->event_thread->stepping_over_breakpoint = 1;
3352ef37
AC
3072 keep_going (ecs);
3073 return;
3074 }
3075 else if (step_through_delay)
3076 {
3077 /* The user issued a step when stopped at a breakpoint.
3078 Maybe we should stop, maybe we should not - the delay
3079 slot *might* correspond to a line of source. In any
ca67fcb8
VP
3080 case, don't decide that here, just set
3081 ecs->stepping_over_breakpoint, making sure we
3082 single-step again before breakpoints are re-inserted. */
4e1c45ea 3083 ecs->event_thread->stepping_over_breakpoint = 1;
3352ef37
AC
3084 }
3085 }
3086
488f131b
JB
3087 /* Look at the cause of the stop, and decide what to do.
3088 The alternatives are:
0d1e5fa7
PA
3089 1) stop_stepping and return; to really stop and return to the debugger,
3090 2) keep_going and return to start up again
4e1c45ea 3091 (set ecs->event_thread->stepping_over_breakpoint to 1 to single step once)
488f131b
JB
3092 3) set ecs->random_signal to 1, and the decision between 1 and 2
3093 will be made according to the signal handling tables. */
3094
3095 /* First, distinguish signals caused by the debugger from signals
03cebad2
MK
3096 that have to do with the program's own actions. Note that
3097 breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
3098 on the operating system version. Here we detect when a SIGILL or
3099 SIGEMT is really a breakpoint and change it to SIGTRAP. We do
3100 something similar for SIGSEGV, since a SIGSEGV will be generated
3101 when we're trying to execute a breakpoint instruction on a
3102 non-executable stack. This happens for call dummy breakpoints
3103 for architectures like SPARC that place call dummies on the
237fc4c9 3104 stack.
488f131b 3105
237fc4c9
PA
3106 If we're doing a displaced step past a breakpoint, then the
3107 breakpoint is always inserted at the original instruction;
3108 non-standard signals can't be explained by the breakpoint. */
2020b7ab 3109 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP
4e1c45ea 3110 || (! ecs->event_thread->trap_expected
237fc4c9 3111 && breakpoint_inserted_here_p (stop_pc)
2020b7ab
PA
3112 && (ecs->event_thread->stop_signal == TARGET_SIGNAL_ILL
3113 || ecs->event_thread->stop_signal == TARGET_SIGNAL_SEGV
3114 || ecs->event_thread->stop_signal == TARGET_SIGNAL_EMT))
b0f4b84b
DJ
3115 || stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_NO_SIGSTOP
3116 || stop_soon == STOP_QUIETLY_REMOTE)
488f131b 3117 {
2020b7ab 3118 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP && stop_after_trap)
488f131b 3119 {
527159b7 3120 if (debug_infrun)
8a9de0e4 3121 fprintf_unfiltered (gdb_stdlog, "infrun: stopped\n");
488f131b
JB
3122 stop_print_frame = 0;
3123 stop_stepping (ecs);
3124 return;
3125 }
c54cfec8
EZ
3126
3127 /* This is originated from start_remote(), start_inferior() and
3128 shared libraries hook functions. */
b0f4b84b 3129 if (stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_REMOTE)
488f131b 3130 {
527159b7 3131 if (debug_infrun)
8a9de0e4 3132 fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
488f131b
JB
3133 stop_stepping (ecs);
3134 return;
3135 }
3136
c54cfec8 3137 /* This originates from attach_command(). We need to overwrite
a0d21d28
PA
3138 the stop_signal here, because some kernels don't ignore a
3139 SIGSTOP in a subsequent ptrace(PTRACE_CONT,SIGSTOP) call.
3140 See more comments in inferior.h. On the other hand, if we
a0ef4274 3141 get a non-SIGSTOP, report it to the user - assume the backend
a0d21d28
PA
3142 will handle the SIGSTOP if it should show up later.
3143
3144 Also consider that the attach is complete when we see a
3145 SIGTRAP. Some systems (e.g. Windows), and stubs supporting
3146 target extended-remote report it instead of a SIGSTOP
3147 (e.g. gdbserver). We already rely on SIGTRAP being our
e0ba6746
PA
3148 signal, so this is no exception.
3149
3150 Also consider that the attach is complete when we see a
3151 TARGET_SIGNAL_0. In non-stop mode, GDB will explicitly tell
3152 the target to stop all threads of the inferior, in case the
3153 low level attach operation doesn't stop them implicitly. If
3154 they weren't stopped implicitly, then the stub will report a
3155 TARGET_SIGNAL_0, meaning: stopped for no particular reason
3156 other than GDB's request. */
a0ef4274 3157 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
2020b7ab 3158 && (ecs->event_thread->stop_signal == TARGET_SIGNAL_STOP
e0ba6746
PA
3159 || ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP
3160 || ecs->event_thread->stop_signal == TARGET_SIGNAL_0))
c54cfec8
EZ
3161 {
3162 stop_stepping (ecs);
2020b7ab 3163 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
c54cfec8
EZ
3164 return;
3165 }
3166
fba57f8f 3167 /* See if there is a breakpoint at the current PC. */
347bddb7 3168 ecs->event_thread->stop_bpstat = bpstat_stop_status (stop_pc, ecs->ptid);
fba57f8f
VP
3169
3170 /* Following in case break condition called a
3171 function. */
3172 stop_print_frame = 1;
488f131b 3173
73dd234f 3174 /* NOTE: cagney/2003-03-29: These two checks for a random signal
8fb3e588
AC
3175 at one stage in the past included checks for an inferior
3176 function call's call dummy's return breakpoint. The original
3177 comment, that went with the test, read:
73dd234f 3178
8fb3e588
AC
3179 ``End of a stack dummy. Some systems (e.g. Sony news) give
3180 another signal besides SIGTRAP, so check here as well as
3181 above.''
73dd234f 3182
8002d778 3183 If someone ever tries to get call dummys on a
73dd234f 3184 non-executable stack to work (where the target would stop
03cebad2
MK
3185 with something like a SIGSEGV), then those tests might need
3186 to be re-instated. Given, however, that the tests were only
73dd234f 3187 enabled when momentary breakpoints were not being used, I
03cebad2
MK
3188 suspect that it won't be the case.
3189
8fb3e588
AC
3190 NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
3191 be necessary for call dummies on a non-executable stack on
3192 SPARC. */
73dd234f 3193
2020b7ab 3194 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP)
488f131b 3195 ecs->random_signal
347bddb7 3196 = !(bpstat_explains_signal (ecs->event_thread->stop_bpstat)
4e1c45ea
PA
3197 || ecs->event_thread->trap_expected
3198 || (ecs->event_thread->step_range_end
3199 && ecs->event_thread->step_resume_breakpoint == NULL));
488f131b
JB
3200 else
3201 {
347bddb7 3202 ecs->random_signal = !bpstat_explains_signal (ecs->event_thread->stop_bpstat);
488f131b 3203 if (!ecs->random_signal)
2020b7ab 3204 ecs->event_thread->stop_signal = TARGET_SIGNAL_TRAP;
488f131b
JB
3205 }
3206 }
3207
3208 /* When we reach this point, we've pretty much decided
3209 that the reason for stopping must've been a random
3210 (unexpected) signal. */
3211
3212 else
3213 ecs->random_signal = 1;
488f131b 3214
04e68871 3215process_event_stop_test:
488f131b
JB
3216 /* For the program's own signals, act according to
3217 the signal handling tables. */
3218
3219 if (ecs->random_signal)
3220 {
3221 /* Signal not for debugging purposes. */
3222 int printed = 0;
3223
527159b7 3224 if (debug_infrun)
2020b7ab
PA
3225 fprintf_unfiltered (gdb_stdlog, "infrun: random signal %d\n",
3226 ecs->event_thread->stop_signal);
527159b7 3227
488f131b
JB
3228 stopped_by_random_signal = 1;
3229
2020b7ab 3230 if (signal_print[ecs->event_thread->stop_signal])
488f131b
JB
3231 {
3232 printed = 1;
3233 target_terminal_ours_for_output ();
2020b7ab 3234 print_stop_reason (SIGNAL_RECEIVED, ecs->event_thread->stop_signal);
488f131b 3235 }
252fbfc8
PA
3236 /* Always stop on signals if we're either just gaining control
3237 of the program, or the user explicitly requested this thread
3238 to remain stopped. */
d6b48e9c 3239 if (stop_soon != NO_STOP_QUIETLY
252fbfc8 3240 || ecs->event_thread->stop_requested
d6b48e9c 3241 || signal_stop_state (ecs->event_thread->stop_signal))
488f131b
JB
3242 {
3243 stop_stepping (ecs);
3244 return;
3245 }
3246 /* If not going to stop, give terminal back
3247 if we took it away. */
3248 else if (printed)
3249 target_terminal_inferior ();
3250
3251 /* Clear the signal if it should not be passed. */
2020b7ab
PA
3252 if (signal_program[ecs->event_thread->stop_signal] == 0)
3253 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
488f131b 3254
fb14de7b 3255 if (ecs->event_thread->prev_pc == stop_pc
4e1c45ea
PA
3256 && ecs->event_thread->trap_expected
3257 && ecs->event_thread->step_resume_breakpoint == NULL)
68f53502
AC
3258 {
3259 /* We were just starting a new sequence, attempting to
3260 single-step off of a breakpoint and expecting a SIGTRAP.
237fc4c9 3261 Instead this signal arrives. This signal will take us out
68f53502
AC
3262 of the stepping range so GDB needs to remember to, when
3263 the signal handler returns, resume stepping off that
3264 breakpoint. */
3265 /* To simplify things, "continue" is forced to use the same
3266 code paths as single-step - set a breakpoint at the
3267 signal return address and then, once hit, step off that
3268 breakpoint. */
237fc4c9
PA
3269 if (debug_infrun)
3270 fprintf_unfiltered (gdb_stdlog,
3271 "infrun: signal arrived while stepping over "
3272 "breakpoint\n");
d3169d93 3273
44cbf7b5 3274 insert_step_resume_breakpoint_at_frame (get_current_frame ());
4e1c45ea 3275 ecs->event_thread->step_after_step_resume_breakpoint = 1;
9d799f85
AC
3276 keep_going (ecs);
3277 return;
68f53502 3278 }
9d799f85 3279
4e1c45ea 3280 if (ecs->event_thread->step_range_end != 0
2020b7ab 3281 && ecs->event_thread->stop_signal != TARGET_SIGNAL_0
4e1c45ea
PA
3282 && (ecs->event_thread->step_range_start <= stop_pc
3283 && stop_pc < ecs->event_thread->step_range_end)
9d799f85 3284 && frame_id_eq (get_frame_id (get_current_frame ()),
4e1c45ea
PA
3285 ecs->event_thread->step_frame_id)
3286 && ecs->event_thread->step_resume_breakpoint == NULL)
d303a6c7
AC
3287 {
3288 /* The inferior is about to take a signal that will take it
3289 out of the single step range. Set a breakpoint at the
3290 current PC (which is presumably where the signal handler
3291 will eventually return) and then allow the inferior to
3292 run free.
3293
3294 Note that this is only needed for a signal delivered
3295 while in the single-step range. Nested signals aren't a
3296 problem as they eventually all return. */
237fc4c9
PA
3297 if (debug_infrun)
3298 fprintf_unfiltered (gdb_stdlog,
3299 "infrun: signal may take us out of "
3300 "single-step range\n");
3301
44cbf7b5 3302 insert_step_resume_breakpoint_at_frame (get_current_frame ());
9d799f85
AC
3303 keep_going (ecs);
3304 return;
d303a6c7 3305 }
9d799f85
AC
3306
3307 /* Note: step_resume_breakpoint may be non-NULL. This occures
3308 when either there's a nested signal, or when there's a
3309 pending signal enabled just as the signal handler returns
3310 (leaving the inferior at the step-resume-breakpoint without
3311 actually executing it). Either way continue until the
3312 breakpoint is really hit. */
488f131b
JB
3313 keep_going (ecs);
3314 return;
3315 }
3316
3317 /* Handle cases caused by hitting a breakpoint. */
3318 {
3319 CORE_ADDR jmp_buf_pc;
3320 struct bpstat_what what;
3321
347bddb7 3322 what = bpstat_what (ecs->event_thread->stop_bpstat);
488f131b
JB
3323
3324 if (what.call_dummy)
3325 {
3326 stop_stack_dummy = 1;
c5aa993b 3327 }
c906108c 3328
488f131b 3329 switch (what.main_action)
c5aa993b 3330 {
488f131b 3331 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
611c83ae
PA
3332 /* If we hit the breakpoint at longjmp while stepping, we
3333 install a momentary breakpoint at the target of the
3334 jmp_buf. */
3335
3336 if (debug_infrun)
3337 fprintf_unfiltered (gdb_stdlog,
3338 "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME\n");
3339
4e1c45ea 3340 ecs->event_thread->stepping_over_breakpoint = 1;
611c83ae 3341
91104499 3342 if (!gdbarch_get_longjmp_target_p (current_gdbarch)
60ade65d
UW
3343 || !gdbarch_get_longjmp_target (current_gdbarch,
3344 get_current_frame (), &jmp_buf_pc))
c5aa993b 3345 {
611c83ae
PA
3346 if (debug_infrun)
3347 fprintf_unfiltered (gdb_stdlog, "\
3348infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME (!gdbarch_get_longjmp_target)\n");
488f131b 3349 keep_going (ecs);
104c1213 3350 return;
c5aa993b 3351 }
488f131b 3352
611c83ae
PA
3353 /* We're going to replace the current step-resume breakpoint
3354 with a longjmp-resume breakpoint. */
4e1c45ea 3355 delete_step_resume_breakpoint (ecs->event_thread);
611c83ae
PA
3356
3357 /* Insert a breakpoint at resume address. */
3358 insert_longjmp_resume_breakpoint (jmp_buf_pc);
c906108c 3359
488f131b
JB
3360 keep_going (ecs);
3361 return;
c906108c 3362
488f131b 3363 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
527159b7 3364 if (debug_infrun)
611c83ae
PA
3365 fprintf_unfiltered (gdb_stdlog,
3366 "infrun: BPSTAT_WHAT_CLEAR_LONGJMP_RESUME\n");
3367
4e1c45ea
PA
3368 gdb_assert (ecs->event_thread->step_resume_breakpoint != NULL);
3369 delete_step_resume_breakpoint (ecs->event_thread);
611c83ae 3370
414c69f7 3371 ecs->event_thread->stop_step = 1;
611c83ae
PA
3372 print_stop_reason (END_STEPPING_RANGE, 0);
3373 stop_stepping (ecs);
3374 return;
488f131b
JB
3375
3376 case BPSTAT_WHAT_SINGLE:
527159b7 3377 if (debug_infrun)
8802d8ed 3378 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_SINGLE\n");
4e1c45ea 3379 ecs->event_thread->stepping_over_breakpoint = 1;
488f131b
JB
3380 /* Still need to check other stuff, at least the case
3381 where we are stepping and step out of the right range. */
3382 break;
c906108c 3383
488f131b 3384 case BPSTAT_WHAT_STOP_NOISY:
527159b7 3385 if (debug_infrun)
8802d8ed 3386 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_NOISY\n");
488f131b 3387 stop_print_frame = 1;
c906108c 3388
d303a6c7
AC
3389 /* We are about to nuke the step_resume_breakpointt via the
3390 cleanup chain, so no need to worry about it here. */
c5aa993b 3391
488f131b
JB
3392 stop_stepping (ecs);
3393 return;
c5aa993b 3394
488f131b 3395 case BPSTAT_WHAT_STOP_SILENT:
527159b7 3396 if (debug_infrun)
8802d8ed 3397 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_SILENT\n");
488f131b 3398 stop_print_frame = 0;
c5aa993b 3399
d303a6c7
AC
3400 /* We are about to nuke the step_resume_breakpoin via the
3401 cleanup chain, so no need to worry about it here. */
c5aa993b 3402
488f131b 3403 stop_stepping (ecs);
e441088d 3404 return;
c5aa993b 3405
488f131b 3406 case BPSTAT_WHAT_STEP_RESUME:
527159b7 3407 if (debug_infrun)
8802d8ed 3408 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STEP_RESUME\n");
527159b7 3409
4e1c45ea
PA
3410 delete_step_resume_breakpoint (ecs->event_thread);
3411 if (ecs->event_thread->step_after_step_resume_breakpoint)
68f53502
AC
3412 {
3413 /* Back when the step-resume breakpoint was inserted, we
3414 were trying to single-step off a breakpoint. Go back
3415 to doing that. */
4e1c45ea
PA
3416 ecs->event_thread->step_after_step_resume_breakpoint = 0;
3417 ecs->event_thread->stepping_over_breakpoint = 1;
68f53502
AC
3418 keep_going (ecs);
3419 return;
3420 }
b2175913
MS
3421 if (stop_pc == ecs->stop_func_start
3422 && execution_direction == EXEC_REVERSE)
3423 {
3424 /* We are stepping over a function call in reverse, and
3425 just hit the step-resume breakpoint at the start
3426 address of the function. Go back to single-stepping,
3427 which should take us back to the function call. */
3428 ecs->event_thread->stepping_over_breakpoint = 1;
3429 keep_going (ecs);
3430 return;
3431 }
488f131b
JB
3432 break;
3433
488f131b 3434 case BPSTAT_WHAT_CHECK_SHLIBS:
c906108c 3435 {
527159b7 3436 if (debug_infrun)
8802d8ed 3437 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_CHECK_SHLIBS\n");
488f131b
JB
3438
3439 /* Check for any newly added shared libraries if we're
3440 supposed to be adding them automatically. Switch
3441 terminal for any messages produced by
3442 breakpoint_re_set. */
3443 target_terminal_ours_for_output ();
aff6338a 3444 /* NOTE: cagney/2003-11-25: Make certain that the target
8fb3e588
AC
3445 stack's section table is kept up-to-date. Architectures,
3446 (e.g., PPC64), use the section table to perform
3447 operations such as address => section name and hence
3448 require the table to contain all sections (including
3449 those found in shared libraries). */
aff6338a 3450 /* NOTE: cagney/2003-11-25: Pass current_target and not
8fb3e588
AC
3451 exec_ops to SOLIB_ADD. This is because current GDB is
3452 only tooled to propagate section_table changes out from
3453 the "current_target" (see target_resize_to_sections), and
3454 not up from the exec stratum. This, of course, isn't
3455 right. "infrun.c" should only interact with the
3456 exec/process stratum, instead relying on the target stack
3457 to propagate relevant changes (stop, section table
3458 changed, ...) up to other layers. */
a77053c2 3459#ifdef SOLIB_ADD
aff6338a 3460 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
a77053c2
MK
3461#else
3462 solib_add (NULL, 0, &current_target, auto_solib_add);
3463#endif
488f131b
JB
3464 target_terminal_inferior ();
3465
488f131b
JB
3466 /* If requested, stop when the dynamic linker notifies
3467 gdb of events. This allows the user to get control
3468 and place breakpoints in initializer routines for
3469 dynamically loaded objects (among other things). */
877522db 3470 if (stop_on_solib_events || stop_stack_dummy)
d4f3574e 3471 {
488f131b 3472 stop_stepping (ecs);
d4f3574e
SS
3473 return;
3474 }
c5aa993b 3475 else
c5aa993b 3476 {
488f131b 3477 /* We want to step over this breakpoint, then keep going. */
4e1c45ea 3478 ecs->event_thread->stepping_over_breakpoint = 1;
488f131b 3479 break;
c5aa993b 3480 }
488f131b 3481 }
488f131b 3482 break;
c906108c 3483
488f131b
JB
3484 case BPSTAT_WHAT_LAST:
3485 /* Not a real code, but listed here to shut up gcc -Wall. */
c906108c 3486
488f131b
JB
3487 case BPSTAT_WHAT_KEEP_CHECKING:
3488 break;
3489 }
3490 }
c906108c 3491
488f131b
JB
3492 /* We come here if we hit a breakpoint but should not
3493 stop for it. Possibly we also were stepping
3494 and should stop for that. So fall through and
3495 test for stepping. But, if not stepping,
3496 do not stop. */
c906108c 3497
a7212384
UW
3498 /* In all-stop mode, if we're currently stepping but have stopped in
3499 some other thread, we need to switch back to the stepped thread. */
3500 if (!non_stop)
3501 {
3502 struct thread_info *tp;
b3444185 3503 tp = iterate_over_threads (currently_stepping_or_nexting_callback,
a7212384
UW
3504 ecs->event_thread);
3505 if (tp)
3506 {
3507 /* However, if the current thread is blocked on some internal
3508 breakpoint, and we simply need to step over that breakpoint
3509 to get it going again, do that first. */
3510 if ((ecs->event_thread->trap_expected
3511 && ecs->event_thread->stop_signal != TARGET_SIGNAL_TRAP)
3512 || ecs->event_thread->stepping_over_breakpoint)
3513 {
3514 keep_going (ecs);
3515 return;
3516 }
3517
b3444185
PA
3518 /* If the stepping thread exited, then don't try reverting
3519 back to it, just keep going. We need to query the target
3520 in case it doesn't support thread exit events. */
3521 if (is_exited (tp->ptid)
3522 || !target_thread_alive (tp->ptid))
3523 {
3524 if (debug_infrun)
3525 fprintf_unfiltered (gdb_stdlog, "\
3526infrun: not switching back to stepped thread, it has vanished\n");
3527
3528 delete_thread (tp->ptid);
3529 keep_going (ecs);
3530 return;
3531 }
3532
a7212384
UW
3533 /* Otherwise, we no longer expect a trap in the current thread.
3534 Clear the trap_expected flag before switching back -- this is
3535 what keep_going would do as well, if we called it. */
3536 ecs->event_thread->trap_expected = 0;
3537
3538 if (debug_infrun)
3539 fprintf_unfiltered (gdb_stdlog,
3540 "infrun: switching back to stepped thread\n");
3541
3542 ecs->event_thread = tp;
3543 ecs->ptid = tp->ptid;
3544 context_switch (ecs->ptid);
3545 keep_going (ecs);
3546 return;
3547 }
3548 }
3549
9d1ff73f
MS
3550 /* Are we stepping to get the inferior out of the dynamic linker's
3551 hook (and possibly the dld itself) after catching a shlib
3552 event? */
4e1c45ea 3553 if (ecs->event_thread->stepping_through_solib_after_catch)
488f131b
JB
3554 {
3555#if defined(SOLIB_ADD)
3556 /* Have we reached our destination? If not, keep going. */
3557 if (SOLIB_IN_DYNAMIC_LINKER (PIDGET (ecs->ptid), stop_pc))
3558 {
527159b7 3559 if (debug_infrun)
8a9de0e4 3560 fprintf_unfiltered (gdb_stdlog, "infrun: stepping in dynamic linker\n");
4e1c45ea 3561 ecs->event_thread->stepping_over_breakpoint = 1;
488f131b 3562 keep_going (ecs);
104c1213 3563 return;
488f131b
JB
3564 }
3565#endif
527159b7 3566 if (debug_infrun)
8a9de0e4 3567 fprintf_unfiltered (gdb_stdlog, "infrun: step past dynamic linker\n");
488f131b
JB
3568 /* Else, stop and report the catchpoint(s) whose triggering
3569 caused us to begin stepping. */
4e1c45ea 3570 ecs->event_thread->stepping_through_solib_after_catch = 0;
347bddb7
PA
3571 bpstat_clear (&ecs->event_thread->stop_bpstat);
3572 ecs->event_thread->stop_bpstat
3573 = bpstat_copy (ecs->event_thread->stepping_through_solib_catchpoints);
4e1c45ea 3574 bpstat_clear (&ecs->event_thread->stepping_through_solib_catchpoints);
488f131b
JB
3575 stop_print_frame = 1;
3576 stop_stepping (ecs);
3577 return;
3578 }
c906108c 3579
4e1c45ea 3580 if (ecs->event_thread->step_resume_breakpoint)
488f131b 3581 {
527159b7 3582 if (debug_infrun)
d3169d93
DJ
3583 fprintf_unfiltered (gdb_stdlog,
3584 "infrun: step-resume breakpoint is inserted\n");
527159b7 3585
488f131b
JB
3586 /* Having a step-resume breakpoint overrides anything
3587 else having to do with stepping commands until
3588 that breakpoint is reached. */
488f131b
JB
3589 keep_going (ecs);
3590 return;
3591 }
c5aa993b 3592
4e1c45ea 3593 if (ecs->event_thread->step_range_end == 0)
488f131b 3594 {
527159b7 3595 if (debug_infrun)
8a9de0e4 3596 fprintf_unfiltered (gdb_stdlog, "infrun: no stepping, continue\n");
488f131b 3597 /* Likewise if we aren't even stepping. */
488f131b
JB
3598 keep_going (ecs);
3599 return;
3600 }
c5aa993b 3601
488f131b 3602 /* If stepping through a line, keep going if still within it.
c906108c 3603
488f131b
JB
3604 Note that step_range_end is the address of the first instruction
3605 beyond the step range, and NOT the address of the last instruction
3606 within it! */
4e1c45ea
PA
3607 if (stop_pc >= ecs->event_thread->step_range_start
3608 && stop_pc < ecs->event_thread->step_range_end)
488f131b 3609 {
527159b7 3610 if (debug_infrun)
b2175913 3611 fprintf_unfiltered (gdb_stdlog, "infrun: stepping inside range [0x%s-0x%s]\n",
4e1c45ea
PA
3612 paddr_nz (ecs->event_thread->step_range_start),
3613 paddr_nz (ecs->event_thread->step_range_end));
b2175913
MS
3614
3615 /* When stepping backward, stop at beginning of line range
3616 (unless it's the function entry point, in which case
3617 keep going back to the call point). */
3618 if (stop_pc == ecs->event_thread->step_range_start
3619 && stop_pc != ecs->stop_func_start
3620 && execution_direction == EXEC_REVERSE)
3621 {
3622 ecs->event_thread->stop_step = 1;
3623 print_stop_reason (END_STEPPING_RANGE, 0);
3624 stop_stepping (ecs);
3625 }
3626 else
3627 keep_going (ecs);
3628
488f131b
JB
3629 return;
3630 }
c5aa993b 3631
488f131b 3632 /* We stepped out of the stepping range. */
c906108c 3633
488f131b
JB
3634 /* If we are stepping at the source level and entered the runtime
3635 loader dynamic symbol resolution code, we keep on single stepping
3636 until we exit the run time loader code and reach the callee's
3637 address. */
078130d0 3638 if (ecs->event_thread->step_over_calls == STEP_OVER_UNDEBUGGABLE
cfd8ab24 3639 && in_solib_dynsym_resolve_code (stop_pc))
488f131b 3640 {
4c8c40e6
MK
3641 CORE_ADDR pc_after_resolver =
3642 gdbarch_skip_solib_resolver (current_gdbarch, stop_pc);
c906108c 3643
527159b7 3644 if (debug_infrun)
8a9de0e4 3645 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into dynsym resolve code\n");
527159b7 3646
488f131b
JB
3647 if (pc_after_resolver)
3648 {
3649 /* Set up a step-resume breakpoint at the address
3650 indicated by SKIP_SOLIB_RESOLVER. */
3651 struct symtab_and_line sr_sal;
fe39c653 3652 init_sal (&sr_sal);
488f131b
JB
3653 sr_sal.pc = pc_after_resolver;
3654
44cbf7b5 3655 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
c5aa993b 3656 }
c906108c 3657
488f131b
JB
3658 keep_going (ecs);
3659 return;
3660 }
c906108c 3661
4e1c45ea 3662 if (ecs->event_thread->step_range_end != 1
078130d0
PA
3663 && (ecs->event_thread->step_over_calls == STEP_OVER_UNDEBUGGABLE
3664 || ecs->event_thread->step_over_calls == STEP_OVER_ALL)
42edda50 3665 && get_frame_type (get_current_frame ()) == SIGTRAMP_FRAME)
488f131b 3666 {
527159b7 3667 if (debug_infrun)
8a9de0e4 3668 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into signal trampoline\n");
42edda50 3669 /* The inferior, while doing a "step" or "next", has ended up in
8fb3e588
AC
3670 a signal trampoline (either by a signal being delivered or by
3671 the signal handler returning). Just single-step until the
3672 inferior leaves the trampoline (either by calling the handler
3673 or returning). */
488f131b
JB
3674 keep_going (ecs);
3675 return;
3676 }
c906108c 3677
c17eaafe
DJ
3678 /* Check for subroutine calls. The check for the current frame
3679 equalling the step ID is not necessary - the check of the
3680 previous frame's ID is sufficient - but it is a common case and
3681 cheaper than checking the previous frame's ID.
14e60db5
DJ
3682
3683 NOTE: frame_id_eq will never report two invalid frame IDs as
3684 being equal, so to get into this block, both the current and
3685 previous frame must have valid frame IDs. */
4e1c45ea
PA
3686 if (!frame_id_eq (get_frame_id (get_current_frame ()),
3687 ecs->event_thread->step_frame_id)
b2175913
MS
3688 && (frame_id_eq (frame_unwind_id (get_current_frame ()),
3689 ecs->event_thread->step_frame_id)
3690 || execution_direction == EXEC_REVERSE))
488f131b 3691 {
95918acb 3692 CORE_ADDR real_stop_pc;
8fb3e588 3693
527159b7 3694 if (debug_infrun)
8a9de0e4 3695 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into subroutine\n");
527159b7 3696
078130d0 3697 if ((ecs->event_thread->step_over_calls == STEP_OVER_NONE)
4e1c45ea
PA
3698 || ((ecs->event_thread->step_range_end == 1)
3699 && in_prologue (ecs->event_thread->prev_pc,
3700 ecs->stop_func_start)))
95918acb
AC
3701 {
3702 /* I presume that step_over_calls is only 0 when we're
3703 supposed to be stepping at the assembly language level
3704 ("stepi"). Just stop. */
3705 /* Also, maybe we just did a "nexti" inside a prolog, so we
3706 thought it was a subroutine call but it was not. Stop as
3707 well. FENN */
414c69f7 3708 ecs->event_thread->stop_step = 1;
95918acb
AC
3709 print_stop_reason (END_STEPPING_RANGE, 0);
3710 stop_stepping (ecs);
3711 return;
3712 }
8fb3e588 3713
078130d0 3714 if (ecs->event_thread->step_over_calls == STEP_OVER_ALL)
8567c30f 3715 {
b2175913
MS
3716 /* We're doing a "next".
3717
3718 Normal (forward) execution: set a breakpoint at the
3719 callee's return address (the address at which the caller
3720 will resume).
3721
3722 Reverse (backward) execution. set the step-resume
3723 breakpoint at the start of the function that we just
3724 stepped into (backwards), and continue to there. When we
6130d0b7 3725 get there, we'll need to single-step back to the caller. */
b2175913
MS
3726
3727 if (execution_direction == EXEC_REVERSE)
3728 {
3729 struct symtab_and_line sr_sal;
3067f6e5
MS
3730
3731 if (ecs->stop_func_start == 0
3732 && in_solib_dynsym_resolve_code (stop_pc))
3733 {
3734 /* Stepped into runtime loader dynamic symbol
3735 resolution code. Since we're in reverse,
3736 we have already backed up through the runtime
3737 loader and the dynamic function. This is just
3738 the trampoline (jump table).
3739
3740 Just keep stepping, we'll soon be home.
3741 */
3742 keep_going (ecs);
3743 return;
3744 }
3745 /* Normal (staticly linked) function call return. */
b2175913
MS
3746 init_sal (&sr_sal);
3747 sr_sal.pc = ecs->stop_func_start;
3748 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
3749 }
3750 else
3751 insert_step_resume_breakpoint_at_caller (get_current_frame ());
3752
8567c30f
AC
3753 keep_going (ecs);
3754 return;
3755 }
a53c66de 3756
95918acb 3757 /* If we are in a function call trampoline (a stub between the
8fb3e588
AC
3758 calling routine and the real function), locate the real
3759 function. That's what tells us (a) whether we want to step
3760 into it at all, and (b) what prologue we want to run to the
3761 end of, if we do step into it. */
52f729a7 3762 real_stop_pc = skip_language_trampoline (get_current_frame (), stop_pc);
95918acb 3763 if (real_stop_pc == 0)
52f729a7
UW
3764 real_stop_pc = gdbarch_skip_trampoline_code
3765 (current_gdbarch, get_current_frame (), stop_pc);
95918acb
AC
3766 if (real_stop_pc != 0)
3767 ecs->stop_func_start = real_stop_pc;
8fb3e588 3768
db5f024e 3769 if (real_stop_pc != 0 && in_solib_dynsym_resolve_code (real_stop_pc))
1b2bfbb9
RC
3770 {
3771 struct symtab_and_line sr_sal;
3772 init_sal (&sr_sal);
3773 sr_sal.pc = ecs->stop_func_start;
3774
44cbf7b5 3775 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
8fb3e588
AC
3776 keep_going (ecs);
3777 return;
1b2bfbb9
RC
3778 }
3779
95918acb 3780 /* If we have line number information for the function we are
8fb3e588 3781 thinking of stepping into, step into it.
95918acb 3782
8fb3e588
AC
3783 If there are several symtabs at that PC (e.g. with include
3784 files), just want to know whether *any* of them have line
3785 numbers. find_pc_line handles this. */
95918acb
AC
3786 {
3787 struct symtab_and_line tmp_sal;
8fb3e588 3788
95918acb
AC
3789 tmp_sal = find_pc_line (ecs->stop_func_start, 0);
3790 if (tmp_sal.line != 0)
3791 {
b2175913
MS
3792 if (execution_direction == EXEC_REVERSE)
3793 handle_step_into_function_backward (ecs);
3794 else
3795 handle_step_into_function (ecs);
95918acb
AC
3796 return;
3797 }
3798 }
3799
3800 /* If we have no line number and the step-stop-if-no-debug is
8fb3e588
AC
3801 set, we stop the step so that the user has a chance to switch
3802 in assembly mode. */
078130d0
PA
3803 if (ecs->event_thread->step_over_calls == STEP_OVER_UNDEBUGGABLE
3804 && step_stop_if_no_debug)
95918acb 3805 {
414c69f7 3806 ecs->event_thread->stop_step = 1;
95918acb
AC
3807 print_stop_reason (END_STEPPING_RANGE, 0);
3808 stop_stepping (ecs);
3809 return;
3810 }
3811
b2175913
MS
3812 if (execution_direction == EXEC_REVERSE)
3813 {
3814 /* Set a breakpoint at callee's start address.
3815 From there we can step once and be back in the caller. */
3816 struct symtab_and_line sr_sal;
3817 init_sal (&sr_sal);
3818 sr_sal.pc = ecs->stop_func_start;
3819 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
3820 }
3821 else
3822 /* Set a breakpoint at callee's return address (the address
3823 at which the caller will resume). */
3824 insert_step_resume_breakpoint_at_caller (get_current_frame ());
3825
95918acb 3826 keep_going (ecs);
488f131b 3827 return;
488f131b 3828 }
c906108c 3829
488f131b
JB
3830 /* If we're in the return path from a shared library trampoline,
3831 we want to proceed through the trampoline when stepping. */
e76f05fa
UW
3832 if (gdbarch_in_solib_return_trampoline (current_gdbarch,
3833 stop_pc, ecs->stop_func_name))
488f131b 3834 {
488f131b 3835 /* Determine where this trampoline returns. */
52f729a7
UW
3836 CORE_ADDR real_stop_pc;
3837 real_stop_pc = gdbarch_skip_trampoline_code
3838 (current_gdbarch, get_current_frame (), stop_pc);
c906108c 3839
527159b7 3840 if (debug_infrun)
8a9de0e4 3841 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into solib return tramp\n");
527159b7 3842
488f131b 3843 /* Only proceed through if we know where it's going. */
d764a824 3844 if (real_stop_pc)
488f131b
JB
3845 {
3846 /* And put the step-breakpoint there and go until there. */
3847 struct symtab_and_line sr_sal;
3848
fe39c653 3849 init_sal (&sr_sal); /* initialize to zeroes */
d764a824 3850 sr_sal.pc = real_stop_pc;
488f131b 3851 sr_sal.section = find_pc_overlay (sr_sal.pc);
44cbf7b5
AC
3852
3853 /* Do not specify what the fp should be when we stop since
3854 on some machines the prologue is where the new fp value
3855 is established. */
3856 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
c906108c 3857
488f131b
JB
3858 /* Restart without fiddling with the step ranges or
3859 other state. */
3860 keep_going (ecs);
3861 return;
3862 }
3863 }
c906108c 3864
2afb61aa 3865 stop_pc_sal = find_pc_line (stop_pc, 0);
7ed0fe66 3866
1b2bfbb9
RC
3867 /* NOTE: tausq/2004-05-24: This if block used to be done before all
3868 the trampoline processing logic, however, there are some trampolines
3869 that have no names, so we should do trampoline handling first. */
078130d0 3870 if (ecs->event_thread->step_over_calls == STEP_OVER_UNDEBUGGABLE
7ed0fe66 3871 && ecs->stop_func_name == NULL
2afb61aa 3872 && stop_pc_sal.line == 0)
1b2bfbb9 3873 {
527159b7 3874 if (debug_infrun)
8a9de0e4 3875 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into undebuggable function\n");
527159b7 3876
1b2bfbb9 3877 /* The inferior just stepped into, or returned to, an
7ed0fe66
DJ
3878 undebuggable function (where there is no debugging information
3879 and no line number corresponding to the address where the
1b2bfbb9
RC
3880 inferior stopped). Since we want to skip this kind of code,
3881 we keep going until the inferior returns from this
14e60db5
DJ
3882 function - unless the user has asked us not to (via
3883 set step-mode) or we no longer know how to get back
3884 to the call site. */
3885 if (step_stop_if_no_debug
eb2f4a08 3886 || !frame_id_p (frame_unwind_id (get_current_frame ())))
1b2bfbb9
RC
3887 {
3888 /* If we have no line number and the step-stop-if-no-debug
3889 is set, we stop the step so that the user has a chance to
3890 switch in assembly mode. */
414c69f7 3891 ecs->event_thread->stop_step = 1;
1b2bfbb9
RC
3892 print_stop_reason (END_STEPPING_RANGE, 0);
3893 stop_stepping (ecs);
3894 return;
3895 }
3896 else
3897 {
3898 /* Set a breakpoint at callee's return address (the address
3899 at which the caller will resume). */
14e60db5 3900 insert_step_resume_breakpoint_at_caller (get_current_frame ());
1b2bfbb9
RC
3901 keep_going (ecs);
3902 return;
3903 }
3904 }
3905
4e1c45ea 3906 if (ecs->event_thread->step_range_end == 1)
1b2bfbb9
RC
3907 {
3908 /* It is stepi or nexti. We always want to stop stepping after
3909 one instruction. */
527159b7 3910 if (debug_infrun)
8a9de0e4 3911 fprintf_unfiltered (gdb_stdlog, "infrun: stepi/nexti\n");
414c69f7 3912 ecs->event_thread->stop_step = 1;
1b2bfbb9
RC
3913 print_stop_reason (END_STEPPING_RANGE, 0);
3914 stop_stepping (ecs);
3915 return;
3916 }
3917
2afb61aa 3918 if (stop_pc_sal.line == 0)
488f131b
JB
3919 {
3920 /* We have no line number information. That means to stop
3921 stepping (does this always happen right after one instruction,
3922 when we do "s" in a function with no line numbers,
3923 or can this happen as a result of a return or longjmp?). */
527159b7 3924 if (debug_infrun)
8a9de0e4 3925 fprintf_unfiltered (gdb_stdlog, "infrun: no line number info\n");
414c69f7 3926 ecs->event_thread->stop_step = 1;
488f131b
JB
3927 print_stop_reason (END_STEPPING_RANGE, 0);
3928 stop_stepping (ecs);
3929 return;
3930 }
c906108c 3931
2afb61aa 3932 if ((stop_pc == stop_pc_sal.pc)
4e1c45ea
PA
3933 && (ecs->event_thread->current_line != stop_pc_sal.line
3934 || ecs->event_thread->current_symtab != stop_pc_sal.symtab))
488f131b
JB
3935 {
3936 /* We are at the start of a different line. So stop. Note that
3937 we don't stop if we step into the middle of a different line.
3938 That is said to make things like for (;;) statements work
3939 better. */
527159b7 3940 if (debug_infrun)
8a9de0e4 3941 fprintf_unfiltered (gdb_stdlog, "infrun: stepped to a different line\n");
414c69f7 3942 ecs->event_thread->stop_step = 1;
488f131b
JB
3943 print_stop_reason (END_STEPPING_RANGE, 0);
3944 stop_stepping (ecs);
3945 return;
3946 }
c906108c 3947
488f131b 3948 /* We aren't done stepping.
c906108c 3949
488f131b
JB
3950 Optimize by setting the stepping range to the line.
3951 (We might not be in the original line, but if we entered a
3952 new line in mid-statement, we continue stepping. This makes
3953 things like for(;;) statements work better.) */
c906108c 3954
4e1c45ea
PA
3955 ecs->event_thread->step_range_start = stop_pc_sal.pc;
3956 ecs->event_thread->step_range_end = stop_pc_sal.end;
3957 ecs->event_thread->step_frame_id = get_frame_id (get_current_frame ());
3958 ecs->event_thread->current_line = stop_pc_sal.line;
3959 ecs->event_thread->current_symtab = stop_pc_sal.symtab;
488f131b 3960
527159b7 3961 if (debug_infrun)
8a9de0e4 3962 fprintf_unfiltered (gdb_stdlog, "infrun: keep going\n");
488f131b 3963 keep_going (ecs);
104c1213
JM
3964}
3965
b3444185 3966/* Is thread TP in the middle of single-stepping? */
104c1213 3967
a7212384 3968static int
b3444185 3969currently_stepping (struct thread_info *tp)
a7212384 3970{
b3444185
PA
3971 return ((tp->step_range_end && tp->step_resume_breakpoint == NULL)
3972 || tp->trap_expected
3973 || tp->stepping_through_solib_after_catch
3974 || bpstat_should_step ());
a7212384
UW
3975}
3976
b3444185
PA
3977/* Returns true if any thread *but* the one passed in "data" is in the
3978 middle of stepping or of handling a "next". */
a7212384 3979
104c1213 3980static int
b3444185 3981currently_stepping_or_nexting_callback (struct thread_info *tp, void *data)
104c1213 3982{
b3444185
PA
3983 if (tp == data)
3984 return 0;
3985
3986 return (tp->step_range_end
3987 || tp->trap_expected
3988 || tp->stepping_through_solib_after_catch);
104c1213 3989}
c906108c 3990
b2175913
MS
3991/* Inferior has stepped into a subroutine call with source code that
3992 we should not step over. Do step to the first line of code in
3993 it. */
c2c6d25f
JM
3994
3995static void
b2175913 3996handle_step_into_function (struct execution_control_state *ecs)
c2c6d25f
JM
3997{
3998 struct symtab *s;
2afb61aa 3999 struct symtab_and_line stop_func_sal, sr_sal;
c2c6d25f
JM
4000
4001 s = find_pc_symtab (stop_pc);
4002 if (s && s->language != language_asm)
b2175913
MS
4003 ecs->stop_func_start = gdbarch_skip_prologue (current_gdbarch,
4004 ecs->stop_func_start);
c2c6d25f 4005
2afb61aa 4006 stop_func_sal = find_pc_line (ecs->stop_func_start, 0);
c2c6d25f
JM
4007 /* Use the step_resume_break to step until the end of the prologue,
4008 even if that involves jumps (as it seems to on the vax under
4009 4.2). */
4010 /* If the prologue ends in the middle of a source line, continue to
4011 the end of that source line (if it is still within the function).
4012 Otherwise, just go to end of prologue. */
2afb61aa
PA
4013 if (stop_func_sal.end
4014 && stop_func_sal.pc != ecs->stop_func_start
4015 && stop_func_sal.end < ecs->stop_func_end)
4016 ecs->stop_func_start = stop_func_sal.end;
c2c6d25f 4017
2dbd5e30
KB
4018 /* Architectures which require breakpoint adjustment might not be able
4019 to place a breakpoint at the computed address. If so, the test
4020 ``ecs->stop_func_start == stop_pc'' will never succeed. Adjust
4021 ecs->stop_func_start to an address at which a breakpoint may be
4022 legitimately placed.
8fb3e588 4023
2dbd5e30
KB
4024 Note: kevinb/2004-01-19: On FR-V, if this adjustment is not
4025 made, GDB will enter an infinite loop when stepping through
4026 optimized code consisting of VLIW instructions which contain
4027 subinstructions corresponding to different source lines. On
4028 FR-V, it's not permitted to place a breakpoint on any but the
4029 first subinstruction of a VLIW instruction. When a breakpoint is
4030 set, GDB will adjust the breakpoint address to the beginning of
4031 the VLIW instruction. Thus, we need to make the corresponding
4032 adjustment here when computing the stop address. */
8fb3e588 4033
2dbd5e30
KB
4034 if (gdbarch_adjust_breakpoint_address_p (current_gdbarch))
4035 {
4036 ecs->stop_func_start
4037 = gdbarch_adjust_breakpoint_address (current_gdbarch,
8fb3e588 4038 ecs->stop_func_start);
2dbd5e30
KB
4039 }
4040
c2c6d25f
JM
4041 if (ecs->stop_func_start == stop_pc)
4042 {
4043 /* We are already there: stop now. */
414c69f7 4044 ecs->event_thread->stop_step = 1;
488f131b 4045 print_stop_reason (END_STEPPING_RANGE, 0);
c2c6d25f
JM
4046 stop_stepping (ecs);
4047 return;
4048 }
4049 else
4050 {
4051 /* Put the step-breakpoint there and go until there. */
fe39c653 4052 init_sal (&sr_sal); /* initialize to zeroes */
c2c6d25f
JM
4053 sr_sal.pc = ecs->stop_func_start;
4054 sr_sal.section = find_pc_overlay (ecs->stop_func_start);
44cbf7b5 4055
c2c6d25f 4056 /* Do not specify what the fp should be when we stop since on
488f131b
JB
4057 some machines the prologue is where the new fp value is
4058 established. */
44cbf7b5 4059 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
c2c6d25f
JM
4060
4061 /* And make sure stepping stops right away then. */
4e1c45ea 4062 ecs->event_thread->step_range_end = ecs->event_thread->step_range_start;
c2c6d25f
JM
4063 }
4064 keep_going (ecs);
4065}
d4f3574e 4066
b2175913
MS
4067/* Inferior has stepped backward into a subroutine call with source
4068 code that we should not step over. Do step to the beginning of the
4069 last line of code in it. */
4070
4071static void
4072handle_step_into_function_backward (struct execution_control_state *ecs)
4073{
4074 struct symtab *s;
4075 struct symtab_and_line stop_func_sal, sr_sal;
4076
4077 s = find_pc_symtab (stop_pc);
4078 if (s && s->language != language_asm)
4079 ecs->stop_func_start = gdbarch_skip_prologue (current_gdbarch,
4080 ecs->stop_func_start);
4081
4082 stop_func_sal = find_pc_line (stop_pc, 0);
4083
4084 /* OK, we're just going to keep stepping here. */
4085 if (stop_func_sal.pc == stop_pc)
4086 {
4087 /* We're there already. Just stop stepping now. */
4088 ecs->event_thread->stop_step = 1;
4089 print_stop_reason (END_STEPPING_RANGE, 0);
4090 stop_stepping (ecs);
4091 }
4092 else
4093 {
4094 /* Else just reset the step range and keep going.
4095 No step-resume breakpoint, they don't work for
4096 epilogues, which can have multiple entry paths. */
4097 ecs->event_thread->step_range_start = stop_func_sal.pc;
4098 ecs->event_thread->step_range_end = stop_func_sal.end;
4099 keep_going (ecs);
4100 }
4101 return;
4102}
4103
d3169d93 4104/* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
44cbf7b5
AC
4105 This is used to both functions and to skip over code. */
4106
4107static void
4108insert_step_resume_breakpoint_at_sal (struct symtab_and_line sr_sal,
4109 struct frame_id sr_id)
4110{
611c83ae
PA
4111 /* There should never be more than one step-resume or longjmp-resume
4112 breakpoint per thread, so we should never be setting a new
44cbf7b5 4113 step_resume_breakpoint when one is already active. */
4e1c45ea 4114 gdb_assert (inferior_thread ()->step_resume_breakpoint == NULL);
d3169d93
DJ
4115
4116 if (debug_infrun)
4117 fprintf_unfiltered (gdb_stdlog,
4118 "infrun: inserting step-resume breakpoint at 0x%s\n",
4119 paddr_nz (sr_sal.pc));
4120
4e1c45ea
PA
4121 inferior_thread ()->step_resume_breakpoint
4122 = set_momentary_breakpoint (sr_sal, sr_id, bp_step_resume);
44cbf7b5 4123}
7ce450bd 4124
d3169d93 4125/* Insert a "step-resume breakpoint" at RETURN_FRAME.pc. This is used
14e60db5 4126 to skip a potential signal handler.
7ce450bd 4127
14e60db5
DJ
4128 This is called with the interrupted function's frame. The signal
4129 handler, when it returns, will resume the interrupted function at
4130 RETURN_FRAME.pc. */
d303a6c7
AC
4131
4132static void
44cbf7b5 4133insert_step_resume_breakpoint_at_frame (struct frame_info *return_frame)
d303a6c7
AC
4134{
4135 struct symtab_and_line sr_sal;
4136
f4c1edd8 4137 gdb_assert (return_frame != NULL);
d303a6c7
AC
4138 init_sal (&sr_sal); /* initialize to zeros */
4139
bf6ae464
UW
4140 sr_sal.pc = gdbarch_addr_bits_remove
4141 (current_gdbarch, get_frame_pc (return_frame));
d303a6c7
AC
4142 sr_sal.section = find_pc_overlay (sr_sal.pc);
4143
44cbf7b5 4144 insert_step_resume_breakpoint_at_sal (sr_sal, get_frame_id (return_frame));
d303a6c7
AC
4145}
4146
14e60db5
DJ
4147/* Similar to insert_step_resume_breakpoint_at_frame, except
4148 but a breakpoint at the previous frame's PC. This is used to
4149 skip a function after stepping into it (for "next" or if the called
4150 function has no debugging information).
4151
4152 The current function has almost always been reached by single
4153 stepping a call or return instruction. NEXT_FRAME belongs to the
4154 current function, and the breakpoint will be set at the caller's
4155 resume address.
4156
4157 This is a separate function rather than reusing
4158 insert_step_resume_breakpoint_at_frame in order to avoid
4159 get_prev_frame, which may stop prematurely (see the implementation
eb2f4a08 4160 of frame_unwind_id for an example). */
14e60db5
DJ
4161
4162static void
4163insert_step_resume_breakpoint_at_caller (struct frame_info *next_frame)
4164{
4165 struct symtab_and_line sr_sal;
4166
4167 /* We shouldn't have gotten here if we don't know where the call site
4168 is. */
eb2f4a08 4169 gdb_assert (frame_id_p (frame_unwind_id (next_frame)));
14e60db5
DJ
4170
4171 init_sal (&sr_sal); /* initialize to zeros */
4172
bf6ae464 4173 sr_sal.pc = gdbarch_addr_bits_remove
eb2f4a08 4174 (current_gdbarch, frame_pc_unwind (next_frame));
14e60db5
DJ
4175 sr_sal.section = find_pc_overlay (sr_sal.pc);
4176
eb2f4a08 4177 insert_step_resume_breakpoint_at_sal (sr_sal, frame_unwind_id (next_frame));
14e60db5
DJ
4178}
4179
611c83ae
PA
4180/* Insert a "longjmp-resume" breakpoint at PC. This is used to set a
4181 new breakpoint at the target of a jmp_buf. The handling of
4182 longjmp-resume uses the same mechanisms used for handling
4183 "step-resume" breakpoints. */
4184
4185static void
4186insert_longjmp_resume_breakpoint (CORE_ADDR pc)
4187{
4188 /* There should never be more than one step-resume or longjmp-resume
4189 breakpoint per thread, so we should never be setting a new
4190 longjmp_resume_breakpoint when one is already active. */
4e1c45ea 4191 gdb_assert (inferior_thread ()->step_resume_breakpoint == NULL);
611c83ae
PA
4192
4193 if (debug_infrun)
4194 fprintf_unfiltered (gdb_stdlog,
4195 "infrun: inserting longjmp-resume breakpoint at 0x%s\n",
4196 paddr_nz (pc));
4197
4e1c45ea 4198 inferior_thread ()->step_resume_breakpoint =
611c83ae
PA
4199 set_momentary_breakpoint_at_pc (pc, bp_longjmp_resume);
4200}
4201
104c1213
JM
4202static void
4203stop_stepping (struct execution_control_state *ecs)
4204{
527159b7 4205 if (debug_infrun)
8a9de0e4 4206 fprintf_unfiltered (gdb_stdlog, "infrun: stop_stepping\n");
527159b7 4207
cd0fc7c3
SS
4208 /* Let callers know we don't want to wait for the inferior anymore. */
4209 ecs->wait_some_more = 0;
4210}
4211
d4f3574e
SS
4212/* This function handles various cases where we need to continue
4213 waiting for the inferior. */
4214/* (Used to be the keep_going: label in the old wait_for_inferior) */
4215
4216static void
4217keep_going (struct execution_control_state *ecs)
4218{
d4f3574e 4219 /* Save the pc before execution, to compare with pc after stop. */
fb14de7b
UW
4220 ecs->event_thread->prev_pc
4221 = regcache_read_pc (get_thread_regcache (ecs->ptid));
d4f3574e 4222
d4f3574e
SS
4223 /* If we did not do break;, it means we should keep running the
4224 inferior and not return to debugger. */
4225
2020b7ab
PA
4226 if (ecs->event_thread->trap_expected
4227 && ecs->event_thread->stop_signal != TARGET_SIGNAL_TRAP)
d4f3574e
SS
4228 {
4229 /* We took a signal (which we are supposed to pass through to
4e1c45ea
PA
4230 the inferior, else we'd not get here) and we haven't yet
4231 gotten our trap. Simply continue. */
2020b7ab
PA
4232 resume (currently_stepping (ecs->event_thread),
4233 ecs->event_thread->stop_signal);
d4f3574e
SS
4234 }
4235 else
4236 {
4237 /* Either the trap was not expected, but we are continuing
488f131b
JB
4238 anyway (the user asked that this signal be passed to the
4239 child)
4240 -- or --
4241 The signal was SIGTRAP, e.g. it was our signal, but we
4242 decided we should resume from it.
d4f3574e 4243
c36b740a 4244 We're going to run this baby now!
d4f3574e 4245
c36b740a
VP
4246 Note that insert_breakpoints won't try to re-insert
4247 already inserted breakpoints. Therefore, we don't
4248 care if breakpoints were already inserted, or not. */
4249
4e1c45ea 4250 if (ecs->event_thread->stepping_over_breakpoint)
45e8c884 4251 {
237fc4c9
PA
4252 if (! use_displaced_stepping (current_gdbarch))
4253 /* Since we can't do a displaced step, we have to remove
4254 the breakpoint while we step it. To keep things
4255 simple, we remove them all. */
4256 remove_breakpoints ();
45e8c884
VP
4257 }
4258 else
d4f3574e 4259 {
e236ba44 4260 struct gdb_exception e;
569631c6
UW
4261 /* Stop stepping when inserting breakpoints
4262 has failed. */
e236ba44
VP
4263 TRY_CATCH (e, RETURN_MASK_ERROR)
4264 {
4265 insert_breakpoints ();
4266 }
4267 if (e.reason < 0)
d4f3574e
SS
4268 {
4269 stop_stepping (ecs);
4270 return;
4271 }
d4f3574e
SS
4272 }
4273
4e1c45ea 4274 ecs->event_thread->trap_expected = ecs->event_thread->stepping_over_breakpoint;
d4f3574e
SS
4275
4276 /* Do not deliver SIGNAL_TRAP (except when the user explicitly
488f131b
JB
4277 specifies that such a signal should be delivered to the
4278 target program).
4279
4280 Typically, this would occure when a user is debugging a
4281 target monitor on a simulator: the target monitor sets a
4282 breakpoint; the simulator encounters this break-point and
4283 halts the simulation handing control to GDB; GDB, noteing
4284 that the break-point isn't valid, returns control back to the
4285 simulator; the simulator then delivers the hardware
4286 equivalent of a SIGNAL_TRAP to the program being debugged. */
4287
2020b7ab
PA
4288 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP
4289 && !signal_program[ecs->event_thread->stop_signal])
4290 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
d4f3574e 4291
2020b7ab
PA
4292 resume (currently_stepping (ecs->event_thread),
4293 ecs->event_thread->stop_signal);
d4f3574e
SS
4294 }
4295
488f131b 4296 prepare_to_wait (ecs);
d4f3574e
SS
4297}
4298
104c1213
JM
4299/* This function normally comes after a resume, before
4300 handle_inferior_event exits. It takes care of any last bits of
4301 housekeeping, and sets the all-important wait_some_more flag. */
cd0fc7c3 4302
104c1213
JM
4303static void
4304prepare_to_wait (struct execution_control_state *ecs)
cd0fc7c3 4305{
527159b7 4306 if (debug_infrun)
8a9de0e4 4307 fprintf_unfiltered (gdb_stdlog, "infrun: prepare_to_wait\n");
0d1e5fa7 4308 if (infwait_state == infwait_normal_state)
104c1213
JM
4309 {
4310 overlay_cache_invalid = 1;
4311
4312 /* We have to invalidate the registers BEFORE calling
488f131b
JB
4313 target_wait because they can be loaded from the target while
4314 in target_wait. This makes remote debugging a bit more
4315 efficient for those targets that provide critical registers
4316 as part of their normal status mechanism. */
104c1213
JM
4317
4318 registers_changed ();
0d1e5fa7 4319 waiton_ptid = pid_to_ptid (-1);
104c1213
JM
4320 }
4321 /* This is the old end of the while loop. Let everybody know we
4322 want to wait for the inferior some more and get called again
4323 soon. */
4324 ecs->wait_some_more = 1;
c906108c 4325}
11cf8741
JM
4326
4327/* Print why the inferior has stopped. We always print something when
4328 the inferior exits, or receives a signal. The rest of the cases are
4329 dealt with later on in normal_stop() and print_it_typical(). Ideally
4330 there should be a call to this function from handle_inferior_event()
4331 each time stop_stepping() is called.*/
4332static void
4333print_stop_reason (enum inferior_stop_reason stop_reason, int stop_info)
4334{
4335 switch (stop_reason)
4336 {
11cf8741
JM
4337 case END_STEPPING_RANGE:
4338 /* We are done with a step/next/si/ni command. */
4339 /* For now print nothing. */
fb40c209 4340 /* Print a message only if not in the middle of doing a "step n"
488f131b 4341 operation for n > 1 */
414c69f7
PA
4342 if (!inferior_thread ()->step_multi
4343 || !inferior_thread ()->stop_step)
9dc5e2a9 4344 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4345 ui_out_field_string
4346 (uiout, "reason",
4347 async_reason_lookup (EXEC_ASYNC_END_STEPPING_RANGE));
11cf8741 4348 break;
11cf8741
JM
4349 case SIGNAL_EXITED:
4350 /* The inferior was terminated by a signal. */
8b93c638 4351 annotate_signalled ();
9dc5e2a9 4352 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4353 ui_out_field_string
4354 (uiout, "reason",
4355 async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED));
8b93c638
JM
4356 ui_out_text (uiout, "\nProgram terminated with signal ");
4357 annotate_signal_name ();
488f131b
JB
4358 ui_out_field_string (uiout, "signal-name",
4359 target_signal_to_name (stop_info));
8b93c638
JM
4360 annotate_signal_name_end ();
4361 ui_out_text (uiout, ", ");
4362 annotate_signal_string ();
488f131b
JB
4363 ui_out_field_string (uiout, "signal-meaning",
4364 target_signal_to_string (stop_info));
8b93c638
JM
4365 annotate_signal_string_end ();
4366 ui_out_text (uiout, ".\n");
4367 ui_out_text (uiout, "The program no longer exists.\n");
11cf8741
JM
4368 break;
4369 case EXITED:
4370 /* The inferior program is finished. */
8b93c638
JM
4371 annotate_exited (stop_info);
4372 if (stop_info)
4373 {
9dc5e2a9 4374 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4375 ui_out_field_string (uiout, "reason",
4376 async_reason_lookup (EXEC_ASYNC_EXITED));
8b93c638 4377 ui_out_text (uiout, "\nProgram exited with code ");
488f131b
JB
4378 ui_out_field_fmt (uiout, "exit-code", "0%o",
4379 (unsigned int) stop_info);
8b93c638
JM
4380 ui_out_text (uiout, ".\n");
4381 }
4382 else
4383 {
9dc5e2a9 4384 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4385 ui_out_field_string
4386 (uiout, "reason",
4387 async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY));
8b93c638
JM
4388 ui_out_text (uiout, "\nProgram exited normally.\n");
4389 }
f17517ea
AS
4390 /* Support the --return-child-result option. */
4391 return_child_result_value = stop_info;
11cf8741
JM
4392 break;
4393 case SIGNAL_RECEIVED:
252fbfc8
PA
4394 /* Signal received. The signal table tells us to print about
4395 it. */
8b93c638 4396 annotate_signal ();
252fbfc8
PA
4397
4398 if (stop_info == TARGET_SIGNAL_0 && !ui_out_is_mi_like_p (uiout))
4399 {
4400 struct thread_info *t = inferior_thread ();
4401
4402 ui_out_text (uiout, "\n[");
4403 ui_out_field_string (uiout, "thread-name",
4404 target_pid_to_str (t->ptid));
4405 ui_out_field_fmt (uiout, "thread-id", "] #%d", t->num);
4406 ui_out_text (uiout, " stopped");
4407 }
4408 else
4409 {
4410 ui_out_text (uiout, "\nProgram received signal ");
4411 annotate_signal_name ();
4412 if (ui_out_is_mi_like_p (uiout))
4413 ui_out_field_string
4414 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED));
4415 ui_out_field_string (uiout, "signal-name",
4416 target_signal_to_name (stop_info));
4417 annotate_signal_name_end ();
4418 ui_out_text (uiout, ", ");
4419 annotate_signal_string ();
4420 ui_out_field_string (uiout, "signal-meaning",
4421 target_signal_to_string (stop_info));
4422 annotate_signal_string_end ();
4423 }
8b93c638 4424 ui_out_text (uiout, ".\n");
11cf8741 4425 break;
b2175913
MS
4426 case NO_HISTORY:
4427 /* Reverse execution: target ran out of history info. */
4428 ui_out_text (uiout, "\nNo more reverse-execution history.\n");
4429 break;
11cf8741 4430 default:
8e65ff28 4431 internal_error (__FILE__, __LINE__,
e2e0b3e5 4432 _("print_stop_reason: unrecognized enum value"));
11cf8741
JM
4433 break;
4434 }
4435}
c906108c 4436\f
43ff13b4 4437
c906108c
SS
4438/* Here to return control to GDB when the inferior stops for real.
4439 Print appropriate messages, remove breakpoints, give terminal our modes.
4440
4441 STOP_PRINT_FRAME nonzero means print the executing frame
4442 (pc, function, args, file, line number and line text).
4443 BREAKPOINTS_FAILED nonzero means stop was due to error
4444 attempting to insert breakpoints. */
4445
4446void
96baa820 4447normal_stop (void)
c906108c 4448{
73b65bb0
DJ
4449 struct target_waitstatus last;
4450 ptid_t last_ptid;
29f49a6a 4451 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
73b65bb0
DJ
4452
4453 get_last_target_status (&last_ptid, &last);
4454
29f49a6a
PA
4455 /* If an exception is thrown from this point on, make sure to
4456 propagate GDB's knowledge of the executing state to the
4457 frontend/user running state. A QUIT is an easy exception to see
4458 here, so do this before any filtered output. */
4459 if (target_has_execution)
4460 {
4461 if (!non_stop)
fee0be5d 4462 make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
29f49a6a
PA
4463 else if (last.kind != TARGET_WAITKIND_SIGNALLED
4464 && last.kind != TARGET_WAITKIND_EXITED)
fee0be5d 4465 make_cleanup (finish_thread_state_cleanup, &inferior_ptid);
29f49a6a
PA
4466 }
4467
4f8d22e3
PA
4468 /* In non-stop mode, we don't want GDB to switch threads behind the
4469 user's back, to avoid races where the user is typing a command to
4470 apply to thread x, but GDB switches to thread y before the user
4471 finishes entering the command. */
4472
c906108c
SS
4473 /* As with the notification of thread events, we want to delay
4474 notifying the user that we've switched thread context until
4475 the inferior actually stops.
4476
73b65bb0
DJ
4477 There's no point in saying anything if the inferior has exited.
4478 Note that SIGNALLED here means "exited with a signal", not
4479 "received a signal". */
4f8d22e3
PA
4480 if (!non_stop
4481 && !ptid_equal (previous_inferior_ptid, inferior_ptid)
73b65bb0
DJ
4482 && target_has_execution
4483 && last.kind != TARGET_WAITKIND_SIGNALLED
4484 && last.kind != TARGET_WAITKIND_EXITED)
c906108c
SS
4485 {
4486 target_terminal_ours_for_output ();
a3f17187 4487 printf_filtered (_("[Switching to %s]\n"),
c95310c6 4488 target_pid_to_str (inferior_ptid));
b8fa951a 4489 annotate_thread_changed ();
39f77062 4490 previous_inferior_ptid = inferior_ptid;
c906108c 4491 }
c906108c 4492
74960c60 4493 if (!breakpoints_always_inserted_mode () && target_has_execution)
c906108c
SS
4494 {
4495 if (remove_breakpoints ())
4496 {
4497 target_terminal_ours_for_output ();
a3f17187
AC
4498 printf_filtered (_("\
4499Cannot remove breakpoints because program is no longer writable.\n\
a3f17187 4500Further execution is probably impossible.\n"));
c906108c
SS
4501 }
4502 }
c906108c 4503
c906108c
SS
4504 /* If an auto-display called a function and that got a signal,
4505 delete that auto-display to avoid an infinite recursion. */
4506
4507 if (stopped_by_random_signal)
4508 disable_current_display ();
4509
4510 /* Don't print a message if in the middle of doing a "step n"
4511 operation for n > 1 */
af679fd0
PA
4512 if (target_has_execution
4513 && last.kind != TARGET_WAITKIND_SIGNALLED
4514 && last.kind != TARGET_WAITKIND_EXITED
4515 && inferior_thread ()->step_multi
414c69f7 4516 && inferior_thread ()->stop_step)
c906108c
SS
4517 goto done;
4518
4519 target_terminal_ours ();
4520
7abfe014
DJ
4521 /* Set the current source location. This will also happen if we
4522 display the frame below, but the current SAL will be incorrect
4523 during a user hook-stop function. */
d729566a 4524 if (has_stack_frames () && !stop_stack_dummy)
7abfe014
DJ
4525 set_current_sal_from_frame (get_current_frame (), 1);
4526
dd7e2d2b
PA
4527 /* Let the user/frontend see the threads as stopped. */
4528 do_cleanups (old_chain);
4529
4530 /* Look up the hook_stop and run it (CLI internally handles problem
4531 of stop_command's pre-hook not existing). */
4532 if (stop_command)
4533 catch_errors (hook_stop_stub, stop_command,
4534 "Error while running hook_stop:\n", RETURN_MASK_ALL);
4535
d729566a 4536 if (!has_stack_frames ())
d51fd4c8 4537 goto done;
c906108c 4538
32400beb
PA
4539 if (last.kind == TARGET_WAITKIND_SIGNALLED
4540 || last.kind == TARGET_WAITKIND_EXITED)
4541 goto done;
4542
c906108c
SS
4543 /* Select innermost stack frame - i.e., current frame is frame 0,
4544 and current location is based on that.
4545 Don't do this on return from a stack dummy routine,
4546 or if the program has exited. */
4547
4548 if (!stop_stack_dummy)
4549 {
0f7d239c 4550 select_frame (get_current_frame ());
c906108c
SS
4551
4552 /* Print current location without a level number, if
c5aa993b
JM
4553 we have changed functions or hit a breakpoint.
4554 Print source line if we have one.
4555 bpstat_print() contains the logic deciding in detail
4556 what to print, based on the event(s) that just occurred. */
c906108c 4557
d01a8610
AS
4558 /* If --batch-silent is enabled then there's no need to print the current
4559 source location, and to try risks causing an error message about
4560 missing source files. */
4561 if (stop_print_frame && !batch_silent)
c906108c
SS
4562 {
4563 int bpstat_ret;
4564 int source_flag;
917317f4 4565 int do_frame_printing = 1;
347bddb7 4566 struct thread_info *tp = inferior_thread ();
c906108c 4567
347bddb7 4568 bpstat_ret = bpstat_print (tp->stop_bpstat);
917317f4
JM
4569 switch (bpstat_ret)
4570 {
4571 case PRINT_UNKNOWN:
b0f4b84b
DJ
4572 /* If we had hit a shared library event breakpoint,
4573 bpstat_print would print out this message. If we hit
4574 an OS-level shared library event, do the same
4575 thing. */
4576 if (last.kind == TARGET_WAITKIND_LOADED)
4577 {
4578 printf_filtered (_("Stopped due to shared library event\n"));
4579 source_flag = SRC_LINE; /* something bogus */
4580 do_frame_printing = 0;
4581 break;
4582 }
4583
aa0cd9c1 4584 /* FIXME: cagney/2002-12-01: Given that a frame ID does
8fb3e588
AC
4585 (or should) carry around the function and does (or
4586 should) use that when doing a frame comparison. */
414c69f7 4587 if (tp->stop_step
347bddb7 4588 && frame_id_eq (tp->step_frame_id,
aa0cd9c1 4589 get_frame_id (get_current_frame ()))
917317f4 4590 && step_start_function == find_pc_function (stop_pc))
488f131b 4591 source_flag = SRC_LINE; /* finished step, just print source line */
917317f4 4592 else
488f131b 4593 source_flag = SRC_AND_LOC; /* print location and source line */
917317f4
JM
4594 break;
4595 case PRINT_SRC_AND_LOC:
488f131b 4596 source_flag = SRC_AND_LOC; /* print location and source line */
917317f4
JM
4597 break;
4598 case PRINT_SRC_ONLY:
c5394b80 4599 source_flag = SRC_LINE;
917317f4
JM
4600 break;
4601 case PRINT_NOTHING:
488f131b 4602 source_flag = SRC_LINE; /* something bogus */
917317f4
JM
4603 do_frame_printing = 0;
4604 break;
4605 default:
e2e0b3e5 4606 internal_error (__FILE__, __LINE__, _("Unknown value."));
917317f4 4607 }
c906108c
SS
4608
4609 /* The behavior of this routine with respect to the source
4610 flag is:
c5394b80
JM
4611 SRC_LINE: Print only source line
4612 LOCATION: Print only location
4613 SRC_AND_LOC: Print location and source line */
917317f4 4614 if (do_frame_printing)
b04f3ab4 4615 print_stack_frame (get_selected_frame (NULL), 0, source_flag);
c906108c
SS
4616
4617 /* Display the auto-display expressions. */
4618 do_displays ();
4619 }
4620 }
4621
4622 /* Save the function value return registers, if we care.
4623 We might be about to restore their previous contents. */
32400beb 4624 if (inferior_thread ()->proceed_to_finish)
d5c31457
UW
4625 {
4626 /* This should not be necessary. */
4627 if (stop_registers)
4628 regcache_xfree (stop_registers);
4629
4630 /* NB: The copy goes through to the target picking up the value of
4631 all the registers. */
4632 stop_registers = regcache_dup (get_current_regcache ());
4633 }
c906108c
SS
4634
4635 if (stop_stack_dummy)
4636 {
b89667eb
DE
4637 /* Pop the empty frame that contains the stack dummy.
4638 This also restores inferior state prior to the call
4639 (struct inferior_thread_state). */
4640 struct frame_info *frame = get_current_frame ();
4641 gdb_assert (get_frame_type (frame) == DUMMY_FRAME);
4642 frame_pop (frame);
4643 /* frame_pop() calls reinit_frame_cache as the last thing it does
4644 which means there's currently no selected frame. We don't need
4645 to re-establish a selected frame if the dummy call returns normally,
4646 that will be done by restore_inferior_status. However, we do have
4647 to handle the case where the dummy call is returning after being
4648 stopped (e.g. the dummy call previously hit a breakpoint). We
4649 can't know which case we have so just always re-establish a
4650 selected frame here. */
0f7d239c 4651 select_frame (get_current_frame ());
c906108c
SS
4652 }
4653
c906108c
SS
4654done:
4655 annotate_stopped ();
41d2bdb4
PA
4656
4657 /* Suppress the stop observer if we're in the middle of:
4658
4659 - a step n (n > 1), as there still more steps to be done.
4660
4661 - a "finish" command, as the observer will be called in
4662 finish_command_continuation, so it can include the inferior
4663 function's return value.
4664
4665 - calling an inferior function, as we pretend we inferior didn't
4666 run at all. The return value of the call is handled by the
4667 expression evaluator, through call_function_by_hand. */
4668
4669 if (!target_has_execution
4670 || last.kind == TARGET_WAITKIND_SIGNALLED
4671 || last.kind == TARGET_WAITKIND_EXITED
4672 || (!inferior_thread ()->step_multi
4673 && !(inferior_thread ()->stop_bpstat
c5a4d20b
PA
4674 && inferior_thread ()->proceed_to_finish)
4675 && !inferior_thread ()->in_infcall))
347bddb7
PA
4676 {
4677 if (!ptid_equal (inferior_ptid, null_ptid))
1d33d6ba
VP
4678 observer_notify_normal_stop (inferior_thread ()->stop_bpstat,
4679 stop_print_frame);
347bddb7 4680 else
1d33d6ba 4681 observer_notify_normal_stop (NULL, stop_print_frame);
347bddb7 4682 }
347bddb7 4683
48844aa6
PA
4684 if (target_has_execution)
4685 {
4686 if (last.kind != TARGET_WAITKIND_SIGNALLED
4687 && last.kind != TARGET_WAITKIND_EXITED)
4688 /* Delete the breakpoint we stopped at, if it wants to be deleted.
4689 Delete any breakpoint that is to be deleted at the next stop. */
4690 breakpoint_auto_delete (inferior_thread ()->stop_bpstat);
94cc34af 4691 }
c906108c
SS
4692}
4693
4694static int
96baa820 4695hook_stop_stub (void *cmd)
c906108c 4696{
5913bcb0 4697 execute_cmd_pre_hook ((struct cmd_list_element *) cmd);
c906108c
SS
4698 return (0);
4699}
4700\f
c5aa993b 4701int
96baa820 4702signal_stop_state (int signo)
c906108c 4703{
d6b48e9c 4704 return signal_stop[signo];
c906108c
SS
4705}
4706
c5aa993b 4707int
96baa820 4708signal_print_state (int signo)
c906108c
SS
4709{
4710 return signal_print[signo];
4711}
4712
c5aa993b 4713int
96baa820 4714signal_pass_state (int signo)
c906108c
SS
4715{
4716 return signal_program[signo];
4717}
4718
488f131b 4719int
7bda5e4a 4720signal_stop_update (int signo, int state)
d4f3574e
SS
4721{
4722 int ret = signal_stop[signo];
4723 signal_stop[signo] = state;
4724 return ret;
4725}
4726
488f131b 4727int
7bda5e4a 4728signal_print_update (int signo, int state)
d4f3574e
SS
4729{
4730 int ret = signal_print[signo];
4731 signal_print[signo] = state;
4732 return ret;
4733}
4734
488f131b 4735int
7bda5e4a 4736signal_pass_update (int signo, int state)
d4f3574e
SS
4737{
4738 int ret = signal_program[signo];
4739 signal_program[signo] = state;
4740 return ret;
4741}
4742
c906108c 4743static void
96baa820 4744sig_print_header (void)
c906108c 4745{
a3f17187
AC
4746 printf_filtered (_("\
4747Signal Stop\tPrint\tPass to program\tDescription\n"));
c906108c
SS
4748}
4749
4750static void
96baa820 4751sig_print_info (enum target_signal oursig)
c906108c 4752{
54363045 4753 const char *name = target_signal_to_name (oursig);
c906108c 4754 int name_padding = 13 - strlen (name);
96baa820 4755
c906108c
SS
4756 if (name_padding <= 0)
4757 name_padding = 0;
4758
4759 printf_filtered ("%s", name);
488f131b 4760 printf_filtered ("%*.*s ", name_padding, name_padding, " ");
c906108c
SS
4761 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
4762 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
4763 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
4764 printf_filtered ("%s\n", target_signal_to_string (oursig));
4765}
4766
4767/* Specify how various signals in the inferior should be handled. */
4768
4769static void
96baa820 4770handle_command (char *args, int from_tty)
c906108c
SS
4771{
4772 char **argv;
4773 int digits, wordlen;
4774 int sigfirst, signum, siglast;
4775 enum target_signal oursig;
4776 int allsigs;
4777 int nsigs;
4778 unsigned char *sigs;
4779 struct cleanup *old_chain;
4780
4781 if (args == NULL)
4782 {
e2e0b3e5 4783 error_no_arg (_("signal to handle"));
c906108c
SS
4784 }
4785
4786 /* Allocate and zero an array of flags for which signals to handle. */
4787
4788 nsigs = (int) TARGET_SIGNAL_LAST;
4789 sigs = (unsigned char *) alloca (nsigs);
4790 memset (sigs, 0, nsigs);
4791
4792 /* Break the command line up into args. */
4793
d1a41061 4794 argv = gdb_buildargv (args);
7a292a7a 4795 old_chain = make_cleanup_freeargv (argv);
c906108c
SS
4796
4797 /* Walk through the args, looking for signal oursigs, signal names, and
4798 actions. Signal numbers and signal names may be interspersed with
4799 actions, with the actions being performed for all signals cumulatively
4800 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
4801
4802 while (*argv != NULL)
4803 {
4804 wordlen = strlen (*argv);
4805 for (digits = 0; isdigit ((*argv)[digits]); digits++)
4806 {;
4807 }
4808 allsigs = 0;
4809 sigfirst = siglast = -1;
4810
4811 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
4812 {
4813 /* Apply action to all signals except those used by the
4814 debugger. Silently skip those. */
4815 allsigs = 1;
4816 sigfirst = 0;
4817 siglast = nsigs - 1;
4818 }
4819 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
4820 {
4821 SET_SIGS (nsigs, sigs, signal_stop);
4822 SET_SIGS (nsigs, sigs, signal_print);
4823 }
4824 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
4825 {
4826 UNSET_SIGS (nsigs, sigs, signal_program);
4827 }
4828 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
4829 {
4830 SET_SIGS (nsigs, sigs, signal_print);
4831 }
4832 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
4833 {
4834 SET_SIGS (nsigs, sigs, signal_program);
4835 }
4836 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
4837 {
4838 UNSET_SIGS (nsigs, sigs, signal_stop);
4839 }
4840 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
4841 {
4842 SET_SIGS (nsigs, sigs, signal_program);
4843 }
4844 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
4845 {
4846 UNSET_SIGS (nsigs, sigs, signal_print);
4847 UNSET_SIGS (nsigs, sigs, signal_stop);
4848 }
4849 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
4850 {
4851 UNSET_SIGS (nsigs, sigs, signal_program);
4852 }
4853 else if (digits > 0)
4854 {
4855 /* It is numeric. The numeric signal refers to our own
4856 internal signal numbering from target.h, not to host/target
4857 signal number. This is a feature; users really should be
4858 using symbolic names anyway, and the common ones like
4859 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
4860
4861 sigfirst = siglast = (int)
4862 target_signal_from_command (atoi (*argv));
4863 if ((*argv)[digits] == '-')
4864 {
4865 siglast = (int)
4866 target_signal_from_command (atoi ((*argv) + digits + 1));
4867 }
4868 if (sigfirst > siglast)
4869 {
4870 /* Bet he didn't figure we'd think of this case... */
4871 signum = sigfirst;
4872 sigfirst = siglast;
4873 siglast = signum;
4874 }
4875 }
4876 else
4877 {
4878 oursig = target_signal_from_name (*argv);
4879 if (oursig != TARGET_SIGNAL_UNKNOWN)
4880 {
4881 sigfirst = siglast = (int) oursig;
4882 }
4883 else
4884 {
4885 /* Not a number and not a recognized flag word => complain. */
8a3fe4f8 4886 error (_("Unrecognized or ambiguous flag word: \"%s\"."), *argv);
c906108c
SS
4887 }
4888 }
4889
4890 /* If any signal numbers or symbol names were found, set flags for
c5aa993b 4891 which signals to apply actions to. */
c906108c
SS
4892
4893 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
4894 {
4895 switch ((enum target_signal) signum)
4896 {
4897 case TARGET_SIGNAL_TRAP:
4898 case TARGET_SIGNAL_INT:
4899 if (!allsigs && !sigs[signum])
4900 {
9e2f0ad4
HZ
4901 if (query (_("%s is used by the debugger.\n\
4902Are you sure you want to change it? "), target_signal_to_name ((enum target_signal) signum)))
c906108c
SS
4903 {
4904 sigs[signum] = 1;
4905 }
4906 else
4907 {
a3f17187 4908 printf_unfiltered (_("Not confirmed, unchanged.\n"));
c906108c
SS
4909 gdb_flush (gdb_stdout);
4910 }
4911 }
4912 break;
4913 case TARGET_SIGNAL_0:
4914 case TARGET_SIGNAL_DEFAULT:
4915 case TARGET_SIGNAL_UNKNOWN:
4916 /* Make sure that "all" doesn't print these. */
4917 break;
4918 default:
4919 sigs[signum] = 1;
4920 break;
4921 }
4922 }
4923
4924 argv++;
4925 }
4926
3a031f65
PA
4927 for (signum = 0; signum < nsigs; signum++)
4928 if (sigs[signum])
4929 {
4930 target_notice_signals (inferior_ptid);
c906108c 4931
3a031f65
PA
4932 if (from_tty)
4933 {
4934 /* Show the results. */
4935 sig_print_header ();
4936 for (; signum < nsigs; signum++)
4937 if (sigs[signum])
4938 sig_print_info (signum);
4939 }
4940
4941 break;
4942 }
c906108c
SS
4943
4944 do_cleanups (old_chain);
4945}
4946
4947static void
96baa820 4948xdb_handle_command (char *args, int from_tty)
c906108c
SS
4949{
4950 char **argv;
4951 struct cleanup *old_chain;
4952
d1a41061
PP
4953 if (args == NULL)
4954 error_no_arg (_("xdb command"));
4955
c906108c
SS
4956 /* Break the command line up into args. */
4957
d1a41061 4958 argv = gdb_buildargv (args);
7a292a7a 4959 old_chain = make_cleanup_freeargv (argv);
c906108c
SS
4960 if (argv[1] != (char *) NULL)
4961 {
4962 char *argBuf;
4963 int bufLen;
4964
4965 bufLen = strlen (argv[0]) + 20;
4966 argBuf = (char *) xmalloc (bufLen);
4967 if (argBuf)
4968 {
4969 int validFlag = 1;
4970 enum target_signal oursig;
4971
4972 oursig = target_signal_from_name (argv[0]);
4973 memset (argBuf, 0, bufLen);
4974 if (strcmp (argv[1], "Q") == 0)
4975 sprintf (argBuf, "%s %s", argv[0], "noprint");
4976 else
4977 {
4978 if (strcmp (argv[1], "s") == 0)
4979 {
4980 if (!signal_stop[oursig])
4981 sprintf (argBuf, "%s %s", argv[0], "stop");
4982 else
4983 sprintf (argBuf, "%s %s", argv[0], "nostop");
4984 }
4985 else if (strcmp (argv[1], "i") == 0)
4986 {
4987 if (!signal_program[oursig])
4988 sprintf (argBuf, "%s %s", argv[0], "pass");
4989 else
4990 sprintf (argBuf, "%s %s", argv[0], "nopass");
4991 }
4992 else if (strcmp (argv[1], "r") == 0)
4993 {
4994 if (!signal_print[oursig])
4995 sprintf (argBuf, "%s %s", argv[0], "print");
4996 else
4997 sprintf (argBuf, "%s %s", argv[0], "noprint");
4998 }
4999 else
5000 validFlag = 0;
5001 }
5002 if (validFlag)
5003 handle_command (argBuf, from_tty);
5004 else
a3f17187 5005 printf_filtered (_("Invalid signal handling flag.\n"));
c906108c 5006 if (argBuf)
b8c9b27d 5007 xfree (argBuf);
c906108c
SS
5008 }
5009 }
5010 do_cleanups (old_chain);
5011}
5012
5013/* Print current contents of the tables set by the handle command.
5014 It is possible we should just be printing signals actually used
5015 by the current target (but for things to work right when switching
5016 targets, all signals should be in the signal tables). */
5017
5018static void
96baa820 5019signals_info (char *signum_exp, int from_tty)
c906108c
SS
5020{
5021 enum target_signal oursig;
5022 sig_print_header ();
5023
5024 if (signum_exp)
5025 {
5026 /* First see if this is a symbol name. */
5027 oursig = target_signal_from_name (signum_exp);
5028 if (oursig == TARGET_SIGNAL_UNKNOWN)
5029 {
5030 /* No, try numeric. */
5031 oursig =
bb518678 5032 target_signal_from_command (parse_and_eval_long (signum_exp));
c906108c
SS
5033 }
5034 sig_print_info (oursig);
5035 return;
5036 }
5037
5038 printf_filtered ("\n");
5039 /* These ugly casts brought to you by the native VAX compiler. */
5040 for (oursig = TARGET_SIGNAL_FIRST;
5041 (int) oursig < (int) TARGET_SIGNAL_LAST;
5042 oursig = (enum target_signal) ((int) oursig + 1))
5043 {
5044 QUIT;
5045
5046 if (oursig != TARGET_SIGNAL_UNKNOWN
488f131b 5047 && oursig != TARGET_SIGNAL_DEFAULT && oursig != TARGET_SIGNAL_0)
c906108c
SS
5048 sig_print_info (oursig);
5049 }
5050
a3f17187 5051 printf_filtered (_("\nUse the \"handle\" command to change these tables.\n"));
c906108c 5052}
4aa995e1
PA
5053
5054/* The $_siginfo convenience variable is a bit special. We don't know
5055 for sure the type of the value until we actually have a chance to
5056 fetch the data. The type can change depending on gdbarch, so it it
5057 also dependent on which thread you have selected.
5058
5059 1. making $_siginfo be an internalvar that creates a new value on
5060 access.
5061
5062 2. making the value of $_siginfo be an lval_computed value. */
5063
5064/* This function implements the lval_computed support for reading a
5065 $_siginfo value. */
5066
5067static void
5068siginfo_value_read (struct value *v)
5069{
5070 LONGEST transferred;
5071
5072 transferred =
5073 target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO,
5074 NULL,
5075 value_contents_all_raw (v),
5076 value_offset (v),
5077 TYPE_LENGTH (value_type (v)));
5078
5079 if (transferred != TYPE_LENGTH (value_type (v)))
5080 error (_("Unable to read siginfo"));
5081}
5082
5083/* This function implements the lval_computed support for writing a
5084 $_siginfo value. */
5085
5086static void
5087siginfo_value_write (struct value *v, struct value *fromval)
5088{
5089 LONGEST transferred;
5090
5091 transferred = target_write (&current_target,
5092 TARGET_OBJECT_SIGNAL_INFO,
5093 NULL,
5094 value_contents_all_raw (fromval),
5095 value_offset (v),
5096 TYPE_LENGTH (value_type (fromval)));
5097
5098 if (transferred != TYPE_LENGTH (value_type (fromval)))
5099 error (_("Unable to write siginfo"));
5100}
5101
5102static struct lval_funcs siginfo_value_funcs =
5103 {
5104 siginfo_value_read,
5105 siginfo_value_write
5106 };
5107
5108/* Return a new value with the correct type for the siginfo object of
5109 the current thread. Return a void value if there's no object
5110 available. */
5111
2c0b251b 5112static struct value *
4aa995e1
PA
5113siginfo_make_value (struct internalvar *var)
5114{
5115 struct type *type;
5116 struct gdbarch *gdbarch;
5117
5118 if (target_has_stack
5119 && !ptid_equal (inferior_ptid, null_ptid))
5120 {
5121 gdbarch = get_frame_arch (get_current_frame ());
5122
5123 if (gdbarch_get_siginfo_type_p (gdbarch))
5124 {
5125 type = gdbarch_get_siginfo_type (gdbarch);
5126
5127 return allocate_computed_value (type, &siginfo_value_funcs, NULL);
5128 }
5129 }
5130
5131 return allocate_value (builtin_type_void);
5132}
5133
c906108c 5134\f
b89667eb
DE
5135/* Inferior thread state.
5136 These are details related to the inferior itself, and don't include
5137 things like what frame the user had selected or what gdb was doing
5138 with the target at the time.
5139 For inferior function calls these are things we want to restore
5140 regardless of whether the function call successfully completes
5141 or the dummy frame has to be manually popped. */
5142
5143struct inferior_thread_state
7a292a7a
SS
5144{
5145 enum target_signal stop_signal;
5146 CORE_ADDR stop_pc;
b89667eb
DE
5147 struct regcache *registers;
5148};
5149
5150struct inferior_thread_state *
5151save_inferior_thread_state (void)
5152{
5153 struct inferior_thread_state *inf_state = XMALLOC (struct inferior_thread_state);
5154 struct thread_info *tp = inferior_thread ();
5155
5156 inf_state->stop_signal = tp->stop_signal;
5157 inf_state->stop_pc = stop_pc;
5158
5159 inf_state->registers = regcache_dup (get_current_regcache ());
5160
5161 return inf_state;
5162}
5163
5164/* Restore inferior session state to INF_STATE. */
5165
5166void
5167restore_inferior_thread_state (struct inferior_thread_state *inf_state)
5168{
5169 struct thread_info *tp = inferior_thread ();
5170
5171 tp->stop_signal = inf_state->stop_signal;
5172 stop_pc = inf_state->stop_pc;
5173
5174 /* The inferior can be gone if the user types "print exit(0)"
5175 (and perhaps other times). */
5176 if (target_has_execution)
5177 /* NB: The register write goes through to the target. */
5178 regcache_cpy (get_current_regcache (), inf_state->registers);
5179 regcache_xfree (inf_state->registers);
5180 xfree (inf_state);
5181}
5182
5183static void
5184do_restore_inferior_thread_state_cleanup (void *state)
5185{
5186 restore_inferior_thread_state (state);
5187}
5188
5189struct cleanup *
5190make_cleanup_restore_inferior_thread_state (struct inferior_thread_state *inf_state)
5191{
5192 return make_cleanup (do_restore_inferior_thread_state_cleanup, inf_state);
5193}
5194
5195void
5196discard_inferior_thread_state (struct inferior_thread_state *inf_state)
5197{
5198 regcache_xfree (inf_state->registers);
5199 xfree (inf_state);
5200}
5201
5202struct regcache *
5203get_inferior_thread_state_regcache (struct inferior_thread_state *inf_state)
5204{
5205 return inf_state->registers;
5206}
5207
5208/* Session related state for inferior function calls.
5209 These are the additional bits of state that need to be restored
5210 when an inferior function call successfully completes. */
5211
5212struct inferior_status
5213{
7a292a7a
SS
5214 bpstat stop_bpstat;
5215 int stop_step;
5216 int stop_stack_dummy;
5217 int stopped_by_random_signal;
ca67fcb8 5218 int stepping_over_breakpoint;
7a292a7a
SS
5219 CORE_ADDR step_range_start;
5220 CORE_ADDR step_range_end;
aa0cd9c1 5221 struct frame_id step_frame_id;
5fbbeb29 5222 enum step_over_calls_kind step_over_calls;
7a292a7a
SS
5223 CORE_ADDR step_resume_break_address;
5224 int stop_after_trap;
c0236d92 5225 int stop_soon;
7a292a7a 5226
b89667eb 5227 /* ID if the selected frame when the inferior function call was made. */
101dcfbe
AC
5228 struct frame_id selected_frame_id;
5229
7a292a7a 5230 int proceed_to_finish;
c5a4d20b 5231 int in_infcall;
7a292a7a
SS
5232};
5233
c906108c 5234/* Save all of the information associated with the inferior<==>gdb
b89667eb 5235 connection. */
c906108c 5236
7a292a7a 5237struct inferior_status *
b89667eb 5238save_inferior_status (void)
c906108c 5239{
72cec141 5240 struct inferior_status *inf_status = XMALLOC (struct inferior_status);
4e1c45ea 5241 struct thread_info *tp = inferior_thread ();
d6b48e9c 5242 struct inferior *inf = current_inferior ();
7a292a7a 5243
414c69f7 5244 inf_status->stop_step = tp->stop_step;
c906108c
SS
5245 inf_status->stop_stack_dummy = stop_stack_dummy;
5246 inf_status->stopped_by_random_signal = stopped_by_random_signal;
4e1c45ea
PA
5247 inf_status->stepping_over_breakpoint = tp->trap_expected;
5248 inf_status->step_range_start = tp->step_range_start;
5249 inf_status->step_range_end = tp->step_range_end;
5250 inf_status->step_frame_id = tp->step_frame_id;
078130d0 5251 inf_status->step_over_calls = tp->step_over_calls;
c906108c 5252 inf_status->stop_after_trap = stop_after_trap;
d6b48e9c 5253 inf_status->stop_soon = inf->stop_soon;
c906108c
SS
5254 /* Save original bpstat chain here; replace it with copy of chain.
5255 If caller's caller is walking the chain, they'll be happier if we
7a292a7a
SS
5256 hand them back the original chain when restore_inferior_status is
5257 called. */
347bddb7
PA
5258 inf_status->stop_bpstat = tp->stop_bpstat;
5259 tp->stop_bpstat = bpstat_copy (tp->stop_bpstat);
32400beb 5260 inf_status->proceed_to_finish = tp->proceed_to_finish;
c5a4d20b 5261 inf_status->in_infcall = tp->in_infcall;
c5aa993b 5262
206415a3 5263 inf_status->selected_frame_id = get_frame_id (get_selected_frame (NULL));
b89667eb 5264
7a292a7a 5265 return inf_status;
c906108c
SS
5266}
5267
c906108c 5268static int
96baa820 5269restore_selected_frame (void *args)
c906108c 5270{
488f131b 5271 struct frame_id *fid = (struct frame_id *) args;
c906108c 5272 struct frame_info *frame;
c906108c 5273
101dcfbe 5274 frame = frame_find_by_id (*fid);
c906108c 5275
aa0cd9c1
AC
5276 /* If inf_status->selected_frame_id is NULL, there was no previously
5277 selected frame. */
101dcfbe 5278 if (frame == NULL)
c906108c 5279 {
8a3fe4f8 5280 warning (_("Unable to restore previously selected frame."));
c906108c
SS
5281 return 0;
5282 }
5283
0f7d239c 5284 select_frame (frame);
c906108c
SS
5285
5286 return (1);
5287}
5288
b89667eb
DE
5289/* Restore inferior session state to INF_STATUS. */
5290
c906108c 5291void
96baa820 5292restore_inferior_status (struct inferior_status *inf_status)
c906108c 5293{
4e1c45ea 5294 struct thread_info *tp = inferior_thread ();
d6b48e9c 5295 struct inferior *inf = current_inferior ();
4e1c45ea 5296
414c69f7 5297 tp->stop_step = inf_status->stop_step;
c906108c
SS
5298 stop_stack_dummy = inf_status->stop_stack_dummy;
5299 stopped_by_random_signal = inf_status->stopped_by_random_signal;
4e1c45ea
PA
5300 tp->trap_expected = inf_status->stepping_over_breakpoint;
5301 tp->step_range_start = inf_status->step_range_start;
5302 tp->step_range_end = inf_status->step_range_end;
5303 tp->step_frame_id = inf_status->step_frame_id;
078130d0 5304 tp->step_over_calls = inf_status->step_over_calls;
c906108c 5305 stop_after_trap = inf_status->stop_after_trap;
d6b48e9c 5306 inf->stop_soon = inf_status->stop_soon;
347bddb7
PA
5307 bpstat_clear (&tp->stop_bpstat);
5308 tp->stop_bpstat = inf_status->stop_bpstat;
b89667eb 5309 inf_status->stop_bpstat = NULL;
32400beb 5310 tp->proceed_to_finish = inf_status->proceed_to_finish;
c5a4d20b 5311 tp->in_infcall = inf_status->in_infcall;
c906108c 5312
b89667eb 5313 if (target_has_stack)
c906108c 5314 {
c906108c 5315 /* The point of catch_errors is that if the stack is clobbered,
101dcfbe
AC
5316 walking the stack might encounter a garbage pointer and
5317 error() trying to dereference it. */
488f131b
JB
5318 if (catch_errors
5319 (restore_selected_frame, &inf_status->selected_frame_id,
5320 "Unable to restore previously selected frame:\n",
5321 RETURN_MASK_ERROR) == 0)
c906108c
SS
5322 /* Error in restoring the selected frame. Select the innermost
5323 frame. */
0f7d239c 5324 select_frame (get_current_frame ());
c906108c 5325 }
c906108c 5326
72cec141 5327 xfree (inf_status);
7a292a7a 5328}
c906108c 5329
74b7792f
AC
5330static void
5331do_restore_inferior_status_cleanup (void *sts)
5332{
5333 restore_inferior_status (sts);
5334}
5335
5336struct cleanup *
5337make_cleanup_restore_inferior_status (struct inferior_status *inf_status)
5338{
5339 return make_cleanup (do_restore_inferior_status_cleanup, inf_status);
5340}
5341
c906108c 5342void
96baa820 5343discard_inferior_status (struct inferior_status *inf_status)
7a292a7a
SS
5344{
5345 /* See save_inferior_status for info on stop_bpstat. */
5346 bpstat_clear (&inf_status->stop_bpstat);
72cec141 5347 xfree (inf_status);
7a292a7a 5348}
b89667eb 5349\f
47932f85 5350int
3a3e9ee3 5351inferior_has_forked (ptid_t pid, ptid_t *child_pid)
47932f85
DJ
5352{
5353 struct target_waitstatus last;
5354 ptid_t last_ptid;
5355
5356 get_last_target_status (&last_ptid, &last);
5357
5358 if (last.kind != TARGET_WAITKIND_FORKED)
5359 return 0;
5360
3a3e9ee3 5361 if (!ptid_equal (last_ptid, pid))
47932f85
DJ
5362 return 0;
5363
5364 *child_pid = last.value.related_pid;
5365 return 1;
5366}
5367
5368int
3a3e9ee3 5369inferior_has_vforked (ptid_t pid, ptid_t *child_pid)
47932f85
DJ
5370{
5371 struct target_waitstatus last;
5372 ptid_t last_ptid;
5373
5374 get_last_target_status (&last_ptid, &last);
5375
5376 if (last.kind != TARGET_WAITKIND_VFORKED)
5377 return 0;
5378
3a3e9ee3 5379 if (!ptid_equal (last_ptid, pid))
47932f85
DJ
5380 return 0;
5381
5382 *child_pid = last.value.related_pid;
5383 return 1;
5384}
5385
5386int
3a3e9ee3 5387inferior_has_execd (ptid_t pid, char **execd_pathname)
47932f85
DJ
5388{
5389 struct target_waitstatus last;
5390 ptid_t last_ptid;
5391
5392 get_last_target_status (&last_ptid, &last);
5393
5394 if (last.kind != TARGET_WAITKIND_EXECD)
5395 return 0;
5396
3a3e9ee3 5397 if (!ptid_equal (last_ptid, pid))
47932f85
DJ
5398 return 0;
5399
5400 *execd_pathname = xstrdup (last.value.execd_pathname);
5401 return 1;
5402}
5403
ca6724c1
KB
5404/* Oft used ptids */
5405ptid_t null_ptid;
5406ptid_t minus_one_ptid;
5407
5408/* Create a ptid given the necessary PID, LWP, and TID components. */
488f131b 5409
ca6724c1
KB
5410ptid_t
5411ptid_build (int pid, long lwp, long tid)
5412{
5413 ptid_t ptid;
5414
5415 ptid.pid = pid;
5416 ptid.lwp = lwp;
5417 ptid.tid = tid;
5418 return ptid;
5419}
5420
5421/* Create a ptid from just a pid. */
5422
5423ptid_t
5424pid_to_ptid (int pid)
5425{
5426 return ptid_build (pid, 0, 0);
5427}
5428
5429/* Fetch the pid (process id) component from a ptid. */
5430
5431int
5432ptid_get_pid (ptid_t ptid)
5433{
5434 return ptid.pid;
5435}
5436
5437/* Fetch the lwp (lightweight process) component from a ptid. */
5438
5439long
5440ptid_get_lwp (ptid_t ptid)
5441{
5442 return ptid.lwp;
5443}
5444
5445/* Fetch the tid (thread id) component from a ptid. */
5446
5447long
5448ptid_get_tid (ptid_t ptid)
5449{
5450 return ptid.tid;
5451}
5452
5453/* ptid_equal() is used to test equality of two ptids. */
5454
5455int
5456ptid_equal (ptid_t ptid1, ptid_t ptid2)
5457{
5458 return (ptid1.pid == ptid2.pid && ptid1.lwp == ptid2.lwp
488f131b 5459 && ptid1.tid == ptid2.tid);
ca6724c1
KB
5460}
5461
252fbfc8
PA
5462/* Returns true if PTID represents a process. */
5463
5464int
5465ptid_is_pid (ptid_t ptid)
5466{
5467 if (ptid_equal (minus_one_ptid, ptid))
5468 return 0;
5469 if (ptid_equal (null_ptid, ptid))
5470 return 0;
5471
5472 return (ptid_get_lwp (ptid) == 0 && ptid_get_tid (ptid) == 0);
5473}
5474
ca6724c1
KB
5475/* restore_inferior_ptid() will be used by the cleanup machinery
5476 to restore the inferior_ptid value saved in a call to
5477 save_inferior_ptid(). */
ce696e05
KB
5478
5479static void
5480restore_inferior_ptid (void *arg)
5481{
5482 ptid_t *saved_ptid_ptr = arg;
5483 inferior_ptid = *saved_ptid_ptr;
5484 xfree (arg);
5485}
5486
5487/* Save the value of inferior_ptid so that it may be restored by a
5488 later call to do_cleanups(). Returns the struct cleanup pointer
5489 needed for later doing the cleanup. */
5490
5491struct cleanup *
5492save_inferior_ptid (void)
5493{
5494 ptid_t *saved_ptid_ptr;
5495
5496 saved_ptid_ptr = xmalloc (sizeof (ptid_t));
5497 *saved_ptid_ptr = inferior_ptid;
5498 return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
5499}
c5aa993b 5500\f
488f131b 5501
b2175913
MS
5502/* User interface for reverse debugging:
5503 Set exec-direction / show exec-direction commands
5504 (returns error unless target implements to_set_exec_direction method). */
5505
5506enum exec_direction_kind execution_direction = EXEC_FORWARD;
5507static const char exec_forward[] = "forward";
5508static const char exec_reverse[] = "reverse";
5509static const char *exec_direction = exec_forward;
5510static const char *exec_direction_names[] = {
5511 exec_forward,
5512 exec_reverse,
5513 NULL
5514};
5515
5516static void
5517set_exec_direction_func (char *args, int from_tty,
5518 struct cmd_list_element *cmd)
5519{
5520 if (target_can_execute_reverse)
5521 {
5522 if (!strcmp (exec_direction, exec_forward))
5523 execution_direction = EXEC_FORWARD;
5524 else if (!strcmp (exec_direction, exec_reverse))
5525 execution_direction = EXEC_REVERSE;
5526 }
5527}
5528
5529static void
5530show_exec_direction_func (struct ui_file *out, int from_tty,
5531 struct cmd_list_element *cmd, const char *value)
5532{
5533 switch (execution_direction) {
5534 case EXEC_FORWARD:
5535 fprintf_filtered (out, _("Forward.\n"));
5536 break;
5537 case EXEC_REVERSE:
5538 fprintf_filtered (out, _("Reverse.\n"));
5539 break;
5540 case EXEC_ERROR:
5541 default:
5542 fprintf_filtered (out,
5543 _("Forward (target `%s' does not support exec-direction).\n"),
5544 target_shortname);
5545 break;
5546 }
5547}
5548
5549/* User interface for non-stop mode. */
5550
ad52ddc6
PA
5551int non_stop = 0;
5552static int non_stop_1 = 0;
5553
5554static void
5555set_non_stop (char *args, int from_tty,
5556 struct cmd_list_element *c)
5557{
5558 if (target_has_execution)
5559 {
5560 non_stop_1 = non_stop;
5561 error (_("Cannot change this setting while the inferior is running."));
5562 }
5563
5564 non_stop = non_stop_1;
5565}
5566
5567static void
5568show_non_stop (struct ui_file *file, int from_tty,
5569 struct cmd_list_element *c, const char *value)
5570{
5571 fprintf_filtered (file,
5572 _("Controlling the inferior in non-stop mode is %s.\n"),
5573 value);
5574}
5575
5576
c906108c 5577void
96baa820 5578_initialize_infrun (void)
c906108c 5579{
52f0bd74
AC
5580 int i;
5581 int numsigs;
c906108c
SS
5582 struct cmd_list_element *c;
5583
1bedd215
AC
5584 add_info ("signals", signals_info, _("\
5585What debugger does when program gets various signals.\n\
5586Specify a signal as argument to print info on that signal only."));
c906108c
SS
5587 add_info_alias ("handle", "signals", 0);
5588
1bedd215
AC
5589 add_com ("handle", class_run, handle_command, _("\
5590Specify how to handle a signal.\n\
c906108c
SS
5591Args are signals and actions to apply to those signals.\n\
5592Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
5593from 1-15 are allowed for compatibility with old versions of GDB.\n\
5594Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
5595The special arg \"all\" is recognized to mean all signals except those\n\
1bedd215
AC
5596used by the debugger, typically SIGTRAP and SIGINT.\n\
5597Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
c906108c
SS
5598\"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
5599Stop means reenter debugger if this signal happens (implies print).\n\
5600Print means print a message if this signal happens.\n\
5601Pass means let program see this signal; otherwise program doesn't know.\n\
5602Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
1bedd215 5603Pass and Stop may be combined."));
c906108c
SS
5604 if (xdb_commands)
5605 {
1bedd215
AC
5606 add_com ("lz", class_info, signals_info, _("\
5607What debugger does when program gets various signals.\n\
5608Specify a signal as argument to print info on that signal only."));
5609 add_com ("z", class_run, xdb_handle_command, _("\
5610Specify how to handle a signal.\n\
c906108c
SS
5611Args are signals and actions to apply to those signals.\n\
5612Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
5613from 1-15 are allowed for compatibility with old versions of GDB.\n\
5614Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
5615The special arg \"all\" is recognized to mean all signals except those\n\
1bedd215
AC
5616used by the debugger, typically SIGTRAP and SIGINT.\n\
5617Recognized actions include \"s\" (toggles between stop and nostop), \n\
c906108c
SS
5618\"r\" (toggles between print and noprint), \"i\" (toggles between pass and \
5619nopass), \"Q\" (noprint)\n\
5620Stop means reenter debugger if this signal happens (implies print).\n\
5621Print means print a message if this signal happens.\n\
5622Pass means let program see this signal; otherwise program doesn't know.\n\
5623Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
1bedd215 5624Pass and Stop may be combined."));
c906108c
SS
5625 }
5626
5627 if (!dbx_commands)
1a966eab
AC
5628 stop_command = add_cmd ("stop", class_obscure,
5629 not_just_help_class_command, _("\
5630There is no `stop' command, but you can set a hook on `stop'.\n\
c906108c 5631This allows you to set a list of commands to be run each time execution\n\
1a966eab 5632of the program stops."), &cmdlist);
c906108c 5633
85c07804
AC
5634 add_setshow_zinteger_cmd ("infrun", class_maintenance, &debug_infrun, _("\
5635Set inferior debugging."), _("\
5636Show inferior debugging."), _("\
5637When non-zero, inferior specific debugging is enabled."),
5638 NULL,
920d2a44 5639 show_debug_infrun,
85c07804 5640 &setdebuglist, &showdebuglist);
527159b7 5641
237fc4c9
PA
5642 add_setshow_boolean_cmd ("displaced", class_maintenance, &debug_displaced, _("\
5643Set displaced stepping debugging."), _("\
5644Show displaced stepping debugging."), _("\
5645When non-zero, displaced stepping specific debugging is enabled."),
5646 NULL,
5647 show_debug_displaced,
5648 &setdebuglist, &showdebuglist);
5649
ad52ddc6
PA
5650 add_setshow_boolean_cmd ("non-stop", no_class,
5651 &non_stop_1, _("\
5652Set whether gdb controls the inferior in non-stop mode."), _("\
5653Show whether gdb controls the inferior in non-stop mode."), _("\
5654When debugging a multi-threaded program and this setting is\n\
5655off (the default, also called all-stop mode), when one thread stops\n\
5656(for a breakpoint, watchpoint, exception, or similar events), GDB stops\n\
5657all other threads in the program while you interact with the thread of\n\
5658interest. When you continue or step a thread, you can allow the other\n\
5659threads to run, or have them remain stopped, but while you inspect any\n\
5660thread's state, all threads stop.\n\
5661\n\
5662In non-stop mode, when one thread stops, other threads can continue\n\
5663to run freely. You'll be able to step each thread independently,\n\
5664leave it stopped or free to run as needed."),
5665 set_non_stop,
5666 show_non_stop,
5667 &setlist,
5668 &showlist);
5669
c906108c 5670 numsigs = (int) TARGET_SIGNAL_LAST;
488f131b 5671 signal_stop = (unsigned char *) xmalloc (sizeof (signal_stop[0]) * numsigs);
c906108c
SS
5672 signal_print = (unsigned char *)
5673 xmalloc (sizeof (signal_print[0]) * numsigs);
5674 signal_program = (unsigned char *)
5675 xmalloc (sizeof (signal_program[0]) * numsigs);
5676 for (i = 0; i < numsigs; i++)
5677 {
5678 signal_stop[i] = 1;
5679 signal_print[i] = 1;
5680 signal_program[i] = 1;
5681 }
5682
5683 /* Signals caused by debugger's own actions
5684 should not be given to the program afterwards. */
5685 signal_program[TARGET_SIGNAL_TRAP] = 0;
5686 signal_program[TARGET_SIGNAL_INT] = 0;
5687
5688 /* Signals that are not errors should not normally enter the debugger. */
5689 signal_stop[TARGET_SIGNAL_ALRM] = 0;
5690 signal_print[TARGET_SIGNAL_ALRM] = 0;
5691 signal_stop[TARGET_SIGNAL_VTALRM] = 0;
5692 signal_print[TARGET_SIGNAL_VTALRM] = 0;
5693 signal_stop[TARGET_SIGNAL_PROF] = 0;
5694 signal_print[TARGET_SIGNAL_PROF] = 0;
5695 signal_stop[TARGET_SIGNAL_CHLD] = 0;
5696 signal_print[TARGET_SIGNAL_CHLD] = 0;
5697 signal_stop[TARGET_SIGNAL_IO] = 0;
5698 signal_print[TARGET_SIGNAL_IO] = 0;
5699 signal_stop[TARGET_SIGNAL_POLL] = 0;
5700 signal_print[TARGET_SIGNAL_POLL] = 0;
5701 signal_stop[TARGET_SIGNAL_URG] = 0;
5702 signal_print[TARGET_SIGNAL_URG] = 0;
5703 signal_stop[TARGET_SIGNAL_WINCH] = 0;
5704 signal_print[TARGET_SIGNAL_WINCH] = 0;
5705
cd0fc7c3
SS
5706 /* These signals are used internally by user-level thread
5707 implementations. (See signal(5) on Solaris.) Like the above
5708 signals, a healthy program receives and handles them as part of
5709 its normal operation. */
5710 signal_stop[TARGET_SIGNAL_LWP] = 0;
5711 signal_print[TARGET_SIGNAL_LWP] = 0;
5712 signal_stop[TARGET_SIGNAL_WAITING] = 0;
5713 signal_print[TARGET_SIGNAL_WAITING] = 0;
5714 signal_stop[TARGET_SIGNAL_CANCEL] = 0;
5715 signal_print[TARGET_SIGNAL_CANCEL] = 0;
5716
85c07804
AC
5717 add_setshow_zinteger_cmd ("stop-on-solib-events", class_support,
5718 &stop_on_solib_events, _("\
5719Set stopping for shared library events."), _("\
5720Show stopping for shared library events."), _("\
c906108c
SS
5721If nonzero, gdb will give control to the user when the dynamic linker\n\
5722notifies gdb of shared library events. The most common event of interest\n\
85c07804
AC
5723to the user would be loading/unloading of a new library."),
5724 NULL,
920d2a44 5725 show_stop_on_solib_events,
85c07804 5726 &setlist, &showlist);
c906108c 5727
7ab04401
AC
5728 add_setshow_enum_cmd ("follow-fork-mode", class_run,
5729 follow_fork_mode_kind_names,
5730 &follow_fork_mode_string, _("\
5731Set debugger response to a program call of fork or vfork."), _("\
5732Show debugger response to a program call of fork or vfork."), _("\
c906108c
SS
5733A fork or vfork creates a new process. follow-fork-mode can be:\n\
5734 parent - the original process is debugged after a fork\n\
5735 child - the new process is debugged after a fork\n\
ea1dd7bc 5736The unfollowed process will continue to run.\n\
7ab04401
AC
5737By default, the debugger will follow the parent process."),
5738 NULL,
920d2a44 5739 show_follow_fork_mode_string,
7ab04401
AC
5740 &setlist, &showlist);
5741
5742 add_setshow_enum_cmd ("scheduler-locking", class_run,
5743 scheduler_enums, &scheduler_mode, _("\
5744Set mode for locking scheduler during execution."), _("\
5745Show mode for locking scheduler during execution."), _("\
c906108c
SS
5746off == no locking (threads may preempt at any time)\n\
5747on == full locking (no thread except the current thread may run)\n\
5748step == scheduler locked during every single-step operation.\n\
5749 In this mode, no other thread may run during a step command.\n\
7ab04401
AC
5750 Other threads may run while stepping over a function call ('next')."),
5751 set_schedlock_func, /* traps on target vector */
920d2a44 5752 show_scheduler_mode,
7ab04401 5753 &setlist, &showlist);
5fbbeb29 5754
5bf193a2
AC
5755 add_setshow_boolean_cmd ("step-mode", class_run, &step_stop_if_no_debug, _("\
5756Set mode of the step operation."), _("\
5757Show mode of the step operation."), _("\
5758When set, doing a step over a function without debug line information\n\
5759will stop at the first instruction of that function. Otherwise, the\n\
5760function is skipped and the step command stops at a different source line."),
5761 NULL,
920d2a44 5762 show_step_stop_if_no_debug,
5bf193a2 5763 &setlist, &showlist);
ca6724c1 5764
fff08868
HZ
5765 add_setshow_enum_cmd ("displaced-stepping", class_run,
5766 can_use_displaced_stepping_enum,
5767 &can_use_displaced_stepping, _("\
237fc4c9
PA
5768Set debugger's willingness to use displaced stepping."), _("\
5769Show debugger's willingness to use displaced stepping."), _("\
fff08868
HZ
5770If on, gdb will use displaced stepping to step over breakpoints if it is\n\
5771supported by the target architecture. If off, gdb will not use displaced\n\
5772stepping to step over breakpoints, even if such is supported by the target\n\
5773architecture. If auto (which is the default), gdb will use displaced stepping\n\
5774if the target architecture supports it and non-stop mode is active, but will not\n\
5775use it in all-stop mode (see help set non-stop)."),
5776 NULL,
5777 show_can_use_displaced_stepping,
5778 &setlist, &showlist);
237fc4c9 5779
b2175913
MS
5780 add_setshow_enum_cmd ("exec-direction", class_run, exec_direction_names,
5781 &exec_direction, _("Set direction of execution.\n\
5782Options are 'forward' or 'reverse'."),
5783 _("Show direction of execution (forward/reverse)."),
5784 _("Tells gdb whether to execute forward or backward."),
5785 set_exec_direction_func, show_exec_direction_func,
5786 &setlist, &showlist);
5787
ca6724c1
KB
5788 /* ptid initializations */
5789 null_ptid = ptid_build (0, 0, 0);
5790 minus_one_ptid = ptid_build (-1, 0, 0);
5791 inferior_ptid = null_ptid;
5792 target_last_wait_ptid = minus_one_ptid;
237fc4c9 5793 displaced_step_ptid = null_ptid;
5231c1fd
PA
5794
5795 observer_attach_thread_ptid_changed (infrun_thread_ptid_changed);
252fbfc8 5796 observer_attach_thread_stop_requested (infrun_thread_stop_requested);
a07daef3 5797 observer_attach_thread_exit (infrun_thread_thread_exit);
4aa995e1
PA
5798
5799 /* Explicitly create without lookup, since that tries to create a
5800 value with a void typed value, and when we get here, gdbarch
5801 isn't initialized yet. At this point, we're quite sure there
5802 isn't another convenience variable of the same name. */
5803 create_internalvar_type_lazy ("_siginfo", siginfo_make_value);
c906108c 5804}
This page took 1.840257 seconds and 4 git commands to generate.