addrtranslate.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 
00031 #include "common/h/addrtranslate.h"
00032 
00033 #include <cstdio>
00034 
00035 using namespace Dyninst;
00036 using namespace std;
00037 
00038 AddressTranslate::AddressTranslate(PID pid_, PROC_HANDLE phand, std::string exename) :
00039    pid(pid_),
00040    phandle(phand),
00041    creation_error(false),
00042    exec_name(exename),
00043    exec(NULL),
00044    symfactory(NULL),
00045    read_abort(false)
00046 {
00047 }
00048 
00049 bool AddressTranslate::getLibAtAddress(Address addr, LoadedLib* &lib)
00050 {
00051 
00052    /**
00053     * This code sets lower_lib_index and higher_lib_index to point
00054     * to the indexes in libs[] that have dynamic section addresses
00055     * that are the closest to the target address.
00056     **/
00057    Address lower_dynamic_addr = 0, higher_dynamic_addr = 0;
00058    int lower_lib_index = -1, higher_lib_index = -1;
00059    for (unsigned i=0; i<libs.size(); i++)
00060    {
00061       LoadedLib *l = libs[i];
00062       if (!l) continue;
00063       Address daddr = l->getDynamicAddr();
00064       if (!daddr) continue;
00065       if (daddr < addr) {
00066          if (daddr > lower_dynamic_addr) {
00067             lower_dynamic_addr = daddr;
00068             lower_lib_index = i;
00069          }
00070       }
00071       else if (daddr > addr) {
00072          if (!higher_dynamic_addr || daddr < higher_dynamic_addr) {
00073             higher_dynamic_addr = daddr;
00074             higher_lib_index = i;
00075          }
00076       }
00077    }
00078    
00079    /**
00080     * Let's search this list in a not-stupid order.  We'll
00081     * check lower_lib_index and higher_lib_index first
00082     * (the -1 and -2 cases).  If neither of those look right,
00083     * then we'll just have to walk the entire list.
00084     **/
00085    for (int i=-2; i< (int) libs.size(); i++)
00086    {
00087       LoadedLib *l = NULL;
00088 
00089       if (i == -2) {
00090          if (lower_lib_index ==  -1)
00091             continue;
00092          l = libs[lower_lib_index];
00093       }
00094       else if (i == -1) {
00095          if (higher_lib_index ==  -1)
00096             continue;
00097          l = libs[higher_lib_index];
00098       }
00099       else if (i == lower_lib_index || i == higher_lib_index) {
00100          continue;
00101       }
00102       else {
00103          l = libs[i];
00104       }
00105       if (!l)
00106          continue;
00107 
00108       vector<pair<Address, unsigned long> > *addresses = l->getMappedRegions();
00109       if (!addresses)
00110          continue;
00111       for (unsigned j = 0; j<addresses->size(); j++) {
00112          if (addr >= (*addresses)[j].first && 
00113              addr < (*addresses)[j].first + (*addresses)[j].second)
00114          {
00115             lib = l;
00116             return true;
00117          }
00118       }
00119    }
00120    return false;
00121 }
00122 
00123 #include <iostream>
00124 bool AddressTranslate::getLibs(vector<LoadedLib *> &libs_)
00125 {
00126    libs_.clear();
00127    for (unsigned i=0; i<libs.size(); i++)
00128       libs_.push_back(libs[i]);
00129    return true;
00130 }
00131 
00132 PID AddressTranslate::getPid()
00133 {
00134    return pid;
00135 }
00136 
00137 LoadedLib *AddressTranslate::getLoadedLib(std::string name)
00138 {
00139    for (unsigned i=0; i<libs.size(); i++)
00140    {
00141       if (libs[i]->getName() == name)
00142       {
00143          return libs[i];
00144       }
00145    }
00146    return NULL;
00147 }
00148 
00149 void AddressTranslate::setReadAbort(bool b)
00150 {
00151    read_abort = b;
00152 }
00153 
00154 AddressTranslate::~AddressTranslate()
00155 {
00156    for (vector<LoadedLib *>::iterator i = libs.begin(); i != libs.end(); i++)
00157    {
00158       if (*i == exec)
00159          exec = NULL;
00160       delete *i;
00161    }
00162    if (exec) {
00163       delete exec;
00164       exec = NULL;
00165    }
00166 }
00167 
00168 LoadedLib *AddressTranslate::getExecutable()
00169 {
00170    return exec;
00171 }
00172 
00173 bool AddressTranslate::getArchLibs(std::vector<LoadedLib *> &)
00174 {
00175    return true;
00176 }
00177 
00178 string LoadedLib::getName() const {
00179    return name;
00180 }
00181 
00182 void LoadedLib::add_mapped_region(Address addr, unsigned long size)
00183 {
00184    mapped_regions.push_back(pair<Address, unsigned long>(addr, size));   
00185 }
00186 
00187 void LoadedLib::setShouldClean(bool b)
00188 {
00189    should_clean = b;
00190 }
00191 
00192 bool LoadedLib::shouldClean()
00193 {
00194    return should_clean;
00195 }
00196 
00197 LoadedLib::LoadedLib(string n, Address la) :
00198    name(n),
00199    load_addr(la),
00200    data_load_addr(0),
00201    dynamic_addr(0),
00202    should_clean(false),
00203    symreader(NULL),
00204    symreader_factory(NULL),
00205    up_ptr(NULL)
00206 {
00207 }
00208 
00209 LoadedLib::~LoadedLib()
00210 {
00211 }
00212 
00213 void LoadedLib::setDataLoadAddr(Address a)
00214 {
00215    data_load_addr = a;
00216 }
00217 
00218 Address LoadedLib::offToAddress(Offset off)
00219 {
00220    return off + getCodeLoadAddr();
00221 }
00222 
00223 Offset LoadedLib::addrToOffset(Address addr)
00224 {
00225    return addr - getCodeLoadAddr();
00226 }
00227 
00228 Address LoadedLib::getCodeLoadAddr() const
00229 {
00230    return load_addr;
00231 }
00232 
00233 Address LoadedLib::getDataLoadAddr() const
00234 {
00235    return data_load_addr;
00236 }
00237 
00238 Address LoadedLib::getDynamicAddr() const
00239 {
00240    return dynamic_addr;
00241 }
00242 
00243 void LoadedLib::setFactory(SymbolReaderFactory *factory)
00244 {
00245    symreader_factory = factory;
00246 }
00247 
00248 void LoadedLib::getOutputs(string &filename, Address &code, Address &data)
00249 {
00250    filename = name;
00251    code = load_addr;
00252    data = 0;
00253 }
00254 
00255 void* LoadedLib::getUpPtr()
00256 {
00257    return up_ptr;
00258 }
00259 
00260 void LoadedLib::setUpPtr(void *v)
00261 {
00262    up_ptr = v;
00263 }
00264 
00265 #include <stdarg.h>
00266 
00267 //#if !defined(os_linux) && !defined(os_solaris)
00268 //This definition is for all the non-System V systems
00269 Address AddressTranslate::getLibraryTrapAddrSysV()
00270 {
00271    return 0x0;
00272 }
00273 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1