blob: 440b51b6e36859bbaf9814f22849260cb50b1bf5 [file] [log] [blame]
Elliott Hughes4b9702c2013-02-20 18:13:24 -08001/*
2 * Copyright (C) 2013 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#include "jdwp/jdwp.h"
18
19#include "base/stringprintf.h"
Elliott Hughescb693062013-02-21 09:48:08 -080020#include "jdwp/jdwp_priv.h"
Elliott Hughes4b9702c2013-02-20 18:13:24 -080021
22namespace art {
23
24namespace JDWP {
25
Elliott Hughescb693062013-02-21 09:48:08 -080026Request::Request(const uint8_t* bytes, uint32_t available) : p_(bytes) {
27 byte_count_ = Read4BE();
28 end_ = bytes + byte_count_;
29 CHECK_LE(byte_count_, available);
30
31 id_ = Read4BE();
32 int8_t flags = Read1();
33 if ((flags & kJDWPFlagReply) != 0) {
34 LOG(FATAL) << "reply?!";
35 }
36
37 command_set_ = Read1();
38 command_ = Read1();
Elliott Hughes4b9702c2013-02-20 18:13:24 -080039}
40
41Request::~Request() {
42}
43
44void Request::CheckConsumed() {
45 if (p_ < end_) {
46 CHECK(p_ == end_) << "read too few bytes: " << (end_ - p_);
47 } else if (p_ > end_) {
48 CHECK(p_ == end_) << "read too many bytes: " << (p_ - end_);
49 }
50}
51
52std::string Request::ReadUtf8String() {
Elliott Hughescb693062013-02-21 09:48:08 -080053 uint32_t length = Read4BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -080054 std::string s;
55 s.resize(length);
56 memcpy(&s[0], p_, length);
57 p_ += length;
58 VLOG(jdwp) << " string \"" << s << "\"";
59 return s;
60}
61
62// Helper function: read a variable-width value from the input buffer.
63uint64_t Request::ReadValue(size_t width) {
64 uint64_t value = -1;
65 switch (width) {
Elliott Hughescb693062013-02-21 09:48:08 -080066 case 1: value = Read1(); break;
Elliott Hughes4b9702c2013-02-20 18:13:24 -080067 case 2: value = Read2BE(); break;
Elliott Hughescb693062013-02-21 09:48:08 -080068 case 4: value = Read4BE(); break;
Elliott Hughes4b9702c2013-02-20 18:13:24 -080069 case 8: value = Read8BE(); break;
70 default: LOG(FATAL) << width; break;
71 }
72 return value;
73}
74
75int32_t Request::ReadSigned32(const char* what) {
Elliott Hughescb693062013-02-21 09:48:08 -080076 int32_t value = static_cast<int32_t>(Read4BE());
Elliott Hughes4b9702c2013-02-20 18:13:24 -080077 VLOG(jdwp) << " " << what << " " << value;
78 return value;
79}
80
81uint32_t Request::ReadUnsigned32(const char* what) {
Elliott Hughescb693062013-02-21 09:48:08 -080082 uint32_t value = Read4BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -080083 VLOG(jdwp) << " " << what << " " << value;
84 return value;
85}
86
87FieldId Request::ReadFieldId() {
Elliott Hughescb693062013-02-21 09:48:08 -080088 FieldId id = Read4BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -080089 VLOG(jdwp) << " field id " << DescribeField(id);
90 return id;
91}
92
93MethodId Request::ReadMethodId() {
Elliott Hughescb693062013-02-21 09:48:08 -080094 MethodId id = Read4BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -080095 VLOG(jdwp) << " method id " << DescribeMethod(id);
96 return id;
97}
98
99ObjectId Request::ReadObjectId(const char* specific_kind) {
100 ObjectId id = Read8BE();
101 VLOG(jdwp) << StringPrintf(" %s id %#llx", specific_kind, id);
102 return id;
103}
104
105ObjectId Request::ReadArrayId() {
106 return ReadObjectId("array");
107}
108
109ObjectId Request::ReadObjectId() {
110 return ReadObjectId("object");
111}
112
113ObjectId Request::ReadThreadId() {
114 return ReadObjectId("thread");
115}
116
117ObjectId Request::ReadThreadGroupId() {
118 return ReadObjectId("thread group");
119}
120
121RefTypeId Request::ReadRefTypeId() {
122 RefTypeId id = Read8BE();
123 VLOG(jdwp) << " ref type id " << DescribeRefTypeId(id);
124 return id;
125}
126
127FrameId Request::ReadFrameId() {
128 FrameId id = Read8BE();
129 VLOG(jdwp) << " frame id " << id;
130 return id;
131}
132
133JdwpTag Request::ReadTag() {
134 return ReadEnum1<JdwpTag>("tag");
135}
136
137JdwpTypeTag Request::ReadTypeTag() {
138 return ReadEnum1<JdwpTypeTag>("type tag");
139}
140
141JdwpLocation Request::ReadLocation() {
142 JdwpLocation location;
143 memset(&location, 0, sizeof(location)); // Allows memcmp(3) later.
144 location.type_tag = ReadTypeTag();
145 location.class_id = ReadObjectId("class");
146 location.method_id = ReadMethodId();
147 location.dex_pc = Read8BE();
148 VLOG(jdwp) << " location " << location;
149 return location;
150}
151
152JdwpModKind Request::ReadModKind() {
153 return ReadEnum1<JdwpModKind>("mod kind");
154}
155
Elliott Hughescb693062013-02-21 09:48:08 -0800156uint8_t Request::Read1() {
157 return *p_++;
158}
159
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800160uint16_t Request::Read2BE() {
161 uint16_t result = p_[0] << 8 | p_[1];
162 p_ += 2;
163 return result;
164}
165
Elliott Hughescb693062013-02-21 09:48:08 -0800166uint32_t Request::Read4BE() {
167 uint32_t result = p_[0] << 24;
168 result |= p_[1] << 16;
169 result |= p_[2] << 8;
170 result |= p_[3];
171 p_ += 4;
172 return result;
173}
174
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800175uint64_t Request::Read8BE() {
Elliott Hughescb693062013-02-21 09:48:08 -0800176 uint64_t high = Read4BE();
177 uint64_t low = Read4BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800178 return (high << 32) | low;
179}
180
181} // namespace JDWP
182
183} // namespace art