/* Return nonzero if this process stopped at a breakpoint which no longer appears to be inserted. Also adjust the PC appropriately to resume where the breakpoint used to be. */ static int check_removed_breakpoint (struct process_info *event_child) { CORE_ADDR stop_pc; struct thread_info *saved_inferior; if (event_child->pending_is_breakpoint == 0) return 0; if (debug_threads) fprintf (stderr, "Checking for breakpoint.\n"); saved_inferior = current_inferior; current_inferior = get_process_thread (event_child); stop_pc = get_stop_pc (); /* If the PC has changed since we stopped, then we shouldn't do anything. This happens if, for instance, GDB handled the decr_pc_after_break subtraction itself. */ if (stop_pc != event_child->pending_stop_pc) { if (debug_threads) fprintf (stderr, "Ignoring, PC was changed.\n"); event_child->pending_is_breakpoint = 0; current_inferior = saved_inferior; return 0; } /* If the breakpoint is still there, we will report hitting it. */ if ((*the_low_target.breakpoint_at) (stop_pc)) { if (debug_threads) fprintf (stderr, "Ignoring, breakpoint is still present.\n"); current_inferior = saved_inferior; return 0; } if (debug_threads) fprintf (stderr, "Removed breakpoint.\n"); /* For decr_pc_after_break targets, here is where we perform the decrement. We go immediately from this function to resuming, and can not safely call get_stop_pc () again. */ if (the_low_target.set_pc != NULL) (*the_low_target.set_pc) (stop_pc); /* We consumed the pending SIGTRAP. */ event_child->pending_is_breakpoint = 0; event_child->status_pending_p = 0; event_child->status_pending = 0; current_inferior = saved_inferior; return 1; }
static void linux_wait_for_process (struct process_info **childp, int *wstatp) { int ret; int to_wait_for = -1; if (*childp != NULL) to_wait_for = (*childp)->lwpid; while (1) { ret = waitpid (to_wait_for, wstatp, WNOHANG); if (ret == -1) { if (errno != ECHILD) perror_with_name ("waitpid"); } else if (ret > 0) break; ret = waitpid (to_wait_for, wstatp, WNOHANG | __WCLONE); if (ret == -1) { if (errno != ECHILD) perror_with_name ("waitpid (WCLONE)"); } else if (ret > 0) break; usleep (1000); } if (debug_threads && (!WIFSTOPPED (*wstatp) || (WSTOPSIG (*wstatp) != 32 && WSTOPSIG (*wstatp) != 33))) fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp); if (to_wait_for == -1) *childp = (struct process_info *) find_inferior_id (&all_processes, ret); (*childp)->stopped = 1; (*childp)->pending_is_breakpoint = 0; if (debug_threads && WIFSTOPPED (*wstatp)) { current_inferior = (struct thread_info *) find_inferior_id (&all_threads, (*childp)->tid); /* For testing only; i386_stop_pc prints out a diagnostic. */ if (the_low_target.get_pc != NULL) get_stop_pc (); } }
static int fbsd_wait_for_event (struct thread_info *child) { CORE_ADDR stop_pc; struct process_info *event_child; int wstat; /* Check for a process with a pending status. */ /* It is possible that the user changed the pending task's registers since it stopped. We correctly handle the change of PC if we hit a breakpoint (in check_removed_breakpoint); signals should be reported anyway. */ if (child == NULL) { event_child = (struct process_info *) find_inferior (&all_processes, status_pending_p, NULL); if (debug_threads && event_child) fprintf (stderr, "Got a pending child %d\n", event_child->lwpid); } else { event_child = get_thread_process (child); if (event_child->status_pending_p && check_removed_breakpoint (event_child)) event_child = NULL; } if (event_child != NULL) { if (event_child->status_pending_p) { if (debug_threads) fprintf (stderr, "Got an event from pending child %d (%04x)\n", event_child->lwpid, event_child->status_pending); wstat = event_child->status_pending; event_child->status_pending_p = 0; event_child->status_pending = 0; current_inferior = get_process_thread (event_child); return wstat; } } /* We only enter this loop if no process has a pending wait status. Thus any action taken in response to a wait status inside this loop is responding as soon as we detect the status, not after any pending events. */ while (1) { if (child == NULL) event_child = NULL; else event_child = get_thread_process (child); fbsd_wait_for_process (&event_child, &wstat); if (event_child == NULL) error ("event from unknown child"); current_inferior = (struct thread_info *) find_inferior_id (&all_threads, event_child->tid); if (using_threads) { /* Check for thread exit. */ if (! WIFSTOPPED (wstat)) { if (debug_threads) fprintf (stderr, "Thread %d (LWP %d) exiting\n", event_child->tid, event_child->head.id); /* If the last thread is exiting, just return. */ if (all_threads.head == all_threads.tail) return wstat; dead_thread_notify (event_child->tid); remove_inferior (&all_processes, &event_child->head); free (event_child); remove_thread (current_inferior); current_inferior = (struct thread_info *) all_threads.head; /* If we were waiting for this particular child to do something... well, it did something. */ if (child != NULL) return wstat; /* Wait for a more interesting event. */ continue; } if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGSTOP && event_child->stop_expected) { if (debug_threads) fprintf (stderr, "Expected stop.\n"); event_child->stop_expected = 0; fbsd_resume_one_process (&event_child->head, event_child->stepping, 0); continue; } /* FIXME drow/2002-06-09: Get signal numbers from the inferior's thread library? */ if (WIFSTOPPED (wstat)) { if (debug_threads) fprintf (stderr, "Ignored signal %d for %d (LWP %d).\n", WSTOPSIG (wstat), event_child->tid, event_child->head.id); fbsd_resume_one_process (&event_child->head, event_child->stepping, WSTOPSIG (wstat)); continue; } } /* If this event was not handled above, and is not a SIGTRAP, report it. */ if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGTRAP) return wstat; /* If this target does not support breakpoints, we simply report the SIGTRAP; it's of no concern to us. */ if (the_low_target.get_pc == NULL) return wstat; stop_pc = get_stop_pc (); /* bp_reinsert will only be set if we were single-stepping. Notice that we will resume the process after hitting a gdbserver breakpoint; single-stepping to/over one is not supported (yet). */ if (event_child->bp_reinsert != 0) { if (debug_threads) fprintf (stderr, "Reinserted breakpoint.\n"); reinsert_breakpoint (event_child->bp_reinsert); event_child->bp_reinsert = 0; /* Clear the single-stepping flag and SIGTRAP as we resume. */ fbsd_resume_one_process (&event_child->head, 0, 0); continue; } if (debug_threads) fprintf (stderr, "Hit a (non-reinsert) breakpoint.\n"); if (check_breakpoints (stop_pc) != 0) { /* We hit one of our own breakpoints. We mark it as a pending breakpoint, so that check_removed_breakpoint () will do the PC adjustment for us at the appropriate time. */ event_child->pending_is_breakpoint = 1; event_child->pending_stop_pc = stop_pc; /* Now we need to put the breakpoint back. We continue in the event loop instead of simply replacing the breakpoint right away, in order to not lose signals sent to the thread that hit the breakpoint. Unfortunately this increases the window where another thread could sneak past the removed breakpoint. For the current use of server-side breakpoints (thread creation) this is acceptable; but it needs to be considered before this breakpoint mechanism can be used in more general ways. For some breakpoints it may be necessary to stop all other threads, but that should be avoided where possible. If breakpoint_reinsert_addr is NULL, that means that we can use PT_STEP on this platform. Uninsert the breakpoint, mark it for reinsertion, and single-step. Otherwise, call the target function to figure out where we need our temporary breakpoint, create it, and continue executing this process. */ if (the_low_target.breakpoint_reinsert_addr == NULL) { event_child->bp_reinsert = stop_pc; uninsert_breakpoint (stop_pc); fbsd_resume_one_process (&event_child->head, 1, 0); } else { reinsert_breakpoint_by_bp (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ()); fbsd_resume_one_process (&event_child->head, 0, 0); } continue; } /* If we were single-stepping, we definitely want to report the SIGTRAP. The single-step operation has completed, so also clear the stepping flag; in general this does not matter, because the SIGTRAP will be reported to the client, which will give us a new action for this thread, but clear it for consistency anyway. It's safe to clear the stepping flag because the only consumer of get_stop_pc () after this point is check_removed_breakpoint, and pending_is_breakpoint is not set. It might be wiser to use a step_completed flag instead. */ if (event_child->stepping) { event_child->stepping = 0; return wstat; } /* A SIGTRAP that we can't explain. It may have been a breakpoint. Check if it is a breakpoint, and if so mark the process information accordingly. This will handle both the necessary fiddling with the PC on decr_pc_after_break targets and suppressing extra threads hitting a breakpoint if two hit it at once and then GDB removes it after the first is reported. Arguably it would be better to report multiple threads hitting breakpoints simultaneously, but the current remote protocol does not allow this. */ if ((*the_low_target.breakpoint_at) (stop_pc)) { event_child->pending_is_breakpoint = 1; event_child->pending_stop_pc = stop_pc; } return wstat; } /* NOTREACHED */ return 0; }