[NativeProcessLinux] Remove the stop callback

Summary:
The stop callback is a remnant of the ThreadStateCoordinator. We don't need it now that TSC is
gone, as we know exactly which function to call when threads stop. This also removes some
stop-related functions, which were just forwarding calls to one another.

Test Plan: ninja check-lldb continues to pass

Reviewers: chaoren, ovyalov

Subscribers: lldb-commits

Differential Revision: http://reviews.llvm.org/D9531

llvm-svn: 236814
This commit is contained in:
Pavel Labath 2015-05-08 08:57:45 +00:00
parent 7325aee167
commit 337f3eb929
2 changed files with 71 additions and 157 deletions

View File

@ -1978,7 +1978,7 @@ NativeProcessLinux::Launch(LaunchArgs *args, Error &error)
thread_sp = AddThread (pid); thread_sp = AddThread (pid);
assert (thread_sp && "AddThread() returned a nullptr thread"); assert (thread_sp && "AddThread() returned a nullptr thread");
NotifyThreadCreateStopped (pid); NotifyThreadCreate (pid, ThreadState::Stopped);
std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP); std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP);
// Let our process instance know the thread has stopped. // Let our process instance know the thread has stopped.
@ -2074,7 +2074,7 @@ NativeProcessLinux::Attach(lldb::pid_t pid, Error &error)
assert (thread_sp && "AddThread() returned a nullptr"); assert (thread_sp && "AddThread() returned a nullptr");
// This will notify this is a new thread and tell the system it is stopped. // This will notify this is a new thread and tell the system it is stopped.
NotifyThreadCreateStopped (tid); NotifyThreadCreate (tid, ThreadState::Stopped);
std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP); std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP);
SetCurrentThreadID (thread_sp->GetID ()); SetCurrentThreadID (thread_sp->GetID ());
} }
@ -2331,7 +2331,7 @@ NativeProcessLinux::WaitForNewThread(::pid_t tid)
new_thread_sp = AddThread(tid); new_thread_sp = AddThread(tid);
std::static_pointer_cast<NativeThreadLinux> (new_thread_sp)->SetRunning (); std::static_pointer_cast<NativeThreadLinux> (new_thread_sp)->SetRunning ();
Resume (tid, LLDB_INVALID_SIGNAL_NUMBER); Resume (tid, LLDB_INVALID_SIGNAL_NUMBER);
NotifyThreadCreate (tid, false); NotifyThreadCreate (tid, ThreadState::Running);
} }
void void
@ -2427,7 +2427,7 @@ NativeProcessLinux::MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid)
// Tell coordinator about about the "new" (since exec) stopped main thread. // Tell coordinator about about the "new" (since exec) stopped main thread.
const lldb::tid_t main_thread_tid = GetID (); const lldb::tid_t main_thread_tid = GetID ();
NotifyThreadCreateStopped (main_thread_tid); NotifyThreadCreate (main_thread_tid, ThreadState::Stopped);
// NOTE: ideally these next statements would execute at the same time as the coordinator thread create was executed. // NOTE: ideally these next statements would execute at the same time as the coordinator thread create was executed.
// Consider a handler that can execute when that happens. // Consider a handler that can execute when that happens.
@ -2686,7 +2686,7 @@ NativeProcessLinux::MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool e
// We can now resume the newly created thread. // We can now resume the newly created thread.
std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning ();
Resume (pid, LLDB_INVALID_SIGNAL_NUMBER); Resume (pid, LLDB_INVALID_SIGNAL_NUMBER);
NotifyThreadCreate (pid, false); NotifyThreadCreate (pid, ThreadState::Running);
// Done handling. // Done handling.
return; return;
} }
@ -4175,58 +4175,6 @@ NativeProcessLinux::FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp
return error; return error;
} }
void
NativeProcessLinux::NotifyThreadCreateStopped (lldb::tid_t tid)
{
const bool is_stopped = true;
NotifyThreadCreate (tid, is_stopped);
}
void
NativeProcessLinux::StopRunningThreads(lldb::tid_t trigerring_tid)
{
Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
if (log)
log->Printf("NativeProcessLinux::%s tid %" PRIu64, __FUNCTION__, trigerring_tid);
const lldb::pid_t pid = GetID ();
StopRunningThreads(trigerring_tid,
[=](lldb::tid_t request_stop_tid) { return RequestThreadStop(pid, request_stop_tid); });
}
void
NativeProcessLinux::StopRunningThreadsWithSkipTID(lldb::tid_t deferred_signal_tid,
lldb::tid_t skip_stop_request_tid)
{
Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
if (log)
log->Printf("NativeProcessLinux::%s deferred_signal_tid %" PRIu64 ", skip_stop_request_tid %" PRIu64, __FUNCTION__, deferred_signal_tid, skip_stop_request_tid);
const lldb::pid_t pid = GetID ();
StopRunningThreadsWithSkipTID(deferred_signal_tid,
skip_stop_request_tid != LLDB_INVALID_THREAD_ID ? NativeProcessLinux::ThreadIDSet {skip_stop_request_tid} : NativeProcessLinux::ThreadIDSet (),
[=](lldb::tid_t request_stop_tid) { return RequestThreadStop(pid, request_stop_tid); });
}
Error
NativeProcessLinux::RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid)
{
Log* log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
if (log)
log->Printf ("NativeProcessLinux::%s requesting thread stop(pid: %" PRIu64 ", tid: %" PRIu64 ")", __FUNCTION__, pid, tid);
Error err;
errno = 0;
if (::tgkill (pid, tid, SIGSTOP) != 0)
{
err.SetErrorToErrno ();
if (log)
log->Printf ("NativeProcessLinux::%s tgkill(%" PRIu64 ", %" PRIu64 ", SIGSTOP) failed: %s", __FUNCTION__, pid, tid, err.AsCString ());
}
return err;
}
Error Error
NativeProcessLinux::GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) NativeProcessLinux::GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec)
{ {
@ -4342,8 +4290,7 @@ NativeProcessLinux::DoResume(
void void
NativeProcessLinux::StopThreads(const lldb::tid_t triggering_tid, NativeProcessLinux::StopThreads(const lldb::tid_t triggering_tid,
const ThreadIDSet &wait_for_stop_tids, const ThreadIDSet &wait_for_stop_tids)
const StopThreadFunction &request_thread_stop_function)
{ {
Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD);
std::lock_guard<std::mutex> lock(m_event_mutex); std::lock_guard<std::mutex> lock(m_event_mutex);
@ -4355,7 +4302,7 @@ NativeProcessLinux::StopThreads(const lldb::tid_t triggering_tid,
} }
DoStopThreads(PendingNotificationUP(new PendingNotification( DoStopThreads(PendingNotificationUP(new PendingNotification(
triggering_tid, wait_for_stop_tids, request_thread_stop_function))); triggering_tid, wait_for_stop_tids, ThreadIDSet())));
if (log) if (log)
{ {
@ -4364,8 +4311,7 @@ NativeProcessLinux::StopThreads(const lldb::tid_t triggering_tid,
} }
void void
NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid, NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid)
const StopThreadFunction &request_thread_stop_function)
{ {
Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD);
std::lock_guard<std::mutex> lock(m_event_mutex); std::lock_guard<std::mutex> lock(m_event_mutex);
@ -4376,9 +4322,7 @@ NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid,
__FUNCTION__, triggering_tid); __FUNCTION__, triggering_tid);
} }
DoStopThreads(PendingNotificationUP(new PendingNotification( DoStopThreads(PendingNotificationUP(new PendingNotification(triggering_tid)));
triggering_tid,
request_thread_stop_function)));
if (log) if (log)
{ {
@ -4388,22 +4332,21 @@ NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid,
void void
NativeProcessLinux::StopRunningThreadsWithSkipTID(lldb::tid_t triggering_tid, NativeProcessLinux::StopRunningThreadsWithSkipTID(lldb::tid_t triggering_tid,
const ThreadIDSet &skip_stop_request_tids, lldb::tid_t skip_stop_request_tid)
const StopThreadFunction &request_thread_stop_function)
{ {
Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD);
std::lock_guard<std::mutex> lock(m_event_mutex); std::lock_guard<std::mutex> lock(m_event_mutex);
if (log) if (log)
{ {
log->Printf("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ", skip_stop_request_tids.size(): %zd)", log->Printf("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ", skip_stop_request_tid: %" PRIu64 ")",
__FUNCTION__, triggering_tid, skip_stop_request_tids.size()); __FUNCTION__, triggering_tid, skip_stop_request_tid);
} }
DoStopThreads(PendingNotificationUP(new PendingNotification( DoStopThreads(PendingNotificationUP(new PendingNotification(
triggering_tid, triggering_tid,
request_thread_stop_function, ThreadIDSet(),
skip_stop_request_tids))); skip_stop_request_tid != LLDB_INVALID_THREAD_ID ? NativeProcessLinux::ThreadIDSet {skip_stop_request_tid} : ThreadIDSet ())));
if (log) if (log)
{ {
@ -4485,19 +4428,28 @@ NativeProcessLinux::RequestStopOnAllRunningThreads()
m_pending_notification_up->wait_for_stop_tids.swap (sent_tids); m_pending_notification_up->wait_for_stop_tids.swap (sent_tids);
} }
void Error
NativeProcessLinux::RequestThreadStop (lldb::tid_t tid, ThreadContext& context) NativeProcessLinux::RequestThreadStop (lldb::tid_t tid, ThreadContext& context)
{ {
const auto error = m_pending_notification_up->request_thread_stop_function (tid); Log* log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
if (error.Success ())
context.m_stop_requested = true; lldb::pid_t pid = GetID();
else
if (log)
log->Printf ("NativeProcessLinux::%s requesting thread stop(pid: %" PRIu64 ", tid: %" PRIu64 ")", __FUNCTION__, pid, tid);
Error err;
errno = 0;
if (::tgkill (pid, tid, SIGSTOP) != 0)
{ {
Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); err.SetErrorToErrno ();
if (log) if (log)
log->Printf("NativeProcessLinux::%s failed to request thread stop tid %" PRIu64 ": %s", log->Printf ("NativeProcessLinux::%s tgkill(%" PRIu64 ", %" PRIu64 ", SIGSTOP) failed: %s", __FUNCTION__, pid, tid, err.AsCString ());
__FUNCTION__, tid, error.AsCString ());
} }
else
context.m_stop_requested = true;
return err;
} }
@ -4576,22 +4528,20 @@ NativeProcessLinux::DoStopThreads(PendingNotificationUP &&notification_up)
} }
void void
NativeProcessLinux::ThreadWasCreated (lldb::tid_t tid, bool is_stopped) NativeProcessLinux::ThreadWasCreated (lldb::tid_t tid, ThreadState state)
{ {
// Ensure we don't already know about the thread. // Ensure we don't already know about the thread.
lldbassert(m_tid_map.find(tid) == m_tid_map.end()); lldbassert(m_tid_map.find(tid) == m_tid_map.end());
// Add the new thread to the stop map. // Add the new thread to the stop map.
ThreadContext ctx; auto tid_it = m_tid_map.emplace(tid, ThreadContext(state)).first;
ctx.m_state = (is_stopped) ? ThreadState::Stopped : ThreadState::Running;
m_tid_map[tid] = std::move(ctx);
if (m_pending_notification_up && !is_stopped) if (m_pending_notification_up && state == ThreadState::Running)
{ {
// We will need to wait for this new thread to stop as well before firing the // We will need to wait for this new thread to stop as well before firing the
// notification. // notification.
m_pending_notification_up->wait_for_stop_tids.insert(tid); m_pending_notification_up->wait_for_stop_tids.insert(tid);
m_pending_notification_up->request_thread_stop_function(tid); RequestThreadStop(tid, tid_it->second);
} }
} }
@ -4684,8 +4634,7 @@ NativeProcessLinux::RequestThreadResumeAsNeeded (lldb::tid_t tid,
} }
void void
NativeProcessLinux::NotifyThreadCreate (lldb::tid_t tid, NativeProcessLinux::NotifyThreadCreate (lldb::tid_t tid, ThreadState state)
bool is_stopped)
{ {
Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD);
std::lock_guard<std::mutex> lock(m_event_mutex); std::lock_guard<std::mutex> lock(m_event_mutex);
@ -4693,10 +4642,10 @@ NativeProcessLinux::NotifyThreadCreate (lldb::tid_t tid,
if (log) if (log)
{ {
log->Printf("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ", is %sstopped)", log->Printf("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ", is %sstopped)",
__FUNCTION__, tid, is_stopped?"":"not "); __FUNCTION__, tid, state==ThreadState::Stopped?"":"not ");
} }
ThreadWasCreated (tid, is_stopped); ThreadWasCreated (tid, state);
if (log) if (log)
{ {

View File

@ -346,69 +346,52 @@ namespace process_linux {
SingleStep(lldb::tid_t tid, uint32_t signo); SingleStep(lldb::tid_t tid, uint32_t signo);
// ThreadStateCoordinator helper methods. // ThreadStateCoordinator helper methods.
void
NotifyThreadCreateStopped (lldb::tid_t tid);
void
NotifyThreadCreateRunning (lldb::tid_t tid);
void void
NotifyThreadDeath (lldb::tid_t tid); NotifyThreadDeath (lldb::tid_t tid);
void
StopRunningThreads (lldb::tid_t triggering_tid);
void
StopRunningThreadsWithSkipTID (lldb::tid_t deferred_signal_tid,
lldb::tid_t skip_stop_request_tid);
Error Error
Detach(lldb::tid_t tid); Detach(lldb::tid_t tid);
Error
RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid);
public: public:
// Typedefs. // Typedefs.
typedef std::unordered_set<lldb::tid_t> ThreadIDSet; typedef std::unordered_set<lldb::tid_t> ThreadIDSet;
// Callback/block definitions. // Callback/block definitions.
typedef std::function<Error (lldb::tid_t tid)> StopThreadFunction;
typedef std::function<Error (lldb::tid_t tid, bool supress_signal)> ResumeThreadFunction; typedef std::function<Error (lldb::tid_t tid, bool supress_signal)> ResumeThreadFunction;
private: private:
// Notify the coordinator when a thread is created and/or starting to be enum class ThreadState
// tracked. is_stopped should be true if the thread is currently stopped; {
// otherwise, it should be set false if it is already running. Will Running,
// call the error function if the thread id is already tracked. Stopped
};
// Notify that a thread is created and/or starting to be
// tracked. The state parameter should reflect whether the thread is created in a running
// or stopped state.
void void
NotifyThreadCreate(lldb::tid_t tid, bool is_stopped); NotifyThreadCreate(lldb::tid_t tid, ThreadState state);
// Notify the delegate after a given set of threads stops. The triggering_tid will be set // Notify the delegate after a given set of threads stops. The triggering_tid will be set
// as the current thread. The error_function will be fired if either the triggering tid // as the current thread. The error_function will be fired if either the triggering tid
// or any of the wait_for_stop_tids are unknown. // or any of the wait_for_stop_tids are unknown.
void void
StopThreads(lldb::tid_t triggering_tid, StopThreads(lldb::tid_t triggering_tid, const ThreadIDSet &wait_for_stop_tids);
const ThreadIDSet &wait_for_stop_tids,
const StopThreadFunction &request_thread_stop_function);
// Notify the delegate after all non-stopped threads stop. The triggering_tid will be set // Notify the delegate after all non-stopped threads stop. The triggering_tid will be set
// as the current thread. The error_function will be fired if the triggering tid // as the current thread. The error_function will be fired if the triggering tid
// is unknown. // is unknown.
void void
StopRunningThreads(lldb::tid_t triggering_tid, StopRunningThreads(lldb::tid_t triggering_tid);
const StopThreadFunction &request_thread_stop_function);
// Notify the delegate after all non-stopped threads stop. The triggering_tid will be set // Notify the delegate after all non-stopped threads stop. The triggering_tid will be set
// as the current thread. The error_function will be fired if either the triggering tid // as the current thread. The error_function will be fired if either the triggering tid
// or any of the wait_for_stop_tids are unknown. This variant will send stop requests to // or any of the wait_for_stop_tids are unknown. This variant will send stop requests to
// all non-stopped threads except for any contained in skip_stop_request_tids. // all non-stopped threads except skip_stop_request_tid.
void void
StopRunningThreadsWithSkipTID(lldb::tid_t triggering_tid, StopRunningThreadsWithSkipTID(lldb::tid_t triggering_tid, lldb::tid_t skip_stop_request_tid);
const ThreadIDSet &skip_stop_request_tids,
const StopThreadFunction &request_thread_stop_function);
// Notify the thread stopped. Will trigger error at time of execution if we // Notify the thread stopped. Will trigger error at time of execution if we
// already think it is stopped. // already think it is stopped.
@ -436,61 +419,43 @@ namespace process_linux {
private: private:
enum class ThreadState
{
Running,
Stopped
};
struct ThreadContext struct ThreadContext
{ {
ThreadState m_state; ThreadState m_state;
bool m_stop_requested = false; bool m_stop_requested = false;
ResumeThreadFunction m_request_resume_function; ResumeThreadFunction m_request_resume_function;
explicit ThreadContext(ThreadState state)
: m_state(state)
{}
}; };
typedef std::unordered_map<lldb::tid_t, ThreadContext> TIDContextMap; typedef std::unordered_map<lldb::tid_t, ThreadContext> TIDContextMap;
struct PendingNotification struct PendingNotification
{ {
PendingNotification (lldb::tid_t triggering_tid, PendingNotification (lldb::tid_t triggering_tid,
const ThreadIDSet &wait_for_stop_tids, const ThreadIDSet &wait_for_stop_tids,
const StopThreadFunction &request_thread_stop_function): const ThreadIDSet &skip_stop_request_tids):
triggering_tid (triggering_tid), triggering_tid (triggering_tid),
wait_for_stop_tids (wait_for_stop_tids), wait_for_stop_tids (wait_for_stop_tids),
original_wait_for_stop_tids (wait_for_stop_tids), original_wait_for_stop_tids (wait_for_stop_tids),
request_thread_stop_function (request_thread_stop_function), request_stop_on_all_unstopped_threads (false),
request_stop_on_all_unstopped_threads (false), skip_stop_request_tids (skip_stop_request_tids)
skip_stop_request_tids ()
{ {
} }
PendingNotification (lldb::tid_t triggering_tid, PendingNotification (lldb::tid_t triggering_tid):
const StopThreadFunction &request_thread_stop_function): triggering_tid (triggering_tid),
triggering_tid (triggering_tid), wait_for_stop_tids (),
wait_for_stop_tids (), original_wait_for_stop_tids (),
original_wait_for_stop_tids (), request_stop_on_all_unstopped_threads (true),
request_thread_stop_function (request_thread_stop_function), skip_stop_request_tids ()
request_stop_on_all_unstopped_threads (true),
skip_stop_request_tids ()
{
}
PendingNotification (lldb::tid_t triggering_tid,
const StopThreadFunction &request_thread_stop_function,
const ThreadIDSet &skip_stop_request_tids):
triggering_tid (triggering_tid),
wait_for_stop_tids (),
original_wait_for_stop_tids (),
request_thread_stop_function (request_thread_stop_function),
request_stop_on_all_unstopped_threads (true),
skip_stop_request_tids (skip_stop_request_tids)
{ {
} }
const lldb::tid_t triggering_tid; const lldb::tid_t triggering_tid;
ThreadIDSet wait_for_stop_tids; ThreadIDSet wait_for_stop_tids;
const ThreadIDSet original_wait_for_stop_tids; const ThreadIDSet original_wait_for_stop_tids;
StopThreadFunction request_thread_stop_function;
const bool request_stop_on_all_unstopped_threads; const bool request_stop_on_all_unstopped_threads;
ThreadIDSet skip_stop_request_tids; ThreadIDSet skip_stop_request_tids;
}; };
@ -505,7 +470,7 @@ namespace process_linux {
void void
RequestStopOnAllRunningThreads(); RequestStopOnAllRunningThreads();
void Error
RequestThreadStop (lldb::tid_t tid, ThreadContext& context); RequestThreadStop (lldb::tid_t tid, ThreadContext& context);
std::mutex m_event_mutex; // Serializes execution of ProcessEvent. XXX std::mutex m_event_mutex; // Serializes execution of ProcessEvent. XXX
@ -521,7 +486,7 @@ namespace process_linux {
DoStopThreads(PendingNotificationUP &&notification_up); DoStopThreads(PendingNotificationUP &&notification_up);
void void
ThreadWasCreated (lldb::tid_t tid, bool is_stopped); ThreadWasCreated (lldb::tid_t tid, ThreadState state);
void void
ThreadDidDie (lldb::tid_t tid); ThreadDidDie (lldb::tid_t tid);