Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 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. |
| 15 | */ |
| 16 | /* |
| 17 | * JDWP internal interfaces. |
| 18 | */ |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 19 | #ifndef ART_RUNTIME_JDWP_JDWP_PRIV_H_ |
| 20 | #define ART_RUNTIME_JDWP_JDWP_PRIV_H_ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 21 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 22 | #include "debugger.h" |
| 23 | #include "jdwp/jdwp.h" |
| 24 | #include "jdwp/jdwp_event.h" |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 25 | |
| 26 | #include <pthread.h> |
| 27 | #include <sys/uio.h> |
| 28 | |
| 29 | /* |
| 30 | * JDWP constants. |
| 31 | */ |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 32 | static constexpr size_t kJDWPHeaderSizeOffset = 0U; |
| 33 | static constexpr size_t kJDWPHeaderIdOffset = 4U; |
| 34 | static constexpr size_t kJDWPHeaderFlagsOffset = 8U; |
| 35 | static constexpr size_t kJDWPHeaderErrorCodeOffset = 9U; |
| 36 | static constexpr size_t kJDWPHeaderCmdSetOffset = 9U; |
| 37 | static constexpr size_t kJDWPHeaderCmdOffset = 10U; |
| 38 | static constexpr size_t kJDWPHeaderLen = 11U; |
| 39 | static constexpr uint8_t kJDWPFlagReply = 0x80; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 40 | |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 41 | static constexpr const char kMagicHandshake[] = "JDWP-Handshake"; |
| 42 | static constexpr size_t kMagicHandshakeLen = sizeof(kMagicHandshake) - 1; |
| 43 | |
| 44 | /* Invoke commands */ |
| 45 | static constexpr uint8_t kJDWPClassTypeCmdSet = 3U; |
| 46 | static constexpr uint8_t kJDWPClassTypeInvokeMethodCmd = 3U; |
| 47 | static constexpr uint8_t kJDWPClassTypeNewInstanceCmd = 4U; |
Alex Light | 70fa1a5 | 2016-02-24 16:35:59 -0800 | [diff] [blame] | 48 | static constexpr uint8_t kJDWPInterfaceTypeCmdSet = 5U; |
| 49 | static constexpr uint8_t kJDWPInterfaceTypeInvokeMethodCmd = 1U; |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 50 | static constexpr uint8_t kJDWPObjectReferenceCmdSet = 9U; |
| 51 | static constexpr uint8_t kJDWPObjectReferenceInvokeCmd = 6U; |
| 52 | |
| 53 | /* Event command */ |
| 54 | static constexpr uint8_t kJDWPEventCmdSet = 64U; |
| 55 | static constexpr uint8_t kJDWPEventCompositeCmd = 100U; |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 56 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 57 | /* DDM support */ |
Sebastien Hertz | cbc5064 | 2015-06-01 17:33:12 +0200 | [diff] [blame] | 58 | static constexpr uint8_t kJDWPDdmCmdSet = 199U; // 0xc7, or 'G'+128 |
| 59 | static constexpr uint8_t kJDWPDdmCmd = 1U; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 60 | |
| 61 | namespace art { |
| 62 | |
| 63 | namespace JDWP { |
| 64 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 65 | struct JdwpState; |
| 66 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 67 | bool InitSocketTransport(JdwpState*, const JdwpOptions*); |
| 68 | bool InitAdbTransport(JdwpState*, const JdwpOptions*); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 69 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 70 | /* |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 71 | * Base class for the adb and socket JdwpNetState implementations. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 72 | */ |
| 73 | class JdwpNetStateBase { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 74 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 75 | explicit JdwpNetStateBase(JdwpState*); |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 76 | virtual ~JdwpNetStateBase(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 77 | |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 78 | virtual bool Accept() = 0; |
| 79 | virtual bool Establish(const JdwpOptions*) = 0; |
| 80 | virtual void Shutdown() = 0; |
| 81 | virtual bool ProcessIncoming() = 0; |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 82 | |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 83 | void ConsumeBytes(size_t byte_count); |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 84 | |
| 85 | bool IsConnected(); |
| 86 | |
| 87 | bool IsAwaitingHandshake(); |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 88 | |
| 89 | void Close(); |
| 90 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 91 | ssize_t WritePacket(ExpandBuf* pReply, size_t length) REQUIRES(!socket_lock_); |
| 92 | ssize_t WriteBufferedPacket(const std::vector<iovec>& iov) REQUIRES(!socket_lock_); |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 93 | Mutex* GetSocketLock() { |
| 94 | return &socket_lock_; |
| 95 | } |
| 96 | ssize_t WriteBufferedPacketLocked(const std::vector<iovec>& iov); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 97 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 98 | int clientSock; // Active connection to debugger. |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 99 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 100 | int wake_pipe_[2]; // Used to break out of select. |
Elliott Hughes | 5d10a87 | 2013-04-17 19:26:43 -0700 | [diff] [blame] | 101 | |
| 102 | uint8_t input_buffer_[8192]; |
| 103 | size_t input_count_; |
| 104 | |
| 105 | protected: |
| 106 | bool HaveFullPacket(); |
| 107 | |
| 108 | bool MakePipe(); |
| 109 | void WakePipe(); |
| 110 | |
| 111 | void SetAwaitingHandshake(bool new_state); |
| 112 | |
| 113 | JdwpState* state_; |
| 114 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 115 | private: |
| 116 | // Used to serialize writes to the socket. |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 117 | Mutex socket_lock_; |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame] | 118 | |
| 119 | // Are we waiting for the JDWP handshake? |
| 120 | bool awaiting_handshake_; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 123 | } // namespace JDWP |
| 124 | |
| 125 | } // namespace art |
| 126 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 127 | #endif // ART_RUNTIME_JDWP_JDWP_PRIV_H_ |