* From Peter Schauer:
[deliverable/binutils-gdb.git] / gdb / infrun.c
CommitLineData
3aa6856a 1/* Target-struct-independent code to start (run) and stop an inferior process.
87273c71 2 Copyright 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996
101b7f9c 3 Free Software Foundation, Inc.
bd5635a1
RP
4
5This file is part of GDB.
6
3b271cf4 7This program is free software; you can redistribute it and/or modify
bd5635a1 8it under the terms of the GNU General Public License as published by
3b271cf4
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
bd5635a1 11
3b271cf4 12This program is distributed in the hope that it will be useful,
bd5635a1
RP
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
3b271cf4 18along with this program; if not, write to the Free Software
3f687c78 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
bd5635a1 20
bd5635a1 21#include "defs.h"
2b576293 22#include "gdb_string.h"
a6b98cb9 23#include <ctype.h>
bd5635a1
RP
24#include "symtab.h"
25#include "frame.h"
26#include "inferior.h"
27#include "breakpoint.h"
28#include "wait.h"
29#include "gdbcore.h"
3950a34e 30#include "gdbcmd.h"
bd5635a1 31#include "target.h"
100f92e2 32#include "thread.h"
1c95d7ab 33#include "annotate.h"
bd5635a1
RP
34
35#include <signal.h>
36
37/* unistd.h is needed to #define X_OK */
38#ifdef USG
39#include <unistd.h>
40#else
41#include <sys/file.h>
42#endif
43
30875e1c 44/* Prototypes for local functions */
bd5635a1 45
4cc1b3f7 46static void signals_info PARAMS ((char *, int));
619fd145 47
4cc1b3f7 48static void handle_command PARAMS ((char *, int));
30875e1c 49
67ac9759 50static void sig_print_info PARAMS ((enum target_signal));
30875e1c 51
4cc1b3f7 52static void sig_print_header PARAMS ((void));
30875e1c 53
4cc1b3f7 54static void resume_cleanups PARAMS ((int));
30875e1c 55
4cc1b3f7 56static int hook_stop_stub PARAMS ((char *));
3950a34e 57
30875e1c
SG
58/* GET_LONGJMP_TARGET returns the PC at which longjmp() will resume the
59 program. It needs to examine the jmp_buf argument and extract the PC
60 from it. The return value is non-zero on success, zero otherwise. */
4cc1b3f7 61
30875e1c
SG
62#ifndef GET_LONGJMP_TARGET
63#define GET_LONGJMP_TARGET(PC_ADDR) 0
64#endif
65
d747e0af
MT
66
67/* Some machines have trampoline code that sits between function callers
68 and the actual functions themselves. If this machine doesn't have
69 such things, disable their processing. */
4cc1b3f7 70
d747e0af
MT
71#ifndef SKIP_TRAMPOLINE_CODE
72#define SKIP_TRAMPOLINE_CODE(pc) 0
73#endif
74
87273c71
JL
75/* Dynamic function trampolines are similar to solib trampolines in that they
76 are between the caller and the callee. The difference is that when you
77 enter a dynamic trampoline, you can't determine the callee's address. Some
78 (usually complex) code needs to run in the dynamic trampoline to figure out
79 the callee's address. This macro is usually called twice. First, when we
80 enter the trampoline (looks like a normal function call at that point). It
81 should return the PC of a point within the trampoline where the callee's
82 address is known. Second, when we hit the breakpoint, this routine returns
83 the callee's address. At that point, things proceed as per a step resume
84 breakpoint. */
85
86#ifndef DYNAMIC_TRAMPOLINE_NEXTPC
87#define DYNAMIC_TRAMPOLINE_NEXTPC(pc) 0
88#endif
89
1eeba686 90/* For SVR4 shared libraries, each call goes through a small piece of
4cc1b3f7 91 trampoline code in the ".plt" section. IN_SOLIB_CALL_TRAMPOLINE evaluates
1eeba686 92 to nonzero if we are current stopped in one of these. */
4cc1b3f7
JK
93
94#ifndef IN_SOLIB_CALL_TRAMPOLINE
95#define IN_SOLIB_CALL_TRAMPOLINE(pc,name) 0
96#endif
97
98/* In some shared library schemes, the return path from a shared library
99 call may need to go through a trampoline too. */
100
101#ifndef IN_SOLIB_RETURN_TRAMPOLINE
102#define IN_SOLIB_RETURN_TRAMPOLINE(pc,name) 0
1eeba686 103#endif
d747e0af 104
9f739abd
SG
105/* On some systems, the PC may be left pointing at an instruction that won't
106 actually be executed. This is usually indicated by a bit in the PSW. If
107 we find ourselves in such a state, then we step the target beyond the
108 nullified instruction before returning control to the user so as to avoid
109 confusion. */
110
111#ifndef INSTRUCTION_NULLIFIED
112#define INSTRUCTION_NULLIFIED 0
113#endif
114
bd5635a1
RP
115/* Tables of how to react to signals; the user sets them. */
116
072b552a
JG
117static unsigned char *signal_stop;
118static unsigned char *signal_print;
119static unsigned char *signal_program;
120
121#define SET_SIGS(nsigs,sigs,flags) \
122 do { \
123 int signum = (nsigs); \
124 while (signum-- > 0) \
125 if ((sigs)[signum]) \
126 (flags)[signum] = 1; \
127 } while (0)
128
129#define UNSET_SIGS(nsigs,sigs,flags) \
130 do { \
131 int signum = (nsigs); \
132 while (signum-- > 0) \
133 if ((sigs)[signum]) \
134 (flags)[signum] = 0; \
135 } while (0)
bd5635a1 136
3950a34e
RP
137
138/* Command list pointer for the "stop" placeholder. */
139
140static struct cmd_list_element *stop_command;
141
bd5635a1 142/* Nonzero if breakpoints are now inserted in the inferior. */
bd5635a1 143
3950a34e 144static int breakpoints_inserted;
bd5635a1
RP
145
146/* Function inferior was in as of last step command. */
147
148static struct symbol *step_start_function;
149
bd5635a1
RP
150/* Nonzero if we are expecting a trace trap and should proceed from it. */
151
152static int trap_expected;
153
87273c71
JL
154/* Nonzero if we want to give control to the user when we're notified
155 of shared library events by the dynamic linker. */
156static int stop_on_solib_events;
157
c66ed884 158#ifdef HP_OS_BUG
bd5635a1
RP
159/* Nonzero if the next time we try to continue the inferior, it will
160 step one instruction and generate a spurious trace trap.
161 This is used to compensate for a bug in HP-UX. */
162
163static int trap_expected_after_continue;
c66ed884 164#endif
bd5635a1
RP
165
166/* Nonzero means expecting a trace trap
167 and should stop the inferior and return silently when it happens. */
168
169int stop_after_trap;
170
171/* Nonzero means expecting a trap and caller will handle it themselves.
172 It is used after attach, due to attaching to a process;
173 when running in the shell before the child program has been exec'd;
174 and when running some kinds of remote stuff (FIXME?). */
175
176int stop_soon_quietly;
177
bd5635a1
RP
178/* Nonzero if proceed is being used for a "finish" command or a similar
179 situation when stop_registers should be saved. */
180
181int proceed_to_finish;
182
183/* Save register contents here when about to pop a stack dummy frame,
184 if-and-only-if proceed_to_finish is set.
185 Thus this contains the return value from the called function (assuming
186 values are returned in a register). */
187
188char stop_registers[REGISTER_BYTES];
189
190/* Nonzero if program stopped due to error trying to insert breakpoints. */
191
192static int breakpoints_failed;
193
194/* Nonzero after stop if current stack frame should be printed. */
195
196static int stop_print_frame;
197
198#ifdef NO_SINGLE_STEP
199extern int one_stepped; /* From machine dependent code */
200extern void single_step (); /* Same. */
201#endif /* NO_SINGLE_STEP */
202
894d8e69
JL
203extern void write_pc_pid PARAMS ((CORE_ADDR, int));
204
a71d17b1
JK
205\f
206/* Things to clean up if we QUIT out of resume (). */
e1ce8aa5 207/* ARGSUSED */
a71d17b1
JK
208static void
209resume_cleanups (arg)
210 int arg;
211{
212 normal_stop ();
213}
214
215/* Resume the inferior, but allow a QUIT. This is useful if the user
216 wants to interrupt some lengthy single-stepping operation
217 (for child processes, the SIGINT goes to the inferior, and so
218 we get a SIGINT random_signal, but for remote debugging and perhaps
219 other targets, that's not true).
220
221 STEP nonzero if we should step (zero to continue instead).
222 SIG is the signal to give the inferior (zero for none). */
310cc570 223void
a71d17b1
JK
224resume (step, sig)
225 int step;
67ac9759 226 enum target_signal sig;
a71d17b1
JK
227{
228 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
229 QUIT;
d11c44f1 230
cef4c2e7
PS
231#ifdef CANNOT_STEP_BREAKPOINT
232 /* Most targets can step a breakpoint instruction, thus executing it
233 normally. But if this one cannot, just continue and we will hit
234 it anyway. */
235 if (step && breakpoints_inserted && breakpoint_here_p (read_pc ()))
236 step = 0;
237#endif
238
d11c44f1
JG
239#ifdef NO_SINGLE_STEP
240 if (step) {
818de002 241 single_step(sig); /* Do it the hard way, w/temp breakpoints */
d11c44f1
JG
242 step = 0; /* ...and don't ask hardware to do it. */
243 }
244#endif
245
bdbd5f50
JG
246 /* Handle any optimized stores to the inferior NOW... */
247#ifdef DO_DEFERRED_STORES
248 DO_DEFERRED_STORES;
249#endif
250
2f1c7c3f
JK
251 /* Install inferior's terminal modes. */
252 target_terminal_inferior ();
253
de43d7d0 254 target_resume (-1, step, sig);
a71d17b1
JK
255 discard_cleanups (old_cleanups);
256}
257
bd5635a1
RP
258\f
259/* Clear out all variables saying what to do when inferior is continued.
260 First do this, then set the ones you want, then call `proceed'. */
261
262void
263clear_proceed_status ()
264{
265 trap_expected = 0;
266 step_range_start = 0;
267 step_range_end = 0;
268 step_frame_address = 0;
269 step_over_calls = -1;
bd5635a1
RP
270 stop_after_trap = 0;
271 stop_soon_quietly = 0;
272 proceed_to_finish = 0;
273 breakpoint_proceeded = 1; /* We're about to proceed... */
274
275 /* Discard any remaining commands or status from previous stop. */
276 bpstat_clear (&stop_bpstat);
277}
278
279/* Basic routine for continuing the program in various fashions.
280
281 ADDR is the address to resume at, or -1 for resume where stopped.
282 SIGGNAL is the signal to give it, or 0 for none,
283 or -1 for act according to how it stopped.
284 STEP is nonzero if should trap after one instruction.
285 -1 means return after that and print nothing.
286 You should probably set various step_... variables
287 before calling here, if you are stepping.
288
289 You should call clear_proceed_status before calling proceed. */
290
291void
292proceed (addr, siggnal, step)
293 CORE_ADDR addr;
67ac9759 294 enum target_signal siggnal;
bd5635a1
RP
295 int step;
296{
297 int oneproc = 0;
298
299 if (step > 0)
300 step_start_function = find_pc_function (read_pc ());
301 if (step < 0)
302 stop_after_trap = 1;
303
bdbd5f50 304 if (addr == (CORE_ADDR)-1)
bd5635a1
RP
305 {
306 /* If there is a breakpoint at the address we will resume at,
307 step one instruction before inserting breakpoints
308 so that we do not stop right away. */
309
37c99ddb 310 if (breakpoint_here_p (read_pc ()))
bd5635a1 311 oneproc = 1;
b5aff268
JK
312
313#ifdef STEP_SKIPS_DELAY
314 /* Check breakpoint_here_p first, because breakpoint_here_p is fast
315 (it just checks internal GDB data structures) and STEP_SKIPS_DELAY
316 is slow (it needs to read memory from the target). */
317 if (breakpoint_here_p (read_pc () + 4)
318 && STEP_SKIPS_DELAY (read_pc ()))
319 oneproc = 1;
320#endif /* STEP_SKIPS_DELAY */
bd5635a1
RP
321 }
322 else
101b7f9c 323 write_pc (addr);
bd5635a1 324
320f93f7
SG
325#ifdef PREPARE_TO_PROCEED
326 /* In a multi-threaded task we may select another thread and then continue.
327
328 In this case the thread that stopped at a breakpoint will immediately
329 cause another stop, if it is not stepped over first. On the other hand,
330 if (ADDR != -1) we only want to single step over the breakpoint if we did
331 switch to another thread.
332
333 If we are single stepping, don't do any of the above.
334 (Note that in the current implementation single stepping another
335 thread after a breakpoint and then continuing will cause the original
336 breakpoint to be hit again, but you can always continue, so it's not
337 a big deal.) */
338
479f0f18 339 if (! step && PREPARE_TO_PROCEED (1) && breakpoint_here_p (read_pc ()))
320f93f7
SG
340 oneproc = 1;
341#endif /* PREPARE_TO_PROCEED */
342
c66ed884 343#ifdef HP_OS_BUG
bd5635a1
RP
344 if (trap_expected_after_continue)
345 {
346 /* If (step == 0), a trap will be automatically generated after
347 the first instruction is executed. Force step one
348 instruction to clear this condition. This should not occur
349 if step is nonzero, but it is harmless in that case. */
350 oneproc = 1;
351 trap_expected_after_continue = 0;
352 }
c66ed884 353#endif /* HP_OS_BUG */
bd5635a1
RP
354
355 if (oneproc)
356 /* We will get a trace trap after one instruction.
357 Continue it automatically and insert breakpoints then. */
358 trap_expected = 1;
359 else
360 {
361 int temp = insert_breakpoints ();
362 if (temp)
363 {
364 print_sys_errmsg ("ptrace", temp);
365 error ("Cannot insert breakpoints.\n\
366The same program may be running in another process.");
367 }
368 breakpoints_inserted = 1;
369 }
370
fcbc95a7 371 if (siggnal != TARGET_SIGNAL_DEFAULT)
bd5635a1
RP
372 stop_signal = siggnal;
373 /* If this signal should not be seen by program,
374 give it zero. Used for debugging signals. */
67ac9759 375 else if (!signal_program[stop_signal])
fcbc95a7 376 stop_signal = TARGET_SIGNAL_0;
bd5635a1 377
1c95d7ab
JK
378 annotate_starting ();
379
c66ed884
SG
380 /* Make sure that output from GDB appears before output from the
381 inferior. */
382 gdb_flush (gdb_stdout);
383
bd5635a1 384 /* Resume inferior. */
a71d17b1 385 resume (oneproc || step || bpstat_should_step (), stop_signal);
bd5635a1
RP
386
387 /* Wait for it to stop (if not standalone)
388 and in any case decode why it stopped, and act accordingly. */
389
390 wait_for_inferior ();
391 normal_stop ();
392}
393
bd5635a1
RP
394/* Record the pc and sp of the program the last time it stopped.
395 These are just used internally by wait_for_inferior, but need
396 to be preserved over calls to it and cleared when the inferior
397 is started. */
398static CORE_ADDR prev_pc;
bd5635a1
RP
399static CORE_ADDR prev_func_start;
400static char *prev_func_name;
401
a71d17b1 402\f
bd5635a1
RP
403/* Start remote-debugging of a machine over a serial link. */
404
405void
406start_remote ()
407{
4cc1b3f7 408 init_thread_list ();
bd5635a1
RP
409 init_wait_for_inferior ();
410 clear_proceed_status ();
411 stop_soon_quietly = 1;
412 trap_expected = 0;
98885d76
JK
413 wait_for_inferior ();
414 normal_stop ();
bd5635a1
RP
415}
416
417/* Initialize static vars when a new inferior begins. */
418
419void
420init_wait_for_inferior ()
421{
422 /* These are meaningless until the first time through wait_for_inferior. */
423 prev_pc = 0;
bd5635a1
RP
424 prev_func_start = 0;
425 prev_func_name = NULL;
426
c66ed884 427#ifdef HP_OS_BUG
bd5635a1 428 trap_expected_after_continue = 0;
c66ed884 429#endif
bd5635a1 430 breakpoints_inserted = 0;
cf3e377e 431 breakpoint_init_inferior ();
67ac9759
JK
432
433 /* Don't confuse first call to proceed(). */
434 stop_signal = TARGET_SIGNAL_0;
bd5635a1
RP
435}
436
fe675038
JK
437static void
438delete_breakpoint_current_contents (arg)
439 PTR arg;
440{
441 struct breakpoint **breakpointp = (struct breakpoint **)arg;
442 if (*breakpointp != NULL)
443 delete_breakpoint (*breakpointp);
444}
bd5635a1
RP
445\f
446/* Wait for control to return from inferior to debugger.
447 If inferior gets a signal, we may decide to start it up again
448 instead of returning. That is why there is a loop in this function.
449 When this function actually returns it means the inferior
450 should be left stopped and GDB should read more commands. */
451
452void
453wait_for_inferior ()
454{
fe675038 455 struct cleanup *old_cleanups;
67ac9759 456 struct target_waitstatus w;
bd5635a1
RP
457 int another_trap;
458 int random_signal;
bd5635a1 459 CORE_ADDR stop_func_start;
67ac9759 460 CORE_ADDR stop_func_end;
bd5635a1 461 char *stop_func_name;
894d8e69
JL
462#if 0
463 CORE_ADDR prologue_pc = 0;
464#endif
465 CORE_ADDR tmp;
bd5635a1
RP
466 struct symtab_and_line sal;
467 int remove_breakpoints_on_following_step = 0;
b3b39c0c 468 int current_line;
b2f03c30 469 struct symtab *current_symtab;
30875e1c 470 int handling_longjmp = 0; /* FIXME */
fe675038 471 struct breakpoint *step_resume_breakpoint = NULL;
bcc37718 472 struct breakpoint *through_sigtramp_breakpoint = NULL;
37c99ddb 473 int pid;
479f0f18 474 int update_step_sp = 0;
bd5635a1 475
fe675038
JK
476 old_cleanups = make_cleanup (delete_breakpoint_current_contents,
477 &step_resume_breakpoint);
bcc37718
JK
478 make_cleanup (delete_breakpoint_current_contents,
479 &through_sigtramp_breakpoint);
b3b39c0c
SG
480 sal = find_pc_line(prev_pc, 0);
481 current_line = sal.line;
b2f03c30 482 current_symtab = sal.symtab;
b3b39c0c 483
cb6b0202 484 /* Are we stepping? */
bcc37718
JK
485#define CURRENTLY_STEPPING() \
486 ((through_sigtramp_breakpoint == NULL \
487 && !handling_longjmp \
488 && ((step_range_end && step_resume_breakpoint == NULL) \
489 || trap_expected)) \
490 || bpstat_should_step ())
cb6b0202 491
bd5635a1
RP
492 while (1)
493 {
320f93f7
SG
494 /* We have to invalidate the registers BEFORE calling target_wait because
495 they can be loaded from the target while in target_wait. This makes
496 remote debugging a bit more efficient for those targets that provide
497 critical registers as part of their normal status mechanism. */
498
499 registers_changed ();
500
479f0f18
SG
501 if (target_wait_hook)
502 pid = target_wait_hook (-1, &w);
503 else
504 pid = target_wait (-1, &w);
1c95d7ab 505
894d8e69
JL
506 /* Gross.
507
508 We goto this label from elsewhere in wait_for_inferior when we want
509 to continue the main loop without calling "wait" and trashing the
510 waitstatus contained in W. */
48f4903f
JL
511 have_waited:
512
bd5635a1 513 flush_cached_frames ();
320f93f7
SG
514
515 /* If it's a new process, add it to the thread database */
516
517 if (pid != inferior_pid
518 && !in_thread_list (pid))
519 {
520 fprintf_unfiltered (gdb_stderr, "[New %s]\n", target_pid_to_str (pid));
521 add_thread (pid);
479f0f18
SG
522
523 /* We may want to consider not doing a resume here in order to give
524 the user a chance to play with the new thread. It might be good
525 to make that a user-settable option. */
526
527 /* At this point, all threads are stopped (happens automatically in
528 either the OS or the native code). Therefore we need to continue
529 all threads in order to make progress. */
530
531 target_resume (-1, 0, TARGET_SIGNAL_0);
532 continue;
320f93f7 533 }
bd5635a1 534
fcbc95a7
JK
535 switch (w.kind)
536 {
537 case TARGET_WAITKIND_LOADED:
538 /* Ignore it gracefully. */
539 if (breakpoints_inserted)
540 {
541 mark_breakpoints_out ();
542 insert_breakpoints ();
543 }
544 resume (0, TARGET_SIGNAL_0);
545 continue;
1eeba686 546
fcbc95a7
JK
547 case TARGET_WAITKIND_SPURIOUS:
548 resume (0, TARGET_SIGNAL_0);
549 continue;
1eeba686 550
fcbc95a7 551 case TARGET_WAITKIND_EXITED:
bd5635a1 552 target_terminal_ours (); /* Must do this before mourn anyway */
1c95d7ab 553 annotate_exited (w.value.integer);
67ac9759 554 if (w.value.integer)
e37a6e9c 555 printf_filtered ("\nProgram exited with code 0%o.\n",
67ac9759 556 (unsigned int)w.value.integer);
bd5635a1 557 else
479f0f18 558 printf_filtered ("\nProgram exited normally.\n");
2b576293
C
559
560 /* Record the exit code in the convenience variable $_exitcode, so
561 that the user can inspect this again later. */
562 set_internalvar (lookup_internalvar ("_exitcode"),
563 value_from_longest (builtin_type_int,
564 (LONGEST) w.value.integer));
199b2450 565 gdb_flush (gdb_stdout);
bd5635a1
RP
566 target_mourn_inferior ();
567#ifdef NO_SINGLE_STEP
568 one_stepped = 0;
569#endif
570 stop_print_frame = 0;
fcbc95a7 571 goto stop_stepping;
67ac9759 572
fcbc95a7 573 case TARGET_WAITKIND_SIGNALLED:
bd5635a1 574 stop_print_frame = 0;
67ac9759 575 stop_signal = w.value.sig;
bd5635a1 576 target_terminal_ours (); /* Must do this before mourn anyway */
1c95d7ab 577 annotate_signalled ();
4cc1b3f7
JK
578
579 /* This looks pretty bogus to me. Doesn't TARGET_WAITKIND_SIGNALLED
580 mean it is already dead? This has been here since GDB 2.8, so
581 perhaps it means rms didn't understand unix waitstatuses?
582 For the moment I'm just kludging around this in remote.c
583 rather than trying to change it here --kingdon, 5 Dec 1994. */
30875e1c 584 target_kill (); /* kill mourns as well */
4cc1b3f7 585
1c95d7ab
JK
586 printf_filtered ("\nProgram terminated with signal ");
587 annotate_signal_name ();
588 printf_filtered ("%s", target_signal_to_name (stop_signal));
589 annotate_signal_name_end ();
590 printf_filtered (", ");
591 annotate_signal_string ();
592 printf_filtered ("%s", target_signal_to_string (stop_signal));
593 annotate_signal_string_end ();
594 printf_filtered (".\n");
67ac9759 595
fee44494 596 printf_filtered ("The program no longer exists.\n");
199b2450 597 gdb_flush (gdb_stdout);
bd5635a1
RP
598#ifdef NO_SINGLE_STEP
599 one_stepped = 0;
600#endif
fcbc95a7
JK
601 goto stop_stepping;
602
603 case TARGET_WAITKIND_STOPPED:
604 /* This is the only case in which we keep going; the above cases
605 end in a continue or goto. */
bd5635a1
RP
606 break;
607 }
de43d7d0 608
48f4903f
JL
609 stop_signal = w.value.sig;
610
611 stop_pc = read_pc_pid (pid);
612
320f93f7
SG
613 /* See if a thread hit a thread-specific breakpoint that was meant for
614 another thread. If so, then step that thread past the breakpoint,
615 and continue it. */
de43d7d0 616
67ac9759 617 if (stop_signal == TARGET_SIGNAL_TRAP
320f93f7 618 && breakpoints_inserted
de43d7d0 619 && breakpoint_here_p (stop_pc - DECR_PC_AFTER_BREAK))
b2f03c30 620 {
320f93f7 621 random_signal = 0;
b2f03c30
JK
622 if (!breakpoint_thread_match (stop_pc - DECR_PC_AFTER_BREAK, pid))
623 {
624 /* Saw a breakpoint, but it was hit by the wrong thread. Just continue. */
48f4903f 625 write_pc_pid (stop_pc - DECR_PC_AFTER_BREAK, pid);
320f93f7
SG
626
627 remove_breakpoints ();
628 target_resume (pid, 1, TARGET_SIGNAL_0); /* Single step */
629 /* FIXME: What if a signal arrives instead of the single-step
630 happening? */
479f0f18
SG
631
632 if (target_wait_hook)
633 target_wait_hook (pid, &w);
634 else
635 target_wait (pid, &w);
320f93f7 636 insert_breakpoints ();
48f4903f
JL
637
638 /* We need to restart all the threads now. */
639 target_resume (-1, 0, TARGET_SIGNAL_0);
b2f03c30
JK
640 continue;
641 }
b2f03c30 642 }
320f93f7
SG
643 else
644 random_signal = 1;
645
646 /* See if something interesting happened to the non-current thread. If
647 so, then switch to that thread, and eventually give control back to
648 the user. */
de43d7d0 649
37c99ddb
JK
650 if (pid != inferior_pid)
651 {
652 int printed = 0;
653
320f93f7
SG
654 /* If it's a random signal for a non-current thread, notify user
655 if he's expressed an interest. */
656
657 if (random_signal
658 && signal_print[stop_signal])
659 {
660 printed = 1;
661 target_terminal_ours_for_output ();
662 printf_filtered ("\nProgram received signal %s, %s.\n",
663 target_signal_to_name (stop_signal),
664 target_signal_to_string (stop_signal));
665 gdb_flush (gdb_stdout);
666 }
667
668 /* If it's not SIGTRAP and not a signal we want to stop for, then
669 continue the thread. */
670
671 if (stop_signal != TARGET_SIGNAL_TRAP
672 && !signal_stop[stop_signal])
37c99ddb 673 {
320f93f7
SG
674 if (printed)
675 target_terminal_inferior ();
37c99ddb 676
320f93f7
SG
677 /* Clear the signal if it should not be passed. */
678 if (signal_program[stop_signal] == 0)
679 stop_signal = TARGET_SIGNAL_0;
680
681 target_resume (pid, 0, stop_signal);
37c99ddb
JK
682 continue;
683 }
320f93f7
SG
684
685 /* It's a SIGTRAP or a signal we're interested in. Switch threads,
686 and fall into the rest of wait_for_inferior(). */
687
2b576293
C
688 /* Save infrun state for the old thread. */
689 save_infrun_state (inferior_pid, prev_pc,
690 prev_func_start, prev_func_name,
691 trap_expected, step_resume_breakpoint,
692 through_sigtramp_breakpoint,
693 step_range_start, step_range_end,
694 step_frame_address, handling_longjmp,
695 another_trap);
696
320f93f7 697 inferior_pid = pid;
2b576293
C
698
699 /* Load infrun state for the new thread. */
700 load_infrun_state (inferior_pid, &prev_pc,
701 &prev_func_start, &prev_func_name,
702 &trap_expected, &step_resume_breakpoint,
703 &through_sigtramp_breakpoint,
704 &step_range_start, &step_range_end,
705 &step_frame_address, &handling_longjmp,
706 &another_trap);
320f93f7
SG
707 printf_filtered ("[Switching to %s]\n", target_pid_to_str (pid));
708
709 flush_cached_frames ();
37c99ddb
JK
710 }
711
bd5635a1
RP
712#ifdef NO_SINGLE_STEP
713 if (one_stepped)
714 single_step (0); /* This actually cleans up the ss */
715#endif /* NO_SINGLE_STEP */
716
999dd04b
JL
717 /* If PC is pointing at a nullified instruction, then step beyond
718 it so that the user won't be confused when GDB appears to be ready
719 to execute it. */
9f739abd
SG
720
721 if (INSTRUCTION_NULLIFIED)
722 {
894d8e69
JL
723 struct target_waitstatus tmpstatus;
724
7dbb5eed 725 registers_changed ();
894d8e69
JL
726 target_resume (pid, 1, TARGET_SIGNAL_0);
727
728 /* We may have received a signal that we want to pass to
729 the inferior; therefore, we must not clobber the waitstatus
730 in W. So we call wait ourselves, then continue the loop
731 at the "have_waited" label. */
732 if (target_wait_hook)
733 target_wait_hook (pid, &tmpstatus);
734 else
735 target_wait (pid, &tmpstatus);
736
7dbb5eed 737
894d8e69 738 goto have_waited;
9f739abd
SG
739 }
740
48f4903f
JL
741#ifdef HAVE_STEPPABLE_WATCHPOINT
742 /* It may not be necessary to disable the watchpoint to stop over
743 it. For example, the PA can (with some kernel cooperation)
744 single step over a watchpoint without disabling the watchpoint. */
745 if (STOPPED_BY_WATCHPOINT (w))
746 {
747 resume (1, 0);
748 continue;
749 }
750#endif
751
752#ifdef HAVE_NONSTEPPABLE_WATCHPOINT
753 /* It is far more common to need to disable a watchpoint
754 to step the inferior over it. FIXME. What else might
755 a debug register or page protection watchpoint scheme need
756 here? */
757 if (STOPPED_BY_WATCHPOINT (w))
758 {
759/* At this point, we are stopped at an instruction which has attempted to write
760 to a piece of memory under control of a watchpoint. The instruction hasn't
761 actually executed yet. If we were to evaluate the watchpoint expression
762 now, we would get the old value, and therefore no change would seem to have
763 occurred.
764
765 In order to make watchpoints work `right', we really need to complete the
766 memory write, and then evaluate the watchpoint expression. The following
767 code does that by removing the watchpoint (actually, all watchpoints and
768 breakpoints), single-stepping the target, re-inserting watchpoints, and then
769 falling through to let normal single-step processing handle proceed. Since
770 this includes evaluating watchpoints, things will come to a stop in the
771 correct manner. */
772
773 write_pc (stop_pc - DECR_PC_AFTER_BREAK);
774
775 remove_breakpoints ();
776 target_resume (pid, 1, TARGET_SIGNAL_0); /* Single step */
777
778 if (target_wait_hook)
779 target_wait_hook (pid, &w);
780 else
781 target_wait (pid, &w);
782 insert_breakpoints ();
783 /* FIXME-maybe: is this cleaner than setting a flag? Does it
784 handle things like signals arriving and other things happening
785 in combination correctly? */
786 goto have_waited;
787 }
788#endif
789
790#ifdef HAVE_CONTINUABLE_WATCHPOINT
791 /* It may be possible to simply continue after a watchpoint. */
792 STOPPED_BY_WATCHPOINT (w);
793#endif
794
bd5635a1
RP
795 stop_func_start = 0;
796 stop_func_name = 0;
797 /* Don't care about return value; stop_func_start and stop_func_name
798 will both be 0 if it doesn't work. */
37c99ddb 799 find_pc_partial_function (stop_pc, &stop_func_name, &stop_func_start,
67ac9759 800 &stop_func_end);
bd5635a1
RP
801 stop_func_start += FUNCTION_START_OFFSET;
802 another_trap = 0;
803 bpstat_clear (&stop_bpstat);
804 stop_step = 0;
805 stop_stack_dummy = 0;
806 stop_print_frame = 1;
bd5635a1
RP
807 random_signal = 0;
808 stopped_by_random_signal = 0;
809 breakpoints_failed = 0;
810
811 /* Look at the cause of the stop, and decide what to do.
812 The alternatives are:
813 1) break; to really stop and return to the debugger,
814 2) drop through to start up again
815 (set another_trap to 1 to single step once)
816 3) set random_signal to 1, and the decision between 1 and 2
817 will be made according to the signal handling tables. */
818
bd5635a1
RP
819 /* First, distinguish signals caused by the debugger from signals
820 that have to do with the program's own actions.
821 Note that breakpoint insns may cause SIGTRAP or SIGILL
822 or SIGEMT, depending on the operating system version.
823 Here we detect when a SIGILL or SIGEMT is really a breakpoint
824 and change it to SIGTRAP. */
825
67ac9759 826 if (stop_signal == TARGET_SIGNAL_TRAP
bd5635a1 827 || (breakpoints_inserted &&
67ac9759
JK
828 (stop_signal == TARGET_SIGNAL_ILL
829 || stop_signal == TARGET_SIGNAL_EMT
e37a6e9c 830 ))
bd5635a1
RP
831 || stop_soon_quietly)
832 {
67ac9759 833 if (stop_signal == TARGET_SIGNAL_TRAP && stop_after_trap)
bd5635a1
RP
834 {
835 stop_print_frame = 0;
836 break;
837 }
838 if (stop_soon_quietly)
839 break;
840
841 /* Don't even think about breakpoints
842 if just proceeded over a breakpoint.
843
844 However, if we are trying to proceed over a breakpoint
bcc37718 845 and end up in sigtramp, then through_sigtramp_breakpoint
bd5635a1
RP
846 will be set and we should check whether we've hit the
847 step breakpoint. */
67ac9759 848 if (stop_signal == TARGET_SIGNAL_TRAP && trap_expected
bcc37718 849 && through_sigtramp_breakpoint == NULL)
bd5635a1
RP
850 bpstat_clear (&stop_bpstat);
851 else
852 {
853 /* See if there is a breakpoint at the current PC. */
cb6b0202 854 stop_bpstat = bpstat_stop_status
479f0f18 855 (&stop_pc,
bd5635a1 856#if DECR_PC_AFTER_BREAK
cb6b0202
JK
857 /* Notice the case of stepping through a jump
858 that lands just after a breakpoint.
859 Don't confuse that with hitting the breakpoint.
860 What we check for is that 1) stepping is going on
861 and 2) the pc before the last insn does not match
862 the address of the breakpoint before the current pc. */
863 (prev_pc != stop_pc - DECR_PC_AFTER_BREAK
864 && CURRENTLY_STEPPING ())
865#else /* DECR_PC_AFTER_BREAK zero */
866 0
867#endif /* DECR_PC_AFTER_BREAK zero */
868 );
869 /* Following in case break condition called a
870 function. */
871 stop_print_frame = 1;
bd5635a1 872 }
fe675038 873
67ac9759 874 if (stop_signal == TARGET_SIGNAL_TRAP)
bd5635a1
RP
875 random_signal
876 = !(bpstat_explains_signal (stop_bpstat)
877 || trap_expected
84d59861 878#ifndef CALL_DUMMY_BREAKPOINT_OFFSET
479f0f18
SG
879 || PC_IN_CALL_DUMMY (stop_pc, read_sp (),
880 FRAME_FP (get_current_frame ()))
84d59861 881#endif /* No CALL_DUMMY_BREAKPOINT_OFFSET. */
fe675038 882 || (step_range_end && step_resume_breakpoint == NULL));
bd5635a1
RP
883 else
884 {
885 random_signal
886 = !(bpstat_explains_signal (stop_bpstat)
bd5635a1
RP
887 /* End of a stack dummy. Some systems (e.g. Sony
888 news) give another signal besides SIGTRAP,
889 so check here as well as above. */
84d59861 890#ifndef CALL_DUMMY_BREAKPOINT_OFFSET
479f0f18
SG
891 || PC_IN_CALL_DUMMY (stop_pc, read_sp (),
892 FRAME_FP (get_current_frame ()))
84d59861 893#endif /* No CALL_DUMMY_BREAKPOINT_OFFSET. */
bd5635a1
RP
894 );
895 if (!random_signal)
67ac9759 896 stop_signal = TARGET_SIGNAL_TRAP;
bd5635a1
RP
897 }
898 }
899 else
900 random_signal = 1;
fe675038 901
bd5635a1
RP
902 /* For the program's own signals, act according to
903 the signal handling tables. */
fe675038 904
bd5635a1
RP
905 if (random_signal)
906 {
907 /* Signal not for debugging purposes. */
908 int printed = 0;
909
910 stopped_by_random_signal = 1;
911
67ac9759 912 if (signal_print[stop_signal])
bd5635a1
RP
913 {
914 printed = 1;
915 target_terminal_ours_for_output ();
1c95d7ab
JK
916 annotate_signal ();
917 printf_filtered ("\nProgram received signal ");
918 annotate_signal_name ();
919 printf_filtered ("%s", target_signal_to_name (stop_signal));
920 annotate_signal_name_end ();
921 printf_filtered (", ");
922 annotate_signal_string ();
923 printf_filtered ("%s", target_signal_to_string (stop_signal));
924 annotate_signal_string_end ();
925 printf_filtered (".\n");
199b2450 926 gdb_flush (gdb_stdout);
bd5635a1 927 }
67ac9759 928 if (signal_stop[stop_signal])
bd5635a1
RP
929 break;
930 /* If not going to stop, give terminal back
931 if we took it away. */
932 else if (printed)
933 target_terminal_inferior ();
b7f81b57 934
101b7f9c
PS
935 /* Clear the signal if it should not be passed. */
936 if (signal_program[stop_signal] == 0)
67ac9759 937 stop_signal = TARGET_SIGNAL_0;
101b7f9c 938
fe675038
JK
939 /* I'm not sure whether this needs to be check_sigtramp2 or
940 whether it could/should be keep_going. */
941 goto check_sigtramp2;
bd5635a1 942 }
30875e1c 943
bd5635a1 944 /* Handle cases caused by hitting a breakpoint. */
fe675038
JK
945 {
946 CORE_ADDR jmp_buf_pc;
29c6dce2
JK
947 struct bpstat_what what;
948
949 what = bpstat_what (stop_bpstat);
bd5635a1 950
84d59861
JK
951 if (what.call_dummy)
952 {
953 stop_stack_dummy = 1;
954#ifdef HP_OS_BUG
955 trap_expected_after_continue = 1;
956#endif
957 }
958
fe675038
JK
959 switch (what.main_action)
960 {
961 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
962 /* If we hit the breakpoint at longjmp, disable it for the
963 duration of this command. Then, install a temporary
964 breakpoint at the target of the jmp_buf. */
965 disable_longjmp_breakpoint();
966 remove_breakpoints ();
967 breakpoints_inserted = 0;
968 if (!GET_LONGJMP_TARGET(&jmp_buf_pc)) goto keep_going;
969
970 /* Need to blow away step-resume breakpoint, as it
971 interferes with us */
972 if (step_resume_breakpoint != NULL)
973 {
974 delete_breakpoint (step_resume_breakpoint);
975 step_resume_breakpoint = NULL;
bcc37718
JK
976 }
977 /* Not sure whether we need to blow this away too, but probably
978 it is like the step-resume breakpoint. */
979 if (through_sigtramp_breakpoint != NULL)
980 {
981 delete_breakpoint (through_sigtramp_breakpoint);
982 through_sigtramp_breakpoint = NULL;
fe675038 983 }
30875e1c 984
101b7f9c 985#if 0
fe675038
JK
986 /* FIXME - Need to implement nested temporary breakpoints */
987 if (step_over_calls > 0)
988 set_longjmp_resume_breakpoint(jmp_buf_pc,
989 get_current_frame());
990 else
30875e1c 991#endif /* 0 */
fe675038
JK
992 set_longjmp_resume_breakpoint(jmp_buf_pc, NULL);
993 handling_longjmp = 1; /* FIXME */
994 goto keep_going;
995
996 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
997 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE:
998 remove_breakpoints ();
999 breakpoints_inserted = 0;
101b7f9c 1000#if 0
fe675038
JK
1001 /* FIXME - Need to implement nested temporary breakpoints */
1002 if (step_over_calls
479f0f18 1003 && (FRAME_FP (get_current_frame ())
fe675038
JK
1004 INNER_THAN step_frame_address))
1005 {
1006 another_trap = 1;
1007 goto keep_going;
1008 }
30875e1c 1009#endif /* 0 */
fe675038
JK
1010 disable_longjmp_breakpoint();
1011 handling_longjmp = 0; /* FIXME */
1012 if (what.main_action == BPSTAT_WHAT_CLEAR_LONGJMP_RESUME)
101b7f9c 1013 break;
fe675038
JK
1014 /* else fallthrough */
1015
1016 case BPSTAT_WHAT_SINGLE:
1017 if (breakpoints_inserted)
1018 remove_breakpoints ();
1019 breakpoints_inserted = 0;
1020 another_trap = 1;
1021 /* Still need to check other stuff, at least the case
1022 where we are stepping and step out of the right range. */
1023 break;
1024
1025 case BPSTAT_WHAT_STOP_NOISY:
1026 stop_print_frame = 1;
bcc37718
JK
1027
1028 /* We are about to nuke the step_resume_breakpoint and
1029 through_sigtramp_breakpoint via the cleanup chain, so
1030 no need to worry about it here. */
1031
fe675038 1032 goto stop_stepping;
101b7f9c 1033
fe675038
JK
1034 case BPSTAT_WHAT_STOP_SILENT:
1035 stop_print_frame = 0;
fe675038 1036
bcc37718
JK
1037 /* We are about to nuke the step_resume_breakpoint and
1038 through_sigtramp_breakpoint via the cleanup chain, so
1039 no need to worry about it here. */
100f92e2 1040
bcc37718 1041 goto stop_stepping;
fe675038 1042
bcc37718 1043 case BPSTAT_WHAT_STEP_RESUME:
fe675038
JK
1044 delete_breakpoint (step_resume_breakpoint);
1045 step_resume_breakpoint = NULL;
bcc37718
JK
1046 break;
1047
1048 case BPSTAT_WHAT_THROUGH_SIGTRAMP:
479f0f18
SG
1049 if (through_sigtramp_breakpoint)
1050 delete_breakpoint (through_sigtramp_breakpoint);
bcc37718 1051 through_sigtramp_breakpoint = NULL;
30875e1c 1052
fe675038
JK
1053 /* If were waiting for a trap, hitting the step_resume_break
1054 doesn't count as getting it. */
1055 if (trap_expected)
1056 another_trap = 1;
bcc37718
JK
1057 break;
1058
87273c71
JL
1059#ifdef SOLIB_ADD
1060 case BPSTAT_WHAT_CHECK_SHLIBS:
1061 {
1062 extern int auto_solib_add;
1063
fa3764e2
JL
1064 /* Remove breakpoints, we eventually want to step over the
1065 shlib event breakpoint, and SOLIB_ADD might adjust
1066 breakpoint addresses via breakpoint_re_set. */
1067 if (breakpoints_inserted)
1068 remove_breakpoints ();
1069 breakpoints_inserted = 0;
1070
87273c71
JL
1071 /* Check for any newly added shared libraries if we're
1072 supposed to be adding them automatically. */
1073 if (auto_solib_add)
11be829f 1074 {
11be829f
JL
1075 /* Switch terminal for any messages produced by
1076 breakpoint_re_set. */
1077 target_terminal_ours_for_output ();
1078 SOLIB_ADD (NULL, 0, NULL);
fa3764e2 1079 re_enable_breakpoints_in_shlibs ();
11be829f
JL
1080 target_terminal_inferior ();
1081 }
87273c71
JL
1082
1083 /* If requested, stop when the dynamic linker notifies
1084 gdb of events. This allows the user to get control
1085 and place breakpoints in initializer routines for
1086 dynamically loaded objects (among other things). */
1087 if (stop_on_solib_events)
1088 {
1089 stop_print_frame = 0;
1090 goto stop_stepping;
1091 }
1092 else
1093 {
1094 /* We want to step over this breakpoint, then keep going. */
1095 another_trap = 1;
87273c71
JL
1096 break;
1097 }
1098 }
1099#endif
1100
bcc37718
JK
1101 case BPSTAT_WHAT_LAST:
1102 /* Not a real code, but listed here to shut up gcc -Wall. */
1103
1104 case BPSTAT_WHAT_KEEP_CHECKING:
1105 break;
30875e1c 1106 }
fe675038 1107 }
30875e1c
SG
1108
1109 /* We come here if we hit a breakpoint but should not
1110 stop for it. Possibly we also were stepping
1111 and should stop for that. So fall through and
1112 test for stepping. But, if not stepping,
1113 do not stop. */
1114
84d59861
JK
1115#ifndef CALL_DUMMY_BREAKPOINT_OFFSET
1116 /* This is the old way of detecting the end of the stack dummy.
1117 An architecture which defines CALL_DUMMY_BREAKPOINT_OFFSET gets
1118 handled above. As soon as we can test it on all of them, all
1119 architectures should define it. */
1120
bd5635a1 1121 /* If this is the breakpoint at the end of a stack dummy,
c9de302b
SG
1122 just stop silently, unless the user was doing an si/ni, in which
1123 case she'd better know what she's doing. */
1124
479f0f18 1125 if (PC_IN_CALL_DUMMY (stop_pc, read_sp (), FRAME_FP (get_current_frame ()))
c9de302b
SG
1126 && !step_range_end)
1127 {
1128 stop_print_frame = 0;
1129 stop_stack_dummy = 1;
bd5635a1 1130#ifdef HP_OS_BUG
c9de302b 1131 trap_expected_after_continue = 1;
bd5635a1 1132#endif
c9de302b
SG
1133 break;
1134 }
84d59861
JK
1135#endif /* No CALL_DUMMY_BREAKPOINT_OFFSET. */
1136
fe675038 1137 if (step_resume_breakpoint)
bd5635a1
RP
1138 /* Having a step-resume breakpoint overrides anything
1139 else having to do with stepping commands until
1140 that breakpoint is reached. */
bcc37718
JK
1141 /* I'm not sure whether this needs to be check_sigtramp2 or
1142 whether it could/should be keep_going. */
fe675038
JK
1143 goto check_sigtramp2;
1144
1145 if (step_range_end == 0)
1146 /* Likewise if we aren't even stepping. */
1147 /* I'm not sure whether this needs to be check_sigtramp2 or
1148 whether it could/should be keep_going. */
1149 goto check_sigtramp2;
1150
bd5635a1 1151 /* If stepping through a line, keep going if still within it. */
fe675038
JK
1152 if (stop_pc >= step_range_start
1153 && stop_pc < step_range_end
3f687c78
SG
1154#if 0
1155/* I haven't a clue what might trigger this clause, and it seems wrong anyway,
1156 so I've disabled it until someone complains. -Stu 10/24/95 */
1157
fe675038
JK
1158 /* The step range might include the start of the
1159 function, so if we are at the start of the
1160 step range and either the stack or frame pointers
1161 just changed, we've stepped outside */
1162 && !(stop_pc == step_range_start
479f0f18
SG
1163 && FRAME_FP (get_current_frame ())
1164 && (read_sp () INNER_THAN step_sp
3f687c78
SG
1165 || FRAME_FP (get_current_frame ()) != step_frame_address))
1166#endif
1167)
bd5635a1 1168 {
fe675038
JK
1169 /* We might be doing a BPSTAT_WHAT_SINGLE and getting a signal.
1170 So definately need to check for sigtramp here. */
1171 goto check_sigtramp2;
bd5635a1 1172 }
fe675038 1173
479f0f18
SG
1174 /* We stepped out of the stepping range. */
1175
1176 /* We can't update step_sp every time through the loop, because
1177 reading the stack pointer would slow down stepping too much.
1178 But we can update it every time we leave the step range. */
1179 update_step_sp = 1;
fe675038
JK
1180
1181 /* Did we just take a signal? */
1182 if (IN_SIGTRAMP (stop_pc, stop_func_name)
1183 && !IN_SIGTRAMP (prev_pc, prev_func_name))
bd5635a1 1184 {
bcc37718
JK
1185 /* We've just taken a signal; go until we are back to
1186 the point where we took it and one more. */
1187
fe675038
JK
1188 /* This code is needed at least in the following case:
1189 The user types "next" and then a signal arrives (before
1190 the "next" is done). */
bcc37718
JK
1191
1192 /* Note that if we are stopped at a breakpoint, then we need
1193 the step_resume breakpoint to override any breakpoints at
1194 the same location, so that we will still step over the
1195 breakpoint even though the signal happened. */
1196
fe675038
JK
1197 {
1198 struct symtab_and_line sr_sal;
1199
1200 sr_sal.pc = prev_pc;
1201 sr_sal.symtab = NULL;
1202 sr_sal.line = 0;
d1c0c6cf 1203 /* We could probably be setting the frame to
479f0f18 1204 step_frame_address; I don't think anyone thought to try it. */
fe675038 1205 step_resume_breakpoint =
bcc37718 1206 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
fe675038
JK
1207 if (breakpoints_inserted)
1208 insert_breakpoints ();
1209 }
bd5635a1 1210
fe675038
JK
1211 /* If this is stepi or nexti, make sure that the stepping range
1212 gets us past that instruction. */
1213 if (step_range_end == 1)
1214 /* FIXME: Does this run afoul of the code below which, if
1215 we step into the middle of a line, resets the stepping
1216 range? */
1217 step_range_end = (step_range_start = prev_pc) + 1;
101b7f9c 1218
fe675038
JK
1219 remove_breakpoints_on_following_step = 1;
1220 goto keep_going;
1221 }
30875e1c 1222
3f687c78
SG
1223#if 0
1224 /* I disabled this test because it was too complicated and slow. The
1225 SKIP_PROLOGUE was especially slow, because it caused unnecessary
1226 prologue examination on various architectures. The code in the #else
1227 clause has been tested on the Sparc, Mips, PA, and Power
1228 architectures, so it's pretty likely to be correct. -Stu 10/24/95 */
1229
479f0f18
SG
1230 /* See if we left the step range due to a subroutine call that
1231 we should proceed to the end of. */
1232
fe675038
JK
1233 if (stop_func_start)
1234 {
320f93f7
SG
1235 struct symtab *s;
1236
fe675038
JK
1237 /* Do this after the IN_SIGTRAMP check; it might give
1238 an error. */
1239 prologue_pc = stop_func_start;
320f93f7
SG
1240
1241 /* Don't skip the prologue if this is assembly source */
1242 s = find_pc_symtab (stop_pc);
1243 if (s && s->language != language_asm)
1244 SKIP_PROLOGUE (prologue_pc);
fe675038 1245 }
30875e1c 1246
c0c14c1e
JK
1247 if ((/* Might be a non-recursive call. If the symbols are missing
1248 enough that stop_func_start == prev_func_start even though
1249 they are really two functions, we will treat some calls as
1250 jumps. */
1251 stop_func_start != prev_func_start
1252
1253 /* Might be a recursive call if either we have a prologue
1254 or the call instruction itself saves the PC on the stack. */
1255 || prologue_pc != stop_func_start
479f0f18 1256 || read_sp () != step_sp)
199b2450
TL
1257 && (/* PC is completely out of bounds of any known objfiles. Treat
1258 like a subroutine call. */
1259 ! stop_func_start
c0c14c1e 1260
f1619234 1261 /* If we do a call, we will be at the start of a function... */
c0c14c1e 1262 || stop_pc == stop_func_start
f1619234
JK
1263
1264 /* ...except on the Alpha with -O (and also Irix 5 and
1265 perhaps others), in which we might call the address
1266 after the load of gp. Since prologues don't contain
1267 calls, we can't return to within one, and we don't
1268 jump back into them, so this check is OK. */
c0c14c1e 1269
c0c14c1e 1270 || stop_pc < prologue_pc
d747e0af 1271
479f0f18
SG
1272 /* ...and if it is a leaf function, the prologue might
1273 consist of gp loading only, so the call transfers to
1274 the first instruction after the prologue. */
1275 || (stop_pc == prologue_pc
1276
1277 /* Distinguish this from the case where we jump back
1278 to the first instruction after the prologue,
1279 within a function. */
1280 && stop_func_start != prev_func_start)
1281
c0c14c1e
JK
1282 /* If we end up in certain places, it means we did a subroutine
1283 call. I'm not completely sure this is necessary now that we
1284 have the above checks with stop_func_start (and now that
100f92e2 1285 find_pc_partial_function is pickier). */
4cc1b3f7 1286 || IN_SOLIB_CALL_TRAMPOLINE (stop_pc, stop_func_name)
c0c14c1e
JK
1287
1288 /* If none of the above apply, it is a jump within a function,
1289 or a return from a subroutine. The other case is longjmp,
1290 which can no longer happen here as long as the
1291 handling_longjmp stuff is working. */
1292 ))
320f93f7 1293#else
87273c71
JL
1294 /* This test is a much more streamlined, (but hopefully correct)
1295 replacement for the code above. It's been tested on the Sparc,
1296 Mips, PA, and Power architectures with good results. */
320f93f7 1297
3f687c78
SG
1298 if (stop_pc == stop_func_start /* Quick test */
1299 || in_prologue (stop_pc, stop_func_start)
1300 || IN_SOLIB_CALL_TRAMPOLINE (stop_pc, stop_func_name)
1301 || stop_func_start == 0)
320f93f7 1302#endif
3f687c78 1303
fe675038
JK
1304 {
1305 /* It's a subroutine call. */
fee44494 1306
fe675038
JK
1307 if (step_over_calls == 0)
1308 {
1309 /* I presume that step_over_calls is only 0 when we're
1310 supposed to be stepping at the assembly language level
1311 ("stepi"). Just stop. */
1312 stop_step = 1;
1313 break;
1314 }
fee44494 1315
fe675038
JK
1316 if (step_over_calls > 0)
1317 /* We're doing a "next". */
1318 goto step_over_function;
1319
1320 /* If we are in a function call trampoline (a stub between
1321 the calling routine and the real function), locate the real
1322 function. That's what tells us (a) whether we want to step
1323 into it at all, and (b) what prologue we want to run to
1324 the end of, if we do step into it. */
1325 tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
1326 if (tmp != 0)
1327 stop_func_start = tmp;
87273c71
JL
1328 else
1329 {
1330 tmp = DYNAMIC_TRAMPOLINE_NEXTPC (stop_pc);
1331 if (tmp)
1332 {
1333 struct symtab_and_line xxx;
1334
1335 xxx.pc = tmp;
1336 xxx.symtab = NULL;
1337 xxx.line = 0;
1338 step_resume_breakpoint =
1339 set_momentary_breakpoint (xxx, NULL, bp_step_resume);
1340 insert_breakpoints ();
1341 goto keep_going;
1342 }
1343 }
fe675038
JK
1344
1345 /* If we have line number information for the function we
1346 are thinking of stepping into, step into it.
1347
1348 If there are several symtabs at that PC (e.g. with include
1349 files), just want to know whether *any* of them have line
1350 numbers. find_pc_line handles this. */
1351 {
1352 struct symtab_and_line tmp_sal;
1353
1354 tmp_sal = find_pc_line (stop_func_start, 0);
1355 if (tmp_sal.line != 0)
1356 goto step_into_function;
1357 }
d747e0af
MT
1358
1359step_over_function:
fe675038
JK
1360 /* A subroutine call has happened. */
1361 {
1362 /* Set a special breakpoint after the return */
1363 struct symtab_and_line sr_sal;
1364 sr_sal.pc =
1365 ADDR_BITS_REMOVE
1366 (SAVED_PC_AFTER_CALL (get_current_frame ()));
1367 sr_sal.symtab = NULL;
1368 sr_sal.line = 0;
1369 step_resume_breakpoint =
1370 set_momentary_breakpoint (sr_sal, get_current_frame (),
1371 bp_step_resume);
479f0f18 1372 step_resume_breakpoint->frame = step_frame_address;
fe675038
JK
1373 if (breakpoints_inserted)
1374 insert_breakpoints ();
1375 }
1376 goto keep_going;
d747e0af
MT
1377
1378step_into_function:
fe675038
JK
1379 /* Subroutine call with source code we should not step over.
1380 Do step to the first line of code in it. */
320f93f7
SG
1381 {
1382 struct symtab *s;
1383
1384 s = find_pc_symtab (stop_pc);
1385 if (s && s->language != language_asm)
1386 SKIP_PROLOGUE (stop_func_start);
1387 }
fe675038
JK
1388 sal = find_pc_line (stop_func_start, 0);
1389 /* Use the step_resume_break to step until
1390 the end of the prologue, even if that involves jumps
1391 (as it seems to on the vax under 4.2). */
1392 /* If the prologue ends in the middle of a source line,
67ac9759
JK
1393 continue to the end of that source line (if it is still
1394 within the function). Otherwise, just go to end of prologue. */
bd5635a1 1395#ifdef PROLOGUE_FIRSTLINE_OVERLAP
fe675038
JK
1396 /* no, don't either. It skips any code that's
1397 legitimately on the first line. */
bd5635a1 1398#else
67ac9759 1399 if (sal.end && sal.pc != stop_func_start && sal.end < stop_func_end)
fe675038 1400 stop_func_start = sal.end;
bd5635a1 1401#endif
d747e0af 1402
fe675038
JK
1403 if (stop_func_start == stop_pc)
1404 {
1405 /* We are already there: stop now. */
1406 stop_step = 1;
1407 break;
1408 }
1409 else
1410 /* Put the step-breakpoint there and go until there. */
1411 {
1412 struct symtab_and_line sr_sal;
1413
1414 sr_sal.pc = stop_func_start;
1415 sr_sal.symtab = NULL;
1416 sr_sal.line = 0;
1417 /* Do not specify what the fp should be when we stop
1418 since on some machines the prologue
1419 is where the new fp value is established. */
1420 step_resume_breakpoint =
84d59861 1421 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
fe675038
JK
1422 if (breakpoints_inserted)
1423 insert_breakpoints ();
1424
1425 /* And make sure stepping stops right away then. */
1426 step_range_end = step_range_start;
bd5635a1 1427 }
fe675038
JK
1428 goto keep_going;
1429 }
d747e0af 1430
b2f03c30 1431 /* We've wandered out of the step range. */
d747e0af 1432
fe675038
JK
1433 sal = find_pc_line(stop_pc, 0);
1434
1435 if (step_range_end == 1)
1436 {
1437 /* It is stepi or nexti. We always want to stop stepping after
1438 one instruction. */
1439 stop_step = 1;
1440 break;
1441 }
1442
4cc1b3f7
JK
1443 /* If we're in the return path from a shared library trampoline,
1444 we want to proceed through the trampoline when stepping. */
1445 if (IN_SOLIB_RETURN_TRAMPOLINE(stop_pc, stop_func_name))
1446 {
1447 CORE_ADDR tmp;
1448
1449 /* Determine where this trampoline returns. */
1450 tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
1451
1452 /* Only proceed through if we know where it's going. */
1453 if (tmp)
1454 {
1455 /* And put the step-breakpoint there and go until there. */
1456 struct symtab_and_line sr_sal;
1457
1458 sr_sal.pc = tmp;
1459 sr_sal.symtab = NULL;
1460 sr_sal.line = 0;
1461 /* Do not specify what the fp should be when we stop
1462 since on some machines the prologue
1463 is where the new fp value is established. */
1464 step_resume_breakpoint =
1465 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
1466 if (breakpoints_inserted)
1467 insert_breakpoints ();
1468
1469 /* Restart without fiddling with the step ranges or
1470 other state. */
1471 goto keep_going;
1472 }
1473 }
1474
fe675038
JK
1475 if (sal.line == 0)
1476 {
1477 /* We have no line number information. That means to stop
1478 stepping (does this always happen right after one instruction,
1479 when we do "s" in a function with no line numbers,
1480 or can this happen as a result of a return or longjmp?). */
1481 stop_step = 1;
1482 break;
1483 }
1484
b2f03c30
JK
1485 if (stop_pc == sal.pc
1486 && (current_line != sal.line || current_symtab != sal.symtab))
fe675038
JK
1487 {
1488 /* We are at the start of a different line. So stop. Note that
1489 we don't stop if we step into the middle of a different line.
1490 That is said to make things like for (;;) statements work
1491 better. */
1492 stop_step = 1;
1493 break;
bd5635a1
RP
1494 }
1495
fe675038
JK
1496 /* We aren't done stepping.
1497
1498 Optimize by setting the stepping range to the line.
1499 (We might not be in the original line, but if we entered a
1500 new line in mid-statement, we continue stepping. This makes
1501 things like for(;;) statements work better.) */
67ac9759
JK
1502
1503 if (stop_func_end && sal.end >= stop_func_end)
1504 {
1505 /* If this is the last line of the function, don't keep stepping
1506 (it would probably step us out of the function).
1507 This is particularly necessary for a one-line function,
1508 in which after skipping the prologue we better stop even though
1509 we will be in mid-line. */
1510 stop_step = 1;
1511 break;
1512 }
fe675038
JK
1513 step_range_start = sal.pc;
1514 step_range_end = sal.end;
1515 goto keep_going;
1516
1517 check_sigtramp2:
d747e0af
MT
1518 if (trap_expected
1519 && IN_SIGTRAMP (stop_pc, stop_func_name)
1520 && !IN_SIGTRAMP (prev_pc, prev_func_name))
bd5635a1
RP
1521 {
1522 /* What has happened here is that we have just stepped the inferior
1523 with a signal (because it is a signal which shouldn't make
1524 us stop), thus stepping into sigtramp.
1525
1526 So we need to set a step_resume_break_address breakpoint
fe675038
JK
1527 and continue until we hit it, and then step. FIXME: This should
1528 be more enduring than a step_resume breakpoint; we should know
1529 that we will later need to keep going rather than re-hitting
1530 the breakpoint here (see testsuite/gdb.t06/signals.exp where
1531 it says "exceedingly difficult"). */
1532 struct symtab_and_line sr_sal;
1533
1534 sr_sal.pc = prev_pc;
1535 sr_sal.symtab = NULL;
1536 sr_sal.line = 0;
bcc37718
JK
1537 /* We perhaps could set the frame if we kept track of what
1538 the frame corresponding to prev_pc was. But we don't,
1539 so don't. */
1540 through_sigtramp_breakpoint =
1541 set_momentary_breakpoint (sr_sal, NULL, bp_through_sigtramp);
bd5635a1 1542 if (breakpoints_inserted)
fe675038
JK
1543 insert_breakpoints ();
1544
bd5635a1
RP
1545 remove_breakpoints_on_following_step = 1;
1546 another_trap = 1;
1547 }
1548
30875e1c 1549 keep_going:
fe675038
JK
1550 /* Come to this label when you need to resume the inferior.
1551 It's really much cleaner to do a goto than a maze of if-else
1552 conditions. */
30875e1c 1553
bd5635a1
RP
1554 /* Save the pc before execution, to compare with pc after stop. */
1555 prev_pc = read_pc (); /* Might have been DECR_AFTER_BREAK */
1556 prev_func_start = stop_func_start; /* Ok, since if DECR_PC_AFTER
1557 BREAK is defined, the
1558 original pc would not have
1559 been at the start of a
1560 function. */
1561 prev_func_name = stop_func_name;
479f0f18
SG
1562
1563 if (update_step_sp)
1564 step_sp = read_sp ();
1565 update_step_sp = 0;
bd5635a1
RP
1566
1567 /* If we did not do break;, it means we should keep
1568 running the inferior and not return to debugger. */
1569
67ac9759 1570 if (trap_expected && stop_signal != TARGET_SIGNAL_TRAP)
bd5635a1
RP
1571 {
1572 /* We took a signal (which we are supposed to pass through to
1573 the inferior, else we'd have done a break above) and we
1574 haven't yet gotten our trap. Simply continue. */
cb6b0202 1575 resume (CURRENTLY_STEPPING (), stop_signal);
bd5635a1
RP
1576 }
1577 else
1578 {
1579 /* Either the trap was not expected, but we are continuing
1580 anyway (the user asked that this signal be passed to the
1581 child)
1582 -- or --
1583 The signal was SIGTRAP, e.g. it was our signal, but we
1584 decided we should resume from it.
1585
1586 We're going to run this baby now!
1587
1588 Insert breakpoints now, unless we are trying
1589 to one-proceed past a breakpoint. */
1590 /* If we've just finished a special step resume and we don't
1591 want to hit a breakpoint, pull em out. */
d1c0c6cf
JK
1592 if (step_resume_breakpoint == NULL
1593 && through_sigtramp_breakpoint == NULL
1594 && remove_breakpoints_on_following_step)
bd5635a1
RP
1595 {
1596 remove_breakpoints_on_following_step = 0;
1597 remove_breakpoints ();
1598 breakpoints_inserted = 0;
1599 }
1600 else if (!breakpoints_inserted &&
bcc37718 1601 (through_sigtramp_breakpoint != NULL || !another_trap))
bd5635a1 1602 {
bd5635a1
RP
1603 breakpoints_failed = insert_breakpoints ();
1604 if (breakpoints_failed)
1605 break;
1606 breakpoints_inserted = 1;
1607 }
1608
1609 trap_expected = another_trap;
1610
67ac9759
JK
1611 if (stop_signal == TARGET_SIGNAL_TRAP)
1612 stop_signal = TARGET_SIGNAL_0;
bd5635a1
RP
1613
1614#ifdef SHIFT_INST_REGS
1615 /* I'm not sure when this following segment applies. I do know, now,
1616 that we shouldn't rewrite the regs when we were stopped by a
1617 random signal from the inferior process. */
cef4c2e7
PS
1618 /* FIXME: Shouldn't this be based on the valid bit of the SXIP?
1619 (this is only used on the 88k). */
bd5635a1 1620
d11c44f1 1621 if (!bpstat_explains_signal (stop_bpstat)
67ac9759 1622 && (stop_signal != TARGET_SIGNAL_CHLD)
bd5635a1 1623 && !stopped_by_random_signal)
07a5991a 1624 SHIFT_INST_REGS();
bd5635a1
RP
1625#endif /* SHIFT_INST_REGS */
1626
cb6b0202 1627 resume (CURRENTLY_STEPPING (), stop_signal);
bd5635a1
RP
1628 }
1629 }
30875e1c
SG
1630
1631 stop_stepping:
bd5635a1
RP
1632 if (target_has_execution)
1633 {
1634 /* Assuming the inferior still exists, set these up for next
1635 time, just like we did above if we didn't break out of the
1636 loop. */
1637 prev_pc = read_pc ();
1638 prev_func_start = stop_func_start;
1639 prev_func_name = stop_func_name;
bd5635a1 1640 }
fe675038 1641 do_cleanups (old_cleanups);
bd5635a1
RP
1642}
1643\f
1644/* Here to return control to GDB when the inferior stops for real.
1645 Print appropriate messages, remove breakpoints, give terminal our modes.
1646
1647 STOP_PRINT_FRAME nonzero means print the executing frame
1648 (pc, function, args, file, line number and line text).
1649 BREAKPOINTS_FAILED nonzero means stop was due to error
1650 attempting to insert breakpoints. */
1651
1652void
1653normal_stop ()
1654{
1655 /* Make sure that the current_frame's pc is correct. This
1656 is a correction for setting up the frame info before doing
1657 DECR_PC_AFTER_BREAK */
3f0184ac 1658 if (target_has_execution && get_current_frame())
bd5635a1
RP
1659 (get_current_frame ())->pc = read_pc ();
1660
1661 if (breakpoints_failed)
1662 {
1663 target_terminal_ours_for_output ();
1664 print_sys_errmsg ("ptrace", breakpoints_failed);
e37a6e9c 1665 printf_filtered ("Stopped; cannot insert breakpoints.\n\
bd5635a1
RP
1666The same program may be running in another process.\n");
1667 }
1668
bd5635a1
RP
1669 if (target_has_execution && breakpoints_inserted)
1670 if (remove_breakpoints ())
1671 {
1672 target_terminal_ours_for_output ();
e37a6e9c 1673 printf_filtered ("Cannot remove breakpoints because program is no longer writable.\n\
bd5635a1
RP
1674It might be running in another process.\n\
1675Further execution is probably impossible.\n");
1676 }
1677
1678 breakpoints_inserted = 0;
1679
1680 /* Delete the breakpoint we stopped at, if it wants to be deleted.
1681 Delete any breakpoint that is to be deleted at the next stop. */
1682
1683 breakpoint_auto_delete (stop_bpstat);
1684
1685 /* If an auto-display called a function and that got a signal,
1686 delete that auto-display to avoid an infinite recursion. */
1687
1688 if (stopped_by_random_signal)
1689 disable_current_display ();
1690
1691 if (step_multi && stop_step)
1c95d7ab 1692 goto done;
bd5635a1
RP
1693
1694 target_terminal_ours ();
1695
11be829f
JL
1696 if (stop_bpstat
1697 && stop_bpstat->breakpoint_at
1698 && stop_bpstat->breakpoint_at->type == bp_shlib_event)
87273c71
JL
1699 printf_filtered ("Stopped due to shared library event\n");
1700
3950a34e
RP
1701 /* Look up the hook_stop and run it if it exists. */
1702
1703 if (stop_command->hook)
1704 {
1705 catch_errors (hook_stop_stub, (char *)stop_command->hook,
fee44494 1706 "Error while running hook_stop:\n", RETURN_MASK_ALL);
3950a34e
RP
1707 }
1708
bd5635a1 1709 if (!target_has_stack)
1c95d7ab 1710 goto done;
bd5635a1
RP
1711
1712 /* Select innermost stack frame except on return from a stack dummy routine,
1515ff18
JG
1713 or if the program has exited. Print it without a level number if
1714 we have changed functions or hit a breakpoint. Print source line
1715 if we have one. */
bd5635a1
RP
1716 if (!stop_stack_dummy)
1717 {
479f0f18
SG
1718 select_frame (get_current_frame (), 0);
1719
bd5635a1
RP
1720 if (stop_print_frame)
1721 {
1515ff18
JG
1722 int source_only;
1723
1724 source_only = bpstat_print (stop_bpstat);
1725 source_only = source_only ||
1726 ( stop_step
479f0f18 1727 && step_frame_address == FRAME_FP (get_current_frame ())
1515ff18
JG
1728 && step_start_function == find_pc_function (stop_pc));
1729
1730 print_stack_frame (selected_frame, -1, source_only? -1: 1);
bd5635a1
RP
1731
1732 /* Display the auto-display expressions. */
1733 do_displays ();
1734 }
1735 }
1736
1737 /* Save the function value return registers, if we care.
1738 We might be about to restore their previous contents. */
1739 if (proceed_to_finish)
1740 read_register_bytes (0, stop_registers, REGISTER_BYTES);
1741
1742 if (stop_stack_dummy)
1743 {
1744 /* Pop the empty frame that contains the stack dummy.
1745 POP_FRAME ends with a setting of the current frame, so we
1746 can use that next. */
1747 POP_FRAME;
f1de67d3
PS
1748 /* Set stop_pc to what it was before we called the function. Can't rely
1749 on restore_inferior_status because that only gets called if we don't
1750 stop in the called function. */
1751 stop_pc = read_pc();
bd5635a1
RP
1752 select_frame (get_current_frame (), 0);
1753 }
1c95d7ab
JK
1754 done:
1755 annotate_stopped ();
bd5635a1 1756}
3950a34e
RP
1757
1758static int
1759hook_stop_stub (cmd)
1760 char *cmd;
1761{
1762 execute_user_command ((struct cmd_list_element *)cmd, 0);
a8a69e63 1763 return (0);
3950a34e 1764}
bd5635a1 1765\f
cc221e76
FF
1766int signal_stop_state (signo)
1767 int signo;
1768{
67ac9759 1769 return signal_stop[signo];
cc221e76
FF
1770}
1771
1772int signal_print_state (signo)
1773 int signo;
1774{
67ac9759 1775 return signal_print[signo];
cc221e76
FF
1776}
1777
1778int signal_pass_state (signo)
1779 int signo;
1780{
67ac9759 1781 return signal_program[signo];
cc221e76
FF
1782}
1783
bd5635a1
RP
1784static void
1785sig_print_header ()
1786{
67ac9759
JK
1787 printf_filtered ("\
1788Signal Stop\tPrint\tPass to program\tDescription\n");
bd5635a1
RP
1789}
1790
1791static void
67ac9759
JK
1792sig_print_info (oursig)
1793 enum target_signal oursig;
bd5635a1 1794{
67ac9759
JK
1795 char *name = target_signal_to_name (oursig);
1796 printf_filtered ("%s", name);
1797 printf_filtered ("%*.*s ", 13 - strlen (name), 13 - strlen (name),
1798 " ");
1799 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
1800 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
1801 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
1802 printf_filtered ("%s\n", target_signal_to_string (oursig));
bd5635a1
RP
1803}
1804
1805/* Specify how various signals in the inferior should be handled. */
1806
1807static void
1808handle_command (args, from_tty)
1809 char *args;
1810 int from_tty;
1811{
072b552a
JG
1812 char **argv;
1813 int digits, wordlen;
1814 int sigfirst, signum, siglast;
67ac9759 1815 enum target_signal oursig;
072b552a
JG
1816 int allsigs;
1817 int nsigs;
1818 unsigned char *sigs;
1819 struct cleanup *old_chain;
1820
1821 if (args == NULL)
1822 {
1823 error_no_arg ("signal to handle");
1824 }
bd5635a1 1825
072b552a
JG
1826 /* Allocate and zero an array of flags for which signals to handle. */
1827
67ac9759 1828 nsigs = (int)TARGET_SIGNAL_LAST;
072b552a
JG
1829 sigs = (unsigned char *) alloca (nsigs);
1830 memset (sigs, 0, nsigs);
bd5635a1 1831
072b552a
JG
1832 /* Break the command line up into args. */
1833
1834 argv = buildargv (args);
1835 if (argv == NULL)
bd5635a1 1836 {
072b552a
JG
1837 nomem (0);
1838 }
1839 old_chain = make_cleanup (freeargv, (char *) argv);
bd5635a1 1840
67ac9759 1841 /* Walk through the args, looking for signal oursigs, signal names, and
072b552a
JG
1842 actions. Signal numbers and signal names may be interspersed with
1843 actions, with the actions being performed for all signals cumulatively
1844 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
bd5635a1 1845
072b552a
JG
1846 while (*argv != NULL)
1847 {
1848 wordlen = strlen (*argv);
1849 for (digits = 0; isdigit ((*argv)[digits]); digits++) {;}
1850 allsigs = 0;
1851 sigfirst = siglast = -1;
1852
1853 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
1854 {
1855 /* Apply action to all signals except those used by the
1856 debugger. Silently skip those. */
1857 allsigs = 1;
1858 sigfirst = 0;
1859 siglast = nsigs - 1;
1860 }
1861 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
1862 {
1863 SET_SIGS (nsigs, sigs, signal_stop);
1864 SET_SIGS (nsigs, sigs, signal_print);
1865 }
1866 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
1867 {
1868 UNSET_SIGS (nsigs, sigs, signal_program);
1869 }
1870 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
1871 {
1872 SET_SIGS (nsigs, sigs, signal_print);
1873 }
1874 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
1875 {
1876 SET_SIGS (nsigs, sigs, signal_program);
1877 }
1878 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
1879 {
1880 UNSET_SIGS (nsigs, sigs, signal_stop);
1881 }
1882 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
1883 {
1884 SET_SIGS (nsigs, sigs, signal_program);
1885 }
1886 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
1887 {
1888 UNSET_SIGS (nsigs, sigs, signal_print);
1889 UNSET_SIGS (nsigs, sigs, signal_stop);
1890 }
1891 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
1892 {
1893 UNSET_SIGS (nsigs, sigs, signal_program);
1894 }
1895 else if (digits > 0)
bd5635a1 1896 {
67ac9759
JK
1897 /* It is numeric. The numeric signal refers to our own internal
1898 signal numbering from target.h, not to host/target signal number.
1899 This is a feature; users really should be using symbolic names
1900 anyway, and the common ones like SIGHUP, SIGINT, SIGALRM, etc.
1901 will work right anyway. */
1902
c66ed884 1903 sigfirst = siglast = (int) target_signal_from_command (atoi (*argv));
072b552a 1904 if ((*argv)[digits] == '-')
bd5635a1 1905 {
c66ed884
SG
1906 siglast =
1907 (int) target_signal_from_command (atoi ((*argv) + digits + 1));
bd5635a1 1908 }
072b552a 1909 if (sigfirst > siglast)
bd5635a1 1910 {
072b552a
JG
1911 /* Bet he didn't figure we'd think of this case... */
1912 signum = sigfirst;
1913 sigfirst = siglast;
1914 siglast = signum;
bd5635a1 1915 }
bd5635a1 1916 }
072b552a 1917 else
bd5635a1 1918 {
fcbc95a7
JK
1919 oursig = target_signal_from_name (*argv);
1920 if (oursig != TARGET_SIGNAL_UNKNOWN)
1921 {
1922 sigfirst = siglast = (int)oursig;
1923 }
1924 else
1925 {
1926 /* Not a number and not a recognized flag word => complain. */
1927 error ("Unrecognized or ambiguous flag word: \"%s\".", *argv);
1928 }
bd5635a1 1929 }
072b552a
JG
1930
1931 /* If any signal numbers or symbol names were found, set flags for
1932 which signals to apply actions to. */
1933
1934 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
bd5635a1 1935 {
67ac9759 1936 switch ((enum target_signal)signum)
072b552a 1937 {
67ac9759
JK
1938 case TARGET_SIGNAL_TRAP:
1939 case TARGET_SIGNAL_INT:
072b552a
JG
1940 if (!allsigs && !sigs[signum])
1941 {
67ac9759
JK
1942 if (query ("%s is used by the debugger.\n\
1943Are you sure you want to change it? ",
1944 target_signal_to_name
1945 ((enum target_signal)signum)))
072b552a
JG
1946 {
1947 sigs[signum] = 1;
1948 }
1949 else
1950 {
199b2450
TL
1951 printf_unfiltered ("Not confirmed, unchanged.\n");
1952 gdb_flush (gdb_stdout);
072b552a
JG
1953 }
1954 }
1955 break;
c66ed884
SG
1956 case TARGET_SIGNAL_0:
1957 case TARGET_SIGNAL_DEFAULT:
1958 case TARGET_SIGNAL_UNKNOWN:
1959 /* Make sure that "all" doesn't print these. */
1960 break;
072b552a
JG
1961 default:
1962 sigs[signum] = 1;
1963 break;
1964 }
bd5635a1
RP
1965 }
1966
072b552a 1967 argv++;
bd5635a1
RP
1968 }
1969
de43d7d0 1970 target_notice_signals(inferior_pid);
cc221e76 1971
bd5635a1
RP
1972 if (from_tty)
1973 {
1974 /* Show the results. */
1975 sig_print_header ();
072b552a
JG
1976 for (signum = 0; signum < nsigs; signum++)
1977 {
1978 if (sigs[signum])
1979 {
1980 sig_print_info (signum);
1981 }
1982 }
bd5635a1 1983 }
072b552a
JG
1984
1985 do_cleanups (old_chain);
bd5635a1
RP
1986}
1987
67ac9759
JK
1988/* Print current contents of the tables set by the handle command.
1989 It is possible we should just be printing signals actually used
1990 by the current target (but for things to work right when switching
1991 targets, all signals should be in the signal tables). */
bd5635a1
RP
1992
1993static void
e37a6e9c 1994signals_info (signum_exp, from_tty)
bd5635a1 1995 char *signum_exp;
e37a6e9c 1996 int from_tty;
bd5635a1 1997{
67ac9759 1998 enum target_signal oursig;
bd5635a1
RP
1999 sig_print_header ();
2000
2001 if (signum_exp)
2002 {
2003 /* First see if this is a symbol name. */
67ac9759
JK
2004 oursig = target_signal_from_name (signum_exp);
2005 if (oursig == TARGET_SIGNAL_UNKNOWN)
bd5635a1 2006 {
c66ed884
SG
2007 /* No, try numeric. */
2008 oursig =
2009 target_signal_from_command (parse_and_eval_address (signum_exp));
bd5635a1 2010 }
67ac9759 2011 sig_print_info (oursig);
bd5635a1
RP
2012 return;
2013 }
2014
2015 printf_filtered ("\n");
db4340a6 2016 /* These ugly casts brought to you by the native VAX compiler. */
2fe3b329 2017 for (oursig = TARGET_SIGNAL_FIRST;
db4340a6
JK
2018 (int)oursig < (int)TARGET_SIGNAL_LAST;
2019 oursig = (enum target_signal)((int)oursig + 1))
bd5635a1
RP
2020 {
2021 QUIT;
2022
fcbc95a7
JK
2023 if (oursig != TARGET_SIGNAL_UNKNOWN
2024 && oursig != TARGET_SIGNAL_DEFAULT
2025 && oursig != TARGET_SIGNAL_0)
67ac9759 2026 sig_print_info (oursig);
bd5635a1
RP
2027 }
2028
2029 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
2030}
2031\f
2032/* Save all of the information associated with the inferior<==>gdb
2033 connection. INF_STATUS is a pointer to a "struct inferior_status"
2034 (defined in inferior.h). */
2035
2036void
2037save_inferior_status (inf_status, restore_stack_info)
2038 struct inferior_status *inf_status;
2039 int restore_stack_info;
2040{
bd5635a1
RP
2041 inf_status->stop_signal = stop_signal;
2042 inf_status->stop_pc = stop_pc;
bd5635a1
RP
2043 inf_status->stop_step = stop_step;
2044 inf_status->stop_stack_dummy = stop_stack_dummy;
2045 inf_status->stopped_by_random_signal = stopped_by_random_signal;
2046 inf_status->trap_expected = trap_expected;
2047 inf_status->step_range_start = step_range_start;
2048 inf_status->step_range_end = step_range_end;
2049 inf_status->step_frame_address = step_frame_address;
2050 inf_status->step_over_calls = step_over_calls;
bd5635a1
RP
2051 inf_status->stop_after_trap = stop_after_trap;
2052 inf_status->stop_soon_quietly = stop_soon_quietly;
2053 /* Save original bpstat chain here; replace it with copy of chain.
2054 If caller's caller is walking the chain, they'll be happier if we
2055 hand them back the original chain when restore_i_s is called. */
2056 inf_status->stop_bpstat = stop_bpstat;
2057 stop_bpstat = bpstat_copy (stop_bpstat);
2058 inf_status->breakpoint_proceeded = breakpoint_proceeded;
2059 inf_status->restore_stack_info = restore_stack_info;
2060 inf_status->proceed_to_finish = proceed_to_finish;
2061
072b552a 2062 memcpy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
37c99ddb
JK
2063
2064 read_register_bytes (0, inf_status->registers, REGISTER_BYTES);
2065
bd5635a1
RP
2066 record_selected_frame (&(inf_status->selected_frame_address),
2067 &(inf_status->selected_level));
2068 return;
2069}
2070
37c99ddb 2071struct restore_selected_frame_args {
4cc1b3f7 2072 CORE_ADDR frame_address;
37c99ddb
JK
2073 int level;
2074};
2075
2076static int restore_selected_frame PARAMS ((char *));
2077
2078/* Restore the selected frame. args is really a struct
2079 restore_selected_frame_args * (declared as char * for catch_errors)
2080 telling us what frame to restore. Returns 1 for success, or 0 for
2081 failure. An error message will have been printed on error. */
4cc1b3f7 2082
37c99ddb
JK
2083static int
2084restore_selected_frame (args)
2085 char *args;
2086{
2087 struct restore_selected_frame_args *fr =
2088 (struct restore_selected_frame_args *) args;
4cc1b3f7 2089 struct frame_info *frame;
37c99ddb
JK
2090 int level = fr->level;
2091
4cc1b3f7 2092 frame = find_relative_frame (get_current_frame (), &level);
37c99ddb
JK
2093
2094 /* If inf_status->selected_frame_address is NULL, there was no
2095 previously selected frame. */
4cc1b3f7
JK
2096 if (frame == NULL ||
2097 FRAME_FP (frame) != fr->frame_address ||
37c99ddb
JK
2098 level != 0)
2099 {
2100 warning ("Unable to restore previously selected frame.\n");
2101 return 0;
2102 }
4cc1b3f7 2103 select_frame (frame, fr->level);
37c99ddb
JK
2104 return(1);
2105}
2106
bd5635a1
RP
2107void
2108restore_inferior_status (inf_status)
2109 struct inferior_status *inf_status;
2110{
bd5635a1
RP
2111 stop_signal = inf_status->stop_signal;
2112 stop_pc = inf_status->stop_pc;
bd5635a1
RP
2113 stop_step = inf_status->stop_step;
2114 stop_stack_dummy = inf_status->stop_stack_dummy;
2115 stopped_by_random_signal = inf_status->stopped_by_random_signal;
2116 trap_expected = inf_status->trap_expected;
2117 step_range_start = inf_status->step_range_start;
2118 step_range_end = inf_status->step_range_end;
2119 step_frame_address = inf_status->step_frame_address;
2120 step_over_calls = inf_status->step_over_calls;
bd5635a1
RP
2121 stop_after_trap = inf_status->stop_after_trap;
2122 stop_soon_quietly = inf_status->stop_soon_quietly;
2123 bpstat_clear (&stop_bpstat);
2124 stop_bpstat = inf_status->stop_bpstat;
2125 breakpoint_proceeded = inf_status->breakpoint_proceeded;
2126 proceed_to_finish = inf_status->proceed_to_finish;
2127
072b552a 2128 memcpy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
bd5635a1
RP
2129
2130 /* The inferior can be gone if the user types "print exit(0)"
2131 (and perhaps other times). */
37c99ddb
JK
2132 if (target_has_execution)
2133 write_register_bytes (0, inf_status->registers, REGISTER_BYTES);
2134
2135 /* The inferior can be gone if the user types "print exit(0)"
2136 (and perhaps other times). */
2137
2138 /* FIXME: If we are being called after stopping in a function which
2139 is called from gdb, we should not be trying to restore the
2140 selected frame; it just prints a spurious error message (The
2141 message is useful, however, in detecting bugs in gdb (like if gdb
2142 clobbers the stack)). In fact, should we be restoring the
2143 inferior status at all in that case? . */
2144
bd5635a1
RP
2145 if (target_has_stack && inf_status->restore_stack_info)
2146 {
37c99ddb
JK
2147 struct restore_selected_frame_args fr;
2148 fr.level = inf_status->selected_level;
2149 fr.frame_address = inf_status->selected_frame_address;
2150 /* The point of catch_errors is that if the stack is clobbered,
2151 walking the stack might encounter a garbage pointer and error()
2152 trying to dereference it. */
2153 if (catch_errors (restore_selected_frame, &fr,
2154 "Unable to restore previously selected frame:\n",
2155 RETURN_MASK_ERROR) == 0)
2156 /* Error in restoring the selected frame. Select the innermost
2157 frame. */
2158 select_frame (get_current_frame (), 0);
bd5635a1
RP
2159 }
2160}
2161
2162\f
2163void
2164_initialize_infrun ()
2165{
2166 register int i;
e37a6e9c 2167 register int numsigs;
bd5635a1
RP
2168
2169 add_info ("signals", signals_info,
2170 "What debugger does when program gets various signals.\n\
c66ed884 2171Specify a signal as argument to print info on that signal only.");
6b50c5c2 2172 add_info_alias ("handle", "signals", 0);
bd5635a1
RP
2173
2174 add_com ("handle", class_run, handle_command,
c66ed884
SG
2175 concat ("Specify how to handle a signal.\n\
2176Args are signals and actions to apply to those signals.\n\
2177Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
2178from 1-15 are allowed for compatibility with old versions of GDB.\n\
2179Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
072b552a 2180The special arg \"all\" is recognized to mean all signals except those\n\
c66ed884
SG
2181used by the debugger, typically SIGTRAP and SIGINT.\n",
2182"Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
072b552a 2183\"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
bd5635a1 2184Stop means reenter debugger if this signal happens (implies print).\n\
072b552a 2185Print means print a message if this signal happens.\n\
bd5635a1 2186Pass means let program see this signal; otherwise program doesn't know.\n\
072b552a 2187Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
c66ed884 2188Pass and Stop may be combined.", NULL));
bd5635a1 2189
a8a69e63 2190 stop_command = add_cmd ("stop", class_obscure, not_just_help_class_command,
3950a34e
RP
2191 "There is no `stop' command, but you can set a hook on `stop'.\n\
2192This allows you to set a list of commands to be run each time execution\n\
fee44494 2193of the program stops.", &cmdlist);
3950a34e 2194
67ac9759
JK
2195 numsigs = (int)TARGET_SIGNAL_LAST;
2196 signal_stop = (unsigned char *)
2197 xmalloc (sizeof (signal_stop[0]) * numsigs);
2198 signal_print = (unsigned char *)
2199 xmalloc (sizeof (signal_print[0]) * numsigs);
072b552a 2200 signal_program = (unsigned char *)
67ac9759 2201 xmalloc (sizeof (signal_program[0]) * numsigs);
e37a6e9c 2202 for (i = 0; i < numsigs; i++)
bd5635a1
RP
2203 {
2204 signal_stop[i] = 1;
2205 signal_print[i] = 1;
2206 signal_program[i] = 1;
2207 }
2208
2209 /* Signals caused by debugger's own actions
2210 should not be given to the program afterwards. */
67ac9759
JK
2211 signal_program[TARGET_SIGNAL_TRAP] = 0;
2212 signal_program[TARGET_SIGNAL_INT] = 0;
bd5635a1
RP
2213
2214 /* Signals that are not errors should not normally enter the debugger. */
67ac9759
JK
2215 signal_stop[TARGET_SIGNAL_ALRM] = 0;
2216 signal_print[TARGET_SIGNAL_ALRM] = 0;
2217 signal_stop[TARGET_SIGNAL_VTALRM] = 0;
2218 signal_print[TARGET_SIGNAL_VTALRM] = 0;
2219 signal_stop[TARGET_SIGNAL_PROF] = 0;
2220 signal_print[TARGET_SIGNAL_PROF] = 0;
2221 signal_stop[TARGET_SIGNAL_CHLD] = 0;
2222 signal_print[TARGET_SIGNAL_CHLD] = 0;
2223 signal_stop[TARGET_SIGNAL_IO] = 0;
2224 signal_print[TARGET_SIGNAL_IO] = 0;
4d4f2d50
JK
2225 signal_stop[TARGET_SIGNAL_POLL] = 0;
2226 signal_print[TARGET_SIGNAL_POLL] = 0;
67ac9759
JK
2227 signal_stop[TARGET_SIGNAL_URG] = 0;
2228 signal_print[TARGET_SIGNAL_URG] = 0;
87273c71
JL
2229
2230#ifdef SOLIB_ADD
2231 add_show_from_set
2232 (add_set_cmd ("stop-on-solib-events", class_support, var_zinteger,
2233 (char *) &stop_on_solib_events,
2234 "Set stopping for shared library events.\n\
2235If nonzero, gdb will give control to the user when the dynamic linker\n\
2236notifies gdb of shared library events. The most common event of interest\n\
2237to the user would be loading/unloading of a new library.\n",
2238 &setlist),
2239 &showlist);
2240#endif
bd5635a1 2241}
This page took 0.354685 seconds and 4 git commands to generate.