📄 linux-low.c
字号:
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; linux_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) && (WSTOPSIG (wstat) == __SIGRTMIN || WSTOPSIG (wstat) == __SIGRTMIN + 1)) { if (debug_threads) fprintf (stderr, "Ignored signal %d for %d (LWP %d).\n", WSTOPSIG (wstat), event_child->tid, event_child->head.id); linux_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. */ linux_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 PTRACE_SINGLESTEP 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); linux_resume_one_process (&event_child->head, 1, 0); } else { reinsert_breakpoint_by_bp (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ()); linux_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;}/* Wait for process, returns status. */static unsigned charlinux_wait (char *status){ int w; struct thread_info *child = NULL;retry: /* If we were only supposed to resume one thread, only wait for that thread - if it's still alive. If it died, however - which can happen if we're coming from the thread death case below - then we need to make sure we restart the other threads. We could pick a thread at random or restart all; restarting all is less arbitrary. */ if (cont_thread > 0) { child = (struct thread_info *) find_inferior_id (&all_threads, cont_thread); /* No stepping, no signal - unless one is pending already, of course. */ if (child == NULL) { struct thread_resume resume_info; resume_info.thread = -1; resume_info.step = resume_info.sig = resume_info.leave_stopped = 0; linux_resume (&resume_info); } } enable_async_io (); unblock_async_io (); w = linux_wait_for_event (child); stop_all_processes (); disable_async_io (); /* If we are waiting for a particular child, and it exited, linux_wait_for_event will return its exit status. Similarly if the last child exited. If this is not the last child, however, do not report it as exited until there is a 'thread exited' response available in the remote protocol. Instead, just wait for another event. This should be safe, because if the thread crashed we will already have reported the termination signal to GDB; that should stop any in-progress stepping operations, etc. Report the exit status of the last thread to exit. This matches LinuxThreads' behavior. */ if (all_threads.head == all_threads.tail) { if (WIFEXITED (w)) { fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w)); *status = 'W'; clear_inferiors (); free (all_processes.head); all_processes.head = all_processes.tail = NULL; return ((unsigned char) WEXITSTATUS (w)); } else if (!WIFSTOPPED (w)) { fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); *status = 'X'; clear_inferiors (); free (all_processes.head); all_processes.head = all_processes.tail = NULL; return ((unsigned char) WTERMSIG (w)); } } else { if (!WIFSTOPPED (w)) goto retry; } *status = 'T'; return ((unsigned char) WSTOPSIG (w));}/* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if thread groups are in use, we need to use tkill. */static intkill_lwp (int lwpid, int signo){ static int tkill_failed; errno = 0;#ifdef SYS_tkill if (!tkill_failed) { int ret = syscall (SYS_tkill, lwpid, signo); if (errno != ENOSYS) return ret; errno = 0; tkill_failed = 1; }#endif return kill (lwpid, signo);}static voidsend_sigstop (struct inferior_list_entry *entry){ struct process_info *process = (struct process_info *) entry; if (process->stopped) return; /* If we already have a pending stop signal for this process, don't send another. */ if (process->stop_expected) { process->stop_expected = 0; return; } if (debug_threads) fprintf (stderr, "Sending sigstop to process %d\n", process->head.id); kill_lwp (process->head.id, SIGSTOP); process->sigstop_sent = 1;}static voidwait_for_sigstop (struct inferior_list_entry *entry){ struct process_info *process = (struct process_info *) entry; struct thread_info *saved_inferior, *thread; int wstat, saved_tid; if (process->stopped) return; saved_inferior = current_inferior; saved_tid = ((struct inferior_list_entry *) saved_inferior)->id; thread = (struct thread_info *) find_inferior_id (&all_threads, process->tid); wstat = linux_wait_for_event (thread); /* If we stopped with a non-SIGSTOP signal, save it for later and record the pending SIGSTOP. If the process exited, just return. */ if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) != SIGSTOP) { if (debug_threads) fprintf (stderr, "Stopped with non-sigstop signal\n"); process->status_pending_p = 1; process->status_pending = wstat; process->stop_expected = 1; } if (linux_thread_alive (saved_tid)) current_inferior = saved_inferior; else { if (debug_threads) fprintf (stderr, "Previously current thread died.\n"); /* Set a valid thread as current. */ set_desired_inferior (0); }}static voidstop_all_processes (void){ stopping_threads = 1; for_each_inferior (&all_processes, send_sigstop); for_each_inferior (&all_processes, wait_for_sigstop); stopping_threads = 0;}/* Resume execution of the inferior process. If STEP is nonzero, single-step it. If SIGNAL is nonzero, give it that signal. */static voidlinux_resume_one_process (struct inferior_list_entry *entry, int step, int signal){ struct process_info *process = (struct process_info *) entry; struct thread_info *saved_inferior; if (process->stopped == 0) return; /* If we have pending signals or status, and a new signal, enqueue the signal. Also enqueue the signal if we are waiting to reinsert a breakpoint; it will be picked up again below. */ if (signal != 0 && (process->status_pending_p || process->pending_signals != NULL || process->bp_reinsert != 0)) { struct pending_signals *p_sig; p_sig = malloc (sizeof (*p_sig)); p_sig->prev = process->pending_signals; p_sig->signal = signal; process->pending_signals = p_sig; } if (process->status_pending_p && !check_removed_breakpoint (process)) return; saved_inferior = current_inferior; current_inferior = get_process_thread (process); if (debug_threads) fprintf (stderr, "Resuming process %d (%s, signal %d, stop %s)\n", inferior_pid, step ? "step" : "continue", signal, process->stop_expected ? "expected" : "not expected"); /* This bit needs some thinking about. If we get a signal that we must report while a single-step reinsert is still pending, we often end up resuming the thread. It might be better to (ew) allow a stack of pending events; then we could be sure that the reinsert happened right away and not lose any signals. Making this stack would also shrink the window in which breakpoints are uninserted (see comment in linux_wait_for_process) but not enough for complete correctness, so it won't solve that problem. It may be worthwhile just to solve this one, however. */ if (process->bp_reinsert != 0) { if (debug_threads) fprintf (stderr, " pending reinsert at %08lx", (long)process->bp_reinsert); if (step == 0) fprintf (stderr, "BAD - reinserting but not stepping.\n"); step = 1; /* Postpone any pending signal. It was enqueued above. */ signal = 0; } check_removed_breakpoint (process); if (debug_threads && the_low_target.get_pc != NULL) { fprintf (stderr, " "); (long) (*the_low_target.get_pc) (); } /* If we have pending signals, consume one unless we are trying to reinsert a breakpoint. */ if (process->pending_signals != NULL && process->bp_reinsert == 0) { struct pending_signals **p_sig; p_sig = &process->pending_signals; while ((*p_sig)->prev != NULL) p_sig = &(*p_sig)->prev; signal = (*p_sig)->signal; free (*p_sig); *p_sig = NULL; } regcache_invalidate_one ((struct inferior_list_entry *) get_process_thread (process)); errno = 0; process->stopped = 0; process->stepping = step; ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, process->lwpid, 0, signal); current_inferior = saved_inferior; if (errno) perror_with_name ("ptrace");}static struct thread_resume *resume_ptr;/* This function is called once per thread. We look up the thread in RESUME_PTR, and mark the thread with a pointer to the appropriate resume request. This algorithm is O(threads * resume elements), but resume elements is small (and will remain small at least until GDB supports thread suspension). */static voidlinux_set_resume_request (struct inferior_list_entry *entry){ struct process_info *process; struct thread_info *thread; int ndx; thread = (struct thread_info *) entry; process = get_thread_process (thread); ndx = 0; while (resume_ptr[ndx].thread != -1 && resume_ptr[ndx].thread != entry->id) ndx++; process->resume = &resume_ptr[ndx];}/* This function is called once per thread. We check the thread's resume request, which will tell us whether to resume, step, or leave the thread stopped; and what signal, if any, it should be sent. For threads which we aren't explicitly told otherwise, we preserve the stepping flag; this is used for stepping over gdbserver-placed breakpoints. */static voidlinux_continue_one_thread (struct inferior_list_entry *entry){ struct process_info *process; struct thread_info *thread; int step; thread = (struct thread_info *) entry; process = get_thread_process (thread); if (process->resume->leave_stopped) return; if (process->resume->thread == -1) step = process->stepping || process->resume->step; else step = process->resume->step; linux_resume_one_process (&process->head, step, process->resume->sig); process->resume = NULL;}/* This function is called once per thread. We check the thread's resume request, which will tell us whether to resume, step, or leave the thread stopped; and what signal, if any, it should be sent. We queue any needed signals, since we won't actually resume. We already have a pending event to report, so we don't need to preserve any step requests; they should be re-issued if necessary. */static voidlinux_queue_one_thread (struct inferior_list_entry *entry){ struct process_info *process; struct thread_info *thread; thread = (struct thread_info *) entry; process = get_thread_process (thread); if (process->resume->leave_stopped) return; /* If we have a new signal, enqueue the signal. */ if (process->resume->sig != 0) { struct pending_signals *p_sig;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -