BG_AuxvReader.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "BG_AuxvReader.h"
00031 #include "auxvtypes.h"
00032 using namespace DebuggerInterface;
00033
00034 #include <cstring>
00035 #include <sstream>
00036 using namespace std;
00037
00038
00039 #define AUX(msg) (msg).dataArea.GET_AUX_VECTORS
00040 #define ACK(msg) (msg).dataArea.GET_AUX_VECTORS_ACK
00041
00042 BG_AuxvReader::BG_AuxvReader(int _pid)
00043 : buffer_size(BG_Debugger_AUX_VECS_BUFFER),
00044 pid(_pid),
00045 fetch_offset(0),
00046 read_offset(0),
00047 ack_msg(GET_AUX_VECTORS_ACK, pid, 0, 0, 0),
00048 error(NULL)
00049 {
00050
00051 ACK(ack_msg).auxVecBufferLength = 0;
00052 ACK(ack_msg).endOfVecData = false;
00053 elt.type = AT_IGNORE;
00054 }
00055
00056
00057 void BG_AuxvReader::check_buffer() {
00058 if (read_offset < ACK(ack_msg).auxVecBufferLength
00059 || ACK(ack_msg).endOfVecData) {
00060 return;
00061 }
00062
00063
00064 BG_Debugger_Msg get_aux_msg(GET_AUX_VECTORS, pid, 0, 0, 0);
00065 get_aux_msg.header.dataLength = sizeof(get_aux_msg.dataArea.GET_AUX_VECTORS);
00066 get_aux_msg.dataArea.GET_AUX_VECTORS.auxVecBufferOffset = fetch_offset;
00067 get_aux_msg.dataArea.GET_AUX_VECTORS.auxVecBufferLength = buffer_size;
00068
00069 if (!BG_Debugger_Msg::writeOnFd(BG_DEBUGGER_WRITE_PIPE, get_aux_msg)) {
00070 error = "Error writing GET_AUX_VECTORS to BG debug stream.";
00071 return;
00072 }
00073
00074
00075
00076 ack_msg = BG_Debugger_Msg(GET_AUX_VECTORS_ACK, pid, 0, 0, 0);
00077 ack_msg.header.dataLength = sizeof(ack_msg.dataArea.GET_AUX_VECTORS_ACK);
00078 if (!BG_Debugger_Msg::readFromFd(BG_DEBUGGER_READ_PIPE, ack_msg)) {
00079 error = "Error reading from BG debug stream.";
00080 return;
00081 }
00082
00083 if (ack_msg.header.messageType != GET_AUX_VECTORS_ACK) {
00084 ostringstream err;
00085 err << "Got invalid response from BG debug stream. Expected GET_AUX_VECTORS_ACK, but got ";
00086 err << BG_Debugger_Msg::getMessageName(ack_msg.header.messageType);
00087 err << ".";
00088 string errstr = err.str();
00089 error = strdup(errstr.c_str());
00090 return;
00091 }
00092
00093
00094 if (ACK(ack_msg).auxVecBufferLength > AUX(get_aux_msg).auxVecBufferLength ||
00095 ACK(ack_msg).auxVecBufferOffset != AUX(get_aux_msg).auxVecBufferOffset)
00096 {
00097 error = "Sanity check on GET_AUX_VECTORS_ACK failed.";
00098 return;
00099 }
00100
00101
00102 fetch_offset += buffer_size;
00103 read_offset = 0;
00104 }
00105
00106
00107 BG_AuxvReader::~BG_AuxvReader() { }
00108
00109
00110 bool BG_AuxvReader::good() {
00111 return !error;
00112 }
00113
00114
00115 const char *BG_AuxvReader::error_msg() {
00116 return error;
00117 }
00118
00119
00120 bool BG_AuxvReader::has_next() {
00121 check_buffer();
00122 if (error) return false;
00123
00124 return elt.type != AT_NULL
00125 && read_offset < ACK(ack_msg).auxVecBufferLength;
00126 }
00127
00128
00129 auxv_element BG_AuxvReader::next() {
00130
00131 check_buffer();
00132
00133 elt.type = ACK(ack_msg).auxVecData[read_offset++];
00134 elt.value = ACK(ack_msg).auxVecData[read_offset++];
00135
00136 return elt;
00137 }
00138