blob: d58467d10867ab5b6c626c098bdd4687b6991544 [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070019#ifndef ART_RUNTIME_JDWP_JDWP_PRIV_H_
20#define ART_RUNTIME_JDWP_JDWP_PRIV_H_
Elliott Hughes872d4ec2011-10-21 17:07:15 -070021
Elliott Hughes872d4ec2011-10-21 17:07:15 -070022#include "debugger.h"
23#include "jdwp/jdwp.h"
24#include "jdwp/jdwp_event.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070025
26#include <pthread.h>
27#include <sys/uio.h>
28
29/*
30 * JDWP constants.
31 */
Sebastien Hertzcbc50642015-06-01 17:33:12 +020032static constexpr size_t kJDWPHeaderSizeOffset = 0U;
33static constexpr size_t kJDWPHeaderIdOffset = 4U;
34static constexpr size_t kJDWPHeaderFlagsOffset = 8U;
35static constexpr size_t kJDWPHeaderErrorCodeOffset = 9U;
36static constexpr size_t kJDWPHeaderCmdSetOffset = 9U;
37static constexpr size_t kJDWPHeaderCmdOffset = 10U;
38static constexpr size_t kJDWPHeaderLen = 11U;
39static constexpr uint8_t kJDWPFlagReply = 0x80;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070040
Sebastien Hertzcbc50642015-06-01 17:33:12 +020041static constexpr const char kMagicHandshake[] = "JDWP-Handshake";
42static constexpr size_t kMagicHandshakeLen = sizeof(kMagicHandshake) - 1;
43
44/* Invoke commands */
45static constexpr uint8_t kJDWPClassTypeCmdSet = 3U;
46static constexpr uint8_t kJDWPClassTypeInvokeMethodCmd = 3U;
47static constexpr uint8_t kJDWPClassTypeNewInstanceCmd = 4U;
48static constexpr uint8_t kJDWPObjectReferenceCmdSet = 9U;
49static constexpr uint8_t kJDWPObjectReferenceInvokeCmd = 6U;
50
51/* Event command */
52static constexpr uint8_t kJDWPEventCmdSet = 64U;
53static constexpr uint8_t kJDWPEventCompositeCmd = 100U;
Elliott Hughes74847412012-06-20 18:10:21 -070054
Elliott Hughes872d4ec2011-10-21 17:07:15 -070055/* DDM support */
Sebastien Hertzcbc50642015-06-01 17:33:12 +020056static constexpr uint8_t kJDWPDdmCmdSet = 199U; // 0xc7, or 'G'+128
57static constexpr uint8_t kJDWPDdmCmd = 1U;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070058
59namespace art {
60
61namespace JDWP {
62
Elliott Hughes872d4ec2011-10-21 17:07:15 -070063struct JdwpState;
64
Elliott Hughes5d10a872013-04-17 19:26:43 -070065bool InitSocketTransport(JdwpState*, const JdwpOptions*);
66bool InitAdbTransport(JdwpState*, const JdwpOptions*);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070067
Elliott Hughes872d4ec2011-10-21 17:07:15 -070068/*
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070069 * Base class for the adb and socket JdwpNetState implementations.
Elliott Hughes872d4ec2011-10-21 17:07:15 -070070 */
71class JdwpNetStateBase {
Elliott Hughesf8349362012-06-18 15:00:06 -070072 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070073 explicit JdwpNetStateBase(JdwpState*);
Elliott Hughes5d10a872013-04-17 19:26:43 -070074 virtual ~JdwpNetStateBase();
Elliott Hughes872d4ec2011-10-21 17:07:15 -070075
Elliott Hughes5d10a872013-04-17 19:26:43 -070076 virtual bool Accept() = 0;
77 virtual bool Establish(const JdwpOptions*) = 0;
78 virtual void Shutdown() = 0;
79 virtual bool ProcessIncoming() = 0;
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070080
Elliott Hughescb693062013-02-21 09:48:08 -080081 void ConsumeBytes(size_t byte_count);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070082
83 bool IsConnected();
84
85 bool IsAwaitingHandshake();
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070086
87 void Close();
88
Sebastien Hertz43c8d722014-03-18 12:19:52 +010089 ssize_t WritePacket(ExpandBuf* pReply, size_t length) LOCKS_EXCLUDED(socket_lock_);
90 ssize_t WriteBufferedPacket(const std::vector<iovec>& iov) LOCKS_EXCLUDED(socket_lock_);
Mathieu Chartierad466ad2015-01-08 16:28:08 -080091 Mutex* GetSocketLock() {
92 return &socket_lock_;
93 }
94 ssize_t WriteBufferedPacketLocked(const std::vector<iovec>& iov);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070095
Brian Carlstrom7934ac22013-07-26 10:54:15 -070096 int clientSock; // Active connection to debugger.
Elliott Hughes5d10a872013-04-17 19:26:43 -070097
Brian Carlstrom7934ac22013-07-26 10:54:15 -070098 int wake_pipe_[2]; // Used to break out of select.
Elliott Hughes5d10a872013-04-17 19:26:43 -070099
100 uint8_t input_buffer_[8192];
101 size_t input_count_;
102
103 protected:
104 bool HaveFullPacket();
105
106 bool MakePipe();
107 void WakePipe();
108
109 void SetAwaitingHandshake(bool new_state);
110
111 JdwpState* state_;
112
Elliott Hughesf8349362012-06-18 15:00:06 -0700113 private:
114 // Used to serialize writes to the socket.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700115 Mutex socket_lock_;
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700116
117 // Are we waiting for the JDWP handshake?
118 bool awaiting_handshake_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700119};
120
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121} // namespace JDWP
122
123} // namespace art
124
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700125#endif // ART_RUNTIME_JDWP_JDWP_PRIV_H_