![]() |
|
|
00001 /*************************************************************************** 00002 * inspectorJ - java profiler * 00003 * Copyright (C) 2007 by James May 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 * This program is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00013 * GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU General Public License * 00016 * along with this program; if not, write to the * 00017 * Free Software Foundation, Inc., * 00018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00019 ***************************************************************************/ 00020 00027 #include "jdwppacket.h" 00028 #include <iostream> 00029 #include <QIODevice> 00030 #include <QDataStream> 00031 00032 using namespace inspectorj::jdwp; 00033 00034 // initialize the static data type sizes 00035 int JDWPPacket::FIELD_ID_SIZE = 0; 00036 int JDWPPacket::OBJECT_ID_SIZE = 0; 00037 int JDWPPacket::METHOD_ID_SIZE = 0; 00038 int JDWPPacket::REFERENCE_TYPE_ID_SIZE = 0; 00039 int JDWPPacket::FRAME_ID_SIZE = 0; 00040 00044 JDWPPacket::JDWPPacket() 00045 { 00046 } 00047 00051 JDWPPacket::~JDWPPacket() 00052 { 00053 delete data; 00054 data = 0; 00055 } 00056 00060 void JDWPPacket::setReadMode() 00061 { 00062 if (! data->device()->isReadable()) { 00063 data->device()->open(QIODevice::ReadOnly); 00064 } 00065 } 00066 00070 void JDWPPacket::setWriteMode() 00071 { 00072 if (! data->device()->isWritable()) { 00073 data->device()->open(QIODevice::WriteOnly); 00074 } 00075 } 00076 00083 void JDWPPacket::initIDSizes(JDWPPacket &packet) 00084 { 00085 packet >> JDWPPacket::FIELD_ID_SIZE; 00086 packet >> JDWPPacket::METHOD_ID_SIZE; 00087 packet >> JDWPPacket::OBJECT_ID_SIZE; 00088 packet >> JDWPPacket::REFERENCE_TYPE_ID_SIZE; 00089 packet >> JDWPPacket::FRAME_ID_SIZE; 00090 } 00091 00096 short JDWPPacket::getErrorCode() 00097 { 00098 return ERR_NONE; 00099 } 00100 00104 jint JDWPPacket::getLength() 00105 { 00106 return bytes.size() + JDWP_HEADER_LENGTH; 00107 } 00108 00113 char* JDWPPacket::getDataBytes() 00114 { 00115 return this->bytes.data(); 00116 } 00117 00118 00120 /* Write operators */ 00122 00127 JDWPPacket& JDWPPacket::operator<<(jbyte &b) 00128 { 00129 setWriteMode(); 00130 *data << (jbyte) b; 00131 return *this; 00132 } 00133 00138 JDWPPacket& JDWPPacket::operator<<(bool &b) 00139 { 00140 setWriteMode(); 00141 if (b) { 00142 *data << (jbyte) 1; 00143 } else { 00144 *data << (jbyte) 0; 00145 } 00146 return *this; 00147 } 00148 00153 JDWPPacket& JDWPPacket::operator<<(jint &i) 00154 { 00155 setWriteMode(); 00156 if (sizeof(jint) == sizeof(qint32)) { 00157 *data << (qint32)i; 00158 } else if (sizeof(jint) == sizeof(qint64)) { 00159 *data << (qint64)i; 00160 } 00161 return *this; 00162 } 00163 00164 #if defined (_WIN32) 00165 00169 JDWPPacket& JDWPPacket::operator<<(qint32 &i) 00170 { 00171 setWriteMode(); 00172 *data << i; 00173 return *this; 00174 } 00175 #endif 00176 00181 JDWPPacket& JDWPPacket::operator<<(qint64 &i) 00182 { 00183 setWriteMode(); 00184 *data << i; 00185 return *this; 00186 } 00187 00193 JDWPPacket& JDWPPacket::operator<<(QString &s) 00194 { 00195 setWriteMode(); 00196 00197 // add the string size 00198 *data << (jint) s.size(); 00199 00200 // add the string data (char*) 00201 data->writeRawData(s.toAscii().data(), s.size()); 00202 return *this; 00203 } 00204 00209 JDWPPacket& JDWPPacket::operator<<(ObjectID &oid) 00210 { 00211 if (sizeof(qint64) == OBJECT_ID_SIZE) { 00212 *this << oid.value; 00213 } 00214 else if (sizeof(qint32) == OBJECT_ID_SIZE) { 00215 *this << (qint32&)(oid.value); 00216 } 00217 00218 return *this; 00219 } 00220 00225 JDWPPacket& JDWPPacket::operator<<(FieldID &fid) 00226 { 00227 if (sizeof(qint64) == FIELD_ID_SIZE) { 00228 *this << fid.value; 00229 } 00230 else if (sizeof(qint32) == FIELD_ID_SIZE) { 00231 *this << (qint32&)(fid.value); 00232 } 00233 00234 return *this; 00235 } 00236 00237 00242 JDWPPacket& JDWPPacket::operator<<(MethodID &mid) 00243 { 00244 if (sizeof(qint64) == METHOD_ID_SIZE) { 00245 *this << mid.value; 00246 } 00247 else if (sizeof(qint32) == METHOD_ID_SIZE) { 00248 *this << (qint32&)(mid.value); 00249 } 00250 00251 return *this; 00252 } 00253 00258 JDWPPacket& JDWPPacket::operator<<(ReferenceTypeID &rid) 00259 { 00260 if (sizeof(qint64) == REFERENCE_TYPE_ID_SIZE) { 00261 *this << rid.value; 00262 } 00263 else if (sizeof(qint32) == REFERENCE_TYPE_ID_SIZE) { 00264 *this << (qint32&)(rid.value); 00265 } 00266 00267 return *this; 00268 } 00269 00274 JDWPPacket& JDWPPacket::operator<<(FrameID &fid) 00275 { 00276 if (sizeof(qint64) == FRAME_ID_SIZE) { 00277 *this << fid.value; 00278 } 00279 else if (sizeof(qint32) == FRAME_ID_SIZE) { 00280 *this << (qint32&)(fid.value); 00281 } 00282 00283 return *this; 00284 } 00285 00287 /* Read operators */ 00289 00294 JDWPPacket& JDWPPacket::operator>>(jbyte &b) 00295 { 00296 setReadMode(); 00297 *data >> b; 00298 return *this; 00299 } 00300 00306 JDWPPacket& JDWPPacket::operator>>(bool &b) 00307 { 00308 setReadMode(); 00309 jbyte jb; 00310 *data >> jb; 00311 00312 b = (jb != 0 ); 00313 00314 return *this; 00315 } 00316 00321 JDWPPacket& JDWPPacket::operator>>(qint32 &i) 00322 { 00323 setReadMode(); 00324 *data >> i; 00325 return *this; 00326 } 00327 00332 JDWPPacket& JDWPPacket::operator>>(qint64 &i) 00333 { 00334 setReadMode(); 00335 *data >> i; 00336 return *this; 00337 } 00338 00346 JDWPPacket& JDWPPacket::operator>>(QString &s) 00347 { 00348 setReadMode(); 00349 00350 // get the length of the data string 00351 int len; 00352 *data >> len; 00353 00354 // create a char array and read the data from the socket 00355 char *tmp = new char[len + 1]; 00356 data->readRawData(tmp, len); 00357 tmp[len] = 0; 00358 00359 // create a QString from the data string and assign it to s 00360 s = QString(tmp); 00361 delete[] tmp; 00362 tmp = NULL; 00363 00364 return *this; 00365 } 00366 00371 JDWPPacket& JDWPPacket::operator>>(ObjectID &oid) 00372 { 00373 if (sizeof(qint64) == OBJECT_ID_SIZE) { 00374 *this >> oid.value; 00375 } 00376 else if (sizeof(qint32) == OBJECT_ID_SIZE) { 00377 qint32 tmp; 00378 *this >> tmp; 00379 oid.value = qint64(tmp); 00380 } 00381 return *this; 00382 } 00383 00388 JDWPPacket& JDWPPacket::operator>>(FieldID &fid) 00389 { 00390 if (sizeof(qint64) == FIELD_ID_SIZE) { 00391 *this >> fid.value; 00392 } 00393 else if (sizeof(qint32) == FIELD_ID_SIZE) { 00394 qint32 tmp; 00395 *this >> tmp; 00396 fid.value = qint64(tmp); 00397 } 00398 return *this; 00399 } 00400 00405 JDWPPacket& JDWPPacket::operator>>(MethodID &mid) 00406 { 00407 if (sizeof(qint64) == METHOD_ID_SIZE) { 00408 *this >> mid.value; 00409 } 00410 else if (sizeof(qint32) == METHOD_ID_SIZE) { 00411 qint32 tmp; 00412 *this >> tmp; 00413 mid.value = qint64(tmp); 00414 } 00415 return *this; 00416 } 00417 00423 JDWPPacket& JDWPPacket::operator>>(ReferenceTypeID &rid) 00424 { 00425 if (sizeof(qint64) == REFERENCE_TYPE_ID_SIZE) { 00426 *this >> rid.value; 00427 } 00428 else if (sizeof(qint32) == REFERENCE_TYPE_ID_SIZE) { 00429 qint32 tmp; 00430 *this >> tmp; 00431 rid.value = qint64(tmp); 00432 } 00433 return *this; 00434 } 00435 00440 JDWPPacket& JDWPPacket::operator>>(FrameID &fid) 00441 { 00442 if (sizeof(qint64) == FRAME_ID_SIZE) { 00443 *this >> fid.value; 00444 } 00445 else if (sizeof(qint32) == FRAME_ID_SIZE) { 00446 qint32 tmp; 00447 *this >> tmp; 00448 fid.value = qint64(tmp); 00449 } 00450 return *this; 00451 }