jdwp_request.cc revision 4b9702c6912c6f8745a77f5b5af56e7fe196e7c2
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
194b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes#include "base/stringprintf.h"
204b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
214b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesnamespace art {
224b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
234b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesnamespace JDWP {
244b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
254b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesRequest::Request(const uint8_t* bytes, size_t byte_count) : p_(bytes), end_(bytes + byte_count) {
264b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
274b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
284b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesRequest::~Request() {
294b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
304b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
314b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesvoid Request::CheckConsumed() {
324b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (p_ < end_) {
334b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    CHECK(p_ == end_) << "read too few bytes: " << (end_ - p_);
344b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  } else if (p_ > end_) {
354b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    CHECK(p_ == end_) << "read too many bytes: " << (p_ - end_);
364b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  }
374b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
384b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
394b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesstd::string Request::ReadUtf8String() {
404b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t length = Read4BE(&p_);
414b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  std::string s;
424b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  s.resize(length);
434b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  memcpy(&s[0], p_, length);
444b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  p_ += length;
454b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    string \"" << s << "\"";
464b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return s;
474b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
484b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
494b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes// Helper function: read a variable-width value from the input buffer.
504b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesuint64_t Request::ReadValue(size_t width) {
514b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint64_t value = -1;
524b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  switch (width) {
534b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    case 1: value = Read1(&p_); break;
544b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    case 2: value = Read2BE(); break;
554b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    case 4: value = Read4BE(&p_); break;
564b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    case 8: value = Read8BE(); break;
574b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    default: LOG(FATAL) << width; break;
584b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  }
594b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return value;
604b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
614b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
624b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesint32_t Request::ReadSigned32(const char* what) {
634b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  int32_t value = static_cast<int32_t>(Read4BE(&p_));
644b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    " << what << " " << value;
654b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return value;
664b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
674b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
684b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesuint32_t Request::ReadUnsigned32(const char* what) {
694b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t value = Read4BE(&p_);
704b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    " << what << " " << value;
714b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return value;
724b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
734b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
744b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesFieldId Request::ReadFieldId() {
754b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  FieldId id = Read4BE(&p_);
764b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    field id " << DescribeField(id);
774b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return id;
784b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
794b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
804b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesMethodId Request::ReadMethodId() {
814b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  MethodId id = Read4BE(&p_);
824b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    method id " << DescribeMethod(id);
834b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return id;
844b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
854b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
864b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesObjectId Request::ReadObjectId(const char* specific_kind) {
874b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  ObjectId id = Read8BE();
884b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << StringPrintf("    %s id %#llx", specific_kind, id);
894b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return id;
904b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
914b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
924b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesObjectId Request::ReadArrayId() {
934b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadObjectId("array");
944b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
954b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
964b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesObjectId Request::ReadObjectId() {
974b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadObjectId("object");
984b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
994b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1004b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesObjectId Request::ReadThreadId() {
1014b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadObjectId("thread");
1024b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1034b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1044b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesObjectId Request::ReadThreadGroupId() {
1054b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadObjectId("thread group");
1064b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1074b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1084b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesRefTypeId Request::ReadRefTypeId() {
1094b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  RefTypeId id = Read8BE();
1104b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    ref type id " << DescribeRefTypeId(id);
1114b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return id;
1124b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1134b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1144b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesFrameId Request::ReadFrameId() {
1154b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  FrameId id = Read8BE();
1164b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    frame id " << id;
1174b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return id;
1184b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1194b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1204b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesJdwpTag Request::ReadTag() {
1214b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadEnum1<JdwpTag>("tag");
1224b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1234b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1244b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesJdwpTypeTag Request::ReadTypeTag() {
1254b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadEnum1<JdwpTypeTag>("type tag");
1264b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1274b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1284b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesJdwpLocation Request::ReadLocation() {
1294b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  JdwpLocation location;
1304b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  memset(&location, 0, sizeof(location)); // Allows memcmp(3) later.
1314b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  location.type_tag = ReadTypeTag();
1324b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  location.class_id = ReadObjectId("class");
1334b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  location.method_id = ReadMethodId();
1344b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  location.dex_pc = Read8BE();
1354b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << "    location " << location;
1364b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return location;
1374b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1384b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1394b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott HughesJdwpModKind Request::ReadModKind() {
1404b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return ReadEnum1<JdwpModKind>("mod kind");
1414b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1424b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1434b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesuint16_t Request::Read2BE() {
1444b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint16_t result = p_[0] << 8 | p_[1];
1454b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  p_ += 2;
1464b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return result;
1474b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1484b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1494b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesuint64_t Request::Read8BE() {
1504b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint64_t high = Read4BE(&p_);
1514b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint64_t low = Read4BE(&p_);
1524b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  return (high << 32) | low;
1534b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
1544b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1554b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}  // namespace JDWP
1564b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1574b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}  // namespace art
158