BG_AuxvReader.C

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 // these defines are for readability 
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   // set this up to trigger an initial fetch of some data.
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   // get a single auxv element
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   // wait for the ack (and the buffer that goes with it)
00075   // make sure we got the right kind of ack, too.
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   // sanity check to make sure the bounds are ok in the response.
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   // set offsets so we can read what we just fetched.
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;//.length();
00112 }
00113   
00114 
00115 const char *BG_AuxvReader::error_msg() {
00116   return error;//.c_str();
00117 }
00118   
00119 
00120 bool BG_AuxvReader::has_next() {
00121   check_buffer();
00122   if (error/*.length()*/) 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   // check here too, just in case someone decides they *know* we have a next element.
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 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1