Lines Matching +full:state +full:- +full:of +full:- +full:the +full:- +full:art

2  * Copyright (C) 2017 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
32 #include "android-base/endian.h"
33 #include "android-base/stringprintf.h"
34 #include "android-base/unique_fd.h"
35 #include "art_field-inl.h"
36 #include "art_method-alloc-inl.h"
48 #include "mirror/class-alloc-inl.h"
52 #include "runtime-inl.h"
54 #include "scoped_thread_state_change-inl.h"
64 // Messages sent from the transport
71 // Messages sent to the transport
78 'J', 'D', 'W', 'P', '-', 'H', 'a', 'n', 'd', 's', 'h', 'a', 'k', 'e'};
173 static bool IsDebuggingPossible() { return art::Dbg::IsJdwpAllowed(); } in IsDebuggingPossible()
176 return IsDebuggingPossible() || art::Runtime::Current()->IsProfileableFromShell(); in IsDebuggableOrProfilable()
179 // Begin running the debugger.
181 // The debugger thread is started for a debuggable or profileable-from-shell process. in StartDebugger()
182 // The pid will be send to adbd for adb's "track-jdwp" and "track-app" services. in StartDebugger()
183 // The thread will also set up the jdwp tunnel if the process is debuggable. in StartDebugger()
185 connection_->StartDebuggerThreads(); in StartDebugger()
187 LOG(ERROR) << "Not starting debugger since process cannot load the jdwp agent."; in StartDebugger()
191 // The debugger should have already shut down since the runtime is ending. As far
192 // as the agent is concerned shutdown already happened when we went to kDeath
193 // state. We need to clean up our threads still though and this is a good time
194 // to do it since the runtime is still able to handle all the normal state
198 gState->StopDebuggerThreads(); in StopDebugger()
199 // Wait for our threads to actually return and cleanup the pthread. in StopDebugger()
210 return IsDebuggingPossible() && !art::Runtime::Current()->GetJdwpOptions().empty(); in IsDebuggerConfigured()
214 const art::ArrayRef<const uint8_t>& data) { in DdmPublishChunk()
215 connection_->PublishDdmData(type, data); in DdmPublishChunk()
219 connection_->SetCurrentProcessName(process_name); in SetCurrentProcessName()
223 connection_->AddApplication(package_name); in AddApplication()
227 connection_->RemoveApplication(package_name); in RemoveApplication()
231 connection_->SetWaitingForDebugger(waiting); in SetWaitingForDebugger()
234 void AdbConnectionAppInfoCallback::SetUserId(int user_id) { connection_->SetUserId(user_id); } in SetUserId()
254 sleep_event_fd_(-1), in AdbConnectionState()
256 local_agent_control_sock_(-1), in AdbConnectionState()
257 remote_agent_control_sock_(-1), in AdbConnectionState()
258 adb_connection_socket_(-1), in AdbConnectionState()
259 adb_write_event_fd_(-1), in AdbConnectionState()
269 // Add the startup callback. in AdbConnectionState()
270 art::ScopedObjectAccess soa(art::Thread::Current()); in AdbConnectionState()
271 art::Runtime::Current()->GetRuntimeCallbacks()->AddDebuggerControlCallback(&controller_); in AdbConnectionState()
275 // Remove the startup callback. in ~AdbConnectionState()
276 art::Thread* self = art::Thread::Current(); in ~AdbConnectionState()
278 art::ScopedObjectAccess soa(self); in ~AdbConnectionState()
279 art::Runtime::Current()->GetRuntimeCallbacks()->RemoveDebuggerControlCallback(&controller_); in ~AdbConnectionState()
283 static art::ObjPtr<art::mirror::Object> CreateAdbConnectionThread(art::Thread* self) in CreateAdbConnectionThread()
284 REQUIRES_SHARED(art::Locks::mutator_lock_) { in CreateAdbConnectionThread()
285 art::StackHandleScope<3u> hs(self); in CreateAdbConnectionThread()
286 art::Handle<art::mirror::String> thr_name = in CreateAdbConnectionThread()
287 hs.NewHandle(art::mirror::String::AllocFromModifiedUtf8(self, kAdbConnectionThreadName)); in CreateAdbConnectionThread()
289 DCHECK(self->IsExceptionPending()); in CreateAdbConnectionThread()
292 art::ArtField* system_thread_group_field = in CreateAdbConnectionThread()
293 art::WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup; in CreateAdbConnectionThread()
294 DCHECK(system_thread_group_field->GetDeclaringClass()->IsInitialized()); in CreateAdbConnectionThread()
297 art::Handle<art::mirror::Object> system_thread_group = hs.NewHandle( in CreateAdbConnectionThread()
298 system_thread_group_field->GetDeclaringClass()->GetFieldObject<art::mirror::Object>( in CreateAdbConnectionThread()
299 system_thread_group_field->GetOffset())); in CreateAdbConnectionThread()
300 return art::WellKnownClasses::java_lang_Thread_init in CreateAdbConnectionThread()
301 ->NewObject<'L', 'L', 'I', 'Z'>( in CreateAdbConnectionThread()
313 art::Thread* self = art::Thread::Attach(kAdbConnectionThreadName, true, data->thr_); in CallbackFunction()
315 // The name in Attach() is only for logging. Set the thread name. This is important so in CallbackFunction()
316 // that the thread is no longer seen as starting up. in CallbackFunction()
318 art::ScopedObjectAccess soa(self); in CallbackFunction()
319 self->SetThreadName(kAdbConnectionThreadName); in CallbackFunction()
322 // Release the peer. in CallbackFunction()
323 JNIEnv* env = self->GetJniEnv(); in CallbackFunction()
324 env->DeleteGlobalRef(data->thr_); in CallbackFunction()
325 data->thr_ = nullptr; in CallbackFunction()
327 // The StartThreadBirth was called in the parent thread. We let the runtime know we are up in CallbackFunction()
328 // before going into the provided code. in CallbackFunction()
329 art::MutexLock mu(self, *art::Locks::runtime_shutdown_lock_); in CallbackFunction()
330 art::Runtime::Current()->EndThreadBirth(); in CallbackFunction()
332 data->this_->RunPollLoop(self); in CallbackFunction()
333 int detach_result = art::Runtime::Current()->GetJavaVM()->DetachCurrentThread(); in CallbackFunction()
340 // First do all the final setup we need. in StartDebuggerThreads()
341 CHECK_EQ(adb_write_event_fd_.get(), -1); in StartDebuggerThreads()
342 CHECK_EQ(sleep_event_fd_.get(), -1); in StartDebuggerThreads()
343 CHECK_EQ(local_agent_control_sock_.get(), -1); in StartDebuggerThreads()
344 CHECK_EQ(remote_agent_control_sock_.get(), -1); in StartDebuggerThreads()
347 CHECK_NE(sleep_event_fd_.get(), -1) << "Unable to create wakeup eventfd."; in StartDebuggerThreads()
349 CHECK_NE(adb_write_event_fd_.get(), -1) << "Unable to create write-lock eventfd."; in StartDebuggerThreads()
352 art::ScopedObjectAccess soa(art::Thread::Current()); in StartDebuggerThreads()
353 art::Runtime::Current()->GetRuntimeCallbacks()->AddDdmCallback(&ddm_callback_); in StartDebuggerThreads()
354 art::Runtime::Current()->GetRuntimeCallbacks()->AddAppInfoCallback(&appinfo_callback_); in StartDebuggerThreads()
356 // Setup the socketpair we use to talk to the agent. in StartDebuggerThreads()
369 // Next start the threads. in StartDebuggerThreads()
370 art::Thread* self = art::Thread::Current(); in StartDebuggerThreads()
371 art::ScopedObjectAccess soa(self); in StartDebuggerThreads()
373 art::Runtime* runtime = art::Runtime::Current(); in StartDebuggerThreads()
374 art::MutexLock mu(self, *art::Locks::runtime_shutdown_lock_); in StartDebuggerThreads()
375 if (runtime->IsShuttingDownLocked()) { in StartDebuggerThreads()
376 // The runtime is shutting down so we cannot create new threads. This shouldn't really happen. in StartDebuggerThreads()
377 LOG(ERROR) << "The runtime is shutting down when we are trying to start up the debugger!"; in StartDebuggerThreads()
380 runtime->StartThreadBirth(); in StartDebuggerThreads()
382 jobject thr = soa.Env()->GetVm()->AddGlobalRef(self, CreateAdbConnectionThread(soa.Self())); in StartDebuggerThreads()
387 // Note: Using pthreads instead of std::thread to not abort when the thread cannot be in StartDebuggerThreads()
397 // If the create succeeded the other thread will call EndThreadBirth. in StartDebuggerThreads()
398 art::Runtime* runtime = art::Runtime::Current(); in StartDebuggerThreads()
399 soa.Env()->DeleteGlobalRef(thr); in StartDebuggerThreads()
400 LOG(ERROR) << "Failed to create thread for adb-jdwp connection manager!"; in StartDebuggerThreads()
401 art::MutexLock mu(art::Thread::Current(), *art::Locks::runtime_shutdown_lock_); in StartDebuggerThreads()
402 runtime->EndThreadBirth(); in StartDebuggerThreads()
412 // Lock the write_event_fd so that concurrent PublishDdms will see that the connection is in CloseFds()
419 // If we didn't load anything we will need to do the handshake again. in CloseFds()
422 // If the agent isn't loaded we might need to tell ddms code the connection is closed. in CloseFds()
429 art::ScopedObjectAccess soa(art::Thread::Current()); in NotifyDdms()
433 art::Dbg::DdmConnected(); in NotifyDdms()
435 art::Dbg::DdmDisconnected(); in NotifyDdms()
440 // Just have a normal counter but always set the sign bit. in NextDdmId()
444 void AdbConnectionState::PublishDdmData(uint32_t type, const art::ArrayRef<const uint8_t>& data) { in PublishDdmData()
486 art::ArrayRef<const uint8_t> data) { in SendDdmPacket()
487 // Get the write_event early to fail fast. in SendDdmPacket()
489 if (adb_connection_socket_ == -1 || !performed_handshake_) { in SendDdmPacket()
490 VLOG(jdwp) << "Not sending ddms data of type " in SendDdmPacket()
500 // the adb_write_event_fd_ will ensure that the adb_connection_socket_ will not go away until in SendDdmPacket()
509 // Write the length first. in SendDdmPacket()
513 // Write the id next; in SendDdmPacket()
517 // next the flags. (0 for cmd packet because DDMS). in SendDdmPacket()
521 // Now the cmd-set in SendDdmPacket()
523 // Now the command in SendDdmPacket()
528 // This is the error code bytes which are all 0 in SendDdmPacket()
535 // now the type. in SendDdmPacket()
540 // Now the data.size() in SendDdmPacket()
550 // now pkt_header has the header. in SendDdmPacket()
551 // use writev to send the actual data. in SendDdmPacket()
554 PLOG(ERROR) << StringPrintf("Failed to send DDMS packet %c%c%c%c to debugger (%zd of %zu)", in SendDdmPacket()
591 cmsg->cmsg_len = CMSG_LEN(dt_fd_forward::FdSet::kDataLength); in SendAgentFds()
592 cmsg->cmsg_level = SOL_SOCKET; in SendAgentFds()
593 cmsg->cmsg_type = SCM_RIGHTS; in SendAgentFds()
595 // Duplicate the fds before sending them. in SendAgentFds()
596 android::base::unique_fd read_fd(art::DupCloexec(adb_connection_socket_)); in SendAgentFds()
597 CHECK_NE(read_fd.get(), -1) << "Failed to dup read_fd_: " << strerror(errno); in SendAgentFds()
598 android::base::unique_fd write_fd(art::DupCloexec(adb_connection_socket_)); in SendAgentFds()
599 CHECK_NE(write_fd.get(), -1) << "Failed to dup write_fd: " << strerror(errno); in SendAgentFds()
600 android::base::unique_fd write_lock_fd(art::DupCloexec(adb_write_event_fd_)); in SendAgentFds()
601 CHECK_NE(write_lock_fd.get(), -1) << "Failed to dup write_lock_fd: " << strerror(errno); in SendAgentFds()
620 const char* isa = GetInstructionSetString(art::Runtime::Current()->GetInstructionSet()); in SetupAdbConnection()
627 .data.profileable = art::Runtime::Current()->IsProfileableFromShell()}, in SetupAdbConnection()
629 // GetInstructionSetString() returns a null-terminating C-style string. in SetupAdbConnection()
637 // perhaps the system is restarting it for "adb root", the in SetupAdbConnection()
641 // Waking up and polling every couple of seconds is generally a in SetupAdbConnection()
642 // bad thing to do, but we only do this if the application is in SetupAdbConnection()
643 // debuggable *and* adbd isn't running. Still, for the sake in SetupAdbConnection()
644 // of battery life, we should consider timing out and giving in SetupAdbConnection()
646 // the debuggable flag set. in SetupAdbConnection()
664 void AdbConnectionState::RunPollLoop(art::Thread* self) { in RunPollLoop()
667 CHECK_EQ(self->GetState(), art::ThreadState::kNative); in RunPollLoop()
668 art::Locks::mutator_lock_->AssertNotHeld(self); in RunPollLoop()
669 self->SetState(art::ThreadState::kWaitingInMainDebuggerLoop); in RunPollLoop()
679 // By default we are always interested in read and hangup events on the control ctx. in RunPollLoop()
682 // If we have an update for ADBd, we also want to know when the control ctx in RunPollLoop()
688 // -1 as an fd causes it to be ignored by poll in RunPollLoop()
689 {(agent_loaded_ ? local_agent_control_sock_ : -1), POLLIN, 0}, in RunPollLoop()
690 // Check for the control_sock_ actually going away. We always monitor for POLLIN, even if in RunPollLoop()
694 // if we have not loaded the agent either the adb_connection_socket_ is -1 meaning we in RunPollLoop()
695 // don't have a real connection yet or the socket through adb needs to be listened to for in RunPollLoop()
696 // incoming data that the agent or this plugin can handle. in RunPollLoop()
697 {should_listen_on_connection ? adb_connection_socket_ : -1, POLLIN | POLLRDHUP, 0}}; in RunPollLoop()
698 int res = TEMP_FAILURE_RETRY(poll(pollfds, 4, -1)); in RunPollLoop()
713 res = TEMP_FAILURE_RETRY(recv(local_agent_control_sock_, buf, sizeof(buf) - 1, 0)); in RunPollLoop()
723 if (adb_connection_socket_ != -1) { in RunPollLoop()
738 // We will only ever do the handshake once so reset this. in RunPollLoop()
745 // For a profielable process, this path can execute when the adbd restarts. in RunPollLoop()
754 if (new_fd == -1) { in RunPollLoop()
755 // Something went wrong. We need to retry getting the control socket. in RunPollLoop()
758 } else if (adb_connection_socket_ != -1) { in RunPollLoop()
772 // The agent was already loaded so this must be after a disconnection. Therefore have the in RunPollLoop()
773 // transport perform the handshake. in RunPollLoop()
777 // The other end of the adb connection just dropped it. in RunPollLoop()
778 // Reset the connection since we don't have an active socket through the adb server. in RunPollLoop()
791 // Agent was already loaded so it can deal with the handshake. in RunPollLoop()
795 VLOG(jdwp) << "Sending state update to adbd"; in RunPollLoop()
812 static uint32_t ReadUint32AndAdvance(/*in-out*/uint8_t** in) { in ReadUint32AndAdvance()
819 void AdbConnectionState::HandleDataWithoutAgent(art::Thread* self) { in HandleDataWithoutAgent()
823 CHECK(art::Dbg::IsJdwpAllowed()); in HandleDataWithoutAgent()
824 // We try to avoid loading the agent which is expensive. First lets just perform the handshake. in HandleDataWithoutAgent()
829 // Read the packet header to figure out if it is one we can handle. We only 'peek' into the stream in HandleDataWithoutAgent()
830 // to see if it's one we can handle. This doesn't change the state of the socket. in HandleDataWithoutAgent()
836 // We want to be very careful not to change the socket state until we know we succeeded. This will in HandleDataWithoutAgent()
837 // let us fall-back to just loading the agent and letting it deal with everything. in HandleDataWithoutAgent()
839 // Close the socket. We either hit EOF or an error. in HandleDataWithoutAgent()
861 uint32_t avail = -1; in HandleDataWithoutAgent()
864 PLOG(ERROR) << "Failed to determine amount of readable data in socket! Closing connection"; in HandleDataWithoutAgent()
874 // Actually read the data. in HandleDataWithoutAgent()
884 size_t data_size = full_len - kPacketHeaderLen; in HandleDataWithoutAgent()
886 // This is an error (the data isn't long enough) but to match historical behavior we need to in HandleDataWithoutAgent()
893 if (ddm_len > data_size - (2 * sizeof(uint32_t))) { in HandleDataWithoutAgent()
894 // This is an error (the data isn't long enough) but to match historical behavior we need to in HandleDataWithoutAgent()
904 if (!art::Dbg::DdmHandleChunk(self->GetJniEnv(), in HandleDataWithoutAgent()
906 art::ArrayRef<const jbyte>(reinterpret_cast<const jbyte*>(ddm_data), in HandleDataWithoutAgent()
916 art::ArrayRef<const uint8_t>(reply)); in HandleDataWithoutAgent()
921 // Check to make sure we are able to read the whole handshake. in PerformHandshake()
922 uint32_t avail = -1; in PerformHandshake()
926 PLOG(ERROR) << "Failed to determine amount of readable data for handshake!"; in PerformHandshake()
932 // Perform the handshake. in PerformHandshake()
947 // Send the handshake back. in PerformHandshake()
953 PLOG(ERROR) << "Failed to send jdwp-handshake response."; in PerformHandshake()
960 void AdbConnectionState::AttachJdwpAgent(art::Thread* self) { in AttachJdwpAgent()
961 art::Runtime* runtime = art::Runtime::Current(); in AttachJdwpAgent()
962 self->AssertNoPendingException(); in AttachJdwpAgent()
967 runtime->AttachAgent(/* env= */ nullptr, in AttachJdwpAgent()
970 if (self->IsExceptionPending()) { in AttachJdwpAgent()
972 art::ScopedObjectAccess soa(self); in AttachJdwpAgent()
973 self->GetException()->Dump(); in AttachJdwpAgent()
974 self->ClearException(); in AttachJdwpAgent()
986 // The adbconnection plugin requires that the jdwp agent be configured as a 'server' because that in ValidateJdwpOptions()
987 // is what adb expects and otherwise we will hit a deadlock as the poll loop thread stops waiting in ValidateJdwpOptions()
988 // for the fd's to be passed down. in ValidateJdwpOptions()
993 // We don't start the jdwp agent until threads are already running. It is far too late to suspend in ValidateJdwpOptions()
997 LOG(ERROR) << "Cannot use suspend=y with late-init jdwp."; in ValidateJdwpOptions()
1005 // On Android, an app will not have write access to the cwd (which is "/"). in FixLogfile()
1021 std::string packageName = art::Runtime::Current()->GetProcessPackageName(); in FixLogfile()
1033 const std::string& opts = art::Runtime::Current()->GetJdwpOptions(); in MakeAgentArg()
1039 // See the comment above for why we need to be server=y. Since the agent defaults to server=n in MakeAgentArg()
1043 // See the comment above for why we need to be suspend=n. Since the agent defaults to in MakeAgentArg()
1069 if (sleep_event_fd_ != -1) { in WakeupPollLoop()
1076 if (sleep_event_fd_ != -1) { in AcknowledgeWakeup()
1082 // The regular agent system will take care of unloading the agent (if needed). in StopDebuggerThreads()
1087 // The plugin initialization function.
1089 DCHECK(art::Runtime::Current()->GetJdwpProvider() == art::JdwpProvider::kAdbConnection); in ArtPlugin_Initialize()
1095 return ValidateJdwpOptions(art::Runtime::Current()->GetJdwpOptions()); in ArtPlugin_Initialize()
1099 // We don't actually have to do anything here. The debugger (if one was in ArtPlugin_Deinitialize()
1100 // attached) was shutdown by the move to the kDeath runtime phase and the in ArtPlugin_Deinitialize()