Module.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 <string.h>
00032 
00033 #include "Annotatable.h"
00034 #include "Module.h"
00035 #include "Symtab.h"
00036 #include "Collections.h"
00037 #include "Function.h"
00038 #include "Variable.h"
00039 #include "LineInformation.h"
00040 #include "symutil.h"
00041 #include "annotations.h"
00042 
00043 #include "common/h/pathName.h"
00044 #include "common/h/serialize.h"
00045 
00046 using namespace Dyninst;
00047 using namespace Dyninst::SymtabAPI;
00048 using namespace std;
00049 
00050 static SymtabError serr;
00051 
00052 
00053 bool Module::findSymbol(std::vector<Symbol *> &found, 
00054                         const std::string& name,
00055                         Symbol::SymbolType sType, 
00056                         NameType nameType,
00057                         bool isRegex,
00058                         bool checkCase,
00059                         bool includeUndefined) {
00060     unsigned orig_size = found.size();
00061     std::vector<Symbol *> obj_syms;
00062     
00063     if (exec()->findSymbol(obj_syms, name, sType, nameType, isRegex, checkCase, includeUndefined)) {
00064         //fprintf(stderr, "%s[%d]:  no symbols matching %s found\n", FILE__, __LINE__, name.c_str());
00065         return false;
00066     }
00067     
00068     for (unsigned i = 0; i < obj_syms.size(); i++) {
00069         if (obj_syms[i]->getModule() == this)
00070             found.push_back(obj_syms[i]);
00071     }
00072     
00073     if (found.size() > orig_size) 
00074         return true;
00075     
00076     return false;        
00077 }
00078 
00079 bool Module::getAllSymbols(std::vector<Symbol *> &found) {
00080     unsigned orig_size = found.size();
00081     std::vector<Symbol *> obj_syms;
00082     
00083     if (!exec()->getAllSymbols(obj_syms)) {
00084         //fprintf(stderr, "%s[%d]:  no symbols matching %s found\n", FILE__, __LINE__, name.c_str());
00085         return false;
00086     }
00087     
00088     for (unsigned i = 0; i < obj_syms.size(); i++) {
00089         if (obj_syms[i]->getModule() == this)
00090             found.push_back(obj_syms[i]);
00091     }
00092     
00093     if (found.size() > orig_size) 
00094         return true;
00095     
00096     return false;        
00097 }
00098 
00099 const std::string &Module::fileName() const
00100 {
00101    return fileName_;
00102 }
00103 
00104 const std::string &Module::fullName() const
00105 {
00106    return fullName_;
00107 }
00108 
00109  Symtab *Module::exec() const
00110 {
00111    return exec_;
00112 }
00113 
00114 supportedLanguages Module::language() const
00115 {
00116    return language_;
00117 }
00118 
00119 bool Module::hasLineInformation()
00120 {
00121    LineInformation *li =  NULL;
00122    if (getAnnotation(li, ModuleLineInfoAnno)) 
00123    {
00124       if (!li) 
00125       {
00126          fprintf(stderr, "%s[%d]:  weird inconsistency with getAnnotation here\n", 
00127                FILE__, __LINE__);
00128          return false;
00129       }
00130 
00131       if (li->getSize())
00132       {
00133          return true;
00134       }
00135    }
00136 
00137    return false;
00138 }
00139 
00140 bool Module::getAddressRanges(std::vector<pair<Offset, Offset> >&ranges,
00141       std::string lineSource, unsigned int lineNo)
00142 {
00143    unsigned int originalSize = ranges.size();
00144 
00145    LineInformation *lineInformation = getLineInformation();
00146    if (lineInformation)
00147       lineInformation->getAddressRanges( lineSource.c_str(), lineNo, ranges );
00148 
00149    if ( ranges.size() != originalSize )
00150       return true;
00151 
00152    return false;
00153 }
00154 
00155 bool Module::getSourceLines(std::vector<Statement *> &lines, Offset addressInRange)
00156 {
00157    unsigned int originalSize = lines.size();
00158 
00159    LineInformation *lineInformation = getLineInformation();
00160    if (lineInformation)
00161       lineInformation->getSourceLines( addressInRange, lines );
00162 
00163    if ( lines.size() != originalSize )
00164       return true;
00165 
00166    return false;
00167 }
00168 
00169 bool Module::getSourceLines(std::vector<LineNoTuple> &lines, Offset addressInRange)
00170 {
00171    unsigned int originalSize = lines.size();
00172    
00173    LineInformation *lineInformation = getLineInformation();
00174    if (lineInformation)
00175       lineInformation->getSourceLines( addressInRange, lines );
00176 
00177    if ( lines.size() != originalSize )
00178       return true;
00179    
00180    return false;
00181 }
00182 
00183 bool Module::getStatements(std::vector<Statement *> &statements)
00184 {
00185     unsigned initial_size = statements.size();
00186     LineInformation *li = getLineInformation();
00187 
00188     if (!li) 
00189     {
00190         //fprintf(stderr, "%s[%d]:  WARNING:  no line info for module %s\n", FILE__, __LINE__, fileName_.c_str());
00191         //annotationsReport();
00192         return false;
00193     }
00194 
00195 #if 0
00196     statements.resize(initial_size + li->getSize());
00197 
00198     int index = initial_size; 
00199 
00200     for (LineInformation::const_iterator i = li->begin();
00201             i != li->end();
00202             ++i)
00203     { 
00204         //statements[index].start_addr = (*i).first.first;
00205         //statements[index].end_addr = (*i).first.second;
00206         statements[index] = const_cast<Statement *>(&(*i).second);
00207         index++;
00208     }
00209 #endif
00210     for (LineInformation::const_iterator i = li->begin();
00211             i != li->end();
00212             ++i)
00213     {
00214         statements.push_back(const_cast<Statement *>(&(i->second)));
00215     }
00216 
00217     return (statements.size() > initial_size);
00218 }
00219 
00220 vector<Type *> *Module::getAllTypes()
00221 {
00222     exec_->parseTypesNow();
00223 
00224     typeCollection *tc = NULL;
00225     if (!getAnnotation(tc, ModuleTypeInfoAnno))
00226     {
00227         return NULL;
00228     }
00229     if (!tc)
00230     {
00231         fprintf(stderr, "%s[%d]:  failed to getAnnotation here\n", FILE__, __LINE__);
00232         return NULL;
00233     }
00234 
00235     return tc->getAllTypes();
00236 }
00237 
00238 vector<pair<string, Type *> > *Module::getAllGlobalVars()
00239 {
00240     exec_->parseTypesNow();
00241 
00242     typeCollection *tc = NULL;
00243     if (!getAnnotation(tc, ModuleTypeInfoAnno))
00244    {
00245       return NULL;
00246    }
00247    if (!tc)
00248    {
00249       fprintf(stderr, "%s[%d]:  failed to addAnnotation here\n", FILE__, __LINE__);
00250       return NULL;
00251    }
00252 
00253    return tc->getAllGlobalVariables();
00254 }
00255 
00256 typeCollection *Module::getModuleTypes()
00257 {
00258     exec_->parseTypesNow();
00259     return getModuleTypesPrivate();
00260 }
00261 
00262 typeCollection *Module::getModuleTypesPrivate()
00263 {
00264    typeCollection *tc = NULL;
00265    if (!getAnnotation(tc, ModuleTypeInfoAnno))
00266    {
00267       return NULL;
00268    }
00269 
00270    return tc;
00271 }
00272 
00273 bool Module::findType(Type *&type, std::string name)
00274 {
00275     typeCollection *tc = getModuleTypes();
00276     if (!tc) return false;
00277 
00278    type = tc->findType(name);
00279 
00280    if (type == NULL)
00281       return false;
00282 
00283    return true;
00284 }
00285 
00286 bool Module::findVariableType(Type *&type, std::string name)
00287 {
00288     typeCollection *tc = getModuleTypes();
00289     if (!tc) return false;
00290 
00291     type = tc->findVariableType(name);
00292 
00293    if (type == NULL)
00294       return false;
00295 
00296    return true;
00297 }
00298 
00299 
00300 bool Module::setLineInfo(LineInformation *lineInfo)
00301 {
00302    LineInformation *li =  NULL;
00303 
00304    if (!getAnnotation(li, ModuleLineInfoAnno)) 
00305    {
00306       if (li) 
00307       {
00308          return false;
00309       }
00310 
00311       if (!addAnnotation(lineInfo, ModuleLineInfoAnno))
00312       {
00313          return false;
00314       }
00315 
00316       return true;
00317    }
00318 
00319    if (li != lineInfo)
00320    {
00321        fprintf(stderr, "%s[%d]:  REMOVED DELETE\n", FILE__, __LINE__);
00322      //delete li;
00323    }
00324    
00325    if (!addAnnotation(lineInfo, ModuleLineInfoAnno))
00326    {
00327      fprintf(stderr, "%s[%d]:  failed to add lineInfo annotation\n", FILE__, __LINE__);
00328      return false;
00329    }
00330 
00331    return false;
00332 }
00333 
00334 LineInformation *Module::getLineInformation()
00335 {
00336     if (!exec_->isLineInfoValid_)
00337     {
00338         //fprintf(stderr, "%s[%d]:  TRIGGERING FL INFO PARSE\n", FILE__, __LINE__);
00339         exec_->parseLineInformation();
00340     }
00341 
00342     if (!exec_->isLineInfoValid_) 
00343     {
00344         fprintf(stderr, "%s[%d]:  FIXME\n", FILE__, __LINE__);
00345         return NULL;
00346     }
00347 
00348     LineInformation *li =  NULL;
00349     if (getAnnotation(li, ModuleLineInfoAnno))
00350     {
00351         if (!li) 
00352         {
00353             fprintf(stderr, "%s[%d]:  weird inconsistency with getAnnotation here\n",
00354                     FILE__, __LINE__);
00355             return NULL;
00356         }
00357 
00358         if (!li->getSize())
00359         {
00360             fprintf(stderr, "%s[%d]:  EMPTY LINE INFO ANNO\n", FILE__, __LINE__);
00361             return NULL;
00362         }
00363     }
00364 
00365     return li;
00366 }
00367 
00368 bool Module::findLocalVariable(std::vector<localVar *>&vars, std::string name)
00369 {
00370     std::vector<Function *>mod_funcs;
00371 
00372     if (!exec_->getAllFunctions(mod_funcs))
00373     {
00374         return false;
00375     }
00376 
00377     unsigned origSize = vars.size();
00378 
00379     for (unsigned int i = 0; i < mod_funcs.size(); i++)
00380     {
00381         mod_funcs[i]->findLocalVariable(vars, name);
00382     }
00383 
00384     if (vars.size() > origSize)
00385         return true;
00386 
00387     return false;
00388 }
00389 
00390 Module::Module(supportedLanguages lang, Offset adr,
00391       std::string fullNm, Symtab *img) :
00392    fullName_(fullNm),
00393    language_(lang),
00394    addr_(adr),
00395    exec_(img)
00396 {
00397    fileName_ = extract_pathname_tail(fullNm);
00398 }
00399 
00400 Module::Module() :
00401    fileName_(""),
00402    fullName_(""),
00403    language_(lang_Unknown),
00404    addr_(0),
00405    exec_(NULL)
00406 {
00407 }
00408 
00409 Module::Module(const Module &mod) :
00410    LookupInterface(),
00411    Serializable(),
00412    MODULE_ANNOTATABLE_CLASS(),
00413    fileName_(mod.fileName_),
00414    fullName_(mod.fullName_),
00415    language_(mod.language_),
00416    addr_(mod.addr_),
00417    exec_(mod.exec_)
00418 {
00419    //  Copy annotations here or no?
00420 }
00421 
00422 Module::~Module()
00423 {
00424    LineInformation *li =  NULL;
00425    if (getAnnotation(li, ModuleLineInfoAnno)) 
00426    {
00427       if (li) 
00428       {
00429           if (!removeAnnotation(ModuleLineInfoAnno))
00430           {
00431               fprintf(stderr, "%s[%d]:  FIXME:  failed to remove LineInfo\n", 
00432                       FILE__, __LINE__);
00433           }
00434           else
00435           {
00436               fprintf(stderr, "%s[%d]:  removed delete for %p\n", FILE__, __LINE__, li);
00437               delete li;
00438           }
00439       }
00440    }
00441     typeCollection *tc = NULL;
00442     if (getAnnotation(tc, ModuleTypeInfoAnno))
00443     {
00444         if (tc)
00445         {
00446             if (!removeAnnotation(ModuleTypeInfoAnno))
00447             {
00448                 fprintf(stderr, "%s[%d]:  FIXME:  failed to remove LineInfo\n", 
00449                         FILE__, __LINE__);
00450             }
00451             else
00452                 delete tc;
00453         }
00454     }
00455 }
00456 
00457 bool Module::isShared() const
00458 {
00459    return exec_->getObjectType() == obj_SharedLib;
00460 }
00461 
00462 bool Module::getAllSymbolsByType(std::vector<Symbol *> &found, Symbol::SymbolType sType)
00463 {
00464    unsigned orig_size = found.size();
00465    std::vector<Symbol *> obj_syms;
00466 
00467    if (!exec()->getAllSymbolsByType(obj_syms, sType))
00468       return false;
00469 
00470    for (unsigned i = 0; i < obj_syms.size(); i++) 
00471    {
00472       if (obj_syms[i]->getModule() == this)
00473          found.push_back(obj_syms[i]);
00474    }
00475 
00476    if (found.size() > orig_size)
00477    {
00478       return true;
00479    }
00480 
00481    serr = No_Such_Symbol;
00482    return false;
00483 }
00484 
00485 bool Module::getAllFunctions(std::vector<Function *> &ret)
00486 {
00487     return exec()->getAllFunctions(ret);
00488 }
00489 
00490 bool Module::operator==(Module &mod) 
00491 {
00492    LineInformation *li =  NULL;
00493    LineInformation *li_src =  NULL;
00494    bool get_anno_res = false, get_anno_res_src = false;
00495    get_anno_res = getAnnotation(li, ModuleLineInfoAnno);
00496    get_anno_res_src = mod.getAnnotation(li_src, ModuleLineInfoAnno);
00497 
00498    if (get_anno_res != get_anno_res_src)
00499    {
00500       fprintf(stderr, "%s[%d]:  weird inconsistency with getAnnotation here\n", 
00501             FILE__, __LINE__);
00502       return false;
00503    }
00504 
00505    if (li) 
00506    {
00507       if (!li_src) 
00508       {
00509          fprintf(stderr, "%s[%d]:  weird inconsistency with getAnnotation here\n", 
00510                FILE__, __LINE__);
00511          return false;
00512       }
00513 
00514       if (li->getSize() != li_src->getSize()) 
00515          return false;
00516 
00517       if ((li != li_src)) 
00518          return false;
00519    }
00520    else
00521    {
00522       if (li_src) 
00523       {
00524          fprintf(stderr, "%s[%d]:  weird inconsistency with getAnnotation here\n", 
00525                FILE__, __LINE__);
00526          return false;
00527       }
00528    }
00529 
00530    if (exec_ && !mod.exec_) return false;
00531    if (!exec_ && mod.exec_) return false;
00532    if (exec_)
00533    {
00534        if (exec_->file() != mod.exec_->file()) return false;
00535        if (exec_->name() != mod.exec_->name()) return false;
00536    }
00537 
00538    return (
00539          (language_==mod.language_)
00540          && (addr_==mod.addr_)
00541          && (fullName_==mod.fullName_)
00542          && (fileName_==mod.fileName_)
00543          );
00544 }
00545 
00546 bool Module::setName(std::string newName)
00547 {
00548    fullName_ = newName;
00549    fileName_ = extract_pathname_tail(fullName_);
00550    return true;
00551 }
00552 
00553 void Module::setLanguage(supportedLanguages lang)
00554 {
00555    language_ = lang;
00556 }
00557 
00558 Offset Module::addr() const
00559 {
00560    return addr_;
00561 }
00562 
00563 bool Module::setDefaultNamespacePrefix(string str)
00564 {
00565     return exec_->setDefaultNamespacePrefix(str);
00566 }
00567 
00568 bool Module::findVariablesByName(std::vector<Variable *> &ret, const std::string& name,
00569                  NameType nameType,
00570                  bool isRegex,
00571                  bool checkCase) {
00572   bool succ = false;
00573   std::vector<Variable *> tmp;
00574 
00575   if (!exec()->findVariablesByName(tmp, name, nameType, isRegex, checkCase)) {
00576     return false;
00577   }
00578   for (unsigned i = 0; i < tmp.size(); i++) {
00579     if (tmp[i]->getModule() == this) {
00580       ret.push_back(tmp[i]);
00581       succ = true;
00582     }
00583   }
00584   return succ;
00585 }
00586 
00587 #if !defined(SERIALIZATION_DISABLED)
00588 Serializable * Module::serialize_impl(SerializerBase *sb, const char *tag) THROW_SPEC (SerializerError)
00589 {
00590    ifxml_start_element(sb, tag);
00591    gtranslate(sb, fileName_, "fileName");
00592    gtranslate(sb, fullName_, "fullName");
00593    gtranslate(sb, addr_, "Address");
00594    gtranslate(sb, language_, supportedLanguages2Str, "Language");
00595    ifxml_end_element(sb, tag);
00596 
00597    if (sb->isInput())
00598    {
00599        SerContextBase *scb = sb->getContext();
00600        if (!scb)
00601        {
00602            fprintf(stderr, "%s[%d]:  SERIOUS:  FIXME\n", FILE__, __LINE__);
00603            SER_ERR("FIXME");
00604        }
00605 
00606        SerContext<Symtab> *scs = dynamic_cast<SerContext<Symtab> *>(scb);
00607 
00608        if (!scs)
00609        {
00610            fprintf(stderr, "%s[%d]:  SERIOUS:  FIXME\n", FILE__, __LINE__);
00611            SER_ERR("FIXME");
00612        }
00613 
00614        Symtab *st = scs->getScope();
00615 
00616        if (!st)
00617        {
00618            fprintf(stderr, "%s[%d]:  FIXME\n", FILE__, __LINE__);
00619            SER_ERR("getScope");
00620        }
00621 
00622        exec_ = st;
00623    }
00624    return NULL;
00625 }
00626   
00627 Serializable *Statement::serialize_impl(SerializerBase *sb, const char *tag) THROW_SPEC(SerializerError)
00628 {
00629     ifxml_start_element(sb, tag);
00630     gtranslate(sb, file_, "file");
00631     gtranslate(sb, line_, "line");
00632     gtranslate(sb, column, "column");
00633     gtranslate(sb, start_addr_, "startAddress");
00634     gtranslate(sb, end_addr_, "endAddress");
00635     ifxml_end_element(sb, tag);
00636     return NULL;
00637 }
00638 
00639 #else
00640 
00641 Serializable *Module::serialize_impl(SerializerBase *, const char *) THROW_SPEC (SerializerError)
00642 {
00643    return NULL;
00644 }
00645 
00646 Serializable *Statement::serialize_impl(SerializerBase *, const char *) THROW_SPEC(SerializerError)
00647 {
00648    return NULL;
00649 }
00650 
00651 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1