blob: a77962e2fa062a404471dead5be112775a875834 [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
Ian Rogersd9e4e0c2014-01-23 20:11:40 -080019#include <inttypes.h>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Elliott Hughescb693062013-02-21 09:48:08 -080023#include "jdwp/jdwp_priv.h"
Elliott Hughes4b9702c2013-02-20 18:13:24 -080024
25namespace art {
26
27namespace JDWP {
28
Elliott Hughescb693062013-02-21 09:48:08 -080029Request::Request(const uint8_t* bytes, uint32_t available) : p_(bytes) {
30 byte_count_ = Read4BE();
31 end_ = bytes + byte_count_;
32 CHECK_LE(byte_count_, available);
33
34 id_ = Read4BE();
35 int8_t flags = Read1();
36 if ((flags & kJDWPFlagReply) != 0) {
37 LOG(FATAL) << "reply?!";
38 }
39
40 command_set_ = Read1();
41 command_ = Read1();
Elliott Hughes4b9702c2013-02-20 18:13:24 -080042}
43
44Request::~Request() {
45}
46
47void Request::CheckConsumed() {
48 if (p_ < end_) {
49 CHECK(p_ == end_) << "read too few bytes: " << (end_ - p_);
50 } else if (p_ > end_) {
51 CHECK(p_ == end_) << "read too many bytes: " << (p_ - end_);
52 }
53}
54
55std::string Request::ReadUtf8String() {
Elliott Hughescb693062013-02-21 09:48:08 -080056 uint32_t length = Read4BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -080057 std::string s;
58 s.resize(length);
59 memcpy(&s[0], p_, length);
60 p_ += length;
61 VLOG(jdwp) << " string \"" << s << "\"";
62 return s;
63}
64
65// Helper function: read a variable-width value from the input buffer.
66uint64_t Request::ReadValue(size_t width) {
67 uint64_t value = -1;
68 switch (width) {
Elliott Hughescb693062013-02-21 09:48:08 -080069 case 1: value = Read1(); break;
Elliott Hughes4b9702c2013-02-20 18:13:24 -080070 case 2: value = Read2BE(); break;
Elliott Hughescb693062013-02-21 09:48:08 -080071 case 4: value = Read4BE(); break;
Elliott Hughes4b9702c2013-02-20 18:13:24 -080072 case 8: value = Read8BE(); break;
Elliott Hughesc1896c92018-11-29 11:33:18 -080073 default: LOG(FATAL) << width;
Elliott Hughes4b9702c2013-02-20 18:13:24 -080074 }
75 return value;
76}
77
78int32_t Request::ReadSigned32(const char* what) {
Elliott Hughescb693062013-02-21 09:48:08 -080079 int32_t value = static_cast<int32_t>(Read4BE());
Elliott Hughes4b9702c2013-02-20 18:13:24 -080080 VLOG(jdwp) << " " << what << " " << value;
81 return value;
82}
83
84uint32_t Request::ReadUnsigned32(const char* what) {
Elliott Hughescb693062013-02-21 09:48:08 -080085 uint32_t value = Read4BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -080086 VLOG(jdwp) << " " << what << " " << value;
87 return value;
88}
89
90FieldId Request::ReadFieldId() {
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070091 FieldId id = Read8BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -080092 VLOG(jdwp) << " field id " << DescribeField(id);
93 return id;
94}
95
96MethodId Request::ReadMethodId() {
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -070097 MethodId id = Read8BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -080098 VLOG(jdwp) << " method id " << DescribeMethod(id);
99 return id;
100}
101
102ObjectId Request::ReadObjectId(const char* specific_kind) {
103 ObjectId id = Read8BE();
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800104 VLOG(jdwp) << android::base::StringPrintf(" %s id %#" PRIx64, specific_kind, id);
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800105 return id;
106}
107
108ObjectId Request::ReadArrayId() {
109 return ReadObjectId("array");
110}
111
112ObjectId Request::ReadObjectId() {
113 return ReadObjectId("object");
114}
115
116ObjectId Request::ReadThreadId() {
117 return ReadObjectId("thread");
118}
119
120ObjectId Request::ReadThreadGroupId() {
121 return ReadObjectId("thread group");
122}
123
124RefTypeId Request::ReadRefTypeId() {
125 RefTypeId id = Read8BE();
126 VLOG(jdwp) << " ref type id " << DescribeRefTypeId(id);
127 return id;
128}
129
130FrameId Request::ReadFrameId() {
131 FrameId id = Read8BE();
132 VLOG(jdwp) << " frame id " << id;
133 return id;
134}
135
136JdwpTag Request::ReadTag() {
137 return ReadEnum1<JdwpTag>("tag");
138}
139
140JdwpTypeTag Request::ReadTypeTag() {
141 return ReadEnum1<JdwpTypeTag>("type tag");
142}
143
144JdwpLocation Request::ReadLocation() {
145 JdwpLocation location;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700146 memset(&location, 0, sizeof(location)); // Allows memcmp(3) later.
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800147 location.type_tag = ReadTypeTag();
148 location.class_id = ReadObjectId("class");
149 location.method_id = ReadMethodId();
150 location.dex_pc = Read8BE();
151 VLOG(jdwp) << " location " << location;
152 return location;
153}
154
155JdwpModKind Request::ReadModKind() {
156 return ReadEnum1<JdwpModKind>("mod kind");
157}
158
Elliott Hughescb693062013-02-21 09:48:08 -0800159uint8_t Request::Read1() {
160 return *p_++;
161}
162
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800163uint16_t Request::Read2BE() {
164 uint16_t result = p_[0] << 8 | p_[1];
165 p_ += 2;
166 return result;
167}
168
Elliott Hughescb693062013-02-21 09:48:08 -0800169uint32_t Request::Read4BE() {
170 uint32_t result = p_[0] << 24;
171 result |= p_[1] << 16;
172 result |= p_[2] << 8;
173 result |= p_[3];
174 p_ += 4;
175 return result;
176}
177
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800178uint64_t Request::Read8BE() {
Elliott Hughescb693062013-02-21 09:48:08 -0800179 uint64_t high = Read4BE();
180 uint64_t low = Read4BE();
Elliott Hughes4b9702c2013-02-20 18:13:24 -0800181 return (high << 32) | low;
182}
183
184} // namespace JDWP
185
186} // namespace art