Elliott Hughes | 4b9702c | 2013-02-20 18:13:24 -0800 | [diff] [blame^] | 1 | /* |
| 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" |
| 20 | |
| 21 | namespace art { |
| 22 | |
| 23 | namespace JDWP { |
| 24 | |
| 25 | Request::Request(const uint8_t* bytes, size_t byte_count) : p_(bytes), end_(bytes + byte_count) { |
| 26 | } |
| 27 | |
| 28 | Request::~Request() { |
| 29 | } |
| 30 | |
| 31 | void Request::CheckConsumed() { |
| 32 | if (p_ < end_) { |
| 33 | CHECK(p_ == end_) << "read too few bytes: " << (end_ - p_); |
| 34 | } else if (p_ > end_) { |
| 35 | CHECK(p_ == end_) << "read too many bytes: " << (p_ - end_); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | std::string Request::ReadUtf8String() { |
| 40 | uint32_t length = Read4BE(&p_); |
| 41 | std::string s; |
| 42 | s.resize(length); |
| 43 | memcpy(&s[0], p_, length); |
| 44 | p_ += length; |
| 45 | VLOG(jdwp) << " string \"" << s << "\""; |
| 46 | return s; |
| 47 | } |
| 48 | |
| 49 | // Helper function: read a variable-width value from the input buffer. |
| 50 | uint64_t Request::ReadValue(size_t width) { |
| 51 | uint64_t value = -1; |
| 52 | switch (width) { |
| 53 | case 1: value = Read1(&p_); break; |
| 54 | case 2: value = Read2BE(); break; |
| 55 | case 4: value = Read4BE(&p_); break; |
| 56 | case 8: value = Read8BE(); break; |
| 57 | default: LOG(FATAL) << width; break; |
| 58 | } |
| 59 | return value; |
| 60 | } |
| 61 | |
| 62 | int32_t Request::ReadSigned32(const char* what) { |
| 63 | int32_t value = static_cast<int32_t>(Read4BE(&p_)); |
| 64 | VLOG(jdwp) << " " << what << " " << value; |
| 65 | return value; |
| 66 | } |
| 67 | |
| 68 | uint32_t Request::ReadUnsigned32(const char* what) { |
| 69 | uint32_t value = Read4BE(&p_); |
| 70 | VLOG(jdwp) << " " << what << " " << value; |
| 71 | return value; |
| 72 | } |
| 73 | |
| 74 | FieldId Request::ReadFieldId() { |
| 75 | FieldId id = Read4BE(&p_); |
| 76 | VLOG(jdwp) << " field id " << DescribeField(id); |
| 77 | return id; |
| 78 | } |
| 79 | |
| 80 | MethodId Request::ReadMethodId() { |
| 81 | MethodId id = Read4BE(&p_); |
| 82 | VLOG(jdwp) << " method id " << DescribeMethod(id); |
| 83 | return id; |
| 84 | } |
| 85 | |
| 86 | ObjectId Request::ReadObjectId(const char* specific_kind) { |
| 87 | ObjectId id = Read8BE(); |
| 88 | VLOG(jdwp) << StringPrintf(" %s id %#llx", specific_kind, id); |
| 89 | return id; |
| 90 | } |
| 91 | |
| 92 | ObjectId Request::ReadArrayId() { |
| 93 | return ReadObjectId("array"); |
| 94 | } |
| 95 | |
| 96 | ObjectId Request::ReadObjectId() { |
| 97 | return ReadObjectId("object"); |
| 98 | } |
| 99 | |
| 100 | ObjectId Request::ReadThreadId() { |
| 101 | return ReadObjectId("thread"); |
| 102 | } |
| 103 | |
| 104 | ObjectId Request::ReadThreadGroupId() { |
| 105 | return ReadObjectId("thread group"); |
| 106 | } |
| 107 | |
| 108 | RefTypeId Request::ReadRefTypeId() { |
| 109 | RefTypeId id = Read8BE(); |
| 110 | VLOG(jdwp) << " ref type id " << DescribeRefTypeId(id); |
| 111 | return id; |
| 112 | } |
| 113 | |
| 114 | FrameId Request::ReadFrameId() { |
| 115 | FrameId id = Read8BE(); |
| 116 | VLOG(jdwp) << " frame id " << id; |
| 117 | return id; |
| 118 | } |
| 119 | |
| 120 | JdwpTag Request::ReadTag() { |
| 121 | return ReadEnum1<JdwpTag>("tag"); |
| 122 | } |
| 123 | |
| 124 | JdwpTypeTag Request::ReadTypeTag() { |
| 125 | return ReadEnum1<JdwpTypeTag>("type tag"); |
| 126 | } |
| 127 | |
| 128 | JdwpLocation Request::ReadLocation() { |
| 129 | JdwpLocation location; |
| 130 | memset(&location, 0, sizeof(location)); // Allows memcmp(3) later. |
| 131 | location.type_tag = ReadTypeTag(); |
| 132 | location.class_id = ReadObjectId("class"); |
| 133 | location.method_id = ReadMethodId(); |
| 134 | location.dex_pc = Read8BE(); |
| 135 | VLOG(jdwp) << " location " << location; |
| 136 | return location; |
| 137 | } |
| 138 | |
| 139 | JdwpModKind Request::ReadModKind() { |
| 140 | return ReadEnum1<JdwpModKind>("mod kind"); |
| 141 | } |
| 142 | |
| 143 | uint16_t Request::Read2BE() { |
| 144 | uint16_t result = p_[0] << 8 | p_[1]; |
| 145 | p_ += 2; |
| 146 | return result; |
| 147 | } |
| 148 | |
| 149 | uint64_t Request::Read8BE() { |
| 150 | uint64_t high = Read4BE(&p_); |
| 151 | uint64_t low = Read4BE(&p_); |
| 152 | return (high << 32) | low; |
| 153 | } |
| 154 | |
| 155 | } // namespace JDWP |
| 156 | |
| 157 | } // namespace art |