14b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes/*
24b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * Copyright (C) 2013 The Android Open Source Project
34b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes *
44b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
54b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * you may not use this file except in compliance with the License.
64b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * You may obtain a copy of the License at
74b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes *
84b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
94b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes *
104b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * Unless required by applicable law or agreed to in writing, software
114b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
124b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * See the License for the specific language governing permissions and
144b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * limitations under the License.
154b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes */
164b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
174b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes#include "jdwp/jdwp.h"
184b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
19d9e4e0c8606f8b87e0a48aab68125dbee543bf88Ian Rogers#include <inttypes.h>
20d9e4e0c8606f8b87e0a48aab68125dbee543bf88Ian Rogers
214b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes#include "base/stringprintf.h"
22cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes#include "jdwp/jdwp_priv.h"
234b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
244b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesnamespace art {
254b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
264b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesnamespace JDWP {
274b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
28cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott HughesRequest::Request(const uint8_t* bytes, uint32_t available) : p_(bytes) {
29cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  byte_count_ = Read4BE();
30cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  end_ =  bytes + byte_count_;
31cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  CHECK_LE(byte_count_, available);
32cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes
33cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  id_ = Read4BE();
34cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  int8_t flags = Read1();
35cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  if ((flags & kJDWPFlagReply) != 0) {
36cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes    LOG(FATAL) << "reply?!";
37cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  }
38cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes
39cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  command_set_ = Read1();
40cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  command_ = Read1();
414b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
424b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
434b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesRequest::~Request() {
444b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
454b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
464b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesvoid Request::CheckConsumed() {
474b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (p_ < end_) {
484b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    CHECK(p_ == end_) << "read too few bytes: " << (end_ - p_);
494b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  } else if (p_ > end_) {
504b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    CHECK(p_ == end_) << "read too many bytes: " << (p_ - end_);
514b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  }
524b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
534b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
544b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesstd::string Request::ReadUtf8String() {
55cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  uint32_t length = Read4BE();
564b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  std::string s;
574b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  s.resize(length);
584b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  memcpy(&s[0], p_, length);
594b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  p_ += length;
604b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    string \"" << s << "\"";
614b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return s;
624b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
634b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
644b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes// Helper function: read a variable-width value from the input buffer.
654b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesuint64_t Request::ReadValue(size_t width) {
664b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint64_t value = -1;
674b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  switch (width) {
68cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes    case 1: value = Read1(); break;
694b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    case 2: value = Read2BE(); break;
70cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes    case 4: value = Read4BE(); break;
714b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    case 8: value = Read8BE(); break;
724b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    default: LOG(FATAL) << width; break;
734b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  }
744b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return value;
754b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
764b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
774b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesint32_t Request::ReadSigned32(const char* what) {
78cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  int32_t value = static_cast<int32_t>(Read4BE());
794b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    " << what << " " << value;
804b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return value;
814b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
824b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
834b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesuint32_t Request::ReadUnsigned32(const char* what) {
84cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  uint32_t value = Read4BE();
854b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    " << what << " " << value;
864b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return value;
874b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
884b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
894b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesFieldId Request::ReadFieldId() {
90cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  FieldId id = Read4BE();
914b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    field id " << DescribeField(id);
924b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return id;
934b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
944b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
954b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesMethodId Request::ReadMethodId() {
96cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  MethodId id = Read4BE();
974b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    method id " << DescribeMethod(id);
984b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return id;
994b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1004b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1014b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesObjectId Request::ReadObjectId(const char* specific_kind) {
1024b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  ObjectId id = Read8BE();
103d9e4e0c8606f8b87e0a48aab68125dbee543bf88Ian Rogers  VLOG(jdwp) << StringPrintf("    %s id %#" PRIx64, specific_kind, id);
1044b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return id;
1054b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1064b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1074b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesObjectId Request::ReadArrayId() {
1084b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadObjectId("array");
1094b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1104b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1114b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesObjectId Request::ReadObjectId() {
1124b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadObjectId("object");
1134b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1144b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1154b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesObjectId Request::ReadThreadId() {
1164b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadObjectId("thread");
1174b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1184b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1194b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesObjectId Request::ReadThreadGroupId() {
1204b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadObjectId("thread group");
1214b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1224b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1234b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesRefTypeId Request::ReadRefTypeId() {
1244b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  RefTypeId id = Read8BE();
1254b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    ref type id " << DescribeRefTypeId(id);
1264b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return id;
1274b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1284b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1294b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesFrameId Request::ReadFrameId() {
1304b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  FrameId id = Read8BE();
1314b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    frame id " << id;
1324b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return id;
1334b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1344b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1354b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesJdwpTag Request::ReadTag() {
1364b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadEnum1<JdwpTag>("tag");
1374b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1384b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1394b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesJdwpTypeTag Request::ReadTypeTag() {
1404b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadEnum1<JdwpTypeTag>("type tag");
1414b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1424b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1434b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesJdwpLocation Request::ReadLocation() {
1444b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  JdwpLocation location;
1457934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  memset(&location, 0, sizeof(location));  // Allows memcmp(3) later.
1464b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  location.type_tag = ReadTypeTag();
1474b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  location.class_id = ReadObjectId("class");
1484b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  location.method_id = ReadMethodId();
1494b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  location.dex_pc = Read8BE();
1504b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    location " << location;
1514b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return location;
1524b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1534b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1544b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesJdwpModKind Request::ReadModKind() {
1554b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadEnum1<JdwpModKind>("mod kind");
1564b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1574b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
158cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughesuint8_t Request::Read1() {
159cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  return *p_++;
160cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes}
161cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes
1624b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesuint16_t Request::Read2BE() {
1634b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint16_t result = p_[0] << 8 | p_[1];
1644b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  p_ += 2;
1654b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return result;
1664b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1674b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
168cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughesuint32_t Request::Read4BE() {
169cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  uint32_t result = p_[0] << 24;
170cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  result |= p_[1] << 16;
171cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  result |= p_[2] << 8;
172cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  result |= p_[3];
173cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  p_ += 4;
174cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  return result;
175cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes}
176cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes
1774b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesuint64_t Request::Read8BE() {
178cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  uint64_t high = Read4BE();
179cb69306eefc1e6e42b3eaec8b479a268222b01b6Elliott Hughes  uint64_t low = Read4BE();
1804b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return (high << 32) | low;
1814b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1824b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1834b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}  // namespace JDWP
1844b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1854b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}  // namespace art
186