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