blob: 4e1bda899fcb153d2e45e307ffcf9fb9054bbce7 [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;
Alex Light70fa1a52016-02-24 16:35:59 -080048static constexpr uint8_t kJDWPInterfaceTypeCmdSet = 5U;
49static constexpr uint8_t kJDWPInterfaceTypeInvokeMethodCmd = 1U;
Sebastien Hertzcbc50642015-06-01 17:33:12 +020050static constexpr uint8_t kJDWPObjectReferenceCmdSet = 9U;
51static constexpr uint8_t kJDWPObjectReferenceInvokeCmd = 6U;
52
53/* Event command */
54static constexpr uint8_t kJDWPEventCmdSet = 64U;
55static constexpr uint8_t kJDWPEventCompositeCmd = 100U;
Elliott Hughes74847412012-06-20 18:10:21 -070056
Elliott Hughes872d4ec2011-10-21 17:07:15 -070057/* DDM support */
Sebastien Hertzcbc50642015-06-01 17:33:12 +020058static constexpr uint8_t kJDWPDdmCmdSet = 199U; // 0xc7, or 'G'+128
59static constexpr uint8_t kJDWPDdmCmd = 1U;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070060
61namespace art {
62
63namespace JDWP {
64
Elliott Hughes872d4ec2011-10-21 17:07:15 -070065struct JdwpState;
66
Elliott Hughes5d10a872013-04-17 19:26:43 -070067bool InitSocketTransport(JdwpState*, const JdwpOptions*);
68bool InitAdbTransport(JdwpState*, const JdwpOptions*);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070069
Elliott Hughes872d4ec2011-10-21 17:07:15 -070070/*
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070071 * Base class for the adb and socket JdwpNetState implementations.
Elliott Hughes872d4ec2011-10-21 17:07:15 -070072 */
73class JdwpNetStateBase {
Elliott Hughesf8349362012-06-18 15:00:06 -070074 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070075 explicit JdwpNetStateBase(JdwpState*);
Elliott Hughes5d10a872013-04-17 19:26:43 -070076 virtual ~JdwpNetStateBase();
Elliott Hughes872d4ec2011-10-21 17:07:15 -070077
Elliott Hughes5d10a872013-04-17 19:26:43 -070078 virtual bool Accept() = 0;
79 virtual bool Establish(const JdwpOptions*) = 0;
80 virtual void Shutdown() = 0;
81 virtual bool ProcessIncoming() = 0;
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070082
Elliott Hughescb693062013-02-21 09:48:08 -080083 void ConsumeBytes(size_t byte_count);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070084
85 bool IsConnected();
86
87 bool IsAwaitingHandshake();
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070088
89 void Close();
90
Mathieu Chartier90443472015-07-16 20:32:27 -070091 ssize_t WritePacket(ExpandBuf* pReply, size_t length) REQUIRES(!socket_lock_);
92 ssize_t WriteBufferedPacket(const std::vector<iovec>& iov) REQUIRES(!socket_lock_);
Mathieu Chartierad466ad2015-01-08 16:28:08 -080093 Mutex* GetSocketLock() {
94 return &socket_lock_;
95 }
96 ssize_t WriteBufferedPacketLocked(const std::vector<iovec>& iov);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070097
Brian Carlstrom7934ac22013-07-26 10:54:15 -070098 int clientSock; // Active connection to debugger.
Elliott Hughes5d10a872013-04-17 19:26:43 -070099
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700100 int wake_pipe_[2]; // Used to break out of select.
Elliott Hughes5d10a872013-04-17 19:26:43 -0700101
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 Hughesf8349362012-06-18 15:00:06 -0700115 private:
116 // Used to serialize writes to the socket.
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700117 Mutex socket_lock_;
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700118
119 // Are we waiting for the JDWP handshake?
120 bool awaiting_handshake_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121};
122
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700123} // namespace JDWP
124
125} // namespace art
126
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700127#endif // ART_RUNTIME_JDWP_JDWP_PRIV_H_